builtin-report.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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 struct symbol *
  529. resolve_symbol(struct thread *thread, struct map **mapp,
  530. struct dso **dsop, u64 *ipp)
  531. {
  532. struct dso *dso = dsop ? *dsop : NULL;
  533. struct map *map = mapp ? *mapp : NULL;
  534. u64 ip = *ipp;
  535. if (!thread)
  536. return NULL;
  537. if (dso)
  538. goto got_dso;
  539. if (map)
  540. goto got_map;
  541. map = thread__find_map(thread, ip);
  542. if (map != NULL) {
  543. /*
  544. * We have to do this here as we may have a dso
  545. * with no symbol hit that has a name longer than
  546. * the ones with symbols sampled.
  547. */
  548. if (!sort_dso.elide && !map->dso->slen_calculated)
  549. dso__calc_col_width(map->dso);
  550. if (mapp)
  551. *mapp = map;
  552. got_map:
  553. ip = map->map_ip(map, ip);
  554. dso = map->dso;
  555. } else {
  556. /*
  557. * If this is outside of all known maps,
  558. * and is a negative address, try to look it
  559. * up in the kernel dso, as it might be a
  560. * vsyscall (which executes in user-mode):
  561. */
  562. if ((long long)ip < 0)
  563. dso = kernel_dso;
  564. }
  565. dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  566. dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  567. *ipp = ip;
  568. if (dsop)
  569. *dsop = dso;
  570. if (!dso)
  571. return NULL;
  572. got_dso:
  573. return dso->find_symbol(dso, ip);
  574. }
  575. static int call__match(struct symbol *sym)
  576. {
  577. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  578. return 1;
  579. return 0;
  580. }
  581. static struct symbol **
  582. resolve_callchain(struct thread *thread, struct map *map __used,
  583. struct ip_callchain *chain, struct hist_entry *entry)
  584. {
  585. u64 context = PERF_CONTEXT_MAX;
  586. struct symbol **syms = NULL;
  587. unsigned int i;
  588. if (callchain) {
  589. syms = calloc(chain->nr, sizeof(*syms));
  590. if (!syms) {
  591. fprintf(stderr, "Can't allocate memory for symbols\n");
  592. exit(-1);
  593. }
  594. }
  595. for (i = 0; i < chain->nr; i++) {
  596. u64 ip = chain->ips[i];
  597. struct dso *dso = NULL;
  598. struct symbol *sym;
  599. if (ip >= PERF_CONTEXT_MAX) {
  600. context = ip;
  601. continue;
  602. }
  603. switch (context) {
  604. case PERF_CONTEXT_HV:
  605. dso = hypervisor_dso;
  606. break;
  607. case PERF_CONTEXT_KERNEL:
  608. dso = kernel_dso;
  609. break;
  610. default:
  611. break;
  612. }
  613. sym = resolve_symbol(thread, NULL, &dso, &ip);
  614. if (sym) {
  615. if (sort__has_parent && call__match(sym) &&
  616. !entry->parent)
  617. entry->parent = sym;
  618. if (!callchain)
  619. break;
  620. syms[i] = sym;
  621. }
  622. }
  623. return syms;
  624. }
  625. /*
  626. * collect histogram counts
  627. */
  628. static int
  629. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  630. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  631. char level, u64 count)
  632. {
  633. struct rb_node **p = &hist.rb_node;
  634. struct rb_node *parent = NULL;
  635. struct hist_entry *he;
  636. struct symbol **syms = NULL;
  637. struct hist_entry entry = {
  638. .thread = thread,
  639. .map = map,
  640. .dso = dso,
  641. .sym = sym,
  642. .ip = ip,
  643. .level = level,
  644. .count = count,
  645. .parent = NULL,
  646. .sorted_chain = RB_ROOT
  647. };
  648. int cmp;
  649. if ((sort__has_parent || callchain) && chain)
  650. syms = resolve_callchain(thread, map, chain, &entry);
  651. while (*p != NULL) {
  652. parent = *p;
  653. he = rb_entry(parent, struct hist_entry, rb_node);
  654. cmp = hist_entry__cmp(&entry, he);
  655. if (!cmp) {
  656. he->count += count;
  657. if (callchain) {
  658. append_chain(&he->callchain, chain, syms);
  659. free(syms);
  660. }
  661. return 0;
  662. }
  663. if (cmp < 0)
  664. p = &(*p)->rb_left;
  665. else
  666. p = &(*p)->rb_right;
  667. }
  668. he = malloc(sizeof(*he));
  669. if (!he)
  670. return -ENOMEM;
  671. *he = entry;
  672. if (callchain) {
  673. callchain_init(&he->callchain);
  674. append_chain(&he->callchain, chain, syms);
  675. free(syms);
  676. }
  677. rb_link_node(&he->rb_node, parent, p);
  678. rb_insert_color(&he->rb_node, &hist);
  679. return 0;
  680. }
  681. static void hist_entry__free(struct hist_entry *he)
  682. {
  683. free(he);
  684. }
  685. /*
  686. * collapse the histogram
  687. */
  688. static struct rb_root collapse_hists;
  689. static void collapse__insert_entry(struct hist_entry *he)
  690. {
  691. struct rb_node **p = &collapse_hists.rb_node;
  692. struct rb_node *parent = NULL;
  693. struct hist_entry *iter;
  694. int64_t cmp;
  695. while (*p != NULL) {
  696. parent = *p;
  697. iter = rb_entry(parent, struct hist_entry, rb_node);
  698. cmp = hist_entry__collapse(iter, he);
  699. if (!cmp) {
  700. iter->count += he->count;
  701. hist_entry__free(he);
  702. return;
  703. }
  704. if (cmp < 0)
  705. p = &(*p)->rb_left;
  706. else
  707. p = &(*p)->rb_right;
  708. }
  709. rb_link_node(&he->rb_node, parent, p);
  710. rb_insert_color(&he->rb_node, &collapse_hists);
  711. }
  712. static void collapse__resort(void)
  713. {
  714. struct rb_node *next;
  715. struct hist_entry *n;
  716. if (!sort__need_collapse)
  717. return;
  718. next = rb_first(&hist);
  719. while (next) {
  720. n = rb_entry(next, struct hist_entry, rb_node);
  721. next = rb_next(&n->rb_node);
  722. rb_erase(&n->rb_node, &hist);
  723. collapse__insert_entry(n);
  724. }
  725. }
  726. /*
  727. * reverse the map, sort on count.
  728. */
  729. static struct rb_root output_hists;
  730. static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
  731. {
  732. struct rb_node **p = &output_hists.rb_node;
  733. struct rb_node *parent = NULL;
  734. struct hist_entry *iter;
  735. if (callchain)
  736. callchain_param.sort(&he->sorted_chain, &he->callchain,
  737. min_callchain_hits, &callchain_param);
  738. while (*p != NULL) {
  739. parent = *p;
  740. iter = rb_entry(parent, struct hist_entry, rb_node);
  741. if (he->count > iter->count)
  742. p = &(*p)->rb_left;
  743. else
  744. p = &(*p)->rb_right;
  745. }
  746. rb_link_node(&he->rb_node, parent, p);
  747. rb_insert_color(&he->rb_node, &output_hists);
  748. }
  749. static void output__resort(u64 total_samples)
  750. {
  751. struct rb_node *next;
  752. struct hist_entry *n;
  753. struct rb_root *tree = &hist;
  754. u64 min_callchain_hits;
  755. min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
  756. if (sort__need_collapse)
  757. tree = &collapse_hists;
  758. next = rb_first(tree);
  759. while (next) {
  760. n = rb_entry(next, struct hist_entry, rb_node);
  761. next = rb_next(&n->rb_node);
  762. rb_erase(&n->rb_node, tree);
  763. output__insert_entry(n, min_callchain_hits);
  764. }
  765. }
  766. static size_t output__fprintf(FILE *fp, u64 total_samples)
  767. {
  768. struct hist_entry *pos;
  769. struct sort_entry *se;
  770. struct rb_node *nd;
  771. size_t ret = 0;
  772. unsigned int width;
  773. char *col_width = col_width_list_str;
  774. int raw_printing_style;
  775. raw_printing_style = !strcmp(pretty_printing_style, "raw");
  776. init_rem_hits();
  777. fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
  778. fprintf(fp, "#\n");
  779. fprintf(fp, "# Overhead");
  780. if (show_nr_samples) {
  781. if (field_sep)
  782. fprintf(fp, "%cSamples", *field_sep);
  783. else
  784. fputs(" Samples ", fp);
  785. }
  786. list_for_each_entry(se, &hist_entry__sort_list, list) {
  787. if (se->elide)
  788. continue;
  789. if (field_sep) {
  790. fprintf(fp, "%c%s", *field_sep, se->header);
  791. continue;
  792. }
  793. width = strlen(se->header);
  794. if (se->width) {
  795. if (col_width_list_str) {
  796. if (col_width) {
  797. *se->width = atoi(col_width);
  798. col_width = strchr(col_width, ',');
  799. if (col_width)
  800. ++col_width;
  801. }
  802. }
  803. width = *se->width = max(*se->width, width);
  804. }
  805. fprintf(fp, " %*s", width, se->header);
  806. }
  807. fprintf(fp, "\n");
  808. if (field_sep)
  809. goto print_entries;
  810. fprintf(fp, "# ........");
  811. if (show_nr_samples)
  812. fprintf(fp, " ..........");
  813. list_for_each_entry(se, &hist_entry__sort_list, list) {
  814. unsigned int i;
  815. if (se->elide)
  816. continue;
  817. fprintf(fp, " ");
  818. if (se->width)
  819. width = *se->width;
  820. else
  821. width = strlen(se->header);
  822. for (i = 0; i < width; i++)
  823. fprintf(fp, ".");
  824. }
  825. fprintf(fp, "\n");
  826. fprintf(fp, "#\n");
  827. print_entries:
  828. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  829. pos = rb_entry(nd, struct hist_entry, rb_node);
  830. ret += hist_entry__fprintf(fp, pos, total_samples);
  831. }
  832. if (sort_order == default_sort_order &&
  833. parent_pattern == default_parent_pattern) {
  834. fprintf(fp, "#\n");
  835. fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
  836. fprintf(fp, "#\n");
  837. }
  838. fprintf(fp, "\n");
  839. free(rem_sq_bracket);
  840. if (show_threads)
  841. perf_read_values_display(fp, &show_threads_values,
  842. raw_printing_style);
  843. return ret;
  844. }
  845. static void register_idle_thread(void)
  846. {
  847. struct thread *thread = threads__findnew(0, &threads, &last_match);
  848. if (thread == NULL ||
  849. thread__set_comm(thread, "[idle]")) {
  850. fprintf(stderr, "problem inserting idle task.\n");
  851. exit(-1);
  852. }
  853. }
  854. static unsigned long total = 0,
  855. total_mmap = 0,
  856. total_comm = 0,
  857. total_fork = 0,
  858. total_unknown = 0,
  859. total_lost = 0;
  860. static int validate_chain(struct ip_callchain *chain, event_t *event)
  861. {
  862. unsigned int chain_size;
  863. chain_size = event->header.size;
  864. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  865. if (chain->nr*sizeof(u64) > chain_size)
  866. return -1;
  867. return 0;
  868. }
  869. static int
  870. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  871. {
  872. char level;
  873. int show = 0;
  874. struct dso *dso = NULL;
  875. struct thread *thread;
  876. u64 ip = event->ip.ip;
  877. u64 period = 1;
  878. struct map *map = NULL;
  879. void *more_data = event->ip.__more_data;
  880. struct ip_callchain *chain = NULL;
  881. int cpumode;
  882. thread = threads__findnew(event->ip.pid, &threads, &last_match);
  883. if (sample_type & PERF_SAMPLE_PERIOD) {
  884. period = *(u64 *)more_data;
  885. more_data += sizeof(u64);
  886. }
  887. dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
  888. (void *)(offset + head),
  889. (void *)(long)(event->header.size),
  890. event->header.misc,
  891. event->ip.pid, event->ip.tid,
  892. (void *)(long)ip,
  893. (long long)period);
  894. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  895. unsigned int i;
  896. chain = (void *)more_data;
  897. dump_printf("... chain: nr:%Lu\n", chain->nr);
  898. if (validate_chain(chain, event) < 0) {
  899. eprintf("call-chain problem with event, skipping it.\n");
  900. return 0;
  901. }
  902. if (dump_trace) {
  903. for (i = 0; i < chain->nr; i++)
  904. dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
  905. }
  906. }
  907. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  908. if (thread == NULL) {
  909. eprintf("problem processing %d event, skipping it.\n",
  910. event->header.type);
  911. return -1;
  912. }
  913. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  914. return 0;
  915. cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
  916. if (cpumode == PERF_EVENT_MISC_KERNEL) {
  917. show = SHOW_KERNEL;
  918. level = 'k';
  919. dso = kernel_dso;
  920. dump_printf(" ...... dso: %s\n", dso->name);
  921. } else if (cpumode == PERF_EVENT_MISC_USER) {
  922. show = SHOW_USER;
  923. level = '.';
  924. } else {
  925. show = SHOW_HV;
  926. level = 'H';
  927. dso = hypervisor_dso;
  928. dump_printf(" ...... dso: [hypervisor]\n");
  929. }
  930. if (show & show_mask) {
  931. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  932. if (dso_list && (!dso || !dso->name ||
  933. !strlist__has_entry(dso_list, dso->name)))
  934. return 0;
  935. if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
  936. return 0;
  937. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  938. eprintf("problem incrementing symbol count, skipping event\n");
  939. return -1;
  940. }
  941. }
  942. total += period;
  943. return 0;
  944. }
  945. static int
  946. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  947. {
  948. struct thread *thread;
  949. struct map *map = map__new(&event->mmap, cwd, cwdlen);
  950. thread = threads__findnew(event->mmap.pid, &threads, &last_match);
  951. dump_printf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
  952. (void *)(offset + head),
  953. (void *)(long)(event->header.size),
  954. event->mmap.pid,
  955. event->mmap.tid,
  956. (void *)(long)event->mmap.start,
  957. (void *)(long)event->mmap.len,
  958. (void *)(long)event->mmap.pgoff,
  959. event->mmap.filename);
  960. if (thread == NULL || map == NULL) {
  961. dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  962. return 0;
  963. }
  964. thread__insert_map(thread, map);
  965. total_mmap++;
  966. return 0;
  967. }
  968. static int
  969. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  970. {
  971. struct thread *thread;
  972. thread = threads__findnew(event->comm.pid, &threads, &last_match);
  973. dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  974. (void *)(offset + head),
  975. (void *)(long)(event->header.size),
  976. event->comm.comm, event->comm.pid);
  977. if (thread == NULL ||
  978. thread__set_comm(thread, event->comm.comm)) {
  979. dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
  980. return -1;
  981. }
  982. total_comm++;
  983. return 0;
  984. }
  985. static int
  986. process_task_event(event_t *event, unsigned long offset, unsigned long head)
  987. {
  988. struct thread *thread;
  989. struct thread *parent;
  990. thread = threads__findnew(event->fork.pid, &threads, &last_match);
  991. parent = threads__findnew(event->fork.ppid, &threads, &last_match);
  992. dump_printf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
  993. (void *)(offset + head),
  994. (void *)(long)(event->header.size),
  995. event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
  996. event->fork.pid, event->fork.tid,
  997. event->fork.ppid, event->fork.ptid);
  998. /*
  999. * A thread clone will have the same PID for both
  1000. * parent and child.
  1001. */
  1002. if (thread == parent)
  1003. return 0;
  1004. if (event->header.type == PERF_EVENT_EXIT)
  1005. return 0;
  1006. if (!thread || !parent || thread__fork(thread, parent)) {
  1007. dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
  1008. return -1;
  1009. }
  1010. total_fork++;
  1011. return 0;
  1012. }
  1013. static int
  1014. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  1015. {
  1016. dump_printf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
  1017. (void *)(offset + head),
  1018. (void *)(long)(event->header.size),
  1019. event->lost.id,
  1020. event->lost.lost);
  1021. total_lost += event->lost.lost;
  1022. return 0;
  1023. }
  1024. static int
  1025. process_read_event(event_t *event, unsigned long offset, unsigned long head)
  1026. {
  1027. struct perf_counter_attr *attr;
  1028. attr = perf_header__find_attr(event->read.id, header);
  1029. if (show_threads) {
  1030. const char *name = attr ? __event_name(attr->type, attr->config)
  1031. : "unknown";
  1032. perf_read_values_add_value(&show_threads_values,
  1033. event->read.pid, event->read.tid,
  1034. event->read.id,
  1035. name,
  1036. event->read.value);
  1037. }
  1038. dump_printf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
  1039. (void *)(offset + head),
  1040. (void *)(long)(event->header.size),
  1041. event->read.pid,
  1042. event->read.tid,
  1043. attr ? __event_name(attr->type, attr->config)
  1044. : "FAIL",
  1045. event->read.value);
  1046. return 0;
  1047. }
  1048. static int
  1049. process_event(event_t *event, unsigned long offset, unsigned long head)
  1050. {
  1051. trace_event(event);
  1052. switch (event->header.type) {
  1053. case PERF_EVENT_SAMPLE:
  1054. return process_sample_event(event, offset, head);
  1055. case PERF_EVENT_MMAP:
  1056. return process_mmap_event(event, offset, head);
  1057. case PERF_EVENT_COMM:
  1058. return process_comm_event(event, offset, head);
  1059. case PERF_EVENT_FORK:
  1060. case PERF_EVENT_EXIT:
  1061. return process_task_event(event, offset, head);
  1062. case PERF_EVENT_LOST:
  1063. return process_lost_event(event, offset, head);
  1064. case PERF_EVENT_READ:
  1065. return process_read_event(event, offset, head);
  1066. /*
  1067. * We dont process them right now but they are fine:
  1068. */
  1069. case PERF_EVENT_THROTTLE:
  1070. case PERF_EVENT_UNTHROTTLE:
  1071. return 0;
  1072. default:
  1073. return -1;
  1074. }
  1075. return 0;
  1076. }
  1077. static int __cmd_report(void)
  1078. {
  1079. int ret, rc = EXIT_FAILURE;
  1080. unsigned long offset = 0;
  1081. unsigned long head, shift;
  1082. struct stat input_stat;
  1083. event_t *event;
  1084. uint32_t size;
  1085. char *buf;
  1086. register_idle_thread();
  1087. if (show_threads)
  1088. perf_read_values_init(&show_threads_values);
  1089. input = open(input_name, O_RDONLY);
  1090. if (input < 0) {
  1091. fprintf(stderr, " failed to open file: %s", input_name);
  1092. if (!strcmp(input_name, "perf.data"))
  1093. fprintf(stderr, " (try 'perf record' first)");
  1094. fprintf(stderr, "\n");
  1095. exit(-1);
  1096. }
  1097. ret = fstat(input, &input_stat);
  1098. if (ret < 0) {
  1099. perror("failed to stat file");
  1100. exit(-1);
  1101. }
  1102. if (!input_stat.st_size) {
  1103. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1104. exit(0);
  1105. }
  1106. header = perf_header__read(input);
  1107. head = header->data_offset;
  1108. sample_type = perf_header__sample_type(header);
  1109. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  1110. if (sort__has_parent) {
  1111. fprintf(stderr, "selected --sort parent, but no"
  1112. " callchain data. Did you call"
  1113. " perf record without -g?\n");
  1114. exit(-1);
  1115. }
  1116. if (callchain) {
  1117. fprintf(stderr, "selected -g but no callchain data."
  1118. " Did you call perf record without"
  1119. " -g?\n");
  1120. exit(-1);
  1121. }
  1122. } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
  1123. callchain = 1;
  1124. if (register_callchain_param(&callchain_param) < 0) {
  1125. fprintf(stderr, "Can't register callchain"
  1126. " params\n");
  1127. exit(-1);
  1128. }
  1129. }
  1130. if (load_kernel() < 0) {
  1131. perror("failed to load kernel symbols");
  1132. return EXIT_FAILURE;
  1133. }
  1134. if (!full_paths) {
  1135. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  1136. perror("failed to get the current directory");
  1137. return EXIT_FAILURE;
  1138. }
  1139. cwdlen = strlen(cwd);
  1140. } else {
  1141. cwd = NULL;
  1142. cwdlen = 0;
  1143. }
  1144. shift = page_size * (head / page_size);
  1145. offset += shift;
  1146. head -= shift;
  1147. remap:
  1148. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1149. MAP_SHARED, input, offset);
  1150. if (buf == MAP_FAILED) {
  1151. perror("failed to mmap file");
  1152. exit(-1);
  1153. }
  1154. more:
  1155. event = (event_t *)(buf + head);
  1156. size = event->header.size;
  1157. if (!size)
  1158. size = 8;
  1159. if (head + event->header.size >= page_size * mmap_window) {
  1160. int munmap_ret;
  1161. shift = page_size * (head / page_size);
  1162. munmap_ret = munmap(buf, page_size * mmap_window);
  1163. assert(munmap_ret == 0);
  1164. offset += shift;
  1165. head -= shift;
  1166. goto remap;
  1167. }
  1168. size = event->header.size;
  1169. dump_printf("\n%p [%p]: event: %d\n",
  1170. (void *)(offset + head),
  1171. (void *)(long)event->header.size,
  1172. event->header.type);
  1173. if (!size || process_event(event, offset, head) < 0) {
  1174. dump_printf("%p [%p]: skipping unknown header type: %d\n",
  1175. (void *)(offset + head),
  1176. (void *)(long)(event->header.size),
  1177. event->header.type);
  1178. total_unknown++;
  1179. /*
  1180. * assume we lost track of the stream, check alignment, and
  1181. * increment a single u64 in the hope to catch on again 'soon'.
  1182. */
  1183. if (unlikely(head & 7))
  1184. head &= ~7ULL;
  1185. size = 8;
  1186. }
  1187. head += size;
  1188. if (offset + head >= header->data_offset + header->data_size)
  1189. goto done;
  1190. if (offset + head < (unsigned long)input_stat.st_size)
  1191. goto more;
  1192. done:
  1193. rc = EXIT_SUCCESS;
  1194. close(input);
  1195. dump_printf(" IP events: %10ld\n", total);
  1196. dump_printf(" mmap events: %10ld\n", total_mmap);
  1197. dump_printf(" comm events: %10ld\n", total_comm);
  1198. dump_printf(" fork events: %10ld\n", total_fork);
  1199. dump_printf(" lost events: %10ld\n", total_lost);
  1200. dump_printf(" unknown events: %10ld\n", total_unknown);
  1201. if (dump_trace)
  1202. return 0;
  1203. if (verbose >= 3)
  1204. threads__fprintf(stdout, &threads);
  1205. if (verbose >= 2)
  1206. dsos__fprintf(stdout);
  1207. collapse__resort();
  1208. output__resort(total);
  1209. output__fprintf(stdout, total);
  1210. if (show_threads)
  1211. perf_read_values_destroy(&show_threads_values);
  1212. return rc;
  1213. }
  1214. static int
  1215. parse_callchain_opt(const struct option *opt __used, const char *arg,
  1216. int unset __used)
  1217. {
  1218. char *tok;
  1219. char *endptr;
  1220. callchain = 1;
  1221. if (!arg)
  1222. return 0;
  1223. tok = strtok((char *)arg, ",");
  1224. if (!tok)
  1225. return -1;
  1226. /* get the output mode */
  1227. if (!strncmp(tok, "graph", strlen(arg)))
  1228. callchain_param.mode = CHAIN_GRAPH_ABS;
  1229. else if (!strncmp(tok, "flat", strlen(arg)))
  1230. callchain_param.mode = CHAIN_FLAT;
  1231. else if (!strncmp(tok, "fractal", strlen(arg)))
  1232. callchain_param.mode = CHAIN_GRAPH_REL;
  1233. else if (!strncmp(tok, "none", strlen(arg))) {
  1234. callchain_param.mode = CHAIN_NONE;
  1235. callchain = 0;
  1236. return 0;
  1237. }
  1238. else
  1239. return -1;
  1240. /* get the min percentage */
  1241. tok = strtok(NULL, ",");
  1242. if (!tok)
  1243. goto setup;
  1244. callchain_param.min_percent = strtod(tok, &endptr);
  1245. if (tok == endptr)
  1246. return -1;
  1247. setup:
  1248. if (register_callchain_param(&callchain_param) < 0) {
  1249. fprintf(stderr, "Can't register callchain params\n");
  1250. return -1;
  1251. }
  1252. return 0;
  1253. }
  1254. static const char * const report_usage[] = {
  1255. "perf report [<options>] <command>",
  1256. NULL
  1257. };
  1258. static const struct option options[] = {
  1259. OPT_STRING('i', "input", &input_name, "file",
  1260. "input file name"),
  1261. OPT_BOOLEAN('v', "verbose", &verbose,
  1262. "be more verbose (show symbol address, etc)"),
  1263. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1264. "dump raw trace in ASCII"),
  1265. OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
  1266. OPT_BOOLEAN('m', "modules", &modules,
  1267. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1268. OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
  1269. "Show a column with the number of samples"),
  1270. OPT_BOOLEAN('T', "threads", &show_threads,
  1271. "Show per-thread event counters"),
  1272. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  1273. "pretty printing style key: normal raw"),
  1274. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1275. "sort by key(s): pid, comm, dso, symbol, parent"),
  1276. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1277. "Don't shorten the pathnames taking into account the cwd"),
  1278. OPT_STRING('p', "parent", &parent_pattern, "regex",
  1279. "regex filter to identify parent, see: '--sort parent'"),
  1280. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  1281. "Only display entries with parent-match"),
  1282. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  1283. "Display callchains using output_type and min percent threshold. "
  1284. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  1285. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  1286. "only consider symbols in these dsos"),
  1287. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  1288. "only consider symbols in these comms"),
  1289. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  1290. "only consider these symbols"),
  1291. OPT_STRING('w', "column-widths", &col_width_list_str,
  1292. "width[,width...]",
  1293. "don't try to adjust column width, use these fixed values"),
  1294. OPT_STRING('t', "field-separator", &field_sep, "separator",
  1295. "separator for columns, no spaces will be added between "
  1296. "columns '.' is reserved."),
  1297. OPT_END()
  1298. };
  1299. static void setup_sorting(void)
  1300. {
  1301. char *tmp, *tok, *str = strdup(sort_order);
  1302. for (tok = strtok_r(str, ", ", &tmp);
  1303. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1304. if (sort_dimension__add(tok) < 0) {
  1305. error("Unknown --sort key: `%s'", tok);
  1306. usage_with_options(report_usage, options);
  1307. }
  1308. }
  1309. free(str);
  1310. }
  1311. static void setup_list(struct strlist **list, const char *list_str,
  1312. struct sort_entry *se, const char *list_name,
  1313. FILE *fp)
  1314. {
  1315. if (list_str) {
  1316. *list = strlist__new(true, list_str);
  1317. if (!*list) {
  1318. fprintf(stderr, "problems parsing %s list\n",
  1319. list_name);
  1320. exit(129);
  1321. }
  1322. if (strlist__nr_entries(*list) == 1) {
  1323. fprintf(fp, "# %s: %s\n", list_name,
  1324. strlist__entry(*list, 0)->s);
  1325. se->elide = true;
  1326. }
  1327. }
  1328. }
  1329. int cmd_report(int argc, const char **argv, const char *prefix __used)
  1330. {
  1331. symbol__init();
  1332. page_size = getpagesize();
  1333. argc = parse_options(argc, argv, options, report_usage, 0);
  1334. setup_sorting();
  1335. if (parent_pattern != default_parent_pattern) {
  1336. sort_dimension__add("parent");
  1337. sort_parent.elide = 1;
  1338. } else
  1339. exclude_other = 0;
  1340. /*
  1341. * Any (unrecognized) arguments left?
  1342. */
  1343. if (argc)
  1344. usage_with_options(report_usage, options);
  1345. setup_pager();
  1346. setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
  1347. setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
  1348. setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
  1349. if (field_sep && *field_sep == '.') {
  1350. fputs("'.' is the only non valid --field-separator argument\n",
  1351. stderr);
  1352. exit(129);
  1353. }
  1354. return __cmd_report();
  1355. }