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 "evlist.h"
11 #include "evsel.h"
12 #include "stat.h"
13 #include "top.h"
14 #include "thread_map.h"
15 #include "cpumap.h"
16 #include "string2.h"
17 #include <linux/ctype.h>
18 #include "cgroup.h"
19 #include <api/fs/fs.h>
20 #include "util.h"
21 #include "iostat.h"
22 #include "pmu-hybrid.h"
23 #include "evlist-hybrid.h"
24
25 #define CNTR_NOT_SUPPORTED "<not supported>"
26 #define CNTR_NOT_COUNTED "<not counted>"
27
print_running(struct perf_stat_config * config,u64 run,u64 ena)28 static void print_running(struct perf_stat_config *config,
29 u64 run, u64 ena)
30 {
31
32 double enabled_percent = 100;
33
34 if (run != ena)
35 enabled_percent = 100 * run / ena;
36 if (config->json_output)
37 fprintf(config->output,
38 "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f, ",
39 run, enabled_percent);
40 else if (config->csv_output)
41 fprintf(config->output,
42 "%s%" PRIu64 "%s%.2f", config->csv_sep,
43 run, config->csv_sep, enabled_percent);
44 else if (run != ena)
45 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
46 }
47
print_noise_pct(struct perf_stat_config * config,double total,double avg)48 static void print_noise_pct(struct perf_stat_config *config,
49 double total, double avg)
50 {
51 double pct = rel_stddev_stats(total, avg);
52
53 if (config->json_output)
54 fprintf(config->output, "\"variance\" : %.2f, ", pct);
55 else if (config->csv_output)
56 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
57 else if (pct)
58 fprintf(config->output, " ( +-%6.2f%% )", pct);
59 }
60
print_noise(struct perf_stat_config * config,struct evsel * evsel,double avg)61 static void print_noise(struct perf_stat_config *config,
62 struct evsel *evsel, double avg)
63 {
64 struct perf_stat_evsel *ps;
65
66 if (config->run_count == 1)
67 return;
68
69 ps = evsel->stats;
70 print_noise_pct(config, stddev_stats(&ps->res_stats), avg);
71 }
72
print_cgroup(struct perf_stat_config * config,struct evsel * evsel)73 static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
74 {
75 if (nr_cgroups) {
76 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
77
78 if (config->json_output)
79 fprintf(config->output, "\"cgroup\" : \"%s\", ", cgrp_name);
80 else
81 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
82 }
83 }
84
85
aggr_printout(struct perf_stat_config * config,struct evsel * evsel,struct aggr_cpu_id id,int nr)86 static void aggr_printout(struct perf_stat_config *config,
87 struct evsel *evsel, struct aggr_cpu_id id, int nr)
88 {
89
90
91 if (config->json_output && !config->interval)
92 fprintf(config->output, "{");
93
94 switch (config->aggr_mode) {
95 case AGGR_CORE:
96 if (config->json_output) {
97 fprintf(config->output,
98 "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
99 id.socket,
100 id.die,
101 id.core,
102 nr);
103 } else {
104 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
105 id.socket,
106 id.die,
107 config->csv_output ? 0 : -8,
108 id.core,
109 config->csv_sep,
110 config->csv_output ? 0 : 4,
111 nr,
112 config->csv_sep);
113 }
114 break;
115 case AGGR_DIE:
116 if (config->json_output) {
117 fprintf(config->output,
118 "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
119 id.socket,
120 id.die,
121 nr);
122 } else {
123 fprintf(config->output, "S%d-D%*d%s%*d%s",
124 id.socket,
125 config->csv_output ? 0 : -8,
126 id.die,
127 config->csv_sep,
128 config->csv_output ? 0 : 4,
129 nr,
130 config->csv_sep);
131 }
132 break;
133 case AGGR_SOCKET:
134 if (config->json_output) {
135 fprintf(config->output,
136 "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
137 id.socket,
138 nr);
139 } else {
140 fprintf(config->output, "S%*d%s%*d%s",
141 config->csv_output ? 0 : -5,
142 id.socket,
143 config->csv_sep,
144 config->csv_output ? 0 : 4,
145 nr,
146 config->csv_sep);
147 }
148 break;
149 case AGGR_NODE:
150 if (config->json_output) {
151 fprintf(config->output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
152 id.node,
153 nr);
154 } else {
155 fprintf(config->output, "N%*d%s%*d%s",
156 config->csv_output ? 0 : -5,
157 id.node,
158 config->csv_sep,
159 config->csv_output ? 0 : 4,
160 nr,
161 config->csv_sep);
162 }
163 break;
164 case AGGR_NONE:
165 if (config->json_output) {
166 if (evsel->percore && !config->percore_show_thread) {
167 fprintf(config->output, "\"core\" : \"S%d-D%d-C%d\"",
168 id.socket,
169 id.die,
170 id.core);
171 } else if (id.cpu.cpu > -1) {
172 fprintf(config->output, "\"cpu\" : \"%d\", ",
173 id.cpu.cpu);
174 }
175 } else {
176 if (evsel->percore && !config->percore_show_thread) {
177 fprintf(config->output, "S%d-D%d-C%*d%s",
178 id.socket,
179 id.die,
180 config->csv_output ? 0 : -3,
181 id.core, config->csv_sep);
182 } else if (id.cpu.cpu > -1) {
183 fprintf(config->output, "CPU%*d%s",
184 config->csv_output ? 0 : -7,
185 id.cpu.cpu, config->csv_sep);
186 }
187 }
188 break;
189 case AGGR_THREAD:
190 if (config->json_output) {
191 fprintf(config->output, "\"thread\" : \"%s-%d\", ",
192 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
193 perf_thread_map__pid(evsel->core.threads, id.thread_idx));
194 } else {
195 fprintf(config->output, "%*s-%*d%s",
196 config->csv_output ? 0 : 16,
197 perf_thread_map__comm(evsel->core.threads, id.thread_idx),
198 config->csv_output ? 0 : -8,
199 perf_thread_map__pid(evsel->core.threads, id.thread_idx),
200 config->csv_sep);
201 }
202 break;
203 case AGGR_GLOBAL:
204 case AGGR_UNSET:
205 case AGGR_MAX:
206 default:
207 break;
208 }
209 }
210
211 struct outstate {
212 FILE *fh;
213 bool newline;
214 const char *prefix;
215 int nfields;
216 int nr;
217 struct aggr_cpu_id id;
218 struct evsel *evsel;
219 };
220
221 #define METRIC_LEN 35
222
new_line_std(struct perf_stat_config * config __maybe_unused,void * ctx)223 static void new_line_std(struct perf_stat_config *config __maybe_unused,
224 void *ctx)
225 {
226 struct outstate *os = ctx;
227
228 os->newline = true;
229 }
230
do_new_line_std(struct perf_stat_config * config,struct outstate * os)231 static void do_new_line_std(struct perf_stat_config *config,
232 struct outstate *os)
233 {
234 fputc('\n', os->fh);
235 fputs(os->prefix, os->fh);
236 aggr_printout(config, os->evsel, os->id, os->nr);
237 if (config->aggr_mode == AGGR_NONE)
238 fprintf(os->fh, " ");
239 fprintf(os->fh, " ");
240 }
241
print_metric_std(struct perf_stat_config * config,void * ctx,const char * color,const char * fmt,const char * unit,double val)242 static void print_metric_std(struct perf_stat_config *config,
243 void *ctx, const char *color, const char *fmt,
244 const char *unit, double val)
245 {
246 struct outstate *os = ctx;
247 FILE *out = os->fh;
248 int n;
249 bool newline = os->newline;
250
251 os->newline = false;
252
253 if (unit == NULL || fmt == NULL) {
254 fprintf(out, "%-*s", METRIC_LEN, "");
255 return;
256 }
257
258 if (newline)
259 do_new_line_std(config, os);
260
261 n = fprintf(out, " # ");
262 if (color)
263 n += color_fprintf(out, color, fmt, val);
264 else
265 n += fprintf(out, fmt, val);
266 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
267 }
268
new_line_csv(struct perf_stat_config * config,void * ctx)269 static void new_line_csv(struct perf_stat_config *config, void *ctx)
270 {
271 struct outstate *os = ctx;
272 int i;
273
274 fputc('\n', os->fh);
275 if (os->prefix)
276 fprintf(os->fh, "%s", os->prefix);
277 aggr_printout(config, os->evsel, os->id, os->nr);
278 for (i = 0; i < os->nfields; i++)
279 fputs(config->csv_sep, os->fh);
280 }
281
print_metric_csv(struct perf_stat_config * config __maybe_unused,void * ctx,const char * color __maybe_unused,const char * fmt,const char * unit,double val)282 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
283 void *ctx,
284 const char *color __maybe_unused,
285 const char *fmt, const char *unit, double val)
286 {
287 struct outstate *os = ctx;
288 FILE *out = os->fh;
289 char buf[64], *vals, *ends;
290
291 if (unit == NULL || fmt == NULL) {
292 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
293 return;
294 }
295 snprintf(buf, sizeof(buf), fmt, val);
296 ends = vals = skip_spaces(buf);
297 while (isdigit(*ends) || *ends == '.')
298 ends++;
299 *ends = 0;
300 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
301 }
302
print_metric_json(struct perf_stat_config * config __maybe_unused,void * ctx,const char * color __maybe_unused,const char * fmt __maybe_unused,const char * unit,double val)303 static void print_metric_json(struct perf_stat_config *config __maybe_unused,
304 void *ctx,
305 const char *color __maybe_unused,
306 const char *fmt __maybe_unused,
307 const char *unit, double val)
308 {
309 struct outstate *os = ctx;
310 FILE *out = os->fh;
311
312 fprintf(out, "\"metric-value\" : %f, ", val);
313 fprintf(out, "\"metric-unit\" : \"%s\"", unit);
314 if (!config->metric_only)
315 fprintf(out, "}");
316 }
317
new_line_json(struct perf_stat_config * config,void * ctx)318 static void new_line_json(struct perf_stat_config *config, void *ctx)
319 {
320 struct outstate *os = ctx;
321
322 fputc('\n', os->fh);
323 if (os->prefix)
324 fprintf(os->fh, "%s", os->prefix);
325 aggr_printout(config, os->evsel, os->id, os->nr);
326 }
327
328 /* Filter out some columns that don't work well in metrics only mode */
329
valid_only_metric(const char * unit)330 static bool valid_only_metric(const char *unit)
331 {
332 if (!unit)
333 return false;
334 if (strstr(unit, "/sec") ||
335 strstr(unit, "CPUs utilized"))
336 return false;
337 return true;
338 }
339
fixunit(char * buf,struct evsel * evsel,const char * unit)340 static const char *fixunit(char *buf, struct evsel *evsel,
341 const char *unit)
342 {
343 if (!strncmp(unit, "of all", 6)) {
344 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
345 unit);
346 return buf;
347 }
348 return unit;
349 }
350
print_metric_only(struct perf_stat_config * config,void * ctx,const char * color,const char * fmt,const char * unit,double val)351 static void print_metric_only(struct perf_stat_config *config,
352 void *ctx, const char *color, const char *fmt,
353 const char *unit, double val)
354 {
355 struct outstate *os = ctx;
356 FILE *out = os->fh;
357 char buf[1024], str[1024];
358 unsigned mlen = config->metric_only_len;
359
360 if (!valid_only_metric(unit))
361 return;
362 unit = fixunit(buf, os->evsel, unit);
363 if (mlen < strlen(unit))
364 mlen = strlen(unit) + 1;
365
366 if (color)
367 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
368
369 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
370 fprintf(out, "%*s ", mlen, str);
371 }
372
print_metric_only_csv(struct perf_stat_config * config __maybe_unused,void * ctx,const char * color __maybe_unused,const char * fmt,const char * unit,double val)373 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
374 void *ctx, const char *color __maybe_unused,
375 const char *fmt,
376 const char *unit, double val)
377 {
378 struct outstate *os = ctx;
379 FILE *out = os->fh;
380 char buf[64], *vals, *ends;
381 char tbuf[1024];
382
383 if (!valid_only_metric(unit))
384 return;
385 unit = fixunit(tbuf, os->evsel, unit);
386 snprintf(buf, sizeof buf, fmt, val);
387 ends = vals = skip_spaces(buf);
388 while (isdigit(*ends) || *ends == '.')
389 ends++;
390 *ends = 0;
391 fprintf(out, "%s%s", vals, config->csv_sep);
392 }
393
print_metric_only_json(struct perf_stat_config * config __maybe_unused,void * ctx,const char * color __maybe_unused,const char * fmt,const char * unit,double val)394 static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
395 void *ctx, const char *color __maybe_unused,
396 const char *fmt,
397 const char *unit, double val)
398 {
399 struct outstate *os = ctx;
400 FILE *out = os->fh;
401 char buf[64], *vals, *ends;
402 char tbuf[1024];
403
404 if (!valid_only_metric(unit))
405 return;
406 unit = fixunit(tbuf, os->evsel, unit);
407 snprintf(buf, sizeof(buf), fmt, val);
408 ends = vals = skip_spaces(buf);
409 while (isdigit(*ends) || *ends == '.')
410 ends++;
411 *ends = 0;
412 fprintf(out, "{\"metric-value\" : \"%s\"}", vals);
413 }
414
new_line_metric(struct perf_stat_config * config __maybe_unused,void * ctx __maybe_unused)415 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
416 void *ctx __maybe_unused)
417 {
418 }
419
print_metric_header(struct perf_stat_config * config,void * ctx,const char * color __maybe_unused,const char * fmt __maybe_unused,const char * unit,double val __maybe_unused)420 static void print_metric_header(struct perf_stat_config *config,
421 void *ctx, const char *color __maybe_unused,
422 const char *fmt __maybe_unused,
423 const char *unit, double val __maybe_unused)
424 {
425 struct outstate *os = ctx;
426 char tbuf[1024];
427
428 /* In case of iostat, print metric header for first root port only */
429 if (config->iostat_run &&
430 os->evsel->priv != os->evsel->evlist->selected->priv)
431 return;
432
433 if (!valid_only_metric(unit) && !config->json_output)
434 return;
435 unit = fixunit(tbuf, os->evsel, unit);
436
437 if (config->json_output)
438 fprintf(os->fh, "\"unit\" : \"%s\"", unit);
439 else if (config->csv_output)
440 fprintf(os->fh, "%s%s", unit, config->csv_sep);
441 else
442 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
443 }
444
first_shadow_map_idx(struct perf_stat_config * config,struct evsel * evsel,const struct aggr_cpu_id * id)445 static int first_shadow_map_idx(struct perf_stat_config *config,
446 struct evsel *evsel, const struct aggr_cpu_id *id)
447 {
448 struct perf_cpu_map *cpus = evsel__cpus(evsel);
449 struct perf_cpu cpu;
450 int idx;
451
452 if (config->aggr_mode == AGGR_NONE)
453 return perf_cpu_map__idx(cpus, id->cpu);
454
455 if (config->aggr_mode == AGGR_THREAD)
456 return id->thread_idx;
457
458 if (!config->aggr_get_id)
459 return 0;
460
461 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
462 struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu);
463
464 if (aggr_cpu_id__equal(&cpu_id, id))
465 return idx;
466 }
467 return 0;
468 }
469
abs_printout(struct perf_stat_config * config,struct aggr_cpu_id id,int nr,struct evsel * evsel,double avg)470 static void abs_printout(struct perf_stat_config *config,
471 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
472 {
473 FILE *output = config->output;
474 double sc = evsel->scale;
475 const char *fmt;
476
477 if (config->csv_output) {
478 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
479 } else {
480 if (config->big_num)
481 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
482 else
483 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
484 }
485
486 aggr_printout(config, evsel, id, nr);
487
488 if (config->json_output)
489 fprintf(output, "\"counter-value\" : \"%f\", ", avg);
490 else
491 fprintf(output, fmt, avg, config->csv_sep);
492
493 if (config->json_output) {
494 if (evsel->unit) {
495 fprintf(output, "\"unit\" : \"%s\", ",
496 evsel->unit);
497 }
498 } else {
499 if (evsel->unit)
500 fprintf(output, "%-*s%s",
501 config->csv_output ? 0 : config->unit_width,
502 evsel->unit, config->csv_sep);
503 }
504
505 if (config->json_output)
506 fprintf(output, "\"event\" : \"%s\", ", evsel__name(evsel));
507 else
508 fprintf(output, "%-*s", config->csv_output ? 0 : 32, evsel__name(evsel));
509
510 print_cgroup(config, evsel);
511 }
512
is_mixed_hw_group(struct evsel * counter)513 static bool is_mixed_hw_group(struct evsel *counter)
514 {
515 struct evlist *evlist = counter->evlist;
516 u32 pmu_type = counter->core.attr.type;
517 struct evsel *pos;
518
519 if (counter->core.nr_members < 2)
520 return false;
521
522 evlist__for_each_entry(evlist, pos) {
523 /* software events can be part of any hardware group */
524 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
525 continue;
526 if (pmu_type == PERF_TYPE_SOFTWARE) {
527 pmu_type = pos->core.attr.type;
528 continue;
529 }
530 if (pmu_type != pos->core.attr.type)
531 return true;
532 }
533
534 return false;
535 }
536
printout(struct perf_stat_config * config,struct aggr_cpu_id id,int nr,struct evsel * counter,double uval,char * prefix,u64 run,u64 ena,double noise,struct runtime_stat * st)537 static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
538 struct evsel *counter, double uval,
539 char *prefix, u64 run, u64 ena, double noise,
540 struct runtime_stat *st)
541 {
542 struct perf_stat_output_ctx out;
543 struct outstate os = {
544 .fh = config->output,
545 .prefix = prefix ? prefix : "",
546 .id = id,
547 .nr = nr,
548 .evsel = counter,
549 };
550 print_metric_t pm;
551 new_line_t nl;
552
553 if (config->csv_output) {
554 static const int aggr_fields[AGGR_MAX] = {
555 [AGGR_NONE] = 1,
556 [AGGR_GLOBAL] = 0,
557 [AGGR_SOCKET] = 2,
558 [AGGR_DIE] = 2,
559 [AGGR_CORE] = 2,
560 [AGGR_THREAD] = 1,
561 [AGGR_UNSET] = 0,
562 [AGGR_NODE] = 1,
563 };
564
565 pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
566 nl = config->metric_only ? new_line_metric : new_line_csv;
567 os.nfields = 3 + aggr_fields[config->aggr_mode] + (counter->cgrp ? 1 : 0);
568 } else if (config->json_output) {
569 pm = config->metric_only ? print_metric_only_json : print_metric_json;
570 nl = config->metric_only ? new_line_metric : new_line_json;
571 } else {
572 pm = config->metric_only ? print_metric_only : print_metric_std;
573 nl = config->metric_only ? new_line_metric : new_line_std;
574 }
575
576 if (!config->no_csv_summary && config->csv_output &&
577 config->summary && !config->interval) {
578 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
579 }
580
581 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
582 if (config->metric_only) {
583 pm(config, &os, NULL, "", "", 0);
584 return;
585 }
586 aggr_printout(config, counter, id, nr);
587
588 if (config->json_output) {
589 fprintf(config->output, "\"counter-value\" : \"%s\", ",
590 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED);
591 } else {
592 fprintf(config->output, "%*s%s",
593 config->csv_output ? 0 : 18,
594 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
595 config->csv_sep);
596 }
597
598 if (counter->supported) {
599 if (!evlist__has_hybrid(counter->evlist)) {
600 config->print_free_counters_hint = 1;
601 if (is_mixed_hw_group(counter))
602 config->print_mixed_hw_group_error = 1;
603 }
604 }
605
606 if (config->json_output) {
607 fprintf(config->output, "\"unit\" : \"%s\", ", counter->unit);
608 } else {
609 fprintf(config->output, "%-*s%s",
610 config->csv_output ? 0 : config->unit_width,
611 counter->unit, config->csv_sep);
612 }
613
614 if (config->json_output) {
615 fprintf(config->output, "\"event\" : \"%s\", ",
616 evsel__name(counter));
617 } else {
618 fprintf(config->output, "%*s",
619 config->csv_output ? 0 : -25, evsel__name(counter));
620 }
621
622 print_cgroup(config, counter);
623
624 if (!config->csv_output && !config->json_output)
625 pm(config, &os, NULL, NULL, "", 0);
626 print_noise(config, counter, noise);
627 print_running(config, run, ena);
628 if (config->csv_output)
629 pm(config, &os, NULL, NULL, "", 0);
630 else if (config->json_output)
631 pm(config, &os, NULL, NULL, "", 0);
632 return;
633 }
634
635 if (!config->metric_only)
636 abs_printout(config, id, nr, counter, uval);
637
638 out.print_metric = pm;
639 out.new_line = nl;
640 out.ctx = &os;
641 out.force_header = false;
642
643 if (config->csv_output && !config->metric_only) {
644 print_noise(config, counter, noise);
645 print_running(config, run, ena);
646 } else if (config->json_output && !config->metric_only) {
647 print_noise(config, counter, noise);
648 print_running(config, run, ena);
649 }
650
651 perf_stat__print_shadow_stats(config, counter, uval,
652 first_shadow_map_idx(config, counter, &id),
653 &out, &config->metric_events, st);
654 if (!config->csv_output && !config->metric_only && !config->json_output) {
655 print_noise(config, counter, noise);
656 print_running(config, run, ena);
657 }
658 }
659
aggr_update_shadow(struct perf_stat_config * config,struct evlist * evlist)660 static void aggr_update_shadow(struct perf_stat_config *config,
661 struct evlist *evlist)
662 {
663 int idx, s;
664 struct perf_cpu cpu;
665 struct aggr_cpu_id s2, id;
666 u64 val;
667 struct evsel *counter;
668 struct perf_cpu_map *cpus;
669
670 for (s = 0; s < config->aggr_map->nr; s++) {
671 id = config->aggr_map->map[s];
672 evlist__for_each_entry(evlist, counter) {
673 cpus = evsel__cpus(counter);
674 val = 0;
675 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
676 s2 = config->aggr_get_id(config, cpu);
677 if (!aggr_cpu_id__equal(&s2, &id))
678 continue;
679 val += perf_counts(counter->counts, idx, 0)->val;
680 }
681 perf_stat__update_shadow_stats(counter, val,
682 first_shadow_map_idx(config, counter, &id),
683 &rt_stat);
684 }
685 }
686 }
687
uniquify_event_name(struct evsel * counter)688 static void uniquify_event_name(struct evsel *counter)
689 {
690 char *new_name;
691 char *config;
692 int ret = 0;
693
694 if (counter->uniquified_name || counter->use_config_name ||
695 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
696 strlen(counter->pmu_name)))
697 return;
698
699 config = strchr(counter->name, '/');
700 if (config) {
701 if (asprintf(&new_name,
702 "%s%s", counter->pmu_name, config) > 0) {
703 free(counter->name);
704 counter->name = new_name;
705 }
706 } else {
707 if (perf_pmu__has_hybrid()) {
708 ret = asprintf(&new_name, "%s/%s/",
709 counter->pmu_name, counter->name);
710 } else {
711 ret = asprintf(&new_name, "%s [%s]",
712 counter->name, counter->pmu_name);
713 }
714
715 if (ret) {
716 free(counter->name);
717 counter->name = new_name;
718 }
719 }
720
721 counter->uniquified_name = true;
722 }
723
collect_all_aliases(struct perf_stat_config * config,struct evsel * counter,void (* cb)(struct perf_stat_config * config,struct evsel * counter,void * data,bool first),void * data)724 static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
725 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
726 bool first),
727 void *data)
728 {
729 struct evlist *evlist = counter->evlist;
730 struct evsel *alias;
731
732 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
733 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
734 /* Merge events with the same name, etc. but on different PMUs. */
735 if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
736 alias->scale == counter->scale &&
737 alias->cgrp == counter->cgrp &&
738 !strcmp(alias->unit, counter->unit) &&
739 evsel__is_clock(alias) == evsel__is_clock(counter) &&
740 strcmp(alias->pmu_name, counter->pmu_name)) {
741 alias->merged_stat = true;
742 cb(config, alias, data, false);
743 }
744 }
745 }
746
is_uncore(struct evsel * evsel)747 static bool is_uncore(struct evsel *evsel)
748 {
749 struct perf_pmu *pmu = evsel__find_pmu(evsel);
750
751 return pmu && pmu->is_uncore;
752 }
753
hybrid_uniquify(struct evsel * evsel)754 static bool hybrid_uniquify(struct evsel *evsel)
755 {
756 return perf_pmu__has_hybrid() && !is_uncore(evsel);
757 }
758
hybrid_merge(struct evsel * counter,struct perf_stat_config * config,bool check)759 static bool hybrid_merge(struct evsel *counter, struct perf_stat_config *config,
760 bool check)
761 {
762 if (hybrid_uniquify(counter)) {
763 if (check)
764 return config && config->hybrid_merge;
765 else
766 return config && !config->hybrid_merge;
767 }
768
769 return false;
770 }
771
collect_data(struct perf_stat_config * config,struct evsel * counter,void (* cb)(struct perf_stat_config * config,struct evsel * counter,void * data,bool first),void * data)772 static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
773 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
774 bool first),
775 void *data)
776 {
777 if (counter->merged_stat)
778 return false;
779 cb(config, counter, data, true);
780 if (config->no_merge || hybrid_merge(counter, config, false))
781 uniquify_event_name(counter);
782 else if (counter->auto_merge_stats || hybrid_merge(counter, config, true))
783 collect_all_aliases(config, counter, cb, data);
784 return true;
785 }
786
787 struct aggr_data {
788 u64 ena, run, val;
789 struct aggr_cpu_id id;
790 int nr;
791 int cpu_map_idx;
792 };
793
aggr_cb(struct perf_stat_config * config,struct evsel * counter,void * data,bool first)794 static void aggr_cb(struct perf_stat_config *config,
795 struct evsel *counter, void *data, bool first)
796 {
797 struct aggr_data *ad = data;
798 int idx;
799 struct perf_cpu cpu;
800 struct perf_cpu_map *cpus;
801 struct aggr_cpu_id s2;
802
803 cpus = evsel__cpus(counter);
804 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
805 struct perf_counts_values *counts;
806
807 s2 = config->aggr_get_id(config, cpu);
808 if (!aggr_cpu_id__equal(&s2, &ad->id))
809 continue;
810 if (first)
811 ad->nr++;
812 counts = perf_counts(counter->counts, idx, 0);
813 /*
814 * When any result is bad, make them all to give
815 * consistent output in interval mode.
816 */
817 if (counts->ena == 0 || counts->run == 0 ||
818 counter->counts->scaled == -1) {
819 ad->ena = 0;
820 ad->run = 0;
821 break;
822 }
823 ad->val += counts->val;
824 ad->ena += counts->ena;
825 ad->run += counts->run;
826 }
827 }
828
print_counter_aggrdata(struct perf_stat_config * config,struct evsel * counter,int s,char * prefix,bool metric_only,bool * first,struct perf_cpu cpu)829 static void print_counter_aggrdata(struct perf_stat_config *config,
830 struct evsel *counter, int s,
831 char *prefix, bool metric_only,
832 bool *first, struct perf_cpu cpu)
833 {
834 struct aggr_data ad;
835 FILE *output = config->output;
836 u64 ena, run, val;
837 int nr;
838 struct aggr_cpu_id id;
839 double uval;
840
841 ad.id = id = config->aggr_map->map[s];
842 ad.val = ad.ena = ad.run = 0;
843 ad.nr = 0;
844 if (!collect_data(config, counter, aggr_cb, &ad))
845 return;
846
847 if (perf_pmu__has_hybrid() && ad.ena == 0)
848 return;
849
850 nr = ad.nr;
851 ena = ad.ena;
852 run = ad.run;
853 val = ad.val;
854 if (*first && metric_only) {
855 *first = false;
856 aggr_printout(config, counter, id, nr);
857 }
858 if (prefix && !metric_only)
859 fprintf(output, "%s", prefix);
860
861 uval = val * counter->scale;
862 if (cpu.cpu != -1)
863 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
864
865 printout(config, id, nr, counter, uval,
866 prefix, run, ena, 1.0, &rt_stat);
867 if (!metric_only)
868 fputc('\n', output);
869 }
870
print_aggr(struct perf_stat_config * config,struct evlist * evlist,char * prefix)871 static void print_aggr(struct perf_stat_config *config,
872 struct evlist *evlist,
873 char *prefix)
874 {
875 bool metric_only = config->metric_only;
876 FILE *output = config->output;
877 struct evsel *counter;
878 int s;
879 bool first;
880
881 if (!config->aggr_map || !config->aggr_get_id)
882 return;
883
884 aggr_update_shadow(config, evlist);
885
886 /*
887 * With metric_only everything is on a single line.
888 * Without each counter has its own line.
889 */
890 for (s = 0; s < config->aggr_map->nr; s++) {
891 if (prefix && metric_only)
892 fprintf(output, "%s", prefix);
893
894 first = true;
895 evlist__for_each_entry(evlist, counter) {
896 print_counter_aggrdata(config, counter, s,
897 prefix, metric_only,
898 &first, (struct perf_cpu){ .cpu = -1 });
899 }
900 if (metric_only)
901 fputc('\n', output);
902 }
903 }
904
cmp_val(const void * a,const void * b)905 static int cmp_val(const void *a, const void *b)
906 {
907 return ((struct perf_aggr_thread_value *)b)->val -
908 ((struct perf_aggr_thread_value *)a)->val;
909 }
910
sort_aggr_thread(struct evsel * counter,int * ret,struct target * _target)911 static struct perf_aggr_thread_value *sort_aggr_thread(
912 struct evsel *counter,
913 int *ret,
914 struct target *_target)
915 {
916 int nthreads = perf_thread_map__nr(counter->core.threads);
917 int i = 0;
918 double uval;
919 struct perf_aggr_thread_value *buf;
920
921 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
922 if (!buf)
923 return NULL;
924
925 for (int thread = 0; thread < nthreads; thread++) {
926 int idx;
927 u64 ena = 0, run = 0, val = 0;
928
929 perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) {
930 struct perf_counts_values *counts =
931 perf_counts(counter->counts, idx, thread);
932
933 val += counts->val;
934 ena += counts->ena;
935 run += counts->run;
936 }
937
938 uval = val * counter->scale;
939
940 /*
941 * Skip value 0 when enabling --per-thread globally,
942 * otherwise too many 0 output.
943 */
944 if (uval == 0.0 && target__has_per_thread(_target))
945 continue;
946
947 buf[i].counter = counter;
948 buf[i].id = aggr_cpu_id__empty();
949 buf[i].id.thread_idx = thread;
950 buf[i].uval = uval;
951 buf[i].val = val;
952 buf[i].run = run;
953 buf[i].ena = ena;
954 i++;
955 }
956
957 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
958
959 if (ret)
960 *ret = i;
961
962 return buf;
963 }
964
print_aggr_thread(struct perf_stat_config * config,struct target * _target,struct evsel * counter,char * prefix)965 static void print_aggr_thread(struct perf_stat_config *config,
966 struct target *_target,
967 struct evsel *counter, char *prefix)
968 {
969 FILE *output = config->output;
970 int thread, sorted_threads;
971 struct aggr_cpu_id id;
972 struct perf_aggr_thread_value *buf;
973
974 buf = sort_aggr_thread(counter, &sorted_threads, _target);
975 if (!buf) {
976 perror("cannot sort aggr thread");
977 return;
978 }
979
980 for (thread = 0; thread < sorted_threads; thread++) {
981 if (prefix)
982 fprintf(output, "%s", prefix);
983
984 id = buf[thread].id;
985 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
986 prefix, buf[thread].run, buf[thread].ena, 1.0,
987 &rt_stat);
988 fputc('\n', output);
989 }
990
991 free(buf);
992 }
993
994 struct caggr_data {
995 double avg, avg_enabled, avg_running;
996 };
997
counter_aggr_cb(struct perf_stat_config * config __maybe_unused,struct evsel * counter,void * data,bool first __maybe_unused)998 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
999 struct evsel *counter, void *data,
1000 bool first __maybe_unused)
1001 {
1002 struct caggr_data *cd = data;
1003 struct perf_counts_values *aggr = &counter->counts->aggr;
1004
1005 cd->avg += aggr->val;
1006 cd->avg_enabled += aggr->ena;
1007 cd->avg_running += aggr->run;
1008 }
1009
1010 /*
1011 * Print out the results of a single counter:
1012 * aggregated counts in system-wide mode
1013 */
print_counter_aggr(struct perf_stat_config * config,struct evsel * counter,char * prefix)1014 static void print_counter_aggr(struct perf_stat_config *config,
1015 struct evsel *counter, char *prefix)
1016 {
1017 bool metric_only = config->metric_only;
1018 FILE *output = config->output;
1019 double uval;
1020 struct caggr_data cd = { .avg = 0.0 };
1021
1022 if (!collect_data(config, counter, counter_aggr_cb, &cd))
1023 return;
1024
1025 if (prefix && !metric_only)
1026 fprintf(output, "%s", prefix);
1027
1028 uval = cd.avg * counter->scale;
1029 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
1030 cd.avg_enabled, cd.avg, &rt_stat);
1031 if (!metric_only)
1032 fprintf(output, "\n");
1033 }
1034
counter_cb(struct perf_stat_config * config __maybe_unused,struct evsel * counter,void * data,bool first __maybe_unused)1035 static void counter_cb(struct perf_stat_config *config __maybe_unused,
1036 struct evsel *counter, void *data,
1037 bool first __maybe_unused)
1038 {
1039 struct aggr_data *ad = data;
1040
1041 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
1042 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
1043 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
1044 }
1045
1046 /*
1047 * Print out the results of a single counter:
1048 * does not use aggregated count in system-wide
1049 */
print_counter(struct perf_stat_config * config,struct evsel * counter,char * prefix)1050 static void print_counter(struct perf_stat_config *config,
1051 struct evsel *counter, char *prefix)
1052 {
1053 FILE *output = config->output;
1054 u64 ena, run, val;
1055 double uval;
1056 int idx;
1057 struct perf_cpu cpu;
1058 struct aggr_cpu_id id;
1059
1060 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
1061 struct aggr_data ad = { .cpu_map_idx = idx };
1062
1063 if (!collect_data(config, counter, counter_cb, &ad))
1064 return;
1065 val = ad.val;
1066 ena = ad.ena;
1067 run = ad.run;
1068
1069 if (prefix)
1070 fprintf(output, "%s", prefix);
1071
1072 uval = val * counter->scale;
1073 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1074 printout(config, id, 0, counter, uval, prefix,
1075 run, ena, 1.0, &rt_stat);
1076
1077 fputc('\n', output);
1078 }
1079 }
1080
print_no_aggr_metric(struct perf_stat_config * config,struct evlist * evlist,char * prefix)1081 static void print_no_aggr_metric(struct perf_stat_config *config,
1082 struct evlist *evlist,
1083 char *prefix)
1084 {
1085 int all_idx;
1086 struct perf_cpu cpu;
1087
1088 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1089 struct evsel *counter;
1090 bool first = true;
1091
1092 evlist__for_each_entry(evlist, counter) {
1093 u64 ena, run, val;
1094 double uval;
1095 struct aggr_cpu_id id;
1096 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
1097
1098 if (counter_idx < 0)
1099 continue;
1100
1101 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1102 if (first) {
1103 if (prefix)
1104 fputs(prefix, config->output);
1105 aggr_printout(config, counter, id, 0);
1106 first = false;
1107 }
1108 val = perf_counts(counter->counts, counter_idx, 0)->val;
1109 ena = perf_counts(counter->counts, counter_idx, 0)->ena;
1110 run = perf_counts(counter->counts, counter_idx, 0)->run;
1111
1112 uval = val * counter->scale;
1113 printout(config, id, 0, counter, uval, prefix,
1114 run, ena, 1.0, &rt_stat);
1115 }
1116 if (!first)
1117 fputc('\n', config->output);
1118 }
1119 }
1120
1121 static int aggr_header_lens[] = {
1122 [AGGR_CORE] = 24,
1123 [AGGR_DIE] = 18,
1124 [AGGR_SOCKET] = 12,
1125 [AGGR_NONE] = 6,
1126 [AGGR_THREAD] = 24,
1127 [AGGR_NODE] = 6,
1128 [AGGR_GLOBAL] = 0,
1129 };
1130
1131 static const char *aggr_header_csv[] = {
1132 [AGGR_CORE] = "core,cpus,",
1133 [AGGR_DIE] = "die,cpus",
1134 [AGGR_SOCKET] = "socket,cpus",
1135 [AGGR_NONE] = "cpu,",
1136 [AGGR_THREAD] = "comm-pid,",
1137 [AGGR_NODE] = "node,",
1138 [AGGR_GLOBAL] = ""
1139 };
1140
print_metric_headers(struct perf_stat_config * config,struct evlist * evlist,const char * prefix,bool no_indent)1141 static void print_metric_headers(struct perf_stat_config *config,
1142 struct evlist *evlist,
1143 const char *prefix, bool no_indent)
1144 {
1145 struct perf_stat_output_ctx out;
1146 struct evsel *counter;
1147 struct outstate os = {
1148 .fh = config->output
1149 };
1150 bool first = true;
1151
1152 if (config->json_output && !config->interval)
1153 fprintf(config->output, "{");
1154
1155 if (prefix && !config->json_output)
1156 fprintf(config->output, "%s", prefix);
1157
1158 if (!config->csv_output && !no_indent)
1159 fprintf(config->output, "%*s",
1160 aggr_header_lens[config->aggr_mode], "");
1161 if (config->csv_output) {
1162 if (config->interval)
1163 fputs("time,", config->output);
1164 if (!config->iostat_run)
1165 fputs(aggr_header_csv[config->aggr_mode], config->output);
1166 }
1167 if (config->iostat_run)
1168 iostat_print_header_prefix(config);
1169
1170 /* Print metrics headers only */
1171 evlist__for_each_entry(evlist, counter) {
1172 os.evsel = counter;
1173 out.ctx = &os;
1174 out.print_metric = print_metric_header;
1175 if (!first && config->json_output)
1176 fprintf(config->output, ", ");
1177 first = false;
1178 out.new_line = new_line_metric;
1179 out.force_header = true;
1180 perf_stat__print_shadow_stats(config, counter, 0,
1181 0,
1182 &out,
1183 &config->metric_events,
1184 &rt_stat);
1185 }
1186 if (config->json_output)
1187 fprintf(config->output, "}");
1188 fputc('\n', config->output);
1189 }
1190
print_interval(struct perf_stat_config * config,struct evlist * evlist,char * prefix,struct timespec * ts)1191 static void print_interval(struct perf_stat_config *config,
1192 struct evlist *evlist,
1193 char *prefix, struct timespec *ts)
1194 {
1195 bool metric_only = config->metric_only;
1196 unsigned int unit_width = config->unit_width;
1197 FILE *output = config->output;
1198 static int num_print_interval;
1199
1200 if (config->interval_clear)
1201 puts(CONSOLE_CLEAR);
1202
1203 if (!config->iostat_run && !config->json_output)
1204 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec,
1205 ts->tv_nsec, config->csv_sep);
1206 if (!config->iostat_run && config->json_output && !config->metric_only)
1207 sprintf(prefix, "{\"interval\" : %lu.%09lu, ", (unsigned long)
1208 ts->tv_sec, ts->tv_nsec);
1209 if (!config->iostat_run && config->json_output && config->metric_only)
1210 sprintf(prefix, "{\"interval\" : %lu.%09lu}", (unsigned long)
1211 ts->tv_sec, ts->tv_nsec);
1212
1213 if ((num_print_interval == 0 && !config->csv_output && !config->json_output)
1214 || config->interval_clear) {
1215 switch (config->aggr_mode) {
1216 case AGGR_NODE:
1217 fprintf(output, "# time node cpus");
1218 if (!metric_only)
1219 fprintf(output, " counts %*s events\n", unit_width, "unit");
1220 break;
1221 case AGGR_SOCKET:
1222 fprintf(output, "# time socket cpus");
1223 if (!metric_only)
1224 fprintf(output, " counts %*s events\n", unit_width, "unit");
1225 break;
1226 case AGGR_DIE:
1227 fprintf(output, "# time die cpus");
1228 if (!metric_only)
1229 fprintf(output, " counts %*s events\n", unit_width, "unit");
1230 break;
1231 case AGGR_CORE:
1232 fprintf(output, "# time core cpus");
1233 if (!metric_only)
1234 fprintf(output, " counts %*s events\n", unit_width, "unit");
1235 break;
1236 case AGGR_NONE:
1237 fprintf(output, "# time CPU ");
1238 if (!metric_only)
1239 fprintf(output, " counts %*s events\n", unit_width, "unit");
1240 break;
1241 case AGGR_THREAD:
1242 fprintf(output, "# time comm-pid");
1243 if (!metric_only)
1244 fprintf(output, " counts %*s events\n", unit_width, "unit");
1245 break;
1246 case AGGR_GLOBAL:
1247 default:
1248 if (!config->iostat_run) {
1249 fprintf(output, "# time");
1250 if (!metric_only)
1251 fprintf(output, " counts %*s events\n", unit_width, "unit");
1252 }
1253 case AGGR_UNSET:
1254 case AGGR_MAX:
1255 break;
1256 }
1257 }
1258
1259 if ((num_print_interval == 0 || config->interval_clear)
1260 && metric_only && !config->json_output)
1261 print_metric_headers(config, evlist, " ", true);
1262 if ((num_print_interval == 0 || config->interval_clear)
1263 && metric_only && config->json_output) {
1264 fprintf(output, "{");
1265 print_metric_headers(config, evlist, " ", true);
1266 }
1267 if (++num_print_interval == 25)
1268 num_print_interval = 0;
1269 }
1270
print_header(struct perf_stat_config * config,struct target * _target,int argc,const char ** argv)1271 static void print_header(struct perf_stat_config *config,
1272 struct target *_target,
1273 int argc, const char **argv)
1274 {
1275 FILE *output = config->output;
1276 int i;
1277
1278 fflush(stdout);
1279
1280 if (!config->csv_output && !config->json_output) {
1281 fprintf(output, "\n");
1282 fprintf(output, " Performance counter stats for ");
1283 if (_target->bpf_str)
1284 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1285 else if (_target->system_wide)
1286 fprintf(output, "\'system wide");
1287 else if (_target->cpu_list)
1288 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1289 else if (!target__has_task(_target)) {
1290 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1291 for (i = 1; argv && (i < argc); i++)
1292 fprintf(output, " %s", argv[i]);
1293 } else if (_target->pid)
1294 fprintf(output, "process id \'%s", _target->pid);
1295 else
1296 fprintf(output, "thread id \'%s", _target->tid);
1297
1298 fprintf(output, "\'");
1299 if (config->run_count > 1)
1300 fprintf(output, " (%d runs)", config->run_count);
1301 fprintf(output, ":\n\n");
1302 }
1303 }
1304
get_precision(double num)1305 static int get_precision(double num)
1306 {
1307 if (num > 1)
1308 return 0;
1309
1310 return lround(ceil(-log10(num)));
1311 }
1312
print_table(struct perf_stat_config * config,FILE * output,int precision,double avg)1313 static void print_table(struct perf_stat_config *config,
1314 FILE *output, int precision, double avg)
1315 {
1316 char tmp[64];
1317 int idx, indent = 0;
1318
1319 scnprintf(tmp, 64, " %17.*f", precision, avg);
1320 while (tmp[indent] == ' ')
1321 indent++;
1322
1323 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1324
1325 for (idx = 0; idx < config->run_count; idx++) {
1326 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1327 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1328
1329 fprintf(output, " %17.*f (%+.*f) ",
1330 precision, run, precision, run - avg);
1331
1332 for (h = 0; h < n; h++)
1333 fprintf(output, "#");
1334
1335 fprintf(output, "\n");
1336 }
1337
1338 fprintf(output, "\n%*s# Final result:\n", indent, "");
1339 }
1340
timeval2double(struct timeval * t)1341 static double timeval2double(struct timeval *t)
1342 {
1343 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1344 }
1345
print_footer(struct perf_stat_config * config)1346 static void print_footer(struct perf_stat_config *config)
1347 {
1348 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1349 FILE *output = config->output;
1350
1351 if (!config->null_run)
1352 fprintf(output, "\n");
1353
1354 if (config->run_count == 1) {
1355 fprintf(output, " %17.9f seconds time elapsed", avg);
1356
1357 if (config->ru_display) {
1358 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1359 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1360
1361 fprintf(output, "\n\n");
1362 fprintf(output, " %17.9f seconds user\n", ru_utime);
1363 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1364 }
1365 } else {
1366 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1367 /*
1368 * Display at most 2 more significant
1369 * digits than the stddev inaccuracy.
1370 */
1371 int precision = get_precision(sd) + 2;
1372
1373 if (config->walltime_run_table)
1374 print_table(config, output, precision, avg);
1375
1376 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1377 precision, avg, precision, sd);
1378
1379 print_noise_pct(config, sd, avg);
1380 }
1381 fprintf(output, "\n\n");
1382
1383 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1384 fprintf(output,
1385 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1386 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1387 " perf stat ...\n"
1388 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1389
1390 if (config->print_mixed_hw_group_error)
1391 fprintf(output,
1392 "The events in group usually have to be from "
1393 "the same PMU. Try reorganizing the group.\n");
1394 }
1395
print_percore_thread(struct perf_stat_config * config,struct evsel * counter,char * prefix)1396 static void print_percore_thread(struct perf_stat_config *config,
1397 struct evsel *counter, char *prefix)
1398 {
1399 int s;
1400 struct aggr_cpu_id s2, id;
1401 struct perf_cpu_map *cpus;
1402 bool first = true;
1403 int idx;
1404 struct perf_cpu cpu;
1405
1406 cpus = evsel__cpus(counter);
1407 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
1408 s2 = config->aggr_get_id(config, cpu);
1409 for (s = 0; s < config->aggr_map->nr; s++) {
1410 id = config->aggr_map->map[s];
1411 if (aggr_cpu_id__equal(&s2, &id))
1412 break;
1413 }
1414
1415 print_counter_aggrdata(config, counter, s,
1416 prefix, false,
1417 &first, cpu);
1418 }
1419 }
1420
print_percore(struct perf_stat_config * config,struct evsel * counter,char * prefix)1421 static void print_percore(struct perf_stat_config *config,
1422 struct evsel *counter, char *prefix)
1423 {
1424 bool metric_only = config->metric_only;
1425 FILE *output = config->output;
1426 int s;
1427 bool first = true;
1428
1429 if (!config->aggr_map || !config->aggr_get_id)
1430 return;
1431
1432 if (config->percore_show_thread)
1433 return print_percore_thread(config, counter, prefix);
1434
1435 for (s = 0; s < config->aggr_map->nr; s++) {
1436 if (prefix && metric_only)
1437 fprintf(output, "%s", prefix);
1438
1439 print_counter_aggrdata(config, counter, s,
1440 prefix, metric_only,
1441 &first, (struct perf_cpu){ .cpu = -1 });
1442 }
1443
1444 if (metric_only)
1445 fputc('\n', output);
1446 }
1447
evlist__print_counters(struct evlist * evlist,struct perf_stat_config * config,struct target * _target,struct timespec * ts,int argc,const char ** argv)1448 void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1449 struct target *_target, struct timespec *ts, int argc, const char **argv)
1450 {
1451 bool metric_only = config->metric_only;
1452 int interval = config->interval;
1453 struct evsel *counter;
1454 char buf[64], *prefix = NULL;
1455
1456 if (config->iostat_run)
1457 evlist->selected = evlist__first(evlist);
1458
1459 if (interval)
1460 print_interval(config, evlist, prefix = buf, ts);
1461 else
1462 print_header(config, _target, argc, argv);
1463
1464 if (metric_only) {
1465 static int num_print_iv;
1466
1467 if (num_print_iv == 0 && !interval)
1468 print_metric_headers(config, evlist, prefix, false);
1469 if (num_print_iv++ == 25)
1470 num_print_iv = 0;
1471 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
1472 fprintf(config->output, "%s", prefix);
1473
1474 if (config->json_output && !config->metric_only)
1475 fprintf(config->output, "}");
1476 }
1477
1478 switch (config->aggr_mode) {
1479 case AGGR_CORE:
1480 case AGGR_DIE:
1481 case AGGR_SOCKET:
1482 case AGGR_NODE:
1483 print_aggr(config, evlist, prefix);
1484 break;
1485 case AGGR_THREAD:
1486 evlist__for_each_entry(evlist, counter) {
1487 print_aggr_thread(config, _target, counter, prefix);
1488 }
1489 break;
1490 case AGGR_GLOBAL:
1491 if (config->iostat_run)
1492 iostat_print_counters(evlist, config, ts, prefix = buf,
1493 print_counter_aggr);
1494 else {
1495 evlist__for_each_entry(evlist, counter) {
1496 print_counter_aggr(config, counter, prefix);
1497 }
1498 if (metric_only)
1499 fputc('\n', config->output);
1500 }
1501 break;
1502 case AGGR_NONE:
1503 if (metric_only)
1504 print_no_aggr_metric(config, evlist, prefix);
1505 else {
1506 evlist__for_each_entry(evlist, counter) {
1507 if (counter->percore)
1508 print_percore(config, counter, prefix);
1509 else
1510 print_counter(config, counter, prefix);
1511 }
1512 }
1513 break;
1514 case AGGR_MAX:
1515 case AGGR_UNSET:
1516 default:
1517 break;
1518 }
1519
1520 if (!interval && !config->csv_output && !config->json_output)
1521 print_footer(config);
1522
1523 fflush(config->output);
1524 }
1525