builtin-report.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. /*
  559. * configurable sorting bits
  560. */
  561. struct sort_entry {
  562. struct list_head list;
  563. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  564. size_t (*print)(FILE *fp, struct hist_entry *);
  565. };
  566. static int64_t
  567. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  568. {
  569. return right->thread->pid - left->thread->pid;
  570. }
  571. static size_t
  572. sort__thread_print(FILE *fp, struct hist_entry *self)
  573. {
  574. char bf[32];
  575. return fprintf(fp, "%14s ",
  576. thread__name(self->thread, bf, sizeof(bf)));
  577. }
  578. static struct sort_entry sort_thread = {
  579. .cmp = sort__thread_cmp,
  580. .print = sort__thread_print,
  581. };
  582. static int64_t
  583. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  584. {
  585. uint64_t ip_l, ip_r;
  586. if (left->sym == right->sym)
  587. return 0;
  588. ip_l = left->sym ? left->sym->start : left->ip;
  589. ip_r = right->sym ? right->sym->start : right->ip;
  590. return (int64_t)(ip_r - ip_l);
  591. }
  592. static size_t
  593. sort__sym_print(FILE *fp, struct hist_entry *self)
  594. {
  595. size_t ret = 0;
  596. ret += fprintf(fp, "[%c] ", self->level);
  597. if (verbose)
  598. ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip);
  599. if (self->level != '.')
  600. ret += fprintf(fp, "%s ",
  601. self->sym ? self->sym->name : "<unknown>");
  602. else
  603. ret += fprintf(fp, "%s: %s ",
  604. self->dso ? self->dso->name : "<unknown>",
  605. self->sym ? self->sym->name : "<unknown>");
  606. return ret;
  607. }
  608. static struct sort_entry sort_sym = {
  609. .cmp = sort__sym_cmp,
  610. .print = sort__sym_print,
  611. };
  612. static LIST_HEAD(hist_entry__sort_list);
  613. static void setup_sorting(void)
  614. {
  615. list_add_tail(&sort_thread.list, &hist_entry__sort_list);
  616. list_add_tail(&sort_sym.list, &hist_entry__sort_list);
  617. }
  618. static int64_t
  619. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  620. {
  621. struct sort_entry *se;
  622. int64_t cmp = 0;
  623. list_for_each_entry(se, &hist_entry__sort_list, list) {
  624. cmp = se->cmp(left, right);
  625. if (cmp)
  626. break;
  627. }
  628. return cmp;
  629. }
  630. static size_t
  631. hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
  632. {
  633. struct sort_entry *se;
  634. size_t ret;
  635. if (total_samples) {
  636. ret = fprintf(fp, "%5.2f%% ",
  637. (self->count * 100.0) / total_samples);
  638. } else
  639. ret = fprintf(fp, "%12d ", self->count);
  640. list_for_each_entry(se, &hist_entry__sort_list, list)
  641. ret += se->print(fp, self);
  642. ret += fprintf(fp, "\n");
  643. return ret;
  644. }
  645. /*
  646. * collect histogram counts
  647. */
  648. static int
  649. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  650. struct symbol *sym, uint64_t ip, char level)
  651. {
  652. struct rb_node **p = &hist.rb_node;
  653. struct rb_node *parent = NULL;
  654. struct hist_entry *he;
  655. struct hist_entry entry = {
  656. .thread = thread,
  657. .map = map,
  658. .dso = dso,
  659. .sym = sym,
  660. .ip = ip,
  661. .level = level,
  662. .count = 1,
  663. };
  664. int cmp;
  665. while (*p != NULL) {
  666. parent = *p;
  667. he = rb_entry(parent, struct hist_entry, rb_node);
  668. cmp = hist_entry__cmp(&entry, he);
  669. if (!cmp) {
  670. he->count++;
  671. return 0;
  672. }
  673. if (cmp < 0)
  674. p = &(*p)->rb_left;
  675. else
  676. p = &(*p)->rb_right;
  677. }
  678. he = malloc(sizeof(*he));
  679. if (!he)
  680. return -ENOMEM;
  681. *he = entry;
  682. rb_link_node(&he->rb_node, parent, p);
  683. rb_insert_color(&he->rb_node, &hist);
  684. return 0;
  685. }
  686. /*
  687. * reverse the map, sort on count.
  688. */
  689. static struct rb_root output_hists;
  690. static void output__insert_entry(struct hist_entry *he)
  691. {
  692. struct rb_node **p = &output_hists.rb_node;
  693. struct rb_node *parent = NULL;
  694. struct hist_entry *iter;
  695. while (*p != NULL) {
  696. parent = *p;
  697. iter = rb_entry(parent, struct hist_entry, rb_node);
  698. if (he->count > iter->count)
  699. p = &(*p)->rb_left;
  700. else
  701. p = &(*p)->rb_right;
  702. }
  703. rb_link_node(&he->rb_node, parent, p);
  704. rb_insert_color(&he->rb_node, &output_hists);
  705. }
  706. static void output__resort(void)
  707. {
  708. struct rb_node *next = rb_first(&hist);
  709. struct hist_entry *n;
  710. while (next) {
  711. n = rb_entry(next, struct hist_entry, rb_node);
  712. next = rb_next(&n->rb_node);
  713. rb_erase(&n->rb_node, &hist);
  714. output__insert_entry(n);
  715. }
  716. }
  717. static size_t output__fprintf(FILE *fp, uint64_t total_samples)
  718. {
  719. struct hist_entry *pos;
  720. struct rb_node *nd;
  721. size_t ret = 0;
  722. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  723. pos = rb_entry(nd, struct hist_entry, rb_node);
  724. ret += hist_entry__fprintf(fp, pos, total_samples);
  725. }
  726. return ret;
  727. }
  728. static int __cmd_report(void)
  729. {
  730. unsigned long offset = 0;
  731. unsigned long head = 0;
  732. struct stat stat;
  733. char *buf;
  734. event_t *event;
  735. int ret, rc = EXIT_FAILURE;
  736. uint32_t size;
  737. unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
  738. input = open(input_name, O_RDONLY);
  739. if (input < 0) {
  740. perror("failed to open file");
  741. exit(-1);
  742. }
  743. ret = fstat(input, &stat);
  744. if (ret < 0) {
  745. perror("failed to stat file");
  746. exit(-1);
  747. }
  748. if (!stat.st_size) {
  749. fprintf(stderr, "zero-sized file, nothing to do!\n");
  750. exit(0);
  751. }
  752. if (load_kernel() < 0) {
  753. perror("failed to open kallsyms");
  754. return EXIT_FAILURE;
  755. }
  756. remap:
  757. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  758. MAP_SHARED, input, offset);
  759. if (buf == MAP_FAILED) {
  760. perror("failed to mmap file");
  761. exit(-1);
  762. }
  763. more:
  764. event = (event_t *)(buf + head);
  765. size = event->header.size;
  766. if (!size)
  767. size = 8;
  768. if (head + event->header.size >= page_size * mmap_window) {
  769. unsigned long shift = page_size * (head / page_size);
  770. int ret;
  771. ret = munmap(buf, page_size * mmap_window);
  772. assert(ret == 0);
  773. offset += shift;
  774. head -= shift;
  775. goto remap;
  776. }
  777. size = event->header.size;
  778. if (!size)
  779. goto broken_event;
  780. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  781. char level;
  782. int show = 0;
  783. struct dso *dso = NULL;
  784. struct thread *thread = threads__findnew(event->ip.pid);
  785. uint64_t ip = event->ip.ip;
  786. struct map *map = NULL;
  787. if (dump_trace) {
  788. fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  789. (void *)(offset + head),
  790. (void *)(long)(event->header.size),
  791. event->header.misc,
  792. event->ip.pid,
  793. (void *)(long)ip);
  794. }
  795. if (thread == NULL) {
  796. fprintf(stderr, "problem processing %d event, bailing out\n",
  797. event->header.type);
  798. goto done;
  799. }
  800. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  801. show = SHOW_KERNEL;
  802. level = 'k';
  803. dso = kernel_dso;
  804. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  805. show = SHOW_USER;
  806. level = '.';
  807. map = thread__find_map(thread, ip);
  808. if (map != NULL) {
  809. dso = map->dso;
  810. ip -= map->start + map->pgoff;
  811. }
  812. } else {
  813. show = SHOW_HV;
  814. level = 'H';
  815. }
  816. if (show & show_mask) {
  817. struct symbol *sym = dso__find_symbol(dso, ip);
  818. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  819. fprintf(stderr,
  820. "problem incrementing symbol count, bailing out\n");
  821. goto done;
  822. }
  823. }
  824. total++;
  825. } else switch (event->header.type) {
  826. case PERF_EVENT_MMAP: {
  827. struct thread *thread = threads__findnew(event->mmap.pid);
  828. struct map *map = map__new(&event->mmap);
  829. if (dump_trace) {
  830. fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
  831. (void *)(offset + head),
  832. (void *)(long)(event->header.size),
  833. (void *)(long)event->mmap.start,
  834. (void *)(long)event->mmap.len,
  835. (void *)(long)event->mmap.pgoff,
  836. event->mmap.filename);
  837. }
  838. if (thread == NULL || map == NULL) {
  839. fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
  840. goto done;
  841. }
  842. thread__insert_map(thread, map);
  843. total_mmap++;
  844. break;
  845. }
  846. case PERF_EVENT_COMM: {
  847. struct thread *thread = threads__findnew(event->comm.pid);
  848. if (dump_trace) {
  849. fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  850. (void *)(offset + head),
  851. (void *)(long)(event->header.size),
  852. event->comm.comm, event->comm.pid);
  853. }
  854. if (thread == NULL ||
  855. thread__set_comm(thread, event->comm.comm)) {
  856. fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
  857. goto done;
  858. }
  859. total_comm++;
  860. break;
  861. }
  862. default: {
  863. broken_event:
  864. if (dump_trace)
  865. fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
  866. (void *)(offset + head),
  867. (void *)(long)(event->header.size),
  868. event->header.type);
  869. total_unknown++;
  870. /*
  871. * assume we lost track of the stream, check alignment, and
  872. * increment a single u64 in the hope to catch on again 'soon'.
  873. */
  874. if (unlikely(head & 7))
  875. head &= ~7ULL;
  876. size = 8;
  877. }
  878. }
  879. head += size;
  880. if (offset + head < stat.st_size)
  881. goto more;
  882. rc = EXIT_SUCCESS;
  883. done:
  884. close(input);
  885. if (dump_trace) {
  886. fprintf(stderr, " IP events: %10ld\n", total);
  887. fprintf(stderr, " mmap events: %10ld\n", total_mmap);
  888. fprintf(stderr, " comm events: %10ld\n", total_comm);
  889. fprintf(stderr, " unknown events: %10ld\n", total_unknown);
  890. return 0;
  891. }
  892. if (verbose >= 2)
  893. dsos__fprintf(stdout);
  894. output__resort();
  895. output__fprintf(stdout, total);
  896. return rc;
  897. }
  898. static const char * const report_usage[] = {
  899. "perf report [<options>] <command>",
  900. NULL
  901. };
  902. static const struct option options[] = {
  903. OPT_STRING('i', "input", &input_name, "file",
  904. "input file name"),
  905. OPT_BOOLEAN('v', "verbose", &verbose,
  906. "be more verbose (show symbol address, etc)"),
  907. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  908. "dump raw trace in ASCII"),
  909. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  910. OPT_END()
  911. };
  912. int cmd_report(int argc, const char **argv, const char *prefix)
  913. {
  914. elf_version(EV_CURRENT);
  915. page_size = getpagesize();
  916. parse_options(argc, argv, options, report_usage, 0);
  917. setup_sorting();
  918. setup_pager();
  919. return __cmd_report();
  920. }