builtin-annotate.c 29 KB

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