builtin-report.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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 show_nr_samples;
  34. static int show_threads;
  35. static struct perf_read_values show_threads_values;
  36. static char default_pretty_printing_style[] = "normal";
  37. static char *pretty_printing_style = default_pretty_printing_style;
  38. static int exclude_other = 1;
  39. static char callchain_default_opt[] = "fractal,0.5";
  40. static u64 sample_type;
  41. struct symbol_conf symbol_conf;
  42. static size_t
  43. callchain__fprintf_left_margin(FILE *fp, int left_margin)
  44. {
  45. int i;
  46. int ret;
  47. ret = fprintf(fp, " ");
  48. for (i = 0; i < left_margin; i++)
  49. ret += fprintf(fp, " ");
  50. return ret;
  51. }
  52. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  53. int left_margin)
  54. {
  55. int i;
  56. size_t ret = 0;
  57. ret += callchain__fprintf_left_margin(fp, left_margin);
  58. for (i = 0; i < depth; i++)
  59. if (depth_mask & (1 << i))
  60. ret += fprintf(fp, "| ");
  61. else
  62. ret += fprintf(fp, " ");
  63. ret += fprintf(fp, "\n");
  64. return ret;
  65. }
  66. static size_t
  67. ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
  68. int depth_mask, int count, u64 total_samples,
  69. int hits, int left_margin)
  70. {
  71. int i;
  72. size_t ret = 0;
  73. ret += callchain__fprintf_left_margin(fp, left_margin);
  74. for (i = 0; i < depth; i++) {
  75. if (depth_mask & (1 << i))
  76. ret += fprintf(fp, "|");
  77. else
  78. ret += fprintf(fp, " ");
  79. if (!count && i == depth - 1) {
  80. double percent;
  81. percent = hits * 100.0 / total_samples;
  82. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  83. } else
  84. ret += fprintf(fp, "%s", " ");
  85. }
  86. if (chain->sym)
  87. ret += fprintf(fp, "%s\n", chain->sym->name);
  88. else
  89. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  90. return ret;
  91. }
  92. static struct symbol *rem_sq_bracket;
  93. static struct callchain_list rem_hits;
  94. static void init_rem_hits(void)
  95. {
  96. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  97. if (!rem_sq_bracket) {
  98. fprintf(stderr, "Not enough memory to display remaining hits\n");
  99. return;
  100. }
  101. strcpy(rem_sq_bracket->name, "[...]");
  102. rem_hits.sym = rem_sq_bracket;
  103. }
  104. static size_t
  105. __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  106. u64 total_samples, int depth, int depth_mask,
  107. int left_margin)
  108. {
  109. struct rb_node *node, *next;
  110. struct callchain_node *child;
  111. struct callchain_list *chain;
  112. int new_depth_mask = depth_mask;
  113. u64 new_total;
  114. u64 remaining;
  115. size_t ret = 0;
  116. int i;
  117. if (callchain_param.mode == CHAIN_GRAPH_REL)
  118. new_total = self->children_hit;
  119. else
  120. new_total = total_samples;
  121. remaining = new_total;
  122. node = rb_first(&self->rb_root);
  123. while (node) {
  124. u64 cumul;
  125. child = rb_entry(node, struct callchain_node, rb_node);
  126. cumul = cumul_hits(child);
  127. remaining -= cumul;
  128. /*
  129. * The depth mask manages the output of pipes that show
  130. * the depth. We don't want to keep the pipes of the current
  131. * level for the last child of this depth.
  132. * Except if we have remaining filtered hits. They will
  133. * supersede the last child
  134. */
  135. next = rb_next(node);
  136. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  137. new_depth_mask &= ~(1 << (depth - 1));
  138. /*
  139. * But we keep the older depth mask for the line seperator
  140. * to keep the level link until we reach the last child
  141. */
  142. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  143. left_margin);
  144. i = 0;
  145. list_for_each_entry(chain, &child->val, list) {
  146. if (chain->ip >= PERF_CONTEXT_MAX)
  147. continue;
  148. ret += ipchain__fprintf_graph(fp, chain, depth,
  149. new_depth_mask, i++,
  150. new_total,
  151. cumul,
  152. left_margin);
  153. }
  154. ret += __callchain__fprintf_graph(fp, child, new_total,
  155. depth + 1,
  156. new_depth_mask | (1 << depth),
  157. left_margin);
  158. node = next;
  159. }
  160. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  161. remaining && remaining != new_total) {
  162. if (!rem_sq_bracket)
  163. return ret;
  164. new_depth_mask &= ~(1 << (depth - 1));
  165. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  166. new_depth_mask, 0, new_total,
  167. remaining, left_margin);
  168. }
  169. return ret;
  170. }
  171. static size_t
  172. callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  173. u64 total_samples, int left_margin)
  174. {
  175. struct callchain_list *chain;
  176. bool printed = false;
  177. int i = 0;
  178. int ret = 0;
  179. list_for_each_entry(chain, &self->val, list) {
  180. if (chain->ip >= PERF_CONTEXT_MAX)
  181. continue;
  182. if (!i++ && sort__first_dimension == SORT_SYM)
  183. continue;
  184. if (!printed) {
  185. ret += callchain__fprintf_left_margin(fp, left_margin);
  186. ret += fprintf(fp, "|\n");
  187. ret += callchain__fprintf_left_margin(fp, left_margin);
  188. ret += fprintf(fp, "---");
  189. left_margin += 3;
  190. printed = true;
  191. } else
  192. ret += callchain__fprintf_left_margin(fp, left_margin);
  193. if (chain->sym)
  194. ret += fprintf(fp, " %s\n", chain->sym->name);
  195. else
  196. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  197. }
  198. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  199. return ret;
  200. }
  201. static size_t
  202. callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  203. u64 total_samples)
  204. {
  205. struct callchain_list *chain;
  206. size_t ret = 0;
  207. if (!self)
  208. return 0;
  209. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  210. list_for_each_entry(chain, &self->val, list) {
  211. if (chain->ip >= PERF_CONTEXT_MAX)
  212. continue;
  213. if (chain->sym)
  214. ret += fprintf(fp, " %s\n", chain->sym->name);
  215. else
  216. ret += fprintf(fp, " %p\n",
  217. (void *)(long)chain->ip);
  218. }
  219. return ret;
  220. }
  221. static size_t
  222. hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  223. u64 total_samples, int left_margin)
  224. {
  225. struct rb_node *rb_node;
  226. struct callchain_node *chain;
  227. size_t ret = 0;
  228. rb_node = rb_first(&self->sorted_chain);
  229. while (rb_node) {
  230. double percent;
  231. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  232. percent = chain->hit * 100.0 / total_samples;
  233. switch (callchain_param.mode) {
  234. case CHAIN_FLAT:
  235. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  236. percent);
  237. ret += callchain__fprintf_flat(fp, chain, total_samples);
  238. break;
  239. case CHAIN_GRAPH_ABS: /* Falldown */
  240. case CHAIN_GRAPH_REL:
  241. ret += callchain__fprintf_graph(fp, chain, total_samples,
  242. left_margin);
  243. case CHAIN_NONE:
  244. default:
  245. break;
  246. }
  247. ret += fprintf(fp, "\n");
  248. rb_node = rb_next(rb_node);
  249. }
  250. return ret;
  251. }
  252. static size_t
  253. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  254. {
  255. struct sort_entry *se;
  256. size_t ret;
  257. if (exclude_other && !self->parent)
  258. return 0;
  259. if (total_samples)
  260. ret = percent_color_fprintf(fp,
  261. field_sep ? "%.2f" : " %6.2f%%",
  262. (self->count * 100.0) / total_samples);
  263. else
  264. ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
  265. if (show_nr_samples) {
  266. if (field_sep)
  267. fprintf(fp, "%c%lld", *field_sep, self->count);
  268. else
  269. fprintf(fp, "%11lld", self->count);
  270. }
  271. list_for_each_entry(se, &hist_entry__sort_list, list) {
  272. if (se->elide)
  273. continue;
  274. fprintf(fp, "%s", field_sep ?: " ");
  275. ret += se->print(fp, self, se->width ? *se->width : 0);
  276. }
  277. ret += fprintf(fp, "\n");
  278. if (callchain) {
  279. int left_margin = 0;
  280. if (sort__first_dimension == SORT_COMM) {
  281. se = list_first_entry(&hist_entry__sort_list, typeof(*se),
  282. list);
  283. left_margin = se->width ? *se->width : 0;
  284. left_margin -= thread__comm_len(self->thread);
  285. }
  286. hist_entry_callchain__fprintf(fp, self, total_samples,
  287. left_margin);
  288. }
  289. return ret;
  290. }
  291. /*
  292. *
  293. */
  294. static void dso__calc_col_width(struct dso *self)
  295. {
  296. if (!col_width_list_str && !field_sep &&
  297. (!dso_list || strlist__has_entry(dso_list, self->name))) {
  298. unsigned int slen = strlen(self->name);
  299. if (slen > dsos__col_width)
  300. dsos__col_width = slen;
  301. }
  302. self->slen_calculated = 1;
  303. }
  304. static void thread__comm_adjust(struct thread *self)
  305. {
  306. char *comm = self->comm;
  307. if (!col_width_list_str && !field_sep &&
  308. (!comm_list || strlist__has_entry(comm_list, comm))) {
  309. unsigned int slen = strlen(comm);
  310. if (slen > comms__col_width) {
  311. comms__col_width = slen;
  312. threads__col_width = slen + 6;
  313. }
  314. }
  315. }
  316. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  317. {
  318. int ret = thread__set_comm(self, comm);
  319. if (ret)
  320. return ret;
  321. thread__comm_adjust(self);
  322. return 0;
  323. }
  324. static int call__match(struct symbol *sym)
  325. {
  326. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  327. return 1;
  328. return 0;
  329. }
  330. static struct symbol **resolve_callchain(struct thread *thread,
  331. struct perf_session *session,
  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, session, cpumode,
  362. MAP__FUNCTION, 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 perf_session *session,
  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, session, 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(&hist); 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, struct perf_session *session)
  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 = perf_session__findnew(session, 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, session, 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, session, 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, struct perf_session *session)
  549. {
  550. struct thread *thread = perf_session__findnew(session, 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, struct perf_session *session __used)
  560. {
  561. struct perf_event_attr *attr;
  562. attr = perf_header__find_attr(event->read.id, &session->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_event_ops event_ops = {
  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. int ret;
  616. struct perf_session *session;
  617. session = perf_session__new(input_name, O_RDONLY, force, &symbol_conf);
  618. if (session == NULL)
  619. return -ENOMEM;
  620. if (show_threads)
  621. perf_read_values_init(&show_threads_values);
  622. ret = perf_session__process_events(session, &event_ops);
  623. if (ret)
  624. goto out_delete;
  625. if (dump_trace) {
  626. event__print_totals();
  627. goto out_delete;
  628. }
  629. if (verbose > 3)
  630. perf_session__fprintf(session, stdout);
  631. if (verbose > 2)
  632. dsos__fprintf(stdout);
  633. collapse__resort();
  634. output__resort(event__stats.total);
  635. output__fprintf(stdout, event__stats.total);
  636. if (show_threads)
  637. perf_read_values_destroy(&show_threads_values);
  638. out_delete:
  639. perf_session__delete(session);
  640. return ret;
  641. }
  642. static int
  643. parse_callchain_opt(const struct option *opt __used, const char *arg,
  644. int unset __used)
  645. {
  646. char *tok;
  647. char *endptr;
  648. callchain = 1;
  649. if (!arg)
  650. return 0;
  651. tok = strtok((char *)arg, ",");
  652. if (!tok)
  653. return -1;
  654. /* get the output mode */
  655. if (!strncmp(tok, "graph", strlen(arg)))
  656. callchain_param.mode = CHAIN_GRAPH_ABS;
  657. else if (!strncmp(tok, "flat", strlen(arg)))
  658. callchain_param.mode = CHAIN_FLAT;
  659. else if (!strncmp(tok, "fractal", strlen(arg)))
  660. callchain_param.mode = CHAIN_GRAPH_REL;
  661. else if (!strncmp(tok, "none", strlen(arg))) {
  662. callchain_param.mode = CHAIN_NONE;
  663. callchain = 0;
  664. return 0;
  665. }
  666. else
  667. return -1;
  668. /* get the min percentage */
  669. tok = strtok(NULL, ",");
  670. if (!tok)
  671. goto setup;
  672. callchain_param.min_percent = strtod(tok, &endptr);
  673. if (tok == endptr)
  674. return -1;
  675. setup:
  676. if (register_callchain_param(&callchain_param) < 0) {
  677. fprintf(stderr, "Can't register callchain params\n");
  678. return -1;
  679. }
  680. return 0;
  681. }
  682. //static const char * const report_usage[] = {
  683. const char * const report_usage[] = {
  684. "perf report [<options>] <command>",
  685. NULL
  686. };
  687. static const struct option options[] = {
  688. OPT_STRING('i', "input", &input_name, "file",
  689. "input file name"),
  690. OPT_BOOLEAN('v', "verbose", &verbose,
  691. "be more verbose (show symbol address, etc)"),
  692. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  693. "dump raw trace in ASCII"),
  694. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  695. "file", "vmlinux pathname"),
  696. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  697. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  698. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  699. OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
  700. "Show a column with the number of samples"),
  701. OPT_BOOLEAN('T', "threads", &show_threads,
  702. "Show per-thread event counters"),
  703. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  704. "pretty printing style key: normal raw"),
  705. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  706. "sort by key(s): pid, comm, dso, symbol, parent"),
  707. OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
  708. "Don't shorten the pathnames taking into account the cwd"),
  709. OPT_STRING('p', "parent", &parent_pattern, "regex",
  710. "regex filter to identify parent, see: '--sort parent'"),
  711. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  712. "Only display entries with parent-match"),
  713. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  714. "Display callchains using output_type and min percent threshold. "
  715. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  716. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  717. "only consider symbols in these dsos"),
  718. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  719. "only consider symbols in these comms"),
  720. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  721. "only consider these symbols"),
  722. OPT_STRING('w', "column-widths", &col_width_list_str,
  723. "width[,width...]",
  724. "don't try to adjust column width, use these fixed values"),
  725. OPT_STRING('t', "field-separator", &field_sep, "separator",
  726. "separator for columns, no spaces will be added between "
  727. "columns '.' is reserved."),
  728. OPT_END()
  729. };
  730. static void setup_sorting(void)
  731. {
  732. char *tmp, *tok, *str = strdup(sort_order);
  733. for (tok = strtok_r(str, ", ", &tmp);
  734. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  735. if (sort_dimension__add(tok) < 0) {
  736. error("Unknown --sort key: `%s'", tok);
  737. usage_with_options(report_usage, options);
  738. }
  739. }
  740. free(str);
  741. }
  742. static void setup_list(struct strlist **list, const char *list_str,
  743. struct sort_entry *se, const char *list_name,
  744. FILE *fp)
  745. {
  746. if (list_str) {
  747. *list = strlist__new(true, list_str);
  748. if (!*list) {
  749. fprintf(stderr, "problems parsing %s list\n",
  750. list_name);
  751. exit(129);
  752. }
  753. if (strlist__nr_entries(*list) == 1) {
  754. fprintf(fp, "# %s: %s\n", list_name,
  755. strlist__entry(*list, 0)->s);
  756. se->elide = true;
  757. }
  758. }
  759. }
  760. int cmd_report(int argc, const char **argv, const char *prefix __used)
  761. {
  762. if (symbol__init(&symbol_conf) < 0)
  763. return -1;
  764. argc = parse_options(argc, argv, options, report_usage, 0);
  765. setup_sorting();
  766. if (parent_pattern != default_parent_pattern) {
  767. sort_dimension__add("parent");
  768. sort_parent.elide = 1;
  769. } else
  770. exclude_other = 0;
  771. /*
  772. * Any (unrecognized) arguments left?
  773. */
  774. if (argc)
  775. usage_with_options(report_usage, options);
  776. setup_pager();
  777. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  778. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  779. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  780. if (field_sep && *field_sep == '.') {
  781. fputs("'.' is the only non valid --field-separator argument\n",
  782. stderr);
  783. exit(129);
  784. }
  785. return __cmd_report();
  786. }