builtin-report.c 23 KB

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