builtin-report.c 24 KB

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