builtin-report.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 <linux/list.h>
  12. #include "util/cache.h"
  13. #include <linux/rbtree.h>
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "util/callchain.h"
  17. #include "util/strlist.h"
  18. #include "util/values.h"
  19. #include "perf.h"
  20. #include "util/debug.h"
  21. #include "util/header.h"
  22. #include "util/parse-options.h"
  23. #include "util/parse-events.h"
  24. #include "util/data_map.h"
  25. #include "util/thread.h"
  26. #include "util/sort.h"
  27. #include "util/hist.h"
  28. static char const *input_name = "perf.data";
  29. static char *dso_list_str, *comm_list_str, *sym_list_str,
  30. *col_width_list_str;
  31. static struct strlist *dso_list, *comm_list, *sym_list;
  32. static int force;
  33. static int full_paths;
  34. static int show_nr_samples;
  35. static int show_threads;
  36. static struct perf_read_values show_threads_values;
  37. static char default_pretty_printing_style[] = "normal";
  38. static char *pretty_printing_style = default_pretty_printing_style;
  39. static int exclude_other = 1;
  40. static char callchain_default_opt[] = "fractal,0.5";
  41. static char *cwd;
  42. static int cwdlen;
  43. static struct perf_header *header;
  44. static u64 sample_type;
  45. static size_t
  46. callchain__fprintf_left_margin(FILE *fp, int left_margin)
  47. {
  48. int i;
  49. int ret;
  50. ret = fprintf(fp, " ");
  51. for (i = 0; i < left_margin; i++)
  52. ret += fprintf(fp, " ");
  53. return ret;
  54. }
  55. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  56. int left_margin)
  57. {
  58. int i;
  59. size_t ret = 0;
  60. ret += callchain__fprintf_left_margin(fp, left_margin);
  61. for (i = 0; i < depth; i++)
  62. if (depth_mask & (1 << i))
  63. ret += fprintf(fp, "| ");
  64. else
  65. ret += fprintf(fp, " ");
  66. ret += fprintf(fp, "\n");
  67. return ret;
  68. }
  69. static size_t
  70. ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
  71. int depth_mask, int count, u64 total_samples,
  72. int hits, int left_margin)
  73. {
  74. int i;
  75. size_t ret = 0;
  76. ret += callchain__fprintf_left_margin(fp, left_margin);
  77. for (i = 0; i < depth; i++) {
  78. if (depth_mask & (1 << i))
  79. ret += fprintf(fp, "|");
  80. else
  81. ret += fprintf(fp, " ");
  82. if (!count && i == depth - 1) {
  83. double percent;
  84. percent = hits * 100.0 / total_samples;
  85. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  86. } else
  87. ret += fprintf(fp, "%s", " ");
  88. }
  89. if (chain->sym)
  90. ret += fprintf(fp, "%s\n", chain->sym->name);
  91. else
  92. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  93. return ret;
  94. }
  95. static struct symbol *rem_sq_bracket;
  96. static struct callchain_list rem_hits;
  97. static void init_rem_hits(void)
  98. {
  99. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  100. if (!rem_sq_bracket) {
  101. fprintf(stderr, "Not enough memory to display remaining hits\n");
  102. return;
  103. }
  104. strcpy(rem_sq_bracket->name, "[...]");
  105. rem_hits.sym = rem_sq_bracket;
  106. }
  107. static size_t
  108. __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  109. u64 total_samples, int depth, int depth_mask,
  110. int left_margin)
  111. {
  112. struct rb_node *node, *next;
  113. struct callchain_node *child;
  114. struct callchain_list *chain;
  115. int new_depth_mask = depth_mask;
  116. u64 new_total;
  117. u64 remaining;
  118. size_t ret = 0;
  119. int i;
  120. if (callchain_param.mode == CHAIN_GRAPH_REL)
  121. new_total = self->children_hit;
  122. else
  123. new_total = total_samples;
  124. remaining = new_total;
  125. node = rb_first(&self->rb_root);
  126. while (node) {
  127. u64 cumul;
  128. child = rb_entry(node, struct callchain_node, rb_node);
  129. cumul = cumul_hits(child);
  130. remaining -= cumul;
  131. /*
  132. * The depth mask manages the output of pipes that show
  133. * the depth. We don't want to keep the pipes of the current
  134. * level for the last child of this depth.
  135. * Except if we have remaining filtered hits. They will
  136. * supersede the last child
  137. */
  138. next = rb_next(node);
  139. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  140. new_depth_mask &= ~(1 << (depth - 1));
  141. /*
  142. * But we keep the older depth mask for the line seperator
  143. * to keep the level link until we reach the last child
  144. */
  145. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  146. left_margin);
  147. i = 0;
  148. list_for_each_entry(chain, &child->val, list) {
  149. if (chain->ip >= PERF_CONTEXT_MAX)
  150. continue;
  151. ret += ipchain__fprintf_graph(fp, chain, depth,
  152. new_depth_mask, i++,
  153. new_total,
  154. cumul,
  155. left_margin);
  156. }
  157. ret += __callchain__fprintf_graph(fp, child, new_total,
  158. depth + 1,
  159. new_depth_mask | (1 << depth),
  160. left_margin);
  161. node = next;
  162. }
  163. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  164. remaining && remaining != new_total) {
  165. if (!rem_sq_bracket)
  166. return ret;
  167. new_depth_mask &= ~(1 << (depth - 1));
  168. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  169. new_depth_mask, 0, new_total,
  170. remaining, left_margin);
  171. }
  172. return ret;
  173. }
  174. static size_t
  175. callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  176. u64 total_samples, int left_margin)
  177. {
  178. struct callchain_list *chain;
  179. bool printed = false;
  180. int i = 0;
  181. int ret = 0;
  182. list_for_each_entry(chain, &self->val, list) {
  183. if (chain->ip >= PERF_CONTEXT_MAX)
  184. continue;
  185. if (!i++ && sort__first_dimension == SORT_SYM)
  186. continue;
  187. if (!printed) {
  188. ret += callchain__fprintf_left_margin(fp, left_margin);
  189. ret += fprintf(fp, "|\n");
  190. ret += callchain__fprintf_left_margin(fp, left_margin);
  191. ret += fprintf(fp, "---");
  192. left_margin += 3;
  193. printed = true;
  194. } else
  195. ret += callchain__fprintf_left_margin(fp, left_margin);
  196. if (chain->sym)
  197. ret += fprintf(fp, " %s\n", chain->sym->name);
  198. else
  199. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  200. }
  201. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  202. return ret;
  203. }
  204. static size_t
  205. callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  206. u64 total_samples)
  207. {
  208. struct callchain_list *chain;
  209. size_t ret = 0;
  210. if (!self)
  211. return 0;
  212. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  213. list_for_each_entry(chain, &self->val, list) {
  214. if (chain->ip >= PERF_CONTEXT_MAX)
  215. continue;
  216. if (chain->sym)
  217. ret += fprintf(fp, " %s\n", chain->sym->name);
  218. else
  219. ret += fprintf(fp, " %p\n",
  220. (void *)(long)chain->ip);
  221. }
  222. return ret;
  223. }
  224. static size_t
  225. hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  226. u64 total_samples, int left_margin)
  227. {
  228. struct rb_node *rb_node;
  229. struct callchain_node *chain;
  230. size_t ret = 0;
  231. rb_node = rb_first(&self->sorted_chain);
  232. while (rb_node) {
  233. double percent;
  234. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  235. percent = chain->hit * 100.0 / total_samples;
  236. switch (callchain_param.mode) {
  237. case CHAIN_FLAT:
  238. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  239. percent);
  240. ret += callchain__fprintf_flat(fp, chain, total_samples);
  241. break;
  242. case CHAIN_GRAPH_ABS: /* Falldown */
  243. case CHAIN_GRAPH_REL:
  244. ret += callchain__fprintf_graph(fp, chain, total_samples,
  245. left_margin);
  246. case CHAIN_NONE:
  247. default:
  248. break;
  249. }
  250. ret += fprintf(fp, "\n");
  251. rb_node = rb_next(rb_node);
  252. }
  253. return ret;
  254. }
  255. static size_t
  256. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  257. {
  258. struct sort_entry *se;
  259. size_t ret;
  260. if (exclude_other && !self->parent)
  261. return 0;
  262. if (total_samples)
  263. ret = percent_color_fprintf(fp,
  264. field_sep ? "%.2f" : " %6.2f%%",
  265. (self->count * 100.0) / total_samples);
  266. else
  267. ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
  268. if (show_nr_samples) {
  269. if (field_sep)
  270. fprintf(fp, "%c%lld", *field_sep, self->count);
  271. else
  272. fprintf(fp, "%11lld", self->count);
  273. }
  274. list_for_each_entry(se, &hist_entry__sort_list, list) {
  275. if (se->elide)
  276. continue;
  277. fprintf(fp, "%s", field_sep ?: " ");
  278. ret += se->print(fp, self, se->width ? *se->width : 0);
  279. }
  280. ret += fprintf(fp, "\n");
  281. if (callchain) {
  282. int left_margin = 0;
  283. if (sort__first_dimension == SORT_COMM) {
  284. se = list_first_entry(&hist_entry__sort_list, typeof(*se),
  285. list);
  286. left_margin = se->width ? *se->width : 0;
  287. left_margin -= thread__comm_len(self->thread);
  288. }
  289. hist_entry_callchain__fprintf(fp, self, total_samples,
  290. left_margin);
  291. }
  292. return ret;
  293. }
  294. /*
  295. *
  296. */
  297. static void dso__calc_col_width(struct dso *self)
  298. {
  299. if (!col_width_list_str && !field_sep &&
  300. (!dso_list || strlist__has_entry(dso_list, self->name))) {
  301. unsigned int slen = strlen(self->name);
  302. if (slen > dsos__col_width)
  303. dsos__col_width = slen;
  304. }
  305. self->slen_calculated = 1;
  306. }
  307. static void thread__comm_adjust(struct thread *self)
  308. {
  309. char *comm = self->comm;
  310. if (!col_width_list_str && !field_sep &&
  311. (!comm_list || strlist__has_entry(comm_list, comm))) {
  312. unsigned int slen = strlen(comm);
  313. if (slen > comms__col_width) {
  314. comms__col_width = slen;
  315. threads__col_width = slen + 6;
  316. }
  317. }
  318. }
  319. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  320. {
  321. int ret = thread__set_comm(self, comm);
  322. if (ret)
  323. return ret;
  324. thread__comm_adjust(self);
  325. return 0;
  326. }
  327. static struct symbol *
  328. resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
  329. {
  330. struct map *map = mapp ? *mapp : NULL;
  331. u64 ip = *ipp;
  332. if (map)
  333. goto got_map;
  334. if (!thread)
  335. return NULL;
  336. map = thread__find_map(thread, ip);
  337. if (map != NULL) {
  338. /*
  339. * We have to do this here as we may have a dso
  340. * with no symbol hit that has a name longer than
  341. * the ones with symbols sampled.
  342. */
  343. if (!sort_dso.elide && !map->dso->slen_calculated)
  344. dso__calc_col_width(map->dso);
  345. if (mapp)
  346. *mapp = map;
  347. got_map:
  348. ip = map->map_ip(map, ip);
  349. } else {
  350. /*
  351. * If this is outside of all known maps,
  352. * and is a negative address, try to look it
  353. * up in the kernel dso, as it might be a
  354. * vsyscall or vdso (which executes in user-mode).
  355. *
  356. * XXX This is nasty, we should have a symbol list in
  357. * the "[vdso]" dso, but for now lets use the old
  358. * trick of looking in the whole kernel symbol list.
  359. */
  360. if ((long long)ip < 0)
  361. return kernel_maps__find_symbol(ip, mapp);
  362. }
  363. dump_printf(" ...... dso: %s\n",
  364. map ? map->dso->long_name : "<not found>");
  365. dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  366. *ipp = ip;
  367. return map ? map->dso->find_symbol(map->dso, ip) : NULL;
  368. }
  369. static int call__match(struct symbol *sym)
  370. {
  371. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  372. return 1;
  373. return 0;
  374. }
  375. static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
  376. struct ip_callchain *chain,
  377. struct symbol **parent)
  378. {
  379. u64 context = PERF_CONTEXT_MAX;
  380. struct symbol **syms = NULL;
  381. unsigned int i;
  382. if (callchain) {
  383. syms = calloc(chain->nr, sizeof(*syms));
  384. if (!syms) {
  385. fprintf(stderr, "Can't allocate memory for symbols\n");
  386. exit(-1);
  387. }
  388. }
  389. for (i = 0; i < chain->nr; i++) {
  390. u64 ip = chain->ips[i];
  391. struct symbol *sym = NULL;
  392. if (ip >= PERF_CONTEXT_MAX) {
  393. context = ip;
  394. continue;
  395. }
  396. switch (context) {
  397. case PERF_CONTEXT_HV:
  398. break;
  399. case PERF_CONTEXT_KERNEL:
  400. sym = kernel_maps__find_symbol(ip, &map);
  401. break;
  402. default:
  403. sym = resolve_symbol(thread, &map, &ip);
  404. break;
  405. }
  406. if (sym) {
  407. if (sort__has_parent && !*parent && call__match(sym))
  408. *parent = sym;
  409. if (!callchain)
  410. break;
  411. syms[i] = sym;
  412. }
  413. }
  414. return syms;
  415. }
  416. /*
  417. * collect histogram counts
  418. */
  419. static int
  420. hist_entry__add(struct thread *thread, struct map *map,
  421. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  422. char level, u64 count)
  423. {
  424. struct symbol **syms = NULL, *parent = NULL;
  425. bool hit;
  426. struct hist_entry *he;
  427. if ((sort__has_parent || callchain) && chain)
  428. syms = resolve_callchain(thread, map, chain, &parent);
  429. he = __hist_entry__add(thread, map, sym, parent,
  430. ip, count, level, &hit);
  431. if (he == NULL)
  432. return -ENOMEM;
  433. if (hit)
  434. he->count += count;
  435. if (callchain) {
  436. if (!hit)
  437. callchain_init(&he->callchain);
  438. append_chain(&he->callchain, chain, syms);
  439. free(syms);
  440. }
  441. return 0;
  442. }
  443. static size_t output__fprintf(FILE *fp, u64 total_samples)
  444. {
  445. struct hist_entry *pos;
  446. struct sort_entry *se;
  447. struct rb_node *nd;
  448. size_t ret = 0;
  449. unsigned int width;
  450. char *col_width = col_width_list_str;
  451. int raw_printing_style;
  452. raw_printing_style = !strcmp(pretty_printing_style, "raw");
  453. init_rem_hits();
  454. fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
  455. fprintf(fp, "#\n");
  456. fprintf(fp, "# Overhead");
  457. if (show_nr_samples) {
  458. if (field_sep)
  459. fprintf(fp, "%cSamples", *field_sep);
  460. else
  461. fputs(" Samples ", fp);
  462. }
  463. list_for_each_entry(se, &hist_entry__sort_list, list) {
  464. if (se->elide)
  465. continue;
  466. if (field_sep) {
  467. fprintf(fp, "%c%s", *field_sep, se->header);
  468. continue;
  469. }
  470. width = strlen(se->header);
  471. if (se->width) {
  472. if (col_width_list_str) {
  473. if (col_width) {
  474. *se->width = atoi(col_width);
  475. col_width = strchr(col_width, ',');
  476. if (col_width)
  477. ++col_width;
  478. }
  479. }
  480. width = *se->width = max(*se->width, width);
  481. }
  482. fprintf(fp, " %*s", width, se->header);
  483. }
  484. fprintf(fp, "\n");
  485. if (field_sep)
  486. goto print_entries;
  487. fprintf(fp, "# ........");
  488. if (show_nr_samples)
  489. fprintf(fp, " ..........");
  490. list_for_each_entry(se, &hist_entry__sort_list, list) {
  491. unsigned int i;
  492. if (se->elide)
  493. continue;
  494. fprintf(fp, " ");
  495. if (se->width)
  496. width = *se->width;
  497. else
  498. width = strlen(se->header);
  499. for (i = 0; i < width; i++)
  500. fprintf(fp, ".");
  501. }
  502. fprintf(fp, "\n");
  503. fprintf(fp, "#\n");
  504. print_entries:
  505. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  506. pos = rb_entry(nd, struct hist_entry, rb_node);
  507. ret += hist_entry__fprintf(fp, pos, total_samples);
  508. }
  509. if (sort_order == default_sort_order &&
  510. parent_pattern == default_parent_pattern) {
  511. fprintf(fp, "#\n");
  512. fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
  513. fprintf(fp, "#\n");
  514. }
  515. fprintf(fp, "\n");
  516. free(rem_sq_bracket);
  517. if (show_threads)
  518. perf_read_values_display(fp, &show_threads_values,
  519. raw_printing_style);
  520. return ret;
  521. }
  522. static int validate_chain(struct ip_callchain *chain, event_t *event)
  523. {
  524. unsigned int chain_size;
  525. chain_size = event->header.size;
  526. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  527. if (chain->nr*sizeof(u64) > chain_size)
  528. return -1;
  529. return 0;
  530. }
  531. static int
  532. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  533. {
  534. char level;
  535. struct symbol *sym = NULL;
  536. u64 ip = event->ip.ip;
  537. u64 period = 1;
  538. struct map *map = NULL;
  539. void *more_data = event->ip.__more_data;
  540. struct ip_callchain *chain = NULL;
  541. int cpumode;
  542. struct thread *thread = threads__findnew(event->ip.pid);
  543. if (sample_type & PERF_SAMPLE_PERIOD) {
  544. period = *(u64 *)more_data;
  545. more_data += sizeof(u64);
  546. }
  547. dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
  548. (void *)(offset + head),
  549. (void *)(long)(event->header.size),
  550. event->header.misc,
  551. event->ip.pid, event->ip.tid,
  552. (void *)(long)ip,
  553. (long long)period);
  554. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  555. unsigned int i;
  556. chain = (void *)more_data;
  557. dump_printf("... chain: nr:%Lu\n", chain->nr);
  558. if (validate_chain(chain, event) < 0) {
  559. eprintf("call-chain problem with event, skipping it.\n");
  560. return 0;
  561. }
  562. if (dump_trace) {
  563. for (i = 0; i < chain->nr; i++)
  564. dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
  565. }
  566. }
  567. if (thread == NULL) {
  568. eprintf("problem processing %d event, skipping it.\n",
  569. event->header.type);
  570. return -1;
  571. }
  572. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  573. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  574. return 0;
  575. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  576. if (cpumode == PERF_RECORD_MISC_KERNEL) {
  577. level = 'k';
  578. sym = kernel_maps__find_symbol(ip, &map);
  579. dump_printf(" ...... dso: %s\n",
  580. map ? map->dso->long_name : "<not found>");
  581. } else if (cpumode == PERF_RECORD_MISC_USER) {
  582. level = '.';
  583. sym = resolve_symbol(thread, &map, &ip);
  584. } else {
  585. level = 'H';
  586. dump_printf(" ...... dso: [hypervisor]\n");
  587. }
  588. if (dso_list &&
  589. (!map || !map->dso ||
  590. !(strlist__has_entry(dso_list, map->dso->short_name) ||
  591. (map->dso->short_name != map->dso->long_name &&
  592. strlist__has_entry(dso_list, map->dso->long_name)))))
  593. return 0;
  594. if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
  595. return 0;
  596. if (hist_entry__add(thread, map, sym, ip,
  597. chain, level, period)) {
  598. eprintf("problem incrementing symbol count, skipping event\n");
  599. return -1;
  600. }
  601. total += period;
  602. return 0;
  603. }
  604. static int
  605. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  606. {
  607. struct map *map = map__new(&event->mmap, cwd, cwdlen, 0, NULL, verbose);
  608. struct thread *thread = threads__findnew(event->mmap.pid);
  609. dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
  610. (void *)(offset + head),
  611. (void *)(long)(event->header.size),
  612. event->mmap.pid,
  613. event->mmap.tid,
  614. (void *)(long)event->mmap.start,
  615. (void *)(long)event->mmap.len,
  616. (void *)(long)event->mmap.pgoff,
  617. event->mmap.filename);
  618. if (thread == NULL || map == NULL) {
  619. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  620. return 0;
  621. }
  622. thread__insert_map(thread, map);
  623. total_mmap++;
  624. return 0;
  625. }
  626. static int
  627. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  628. {
  629. struct thread *thread = threads__findnew(event->comm.pid);
  630. dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
  631. (void *)(offset + head),
  632. (void *)(long)(event->header.size),
  633. event->comm.comm, event->comm.pid);
  634. if (thread == NULL ||
  635. thread__set_comm_adjust(thread, event->comm.comm)) {
  636. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  637. return -1;
  638. }
  639. total_comm++;
  640. return 0;
  641. }
  642. static int
  643. process_task_event(event_t *event, unsigned long offset, unsigned long head)
  644. {
  645. struct thread *thread = threads__findnew(event->fork.pid);
  646. struct thread *parent = threads__findnew(event->fork.ppid);
  647. dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
  648. (void *)(offset + head),
  649. (void *)(long)(event->header.size),
  650. event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
  651. event->fork.pid, event->fork.tid,
  652. event->fork.ppid, event->fork.ptid);
  653. /*
  654. * A thread clone will have the same PID for both
  655. * parent and child.
  656. */
  657. if (thread == parent)
  658. return 0;
  659. if (event->header.type == PERF_RECORD_EXIT)
  660. return 0;
  661. if (!thread || !parent || thread__fork(thread, parent)) {
  662. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  663. return -1;
  664. }
  665. total_fork++;
  666. return 0;
  667. }
  668. static int
  669. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  670. {
  671. dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
  672. (void *)(offset + head),
  673. (void *)(long)(event->header.size),
  674. event->lost.id,
  675. event->lost.lost);
  676. total_lost += event->lost.lost;
  677. return 0;
  678. }
  679. static int
  680. process_read_event(event_t *event, unsigned long offset, unsigned long head)
  681. {
  682. struct perf_event_attr *attr;
  683. attr = perf_header__find_attr(event->read.id, header);
  684. if (show_threads) {
  685. const char *name = attr ? __event_name(attr->type, attr->config)
  686. : "unknown";
  687. perf_read_values_add_value(&show_threads_values,
  688. event->read.pid, event->read.tid,
  689. event->read.id,
  690. name,
  691. event->read.value);
  692. }
  693. dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
  694. (void *)(offset + head),
  695. (void *)(long)(event->header.size),
  696. event->read.pid,
  697. event->read.tid,
  698. attr ? __event_name(attr->type, attr->config)
  699. : "FAIL",
  700. event->read.value);
  701. return 0;
  702. }
  703. static int sample_type_check(u64 type)
  704. {
  705. sample_type = type;
  706. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  707. if (sort__has_parent) {
  708. fprintf(stderr, "selected --sort parent, but no"
  709. " callchain data. Did you call"
  710. " perf record without -g?\n");
  711. return -1;
  712. }
  713. if (callchain) {
  714. fprintf(stderr, "selected -g but no callchain data."
  715. " Did you call perf record without"
  716. " -g?\n");
  717. return -1;
  718. }
  719. } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
  720. callchain = 1;
  721. if (register_callchain_param(&callchain_param) < 0) {
  722. fprintf(stderr, "Can't register callchain"
  723. " params\n");
  724. return -1;
  725. }
  726. }
  727. return 0;
  728. }
  729. static struct perf_file_handler file_handler = {
  730. .process_sample_event = process_sample_event,
  731. .process_mmap_event = process_mmap_event,
  732. .process_comm_event = process_comm_event,
  733. .process_exit_event = process_task_event,
  734. .process_fork_event = process_task_event,
  735. .process_lost_event = process_lost_event,
  736. .process_read_event = process_read_event,
  737. .sample_type_check = sample_type_check,
  738. };
  739. static int __cmd_report(void)
  740. {
  741. struct thread *idle;
  742. int ret;
  743. idle = register_idle_thread();
  744. thread__comm_adjust(idle);
  745. if (show_threads)
  746. perf_read_values_init(&show_threads_values);
  747. register_perf_file_handler(&file_handler);
  748. ret = mmap_dispatch_perf_file(&header, input_name, force, full_paths,
  749. &cwdlen, &cwd);
  750. if (ret)
  751. return ret;
  752. dump_printf(" IP events: %10ld\n", total);
  753. dump_printf(" mmap events: %10ld\n", total_mmap);
  754. dump_printf(" comm events: %10ld\n", total_comm);
  755. dump_printf(" fork events: %10ld\n", total_fork);
  756. dump_printf(" lost events: %10ld\n", total_lost);
  757. dump_printf(" unknown events: %10ld\n", file_handler.total_unknown);
  758. if (dump_trace)
  759. return 0;
  760. if (verbose > 3)
  761. threads__fprintf(stdout);
  762. if (verbose > 2)
  763. dsos__fprintf(stdout);
  764. collapse__resort();
  765. output__resort(total);
  766. output__fprintf(stdout, total);
  767. if (show_threads)
  768. perf_read_values_destroy(&show_threads_values);
  769. return ret;
  770. }
  771. static int
  772. parse_callchain_opt(const struct option *opt __used, const char *arg,
  773. int unset __used)
  774. {
  775. char *tok;
  776. char *endptr;
  777. callchain = 1;
  778. if (!arg)
  779. return 0;
  780. tok = strtok((char *)arg, ",");
  781. if (!tok)
  782. return -1;
  783. /* get the output mode */
  784. if (!strncmp(tok, "graph", strlen(arg)))
  785. callchain_param.mode = CHAIN_GRAPH_ABS;
  786. else if (!strncmp(tok, "flat", strlen(arg)))
  787. callchain_param.mode = CHAIN_FLAT;
  788. else if (!strncmp(tok, "fractal", strlen(arg)))
  789. callchain_param.mode = CHAIN_GRAPH_REL;
  790. else if (!strncmp(tok, "none", strlen(arg))) {
  791. callchain_param.mode = CHAIN_NONE;
  792. callchain = 0;
  793. return 0;
  794. }
  795. else
  796. return -1;
  797. /* get the min percentage */
  798. tok = strtok(NULL, ",");
  799. if (!tok)
  800. goto setup;
  801. callchain_param.min_percent = strtod(tok, &endptr);
  802. if (tok == endptr)
  803. return -1;
  804. setup:
  805. if (register_callchain_param(&callchain_param) < 0) {
  806. fprintf(stderr, "Can't register callchain params\n");
  807. return -1;
  808. }
  809. return 0;
  810. }
  811. //static const char * const report_usage[] = {
  812. const char * const report_usage[] = {
  813. "perf report [<options>] <command>",
  814. NULL
  815. };
  816. static const struct option options[] = {
  817. OPT_STRING('i', "input", &input_name, "file",
  818. "input file name"),
  819. OPT_BOOLEAN('v', "verbose", &verbose,
  820. "be more verbose (show symbol address, etc)"),
  821. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  822. "dump raw trace in ASCII"),
  823. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  824. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  825. OPT_BOOLEAN('m', "modules", &modules,
  826. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  827. OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
  828. "Show a column with the number of samples"),
  829. OPT_BOOLEAN('T', "threads", &show_threads,
  830. "Show per-thread event counters"),
  831. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  832. "pretty printing style key: normal raw"),
  833. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  834. "sort by key(s): pid, comm, dso, symbol, parent"),
  835. OPT_BOOLEAN('P', "full-paths", &full_paths,
  836. "Don't shorten the pathnames taking into account the cwd"),
  837. OPT_STRING('p', "parent", &parent_pattern, "regex",
  838. "regex filter to identify parent, see: '--sort parent'"),
  839. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  840. "Only display entries with parent-match"),
  841. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  842. "Display callchains using output_type and min percent threshold. "
  843. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  844. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  845. "only consider symbols in these dsos"),
  846. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  847. "only consider symbols in these comms"),
  848. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  849. "only consider these symbols"),
  850. OPT_STRING('w', "column-widths", &col_width_list_str,
  851. "width[,width...]",
  852. "don't try to adjust column width, use these fixed values"),
  853. OPT_STRING('t', "field-separator", &field_sep, "separator",
  854. "separator for columns, no spaces will be added between "
  855. "columns '.' is reserved."),
  856. OPT_END()
  857. };
  858. static void setup_sorting(void)
  859. {
  860. char *tmp, *tok, *str = strdup(sort_order);
  861. for (tok = strtok_r(str, ", ", &tmp);
  862. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  863. if (sort_dimension__add(tok) < 0) {
  864. error("Unknown --sort key: `%s'", tok);
  865. usage_with_options(report_usage, options);
  866. }
  867. }
  868. free(str);
  869. }
  870. static void setup_list(struct strlist **list, const char *list_str,
  871. struct sort_entry *se, const char *list_name,
  872. FILE *fp)
  873. {
  874. if (list_str) {
  875. *list = strlist__new(true, list_str);
  876. if (!*list) {
  877. fprintf(stderr, "problems parsing %s list\n",
  878. list_name);
  879. exit(129);
  880. }
  881. if (strlist__nr_entries(*list) == 1) {
  882. fprintf(fp, "# %s: %s\n", list_name,
  883. strlist__entry(*list, 0)->s);
  884. se->elide = true;
  885. }
  886. }
  887. }
  888. int cmd_report(int argc, const char **argv, const char *prefix __used)
  889. {
  890. symbol__init();
  891. argc = parse_options(argc, argv, options, report_usage, 0);
  892. setup_sorting();
  893. if (parent_pattern != default_parent_pattern) {
  894. sort_dimension__add("parent");
  895. sort_parent.elide = 1;
  896. } else
  897. exclude_other = 0;
  898. /*
  899. * Any (unrecognized) arguments left?
  900. */
  901. if (argc)
  902. usage_with_options(report_usage, options);
  903. setup_pager();
  904. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  905. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  906. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  907. if (field_sep && *field_sep == '.') {
  908. fputs("'.' is the only non valid --field-separator argument\n",
  909. stderr);
  910. exit(129);
  911. }
  912. return __cmd_report();
  913. }