hist.c 14 KB

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