builtin-annotate.c 28 KB

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