builtin-annotate.c 28 KB

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