builtin-report.c 25 KB

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