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