builtin-report.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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. static char const *input_name = "perf.data";
  26. static char default_sort_order[] = "comm,dso,symbol";
  27. static char *sort_order = default_sort_order;
  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 char *field_sep;
  32. static int input;
  33. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  34. static int full_paths;
  35. static int show_nr_samples;
  36. static int show_threads;
  37. static struct perf_read_values show_threads_values;
  38. static char default_pretty_printing_style[] = "normal";
  39. static char *pretty_printing_style = default_pretty_printing_style;
  40. static unsigned long page_size;
  41. static unsigned long mmap_window = 32;
  42. static char default_parent_pattern[] = "^sys_|^do_page_fault";
  43. static char *parent_pattern = default_parent_pattern;
  44. static regex_t parent_regex;
  45. static int exclude_other = 1;
  46. static char callchain_default_opt[] = "fractal,0.5";
  47. static int callchain;
  48. static char __cwd[PATH_MAX];
  49. static char *cwd = __cwd;
  50. static int cwdlen;
  51. static struct rb_root threads;
  52. static struct thread *last_match;
  53. static struct perf_header *header;
  54. static
  55. struct callchain_param callchain_param = {
  56. .mode = CHAIN_GRAPH_REL,
  57. .min_percent = 0.5
  58. };
  59. static u64 sample_type;
  60. static int repsep_fprintf(FILE *fp, const char *fmt, ...)
  61. {
  62. int n;
  63. va_list ap;
  64. va_start(ap, fmt);
  65. if (!field_sep)
  66. n = vfprintf(fp, fmt, ap);
  67. else {
  68. char *bf = NULL;
  69. n = vasprintf(&bf, fmt, ap);
  70. if (n > 0) {
  71. char *sep = bf;
  72. while (1) {
  73. sep = strchr(sep, *field_sep);
  74. if (sep == NULL)
  75. break;
  76. *sep = '.';
  77. }
  78. }
  79. fputs(bf, fp);
  80. free(bf);
  81. }
  82. va_end(ap);
  83. return n;
  84. }
  85. static unsigned int dsos__col_width,
  86. comms__col_width,
  87. threads__col_width;
  88. /*
  89. * histogram, sorted on item, collects counts
  90. */
  91. static struct rb_root hist;
  92. struct hist_entry {
  93. struct rb_node rb_node;
  94. struct thread *thread;
  95. struct map *map;
  96. struct dso *dso;
  97. struct symbol *sym;
  98. struct symbol *parent;
  99. u64 ip;
  100. char level;
  101. struct callchain_node callchain;
  102. struct rb_root sorted_chain;
  103. u64 count;
  104. };
  105. /*
  106. * configurable sorting bits
  107. */
  108. struct sort_entry {
  109. struct list_head list;
  110. const char *header;
  111. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  112. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  113. size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
  114. unsigned int *width;
  115. bool elide;
  116. };
  117. static int64_t cmp_null(void *l, void *r)
  118. {
  119. if (!l && !r)
  120. return 0;
  121. else if (!l)
  122. return -1;
  123. else
  124. return 1;
  125. }
  126. /* --sort pid */
  127. static int64_t
  128. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  129. {
  130. return right->thread->pid - left->thread->pid;
  131. }
  132. static size_t
  133. sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
  134. {
  135. return repsep_fprintf(fp, "%*s:%5d", width - 6,
  136. self->thread->comm ?: "", self->thread->pid);
  137. }
  138. static struct sort_entry sort_thread = {
  139. .header = "Command: Pid",
  140. .cmp = sort__thread_cmp,
  141. .print = sort__thread_print,
  142. .width = &threads__col_width,
  143. };
  144. /* --sort comm */
  145. static int64_t
  146. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  147. {
  148. return right->thread->pid - left->thread->pid;
  149. }
  150. static int64_t
  151. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  152. {
  153. char *comm_l = left->thread->comm;
  154. char *comm_r = right->thread->comm;
  155. if (!comm_l || !comm_r)
  156. return cmp_null(comm_l, comm_r);
  157. return strcmp(comm_l, comm_r);
  158. }
  159. static size_t
  160. sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
  161. {
  162. return repsep_fprintf(fp, "%*s", width, self->thread->comm);
  163. }
  164. static struct sort_entry sort_comm = {
  165. .header = "Command",
  166. .cmp = sort__comm_cmp,
  167. .collapse = sort__comm_collapse,
  168. .print = sort__comm_print,
  169. .width = &comms__col_width,
  170. };
  171. /* --sort dso */
  172. static int64_t
  173. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  174. {
  175. struct dso *dso_l = left->dso;
  176. struct dso *dso_r = right->dso;
  177. if (!dso_l || !dso_r)
  178. return cmp_null(dso_l, dso_r);
  179. return strcmp(dso_l->name, dso_r->name);
  180. }
  181. static size_t
  182. sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
  183. {
  184. if (self->dso)
  185. return repsep_fprintf(fp, "%-*s", width, self->dso->name);
  186. return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
  187. }
  188. static struct sort_entry sort_dso = {
  189. .header = "Shared Object",
  190. .cmp = sort__dso_cmp,
  191. .print = sort__dso_print,
  192. .width = &dsos__col_width,
  193. };
  194. /* --sort symbol */
  195. static int64_t
  196. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  197. {
  198. u64 ip_l, ip_r;
  199. if (left->sym == right->sym)
  200. return 0;
  201. ip_l = left->sym ? left->sym->start : left->ip;
  202. ip_r = right->sym ? right->sym->start : right->ip;
  203. return (int64_t)(ip_r - ip_l);
  204. }
  205. static size_t
  206. sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
  207. {
  208. size_t ret = 0;
  209. if (verbose)
  210. ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
  211. dso__symtab_origin(self->dso));
  212. ret += repsep_fprintf(fp, "[%c] ", self->level);
  213. if (self->sym) {
  214. ret += repsep_fprintf(fp, "%s", self->sym->name);
  215. if (self->sym->module)
  216. ret += repsep_fprintf(fp, "\t[%s]",
  217. self->sym->module->name);
  218. } else {
  219. ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
  220. }
  221. return ret;
  222. }
  223. static struct sort_entry sort_sym = {
  224. .header = "Symbol",
  225. .cmp = sort__sym_cmp,
  226. .print = sort__sym_print,
  227. };
  228. /* --sort parent */
  229. static int64_t
  230. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  231. {
  232. struct symbol *sym_l = left->parent;
  233. struct symbol *sym_r = right->parent;
  234. if (!sym_l || !sym_r)
  235. return cmp_null(sym_l, sym_r);
  236. return strcmp(sym_l->name, sym_r->name);
  237. }
  238. static size_t
  239. sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
  240. {
  241. return repsep_fprintf(fp, "%-*s", width,
  242. self->parent ? self->parent->name : "[other]");
  243. }
  244. static unsigned int parent_symbol__col_width;
  245. static struct sort_entry sort_parent = {
  246. .header = "Parent symbol",
  247. .cmp = sort__parent_cmp,
  248. .print = sort__parent_print,
  249. .width = &parent_symbol__col_width,
  250. };
  251. static int sort__need_collapse = 0;
  252. static int sort__has_parent = 0;
  253. struct sort_dimension {
  254. const char *name;
  255. struct sort_entry *entry;
  256. int taken;
  257. };
  258. static struct sort_dimension sort_dimensions[] = {
  259. { .name = "pid", .entry = &sort_thread, },
  260. { .name = "comm", .entry = &sort_comm, },
  261. { .name = "dso", .entry = &sort_dso, },
  262. { .name = "symbol", .entry = &sort_sym, },
  263. { .name = "parent", .entry = &sort_parent, },
  264. };
  265. static LIST_HEAD(hist_entry__sort_list);
  266. static int sort_dimension__add(const char *tok)
  267. {
  268. unsigned int i;
  269. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  270. struct sort_dimension *sd = &sort_dimensions[i];
  271. if (sd->taken)
  272. continue;
  273. if (strncasecmp(tok, sd->name, strlen(tok)))
  274. continue;
  275. if (sd->entry->collapse)
  276. sort__need_collapse = 1;
  277. if (sd->entry == &sort_parent) {
  278. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  279. if (ret) {
  280. char err[BUFSIZ];
  281. regerror(ret, &parent_regex, err, sizeof(err));
  282. fprintf(stderr, "Invalid regex: %s\n%s",
  283. parent_pattern, err);
  284. exit(-1);
  285. }
  286. sort__has_parent = 1;
  287. }
  288. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  289. sd->taken = 1;
  290. return 0;
  291. }
  292. return -ESRCH;
  293. }
  294. static int64_t
  295. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  296. {
  297. struct sort_entry *se;
  298. int64_t cmp = 0;
  299. list_for_each_entry(se, &hist_entry__sort_list, list) {
  300. cmp = se->cmp(left, right);
  301. if (cmp)
  302. break;
  303. }
  304. return cmp;
  305. }
  306. static int64_t
  307. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  308. {
  309. struct sort_entry *se;
  310. int64_t cmp = 0;
  311. list_for_each_entry(se, &hist_entry__sort_list, list) {
  312. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  313. f = se->collapse ?: se->cmp;
  314. cmp = f(left, right);
  315. if (cmp)
  316. break;
  317. }
  318. return cmp;
  319. }
  320. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
  321. {
  322. int i;
  323. size_t ret = 0;
  324. ret += fprintf(fp, "%s", " ");
  325. for (i = 0; i < depth; i++)
  326. if (depth_mask & (1 << i))
  327. ret += fprintf(fp, "| ");
  328. else
  329. ret += fprintf(fp, " ");
  330. ret += fprintf(fp, "\n");
  331. return ret;
  332. }
  333. static size_t
  334. ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
  335. int depth_mask, int count, u64 total_samples,
  336. int hits)
  337. {
  338. int i;
  339. size_t ret = 0;
  340. ret += fprintf(fp, "%s", " ");
  341. for (i = 0; i < depth; i++) {
  342. if (depth_mask & (1 << i))
  343. ret += fprintf(fp, "|");
  344. else
  345. ret += fprintf(fp, " ");
  346. if (!count && i == depth - 1) {
  347. double percent;
  348. percent = hits * 100.0 / total_samples;
  349. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  350. } else
  351. ret += fprintf(fp, "%s", " ");
  352. }
  353. if (chain->sym)
  354. ret += fprintf(fp, "%s\n", chain->sym->name);
  355. else
  356. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  357. return ret;
  358. }
  359. static struct symbol *rem_sq_bracket;
  360. static struct callchain_list rem_hits;
  361. static void init_rem_hits(void)
  362. {
  363. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  364. if (!rem_sq_bracket) {
  365. fprintf(stderr, "Not enough memory to display remaining hits\n");
  366. return;
  367. }
  368. strcpy(rem_sq_bracket->name, "[...]");
  369. rem_hits.sym = rem_sq_bracket;
  370. }
  371. static size_t
  372. callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  373. u64 total_samples, int depth, int depth_mask)
  374. {
  375. struct rb_node *node, *next;
  376. struct callchain_node *child;
  377. struct callchain_list *chain;
  378. int new_depth_mask = depth_mask;
  379. u64 new_total;
  380. u64 remaining;
  381. size_t ret = 0;
  382. int i;
  383. if (callchain_param.mode == CHAIN_GRAPH_REL)
  384. new_total = self->children_hit;
  385. else
  386. new_total = total_samples;
  387. remaining = new_total;
  388. node = rb_first(&self->rb_root);
  389. while (node) {
  390. u64 cumul;
  391. child = rb_entry(node, struct callchain_node, rb_node);
  392. cumul = cumul_hits(child);
  393. remaining -= cumul;
  394. /*
  395. * The depth mask manages the output of pipes that show
  396. * the depth. We don't want to keep the pipes of the current
  397. * level for the last child of this depth.
  398. * Except if we have remaining filtered hits. They will
  399. * supersede the last child
  400. */
  401. next = rb_next(node);
  402. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  403. new_depth_mask &= ~(1 << (depth - 1));
  404. /*
  405. * But we keep the older depth mask for the line seperator
  406. * to keep the level link until we reach the last child
  407. */
  408. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
  409. i = 0;
  410. list_for_each_entry(chain, &child->val, list) {
  411. if (chain->ip >= PERF_CONTEXT_MAX)
  412. continue;
  413. ret += ipchain__fprintf_graph(fp, chain, depth,
  414. new_depth_mask, i++,
  415. new_total,
  416. cumul);
  417. }
  418. ret += callchain__fprintf_graph(fp, child, new_total,
  419. depth + 1,
  420. new_depth_mask | (1 << depth));
  421. node = next;
  422. }
  423. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  424. remaining && remaining != new_total) {
  425. if (!rem_sq_bracket)
  426. return ret;
  427. new_depth_mask &= ~(1 << (depth - 1));
  428. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  429. new_depth_mask, 0, new_total,
  430. remaining);
  431. }
  432. return ret;
  433. }
  434. static size_t
  435. callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  436. u64 total_samples)
  437. {
  438. struct callchain_list *chain;
  439. size_t ret = 0;
  440. if (!self)
  441. return 0;
  442. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  443. list_for_each_entry(chain, &self->val, list) {
  444. if (chain->ip >= PERF_CONTEXT_MAX)
  445. continue;
  446. if (chain->sym)
  447. ret += fprintf(fp, " %s\n", chain->sym->name);
  448. else
  449. ret += fprintf(fp, " %p\n",
  450. (void *)(long)chain->ip);
  451. }
  452. return ret;
  453. }
  454. static size_t
  455. hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  456. u64 total_samples)
  457. {
  458. struct rb_node *rb_node;
  459. struct callchain_node *chain;
  460. size_t ret = 0;
  461. rb_node = rb_first(&self->sorted_chain);
  462. while (rb_node) {
  463. double percent;
  464. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  465. percent = chain->hit * 100.0 / total_samples;
  466. switch (callchain_param.mode) {
  467. case CHAIN_FLAT:
  468. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  469. percent);
  470. ret += callchain__fprintf_flat(fp, chain, total_samples);
  471. break;
  472. case CHAIN_GRAPH_ABS: /* Falldown */
  473. case CHAIN_GRAPH_REL:
  474. ret += callchain__fprintf_graph(fp, chain,
  475. total_samples, 1, 1);
  476. case CHAIN_NONE:
  477. default:
  478. break;
  479. }
  480. ret += fprintf(fp, "\n");
  481. rb_node = rb_next(rb_node);
  482. }
  483. return ret;
  484. }
  485. static size_t
  486. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  487. {
  488. struct sort_entry *se;
  489. size_t ret;
  490. if (exclude_other && !self->parent)
  491. return 0;
  492. if (total_samples)
  493. ret = percent_color_fprintf(fp,
  494. field_sep ? "%.2f" : " %6.2f%%",
  495. (self->count * 100.0) / total_samples);
  496. else
  497. ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
  498. if (show_nr_samples) {
  499. if (field_sep)
  500. fprintf(fp, "%c%lld", *field_sep, self->count);
  501. else
  502. fprintf(fp, "%11lld", self->count);
  503. }
  504. list_for_each_entry(se, &hist_entry__sort_list, list) {
  505. if (se->elide)
  506. continue;
  507. fprintf(fp, "%s", field_sep ?: " ");
  508. ret += se->print(fp, self, se->width ? *se->width : 0);
  509. }
  510. ret += fprintf(fp, "\n");
  511. if (callchain)
  512. hist_entry_callchain__fprintf(fp, self, total_samples);
  513. return ret;
  514. }
  515. /*
  516. *
  517. */
  518. static void dso__calc_col_width(struct dso *self)
  519. {
  520. if (!col_width_list_str && !field_sep &&
  521. (!dso_list || strlist__has_entry(dso_list, self->name))) {
  522. unsigned int slen = strlen(self->name);
  523. if (slen > dsos__col_width)
  524. dsos__col_width = slen;
  525. }
  526. self->slen_calculated = 1;
  527. }
  528. static int thread__set_comm_adjust(struct thread *self, const char *comm)
  529. {
  530. int ret = thread__set_comm(self, comm);
  531. if (ret)
  532. return ret;
  533. if (!col_width_list_str && !field_sep &&
  534. (!comm_list || strlist__has_entry(comm_list, comm))) {
  535. unsigned int slen = strlen(comm);
  536. if (slen > comms__col_width) {
  537. comms__col_width = slen;
  538. threads__col_width = slen + 6;
  539. }
  540. }
  541. return 0;
  542. }
  543. static struct symbol *
  544. resolve_symbol(struct thread *thread, struct map **mapp,
  545. struct dso **dsop, u64 *ipp)
  546. {
  547. struct dso *dso = dsop ? *dsop : NULL;
  548. struct map *map = mapp ? *mapp : NULL;
  549. u64 ip = *ipp;
  550. if (!thread)
  551. return NULL;
  552. if (dso)
  553. goto got_dso;
  554. if (map)
  555. goto got_map;
  556. map = thread__find_map(thread, ip);
  557. if (map != NULL) {
  558. /*
  559. * We have to do this here as we may have a dso
  560. * with no symbol hit that has a name longer than
  561. * the ones with symbols sampled.
  562. */
  563. if (!sort_dso.elide && !map->dso->slen_calculated)
  564. dso__calc_col_width(map->dso);
  565. if (mapp)
  566. *mapp = map;
  567. got_map:
  568. ip = map->map_ip(map, ip);
  569. dso = map->dso;
  570. } else {
  571. /*
  572. * If this is outside of all known maps,
  573. * and is a negative address, try to look it
  574. * up in the kernel dso, as it might be a
  575. * vsyscall (which executes in user-mode):
  576. */
  577. if ((long long)ip < 0)
  578. dso = kernel_dso;
  579. }
  580. dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  581. dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  582. *ipp = ip;
  583. if (dsop)
  584. *dsop = dso;
  585. if (!dso)
  586. return NULL;
  587. got_dso:
  588. return dso->find_symbol(dso, ip);
  589. }
  590. static int call__match(struct symbol *sym)
  591. {
  592. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  593. return 1;
  594. return 0;
  595. }
  596. static struct symbol **
  597. resolve_callchain(struct thread *thread, struct map *map __used,
  598. struct ip_callchain *chain, struct hist_entry *entry)
  599. {
  600. u64 context = PERF_CONTEXT_MAX;
  601. struct symbol **syms = NULL;
  602. unsigned int i;
  603. if (callchain) {
  604. syms = calloc(chain->nr, sizeof(*syms));
  605. if (!syms) {
  606. fprintf(stderr, "Can't allocate memory for symbols\n");
  607. exit(-1);
  608. }
  609. }
  610. for (i = 0; i < chain->nr; i++) {
  611. u64 ip = chain->ips[i];
  612. struct dso *dso = NULL;
  613. struct symbol *sym;
  614. if (ip >= PERF_CONTEXT_MAX) {
  615. context = ip;
  616. continue;
  617. }
  618. switch (context) {
  619. case PERF_CONTEXT_HV:
  620. dso = hypervisor_dso;
  621. break;
  622. case PERF_CONTEXT_KERNEL:
  623. dso = kernel_dso;
  624. break;
  625. default:
  626. break;
  627. }
  628. sym = resolve_symbol(thread, NULL, &dso, &ip);
  629. if (sym) {
  630. if (sort__has_parent && call__match(sym) &&
  631. !entry->parent)
  632. entry->parent = sym;
  633. if (!callchain)
  634. break;
  635. syms[i] = sym;
  636. }
  637. }
  638. return syms;
  639. }
  640. /*
  641. * collect histogram counts
  642. */
  643. static int
  644. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  645. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  646. char level, u64 count)
  647. {
  648. struct rb_node **p = &hist.rb_node;
  649. struct rb_node *parent = NULL;
  650. struct hist_entry *he;
  651. struct symbol **syms = NULL;
  652. struct hist_entry entry = {
  653. .thread = thread,
  654. .map = map,
  655. .dso = dso,
  656. .sym = sym,
  657. .ip = ip,
  658. .level = level,
  659. .count = count,
  660. .parent = NULL,
  661. .sorted_chain = RB_ROOT
  662. };
  663. int cmp;
  664. if ((sort__has_parent || callchain) && chain)
  665. syms = resolve_callchain(thread, map, chain, &entry);
  666. while (*p != NULL) {
  667. parent = *p;
  668. he = rb_entry(parent, struct hist_entry, rb_node);
  669. cmp = hist_entry__cmp(&entry, he);
  670. if (!cmp) {
  671. he->count += count;
  672. if (callchain) {
  673. append_chain(&he->callchain, chain, syms);
  674. free(syms);
  675. }
  676. return 0;
  677. }
  678. if (cmp < 0)
  679. p = &(*p)->rb_left;
  680. else
  681. p = &(*p)->rb_right;
  682. }
  683. he = malloc(sizeof(*he));
  684. if (!he)
  685. return -ENOMEM;
  686. *he = entry;
  687. if (callchain) {
  688. callchain_init(&he->callchain);
  689. append_chain(&he->callchain, chain, syms);
  690. free(syms);
  691. }
  692. rb_link_node(&he->rb_node, parent, p);
  693. rb_insert_color(&he->rb_node, &hist);
  694. return 0;
  695. }
  696. static void hist_entry__free(struct hist_entry *he)
  697. {
  698. free(he);
  699. }
  700. /*
  701. * collapse the histogram
  702. */
  703. static struct rb_root collapse_hists;
  704. static void collapse__insert_entry(struct hist_entry *he)
  705. {
  706. struct rb_node **p = &collapse_hists.rb_node;
  707. struct rb_node *parent = NULL;
  708. struct hist_entry *iter;
  709. int64_t cmp;
  710. while (*p != NULL) {
  711. parent = *p;
  712. iter = rb_entry(parent, struct hist_entry, rb_node);
  713. cmp = hist_entry__collapse(iter, he);
  714. if (!cmp) {
  715. iter->count += he->count;
  716. hist_entry__free(he);
  717. return;
  718. }
  719. if (cmp < 0)
  720. p = &(*p)->rb_left;
  721. else
  722. p = &(*p)->rb_right;
  723. }
  724. rb_link_node(&he->rb_node, parent, p);
  725. rb_insert_color(&he->rb_node, &collapse_hists);
  726. }
  727. static void collapse__resort(void)
  728. {
  729. struct rb_node *next;
  730. struct hist_entry *n;
  731. if (!sort__need_collapse)
  732. return;
  733. next = rb_first(&hist);
  734. while (next) {
  735. n = rb_entry(next, struct hist_entry, rb_node);
  736. next = rb_next(&n->rb_node);
  737. rb_erase(&n->rb_node, &hist);
  738. collapse__insert_entry(n);
  739. }
  740. }
  741. /*
  742. * reverse the map, sort on count.
  743. */
  744. static struct rb_root output_hists;
  745. static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
  746. {
  747. struct rb_node **p = &output_hists.rb_node;
  748. struct rb_node *parent = NULL;
  749. struct hist_entry *iter;
  750. if (callchain)
  751. callchain_param.sort(&he->sorted_chain, &he->callchain,
  752. min_callchain_hits, &callchain_param);
  753. while (*p != NULL) {
  754. parent = *p;
  755. iter = rb_entry(parent, struct hist_entry, rb_node);
  756. if (he->count > iter->count)
  757. p = &(*p)->rb_left;
  758. else
  759. p = &(*p)->rb_right;
  760. }
  761. rb_link_node(&he->rb_node, parent, p);
  762. rb_insert_color(&he->rb_node, &output_hists);
  763. }
  764. static void output__resort(u64 total_samples)
  765. {
  766. struct rb_node *next;
  767. struct hist_entry *n;
  768. struct rb_root *tree = &hist;
  769. u64 min_callchain_hits;
  770. min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
  771. if (sort__need_collapse)
  772. tree = &collapse_hists;
  773. next = rb_first(tree);
  774. while (next) {
  775. n = rb_entry(next, struct hist_entry, rb_node);
  776. next = rb_next(&n->rb_node);
  777. rb_erase(&n->rb_node, tree);
  778. output__insert_entry(n, min_callchain_hits);
  779. }
  780. }
  781. static size_t output__fprintf(FILE *fp, u64 total_samples)
  782. {
  783. struct hist_entry *pos;
  784. struct sort_entry *se;
  785. struct rb_node *nd;
  786. size_t ret = 0;
  787. unsigned int width;
  788. char *col_width = col_width_list_str;
  789. int raw_printing_style;
  790. raw_printing_style = !strcmp(pretty_printing_style, "raw");
  791. init_rem_hits();
  792. fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
  793. fprintf(fp, "#\n");
  794. fprintf(fp, "# Overhead");
  795. if (show_nr_samples) {
  796. if (field_sep)
  797. fprintf(fp, "%cSamples", *field_sep);
  798. else
  799. fputs(" Samples ", fp);
  800. }
  801. list_for_each_entry(se, &hist_entry__sort_list, list) {
  802. if (se->elide)
  803. continue;
  804. if (field_sep) {
  805. fprintf(fp, "%c%s", *field_sep, se->header);
  806. continue;
  807. }
  808. width = strlen(se->header);
  809. if (se->width) {
  810. if (col_width_list_str) {
  811. if (col_width) {
  812. *se->width = atoi(col_width);
  813. col_width = strchr(col_width, ',');
  814. if (col_width)
  815. ++col_width;
  816. }
  817. }
  818. width = *se->width = max(*se->width, width);
  819. }
  820. fprintf(fp, " %*s", width, se->header);
  821. }
  822. fprintf(fp, "\n");
  823. if (field_sep)
  824. goto print_entries;
  825. fprintf(fp, "# ........");
  826. if (show_nr_samples)
  827. fprintf(fp, " ..........");
  828. list_for_each_entry(se, &hist_entry__sort_list, list) {
  829. unsigned int i;
  830. if (se->elide)
  831. continue;
  832. fprintf(fp, " ");
  833. if (se->width)
  834. width = *se->width;
  835. else
  836. width = strlen(se->header);
  837. for (i = 0; i < width; i++)
  838. fprintf(fp, ".");
  839. }
  840. fprintf(fp, "\n");
  841. fprintf(fp, "#\n");
  842. print_entries:
  843. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  844. pos = rb_entry(nd, struct hist_entry, rb_node);
  845. ret += hist_entry__fprintf(fp, pos, total_samples);
  846. }
  847. if (sort_order == default_sort_order &&
  848. parent_pattern == default_parent_pattern) {
  849. fprintf(fp, "#\n");
  850. fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
  851. fprintf(fp, "#\n");
  852. }
  853. fprintf(fp, "\n");
  854. free(rem_sq_bracket);
  855. if (show_threads)
  856. perf_read_values_display(fp, &show_threads_values,
  857. raw_printing_style);
  858. return ret;
  859. }
  860. static void register_idle_thread(void)
  861. {
  862. struct thread *thread = threads__findnew(0, &threads, &last_match);
  863. if (thread == NULL ||
  864. thread__set_comm_adjust(thread, "[idle]")) {
  865. fprintf(stderr, "problem inserting idle task.\n");
  866. exit(-1);
  867. }
  868. }
  869. static unsigned long total = 0,
  870. total_mmap = 0,
  871. total_comm = 0,
  872. total_fork = 0,
  873. total_unknown = 0,
  874. total_lost = 0;
  875. static int validate_chain(struct ip_callchain *chain, event_t *event)
  876. {
  877. unsigned int chain_size;
  878. chain_size = event->header.size;
  879. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  880. if (chain->nr*sizeof(u64) > chain_size)
  881. return -1;
  882. return 0;
  883. }
  884. static int
  885. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  886. {
  887. char level;
  888. int show = 0;
  889. struct dso *dso = NULL;
  890. struct thread *thread;
  891. u64 ip = event->ip.ip;
  892. u64 period = 1;
  893. struct map *map = NULL;
  894. void *more_data = event->ip.__more_data;
  895. struct ip_callchain *chain = NULL;
  896. int cpumode;
  897. thread = threads__findnew(event->ip.pid, &threads, &last_match);
  898. if (sample_type & PERF_SAMPLE_PERIOD) {
  899. period = *(u64 *)more_data;
  900. more_data += sizeof(u64);
  901. }
  902. dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
  903. (void *)(offset + head),
  904. (void *)(long)(event->header.size),
  905. event->header.misc,
  906. event->ip.pid, event->ip.tid,
  907. (void *)(long)ip,
  908. (long long)period);
  909. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  910. unsigned int i;
  911. chain = (void *)more_data;
  912. dump_printf("... chain: nr:%Lu\n", chain->nr);
  913. if (validate_chain(chain, event) < 0) {
  914. eprintf("call-chain problem with event, skipping it.\n");
  915. return 0;
  916. }
  917. if (dump_trace) {
  918. for (i = 0; i < chain->nr; i++)
  919. dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
  920. }
  921. }
  922. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  923. if (thread == NULL) {
  924. eprintf("problem processing %d event, skipping it.\n",
  925. event->header.type);
  926. return -1;
  927. }
  928. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  929. return 0;
  930. cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
  931. if (cpumode == PERF_EVENT_MISC_KERNEL) {
  932. show = SHOW_KERNEL;
  933. level = 'k';
  934. dso = kernel_dso;
  935. dump_printf(" ...... dso: %s\n", dso->name);
  936. } else if (cpumode == PERF_EVENT_MISC_USER) {
  937. show = SHOW_USER;
  938. level = '.';
  939. } else {
  940. show = SHOW_HV;
  941. level = 'H';
  942. dso = hypervisor_dso;
  943. dump_printf(" ...... dso: [hypervisor]\n");
  944. }
  945. if (show & show_mask) {
  946. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  947. if (dso_list && (!dso || !dso->name ||
  948. !strlist__has_entry(dso_list, dso->name)))
  949. return 0;
  950. if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
  951. return 0;
  952. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  953. eprintf("problem incrementing symbol count, skipping event\n");
  954. return -1;
  955. }
  956. }
  957. total += period;
  958. return 0;
  959. }
  960. static int
  961. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  962. {
  963. struct thread *thread;
  964. struct map *map = map__new(&event->mmap, cwd, cwdlen);
  965. thread = threads__findnew(event->mmap.pid, &threads, &last_match);
  966. dump_printf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
  967. (void *)(offset + head),
  968. (void *)(long)(event->header.size),
  969. event->mmap.pid,
  970. event->mmap.tid,
  971. (void *)(long)event->mmap.start,
  972. (void *)(long)event->mmap.len,
  973. (void *)(long)event->mmap.pgoff,
  974. event->mmap.filename);
  975. if (thread == NULL || map == NULL) {
  976. dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  977. return 0;
  978. }
  979. thread__insert_map(thread, map);
  980. total_mmap++;
  981. return 0;
  982. }
  983. static int
  984. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  985. {
  986. struct thread *thread;
  987. thread = threads__findnew(event->comm.pid, &threads, &last_match);
  988. dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  989. (void *)(offset + head),
  990. (void *)(long)(event->header.size),
  991. event->comm.comm, event->comm.pid);
  992. if (thread == NULL ||
  993. thread__set_comm_adjust(thread, event->comm.comm)) {
  994. dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
  995. return -1;
  996. }
  997. total_comm++;
  998. return 0;
  999. }
  1000. static int
  1001. process_task_event(event_t *event, unsigned long offset, unsigned long head)
  1002. {
  1003. struct thread *thread;
  1004. struct thread *parent;
  1005. thread = threads__findnew(event->fork.pid, &threads, &last_match);
  1006. parent = threads__findnew(event->fork.ppid, &threads, &last_match);
  1007. dump_printf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
  1008. (void *)(offset + head),
  1009. (void *)(long)(event->header.size),
  1010. event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
  1011. event->fork.pid, event->fork.tid,
  1012. event->fork.ppid, event->fork.ptid);
  1013. /*
  1014. * A thread clone will have the same PID for both
  1015. * parent and child.
  1016. */
  1017. if (thread == parent)
  1018. return 0;
  1019. if (event->header.type == PERF_EVENT_EXIT)
  1020. return 0;
  1021. if (!thread || !parent || thread__fork(thread, parent)) {
  1022. dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
  1023. return -1;
  1024. }
  1025. total_fork++;
  1026. return 0;
  1027. }
  1028. static int
  1029. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  1030. {
  1031. dump_printf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
  1032. (void *)(offset + head),
  1033. (void *)(long)(event->header.size),
  1034. event->lost.id,
  1035. event->lost.lost);
  1036. total_lost += event->lost.lost;
  1037. return 0;
  1038. }
  1039. static int
  1040. process_read_event(event_t *event, unsigned long offset, unsigned long head)
  1041. {
  1042. struct perf_counter_attr *attr;
  1043. attr = perf_header__find_attr(event->read.id, header);
  1044. if (show_threads) {
  1045. const char *name = attr ? __event_name(attr->type, attr->config)
  1046. : "unknown";
  1047. perf_read_values_add_value(&show_threads_values,
  1048. event->read.pid, event->read.tid,
  1049. event->read.id,
  1050. name,
  1051. event->read.value);
  1052. }
  1053. dump_printf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
  1054. (void *)(offset + head),
  1055. (void *)(long)(event->header.size),
  1056. event->read.pid,
  1057. event->read.tid,
  1058. attr ? __event_name(attr->type, attr->config)
  1059. : "FAIL",
  1060. event->read.value);
  1061. return 0;
  1062. }
  1063. static int
  1064. process_event(event_t *event, unsigned long offset, unsigned long head)
  1065. {
  1066. trace_event(event);
  1067. switch (event->header.type) {
  1068. case PERF_EVENT_SAMPLE:
  1069. return process_sample_event(event, offset, head);
  1070. case PERF_EVENT_MMAP:
  1071. return process_mmap_event(event, offset, head);
  1072. case PERF_EVENT_COMM:
  1073. return process_comm_event(event, offset, head);
  1074. case PERF_EVENT_FORK:
  1075. case PERF_EVENT_EXIT:
  1076. return process_task_event(event, offset, head);
  1077. case PERF_EVENT_LOST:
  1078. return process_lost_event(event, offset, head);
  1079. case PERF_EVENT_READ:
  1080. return process_read_event(event, offset, head);
  1081. /*
  1082. * We dont process them right now but they are fine:
  1083. */
  1084. case PERF_EVENT_THROTTLE:
  1085. case PERF_EVENT_UNTHROTTLE:
  1086. return 0;
  1087. default:
  1088. return -1;
  1089. }
  1090. return 0;
  1091. }
  1092. static int __cmd_report(void)
  1093. {
  1094. int ret, rc = EXIT_FAILURE;
  1095. unsigned long offset = 0;
  1096. unsigned long head, shift;
  1097. struct stat input_stat;
  1098. event_t *event;
  1099. uint32_t size;
  1100. char *buf;
  1101. register_idle_thread();
  1102. if (show_threads)
  1103. perf_read_values_init(&show_threads_values);
  1104. input = open(input_name, O_RDONLY);
  1105. if (input < 0) {
  1106. fprintf(stderr, " failed to open file: %s", input_name);
  1107. if (!strcmp(input_name, "perf.data"))
  1108. fprintf(stderr, " (try 'perf record' first)");
  1109. fprintf(stderr, "\n");
  1110. exit(-1);
  1111. }
  1112. ret = fstat(input, &input_stat);
  1113. if (ret < 0) {
  1114. perror("failed to stat file");
  1115. exit(-1);
  1116. }
  1117. if (!input_stat.st_size) {
  1118. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1119. exit(0);
  1120. }
  1121. header = perf_header__read(input);
  1122. head = header->data_offset;
  1123. sample_type = perf_header__sample_type(header);
  1124. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  1125. if (sort__has_parent) {
  1126. fprintf(stderr, "selected --sort parent, but no"
  1127. " callchain data. Did you call"
  1128. " perf record without -g?\n");
  1129. exit(-1);
  1130. }
  1131. if (callchain) {
  1132. fprintf(stderr, "selected -c but no callchain data."
  1133. " Did you call perf record without"
  1134. " -g?\n");
  1135. exit(-1);
  1136. }
  1137. } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
  1138. callchain = 1;
  1139. if (register_callchain_param(&callchain_param) < 0) {
  1140. fprintf(stderr, "Can't register callchain"
  1141. " params\n");
  1142. exit(-1);
  1143. }
  1144. }
  1145. if (load_kernel() < 0) {
  1146. perror("failed to load kernel symbols");
  1147. return EXIT_FAILURE;
  1148. }
  1149. if (!full_paths) {
  1150. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  1151. perror("failed to get the current directory");
  1152. return EXIT_FAILURE;
  1153. }
  1154. cwdlen = strlen(cwd);
  1155. } else {
  1156. cwd = NULL;
  1157. cwdlen = 0;
  1158. }
  1159. shift = page_size * (head / page_size);
  1160. offset += shift;
  1161. head -= shift;
  1162. remap:
  1163. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1164. MAP_SHARED, input, offset);
  1165. if (buf == MAP_FAILED) {
  1166. perror("failed to mmap file");
  1167. exit(-1);
  1168. }
  1169. more:
  1170. event = (event_t *)(buf + head);
  1171. size = event->header.size;
  1172. if (!size)
  1173. size = 8;
  1174. if (head + event->header.size >= page_size * mmap_window) {
  1175. int munmap_ret;
  1176. shift = page_size * (head / page_size);
  1177. munmap_ret = munmap(buf, page_size * mmap_window);
  1178. assert(munmap_ret == 0);
  1179. offset += shift;
  1180. head -= shift;
  1181. goto remap;
  1182. }
  1183. size = event->header.size;
  1184. dump_printf("\n%p [%p]: event: %d\n",
  1185. (void *)(offset + head),
  1186. (void *)(long)event->header.size,
  1187. event->header.type);
  1188. if (!size || process_event(event, offset, head) < 0) {
  1189. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  1190. (void *)(offset + head),
  1191. (void *)(long)(event->header.size),
  1192. event->header.type);
  1193. total_unknown++;
  1194. /*
  1195. * assume we lost track of the stream, check alignment, and
  1196. * increment a single u64 in the hope to catch on again 'soon'.
  1197. */
  1198. if (unlikely(head & 7))
  1199. head &= ~7ULL;
  1200. size = 8;
  1201. }
  1202. head += size;
  1203. if (offset + head >= header->data_offset + header->data_size)
  1204. goto done;
  1205. if (offset + head < (unsigned long)input_stat.st_size)
  1206. goto more;
  1207. done:
  1208. rc = EXIT_SUCCESS;
  1209. close(input);
  1210. dump_printf(" IP events: %10ld\n", total);
  1211. dump_printf(" mmap events: %10ld\n", total_mmap);
  1212. dump_printf(" comm events: %10ld\n", total_comm);
  1213. dump_printf(" fork events: %10ld\n", total_fork);
  1214. dump_printf(" lost events: %10ld\n", total_lost);
  1215. dump_printf(" unknown events: %10ld\n", total_unknown);
  1216. if (dump_trace)
  1217. return 0;
  1218. if (verbose >= 3)
  1219. threads__fprintf(stdout, &threads);
  1220. if (verbose >= 2)
  1221. dsos__fprintf(stdout);
  1222. collapse__resort();
  1223. output__resort(total);
  1224. output__fprintf(stdout, total);
  1225. if (show_threads)
  1226. perf_read_values_destroy(&show_threads_values);
  1227. return rc;
  1228. }
  1229. static int
  1230. parse_callchain_opt(const struct option *opt __used, const char *arg,
  1231. int unset __used)
  1232. {
  1233. char *tok;
  1234. char *endptr;
  1235. callchain = 1;
  1236. if (!arg)
  1237. return 0;
  1238. tok = strtok((char *)arg, ",");
  1239. if (!tok)
  1240. return -1;
  1241. /* get the output mode */
  1242. if (!strncmp(tok, "graph", strlen(arg)))
  1243. callchain_param.mode = CHAIN_GRAPH_ABS;
  1244. else if (!strncmp(tok, "flat", strlen(arg)))
  1245. callchain_param.mode = CHAIN_FLAT;
  1246. else if (!strncmp(tok, "fractal", strlen(arg)))
  1247. callchain_param.mode = CHAIN_GRAPH_REL;
  1248. else if (!strncmp(tok, "none", strlen(arg))) {
  1249. callchain_param.mode = CHAIN_NONE;
  1250. callchain = 0;
  1251. return 0;
  1252. }
  1253. else
  1254. return -1;
  1255. /* get the min percentage */
  1256. tok = strtok(NULL, ",");
  1257. if (!tok)
  1258. goto setup;
  1259. callchain_param.min_percent = strtod(tok, &endptr);
  1260. if (tok == endptr)
  1261. return -1;
  1262. setup:
  1263. if (register_callchain_param(&callchain_param) < 0) {
  1264. fprintf(stderr, "Can't register callchain params\n");
  1265. return -1;
  1266. }
  1267. return 0;
  1268. }
  1269. static const char * const report_usage[] = {
  1270. "perf report [<options>] <command>",
  1271. NULL
  1272. };
  1273. static const struct option options[] = {
  1274. OPT_STRING('i', "input", &input_name, "file",
  1275. "input file name"),
  1276. OPT_BOOLEAN('v', "verbose", &verbose,
  1277. "be more verbose (show symbol address, etc)"),
  1278. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1279. "dump raw trace in ASCII"),
  1280. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  1281. OPT_BOOLEAN('m', "modules", &modules,
  1282. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1283. OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
  1284. "Show a column with the number of samples"),
  1285. OPT_BOOLEAN('T', "threads", &show_threads,
  1286. "Show per-thread event counters"),
  1287. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  1288. "pretty printing style key: normal raw"),
  1289. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1290. "sort by key(s): pid, comm, dso, symbol, parent"),
  1291. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1292. "Don't shorten the pathnames taking into account the cwd"),
  1293. OPT_STRING('p', "parent", &parent_pattern, "regex",
  1294. "regex filter to identify parent, see: '--sort parent'"),
  1295. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  1296. "Only display entries with parent-match"),
  1297. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  1298. "Display callchains using output_type and min percent threshold. "
  1299. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  1300. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  1301. "only consider symbols in these dsos"),
  1302. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  1303. "only consider symbols in these comms"),
  1304. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  1305. "only consider these symbols"),
  1306. OPT_STRING('w', "column-widths", &col_width_list_str,
  1307. "width[,width...]",
  1308. "don't try to adjust column width, use these fixed values"),
  1309. OPT_STRING('t', "field-separator", &field_sep, "separator",
  1310. "separator for columns, no spaces will be added between "
  1311. "columns '.' is reserved."),
  1312. OPT_END()
  1313. };
  1314. static void setup_sorting(void)
  1315. {
  1316. char *tmp, *tok, *str = strdup(sort_order);
  1317. for (tok = strtok_r(str, ", ", &tmp);
  1318. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1319. if (sort_dimension__add(tok) < 0) {
  1320. error("Unknown --sort key: `%s'", tok);
  1321. usage_with_options(report_usage, options);
  1322. }
  1323. }
  1324. free(str);
  1325. }
  1326. static void setup_list(struct strlist **list, const char *list_str,
  1327. struct sort_entry *se, const char *list_name,
  1328. FILE *fp)
  1329. {
  1330. if (list_str) {
  1331. *list = strlist__new(true, list_str);
  1332. if (!*list) {
  1333. fprintf(stderr, "problems parsing %s list\n",
  1334. list_name);
  1335. exit(129);
  1336. }
  1337. if (strlist__nr_entries(*list) == 1) {
  1338. fprintf(fp, "# %s: %s\n", list_name,
  1339. strlist__entry(*list, 0)->s);
  1340. se->elide = true;
  1341. }
  1342. }
  1343. }
  1344. int cmd_report(int argc, const char **argv, const char *prefix __used)
  1345. {
  1346. symbol__init();
  1347. page_size = getpagesize();
  1348. argc = parse_options(argc, argv, options, report_usage, 0);
  1349. setup_sorting();
  1350. if (parent_pattern != default_parent_pattern) {
  1351. sort_dimension__add("parent");
  1352. sort_parent.elide = 1;
  1353. } else
  1354. exclude_other = 0;
  1355. /*
  1356. * Any (unrecognized) arguments left?
  1357. */
  1358. if (argc)
  1359. usage_with_options(report_usage, options);
  1360. setup_pager();
  1361. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  1362. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  1363. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  1364. if (field_sep && *field_sep == '.') {
  1365. fputs("'.' is the only non valid --field-separator argument\n",
  1366. stderr);
  1367. exit(129);
  1368. }
  1369. return __cmd_report();
  1370. }