hist.c 27 KB

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