hist.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. u64 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. u64 nr_hists = 0;
  158. min_callchain_hits =
  159. total_samples * (callchain_param.min_percent / 100);
  160. tmp = RB_ROOT;
  161. next = rb_first(hists);
  162. while (next) {
  163. n = rb_entry(next, struct hist_entry, rb_node);
  164. next = rb_next(&n->rb_node);
  165. rb_erase(&n->rb_node, hists);
  166. perf_session__insert_output_hist_entry(&tmp, n,
  167. min_callchain_hits);
  168. ++nr_hists;
  169. }
  170. *hists = tmp;
  171. return nr_hists;
  172. }
  173. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  174. {
  175. int i;
  176. int ret = fprintf(fp, " ");
  177. for (i = 0; i < left_margin; i++)
  178. ret += fprintf(fp, " ");
  179. return ret;
  180. }
  181. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  182. int left_margin)
  183. {
  184. int i;
  185. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  186. for (i = 0; i < depth; i++)
  187. if (depth_mask & (1 << i))
  188. ret += fprintf(fp, "| ");
  189. else
  190. ret += fprintf(fp, " ");
  191. ret += fprintf(fp, "\n");
  192. return ret;
  193. }
  194. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  195. int depth, int depth_mask, int count,
  196. u64 total_samples, int hits,
  197. int left_margin)
  198. {
  199. int i;
  200. size_t ret = 0;
  201. ret += callchain__fprintf_left_margin(fp, left_margin);
  202. for (i = 0; i < depth; i++) {
  203. if (depth_mask & (1 << i))
  204. ret += fprintf(fp, "|");
  205. else
  206. ret += fprintf(fp, " ");
  207. if (!count && i == depth - 1) {
  208. double percent;
  209. percent = hits * 100.0 / total_samples;
  210. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  211. } else
  212. ret += fprintf(fp, "%s", " ");
  213. }
  214. if (chain->ms.sym)
  215. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  216. else
  217. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  218. return ret;
  219. }
  220. static struct symbol *rem_sq_bracket;
  221. static struct callchain_list rem_hits;
  222. static void init_rem_hits(void)
  223. {
  224. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  225. if (!rem_sq_bracket) {
  226. fprintf(stderr, "Not enough memory to display remaining hits\n");
  227. return;
  228. }
  229. strcpy(rem_sq_bracket->name, "[...]");
  230. rem_hits.ms.sym = rem_sq_bracket;
  231. }
  232. static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  233. u64 total_samples, int depth,
  234. int depth_mask, int left_margin)
  235. {
  236. struct rb_node *node, *next;
  237. struct callchain_node *child;
  238. struct callchain_list *chain;
  239. int new_depth_mask = depth_mask;
  240. u64 new_total;
  241. u64 remaining;
  242. size_t ret = 0;
  243. int i;
  244. if (callchain_param.mode == CHAIN_GRAPH_REL)
  245. new_total = self->children_hit;
  246. else
  247. new_total = total_samples;
  248. remaining = new_total;
  249. node = rb_first(&self->rb_root);
  250. while (node) {
  251. u64 cumul;
  252. child = rb_entry(node, struct callchain_node, rb_node);
  253. cumul = cumul_hits(child);
  254. remaining -= cumul;
  255. /*
  256. * The depth mask manages the output of pipes that show
  257. * the depth. We don't want to keep the pipes of the current
  258. * level for the last child of this depth.
  259. * Except if we have remaining filtered hits. They will
  260. * supersede the last child
  261. */
  262. next = rb_next(node);
  263. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  264. new_depth_mask &= ~(1 << (depth - 1));
  265. /*
  266. * But we keep the older depth mask for the line separator
  267. * to keep the level link until we reach the last child
  268. */
  269. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  270. left_margin);
  271. i = 0;
  272. list_for_each_entry(chain, &child->val, list) {
  273. ret += ipchain__fprintf_graph(fp, chain, depth,
  274. new_depth_mask, i++,
  275. new_total,
  276. cumul,
  277. left_margin);
  278. }
  279. ret += __callchain__fprintf_graph(fp, child, new_total,
  280. depth + 1,
  281. new_depth_mask | (1 << depth),
  282. left_margin);
  283. node = next;
  284. }
  285. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  286. remaining && remaining != new_total) {
  287. if (!rem_sq_bracket)
  288. return ret;
  289. new_depth_mask &= ~(1 << (depth - 1));
  290. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  291. new_depth_mask, 0, new_total,
  292. remaining, left_margin);
  293. }
  294. return ret;
  295. }
  296. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  297. u64 total_samples, int left_margin)
  298. {
  299. struct callchain_list *chain;
  300. bool printed = false;
  301. int i = 0;
  302. int ret = 0;
  303. list_for_each_entry(chain, &self->val, list) {
  304. if (!i++ && sort__first_dimension == SORT_SYM)
  305. continue;
  306. if (!printed) {
  307. ret += callchain__fprintf_left_margin(fp, left_margin);
  308. ret += fprintf(fp, "|\n");
  309. ret += callchain__fprintf_left_margin(fp, left_margin);
  310. ret += fprintf(fp, "---");
  311. left_margin += 3;
  312. printed = true;
  313. } else
  314. ret += callchain__fprintf_left_margin(fp, left_margin);
  315. if (chain->ms.sym)
  316. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  317. else
  318. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  319. }
  320. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  321. return ret;
  322. }
  323. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  324. u64 total_samples)
  325. {
  326. struct callchain_list *chain;
  327. size_t ret = 0;
  328. if (!self)
  329. return 0;
  330. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  331. list_for_each_entry(chain, &self->val, list) {
  332. if (chain->ip >= PERF_CONTEXT_MAX)
  333. continue;
  334. if (chain->ms.sym)
  335. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  336. else
  337. ret += fprintf(fp, " %p\n",
  338. (void *)(long)chain->ip);
  339. }
  340. return ret;
  341. }
  342. static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  343. u64 total_samples, int left_margin)
  344. {
  345. struct rb_node *rb_node;
  346. struct callchain_node *chain;
  347. size_t ret = 0;
  348. rb_node = rb_first(&self->sorted_chain);
  349. while (rb_node) {
  350. double percent;
  351. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  352. percent = chain->hit * 100.0 / total_samples;
  353. switch (callchain_param.mode) {
  354. case CHAIN_FLAT:
  355. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  356. percent);
  357. ret += callchain__fprintf_flat(fp, chain, total_samples);
  358. break;
  359. case CHAIN_GRAPH_ABS: /* Falldown */
  360. case CHAIN_GRAPH_REL:
  361. ret += callchain__fprintf_graph(fp, chain, total_samples,
  362. left_margin);
  363. case CHAIN_NONE:
  364. default:
  365. break;
  366. }
  367. ret += fprintf(fp, "\n");
  368. rb_node = rb_next(rb_node);
  369. }
  370. return ret;
  371. }
  372. int hist_entry__snprintf(struct hist_entry *self,
  373. char *s, size_t size,
  374. struct perf_session *pair_session,
  375. bool show_displacement,
  376. long displacement, bool color,
  377. u64 session_total)
  378. {
  379. struct sort_entry *se;
  380. u64 count, total;
  381. const char *sep = symbol_conf.field_sep;
  382. int ret;
  383. if (symbol_conf.exclude_other && !self->parent)
  384. return 0;
  385. if (pair_session) {
  386. count = self->pair ? self->pair->count : 0;
  387. total = pair_session->events_stats.total;
  388. } else {
  389. count = self->count;
  390. total = session_total;
  391. }
  392. if (total) {
  393. if (color)
  394. ret = percent_color_snprintf(s, size,
  395. sep ? "%.2f" : " %6.2f%%",
  396. (count * 100.0) / total);
  397. else
  398. ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  399. (count * 100.0) / total);
  400. } else
  401. ret = snprintf(s, size, sep ? "%lld" : "%12lld ", count);
  402. if (symbol_conf.show_nr_samples) {
  403. if (sep)
  404. ret += snprintf(s + ret, size - ret, "%c%lld", *sep, count);
  405. else
  406. ret += snprintf(s + ret, size - ret, "%11lld", count);
  407. }
  408. if (pair_session) {
  409. char bf[32];
  410. double old_percent = 0, new_percent = 0, diff;
  411. if (total > 0)
  412. old_percent = (count * 100.0) / total;
  413. if (session_total > 0)
  414. new_percent = (self->count * 100.0) / session_total;
  415. diff = new_percent - old_percent;
  416. if (fabs(diff) >= 0.01)
  417. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  418. else
  419. snprintf(bf, sizeof(bf), " ");
  420. if (sep)
  421. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  422. else
  423. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  424. if (show_displacement) {
  425. if (displacement)
  426. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  427. else
  428. snprintf(bf, sizeof(bf), " ");
  429. if (sep)
  430. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  431. else
  432. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  433. }
  434. }
  435. list_for_each_entry(se, &hist_entry__sort_list, list) {
  436. if (se->elide)
  437. continue;
  438. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  439. ret += se->snprintf(self, s + ret, size - ret,
  440. se->width ? *se->width : 0);
  441. }
  442. return ret;
  443. }
  444. int hist_entry__fprintf(struct hist_entry *self,
  445. struct perf_session *pair_session,
  446. bool show_displacement,
  447. long displacement, FILE *fp,
  448. u64 session_total)
  449. {
  450. char bf[512];
  451. hist_entry__snprintf(self, bf, sizeof(bf), pair_session,
  452. show_displacement, displacement,
  453. true, session_total);
  454. return fprintf(fp, "%s\n", bf);
  455. }
  456. static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
  457. u64 session_total)
  458. {
  459. int left_margin = 0;
  460. if (sort__first_dimension == SORT_COMM) {
  461. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  462. typeof(*se), list);
  463. left_margin = se->width ? *se->width : 0;
  464. left_margin -= thread__comm_len(self->thread);
  465. }
  466. return hist_entry_callchain__fprintf(fp, self, session_total,
  467. left_margin);
  468. }
  469. size_t perf_session__fprintf_hists(struct rb_root *hists,
  470. struct perf_session *pair,
  471. bool show_displacement, FILE *fp,
  472. u64 session_total)
  473. {
  474. struct sort_entry *se;
  475. struct rb_node *nd;
  476. size_t ret = 0;
  477. unsigned long position = 1;
  478. long displacement = 0;
  479. unsigned int width;
  480. const char *sep = symbol_conf.field_sep;
  481. char *col_width = symbol_conf.col_width_list_str;
  482. init_rem_hits();
  483. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  484. if (symbol_conf.show_nr_samples) {
  485. if (sep)
  486. fprintf(fp, "%cSamples", *sep);
  487. else
  488. fputs(" Samples ", fp);
  489. }
  490. if (pair) {
  491. if (sep)
  492. ret += fprintf(fp, "%cDelta", *sep);
  493. else
  494. ret += fprintf(fp, " Delta ");
  495. if (show_displacement) {
  496. if (sep)
  497. ret += fprintf(fp, "%cDisplacement", *sep);
  498. else
  499. ret += fprintf(fp, " Displ");
  500. }
  501. }
  502. list_for_each_entry(se, &hist_entry__sort_list, list) {
  503. if (se->elide)
  504. continue;
  505. if (sep) {
  506. fprintf(fp, "%c%s", *sep, se->header);
  507. continue;
  508. }
  509. width = strlen(se->header);
  510. if (se->width) {
  511. if (symbol_conf.col_width_list_str) {
  512. if (col_width) {
  513. *se->width = atoi(col_width);
  514. col_width = strchr(col_width, ',');
  515. if (col_width)
  516. ++col_width;
  517. }
  518. }
  519. width = *se->width = max(*se->width, width);
  520. }
  521. fprintf(fp, " %*s", width, se->header);
  522. }
  523. fprintf(fp, "\n");
  524. if (sep)
  525. goto print_entries;
  526. fprintf(fp, "# ........");
  527. if (symbol_conf.show_nr_samples)
  528. fprintf(fp, " ..........");
  529. if (pair) {
  530. fprintf(fp, " ..........");
  531. if (show_displacement)
  532. fprintf(fp, " .....");
  533. }
  534. list_for_each_entry(se, &hist_entry__sort_list, list) {
  535. unsigned int i;
  536. if (se->elide)
  537. continue;
  538. fprintf(fp, " ");
  539. if (se->width)
  540. width = *se->width;
  541. else
  542. width = strlen(se->header);
  543. for (i = 0; i < width; i++)
  544. fprintf(fp, ".");
  545. }
  546. fprintf(fp, "\n#\n");
  547. print_entries:
  548. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  549. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  550. if (show_displacement) {
  551. if (h->pair != NULL)
  552. displacement = ((long)h->pair->position -
  553. (long)position);
  554. else
  555. displacement = 0;
  556. ++position;
  557. }
  558. ret += hist_entry__fprintf(h, pair, show_displacement,
  559. displacement, fp, session_total);
  560. if (symbol_conf.use_callchain)
  561. ret += hist_entry__fprintf_callchain(h, fp, session_total);
  562. if (h->ms.map == NULL && verbose > 1) {
  563. __map_groups__fprintf_maps(&h->thread->mg,
  564. MAP__FUNCTION, verbose, fp);
  565. fprintf(fp, "%.10s end\n", graph_dotted_line);
  566. }
  567. }
  568. free(rem_sq_bracket);
  569. return ret;
  570. }