builtin-annotate.c 29 KB

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