builtin-annotate.c 26 KB

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