hist.c 32 KB

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