Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf_counter tools: Provide helper to print percents color

Among perf annotate, perf report and perf top, we can find the
common colored printing of percents according to the following
rules:

High overhead = > 5%, colored in red
Mid overhead = > 0.5%, colored in green
Low overhead = < 0.5%, default color

Factorize these multiple checks in a single function named
percent_color_fprintf() and also provide a get_percent_color()
for sites which print percentages and other things at the same
time.

Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Anton Blanchard <anton@samba.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
LKML-Reference: <1246558475-10624-2-git-send-email-fweisbec@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

authored by

Frederic Weisbecker and committed by
Ingo Molnar
1e11fd82 c20ab37e

+38 -56
+2 -24
tools/perf/builtin-annotate.c
··· 25 25 #define SHOW_USER 2 26 26 #define SHOW_HV 4 27 27 28 - #define MIN_GREEN 0.5 29 - #define MIN_RED 5.0 30 - 31 - 32 28 static char const *input_name = "perf.data"; 33 29 static char *vmlinux = "vmlinux"; 34 30 ··· 1043 1047 return 0; 1044 1048 } 1045 1049 1046 - static char *get_color(double percent) 1047 - { 1048 - char *color = PERF_COLOR_NORMAL; 1049 - 1050 - /* 1051 - * We color high-overhead entries in red, mid-overhead 1052 - * entries in green - and keep the low overhead places 1053 - * normal: 1054 - */ 1055 - if (percent >= MIN_RED) 1056 - color = PERF_COLOR_RED; 1057 - else { 1058 - if (percent > MIN_GREEN) 1059 - color = PERF_COLOR_GREEN; 1060 - } 1061 - return color; 1062 - } 1063 - 1064 1050 static int 1065 1051 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) 1066 1052 { ··· 1104 1126 } else if (sym->hist_sum) 1105 1127 percent = 100.0 * hits / sym->hist_sum; 1106 1128 1107 - color = get_color(percent); 1129 + color = get_percent_color(percent); 1108 1130 1109 1131 /* 1110 1132 * Also color the filename and line if needed, with ··· 1240 1262 1241 1263 sym_ext = rb_entry(node, struct sym_ext, node); 1242 1264 percent = sym_ext->percent; 1243 - color = get_color(percent); 1265 + color = get_percent_color(percent); 1244 1266 path = sym_ext->path; 1245 1267 1246 1268 color_fprintf(stdout, color, " %7.2f %s", percent, path);
+3 -18
tools/perf/builtin-report.c
··· 947 947 if (exclude_other && !self->parent) 948 948 return 0; 949 949 950 - if (total_samples) { 951 - double percent = self->count * 100.0 / total_samples; 952 - char *color = PERF_COLOR_NORMAL; 953 - 954 - /* 955 - * We color high-overhead entries in red, mid-overhead 956 - * entries in green - and keep the low overhead places 957 - * normal: 958 - */ 959 - if (percent >= 5.0) { 960 - color = PERF_COLOR_RED; 961 - } else { 962 - if (percent >= 0.5) 963 - color = PERF_COLOR_GREEN; 964 - } 965 - 966 - ret = color_fprintf(fp, color, " %6.2f%%", 950 + if (total_samples) 951 + ret = percent_color_fprintf(fp, " %6.2f%%", 967 952 (self->count * 100.0) / total_samples); 968 - } else 953 + else 969 954 ret = fprintf(fp, "%12Ld ", self->count); 970 955 971 956 list_for_each_entry(se, &hist_entry__sort_list, list) {
+1 -14
tools/perf/builtin-top.c
··· 239 239 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { 240 240 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node); 241 241 struct symbol *sym = (struct symbol *)(syme + 1); 242 - char *color = PERF_COLOR_NORMAL; 243 242 double pcnt; 244 243 245 244 if (++printed > print_entries || syme->snap_count < count_filter) ··· 247 248 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / 248 249 sum_ksamples)); 249 250 250 - /* 251 - * We color high-overhead entries in red, mid-overhead 252 - * entries in green - and keep the low overhead places 253 - * normal: 254 - */ 255 - if (pcnt >= 5.0) { 256 - color = PERF_COLOR_RED; 257 - } else { 258 - if (pcnt >= 0.5) 259 - color = PERF_COLOR_GREEN; 260 - } 261 - 262 251 if (nr_counters == 1) 263 252 printf("%20.2f - ", syme->weight); 264 253 else 265 254 printf("%9.1f %10ld - ", syme->weight, syme->snap_count); 266 255 267 - color_fprintf(stdout, color, "%4.1f%%", pcnt); 256 + percent_color_fprintf(stdout, "%4.1f%%", pcnt); 268 257 printf(" - %016llx : %s", sym->start, sym->name); 269 258 if (sym->module) 270 259 printf("\t[%s]", sym->module->name);
+27
tools/perf/util/color.c
··· 242 242 return 0; 243 243 } 244 244 245 + char *get_percent_color(double percent) 246 + { 247 + char *color = PERF_COLOR_NORMAL; 245 248 249 + /* 250 + * We color high-overhead entries in red, mid-overhead 251 + * entries in green - and keep the low overhead places 252 + * normal: 253 + */ 254 + if (percent >= MIN_RED) 255 + color = PERF_COLOR_RED; 256 + else { 257 + if (percent > MIN_GREEN) 258 + color = PERF_COLOR_GREEN; 259 + } 260 + return color; 261 + } 262 + 263 + int percent_color_fprintf(FILE *fp, const char *fmt, double percent) 264 + { 265 + int r; 266 + char *color; 267 + 268 + color = get_percent_color(percent); 269 + r = color_fprintf(fp, color, fmt, percent); 270 + 271 + return r; 272 + }
+5
tools/perf/util/color.h
··· 15 15 #define PERF_COLOR_CYAN "\033[36m" 16 16 #define PERF_COLOR_BG_RED "\033[41m" 17 17 18 + #define MIN_GREEN 0.5 19 + #define MIN_RED 5.0 20 + 18 21 /* 19 22 * This variable stores the value of color.ui 20 23 */ ··· 35 32 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...); 36 33 int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...); 37 34 int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *buf); 35 + int percent_color_fprintf(FILE *fp, const char *fmt, double percent); 36 + char *get_percent_color(double percent); 38 37 39 38 #endif /* COLOR_H */