builtin-report.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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/parse-options.h"
  23. #include "util/parse-events.h"
  24. #include "util/thread.h"
  25. #include "util/sort.h"
  26. #include "util/hist.h"
  27. static char const *input_name = "perf.data";
  28. static char *dso_list_str, *comm_list_str, *sym_list_str,
  29. *col_width_list_str;
  30. static struct strlist *dso_list, *comm_list, *sym_list;
  31. static int force;
  32. static int input;
  33. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  34. static int full_paths;
  35. static int show_nr_samples;
  36. static int show_threads;
  37. static struct perf_read_values show_threads_values;
  38. static char default_pretty_printing_style[] = "normal";
  39. static char *pretty_printing_style = default_pretty_printing_style;
  40. static unsigned long page_size;
  41. static unsigned long mmap_window = 32;
  42. static int exclude_other = 1;
  43. static char callchain_default_opt[] = "fractal,0.5";
  44. static char __cwd[PATH_MAX];
  45. static char *cwd = __cwd;
  46. static int cwdlen;
  47. static struct rb_root threads;
  48. static struct thread *last_match;
  49. static struct perf_header *header;
  50. static u64 sample_type;
  51. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
  52. {
  53. int i;
  54. size_t ret = 0;
  55. ret += fprintf(fp, "%s", " ");
  56. for (i = 0; i < depth; i++)
  57. if (depth_mask & (1 << i))
  58. ret += fprintf(fp, "| ");
  59. else
  60. ret += fprintf(fp, " ");
  61. ret += fprintf(fp, "\n");
  62. return ret;
  63. }
  64. static size_t
  65. ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
  66. int depth_mask, int count, u64 total_samples,
  67. int hits)
  68. {
  69. int i;
  70. size_t ret = 0;
  71. ret += fprintf(fp, "%s", " ");
  72. for (i = 0; i < depth; i++) {
  73. if (depth_mask & (1 << i))
  74. ret += fprintf(fp, "|");
  75. else
  76. ret += fprintf(fp, " ");
  77. if (!count && i == depth - 1) {
  78. double percent;
  79. percent = hits * 100.0 / total_samples;
  80. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  81. } else
  82. ret += fprintf(fp, "%s", " ");
  83. }
  84. if (chain->sym)
  85. ret += fprintf(fp, "%s\n", chain->sym->name);
  86. else
  87. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  88. return ret;
  89. }
  90. static struct symbol *rem_sq_bracket;
  91. static struct callchain_list rem_hits;
  92. static void init_rem_hits(void)
  93. {
  94. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  95. if (!rem_sq_bracket) {
  96. fprintf(stderr, "Not enough memory to display remaining hits\n");
  97. return;
  98. }
  99. strcpy(rem_sq_bracket->name, "[...]");
  100. rem_hits.sym = rem_sq_bracket;
  101. }
  102. static size_t
  103. callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  104. u64 total_samples, int depth, int depth_mask)
  105. {
  106. struct rb_node *node, *next;
  107. struct callchain_node *child;
  108. struct callchain_list *chain;
  109. int new_depth_mask = depth_mask;
  110. u64 new_total;
  111. u64 remaining;
  112. size_t ret = 0;
  113. int i;
  114. if (callchain_param.mode == CHAIN_GRAPH_REL)
  115. new_total = self->children_hit;
  116. else
  117. new_total = total_samples;
  118. remaining = new_total;
  119. node = rb_first(&self->rb_root);
  120. while (node) {
  121. u64 cumul;
  122. child = rb_entry(node, struct callchain_node, rb_node);
  123. cumul = cumul_hits(child);
  124. remaining -= cumul;
  125. /*
  126. * The depth mask manages the output of pipes that show
  127. * the depth. We don't want to keep the pipes of the current
  128. * level for the last child of this depth.
  129. * Except if we have remaining filtered hits. They will
  130. * supersede the last child
  131. */
  132. next = rb_next(node);
  133. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  134. new_depth_mask &= ~(1 << (depth - 1));
  135. /*
  136. * But we keep the older depth mask for the line seperator
  137. * to keep the level link until we reach the last child
  138. */
  139. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
  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. }
  149. ret += callchain__fprintf_graph(fp, child, new_total,
  150. depth + 1,
  151. new_depth_mask | (1 << depth));
  152. node = next;
  153. }
  154. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  155. remaining && remaining != new_total) {
  156. if (!rem_sq_bracket)
  157. return ret;
  158. new_depth_mask &= ~(1 << (depth - 1));
  159. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  160. new_depth_mask, 0, new_total,
  161. remaining);
  162. }
  163. return ret;
  164. }
  165. static size_t
  166. callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  167. u64 total_samples)
  168. {
  169. struct callchain_list *chain;
  170. size_t ret = 0;
  171. if (!self)
  172. return 0;
  173. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  174. list_for_each_entry(chain, &self->val, list) {
  175. if (chain->ip >= PERF_CONTEXT_MAX)
  176. continue;
  177. if (chain->sym)
  178. ret += fprintf(fp, " %s\n", chain->sym->name);
  179. else
  180. ret += fprintf(fp, " %p\n",
  181. (void *)(long)chain->ip);
  182. }
  183. return ret;
  184. }
  185. static size_t
  186. hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  187. u64 total_samples)
  188. {
  189. struct rb_node *rb_node;
  190. struct callchain_node *chain;
  191. size_t ret = 0;
  192. rb_node = rb_first(&self->sorted_chain);
  193. while (rb_node) {
  194. double percent;
  195. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  196. percent = chain->hit * 100.0 / total_samples;
  197. switch (callchain_param.mode) {
  198. case CHAIN_FLAT:
  199. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  200. percent);
  201. ret += callchain__fprintf_flat(fp, chain, total_samples);
  202. break;
  203. case CHAIN_GRAPH_ABS: /* Falldown */
  204. case CHAIN_GRAPH_REL:
  205. ret += callchain__fprintf_graph(fp, chain,
  206. total_samples, 1, 1);
  207. case CHAIN_NONE:
  208. default:
  209. break;
  210. }
  211. ret += fprintf(fp, "\n");
  212. rb_node = rb_next(rb_node);
  213. }
  214. return ret;
  215. }
  216. static size_t
  217. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  218. {
  219. struct sort_entry *se;
  220. size_t ret;
  221. if (exclude_other && !self->parent)
  222. return 0;
  223. if (total_samples)
  224. ret = percent_color_fprintf(fp,
  225. field_sep ? "%.2f" : " %6.2f%%",
  226. (self->count * 100.0) / total_samples);
  227. else
  228. ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
  229. if (show_nr_samples) {
  230. if (field_sep)
  231. fprintf(fp, "%c%lld", *field_sep, self->count);
  232. else
  233. fprintf(fp, "%11lld", self->count);
  234. }
  235. list_for_each_entry(se, &hist_entry__sort_list, list) {
  236. if (se->elide)
  237. continue;
  238. fprintf(fp, "%s", field_sep ?: " ");
  239. ret += se->print(fp, self, se->width ? *se->width : 0);
  240. }
  241. ret += fprintf(fp, "\n");
  242. if (callchain)
  243. hist_entry_callchain__fprintf(fp, self, total_samples);
  244. return ret;
  245. }
  246. /*
  247. *
  248. */
  249. static void dso__calc_col_width(struct dso *self)
  250. {
  251. if (!col_width_list_str && !field_sep &&
  252. (!dso_list || strlist__has_entry(dso_list, self->name))) {
  253. unsigned int slen = strlen(self->name);
  254. if (slen > dsos__col_width)
  255. dsos__col_width = slen;
  256. }
  257. self->slen_calculated = 1;
  258. }
  259. static void thread__comm_adjust(struct thread *self)
  260. {
  261. char *comm = self->comm;
  262. if (!col_width_list_str && !field_sep &&
  263. (!comm_list || strlist__has_entry(comm_list, comm))) {
  264. unsigned int slen = strlen(comm);
  265. if (slen > comms__col_width) {
  266. comms__col_width = slen;
  267. threads__col_width = slen + 6;
  268. }
  269. }
  270. }
  271. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  272. {
  273. int ret = thread__set_comm(self, comm);
  274. if (ret)
  275. return ret;
  276. thread__comm_adjust(self);
  277. return 0;
  278. }
  279. static struct symbol *
  280. resolve_symbol(struct thread *thread, struct map **mapp,
  281. struct dso **dsop, u64 *ipp)
  282. {
  283. struct dso *dso = dsop ? *dsop : NULL;
  284. struct map *map = mapp ? *mapp : NULL;
  285. u64 ip = *ipp;
  286. if (!thread)
  287. return NULL;
  288. if (dso)
  289. goto got_dso;
  290. if (map)
  291. goto got_map;
  292. map = thread__find_map(thread, ip);
  293. if (map != NULL) {
  294. /*
  295. * We have to do this here as we may have a dso
  296. * with no symbol hit that has a name longer than
  297. * the ones with symbols sampled.
  298. */
  299. if (!sort_dso.elide && !map->dso->slen_calculated)
  300. dso__calc_col_width(map->dso);
  301. if (mapp)
  302. *mapp = map;
  303. got_map:
  304. ip = map->map_ip(map, ip);
  305. dso = map->dso;
  306. } else {
  307. /*
  308. * If this is outside of all known maps,
  309. * and is a negative address, try to look it
  310. * up in the kernel dso, as it might be a
  311. * vsyscall (which executes in user-mode):
  312. */
  313. if ((long long)ip < 0)
  314. dso = kernel_dso;
  315. }
  316. dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  317. dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  318. *ipp = ip;
  319. if (dsop)
  320. *dsop = dso;
  321. if (!dso)
  322. return NULL;
  323. got_dso:
  324. return dso->find_symbol(dso, ip);
  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 **
  333. resolve_callchain(struct thread *thread, struct map *map __used,
  334. struct ip_callchain *chain, struct hist_entry *entry)
  335. {
  336. u64 context = PERF_CONTEXT_MAX;
  337. struct symbol **syms = NULL;
  338. unsigned int i;
  339. if (callchain) {
  340. syms = calloc(chain->nr, sizeof(*syms));
  341. if (!syms) {
  342. fprintf(stderr, "Can't allocate memory for symbols\n");
  343. exit(-1);
  344. }
  345. }
  346. for (i = 0; i < chain->nr; i++) {
  347. u64 ip = chain->ips[i];
  348. struct dso *dso = NULL;
  349. struct symbol *sym;
  350. if (ip >= PERF_CONTEXT_MAX) {
  351. context = ip;
  352. continue;
  353. }
  354. switch (context) {
  355. case PERF_CONTEXT_HV:
  356. dso = hypervisor_dso;
  357. break;
  358. case PERF_CONTEXT_KERNEL:
  359. dso = kernel_dso;
  360. break;
  361. default:
  362. break;
  363. }
  364. sym = resolve_symbol(thread, NULL, &dso, &ip);
  365. if (sym) {
  366. if (sort__has_parent && call__match(sym) &&
  367. !entry->parent)
  368. entry->parent = sym;
  369. if (!callchain)
  370. break;
  371. syms[i] = sym;
  372. }
  373. }
  374. return syms;
  375. }
  376. /*
  377. * collect histogram counts
  378. */
  379. static int
  380. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  381. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  382. char level, u64 count)
  383. {
  384. struct rb_node **p = &hist.rb_node;
  385. struct rb_node *parent = NULL;
  386. struct hist_entry *he;
  387. struct symbol **syms = NULL;
  388. struct hist_entry entry = {
  389. .thread = thread,
  390. .map = map,
  391. .dso = dso,
  392. .sym = sym,
  393. .ip = ip,
  394. .level = level,
  395. .count = count,
  396. .parent = NULL,
  397. .sorted_chain = RB_ROOT
  398. };
  399. int cmp;
  400. if ((sort__has_parent || callchain) && chain)
  401. syms = resolve_callchain(thread, map, chain, &entry);
  402. while (*p != NULL) {
  403. parent = *p;
  404. he = rb_entry(parent, struct hist_entry, rb_node);
  405. cmp = hist_entry__cmp(&entry, he);
  406. if (!cmp) {
  407. he->count += count;
  408. if (callchain) {
  409. append_chain(&he->callchain, chain, syms);
  410. free(syms);
  411. }
  412. return 0;
  413. }
  414. if (cmp < 0)
  415. p = &(*p)->rb_left;
  416. else
  417. p = &(*p)->rb_right;
  418. }
  419. he = malloc(sizeof(*he));
  420. if (!he)
  421. return -ENOMEM;
  422. *he = entry;
  423. if (callchain) {
  424. callchain_init(&he->callchain);
  425. append_chain(&he->callchain, chain, syms);
  426. free(syms);
  427. }
  428. rb_link_node(&he->rb_node, parent, p);
  429. rb_insert_color(&he->rb_node, &hist);
  430. return 0;
  431. }
  432. static size_t output__fprintf(FILE *fp, u64 total_samples)
  433. {
  434. struct hist_entry *pos;
  435. struct sort_entry *se;
  436. struct rb_node *nd;
  437. size_t ret = 0;
  438. unsigned int width;
  439. char *col_width = col_width_list_str;
  440. int raw_printing_style;
  441. raw_printing_style = !strcmp(pretty_printing_style, "raw");
  442. init_rem_hits();
  443. fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
  444. fprintf(fp, "#\n");
  445. fprintf(fp, "# Overhead");
  446. if (show_nr_samples) {
  447. if (field_sep)
  448. fprintf(fp, "%cSamples", *field_sep);
  449. else
  450. fputs(" Samples ", fp);
  451. }
  452. list_for_each_entry(se, &hist_entry__sort_list, list) {
  453. if (se->elide)
  454. continue;
  455. if (field_sep) {
  456. fprintf(fp, "%c%s", *field_sep, se->header);
  457. continue;
  458. }
  459. width = strlen(se->header);
  460. if (se->width) {
  461. if (col_width_list_str) {
  462. if (col_width) {
  463. *se->width = atoi(col_width);
  464. col_width = strchr(col_width, ',');
  465. if (col_width)
  466. ++col_width;
  467. }
  468. }
  469. width = *se->width = max(*se->width, width);
  470. }
  471. fprintf(fp, " %*s", width, se->header);
  472. }
  473. fprintf(fp, "\n");
  474. if (field_sep)
  475. goto print_entries;
  476. fprintf(fp, "# ........");
  477. if (show_nr_samples)
  478. fprintf(fp, " ..........");
  479. list_for_each_entry(se, &hist_entry__sort_list, list) {
  480. unsigned int i;
  481. if (se->elide)
  482. continue;
  483. fprintf(fp, " ");
  484. if (se->width)
  485. width = *se->width;
  486. else
  487. width = strlen(se->header);
  488. for (i = 0; i < width; i++)
  489. fprintf(fp, ".");
  490. }
  491. fprintf(fp, "\n");
  492. fprintf(fp, "#\n");
  493. print_entries:
  494. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  495. pos = rb_entry(nd, struct hist_entry, rb_node);
  496. ret += hist_entry__fprintf(fp, pos, total_samples);
  497. }
  498. if (sort_order == default_sort_order &&
  499. parent_pattern == default_parent_pattern) {
  500. fprintf(fp, "#\n");
  501. fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
  502. fprintf(fp, "#\n");
  503. }
  504. fprintf(fp, "\n");
  505. free(rem_sq_bracket);
  506. if (show_threads)
  507. perf_read_values_display(fp, &show_threads_values,
  508. raw_printing_style);
  509. return ret;
  510. }
  511. static int validate_chain(struct ip_callchain *chain, event_t *event)
  512. {
  513. unsigned int chain_size;
  514. chain_size = event->header.size;
  515. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  516. if (chain->nr*sizeof(u64) > chain_size)
  517. return -1;
  518. return 0;
  519. }
  520. static int
  521. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  522. {
  523. char level;
  524. int show = 0;
  525. struct dso *dso = NULL;
  526. struct thread *thread;
  527. u64 ip = event->ip.ip;
  528. u64 period = 1;
  529. struct map *map = NULL;
  530. void *more_data = event->ip.__more_data;
  531. struct ip_callchain *chain = NULL;
  532. int cpumode;
  533. thread = threads__findnew(event->ip.pid, &threads, &last_match);
  534. if (sample_type & PERF_SAMPLE_PERIOD) {
  535. period = *(u64 *)more_data;
  536. more_data += sizeof(u64);
  537. }
  538. dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
  539. (void *)(offset + head),
  540. (void *)(long)(event->header.size),
  541. event->header.misc,
  542. event->ip.pid, event->ip.tid,
  543. (void *)(long)ip,
  544. (long long)period);
  545. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  546. unsigned int i;
  547. chain = (void *)more_data;
  548. dump_printf("... chain: nr:%Lu\n", chain->nr);
  549. if (validate_chain(chain, event) < 0) {
  550. eprintf("call-chain problem with event, skipping it.\n");
  551. return 0;
  552. }
  553. if (dump_trace) {
  554. for (i = 0; i < chain->nr; i++)
  555. dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
  556. }
  557. }
  558. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  559. if (thread == NULL) {
  560. eprintf("problem processing %d event, skipping it.\n",
  561. event->header.type);
  562. return -1;
  563. }
  564. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  565. return 0;
  566. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  567. if (cpumode == PERF_RECORD_MISC_KERNEL) {
  568. show = SHOW_KERNEL;
  569. level = 'k';
  570. dso = kernel_dso;
  571. dump_printf(" ...... dso: %s\n", dso->name);
  572. } else if (cpumode == PERF_RECORD_MISC_USER) {
  573. show = SHOW_USER;
  574. level = '.';
  575. } else {
  576. show = SHOW_HV;
  577. level = 'H';
  578. dso = hypervisor_dso;
  579. dump_printf(" ...... dso: [hypervisor]\n");
  580. }
  581. if (show & show_mask) {
  582. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  583. if (dso_list && (!dso || !dso->name ||
  584. !strlist__has_entry(dso_list, dso->name)))
  585. return 0;
  586. if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
  587. return 0;
  588. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  589. eprintf("problem incrementing symbol count, skipping event\n");
  590. return -1;
  591. }
  592. }
  593. total += period;
  594. return 0;
  595. }
  596. static int
  597. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  598. {
  599. struct thread *thread;
  600. struct map *map = map__new(&event->mmap, cwd, cwdlen);
  601. thread = threads__findnew(event->mmap.pid, &threads, &last_match);
  602. dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
  603. (void *)(offset + head),
  604. (void *)(long)(event->header.size),
  605. event->mmap.pid,
  606. event->mmap.tid,
  607. (void *)(long)event->mmap.start,
  608. (void *)(long)event->mmap.len,
  609. (void *)(long)event->mmap.pgoff,
  610. event->mmap.filename);
  611. if (thread == NULL || map == NULL) {
  612. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  613. return 0;
  614. }
  615. thread__insert_map(thread, map);
  616. total_mmap++;
  617. return 0;
  618. }
  619. static int
  620. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  621. {
  622. struct thread *thread;
  623. thread = threads__findnew(event->comm.pid, &threads, &last_match);
  624. dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
  625. (void *)(offset + head),
  626. (void *)(long)(event->header.size),
  627. event->comm.comm, event->comm.pid);
  628. if (thread == NULL ||
  629. thread__set_comm_adjust(thread, event->comm.comm)) {
  630. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  631. return -1;
  632. }
  633. total_comm++;
  634. return 0;
  635. }
  636. static int
  637. process_task_event(event_t *event, unsigned long offset, unsigned long head)
  638. {
  639. struct thread *thread;
  640. struct thread *parent;
  641. thread = threads__findnew(event->fork.pid, &threads, &last_match);
  642. parent = threads__findnew(event->fork.ppid, &threads, &last_match);
  643. dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
  644. (void *)(offset + head),
  645. (void *)(long)(event->header.size),
  646. event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
  647. event->fork.pid, event->fork.tid,
  648. event->fork.ppid, event->fork.ptid);
  649. /*
  650. * A thread clone will have the same PID for both
  651. * parent and child.
  652. */
  653. if (thread == parent)
  654. return 0;
  655. if (event->header.type == PERF_RECORD_EXIT)
  656. return 0;
  657. if (!thread || !parent || thread__fork(thread, parent)) {
  658. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  659. return -1;
  660. }
  661. total_fork++;
  662. return 0;
  663. }
  664. static int
  665. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  666. {
  667. dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
  668. (void *)(offset + head),
  669. (void *)(long)(event->header.size),
  670. event->lost.id,
  671. event->lost.lost);
  672. total_lost += event->lost.lost;
  673. return 0;
  674. }
  675. static int
  676. process_read_event(event_t *event, unsigned long offset, unsigned long head)
  677. {
  678. struct perf_event_attr *attr;
  679. attr = perf_header__find_attr(event->read.id, header);
  680. if (show_threads) {
  681. const char *name = attr ? __event_name(attr->type, attr->config)
  682. : "unknown";
  683. perf_read_values_add_value(&show_threads_values,
  684. event->read.pid, event->read.tid,
  685. event->read.id,
  686. name,
  687. event->read.value);
  688. }
  689. dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
  690. (void *)(offset + head),
  691. (void *)(long)(event->header.size),
  692. event->read.pid,
  693. event->read.tid,
  694. attr ? __event_name(attr->type, attr->config)
  695. : "FAIL",
  696. event->read.value);
  697. return 0;
  698. }
  699. static int
  700. process_event(event_t *event, unsigned long offset, unsigned long head)
  701. {
  702. trace_event(event);
  703. switch (event->header.type) {
  704. case PERF_RECORD_SAMPLE:
  705. return process_sample_event(event, offset, head);
  706. case PERF_RECORD_MMAP:
  707. return process_mmap_event(event, offset, head);
  708. case PERF_RECORD_COMM:
  709. return process_comm_event(event, offset, head);
  710. case PERF_RECORD_FORK:
  711. case PERF_RECORD_EXIT:
  712. return process_task_event(event, offset, head);
  713. case PERF_RECORD_LOST:
  714. return process_lost_event(event, offset, head);
  715. case PERF_RECORD_READ:
  716. return process_read_event(event, offset, head);
  717. /*
  718. * We dont process them right now but they are fine:
  719. */
  720. case PERF_RECORD_THROTTLE:
  721. case PERF_RECORD_UNTHROTTLE:
  722. return 0;
  723. default:
  724. return -1;
  725. }
  726. return 0;
  727. }
  728. static int __cmd_report(void)
  729. {
  730. int ret, rc = EXIT_FAILURE;
  731. unsigned long offset = 0;
  732. unsigned long head, shift;
  733. struct stat input_stat;
  734. struct thread *idle;
  735. event_t *event;
  736. uint32_t size;
  737. char *buf;
  738. idle = register_idle_thread(&threads, &last_match);
  739. thread__comm_adjust(idle);
  740. if (show_threads)
  741. perf_read_values_init(&show_threads_values);
  742. input = open(input_name, O_RDONLY);
  743. if (input < 0) {
  744. fprintf(stderr, " failed to open file: %s", input_name);
  745. if (!strcmp(input_name, "perf.data"))
  746. fprintf(stderr, " (try 'perf record' first)");
  747. fprintf(stderr, "\n");
  748. exit(-1);
  749. }
  750. ret = fstat(input, &input_stat);
  751. if (ret < 0) {
  752. perror("failed to stat file");
  753. exit(-1);
  754. }
  755. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  756. fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
  757. exit(-1);
  758. }
  759. if (!input_stat.st_size) {
  760. fprintf(stderr, "zero-sized file, nothing to do!\n");
  761. exit(0);
  762. }
  763. header = perf_header__read(input);
  764. head = header->data_offset;
  765. sample_type = perf_header__sample_type(header);
  766. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  767. if (sort__has_parent) {
  768. fprintf(stderr, "selected --sort parent, but no"
  769. " callchain data. Did you call"
  770. " perf record without -g?\n");
  771. exit(-1);
  772. }
  773. if (callchain) {
  774. fprintf(stderr, "selected -g but no callchain data."
  775. " Did you call perf record without"
  776. " -g?\n");
  777. exit(-1);
  778. }
  779. } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
  780. callchain = 1;
  781. if (register_callchain_param(&callchain_param) < 0) {
  782. fprintf(stderr, "Can't register callchain"
  783. " params\n");
  784. exit(-1);
  785. }
  786. }
  787. if (load_kernel() < 0) {
  788. perror("failed to load kernel symbols");
  789. return EXIT_FAILURE;
  790. }
  791. if (!full_paths) {
  792. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  793. perror("failed to get the current directory");
  794. return EXIT_FAILURE;
  795. }
  796. cwdlen = strlen(cwd);
  797. } else {
  798. cwd = NULL;
  799. cwdlen = 0;
  800. }
  801. shift = page_size * (head / page_size);
  802. offset += shift;
  803. head -= shift;
  804. remap:
  805. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  806. MAP_SHARED, input, offset);
  807. if (buf == MAP_FAILED) {
  808. perror("failed to mmap file");
  809. exit(-1);
  810. }
  811. more:
  812. event = (event_t *)(buf + head);
  813. size = event->header.size;
  814. if (!size)
  815. size = 8;
  816. if (head + event->header.size >= page_size * mmap_window) {
  817. int munmap_ret;
  818. shift = page_size * (head / page_size);
  819. munmap_ret = munmap(buf, page_size * mmap_window);
  820. assert(munmap_ret == 0);
  821. offset += shift;
  822. head -= shift;
  823. goto remap;
  824. }
  825. size = event->header.size;
  826. dump_printf("\n%p [%p]: event: %d\n",
  827. (void *)(offset + head),
  828. (void *)(long)event->header.size,
  829. event->header.type);
  830. if (!size || process_event(event, offset, head) < 0) {
  831. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  832. (void *)(offset + head),
  833. (void *)(long)(event->header.size),
  834. event->header.type);
  835. total_unknown++;
  836. /*
  837. * assume we lost track of the stream, check alignment, and
  838. * increment a single u64 in the hope to catch on again 'soon'.
  839. */
  840. if (unlikely(head & 7))
  841. head &= ~7ULL;
  842. size = 8;
  843. }
  844. head += size;
  845. if (offset + head >= header->data_offset + header->data_size)
  846. goto done;
  847. if (offset + head < (unsigned long)input_stat.st_size)
  848. goto more;
  849. done:
  850. rc = EXIT_SUCCESS;
  851. close(input);
  852. dump_printf(" IP events: %10ld\n", total);
  853. dump_printf(" mmap events: %10ld\n", total_mmap);
  854. dump_printf(" comm events: %10ld\n", total_comm);
  855. dump_printf(" fork events: %10ld\n", total_fork);
  856. dump_printf(" lost events: %10ld\n", total_lost);
  857. dump_printf(" unknown events: %10ld\n", total_unknown);
  858. if (dump_trace)
  859. return 0;
  860. if (verbose >= 3)
  861. threads__fprintf(stdout, &threads);
  862. if (verbose >= 2)
  863. dsos__fprintf(stdout);
  864. collapse__resort();
  865. output__resort(total);
  866. output__fprintf(stdout, total);
  867. if (show_threads)
  868. perf_read_values_destroy(&show_threads_values);
  869. return rc;
  870. }
  871. static int
  872. parse_callchain_opt(const struct option *opt __used, const char *arg,
  873. int unset __used)
  874. {
  875. char *tok;
  876. char *endptr;
  877. callchain = 1;
  878. if (!arg)
  879. return 0;
  880. tok = strtok((char *)arg, ",");
  881. if (!tok)
  882. return -1;
  883. /* get the output mode */
  884. if (!strncmp(tok, "graph", strlen(arg)))
  885. callchain_param.mode = CHAIN_GRAPH_ABS;
  886. else if (!strncmp(tok, "flat", strlen(arg)))
  887. callchain_param.mode = CHAIN_FLAT;
  888. else if (!strncmp(tok, "fractal", strlen(arg)))
  889. callchain_param.mode = CHAIN_GRAPH_REL;
  890. else if (!strncmp(tok, "none", strlen(arg))) {
  891. callchain_param.mode = CHAIN_NONE;
  892. callchain = 0;
  893. return 0;
  894. }
  895. else
  896. return -1;
  897. /* get the min percentage */
  898. tok = strtok(NULL, ",");
  899. if (!tok)
  900. goto setup;
  901. callchain_param.min_percent = strtod(tok, &endptr);
  902. if (tok == endptr)
  903. return -1;
  904. setup:
  905. if (register_callchain_param(&callchain_param) < 0) {
  906. fprintf(stderr, "Can't register callchain params\n");
  907. return -1;
  908. }
  909. return 0;
  910. }
  911. //static const char * const report_usage[] = {
  912. const char * const report_usage[] = {
  913. "perf report [<options>] <command>",
  914. NULL
  915. };
  916. static const struct option options[] = {
  917. OPT_STRING('i', "input", &input_name, "file",
  918. "input file name"),
  919. OPT_BOOLEAN('v', "verbose", &verbose,
  920. "be more verbose (show symbol address, etc)"),
  921. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  922. "dump raw trace in ASCII"),
  923. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  924. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  925. OPT_BOOLEAN('m', "modules", &modules,
  926. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  927. OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
  928. "Show a column with the number of samples"),
  929. OPT_BOOLEAN('T', "threads", &show_threads,
  930. "Show per-thread event counters"),
  931. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  932. "pretty printing style key: normal raw"),
  933. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  934. "sort by key(s): pid, comm, dso, symbol, parent"),
  935. OPT_BOOLEAN('P', "full-paths", &full_paths,
  936. "Don't shorten the pathnames taking into account the cwd"),
  937. OPT_STRING('p', "parent", &parent_pattern, "regex",
  938. "regex filter to identify parent, see: '--sort parent'"),
  939. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  940. "Only display entries with parent-match"),
  941. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  942. "Display callchains using output_type and min percent threshold. "
  943. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  944. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  945. "only consider symbols in these dsos"),
  946. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  947. "only consider symbols in these comms"),
  948. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  949. "only consider these symbols"),
  950. OPT_STRING('w', "column-widths", &col_width_list_str,
  951. "width[,width...]",
  952. "don't try to adjust column width, use these fixed values"),
  953. OPT_STRING('t', "field-separator", &field_sep, "separator",
  954. "separator for columns, no spaces will be added between "
  955. "columns '.' is reserved."),
  956. OPT_END()
  957. };
  958. static void setup_sorting(void)
  959. {
  960. char *tmp, *tok, *str = strdup(sort_order);
  961. for (tok = strtok_r(str, ", ", &tmp);
  962. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  963. if (sort_dimension__add(tok) < 0) {
  964. error("Unknown --sort key: `%s'", tok);
  965. usage_with_options(report_usage, options);
  966. }
  967. }
  968. free(str);
  969. }
  970. static void setup_list(struct strlist **list, const char *list_str,
  971. struct sort_entry *se, const char *list_name,
  972. FILE *fp)
  973. {
  974. if (list_str) {
  975. *list = strlist__new(true, list_str);
  976. if (!*list) {
  977. fprintf(stderr, "problems parsing %s list\n",
  978. list_name);
  979. exit(129);
  980. }
  981. if (strlist__nr_entries(*list) == 1) {
  982. fprintf(fp, "# %s: %s\n", list_name,
  983. strlist__entry(*list, 0)->s);
  984. se->elide = true;
  985. }
  986. }
  987. }
  988. int cmd_report(int argc, const char **argv, const char *prefix __used)
  989. {
  990. symbol__init();
  991. page_size = getpagesize();
  992. argc = parse_options(argc, argv, options, report_usage, 0);
  993. setup_sorting();
  994. if (parent_pattern != default_parent_pattern) {
  995. sort_dimension__add("parent");
  996. sort_parent.elide = 1;
  997. } else
  998. exclude_other = 0;
  999. /*
  1000. * Any (unrecognized) arguments left?
  1001. */
  1002. if (argc)
  1003. usage_with_options(report_usage, options);
  1004. setup_pager();
  1005. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  1006. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  1007. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  1008. if (field_sep && *field_sep == '.') {
  1009. fputs("'.' is the only non valid --field-separator argument\n",
  1010. stderr);
  1011. exit(129);
  1012. }
  1013. return __cmd_report();
  1014. }