hist.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. #include "annotate.h"
  2. #include "util.h"
  3. #include "build-id.h"
  4. #include "hist.h"
  5. #include "session.h"
  6. #include "sort.h"
  7. #include <math.h>
  8. static bool hists__filter_entry_by_dso(struct hists *hists,
  9. struct hist_entry *he);
  10. static bool hists__filter_entry_by_thread(struct hists *hists,
  11. struct hist_entry *he);
  12. enum hist_filter {
  13. HIST_FILTER__DSO,
  14. HIST_FILTER__THREAD,
  15. HIST_FILTER__PARENT,
  16. };
  17. struct callchain_param callchain_param = {
  18. .mode = CHAIN_GRAPH_REL,
  19. .min_percent = 0.5,
  20. .order = ORDER_CALLEE
  21. };
  22. u16 hists__col_len(struct hists *hists, enum hist_column col)
  23. {
  24. return hists->col_len[col];
  25. }
  26. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
  27. {
  28. hists->col_len[col] = len;
  29. }
  30. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
  31. {
  32. if (len > hists__col_len(hists, col)) {
  33. hists__set_col_len(hists, col, len);
  34. return true;
  35. }
  36. return false;
  37. }
  38. static void hists__reset_col_len(struct hists *hists)
  39. {
  40. enum hist_column col;
  41. for (col = 0; col < HISTC_NR_COLS; ++col)
  42. hists__set_col_len(hists, col, 0);
  43. }
  44. static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
  45. {
  46. u16 len;
  47. if (h->ms.sym)
  48. hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen);
  49. else {
  50. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  51. if (hists__col_len(hists, HISTC_DSO) < unresolved_col_width &&
  52. !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  53. !symbol_conf.dso_list)
  54. hists__set_col_len(hists, HISTC_DSO,
  55. unresolved_col_width);
  56. }
  57. len = thread__comm_len(h->thread);
  58. if (hists__new_col_len(hists, HISTC_COMM, len))
  59. hists__set_col_len(hists, HISTC_THREAD, len + 6);
  60. if (h->ms.map) {
  61. len = dso__name_len(h->ms.map->dso);
  62. hists__new_col_len(hists, HISTC_DSO, len);
  63. }
  64. }
  65. static void hist_entry__add_cpumode_period(struct hist_entry *he,
  66. unsigned int cpumode, u64 period)
  67. {
  68. switch (cpumode) {
  69. case PERF_RECORD_MISC_KERNEL:
  70. he->period_sys += period;
  71. break;
  72. case PERF_RECORD_MISC_USER:
  73. he->period_us += period;
  74. break;
  75. case PERF_RECORD_MISC_GUEST_KERNEL:
  76. he->period_guest_sys += period;
  77. break;
  78. case PERF_RECORD_MISC_GUEST_USER:
  79. he->period_guest_us += period;
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. static void hist_entry__decay(struct hist_entry *he)
  86. {
  87. he->period = (he->period * 7) / 8;
  88. he->nr_events = (he->nr_events * 7) / 8;
  89. }
  90. static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
  91. {
  92. u64 prev_period = he->period;
  93. if (prev_period == 0)
  94. return true;
  95. hist_entry__decay(he);
  96. if (!he->filtered)
  97. hists->stats.total_period -= prev_period - he->period;
  98. return he->period == 0;
  99. }
  100. static void __hists__decay_entries(struct hists *hists, bool zap_user,
  101. bool zap_kernel, bool threaded)
  102. {
  103. struct rb_node *next = rb_first(&hists->entries);
  104. struct hist_entry *n;
  105. while (next) {
  106. n = rb_entry(next, struct hist_entry, rb_node);
  107. next = rb_next(&n->rb_node);
  108. /*
  109. * We may be annotating this, for instance, so keep it here in
  110. * case some it gets new samples, we'll eventually free it when
  111. * the user stops browsing and it agains gets fully decayed.
  112. */
  113. if (((zap_user && n->level == '.') ||
  114. (zap_kernel && n->level != '.') ||
  115. hists__decay_entry(hists, n)) &&
  116. !n->used) {
  117. rb_erase(&n->rb_node, &hists->entries);
  118. if (sort__need_collapse || threaded)
  119. rb_erase(&n->rb_node_in, &hists->entries_collapsed);
  120. hist_entry__free(n);
  121. --hists->nr_entries;
  122. }
  123. }
  124. }
  125. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  126. {
  127. return __hists__decay_entries(hists, zap_user, zap_kernel, false);
  128. }
  129. void hists__decay_entries_threaded(struct hists *hists,
  130. bool zap_user, bool zap_kernel)
  131. {
  132. return __hists__decay_entries(hists, zap_user, zap_kernel, true);
  133. }
  134. /*
  135. * histogram, sorted on item, collects periods
  136. */
  137. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  138. {
  139. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  140. struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
  141. if (he != NULL) {
  142. *he = *template;
  143. he->nr_events = 1;
  144. if (he->ms.map)
  145. he->ms.map->referenced = true;
  146. if (symbol_conf.use_callchain)
  147. callchain_init(he->callchain);
  148. }
  149. return he;
  150. }
  151. static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  152. {
  153. if (!h->filtered) {
  154. hists__calc_col_len(hists, h);
  155. ++hists->nr_entries;
  156. hists->stats.total_period += h->period;
  157. }
  158. }
  159. static u8 symbol__parent_filter(const struct symbol *parent)
  160. {
  161. if (symbol_conf.exclude_other && parent == NULL)
  162. return 1 << HIST_FILTER__PARENT;
  163. return 0;
  164. }
  165. struct hist_entry *__hists__add_entry(struct hists *hists,
  166. struct addr_location *al,
  167. struct symbol *sym_parent, u64 period)
  168. {
  169. struct rb_node **p;
  170. struct rb_node *parent = NULL;
  171. struct hist_entry *he;
  172. struct hist_entry entry = {
  173. .thread = al->thread,
  174. .ms = {
  175. .map = al->map,
  176. .sym = al->sym,
  177. },
  178. .cpu = al->cpu,
  179. .ip = al->addr,
  180. .level = al->level,
  181. .period = period,
  182. .parent = sym_parent,
  183. .filtered = symbol__parent_filter(sym_parent),
  184. };
  185. int cmp;
  186. pthread_mutex_lock(&hists->lock);
  187. p = &hists->entries_in->rb_node;
  188. while (*p != NULL) {
  189. parent = *p;
  190. he = rb_entry(parent, struct hist_entry, rb_node_in);
  191. cmp = hist_entry__cmp(&entry, he);
  192. if (!cmp) {
  193. he->period += period;
  194. ++he->nr_events;
  195. goto out;
  196. }
  197. if (cmp < 0)
  198. p = &(*p)->rb_left;
  199. else
  200. p = &(*p)->rb_right;
  201. }
  202. he = hist_entry__new(&entry);
  203. if (!he)
  204. goto out_unlock;
  205. rb_link_node(&he->rb_node_in, parent, p);
  206. rb_insert_color(&he->rb_node_in, hists->entries_in);
  207. out:
  208. hist_entry__add_cpumode_period(he, al->cpumode, period);
  209. out_unlock:
  210. pthread_mutex_unlock(&hists->lock);
  211. return he;
  212. }
  213. int64_t
  214. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  215. {
  216. struct sort_entry *se;
  217. int64_t cmp = 0;
  218. list_for_each_entry(se, &hist_entry__sort_list, list) {
  219. cmp = se->se_cmp(left, right);
  220. if (cmp)
  221. break;
  222. }
  223. return cmp;
  224. }
  225. int64_t
  226. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  227. {
  228. struct sort_entry *se;
  229. int64_t cmp = 0;
  230. list_for_each_entry(se, &hist_entry__sort_list, list) {
  231. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  232. f = se->se_collapse ?: se->se_cmp;
  233. cmp = f(left, right);
  234. if (cmp)
  235. break;
  236. }
  237. return cmp;
  238. }
  239. void hist_entry__free(struct hist_entry *he)
  240. {
  241. free(he);
  242. }
  243. /*
  244. * collapse the histogram
  245. */
  246. static bool hists__collapse_insert_entry(struct hists *hists,
  247. struct rb_root *root,
  248. struct hist_entry *he)
  249. {
  250. struct rb_node **p = &root->rb_node;
  251. struct rb_node *parent = NULL;
  252. struct hist_entry *iter;
  253. int64_t cmp;
  254. while (*p != NULL) {
  255. parent = *p;
  256. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  257. cmp = hist_entry__collapse(iter, he);
  258. if (!cmp) {
  259. iter->period += he->period;
  260. iter->nr_events += he->nr_events;
  261. if (symbol_conf.use_callchain) {
  262. callchain_cursor_reset(&hists->callchain_cursor);
  263. callchain_merge(&hists->callchain_cursor, iter->callchain,
  264. he->callchain);
  265. }
  266. hist_entry__free(he);
  267. return false;
  268. }
  269. if (cmp < 0)
  270. p = &(*p)->rb_left;
  271. else
  272. p = &(*p)->rb_right;
  273. }
  274. rb_link_node(&he->rb_node_in, parent, p);
  275. rb_insert_color(&he->rb_node_in, root);
  276. return true;
  277. }
  278. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  279. {
  280. struct rb_root *root;
  281. pthread_mutex_lock(&hists->lock);
  282. root = hists->entries_in;
  283. if (++hists->entries_in > &hists->entries_in_array[1])
  284. hists->entries_in = &hists->entries_in_array[0];
  285. pthread_mutex_unlock(&hists->lock);
  286. return root;
  287. }
  288. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  289. {
  290. hists__filter_entry_by_dso(hists, he);
  291. hists__filter_entry_by_thread(hists, he);
  292. }
  293. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  294. {
  295. struct rb_root *root;
  296. struct rb_node *next;
  297. struct hist_entry *n;
  298. if (!sort__need_collapse && !threaded)
  299. return;
  300. root = hists__get_rotate_entries_in(hists);
  301. next = rb_first(root);
  302. while (next) {
  303. n = rb_entry(next, struct hist_entry, rb_node_in);
  304. next = rb_next(&n->rb_node_in);
  305. rb_erase(&n->rb_node_in, root);
  306. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  307. /*
  308. * If it wasn't combined with one of the entries already
  309. * collapsed, we need to apply the filters that may have
  310. * been set by, say, the hist_browser.
  311. */
  312. hists__apply_filters(hists, n);
  313. }
  314. }
  315. }
  316. void hists__collapse_resort(struct hists *hists)
  317. {
  318. return __hists__collapse_resort(hists, false);
  319. }
  320. void hists__collapse_resort_threaded(struct hists *hists)
  321. {
  322. return __hists__collapse_resort(hists, true);
  323. }
  324. /*
  325. * reverse the map, sort on period.
  326. */
  327. static void __hists__insert_output_entry(struct rb_root *entries,
  328. struct hist_entry *he,
  329. u64 min_callchain_hits)
  330. {
  331. struct rb_node **p = &entries->rb_node;
  332. struct rb_node *parent = NULL;
  333. struct hist_entry *iter;
  334. if (symbol_conf.use_callchain)
  335. callchain_param.sort(&he->sorted_chain, he->callchain,
  336. min_callchain_hits, &callchain_param);
  337. while (*p != NULL) {
  338. parent = *p;
  339. iter = rb_entry(parent, struct hist_entry, rb_node);
  340. if (he->period > iter->period)
  341. p = &(*p)->rb_left;
  342. else
  343. p = &(*p)->rb_right;
  344. }
  345. rb_link_node(&he->rb_node, parent, p);
  346. rb_insert_color(&he->rb_node, entries);
  347. }
  348. static void __hists__output_resort(struct hists *hists, bool threaded)
  349. {
  350. struct rb_root *root;
  351. struct rb_node *next;
  352. struct hist_entry *n;
  353. u64 min_callchain_hits;
  354. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  355. if (sort__need_collapse || threaded)
  356. root = &hists->entries_collapsed;
  357. else
  358. root = hists->entries_in;
  359. next = rb_first(root);
  360. hists->entries = RB_ROOT;
  361. hists->nr_entries = 0;
  362. hists->stats.total_period = 0;
  363. hists__reset_col_len(hists);
  364. while (next) {
  365. n = rb_entry(next, struct hist_entry, rb_node_in);
  366. next = rb_next(&n->rb_node_in);
  367. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  368. hists__inc_nr_entries(hists, n);
  369. }
  370. }
  371. void hists__output_resort(struct hists *hists)
  372. {
  373. return __hists__output_resort(hists, false);
  374. }
  375. void hists__output_resort_threaded(struct hists *hists)
  376. {
  377. return __hists__output_resort(hists, true);
  378. }
  379. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  380. {
  381. int i;
  382. int ret = fprintf(fp, " ");
  383. for (i = 0; i < left_margin; i++)
  384. ret += fprintf(fp, " ");
  385. return ret;
  386. }
  387. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  388. int left_margin)
  389. {
  390. int i;
  391. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  392. for (i = 0; i < depth; i++)
  393. if (depth_mask & (1 << i))
  394. ret += fprintf(fp, "| ");
  395. else
  396. ret += fprintf(fp, " ");
  397. ret += fprintf(fp, "\n");
  398. return ret;
  399. }
  400. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  401. int depth, int depth_mask, int period,
  402. u64 total_samples, u64 hits,
  403. int left_margin)
  404. {
  405. int i;
  406. size_t ret = 0;
  407. ret += callchain__fprintf_left_margin(fp, left_margin);
  408. for (i = 0; i < depth; i++) {
  409. if (depth_mask & (1 << i))
  410. ret += fprintf(fp, "|");
  411. else
  412. ret += fprintf(fp, " ");
  413. if (!period && i == depth - 1) {
  414. double percent;
  415. percent = hits * 100.0 / total_samples;
  416. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  417. } else
  418. ret += fprintf(fp, "%s", " ");
  419. }
  420. if (chain->ms.sym)
  421. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  422. else
  423. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  424. return ret;
  425. }
  426. static struct symbol *rem_sq_bracket;
  427. static struct callchain_list rem_hits;
  428. static void init_rem_hits(void)
  429. {
  430. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  431. if (!rem_sq_bracket) {
  432. fprintf(stderr, "Not enough memory to display remaining hits\n");
  433. return;
  434. }
  435. strcpy(rem_sq_bracket->name, "[...]");
  436. rem_hits.ms.sym = rem_sq_bracket;
  437. }
  438. static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  439. u64 total_samples, int depth,
  440. int depth_mask, int left_margin)
  441. {
  442. struct rb_node *node, *next;
  443. struct callchain_node *child;
  444. struct callchain_list *chain;
  445. int new_depth_mask = depth_mask;
  446. u64 new_total;
  447. u64 remaining;
  448. size_t ret = 0;
  449. int i;
  450. uint entries_printed = 0;
  451. if (callchain_param.mode == CHAIN_GRAPH_REL)
  452. new_total = self->children_hit;
  453. else
  454. new_total = total_samples;
  455. remaining = new_total;
  456. node = rb_first(&self->rb_root);
  457. while (node) {
  458. u64 cumul;
  459. child = rb_entry(node, struct callchain_node, rb_node);
  460. cumul = callchain_cumul_hits(child);
  461. remaining -= cumul;
  462. /*
  463. * The depth mask manages the output of pipes that show
  464. * the depth. We don't want to keep the pipes of the current
  465. * level for the last child of this depth.
  466. * Except if we have remaining filtered hits. They will
  467. * supersede the last child
  468. */
  469. next = rb_next(node);
  470. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  471. new_depth_mask &= ~(1 << (depth - 1));
  472. /*
  473. * But we keep the older depth mask for the line separator
  474. * to keep the level link until we reach the last child
  475. */
  476. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  477. left_margin);
  478. i = 0;
  479. list_for_each_entry(chain, &child->val, list) {
  480. ret += ipchain__fprintf_graph(fp, chain, depth,
  481. new_depth_mask, i++,
  482. new_total,
  483. cumul,
  484. left_margin);
  485. }
  486. ret += __callchain__fprintf_graph(fp, child, new_total,
  487. depth + 1,
  488. new_depth_mask | (1 << depth),
  489. left_margin);
  490. node = next;
  491. if (++entries_printed == callchain_param.print_limit)
  492. break;
  493. }
  494. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  495. remaining && remaining != new_total) {
  496. if (!rem_sq_bracket)
  497. return ret;
  498. new_depth_mask &= ~(1 << (depth - 1));
  499. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  500. new_depth_mask, 0, new_total,
  501. remaining, left_margin);
  502. }
  503. return ret;
  504. }
  505. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  506. u64 total_samples, int left_margin)
  507. {
  508. struct callchain_list *chain;
  509. bool printed = false;
  510. int i = 0;
  511. int ret = 0;
  512. u32 entries_printed = 0;
  513. list_for_each_entry(chain, &self->val, list) {
  514. if (!i++ && sort__first_dimension == SORT_SYM)
  515. continue;
  516. if (!printed) {
  517. ret += callchain__fprintf_left_margin(fp, left_margin);
  518. ret += fprintf(fp, "|\n");
  519. ret += callchain__fprintf_left_margin(fp, left_margin);
  520. ret += fprintf(fp, "---");
  521. left_margin += 3;
  522. printed = true;
  523. } else
  524. ret += callchain__fprintf_left_margin(fp, left_margin);
  525. if (chain->ms.sym)
  526. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  527. else
  528. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  529. if (++entries_printed == callchain_param.print_limit)
  530. break;
  531. }
  532. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  533. return ret;
  534. }
  535. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  536. u64 total_samples)
  537. {
  538. struct callchain_list *chain;
  539. size_t ret = 0;
  540. if (!self)
  541. return 0;
  542. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  543. list_for_each_entry(chain, &self->val, list) {
  544. if (chain->ip >= PERF_CONTEXT_MAX)
  545. continue;
  546. if (chain->ms.sym)
  547. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  548. else
  549. ret += fprintf(fp, " %p\n",
  550. (void *)(long)chain->ip);
  551. }
  552. return ret;
  553. }
  554. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  555. u64 total_samples, int left_margin,
  556. FILE *fp)
  557. {
  558. struct rb_node *rb_node;
  559. struct callchain_node *chain;
  560. size_t ret = 0;
  561. u32 entries_printed = 0;
  562. rb_node = rb_first(&he->sorted_chain);
  563. while (rb_node) {
  564. double percent;
  565. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  566. percent = chain->hit * 100.0 / total_samples;
  567. switch (callchain_param.mode) {
  568. case CHAIN_FLAT:
  569. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  570. percent);
  571. ret += callchain__fprintf_flat(fp, chain, total_samples);
  572. break;
  573. case CHAIN_GRAPH_ABS: /* Falldown */
  574. case CHAIN_GRAPH_REL:
  575. ret += callchain__fprintf_graph(fp, chain, total_samples,
  576. left_margin);
  577. case CHAIN_NONE:
  578. default:
  579. break;
  580. }
  581. ret += fprintf(fp, "\n");
  582. if (++entries_printed == callchain_param.print_limit)
  583. break;
  584. rb_node = rb_next(rb_node);
  585. }
  586. return ret;
  587. }
  588. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  589. {
  590. struct rb_node *next = rb_first(&hists->entries);
  591. struct hist_entry *n;
  592. int row = 0;
  593. hists__reset_col_len(hists);
  594. while (next && row++ < max_rows) {
  595. n = rb_entry(next, struct hist_entry, rb_node);
  596. if (!n->filtered)
  597. hists__calc_col_len(hists, n);
  598. next = rb_next(&n->rb_node);
  599. }
  600. }
  601. static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
  602. size_t size, struct hists *pair_hists,
  603. bool show_displacement, long displacement,
  604. bool color, u64 total_period)
  605. {
  606. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  607. u64 nr_events;
  608. const char *sep = symbol_conf.field_sep;
  609. int ret;
  610. if (symbol_conf.exclude_other && !he->parent)
  611. return 0;
  612. if (pair_hists) {
  613. period = he->pair ? he->pair->period : 0;
  614. nr_events = he->pair ? he->pair->nr_events : 0;
  615. total = pair_hists->stats.total_period;
  616. period_sys = he->pair ? he->pair->period_sys : 0;
  617. period_us = he->pair ? he->pair->period_us : 0;
  618. period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
  619. period_guest_us = he->pair ? he->pair->period_guest_us : 0;
  620. } else {
  621. period = he->period;
  622. nr_events = he->nr_events;
  623. total = total_period;
  624. period_sys = he->period_sys;
  625. period_us = he->period_us;
  626. period_guest_sys = he->period_guest_sys;
  627. period_guest_us = he->period_guest_us;
  628. }
  629. if (total) {
  630. if (color)
  631. ret = percent_color_snprintf(s, size,
  632. sep ? "%.2f" : " %6.2f%%",
  633. (period * 100.0) / total);
  634. else
  635. ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  636. (period * 100.0) / total);
  637. if (symbol_conf.show_cpu_utilization) {
  638. ret += percent_color_snprintf(s + ret, size - ret,
  639. sep ? "%.2f" : " %6.2f%%",
  640. (period_sys * 100.0) / total);
  641. ret += percent_color_snprintf(s + ret, size - ret,
  642. sep ? "%.2f" : " %6.2f%%",
  643. (period_us * 100.0) / total);
  644. if (perf_guest) {
  645. ret += percent_color_snprintf(s + ret,
  646. size - ret,
  647. sep ? "%.2f" : " %6.2f%%",
  648. (period_guest_sys * 100.0) /
  649. total);
  650. ret += percent_color_snprintf(s + ret,
  651. size - ret,
  652. sep ? "%.2f" : " %6.2f%%",
  653. (period_guest_us * 100.0) /
  654. total);
  655. }
  656. }
  657. } else
  658. ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  659. if (symbol_conf.show_nr_samples) {
  660. if (sep)
  661. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  662. else
  663. ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  664. }
  665. if (symbol_conf.show_total_period) {
  666. if (sep)
  667. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  668. else
  669. ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
  670. }
  671. if (pair_hists) {
  672. char bf[32];
  673. double old_percent = 0, new_percent = 0, diff;
  674. if (total > 0)
  675. old_percent = (period * 100.0) / total;
  676. if (total_period > 0)
  677. new_percent = (he->period * 100.0) / total_period;
  678. diff = new_percent - old_percent;
  679. if (fabs(diff) >= 0.01)
  680. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  681. else
  682. snprintf(bf, sizeof(bf), " ");
  683. if (sep)
  684. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  685. else
  686. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  687. if (show_displacement) {
  688. if (displacement)
  689. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  690. else
  691. snprintf(bf, sizeof(bf), " ");
  692. if (sep)
  693. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  694. else
  695. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  696. }
  697. }
  698. return ret;
  699. }
  700. int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
  701. struct hists *hists)
  702. {
  703. const char *sep = symbol_conf.field_sep;
  704. struct sort_entry *se;
  705. int ret = 0;
  706. list_for_each_entry(se, &hist_entry__sort_list, list) {
  707. if (se->elide)
  708. continue;
  709. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  710. ret += se->se_snprintf(he, s + ret, size - ret,
  711. hists__col_len(hists, se->se_width_idx));
  712. }
  713. return ret;
  714. }
  715. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  716. struct hists *hists, struct hists *pair_hists,
  717. bool show_displacement, long displacement,
  718. u64 total_period, FILE *fp)
  719. {
  720. char bf[512];
  721. int ret;
  722. if (size == 0 || size > sizeof(bf))
  723. size = sizeof(bf);
  724. ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
  725. show_displacement, displacement,
  726. true, total_period);
  727. hist_entry__snprintf(he, bf + ret, size - ret, hists);
  728. return fprintf(fp, "%s\n", bf);
  729. }
  730. static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
  731. struct hists *hists,
  732. u64 total_period, FILE *fp)
  733. {
  734. int left_margin = 0;
  735. if (sort__first_dimension == SORT_COMM) {
  736. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  737. typeof(*se), list);
  738. left_margin = hists__col_len(hists, se->se_width_idx);
  739. left_margin -= thread__comm_len(he->thread);
  740. }
  741. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  742. }
  743. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  744. bool show_displacement, bool show_header, int max_rows,
  745. int max_cols, FILE *fp)
  746. {
  747. struct sort_entry *se;
  748. struct rb_node *nd;
  749. size_t ret = 0;
  750. u64 total_period;
  751. unsigned long position = 1;
  752. long displacement = 0;
  753. unsigned int width;
  754. const char *sep = symbol_conf.field_sep;
  755. const char *col_width = symbol_conf.col_width_list_str;
  756. int nr_rows = 0;
  757. init_rem_hits();
  758. if (!show_header)
  759. goto print_entries;
  760. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  761. if (symbol_conf.show_cpu_utilization) {
  762. if (sep) {
  763. ret += fprintf(fp, "%csys", *sep);
  764. ret += fprintf(fp, "%cus", *sep);
  765. if (perf_guest) {
  766. ret += fprintf(fp, "%cguest sys", *sep);
  767. ret += fprintf(fp, "%cguest us", *sep);
  768. }
  769. } else {
  770. ret += fprintf(fp, " sys ");
  771. ret += fprintf(fp, " us ");
  772. if (perf_guest) {
  773. ret += fprintf(fp, " guest sys ");
  774. ret += fprintf(fp, " guest us ");
  775. }
  776. }
  777. }
  778. if (symbol_conf.show_nr_samples) {
  779. if (sep)
  780. fprintf(fp, "%cSamples", *sep);
  781. else
  782. fputs(" Samples ", fp);
  783. }
  784. if (symbol_conf.show_total_period) {
  785. if (sep)
  786. ret += fprintf(fp, "%cPeriod", *sep);
  787. else
  788. ret += fprintf(fp, " Period ");
  789. }
  790. if (pair) {
  791. if (sep)
  792. ret += fprintf(fp, "%cDelta", *sep);
  793. else
  794. ret += fprintf(fp, " Delta ");
  795. if (show_displacement) {
  796. if (sep)
  797. ret += fprintf(fp, "%cDisplacement", *sep);
  798. else
  799. ret += fprintf(fp, " Displ");
  800. }
  801. }
  802. list_for_each_entry(se, &hist_entry__sort_list, list) {
  803. if (se->elide)
  804. continue;
  805. if (sep) {
  806. fprintf(fp, "%c%s", *sep, se->se_header);
  807. continue;
  808. }
  809. width = strlen(se->se_header);
  810. if (symbol_conf.col_width_list_str) {
  811. if (col_width) {
  812. hists__set_col_len(hists, se->se_width_idx,
  813. atoi(col_width));
  814. col_width = strchr(col_width, ',');
  815. if (col_width)
  816. ++col_width;
  817. }
  818. }
  819. if (!hists__new_col_len(hists, se->se_width_idx, width))
  820. width = hists__col_len(hists, se->se_width_idx);
  821. fprintf(fp, " %*s", width, se->se_header);
  822. }
  823. fprintf(fp, "\n");
  824. if (max_rows && ++nr_rows >= max_rows)
  825. goto out;
  826. if (sep)
  827. goto print_entries;
  828. fprintf(fp, "# ........");
  829. if (symbol_conf.show_cpu_utilization)
  830. fprintf(fp, " ....... .......");
  831. if (symbol_conf.show_nr_samples)
  832. fprintf(fp, " ..........");
  833. if (symbol_conf.show_total_period)
  834. fprintf(fp, " ............");
  835. if (pair) {
  836. fprintf(fp, " ..........");
  837. if (show_displacement)
  838. fprintf(fp, " .....");
  839. }
  840. list_for_each_entry(se, &hist_entry__sort_list, list) {
  841. unsigned int i;
  842. if (se->elide)
  843. continue;
  844. fprintf(fp, " ");
  845. width = hists__col_len(hists, se->se_width_idx);
  846. if (width == 0)
  847. width = strlen(se->se_header);
  848. for (i = 0; i < width; i++)
  849. fprintf(fp, ".");
  850. }
  851. fprintf(fp, "\n");
  852. if (max_rows && ++nr_rows >= max_rows)
  853. goto out;
  854. fprintf(fp, "#\n");
  855. if (max_rows && ++nr_rows >= max_rows)
  856. goto out;
  857. print_entries:
  858. total_period = hists->stats.total_period;
  859. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  860. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  861. if (h->filtered)
  862. continue;
  863. if (show_displacement) {
  864. if (h->pair != NULL)
  865. displacement = ((long)h->pair->position -
  866. (long)position);
  867. else
  868. displacement = 0;
  869. ++position;
  870. }
  871. ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
  872. displacement, total_period, fp);
  873. if (symbol_conf.use_callchain)
  874. ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
  875. if (max_rows && ++nr_rows >= max_rows)
  876. goto out;
  877. if (h->ms.map == NULL && verbose > 1) {
  878. __map_groups__fprintf_maps(&h->thread->mg,
  879. MAP__FUNCTION, verbose, fp);
  880. fprintf(fp, "%.10s end\n", graph_dotted_line);
  881. }
  882. }
  883. out:
  884. free(rem_sq_bracket);
  885. return ret;
  886. }
  887. /*
  888. * See hists__fprintf to match the column widths
  889. */
  890. unsigned int hists__sort_list_width(struct hists *hists)
  891. {
  892. struct sort_entry *se;
  893. int ret = 9; /* total % */
  894. if (symbol_conf.show_cpu_utilization) {
  895. ret += 7; /* count_sys % */
  896. ret += 6; /* count_us % */
  897. if (perf_guest) {
  898. ret += 13; /* count_guest_sys % */
  899. ret += 12; /* count_guest_us % */
  900. }
  901. }
  902. if (symbol_conf.show_nr_samples)
  903. ret += 11;
  904. if (symbol_conf.show_total_period)
  905. ret += 13;
  906. list_for_each_entry(se, &hist_entry__sort_list, list)
  907. if (!se->elide)
  908. ret += 2 + hists__col_len(hists, se->se_width_idx);
  909. if (verbose) /* Addr + origin */
  910. ret += 3 + BITS_PER_LONG / 4;
  911. return ret;
  912. }
  913. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  914. enum hist_filter filter)
  915. {
  916. h->filtered &= ~(1 << filter);
  917. if (h->filtered)
  918. return;
  919. ++hists->nr_entries;
  920. if (h->ms.unfolded)
  921. hists->nr_entries += h->nr_rows;
  922. h->row_offset = 0;
  923. hists->stats.total_period += h->period;
  924. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  925. hists__calc_col_len(hists, h);
  926. }
  927. static bool hists__filter_entry_by_dso(struct hists *hists,
  928. struct hist_entry *he)
  929. {
  930. if (hists->dso_filter != NULL &&
  931. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  932. he->filtered |= (1 << HIST_FILTER__DSO);
  933. return true;
  934. }
  935. return false;
  936. }
  937. void hists__filter_by_dso(struct hists *hists)
  938. {
  939. struct rb_node *nd;
  940. hists->nr_entries = hists->stats.total_period = 0;
  941. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  942. hists__reset_col_len(hists);
  943. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  944. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  945. if (symbol_conf.exclude_other && !h->parent)
  946. continue;
  947. if (hists__filter_entry_by_dso(hists, h))
  948. continue;
  949. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  950. }
  951. }
  952. static bool hists__filter_entry_by_thread(struct hists *hists,
  953. struct hist_entry *he)
  954. {
  955. if (hists->thread_filter != NULL &&
  956. he->thread != hists->thread_filter) {
  957. he->filtered |= (1 << HIST_FILTER__THREAD);
  958. return true;
  959. }
  960. return false;
  961. }
  962. void hists__filter_by_thread(struct hists *hists)
  963. {
  964. struct rb_node *nd;
  965. hists->nr_entries = hists->stats.total_period = 0;
  966. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  967. hists__reset_col_len(hists);
  968. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  969. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  970. if (hists__filter_entry_by_thread(hists, h))
  971. continue;
  972. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  973. }
  974. }
  975. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  976. {
  977. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  978. }
  979. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  980. {
  981. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  982. }
  983. void hists__inc_nr_events(struct hists *hists, u32 type)
  984. {
  985. ++hists->stats.nr_events[0];
  986. ++hists->stats.nr_events[type];
  987. }
  988. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  989. {
  990. int i;
  991. size_t ret = 0;
  992. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  993. const char *name;
  994. if (hists->stats.nr_events[i] == 0)
  995. continue;
  996. name = perf_event__name(i);
  997. if (!strcmp(name, "UNKNOWN"))
  998. continue;
  999. ret += fprintf(fp, "%16s events: %10d\n", name,
  1000. hists->stats.nr_events[i]);
  1001. }
  1002. return ret;
  1003. }