hist.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. #include "util.h"
  2. #include "build-id.h"
  3. #include "hist.h"
  4. #include "session.h"
  5. #include "sort.h"
  6. #include <math.h>
  7. enum hist_filter {
  8. HIST_FILTER__DSO,
  9. HIST_FILTER__THREAD,
  10. HIST_FILTER__PARENT,
  11. };
  12. struct callchain_param callchain_param = {
  13. .mode = CHAIN_GRAPH_REL,
  14. .min_percent = 0.5
  15. };
  16. u16 hists__col_len(struct hists *self, enum hist_column col)
  17. {
  18. return self->col_len[col];
  19. }
  20. void hists__set_col_len(struct hists *self, enum hist_column col, u16 len)
  21. {
  22. self->col_len[col] = len;
  23. }
  24. bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len)
  25. {
  26. if (len > hists__col_len(self, col)) {
  27. hists__set_col_len(self, col, len);
  28. return true;
  29. }
  30. return false;
  31. }
  32. static void hists__reset_col_len(struct hists *self)
  33. {
  34. enum hist_column col;
  35. for (col = 0; col < HISTC_NR_COLS; ++col)
  36. hists__set_col_len(self, col, 0);
  37. }
  38. static void hists__calc_col_len(struct hists *self, struct hist_entry *h)
  39. {
  40. u16 len;
  41. if (h->ms.sym)
  42. hists__new_col_len(self, HISTC_SYMBOL, h->ms.sym->namelen);
  43. len = thread__comm_len(h->thread);
  44. if (hists__new_col_len(self, HISTC_COMM, len))
  45. hists__set_col_len(self, HISTC_THREAD, len + 6);
  46. if (h->ms.map) {
  47. len = dso__name_len(h->ms.map->dso);
  48. hists__new_col_len(self, HISTC_DSO, len);
  49. }
  50. }
  51. static void hist_entry__add_cpumode_period(struct hist_entry *self,
  52. unsigned int cpumode, u64 period)
  53. {
  54. switch (cpumode) {
  55. case PERF_RECORD_MISC_KERNEL:
  56. self->period_sys += period;
  57. break;
  58. case PERF_RECORD_MISC_USER:
  59. self->period_us += period;
  60. break;
  61. case PERF_RECORD_MISC_GUEST_KERNEL:
  62. self->period_guest_sys += period;
  63. break;
  64. case PERF_RECORD_MISC_GUEST_USER:
  65. self->period_guest_us += period;
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. /*
  72. * histogram, sorted on item, collects periods
  73. */
  74. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  75. {
  76. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  77. struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
  78. if (self != NULL) {
  79. *self = *template;
  80. self->nr_events = 1;
  81. if (self->ms.map)
  82. self->ms.map->referenced = true;
  83. if (symbol_conf.use_callchain)
  84. callchain_init(self->callchain);
  85. }
  86. return self;
  87. }
  88. static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h)
  89. {
  90. if (!h->filtered) {
  91. hists__calc_col_len(self, h);
  92. ++self->nr_entries;
  93. }
  94. }
  95. static u8 symbol__parent_filter(const struct symbol *parent)
  96. {
  97. if (symbol_conf.exclude_other && parent == NULL)
  98. return 1 << HIST_FILTER__PARENT;
  99. return 0;
  100. }
  101. struct hist_entry *__hists__add_entry(struct hists *self,
  102. struct addr_location *al,
  103. struct symbol *sym_parent, u64 period)
  104. {
  105. struct rb_node **p = &self->entries.rb_node;
  106. struct rb_node *parent = NULL;
  107. struct hist_entry *he;
  108. struct hist_entry entry = {
  109. .thread = al->thread,
  110. .ms = {
  111. .map = al->map,
  112. .sym = al->sym,
  113. },
  114. .cpu = al->cpu,
  115. .ip = al->addr,
  116. .level = al->level,
  117. .period = period,
  118. .parent = sym_parent,
  119. .filtered = symbol__parent_filter(sym_parent),
  120. };
  121. int cmp;
  122. while (*p != NULL) {
  123. parent = *p;
  124. he = rb_entry(parent, struct hist_entry, rb_node);
  125. cmp = hist_entry__cmp(&entry, he);
  126. if (!cmp) {
  127. he->period += period;
  128. ++he->nr_events;
  129. goto out;
  130. }
  131. if (cmp < 0)
  132. p = &(*p)->rb_left;
  133. else
  134. p = &(*p)->rb_right;
  135. }
  136. he = hist_entry__new(&entry);
  137. if (!he)
  138. return NULL;
  139. rb_link_node(&he->rb_node, parent, p);
  140. rb_insert_color(&he->rb_node, &self->entries);
  141. hists__inc_nr_entries(self, he);
  142. out:
  143. hist_entry__add_cpumode_period(he, al->cpumode, period);
  144. return he;
  145. }
  146. int64_t
  147. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  148. {
  149. struct sort_entry *se;
  150. int64_t cmp = 0;
  151. list_for_each_entry(se, &hist_entry__sort_list, list) {
  152. cmp = se->se_cmp(left, right);
  153. if (cmp)
  154. break;
  155. }
  156. return cmp;
  157. }
  158. int64_t
  159. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  160. {
  161. struct sort_entry *se;
  162. int64_t cmp = 0;
  163. list_for_each_entry(se, &hist_entry__sort_list, list) {
  164. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  165. f = se->se_collapse ?: se->se_cmp;
  166. cmp = f(left, right);
  167. if (cmp)
  168. break;
  169. }
  170. return cmp;
  171. }
  172. void hist_entry__free(struct hist_entry *he)
  173. {
  174. free(he);
  175. }
  176. /*
  177. * collapse the histogram
  178. */
  179. static bool hists__collapse_insert_entry(struct hists *self,
  180. struct rb_root *root,
  181. struct hist_entry *he)
  182. {
  183. struct rb_node **p = &root->rb_node;
  184. struct rb_node *parent = NULL;
  185. struct hist_entry *iter;
  186. int64_t cmp;
  187. while (*p != NULL) {
  188. parent = *p;
  189. iter = rb_entry(parent, struct hist_entry, rb_node);
  190. cmp = hist_entry__collapse(iter, he);
  191. if (!cmp) {
  192. iter->period += he->period;
  193. if (symbol_conf.use_callchain) {
  194. callchain_cursor_reset(&self->callchain_cursor);
  195. callchain_merge(&self->callchain_cursor, iter->callchain,
  196. he->callchain);
  197. }
  198. hist_entry__free(he);
  199. return false;
  200. }
  201. if (cmp < 0)
  202. p = &(*p)->rb_left;
  203. else
  204. p = &(*p)->rb_right;
  205. }
  206. rb_link_node(&he->rb_node, parent, p);
  207. rb_insert_color(&he->rb_node, root);
  208. return true;
  209. }
  210. void hists__collapse_resort(struct hists *self)
  211. {
  212. struct rb_root tmp;
  213. struct rb_node *next;
  214. struct hist_entry *n;
  215. if (!sort__need_collapse)
  216. return;
  217. tmp = RB_ROOT;
  218. next = rb_first(&self->entries);
  219. self->nr_entries = 0;
  220. hists__reset_col_len(self);
  221. while (next) {
  222. n = rb_entry(next, struct hist_entry, rb_node);
  223. next = rb_next(&n->rb_node);
  224. rb_erase(&n->rb_node, &self->entries);
  225. if (hists__collapse_insert_entry(self, &tmp, n))
  226. hists__inc_nr_entries(self, n);
  227. }
  228. self->entries = tmp;
  229. }
  230. /*
  231. * reverse the map, sort on period.
  232. */
  233. static void __hists__insert_output_entry(struct rb_root *entries,
  234. struct hist_entry *he,
  235. u64 min_callchain_hits)
  236. {
  237. struct rb_node **p = &entries->rb_node;
  238. struct rb_node *parent = NULL;
  239. struct hist_entry *iter;
  240. if (symbol_conf.use_callchain)
  241. callchain_param.sort(&he->sorted_chain, he->callchain,
  242. min_callchain_hits, &callchain_param);
  243. while (*p != NULL) {
  244. parent = *p;
  245. iter = rb_entry(parent, struct hist_entry, rb_node);
  246. if (he->period > iter->period)
  247. p = &(*p)->rb_left;
  248. else
  249. p = &(*p)->rb_right;
  250. }
  251. rb_link_node(&he->rb_node, parent, p);
  252. rb_insert_color(&he->rb_node, entries);
  253. }
  254. void hists__output_resort(struct hists *self)
  255. {
  256. struct rb_root tmp;
  257. struct rb_node *next;
  258. struct hist_entry *n;
  259. u64 min_callchain_hits;
  260. min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
  261. tmp = RB_ROOT;
  262. next = rb_first(&self->entries);
  263. self->nr_entries = 0;
  264. hists__reset_col_len(self);
  265. while (next) {
  266. n = rb_entry(next, struct hist_entry, rb_node);
  267. next = rb_next(&n->rb_node);
  268. rb_erase(&n->rb_node, &self->entries);
  269. __hists__insert_output_entry(&tmp, n, min_callchain_hits);
  270. hists__inc_nr_entries(self, n);
  271. }
  272. self->entries = tmp;
  273. }
  274. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  275. {
  276. int i;
  277. int ret = fprintf(fp, " ");
  278. for (i = 0; i < left_margin; i++)
  279. ret += fprintf(fp, " ");
  280. return ret;
  281. }
  282. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  283. int left_margin)
  284. {
  285. int i;
  286. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  287. for (i = 0; i < depth; i++)
  288. if (depth_mask & (1 << i))
  289. ret += fprintf(fp, "| ");
  290. else
  291. ret += fprintf(fp, " ");
  292. ret += fprintf(fp, "\n");
  293. return ret;
  294. }
  295. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  296. int depth, int depth_mask, int period,
  297. u64 total_samples, u64 hits,
  298. int left_margin)
  299. {
  300. int i;
  301. size_t ret = 0;
  302. ret += callchain__fprintf_left_margin(fp, left_margin);
  303. for (i = 0; i < depth; i++) {
  304. if (depth_mask & (1 << i))
  305. ret += fprintf(fp, "|");
  306. else
  307. ret += fprintf(fp, " ");
  308. if (!period && i == depth - 1) {
  309. double percent;
  310. percent = hits * 100.0 / total_samples;
  311. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  312. } else
  313. ret += fprintf(fp, "%s", " ");
  314. }
  315. if (chain->ms.sym)
  316. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  317. else
  318. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  319. return ret;
  320. }
  321. static struct symbol *rem_sq_bracket;
  322. static struct callchain_list rem_hits;
  323. static void init_rem_hits(void)
  324. {
  325. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  326. if (!rem_sq_bracket) {
  327. fprintf(stderr, "Not enough memory to display remaining hits\n");
  328. return;
  329. }
  330. strcpy(rem_sq_bracket->name, "[...]");
  331. rem_hits.ms.sym = rem_sq_bracket;
  332. }
  333. static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  334. u64 total_samples, int depth,
  335. int depth_mask, int left_margin)
  336. {
  337. struct rb_node *node, *next;
  338. struct callchain_node *child;
  339. struct callchain_list *chain;
  340. int new_depth_mask = depth_mask;
  341. u64 new_total;
  342. u64 remaining;
  343. size_t ret = 0;
  344. int i;
  345. uint entries_printed = 0;
  346. if (callchain_param.mode == CHAIN_GRAPH_REL)
  347. new_total = self->children_hit;
  348. else
  349. new_total = total_samples;
  350. remaining = new_total;
  351. node = rb_first(&self->rb_root);
  352. while (node) {
  353. u64 cumul;
  354. child = rb_entry(node, struct callchain_node, rb_node);
  355. cumul = callchain_cumul_hits(child);
  356. remaining -= cumul;
  357. /*
  358. * The depth mask manages the output of pipes that show
  359. * the depth. We don't want to keep the pipes of the current
  360. * level for the last child of this depth.
  361. * Except if we have remaining filtered hits. They will
  362. * supersede the last child
  363. */
  364. next = rb_next(node);
  365. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  366. new_depth_mask &= ~(1 << (depth - 1));
  367. /*
  368. * But we keep the older depth mask for the line separator
  369. * to keep the level link until we reach the last child
  370. */
  371. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  372. left_margin);
  373. i = 0;
  374. list_for_each_entry(chain, &child->val, list) {
  375. ret += ipchain__fprintf_graph(fp, chain, depth,
  376. new_depth_mask, i++,
  377. new_total,
  378. cumul,
  379. left_margin);
  380. }
  381. ret += __callchain__fprintf_graph(fp, child, new_total,
  382. depth + 1,
  383. new_depth_mask | (1 << depth),
  384. left_margin);
  385. node = next;
  386. if (++entries_printed == callchain_param.print_limit)
  387. break;
  388. }
  389. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  390. remaining && remaining != new_total) {
  391. if (!rem_sq_bracket)
  392. return ret;
  393. new_depth_mask &= ~(1 << (depth - 1));
  394. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  395. new_depth_mask, 0, new_total,
  396. remaining, left_margin);
  397. }
  398. return ret;
  399. }
  400. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  401. u64 total_samples, int left_margin)
  402. {
  403. struct callchain_list *chain;
  404. bool printed = false;
  405. int i = 0;
  406. int ret = 0;
  407. u32 entries_printed = 0;
  408. list_for_each_entry(chain, &self->val, list) {
  409. if (!i++ && sort__first_dimension == SORT_SYM)
  410. continue;
  411. if (!printed) {
  412. ret += callchain__fprintf_left_margin(fp, left_margin);
  413. ret += fprintf(fp, "|\n");
  414. ret += callchain__fprintf_left_margin(fp, left_margin);
  415. ret += fprintf(fp, "---");
  416. left_margin += 3;
  417. printed = true;
  418. } else
  419. ret += callchain__fprintf_left_margin(fp, left_margin);
  420. if (chain->ms.sym)
  421. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  422. else
  423. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  424. if (++entries_printed == callchain_param.print_limit)
  425. break;
  426. }
  427. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  428. return ret;
  429. }
  430. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  431. u64 total_samples)
  432. {
  433. struct callchain_list *chain;
  434. size_t ret = 0;
  435. if (!self)
  436. return 0;
  437. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  438. list_for_each_entry(chain, &self->val, list) {
  439. if (chain->ip >= PERF_CONTEXT_MAX)
  440. continue;
  441. if (chain->ms.sym)
  442. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  443. else
  444. ret += fprintf(fp, " %p\n",
  445. (void *)(long)chain->ip);
  446. }
  447. return ret;
  448. }
  449. static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  450. u64 total_samples, int left_margin)
  451. {
  452. struct rb_node *rb_node;
  453. struct callchain_node *chain;
  454. size_t ret = 0;
  455. u32 entries_printed = 0;
  456. rb_node = rb_first(&self->sorted_chain);
  457. while (rb_node) {
  458. double percent;
  459. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  460. percent = chain->hit * 100.0 / total_samples;
  461. switch (callchain_param.mode) {
  462. case CHAIN_FLAT:
  463. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  464. percent);
  465. ret += callchain__fprintf_flat(fp, chain, total_samples);
  466. break;
  467. case CHAIN_GRAPH_ABS: /* Falldown */
  468. case CHAIN_GRAPH_REL:
  469. ret += callchain__fprintf_graph(fp, chain, total_samples,
  470. left_margin);
  471. case CHAIN_NONE:
  472. default:
  473. break;
  474. }
  475. ret += fprintf(fp, "\n");
  476. if (++entries_printed == callchain_param.print_limit)
  477. break;
  478. rb_node = rb_next(rb_node);
  479. }
  480. return ret;
  481. }
  482. int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
  483. struct hists *hists, struct hists *pair_hists,
  484. bool show_displacement, long displacement,
  485. bool color, u64 session_total)
  486. {
  487. struct sort_entry *se;
  488. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  489. const char *sep = symbol_conf.field_sep;
  490. int ret;
  491. if (symbol_conf.exclude_other && !self->parent)
  492. return 0;
  493. if (pair_hists) {
  494. period = self->pair ? self->pair->period : 0;
  495. total = pair_hists->stats.total_period;
  496. period_sys = self->pair ? self->pair->period_sys : 0;
  497. period_us = self->pair ? self->pair->period_us : 0;
  498. period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
  499. period_guest_us = self->pair ? self->pair->period_guest_us : 0;
  500. } else {
  501. period = self->period;
  502. total = session_total;
  503. period_sys = self->period_sys;
  504. period_us = self->period_us;
  505. period_guest_sys = self->period_guest_sys;
  506. period_guest_us = self->period_guest_us;
  507. }
  508. if (total) {
  509. if (color)
  510. ret = percent_color_snprintf(s, size,
  511. sep ? "%.2f" : " %6.2f%%",
  512. (period * 100.0) / total);
  513. else
  514. ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  515. (period * 100.0) / total);
  516. if (symbol_conf.show_cpu_utilization) {
  517. ret += percent_color_snprintf(s + ret, size - ret,
  518. sep ? "%.2f" : " %6.2f%%",
  519. (period_sys * 100.0) / total);
  520. ret += percent_color_snprintf(s + ret, size - ret,
  521. sep ? "%.2f" : " %6.2f%%",
  522. (period_us * 100.0) / total);
  523. if (perf_guest) {
  524. ret += percent_color_snprintf(s + ret,
  525. size - ret,
  526. sep ? "%.2f" : " %6.2f%%",
  527. (period_guest_sys * 100.0) /
  528. total);
  529. ret += percent_color_snprintf(s + ret,
  530. size - ret,
  531. sep ? "%.2f" : " %6.2f%%",
  532. (period_guest_us * 100.0) /
  533. total);
  534. }
  535. }
  536. } else
  537. ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  538. if (symbol_conf.show_nr_samples) {
  539. if (sep)
  540. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  541. else
  542. ret += snprintf(s + ret, size - ret, "%11" PRIu64, period);
  543. }
  544. if (pair_hists) {
  545. char bf[32];
  546. double old_percent = 0, new_percent = 0, diff;
  547. if (total > 0)
  548. old_percent = (period * 100.0) / total;
  549. if (session_total > 0)
  550. new_percent = (self->period * 100.0) / session_total;
  551. diff = new_percent - old_percent;
  552. if (fabs(diff) >= 0.01)
  553. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  554. else
  555. snprintf(bf, sizeof(bf), " ");
  556. if (sep)
  557. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  558. else
  559. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  560. if (show_displacement) {
  561. if (displacement)
  562. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  563. else
  564. snprintf(bf, sizeof(bf), " ");
  565. if (sep)
  566. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  567. else
  568. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  569. }
  570. }
  571. list_for_each_entry(se, &hist_entry__sort_list, list) {
  572. if (se->elide)
  573. continue;
  574. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  575. ret += se->se_snprintf(self, s + ret, size - ret,
  576. hists__col_len(hists, se->se_width_idx));
  577. }
  578. return ret;
  579. }
  580. int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
  581. struct hists *pair_hists, bool show_displacement,
  582. long displacement, FILE *fp, u64 session_total)
  583. {
  584. char bf[512];
  585. hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
  586. show_displacement, displacement,
  587. true, session_total);
  588. return fprintf(fp, "%s\n", bf);
  589. }
  590. static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
  591. struct hists *hists, FILE *fp,
  592. u64 session_total)
  593. {
  594. int left_margin = 0;
  595. if (sort__first_dimension == SORT_COMM) {
  596. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  597. typeof(*se), list);
  598. left_margin = hists__col_len(hists, se->se_width_idx);
  599. left_margin -= thread__comm_len(self->thread);
  600. }
  601. return hist_entry_callchain__fprintf(fp, self, session_total,
  602. left_margin);
  603. }
  604. size_t hists__fprintf(struct hists *self, struct hists *pair,
  605. bool show_displacement, FILE *fp)
  606. {
  607. struct sort_entry *se;
  608. struct rb_node *nd;
  609. size_t ret = 0;
  610. unsigned long position = 1;
  611. long displacement = 0;
  612. unsigned int width;
  613. const char *sep = symbol_conf.field_sep;
  614. const char *col_width = symbol_conf.col_width_list_str;
  615. init_rem_hits();
  616. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  617. if (symbol_conf.show_nr_samples) {
  618. if (sep)
  619. fprintf(fp, "%cSamples", *sep);
  620. else
  621. fputs(" Samples ", fp);
  622. }
  623. if (symbol_conf.show_cpu_utilization) {
  624. if (sep) {
  625. ret += fprintf(fp, "%csys", *sep);
  626. ret += fprintf(fp, "%cus", *sep);
  627. if (perf_guest) {
  628. ret += fprintf(fp, "%cguest sys", *sep);
  629. ret += fprintf(fp, "%cguest us", *sep);
  630. }
  631. } else {
  632. ret += fprintf(fp, " sys ");
  633. ret += fprintf(fp, " us ");
  634. if (perf_guest) {
  635. ret += fprintf(fp, " guest sys ");
  636. ret += fprintf(fp, " guest us ");
  637. }
  638. }
  639. }
  640. if (pair) {
  641. if (sep)
  642. ret += fprintf(fp, "%cDelta", *sep);
  643. else
  644. ret += fprintf(fp, " Delta ");
  645. if (show_displacement) {
  646. if (sep)
  647. ret += fprintf(fp, "%cDisplacement", *sep);
  648. else
  649. ret += fprintf(fp, " Displ");
  650. }
  651. }
  652. list_for_each_entry(se, &hist_entry__sort_list, list) {
  653. if (se->elide)
  654. continue;
  655. if (sep) {
  656. fprintf(fp, "%c%s", *sep, se->se_header);
  657. continue;
  658. }
  659. width = strlen(se->se_header);
  660. if (symbol_conf.col_width_list_str) {
  661. if (col_width) {
  662. hists__set_col_len(self, se->se_width_idx,
  663. atoi(col_width));
  664. col_width = strchr(col_width, ',');
  665. if (col_width)
  666. ++col_width;
  667. }
  668. }
  669. if (!hists__new_col_len(self, se->se_width_idx, width))
  670. width = hists__col_len(self, se->se_width_idx);
  671. fprintf(fp, " %*s", width, se->se_header);
  672. }
  673. fprintf(fp, "\n");
  674. if (sep)
  675. goto print_entries;
  676. fprintf(fp, "# ........");
  677. if (symbol_conf.show_nr_samples)
  678. fprintf(fp, " ..........");
  679. if (pair) {
  680. fprintf(fp, " ..........");
  681. if (show_displacement)
  682. fprintf(fp, " .....");
  683. }
  684. list_for_each_entry(se, &hist_entry__sort_list, list) {
  685. unsigned int i;
  686. if (se->elide)
  687. continue;
  688. fprintf(fp, " ");
  689. width = hists__col_len(self, se->se_width_idx);
  690. if (width == 0)
  691. width = strlen(se->se_header);
  692. for (i = 0; i < width; i++)
  693. fprintf(fp, ".");
  694. }
  695. fprintf(fp, "\n#\n");
  696. print_entries:
  697. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  698. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  699. if (show_displacement) {
  700. if (h->pair != NULL)
  701. displacement = ((long)h->pair->position -
  702. (long)position);
  703. else
  704. displacement = 0;
  705. ++position;
  706. }
  707. ret += hist_entry__fprintf(h, self, pair, show_displacement,
  708. displacement, fp, self->stats.total_period);
  709. if (symbol_conf.use_callchain)
  710. ret += hist_entry__fprintf_callchain(h, self, fp,
  711. self->stats.total_period);
  712. if (h->ms.map == NULL && verbose > 1) {
  713. __map_groups__fprintf_maps(&h->thread->mg,
  714. MAP__FUNCTION, verbose, fp);
  715. fprintf(fp, "%.10s end\n", graph_dotted_line);
  716. }
  717. }
  718. free(rem_sq_bracket);
  719. return ret;
  720. }
  721. /*
  722. * See hists__fprintf to match the column widths
  723. */
  724. unsigned int hists__sort_list_width(struct hists *self)
  725. {
  726. struct sort_entry *se;
  727. int ret = 9; /* total % */
  728. if (symbol_conf.show_cpu_utilization) {
  729. ret += 7; /* count_sys % */
  730. ret += 6; /* count_us % */
  731. if (perf_guest) {
  732. ret += 13; /* count_guest_sys % */
  733. ret += 12; /* count_guest_us % */
  734. }
  735. }
  736. if (symbol_conf.show_nr_samples)
  737. ret += 11;
  738. list_for_each_entry(se, &hist_entry__sort_list, list)
  739. if (!se->elide)
  740. ret += 2 + hists__col_len(self, se->se_width_idx);
  741. if (verbose) /* Addr + origin */
  742. ret += 3 + BITS_PER_LONG / 4;
  743. return ret;
  744. }
  745. static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
  746. enum hist_filter filter)
  747. {
  748. h->filtered &= ~(1 << filter);
  749. if (h->filtered)
  750. return;
  751. ++self->nr_entries;
  752. if (h->ms.unfolded)
  753. self->nr_entries += h->nr_rows;
  754. h->row_offset = 0;
  755. self->stats.total_period += h->period;
  756. self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  757. hists__calc_col_len(self, h);
  758. }
  759. void hists__filter_by_dso(struct hists *self, const struct dso *dso)
  760. {
  761. struct rb_node *nd;
  762. self->nr_entries = self->stats.total_period = 0;
  763. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  764. hists__reset_col_len(self);
  765. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  766. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  767. if (symbol_conf.exclude_other && !h->parent)
  768. continue;
  769. if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
  770. h->filtered |= (1 << HIST_FILTER__DSO);
  771. continue;
  772. }
  773. hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
  774. }
  775. }
  776. void hists__filter_by_thread(struct hists *self, const struct thread *thread)
  777. {
  778. struct rb_node *nd;
  779. self->nr_entries = self->stats.total_period = 0;
  780. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  781. hists__reset_col_len(self);
  782. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  783. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  784. if (thread != NULL && h->thread != thread) {
  785. h->filtered |= (1 << HIST_FILTER__THREAD);
  786. continue;
  787. }
  788. hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
  789. }
  790. }
  791. static int symbol__alloc_hist(struct symbol *self)
  792. {
  793. struct sym_priv *priv = symbol__priv(self);
  794. const int size = (sizeof(*priv->hist) +
  795. (self->end - self->start) * sizeof(u64));
  796. priv->hist = zalloc(size);
  797. return priv->hist == NULL ? -1 : 0;
  798. }
  799. int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
  800. {
  801. unsigned int sym_size, offset;
  802. struct symbol *sym = self->ms.sym;
  803. struct sym_priv *priv;
  804. struct sym_hist *h;
  805. if (!sym || !self->ms.map)
  806. return 0;
  807. priv = symbol__priv(sym);
  808. if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
  809. return -ENOMEM;
  810. sym_size = sym->end - sym->start;
  811. offset = ip - sym->start;
  812. pr_debug3("%s: ip=%#" PRIx64 "\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
  813. if (offset >= sym_size)
  814. return 0;
  815. h = priv->hist;
  816. h->sum++;
  817. h->ip[offset]++;
  818. pr_debug3("%#" PRIx64 " %s: period++ [ip: %#" PRIx64 ", %#" PRIx64
  819. "] => %" PRIu64 "\n", self->ms.sym->start, self->ms.sym->name,
  820. ip, ip - self->ms.sym->start, h->ip[offset]);
  821. return 0;
  822. }
  823. static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
  824. {
  825. struct objdump_line *self = malloc(sizeof(*self) + privsize);
  826. if (self != NULL) {
  827. self->offset = offset;
  828. self->line = line;
  829. }
  830. return self;
  831. }
  832. void objdump_line__free(struct objdump_line *self)
  833. {
  834. free(self->line);
  835. free(self);
  836. }
  837. static void objdump__add_line(struct list_head *head, struct objdump_line *line)
  838. {
  839. list_add_tail(&line->node, head);
  840. }
  841. struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
  842. struct objdump_line *pos)
  843. {
  844. list_for_each_entry_continue(pos, head, node)
  845. if (pos->offset >= 0)
  846. return pos;
  847. return NULL;
  848. }
  849. static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file,
  850. struct list_head *head, size_t privsize)
  851. {
  852. struct symbol *sym = self->ms.sym;
  853. struct objdump_line *objdump_line;
  854. char *line = NULL, *tmp, *tmp2, *c;
  855. size_t line_len;
  856. s64 line_ip, offset = -1;
  857. if (getline(&line, &line_len, file) < 0)
  858. return -1;
  859. if (!line)
  860. return -1;
  861. while (line_len != 0 && isspace(line[line_len - 1]))
  862. line[--line_len] = '\0';
  863. c = strchr(line, '\n');
  864. if (c)
  865. *c = 0;
  866. line_ip = -1;
  867. /*
  868. * Strip leading spaces:
  869. */
  870. tmp = line;
  871. while (*tmp) {
  872. if (*tmp != ' ')
  873. break;
  874. tmp++;
  875. }
  876. if (*tmp) {
  877. /*
  878. * Parse hexa addresses followed by ':'
  879. */
  880. line_ip = strtoull(tmp, &tmp2, 16);
  881. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  882. line_ip = -1;
  883. }
  884. if (line_ip != -1) {
  885. u64 start = map__rip_2objdump(self->ms.map, sym->start),
  886. end = map__rip_2objdump(self->ms.map, sym->end);
  887. offset = line_ip - start;
  888. if (offset < 0 || (u64)line_ip > end)
  889. offset = -1;
  890. }
  891. objdump_line = objdump_line__new(offset, line, privsize);
  892. if (objdump_line == NULL) {
  893. free(line);
  894. return -1;
  895. }
  896. objdump__add_line(head, objdump_line);
  897. return 0;
  898. }
  899. int hist_entry__annotate(struct hist_entry *self, struct list_head *head,
  900. size_t privsize)
  901. {
  902. struct symbol *sym = self->ms.sym;
  903. struct map *map = self->ms.map;
  904. struct dso *dso = map->dso;
  905. char *filename = dso__build_id_filename(dso, NULL, 0);
  906. bool free_filename = true;
  907. char command[PATH_MAX * 2];
  908. FILE *file;
  909. int err = 0;
  910. u64 len;
  911. char symfs_filename[PATH_MAX];
  912. if (filename) {
  913. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  914. symbol_conf.symfs, filename);
  915. }
  916. if (filename == NULL) {
  917. if (dso->has_build_id) {
  918. pr_err("Can't annotate %s: not enough memory\n",
  919. sym->name);
  920. return -ENOMEM;
  921. }
  922. goto fallback;
  923. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  924. strstr(command, "[kernel.kallsyms]") ||
  925. access(symfs_filename, R_OK)) {
  926. free(filename);
  927. fallback:
  928. /*
  929. * If we don't have build-ids or the build-id file isn't in the
  930. * cache, or is just a kallsyms file, well, lets hope that this
  931. * DSO is the same as when 'perf record' ran.
  932. */
  933. filename = dso->long_name;
  934. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  935. symbol_conf.symfs, filename);
  936. free_filename = false;
  937. }
  938. if (dso->origin == DSO__ORIG_KERNEL) {
  939. if (dso->annotate_warned)
  940. goto out_free_filename;
  941. err = -ENOENT;
  942. dso->annotate_warned = 1;
  943. pr_err("Can't annotate %s: No vmlinux file was found in the "
  944. "path\n", sym->name);
  945. goto out_free_filename;
  946. }
  947. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  948. filename, sym->name, map->unmap_ip(map, sym->start),
  949. map->unmap_ip(map, sym->end));
  950. len = sym->end - sym->start;
  951. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  952. dso, dso->long_name, sym, sym->name);
  953. snprintf(command, sizeof(command),
  954. "objdump --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
  955. map__rip_2objdump(map, sym->start),
  956. map__rip_2objdump(map, sym->end),
  957. symfs_filename, filename);
  958. pr_debug("Executing: %s\n", command);
  959. file = popen(command, "r");
  960. if (!file)
  961. goto out_free_filename;
  962. while (!feof(file))
  963. if (hist_entry__parse_objdump_line(self, file, head, privsize) < 0)
  964. break;
  965. pclose(file);
  966. out_free_filename:
  967. if (free_filename)
  968. free(filename);
  969. return err;
  970. }
  971. void hists__inc_nr_events(struct hists *self, u32 type)
  972. {
  973. ++self->stats.nr_events[0];
  974. ++self->stats.nr_events[type];
  975. }
  976. size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
  977. {
  978. int i;
  979. size_t ret = 0;
  980. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  981. const char *name = perf_event__name(i);
  982. if (!strcmp(name, "UNKNOWN"))
  983. continue;
  984. ret += fprintf(fp, "%16s events: %10d\n", name,
  985. self->stats.nr_events[i]);
  986. }
  987. return ret;
  988. }