builtin-report.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. struct thread {
  480. struct rb_node rb_node;
  481. struct list_head maps;
  482. pid_t pid;
  483. char *comm;
  484. };
  485. static struct thread *thread__new(pid_t pid)
  486. {
  487. struct thread *self = malloc(sizeof(*self));
  488. if (self != NULL) {
  489. self->pid = pid;
  490. self->comm = NULL;
  491. INIT_LIST_HEAD(&self->maps);
  492. }
  493. return self;
  494. }
  495. static int thread__set_comm(struct thread *self, const char *comm)
  496. {
  497. self->comm = strdup(comm);
  498. return self->comm ? 0 : -ENOMEM;
  499. }
  500. static struct rb_root threads;
  501. static struct thread *threads__findnew(pid_t pid)
  502. {
  503. struct rb_node **p = &threads.rb_node;
  504. struct rb_node *parent = NULL;
  505. struct thread *th;
  506. while (*p != NULL) {
  507. parent = *p;
  508. th = rb_entry(parent, struct thread, rb_node);
  509. if (th->pid == pid)
  510. return th;
  511. if (pid < th->pid)
  512. p = &(*p)->rb_left;
  513. else
  514. p = &(*p)->rb_right;
  515. }
  516. th = thread__new(pid);
  517. if (th != NULL) {
  518. rb_link_node(&th->rb_node, parent, p);
  519. rb_insert_color(&th->rb_node, &threads);
  520. }
  521. return th;
  522. }
  523. static void thread__insert_map(struct thread *self, struct map *map)
  524. {
  525. list_add_tail(&map->node, &self->maps);
  526. }
  527. static struct map *thread__find_map(struct thread *self, uint64_t ip)
  528. {
  529. struct map *pos;
  530. if (self == NULL)
  531. return NULL;
  532. list_for_each_entry(pos, &self->maps, node)
  533. if (ip >= pos->start && ip <= pos->end)
  534. return pos;
  535. return NULL;
  536. }
  537. /*
  538. * histogram, sorted on item, collects counts
  539. */
  540. static struct rb_root hist;
  541. struct hist_entry {
  542. struct rb_node rb_node;
  543. struct thread *thread;
  544. struct map *map;
  545. struct dso *dso;
  546. struct symbol *sym;
  547. uint64_t ip;
  548. char level;
  549. uint32_t count;
  550. };
  551. /*
  552. * configurable sorting bits
  553. */
  554. struct sort_entry {
  555. struct list_head list;
  556. char *header;
  557. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  558. size_t (*print)(FILE *fp, struct hist_entry *);
  559. };
  560. static int64_t
  561. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  562. {
  563. return right->thread->pid - left->thread->pid;
  564. }
  565. static size_t
  566. sort__thread_print(FILE *fp, struct hist_entry *self)
  567. {
  568. return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
  569. }
  570. static struct sort_entry sort_thread = {
  571. .header = " Command: Pid ",
  572. .cmp = sort__thread_cmp,
  573. .print = sort__thread_print,
  574. };
  575. static int64_t
  576. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  577. {
  578. char *comm_l = left->thread->comm;
  579. char *comm_r = right->thread->comm;
  580. if (!comm_l || !comm_r) {
  581. if (!comm_l && !comm_r)
  582. return 0;
  583. else if (!comm_l)
  584. return -1;
  585. else
  586. return 1;
  587. }
  588. return strcmp(comm_l, comm_r);
  589. }
  590. static size_t
  591. sort__comm_print(FILE *fp, struct hist_entry *self)
  592. {
  593. return fprintf(fp, " %16s", self->thread->comm ?: "<unknown>");
  594. }
  595. static struct sort_entry sort_comm = {
  596. .header = " Command",
  597. .cmp = sort__comm_cmp,
  598. .print = sort__comm_print,
  599. };
  600. static int64_t
  601. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  602. {
  603. struct dso *dso_l = left->dso;
  604. struct dso *dso_r = right->dso;
  605. if (!dso_l || !dso_r) {
  606. if (!dso_l && !dso_r)
  607. return 0;
  608. else if (!dso_l)
  609. return -1;
  610. else
  611. return 1;
  612. }
  613. return strcmp(dso_l->name, dso_r->name);
  614. }
  615. static size_t
  616. sort__dso_print(FILE *fp, struct hist_entry *self)
  617. {
  618. return fprintf(fp, " %64s", self->dso ? self->dso->name : "<unknown>");
  619. }
  620. static struct sort_entry sort_dso = {
  621. .header = " Shared Object",
  622. .cmp = sort__dso_cmp,
  623. .print = sort__dso_print,
  624. };
  625. static int64_t
  626. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  627. {
  628. uint64_t ip_l, ip_r;
  629. if (left->sym == right->sym)
  630. return 0;
  631. ip_l = left->sym ? left->sym->start : left->ip;
  632. ip_r = right->sym ? right->sym->start : right->ip;
  633. return (int64_t)(ip_r - ip_l);
  634. }
  635. static size_t
  636. sort__sym_print(FILE *fp, struct hist_entry *self)
  637. {
  638. size_t ret = 0;
  639. if (verbose)
  640. ret += fprintf(fp, " %#018llx", (unsigned long long)self->ip);
  641. ret += fprintf(fp, " %s: %s",
  642. self->dso ? self->dso->name : "<unknown>",
  643. self->sym ? self->sym->name : "<unknown>");
  644. return ret;
  645. }
  646. static struct sort_entry sort_sym = {
  647. .header = "Shared Object: Symbol",
  648. .cmp = sort__sym_cmp,
  649. .print = sort__sym_print,
  650. };
  651. struct sort_dimension {
  652. char *name;
  653. struct sort_entry *entry;
  654. int taken;
  655. };
  656. static struct sort_dimension sort_dimensions[] = {
  657. { .name = "pid", .entry = &sort_thread, },
  658. { .name = "comm", .entry = &sort_comm, },
  659. { .name = "dso", .entry = &sort_dso, },
  660. { .name = "symbol", .entry = &sort_sym, },
  661. };
  662. static LIST_HEAD(hist_entry__sort_list);
  663. static int sort_dimension__add(char *tok)
  664. {
  665. int i;
  666. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  667. struct sort_dimension *sd = &sort_dimensions[i];
  668. if (sd->taken)
  669. continue;
  670. if (strcmp(tok, sd->name))
  671. continue;
  672. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  673. sd->taken = 1;
  674. return 0;
  675. }
  676. return -ESRCH;
  677. }
  678. static void setup_sorting(void)
  679. {
  680. char *tmp, *tok, *str = strdup(sort_order);
  681. for (tok = strtok_r(str, ", ", &tmp);
  682. tok; tok = strtok_r(NULL, ", ", &tmp))
  683. sort_dimension__add(tok);
  684. free(str);
  685. }
  686. static int64_t
  687. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  688. {
  689. struct sort_entry *se;
  690. int64_t cmp = 0;
  691. list_for_each_entry(se, &hist_entry__sort_list, list) {
  692. cmp = se->cmp(left, right);
  693. if (cmp)
  694. break;
  695. }
  696. return cmp;
  697. }
  698. static size_t
  699. hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
  700. {
  701. struct sort_entry *se;
  702. size_t ret;
  703. if (total_samples) {
  704. ret = fprintf(fp, " %5.2f%%",
  705. (self->count * 100.0) / total_samples);
  706. } else
  707. ret = fprintf(fp, "%12d ", self->count);
  708. list_for_each_entry(se, &hist_entry__sort_list, list)
  709. ret += se->print(fp, self);
  710. ret += fprintf(fp, "\n");
  711. return ret;
  712. }
  713. /*
  714. * collect histogram counts
  715. */
  716. static int
  717. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  718. struct symbol *sym, uint64_t ip, char level)
  719. {
  720. struct rb_node **p = &hist.rb_node;
  721. struct rb_node *parent = NULL;
  722. struct hist_entry *he;
  723. struct hist_entry entry = {
  724. .thread = thread,
  725. .map = map,
  726. .dso = dso,
  727. .sym = sym,
  728. .ip = ip,
  729. .level = level,
  730. .count = 1,
  731. };
  732. int cmp;
  733. while (*p != NULL) {
  734. parent = *p;
  735. he = rb_entry(parent, struct hist_entry, rb_node);
  736. cmp = hist_entry__cmp(&entry, he);
  737. if (!cmp) {
  738. he->count++;
  739. return 0;
  740. }
  741. if (cmp < 0)
  742. p = &(*p)->rb_left;
  743. else
  744. p = &(*p)->rb_right;
  745. }
  746. he = malloc(sizeof(*he));
  747. if (!he)
  748. return -ENOMEM;
  749. *he = entry;
  750. rb_link_node(&he->rb_node, parent, p);
  751. rb_insert_color(&he->rb_node, &hist);
  752. return 0;
  753. }
  754. /*
  755. * reverse the map, sort on count.
  756. */
  757. static struct rb_root output_hists;
  758. static void output__insert_entry(struct hist_entry *he)
  759. {
  760. struct rb_node **p = &output_hists.rb_node;
  761. struct rb_node *parent = NULL;
  762. struct hist_entry *iter;
  763. while (*p != NULL) {
  764. parent = *p;
  765. iter = rb_entry(parent, struct hist_entry, rb_node);
  766. if (he->count > iter->count)
  767. p = &(*p)->rb_left;
  768. else
  769. p = &(*p)->rb_right;
  770. }
  771. rb_link_node(&he->rb_node, parent, p);
  772. rb_insert_color(&he->rb_node, &output_hists);
  773. }
  774. static void output__resort(void)
  775. {
  776. struct rb_node *next = rb_first(&hist);
  777. struct hist_entry *n;
  778. while (next) {
  779. n = rb_entry(next, struct hist_entry, rb_node);
  780. next = rb_next(&n->rb_node);
  781. rb_erase(&n->rb_node, &hist);
  782. output__insert_entry(n);
  783. }
  784. }
  785. static size_t output__fprintf(FILE *fp, uint64_t total_samples)
  786. {
  787. struct hist_entry *pos;
  788. struct sort_entry *se;
  789. struct rb_node *nd;
  790. size_t ret = 0;
  791. fprintf(fp, "#\n");
  792. fprintf(fp, "# Overhead");
  793. list_for_each_entry(se, &hist_entry__sort_list, list)
  794. fprintf(fp, " %s", se->header);
  795. fprintf(fp, "\n");
  796. fprintf(fp, "# ........");
  797. list_for_each_entry(se, &hist_entry__sort_list, list) {
  798. int i;
  799. fprintf(fp, " ");
  800. for (i = 0; i < strlen(se->header); i++)
  801. fprintf(fp, ".");
  802. }
  803. fprintf(fp, "\n");
  804. fprintf(fp, "#\n");
  805. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  806. pos = rb_entry(nd, struct hist_entry, rb_node);
  807. ret += hist_entry__fprintf(fp, pos, total_samples);
  808. }
  809. return ret;
  810. }
  811. static int __cmd_report(void)
  812. {
  813. unsigned long offset = 0;
  814. unsigned long head = 0;
  815. struct stat stat;
  816. char *buf;
  817. event_t *event;
  818. int ret, rc = EXIT_FAILURE;
  819. uint32_t size;
  820. unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
  821. input = open(input_name, O_RDONLY);
  822. if (input < 0) {
  823. perror("failed to open file");
  824. exit(-1);
  825. }
  826. ret = fstat(input, &stat);
  827. if (ret < 0) {
  828. perror("failed to stat file");
  829. exit(-1);
  830. }
  831. if (!stat.st_size) {
  832. fprintf(stderr, "zero-sized file, nothing to do!\n");
  833. exit(0);
  834. }
  835. if (load_kernel() < 0) {
  836. perror("failed to open kallsyms");
  837. return EXIT_FAILURE;
  838. }
  839. remap:
  840. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  841. MAP_SHARED, input, offset);
  842. if (buf == MAP_FAILED) {
  843. perror("failed to mmap file");
  844. exit(-1);
  845. }
  846. more:
  847. event = (event_t *)(buf + head);
  848. size = event->header.size;
  849. if (!size)
  850. size = 8;
  851. if (head + event->header.size >= page_size * mmap_window) {
  852. unsigned long shift = page_size * (head / page_size);
  853. int ret;
  854. ret = munmap(buf, page_size * mmap_window);
  855. assert(ret == 0);
  856. offset += shift;
  857. head -= shift;
  858. goto remap;
  859. }
  860. size = event->header.size;
  861. if (!size)
  862. goto broken_event;
  863. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  864. char level;
  865. int show = 0;
  866. struct dso *dso = NULL;
  867. struct thread *thread = threads__findnew(event->ip.pid);
  868. uint64_t ip = event->ip.ip;
  869. struct map *map = NULL;
  870. if (dump_trace) {
  871. fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  872. (void *)(offset + head),
  873. (void *)(long)(event->header.size),
  874. event->header.misc,
  875. event->ip.pid,
  876. (void *)(long)ip);
  877. }
  878. if (thread == NULL) {
  879. fprintf(stderr, "problem processing %d event, skipping it.\n",
  880. event->header.type);
  881. goto broken_event;
  882. }
  883. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  884. show = SHOW_KERNEL;
  885. level = 'k';
  886. dso = kernel_dso;
  887. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  888. show = SHOW_USER;
  889. level = '.';
  890. map = thread__find_map(thread, ip);
  891. if (map != NULL) {
  892. dso = map->dso;
  893. ip -= map->start + map->pgoff;
  894. }
  895. } else {
  896. show = SHOW_HV;
  897. level = 'H';
  898. }
  899. if (show & show_mask) {
  900. struct symbol *sym = dso__find_symbol(dso, ip);
  901. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  902. fprintf(stderr,
  903. "problem incrementing symbol count, skipping event\n");
  904. goto broken_event;
  905. }
  906. }
  907. total++;
  908. } else switch (event->header.type) {
  909. case PERF_EVENT_MMAP: {
  910. struct thread *thread = threads__findnew(event->mmap.pid);
  911. struct map *map = map__new(&event->mmap);
  912. if (dump_trace) {
  913. fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
  914. (void *)(offset + head),
  915. (void *)(long)(event->header.size),
  916. (void *)(long)event->mmap.start,
  917. (void *)(long)event->mmap.len,
  918. (void *)(long)event->mmap.pgoff,
  919. event->mmap.filename);
  920. }
  921. if (thread == NULL || map == NULL) {
  922. fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
  923. goto broken_event;
  924. }
  925. thread__insert_map(thread, map);
  926. total_mmap++;
  927. break;
  928. }
  929. case PERF_EVENT_COMM: {
  930. struct thread *thread = threads__findnew(event->comm.pid);
  931. if (dump_trace) {
  932. fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  933. (void *)(offset + head),
  934. (void *)(long)(event->header.size),
  935. event->comm.comm, event->comm.pid);
  936. }
  937. if (thread == NULL ||
  938. thread__set_comm(thread, event->comm.comm)) {
  939. fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
  940. goto broken_event;
  941. }
  942. total_comm++;
  943. break;
  944. }
  945. default: {
  946. broken_event:
  947. if (dump_trace)
  948. fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
  949. (void *)(offset + head),
  950. (void *)(long)(event->header.size),
  951. event->header.type);
  952. total_unknown++;
  953. /*
  954. * assume we lost track of the stream, check alignment, and
  955. * increment a single u64 in the hope to catch on again 'soon'.
  956. */
  957. if (unlikely(head & 7))
  958. head &= ~7ULL;
  959. size = 8;
  960. }
  961. }
  962. head += size;
  963. if (offset + head < stat.st_size)
  964. goto more;
  965. rc = EXIT_SUCCESS;
  966. close(input);
  967. if (dump_trace) {
  968. fprintf(stderr, " IP events: %10ld\n", total);
  969. fprintf(stderr, " mmap events: %10ld\n", total_mmap);
  970. fprintf(stderr, " comm events: %10ld\n", total_comm);
  971. fprintf(stderr, " unknown events: %10ld\n", total_unknown);
  972. return 0;
  973. }
  974. if (verbose >= 2)
  975. dsos__fprintf(stdout);
  976. output__resort();
  977. output__fprintf(stdout, total);
  978. return rc;
  979. }
  980. static const char * const report_usage[] = {
  981. "perf report [<options>] <command>",
  982. NULL
  983. };
  984. static const struct option options[] = {
  985. OPT_STRING('i', "input", &input_name, "file",
  986. "input file name"),
  987. OPT_BOOLEAN('v', "verbose", &verbose,
  988. "be more verbose (show symbol address, etc)"),
  989. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  990. "dump raw trace in ASCII"),
  991. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  992. OPT_STRING('s', "sort", &sort_order, "foo", "bar"),
  993. OPT_END()
  994. };
  995. int cmd_report(int argc, const char **argv, const char *prefix)
  996. {
  997. elf_version(EV_CURRENT);
  998. page_size = getpagesize();
  999. parse_options(argc, argv, options, report_usage, 0);
  1000. setup_sorting();
  1001. setup_pager();
  1002. return __cmd_report();
  1003. }