block-info.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <linux/zalloc.h>
  5. #include "block-info.h"
  6. #include "sort.h"
  7. #include "annotate.h"
  8. #include "symbol.h"
  9. #include "dso.h"
  10. #include "map.h"
  11. #include "srcline.h"
  12. #include "evlist.h"
  13. #include "hist.h"
  14. #include "ui/browsers/hists.h"
  15. static struct block_header_column {
  16. const char *name;
  17. int width;
  18. } block_columns[PERF_HPP_REPORT__BLOCK_MAX_INDEX] = {
  19. [PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT] = {
  20. .name = "Sampled Cycles%",
  21. .width = 15,
  22. },
  23. [PERF_HPP_REPORT__BLOCK_LBR_CYCLES] = {
  24. .name = "Sampled Cycles",
  25. .width = 14,
  26. },
  27. [PERF_HPP_REPORT__BLOCK_CYCLES_PCT] = {
  28. .name = "Avg Cycles%",
  29. .width = 11,
  30. },
  31. [PERF_HPP_REPORT__BLOCK_AVG_CYCLES] = {
  32. .name = "Avg Cycles",
  33. .width = 10,
  34. },
  35. [PERF_HPP_REPORT__BLOCK_RANGE] = {
  36. .name = "[Program Block Range]",
  37. .width = 70,
  38. },
  39. [PERF_HPP_REPORT__BLOCK_DSO] = {
  40. .name = "Shared Object",
  41. .width = 20,
  42. },
  43. [PERF_HPP_REPORT__BLOCK_BRANCH_COUNTER] = {
  44. .name = "Branch Counter",
  45. .width = 30,
  46. }
  47. };
  48. static struct block_info *block_info__new(unsigned int br_cntr_nr)
  49. {
  50. struct block_info *bi = zalloc(sizeof(struct block_info));
  51. if (bi && br_cntr_nr) {
  52. bi->br_cntr = calloc(br_cntr_nr, sizeof(u64));
  53. if (!bi->br_cntr) {
  54. free(bi);
  55. return NULL;
  56. }
  57. }
  58. return bi;
  59. }
  60. void block_info__delete(struct block_info *bi)
  61. {
  62. if (bi)
  63. free(bi->br_cntr);
  64. free(bi);
  65. }
  66. int64_t __block_info__cmp(struct hist_entry *left, struct hist_entry *right)
  67. {
  68. struct block_info *bi_l = left->block_info;
  69. struct block_info *bi_r = right->block_info;
  70. int cmp;
  71. if (!bi_l->sym || !bi_r->sym) {
  72. if (!bi_l->sym && !bi_r->sym)
  73. return -1;
  74. else if (!bi_l->sym)
  75. return -1;
  76. else
  77. return 1;
  78. }
  79. cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
  80. if (cmp)
  81. return cmp;
  82. if (bi_l->start != bi_r->start)
  83. return (int64_t)(bi_r->start - bi_l->start);
  84. return (int64_t)(bi_r->end - bi_l->end);
  85. }
  86. int64_t block_info__cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  87. struct hist_entry *left, struct hist_entry *right)
  88. {
  89. return __block_info__cmp(left, right);
  90. }
  91. static void init_block_info(struct block_info *bi, struct symbol *sym,
  92. struct cyc_hist *ch, int offset,
  93. u64 total_cycles, unsigned int br_cntr_nr,
  94. u64 *br_cntr, struct evsel *evsel)
  95. {
  96. bi->sym = sym;
  97. bi->start = ch->start;
  98. bi->end = offset;
  99. bi->cycles = ch->cycles;
  100. bi->cycles_aggr = ch->cycles_aggr;
  101. bi->num = ch->num;
  102. bi->num_aggr = ch->num_aggr;
  103. bi->total_cycles = total_cycles;
  104. memcpy(bi->cycles_spark, ch->cycles_spark,
  105. NUM_SPARKS * sizeof(u64));
  106. if (br_cntr && br_cntr_nr) {
  107. bi->br_cntr_nr = br_cntr_nr;
  108. memcpy(bi->br_cntr, &br_cntr[offset * br_cntr_nr],
  109. br_cntr_nr * sizeof(u64));
  110. }
  111. bi->evsel = evsel;
  112. }
  113. int block_info__process_sym(struct hist_entry *he, struct block_hist *bh,
  114. u64 *block_cycles_aggr, u64 total_cycles,
  115. unsigned int br_cntr_nr)
  116. {
  117. struct annotation *notes;
  118. struct cyc_hist *ch;
  119. static struct addr_location al;
  120. u64 cycles = 0;
  121. if (!he->ms.map || !he->ms.sym)
  122. return 0;
  123. memset(&al, 0, sizeof(al));
  124. al.map = he->ms.map;
  125. al.sym = he->ms.sym;
  126. notes = symbol__annotation(he->ms.sym);
  127. if (!notes || !notes->branch || !notes->branch->cycles_hist)
  128. return 0;
  129. ch = notes->branch->cycles_hist;
  130. for (unsigned int i = 0; i < symbol__size(he->ms.sym); i++) {
  131. if (ch[i].num_aggr) {
  132. struct block_info *bi;
  133. struct hist_entry *he_block;
  134. bi = block_info__new(br_cntr_nr);
  135. if (!bi)
  136. return -1;
  137. init_block_info(bi, he->ms.sym, &ch[i], i,
  138. total_cycles, br_cntr_nr,
  139. notes->branch->br_cntr,
  140. hists_to_evsel(he->hists));
  141. cycles += bi->cycles_aggr / bi->num_aggr;
  142. he_block = hists__add_entry_block(&bh->block_hists,
  143. &al, bi);
  144. if (!he_block) {
  145. block_info__delete(bi);
  146. return -1;
  147. }
  148. }
  149. }
  150. if (block_cycles_aggr)
  151. *block_cycles_aggr += cycles;
  152. return 0;
  153. }
  154. static int block_column_header(struct perf_hpp_fmt *fmt,
  155. struct perf_hpp *hpp,
  156. struct hists *hists __maybe_unused,
  157. int line __maybe_unused,
  158. int *span __maybe_unused)
  159. {
  160. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  161. return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
  162. block_fmt->header);
  163. }
  164. static int block_column_width(struct perf_hpp_fmt *fmt,
  165. struct perf_hpp *hpp __maybe_unused,
  166. struct hists *hists __maybe_unused)
  167. {
  168. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  169. return block_fmt->width;
  170. }
  171. static int color_pct(struct perf_hpp *hpp, int width, double pct)
  172. {
  173. #ifdef HAVE_SLANG_SUPPORT
  174. if (use_browser) {
  175. return __hpp__slsmg_color_printf(hpp, "%*.2f%%",
  176. width - 1, pct);
  177. }
  178. #endif
  179. return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, pct);
  180. }
  181. static int block_total_cycles_pct_entry(struct perf_hpp_fmt *fmt,
  182. struct perf_hpp *hpp,
  183. struct hist_entry *he)
  184. {
  185. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  186. struct block_info *bi = he->block_info;
  187. double ratio = 0.0;
  188. if (block_fmt->total_cycles)
  189. ratio = (double)bi->cycles_aggr / (double)block_fmt->total_cycles;
  190. return color_pct(hpp, block_fmt->width, 100.0 * ratio);
  191. }
  192. static int64_t block_total_cycles_pct_sort(struct perf_hpp_fmt *fmt,
  193. struct hist_entry *left,
  194. struct hist_entry *right)
  195. {
  196. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  197. struct block_info *bi_l = left->block_info;
  198. struct block_info *bi_r = right->block_info;
  199. double l, r;
  200. if (block_fmt->total_cycles) {
  201. l = ((double)bi_l->cycles_aggr /
  202. (double)block_fmt->total_cycles) * 100000.0;
  203. r = ((double)bi_r->cycles_aggr /
  204. (double)block_fmt->total_cycles) * 100000.0;
  205. return (int64_t)l - (int64_t)r;
  206. }
  207. return 0;
  208. }
  209. static void cycles_string(u64 cycles, char *buf, int size)
  210. {
  211. if (cycles >= 1000000)
  212. scnprintf(buf, size, "%.1fM", (double)cycles / 1000000.0);
  213. else if (cycles >= 1000)
  214. scnprintf(buf, size, "%.1fK", (double)cycles / 1000.0);
  215. else
  216. scnprintf(buf, size, "%1d", cycles);
  217. }
  218. static int block_cycles_lbr_entry(struct perf_hpp_fmt *fmt,
  219. struct perf_hpp *hpp, struct hist_entry *he)
  220. {
  221. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  222. struct block_info *bi = he->block_info;
  223. char cycles_buf[16];
  224. cycles_string(bi->cycles_aggr, cycles_buf, sizeof(cycles_buf));
  225. return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
  226. cycles_buf);
  227. }
  228. static int block_cycles_pct_entry(struct perf_hpp_fmt *fmt,
  229. struct perf_hpp *hpp, struct hist_entry *he)
  230. {
  231. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  232. struct block_info *bi = he->block_info;
  233. double ratio = 0.0;
  234. u64 avg;
  235. if (block_fmt->block_cycles && bi->num_aggr) {
  236. avg = bi->cycles_aggr / bi->num_aggr;
  237. ratio = (double)avg / (double)block_fmt->block_cycles;
  238. }
  239. return color_pct(hpp, block_fmt->width, 100.0 * ratio);
  240. }
  241. static int block_avg_cycles_entry(struct perf_hpp_fmt *fmt,
  242. struct perf_hpp *hpp,
  243. struct hist_entry *he)
  244. {
  245. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  246. struct block_info *bi = he->block_info;
  247. char cycles_buf[16];
  248. cycles_string(bi->cycles_aggr / bi->num_aggr, cycles_buf,
  249. sizeof(cycles_buf));
  250. return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
  251. cycles_buf);
  252. }
  253. static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  254. struct hist_entry *he)
  255. {
  256. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  257. struct block_info *bi = he->block_info;
  258. char buf[128];
  259. char *start_line, *end_line;
  260. symbol_conf.disable_add2line_warn = true;
  261. start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
  262. he->ms.sym);
  263. end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
  264. he->ms.sym);
  265. if (start_line != SRCLINE_UNKNOWN &&
  266. end_line != SRCLINE_UNKNOWN) {
  267. scnprintf(buf, sizeof(buf), "[%s -> %s]",
  268. start_line, end_line);
  269. } else {
  270. scnprintf(buf, sizeof(buf), "[%7lx -> %7lx]",
  271. bi->start, bi->end);
  272. }
  273. zfree_srcline(&start_line);
  274. zfree_srcline(&end_line);
  275. return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
  276. }
  277. static int block_dso_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  278. struct hist_entry *he)
  279. {
  280. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  281. struct map *map = he->ms.map;
  282. if (map && map__dso(map)) {
  283. return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
  284. dso__short_name(map__dso(map)));
  285. }
  286. return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width,
  287. "[unknown]");
  288. }
  289. static void init_block_header(struct block_fmt *block_fmt)
  290. {
  291. struct perf_hpp_fmt *fmt = &block_fmt->fmt;
  292. BUG_ON(block_fmt->idx >= PERF_HPP_REPORT__BLOCK_MAX_INDEX);
  293. block_fmt->header = block_columns[block_fmt->idx].name;
  294. block_fmt->width = block_columns[block_fmt->idx].width;
  295. fmt->header = block_column_header;
  296. fmt->width = block_column_width;
  297. }
  298. static int block_branch_counter_entry(struct perf_hpp_fmt *fmt,
  299. struct perf_hpp *hpp,
  300. struct hist_entry *he)
  301. {
  302. struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
  303. struct block_info *bi = he->block_info;
  304. char *buf;
  305. int ret;
  306. if (annotation_br_cntr_entry(&buf, bi->br_cntr_nr, bi->br_cntr,
  307. bi->num_aggr, bi->evsel))
  308. return 0;
  309. ret = scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
  310. free(buf);
  311. return ret;
  312. }
  313. static void hpp_register(struct block_fmt *block_fmt, int idx,
  314. struct perf_hpp_list *hpp_list)
  315. {
  316. struct perf_hpp_fmt *fmt = &block_fmt->fmt;
  317. block_fmt->idx = idx;
  318. INIT_LIST_HEAD(&fmt->list);
  319. INIT_LIST_HEAD(&fmt->sort_list);
  320. switch (idx) {
  321. case PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT:
  322. fmt->color = block_total_cycles_pct_entry;
  323. fmt->cmp = block_info__cmp;
  324. fmt->sort = block_total_cycles_pct_sort;
  325. break;
  326. case PERF_HPP_REPORT__BLOCK_LBR_CYCLES:
  327. fmt->entry = block_cycles_lbr_entry;
  328. break;
  329. case PERF_HPP_REPORT__BLOCK_CYCLES_PCT:
  330. fmt->color = block_cycles_pct_entry;
  331. break;
  332. case PERF_HPP_REPORT__BLOCK_AVG_CYCLES:
  333. fmt->entry = block_avg_cycles_entry;
  334. break;
  335. case PERF_HPP_REPORT__BLOCK_RANGE:
  336. fmt->entry = block_range_entry;
  337. break;
  338. case PERF_HPP_REPORT__BLOCK_DSO:
  339. fmt->entry = block_dso_entry;
  340. break;
  341. case PERF_HPP_REPORT__BLOCK_BRANCH_COUNTER:
  342. fmt->entry = block_branch_counter_entry;
  343. break;
  344. default:
  345. return;
  346. }
  347. init_block_header(block_fmt);
  348. perf_hpp_list__column_register(hpp_list, fmt);
  349. }
  350. static void register_block_columns(struct perf_hpp_list *hpp_list,
  351. struct block_fmt *block_fmts,
  352. int *block_hpps, int nr_hpps)
  353. {
  354. for (int i = 0; i < nr_hpps; i++)
  355. hpp_register(&block_fmts[i], block_hpps[i], hpp_list);
  356. }
  357. static void init_block_hist(struct block_hist *bh, struct block_fmt *block_fmts,
  358. int *block_hpps, int nr_hpps)
  359. {
  360. __hists__init(&bh->block_hists, &bh->block_list);
  361. perf_hpp_list__init(&bh->block_list);
  362. bh->block_list.nr_header_lines = 1;
  363. register_block_columns(&bh->block_list, block_fmts,
  364. block_hpps, nr_hpps);
  365. /* Sort by the first fmt */
  366. perf_hpp_list__register_sort_field(&bh->block_list, &block_fmts[0].fmt);
  367. }
  368. static int process_block_report(struct hists *hists,
  369. struct block_report *block_report,
  370. u64 total_cycles, int *block_hpps,
  371. int nr_hpps, unsigned int br_cntr_nr)
  372. {
  373. struct rb_node *next = rb_first_cached(&hists->entries);
  374. struct block_hist *bh = &block_report->hist;
  375. struct hist_entry *he;
  376. if (nr_hpps > PERF_HPP_REPORT__BLOCK_MAX_INDEX)
  377. return -1;
  378. block_report->nr_fmts = nr_hpps;
  379. init_block_hist(bh, block_report->fmts, block_hpps, nr_hpps);
  380. while (next) {
  381. he = rb_entry(next, struct hist_entry, rb_node);
  382. block_info__process_sym(he, bh, &block_report->cycles,
  383. total_cycles, br_cntr_nr);
  384. next = rb_next(&he->rb_node);
  385. }
  386. for (int i = 0; i < nr_hpps; i++) {
  387. block_report->fmts[i].total_cycles = total_cycles;
  388. block_report->fmts[i].block_cycles = block_report->cycles;
  389. }
  390. hists__output_resort(&bh->block_hists, NULL);
  391. return 0;
  392. }
  393. struct block_report *block_info__create_report(struct evlist *evlist,
  394. u64 total_cycles,
  395. int *block_hpps, int nr_hpps,
  396. int *nr_reps)
  397. {
  398. struct block_report *block_reports;
  399. int nr_hists = evlist->core.nr_entries, i = 0;
  400. struct evsel *pos;
  401. block_reports = calloc(nr_hists, sizeof(struct block_report));
  402. if (!block_reports)
  403. return NULL;
  404. evlist__for_each_entry(evlist, pos) {
  405. struct hists *hists = evsel__hists(pos);
  406. process_block_report(hists, &block_reports[i], total_cycles,
  407. block_hpps, nr_hpps, evlist->nr_br_cntr);
  408. i++;
  409. }
  410. *nr_reps = nr_hists;
  411. return block_reports;
  412. }
  413. void block_info__free_report(struct block_report *reps, int nr_reps)
  414. {
  415. for (int i = 0; i < nr_reps; i++)
  416. hists__delete_entries(&reps[i].hist.block_hists);
  417. free(reps);
  418. }
  419. int report__browse_block_hists(struct block_hist *bh, float min_percent,
  420. struct evsel *evsel, struct perf_env *env)
  421. {
  422. int ret;
  423. switch (use_browser) {
  424. case 0:
  425. symbol_conf.report_individual_block = true;
  426. hists__fprintf(&bh->block_hists, true, 0, 0, min_percent,
  427. stdout, true);
  428. return 0;
  429. case 1:
  430. symbol_conf.report_individual_block = true;
  431. ret = block_hists_tui_browse(bh, evsel, min_percent, env);
  432. return ret;
  433. default:
  434. return -1;
  435. }
  436. return 0;
  437. }
  438. float block_info__total_cycles_percent(struct hist_entry *he)
  439. {
  440. struct block_info *bi = he->block_info;
  441. if (bi->total_cycles)
  442. return bi->cycles * 100.0 / bi->total_cycles;
  443. return 0.0;
  444. }