builtin-report.c 25 KB

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