builtin-report.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /*
  2. * builtin-report.c
  3. *
  4. * Builtin report command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/color.h"
  11. #include "util/list.h"
  12. #include "util/cache.h"
  13. #include "util/rbtree.h"
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "util/callchain.h"
  17. #include "util/strlist.h"
  18. #include "perf.h"
  19. #include "util/header.h"
  20. #include "util/parse-options.h"
  21. #include "util/parse-events.h"
  22. #define SHOW_KERNEL 1
  23. #define SHOW_USER 2
  24. #define SHOW_HV 4
  25. static char const *input_name = "perf.data";
  26. static char *vmlinux = NULL;
  27. static char default_sort_order[] = "comm,dso";
  28. static char *sort_order = default_sort_order;
  29. static char *dso_list_str;
  30. static struct strlist *dso_list;
  31. static int input;
  32. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  33. static int dump_trace = 0;
  34. #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
  35. #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
  36. static int verbose;
  37. #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
  38. static int full_paths;
  39. static unsigned long page_size;
  40. static unsigned long mmap_window = 32;
  41. static char default_parent_pattern[] = "^sys_|^do_page_fault";
  42. static char *parent_pattern = default_parent_pattern;
  43. static regex_t parent_regex;
  44. static int exclude_other = 1;
  45. static int callchain;
  46. static u64 sample_type;
  47. struct ip_event {
  48. struct perf_event_header header;
  49. u64 ip;
  50. u32 pid, tid;
  51. unsigned char __more_data[];
  52. };
  53. struct mmap_event {
  54. struct perf_event_header header;
  55. u32 pid, tid;
  56. u64 start;
  57. u64 len;
  58. u64 pgoff;
  59. char filename[PATH_MAX];
  60. };
  61. struct comm_event {
  62. struct perf_event_header header;
  63. u32 pid, tid;
  64. char comm[16];
  65. };
  66. struct fork_event {
  67. struct perf_event_header header;
  68. u32 pid, ppid;
  69. };
  70. struct period_event {
  71. struct perf_event_header header;
  72. u64 time;
  73. u64 id;
  74. u64 sample_period;
  75. };
  76. struct lost_event {
  77. struct perf_event_header header;
  78. u64 id;
  79. u64 lost;
  80. };
  81. struct read_event {
  82. struct perf_event_header header;
  83. u32 pid,tid;
  84. u64 value;
  85. u64 format[3];
  86. };
  87. typedef union event_union {
  88. struct perf_event_header header;
  89. struct ip_event ip;
  90. struct mmap_event mmap;
  91. struct comm_event comm;
  92. struct fork_event fork;
  93. struct period_event period;
  94. struct lost_event lost;
  95. struct read_event read;
  96. } event_t;
  97. static LIST_HEAD(dsos);
  98. static struct dso *kernel_dso;
  99. static struct dso *vdso;
  100. static void dsos__add(struct dso *dso)
  101. {
  102. list_add_tail(&dso->node, &dsos);
  103. }
  104. static struct dso *dsos__find(const char *name)
  105. {
  106. struct dso *pos;
  107. list_for_each_entry(pos, &dsos, node)
  108. if (strcmp(pos->name, name) == 0)
  109. return pos;
  110. return NULL;
  111. }
  112. static struct dso *dsos__findnew(const char *name)
  113. {
  114. struct dso *dso = dsos__find(name);
  115. int nr;
  116. if (dso)
  117. return dso;
  118. dso = dso__new(name, 0);
  119. if (!dso)
  120. goto out_delete_dso;
  121. nr = dso__load(dso, NULL, verbose);
  122. if (nr < 0) {
  123. eprintf("Failed to open: %s\n", name);
  124. goto out_delete_dso;
  125. }
  126. if (!nr)
  127. eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
  128. dsos__add(dso);
  129. return dso;
  130. out_delete_dso:
  131. dso__delete(dso);
  132. return NULL;
  133. }
  134. static void dsos__fprintf(FILE *fp)
  135. {
  136. struct dso *pos;
  137. list_for_each_entry(pos, &dsos, node)
  138. dso__fprintf(pos, fp);
  139. }
  140. static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
  141. {
  142. return dso__find_symbol(kernel_dso, ip);
  143. }
  144. static int load_kernel(void)
  145. {
  146. int err;
  147. kernel_dso = dso__new("[kernel]", 0);
  148. if (!kernel_dso)
  149. return -1;
  150. err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
  151. if (err) {
  152. dso__delete(kernel_dso);
  153. kernel_dso = NULL;
  154. } else
  155. dsos__add(kernel_dso);
  156. vdso = dso__new("[vdso]", 0);
  157. if (!vdso)
  158. return -1;
  159. vdso->find_symbol = vdso__find_symbol;
  160. dsos__add(vdso);
  161. return err;
  162. }
  163. static char __cwd[PATH_MAX];
  164. static char *cwd = __cwd;
  165. static int cwdlen;
  166. static int strcommon(const char *pathname)
  167. {
  168. int n = 0;
  169. while (pathname[n] == cwd[n] && n < cwdlen)
  170. ++n;
  171. return n;
  172. }
  173. struct map {
  174. struct list_head node;
  175. u64 start;
  176. u64 end;
  177. u64 pgoff;
  178. u64 (*map_ip)(struct map *, u64);
  179. struct dso *dso;
  180. };
  181. static u64 map__map_ip(struct map *map, u64 ip)
  182. {
  183. return ip - map->start + map->pgoff;
  184. }
  185. static u64 vdso__map_ip(struct map *map, u64 ip)
  186. {
  187. return ip;
  188. }
  189. static inline int is_anon_memory(const char *filename)
  190. {
  191. return strcmp(filename, "//anon") == 0;
  192. }
  193. static struct map *map__new(struct mmap_event *event)
  194. {
  195. struct map *self = malloc(sizeof(*self));
  196. if (self != NULL) {
  197. const char *filename = event->filename;
  198. char newfilename[PATH_MAX];
  199. int anon;
  200. if (cwd) {
  201. int n = strcommon(filename);
  202. if (n == cwdlen) {
  203. snprintf(newfilename, sizeof(newfilename),
  204. ".%s", filename + n);
  205. filename = newfilename;
  206. }
  207. }
  208. anon = is_anon_memory(filename);
  209. if (anon) {
  210. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  211. filename = newfilename;
  212. }
  213. self->start = event->start;
  214. self->end = event->start + event->len;
  215. self->pgoff = event->pgoff;
  216. self->dso = dsos__findnew(filename);
  217. if (self->dso == NULL)
  218. goto out_delete;
  219. if (self->dso == vdso || anon)
  220. self->map_ip = vdso__map_ip;
  221. else
  222. self->map_ip = map__map_ip;
  223. }
  224. return self;
  225. out_delete:
  226. free(self);
  227. return NULL;
  228. }
  229. static struct map *map__clone(struct map *self)
  230. {
  231. struct map *map = malloc(sizeof(*self));
  232. if (!map)
  233. return NULL;
  234. memcpy(map, self, sizeof(*self));
  235. return map;
  236. }
  237. static int map__overlap(struct map *l, struct map *r)
  238. {
  239. if (l->start > r->start) {
  240. struct map *t = l;
  241. l = r;
  242. r = t;
  243. }
  244. if (l->end > r->start)
  245. return 1;
  246. return 0;
  247. }
  248. static size_t map__fprintf(struct map *self, FILE *fp)
  249. {
  250. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  251. self->start, self->end, self->pgoff, self->dso->name);
  252. }
  253. struct thread {
  254. struct rb_node rb_node;
  255. struct list_head maps;
  256. pid_t pid;
  257. char *comm;
  258. };
  259. static struct thread *thread__new(pid_t pid)
  260. {
  261. struct thread *self = malloc(sizeof(*self));
  262. if (self != NULL) {
  263. self->pid = pid;
  264. self->comm = malloc(32);
  265. if (self->comm)
  266. snprintf(self->comm, 32, ":%d", self->pid);
  267. INIT_LIST_HEAD(&self->maps);
  268. }
  269. return self;
  270. }
  271. static int thread__set_comm(struct thread *self, const char *comm)
  272. {
  273. if (self->comm)
  274. free(self->comm);
  275. self->comm = strdup(comm);
  276. return self->comm ? 0 : -ENOMEM;
  277. }
  278. static size_t thread__fprintf(struct thread *self, FILE *fp)
  279. {
  280. struct map *pos;
  281. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  282. list_for_each_entry(pos, &self->maps, node)
  283. ret += map__fprintf(pos, fp);
  284. return ret;
  285. }
  286. static struct rb_root threads;
  287. static struct thread *last_match;
  288. static struct thread *threads__findnew(pid_t pid)
  289. {
  290. struct rb_node **p = &threads.rb_node;
  291. struct rb_node *parent = NULL;
  292. struct thread *th;
  293. /*
  294. * Font-end cache - PID lookups come in blocks,
  295. * so most of the time we dont have to look up
  296. * the full rbtree:
  297. */
  298. if (last_match && last_match->pid == pid)
  299. return last_match;
  300. while (*p != NULL) {
  301. parent = *p;
  302. th = rb_entry(parent, struct thread, rb_node);
  303. if (th->pid == pid) {
  304. last_match = th;
  305. return th;
  306. }
  307. if (pid < th->pid)
  308. p = &(*p)->rb_left;
  309. else
  310. p = &(*p)->rb_right;
  311. }
  312. th = thread__new(pid);
  313. if (th != NULL) {
  314. rb_link_node(&th->rb_node, parent, p);
  315. rb_insert_color(&th->rb_node, &threads);
  316. last_match = th;
  317. }
  318. return th;
  319. }
  320. static void thread__insert_map(struct thread *self, struct map *map)
  321. {
  322. struct map *pos, *tmp;
  323. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  324. if (map__overlap(pos, map)) {
  325. if (verbose >= 2) {
  326. printf("overlapping maps:\n");
  327. map__fprintf(map, stdout);
  328. map__fprintf(pos, stdout);
  329. }
  330. if (map->start <= pos->start && map->end > pos->start)
  331. pos->start = map->end;
  332. if (map->end >= pos->end && map->start < pos->end)
  333. pos->end = map->start;
  334. if (verbose >= 2) {
  335. printf("after collision:\n");
  336. map__fprintf(pos, stdout);
  337. }
  338. if (pos->start >= pos->end) {
  339. list_del_init(&pos->node);
  340. free(pos);
  341. }
  342. }
  343. }
  344. list_add_tail(&map->node, &self->maps);
  345. }
  346. static int thread__fork(struct thread *self, struct thread *parent)
  347. {
  348. struct map *map;
  349. if (self->comm)
  350. free(self->comm);
  351. self->comm = strdup(parent->comm);
  352. if (!self->comm)
  353. return -ENOMEM;
  354. list_for_each_entry(map, &parent->maps, node) {
  355. struct map *new = map__clone(map);
  356. if (!new)
  357. return -ENOMEM;
  358. thread__insert_map(self, new);
  359. }
  360. return 0;
  361. }
  362. static struct map *thread__find_map(struct thread *self, u64 ip)
  363. {
  364. struct map *pos;
  365. if (self == NULL)
  366. return NULL;
  367. list_for_each_entry(pos, &self->maps, node)
  368. if (ip >= pos->start && ip <= pos->end)
  369. return pos;
  370. return NULL;
  371. }
  372. static size_t threads__fprintf(FILE *fp)
  373. {
  374. size_t ret = 0;
  375. struct rb_node *nd;
  376. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  377. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  378. ret += thread__fprintf(pos, fp);
  379. }
  380. return ret;
  381. }
  382. /*
  383. * histogram, sorted on item, collects counts
  384. */
  385. static struct rb_root hist;
  386. struct hist_entry {
  387. struct rb_node rb_node;
  388. struct thread *thread;
  389. struct map *map;
  390. struct dso *dso;
  391. struct symbol *sym;
  392. struct symbol *parent;
  393. u64 ip;
  394. char level;
  395. struct callchain_node callchain;
  396. struct rb_root sorted_chain;
  397. u64 count;
  398. };
  399. /*
  400. * configurable sorting bits
  401. */
  402. struct sort_entry {
  403. struct list_head list;
  404. char *header;
  405. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  406. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  407. size_t (*print)(FILE *fp, struct hist_entry *);
  408. };
  409. static int64_t cmp_null(void *l, void *r)
  410. {
  411. if (!l && !r)
  412. return 0;
  413. else if (!l)
  414. return -1;
  415. else
  416. return 1;
  417. }
  418. /* --sort pid */
  419. static int64_t
  420. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  421. {
  422. return right->thread->pid - left->thread->pid;
  423. }
  424. static size_t
  425. sort__thread_print(FILE *fp, struct hist_entry *self)
  426. {
  427. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  428. }
  429. static struct sort_entry sort_thread = {
  430. .header = " Command: Pid",
  431. .cmp = sort__thread_cmp,
  432. .print = sort__thread_print,
  433. };
  434. /* --sort comm */
  435. static int64_t
  436. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  437. {
  438. return right->thread->pid - left->thread->pid;
  439. }
  440. static int64_t
  441. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  442. {
  443. char *comm_l = left->thread->comm;
  444. char *comm_r = right->thread->comm;
  445. if (!comm_l || !comm_r)
  446. return cmp_null(comm_l, comm_r);
  447. return strcmp(comm_l, comm_r);
  448. }
  449. static size_t
  450. sort__comm_print(FILE *fp, struct hist_entry *self)
  451. {
  452. return fprintf(fp, "%16s", self->thread->comm);
  453. }
  454. static struct sort_entry sort_comm = {
  455. .header = " Command",
  456. .cmp = sort__comm_cmp,
  457. .collapse = sort__comm_collapse,
  458. .print = sort__comm_print,
  459. };
  460. /* --sort dso */
  461. static int64_t
  462. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  463. {
  464. struct dso *dso_l = left->dso;
  465. struct dso *dso_r = right->dso;
  466. if (!dso_l || !dso_r)
  467. return cmp_null(dso_l, dso_r);
  468. return strcmp(dso_l->name, dso_r->name);
  469. }
  470. static size_t
  471. sort__dso_print(FILE *fp, struct hist_entry *self)
  472. {
  473. if (self->dso)
  474. return fprintf(fp, "%-25s", self->dso->name);
  475. return fprintf(fp, "%016llx ", (u64)self->ip);
  476. }
  477. static struct sort_entry sort_dso = {
  478. .header = "Shared Object ",
  479. .cmp = sort__dso_cmp,
  480. .print = sort__dso_print,
  481. };
  482. /* --sort symbol */
  483. static int64_t
  484. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  485. {
  486. u64 ip_l, ip_r;
  487. if (left->sym == right->sym)
  488. return 0;
  489. ip_l = left->sym ? left->sym->start : left->ip;
  490. ip_r = right->sym ? right->sym->start : right->ip;
  491. return (int64_t)(ip_r - ip_l);
  492. }
  493. static size_t
  494. sort__sym_print(FILE *fp, struct hist_entry *self)
  495. {
  496. size_t ret = 0;
  497. if (verbose)
  498. ret += fprintf(fp, "%#018llx ", (u64)self->ip);
  499. if (self->sym) {
  500. ret += fprintf(fp, "[%c] %s",
  501. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  502. } else {
  503. ret += fprintf(fp, "%#016llx", (u64)self->ip);
  504. }
  505. return ret;
  506. }
  507. static struct sort_entry sort_sym = {
  508. .header = "Symbol",
  509. .cmp = sort__sym_cmp,
  510. .print = sort__sym_print,
  511. };
  512. /* --sort parent */
  513. static int64_t
  514. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  515. {
  516. struct symbol *sym_l = left->parent;
  517. struct symbol *sym_r = right->parent;
  518. if (!sym_l || !sym_r)
  519. return cmp_null(sym_l, sym_r);
  520. return strcmp(sym_l->name, sym_r->name);
  521. }
  522. static size_t
  523. sort__parent_print(FILE *fp, struct hist_entry *self)
  524. {
  525. size_t ret = 0;
  526. ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
  527. return ret;
  528. }
  529. static struct sort_entry sort_parent = {
  530. .header = "Parent symbol ",
  531. .cmp = sort__parent_cmp,
  532. .print = sort__parent_print,
  533. };
  534. static int sort__need_collapse = 0;
  535. static int sort__has_parent = 0;
  536. struct sort_dimension {
  537. char *name;
  538. struct sort_entry *entry;
  539. int taken;
  540. };
  541. static struct sort_dimension sort_dimensions[] = {
  542. { .name = "pid", .entry = &sort_thread, },
  543. { .name = "comm", .entry = &sort_comm, },
  544. { .name = "dso", .entry = &sort_dso, },
  545. { .name = "symbol", .entry = &sort_sym, },
  546. { .name = "parent", .entry = &sort_parent, },
  547. };
  548. static LIST_HEAD(hist_entry__sort_list);
  549. static int sort_dimension__add(char *tok)
  550. {
  551. int i;
  552. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  553. struct sort_dimension *sd = &sort_dimensions[i];
  554. if (sd->taken)
  555. continue;
  556. if (strncasecmp(tok, sd->name, strlen(tok)))
  557. continue;
  558. if (sd->entry->collapse)
  559. sort__need_collapse = 1;
  560. if (sd->entry == &sort_parent) {
  561. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  562. if (ret) {
  563. char err[BUFSIZ];
  564. regerror(ret, &parent_regex, err, sizeof(err));
  565. fprintf(stderr, "Invalid regex: %s\n%s",
  566. parent_pattern, err);
  567. exit(-1);
  568. }
  569. sort__has_parent = 1;
  570. }
  571. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  572. sd->taken = 1;
  573. return 0;
  574. }
  575. return -ESRCH;
  576. }
  577. static int64_t
  578. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  579. {
  580. struct sort_entry *se;
  581. int64_t cmp = 0;
  582. list_for_each_entry(se, &hist_entry__sort_list, list) {
  583. cmp = se->cmp(left, right);
  584. if (cmp)
  585. break;
  586. }
  587. return cmp;
  588. }
  589. static int64_t
  590. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  591. {
  592. struct sort_entry *se;
  593. int64_t cmp = 0;
  594. list_for_each_entry(se, &hist_entry__sort_list, list) {
  595. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  596. f = se->collapse ?: se->cmp;
  597. cmp = f(left, right);
  598. if (cmp)
  599. break;
  600. }
  601. return cmp;
  602. }
  603. static size_t
  604. callchain__fprintf(FILE *fp, struct callchain_node *self, u64 total_samples)
  605. {
  606. struct callchain_list *chain;
  607. size_t ret = 0;
  608. if (!self)
  609. return 0;
  610. ret += callchain__fprintf(fp, self->parent, total_samples);
  611. list_for_each_entry(chain, &self->val, list)
  612. ret += fprintf(fp, " %p\n", (void *)chain->ip);
  613. return ret;
  614. }
  615. static size_t
  616. hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  617. u64 total_samples)
  618. {
  619. struct rb_node *rb_node;
  620. struct callchain_node *chain;
  621. size_t ret = 0;
  622. rb_node = rb_first(&self->sorted_chain);
  623. while (rb_node) {
  624. double percent;
  625. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  626. percent = chain->hit * 100.0 / total_samples;
  627. ret += fprintf(fp, " %6.2f%%\n", percent);
  628. ret += callchain__fprintf(fp, chain, total_samples);
  629. ret += fprintf(fp, "\n");
  630. rb_node = rb_next(rb_node);
  631. }
  632. return ret;
  633. }
  634. static size_t
  635. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  636. {
  637. struct sort_entry *se;
  638. size_t ret;
  639. if (exclude_other && !self->parent)
  640. return 0;
  641. if (total_samples) {
  642. double percent = self->count * 100.0 / total_samples;
  643. char *color = PERF_COLOR_NORMAL;
  644. /*
  645. * We color high-overhead entries in red, mid-overhead
  646. * entries in green - and keep the low overhead places
  647. * normal:
  648. */
  649. if (percent >= 5.0) {
  650. color = PERF_COLOR_RED;
  651. } else {
  652. if (percent >= 0.5)
  653. color = PERF_COLOR_GREEN;
  654. }
  655. ret = color_fprintf(fp, color, " %6.2f%%",
  656. (self->count * 100.0) / total_samples);
  657. } else
  658. ret = fprintf(fp, "%12Ld ", self->count);
  659. list_for_each_entry(se, &hist_entry__sort_list, list) {
  660. if (exclude_other && (se == &sort_parent))
  661. continue;
  662. fprintf(fp, " ");
  663. ret += se->print(fp, self);
  664. }
  665. ret += fprintf(fp, "\n");
  666. if (callchain)
  667. hist_entry_callchain__fprintf(fp, self, total_samples);
  668. return ret;
  669. }
  670. /*
  671. *
  672. */
  673. static struct symbol *
  674. resolve_symbol(struct thread *thread, struct map **mapp,
  675. struct dso **dsop, u64 *ipp)
  676. {
  677. struct dso *dso = dsop ? *dsop : NULL;
  678. struct map *map = mapp ? *mapp : NULL;
  679. u64 ip = *ipp;
  680. if (!thread)
  681. return NULL;
  682. if (dso)
  683. goto got_dso;
  684. if (map)
  685. goto got_map;
  686. map = thread__find_map(thread, ip);
  687. if (map != NULL) {
  688. if (mapp)
  689. *mapp = map;
  690. got_map:
  691. ip = map->map_ip(map, ip);
  692. dso = map->dso;
  693. } else {
  694. /*
  695. * If this is outside of all known maps,
  696. * and is a negative address, try to look it
  697. * up in the kernel dso, as it might be a
  698. * vsyscall (which executes in user-mode):
  699. */
  700. if ((long long)ip < 0)
  701. dso = kernel_dso;
  702. }
  703. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  704. dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  705. *ipp = ip;
  706. if (dsop)
  707. *dsop = dso;
  708. if (!dso)
  709. return NULL;
  710. got_dso:
  711. return dso->find_symbol(dso, ip);
  712. }
  713. static int call__match(struct symbol *sym)
  714. {
  715. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  716. return 1;
  717. return 0;
  718. }
  719. /*
  720. * collect histogram counts
  721. */
  722. static int
  723. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  724. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  725. char level, u64 count)
  726. {
  727. struct rb_node **p = &hist.rb_node;
  728. struct rb_node *parent = NULL;
  729. struct hist_entry *he;
  730. struct hist_entry entry = {
  731. .thread = thread,
  732. .map = map,
  733. .dso = dso,
  734. .sym = sym,
  735. .ip = ip,
  736. .level = level,
  737. .count = count,
  738. .parent = NULL,
  739. .sorted_chain = RB_ROOT
  740. };
  741. int cmp;
  742. if (sort__has_parent && chain) {
  743. u64 context = PERF_CONTEXT_MAX;
  744. int i;
  745. for (i = 0; i < chain->nr; i++) {
  746. u64 ip = chain->ips[i];
  747. struct dso *dso = NULL;
  748. struct symbol *sym;
  749. if (ip >= PERF_CONTEXT_MAX) {
  750. context = ip;
  751. continue;
  752. }
  753. switch (context) {
  754. case PERF_CONTEXT_KERNEL:
  755. dso = kernel_dso;
  756. break;
  757. default:
  758. break;
  759. }
  760. sym = resolve_symbol(thread, NULL, &dso, &ip);
  761. if (sym && call__match(sym)) {
  762. entry.parent = sym;
  763. break;
  764. }
  765. }
  766. }
  767. while (*p != NULL) {
  768. parent = *p;
  769. he = rb_entry(parent, struct hist_entry, rb_node);
  770. cmp = hist_entry__cmp(&entry, he);
  771. if (!cmp) {
  772. he->count += count;
  773. if (callchain)
  774. append_chain(&he->callchain, chain);
  775. return 0;
  776. }
  777. if (cmp < 0)
  778. p = &(*p)->rb_left;
  779. else
  780. p = &(*p)->rb_right;
  781. }
  782. he = malloc(sizeof(*he));
  783. if (!he)
  784. return -ENOMEM;
  785. *he = entry;
  786. if (callchain) {
  787. callchain_init(&he->callchain);
  788. append_chain(&he->callchain, chain);
  789. }
  790. rb_link_node(&he->rb_node, parent, p);
  791. rb_insert_color(&he->rb_node, &hist);
  792. return 0;
  793. }
  794. static void hist_entry__free(struct hist_entry *he)
  795. {
  796. free(he);
  797. }
  798. /*
  799. * collapse the histogram
  800. */
  801. static struct rb_root collapse_hists;
  802. static void collapse__insert_entry(struct hist_entry *he)
  803. {
  804. struct rb_node **p = &collapse_hists.rb_node;
  805. struct rb_node *parent = NULL;
  806. struct hist_entry *iter;
  807. int64_t cmp;
  808. while (*p != NULL) {
  809. parent = *p;
  810. iter = rb_entry(parent, struct hist_entry, rb_node);
  811. cmp = hist_entry__collapse(iter, he);
  812. if (!cmp) {
  813. iter->count += he->count;
  814. hist_entry__free(he);
  815. return;
  816. }
  817. if (cmp < 0)
  818. p = &(*p)->rb_left;
  819. else
  820. p = &(*p)->rb_right;
  821. }
  822. rb_link_node(&he->rb_node, parent, p);
  823. rb_insert_color(&he->rb_node, &collapse_hists);
  824. }
  825. static void collapse__resort(void)
  826. {
  827. struct rb_node *next;
  828. struct hist_entry *n;
  829. if (!sort__need_collapse)
  830. return;
  831. next = rb_first(&hist);
  832. while (next) {
  833. n = rb_entry(next, struct hist_entry, rb_node);
  834. next = rb_next(&n->rb_node);
  835. rb_erase(&n->rb_node, &hist);
  836. collapse__insert_entry(n);
  837. }
  838. }
  839. /*
  840. * reverse the map, sort on count.
  841. */
  842. static struct rb_root output_hists;
  843. static void output__insert_entry(struct hist_entry *he)
  844. {
  845. struct rb_node **p = &output_hists.rb_node;
  846. struct rb_node *parent = NULL;
  847. struct hist_entry *iter;
  848. if (callchain)
  849. sort_chain_to_rbtree(&he->sorted_chain, &he->callchain);
  850. while (*p != NULL) {
  851. parent = *p;
  852. iter = rb_entry(parent, struct hist_entry, rb_node);
  853. if (he->count > iter->count)
  854. p = &(*p)->rb_left;
  855. else
  856. p = &(*p)->rb_right;
  857. }
  858. rb_link_node(&he->rb_node, parent, p);
  859. rb_insert_color(&he->rb_node, &output_hists);
  860. }
  861. static void output__resort(void)
  862. {
  863. struct rb_node *next;
  864. struct hist_entry *n;
  865. struct rb_root *tree = &hist;
  866. if (sort__need_collapse)
  867. tree = &collapse_hists;
  868. next = rb_first(tree);
  869. while (next) {
  870. n = rb_entry(next, struct hist_entry, rb_node);
  871. next = rb_next(&n->rb_node);
  872. rb_erase(&n->rb_node, tree);
  873. output__insert_entry(n);
  874. }
  875. }
  876. static size_t output__fprintf(FILE *fp, u64 total_samples)
  877. {
  878. struct hist_entry *pos;
  879. struct sort_entry *se;
  880. struct rb_node *nd;
  881. size_t ret = 0;
  882. fprintf(fp, "\n");
  883. fprintf(fp, "#\n");
  884. fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
  885. fprintf(fp, "#\n");
  886. fprintf(fp, "# Overhead");
  887. list_for_each_entry(se, &hist_entry__sort_list, list) {
  888. if (exclude_other && (se == &sort_parent))
  889. continue;
  890. fprintf(fp, " %s", se->header);
  891. }
  892. fprintf(fp, "\n");
  893. fprintf(fp, "# ........");
  894. list_for_each_entry(se, &hist_entry__sort_list, list) {
  895. int i;
  896. if (exclude_other && (se == &sort_parent))
  897. continue;
  898. fprintf(fp, " ");
  899. for (i = 0; i < strlen(se->header); i++)
  900. fprintf(fp, ".");
  901. }
  902. fprintf(fp, "\n");
  903. fprintf(fp, "#\n");
  904. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  905. pos = rb_entry(nd, struct hist_entry, rb_node);
  906. ret += hist_entry__fprintf(fp, pos, total_samples);
  907. }
  908. if (sort_order == default_sort_order &&
  909. parent_pattern == default_parent_pattern) {
  910. fprintf(fp, "#\n");
  911. fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
  912. fprintf(fp, "#\n");
  913. }
  914. fprintf(fp, "\n");
  915. return ret;
  916. }
  917. static void register_idle_thread(void)
  918. {
  919. struct thread *thread = threads__findnew(0);
  920. if (thread == NULL ||
  921. thread__set_comm(thread, "[idle]")) {
  922. fprintf(stderr, "problem inserting idle task.\n");
  923. exit(-1);
  924. }
  925. }
  926. static unsigned long total = 0,
  927. total_mmap = 0,
  928. total_comm = 0,
  929. total_fork = 0,
  930. total_unknown = 0,
  931. total_lost = 0;
  932. static int validate_chain(struct ip_callchain *chain, event_t *event)
  933. {
  934. unsigned int chain_size;
  935. chain_size = event->header.size;
  936. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  937. if (chain->nr*sizeof(u64) > chain_size)
  938. return -1;
  939. return 0;
  940. }
  941. static int
  942. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  943. {
  944. char level;
  945. int show = 0;
  946. struct dso *dso = NULL;
  947. struct thread *thread = threads__findnew(event->ip.pid);
  948. u64 ip = event->ip.ip;
  949. u64 period = 1;
  950. struct map *map = NULL;
  951. void *more_data = event->ip.__more_data;
  952. struct ip_callchain *chain = NULL;
  953. if (sample_type & PERF_SAMPLE_PERIOD) {
  954. period = *(u64 *)more_data;
  955. more_data += sizeof(u64);
  956. }
  957. dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
  958. (void *)(offset + head),
  959. (void *)(long)(event->header.size),
  960. event->header.misc,
  961. event->ip.pid,
  962. (void *)(long)ip,
  963. (long long)period);
  964. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  965. int i;
  966. chain = (void *)more_data;
  967. dprintf("... chain: nr:%Lu\n", chain->nr);
  968. if (validate_chain(chain, event) < 0) {
  969. eprintf("call-chain problem with event, skipping it.\n");
  970. return 0;
  971. }
  972. if (dump_trace) {
  973. for (i = 0; i < chain->nr; i++)
  974. dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
  975. }
  976. }
  977. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  978. if (thread == NULL) {
  979. eprintf("problem processing %d event, skipping it.\n",
  980. event->header.type);
  981. return -1;
  982. }
  983. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  984. show = SHOW_KERNEL;
  985. level = 'k';
  986. dso = kernel_dso;
  987. dprintf(" ...... dso: %s\n", dso->name);
  988. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  989. show = SHOW_USER;
  990. level = '.';
  991. } else {
  992. show = SHOW_HV;
  993. level = 'H';
  994. dprintf(" ...... dso: [hypervisor]\n");
  995. }
  996. if (show & show_mask) {
  997. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  998. if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
  999. return 0;
  1000. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  1001. eprintf("problem incrementing symbol count, skipping event\n");
  1002. return -1;
  1003. }
  1004. }
  1005. total += period;
  1006. return 0;
  1007. }
  1008. static int
  1009. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  1010. {
  1011. struct thread *thread = threads__findnew(event->mmap.pid);
  1012. struct map *map = map__new(&event->mmap);
  1013. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  1014. (void *)(offset + head),
  1015. (void *)(long)(event->header.size),
  1016. event->mmap.pid,
  1017. (void *)(long)event->mmap.start,
  1018. (void *)(long)event->mmap.len,
  1019. (void *)(long)event->mmap.pgoff,
  1020. event->mmap.filename);
  1021. if (thread == NULL || map == NULL) {
  1022. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  1023. return 0;
  1024. }
  1025. thread__insert_map(thread, map);
  1026. total_mmap++;
  1027. return 0;
  1028. }
  1029. static int
  1030. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  1031. {
  1032. struct thread *thread = threads__findnew(event->comm.pid);
  1033. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  1034. (void *)(offset + head),
  1035. (void *)(long)(event->header.size),
  1036. event->comm.comm, event->comm.pid);
  1037. if (thread == NULL ||
  1038. thread__set_comm(thread, event->comm.comm)) {
  1039. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  1040. return -1;
  1041. }
  1042. total_comm++;
  1043. return 0;
  1044. }
  1045. static int
  1046. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  1047. {
  1048. struct thread *thread = threads__findnew(event->fork.pid);
  1049. struct thread *parent = threads__findnew(event->fork.ppid);
  1050. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  1051. (void *)(offset + head),
  1052. (void *)(long)(event->header.size),
  1053. event->fork.pid, event->fork.ppid);
  1054. if (!thread || !parent || thread__fork(thread, parent)) {
  1055. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  1056. return -1;
  1057. }
  1058. total_fork++;
  1059. return 0;
  1060. }
  1061. static int
  1062. process_period_event(event_t *event, unsigned long offset, unsigned long head)
  1063. {
  1064. dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
  1065. (void *)(offset + head),
  1066. (void *)(long)(event->header.size),
  1067. event->period.time,
  1068. event->period.id,
  1069. event->period.sample_period);
  1070. return 0;
  1071. }
  1072. static int
  1073. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  1074. {
  1075. dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
  1076. (void *)(offset + head),
  1077. (void *)(long)(event->header.size),
  1078. event->lost.id,
  1079. event->lost.lost);
  1080. total_lost += event->lost.lost;
  1081. return 0;
  1082. }
  1083. static void trace_event(event_t *event)
  1084. {
  1085. unsigned char *raw_event = (void *)event;
  1086. char *color = PERF_COLOR_BLUE;
  1087. int i, j;
  1088. if (!dump_trace)
  1089. return;
  1090. dprintf(".");
  1091. cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
  1092. for (i = 0; i < event->header.size; i++) {
  1093. if ((i & 15) == 0) {
  1094. dprintf(".");
  1095. cdprintf(" %04x: ", i);
  1096. }
  1097. cdprintf(" %02x", raw_event[i]);
  1098. if (((i & 15) == 15) || i == event->header.size-1) {
  1099. cdprintf(" ");
  1100. for (j = 0; j < 15-(i & 15); j++)
  1101. cdprintf(" ");
  1102. for (j = 0; j < (i & 15); j++) {
  1103. if (isprint(raw_event[i-15+j]))
  1104. cdprintf("%c", raw_event[i-15+j]);
  1105. else
  1106. cdprintf(".");
  1107. }
  1108. cdprintf("\n");
  1109. }
  1110. }
  1111. dprintf(".\n");
  1112. }
  1113. static int
  1114. process_read_event(event_t *event, unsigned long offset, unsigned long head)
  1115. {
  1116. dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
  1117. (void *)(offset + head),
  1118. (void *)(long)(event->header.size),
  1119. event->read.pid,
  1120. event->read.tid,
  1121. event->read.value);
  1122. return 0;
  1123. }
  1124. static int
  1125. process_event(event_t *event, unsigned long offset, unsigned long head)
  1126. {
  1127. trace_event(event);
  1128. switch (event->header.type) {
  1129. case PERF_EVENT_SAMPLE:
  1130. return process_sample_event(event, offset, head);
  1131. case PERF_EVENT_MMAP:
  1132. return process_mmap_event(event, offset, head);
  1133. case PERF_EVENT_COMM:
  1134. return process_comm_event(event, offset, head);
  1135. case PERF_EVENT_FORK:
  1136. return process_fork_event(event, offset, head);
  1137. case PERF_EVENT_PERIOD:
  1138. return process_period_event(event, offset, head);
  1139. case PERF_EVENT_LOST:
  1140. return process_lost_event(event, offset, head);
  1141. case PERF_EVENT_READ:
  1142. return process_read_event(event, offset, head);
  1143. /*
  1144. * We dont process them right now but they are fine:
  1145. */
  1146. case PERF_EVENT_THROTTLE:
  1147. case PERF_EVENT_UNTHROTTLE:
  1148. return 0;
  1149. default:
  1150. return -1;
  1151. }
  1152. return 0;
  1153. }
  1154. static struct perf_header *header;
  1155. static u64 perf_header__sample_type(void)
  1156. {
  1157. u64 sample_type = 0;
  1158. int i;
  1159. for (i = 0; i < header->attrs; i++) {
  1160. struct perf_header_attr *attr = header->attr[i];
  1161. if (!sample_type)
  1162. sample_type = attr->attr.sample_type;
  1163. else if (sample_type != attr->attr.sample_type)
  1164. die("non matching sample_type");
  1165. }
  1166. return sample_type;
  1167. }
  1168. static int __cmd_report(void)
  1169. {
  1170. int ret, rc = EXIT_FAILURE;
  1171. unsigned long offset = 0;
  1172. unsigned long head, shift;
  1173. struct stat stat;
  1174. event_t *event;
  1175. uint32_t size;
  1176. char *buf;
  1177. register_idle_thread();
  1178. input = open(input_name, O_RDONLY);
  1179. if (input < 0) {
  1180. fprintf(stderr, " failed to open file: %s", input_name);
  1181. if (!strcmp(input_name, "perf.data"))
  1182. fprintf(stderr, " (try 'perf record' first)");
  1183. fprintf(stderr, "\n");
  1184. exit(-1);
  1185. }
  1186. ret = fstat(input, &stat);
  1187. if (ret < 0) {
  1188. perror("failed to stat file");
  1189. exit(-1);
  1190. }
  1191. if (!stat.st_size) {
  1192. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1193. exit(0);
  1194. }
  1195. header = perf_header__read(input);
  1196. head = header->data_offset;
  1197. sample_type = perf_header__sample_type();
  1198. if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  1199. fprintf(stderr, "selected --sort parent, but no callchain data\n");
  1200. exit(-1);
  1201. }
  1202. if (load_kernel() < 0) {
  1203. perror("failed to load kernel symbols");
  1204. return EXIT_FAILURE;
  1205. }
  1206. if (!full_paths) {
  1207. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  1208. perror("failed to get the current directory");
  1209. return EXIT_FAILURE;
  1210. }
  1211. cwdlen = strlen(cwd);
  1212. } else {
  1213. cwd = NULL;
  1214. cwdlen = 0;
  1215. }
  1216. shift = page_size * (head / page_size);
  1217. offset += shift;
  1218. head -= shift;
  1219. remap:
  1220. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1221. MAP_SHARED, input, offset);
  1222. if (buf == MAP_FAILED) {
  1223. perror("failed to mmap file");
  1224. exit(-1);
  1225. }
  1226. more:
  1227. event = (event_t *)(buf + head);
  1228. size = event->header.size;
  1229. if (!size)
  1230. size = 8;
  1231. if (head + event->header.size >= page_size * mmap_window) {
  1232. int ret;
  1233. shift = page_size * (head / page_size);
  1234. ret = munmap(buf, page_size * mmap_window);
  1235. assert(ret == 0);
  1236. offset += shift;
  1237. head -= shift;
  1238. goto remap;
  1239. }
  1240. size = event->header.size;
  1241. dprintf("\n%p [%p]: event: %d\n",
  1242. (void *)(offset + head),
  1243. (void *)(long)event->header.size,
  1244. event->header.type);
  1245. if (!size || process_event(event, offset, head) < 0) {
  1246. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1247. (void *)(offset + head),
  1248. (void *)(long)(event->header.size),
  1249. event->header.type);
  1250. total_unknown++;
  1251. /*
  1252. * assume we lost track of the stream, check alignment, and
  1253. * increment a single u64 in the hope to catch on again 'soon'.
  1254. */
  1255. if (unlikely(head & 7))
  1256. head &= ~7ULL;
  1257. size = 8;
  1258. }
  1259. head += size;
  1260. if (offset + head >= header->data_offset + header->data_size)
  1261. goto done;
  1262. if (offset + head < stat.st_size)
  1263. goto more;
  1264. done:
  1265. rc = EXIT_SUCCESS;
  1266. close(input);
  1267. dprintf(" IP events: %10ld\n", total);
  1268. dprintf(" mmap events: %10ld\n", total_mmap);
  1269. dprintf(" comm events: %10ld\n", total_comm);
  1270. dprintf(" fork events: %10ld\n", total_fork);
  1271. dprintf(" lost events: %10ld\n", total_lost);
  1272. dprintf(" unknown events: %10ld\n", total_unknown);
  1273. if (dump_trace)
  1274. return 0;
  1275. if (verbose >= 3)
  1276. threads__fprintf(stdout);
  1277. if (verbose >= 2)
  1278. dsos__fprintf(stdout);
  1279. collapse__resort();
  1280. output__resort();
  1281. output__fprintf(stdout, total);
  1282. return rc;
  1283. }
  1284. static const char * const report_usage[] = {
  1285. "perf report [<options>] <command>",
  1286. NULL
  1287. };
  1288. static const struct option options[] = {
  1289. OPT_STRING('i', "input", &input_name, "file",
  1290. "input file name"),
  1291. OPT_BOOLEAN('v', "verbose", &verbose,
  1292. "be more verbose (show symbol address, etc)"),
  1293. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1294. "dump raw trace in ASCII"),
  1295. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1296. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1297. "sort by key(s): pid, comm, dso, symbol, parent"),
  1298. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1299. "Don't shorten the pathnames taking into account the cwd"),
  1300. OPT_STRING('p', "parent", &parent_pattern, "regex",
  1301. "regex filter to identify parent, see: '--sort parent'"),
  1302. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  1303. "Only display entries with parent-match"),
  1304. OPT_BOOLEAN('c', "callchain", &callchain, "Display callchains"),
  1305. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  1306. "only consider symbols in these dsos"),
  1307. OPT_END()
  1308. };
  1309. static void setup_sorting(void)
  1310. {
  1311. char *tmp, *tok, *str = strdup(sort_order);
  1312. for (tok = strtok_r(str, ", ", &tmp);
  1313. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1314. if (sort_dimension__add(tok) < 0) {
  1315. error("Unknown --sort key: `%s'", tok);
  1316. usage_with_options(report_usage, options);
  1317. }
  1318. }
  1319. free(str);
  1320. }
  1321. int cmd_report(int argc, const char **argv, const char *prefix)
  1322. {
  1323. symbol__init();
  1324. page_size = getpagesize();
  1325. argc = parse_options(argc, argv, options, report_usage, 0);
  1326. setup_sorting();
  1327. if (parent_pattern != default_parent_pattern)
  1328. sort_dimension__add("parent");
  1329. else
  1330. exclude_other = 0;
  1331. /*
  1332. * Any (unrecognized) arguments left?
  1333. */
  1334. if (argc)
  1335. usage_with_options(report_usage, options);
  1336. if (dso_list_str) {
  1337. dso_list = strlist__new(true, dso_list_str);
  1338. if (!dso_list) {
  1339. fprintf(stderr, "problems parsing dso list\n");
  1340. exit(129);
  1341. }
  1342. }
  1343. setup_pager();
  1344. return __cmd_report();
  1345. }