blob: 93147cc40162f4576690d409191d9e1b4382f7db [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001#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 "color.h"
8#include "counts.h"
9#include "evlist.h"
10#include "evsel.h"
11#include "stat.h"
12#include "top.h"
13#include "thread_map.h"
14#include "cpumap.h"
15#include "string2.h"
16#include <linux/ctype.h>
17#include "cgroup.h"
18#include <api/fs/fs.h>
19
20#define CNTR_NOT_SUPPORTED "<not supported>"
21#define CNTR_NOT_COUNTED "<not counted>"
22
23static void print_running(struct perf_stat_config *config,
24 u64 run, u64 ena)
25{
26 if (config->csv_output) {
27 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
28 config->csv_sep,
29 run,
30 config->csv_sep,
31 ena ? 100.0 * run / ena : 100.0);
32 } else if (run != ena) {
33 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
34 }
35}
36
37static void print_noise_pct(struct perf_stat_config *config,
38 double total, double avg)
39{
40 double pct = rel_stddev_stats(total, avg);
41
42 if (config->csv_output)
43 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
44 else if (pct)
45 fprintf(config->output, " ( +-%6.2f%% )", pct);
46}
47
48static void print_noise(struct perf_stat_config *config,
49 struct evsel *evsel, double avg)
50{
51 struct perf_stat_evsel *ps;
52
53 if (config->run_count == 1)
54 return;
55
56 ps = evsel->stats;
57 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
58}
59
60static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
61{
62 if (nr_cgroups) {
63 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
64 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
65 }
66}
67
68
69static void aggr_printout(struct perf_stat_config *config,
70 struct evsel *evsel, int id, int nr)
71{
72 switch (config->aggr_mode) {
73 case AGGR_CORE:
74 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
75 cpu_map__id_to_socket(id),
76 cpu_map__id_to_die(id),
77 config->csv_output ? 0 : -8,
78 cpu_map__id_to_cpu(id),
79 config->csv_sep,
80 config->csv_output ? 0 : 4,
81 nr,
82 config->csv_sep);
83 break;
84 case AGGR_DIE:
85 fprintf(config->output, "S%d-D%*d%s%*d%s",
86 cpu_map__id_to_socket(id << 16),
87 config->csv_output ? 0 : -8,
88 cpu_map__id_to_die(id << 16),
89 config->csv_sep,
90 config->csv_output ? 0 : 4,
91 nr,
92 config->csv_sep);
93 break;
94 case AGGR_SOCKET:
95 fprintf(config->output, "S%*d%s%*d%s",
96 config->csv_output ? 0 : -5,
97 id,
98 config->csv_sep,
99 config->csv_output ? 0 : 4,
100 nr,
101 config->csv_sep);
102 break;
103 case AGGR_NONE:
104 if (evsel->percore) {
105 fprintf(config->output, "S%d-D%d-C%*d%s",
106 cpu_map__id_to_socket(id),
107 cpu_map__id_to_die(id),
108 config->csv_output ? 0 : -5,
109 cpu_map__id_to_cpu(id), config->csv_sep);
110 } else {
111 fprintf(config->output, "CPU%*d%s ",
112 config->csv_output ? 0 : -5,
113 evsel__cpus(evsel)->map[id],
114 config->csv_sep);
115 }
116 break;
117 case AGGR_THREAD:
118 fprintf(config->output, "%*s-%*d%s",
119 config->csv_output ? 0 : 16,
120 perf_thread_map__comm(evsel->core.threads, id),
121 config->csv_output ? 0 : -8,
122 perf_thread_map__pid(evsel->core.threads, id),
123 config->csv_sep);
124 break;
125 case AGGR_GLOBAL:
126 case AGGR_UNSET:
127 default:
128 break;
129 }
130}
131
132struct outstate {
133 FILE *fh;
134 bool newline;
135 const char *prefix;
136 int nfields;
137 int id, nr;
138 struct evsel *evsel;
139};
140
141#define METRIC_LEN 35
142
143static void new_line_std(struct perf_stat_config *config __maybe_unused,
144 void *ctx)
145{
146 struct outstate *os = ctx;
147
148 os->newline = true;
149}
150
151static void do_new_line_std(struct perf_stat_config *config,
152 struct outstate *os)
153{
154 fputc('\n', os->fh);
155 fputs(os->prefix, os->fh);
156 aggr_printout(config, os->evsel, os->id, os->nr);
157 if (config->aggr_mode == AGGR_NONE)
158 fprintf(os->fh, " ");
159 fprintf(os->fh, " ");
160}
161
162static void print_metric_std(struct perf_stat_config *config,
163 void *ctx, const char *color, const char *fmt,
164 const char *unit, double val)
165{
166 struct outstate *os = ctx;
167 FILE *out = os->fh;
168 int n;
169 bool newline = os->newline;
170
171 os->newline = false;
172
173 if (unit == NULL || fmt == NULL) {
174 fprintf(out, "%-*s", METRIC_LEN, "");
175 return;
176 }
177
178 if (newline)
179 do_new_line_std(config, os);
180
181 n = fprintf(out, " # ");
182 if (color)
183 n += color_fprintf(out, color, fmt, val);
184 else
185 n += fprintf(out, fmt, val);
186 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
187}
188
189static void new_line_csv(struct perf_stat_config *config, void *ctx)
190{
191 struct outstate *os = ctx;
192 int i;
193
194 fputc('\n', os->fh);
195 if (os->prefix)
196 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
197 aggr_printout(config, os->evsel, os->id, os->nr);
198 for (i = 0; i < os->nfields; i++)
199 fputs(config->csv_sep, os->fh);
200}
201
202static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
203 void *ctx,
204 const char *color __maybe_unused,
205 const char *fmt, const char *unit, double val)
206{
207 struct outstate *os = ctx;
208 FILE *out = os->fh;
209 char buf[64], *vals, *ends;
210
211 if (unit == NULL || fmt == NULL) {
212 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
213 return;
214 }
215 snprintf(buf, sizeof(buf), fmt, val);
216 ends = vals = skip_spaces(buf);
217 while (isdigit(*ends) || *ends == '.')
218 ends++;
219 *ends = 0;
220 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
221}
222
223/* Filter out some columns that don't work well in metrics only mode */
224
225static bool valid_only_metric(const char *unit)
226{
227 if (!unit)
228 return false;
229 if (strstr(unit, "/sec") ||
230 strstr(unit, "hz") ||
231 strstr(unit, "Hz") ||
232 strstr(unit, "CPUs utilized"))
233 return false;
234 return true;
235}
236
237static const char *fixunit(char *buf, struct evsel *evsel,
238 const char *unit)
239{
240 if (!strncmp(unit, "of all", 6)) {
241 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
242 unit);
243 return buf;
244 }
245 return unit;
246}
247
248static void print_metric_only(struct perf_stat_config *config,
249 void *ctx, const char *color, const char *fmt,
250 const char *unit, double val)
251{
252 struct outstate *os = ctx;
253 FILE *out = os->fh;
254 char buf[1024], str[1024];
255 unsigned mlen = config->metric_only_len;
256
257 if (!valid_only_metric(unit))
258 return;
259 unit = fixunit(buf, os->evsel, unit);
260 if (mlen < strlen(unit))
261 mlen = strlen(unit) + 1;
262
263 if (color)
264 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
265
266 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
267 fprintf(out, "%*s ", mlen, str);
268}
269
270static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
271 void *ctx, const char *color __maybe_unused,
272 const char *fmt,
273 const char *unit, double val)
274{
275 struct outstate *os = ctx;
276 FILE *out = os->fh;
277 char buf[64], *vals, *ends;
278 char tbuf[1024];
279
280 if (!valid_only_metric(unit))
281 return;
282 unit = fixunit(tbuf, os->evsel, unit);
283 snprintf(buf, sizeof buf, fmt, val);
284 ends = vals = skip_spaces(buf);
285 while (isdigit(*ends) || *ends == '.')
286 ends++;
287 *ends = 0;
288 fprintf(out, "%s%s", vals, config->csv_sep);
289}
290
291static void new_line_metric(struct perf_stat_config *config __maybe_unused,
292 void *ctx __maybe_unused)
293{
294}
295
296static void print_metric_header(struct perf_stat_config *config,
297 void *ctx, const char *color __maybe_unused,
298 const char *fmt __maybe_unused,
299 const char *unit, double val __maybe_unused)
300{
301 struct outstate *os = ctx;
302 char tbuf[1024];
303
304 if (!valid_only_metric(unit))
305 return;
306 unit = fixunit(tbuf, os->evsel, unit);
307 if (config->csv_output)
308 fprintf(os->fh, "%s%s", unit, config->csv_sep);
309 else
310 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
311}
312
313static int first_shadow_cpu(struct perf_stat_config *config,
314 struct evsel *evsel, int id)
315{
316 struct evlist *evlist = evsel->evlist;
317 int i;
318
David Brazdil0f672f62019-12-10 10:32:29 +0000319 if (config->aggr_mode == AGGR_NONE)
320 return id;
321
Olivier Deprez0e641232021-09-23 10:07:05 +0200322 if (!config->aggr_get_id)
David Brazdil0f672f62019-12-10 10:32:29 +0000323 return 0;
324
325 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
326 int cpu2 = evsel__cpus(evsel)->map[i];
327
328 if (config->aggr_get_id(config, evlist->core.cpus, cpu2) == id)
329 return cpu2;
330 }
331 return 0;
332}
333
334static void abs_printout(struct perf_stat_config *config,
335 int id, int nr, struct evsel *evsel, double avg)
336{
337 FILE *output = config->output;
338 double sc = evsel->scale;
339 const char *fmt;
340
341 if (config->csv_output) {
342 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
343 } else {
344 if (config->big_num)
345 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
346 else
347 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
348 }
349
350 aggr_printout(config, evsel, id, nr);
351
352 fprintf(output, fmt, avg, config->csv_sep);
353
354 if (evsel->unit)
355 fprintf(output, "%-*s%s",
356 config->csv_output ? 0 : config->unit_width,
357 evsel->unit, config->csv_sep);
358
359 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
360
361 print_cgroup(config, evsel);
362}
363
364static bool is_mixed_hw_group(struct evsel *counter)
365{
366 struct evlist *evlist = counter->evlist;
367 u32 pmu_type = counter->core.attr.type;
368 struct evsel *pos;
369
370 if (counter->core.nr_members < 2)
371 return false;
372
373 evlist__for_each_entry(evlist, pos) {
374 /* software events can be part of any hardware group */
375 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
376 continue;
377 if (pmu_type == PERF_TYPE_SOFTWARE) {
378 pmu_type = pos->core.attr.type;
379 continue;
380 }
381 if (pmu_type != pos->core.attr.type)
382 return true;
383 }
384
385 return false;
386}
387
388static void printout(struct perf_stat_config *config, int id, int nr,
389 struct evsel *counter, double uval,
390 char *prefix, u64 run, u64 ena, double noise,
391 struct runtime_stat *st)
392{
393 struct perf_stat_output_ctx out;
394 struct outstate os = {
395 .fh = config->output,
396 .prefix = prefix ? prefix : "",
397 .id = id,
398 .nr = nr,
399 .evsel = counter,
400 };
401 print_metric_t pm = print_metric_std;
402 new_line_t nl;
403
404 if (config->metric_only) {
405 nl = new_line_metric;
406 if (config->csv_output)
407 pm = print_metric_only_csv;
408 else
409 pm = print_metric_only;
410 } else
411 nl = new_line_std;
412
413 if (config->csv_output && !config->metric_only) {
414 static int aggr_fields[] = {
415 [AGGR_GLOBAL] = 0,
416 [AGGR_THREAD] = 1,
417 [AGGR_NONE] = 1,
418 [AGGR_SOCKET] = 2,
419 [AGGR_DIE] = 2,
420 [AGGR_CORE] = 2,
421 };
422
423 pm = print_metric_csv;
424 nl = new_line_csv;
425 os.nfields = 3;
426 os.nfields += aggr_fields[config->aggr_mode];
427 if (counter->cgrp)
428 os.nfields++;
429 }
430 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
431 if (config->metric_only) {
432 pm(config, &os, NULL, "", "", 0);
433 return;
434 }
435 aggr_printout(config, counter, id, nr);
436
437 fprintf(config->output, "%*s%s",
438 config->csv_output ? 0 : 18,
439 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
440 config->csv_sep);
441
442 if (counter->supported) {
443 config->print_free_counters_hint = 1;
444 if (is_mixed_hw_group(counter))
445 config->print_mixed_hw_group_error = 1;
446 }
447
448 fprintf(config->output, "%-*s%s",
449 config->csv_output ? 0 : config->unit_width,
450 counter->unit, config->csv_sep);
451
452 fprintf(config->output, "%*s",
453 config->csv_output ? 0 : -25,
454 perf_evsel__name(counter));
455
456 print_cgroup(config, counter);
457
458 if (!config->csv_output)
459 pm(config, &os, NULL, NULL, "", 0);
460 print_noise(config, counter, noise);
461 print_running(config, run, ena);
462 if (config->csv_output)
463 pm(config, &os, NULL, NULL, "", 0);
464 return;
465 }
466
467 if (!config->metric_only)
468 abs_printout(config, id, nr, counter, uval);
469
470 out.print_metric = pm;
471 out.new_line = nl;
472 out.ctx = &os;
473 out.force_header = false;
474
475 if (config->csv_output && !config->metric_only) {
476 print_noise(config, counter, noise);
477 print_running(config, run, ena);
478 }
479
480 perf_stat__print_shadow_stats(config, counter, uval,
481 first_shadow_cpu(config, counter, id),
482 &out, &config->metric_events, st);
483 if (!config->csv_output && !config->metric_only) {
484 print_noise(config, counter, noise);
485 print_running(config, run, ena);
486 }
487}
488
489static void aggr_update_shadow(struct perf_stat_config *config,
490 struct evlist *evlist)
491{
492 int cpu, s2, id, s;
493 u64 val;
494 struct evsel *counter;
495
496 for (s = 0; s < config->aggr_map->nr; s++) {
497 id = config->aggr_map->map[s];
498 evlist__for_each_entry(evlist, counter) {
499 val = 0;
500 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
501 s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
502 if (s2 != id)
503 continue;
504 val += perf_counts(counter->counts, cpu, 0)->val;
505 }
506 perf_stat__update_shadow_stats(counter, val,
507 first_shadow_cpu(config, counter, id),
508 &rt_stat);
509 }
510 }
511}
512
513static void uniquify_event_name(struct evsel *counter)
514{
515 char *new_name;
516 char *config;
517
518 if (counter->uniquified_name ||
519 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
520 strlen(counter->pmu_name)))
521 return;
522
523 config = strchr(counter->name, '/');
524 if (config) {
525 if (asprintf(&new_name,
526 "%s%s", counter->pmu_name, config) > 0) {
527 free(counter->name);
528 counter->name = new_name;
529 }
530 } else {
531 if (asprintf(&new_name,
532 "%s [%s]", counter->name, counter->pmu_name) > 0) {
533 free(counter->name);
534 counter->name = new_name;
535 }
536 }
537
538 counter->uniquified_name = true;
539}
540
541static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
542 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
543 bool first),
544 void *data)
545{
546 struct evlist *evlist = counter->evlist;
547 struct evsel *alias;
548
549 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
550 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
551 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
552 alias->scale != counter->scale ||
553 alias->cgrp != counter->cgrp ||
554 strcmp(alias->unit, counter->unit) ||
555 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter) ||
556 !strcmp(alias->pmu_name, counter->pmu_name))
557 break;
558 alias->merged_stat = true;
559 cb(config, alias, data, false);
560 }
561}
562
563static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
564 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
565 bool first),
566 void *data)
567{
568 if (counter->merged_stat)
569 return false;
570 cb(config, counter, data, true);
571 if (config->no_merge)
572 uniquify_event_name(counter);
573 else if (counter->auto_merge_stats)
574 collect_all_aliases(config, counter, cb, data);
575 return true;
576}
577
578struct aggr_data {
579 u64 ena, run, val;
580 int id;
581 int nr;
582 int cpu;
583};
584
585static void aggr_cb(struct perf_stat_config *config,
586 struct evsel *counter, void *data, bool first)
587{
588 struct aggr_data *ad = data;
589 int cpu, s2;
590
591 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
592 struct perf_counts_values *counts;
593
594 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
595 if (s2 != ad->id)
596 continue;
597 if (first)
598 ad->nr++;
599 counts = perf_counts(counter->counts, cpu, 0);
600 /*
601 * When any result is bad, make them all to give
602 * consistent output in interval mode.
603 */
604 if (counts->ena == 0 || counts->run == 0 ||
605 counter->counts->scaled == -1) {
606 ad->ena = 0;
607 ad->run = 0;
608 break;
609 }
610 ad->val += counts->val;
611 ad->ena += counts->ena;
612 ad->run += counts->run;
613 }
614}
615
616static void print_counter_aggrdata(struct perf_stat_config *config,
617 struct evsel *counter, int s,
618 char *prefix, bool metric_only,
619 bool *first)
620{
621 struct aggr_data ad;
622 FILE *output = config->output;
623 u64 ena, run, val;
624 int id, nr;
625 double uval;
626
627 ad.id = id = config->aggr_map->map[s];
628 ad.val = ad.ena = ad.run = 0;
629 ad.nr = 0;
630 if (!collect_data(config, counter, aggr_cb, &ad))
631 return;
632
633 nr = ad.nr;
634 ena = ad.ena;
635 run = ad.run;
636 val = ad.val;
637 if (*first && metric_only) {
638 *first = false;
639 aggr_printout(config, counter, id, nr);
640 }
641 if (prefix && !metric_only)
642 fprintf(output, "%s", prefix);
643
644 uval = val * counter->scale;
645 printout(config, id, nr, counter, uval, prefix,
646 run, ena, 1.0, &rt_stat);
647 if (!metric_only)
648 fputc('\n', output);
649}
650
651static void print_aggr(struct perf_stat_config *config,
652 struct evlist *evlist,
653 char *prefix)
654{
655 bool metric_only = config->metric_only;
656 FILE *output = config->output;
657 struct evsel *counter;
658 int s;
659 bool first;
660
Olivier Deprez0e641232021-09-23 10:07:05 +0200661 if (!config->aggr_map || !config->aggr_get_id)
David Brazdil0f672f62019-12-10 10:32:29 +0000662 return;
663
664 aggr_update_shadow(config, evlist);
665
666 /*
667 * With metric_only everything is on a single line.
668 * Without each counter has its own line.
669 */
670 for (s = 0; s < config->aggr_map->nr; s++) {
671 if (prefix && metric_only)
672 fprintf(output, "%s", prefix);
673
674 first = true;
675 evlist__for_each_entry(evlist, counter) {
676 print_counter_aggrdata(config, counter, s,
677 prefix, metric_only,
678 &first);
679 }
680 if (metric_only)
681 fputc('\n', output);
682 }
683}
684
685static int cmp_val(const void *a, const void *b)
686{
687 return ((struct perf_aggr_thread_value *)b)->val -
688 ((struct perf_aggr_thread_value *)a)->val;
689}
690
691static struct perf_aggr_thread_value *sort_aggr_thread(
692 struct evsel *counter,
693 int nthreads, int ncpus,
694 int *ret,
695 struct target *_target)
696{
697 int cpu, thread, i = 0;
698 double uval;
699 struct perf_aggr_thread_value *buf;
700
701 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
702 if (!buf)
703 return NULL;
704
705 for (thread = 0; thread < nthreads; thread++) {
706 u64 ena = 0, run = 0, val = 0;
707
708 for (cpu = 0; cpu < ncpus; cpu++) {
709 val += perf_counts(counter->counts, cpu, thread)->val;
710 ena += perf_counts(counter->counts, cpu, thread)->ena;
711 run += perf_counts(counter->counts, cpu, thread)->run;
712 }
713
714 uval = val * counter->scale;
715
716 /*
717 * Skip value 0 when enabling --per-thread globally,
718 * otherwise too many 0 output.
719 */
720 if (uval == 0.0 && target__has_per_thread(_target))
721 continue;
722
723 buf[i].counter = counter;
724 buf[i].id = thread;
725 buf[i].uval = uval;
726 buf[i].val = val;
727 buf[i].run = run;
728 buf[i].ena = ena;
729 i++;
730 }
731
732 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
733
734 if (ret)
735 *ret = i;
736
737 return buf;
738}
739
740static void print_aggr_thread(struct perf_stat_config *config,
741 struct target *_target,
742 struct evsel *counter, char *prefix)
743{
744 FILE *output = config->output;
745 int nthreads = perf_thread_map__nr(counter->core.threads);
746 int ncpus = perf_cpu_map__nr(counter->core.cpus);
747 int thread, sorted_threads, id;
748 struct perf_aggr_thread_value *buf;
749
750 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
751 if (!buf) {
752 perror("cannot sort aggr thread");
753 return;
754 }
755
756 for (thread = 0; thread < sorted_threads; thread++) {
757 if (prefix)
758 fprintf(output, "%s", prefix);
759
760 id = buf[thread].id;
761 if (config->stats)
762 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
763 prefix, buf[thread].run, buf[thread].ena, 1.0,
764 &config->stats[id]);
765 else
766 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
767 prefix, buf[thread].run, buf[thread].ena, 1.0,
768 &rt_stat);
769 fputc('\n', output);
770 }
771
772 free(buf);
773}
774
775struct caggr_data {
776 double avg, avg_enabled, avg_running;
777};
778
779static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
780 struct evsel *counter, void *data,
781 bool first __maybe_unused)
782{
783 struct caggr_data *cd = data;
784 struct perf_stat_evsel *ps = counter->stats;
785
786 cd->avg += avg_stats(&ps->res_stats[0]);
787 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
788 cd->avg_running += avg_stats(&ps->res_stats[2]);
789}
790
791/*
792 * Print out the results of a single counter:
793 * aggregated counts in system-wide mode
794 */
795static void print_counter_aggr(struct perf_stat_config *config,
796 struct evsel *counter, char *prefix)
797{
798 bool metric_only = config->metric_only;
799 FILE *output = config->output;
800 double uval;
801 struct caggr_data cd = { .avg = 0.0 };
802
803 if (!collect_data(config, counter, counter_aggr_cb, &cd))
804 return;
805
806 if (prefix && !metric_only)
807 fprintf(output, "%s", prefix);
808
809 uval = cd.avg * counter->scale;
810 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
811 cd.avg, &rt_stat);
812 if (!metric_only)
813 fprintf(output, "\n");
814}
815
816static void counter_cb(struct perf_stat_config *config __maybe_unused,
817 struct evsel *counter, void *data,
818 bool first __maybe_unused)
819{
820 struct aggr_data *ad = data;
821
822 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
823 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
824 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
825}
826
827/*
828 * Print out the results of a single counter:
829 * does not use aggregated count in system-wide
830 */
831static void print_counter(struct perf_stat_config *config,
832 struct evsel *counter, char *prefix)
833{
834 FILE *output = config->output;
835 u64 ena, run, val;
836 double uval;
837 int cpu;
838
839 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
840 struct aggr_data ad = { .cpu = cpu };
841
842 if (!collect_data(config, counter, counter_cb, &ad))
843 return;
844 val = ad.val;
845 ena = ad.ena;
846 run = ad.run;
847
848 if (prefix)
849 fprintf(output, "%s", prefix);
850
851 uval = val * counter->scale;
852 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
853 &rt_stat);
854
855 fputc('\n', output);
856 }
857}
858
859static void print_no_aggr_metric(struct perf_stat_config *config,
860 struct evlist *evlist,
861 char *prefix)
862{
863 int cpu;
864 int nrcpus = 0;
865 struct evsel *counter;
866 u64 ena, run, val;
867 double uval;
868
869 nrcpus = evlist->core.cpus->nr;
870 for (cpu = 0; cpu < nrcpus; cpu++) {
871 bool first = true;
872
873 if (prefix)
874 fputs(prefix, config->output);
875 evlist__for_each_entry(evlist, counter) {
876 if (first) {
877 aggr_printout(config, counter, cpu, 0);
878 first = false;
879 }
880 val = perf_counts(counter->counts, cpu, 0)->val;
881 ena = perf_counts(counter->counts, cpu, 0)->ena;
882 run = perf_counts(counter->counts, cpu, 0)->run;
883
884 uval = val * counter->scale;
885 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
886 &rt_stat);
887 }
888 fputc('\n', config->output);
889 }
890}
891
892static int aggr_header_lens[] = {
893 [AGGR_CORE] = 24,
894 [AGGR_DIE] = 18,
895 [AGGR_SOCKET] = 12,
896 [AGGR_NONE] = 6,
897 [AGGR_THREAD] = 24,
898 [AGGR_GLOBAL] = 0,
899};
900
901static const char *aggr_header_csv[] = {
902 [AGGR_CORE] = "core,cpus,",
903 [AGGR_DIE] = "die,cpus",
904 [AGGR_SOCKET] = "socket,cpus",
905 [AGGR_NONE] = "cpu,",
906 [AGGR_THREAD] = "comm-pid,",
907 [AGGR_GLOBAL] = ""
908};
909
910static void print_metric_headers(struct perf_stat_config *config,
911 struct evlist *evlist,
912 const char *prefix, bool no_indent)
913{
914 struct perf_stat_output_ctx out;
915 struct evsel *counter;
916 struct outstate os = {
917 .fh = config->output
918 };
919
920 if (prefix)
921 fprintf(config->output, "%s", prefix);
922
923 if (!config->csv_output && !no_indent)
924 fprintf(config->output, "%*s",
925 aggr_header_lens[config->aggr_mode], "");
926 if (config->csv_output) {
927 if (config->interval)
928 fputs("time,", config->output);
929 fputs(aggr_header_csv[config->aggr_mode], config->output);
930 }
931
932 /* Print metrics headers only */
933 evlist__for_each_entry(evlist, counter) {
934 os.evsel = counter;
935 out.ctx = &os;
936 out.print_metric = print_metric_header;
937 out.new_line = new_line_metric;
938 out.force_header = true;
939 os.evsel = counter;
940 perf_stat__print_shadow_stats(config, counter, 0,
941 0,
942 &out,
943 &config->metric_events,
944 &rt_stat);
945 }
946 fputc('\n', config->output);
947}
948
949static void print_interval(struct perf_stat_config *config,
950 struct evlist *evlist,
951 char *prefix, struct timespec *ts)
952{
953 bool metric_only = config->metric_only;
954 unsigned int unit_width = config->unit_width;
955 FILE *output = config->output;
956 static int num_print_interval;
957
958 if (config->interval_clear)
959 puts(CONSOLE_CLEAR);
960
961 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
962
963 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
964 switch (config->aggr_mode) {
965 case AGGR_SOCKET:
966 fprintf(output, "# time socket cpus");
967 if (!metric_only)
968 fprintf(output, " counts %*s events\n", unit_width, "unit");
969 break;
970 case AGGR_DIE:
971 fprintf(output, "# time die cpus");
972 if (!metric_only)
973 fprintf(output, " counts %*s events\n", unit_width, "unit");
974 break;
975 case AGGR_CORE:
976 fprintf(output, "# time core cpus");
977 if (!metric_only)
978 fprintf(output, " counts %*s events\n", unit_width, "unit");
979 break;
980 case AGGR_NONE:
981 fprintf(output, "# time CPU ");
982 if (!metric_only)
983 fprintf(output, " counts %*s events\n", unit_width, "unit");
984 break;
985 case AGGR_THREAD:
986 fprintf(output, "# time comm-pid");
987 if (!metric_only)
988 fprintf(output, " counts %*s events\n", unit_width, "unit");
989 break;
990 case AGGR_GLOBAL:
991 default:
992 fprintf(output, "# time");
993 if (!metric_only)
994 fprintf(output, " counts %*s events\n", unit_width, "unit");
995 case AGGR_UNSET:
996 break;
997 }
998 }
999
1000 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1001 print_metric_headers(config, evlist, " ", true);
1002 if (++num_print_interval == 25)
1003 num_print_interval = 0;
1004}
1005
1006static void print_header(struct perf_stat_config *config,
1007 struct target *_target,
1008 int argc, const char **argv)
1009{
1010 FILE *output = config->output;
1011 int i;
1012
1013 fflush(stdout);
1014
1015 if (!config->csv_output) {
1016 fprintf(output, "\n");
1017 fprintf(output, " Performance counter stats for ");
1018 if (_target->system_wide)
1019 fprintf(output, "\'system wide");
1020 else if (_target->cpu_list)
1021 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1022 else if (!target__has_task(_target)) {
1023 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1024 for (i = 1; argv && (i < argc); i++)
1025 fprintf(output, " %s", argv[i]);
1026 } else if (_target->pid)
1027 fprintf(output, "process id \'%s", _target->pid);
1028 else
1029 fprintf(output, "thread id \'%s", _target->tid);
1030
1031 fprintf(output, "\'");
1032 if (config->run_count > 1)
1033 fprintf(output, " (%d runs)", config->run_count);
1034 fprintf(output, ":\n\n");
1035 }
1036}
1037
1038static int get_precision(double num)
1039{
1040 if (num > 1)
1041 return 0;
1042
1043 return lround(ceil(-log10(num)));
1044}
1045
1046static void print_table(struct perf_stat_config *config,
1047 FILE *output, int precision, double avg)
1048{
1049 char tmp[64];
1050 int idx, indent = 0;
1051
1052 scnprintf(tmp, 64, " %17.*f", precision, avg);
1053 while (tmp[indent] == ' ')
1054 indent++;
1055
1056 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1057
1058 for (idx = 0; idx < config->run_count; idx++) {
1059 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1060 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1061
1062 fprintf(output, " %17.*f (%+.*f) ",
1063 precision, run, precision, run - avg);
1064
1065 for (h = 0; h < n; h++)
1066 fprintf(output, "#");
1067
1068 fprintf(output, "\n");
1069 }
1070
1071 fprintf(output, "\n%*s# Final result:\n", indent, "");
1072}
1073
1074static double timeval2double(struct timeval *t)
1075{
1076 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1077}
1078
1079static void print_footer(struct perf_stat_config *config)
1080{
1081 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1082 FILE *output = config->output;
1083 int n;
1084
1085 if (!config->null_run)
1086 fprintf(output, "\n");
1087
1088 if (config->run_count == 1) {
1089 fprintf(output, " %17.9f seconds time elapsed", avg);
1090
1091 if (config->ru_display) {
1092 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1093 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1094
1095 fprintf(output, "\n\n");
1096 fprintf(output, " %17.9f seconds user\n", ru_utime);
1097 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1098 }
1099 } else {
1100 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1101 /*
1102 * Display at most 2 more significant
1103 * digits than the stddev inaccuracy.
1104 */
1105 int precision = get_precision(sd) + 2;
1106
1107 if (config->walltime_run_table)
1108 print_table(config, output, precision, avg);
1109
1110 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1111 precision, avg, precision, sd);
1112
1113 print_noise_pct(config, sd, avg);
1114 }
1115 fprintf(output, "\n\n");
1116
1117 if (config->print_free_counters_hint &&
1118 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1119 n > 0)
1120 fprintf(output,
1121"Some events weren't counted. Try disabling the NMI watchdog:\n"
1122" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1123" perf stat ...\n"
1124" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1125
1126 if (config->print_mixed_hw_group_error)
1127 fprintf(output,
1128 "The events in group usually have to be from "
1129 "the same PMU. Try reorganizing the group.\n");
1130}
1131
1132static void print_percore(struct perf_stat_config *config,
1133 struct evsel *counter, char *prefix)
1134{
1135 bool metric_only = config->metric_only;
1136 FILE *output = config->output;
1137 int s;
1138 bool first = true;
1139
Olivier Deprez0e641232021-09-23 10:07:05 +02001140 if (!config->aggr_map || !config->aggr_get_id)
David Brazdil0f672f62019-12-10 10:32:29 +00001141 return;
1142
1143 for (s = 0; s < config->aggr_map->nr; s++) {
1144 if (prefix && metric_only)
1145 fprintf(output, "%s", prefix);
1146
1147 print_counter_aggrdata(config, counter, s,
1148 prefix, metric_only,
1149 &first);
1150 }
1151
1152 if (metric_only)
1153 fputc('\n', output);
1154}
1155
1156void
1157perf_evlist__print_counters(struct evlist *evlist,
1158 struct perf_stat_config *config,
1159 struct target *_target,
1160 struct timespec *ts,
1161 int argc, const char **argv)
1162{
1163 bool metric_only = config->metric_only;
1164 int interval = config->interval;
1165 struct evsel *counter;
1166 char buf[64], *prefix = NULL;
1167
1168 if (interval)
1169 print_interval(config, evlist, prefix = buf, ts);
1170 else
1171 print_header(config, _target, argc, argv);
1172
1173 if (metric_only) {
1174 static int num_print_iv;
1175
1176 if (num_print_iv == 0 && !interval)
1177 print_metric_headers(config, evlist, prefix, false);
1178 if (num_print_iv++ == 25)
1179 num_print_iv = 0;
1180 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1181 fprintf(config->output, "%s", prefix);
1182 }
1183
1184 switch (config->aggr_mode) {
1185 case AGGR_CORE:
1186 case AGGR_DIE:
1187 case AGGR_SOCKET:
1188 print_aggr(config, evlist, prefix);
1189 break;
1190 case AGGR_THREAD:
1191 evlist__for_each_entry(evlist, counter) {
1192 print_aggr_thread(config, _target, counter, prefix);
1193 }
1194 break;
1195 case AGGR_GLOBAL:
1196 evlist__for_each_entry(evlist, counter) {
1197 print_counter_aggr(config, counter, prefix);
1198 }
1199 if (metric_only)
1200 fputc('\n', config->output);
1201 break;
1202 case AGGR_NONE:
1203 if (metric_only)
1204 print_no_aggr_metric(config, evlist, prefix);
1205 else {
1206 evlist__for_each_entry(evlist, counter) {
1207 if (counter->percore)
1208 print_percore(config, counter, prefix);
1209 else
1210 print_counter(config, counter, prefix);
1211 }
1212 }
1213 break;
1214 case AGGR_UNSET:
1215 default:
1216 break;
1217 }
1218
1219 if (!interval && !config->csv_output)
1220 print_footer(config);
1221
1222 fflush(config->output);
1223}