builtin-report.c 24 KB

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