builtin-annotate.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 = "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 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, __u64 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. __u64 start;
  142. __u64 end;
  143. __u64 pgoff;
  144. __u64 (*map_ip)(struct map *, __u64);
  145. struct dso *dso;
  146. };
  147. static __u64 map__map_ip(struct map *map, __u64 ip)
  148. {
  149. return ip - map->start + map->pgoff;
  150. }
  151. static __u64 vdso__map_ip(struct map *map, __u64 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, " %Lx-%Lx %Lx %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, __u64 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. __u64 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. __u64 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, __u64 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 *)(unsigned long)he->sym->start,
  520. he->sym->name,
  521. (void *)(unsigned long)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, __u64 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. __u64 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, __u64 start, __u64 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, mid-overhead
  854. * entries in green - and keep the low overhead places
  855. * normal:
  856. */
  857. if (percent >= 5.0)
  858. color = PERF_COLOR_RED;
  859. else {
  860. if (percent > 0.5)
  861. color = PERF_COLOR_GREEN;
  862. }
  863. color_fprintf(stdout, color, " %7.2f", percent);
  864. printf(" : ");
  865. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
  866. } else {
  867. if (!*line)
  868. printf(" :\n");
  869. else
  870. printf(" : %s\n", line);
  871. }
  872. return 0;
  873. }
  874. static void annotate_sym(struct dso *dso, struct symbol *sym)
  875. {
  876. char *filename = dso->name;
  877. __u64 start, end, len;
  878. char command[PATH_MAX*2];
  879. FILE *file;
  880. if (!filename)
  881. return;
  882. if (dso == kernel_dso)
  883. filename = vmlinux;
  884. printf("\n------------------------------------------------\n");
  885. printf(" Percent | Source code & Disassembly of %s\n", filename);
  886. printf("------------------------------------------------\n");
  887. if (verbose >= 2)
  888. printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
  889. start = sym->obj_start;
  890. if (!start)
  891. start = sym->start;
  892. end = start + sym->end - sym->start + 1;
  893. len = sym->end - sym->start;
  894. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", (__u64)start, (__u64)end, filename);
  895. if (verbose >= 3)
  896. printf("doing: %s\n", command);
  897. file = popen(command, "r");
  898. if (!file)
  899. return;
  900. while (!feof(file)) {
  901. if (parse_line(file, sym, start, len) < 0)
  902. break;
  903. }
  904. pclose(file);
  905. }
  906. static void find_annotations(void)
  907. {
  908. struct rb_node *nd;
  909. struct dso *dso;
  910. int count = 0;
  911. list_for_each_entry(dso, &dsos, node) {
  912. for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
  913. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  914. if (sym->hist) {
  915. annotate_sym(dso, sym);
  916. count++;
  917. }
  918. }
  919. }
  920. if (!count)
  921. printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
  922. }
  923. static int __cmd_annotate(void)
  924. {
  925. int ret, rc = EXIT_FAILURE;
  926. unsigned long offset = 0;
  927. unsigned long head = 0;
  928. struct stat stat;
  929. event_t *event;
  930. uint32_t size;
  931. char *buf;
  932. register_idle_thread();
  933. input = open(input_name, O_RDONLY);
  934. if (input < 0) {
  935. perror("failed to open file");
  936. exit(-1);
  937. }
  938. ret = fstat(input, &stat);
  939. if (ret < 0) {
  940. perror("failed to stat file");
  941. exit(-1);
  942. }
  943. if (!stat.st_size) {
  944. fprintf(stderr, "zero-sized file, nothing to do!\n");
  945. exit(0);
  946. }
  947. if (load_kernel() < 0) {
  948. perror("failed to load kernel symbols");
  949. return EXIT_FAILURE;
  950. }
  951. remap:
  952. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  953. MAP_SHARED, input, offset);
  954. if (buf == MAP_FAILED) {
  955. perror("failed to mmap file");
  956. exit(-1);
  957. }
  958. more:
  959. event = (event_t *)(buf + head);
  960. size = event->header.size;
  961. if (!size)
  962. size = 8;
  963. if (head + event->header.size >= page_size * mmap_window) {
  964. unsigned long shift = page_size * (head / page_size);
  965. int ret;
  966. ret = munmap(buf, page_size * mmap_window);
  967. assert(ret == 0);
  968. offset += shift;
  969. head -= shift;
  970. goto remap;
  971. }
  972. size = event->header.size;
  973. dprintf("%p [%p]: event: %d\n",
  974. (void *)(offset + head),
  975. (void *)(long)event->header.size,
  976. event->header.type);
  977. if (!size || process_event(event, offset, head) < 0) {
  978. dprintf("%p [%p]: skipping unknown header type: %d\n",
  979. (void *)(offset + head),
  980. (void *)(long)(event->header.size),
  981. event->header.type);
  982. total_unknown++;
  983. /*
  984. * assume we lost track of the stream, check alignment, and
  985. * increment a single u64 in the hope to catch on again 'soon'.
  986. */
  987. if (unlikely(head & 7))
  988. head &= ~7ULL;
  989. size = 8;
  990. }
  991. head += size;
  992. if (offset + head < stat.st_size)
  993. goto more;
  994. rc = EXIT_SUCCESS;
  995. close(input);
  996. dprintf(" IP events: %10ld\n", total);
  997. dprintf(" mmap events: %10ld\n", total_mmap);
  998. dprintf(" comm events: %10ld\n", total_comm);
  999. dprintf(" fork events: %10ld\n", total_fork);
  1000. dprintf(" unknown events: %10ld\n", total_unknown);
  1001. if (dump_trace)
  1002. return 0;
  1003. if (verbose >= 3)
  1004. threads__fprintf(stdout);
  1005. if (verbose >= 2)
  1006. dsos__fprintf(stdout);
  1007. collapse__resort();
  1008. output__resort();
  1009. find_annotations();
  1010. return rc;
  1011. }
  1012. static const char * const annotate_usage[] = {
  1013. "perf annotate [<options>] <command>",
  1014. NULL
  1015. };
  1016. static const struct option options[] = {
  1017. OPT_STRING('i', "input", &input_name, "file",
  1018. "input file name"),
  1019. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  1020. "symbol to annotate"),
  1021. OPT_BOOLEAN('v', "verbose", &verbose,
  1022. "be more verbose (show symbol address, etc)"),
  1023. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1024. "dump raw trace in ASCII"),
  1025. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1026. OPT_END()
  1027. };
  1028. static void setup_sorting(void)
  1029. {
  1030. char *tmp, *tok, *str = strdup(sort_order);
  1031. for (tok = strtok_r(str, ", ", &tmp);
  1032. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1033. if (sort_dimension__add(tok) < 0) {
  1034. error("Unknown --sort key: `%s'", tok);
  1035. usage_with_options(annotate_usage, options);
  1036. }
  1037. }
  1038. free(str);
  1039. }
  1040. int cmd_annotate(int argc, const char **argv, const char *prefix)
  1041. {
  1042. symbol__init();
  1043. page_size = getpagesize();
  1044. argc = parse_options(argc, argv, options, annotate_usage, 0);
  1045. setup_sorting();
  1046. if (argc) {
  1047. /*
  1048. * Special case: if there's an argument left then assume tha
  1049. * it's a symbol filter:
  1050. */
  1051. if (argc > 1)
  1052. usage_with_options(annotate_usage, options);
  1053. sym_hist_filter = argv[0];
  1054. }
  1055. if (!sym_hist_filter)
  1056. usage_with_options(annotate_usage, options);
  1057. setup_pager();
  1058. return __cmd_annotate();
  1059. }