hist.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "hist.h"
  2. #include "session.h"
  3. #include "sort.h"
  4. #include <math.h>
  5. struct callchain_param callchain_param = {
  6. .mode = CHAIN_GRAPH_REL,
  7. .min_percent = 0.5
  8. };
  9. /*
  10. * histogram, sorted on item, collects counts
  11. */
  12. struct hist_entry *__perf_session__add_hist_entry(struct rb_root *hists,
  13. struct addr_location *al,
  14. struct symbol *sym_parent,
  15. u64 count, bool *hit)
  16. {
  17. struct rb_node **p = &hists->rb_node;
  18. struct rb_node *parent = NULL;
  19. struct hist_entry *he;
  20. struct hist_entry entry = {
  21. .thread = al->thread,
  22. .map = al->map,
  23. .sym = al->sym,
  24. .ip = al->addr,
  25. .level = al->level,
  26. .count = count,
  27. .parent = sym_parent,
  28. };
  29. int cmp;
  30. while (*p != NULL) {
  31. parent = *p;
  32. he = rb_entry(parent, struct hist_entry, rb_node);
  33. cmp = hist_entry__cmp(&entry, he);
  34. if (!cmp) {
  35. *hit = true;
  36. return he;
  37. }
  38. if (cmp < 0)
  39. p = &(*p)->rb_left;
  40. else
  41. p = &(*p)->rb_right;
  42. }
  43. he = malloc(sizeof(*he));
  44. if (!he)
  45. return NULL;
  46. *he = entry;
  47. rb_link_node(&he->rb_node, parent, p);
  48. rb_insert_color(&he->rb_node, hists);
  49. *hit = false;
  50. return he;
  51. }
  52. int64_t
  53. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  54. {
  55. struct sort_entry *se;
  56. int64_t cmp = 0;
  57. list_for_each_entry(se, &hist_entry__sort_list, list) {
  58. cmp = se->cmp(left, right);
  59. if (cmp)
  60. break;
  61. }
  62. return cmp;
  63. }
  64. int64_t
  65. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  66. {
  67. struct sort_entry *se;
  68. int64_t cmp = 0;
  69. list_for_each_entry(se, &hist_entry__sort_list, list) {
  70. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  71. f = se->collapse ?: se->cmp;
  72. cmp = f(left, right);
  73. if (cmp)
  74. break;
  75. }
  76. return cmp;
  77. }
  78. void hist_entry__free(struct hist_entry *he)
  79. {
  80. free(he);
  81. }
  82. /*
  83. * collapse the histogram
  84. */
  85. static void collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
  86. {
  87. struct rb_node **p = &root->rb_node;
  88. struct rb_node *parent = NULL;
  89. struct hist_entry *iter;
  90. int64_t cmp;
  91. while (*p != NULL) {
  92. parent = *p;
  93. iter = rb_entry(parent, struct hist_entry, rb_node);
  94. cmp = hist_entry__collapse(iter, he);
  95. if (!cmp) {
  96. iter->count += he->count;
  97. hist_entry__free(he);
  98. return;
  99. }
  100. if (cmp < 0)
  101. p = &(*p)->rb_left;
  102. else
  103. p = &(*p)->rb_right;
  104. }
  105. rb_link_node(&he->rb_node, parent, p);
  106. rb_insert_color(&he->rb_node, root);
  107. }
  108. void perf_session__collapse_resort(struct rb_root *hists)
  109. {
  110. struct rb_root tmp;
  111. struct rb_node *next;
  112. struct hist_entry *n;
  113. if (!sort__need_collapse)
  114. return;
  115. tmp = RB_ROOT;
  116. next = rb_first(hists);
  117. while (next) {
  118. n = rb_entry(next, struct hist_entry, rb_node);
  119. next = rb_next(&n->rb_node);
  120. rb_erase(&n->rb_node, hists);
  121. collapse__insert_entry(&tmp, n);
  122. }
  123. *hists = tmp;
  124. }
  125. /*
  126. * reverse the map, sort on count.
  127. */
  128. static void perf_session__insert_output_hist_entry(struct rb_root *root,
  129. struct hist_entry *he,
  130. u64 min_callchain_hits)
  131. {
  132. struct rb_node **p = &root->rb_node;
  133. struct rb_node *parent = NULL;
  134. struct hist_entry *iter;
  135. if (symbol_conf.use_callchain)
  136. callchain_param.sort(&he->sorted_chain, &he->callchain,
  137. min_callchain_hits, &callchain_param);
  138. while (*p != NULL) {
  139. parent = *p;
  140. iter = rb_entry(parent, struct hist_entry, rb_node);
  141. if (he->count > iter->count)
  142. p = &(*p)->rb_left;
  143. else
  144. p = &(*p)->rb_right;
  145. }
  146. rb_link_node(&he->rb_node, parent, p);
  147. rb_insert_color(&he->rb_node, root);
  148. }
  149. void perf_session__output_resort(struct rb_root *hists, u64 total_samples)
  150. {
  151. struct rb_root tmp;
  152. struct rb_node *next;
  153. struct hist_entry *n;
  154. u64 min_callchain_hits;
  155. min_callchain_hits =
  156. total_samples * (callchain_param.min_percent / 100);
  157. tmp = RB_ROOT;
  158. next = rb_first(hists);
  159. while (next) {
  160. n = rb_entry(next, struct hist_entry, rb_node);
  161. next = rb_next(&n->rb_node);
  162. rb_erase(&n->rb_node, hists);
  163. perf_session__insert_output_hist_entry(&tmp, n,
  164. min_callchain_hits);
  165. }
  166. *hists = tmp;
  167. }
  168. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  169. {
  170. int i;
  171. int ret = fprintf(fp, " ");
  172. for (i = 0; i < left_margin; i++)
  173. ret += fprintf(fp, " ");
  174. return ret;
  175. }
  176. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  177. int left_margin)
  178. {
  179. int i;
  180. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  181. for (i = 0; i < depth; i++)
  182. if (depth_mask & (1 << i))
  183. ret += fprintf(fp, "| ");
  184. else
  185. ret += fprintf(fp, " ");
  186. ret += fprintf(fp, "\n");
  187. return ret;
  188. }
  189. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  190. int depth, int depth_mask, int count,
  191. u64 total_samples, int hits,
  192. int left_margin)
  193. {
  194. int i;
  195. size_t ret = 0;
  196. ret += callchain__fprintf_left_margin(fp, left_margin);
  197. for (i = 0; i < depth; i++) {
  198. if (depth_mask & (1 << i))
  199. ret += fprintf(fp, "|");
  200. else
  201. ret += fprintf(fp, " ");
  202. if (!count && i == depth - 1) {
  203. double percent;
  204. percent = hits * 100.0 / total_samples;
  205. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  206. } else
  207. ret += fprintf(fp, "%s", " ");
  208. }
  209. if (chain->sym)
  210. ret += fprintf(fp, "%s\n", chain->sym->name);
  211. else
  212. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  213. return ret;
  214. }
  215. static struct symbol *rem_sq_bracket;
  216. static struct callchain_list rem_hits;
  217. static void init_rem_hits(void)
  218. {
  219. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  220. if (!rem_sq_bracket) {
  221. fprintf(stderr, "Not enough memory to display remaining hits\n");
  222. return;
  223. }
  224. strcpy(rem_sq_bracket->name, "[...]");
  225. rem_hits.sym = rem_sq_bracket;
  226. }
  227. static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  228. u64 total_samples, int depth,
  229. int depth_mask, int left_margin)
  230. {
  231. struct rb_node *node, *next;
  232. struct callchain_node *child;
  233. struct callchain_list *chain;
  234. int new_depth_mask = depth_mask;
  235. u64 new_total;
  236. u64 remaining;
  237. size_t ret = 0;
  238. int i;
  239. if (callchain_param.mode == CHAIN_GRAPH_REL)
  240. new_total = self->children_hit;
  241. else
  242. new_total = total_samples;
  243. remaining = new_total;
  244. node = rb_first(&self->rb_root);
  245. while (node) {
  246. u64 cumul;
  247. child = rb_entry(node, struct callchain_node, rb_node);
  248. cumul = cumul_hits(child);
  249. remaining -= cumul;
  250. /*
  251. * The depth mask manages the output of pipes that show
  252. * the depth. We don't want to keep the pipes of the current
  253. * level for the last child of this depth.
  254. * Except if we have remaining filtered hits. They will
  255. * supersede the last child
  256. */
  257. next = rb_next(node);
  258. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  259. new_depth_mask &= ~(1 << (depth - 1));
  260. /*
  261. * But we keep the older depth mask for the line separator
  262. * to keep the level link until we reach the last child
  263. */
  264. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  265. left_margin);
  266. i = 0;
  267. list_for_each_entry(chain, &child->val, list) {
  268. ret += ipchain__fprintf_graph(fp, chain, depth,
  269. new_depth_mask, i++,
  270. new_total,
  271. cumul,
  272. left_margin);
  273. }
  274. ret += __callchain__fprintf_graph(fp, child, new_total,
  275. depth + 1,
  276. new_depth_mask | (1 << depth),
  277. left_margin);
  278. node = next;
  279. }
  280. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  281. remaining && remaining != new_total) {
  282. if (!rem_sq_bracket)
  283. return ret;
  284. new_depth_mask &= ~(1 << (depth - 1));
  285. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  286. new_depth_mask, 0, new_total,
  287. remaining, left_margin);
  288. }
  289. return ret;
  290. }
  291. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  292. u64 total_samples, int left_margin)
  293. {
  294. struct callchain_list *chain;
  295. bool printed = false;
  296. int i = 0;
  297. int ret = 0;
  298. list_for_each_entry(chain, &self->val, list) {
  299. if (!i++ && sort__first_dimension == SORT_SYM)
  300. continue;
  301. if (!printed) {
  302. ret += callchain__fprintf_left_margin(fp, left_margin);
  303. ret += fprintf(fp, "|\n");
  304. ret += callchain__fprintf_left_margin(fp, left_margin);
  305. ret += fprintf(fp, "---");
  306. left_margin += 3;
  307. printed = true;
  308. } else
  309. ret += callchain__fprintf_left_margin(fp, left_margin);
  310. if (chain->sym)
  311. ret += fprintf(fp, " %s\n", chain->sym->name);
  312. else
  313. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  314. }
  315. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  316. return ret;
  317. }
  318. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  319. u64 total_samples)
  320. {
  321. struct callchain_list *chain;
  322. size_t ret = 0;
  323. if (!self)
  324. return 0;
  325. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  326. list_for_each_entry(chain, &self->val, list) {
  327. if (chain->ip >= PERF_CONTEXT_MAX)
  328. continue;
  329. if (chain->sym)
  330. ret += fprintf(fp, " %s\n", chain->sym->name);
  331. else
  332. ret += fprintf(fp, " %p\n",
  333. (void *)(long)chain->ip);
  334. }
  335. return ret;
  336. }
  337. static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  338. u64 total_samples, int left_margin)
  339. {
  340. struct rb_node *rb_node;
  341. struct callchain_node *chain;
  342. size_t ret = 0;
  343. rb_node = rb_first(&self->sorted_chain);
  344. while (rb_node) {
  345. double percent;
  346. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  347. percent = chain->hit * 100.0 / total_samples;
  348. switch (callchain_param.mode) {
  349. case CHAIN_FLAT:
  350. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  351. percent);
  352. ret += callchain__fprintf_flat(fp, chain, total_samples);
  353. break;
  354. case CHAIN_GRAPH_ABS: /* Falldown */
  355. case CHAIN_GRAPH_REL:
  356. ret += callchain__fprintf_graph(fp, chain, total_samples,
  357. left_margin);
  358. case CHAIN_NONE:
  359. default:
  360. break;
  361. }
  362. ret += fprintf(fp, "\n");
  363. rb_node = rb_next(rb_node);
  364. }
  365. return ret;
  366. }
  367. size_t hist_entry__fprintf(struct hist_entry *self,
  368. struct perf_session *pair_session,
  369. bool show_displacement,
  370. long displacement, FILE *fp,
  371. u64 session_total)
  372. {
  373. struct sort_entry *se;
  374. u64 count, total;
  375. const char *sep = symbol_conf.field_sep;
  376. size_t ret;
  377. if (symbol_conf.exclude_other && !self->parent)
  378. return 0;
  379. if (pair_session) {
  380. count = self->pair ? self->pair->count : 0;
  381. total = pair_session->events_stats.total;
  382. } else {
  383. count = self->count;
  384. total = session_total;
  385. }
  386. if (total)
  387. ret = percent_color_fprintf(fp, sep ? "%.2f" : " %6.2f%%",
  388. (count * 100.0) / total);
  389. else
  390. ret = fprintf(fp, sep ? "%lld" : "%12lld ", count);
  391. if (symbol_conf.show_nr_samples) {
  392. if (sep)
  393. ret += fprintf(fp, "%c%lld", *sep, count);
  394. else
  395. ret += fprintf(fp, "%11lld", count);
  396. }
  397. if (pair_session) {
  398. char bf[32];
  399. double old_percent = 0, new_percent = 0, diff;
  400. if (total > 0)
  401. old_percent = (count * 100.0) / total;
  402. if (session_total > 0)
  403. new_percent = (self->count * 100.0) / session_total;
  404. diff = new_percent - old_percent;
  405. if (fabs(diff) >= 0.01)
  406. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  407. else
  408. snprintf(bf, sizeof(bf), " ");
  409. if (sep)
  410. ret += fprintf(fp, "%c%s", *sep, bf);
  411. else
  412. ret += fprintf(fp, "%11.11s", bf);
  413. if (show_displacement) {
  414. if (displacement)
  415. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  416. else
  417. snprintf(bf, sizeof(bf), " ");
  418. if (sep)
  419. ret += fprintf(fp, "%c%s", *sep, bf);
  420. else
  421. ret += fprintf(fp, "%6.6s", bf);
  422. }
  423. }
  424. list_for_each_entry(se, &hist_entry__sort_list, list) {
  425. if (se->elide)
  426. continue;
  427. ret += fprintf(fp, "%s", sep ?: " ");
  428. ret += se->print(fp, self, se->width ? *se->width : 0);
  429. }
  430. return ret + fprintf(fp, "\n");
  431. }
  432. static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
  433. u64 session_total)
  434. {
  435. int left_margin = 0;
  436. if (sort__first_dimension == SORT_COMM) {
  437. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  438. typeof(*se), list);
  439. left_margin = se->width ? *se->width : 0;
  440. left_margin -= thread__comm_len(self->thread);
  441. }
  442. return hist_entry_callchain__fprintf(fp, self, session_total,
  443. left_margin);
  444. }
  445. size_t perf_session__fprintf_hists(struct rb_root *hists,
  446. struct perf_session *pair,
  447. bool show_displacement, FILE *fp,
  448. u64 session_total)
  449. {
  450. struct sort_entry *se;
  451. struct rb_node *nd;
  452. size_t ret = 0;
  453. unsigned long position = 1;
  454. long displacement = 0;
  455. unsigned int width;
  456. const char *sep = symbol_conf.field_sep;
  457. char *col_width = symbol_conf.col_width_list_str;
  458. init_rem_hits();
  459. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  460. if (symbol_conf.show_nr_samples) {
  461. if (sep)
  462. fprintf(fp, "%cSamples", *sep);
  463. else
  464. fputs(" Samples ", fp);
  465. }
  466. if (pair) {
  467. if (sep)
  468. ret += fprintf(fp, "%cDelta", *sep);
  469. else
  470. ret += fprintf(fp, " Delta ");
  471. if (show_displacement) {
  472. if (sep)
  473. ret += fprintf(fp, "%cDisplacement", *sep);
  474. else
  475. ret += fprintf(fp, " Displ");
  476. }
  477. }
  478. list_for_each_entry(se, &hist_entry__sort_list, list) {
  479. if (se->elide)
  480. continue;
  481. if (sep) {
  482. fprintf(fp, "%c%s", *sep, se->header);
  483. continue;
  484. }
  485. width = strlen(se->header);
  486. if (se->width) {
  487. if (symbol_conf.col_width_list_str) {
  488. if (col_width) {
  489. *se->width = atoi(col_width);
  490. col_width = strchr(col_width, ',');
  491. if (col_width)
  492. ++col_width;
  493. }
  494. }
  495. width = *se->width = max(*se->width, width);
  496. }
  497. fprintf(fp, " %*s", width, se->header);
  498. }
  499. fprintf(fp, "\n");
  500. if (sep)
  501. goto print_entries;
  502. fprintf(fp, "# ........");
  503. if (symbol_conf.show_nr_samples)
  504. fprintf(fp, " ..........");
  505. if (pair) {
  506. fprintf(fp, " ..........");
  507. if (show_displacement)
  508. fprintf(fp, " .....");
  509. }
  510. list_for_each_entry(se, &hist_entry__sort_list, list) {
  511. unsigned int i;
  512. if (se->elide)
  513. continue;
  514. fprintf(fp, " ");
  515. if (se->width)
  516. width = *se->width;
  517. else
  518. width = strlen(se->header);
  519. for (i = 0; i < width; i++)
  520. fprintf(fp, ".");
  521. }
  522. fprintf(fp, "\n#\n");
  523. print_entries:
  524. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  525. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  526. if (show_displacement) {
  527. if (h->pair != NULL)
  528. displacement = ((long)h->pair->position -
  529. (long)position);
  530. else
  531. displacement = 0;
  532. ++position;
  533. }
  534. ret += hist_entry__fprintf(h, pair, show_displacement,
  535. displacement, fp, session_total);
  536. if (symbol_conf.use_callchain)
  537. ret += hist_entry__fprintf_callchain(h, fp, session_total);
  538. if (h->map == NULL && verbose > 1) {
  539. __map_groups__fprintf_maps(&h->thread->mg,
  540. MAP__FUNCTION, fp);
  541. fprintf(fp, "%.10s end\n", graph_dotted_line);
  542. }
  543. }
  544. free(rem_sq_bracket);
  545. return ret;
  546. }