builtin-report.c 22 KB

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