hist.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. if (chain->ip >= PERF_CONTEXT_MAX)
  269. continue;
  270. ret += ipchain__fprintf_graph(fp, chain, depth,
  271. new_depth_mask, i++,
  272. new_total,
  273. cumul,
  274. left_margin);
  275. }
  276. ret += __callchain__fprintf_graph(fp, child, new_total,
  277. depth + 1,
  278. new_depth_mask | (1 << depth),
  279. left_margin);
  280. node = next;
  281. }
  282. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  283. remaining && remaining != new_total) {
  284. if (!rem_sq_bracket)
  285. return ret;
  286. new_depth_mask &= ~(1 << (depth - 1));
  287. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  288. new_depth_mask, 0, new_total,
  289. remaining, left_margin);
  290. }
  291. return ret;
  292. }
  293. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  294. u64 total_samples, int left_margin)
  295. {
  296. struct callchain_list *chain;
  297. bool printed = false;
  298. int i = 0;
  299. int ret = 0;
  300. list_for_each_entry(chain, &self->val, list) {
  301. if (chain->ip >= PERF_CONTEXT_MAX)
  302. continue;
  303. if (!i++ && sort__first_dimension == SORT_SYM)
  304. continue;
  305. if (!printed) {
  306. ret += callchain__fprintf_left_margin(fp, left_margin);
  307. ret += fprintf(fp, "|\n");
  308. ret += callchain__fprintf_left_margin(fp, left_margin);
  309. ret += fprintf(fp, "---");
  310. left_margin += 3;
  311. printed = true;
  312. } else
  313. ret += callchain__fprintf_left_margin(fp, left_margin);
  314. if (chain->sym)
  315. ret += fprintf(fp, " %s\n", chain->sym->name);
  316. else
  317. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  318. }
  319. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  320. return ret;
  321. }
  322. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  323. u64 total_samples)
  324. {
  325. struct callchain_list *chain;
  326. size_t ret = 0;
  327. if (!self)
  328. return 0;
  329. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  330. list_for_each_entry(chain, &self->val, list) {
  331. if (chain->ip >= PERF_CONTEXT_MAX)
  332. continue;
  333. if (chain->sym)
  334. ret += fprintf(fp, " %s\n", chain->sym->name);
  335. else
  336. ret += fprintf(fp, " %p\n",
  337. (void *)(long)chain->ip);
  338. }
  339. return ret;
  340. }
  341. static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  342. u64 total_samples, int left_margin)
  343. {
  344. struct rb_node *rb_node;
  345. struct callchain_node *chain;
  346. size_t ret = 0;
  347. rb_node = rb_first(&self->sorted_chain);
  348. while (rb_node) {
  349. double percent;
  350. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  351. percent = chain->hit * 100.0 / total_samples;
  352. switch (callchain_param.mode) {
  353. case CHAIN_FLAT:
  354. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  355. percent);
  356. ret += callchain__fprintf_flat(fp, chain, total_samples);
  357. break;
  358. case CHAIN_GRAPH_ABS: /* Falldown */
  359. case CHAIN_GRAPH_REL:
  360. ret += callchain__fprintf_graph(fp, chain, total_samples,
  361. left_margin);
  362. case CHAIN_NONE:
  363. default:
  364. break;
  365. }
  366. ret += fprintf(fp, "\n");
  367. rb_node = rb_next(rb_node);
  368. }
  369. return ret;
  370. }
  371. static size_t hist_entry__fprintf(struct hist_entry *self,
  372. struct perf_session *pair_session,
  373. bool show_displacement,
  374. long displacement, FILE *fp,
  375. u64 session_total)
  376. {
  377. struct sort_entry *se;
  378. u64 count, total;
  379. const char *sep = symbol_conf.field_sep;
  380. size_t ret;
  381. if (symbol_conf.exclude_other && !self->parent)
  382. return 0;
  383. if (pair_session) {
  384. count = self->pair ? self->pair->count : 0;
  385. total = pair_session->events_stats.total;
  386. } else {
  387. count = self->count;
  388. total = session_total;
  389. }
  390. if (total)
  391. ret = percent_color_fprintf(fp, sep ? "%.2f" : " %6.2f%%",
  392. (count * 100.0) / total);
  393. else
  394. ret = fprintf(fp, sep ? "%lld" : "%12lld ", count);
  395. if (symbol_conf.show_nr_samples) {
  396. if (sep)
  397. fprintf(fp, "%c%lld", *sep, count);
  398. else
  399. fprintf(fp, "%11lld", count);
  400. }
  401. if (pair_session) {
  402. char bf[32];
  403. double old_percent = 0, new_percent = 0, diff;
  404. if (total > 0)
  405. old_percent = (count * 100.0) / total;
  406. if (session_total > 0)
  407. new_percent = (self->count * 100.0) / session_total;
  408. diff = new_percent - old_percent;
  409. if (fabs(diff) >= 0.01)
  410. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  411. else
  412. snprintf(bf, sizeof(bf), " ");
  413. if (sep)
  414. ret += fprintf(fp, "%c%s", *sep, bf);
  415. else
  416. ret += fprintf(fp, "%11.11s", bf);
  417. if (show_displacement) {
  418. if (displacement)
  419. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  420. else
  421. snprintf(bf, sizeof(bf), " ");
  422. if (sep)
  423. fprintf(fp, "%c%s", *sep, bf);
  424. else
  425. fprintf(fp, "%6.6s", bf);
  426. }
  427. }
  428. list_for_each_entry(se, &hist_entry__sort_list, list) {
  429. if (se->elide)
  430. continue;
  431. fprintf(fp, "%s", sep ?: " ");
  432. ret += se->print(fp, self, se->width ? *se->width : 0);
  433. }
  434. ret += fprintf(fp, "\n");
  435. if (symbol_conf.use_callchain) {
  436. int left_margin = 0;
  437. if (sort__first_dimension == SORT_COMM) {
  438. se = list_first_entry(&hist_entry__sort_list, typeof(*se),
  439. list);
  440. left_margin = se->width ? *se->width : 0;
  441. left_margin -= thread__comm_len(self->thread);
  442. }
  443. hist_entry_callchain__fprintf(fp, self, session_total,
  444. left_margin);
  445. }
  446. return ret;
  447. }
  448. size_t perf_session__fprintf_hists(struct rb_root *hists,
  449. struct perf_session *pair,
  450. bool show_displacement, FILE *fp,
  451. u64 session_total)
  452. {
  453. struct sort_entry *se;
  454. struct rb_node *nd;
  455. size_t ret = 0;
  456. unsigned long position = 1;
  457. long displacement = 0;
  458. unsigned int width;
  459. const char *sep = symbol_conf.field_sep;
  460. char *col_width = symbol_conf.col_width_list_str;
  461. init_rem_hits();
  462. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  463. if (symbol_conf.show_nr_samples) {
  464. if (sep)
  465. fprintf(fp, "%cSamples", *sep);
  466. else
  467. fputs(" Samples ", fp);
  468. }
  469. if (pair) {
  470. if (sep)
  471. ret += fprintf(fp, "%cDelta", *sep);
  472. else
  473. ret += fprintf(fp, " Delta ");
  474. if (show_displacement) {
  475. if (sep)
  476. ret += fprintf(fp, "%cDisplacement", *sep);
  477. else
  478. ret += fprintf(fp, " Displ");
  479. }
  480. }
  481. list_for_each_entry(se, &hist_entry__sort_list, list) {
  482. if (se->elide)
  483. continue;
  484. if (sep) {
  485. fprintf(fp, "%c%s", *sep, se->header);
  486. continue;
  487. }
  488. width = strlen(se->header);
  489. if (se->width) {
  490. if (symbol_conf.col_width_list_str) {
  491. if (col_width) {
  492. *se->width = atoi(col_width);
  493. col_width = strchr(col_width, ',');
  494. if (col_width)
  495. ++col_width;
  496. }
  497. }
  498. width = *se->width = max(*se->width, width);
  499. }
  500. fprintf(fp, " %*s", width, se->header);
  501. }
  502. fprintf(fp, "\n");
  503. if (sep)
  504. goto print_entries;
  505. fprintf(fp, "# ........");
  506. if (symbol_conf.show_nr_samples)
  507. fprintf(fp, " ..........");
  508. if (pair) {
  509. fprintf(fp, " ..........");
  510. if (show_displacement)
  511. fprintf(fp, " .....");
  512. }
  513. list_for_each_entry(se, &hist_entry__sort_list, list) {
  514. unsigned int i;
  515. if (se->elide)
  516. continue;
  517. fprintf(fp, " ");
  518. if (se->width)
  519. width = *se->width;
  520. else
  521. width = strlen(se->header);
  522. for (i = 0; i < width; i++)
  523. fprintf(fp, ".");
  524. }
  525. fprintf(fp, "\n#\n");
  526. print_entries:
  527. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  528. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  529. if (show_displacement) {
  530. if (h->pair != NULL)
  531. displacement = ((long)h->pair->position -
  532. (long)position);
  533. else
  534. displacement = 0;
  535. ++position;
  536. }
  537. ret += hist_entry__fprintf(h, pair, show_displacement,
  538. displacement, fp, session_total);
  539. if (h->map == NULL && verbose > 1) {
  540. __map_groups__fprintf_maps(&h->thread->mg,
  541. MAP__FUNCTION, fp);
  542. fprintf(fp, "%.10s end\n", graph_dotted_line);
  543. }
  544. }
  545. free(rem_sq_bracket);
  546. return ret;
  547. }