builtin-annotate.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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 (offset < len && 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
  939. get_source_line(struct symbol *sym, u64 start, int len, char *filename)
  940. {
  941. int i;
  942. char cmd[PATH_MAX * 2];
  943. struct sym_ext *sym_ext;
  944. if (!sym->hist_sum)
  945. return;
  946. sym->priv = calloc(len, sizeof(struct sym_ext));
  947. if (!sym->priv)
  948. return;
  949. sym_ext = sym->priv;
  950. for (i = 0; i < len; i++) {
  951. char *path = NULL;
  952. size_t line_len;
  953. u64 offset;
  954. FILE *fp;
  955. sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
  956. if (sym_ext[i].percent <= 0.5)
  957. continue;
  958. offset = start + i;
  959. sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
  960. fp = popen(cmd, "r");
  961. if (!fp)
  962. continue;
  963. if (getline(&path, &line_len, fp) < 0 || !line_len)
  964. goto next;
  965. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  966. if (!sym_ext[i].path)
  967. goto next;
  968. strcpy(sym_ext[i].path, path);
  969. insert_source_line(&sym_ext[i]);
  970. next:
  971. pclose(fp);
  972. }
  973. }
  974. static void print_summary(char *filename)
  975. {
  976. struct sym_ext *sym_ext;
  977. struct rb_node *node;
  978. printf("\nSorted summary for file %s\n", filename);
  979. printf("----------------------------------------------\n\n");
  980. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  981. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  982. return;
  983. }
  984. node = rb_first(&root_sym_ext);
  985. while (node) {
  986. double percent;
  987. char *color;
  988. char *path;
  989. sym_ext = rb_entry(node, struct sym_ext, node);
  990. percent = sym_ext->percent;
  991. color = get_color(percent);
  992. path = sym_ext->path;
  993. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  994. node = rb_next(node);
  995. }
  996. }
  997. static void annotate_sym(struct dso *dso, struct symbol *sym)
  998. {
  999. char *filename = dso->name;
  1000. u64 start, end, len;
  1001. char command[PATH_MAX*2];
  1002. FILE *file;
  1003. if (!filename)
  1004. return;
  1005. if (dso == kernel_dso)
  1006. filename = vmlinux;
  1007. start = sym->obj_start;
  1008. if (!start)
  1009. start = sym->start;
  1010. end = start + sym->end - sym->start + 1;
  1011. len = sym->end - sym->start;
  1012. if (print_line) {
  1013. get_source_line(sym, start, len, filename);
  1014. print_summary(filename);
  1015. }
  1016. printf("\n\n------------------------------------------------\n");
  1017. printf(" Percent | Source code & Disassembly of %s\n", filename);
  1018. printf("------------------------------------------------\n");
  1019. if (verbose >= 2)
  1020. printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
  1021. sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", (u64)start, (u64)end, filename);
  1022. if (verbose >= 3)
  1023. printf("doing: %s\n", command);
  1024. file = popen(command, "r");
  1025. if (!file)
  1026. return;
  1027. while (!feof(file)) {
  1028. if (parse_line(file, sym, start, len) < 0)
  1029. break;
  1030. }
  1031. pclose(file);
  1032. if (print_line)
  1033. free_source_line(sym, len);
  1034. }
  1035. static void find_annotations(void)
  1036. {
  1037. struct rb_node *nd;
  1038. struct dso *dso;
  1039. int count = 0;
  1040. list_for_each_entry(dso, &dsos, node) {
  1041. for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
  1042. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  1043. if (sym->hist) {
  1044. annotate_sym(dso, sym);
  1045. count++;
  1046. }
  1047. }
  1048. }
  1049. if (!count)
  1050. printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
  1051. }
  1052. static int __cmd_annotate(void)
  1053. {
  1054. int ret, rc = EXIT_FAILURE;
  1055. unsigned long offset = 0;
  1056. unsigned long head = 0;
  1057. struct stat stat;
  1058. event_t *event;
  1059. uint32_t size;
  1060. char *buf;
  1061. register_idle_thread();
  1062. input = open(input_name, O_RDONLY);
  1063. if (input < 0) {
  1064. perror("failed to open file");
  1065. exit(-1);
  1066. }
  1067. ret = fstat(input, &stat);
  1068. if (ret < 0) {
  1069. perror("failed to stat file");
  1070. exit(-1);
  1071. }
  1072. if (!stat.st_size) {
  1073. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1074. exit(0);
  1075. }
  1076. if (load_kernel() < 0) {
  1077. perror("failed to load kernel symbols");
  1078. return EXIT_FAILURE;
  1079. }
  1080. remap:
  1081. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1082. MAP_SHARED, input, offset);
  1083. if (buf == MAP_FAILED) {
  1084. perror("failed to mmap file");
  1085. exit(-1);
  1086. }
  1087. more:
  1088. event = (event_t *)(buf + head);
  1089. size = event->header.size;
  1090. if (!size)
  1091. size = 8;
  1092. if (head + event->header.size >= page_size * mmap_window) {
  1093. unsigned long shift = page_size * (head / page_size);
  1094. int ret;
  1095. ret = munmap(buf, page_size * mmap_window);
  1096. assert(ret == 0);
  1097. offset += shift;
  1098. head -= shift;
  1099. goto remap;
  1100. }
  1101. size = event->header.size;
  1102. dprintf("%p [%p]: event: %d\n",
  1103. (void *)(offset + head),
  1104. (void *)(long)event->header.size,
  1105. event->header.type);
  1106. if (!size || process_event(event, offset, head) < 0) {
  1107. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1108. (void *)(offset + head),
  1109. (void *)(long)(event->header.size),
  1110. event->header.type);
  1111. total_unknown++;
  1112. /*
  1113. * assume we lost track of the stream, check alignment, and
  1114. * increment a single u64 in the hope to catch on again 'soon'.
  1115. */
  1116. if (unlikely(head & 7))
  1117. head &= ~7ULL;
  1118. size = 8;
  1119. }
  1120. head += size;
  1121. if (offset + head < stat.st_size)
  1122. goto more;
  1123. rc = EXIT_SUCCESS;
  1124. close(input);
  1125. dprintf(" IP events: %10ld\n", total);
  1126. dprintf(" mmap events: %10ld\n", total_mmap);
  1127. dprintf(" comm events: %10ld\n", total_comm);
  1128. dprintf(" fork events: %10ld\n", total_fork);
  1129. dprintf(" unknown events: %10ld\n", total_unknown);
  1130. if (dump_trace)
  1131. return 0;
  1132. if (verbose >= 3)
  1133. threads__fprintf(stdout);
  1134. if (verbose >= 2)
  1135. dsos__fprintf(stdout);
  1136. collapse__resort();
  1137. output__resort();
  1138. find_annotations();
  1139. return rc;
  1140. }
  1141. static const char * const annotate_usage[] = {
  1142. "perf annotate [<options>] <command>",
  1143. NULL
  1144. };
  1145. static const struct option options[] = {
  1146. OPT_STRING('i', "input", &input_name, "file",
  1147. "input file name"),
  1148. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  1149. "symbol to annotate"),
  1150. OPT_BOOLEAN('v', "verbose", &verbose,
  1151. "be more verbose (show symbol address, etc)"),
  1152. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1153. "dump raw trace in ASCII"),
  1154. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1155. OPT_BOOLEAN('l', "print-line", &print_line,
  1156. "print matching source lines (may be slow)"),
  1157. OPT_END()
  1158. };
  1159. static void setup_sorting(void)
  1160. {
  1161. char *tmp, *tok, *str = strdup(sort_order);
  1162. for (tok = strtok_r(str, ", ", &tmp);
  1163. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1164. if (sort_dimension__add(tok) < 0) {
  1165. error("Unknown --sort key: `%s'", tok);
  1166. usage_with_options(annotate_usage, options);
  1167. }
  1168. }
  1169. free(str);
  1170. }
  1171. int cmd_annotate(int argc, const char **argv, const char *prefix)
  1172. {
  1173. symbol__init();
  1174. page_size = getpagesize();
  1175. argc = parse_options(argc, argv, options, annotate_usage, 0);
  1176. setup_sorting();
  1177. if (argc) {
  1178. /*
  1179. * Special case: if there's an argument left then assume tha
  1180. * it's a symbol filter:
  1181. */
  1182. if (argc > 1)
  1183. usage_with_options(annotate_usage, options);
  1184. sym_hist_filter = argv[0];
  1185. }
  1186. if (!sym_hist_filter)
  1187. usage_with_options(annotate_usage, options);
  1188. setup_pager();
  1189. return __cmd_annotate();
  1190. }