builtin-report.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. #include "util/util.h"
  2. #include "builtin.h"
  3. #include <libelf.h>
  4. #include <gelf.h>
  5. #include <elf.h>
  6. #include "util/list.h"
  7. #include "util/cache.h"
  8. #include "util/rbtree.h"
  9. #include "perf.h"
  10. #include "util/parse-options.h"
  11. #include "util/parse-events.h"
  12. #define SHOW_KERNEL 1
  13. #define SHOW_USER 2
  14. #define SHOW_HV 4
  15. static char const *input_name = "perf.data";
  16. static char *vmlinux = NULL;
  17. static int input;
  18. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  19. static int dump_trace = 0;
  20. static int verbose;
  21. static unsigned long page_size;
  22. static unsigned long mmap_window = 32;
  23. const char *perf_event_names[] = {
  24. [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
  25. [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
  26. [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
  27. };
  28. struct ip_event {
  29. struct perf_event_header header;
  30. __u64 ip;
  31. __u32 pid, tid;
  32. };
  33. struct mmap_event {
  34. struct perf_event_header header;
  35. __u32 pid, tid;
  36. __u64 start;
  37. __u64 len;
  38. __u64 pgoff;
  39. char filename[PATH_MAX];
  40. };
  41. struct comm_event {
  42. struct perf_event_header header;
  43. __u32 pid,tid;
  44. char comm[16];
  45. };
  46. typedef union event_union {
  47. struct perf_event_header header;
  48. struct ip_event ip;
  49. struct mmap_event mmap;
  50. struct comm_event comm;
  51. } event_t;
  52. struct symbol {
  53. struct rb_node rb_node;
  54. __u64 start;
  55. __u64 end;
  56. char name[0];
  57. };
  58. static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
  59. {
  60. struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
  61. if (self != NULL) {
  62. self->start = start;
  63. self->end = start + len;
  64. strcpy(self->name, name);
  65. }
  66. return self;
  67. }
  68. static void symbol__delete(struct symbol *self)
  69. {
  70. free(self);
  71. }
  72. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  73. {
  74. return fprintf(fp, " %llx-%llx %s\n",
  75. self->start, self->end, self->name);
  76. }
  77. struct dso {
  78. struct list_head node;
  79. struct rb_root syms;
  80. char name[0];
  81. };
  82. static struct dso *dso__new(const char *name)
  83. {
  84. struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
  85. if (self != NULL) {
  86. strcpy(self->name, name);
  87. self->syms = RB_ROOT;
  88. }
  89. return self;
  90. }
  91. static void dso__delete_symbols(struct dso *self)
  92. {
  93. struct symbol *pos;
  94. struct rb_node *next = rb_first(&self->syms);
  95. while (next) {
  96. pos = rb_entry(next, struct symbol, rb_node);
  97. next = rb_next(&pos->rb_node);
  98. symbol__delete(pos);
  99. }
  100. }
  101. static void dso__delete(struct dso *self)
  102. {
  103. dso__delete_symbols(self);
  104. free(self);
  105. }
  106. static void dso__insert_symbol(struct dso *self, struct symbol *sym)
  107. {
  108. struct rb_node **p = &self->syms.rb_node;
  109. struct rb_node *parent = NULL;
  110. const uint64_t ip = sym->start;
  111. struct symbol *s;
  112. while (*p != NULL) {
  113. parent = *p;
  114. s = rb_entry(parent, struct symbol, rb_node);
  115. if (ip < s->start)
  116. p = &(*p)->rb_left;
  117. else
  118. p = &(*p)->rb_right;
  119. }
  120. rb_link_node(&sym->rb_node, parent, p);
  121. rb_insert_color(&sym->rb_node, &self->syms);
  122. }
  123. static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
  124. {
  125. struct rb_node *n;
  126. if (self == NULL)
  127. return NULL;
  128. n = self->syms.rb_node;
  129. while (n) {
  130. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  131. if (ip < s->start)
  132. n = n->rb_left;
  133. else if (ip > s->end)
  134. n = n->rb_right;
  135. else
  136. return s;
  137. }
  138. return NULL;
  139. }
  140. /**
  141. * elf_symtab__for_each_symbol - iterate thru all the symbols
  142. *
  143. * @self: struct elf_symtab instance to iterate
  144. * @index: uint32_t index
  145. * @sym: GElf_Sym iterator
  146. */
  147. #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
  148. for (index = 0, gelf_getsym(syms, index, &sym);\
  149. index < nr_syms; \
  150. index++, gelf_getsym(syms, index, &sym))
  151. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  152. {
  153. return GELF_ST_TYPE(sym->st_info);
  154. }
  155. static inline int elf_sym__is_function(const GElf_Sym *sym)
  156. {
  157. return elf_sym__type(sym) == STT_FUNC &&
  158. sym->st_name != 0 &&
  159. sym->st_shndx != SHN_UNDEF &&
  160. sym->st_size != 0;
  161. }
  162. static inline const char *elf_sym__name(const GElf_Sym *sym,
  163. const Elf_Data *symstrs)
  164. {
  165. return symstrs->d_buf + sym->st_name;
  166. }
  167. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  168. GElf_Shdr *shp, const char *name,
  169. size_t *index)
  170. {
  171. Elf_Scn *sec = NULL;
  172. size_t cnt = 1;
  173. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  174. char *str;
  175. gelf_getshdr(sec, shp);
  176. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  177. if (!strcmp(name, str)) {
  178. if (index)
  179. *index = cnt;
  180. break;
  181. }
  182. ++cnt;
  183. }
  184. return sec;
  185. }
  186. static int dso__load_sym(struct dso *self, int fd, char *name)
  187. {
  188. Elf_Data *symstrs;
  189. uint32_t nr_syms;
  190. int err = -1;
  191. uint32_t index;
  192. GElf_Ehdr ehdr;
  193. GElf_Shdr shdr;
  194. Elf_Data *syms;
  195. GElf_Sym sym;
  196. Elf_Scn *sec;
  197. Elf *elf;
  198. int nr = 0;
  199. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  200. if (elf == NULL) {
  201. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  202. __func__, name);
  203. goto out_close;
  204. }
  205. if (gelf_getehdr(elf, &ehdr) == NULL) {
  206. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  207. goto out_elf_end;
  208. }
  209. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  210. if (sec == NULL)
  211. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  212. if (sec == NULL)
  213. goto out_elf_end;
  214. syms = elf_getdata(sec, NULL);
  215. if (syms == NULL)
  216. goto out_elf_end;
  217. sec = elf_getscn(elf, shdr.sh_link);
  218. if (sec == NULL)
  219. goto out_elf_end;
  220. symstrs = elf_getdata(sec, NULL);
  221. if (symstrs == NULL)
  222. goto out_elf_end;
  223. nr_syms = shdr.sh_size / shdr.sh_entsize;
  224. elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
  225. struct symbol *f;
  226. if (!elf_sym__is_function(&sym))
  227. continue;
  228. sec = elf_getscn(elf, sym.st_shndx);
  229. if (!sec)
  230. goto out_elf_end;
  231. gelf_getshdr(sec, &shdr);
  232. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  233. f = symbol__new(sym.st_value, sym.st_size,
  234. elf_sym__name(&sym, symstrs));
  235. if (!f)
  236. goto out_elf_end;
  237. dso__insert_symbol(self, f);
  238. nr++;
  239. }
  240. err = nr;
  241. out_elf_end:
  242. elf_end(elf);
  243. out_close:
  244. return err;
  245. }
  246. static int dso__load(struct dso *self)
  247. {
  248. int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
  249. char *name = malloc(size);
  250. int variant = 0;
  251. int ret = -1;
  252. int fd;
  253. if (!name)
  254. return -1;
  255. more:
  256. do {
  257. switch (variant) {
  258. case 0: /* Fedora */
  259. snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
  260. break;
  261. case 1: /* Ubuntu */
  262. snprintf(name, size, "/usr/lib/debug%s", self->name);
  263. break;
  264. case 2: /* Sane people */
  265. snprintf(name, size, "%s", self->name);
  266. break;
  267. default:
  268. goto out;
  269. }
  270. variant++;
  271. fd = open(name, O_RDONLY);
  272. } while (fd < 0);
  273. ret = dso__load_sym(self, fd, name);
  274. close(fd);
  275. /*
  276. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  277. */
  278. if (!ret)
  279. goto more;
  280. out:
  281. free(name);
  282. return ret;
  283. }
  284. static size_t dso__fprintf(struct dso *self, FILE *fp)
  285. {
  286. size_t ret = fprintf(fp, "dso: %s\n", self->name);
  287. struct rb_node *nd;
  288. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  289. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  290. ret += symbol__fprintf(pos, fp);
  291. }
  292. return ret;
  293. }
  294. static LIST_HEAD(dsos);
  295. static struct dso *kernel_dso;
  296. static void dsos__add(struct dso *dso)
  297. {
  298. list_add_tail(&dso->node, &dsos);
  299. }
  300. static struct dso *dsos__find(const char *name)
  301. {
  302. struct dso *pos;
  303. list_for_each_entry(pos, &dsos, node)
  304. if (strcmp(pos->name, name) == 0)
  305. return pos;
  306. return NULL;
  307. }
  308. static struct dso *dsos__findnew(const char *name)
  309. {
  310. struct dso *dso = dsos__find(name);
  311. int nr;
  312. if (dso == NULL) {
  313. dso = dso__new(name);
  314. if (!dso)
  315. goto out_delete_dso;
  316. nr = dso__load(dso);
  317. if (nr < 0) {
  318. fprintf(stderr, "Failed to open: %s\n", name);
  319. goto out_delete_dso;
  320. }
  321. if (!nr) {
  322. fprintf(stderr,
  323. "Failed to find debug symbols for: %s, maybe install a debug package?\n",
  324. name);
  325. }
  326. dsos__add(dso);
  327. }
  328. return dso;
  329. out_delete_dso:
  330. dso__delete(dso);
  331. return NULL;
  332. }
  333. static void dsos__fprintf(FILE *fp)
  334. {
  335. struct dso *pos;
  336. list_for_each_entry(pos, &dsos, node)
  337. dso__fprintf(pos, fp);
  338. }
  339. static int hex(char ch)
  340. {
  341. if ((ch >= '0') && (ch <= '9'))
  342. return ch - '0';
  343. if ((ch >= 'a') && (ch <= 'f'))
  344. return ch - 'a' + 10;
  345. if ((ch >= 'A') && (ch <= 'F'))
  346. return ch - 'A' + 10;
  347. return -1;
  348. }
  349. /*
  350. * While we find nice hex chars, build a long_val.
  351. * Return number of chars processed.
  352. */
  353. static int hex2long(char *ptr, unsigned long *long_val)
  354. {
  355. const char *p = ptr;
  356. *long_val = 0;
  357. while (*p) {
  358. const int hex_val = hex(*p);
  359. if (hex_val < 0)
  360. break;
  361. *long_val = (*long_val << 4) | hex_val;
  362. p++;
  363. }
  364. return p - ptr;
  365. }
  366. static int load_kallsyms(void)
  367. {
  368. struct rb_node *nd, *prevnd;
  369. char *line = NULL;
  370. FILE *file;
  371. size_t n;
  372. kernel_dso = dso__new("[kernel]");
  373. if (kernel_dso == NULL)
  374. return -1;
  375. file = fopen("/proc/kallsyms", "r");
  376. if (file == NULL)
  377. goto out_delete_dso;
  378. while (!feof(file)) {
  379. unsigned long start;
  380. struct symbol *sym;
  381. int line_len, len;
  382. char symbol_type;
  383. line_len = getline(&line, &n, file);
  384. if (line_len < 0)
  385. break;
  386. if (!line)
  387. goto out_delete_dso;
  388. line[--line_len] = '\0'; /* \n */
  389. len = hex2long(line, &start);
  390. len++;
  391. if (len + 2 >= line_len)
  392. continue;
  393. symbol_type = toupper(line[len]);
  394. /*
  395. * We're interested only in code ('T'ext)
  396. */
  397. if (symbol_type != 'T' && symbol_type != 'W')
  398. continue;
  399. /*
  400. * Well fix up the end later, when we have all sorted.
  401. */
  402. sym = symbol__new(start, 0xdead, line + len + 2);
  403. if (sym == NULL)
  404. goto out_delete_dso;
  405. dso__insert_symbol(kernel_dso, sym);
  406. }
  407. /*
  408. * Now that we have all sorted out, just set the ->end of all
  409. * symbols
  410. */
  411. prevnd = rb_first(&kernel_dso->syms);
  412. if (prevnd == NULL)
  413. goto out_delete_line;
  414. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  415. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  416. *curr = rb_entry(nd, struct symbol, rb_node);
  417. prev->end = curr->start - 1;
  418. prevnd = nd;
  419. }
  420. dsos__add(kernel_dso);
  421. free(line);
  422. fclose(file);
  423. return 0;
  424. out_delete_line:
  425. free(line);
  426. out_delete_dso:
  427. dso__delete(kernel_dso);
  428. return -1;
  429. }
  430. static int load_kernel(void)
  431. {
  432. int fd, nr;
  433. if (!vmlinux)
  434. goto kallsyms;
  435. fd = open(vmlinux, O_RDONLY);
  436. if (fd < 0)
  437. goto kallsyms;
  438. kernel_dso = dso__new("[kernel]");
  439. if (!kernel_dso)
  440. goto fail_open;
  441. nr = dso__load_sym(kernel_dso, fd, vmlinux);
  442. if (nr <= 0)
  443. goto fail_load;
  444. dsos__add(kernel_dso);
  445. close(fd);
  446. return 0;
  447. fail_load:
  448. dso__delete(kernel_dso);
  449. fail_open:
  450. close(fd);
  451. kallsyms:
  452. return load_kallsyms();
  453. }
  454. struct map {
  455. struct list_head node;
  456. uint64_t start;
  457. uint64_t end;
  458. uint64_t pgoff;
  459. struct dso *dso;
  460. };
  461. static struct map *map__new(struct mmap_event *event)
  462. {
  463. struct map *self = malloc(sizeof(*self));
  464. if (self != NULL) {
  465. self->start = event->start;
  466. self->end = event->start + event->len;
  467. self->pgoff = event->pgoff;
  468. self->dso = dsos__findnew(event->filename);
  469. if (self->dso == NULL)
  470. goto out_delete;
  471. }
  472. return self;
  473. out_delete:
  474. free(self);
  475. return NULL;
  476. }
  477. struct thread;
  478. static const char *thread__name(struct thread *self, char *bf, size_t size);
  479. struct thread {
  480. struct rb_node rb_node;
  481. struct list_head maps;
  482. pid_t pid;
  483. char *comm;
  484. };
  485. static const char *thread__name(struct thread *self, char *bf, size_t size)
  486. {
  487. if (self->comm)
  488. return self->comm;
  489. snprintf(bf, sizeof(bf), ":%u", self->pid);
  490. return bf;
  491. }
  492. static struct thread *thread__new(pid_t pid)
  493. {
  494. struct thread *self = malloc(sizeof(*self));
  495. if (self != NULL) {
  496. self->pid = pid;
  497. self->comm = NULL;
  498. INIT_LIST_HEAD(&self->maps);
  499. }
  500. return self;
  501. }
  502. static int thread__set_comm(struct thread *self, const char *comm)
  503. {
  504. self->comm = strdup(comm);
  505. return self->comm ? 0 : -ENOMEM;
  506. }
  507. static struct rb_root threads;
  508. static struct thread *threads__findnew(pid_t pid)
  509. {
  510. struct rb_node **p = &threads.rb_node;
  511. struct rb_node *parent = NULL;
  512. struct thread *th;
  513. while (*p != NULL) {
  514. parent = *p;
  515. th = rb_entry(parent, struct thread, rb_node);
  516. if (th->pid == pid)
  517. return th;
  518. if (pid < th->pid)
  519. p = &(*p)->rb_left;
  520. else
  521. p = &(*p)->rb_right;
  522. }
  523. th = thread__new(pid);
  524. if (th != NULL) {
  525. rb_link_node(&th->rb_node, parent, p);
  526. rb_insert_color(&th->rb_node, &threads);
  527. }
  528. return th;
  529. }
  530. static void thread__insert_map(struct thread *self, struct map *map)
  531. {
  532. list_add_tail(&map->node, &self->maps);
  533. }
  534. static struct map *thread__find_map(struct thread *self, uint64_t ip)
  535. {
  536. struct map *pos;
  537. if (self == NULL)
  538. return NULL;
  539. list_for_each_entry(pos, &self->maps, node)
  540. if (ip >= pos->start && ip <= pos->end)
  541. return pos;
  542. return NULL;
  543. }
  544. /*
  545. * histogram, sorted on item, collects counts
  546. */
  547. static struct rb_root hist;
  548. struct hist_entry {
  549. struct rb_node rb_node;
  550. struct thread *thread;
  551. struct map *map;
  552. struct dso *dso;
  553. struct symbol *sym;
  554. uint64_t ip;
  555. char level;
  556. uint32_t count;
  557. };
  558. static int64_t
  559. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  560. {
  561. uint64_t ip_l, ip_r;
  562. int cmp = right->thread->pid - left->thread->pid;
  563. if (cmp)
  564. return cmp;
  565. if (left->sym == right->sym)
  566. return 0;
  567. ip_l = left->sym ? left->sym->start : left->ip;
  568. ip_r = right->sym ? right->sym->start : right->ip;
  569. return (int64_t)(ip_r - ip_l);
  570. }
  571. static int
  572. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  573. struct symbol *sym, uint64_t ip, char level)
  574. {
  575. struct rb_node **p = &hist.rb_node;
  576. struct rb_node *parent = NULL;
  577. struct hist_entry *he;
  578. struct hist_entry entry = {
  579. .thread = thread,
  580. .map = map,
  581. .dso = dso,
  582. .sym = sym,
  583. .ip = ip,
  584. .level = level,
  585. .count = 1,
  586. };
  587. int cmp;
  588. while (*p != NULL) {
  589. parent = *p;
  590. he = rb_entry(parent, struct hist_entry, rb_node);
  591. cmp = hist_entry__cmp(&entry, he);
  592. if (!cmp) {
  593. he->count++;
  594. return 0;
  595. }
  596. if (cmp < 0)
  597. p = &(*p)->rb_left;
  598. else
  599. p = &(*p)->rb_right;
  600. }
  601. he = malloc(sizeof(*he));
  602. if (!he)
  603. return -ENOMEM;
  604. *he = entry;
  605. rb_link_node(&he->rb_node, parent, p);
  606. rb_insert_color(&he->rb_node, &hist);
  607. return 0;
  608. }
  609. static size_t
  610. hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
  611. {
  612. char bf[32];
  613. size_t ret;
  614. if (total_samples) {
  615. ret = fprintf(fp, "%5.2f%% ",
  616. (self->count * 100.0) / total_samples);
  617. } else
  618. ret = fprintf(fp, "%12d ", self->count);
  619. ret += fprintf(fp, "%14s [%c] ",
  620. thread__name(self->thread, bf, sizeof(bf)),
  621. self->level);
  622. if (verbose)
  623. ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip);
  624. if (self->level != '.')
  625. ret += fprintf(fp, "%s\n",
  626. self->sym ? self->sym->name : "<unknown>");
  627. else
  628. ret += fprintf(fp, "%s: %s\n",
  629. self->dso ? self->dso->name : "<unknown>",
  630. self->sym ? self->sym->name : "<unknown>");
  631. return ret;
  632. }
  633. /*
  634. * reverse the map, sort on count.
  635. */
  636. static struct rb_root output_hists;
  637. static void output__insert_entry(struct hist_entry *he)
  638. {
  639. struct rb_node **p = &output_hists.rb_node;
  640. struct rb_node *parent = NULL;
  641. struct hist_entry *iter;
  642. while (*p != NULL) {
  643. parent = *p;
  644. iter = rb_entry(parent, struct hist_entry, rb_node);
  645. if (he->count > iter->count)
  646. p = &(*p)->rb_left;
  647. else
  648. p = &(*p)->rb_right;
  649. }
  650. rb_link_node(&he->rb_node, parent, p);
  651. rb_insert_color(&he->rb_node, &output_hists);
  652. }
  653. static void output__resort(void)
  654. {
  655. struct rb_node *next = rb_first(&hist);
  656. struct hist_entry *n;
  657. while (next) {
  658. n = rb_entry(next, struct hist_entry, rb_node);
  659. next = rb_next(&n->rb_node);
  660. rb_erase(&n->rb_node, &hist);
  661. output__insert_entry(n);
  662. }
  663. }
  664. static size_t output__fprintf(FILE *fp, uint64_t total_samples)
  665. {
  666. struct hist_entry *pos;
  667. struct rb_node *nd;
  668. size_t ret = 0;
  669. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  670. pos = rb_entry(nd, struct hist_entry, rb_node);
  671. ret += hist_entry__fprintf(fp, pos, total_samples);
  672. }
  673. return ret;
  674. }
  675. static int __cmd_report(void)
  676. {
  677. unsigned long offset = 0;
  678. unsigned long head = 0;
  679. struct stat stat;
  680. char *buf;
  681. event_t *event;
  682. int ret, rc = EXIT_FAILURE;
  683. uint32_t size;
  684. unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
  685. input = open(input_name, O_RDONLY);
  686. if (input < 0) {
  687. perror("failed to open file");
  688. exit(-1);
  689. }
  690. ret = fstat(input, &stat);
  691. if (ret < 0) {
  692. perror("failed to stat file");
  693. exit(-1);
  694. }
  695. if (!stat.st_size) {
  696. fprintf(stderr, "zero-sized file, nothing to do!\n");
  697. exit(0);
  698. }
  699. if (load_kernel() < 0) {
  700. perror("failed to open kallsyms");
  701. return EXIT_FAILURE;
  702. }
  703. remap:
  704. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  705. MAP_SHARED, input, offset);
  706. if (buf == MAP_FAILED) {
  707. perror("failed to mmap file");
  708. exit(-1);
  709. }
  710. more:
  711. event = (event_t *)(buf + head);
  712. size = event->header.size;
  713. if (!size)
  714. size = 8;
  715. if (head + event->header.size >= page_size * mmap_window) {
  716. unsigned long shift = page_size * (head / page_size);
  717. int ret;
  718. ret = munmap(buf, page_size * mmap_window);
  719. assert(ret == 0);
  720. offset += shift;
  721. head -= shift;
  722. goto remap;
  723. }
  724. size = event->header.size;
  725. if (!size)
  726. goto broken_event;
  727. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  728. char level;
  729. int show = 0;
  730. struct dso *dso = NULL;
  731. struct thread *thread = threads__findnew(event->ip.pid);
  732. uint64_t ip = event->ip.ip;
  733. struct map *map = NULL;
  734. if (dump_trace) {
  735. fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  736. (void *)(offset + head),
  737. (void *)(long)(event->header.size),
  738. event->header.misc,
  739. event->ip.pid,
  740. (void *)(long)ip);
  741. }
  742. if (thread == NULL) {
  743. fprintf(stderr, "problem processing %d event, bailing out\n",
  744. event->header.type);
  745. goto done;
  746. }
  747. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  748. show = SHOW_KERNEL;
  749. level = 'k';
  750. dso = kernel_dso;
  751. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  752. show = SHOW_USER;
  753. level = '.';
  754. map = thread__find_map(thread, ip);
  755. if (map != NULL) {
  756. dso = map->dso;
  757. ip -= map->start + map->pgoff;
  758. }
  759. } else {
  760. show = SHOW_HV;
  761. level = 'H';
  762. }
  763. if (show & show_mask) {
  764. struct symbol *sym = dso__find_symbol(dso, ip);
  765. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  766. fprintf(stderr,
  767. "problem incrementing symbol count, bailing out\n");
  768. goto done;
  769. }
  770. }
  771. total++;
  772. } else switch (event->header.type) {
  773. case PERF_EVENT_MMAP: {
  774. struct thread *thread = threads__findnew(event->mmap.pid);
  775. struct map *map = map__new(&event->mmap);
  776. if (dump_trace) {
  777. fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
  778. (void *)(offset + head),
  779. (void *)(long)(event->header.size),
  780. (void *)(long)event->mmap.start,
  781. (void *)(long)event->mmap.len,
  782. (void *)(long)event->mmap.pgoff,
  783. event->mmap.filename);
  784. }
  785. if (thread == NULL || map == NULL) {
  786. fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
  787. goto done;
  788. }
  789. thread__insert_map(thread, map);
  790. total_mmap++;
  791. break;
  792. }
  793. case PERF_EVENT_COMM: {
  794. struct thread *thread = threads__findnew(event->comm.pid);
  795. if (dump_trace) {
  796. fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  797. (void *)(offset + head),
  798. (void *)(long)(event->header.size),
  799. event->comm.comm, event->comm.pid);
  800. }
  801. if (thread == NULL ||
  802. thread__set_comm(thread, event->comm.comm)) {
  803. fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
  804. goto done;
  805. }
  806. total_comm++;
  807. break;
  808. }
  809. default: {
  810. broken_event:
  811. if (dump_trace)
  812. fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
  813. (void *)(offset + head),
  814. (void *)(long)(event->header.size),
  815. event->header.type);
  816. total_unknown++;
  817. /*
  818. * assume we lost track of the stream, check alignment, and
  819. * increment a single u64 in the hope to catch on again 'soon'.
  820. */
  821. if (unlikely(head & 7))
  822. head &= ~7ULL;
  823. size = 8;
  824. }
  825. }
  826. head += size;
  827. if (offset + head < stat.st_size)
  828. goto more;
  829. rc = EXIT_SUCCESS;
  830. done:
  831. close(input);
  832. if (dump_trace) {
  833. fprintf(stderr, " IP events: %10ld\n", total);
  834. fprintf(stderr, " mmap events: %10ld\n", total_mmap);
  835. fprintf(stderr, " comm events: %10ld\n", total_comm);
  836. fprintf(stderr, " unknown events: %10ld\n", total_unknown);
  837. return 0;
  838. }
  839. if (verbose >= 2)
  840. dsos__fprintf(stdout);
  841. output__resort();
  842. output__fprintf(stdout, total);
  843. return rc;
  844. }
  845. static const char * const report_usage[] = {
  846. "perf report [<options>] <command>",
  847. NULL
  848. };
  849. static const struct option options[] = {
  850. OPT_STRING('i', "input", &input_name, "file",
  851. "input file name"),
  852. OPT_BOOLEAN('v', "verbose", &verbose,
  853. "be more verbose (show symbol address, etc)"),
  854. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  855. "dump raw trace in ASCII"),
  856. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  857. OPT_END()
  858. };
  859. int cmd_report(int argc, const char **argv, const char *prefix)
  860. {
  861. elf_version(EV_CURRENT);
  862. page_size = getpagesize();
  863. parse_options(argc, argv, options, report_usage, 0);
  864. setup_pager();
  865. return __cmd_report();
  866. }