builtin-report.c 26 KB

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