builtin-report.c 22 KB

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