builtin-report.c 25 KB

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