1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
11 int perf_use_color_default
= -1;
13 int perf_config_colorbool(const char *var
, const char *value
, int stdout_is_tty
)
16 if (!strcasecmp(value
, "never"))
18 if (!strcasecmp(value
, "always"))
20 if (!strcasecmp(value
, "auto"))
24 /* Missing or explicit false to turn off colorization */
25 if (!perf_config_bool(var
, value
))
28 /* any normal truth value defaults to 'auto' */
30 if (stdout_is_tty
< 0)
31 stdout_is_tty
= isatty(1);
32 if (stdout_is_tty
|| pager_in_use()) {
33 char *term
= getenv("TERM");
34 if (term
&& strcmp(term
, "dumb"))
40 int perf_color_default_config(const char *var
, const char *value
,
41 void *cb __maybe_unused
)
43 if (!strcmp(var
, "color.ui")) {
44 perf_use_color_default
= perf_config_colorbool(var
, value
, -1);
51 static int __color_vsnprintf(char *bf
, size_t size
, const char *color
,
52 const char *fmt
, va_list args
, const char *trail
)
59 if (perf_use_color_default
< 0) {
60 if (isatty(1) || pager_in_use())
61 perf_use_color_default
= 1;
63 perf_use_color_default
= 0;
66 if (perf_use_color_default
&& *color
)
67 r
+= scnprintf(bf
, size
, "%s", color
);
68 r
+= vscnprintf(bf
+ r
, size
- r
, fmt
, args
);
69 if (perf_use_color_default
&& *color
)
70 r
+= scnprintf(bf
+ r
, size
- r
, "%s", PERF_COLOR_RESET
);
72 r
+= scnprintf(bf
+ r
, size
- r
, "%s", trail
);
76 /* Colors are not included in return value */
77 static int __color_vfprintf(FILE *fp
, const char *color
, const char *fmt
,
85 if (perf_use_color_default
< 0) {
86 if (isatty(fileno(fp
)) || pager_in_use())
87 perf_use_color_default
= 1;
89 perf_use_color_default
= 0;
92 if (perf_use_color_default
&& *color
)
93 fprintf(fp
, "%s", color
);
94 r
+= vfprintf(fp
, fmt
, args
);
95 if (perf_use_color_default
&& *color
)
96 fprintf(fp
, "%s", PERF_COLOR_RESET
);
100 int color_vsnprintf(char *bf
, size_t size
, const char *color
,
101 const char *fmt
, va_list args
)
103 return __color_vsnprintf(bf
, size
, color
, fmt
, args
, NULL
);
106 int color_vfprintf(FILE *fp
, const char *color
, const char *fmt
, va_list args
)
108 return __color_vfprintf(fp
, color
, fmt
, args
);
111 int color_snprintf(char *bf
, size_t size
, const char *color
,
112 const char *fmt
, ...)
118 r
= color_vsnprintf(bf
, size
, color
, fmt
, args
);
123 int color_fprintf(FILE *fp
, const char *color
, const char *fmt
, ...)
129 r
= color_vfprintf(fp
, color
, fmt
, args
);
135 * This function splits the buffer by newlines and colors the lines individually.
137 * Returns 0 on success.
139 int color_fwrite_lines(FILE *fp
, const char *color
,
140 size_t count
, const char *buf
)
143 return fwrite(buf
, count
, 1, fp
) != 1;
146 char *p
= memchr(buf
, '\n', count
);
148 if (p
!= buf
&& (fputs(color
, fp
) < 0 ||
149 fwrite(buf
, p
? (size_t)(p
- buf
) : count
, 1, fp
) != 1 ||
150 fputs(PERF_COLOR_RESET
, fp
) < 0))
154 if (fputc('\n', fp
) < 0)
156 count
-= p
+ 1 - buf
;
162 const char *get_percent_color(double percent
)
164 const char *color
= PERF_COLOR_NORMAL
;
167 * We color high-overhead entries in red, mid-overhead
168 * entries in green - and keep the low overhead places
171 if (fabs(percent
) >= MIN_RED
)
172 color
= PERF_COLOR_RED
;
174 if (fabs(percent
) > MIN_GREEN
)
175 color
= PERF_COLOR_GREEN
;
180 int percent_color_fprintf(FILE *fp
, const char *fmt
, double percent
)
185 color
= get_percent_color(percent
);
186 r
= color_fprintf(fp
, color
, fmt
, percent
);
191 int value_color_snprintf(char *bf
, size_t size
, const char *fmt
, double value
)
193 const char *color
= get_percent_color(value
);
194 return color_snprintf(bf
, size
, color
, fmt
, value
);
197 int percent_color_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
203 percent
= va_arg(args
, double);
205 return value_color_snprintf(bf
, size
, fmt
, percent
);
208 int percent_color_len_snprintf(char *bf
, size_t size
, const char *fmt
, ...)
216 len
= va_arg(args
, int);
217 percent
= va_arg(args
, double);
220 color
= get_percent_color(percent
);
221 return color_snprintf(bf
, size
, color
, fmt
, len
, percent
);