builtin-annotate.c 26 KB

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