builtin-report.c 21 KB

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