hist.c 27 KB

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