builtin-annotate.c 29 KB

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