Skip to content

Commit f787fef

Browse files
Jin Yaoacmel
authored andcommitted
perf block-info: Support color ops to print block percents in color
It would be nice to print the block percents with colors. This patch supports the 'Sampled Cycles%' and 'Avg Cycles%' printed in colors. For example, perf record -b ... perf report --total-cycles or perf report --total-cycles --stdio percent > 5%, colored in red percent > 0.5%, colored in green percent < 0.5%, default color Signed-off-by: Jin Yao <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Jin Yao <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent cca0cc7 commit f787fef

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

tools/perf/util/block-info.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,29 @@ static int block_column_width(struct perf_hpp_fmt *fmt,
181181
return block_fmt->width;
182182
}
183183

184+
static int color_pct(struct perf_hpp *hpp, int width, double pct)
185+
{
186+
#ifdef HAVE_SLANG_SUPPORT
187+
if (use_browser) {
188+
return __hpp__slsmg_color_printf(hpp, "%*.2f%%",
189+
width - 1, pct);
190+
}
191+
#endif
192+
return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, pct);
193+
}
194+
184195
static int block_total_cycles_pct_entry(struct perf_hpp_fmt *fmt,
185196
struct perf_hpp *hpp,
186197
struct hist_entry *he)
187198
{
188199
struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
189200
struct block_info *bi = he->block_info;
190201
double ratio = 0.0;
191-
char buf[16];
192202

193203
if (block_fmt->total_cycles)
194204
ratio = (double)bi->cycles / (double)block_fmt->total_cycles;
195205

196-
sprintf(buf, "%.2f%%", 100.0 * ratio);
197-
198-
return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
206+
return color_pct(hpp, block_fmt->width, 100.0 * ratio);
199207
}
200208

201209
static int64_t block_total_cycles_pct_sort(struct perf_hpp_fmt *fmt,
@@ -248,16 +256,13 @@ static int block_cycles_pct_entry(struct perf_hpp_fmt *fmt,
248256
struct block_info *bi = he->block_info;
249257
double ratio = 0.0;
250258
u64 avg;
251-
char buf[16];
252259

253260
if (block_fmt->block_cycles && bi->num_aggr) {
254261
avg = bi->cycles_aggr / bi->num_aggr;
255262
ratio = (double)avg / (double)block_fmt->block_cycles;
256263
}
257264

258-
sprintf(buf, "%.2f%%", 100.0 * ratio);
259-
260-
return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
265+
return color_pct(hpp, block_fmt->width, 100.0 * ratio);
261266
}
262267

263268
static int block_avg_cycles_entry(struct perf_hpp_fmt *fmt,
@@ -345,15 +350,15 @@ static void hpp_register(struct block_fmt *block_fmt, int idx,
345350

346351
switch (idx) {
347352
case PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT:
348-
fmt->entry = block_total_cycles_pct_entry;
353+
fmt->color = block_total_cycles_pct_entry;
349354
fmt->cmp = block_info__cmp;
350355
fmt->sort = block_total_cycles_pct_sort;
351356
break;
352357
case PERF_HPP_REPORT__BLOCK_LBR_CYCLES:
353358
fmt->entry = block_cycles_lbr_entry;
354359
break;
355360
case PERF_HPP_REPORT__BLOCK_CYCLES_PCT:
356-
fmt->entry = block_cycles_pct_entry;
361+
fmt->color = block_cycles_pct_entry;
357362
break;
358363
case PERF_HPP_REPORT__BLOCK_AVG_CYCLES:
359364
fmt->entry = block_avg_cycles_entry;

0 commit comments

Comments
 (0)