builtin-report.c 25 KB

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