hist.c 15 KB

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