builtin-report.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. return kernel_maps__find_symbol(ip, mapp);
  313. }
  314. dump_printf(" ...... dso: %s\n",
  315. map ? map->dso->long_name : "<not found>");
  316. dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  317. *ipp = ip;
  318. return map ? map->dso->find_symbol(map->dso, ip) : NULL;
  319. }
  320. static int call__match(struct symbol *sym)
  321. {
  322. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  323. return 1;
  324. return 0;
  325. }
  326. static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
  327. struct ip_callchain *chain,
  328. struct symbol **parent)
  329. {
  330. u64 context = PERF_CONTEXT_MAX;
  331. struct symbol **syms = NULL;
  332. unsigned int i;
  333. if (callchain) {
  334. syms = calloc(chain->nr, sizeof(*syms));
  335. if (!syms) {
  336. fprintf(stderr, "Can't allocate memory for symbols\n");
  337. exit(-1);
  338. }
  339. }
  340. for (i = 0; i < chain->nr; i++) {
  341. u64 ip = chain->ips[i];
  342. struct symbol *sym = NULL;
  343. if (ip >= PERF_CONTEXT_MAX) {
  344. context = ip;
  345. continue;
  346. }
  347. switch (context) {
  348. case PERF_CONTEXT_HV:
  349. break;
  350. case PERF_CONTEXT_KERNEL:
  351. sym = kernel_maps__find_symbol(ip, &map);
  352. break;
  353. default:
  354. sym = resolve_symbol(thread, &map, &ip);
  355. break;
  356. }
  357. if (sym) {
  358. if (sort__has_parent && !*parent && call__match(sym))
  359. *parent = sym;
  360. if (!callchain)
  361. break;
  362. syms[i] = sym;
  363. }
  364. }
  365. return syms;
  366. }
  367. /*
  368. * collect histogram counts
  369. */
  370. static int
  371. hist_entry__add(struct thread *thread, struct map *map,
  372. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  373. char level, u64 count)
  374. {
  375. struct symbol **syms = NULL, *parent = NULL;
  376. bool hit;
  377. struct hist_entry *he;
  378. if ((sort__has_parent || callchain) && chain)
  379. syms = resolve_callchain(thread, map, chain, &parent);
  380. he = __hist_entry__add(thread, map, sym, parent,
  381. ip, count, level, &hit);
  382. if (he == NULL)
  383. return -ENOMEM;
  384. if (hit)
  385. he->count += count;
  386. if (callchain) {
  387. if (!hit)
  388. callchain_init(&he->callchain);
  389. append_chain(&he->callchain, chain, syms);
  390. free(syms);
  391. }
  392. return 0;
  393. }
  394. static size_t output__fprintf(FILE *fp, u64 total_samples)
  395. {
  396. struct hist_entry *pos;
  397. struct sort_entry *se;
  398. struct rb_node *nd;
  399. size_t ret = 0;
  400. unsigned int width;
  401. char *col_width = col_width_list_str;
  402. int raw_printing_style;
  403. raw_printing_style = !strcmp(pretty_printing_style, "raw");
  404. init_rem_hits();
  405. fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
  406. fprintf(fp, "#\n");
  407. fprintf(fp, "# Overhead");
  408. if (show_nr_samples) {
  409. if (field_sep)
  410. fprintf(fp, "%cSamples", *field_sep);
  411. else
  412. fputs(" Samples ", fp);
  413. }
  414. list_for_each_entry(se, &hist_entry__sort_list, list) {
  415. if (se->elide)
  416. continue;
  417. if (field_sep) {
  418. fprintf(fp, "%c%s", *field_sep, se->header);
  419. continue;
  420. }
  421. width = strlen(se->header);
  422. if (se->width) {
  423. if (col_width_list_str) {
  424. if (col_width) {
  425. *se->width = atoi(col_width);
  426. col_width = strchr(col_width, ',');
  427. if (col_width)
  428. ++col_width;
  429. }
  430. }
  431. width = *se->width = max(*se->width, width);
  432. }
  433. fprintf(fp, " %*s", width, se->header);
  434. }
  435. fprintf(fp, "\n");
  436. if (field_sep)
  437. goto print_entries;
  438. fprintf(fp, "# ........");
  439. if (show_nr_samples)
  440. fprintf(fp, " ..........");
  441. list_for_each_entry(se, &hist_entry__sort_list, list) {
  442. unsigned int i;
  443. if (se->elide)
  444. continue;
  445. fprintf(fp, " ");
  446. if (se->width)
  447. width = *se->width;
  448. else
  449. width = strlen(se->header);
  450. for (i = 0; i < width; i++)
  451. fprintf(fp, ".");
  452. }
  453. fprintf(fp, "\n");
  454. fprintf(fp, "#\n");
  455. print_entries:
  456. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  457. pos = rb_entry(nd, struct hist_entry, rb_node);
  458. ret += hist_entry__fprintf(fp, pos, total_samples);
  459. }
  460. if (sort_order == default_sort_order &&
  461. parent_pattern == default_parent_pattern) {
  462. fprintf(fp, "#\n");
  463. fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
  464. fprintf(fp, "#\n");
  465. }
  466. fprintf(fp, "\n");
  467. free(rem_sq_bracket);
  468. if (show_threads)
  469. perf_read_values_display(fp, &show_threads_values,
  470. raw_printing_style);
  471. return ret;
  472. }
  473. static int validate_chain(struct ip_callchain *chain, event_t *event)
  474. {
  475. unsigned int chain_size;
  476. chain_size = event->header.size;
  477. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  478. if (chain->nr*sizeof(u64) > chain_size)
  479. return -1;
  480. return 0;
  481. }
  482. static int
  483. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  484. {
  485. char level;
  486. struct symbol *sym = NULL;
  487. struct thread *thread;
  488. u64 ip = event->ip.ip;
  489. u64 period = 1;
  490. struct map *map = NULL;
  491. void *more_data = event->ip.__more_data;
  492. struct ip_callchain *chain = NULL;
  493. int cpumode;
  494. thread = threads__findnew(event->ip.pid, &threads, &last_match);
  495. if (sample_type & PERF_SAMPLE_PERIOD) {
  496. period = *(u64 *)more_data;
  497. more_data += sizeof(u64);
  498. }
  499. dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
  500. (void *)(offset + head),
  501. (void *)(long)(event->header.size),
  502. event->header.misc,
  503. event->ip.pid, event->ip.tid,
  504. (void *)(long)ip,
  505. (long long)period);
  506. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  507. unsigned int i;
  508. chain = (void *)more_data;
  509. dump_printf("... chain: nr:%Lu\n", chain->nr);
  510. if (validate_chain(chain, event) < 0) {
  511. eprintf("call-chain problem with event, skipping it.\n");
  512. return 0;
  513. }
  514. if (dump_trace) {
  515. for (i = 0; i < chain->nr; i++)
  516. dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
  517. }
  518. }
  519. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  520. if (thread == NULL) {
  521. eprintf("problem processing %d event, skipping it.\n",
  522. event->header.type);
  523. return -1;
  524. }
  525. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  526. return 0;
  527. cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  528. if (cpumode == PERF_RECORD_MISC_KERNEL) {
  529. level = 'k';
  530. sym = kernel_maps__find_symbol(ip, &map);
  531. dump_printf(" ...... dso: %s\n",
  532. map ? map->dso->long_name : "<not found>");
  533. } else if (cpumode == PERF_RECORD_MISC_USER) {
  534. level = '.';
  535. sym = resolve_symbol(thread, &map, &ip);
  536. } else {
  537. level = 'H';
  538. dump_printf(" ...... dso: [hypervisor]\n");
  539. }
  540. if (dso_list &&
  541. (!map || !map->dso ||
  542. !(strlist__has_entry(dso_list, map->dso->short_name) ||
  543. (map->dso->short_name != map->dso->long_name &&
  544. strlist__has_entry(dso_list, map->dso->long_name)))))
  545. return 0;
  546. if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
  547. return 0;
  548. if (hist_entry__add(thread, map, sym, ip,
  549. chain, level, period)) {
  550. eprintf("problem incrementing symbol count, skipping event\n");
  551. return -1;
  552. }
  553. total += period;
  554. return 0;
  555. }
  556. static int
  557. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  558. {
  559. struct thread *thread;
  560. struct map *map = map__new(&event->mmap, cwd, cwdlen);
  561. thread = threads__findnew(event->mmap.pid, &threads, &last_match);
  562. dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
  563. (void *)(offset + head),
  564. (void *)(long)(event->header.size),
  565. event->mmap.pid,
  566. event->mmap.tid,
  567. (void *)(long)event->mmap.start,
  568. (void *)(long)event->mmap.len,
  569. (void *)(long)event->mmap.pgoff,
  570. event->mmap.filename);
  571. if (thread == NULL || map == NULL) {
  572. dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
  573. return 0;
  574. }
  575. thread__insert_map(thread, map);
  576. total_mmap++;
  577. return 0;
  578. }
  579. static int
  580. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  581. {
  582. struct thread *thread;
  583. thread = threads__findnew(event->comm.pid, &threads, &last_match);
  584. dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
  585. (void *)(offset + head),
  586. (void *)(long)(event->header.size),
  587. event->comm.comm, event->comm.pid);
  588. if (thread == NULL ||
  589. thread__set_comm_adjust(thread, event->comm.comm)) {
  590. dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
  591. return -1;
  592. }
  593. total_comm++;
  594. return 0;
  595. }
  596. static int
  597. process_task_event(event_t *event, unsigned long offset, unsigned long head)
  598. {
  599. struct thread *thread;
  600. struct thread *parent;
  601. thread = threads__findnew(event->fork.pid, &threads, &last_match);
  602. parent = threads__findnew(event->fork.ppid, &threads, &last_match);
  603. dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
  604. (void *)(offset + head),
  605. (void *)(long)(event->header.size),
  606. event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
  607. event->fork.pid, event->fork.tid,
  608. event->fork.ppid, event->fork.ptid);
  609. /*
  610. * A thread clone will have the same PID for both
  611. * parent and child.
  612. */
  613. if (thread == parent)
  614. return 0;
  615. if (event->header.type == PERF_RECORD_EXIT)
  616. return 0;
  617. if (!thread || !parent || thread__fork(thread, parent)) {
  618. dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
  619. return -1;
  620. }
  621. total_fork++;
  622. return 0;
  623. }
  624. static int
  625. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  626. {
  627. dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
  628. (void *)(offset + head),
  629. (void *)(long)(event->header.size),
  630. event->lost.id,
  631. event->lost.lost);
  632. total_lost += event->lost.lost;
  633. return 0;
  634. }
  635. static int
  636. process_read_event(event_t *event, unsigned long offset, unsigned long head)
  637. {
  638. struct perf_event_attr *attr;
  639. attr = perf_header__find_attr(event->read.id, header);
  640. if (show_threads) {
  641. const char *name = attr ? __event_name(attr->type, attr->config)
  642. : "unknown";
  643. perf_read_values_add_value(&show_threads_values,
  644. event->read.pid, event->read.tid,
  645. event->read.id,
  646. name,
  647. event->read.value);
  648. }
  649. dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
  650. (void *)(offset + head),
  651. (void *)(long)(event->header.size),
  652. event->read.pid,
  653. event->read.tid,
  654. attr ? __event_name(attr->type, attr->config)
  655. : "FAIL",
  656. event->read.value);
  657. return 0;
  658. }
  659. static int
  660. process_event(event_t *event, unsigned long offset, unsigned long head)
  661. {
  662. trace_event(event);
  663. switch (event->header.type) {
  664. case PERF_RECORD_SAMPLE:
  665. return process_sample_event(event, offset, head);
  666. case PERF_RECORD_MMAP:
  667. return process_mmap_event(event, offset, head);
  668. case PERF_RECORD_COMM:
  669. return process_comm_event(event, offset, head);
  670. case PERF_RECORD_FORK:
  671. case PERF_RECORD_EXIT:
  672. return process_task_event(event, offset, head);
  673. case PERF_RECORD_LOST:
  674. return process_lost_event(event, offset, head);
  675. case PERF_RECORD_READ:
  676. return process_read_event(event, offset, head);
  677. /*
  678. * We dont process them right now but they are fine:
  679. */
  680. case PERF_RECORD_THROTTLE:
  681. case PERF_RECORD_UNTHROTTLE:
  682. return 0;
  683. default:
  684. return -1;
  685. }
  686. return 0;
  687. }
  688. static int __cmd_report(void)
  689. {
  690. int ret, rc = EXIT_FAILURE;
  691. unsigned long offset = 0;
  692. unsigned long head, shift;
  693. struct stat input_stat;
  694. struct thread *idle;
  695. event_t *event;
  696. uint32_t size;
  697. char *buf;
  698. idle = register_idle_thread(&threads, &last_match);
  699. thread__comm_adjust(idle);
  700. if (show_threads)
  701. perf_read_values_init(&show_threads_values);
  702. input = open(input_name, O_RDONLY);
  703. if (input < 0) {
  704. fprintf(stderr, " failed to open file: %s", input_name);
  705. if (!strcmp(input_name, "perf.data"))
  706. fprintf(stderr, " (try 'perf record' first)");
  707. fprintf(stderr, "\n");
  708. exit(-1);
  709. }
  710. ret = fstat(input, &input_stat);
  711. if (ret < 0) {
  712. perror("failed to stat file");
  713. exit(-1);
  714. }
  715. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  716. fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
  717. exit(-1);
  718. }
  719. if (!input_stat.st_size) {
  720. fprintf(stderr, "zero-sized file, nothing to do!\n");
  721. exit(0);
  722. }
  723. header = perf_header__read(input);
  724. head = header->data_offset;
  725. sample_type = perf_header__sample_type(header);
  726. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  727. if (sort__has_parent) {
  728. fprintf(stderr, "selected --sort parent, but no"
  729. " callchain data. Did you call"
  730. " perf record without -g?\n");
  731. exit(-1);
  732. }
  733. if (callchain) {
  734. fprintf(stderr, "selected -g but no callchain data."
  735. " Did you call perf record without"
  736. " -g?\n");
  737. exit(-1);
  738. }
  739. } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
  740. callchain = 1;
  741. if (register_callchain_param(&callchain_param) < 0) {
  742. fprintf(stderr, "Can't register callchain"
  743. " params\n");
  744. exit(-1);
  745. }
  746. }
  747. if (load_kernel() < 0) {
  748. perror("failed to load kernel symbols");
  749. return EXIT_FAILURE;
  750. }
  751. if (!full_paths) {
  752. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  753. perror("failed to get the current directory");
  754. return EXIT_FAILURE;
  755. }
  756. cwdlen = strlen(cwd);
  757. } else {
  758. cwd = NULL;
  759. cwdlen = 0;
  760. }
  761. shift = page_size * (head / page_size);
  762. offset += shift;
  763. head -= shift;
  764. remap:
  765. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  766. MAP_SHARED, input, offset);
  767. if (buf == MAP_FAILED) {
  768. perror("failed to mmap file");
  769. exit(-1);
  770. }
  771. more:
  772. event = (event_t *)(buf + head);
  773. size = event->header.size;
  774. if (!size)
  775. size = 8;
  776. if (head + event->header.size >= page_size * mmap_window) {
  777. int munmap_ret;
  778. shift = page_size * (head / page_size);
  779. munmap_ret = munmap(buf, page_size * mmap_window);
  780. assert(munmap_ret == 0);
  781. offset += shift;
  782. head -= shift;
  783. goto remap;
  784. }
  785. size = event->header.size;
  786. dump_printf("\n%p [%p]: event: %d\n",
  787. (void *)(offset + head),
  788. (void *)(long)event->header.size,
  789. event->header.type);
  790. if (!size || process_event(event, offset, head) < 0) {
  791. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  792. (void *)(offset + head),
  793. (void *)(long)(event->header.size),
  794. event->header.type);
  795. total_unknown++;
  796. /*
  797. * assume we lost track of the stream, check alignment, and
  798. * increment a single u64 in the hope to catch on again 'soon'.
  799. */
  800. if (unlikely(head & 7))
  801. head &= ~7ULL;
  802. size = 8;
  803. }
  804. head += size;
  805. if (offset + head >= header->data_offset + header->data_size)
  806. goto done;
  807. if (offset + head < (unsigned long)input_stat.st_size)
  808. goto more;
  809. done:
  810. rc = EXIT_SUCCESS;
  811. close(input);
  812. dump_printf(" IP events: %10ld\n", total);
  813. dump_printf(" mmap events: %10ld\n", total_mmap);
  814. dump_printf(" comm events: %10ld\n", total_comm);
  815. dump_printf(" fork events: %10ld\n", total_fork);
  816. dump_printf(" lost events: %10ld\n", total_lost);
  817. dump_printf(" unknown events: %10ld\n", total_unknown);
  818. if (dump_trace)
  819. return 0;
  820. if (verbose >= 3)
  821. threads__fprintf(stdout, &threads);
  822. if (verbose >= 2)
  823. dsos__fprintf(stdout);
  824. collapse__resort();
  825. output__resort(total);
  826. output__fprintf(stdout, total);
  827. if (show_threads)
  828. perf_read_values_destroy(&show_threads_values);
  829. return rc;
  830. }
  831. static int
  832. parse_callchain_opt(const struct option *opt __used, const char *arg,
  833. int unset __used)
  834. {
  835. char *tok;
  836. char *endptr;
  837. callchain = 1;
  838. if (!arg)
  839. return 0;
  840. tok = strtok((char *)arg, ",");
  841. if (!tok)
  842. return -1;
  843. /* get the output mode */
  844. if (!strncmp(tok, "graph", strlen(arg)))
  845. callchain_param.mode = CHAIN_GRAPH_ABS;
  846. else if (!strncmp(tok, "flat", strlen(arg)))
  847. callchain_param.mode = CHAIN_FLAT;
  848. else if (!strncmp(tok, "fractal", strlen(arg)))
  849. callchain_param.mode = CHAIN_GRAPH_REL;
  850. else if (!strncmp(tok, "none", strlen(arg))) {
  851. callchain_param.mode = CHAIN_NONE;
  852. callchain = 0;
  853. return 0;
  854. }
  855. else
  856. return -1;
  857. /* get the min percentage */
  858. tok = strtok(NULL, ",");
  859. if (!tok)
  860. goto setup;
  861. callchain_param.min_percent = strtod(tok, &endptr);
  862. if (tok == endptr)
  863. return -1;
  864. setup:
  865. if (register_callchain_param(&callchain_param) < 0) {
  866. fprintf(stderr, "Can't register callchain params\n");
  867. return -1;
  868. }
  869. return 0;
  870. }
  871. //static const char * const report_usage[] = {
  872. const char * const report_usage[] = {
  873. "perf report [<options>] <command>",
  874. NULL
  875. };
  876. static const struct option options[] = {
  877. OPT_STRING('i', "input", &input_name, "file",
  878. "input file name"),
  879. OPT_BOOLEAN('v', "verbose", &verbose,
  880. "be more verbose (show symbol address, etc)"),
  881. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  882. "dump raw trace in ASCII"),
  883. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  884. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  885. OPT_BOOLEAN('m', "modules", &modules,
  886. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  887. OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
  888. "Show a column with the number of samples"),
  889. OPT_BOOLEAN('T', "threads", &show_threads,
  890. "Show per-thread event counters"),
  891. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  892. "pretty printing style key: normal raw"),
  893. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  894. "sort by key(s): pid, comm, dso, symbol, parent"),
  895. OPT_BOOLEAN('P', "full-paths", &full_paths,
  896. "Don't shorten the pathnames taking into account the cwd"),
  897. OPT_STRING('p', "parent", &parent_pattern, "regex",
  898. "regex filter to identify parent, see: '--sort parent'"),
  899. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  900. "Only display entries with parent-match"),
  901. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  902. "Display callchains using output_type and min percent threshold. "
  903. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  904. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  905. "only consider symbols in these dsos"),
  906. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  907. "only consider symbols in these comms"),
  908. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  909. "only consider these symbols"),
  910. OPT_STRING('w', "column-widths", &col_width_list_str,
  911. "width[,width...]",
  912. "don't try to adjust column width, use these fixed values"),
  913. OPT_STRING('t', "field-separator", &field_sep, "separator",
  914. "separator for columns, no spaces will be added between "
  915. "columns '.' is reserved."),
  916. OPT_END()
  917. };
  918. static void setup_sorting(void)
  919. {
  920. char *tmp, *tok, *str = strdup(sort_order);
  921. for (tok = strtok_r(str, ", ", &tmp);
  922. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  923. if (sort_dimension__add(tok) < 0) {
  924. error("Unknown --sort key: `%s'", tok);
  925. usage_with_options(report_usage, options);
  926. }
  927. }
  928. free(str);
  929. }
  930. static void setup_list(struct strlist **list, const char *list_str,
  931. struct sort_entry *se, const char *list_name,
  932. FILE *fp)
  933. {
  934. if (list_str) {
  935. *list = strlist__new(true, list_str);
  936. if (!*list) {
  937. fprintf(stderr, "problems parsing %s list\n",
  938. list_name);
  939. exit(129);
  940. }
  941. if (strlist__nr_entries(*list) == 1) {
  942. fprintf(fp, "# %s: %s\n", list_name,
  943. strlist__entry(*list, 0)->s);
  944. se->elide = true;
  945. }
  946. }
  947. }
  948. int cmd_report(int argc, const char **argv, const char *prefix __used)
  949. {
  950. symbol__init();
  951. page_size = getpagesize();
  952. argc = parse_options(argc, argv, options, report_usage, 0);
  953. setup_sorting();
  954. if (parent_pattern != default_parent_pattern) {
  955. sort_dimension__add("parent");
  956. sort_parent.elide = 1;
  957. } else
  958. exclude_other = 0;
  959. /*
  960. * Any (unrecognized) arguments left?
  961. */
  962. if (argc)
  963. usage_with_options(report_usage, options);
  964. setup_pager();
  965. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  966. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  967. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  968. if (field_sep && *field_sep == '.') {
  969. fputs("'.' is the only non valid --field-separator argument\n",
  970. stderr);
  971. exit(129);
  972. }
  973. return __cmd_report();
  974. }