builtin-report.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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 bool use_callchain;
  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 hist_entry__fprintf(FILE *fp, struct hist_entry *self,
  254. struct perf_session *session,
  255. 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 (session->use_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 perf_session *session,
  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 (session->use_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, session, cpumode,
  364. MAP__FUNCTION, 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 (!session->use_callchain)
  370. break;
  371. syms[i] = al.sym;
  372. }
  373. }
  374. return syms;
  375. }
  376. /*
  377. * collect histogram counts
  378. */
  379. static int perf_session__add_hist_entry(struct perf_session *self,
  380. struct addr_location *al,
  381. struct ip_callchain *chain, u64 count)
  382. {
  383. struct symbol **syms = NULL, *parent = NULL;
  384. bool hit;
  385. struct hist_entry *he;
  386. if ((sort__has_parent || self->use_callchain) && chain)
  387. syms = resolve_callchain(al->thread, self, chain, &parent);
  388. he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
  389. if (he == NULL)
  390. return -ENOMEM;
  391. if (hit)
  392. he->count += count;
  393. if (self->use_callchain) {
  394. if (!hit)
  395. callchain_init(&he->callchain);
  396. append_chain(&he->callchain, chain, syms);
  397. free(syms);
  398. }
  399. return 0;
  400. }
  401. static size_t perf_session__fprintf_hist_entries(struct perf_session *self,
  402. u64 total_samples, FILE *fp)
  403. {
  404. struct hist_entry *pos;
  405. struct sort_entry *se;
  406. struct rb_node *nd;
  407. size_t ret = 0;
  408. unsigned int width;
  409. char *col_width = col_width_list_str;
  410. int raw_printing_style;
  411. raw_printing_style = !strcmp(pretty_printing_style, "raw");
  412. init_rem_hits();
  413. fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
  414. fprintf(fp, "#\n");
  415. fprintf(fp, "# Overhead");
  416. if (show_nr_samples) {
  417. if (field_sep)
  418. fprintf(fp, "%cSamples", *field_sep);
  419. else
  420. fputs(" Samples ", fp);
  421. }
  422. list_for_each_entry(se, &hist_entry__sort_list, list) {
  423. if (se->elide)
  424. continue;
  425. if (field_sep) {
  426. fprintf(fp, "%c%s", *field_sep, se->header);
  427. continue;
  428. }
  429. width = strlen(se->header);
  430. if (se->width) {
  431. if (col_width_list_str) {
  432. if (col_width) {
  433. *se->width = atoi(col_width);
  434. col_width = strchr(col_width, ',');
  435. if (col_width)
  436. ++col_width;
  437. }
  438. }
  439. width = *se->width = max(*se->width, width);
  440. }
  441. fprintf(fp, " %*s", width, se->header);
  442. }
  443. fprintf(fp, "\n");
  444. if (field_sep)
  445. goto print_entries;
  446. fprintf(fp, "# ........");
  447. if (show_nr_samples)
  448. fprintf(fp, " ..........");
  449. list_for_each_entry(se, &hist_entry__sort_list, list) {
  450. unsigned int i;
  451. if (se->elide)
  452. continue;
  453. fprintf(fp, " ");
  454. if (se->width)
  455. width = *se->width;
  456. else
  457. width = strlen(se->header);
  458. for (i = 0; i < width; i++)
  459. fprintf(fp, ".");
  460. }
  461. fprintf(fp, "\n");
  462. fprintf(fp, "#\n");
  463. print_entries:
  464. for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
  465. pos = rb_entry(nd, struct hist_entry, rb_node);
  466. ret += hist_entry__fprintf(fp, pos, self, total_samples);
  467. }
  468. if (sort_order == default_sort_order &&
  469. parent_pattern == default_parent_pattern) {
  470. fprintf(fp, "#\n");
  471. fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
  472. fprintf(fp, "#\n");
  473. }
  474. fprintf(fp, "\n");
  475. free(rem_sq_bracket);
  476. if (show_threads)
  477. perf_read_values_display(fp, &show_threads_values,
  478. raw_printing_style);
  479. return ret;
  480. }
  481. static int validate_chain(struct ip_callchain *chain, event_t *event)
  482. {
  483. unsigned int chain_size;
  484. chain_size = event->header.size;
  485. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  486. if (chain->nr*sizeof(u64) > chain_size)
  487. return -1;
  488. return 0;
  489. }
  490. static int process_sample_event(event_t *event, struct perf_session *session)
  491. {
  492. struct sample_data data;
  493. int cpumode;
  494. struct addr_location al;
  495. struct thread *thread;
  496. memset(&data, 0, sizeof(data));
  497. data.period = 1;
  498. event__parse_sample(event, sample_type, &data);
  499. dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
  500. event->header.misc,
  501. data.pid, data.tid,
  502. (void *)(long)data.ip,
  503. (long long)data.period);
  504. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  505. unsigned int i;
  506. dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
  507. if (validate_chain(data.callchain, event) < 0) {
  508. pr_debug("call-chain problem with event, "
  509. "skipping it.\n");
  510. return 0;
  511. }
  512. if (dump_trace) {
  513. for (i = 0; i < data.callchain->nr; i++)
  514. dump_printf("..... %2d: %016Lx\n",
  515. i, data.callchain->ips[i]);
  516. }
  517. }
  518. thread = perf_session__findnew(session, data.pid);
  519. if (thread == NULL) {
  520. pr_debug("problem processing %d event, skipping it.\n",
  521. event->header.type);
  522. return -1;
  523. }
  524. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  525. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  526. return 0;
  527. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  528. thread__find_addr_location(thread, session, cpumode,
  529. MAP__FUNCTION, data.ip, &al, NULL);
  530. /*
  531. * We have to do this here as we may have a dso with no symbol hit that
  532. * has a name longer than the ones with symbols sampled.
  533. */
  534. if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
  535. dso__calc_col_width(al.map->dso);
  536. if (dso_list &&
  537. (!al.map || !al.map->dso ||
  538. !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
  539. (al.map->dso->short_name != al.map->dso->long_name &&
  540. strlist__has_entry(dso_list, al.map->dso->long_name)))))
  541. return 0;
  542. if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
  543. return 0;
  544. if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
  545. pr_debug("problem incrementing symbol count, skipping event\n");
  546. return -1;
  547. }
  548. event__stats.total += data.period;
  549. return 0;
  550. }
  551. static int process_comm_event(event_t *event, struct perf_session *session)
  552. {
  553. struct thread *thread = perf_session__findnew(session, event->comm.pid);
  554. dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
  555. if (thread == NULL ||
  556. thread__set_comm_adjust(thread, event->comm.comm)) {
  557. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  558. return -1;
  559. }
  560. return 0;
  561. }
  562. static int process_read_event(event_t *event, struct perf_session *session __used)
  563. {
  564. struct perf_event_attr *attr;
  565. attr = perf_header__find_attr(event->read.id, &session->header);
  566. if (show_threads) {
  567. const char *name = attr ? __event_name(attr->type, attr->config)
  568. : "unknown";
  569. perf_read_values_add_value(&show_threads_values,
  570. event->read.pid, event->read.tid,
  571. event->read.id,
  572. name,
  573. event->read.value);
  574. }
  575. dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
  576. attr ? __event_name(attr->type, attr->config) : "FAIL",
  577. event->read.value);
  578. return 0;
  579. }
  580. static int sample_type_check(u64 type, struct perf_session *session)
  581. {
  582. sample_type = type;
  583. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  584. if (sort__has_parent) {
  585. fprintf(stderr, "selected --sort parent, but no"
  586. " callchain data. Did you call"
  587. " perf record without -g?\n");
  588. return -1;
  589. }
  590. if (session->use_callchain) {
  591. fprintf(stderr, "selected -g but no callchain data."
  592. " Did you call perf record without"
  593. " -g?\n");
  594. return -1;
  595. }
  596. } else if (callchain_param.mode != CHAIN_NONE && !session->use_callchain) {
  597. session->use_callchain = true;
  598. if (register_callchain_param(&callchain_param) < 0) {
  599. fprintf(stderr, "Can't register callchain"
  600. " params\n");
  601. return -1;
  602. }
  603. }
  604. return 0;
  605. }
  606. static struct perf_event_ops event_ops = {
  607. .process_sample_event = process_sample_event,
  608. .process_mmap_event = event__process_mmap,
  609. .process_comm_event = process_comm_event,
  610. .process_exit_event = event__process_task,
  611. .process_fork_event = event__process_task,
  612. .process_lost_event = event__process_lost,
  613. .process_read_event = process_read_event,
  614. .sample_type_check = sample_type_check,
  615. };
  616. static int __cmd_report(void)
  617. {
  618. int ret;
  619. struct perf_session *session;
  620. session = perf_session__new(input_name, O_RDONLY, force, &symbol_conf);
  621. if (session == NULL)
  622. return -ENOMEM;
  623. session->use_callchain = use_callchain;
  624. if (show_threads)
  625. perf_read_values_init(&show_threads_values);
  626. ret = perf_session__process_events(session, &event_ops);
  627. if (ret)
  628. goto out_delete;
  629. if (dump_trace) {
  630. event__print_totals();
  631. goto out_delete;
  632. }
  633. if (verbose > 3)
  634. perf_session__fprintf(session, stdout);
  635. if (verbose > 2)
  636. dsos__fprintf(stdout);
  637. perf_session__collapse_resort(session);
  638. perf_session__output_resort(session, event__stats.total);
  639. perf_session__fprintf_hist_entries(session, event__stats.total, stdout);
  640. if (show_threads)
  641. perf_read_values_destroy(&show_threads_values);
  642. out_delete:
  643. perf_session__delete(session);
  644. return ret;
  645. }
  646. static int
  647. parse_callchain_opt(const struct option *opt __used, const char *arg,
  648. int unset __used)
  649. {
  650. char *tok;
  651. char *endptr;
  652. use_callchain = true;
  653. if (!arg)
  654. return 0;
  655. tok = strtok((char *)arg, ",");
  656. if (!tok)
  657. return -1;
  658. /* get the output mode */
  659. if (!strncmp(tok, "graph", strlen(arg)))
  660. callchain_param.mode = CHAIN_GRAPH_ABS;
  661. else if (!strncmp(tok, "flat", strlen(arg)))
  662. callchain_param.mode = CHAIN_FLAT;
  663. else if (!strncmp(tok, "fractal", strlen(arg)))
  664. callchain_param.mode = CHAIN_GRAPH_REL;
  665. else if (!strncmp(tok, "none", strlen(arg))) {
  666. callchain_param.mode = CHAIN_NONE;
  667. use_callchain = true;
  668. return 0;
  669. }
  670. else
  671. return -1;
  672. /* get the min percentage */
  673. tok = strtok(NULL, ",");
  674. if (!tok)
  675. goto setup;
  676. callchain_param.min_percent = strtod(tok, &endptr);
  677. if (tok == endptr)
  678. return -1;
  679. setup:
  680. if (register_callchain_param(&callchain_param) < 0) {
  681. fprintf(stderr, "Can't register callchain params\n");
  682. return -1;
  683. }
  684. return 0;
  685. }
  686. //static const char * const report_usage[] = {
  687. const char * const report_usage[] = {
  688. "perf report [<options>] <command>",
  689. NULL
  690. };
  691. static const struct option options[] = {
  692. OPT_STRING('i', "input", &input_name, "file",
  693. "input file name"),
  694. OPT_BOOLEAN('v', "verbose", &verbose,
  695. "be more verbose (show symbol address, etc)"),
  696. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  697. "dump raw trace in ASCII"),
  698. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  699. "file", "vmlinux pathname"),
  700. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  701. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  702. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  703. OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
  704. "Show a column with the number of samples"),
  705. OPT_BOOLEAN('T', "threads", &show_threads,
  706. "Show per-thread event counters"),
  707. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  708. "pretty printing style key: normal raw"),
  709. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  710. "sort by key(s): pid, comm, dso, symbol, parent"),
  711. OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
  712. "Don't shorten the pathnames taking into account the cwd"),
  713. OPT_STRING('p', "parent", &parent_pattern, "regex",
  714. "regex filter to identify parent, see: '--sort parent'"),
  715. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  716. "Only display entries with parent-match"),
  717. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  718. "Display callchains using output_type and min percent threshold. "
  719. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  720. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  721. "only consider symbols in these dsos"),
  722. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  723. "only consider symbols in these comms"),
  724. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  725. "only consider these symbols"),
  726. OPT_STRING('w', "column-widths", &col_width_list_str,
  727. "width[,width...]",
  728. "don't try to adjust column width, use these fixed values"),
  729. OPT_STRING('t', "field-separator", &field_sep, "separator",
  730. "separator for columns, no spaces will be added between "
  731. "columns '.' is reserved."),
  732. OPT_END()
  733. };
  734. static void setup_sorting(void)
  735. {
  736. char *tmp, *tok, *str = strdup(sort_order);
  737. for (tok = strtok_r(str, ", ", &tmp);
  738. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  739. if (sort_dimension__add(tok) < 0) {
  740. error("Unknown --sort key: `%s'", tok);
  741. usage_with_options(report_usage, options);
  742. }
  743. }
  744. free(str);
  745. }
  746. static void setup_list(struct strlist **list, const char *list_str,
  747. struct sort_entry *se, const char *list_name,
  748. FILE *fp)
  749. {
  750. if (list_str) {
  751. *list = strlist__new(true, list_str);
  752. if (!*list) {
  753. fprintf(stderr, "problems parsing %s list\n",
  754. list_name);
  755. exit(129);
  756. }
  757. if (strlist__nr_entries(*list) == 1) {
  758. fprintf(fp, "# %s: %s\n", list_name,
  759. strlist__entry(*list, 0)->s);
  760. se->elide = true;
  761. }
  762. }
  763. }
  764. int cmd_report(int argc, const char **argv, const char *prefix __used)
  765. {
  766. if (symbol__init(&symbol_conf) < 0)
  767. return -1;
  768. argc = parse_options(argc, argv, options, report_usage, 0);
  769. setup_sorting();
  770. if (parent_pattern != default_parent_pattern) {
  771. sort_dimension__add("parent");
  772. sort_parent.elide = 1;
  773. } else
  774. exclude_other = 0;
  775. /*
  776. * Any (unrecognized) arguments left?
  777. */
  778. if (argc)
  779. usage_with_options(report_usage, options);
  780. setup_pager();
  781. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  782. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  783. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  784. if (field_sep && *field_sep == '.') {
  785. fputs("'.' is the only non valid --field-separator argument\n",
  786. stderr);
  787. exit(129);
  788. }
  789. return __cmd_report();
  790. }