builtin-report.c 25 KB

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