builtin-report.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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 "util/list.h"
  12. #include "util/cache.h"
  13. #include "util/rbtree.h"
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "perf.h"
  17. #include "util/parse-options.h"
  18. #include "util/parse-events.h"
  19. #define SHOW_KERNEL 1
  20. #define SHOW_USER 2
  21. #define SHOW_HV 4
  22. static char const *input_name = "perf.data";
  23. static char *vmlinux = NULL;
  24. static char default_sort_order[] = "comm,dso";
  25. static char *sort_order = default_sort_order;
  26. static int input;
  27. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  28. static int dump_trace = 0;
  29. #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
  30. #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
  31. static int verbose;
  32. #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
  33. static int full_paths;
  34. static unsigned long page_size;
  35. static unsigned long mmap_window = 32;
  36. static char default_parent_pattern[] = "^sys_|^do_page_fault";
  37. static char *parent_pattern = default_parent_pattern;
  38. static regex_t parent_regex;
  39. static int exclude_other = 1;
  40. struct ip_event {
  41. struct perf_event_header header;
  42. u64 ip;
  43. u32 pid, tid;
  44. unsigned char __more_data[];
  45. };
  46. struct ip_callchain {
  47. u64 nr;
  48. u64 ips[0];
  49. };
  50. struct mmap_event {
  51. struct perf_event_header header;
  52. u32 pid, tid;
  53. u64 start;
  54. u64 len;
  55. u64 pgoff;
  56. char filename[PATH_MAX];
  57. };
  58. struct comm_event {
  59. struct perf_event_header header;
  60. u32 pid, tid;
  61. char comm[16];
  62. };
  63. struct fork_event {
  64. struct perf_event_header header;
  65. u32 pid, ppid;
  66. };
  67. struct period_event {
  68. struct perf_event_header header;
  69. u64 time;
  70. u64 id;
  71. u64 sample_period;
  72. };
  73. struct lost_event {
  74. struct perf_event_header header;
  75. u64 id;
  76. u64 lost;
  77. };
  78. typedef union event_union {
  79. struct perf_event_header header;
  80. struct ip_event ip;
  81. struct mmap_event mmap;
  82. struct comm_event comm;
  83. struct fork_event fork;
  84. struct period_event period;
  85. struct lost_event lost;
  86. } event_t;
  87. static LIST_HEAD(dsos);
  88. static struct dso *kernel_dso;
  89. static struct dso *vdso;
  90. static void dsos__add(struct dso *dso)
  91. {
  92. list_add_tail(&dso->node, &dsos);
  93. }
  94. static struct dso *dsos__find(const char *name)
  95. {
  96. struct dso *pos;
  97. list_for_each_entry(pos, &dsos, node)
  98. if (strcmp(pos->name, name) == 0)
  99. return pos;
  100. return NULL;
  101. }
  102. static struct dso *dsos__findnew(const char *name)
  103. {
  104. struct dso *dso = dsos__find(name);
  105. int nr;
  106. if (dso)
  107. return dso;
  108. dso = dso__new(name, 0);
  109. if (!dso)
  110. goto out_delete_dso;
  111. nr = dso__load(dso, NULL, verbose);
  112. if (nr < 0) {
  113. eprintf("Failed to open: %s\n", name);
  114. goto out_delete_dso;
  115. }
  116. if (!nr)
  117. eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
  118. dsos__add(dso);
  119. return dso;
  120. out_delete_dso:
  121. dso__delete(dso);
  122. return NULL;
  123. }
  124. static void dsos__fprintf(FILE *fp)
  125. {
  126. struct dso *pos;
  127. list_for_each_entry(pos, &dsos, node)
  128. dso__fprintf(pos, fp);
  129. }
  130. static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
  131. {
  132. return dso__find_symbol(kernel_dso, ip);
  133. }
  134. static int load_kernel(void)
  135. {
  136. int err;
  137. kernel_dso = dso__new("[kernel]", 0);
  138. if (!kernel_dso)
  139. return -1;
  140. err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
  141. if (err) {
  142. dso__delete(kernel_dso);
  143. kernel_dso = NULL;
  144. } else
  145. dsos__add(kernel_dso);
  146. vdso = dso__new("[vdso]", 0);
  147. if (!vdso)
  148. return -1;
  149. vdso->find_symbol = vdso__find_symbol;
  150. dsos__add(vdso);
  151. return err;
  152. }
  153. static char __cwd[PATH_MAX];
  154. static char *cwd = __cwd;
  155. static int cwdlen;
  156. static int strcommon(const char *pathname)
  157. {
  158. int n = 0;
  159. while (pathname[n] == cwd[n] && n < cwdlen)
  160. ++n;
  161. return n;
  162. }
  163. struct map {
  164. struct list_head node;
  165. u64 start;
  166. u64 end;
  167. u64 pgoff;
  168. u64 (*map_ip)(struct map *, u64);
  169. struct dso *dso;
  170. };
  171. static u64 map__map_ip(struct map *map, u64 ip)
  172. {
  173. return ip - map->start + map->pgoff;
  174. }
  175. static u64 vdso__map_ip(struct map *map, u64 ip)
  176. {
  177. return ip;
  178. }
  179. static inline int is_anon_memory(const char *filename)
  180. {
  181. return strcmp(filename, "//anon") == 0;
  182. }
  183. static struct map *map__new(struct mmap_event *event)
  184. {
  185. struct map *self = malloc(sizeof(*self));
  186. if (self != NULL) {
  187. const char *filename = event->filename;
  188. char newfilename[PATH_MAX];
  189. int anon;
  190. if (cwd) {
  191. int n = strcommon(filename);
  192. if (n == cwdlen) {
  193. snprintf(newfilename, sizeof(newfilename),
  194. ".%s", filename + n);
  195. filename = newfilename;
  196. }
  197. }
  198. anon = is_anon_memory(filename);
  199. if (anon) {
  200. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  201. filename = newfilename;
  202. }
  203. self->start = event->start;
  204. self->end = event->start + event->len;
  205. self->pgoff = event->pgoff;
  206. self->dso = dsos__findnew(filename);
  207. if (self->dso == NULL)
  208. goto out_delete;
  209. if (self->dso == vdso || anon)
  210. self->map_ip = vdso__map_ip;
  211. else
  212. self->map_ip = map__map_ip;
  213. }
  214. return self;
  215. out_delete:
  216. free(self);
  217. return NULL;
  218. }
  219. static struct map *map__clone(struct map *self)
  220. {
  221. struct map *map = malloc(sizeof(*self));
  222. if (!map)
  223. return NULL;
  224. memcpy(map, self, sizeof(*self));
  225. return map;
  226. }
  227. static int map__overlap(struct map *l, struct map *r)
  228. {
  229. if (l->start > r->start) {
  230. struct map *t = l;
  231. l = r;
  232. r = t;
  233. }
  234. if (l->end > r->start)
  235. return 1;
  236. return 0;
  237. }
  238. static size_t map__fprintf(struct map *self, FILE *fp)
  239. {
  240. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  241. self->start, self->end, self->pgoff, self->dso->name);
  242. }
  243. struct thread {
  244. struct rb_node rb_node;
  245. struct list_head maps;
  246. pid_t pid;
  247. char *comm;
  248. };
  249. static struct thread *thread__new(pid_t pid)
  250. {
  251. struct thread *self = malloc(sizeof(*self));
  252. if (self != NULL) {
  253. self->pid = pid;
  254. self->comm = malloc(32);
  255. if (self->comm)
  256. snprintf(self->comm, 32, ":%d", self->pid);
  257. INIT_LIST_HEAD(&self->maps);
  258. }
  259. return self;
  260. }
  261. static int thread__set_comm(struct thread *self, const char *comm)
  262. {
  263. if (self->comm)
  264. free(self->comm);
  265. self->comm = strdup(comm);
  266. return self->comm ? 0 : -ENOMEM;
  267. }
  268. static size_t thread__fprintf(struct thread *self, FILE *fp)
  269. {
  270. struct map *pos;
  271. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  272. list_for_each_entry(pos, &self->maps, node)
  273. ret += map__fprintf(pos, fp);
  274. return ret;
  275. }
  276. static struct rb_root threads;
  277. static struct thread *last_match;
  278. static struct thread *threads__findnew(pid_t pid)
  279. {
  280. struct rb_node **p = &threads.rb_node;
  281. struct rb_node *parent = NULL;
  282. struct thread *th;
  283. /*
  284. * Font-end cache - PID lookups come in blocks,
  285. * so most of the time we dont have to look up
  286. * the full rbtree:
  287. */
  288. if (last_match && last_match->pid == pid)
  289. return last_match;
  290. while (*p != NULL) {
  291. parent = *p;
  292. th = rb_entry(parent, struct thread, rb_node);
  293. if (th->pid == pid) {
  294. last_match = th;
  295. return th;
  296. }
  297. if (pid < th->pid)
  298. p = &(*p)->rb_left;
  299. else
  300. p = &(*p)->rb_right;
  301. }
  302. th = thread__new(pid);
  303. if (th != NULL) {
  304. rb_link_node(&th->rb_node, parent, p);
  305. rb_insert_color(&th->rb_node, &threads);
  306. last_match = th;
  307. }
  308. return th;
  309. }
  310. static void thread__insert_map(struct thread *self, struct map *map)
  311. {
  312. struct map *pos, *tmp;
  313. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  314. if (map__overlap(pos, map)) {
  315. list_del_init(&pos->node);
  316. /* XXX leaks dsos */
  317. free(pos);
  318. }
  319. }
  320. list_add_tail(&map->node, &self->maps);
  321. }
  322. static int thread__fork(struct thread *self, struct thread *parent)
  323. {
  324. struct map *map;
  325. if (self->comm)
  326. free(self->comm);
  327. self->comm = strdup(parent->comm);
  328. if (!self->comm)
  329. return -ENOMEM;
  330. list_for_each_entry(map, &parent->maps, node) {
  331. struct map *new = map__clone(map);
  332. if (!new)
  333. return -ENOMEM;
  334. thread__insert_map(self, new);
  335. }
  336. return 0;
  337. }
  338. static struct map *thread__find_map(struct thread *self, u64 ip)
  339. {
  340. struct map *pos;
  341. if (self == NULL)
  342. return NULL;
  343. list_for_each_entry(pos, &self->maps, node)
  344. if (ip >= pos->start && ip <= pos->end)
  345. return pos;
  346. return NULL;
  347. }
  348. static size_t threads__fprintf(FILE *fp)
  349. {
  350. size_t ret = 0;
  351. struct rb_node *nd;
  352. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  353. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  354. ret += thread__fprintf(pos, fp);
  355. }
  356. return ret;
  357. }
  358. /*
  359. * histogram, sorted on item, collects counts
  360. */
  361. static struct rb_root hist;
  362. struct hist_entry {
  363. struct rb_node rb_node;
  364. struct thread *thread;
  365. struct map *map;
  366. struct dso *dso;
  367. struct symbol *sym;
  368. struct symbol *parent;
  369. u64 ip;
  370. char level;
  371. u64 count;
  372. };
  373. /*
  374. * configurable sorting bits
  375. */
  376. struct sort_entry {
  377. struct list_head list;
  378. char *header;
  379. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  380. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  381. size_t (*print)(FILE *fp, struct hist_entry *);
  382. };
  383. static int64_t cmp_null(void *l, void *r)
  384. {
  385. if (!l && !r)
  386. return 0;
  387. else if (!l)
  388. return -1;
  389. else
  390. return 1;
  391. }
  392. /* --sort pid */
  393. static int64_t
  394. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  395. {
  396. return right->thread->pid - left->thread->pid;
  397. }
  398. static size_t
  399. sort__thread_print(FILE *fp, struct hist_entry *self)
  400. {
  401. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  402. }
  403. static struct sort_entry sort_thread = {
  404. .header = " Command: Pid",
  405. .cmp = sort__thread_cmp,
  406. .print = sort__thread_print,
  407. };
  408. /* --sort comm */
  409. static int64_t
  410. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  411. {
  412. return right->thread->pid - left->thread->pid;
  413. }
  414. static int64_t
  415. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  416. {
  417. char *comm_l = left->thread->comm;
  418. char *comm_r = right->thread->comm;
  419. if (!comm_l || !comm_r)
  420. return cmp_null(comm_l, comm_r);
  421. return strcmp(comm_l, comm_r);
  422. }
  423. static size_t
  424. sort__comm_print(FILE *fp, struct hist_entry *self)
  425. {
  426. return fprintf(fp, "%16s", self->thread->comm);
  427. }
  428. static struct sort_entry sort_comm = {
  429. .header = " Command",
  430. .cmp = sort__comm_cmp,
  431. .collapse = sort__comm_collapse,
  432. .print = sort__comm_print,
  433. };
  434. /* --sort dso */
  435. static int64_t
  436. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  437. {
  438. struct dso *dso_l = left->dso;
  439. struct dso *dso_r = right->dso;
  440. if (!dso_l || !dso_r)
  441. return cmp_null(dso_l, dso_r);
  442. return strcmp(dso_l->name, dso_r->name);
  443. }
  444. static size_t
  445. sort__dso_print(FILE *fp, struct hist_entry *self)
  446. {
  447. if (self->dso)
  448. return fprintf(fp, "%-25s", self->dso->name);
  449. return fprintf(fp, "%016llx ", (u64)self->ip);
  450. }
  451. static struct sort_entry sort_dso = {
  452. .header = "Shared Object ",
  453. .cmp = sort__dso_cmp,
  454. .print = sort__dso_print,
  455. };
  456. /* --sort symbol */
  457. static int64_t
  458. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  459. {
  460. u64 ip_l, ip_r;
  461. if (left->sym == right->sym)
  462. return 0;
  463. ip_l = left->sym ? left->sym->start : left->ip;
  464. ip_r = right->sym ? right->sym->start : right->ip;
  465. return (int64_t)(ip_r - ip_l);
  466. }
  467. static size_t
  468. sort__sym_print(FILE *fp, struct hist_entry *self)
  469. {
  470. size_t ret = 0;
  471. if (verbose)
  472. ret += fprintf(fp, "%#018llx ", (u64)self->ip);
  473. if (self->sym) {
  474. ret += fprintf(fp, "[%c] %s",
  475. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  476. } else {
  477. ret += fprintf(fp, "%#016llx", (u64)self->ip);
  478. }
  479. return ret;
  480. }
  481. static struct sort_entry sort_sym = {
  482. .header = "Symbol",
  483. .cmp = sort__sym_cmp,
  484. .print = sort__sym_print,
  485. };
  486. /* --sort parent */
  487. static int64_t
  488. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  489. {
  490. struct symbol *sym_l = left->parent;
  491. struct symbol *sym_r = right->parent;
  492. if (!sym_l || !sym_r)
  493. return cmp_null(sym_l, sym_r);
  494. return strcmp(sym_l->name, sym_r->name);
  495. }
  496. static size_t
  497. sort__parent_print(FILE *fp, struct hist_entry *self)
  498. {
  499. size_t ret = 0;
  500. ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
  501. return ret;
  502. }
  503. static struct sort_entry sort_parent = {
  504. .header = "Parent symbol ",
  505. .cmp = sort__parent_cmp,
  506. .print = sort__parent_print,
  507. };
  508. static int sort__need_collapse = 0;
  509. static int sort__has_parent = 0;
  510. struct sort_dimension {
  511. char *name;
  512. struct sort_entry *entry;
  513. int taken;
  514. };
  515. static struct sort_dimension sort_dimensions[] = {
  516. { .name = "pid", .entry = &sort_thread, },
  517. { .name = "comm", .entry = &sort_comm, },
  518. { .name = "dso", .entry = &sort_dso, },
  519. { .name = "symbol", .entry = &sort_sym, },
  520. { .name = "parent", .entry = &sort_parent, },
  521. };
  522. static LIST_HEAD(hist_entry__sort_list);
  523. static int sort_dimension__add(char *tok)
  524. {
  525. int i;
  526. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  527. struct sort_dimension *sd = &sort_dimensions[i];
  528. if (sd->taken)
  529. continue;
  530. if (strncasecmp(tok, sd->name, strlen(tok)))
  531. continue;
  532. if (sd->entry->collapse)
  533. sort__need_collapse = 1;
  534. if (sd->entry == &sort_parent) {
  535. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  536. if (ret) {
  537. char err[BUFSIZ];
  538. regerror(ret, &parent_regex, err, sizeof(err));
  539. fprintf(stderr, "Invalid regex: %s\n%s",
  540. parent_pattern, err);
  541. exit(-1);
  542. }
  543. sort__has_parent = 1;
  544. }
  545. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  546. sd->taken = 1;
  547. return 0;
  548. }
  549. return -ESRCH;
  550. }
  551. static int64_t
  552. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  553. {
  554. struct sort_entry *se;
  555. int64_t cmp = 0;
  556. list_for_each_entry(se, &hist_entry__sort_list, list) {
  557. cmp = se->cmp(left, right);
  558. if (cmp)
  559. break;
  560. }
  561. return cmp;
  562. }
  563. static int64_t
  564. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  565. {
  566. struct sort_entry *se;
  567. int64_t cmp = 0;
  568. list_for_each_entry(se, &hist_entry__sort_list, list) {
  569. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  570. f = se->collapse ?: se->cmp;
  571. cmp = f(left, right);
  572. if (cmp)
  573. break;
  574. }
  575. return cmp;
  576. }
  577. static size_t
  578. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  579. {
  580. struct sort_entry *se;
  581. size_t ret;
  582. if (exclude_other && !self->parent)
  583. return 0;
  584. if (total_samples) {
  585. double percent = self->count * 100.0 / total_samples;
  586. char *color = PERF_COLOR_NORMAL;
  587. /*
  588. * We color high-overhead entries in red, mid-overhead
  589. * entries in green - and keep the low overhead places
  590. * normal:
  591. */
  592. if (percent >= 5.0) {
  593. color = PERF_COLOR_RED;
  594. } else {
  595. if (percent >= 0.5)
  596. color = PERF_COLOR_GREEN;
  597. }
  598. ret = color_fprintf(fp, color, " %6.2f%%",
  599. (self->count * 100.0) / total_samples);
  600. } else
  601. ret = fprintf(fp, "%12Ld ", self->count);
  602. list_for_each_entry(se, &hist_entry__sort_list, list) {
  603. if (exclude_other && (se == &sort_parent))
  604. continue;
  605. fprintf(fp, " ");
  606. ret += se->print(fp, self);
  607. }
  608. ret += fprintf(fp, "\n");
  609. return ret;
  610. }
  611. /*
  612. *
  613. */
  614. static struct symbol *
  615. resolve_symbol(struct thread *thread, struct map **mapp,
  616. struct dso **dsop, u64 *ipp)
  617. {
  618. struct dso *dso = dsop ? *dsop : NULL;
  619. struct map *map = mapp ? *mapp : NULL;
  620. uint64_t ip = *ipp;
  621. if (!thread)
  622. return NULL;
  623. if (dso)
  624. goto got_dso;
  625. if (map)
  626. goto got_map;
  627. map = thread__find_map(thread, ip);
  628. if (map != NULL) {
  629. if (mapp)
  630. *mapp = map;
  631. got_map:
  632. ip = map->map_ip(map, ip);
  633. *ipp = ip;
  634. dso = map->dso;
  635. } else {
  636. /*
  637. * If this is outside of all known maps,
  638. * and is a negative address, try to look it
  639. * up in the kernel dso, as it might be a
  640. * vsyscall (which executes in user-mode):
  641. */
  642. if ((long long)ip < 0)
  643. dso = kernel_dso;
  644. }
  645. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  646. if (dsop)
  647. *dsop = dso;
  648. if (!dso)
  649. return NULL;
  650. got_dso:
  651. return dso->find_symbol(dso, ip);
  652. }
  653. static int call__match(struct symbol *sym)
  654. {
  655. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  656. return 1;
  657. return 0;
  658. }
  659. /*
  660. * collect histogram counts
  661. */
  662. static int
  663. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  664. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  665. char level, u64 count)
  666. {
  667. struct rb_node **p = &hist.rb_node;
  668. struct rb_node *parent = NULL;
  669. struct hist_entry *he;
  670. struct hist_entry entry = {
  671. .thread = thread,
  672. .map = map,
  673. .dso = dso,
  674. .sym = sym,
  675. .ip = ip,
  676. .level = level,
  677. .count = count,
  678. .parent = NULL,
  679. };
  680. int cmp;
  681. if (sort__has_parent && chain) {
  682. u64 context = PERF_CONTEXT_MAX;
  683. int i;
  684. for (i = 0; i < chain->nr; i++) {
  685. u64 ip = chain->ips[i];
  686. struct dso *dso = NULL;
  687. struct symbol *sym;
  688. if (ip >= PERF_CONTEXT_MAX) {
  689. context = ip;
  690. continue;
  691. }
  692. switch (context) {
  693. case PERF_CONTEXT_KERNEL:
  694. dso = kernel_dso;
  695. break;
  696. default:
  697. break;
  698. }
  699. sym = resolve_symbol(thread, NULL, &dso, &ip);
  700. if (sym && call__match(sym)) {
  701. entry.parent = sym;
  702. break;
  703. }
  704. }
  705. }
  706. while (*p != NULL) {
  707. parent = *p;
  708. he = rb_entry(parent, struct hist_entry, rb_node);
  709. cmp = hist_entry__cmp(&entry, he);
  710. if (!cmp) {
  711. he->count += count;
  712. return 0;
  713. }
  714. if (cmp < 0)
  715. p = &(*p)->rb_left;
  716. else
  717. p = &(*p)->rb_right;
  718. }
  719. he = malloc(sizeof(*he));
  720. if (!he)
  721. return -ENOMEM;
  722. *he = entry;
  723. rb_link_node(&he->rb_node, parent, p);
  724. rb_insert_color(&he->rb_node, &hist);
  725. return 0;
  726. }
  727. static void hist_entry__free(struct hist_entry *he)
  728. {
  729. free(he);
  730. }
  731. /*
  732. * collapse the histogram
  733. */
  734. static struct rb_root collapse_hists;
  735. static void collapse__insert_entry(struct hist_entry *he)
  736. {
  737. struct rb_node **p = &collapse_hists.rb_node;
  738. struct rb_node *parent = NULL;
  739. struct hist_entry *iter;
  740. int64_t cmp;
  741. while (*p != NULL) {
  742. parent = *p;
  743. iter = rb_entry(parent, struct hist_entry, rb_node);
  744. cmp = hist_entry__collapse(iter, he);
  745. if (!cmp) {
  746. iter->count += he->count;
  747. hist_entry__free(he);
  748. return;
  749. }
  750. if (cmp < 0)
  751. p = &(*p)->rb_left;
  752. else
  753. p = &(*p)->rb_right;
  754. }
  755. rb_link_node(&he->rb_node, parent, p);
  756. rb_insert_color(&he->rb_node, &collapse_hists);
  757. }
  758. static void collapse__resort(void)
  759. {
  760. struct rb_node *next;
  761. struct hist_entry *n;
  762. if (!sort__need_collapse)
  763. return;
  764. next = rb_first(&hist);
  765. while (next) {
  766. n = rb_entry(next, struct hist_entry, rb_node);
  767. next = rb_next(&n->rb_node);
  768. rb_erase(&n->rb_node, &hist);
  769. collapse__insert_entry(n);
  770. }
  771. }
  772. /*
  773. * reverse the map, sort on count.
  774. */
  775. static struct rb_root output_hists;
  776. static void output__insert_entry(struct hist_entry *he)
  777. {
  778. struct rb_node **p = &output_hists.rb_node;
  779. struct rb_node *parent = NULL;
  780. struct hist_entry *iter;
  781. while (*p != NULL) {
  782. parent = *p;
  783. iter = rb_entry(parent, struct hist_entry, rb_node);
  784. if (he->count > iter->count)
  785. p = &(*p)->rb_left;
  786. else
  787. p = &(*p)->rb_right;
  788. }
  789. rb_link_node(&he->rb_node, parent, p);
  790. rb_insert_color(&he->rb_node, &output_hists);
  791. }
  792. static void output__resort(void)
  793. {
  794. struct rb_node *next;
  795. struct hist_entry *n;
  796. struct rb_root *tree = &hist;
  797. if (sort__need_collapse)
  798. tree = &collapse_hists;
  799. next = rb_first(tree);
  800. while (next) {
  801. n = rb_entry(next, struct hist_entry, rb_node);
  802. next = rb_next(&n->rb_node);
  803. rb_erase(&n->rb_node, tree);
  804. output__insert_entry(n);
  805. }
  806. }
  807. static size_t output__fprintf(FILE *fp, u64 total_samples)
  808. {
  809. struct hist_entry *pos;
  810. struct sort_entry *se;
  811. struct rb_node *nd;
  812. size_t ret = 0;
  813. fprintf(fp, "\n");
  814. fprintf(fp, "#\n");
  815. fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
  816. fprintf(fp, "#\n");
  817. fprintf(fp, "# Overhead");
  818. list_for_each_entry(se, &hist_entry__sort_list, list) {
  819. if (exclude_other && (se == &sort_parent))
  820. continue;
  821. fprintf(fp, " %s", se->header);
  822. }
  823. fprintf(fp, "\n");
  824. fprintf(fp, "# ........");
  825. list_for_each_entry(se, &hist_entry__sort_list, list) {
  826. int i;
  827. if (exclude_other && (se == &sort_parent))
  828. continue;
  829. fprintf(fp, " ");
  830. for (i = 0; i < strlen(se->header); i++)
  831. fprintf(fp, ".");
  832. }
  833. fprintf(fp, "\n");
  834. fprintf(fp, "#\n");
  835. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  836. pos = rb_entry(nd, struct hist_entry, rb_node);
  837. ret += hist_entry__fprintf(fp, pos, total_samples);
  838. }
  839. if (sort_order == default_sort_order &&
  840. parent_pattern == default_parent_pattern) {
  841. fprintf(fp, "#\n");
  842. fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
  843. fprintf(fp, "#\n");
  844. }
  845. fprintf(fp, "\n");
  846. return ret;
  847. }
  848. static void register_idle_thread(void)
  849. {
  850. struct thread *thread = threads__findnew(0);
  851. if (thread == NULL ||
  852. thread__set_comm(thread, "[idle]")) {
  853. fprintf(stderr, "problem inserting idle task.\n");
  854. exit(-1);
  855. }
  856. }
  857. static unsigned long total = 0,
  858. total_mmap = 0,
  859. total_comm = 0,
  860. total_fork = 0,
  861. total_unknown = 0,
  862. total_lost = 0;
  863. static int validate_chain(struct ip_callchain *chain, event_t *event)
  864. {
  865. unsigned int chain_size;
  866. chain_size = event->header.size;
  867. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  868. if (chain->nr*sizeof(u64) > chain_size)
  869. return -1;
  870. return 0;
  871. }
  872. static int
  873. process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
  874. {
  875. char level;
  876. int show = 0;
  877. struct dso *dso = NULL;
  878. struct thread *thread = threads__findnew(event->ip.pid);
  879. u64 ip = event->ip.ip;
  880. u64 period = 1;
  881. struct map *map = NULL;
  882. void *more_data = event->ip.__more_data;
  883. struct ip_callchain *chain = NULL;
  884. if (event->header.type & PERF_SAMPLE_PERIOD) {
  885. period = *(u64 *)more_data;
  886. more_data += sizeof(u64);
  887. }
  888. dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
  889. (void *)(offset + head),
  890. (void *)(long)(event->header.size),
  891. event->header.misc,
  892. event->ip.pid,
  893. (void *)(long)ip,
  894. (long long)period);
  895. if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
  896. int i;
  897. chain = (void *)more_data;
  898. dprintf("... chain: nr:%Lu\n", chain->nr);
  899. if (validate_chain(chain, event) < 0) {
  900. eprintf("call-chain problem with event, skipping it.\n");
  901. return 0;
  902. }
  903. if (dump_trace) {
  904. for (i = 0; i < chain->nr; i++)
  905. dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
  906. }
  907. }
  908. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  909. if (thread == NULL) {
  910. eprintf("problem processing %d event, skipping it.\n",
  911. event->header.type);
  912. return -1;
  913. }
  914. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  915. show = SHOW_KERNEL;
  916. level = 'k';
  917. dso = kernel_dso;
  918. dprintf(" ...... dso: %s\n", dso->name);
  919. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  920. show = SHOW_USER;
  921. level = '.';
  922. } else {
  923. show = SHOW_HV;
  924. level = 'H';
  925. dprintf(" ...... dso: [hypervisor]\n");
  926. }
  927. if (show & show_mask) {
  928. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  929. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  930. eprintf("problem incrementing symbol count, skipping event\n");
  931. return -1;
  932. }
  933. }
  934. total += period;
  935. return 0;
  936. }
  937. static int
  938. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  939. {
  940. struct thread *thread = threads__findnew(event->mmap.pid);
  941. struct map *map = map__new(&event->mmap);
  942. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  943. (void *)(offset + head),
  944. (void *)(long)(event->header.size),
  945. event->mmap.pid,
  946. (void *)(long)event->mmap.start,
  947. (void *)(long)event->mmap.len,
  948. (void *)(long)event->mmap.pgoff,
  949. event->mmap.filename);
  950. if (thread == NULL || map == NULL) {
  951. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  952. return 0;
  953. }
  954. thread__insert_map(thread, map);
  955. total_mmap++;
  956. return 0;
  957. }
  958. static int
  959. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  960. {
  961. struct thread *thread = threads__findnew(event->comm.pid);
  962. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  963. (void *)(offset + head),
  964. (void *)(long)(event->header.size),
  965. event->comm.comm, event->comm.pid);
  966. if (thread == NULL ||
  967. thread__set_comm(thread, event->comm.comm)) {
  968. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  969. return -1;
  970. }
  971. total_comm++;
  972. return 0;
  973. }
  974. static int
  975. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  976. {
  977. struct thread *thread = threads__findnew(event->fork.pid);
  978. struct thread *parent = threads__findnew(event->fork.ppid);
  979. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  980. (void *)(offset + head),
  981. (void *)(long)(event->header.size),
  982. event->fork.pid, event->fork.ppid);
  983. if (!thread || !parent || thread__fork(thread, parent)) {
  984. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  985. return -1;
  986. }
  987. total_fork++;
  988. return 0;
  989. }
  990. static int
  991. process_period_event(event_t *event, unsigned long offset, unsigned long head)
  992. {
  993. dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
  994. (void *)(offset + head),
  995. (void *)(long)(event->header.size),
  996. event->period.time,
  997. event->period.id,
  998. event->period.sample_period);
  999. return 0;
  1000. }
  1001. static int
  1002. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  1003. {
  1004. dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
  1005. (void *)(offset + head),
  1006. (void *)(long)(event->header.size),
  1007. event->lost.id,
  1008. event->lost.lost);
  1009. total_lost += event->lost.lost;
  1010. return 0;
  1011. }
  1012. static void trace_event(event_t *event)
  1013. {
  1014. unsigned char *raw_event = (void *)event;
  1015. char *color = PERF_COLOR_BLUE;
  1016. int i, j;
  1017. if (!dump_trace)
  1018. return;
  1019. dprintf(".");
  1020. cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
  1021. for (i = 0; i < event->header.size; i++) {
  1022. if ((i & 15) == 0) {
  1023. dprintf(".");
  1024. cdprintf(" %04x: ", i);
  1025. }
  1026. cdprintf(" %02x", raw_event[i]);
  1027. if (((i & 15) == 15) || i == event->header.size-1) {
  1028. cdprintf(" ");
  1029. for (j = 0; j < 15-(i & 15); j++)
  1030. cdprintf(" ");
  1031. for (j = 0; j < (i & 15); j++) {
  1032. if (isprint(raw_event[i-15+j]))
  1033. cdprintf("%c", raw_event[i-15+j]);
  1034. else
  1035. cdprintf(".");
  1036. }
  1037. cdprintf("\n");
  1038. }
  1039. }
  1040. dprintf(".\n");
  1041. }
  1042. static int
  1043. process_event(event_t *event, unsigned long offset, unsigned long head)
  1044. {
  1045. trace_event(event);
  1046. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
  1047. return process_overflow_event(event, offset, head);
  1048. switch (event->header.type) {
  1049. case PERF_EVENT_MMAP:
  1050. return process_mmap_event(event, offset, head);
  1051. case PERF_EVENT_COMM:
  1052. return process_comm_event(event, offset, head);
  1053. case PERF_EVENT_FORK:
  1054. return process_fork_event(event, offset, head);
  1055. case PERF_EVENT_PERIOD:
  1056. return process_period_event(event, offset, head);
  1057. case PERF_EVENT_LOST:
  1058. return process_lost_event(event, offset, head);
  1059. /*
  1060. * We dont process them right now but they are fine:
  1061. */
  1062. case PERF_EVENT_THROTTLE:
  1063. case PERF_EVENT_UNTHROTTLE:
  1064. return 0;
  1065. default:
  1066. return -1;
  1067. }
  1068. return 0;
  1069. }
  1070. static struct perf_file_header file_header;
  1071. static int __cmd_report(void)
  1072. {
  1073. int ret, rc = EXIT_FAILURE;
  1074. unsigned long offset = 0;
  1075. unsigned long head = sizeof(file_header);
  1076. struct stat stat;
  1077. event_t *event;
  1078. uint32_t size;
  1079. char *buf;
  1080. register_idle_thread();
  1081. input = open(input_name, O_RDONLY);
  1082. if (input < 0) {
  1083. fprintf(stderr, " failed to open file: %s", input_name);
  1084. if (!strcmp(input_name, "perf.data"))
  1085. fprintf(stderr, " (try 'perf record' first)");
  1086. fprintf(stderr, "\n");
  1087. exit(-1);
  1088. }
  1089. ret = fstat(input, &stat);
  1090. if (ret < 0) {
  1091. perror("failed to stat file");
  1092. exit(-1);
  1093. }
  1094. if (!stat.st_size) {
  1095. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1096. exit(0);
  1097. }
  1098. if (read(input, &file_header, sizeof(file_header)) == -1) {
  1099. perror("failed to read file headers");
  1100. exit(-1);
  1101. }
  1102. if (sort__has_parent &&
  1103. !(file_header.sample_type & PERF_SAMPLE_CALLCHAIN)) {
  1104. fprintf(stderr, "selected --sort parent, but no callchain data\n");
  1105. exit(-1);
  1106. }
  1107. if (load_kernel() < 0) {
  1108. perror("failed to load kernel symbols");
  1109. return EXIT_FAILURE;
  1110. }
  1111. if (!full_paths) {
  1112. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  1113. perror("failed to get the current directory");
  1114. return EXIT_FAILURE;
  1115. }
  1116. cwdlen = strlen(cwd);
  1117. } else {
  1118. cwd = NULL;
  1119. cwdlen = 0;
  1120. }
  1121. remap:
  1122. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1123. MAP_SHARED, input, offset);
  1124. if (buf == MAP_FAILED) {
  1125. perror("failed to mmap file");
  1126. exit(-1);
  1127. }
  1128. more:
  1129. event = (event_t *)(buf + head);
  1130. size = event->header.size;
  1131. if (!size)
  1132. size = 8;
  1133. if (head + event->header.size >= page_size * mmap_window) {
  1134. unsigned long shift = page_size * (head / page_size);
  1135. int ret;
  1136. ret = munmap(buf, page_size * mmap_window);
  1137. assert(ret == 0);
  1138. offset += shift;
  1139. head -= shift;
  1140. goto remap;
  1141. }
  1142. size = event->header.size;
  1143. dprintf("\n%p [%p]: event: %d\n",
  1144. (void *)(offset + head),
  1145. (void *)(long)event->header.size,
  1146. event->header.type);
  1147. if (!size || process_event(event, offset, head) < 0) {
  1148. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1149. (void *)(offset + head),
  1150. (void *)(long)(event->header.size),
  1151. event->header.type);
  1152. total_unknown++;
  1153. /*
  1154. * assume we lost track of the stream, check alignment, and
  1155. * increment a single u64 in the hope to catch on again 'soon'.
  1156. */
  1157. if (unlikely(head & 7))
  1158. head &= ~7ULL;
  1159. size = 8;
  1160. }
  1161. head += size;
  1162. if (offset + head >= sizeof(file_header) + file_header.data_size)
  1163. goto done;
  1164. if (offset + head < stat.st_size)
  1165. goto more;
  1166. done:
  1167. rc = EXIT_SUCCESS;
  1168. close(input);
  1169. dprintf(" IP events: %10ld\n", total);
  1170. dprintf(" mmap events: %10ld\n", total_mmap);
  1171. dprintf(" comm events: %10ld\n", total_comm);
  1172. dprintf(" fork events: %10ld\n", total_fork);
  1173. dprintf(" lost events: %10ld\n", total_lost);
  1174. dprintf(" unknown events: %10ld\n", total_unknown);
  1175. if (dump_trace)
  1176. return 0;
  1177. if (verbose >= 3)
  1178. threads__fprintf(stdout);
  1179. if (verbose >= 2)
  1180. dsos__fprintf(stdout);
  1181. collapse__resort();
  1182. output__resort();
  1183. output__fprintf(stdout, total);
  1184. return rc;
  1185. }
  1186. static const char * const report_usage[] = {
  1187. "perf report [<options>] <command>",
  1188. NULL
  1189. };
  1190. static const struct option options[] = {
  1191. OPT_STRING('i', "input", &input_name, "file",
  1192. "input file name"),
  1193. OPT_BOOLEAN('v', "verbose", &verbose,
  1194. "be more verbose (show symbol address, etc)"),
  1195. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1196. "dump raw trace in ASCII"),
  1197. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1198. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1199. "sort by key(s): pid, comm, dso, symbol, parent"),
  1200. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1201. "Don't shorten the pathnames taking into account the cwd"),
  1202. OPT_STRING('p', "parent", &parent_pattern, "regex",
  1203. "regex filter to identify parent, see: '--sort parent'"),
  1204. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  1205. "Only display entries with parent-match"),
  1206. OPT_END()
  1207. };
  1208. static void setup_sorting(void)
  1209. {
  1210. char *tmp, *tok, *str = strdup(sort_order);
  1211. for (tok = strtok_r(str, ", ", &tmp);
  1212. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1213. if (sort_dimension__add(tok) < 0) {
  1214. error("Unknown --sort key: `%s'", tok);
  1215. usage_with_options(report_usage, options);
  1216. }
  1217. }
  1218. free(str);
  1219. }
  1220. int cmd_report(int argc, const char **argv, const char *prefix)
  1221. {
  1222. symbol__init();
  1223. page_size = getpagesize();
  1224. argc = parse_options(argc, argv, options, report_usage, 0);
  1225. setup_sorting();
  1226. if (parent_pattern != default_parent_pattern)
  1227. sort_dimension__add("parent");
  1228. else
  1229. exclude_other = 0;
  1230. /*
  1231. * Any (unrecognized) arguments left?
  1232. */
  1233. if (argc)
  1234. usage_with_options(report_usage, options);
  1235. setup_pager();
  1236. return __cmd_report();
  1237. }