hist.c 26 KB

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