builtin-report.c 23 KB

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