builtin-report.c 26 KB

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