builtin-report.c 28 KB

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