1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <inttypes.h>
4 #include <linux/string.h>
5 #include <linux/time64.h>
6 #include <math.h>
7 #include <perf/cpumap.h>
8 #include "color.h"
9 #include "counts.h"
10 #include "debug.h"
11 #include "evlist.h"
12 #include "evsel.h"
13 #include "stat.h"
14 #include "top.h"
15 #include "thread_map.h"
16 #include "cpumap.h"
17 #include "string2.h"
18 #include <linux/ctype.h>
19 #include "cgroup.h"
20 #include <api/fs/fs.h>
21 #include "util.h"
22 #include "iostat.h"
23 #include "pmu.h"
24 #include "pmus.h"
25 #include "tool_pmu.h"
26
27 #define CNTR_NOT_SUPPORTED "<not supported>"
28 #define CNTR_NOT_COUNTED "<not counted>"
29
30 #define MGROUP_LEN 50
31 #define METRIC_LEN 38
32 #define EVNAME_LEN 32
33 #define COUNTS_LEN 18
34 #define INTERVAL_LEN 16
35 #define CGROUP_LEN 16
36 #define COMM_LEN 16
37 #define PID_LEN 7
38 #define CPUS_LEN 4
39
40 static int aggr_header_lens[] = {
41 [AGGR_CORE] = 18,
42 [AGGR_CACHE] = 22,
43 [AGGR_CLUSTER] = 20,
44 [AGGR_DIE] = 12,
45 [AGGR_SOCKET] = 6,
46 [AGGR_NODE] = 6,
47 [AGGR_NONE] = 6,
48 [AGGR_THREAD] = 16,
49 [AGGR_GLOBAL] = 0,
50 };
51
52 static const char *aggr_header_csv[] = {
53 [AGGR_CORE] = "core,ctrs,",
54 [AGGR_CACHE] = "cache,ctrs,",
55 [AGGR_CLUSTER] = "cluster,ctrs,",
56 [AGGR_DIE] = "die,ctrs,",
57 [AGGR_SOCKET] = "socket,ctrs,",
58 [AGGR_NONE] = "cpu,",
59 [AGGR_THREAD] = "comm-pid,",
60 [AGGR_NODE] = "node,",
61 [AGGR_GLOBAL] = ""
62 };
63
64 static const char *aggr_header_std[] = {
65 [AGGR_CORE] = "core",
66 [AGGR_CACHE] = "cache",
67 [AGGR_CLUSTER] = "cluster",
68 [AGGR_DIE] = "die",
69 [AGGR_SOCKET] = "socket",
70 [AGGR_NONE] = "cpu",
71 [AGGR_THREAD] = "comm-pid",
72 [AGGR_NODE] = "node",
73 [AGGR_GLOBAL] = ""
74 };
75
metric_threshold_classify__color(enum metric_threshold_classify thresh)76 const char *metric_threshold_classify__color(enum metric_threshold_classify thresh)
77 {
78 const char * const colors[] = {
79 "", /* unknown */
80 PERF_COLOR_RED, /* bad */
81 PERF_COLOR_MAGENTA, /* nearly bad */
82 PERF_COLOR_YELLOW, /* less good */
83 PERF_COLOR_GREEN, /* good */
84 };
85 static_assert(ARRAY_SIZE(colors) - 1 == METRIC_THRESHOLD_GOOD, "missing enum value");
86 return colors[thresh];
87 }
88
metric_threshold_classify__str(enum metric_threshold_classify thresh)89 static const char *metric_threshold_classify__str(enum metric_threshold_classify thresh)
90 {
91 const char * const strs[] = {
92 "unknown",
93 "bad",
94 "nearly bad",
95 "less good",
96 "good",
97 };
98 static_assert(ARRAY_SIZE(strs) - 1 == METRIC_THRESHOLD_GOOD, "missing enum value");
99 return strs[thresh];
100 }
101
print_running_std(struct perf_stat_config * config,u64 run,u64 ena)102 static void print_running_std(struct perf_stat_config *config, u64 run, u64 ena)
103 {
104 if (run != ena)
105 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
106 }
107
print_running_csv(struct perf_stat_config * config,u64 run,u64 ena)108 static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
109 {
110 double enabled_percent = 100;
111
112 if (run != ena)
113 enabled_percent = 100 * run / ena;
114 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
115 config->csv_sep, run, config->csv_sep, enabled_percent);
116 }
117 struct outstate {
118 /* Std mode: insert a newline before the next metric */
119 bool newline;
120 /* JSON mode: track need for comma for a previous field or not */
121 bool first;
122 /* Num CSV separators remaining to pad out when not all fields are printed */
123 int csv_col_pad;
124
125 /*
126 * The following don't track state across fields, but are here as a shortcut to
127 * pass data to the print functions. The alternative would be to update the
128 * function signatures of the entire print stack to pass them through.
129 */
130 /* Place to output to */
131 FILE * const fh;
132 /* Lines are timestamped in --interval-print mode */
133 char timestamp[64];
134 /* Num items aggregated in current line. See struct perf_stat_aggr.nr */
135 int aggr_nr;
136 /* Core/socket/die etc ID for the current line */
137 struct aggr_cpu_id id;
138 /* Event for current line */
139 struct evsel *evsel;
140 /* Cgroup for current line */
141 struct cgroup *cgrp;
142 };
143
json_sep(struct outstate * os)144 static const char *json_sep(struct outstate *os)
145 {
146 const char *sep = os->first ? "" : ", ";
147
148 os->first = false;
149 return sep;
150 }
151
152 #define json_out(os, format, ...) fprintf((os)->fh, "%s" format, json_sep(os), ##__VA_ARGS__)
153
print_running_json(struct outstate * os,u64 run,u64 ena)154 static void print_running_json(struct outstate *os, u64 run, u64 ena)
155 {
156 double enabled_percent = 100;
157
158 if (run != ena)
159 enabled_percent = 100 * run / ena;
160 json_out(os, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f",
161 run, enabled_percent);
162 }
163
print_running(struct perf_stat_config * config,struct outstate * os,u64 run,u64 ena,bool before_metric)164 static void print_running(struct perf_stat_config *config, struct outstate *os,
165 u64 run, u64 ena, bool before_metric)
166 {
167 if (config->json_output) {
168 if (before_metric)
169 print_running_json(os, run, ena);
170 } else if (config->csv_output) {
171 if (before_metric)
172 print_running_csv(config, run, ena);
173 } else {
174 if (!before_metric)
175 print_running_std(config, run, ena);
176 }
177 }
178
print_noise_pct_std(struct perf_stat_config * config,double pct)179 static void print_noise_pct_std(struct perf_stat_config *config,
180 double pct)
181 {
182 if (pct)
183 fprintf(config->output, " ( +-%6.2f%% )", pct);
184 }
185
print_noise_pct_csv(struct perf_stat_config * config,double pct)186 static void print_noise_pct_csv(struct perf_stat_config *config,
187 double pct)
188 {
189 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
190 }
191
print_noise_pct_json(struct outstate * os,double pct)192 static void print_noise_pct_json(struct outstate *os,
193 double pct)
194 {
195 json_out(os, "\"variance\" : %.2f", pct);
196 }
197
print_noise_pct(struct perf_stat_config * config,struct outstate * os,double total,double avg,bool before_metric)198 static void print_noise_pct(struct perf_stat_config *config, struct outstate *os,
199 double total, double avg, bool before_metric)
200 {
201 double pct = rel_stddev_stats(total, avg);
202
203 if (config->json_output) {
204 if (before_metric)
205 print_noise_pct_json(os, pct);
206 } else if (config->csv_output) {
207 if (before_metric)
208 print_noise_pct_csv(config, pct);
209 } else {
210 if (!before_metric)
211 print_noise_pct_std(config, pct);
212 }
213 }
214
print_noise(struct perf_stat_config * config,struct outstate * os,struct evsel * evsel,double avg,bool before_metric)215 static void print_noise(struct perf_stat_config *config, struct outstate *os,
216 struct evsel *evsel, double avg, bool before_metric)
217 {
218 struct perf_stat_evsel *ps;
219
220 if (config->run_count == 1)
221 return;
222
223 ps = evsel->stats;
224 print_noise_pct(config, os, stddev_stats(&ps->res_stats), avg, before_metric);
225 }
226
print_cgroup_std(struct perf_stat_config * config,const char * cgrp_name)227 static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
228 {
229 fprintf(config->output, " %-*s", CGROUP_LEN, cgrp_name);
230 }
231
print_cgroup_csv(struct perf_stat_config * config,const char * cgrp_name)232 static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
233 {
234 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
235 }
236
print_cgroup_json(struct outstate * os,const char * cgrp_name)237 static void print_cgroup_json(struct outstate *os, const char *cgrp_name)
238 {
239 json_out(os, "\"cgroup\" : \"%s\"", cgrp_name);
240 }
241
print_cgroup(struct perf_stat_config * config,struct outstate * os,struct cgroup * cgrp)242 static void print_cgroup(struct perf_stat_config *config, struct outstate *os,
243 struct cgroup *cgrp)
244 {
245 if (nr_cgroups || config->cgroup_list) {
246 const char *cgrp_name = cgrp ? cgrp->name : "";
247
248 if (config->json_output)
249 print_cgroup_json(os, cgrp_name);
250 else if (config->csv_output)
251 print_cgroup_csv(config, cgrp_name);
252 else
253 print_cgroup_std(config, cgrp_name);
254 }
255 }
256
print_aggr_id_std(struct perf_stat_config * config,struct evsel * evsel,struct aggr_cpu_id id,int aggr_nr)257 static void print_aggr_id_std(struct perf_stat_config *config,
258 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
259 {
260 FILE *output = config->output;
261 int idx = config->aggr_mode;
262 char buf[128];
263
264 switch (config->aggr_mode) {
265 case AGGR_CORE:
266 snprintf(buf, sizeof(buf), "S%d-D%d-C%d", id.socket, id.die, id.core);
267 break;
268 case AGGR_CACHE:
269 snprintf(buf, sizeof(buf), "S%d-D%d-L%d-ID%d",
270 id.socket, id.die, id.cache_lvl, id.cache);
271 break;
272 case AGGR_CLUSTER:
273 snprintf(buf, sizeof(buf), "S%d-D%d-CLS%d", id.socket, id.die, id.cluster);
274 break;
275 case AGGR_DIE:
276 snprintf(buf, sizeof(buf), "S%d-D%d", id.socket, id.die);
277 break;
278 case AGGR_SOCKET:
279 snprintf(buf, sizeof(buf), "S%d", id.socket);
280 break;
281 case AGGR_NODE:
282 snprintf(buf, sizeof(buf), "N%d", id.node);
283 break;
284 case AGGR_NONE:
285 if (evsel->percore && !config->percore_show_thread) {
286 snprintf(buf, sizeof(buf), "S%d-D%d-C%d ",
287 id.socket, id.die, id.core);
288 fprintf(output, "%-*s ",
289 aggr_header_lens[AGGR_CORE], buf);
290 } else if (id.cpu.cpu > -1) {
291 fprintf(output, "CPU%-*d ",
292 aggr_header_lens[AGGR_NONE] - 3, id.cpu.cpu);
293 }
294 return;
295 case AGGR_THREAD:
296 fprintf(output, "%*s-%-*d ",
297 COMM_LEN, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
298 PID_LEN, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
299 return;
300 case AGGR_GLOBAL:
301 case AGGR_UNSET:
302 case AGGR_MAX:
303 default:
304 return;
305 }
306
307 fprintf(output, "%-*s %*d ", aggr_header_lens[idx], buf, /*strlen("ctrs")*/ 4, aggr_nr);
308 }
309
print_aggr_id_csv(struct perf_stat_config * config,struct evsel * evsel,struct aggr_cpu_id id,int aggr_nr)310 static void print_aggr_id_csv(struct perf_stat_config *config,
311 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
312 {
313 FILE *output = config->output;
314 const char *sep = config->csv_sep;
315
316 switch (config->aggr_mode) {
317 case AGGR_CORE:
318 fprintf(output, "S%d-D%d-C%d%s%d%s",
319 id.socket, id.die, id.core, sep, aggr_nr, sep);
320 break;
321 case AGGR_CACHE:
322 fprintf(config->output, "S%d-D%d-L%d-ID%d%s%d%s",
323 id.socket, id.die, id.cache_lvl, id.cache, sep, aggr_nr, sep);
324 break;
325 case AGGR_CLUSTER:
326 fprintf(config->output, "S%d-D%d-CLS%d%s%d%s",
327 id.socket, id.die, id.cluster, sep, aggr_nr, sep);
328 break;
329 case AGGR_DIE:
330 fprintf(output, "S%d-D%d%s%d%s",
331 id.socket, id.die, sep, aggr_nr, sep);
332 break;
333 case AGGR_SOCKET:
334 fprintf(output, "S%d%s%d%s",
335 id.socket, sep, aggr_nr, sep);
336 break;
337 case AGGR_NODE:
338 fprintf(output, "N%d%s%d%s",
339 id.node, sep, aggr_nr, sep);
340 break;
341 case AGGR_NONE:
342 if (evsel->percore && !config->percore_show_thread) {
343 fprintf(output, "S%d-D%d-C%d%s",
344 id.socket, id.die, id.core, sep);
345 } else if (id.cpu.cpu > -1) {
346 fprintf(output, "CPU%d%s",
347 id.cpu.cpu, sep);
348 }
349 break;
350 case AGGR_THREAD:
351 fprintf(output, "%s-%d%s",
352 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
353 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
354 sep);
355 break;
356 case AGGR_GLOBAL:
357 case AGGR_UNSET:
358 case AGGR_MAX:
359 default:
360 break;
361 }
362 }
363
print_aggr_id_json(struct perf_stat_config * config,struct outstate * os,struct evsel * evsel,struct aggr_cpu_id id,int aggr_nr)364 static void print_aggr_id_json(struct perf_stat_config *config, struct outstate *os,
365 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
366 {
367 switch (config->aggr_mode) {
368 case AGGR_CORE:
369 json_out(os, "\"core\" : \"S%d-D%d-C%d\", \"counters\" : %d",
370 id.socket, id.die, id.core, aggr_nr);
371 break;
372 case AGGR_CACHE:
373 json_out(os, "\"cache\" : \"S%d-D%d-L%d-ID%d\", \"counters\" : %d",
374 id.socket, id.die, id.cache_lvl, id.cache, aggr_nr);
375 break;
376 case AGGR_CLUSTER:
377 json_out(os, "\"cluster\" : \"S%d-D%d-CLS%d\", \"counters\" : %d",
378 id.socket, id.die, id.cluster, aggr_nr);
379 break;
380 case AGGR_DIE:
381 json_out(os, "\"die\" : \"S%d-D%d\", \"counters\" : %d",
382 id.socket, id.die, aggr_nr);
383 break;
384 case AGGR_SOCKET:
385 json_out(os, "\"socket\" : \"S%d\", \"counters\" : %d",
386 id.socket, aggr_nr);
387 break;
388 case AGGR_NODE:
389 json_out(os, "\"node\" : \"N%d\", \"counters\" : %d",
390 id.node, aggr_nr);
391 break;
392 case AGGR_NONE:
393 if (evsel->percore && !config->percore_show_thread) {
394 json_out(os, "\"core\" : \"S%d-D%d-C%d\"",
395 id.socket, id.die, id.core);
396 } else if (id.cpu.cpu > -1) {
397 json_out(os, "\"cpu\" : \"%d\"",
398 id.cpu.cpu);
399 }
400 break;
401 case AGGR_THREAD:
402 json_out(os, "\"thread\" : \"%s-%d\"",
403 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
404 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
405 break;
406 case AGGR_GLOBAL:
407 case AGGR_UNSET:
408 case AGGR_MAX:
409 default:
410 break;
411 }
412 }
413
aggr_printout(struct perf_stat_config * config,struct outstate * os,struct evsel * evsel,struct aggr_cpu_id id,int aggr_nr)414 static void aggr_printout(struct perf_stat_config *config, struct outstate *os,
415 struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
416 {
417 if (config->json_output)
418 print_aggr_id_json(config, os, evsel, id, aggr_nr);
419 else if (config->csv_output)
420 print_aggr_id_csv(config, evsel, id, aggr_nr);
421 else
422 print_aggr_id_std(config, evsel, id, aggr_nr);
423 }
424
new_line_std(struct perf_stat_config * config __maybe_unused,void * ctx)425 static void new_line_std(struct perf_stat_config *config __maybe_unused,
426 void *ctx)
427 {
428 struct outstate *os = ctx;
429
430 os->newline = true;
431 }
432
__new_line_std_csv(struct perf_stat_config * config,struct outstate * os)433 static inline void __new_line_std_csv(struct perf_stat_config *config,
434 struct outstate *os)
435 {
436 fputc('\n', os->fh);
437 if (config->interval)
438 fputs(os->timestamp, os->fh);
439 aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
440 }
441
__new_line_std(struct outstate * os)442 static inline void __new_line_std(struct outstate *os)
443 {
444 fprintf(os->fh, " ");
445 }
446
do_new_line_std(struct perf_stat_config * config,struct outstate * os)447 static void do_new_line_std(struct perf_stat_config *config,
448 struct outstate *os)
449 {
450 __new_line_std_csv(config, os);
451 if (config->aggr_mode == AGGR_NONE)
452 fprintf(os->fh, " ");
453 __new_line_std(os);
454 }
455
print_metric_std(struct perf_stat_config * config,void * ctx,enum metric_threshold_classify thresh,const char * fmt,const char * unit,double val)456 static void print_metric_std(struct perf_stat_config *config,
457 void *ctx, enum metric_threshold_classify thresh,
458 const char *fmt, const char *unit, double val)
459 {
460 struct outstate *os = ctx;
461 FILE *out = os->fh;
462 int n;
463 bool newline = os->newline;
464 const char *color = metric_threshold_classify__color(thresh);
465
466 os->newline = false;
467
468 if (unit == NULL || fmt == NULL) {
469 fprintf(out, "%-*s", METRIC_LEN, "");
470 return;
471 }
472
473 if (newline)
474 do_new_line_std(config, os);
475
476 n = fprintf(out, " # ");
477 if (color)
478 n += color_fprintf(out, color, fmt, val);
479 else
480 n += fprintf(out, fmt, val);
481 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
482 }
483
new_line_csv(struct perf_stat_config * config,void * ctx)484 static void new_line_csv(struct perf_stat_config *config, void *ctx)
485 {
486 struct outstate *os = ctx;
487 int i;
488
489 __new_line_std_csv(config, os);
490 for (i = 0; i < os->csv_col_pad; i++)
491 fputs(config->csv_sep, os->fh);
492 }
493
print_metric_csv(struct perf_stat_config * config __maybe_unused,void * ctx,enum metric_threshold_classify thresh __maybe_unused,const char * fmt,const char * unit,double val)494 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
495 void *ctx,
496 enum metric_threshold_classify thresh __maybe_unused,
497 const char *fmt, const char *unit, double val)
498 {
499 struct outstate *os = ctx;
500 FILE *out = os->fh;
501 char buf[64], *vals, *ends;
502
503 if (unit == NULL || fmt == NULL) {
504 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
505 return;
506 }
507 snprintf(buf, sizeof(buf), fmt, val);
508 ends = vals = skip_spaces(buf);
509 while (isdigit(*ends) || *ends == '.')
510 ends++;
511 *ends = 0;
512 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
513 }
514
print_metric_json(struct perf_stat_config * config __maybe_unused,void * ctx,enum metric_threshold_classify thresh,const char * fmt __maybe_unused,const char * unit,double val)515 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
516 void *ctx,
517 enum metric_threshold_classify thresh,
518 const char *fmt __maybe_unused,
519 const char *unit, double val)
520 {
521 struct outstate *os = ctx;
522 FILE *out = os->fh;
523
524 if (unit) {
525 json_out(os, "\"metric-value\" : \"%f\", \"metric-unit\" : \"%s\"", val, unit);
526 if (thresh != METRIC_THRESHOLD_UNKNOWN) {
527 json_out(os, "\"metric-threshold\" : \"%s\"",
528 metric_threshold_classify__str(thresh));
529 }
530 }
531 if (!config->metric_only)
532 fprintf(out, "}");
533 }
534
new_line_json(struct perf_stat_config * config,void * ctx)535 static void new_line_json(struct perf_stat_config *config, void *ctx)
536 {
537 struct outstate *os = ctx;
538
539 fputs("\n{", os->fh);
540 os->first = true;
541 if (config->interval)
542 json_out(os, "%s", os->timestamp);
543
544 aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
545 }
546
print_metricgroup_header_json(struct perf_stat_config * config,void * ctx,const char * metricgroup_name)547 static void print_metricgroup_header_json(struct perf_stat_config *config,
548 void *ctx,
549 const char *metricgroup_name)
550 {
551 if (!metricgroup_name)
552 return;
553
554 json_out((struct outstate *) ctx, "\"metricgroup\" : \"%s\"}", metricgroup_name);
555 new_line_json(config, ctx);
556 }
557
print_metricgroup_header_csv(struct perf_stat_config * config,void * ctx,const char * metricgroup_name)558 static void print_metricgroup_header_csv(struct perf_stat_config *config,
559 void *ctx,
560 const char *metricgroup_name)
561 {
562 struct outstate *os = ctx;
563 int i;
564
565 if (!metricgroup_name) {
566 /* Leave space for running and enabling */
567 for (i = 0; i < os->csv_col_pad - 2; i++)
568 fputs(config->csv_sep, os->fh);
569 return;
570 }
571
572 for (i = 0; i < os->csv_col_pad; i++)
573 fputs(config->csv_sep, os->fh);
574 fprintf(config->output, "%s", metricgroup_name);
575 new_line_csv(config, ctx);
576 }
577
print_metricgroup_header_std(struct perf_stat_config * config,void * ctx,const char * metricgroup_name)578 static void print_metricgroup_header_std(struct perf_stat_config *config,
579 void *ctx,
580 const char *metricgroup_name)
581 {
582 struct outstate *os = ctx;
583 int n;
584
585 if (!metricgroup_name) {
586 __new_line_std(os);
587 return;
588 }
589
590 n = fprintf(config->output, " %*s", EVNAME_LEN, metricgroup_name);
591
592 fprintf(config->output, "%*s", MGROUP_LEN - n - 1, "");
593 }
594
595 /* Filter out some columns that don't work well in metrics only mode */
596
valid_only_metric(const char * unit)597 static bool valid_only_metric(const char *unit)
598 {
599 if (!unit)
600 return false;
601 if (strstr(unit, "/sec") ||
602 strstr(unit, "CPUs utilized"))
603 return false;
604 return true;
605 }
606
fixunit(char * buf,struct evsel * evsel,const char * unit)607 static const char *fixunit(char *buf, struct evsel *evsel,
608 const char *unit)
609 {
610 if (!strncmp(unit, "of all", 6)) {
611 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
612 unit);
613 return buf;
614 }
615 return unit;
616 }
617
print_metric_only(struct perf_stat_config * config,void * ctx,enum metric_threshold_classify thresh,const char * fmt,const char * unit,double val)618 static void print_metric_only(struct perf_stat_config *config,
619 void *ctx, enum metric_threshold_classify thresh,
620 const char *fmt, const char *unit, double val)
621 {
622 struct outstate *os = ctx;
623 FILE *out = os->fh;
624 char buf[1024], str[1024];
625 unsigned mlen = config->metric_only_len;
626 const char *color = metric_threshold_classify__color(thresh);
627
628 if (!valid_only_metric(unit))
629 return;
630 unit = fixunit(buf, os->evsel, unit);
631 if (mlen < strlen(unit))
632 mlen = strlen(unit) + 1;
633
634 if (color)
635 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
636
637 color_snprintf(str, sizeof(str), color ?: "", fmt ?: "", val);
638 fprintf(out, "%*s ", mlen, str);
639 os->first = false;
640 }
641
print_metric_only_csv(struct perf_stat_config * config __maybe_unused,void * ctx,enum metric_threshold_classify thresh __maybe_unused,const char * fmt,const char * unit,double val)642 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
643 void *ctx,
644 enum metric_threshold_classify thresh __maybe_unused,
645 const char *fmt,
646 const char *unit, double val)
647 {
648 struct outstate *os = ctx;
649 FILE *out = os->fh;
650 char buf[64], *vals, *ends;
651 char tbuf[1024];
652
653 if (!valid_only_metric(unit))
654 return;
655 unit = fixunit(tbuf, os->evsel, unit);
656 snprintf(buf, sizeof(buf), fmt ?: "", val);
657 ends = vals = skip_spaces(buf);
658 while (isdigit(*ends) || *ends == '.')
659 ends++;
660 *ends = 0;
661 fprintf(out, "%s%s", vals, config->csv_sep);
662 os->first = false;
663 }
664
print_metric_only_json(struct perf_stat_config * config __maybe_unused,void * ctx,enum metric_threshold_classify thresh __maybe_unused,const char * fmt,const char * unit,double val)665 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
666 void *ctx,
667 enum metric_threshold_classify thresh __maybe_unused,
668 const char *fmt,
669 const char *unit, double val)
670 {
671 struct outstate *os = ctx;
672 char buf[64], *ends;
673 char tbuf[1024];
674 const char *vals;
675
676 if (!valid_only_metric(unit))
677 return;
678 unit = fixunit(tbuf, os->evsel, unit);
679 if (!unit[0])
680 return;
681 snprintf(buf, sizeof(buf), fmt ?: "", val);
682 vals = ends = skip_spaces(buf);
683 while (isdigit(*ends) || *ends == '.')
684 ends++;
685 *ends = 0;
686 if (!vals[0])
687 vals = "none";
688 json_out(os, "\"%s\" : \"%s\"", unit, vals);
689 }
690
print_metric_header(struct perf_stat_config * config,void * ctx,enum metric_threshold_classify thresh __maybe_unused,const char * fmt __maybe_unused,const char * unit,double val __maybe_unused)691 static void print_metric_header(struct perf_stat_config *config,
692 void *ctx,
693 enum metric_threshold_classify thresh __maybe_unused,
694 const char *fmt __maybe_unused,
695 const char *unit, double val __maybe_unused)
696 {
697 struct outstate *os = ctx;
698 char tbuf[1024];
699
700 /* In case of iostat, print metric header for first root port only */
701 if (config->iostat_run &&
702 os->evsel->priv != os->evsel->evlist->selected->priv)
703 return;
704
705 if (os->evsel->cgrp != os->cgrp)
706 return;
707
708 if (!valid_only_metric(unit))
709 return;
710 unit = fixunit(tbuf, os->evsel, unit);
711
712 if (config->json_output)
713 return;
714 else if (config->csv_output)
715 fprintf(os->fh, "%s%s", unit, config->csv_sep);
716 else
717 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
718 }
719
print_counter_value_std(struct perf_stat_config * config,struct evsel * evsel,double avg,bool ok)720 static void print_counter_value_std(struct perf_stat_config *config,
721 struct evsel *evsel, double avg, bool ok)
722 {
723 FILE *output = config->output;
724 double sc = evsel->scale;
725 const char *fmt;
726 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
727
728 if (config->big_num)
729 fmt = floor(sc) != sc ? "%'*.2f " : "%'*.0f ";
730 else
731 fmt = floor(sc) != sc ? "%*.2f " : "%*.0f ";
732
733 if (ok)
734 fprintf(output, fmt, COUNTS_LEN, avg);
735 else
736 fprintf(output, "%*s ", COUNTS_LEN, bad_count);
737
738 if (evsel->unit)
739 fprintf(output, "%-*s ", config->unit_width, evsel->unit);
740
741 fprintf(output, "%-*s", EVNAME_LEN, evsel__name(evsel));
742 }
743
print_counter_value_csv(struct perf_stat_config * config,struct evsel * evsel,double avg,bool ok)744 static void print_counter_value_csv(struct perf_stat_config *config,
745 struct evsel *evsel, double avg, bool ok)
746 {
747 FILE *output = config->output;
748 double sc = evsel->scale;
749 const char *sep = config->csv_sep;
750 const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
751 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
752
753 if (ok)
754 fprintf(output, fmt, avg, sep);
755 else
756 fprintf(output, "%s%s", bad_count, sep);
757
758 if (evsel->unit)
759 fprintf(output, "%s%s", evsel->unit, sep);
760
761 fprintf(output, "%s", evsel__name(evsel));
762 }
763
print_counter_value_json(struct outstate * os,struct evsel * evsel,double avg,bool ok)764 static void print_counter_value_json(struct outstate *os,
765 struct evsel *evsel, double avg, bool ok)
766 {
767 const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
768
769 if (ok)
770 json_out(os, "\"counter-value\" : \"%f\"", avg);
771 else
772 json_out(os, "\"counter-value\" : \"%s\"", bad_count);
773
774 if (evsel->unit)
775 json_out(os, "\"unit\" : \"%s\"", evsel->unit);
776
777 json_out(os, "\"event\" : \"%s\"", evsel__name(evsel));
778 }
779
print_counter_value(struct perf_stat_config * config,struct outstate * os,struct evsel * evsel,double avg,bool ok)780 static void print_counter_value(struct perf_stat_config *config, struct outstate *os,
781 struct evsel *evsel, double avg, bool ok)
782 {
783 if (config->json_output)
784 print_counter_value_json(os, evsel, avg, ok);
785 else if (config->csv_output)
786 print_counter_value_csv(config, evsel, avg, ok);
787 else
788 print_counter_value_std(config, evsel, avg, ok);
789 }
790
abs_printout(struct perf_stat_config * config,struct outstate * os,struct aggr_cpu_id id,int aggr_nr,struct evsel * evsel,double avg,bool ok)791 static void abs_printout(struct perf_stat_config *config,
792 struct outstate *os,
793 struct aggr_cpu_id id, int aggr_nr,
794 struct evsel *evsel, double avg, bool ok)
795 {
796 aggr_printout(config, os, evsel, id, aggr_nr);
797 print_counter_value(config, os, evsel, avg, ok);
798 print_cgroup(config, os, evsel->cgrp);
799 }
800
evlist__has_hybrid_pmus(struct evlist * evlist)801 static bool evlist__has_hybrid_pmus(struct evlist *evlist)
802 {
803 struct evsel *evsel;
804 struct perf_pmu *last_core_pmu = NULL;
805
806 if (perf_pmus__num_core_pmus() == 1)
807 return false;
808
809 evlist__for_each_entry(evlist, evsel) {
810 if (evsel->core.is_pmu_core) {
811 struct perf_pmu *pmu = evsel__find_pmu(evsel);
812
813 if (pmu == last_core_pmu)
814 continue;
815
816 if (last_core_pmu == NULL) {
817 last_core_pmu = pmu;
818 continue;
819 }
820 /* A distinct core PMU. */
821 return true;
822 }
823 }
824
825 return false;
826 }
827
printout(struct perf_stat_config * config,struct outstate * os,double uval,u64 run,u64 ena,double noise,int aggr_idx)828 static void printout(struct perf_stat_config *config, struct outstate *os,
829 double uval, u64 run, u64 ena, double noise, int aggr_idx)
830 {
831 struct perf_stat_output_ctx out;
832 print_metric_t pm;
833 new_line_t nl;
834 print_metricgroup_header_t pmh;
835 bool ok = true;
836 struct evsel *counter = os->evsel;
837
838 if (config->csv_output) {
839 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
840 nl = config->metric_only ? NULL : new_line_csv;
841 pmh = print_metricgroup_header_csv;
842 os->csv_col_pad = 4 + (counter->cgrp ? 1 : 0);
843 } else if (config->json_output) {
844 pm = config->metric_only ? print_metric_only_json : print_metric_json;
845 nl = config->metric_only ? NULL : new_line_json;
846 pmh = print_metricgroup_header_json;
847 } else {
848 pm = config->metric_only ? print_metric_only : print_metric_std;
849 nl = config->metric_only ? NULL : new_line_std;
850 pmh = print_metricgroup_header_std;
851 }
852
853 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
854 if (config->metric_only) {
855 pm(config, os, METRIC_THRESHOLD_UNKNOWN, /*format=*/NULL,
856 /*unit=*/NULL, /*val=*/0);
857 return;
858 }
859
860 ok = false;
861
862 if (counter->supported) {
863 if (!evlist__has_hybrid_pmus(counter->evlist)) {
864 config->print_free_counters_hint = 1;
865 }
866 }
867 }
868
869 out.print_metric = pm;
870 out.new_line = nl;
871 out.print_metricgroup_header = pmh;
872 out.ctx = os;
873 out.force_header = false;
874
875 if (!config->metric_only && !counter->default_metricgroup) {
876 abs_printout(config, os, os->id, os->aggr_nr, counter, uval, ok);
877
878 print_noise(config, os, counter, noise, /*before_metric=*/true);
879 print_running(config, os, run, ena, /*before_metric=*/true);
880 }
881
882 if (ok) {
883 if (!config->metric_only && counter->default_metricgroup) {
884 void *from = NULL;
885
886 aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
887 /* Print out all the metricgroup with the same metric event. */
888 do {
889 int num = 0;
890
891 /* Print out the new line for the next new metricgroup. */
892 if (from) {
893 if (config->json_output)
894 new_line_json(config, (void *)os);
895 else
896 __new_line_std_csv(config, os);
897 }
898
899 print_noise(config, os, counter, noise, /*before_metric=*/true);
900 print_running(config, os, run, ena, /*before_metric=*/true);
901 from = perf_stat__print_shadow_stats_metricgroup(config, counter, aggr_idx,
902 &num, from, &out);
903 } while (from != NULL);
904 } else {
905 perf_stat__print_shadow_stats(config, counter, uval, aggr_idx, &out);
906 }
907 } else {
908 pm(config, os, METRIC_THRESHOLD_UNKNOWN, /*format=*/NULL, /*unit=*/NULL, /*val=*/0);
909 }
910
911 if (!config->metric_only) {
912 print_noise(config, os, counter, noise, /*before_metric=*/false);
913 print_running(config, os, run, ena, /*before_metric=*/false);
914 }
915 }
916
917 /**
918 * should_skip_zero_count() - Check if the event should print 0 values.
919 * @config: The perf stat configuration (including aggregation mode).
920 * @counter: The evsel with its associated cpumap.
921 * @id: The aggregation id that is being queried.
922 *
923 * Due to mismatch between the event cpumap or thread-map and the
924 * aggregation mode, sometimes it'd iterate the counter with the map
925 * which does not contain any values.
926 *
927 * For example, uncore events have dedicated CPUs to manage them,
928 * result for other CPUs should be zero and skipped.
929 *
930 * Return: %true if the value should NOT be printed, %false if the value
931 * needs to be printed like "<not counted>" or "<not supported>".
932 */
should_skip_zero_counter(struct perf_stat_config * config,struct evsel * counter,const struct aggr_cpu_id * id)933 static bool should_skip_zero_counter(struct perf_stat_config *config,
934 struct evsel *counter,
935 const struct aggr_cpu_id *id)
936 {
937 struct perf_cpu cpu;
938 int idx;
939
940 /*
941 * Skip unsupported default events when not verbose. (default events
942 * are all marked 'skippable').
943 */
944 if (verbose == 0 && counter->skippable && !counter->supported)
945 return true;
946
947 /*
948 * Skip value 0 when enabling --per-thread globally,
949 * otherwise it will have too many 0 output.
950 */
951 if (config->aggr_mode == AGGR_THREAD && config->system_wide)
952 return true;
953
954 /*
955 * In per-thread mode the aggr_map and aggr_get_id functions may be
956 * NULL, assume all 0 values should be output in that case.
957 */
958 if (!config->aggr_map || !config->aggr_get_id)
959 return false;
960
961 /*
962 * Tool events may be gathered on all logical CPUs, for example
963 * system_time, but for many the first index is the only one used, for
964 * example num_cores. Don't skip for the first index.
965 */
966 if (evsel__is_tool(counter)) {
967 struct aggr_cpu_id own_id =
968 config->aggr_get_id(config, (struct perf_cpu){ .cpu = 0 });
969
970 return !aggr_cpu_id__equal(id, &own_id);
971 }
972 /*
973 * Skip value 0 when the counter's cpumask doesn't match the given aggr
974 * id.
975 */
976
977 perf_cpu_map__for_each_cpu(cpu, idx, counter->core.cpus) {
978 struct aggr_cpu_id own_id = config->aggr_get_id(config, cpu);
979
980 if (aggr_cpu_id__equal(id, &own_id))
981 return false;
982 }
983 return true;
984 }
985
print_counter_aggrdata(struct perf_stat_config * config,struct evsel * counter,int aggr_idx,struct outstate * os)986 static void print_counter_aggrdata(struct perf_stat_config *config,
987 struct evsel *counter, int aggr_idx,
988 struct outstate *os)
989 {
990 FILE *output = config->output;
991 u64 ena, run, val;
992 double uval;
993 struct perf_stat_evsel *ps = counter->stats;
994 struct perf_stat_aggr *aggr = &ps->aggr[aggr_idx];
995 struct aggr_cpu_id id = config->aggr_map->map[aggr_idx];
996 double avg = aggr->counts.val;
997 bool metric_only = config->metric_only;
998
999 os->id = id;
1000 os->aggr_nr = aggr->nr;
1001 os->evsel = counter;
1002
1003 /* Skip already merged uncore/hybrid events */
1004 if (config->aggr_mode != AGGR_NONE) {
1005 if (evsel__is_hybrid(counter)) {
1006 if (config->hybrid_merge && counter->first_wildcard_match != NULL)
1007 return;
1008 } else {
1009 if (counter->first_wildcard_match != NULL)
1010 return;
1011 }
1012 }
1013
1014 val = aggr->counts.val;
1015 ena = aggr->counts.ena;
1016 run = aggr->counts.run;
1017
1018 if (perf_stat__skip_metric_event(counter, ena, run))
1019 return;
1020
1021 if (val == 0 && should_skip_zero_counter(config, counter, &id))
1022 return;
1023
1024 if (!metric_only) {
1025 if (config->json_output) {
1026 os->first = true;
1027 fputc('{', output);
1028 }
1029 if (config->interval) {
1030 if (config->json_output)
1031 json_out(os, "%s", os->timestamp);
1032 else
1033 fprintf(output, "%s", os->timestamp);
1034 } else if (config->summary && config->csv_output &&
1035 !config->no_csv_summary)
1036 fprintf(output, "%s%s", "summary", config->csv_sep);
1037 }
1038
1039 uval = val * counter->scale;
1040
1041 printout(config, os, uval, run, ena, avg, aggr_idx);
1042
1043 if (!metric_only)
1044 fputc('\n', output);
1045 }
1046
print_metric_begin(struct perf_stat_config * config,struct evlist * evlist,struct outstate * os,int aggr_idx)1047 static void print_metric_begin(struct perf_stat_config *config,
1048 struct evlist *evlist,
1049 struct outstate *os, int aggr_idx)
1050 {
1051 struct perf_stat_aggr *aggr;
1052 struct aggr_cpu_id id;
1053 struct evsel *evsel;
1054
1055 os->first = true;
1056 if (!config->metric_only)
1057 return;
1058
1059 if (config->json_output)
1060 fputc('{', config->output);
1061
1062 if (config->interval) {
1063 if (config->json_output)
1064 json_out(os, "%s", os->timestamp);
1065 else
1066 fprintf(config->output, "%s", os->timestamp);
1067 }
1068 evsel = evlist__first(evlist);
1069 id = config->aggr_map->map[aggr_idx];
1070 aggr = &evsel->stats->aggr[aggr_idx];
1071 aggr_printout(config, os, evsel, id, aggr->nr);
1072
1073 print_cgroup(config, os, os->cgrp ? : evsel->cgrp);
1074 }
1075
print_metric_end(struct perf_stat_config * config,struct outstate * os)1076 static void print_metric_end(struct perf_stat_config *config, struct outstate *os)
1077 {
1078 FILE *output = config->output;
1079
1080 if (!config->metric_only)
1081 return;
1082
1083 if (config->json_output) {
1084 if (os->first)
1085 fputs("\"metric-value\" : \"none\"", output);
1086 fputc('}', output);
1087 }
1088 fputc('\n', output);
1089 }
1090
print_aggr(struct perf_stat_config * config,struct evlist * evlist,struct outstate * os)1091 static void print_aggr(struct perf_stat_config *config,
1092 struct evlist *evlist,
1093 struct outstate *os)
1094 {
1095 struct evsel *counter;
1096 int aggr_idx;
1097
1098 if (!config->aggr_map || !config->aggr_get_id)
1099 return;
1100
1101 /*
1102 * With metric_only everything is on a single line.
1103 * Without each counter has its own line.
1104 */
1105 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1106 print_metric_begin(config, evlist, os, aggr_idx);
1107
1108 evlist__for_each_entry(evlist, counter) {
1109 print_counter_aggrdata(config, counter, aggr_idx, os);
1110 }
1111 print_metric_end(config, os);
1112 }
1113 }
1114
print_aggr_cgroup(struct perf_stat_config * config,struct evlist * evlist,struct outstate * os)1115 static void print_aggr_cgroup(struct perf_stat_config *config,
1116 struct evlist *evlist,
1117 struct outstate *os)
1118 {
1119 struct evsel *counter, *evsel;
1120 int aggr_idx;
1121
1122 if (!config->aggr_map || !config->aggr_get_id)
1123 return;
1124
1125 evlist__for_each_entry(evlist, evsel) {
1126 if (os->cgrp == evsel->cgrp)
1127 continue;
1128
1129 os->cgrp = evsel->cgrp;
1130
1131 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1132 print_metric_begin(config, evlist, os, aggr_idx);
1133
1134 evlist__for_each_entry(evlist, counter) {
1135 if (counter->cgrp != os->cgrp)
1136 continue;
1137
1138 print_counter_aggrdata(config, counter, aggr_idx, os);
1139 }
1140 print_metric_end(config, os);
1141 }
1142 }
1143 }
1144
print_counter(struct perf_stat_config * config,struct evsel * counter,struct outstate * os)1145 static void print_counter(struct perf_stat_config *config,
1146 struct evsel *counter, struct outstate *os)
1147 {
1148 int aggr_idx;
1149
1150 /* AGGR_THREAD doesn't have config->aggr_get_id */
1151 if (!config->aggr_map)
1152 return;
1153
1154 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1155 print_counter_aggrdata(config, counter, aggr_idx, os);
1156 }
1157 }
1158
print_no_aggr_metric(struct perf_stat_config * config,struct evlist * evlist,struct outstate * os)1159 static void print_no_aggr_metric(struct perf_stat_config *config,
1160 struct evlist *evlist,
1161 struct outstate *os)
1162 {
1163 int all_idx;
1164 struct perf_cpu cpu;
1165
1166 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1167 struct evsel *counter;
1168 bool first = true;
1169
1170 evlist__for_each_entry(evlist, counter) {
1171 u64 ena, run, val;
1172 double uval;
1173 struct perf_stat_evsel *ps = counter->stats;
1174 int aggr_idx = 0;
1175
1176 if (!perf_cpu_map__has(evsel__cpus(counter), cpu))
1177 continue;
1178
1179 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1180 if (config->aggr_map->map[aggr_idx].cpu.cpu == cpu.cpu)
1181 break;
1182 }
1183
1184 os->evsel = counter;
1185 os->id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1186 if (first) {
1187 print_metric_begin(config, evlist, os, aggr_idx);
1188 first = false;
1189 }
1190 val = ps->aggr[aggr_idx].counts.val;
1191 ena = ps->aggr[aggr_idx].counts.ena;
1192 run = ps->aggr[aggr_idx].counts.run;
1193
1194 uval = val * counter->scale;
1195 printout(config, os, uval, run, ena, 1.0, aggr_idx);
1196 }
1197 if (!first)
1198 print_metric_end(config, os);
1199 }
1200 }
1201
print_metric_headers_std(struct perf_stat_config * config,bool no_indent)1202 static void print_metric_headers_std(struct perf_stat_config *config,
1203 bool no_indent)
1204 {
1205 fputc(' ', config->output);
1206
1207 if (!no_indent) {
1208 int len = aggr_header_lens[config->aggr_mode];
1209
1210 if (nr_cgroups || config->cgroup_list)
1211 len += CGROUP_LEN + 1;
1212
1213 fprintf(config->output, "%*s", len, "");
1214 }
1215 }
1216
print_metric_headers_csv(struct perf_stat_config * config,bool no_indent __maybe_unused)1217 static void print_metric_headers_csv(struct perf_stat_config *config,
1218 bool no_indent __maybe_unused)
1219 {
1220 const char *p;
1221
1222 if (config->interval)
1223 fprintf(config->output, "time%s", config->csv_sep);
1224 if (config->iostat_run)
1225 return;
1226
1227 p = aggr_header_csv[config->aggr_mode];
1228 while (*p) {
1229 if (*p == ',')
1230 fputs(config->csv_sep, config->output);
1231 else
1232 fputc(*p, config->output);
1233 p++;
1234 }
1235 }
1236
print_metric_headers_json(struct perf_stat_config * config __maybe_unused,bool no_indent __maybe_unused)1237 static void print_metric_headers_json(struct perf_stat_config *config __maybe_unused,
1238 bool no_indent __maybe_unused)
1239 {
1240 }
1241
print_metric_headers(struct perf_stat_config * config,struct evlist * evlist,bool no_indent)1242 static void print_metric_headers(struct perf_stat_config *config,
1243 struct evlist *evlist, bool no_indent)
1244 {
1245 struct evsel *counter;
1246 struct outstate os = {
1247 .fh = config->output
1248 };
1249 struct perf_stat_output_ctx out = {
1250 .ctx = &os,
1251 .print_metric = print_metric_header,
1252 .new_line = NULL,
1253 .force_header = true,
1254 };
1255
1256 if (config->json_output)
1257 print_metric_headers_json(config, no_indent);
1258 else if (config->csv_output)
1259 print_metric_headers_csv(config, no_indent);
1260 else
1261 print_metric_headers_std(config, no_indent);
1262
1263 if (config->iostat_run)
1264 iostat_print_header_prefix(config);
1265
1266 if (config->cgroup_list)
1267 os.cgrp = evlist__first(evlist)->cgrp;
1268
1269 /* Print metrics headers only */
1270 evlist__for_each_entry(evlist, counter) {
1271 if (!config->iostat_run &&
1272 config->aggr_mode != AGGR_NONE && counter->metric_leader != counter)
1273 continue;
1274
1275 os.evsel = counter;
1276
1277 perf_stat__print_shadow_stats(config, counter, 0, 0, &out);
1278 }
1279
1280 if (!config->json_output)
1281 fputc('\n', config->output);
1282 }
1283
prepare_timestamp(struct perf_stat_config * config,struct outstate * os,struct timespec * ts)1284 static void prepare_timestamp(struct perf_stat_config *config,
1285 struct outstate *os, struct timespec *ts)
1286 {
1287 if (config->iostat_run)
1288 return;
1289
1290 if (config->json_output)
1291 scnprintf(os->timestamp, sizeof(os->timestamp), "\"interval\" : %lu.%09lu",
1292 (unsigned long) ts->tv_sec, ts->tv_nsec);
1293 else if (config->csv_output)
1294 scnprintf(os->timestamp, sizeof(os->timestamp), "%lu.%09lu%s",
1295 (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1296 else
1297 scnprintf(os->timestamp, sizeof(os->timestamp), "%6lu.%09lu ",
1298 (unsigned long) ts->tv_sec, ts->tv_nsec);
1299 }
1300
print_header_interval_std(struct perf_stat_config * config,struct target * _target __maybe_unused,struct evlist * evlist,int argc __maybe_unused,const char ** argv __maybe_unused)1301 static void print_header_interval_std(struct perf_stat_config *config,
1302 struct target *_target __maybe_unused,
1303 struct evlist *evlist,
1304 int argc __maybe_unused,
1305 const char **argv __maybe_unused)
1306 {
1307 FILE *output = config->output;
1308
1309 switch (config->aggr_mode) {
1310 case AGGR_NODE:
1311 case AGGR_SOCKET:
1312 case AGGR_DIE:
1313 case AGGR_CLUSTER:
1314 case AGGR_CACHE:
1315 case AGGR_CORE:
1316 fprintf(output, "#%*s %-*s ctrs",
1317 INTERVAL_LEN - 1, "time",
1318 aggr_header_lens[config->aggr_mode],
1319 aggr_header_std[config->aggr_mode]);
1320 break;
1321 case AGGR_NONE:
1322 fprintf(output, "#%*s %-*s",
1323 INTERVAL_LEN - 1, "time",
1324 aggr_header_lens[config->aggr_mode],
1325 aggr_header_std[config->aggr_mode]);
1326 break;
1327 case AGGR_THREAD:
1328 fprintf(output, "#%*s %*s-%-*s",
1329 INTERVAL_LEN - 1, "time",
1330 COMM_LEN, "comm", PID_LEN, "pid");
1331 break;
1332 case AGGR_GLOBAL:
1333 default:
1334 if (!config->iostat_run)
1335 fprintf(output, "#%*s",
1336 INTERVAL_LEN - 1, "time");
1337 case AGGR_UNSET:
1338 case AGGR_MAX:
1339 break;
1340 }
1341
1342 if (config->metric_only)
1343 print_metric_headers(config, evlist, true);
1344 else
1345 fprintf(output, " %*s %*s events\n",
1346 COUNTS_LEN, "counts", config->unit_width, "unit");
1347 }
1348
print_header_std(struct perf_stat_config * config,struct target * _target,struct evlist * evlist,int argc,const char ** argv)1349 static void print_header_std(struct perf_stat_config *config,
1350 struct target *_target, struct evlist *evlist,
1351 int argc, const char **argv)
1352 {
1353 FILE *output = config->output;
1354 int i;
1355
1356 fprintf(output, "\n");
1357 fprintf(output, " Performance counter stats for ");
1358 if (_target->bpf_str)
1359 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1360 else if (_target->system_wide)
1361 fprintf(output, "\'system wide");
1362 else if (_target->cpu_list)
1363 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1364 else if (!target__has_task(_target)) {
1365 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1366 for (i = 1; argv && (i < argc); i++)
1367 fprintf(output, " %s", argv[i]);
1368 } else if (_target->pid)
1369 fprintf(output, "process id \'%s", _target->pid);
1370 else
1371 fprintf(output, "thread id \'%s", _target->tid);
1372
1373 fprintf(output, "\'");
1374 if (config->run_count > 1)
1375 fprintf(output, " (%d runs)", config->run_count);
1376 fprintf(output, ":\n\n");
1377
1378 if (config->metric_only)
1379 print_metric_headers(config, evlist, false);
1380 }
1381
print_header_csv(struct perf_stat_config * config,struct target * _target __maybe_unused,struct evlist * evlist,int argc __maybe_unused,const char ** argv __maybe_unused)1382 static void print_header_csv(struct perf_stat_config *config,
1383 struct target *_target __maybe_unused,
1384 struct evlist *evlist,
1385 int argc __maybe_unused,
1386 const char **argv __maybe_unused)
1387 {
1388 if (config->metric_only)
1389 print_metric_headers(config, evlist, true);
1390 }
print_header_json(struct perf_stat_config * config,struct target * _target __maybe_unused,struct evlist * evlist,int argc __maybe_unused,const char ** argv __maybe_unused)1391 static void print_header_json(struct perf_stat_config *config,
1392 struct target *_target __maybe_unused,
1393 struct evlist *evlist,
1394 int argc __maybe_unused,
1395 const char **argv __maybe_unused)
1396 {
1397 if (config->metric_only)
1398 print_metric_headers(config, evlist, true);
1399 }
1400
print_header(struct perf_stat_config * config,struct target * _target,struct evlist * evlist,int argc,const char ** argv)1401 static void print_header(struct perf_stat_config *config,
1402 struct target *_target,
1403 struct evlist *evlist,
1404 int argc, const char **argv)
1405 {
1406 static int num_print_iv;
1407
1408 fflush(stdout);
1409
1410 if (config->interval_clear)
1411 puts(CONSOLE_CLEAR);
1412
1413 if (num_print_iv == 0 || config->interval_clear) {
1414 if (config->json_output)
1415 print_header_json(config, _target, evlist, argc, argv);
1416 else if (config->csv_output)
1417 print_header_csv(config, _target, evlist, argc, argv);
1418 else if (config->interval)
1419 print_header_interval_std(config, _target, evlist, argc, argv);
1420 else
1421 print_header_std(config, _target, evlist, argc, argv);
1422 }
1423
1424 if (num_print_iv++ == 25)
1425 num_print_iv = 0;
1426 }
1427
get_precision(double num)1428 static int get_precision(double num)
1429 {
1430 if (num > 1)
1431 return 0;
1432
1433 return lround(ceil(-log10(num)));
1434 }
1435
print_table(struct perf_stat_config * config,FILE * output,int precision,double avg)1436 static void print_table(struct perf_stat_config *config,
1437 FILE *output, int precision, double avg)
1438 {
1439 char tmp[64];
1440 int idx, indent = 0;
1441
1442 scnprintf(tmp, 64, " %17.*f", precision, avg);
1443 while (tmp[indent] == ' ')
1444 indent++;
1445
1446 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1447
1448 for (idx = 0; idx < config->run_count; idx++) {
1449 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1450 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1451
1452 fprintf(output, " %17.*f (%+.*f) ",
1453 precision, run, precision, run - avg);
1454
1455 for (h = 0; h < n; h++)
1456 fprintf(output, "#");
1457
1458 fprintf(output, "\n");
1459 }
1460
1461 fprintf(output, "\n%*s# Final result:\n", indent, "");
1462 }
1463
timeval2double(struct timeval * t)1464 static double timeval2double(struct timeval *t)
1465 {
1466 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1467 }
1468
print_footer(struct perf_stat_config * config)1469 static void print_footer(struct perf_stat_config *config)
1470 {
1471 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1472 FILE *output = config->output;
1473
1474 if (config->interval || config->csv_output || config->json_output)
1475 return;
1476
1477 if (!config->null_run)
1478 fprintf(output, "\n");
1479
1480 if (config->run_count == 1) {
1481 fprintf(output, " %17.9f seconds time elapsed", avg);
1482
1483 if (config->ru_display) {
1484 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1485 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1486
1487 fprintf(output, "\n\n");
1488 fprintf(output, " %17.9f seconds user\n", ru_utime);
1489 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1490 }
1491 } else {
1492 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1493 /*
1494 * Display at most 2 more significant
1495 * digits than the stddev inaccuracy.
1496 */
1497 int precision = get_precision(sd) + 2;
1498
1499 if (config->walltime_run_table)
1500 print_table(config, output, precision, avg);
1501
1502 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1503 precision, avg, precision, sd);
1504
1505 print_noise_pct(config, NULL, sd, avg, /*before_metric=*/false);
1506 }
1507 fprintf(output, "\n\n");
1508
1509 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1510 fprintf(output,
1511 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1512 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1513 " perf stat ...\n"
1514 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1515 }
1516
print_percore(struct perf_stat_config * config,struct evsel * counter,struct outstate * os)1517 static void print_percore(struct perf_stat_config *config,
1518 struct evsel *counter, struct outstate *os)
1519 {
1520 bool metric_only = config->metric_only;
1521 FILE *output = config->output;
1522 struct cpu_aggr_map *core_map;
1523 int aggr_idx, core_map_len = 0;
1524
1525 if (!config->aggr_map || !config->aggr_get_id)
1526 return;
1527
1528 if (config->percore_show_thread)
1529 return print_counter(config, counter, os);
1530
1531 /*
1532 * core_map will hold the aggr_cpu_id for the cores that have been
1533 * printed so that each core is printed just once.
1534 */
1535 core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1536 if (core_map == NULL) {
1537 fprintf(output, "Cannot allocate per-core aggr map for display\n");
1538 return;
1539 }
1540
1541 cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1542 struct perf_cpu curr_cpu = config->aggr_map->map[aggr_idx].cpu;
1543 struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1544 bool found = false;
1545
1546 for (int i = 0; i < core_map_len; i++) {
1547 if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1548 found = true;
1549 break;
1550 }
1551 }
1552 if (found)
1553 continue;
1554
1555 print_counter_aggrdata(config, counter, aggr_idx, os);
1556
1557 core_map->map[core_map_len++] = core_id;
1558 }
1559 free(core_map);
1560
1561 if (metric_only)
1562 fputc('\n', output);
1563 }
1564
print_cgroup_counter(struct perf_stat_config * config,struct evlist * evlist,struct outstate * os)1565 static void print_cgroup_counter(struct perf_stat_config *config, struct evlist *evlist,
1566 struct outstate *os)
1567 {
1568 struct evsel *counter;
1569
1570 evlist__for_each_entry(evlist, counter) {
1571 if (os->cgrp != counter->cgrp) {
1572 if (os->cgrp != NULL)
1573 print_metric_end(config, os);
1574
1575 os->cgrp = counter->cgrp;
1576 print_metric_begin(config, evlist, os, /*aggr_idx=*/0);
1577 }
1578
1579 print_counter(config, counter, os);
1580 }
1581 if (os->cgrp)
1582 print_metric_end(config, os);
1583 }
1584
evlist__print_counters(struct evlist * evlist,struct perf_stat_config * config,struct target * _target,struct timespec * ts,int argc,const char ** argv)1585 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1586 struct target *_target, struct timespec *ts,
1587 int argc, const char **argv)
1588 {
1589 bool metric_only = config->metric_only;
1590 struct evsel *counter;
1591 struct outstate os = {
1592 .fh = config->output,
1593 .first = true,
1594 };
1595
1596 evlist__uniquify_evsel_names(evlist, config);
1597
1598 if (config->iostat_run)
1599 evlist->selected = evlist__first(evlist);
1600
1601 if (config->interval)
1602 prepare_timestamp(config, &os, ts);
1603
1604 print_header(config, _target, evlist, argc, argv);
1605
1606 switch (config->aggr_mode) {
1607 case AGGR_CORE:
1608 case AGGR_CACHE:
1609 case AGGR_CLUSTER:
1610 case AGGR_DIE:
1611 case AGGR_SOCKET:
1612 case AGGR_NODE:
1613 if (config->cgroup_list)
1614 print_aggr_cgroup(config, evlist, &os);
1615 else
1616 print_aggr(config, evlist, &os);
1617 break;
1618 case AGGR_THREAD:
1619 case AGGR_GLOBAL:
1620 if (config->iostat_run) {
1621 iostat_print_counters(evlist, config, ts, os.timestamp,
1622 (iostat_print_counter_t)print_counter, &os);
1623 } else if (config->cgroup_list) {
1624 print_cgroup_counter(config, evlist, &os);
1625 } else {
1626 print_metric_begin(config, evlist, &os, /*aggr_idx=*/0);
1627 evlist__for_each_entry(evlist, counter) {
1628 print_counter(config, counter, &os);
1629 }
1630 print_metric_end(config, &os);
1631 }
1632 break;
1633 case AGGR_NONE:
1634 if (metric_only)
1635 print_no_aggr_metric(config, evlist, &os);
1636 else {
1637 evlist__for_each_entry(evlist, counter) {
1638 if (counter->percore)
1639 print_percore(config, counter, &os);
1640 else
1641 print_counter(config, counter, &os);
1642 }
1643 }
1644 break;
1645 case AGGR_MAX:
1646 case AGGR_UNSET:
1647 default:
1648 break;
1649 }
1650
1651 print_footer(config);
1652
1653 fflush(config->output);
1654 }
1655