hist.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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 *self,
  66. unsigned int cpumode, u64 period)
  67. {
  68. switch (cpumode) {
  69. case PERF_RECORD_MISC_KERNEL:
  70. self->period_sys += period;
  71. break;
  72. case PERF_RECORD_MISC_USER:
  73. self->period_us += period;
  74. break;
  75. case PERF_RECORD_MISC_GUEST_KERNEL:
  76. self->period_guest_sys += period;
  77. break;
  78. case PERF_RECORD_MISC_GUEST_USER:
  79. self->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. if (he->period == 0)
  93. return true;
  94. hists->stats.total_period -= he->period;
  95. hist_entry__decay(he);
  96. hists->stats.total_period += he->period;
  97. return he->period == 0;
  98. }
  99. static void __hists__decay_entries(struct hists *hists, bool zap_user,
  100. bool zap_kernel, bool threaded)
  101. {
  102. struct rb_node *next = rb_first(&hists->entries);
  103. struct hist_entry *n;
  104. while (next) {
  105. n = rb_entry(next, struct hist_entry, rb_node);
  106. next = rb_next(&n->rb_node);
  107. /*
  108. * We may be annotating this, for instance, so keep it here in
  109. * case some it gets new samples, we'll eventually free it when
  110. * the user stops browsing and it agains gets fully decayed.
  111. */
  112. if (((zap_user && n->level == '.') ||
  113. (zap_kernel && n->level != '.') ||
  114. hists__decay_entry(hists, n)) &&
  115. !n->used) {
  116. rb_erase(&n->rb_node, &hists->entries);
  117. if (sort__need_collapse || threaded)
  118. rb_erase(&n->rb_node_in, &hists->entries_collapsed);
  119. hist_entry__free(n);
  120. --hists->nr_entries;
  121. }
  122. }
  123. }
  124. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  125. {
  126. return __hists__decay_entries(hists, zap_user, zap_kernel, false);
  127. }
  128. void hists__decay_entries_threaded(struct hists *hists,
  129. bool zap_user, bool zap_kernel)
  130. {
  131. return __hists__decay_entries(hists, zap_user, zap_kernel, true);
  132. }
  133. /*
  134. * histogram, sorted on item, collects periods
  135. */
  136. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  137. {
  138. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  139. struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
  140. if (self != NULL) {
  141. *self = *template;
  142. self->nr_events = 1;
  143. if (self->ms.map)
  144. self->ms.map->referenced = true;
  145. if (symbol_conf.use_callchain)
  146. callchain_init(self->callchain);
  147. }
  148. return self;
  149. }
  150. static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  151. {
  152. if (!h->filtered) {
  153. hists__calc_col_len(hists, h);
  154. ++hists->nr_entries;
  155. hists->stats.total_period += h->period;
  156. }
  157. }
  158. static u8 symbol__parent_filter(const struct symbol *parent)
  159. {
  160. if (symbol_conf.exclude_other && parent == NULL)
  161. return 1 << HIST_FILTER__PARENT;
  162. return 0;
  163. }
  164. struct hist_entry *__hists__add_entry(struct hists *hists,
  165. struct addr_location *al,
  166. struct symbol *sym_parent, u64 period)
  167. {
  168. struct rb_node **p;
  169. struct rb_node *parent = NULL;
  170. struct hist_entry *he;
  171. struct hist_entry entry = {
  172. .thread = al->thread,
  173. .ms = {
  174. .map = al->map,
  175. .sym = al->sym,
  176. },
  177. .cpu = al->cpu,
  178. .ip = al->addr,
  179. .level = al->level,
  180. .period = period,
  181. .parent = sym_parent,
  182. .filtered = symbol__parent_filter(sym_parent),
  183. };
  184. int cmp;
  185. pthread_mutex_lock(&hists->lock);
  186. p = &hists->entries_in->rb_node;
  187. while (*p != NULL) {
  188. parent = *p;
  189. he = rb_entry(parent, struct hist_entry, rb_node_in);
  190. cmp = hist_entry__cmp(&entry, he);
  191. if (!cmp) {
  192. he->period += period;
  193. ++he->nr_events;
  194. goto out;
  195. }
  196. if (cmp < 0)
  197. p = &(*p)->rb_left;
  198. else
  199. p = &(*p)->rb_right;
  200. }
  201. he = hist_entry__new(&entry);
  202. if (!he)
  203. goto out_unlock;
  204. rb_link_node(&he->rb_node_in, parent, p);
  205. rb_insert_color(&he->rb_node_in, hists->entries_in);
  206. out:
  207. hist_entry__add_cpumode_period(he, al->cpumode, period);
  208. out_unlock:
  209. pthread_mutex_unlock(&hists->lock);
  210. return he;
  211. }
  212. int64_t
  213. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  214. {
  215. struct sort_entry *se;
  216. int64_t cmp = 0;
  217. list_for_each_entry(se, &hist_entry__sort_list, list) {
  218. cmp = se->se_cmp(left, right);
  219. if (cmp)
  220. break;
  221. }
  222. return cmp;
  223. }
  224. int64_t
  225. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  226. {
  227. struct sort_entry *se;
  228. int64_t cmp = 0;
  229. list_for_each_entry(se, &hist_entry__sort_list, list) {
  230. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  231. f = se->se_collapse ?: se->se_cmp;
  232. cmp = f(left, right);
  233. if (cmp)
  234. break;
  235. }
  236. return cmp;
  237. }
  238. void hist_entry__free(struct hist_entry *he)
  239. {
  240. free(he);
  241. }
  242. /*
  243. * collapse the histogram
  244. */
  245. static bool hists__collapse_insert_entry(struct hists *hists,
  246. struct rb_root *root,
  247. struct hist_entry *he)
  248. {
  249. struct rb_node **p = &root->rb_node;
  250. struct rb_node *parent = NULL;
  251. struct hist_entry *iter;
  252. int64_t cmp;
  253. while (*p != NULL) {
  254. parent = *p;
  255. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  256. cmp = hist_entry__collapse(iter, he);
  257. if (!cmp) {
  258. iter->period += he->period;
  259. iter->nr_events += he->nr_events;
  260. if (symbol_conf.use_callchain) {
  261. callchain_cursor_reset(&hists->callchain_cursor);
  262. callchain_merge(&hists->callchain_cursor, iter->callchain,
  263. he->callchain);
  264. }
  265. hist_entry__free(he);
  266. return false;
  267. }
  268. if (cmp < 0)
  269. p = &(*p)->rb_left;
  270. else
  271. p = &(*p)->rb_right;
  272. }
  273. rb_link_node(&he->rb_node_in, parent, p);
  274. rb_insert_color(&he->rb_node_in, root);
  275. return true;
  276. }
  277. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  278. {
  279. struct rb_root *root;
  280. pthread_mutex_lock(&hists->lock);
  281. root = hists->entries_in;
  282. if (++hists->entries_in > &hists->entries_in_array[1])
  283. hists->entries_in = &hists->entries_in_array[0];
  284. pthread_mutex_unlock(&hists->lock);
  285. return root;
  286. }
  287. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  288. {
  289. hists__filter_entry_by_dso(hists, he);
  290. hists__filter_entry_by_thread(hists, he);
  291. }
  292. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  293. {
  294. struct rb_root *root;
  295. struct rb_node *next;
  296. struct hist_entry *n;
  297. if (!sort__need_collapse && !threaded)
  298. return;
  299. root = hists__get_rotate_entries_in(hists);
  300. next = rb_first(root);
  301. hists->stats.total_period = 0;
  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. hists__inc_nr_entries(hists, n);
  314. }
  315. }
  316. }
  317. void hists__collapse_resort(struct hists *hists)
  318. {
  319. return __hists__collapse_resort(hists, false);
  320. }
  321. void hists__collapse_resort_threaded(struct hists *hists)
  322. {
  323. return __hists__collapse_resort(hists, true);
  324. }
  325. /*
  326. * reverse the map, sort on period.
  327. */
  328. static void __hists__insert_output_entry(struct rb_root *entries,
  329. struct hist_entry *he,
  330. u64 min_callchain_hits)
  331. {
  332. struct rb_node **p = &entries->rb_node;
  333. struct rb_node *parent = NULL;
  334. struct hist_entry *iter;
  335. if (symbol_conf.use_callchain)
  336. callchain_param.sort(&he->sorted_chain, he->callchain,
  337. min_callchain_hits, &callchain_param);
  338. while (*p != NULL) {
  339. parent = *p;
  340. iter = rb_entry(parent, struct hist_entry, rb_node);
  341. if (he->period > iter->period)
  342. p = &(*p)->rb_left;
  343. else
  344. p = &(*p)->rb_right;
  345. }
  346. rb_link_node(&he->rb_node, parent, p);
  347. rb_insert_color(&he->rb_node, entries);
  348. }
  349. static void __hists__output_resort(struct hists *hists, bool threaded)
  350. {
  351. struct rb_root *root;
  352. struct rb_node *next;
  353. struct hist_entry *n;
  354. u64 min_callchain_hits;
  355. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  356. if (sort__need_collapse || threaded)
  357. root = &hists->entries_collapsed;
  358. else
  359. root = hists->entries_in;
  360. next = rb_first(root);
  361. hists->entries = RB_ROOT;
  362. hists->nr_entries = 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(FILE *fp, struct hist_entry *self,
  555. u64 total_samples, int left_margin)
  556. {
  557. struct rb_node *rb_node;
  558. struct callchain_node *chain;
  559. size_t ret = 0;
  560. u32 entries_printed = 0;
  561. rb_node = rb_first(&self->sorted_chain);
  562. while (rb_node) {
  563. double percent;
  564. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  565. percent = chain->hit * 100.0 / total_samples;
  566. switch (callchain_param.mode) {
  567. case CHAIN_FLAT:
  568. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  569. percent);
  570. ret += callchain__fprintf_flat(fp, chain, total_samples);
  571. break;
  572. case CHAIN_GRAPH_ABS: /* Falldown */
  573. case CHAIN_GRAPH_REL:
  574. ret += callchain__fprintf_graph(fp, chain, total_samples,
  575. left_margin);
  576. case CHAIN_NONE:
  577. default:
  578. break;
  579. }
  580. ret += fprintf(fp, "\n");
  581. if (++entries_printed == callchain_param.print_limit)
  582. break;
  583. rb_node = rb_next(rb_node);
  584. }
  585. return ret;
  586. }
  587. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  588. {
  589. struct rb_node *next = rb_first(&hists->entries);
  590. struct hist_entry *n;
  591. int row = 0;
  592. hists__reset_col_len(hists);
  593. while (next && row++ < max_rows) {
  594. n = rb_entry(next, struct hist_entry, rb_node);
  595. hists__calc_col_len(hists, n);
  596. next = rb_next(&n->rb_node);
  597. }
  598. }
  599. static int hist_entry__pcnt_snprintf(struct hist_entry *self, char *s,
  600. size_t size, struct hists *pair_hists,
  601. bool show_displacement, long displacement,
  602. bool color, u64 session_total)
  603. {
  604. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  605. u64 nr_events;
  606. const char *sep = symbol_conf.field_sep;
  607. int ret;
  608. if (symbol_conf.exclude_other && !self->parent)
  609. return 0;
  610. if (pair_hists) {
  611. period = self->pair ? self->pair->period : 0;
  612. nr_events = self->pair ? self->pair->nr_events : 0;
  613. total = pair_hists->stats.total_period;
  614. period_sys = self->pair ? self->pair->period_sys : 0;
  615. period_us = self->pair ? self->pair->period_us : 0;
  616. period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
  617. period_guest_us = self->pair ? self->pair->period_guest_us : 0;
  618. } else {
  619. period = self->period;
  620. nr_events = self->nr_events;
  621. total = session_total;
  622. period_sys = self->period_sys;
  623. period_us = self->period_us;
  624. period_guest_sys = self->period_guest_sys;
  625. period_guest_us = self->period_guest_us;
  626. }
  627. if (total) {
  628. if (color)
  629. ret = percent_color_snprintf(s, size,
  630. sep ? "%.2f" : " %6.2f%%",
  631. (period * 100.0) / total);
  632. else
  633. ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  634. (period * 100.0) / total);
  635. if (symbol_conf.show_cpu_utilization) {
  636. ret += percent_color_snprintf(s + ret, size - ret,
  637. sep ? "%.2f" : " %6.2f%%",
  638. (period_sys * 100.0) / total);
  639. ret += percent_color_snprintf(s + ret, size - ret,
  640. sep ? "%.2f" : " %6.2f%%",
  641. (period_us * 100.0) / total);
  642. if (perf_guest) {
  643. ret += percent_color_snprintf(s + ret,
  644. size - ret,
  645. sep ? "%.2f" : " %6.2f%%",
  646. (period_guest_sys * 100.0) /
  647. total);
  648. ret += percent_color_snprintf(s + ret,
  649. size - ret,
  650. sep ? "%.2f" : " %6.2f%%",
  651. (period_guest_us * 100.0) /
  652. total);
  653. }
  654. }
  655. } else
  656. ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  657. if (symbol_conf.show_nr_samples) {
  658. if (sep)
  659. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  660. else
  661. ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  662. }
  663. if (symbol_conf.show_total_period) {
  664. if (sep)
  665. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  666. else
  667. ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
  668. }
  669. if (pair_hists) {
  670. char bf[32];
  671. double old_percent = 0, new_percent = 0, diff;
  672. if (total > 0)
  673. old_percent = (period * 100.0) / total;
  674. if (session_total > 0)
  675. new_percent = (self->period * 100.0) / session_total;
  676. diff = new_percent - old_percent;
  677. if (fabs(diff) >= 0.01)
  678. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  679. else
  680. snprintf(bf, sizeof(bf), " ");
  681. if (sep)
  682. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  683. else
  684. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  685. if (show_displacement) {
  686. if (displacement)
  687. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  688. else
  689. snprintf(bf, sizeof(bf), " ");
  690. if (sep)
  691. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  692. else
  693. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  694. }
  695. }
  696. return ret;
  697. }
  698. int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
  699. struct hists *hists)
  700. {
  701. const char *sep = symbol_conf.field_sep;
  702. struct sort_entry *se;
  703. int ret = 0;
  704. list_for_each_entry(se, &hist_entry__sort_list, list) {
  705. if (se->elide)
  706. continue;
  707. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  708. ret += se->se_snprintf(he, s + ret, size - ret,
  709. hists__col_len(hists, se->se_width_idx));
  710. }
  711. return ret;
  712. }
  713. int hist_entry__fprintf(struct hist_entry *he, size_t size, struct hists *hists,
  714. struct hists *pair_hists, bool show_displacement,
  715. long displacement, FILE *fp, u64 session_total)
  716. {
  717. char bf[512];
  718. int ret;
  719. if (size == 0 || size > sizeof(bf))
  720. size = sizeof(bf);
  721. ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
  722. show_displacement, displacement,
  723. true, session_total);
  724. hist_entry__snprintf(he, bf + ret, size - ret, hists);
  725. return fprintf(fp, "%s\n", bf);
  726. }
  727. static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
  728. struct hists *hists, FILE *fp,
  729. u64 session_total)
  730. {
  731. int left_margin = 0;
  732. if (sort__first_dimension == SORT_COMM) {
  733. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  734. typeof(*se), list);
  735. left_margin = hists__col_len(hists, se->se_width_idx);
  736. left_margin -= thread__comm_len(self->thread);
  737. }
  738. return hist_entry_callchain__fprintf(fp, self, session_total,
  739. left_margin);
  740. }
  741. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  742. bool show_displacement, bool show_header, int max_rows,
  743. int max_cols, FILE *fp)
  744. {
  745. struct sort_entry *se;
  746. struct rb_node *nd;
  747. size_t ret = 0;
  748. unsigned long position = 1;
  749. long displacement = 0;
  750. unsigned int width;
  751. const char *sep = symbol_conf.field_sep;
  752. const char *col_width = symbol_conf.col_width_list_str;
  753. int nr_rows = 0;
  754. init_rem_hits();
  755. if (!show_header)
  756. goto print_entries;
  757. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  758. if (symbol_conf.show_nr_samples) {
  759. if (sep)
  760. fprintf(fp, "%cSamples", *sep);
  761. else
  762. fputs(" Samples ", fp);
  763. }
  764. if (symbol_conf.show_total_period) {
  765. if (sep)
  766. ret += fprintf(fp, "%cPeriod", *sep);
  767. else
  768. ret += fprintf(fp, " Period ");
  769. }
  770. if (symbol_conf.show_cpu_utilization) {
  771. if (sep) {
  772. ret += fprintf(fp, "%csys", *sep);
  773. ret += fprintf(fp, "%cus", *sep);
  774. if (perf_guest) {
  775. ret += fprintf(fp, "%cguest sys", *sep);
  776. ret += fprintf(fp, "%cguest us", *sep);
  777. }
  778. } else {
  779. ret += fprintf(fp, " sys ");
  780. ret += fprintf(fp, " us ");
  781. if (perf_guest) {
  782. ret += fprintf(fp, " guest sys ");
  783. ret += fprintf(fp, " guest us ");
  784. }
  785. }
  786. }
  787. if (pair) {
  788. if (sep)
  789. ret += fprintf(fp, "%cDelta", *sep);
  790. else
  791. ret += fprintf(fp, " Delta ");
  792. if (show_displacement) {
  793. if (sep)
  794. ret += fprintf(fp, "%cDisplacement", *sep);
  795. else
  796. ret += fprintf(fp, " Displ");
  797. }
  798. }
  799. list_for_each_entry(se, &hist_entry__sort_list, list) {
  800. if (se->elide)
  801. continue;
  802. if (sep) {
  803. fprintf(fp, "%c%s", *sep, se->se_header);
  804. continue;
  805. }
  806. width = strlen(se->se_header);
  807. if (symbol_conf.col_width_list_str) {
  808. if (col_width) {
  809. hists__set_col_len(hists, se->se_width_idx,
  810. atoi(col_width));
  811. col_width = strchr(col_width, ',');
  812. if (col_width)
  813. ++col_width;
  814. }
  815. }
  816. if (!hists__new_col_len(hists, se->se_width_idx, width))
  817. width = hists__col_len(hists, se->se_width_idx);
  818. fprintf(fp, " %*s", width, se->se_header);
  819. }
  820. fprintf(fp, "\n");
  821. if (max_rows && ++nr_rows >= max_rows)
  822. goto out;
  823. if (sep)
  824. goto print_entries;
  825. fprintf(fp, "# ........");
  826. if (symbol_conf.show_nr_samples)
  827. fprintf(fp, " ..........");
  828. if (symbol_conf.show_total_period)
  829. fprintf(fp, " ............");
  830. if (pair) {
  831. fprintf(fp, " ..........");
  832. if (show_displacement)
  833. fprintf(fp, " .....");
  834. }
  835. list_for_each_entry(se, &hist_entry__sort_list, list) {
  836. unsigned int i;
  837. if (se->elide)
  838. continue;
  839. fprintf(fp, " ");
  840. width = hists__col_len(hists, se->se_width_idx);
  841. if (width == 0)
  842. width = strlen(se->se_header);
  843. for (i = 0; i < width; i++)
  844. fprintf(fp, ".");
  845. }
  846. fprintf(fp, "\n");
  847. if (max_rows && ++nr_rows >= max_rows)
  848. goto out;
  849. fprintf(fp, "#\n");
  850. if (max_rows && ++nr_rows >= max_rows)
  851. goto out;
  852. print_entries:
  853. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  854. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  855. if (h->filtered)
  856. continue;
  857. if (show_displacement) {
  858. if (h->pair != NULL)
  859. displacement = ((long)h->pair->position -
  860. (long)position);
  861. else
  862. displacement = 0;
  863. ++position;
  864. }
  865. ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
  866. displacement, fp, hists->stats.total_period);
  867. if (symbol_conf.use_callchain)
  868. ret += hist_entry__fprintf_callchain(h, hists, fp,
  869. hists->stats.total_period);
  870. if (max_rows && ++nr_rows >= max_rows)
  871. goto out;
  872. if (h->ms.map == NULL && verbose > 1) {
  873. __map_groups__fprintf_maps(&h->thread->mg,
  874. MAP__FUNCTION, verbose, fp);
  875. fprintf(fp, "%.10s end\n", graph_dotted_line);
  876. }
  877. }
  878. out:
  879. free(rem_sq_bracket);
  880. return ret;
  881. }
  882. /*
  883. * See hists__fprintf to match the column widths
  884. */
  885. unsigned int hists__sort_list_width(struct hists *hists)
  886. {
  887. struct sort_entry *se;
  888. int ret = 9; /* total % */
  889. if (symbol_conf.show_cpu_utilization) {
  890. ret += 7; /* count_sys % */
  891. ret += 6; /* count_us % */
  892. if (perf_guest) {
  893. ret += 13; /* count_guest_sys % */
  894. ret += 12; /* count_guest_us % */
  895. }
  896. }
  897. if (symbol_conf.show_nr_samples)
  898. ret += 11;
  899. if (symbol_conf.show_total_period)
  900. ret += 13;
  901. list_for_each_entry(se, &hist_entry__sort_list, list)
  902. if (!se->elide)
  903. ret += 2 + hists__col_len(hists, se->se_width_idx);
  904. if (verbose) /* Addr + origin */
  905. ret += 3 + BITS_PER_LONG / 4;
  906. return ret;
  907. }
  908. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  909. enum hist_filter filter)
  910. {
  911. h->filtered &= ~(1 << filter);
  912. if (h->filtered)
  913. return;
  914. ++hists->nr_entries;
  915. if (h->ms.unfolded)
  916. hists->nr_entries += h->nr_rows;
  917. h->row_offset = 0;
  918. hists->stats.total_period += h->period;
  919. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  920. hists__calc_col_len(hists, h);
  921. }
  922. static bool hists__filter_entry_by_dso(struct hists *hists,
  923. struct hist_entry *he)
  924. {
  925. if (hists->dso_filter != NULL &&
  926. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  927. he->filtered |= (1 << HIST_FILTER__DSO);
  928. return true;
  929. }
  930. return false;
  931. }
  932. void hists__filter_by_dso(struct hists *hists)
  933. {
  934. struct rb_node *nd;
  935. hists->nr_entries = hists->stats.total_period = 0;
  936. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  937. hists__reset_col_len(hists);
  938. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  939. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  940. if (symbol_conf.exclude_other && !h->parent)
  941. continue;
  942. if (hists__filter_entry_by_dso(hists, h))
  943. continue;
  944. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  945. }
  946. }
  947. static bool hists__filter_entry_by_thread(struct hists *hists,
  948. struct hist_entry *he)
  949. {
  950. if (hists->thread_filter != NULL &&
  951. he->thread != hists->thread_filter) {
  952. he->filtered |= (1 << HIST_FILTER__THREAD);
  953. return true;
  954. }
  955. return false;
  956. }
  957. void hists__filter_by_thread(struct hists *hists)
  958. {
  959. struct rb_node *nd;
  960. hists->nr_entries = hists->stats.total_period = 0;
  961. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  962. hists__reset_col_len(hists);
  963. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  964. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  965. if (hists__filter_entry_by_thread(hists, h))
  966. continue;
  967. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  968. }
  969. }
  970. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  971. {
  972. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  973. }
  974. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  975. {
  976. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  977. }
  978. void hists__inc_nr_events(struct hists *hists, u32 type)
  979. {
  980. ++hists->stats.nr_events[0];
  981. ++hists->stats.nr_events[type];
  982. }
  983. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  984. {
  985. int i;
  986. size_t ret = 0;
  987. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  988. const char *name;
  989. if (hists->stats.nr_events[i] == 0)
  990. continue;
  991. name = perf_event__name(i);
  992. if (!strcmp(name, "UNKNOWN"))
  993. continue;
  994. ret += fprintf(fp, "%16s events: %10d\n", name,
  995. hists->stats.nr_events[i]);
  996. }
  997. return ret;
  998. }
  999. void hists__init(struct hists *hists)
  1000. {
  1001. memset(hists, 0, sizeof(*hists));
  1002. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  1003. hists->entries_in = &hists->entries_in_array[0];
  1004. hists->entries_collapsed = RB_ROOT;
  1005. hists->entries = RB_ROOT;
  1006. pthread_mutex_init(&hists->lock, NULL);
  1007. }