builtin-report.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 struct symbol_conf symbol_conf;
  42. static size_t
  43. callchain__fprintf_left_margin(FILE *fp, int left_margin)
  44. {
  45. int i;
  46. int ret;
  47. ret = fprintf(fp, " ");
  48. for (i = 0; i < left_margin; i++)
  49. ret += fprintf(fp, " ");
  50. return ret;
  51. }
  52. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  53. int left_margin)
  54. {
  55. int i;
  56. size_t ret = 0;
  57. ret += callchain__fprintf_left_margin(fp, left_margin);
  58. for (i = 0; i < depth; i++)
  59. if (depth_mask & (1 << i))
  60. ret += fprintf(fp, "| ");
  61. else
  62. ret += fprintf(fp, " ");
  63. ret += fprintf(fp, "\n");
  64. return ret;
  65. }
  66. static size_t
  67. ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
  68. int depth_mask, int count, u64 total_samples,
  69. int hits, int left_margin)
  70. {
  71. int i;
  72. size_t ret = 0;
  73. ret += callchain__fprintf_left_margin(fp, left_margin);
  74. for (i = 0; i < depth; i++) {
  75. if (depth_mask & (1 << i))
  76. ret += fprintf(fp, "|");
  77. else
  78. ret += fprintf(fp, " ");
  79. if (!count && i == depth - 1) {
  80. double percent;
  81. percent = hits * 100.0 / total_samples;
  82. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  83. } else
  84. ret += fprintf(fp, "%s", " ");
  85. }
  86. if (chain->sym)
  87. ret += fprintf(fp, "%s\n", chain->sym->name);
  88. else
  89. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  90. return ret;
  91. }
  92. static struct symbol *rem_sq_bracket;
  93. static struct callchain_list rem_hits;
  94. static void init_rem_hits(void)
  95. {
  96. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  97. if (!rem_sq_bracket) {
  98. fprintf(stderr, "Not enough memory to display remaining hits\n");
  99. return;
  100. }
  101. strcpy(rem_sq_bracket->name, "[...]");
  102. rem_hits.sym = rem_sq_bracket;
  103. }
  104. static size_t
  105. __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  106. u64 total_samples, int depth, int depth_mask,
  107. int left_margin)
  108. {
  109. struct rb_node *node, *next;
  110. struct callchain_node *child;
  111. struct callchain_list *chain;
  112. int new_depth_mask = depth_mask;
  113. u64 new_total;
  114. u64 remaining;
  115. size_t ret = 0;
  116. int i;
  117. if (callchain_param.mode == CHAIN_GRAPH_REL)
  118. new_total = self->children_hit;
  119. else
  120. new_total = total_samples;
  121. remaining = new_total;
  122. node = rb_first(&self->rb_root);
  123. while (node) {
  124. u64 cumul;
  125. child = rb_entry(node, struct callchain_node, rb_node);
  126. cumul = cumul_hits(child);
  127. remaining -= cumul;
  128. /*
  129. * The depth mask manages the output of pipes that show
  130. * the depth. We don't want to keep the pipes of the current
  131. * level for the last child of this depth.
  132. * Except if we have remaining filtered hits. They will
  133. * supersede the last child
  134. */
  135. next = rb_next(node);
  136. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  137. new_depth_mask &= ~(1 << (depth - 1));
  138. /*
  139. * But we keep the older depth mask for the line seperator
  140. * to keep the level link until we reach the last child
  141. */
  142. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  143. left_margin);
  144. i = 0;
  145. list_for_each_entry(chain, &child->val, list) {
  146. if (chain->ip >= PERF_CONTEXT_MAX)
  147. continue;
  148. ret += ipchain__fprintf_graph(fp, chain, depth,
  149. new_depth_mask, i++,
  150. new_total,
  151. cumul,
  152. left_margin);
  153. }
  154. ret += __callchain__fprintf_graph(fp, child, new_total,
  155. depth + 1,
  156. new_depth_mask | (1 << depth),
  157. left_margin);
  158. node = next;
  159. }
  160. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  161. remaining && remaining != new_total) {
  162. if (!rem_sq_bracket)
  163. return ret;
  164. new_depth_mask &= ~(1 << (depth - 1));
  165. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  166. new_depth_mask, 0, new_total,
  167. remaining, left_margin);
  168. }
  169. return ret;
  170. }
  171. static size_t
  172. callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  173. u64 total_samples, int left_margin)
  174. {
  175. struct callchain_list *chain;
  176. bool printed = false;
  177. int i = 0;
  178. int ret = 0;
  179. list_for_each_entry(chain, &self->val, list) {
  180. if (chain->ip >= PERF_CONTEXT_MAX)
  181. continue;
  182. if (!i++ && sort__first_dimension == SORT_SYM)
  183. continue;
  184. if (!printed) {
  185. ret += callchain__fprintf_left_margin(fp, left_margin);
  186. ret += fprintf(fp, "|\n");
  187. ret += callchain__fprintf_left_margin(fp, left_margin);
  188. ret += fprintf(fp, "---");
  189. left_margin += 3;
  190. printed = true;
  191. } else
  192. ret += callchain__fprintf_left_margin(fp, left_margin);
  193. if (chain->sym)
  194. ret += fprintf(fp, " %s\n", chain->sym->name);
  195. else
  196. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  197. }
  198. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  199. return ret;
  200. }
  201. static size_t
  202. callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  203. u64 total_samples)
  204. {
  205. struct callchain_list *chain;
  206. size_t ret = 0;
  207. if (!self)
  208. return 0;
  209. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  210. list_for_each_entry(chain, &self->val, list) {
  211. if (chain->ip >= PERF_CONTEXT_MAX)
  212. continue;
  213. if (chain->sym)
  214. ret += fprintf(fp, " %s\n", chain->sym->name);
  215. else
  216. ret += fprintf(fp, " %p\n",
  217. (void *)(long)chain->ip);
  218. }
  219. return ret;
  220. }
  221. static size_t
  222. hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  223. u64 total_samples, int left_margin)
  224. {
  225. struct rb_node *rb_node;
  226. struct callchain_node *chain;
  227. size_t ret = 0;
  228. rb_node = rb_first(&self->sorted_chain);
  229. while (rb_node) {
  230. double percent;
  231. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  232. percent = chain->hit * 100.0 / total_samples;
  233. switch (callchain_param.mode) {
  234. case CHAIN_FLAT:
  235. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  236. percent);
  237. ret += callchain__fprintf_flat(fp, chain, total_samples);
  238. break;
  239. case CHAIN_GRAPH_ABS: /* Falldown */
  240. case CHAIN_GRAPH_REL:
  241. ret += callchain__fprintf_graph(fp, chain, total_samples,
  242. left_margin);
  243. case CHAIN_NONE:
  244. default:
  245. break;
  246. }
  247. ret += fprintf(fp, "\n");
  248. rb_node = rb_next(rb_node);
  249. }
  250. return ret;
  251. }
  252. static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
  253. struct perf_session *session,
  254. u64 total_samples)
  255. {
  256. struct sort_entry *se;
  257. size_t ret;
  258. if (exclude_other && !self->parent)
  259. return 0;
  260. if (total_samples)
  261. ret = percent_color_fprintf(fp,
  262. field_sep ? "%.2f" : " %6.2f%%",
  263. (self->count * 100.0) / total_samples);
  264. else
  265. ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
  266. if (show_nr_samples) {
  267. if (field_sep)
  268. fprintf(fp, "%c%lld", *field_sep, self->count);
  269. else
  270. fprintf(fp, "%11lld", self->count);
  271. }
  272. list_for_each_entry(se, &hist_entry__sort_list, list) {
  273. if (se->elide)
  274. continue;
  275. fprintf(fp, "%s", field_sep ?: " ");
  276. ret += se->print(fp, self, se->width ? *se->width : 0);
  277. }
  278. ret += fprintf(fp, "\n");
  279. if (session->use_callchain) {
  280. int left_margin = 0;
  281. if (sort__first_dimension == SORT_COMM) {
  282. se = list_first_entry(&hist_entry__sort_list, typeof(*se),
  283. list);
  284. left_margin = se->width ? *se->width : 0;
  285. left_margin -= thread__comm_len(self->thread);
  286. }
  287. hist_entry_callchain__fprintf(fp, self, total_samples,
  288. left_margin);
  289. }
  290. return ret;
  291. }
  292. /*
  293. *
  294. */
  295. static void dso__calc_col_width(struct dso *self)
  296. {
  297. if (!col_width_list_str && !field_sep &&
  298. (!dso_list || strlist__has_entry(dso_list, self->name))) {
  299. unsigned int slen = strlen(self->name);
  300. if (slen > dsos__col_width)
  301. dsos__col_width = slen;
  302. }
  303. self->slen_calculated = 1;
  304. }
  305. static void thread__comm_adjust(struct thread *self)
  306. {
  307. char *comm = self->comm;
  308. if (!col_width_list_str && !field_sep &&
  309. (!comm_list || strlist__has_entry(comm_list, comm))) {
  310. unsigned int slen = strlen(comm);
  311. if (slen > comms__col_width) {
  312. comms__col_width = slen;
  313. threads__col_width = slen + 6;
  314. }
  315. }
  316. }
  317. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  318. {
  319. int ret = thread__set_comm(self, comm);
  320. if (ret)
  321. return ret;
  322. thread__comm_adjust(self);
  323. return 0;
  324. }
  325. /*
  326. * collect histogram counts
  327. */
  328. static int perf_session__add_hist_entry(struct perf_session *self,
  329. struct addr_location *al,
  330. struct ip_callchain *chain, u64 count)
  331. {
  332. struct symbol **syms = NULL, *parent = NULL;
  333. bool hit;
  334. struct hist_entry *he;
  335. if ((sort__has_parent || self->use_callchain) && chain)
  336. syms = perf_session__resolve_callchain(self, al->thread,
  337. chain, &parent);
  338. he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
  339. if (he == NULL)
  340. return -ENOMEM;
  341. if (hit)
  342. he->count += count;
  343. if (self->use_callchain) {
  344. if (!hit)
  345. callchain_init(&he->callchain);
  346. append_chain(&he->callchain, chain, syms);
  347. free(syms);
  348. }
  349. return 0;
  350. }
  351. static size_t perf_session__fprintf_hist_entries(struct perf_session *self,
  352. u64 total_samples, FILE *fp)
  353. {
  354. struct hist_entry *pos;
  355. struct sort_entry *se;
  356. struct rb_node *nd;
  357. size_t ret = 0;
  358. unsigned int width;
  359. char *col_width = col_width_list_str;
  360. int raw_printing_style;
  361. raw_printing_style = !strcmp(pretty_printing_style, "raw");
  362. init_rem_hits();
  363. fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
  364. fprintf(fp, "#\n");
  365. fprintf(fp, "# Overhead");
  366. if (show_nr_samples) {
  367. if (field_sep)
  368. fprintf(fp, "%cSamples", *field_sep);
  369. else
  370. fputs(" Samples ", fp);
  371. }
  372. list_for_each_entry(se, &hist_entry__sort_list, list) {
  373. if (se->elide)
  374. continue;
  375. if (field_sep) {
  376. fprintf(fp, "%c%s", *field_sep, se->header);
  377. continue;
  378. }
  379. width = strlen(se->header);
  380. if (se->width) {
  381. if (col_width_list_str) {
  382. if (col_width) {
  383. *se->width = atoi(col_width);
  384. col_width = strchr(col_width, ',');
  385. if (col_width)
  386. ++col_width;
  387. }
  388. }
  389. width = *se->width = max(*se->width, width);
  390. }
  391. fprintf(fp, " %*s", width, se->header);
  392. }
  393. fprintf(fp, "\n");
  394. if (field_sep)
  395. goto print_entries;
  396. fprintf(fp, "# ........");
  397. if (show_nr_samples)
  398. fprintf(fp, " ..........");
  399. list_for_each_entry(se, &hist_entry__sort_list, list) {
  400. unsigned int i;
  401. if (se->elide)
  402. continue;
  403. fprintf(fp, " ");
  404. if (se->width)
  405. width = *se->width;
  406. else
  407. width = strlen(se->header);
  408. for (i = 0; i < width; i++)
  409. fprintf(fp, ".");
  410. }
  411. fprintf(fp, "\n");
  412. fprintf(fp, "#\n");
  413. print_entries:
  414. for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
  415. pos = rb_entry(nd, struct hist_entry, rb_node);
  416. ret += hist_entry__fprintf(fp, pos, self, total_samples);
  417. }
  418. if (sort_order == default_sort_order &&
  419. parent_pattern == default_parent_pattern) {
  420. fprintf(fp, "#\n");
  421. fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
  422. fprintf(fp, "#\n");
  423. }
  424. fprintf(fp, "\n");
  425. free(rem_sq_bracket);
  426. if (show_threads)
  427. perf_read_values_display(fp, &show_threads_values,
  428. raw_printing_style);
  429. return ret;
  430. }
  431. static int validate_chain(struct ip_callchain *chain, event_t *event)
  432. {
  433. unsigned int chain_size;
  434. chain_size = event->header.size;
  435. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  436. if (chain->nr*sizeof(u64) > chain_size)
  437. return -1;
  438. return 0;
  439. }
  440. static int process_sample_event(event_t *event, struct perf_session *session)
  441. {
  442. struct sample_data data;
  443. int cpumode;
  444. struct addr_location al;
  445. struct thread *thread;
  446. memset(&data, 0, sizeof(data));
  447. data.period = 1;
  448. event__parse_sample(event, session->sample_type, &data);
  449. dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
  450. event->header.misc,
  451. data.pid, data.tid,
  452. (void *)(long)data.ip,
  453. (long long)data.period);
  454. if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
  455. unsigned int i;
  456. dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
  457. if (validate_chain(data.callchain, event) < 0) {
  458. pr_debug("call-chain problem with event, "
  459. "skipping it.\n");
  460. return 0;
  461. }
  462. if (dump_trace) {
  463. for (i = 0; i < data.callchain->nr; i++)
  464. dump_printf("..... %2d: %016Lx\n",
  465. i, data.callchain->ips[i]);
  466. }
  467. }
  468. thread = perf_session__findnew(session, data.pid);
  469. if (thread == NULL) {
  470. pr_debug("problem processing %d event, skipping it.\n",
  471. event->header.type);
  472. return -1;
  473. }
  474. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  475. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  476. return 0;
  477. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  478. thread__find_addr_location(thread, session, cpumode,
  479. MAP__FUNCTION, data.ip, &al, NULL);
  480. /*
  481. * We have to do this here as we may have a dso with no symbol hit that
  482. * has a name longer than the ones with symbols sampled.
  483. */
  484. if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
  485. dso__calc_col_width(al.map->dso);
  486. if (dso_list &&
  487. (!al.map || !al.map->dso ||
  488. !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
  489. (al.map->dso->short_name != al.map->dso->long_name &&
  490. strlist__has_entry(dso_list, al.map->dso->long_name)))))
  491. return 0;
  492. if (sym_list && al.sym && !strlist__has_entry(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, &symbol_conf);
  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", &dso_list_str, "dso[,dso...]",
  670. "only consider symbols in these dsos"),
  671. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  672. "only consider symbols in these comms"),
  673. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  674. "only consider these symbols"),
  675. OPT_STRING('w', "column-widths", &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 setup_list(struct strlist **list, const char *list_str,
  684. struct sort_entry *se, const char *list_name,
  685. FILE *fp)
  686. {
  687. if (list_str) {
  688. *list = strlist__new(true, list_str);
  689. if (!*list) {
  690. fprintf(stderr, "problems parsing %s list\n",
  691. list_name);
  692. exit(129);
  693. }
  694. if (strlist__nr_entries(*list) == 1) {
  695. fprintf(fp, "# %s: %s\n", list_name,
  696. strlist__entry(*list, 0)->s);
  697. se->elide = true;
  698. }
  699. }
  700. }
  701. int cmd_report(int argc, const char **argv, const char *prefix __used)
  702. {
  703. if (symbol__init(&symbol_conf) < 0)
  704. return -1;
  705. argc = parse_options(argc, argv, options, report_usage, 0);
  706. setup_sorting(report_usage, options);
  707. if (parent_pattern != default_parent_pattern) {
  708. sort_dimension__add("parent");
  709. sort_parent.elide = 1;
  710. } else
  711. exclude_other = 0;
  712. /*
  713. * Any (unrecognized) arguments left?
  714. */
  715. if (argc)
  716. usage_with_options(report_usage, options);
  717. setup_pager();
  718. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  719. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  720. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  721. if (field_sep && *field_sep == '.') {
  722. fputs("'.' is the only non valid --field-separator argument\n",
  723. stderr);
  724. exit(129);
  725. }
  726. return __cmd_report();
  727. }