hist.c 28 KB

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