builtin-report.c 23 KB

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