blob: 9cbe351b141fd09a91d454d5037a9221c741f858 [file] [log] [blame]
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03001#include <stdlib.h>
Jiri Olsa088519f2018-08-30 08:32:52 +02002#include <stdio.h>
3#include <inttypes.h>
Arnaldo Carvalho de Melo810826a2019-06-25 21:28:49 -03004#include <linux/string.h>
Jiri Olsa088519f2018-08-30 08:32:52 +02005#include <linux/time64.h>
6#include <math.h>
Ian Rogers7ea82fb2022-01-04 22:13:34 -08007#include <perf/cpumap.h>
Arnaldo Carvalho de Melo8a249c72019-01-22 10:47:38 -02008#include "color.h"
Arnaldo Carvalho de Melobfc49182019-08-21 14:02:05 -03009#include "counts.h"
Jiri Olsa088519f2018-08-30 08:32:52 +020010#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"
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030017#include <linux/ctype.h>
Jiri Olsa088519f2018-08-30 08:32:52 +020018#include "cgroup.h"
Jiri Olsa088519f2018-08-30 08:32:52 +020019#include <api/fs/fs.h>
Kan Liang2a14c1b2020-02-24 13:59:22 -080020#include "util.h"
Alexander Antonovf07952b2021-04-19 12:41:44 +030021#include "iostat.h"
Jin Yao12279422021-04-27 15:01:20 +080022#include "pmu-hybrid.h"
Jin Yao493be702021-06-10 11:45:57 +080023#include "evlist-hybrid.h"
Jiri Olsa088519f2018-08-30 08:32:52 +020024
25#define CNTR_NOT_SUPPORTED "<not supported>"
26#define CNTR_NOT_COUNTED "<not counted>"
27
Jiri Olsa088519f2018-08-30 08:32:52 +020028static void print_running(struct perf_stat_config *config,
29 u64 run, u64 ena)
30{
31 if (config->csv_output) {
32 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
33 config->csv_sep,
34 run,
35 config->csv_sep,
36 ena ? 100.0 * run / ena : 100.0);
37 } else if (run != ena) {
38 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
39 }
40}
41
42static void print_noise_pct(struct perf_stat_config *config,
43 double total, double avg)
44{
45 double pct = rel_stddev_stats(total, avg);
46
47 if (config->csv_output)
48 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
49 else if (pct)
50 fprintf(config->output, " ( +-%6.2f%% )", pct);
51}
52
53static void print_noise(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +020054 struct evsel *evsel, double avg)
Jiri Olsa088519f2018-08-30 08:32:52 +020055{
56 struct perf_stat_evsel *ps;
57
58 if (config->run_count == 1)
59 return;
60
61 ps = evsel->stats;
62 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
63}
64
Jiri Olsa32dcd022019-07-21 13:23:51 +020065static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
Stephane Eranianbc4da382018-11-07 02:50:45 -080066{
67 if (nr_cgroups) {
68 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
69 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
70 }
71}
72
73
Jiri Olsa088519f2018-08-30 08:32:52 +020074static void aggr_printout(struct perf_stat_config *config,
James Clark2760f5a12020-11-26 16:13:20 +020075 struct evsel *evsel, struct aggr_cpu_id id, int nr)
Jiri Olsa088519f2018-08-30 08:32:52 +020076{
77 switch (config->aggr_mode) {
78 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -070079 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
James Clark1a270cb2020-11-26 16:13:25 +020080 id.socket,
James Clarkba2ee162020-11-26 16:13:26 +020081 id.die,
Jiri Olsa088519f2018-08-30 08:32:52 +020082 config->csv_output ? 0 : -8,
James Clarkb9933812020-11-26 16:13:27 +020083 id.core,
Jiri Olsa088519f2018-08-30 08:32:52 +020084 config->csv_sep,
85 config->csv_output ? 0 : 4,
86 nr,
87 config->csv_sep);
88 break;
Kan Liangdb5742b2019-06-04 15:50:42 -070089 case AGGR_DIE:
90 fprintf(config->output, "S%d-D%*d%s%*d%s",
James Clark1a270cb2020-11-26 16:13:25 +020091 id.socket,
Kan Liangdb5742b2019-06-04 15:50:42 -070092 config->csv_output ? 0 : -8,
James Clarkba2ee162020-11-26 16:13:26 +020093 id.die,
Kan Liangdb5742b2019-06-04 15:50:42 -070094 config->csv_sep,
95 config->csv_output ? 0 : 4,
96 nr,
97 config->csv_sep);
98 break;
Jiri Olsa088519f2018-08-30 08:32:52 +020099 case AGGR_SOCKET:
100 fprintf(config->output, "S%*d%s%*d%s",
101 config->csv_output ? 0 : -5,
James Clark1a270cb2020-11-26 16:13:25 +0200102 id.socket,
Jiri Olsa088519f2018-08-30 08:32:52 +0200103 config->csv_sep,
104 config->csv_output ? 0 : 4,
105 nr,
106 config->csv_sep);
107 break;
Jiri Olsa86895b42019-08-28 10:17:43 +0200108 case AGGR_NODE:
109 fprintf(config->output, "N%*d%s%*d%s",
110 config->csv_output ? 0 : -5,
James Clarkfcd83a32020-11-26 16:13:24 +0200111 id.node,
Jiri Olsa86895b42019-08-28 10:17:43 +0200112 config->csv_sep,
113 config->csv_output ? 0 : 4,
114 nr,
115 config->csv_sep);
116 break;
Jiri Olsa088519f2018-08-30 08:32:52 +0200117 case AGGR_NONE:
Jin Yao1af62ce2020-02-14 16:04:52 +0800118 if (evsel->percore && !config->percore_show_thread) {
Kan Liangdb5742b2019-06-04 15:50:42 -0700119 fprintf(config->output, "S%d-D%d-C%*d%s",
James Clark1a270cb2020-11-26 16:13:25 +0200120 id.socket,
James Clarkba2ee162020-11-26 16:13:26 +0200121 id.die,
Jin Yaod13e9e42020-02-18 15:16:14 +0800122 config->csv_output ? 0 : -3,
James Clarkb9933812020-11-26 16:13:27 +0200123 id.core, config->csv_sep);
Ian Rogers6d188042022-01-04 22:13:51 -0800124 } else if (id.cpu.cpu > -1) {
Jin Yaod13e9e42020-02-18 15:16:14 +0800125 fprintf(config->output, "CPU%*d%s",
126 config->csv_output ? 0 : -7,
Ian Rogers6d188042022-01-04 22:13:51 -0800127 id.cpu.cpu, config->csv_sep);
Jin Yao4fc4d8d2019-04-12 21:59:49 +0800128 }
Jiri Olsa088519f2018-08-30 08:32:52 +0200129 break;
130 case AGGR_THREAD:
131 fprintf(config->output, "%*s-%*d%s",
132 config->csv_output ? 0 : 16,
James Clark8d4852b2020-11-26 16:13:28 +0200133 perf_thread_map__comm(evsel->core.threads, id.thread),
Jiri Olsa088519f2018-08-30 08:32:52 +0200134 config->csv_output ? 0 : -8,
James Clark8d4852b2020-11-26 16:13:28 +0200135 perf_thread_map__pid(evsel->core.threads, id.thread),
Jiri Olsa088519f2018-08-30 08:32:52 +0200136 config->csv_sep);
137 break;
138 case AGGR_GLOBAL:
139 case AGGR_UNSET:
140 default:
141 break;
142 }
143}
144
145struct outstate {
146 FILE *fh;
147 bool newline;
148 const char *prefix;
149 int nfields;
James Clark2760f5a12020-11-26 16:13:20 +0200150 int nr;
151 struct aggr_cpu_id id;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200152 struct evsel *evsel;
Jiri Olsa088519f2018-08-30 08:32:52 +0200153};
154
155#define METRIC_LEN 35
156
157static void new_line_std(struct perf_stat_config *config __maybe_unused,
158 void *ctx)
159{
160 struct outstate *os = ctx;
161
162 os->newline = true;
163}
164
165static void do_new_line_std(struct perf_stat_config *config,
166 struct outstate *os)
167{
168 fputc('\n', os->fh);
169 fputs(os->prefix, os->fh);
170 aggr_printout(config, os->evsel, os->id, os->nr);
171 if (config->aggr_mode == AGGR_NONE)
172 fprintf(os->fh, " ");
173 fprintf(os->fh, " ");
174}
175
176static void print_metric_std(struct perf_stat_config *config,
177 void *ctx, const char *color, const char *fmt,
178 const char *unit, double val)
179{
180 struct outstate *os = ctx;
181 FILE *out = os->fh;
182 int n;
183 bool newline = os->newline;
184
185 os->newline = false;
186
187 if (unit == NULL || fmt == NULL) {
188 fprintf(out, "%-*s", METRIC_LEN, "");
189 return;
190 }
191
192 if (newline)
193 do_new_line_std(config, os);
194
195 n = fprintf(out, " # ");
196 if (color)
197 n += color_fprintf(out, color, fmt, val);
198 else
199 n += fprintf(out, fmt, val);
200 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
201}
202
203static void new_line_csv(struct perf_stat_config *config, void *ctx)
204{
205 struct outstate *os = ctx;
206 int i;
207
208 fputc('\n', os->fh);
209 if (os->prefix)
210 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
211 aggr_printout(config, os->evsel, os->id, os->nr);
212 for (i = 0; i < os->nfields; i++)
213 fputs(config->csv_sep, os->fh);
214}
215
216static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
217 void *ctx,
218 const char *color __maybe_unused,
219 const char *fmt, const char *unit, double val)
220{
221 struct outstate *os = ctx;
222 FILE *out = os->fh;
223 char buf[64], *vals, *ends;
224
225 if (unit == NULL || fmt == NULL) {
226 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
227 return;
228 }
229 snprintf(buf, sizeof(buf), fmt, val);
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -0300230 ends = vals = skip_spaces(buf);
Jiri Olsa088519f2018-08-30 08:32:52 +0200231 while (isdigit(*ends) || *ends == '.')
232 ends++;
233 *ends = 0;
Arnaldo Carvalho de Melo810826a2019-06-25 21:28:49 -0300234 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
Jiri Olsa088519f2018-08-30 08:32:52 +0200235}
236
237/* Filter out some columns that don't work well in metrics only mode */
238
239static bool valid_only_metric(const char *unit)
240{
241 if (!unit)
242 return false;
243 if (strstr(unit, "/sec") ||
Jiri Olsa088519f2018-08-30 08:32:52 +0200244 strstr(unit, "CPUs utilized"))
245 return false;
246 return true;
247}
248
Jiri Olsa32dcd022019-07-21 13:23:51 +0200249static const char *fixunit(char *buf, struct evsel *evsel,
Jiri Olsa088519f2018-08-30 08:32:52 +0200250 const char *unit)
251{
252 if (!strncmp(unit, "of all", 6)) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300253 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
Jiri Olsa088519f2018-08-30 08:32:52 +0200254 unit);
255 return buf;
256 }
257 return unit;
258}
259
260static void print_metric_only(struct perf_stat_config *config,
261 void *ctx, const char *color, const char *fmt,
262 const char *unit, double val)
263{
264 struct outstate *os = ctx;
265 FILE *out = os->fh;
266 char buf[1024], str[1024];
267 unsigned mlen = config->metric_only_len;
268
269 if (!valid_only_metric(unit))
270 return;
271 unit = fixunit(buf, os->evsel, unit);
272 if (mlen < strlen(unit))
273 mlen = strlen(unit) + 1;
274
275 if (color)
276 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
277
278 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
279 fprintf(out, "%*s ", mlen, str);
280}
281
282static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
283 void *ctx, const char *color __maybe_unused,
284 const char *fmt,
285 const char *unit, double val)
286{
287 struct outstate *os = ctx;
288 FILE *out = os->fh;
289 char buf[64], *vals, *ends;
290 char tbuf[1024];
291
292 if (!valid_only_metric(unit))
293 return;
294 unit = fixunit(tbuf, os->evsel, unit);
295 snprintf(buf, sizeof buf, fmt, val);
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -0300296 ends = vals = skip_spaces(buf);
Jiri Olsa088519f2018-08-30 08:32:52 +0200297 while (isdigit(*ends) || *ends == '.')
298 ends++;
299 *ends = 0;
300 fprintf(out, "%s%s", vals, config->csv_sep);
301}
302
303static void new_line_metric(struct perf_stat_config *config __maybe_unused,
304 void *ctx __maybe_unused)
305{
306}
307
308static void print_metric_header(struct perf_stat_config *config,
309 void *ctx, const char *color __maybe_unused,
310 const char *fmt __maybe_unused,
311 const char *unit, double val __maybe_unused)
312{
313 struct outstate *os = ctx;
314 char tbuf[1024];
315
Alexander Antonovf07952b2021-04-19 12:41:44 +0300316 /* In case of iostat, print metric header for first root port only */
317 if (config->iostat_run &&
318 os->evsel->priv != os->evsel->evlist->selected->priv)
319 return;
320
Jiri Olsa088519f2018-08-30 08:32:52 +0200321 if (!valid_only_metric(unit))
322 return;
323 unit = fixunit(tbuf, os->evsel, unit);
324 if (config->csv_output)
325 fprintf(os->fh, "%s%s", unit, config->csv_sep);
326 else
327 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
328}
329
Ian Rogersce37ab32022-01-04 22:13:50 -0800330static int first_shadow_cpu_map_idx(struct perf_stat_config *config,
331 struct evsel *evsel, const struct aggr_cpu_id *id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200332{
Ian Rogersce37ab32022-01-04 22:13:50 -0800333 struct perf_cpu_map *cpus = evsel__cpus(evsel);
Ian Rogers6d188042022-01-04 22:13:51 -0800334 struct perf_cpu cpu;
335 int idx;
Jiri Olsa088519f2018-08-30 08:32:52 +0200336
Jiri Olsa088519f2018-08-30 08:32:52 +0200337 if (config->aggr_mode == AGGR_NONE)
Ian Rogersce37ab32022-01-04 22:13:50 -0800338 return perf_cpu_map__idx(cpus, id->cpu);
Jiri Olsa088519f2018-08-30 08:32:52 +0200339
Namhyung Kimc0ee1d5a2020-11-27 13:14:03 +0900340 if (!config->aggr_get_id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200341 return 0;
342
Ian Rogersa0232832022-01-04 22:13:07 -0800343 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Ian Rogers3ac23d12022-01-04 22:13:16 -0800344 struct aggr_cpu_id cpu_id = config->aggr_get_id(config, cpu);
345
346 if (aggr_cpu_id__equal(&cpu_id, id))
Ian Rogersce37ab32022-01-04 22:13:50 -0800347 return idx;
Jiri Olsa088519f2018-08-30 08:32:52 +0200348 }
349 return 0;
350}
351
352static void abs_printout(struct perf_stat_config *config,
James Clark2760f5a12020-11-26 16:13:20 +0200353 struct aggr_cpu_id id, int nr, struct evsel *evsel, double avg)
Jiri Olsa088519f2018-08-30 08:32:52 +0200354{
355 FILE *output = config->output;
356 double sc = evsel->scale;
357 const char *fmt;
358
359 if (config->csv_output) {
360 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
361 } else {
362 if (config->big_num)
363 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
364 else
365 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
366 }
367
368 aggr_printout(config, evsel, id, nr);
369
370 fprintf(output, fmt, avg, config->csv_sep);
371
372 if (evsel->unit)
373 fprintf(output, "%-*s%s",
374 config->csv_output ? 0 : config->unit_width,
375 evsel->unit, config->csv_sep);
376
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300377 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
Jiri Olsa088519f2018-08-30 08:32:52 +0200378
Stephane Eranianbc4da382018-11-07 02:50:45 -0800379 print_cgroup(config, evsel);
Jiri Olsa088519f2018-08-30 08:32:52 +0200380}
381
Jiri Olsa32dcd022019-07-21 13:23:51 +0200382static bool is_mixed_hw_group(struct evsel *counter)
Jiri Olsa088519f2018-08-30 08:32:52 +0200383{
Jiri Olsa63503db2019-07-21 13:23:52 +0200384 struct evlist *evlist = counter->evlist;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200385 u32 pmu_type = counter->core.attr.type;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200386 struct evsel *pos;
Jiri Olsa088519f2018-08-30 08:32:52 +0200387
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200388 if (counter->core.nr_members < 2)
Jiri Olsa088519f2018-08-30 08:32:52 +0200389 return false;
390
391 evlist__for_each_entry(evlist, pos) {
392 /* software events can be part of any hardware group */
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200393 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
Jiri Olsa088519f2018-08-30 08:32:52 +0200394 continue;
395 if (pmu_type == PERF_TYPE_SOFTWARE) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200396 pmu_type = pos->core.attr.type;
Jiri Olsa088519f2018-08-30 08:32:52 +0200397 continue;
398 }
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200399 if (pmu_type != pos->core.attr.type)
Jiri Olsa088519f2018-08-30 08:32:52 +0200400 return true;
401 }
402
403 return false;
404}
405
James Clark2760f5a12020-11-26 16:13:20 +0200406static void printout(struct perf_stat_config *config, struct aggr_cpu_id id, int nr,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200407 struct evsel *counter, double uval,
Jiri Olsa088519f2018-08-30 08:32:52 +0200408 char *prefix, u64 run, u64 ena, double noise,
409 struct runtime_stat *st)
410{
411 struct perf_stat_output_ctx out;
412 struct outstate os = {
413 .fh = config->output,
414 .prefix = prefix ? prefix : "",
415 .id = id,
416 .nr = nr,
417 .evsel = counter,
418 };
419 print_metric_t pm = print_metric_std;
420 new_line_t nl;
421
422 if (config->metric_only) {
423 nl = new_line_metric;
424 if (config->csv_output)
425 pm = print_metric_only_csv;
426 else
427 pm = print_metric_only;
428 } else
429 nl = new_line_std;
430
431 if (config->csv_output && !config->metric_only) {
432 static int aggr_fields[] = {
433 [AGGR_GLOBAL] = 0,
434 [AGGR_THREAD] = 1,
435 [AGGR_NONE] = 1,
436 [AGGR_SOCKET] = 2,
Kan Liangdb5742b2019-06-04 15:50:42 -0700437 [AGGR_DIE] = 2,
Jiri Olsa088519f2018-08-30 08:32:52 +0200438 [AGGR_CORE] = 2,
439 };
440
441 pm = print_metric_csv;
442 nl = new_line_csv;
443 os.nfields = 3;
444 os.nfields += aggr_fields[config->aggr_mode];
445 if (counter->cgrp)
446 os.nfields++;
447 }
Jin Yao0bdad972021-03-19 15:01:55 +0800448
449 if (!config->no_csv_summary && config->csv_output &&
450 config->summary && !config->interval) {
451 fprintf(config->output, "%16s%s", "summary", config->csv_sep);
452 }
453
Jiri Olsa088519f2018-08-30 08:32:52 +0200454 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
455 if (config->metric_only) {
456 pm(config, &os, NULL, "", "", 0);
457 return;
458 }
459 aggr_printout(config, counter, id, nr);
460
461 fprintf(config->output, "%*s%s",
462 config->csv_output ? 0 : 18,
463 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
464 config->csv_sep);
465
466 if (counter->supported) {
Jin Yao493be702021-06-10 11:45:57 +0800467 if (!evlist__has_hybrid(counter->evlist)) {
468 config->print_free_counters_hint = 1;
469 if (is_mixed_hw_group(counter))
470 config->print_mixed_hw_group_error = 1;
471 }
Jiri Olsa088519f2018-08-30 08:32:52 +0200472 }
473
474 fprintf(config->output, "%-*s%s",
475 config->csv_output ? 0 : config->unit_width,
476 counter->unit, config->csv_sep);
477
478 fprintf(config->output, "%*s",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300479 config->csv_output ? 0 : -25, evsel__name(counter));
Jiri Olsa088519f2018-08-30 08:32:52 +0200480
Stephane Eranianbc4da382018-11-07 02:50:45 -0800481 print_cgroup(config, counter);
Jiri Olsa088519f2018-08-30 08:32:52 +0200482
483 if (!config->csv_output)
484 pm(config, &os, NULL, NULL, "", 0);
485 print_noise(config, counter, noise);
486 print_running(config, run, ena);
487 if (config->csv_output)
488 pm(config, &os, NULL, NULL, "", 0);
489 return;
490 }
491
492 if (!config->metric_only)
493 abs_printout(config, id, nr, counter, uval);
494
495 out.print_metric = pm;
496 out.new_line = nl;
497 out.ctx = &os;
498 out.force_header = false;
499
500 if (config->csv_output && !config->metric_only) {
501 print_noise(config, counter, noise);
502 print_running(config, run, ena);
503 }
504
505 perf_stat__print_shadow_stats(config, counter, uval,
Ian Rogersce37ab32022-01-04 22:13:50 -0800506 first_shadow_cpu_map_idx(config, counter, &id),
Jiri Olsa088519f2018-08-30 08:32:52 +0200507 &out, &config->metric_events, st);
508 if (!config->csv_output && !config->metric_only) {
509 print_noise(config, counter, noise);
510 print_running(config, run, ena);
511 }
512}
513
514static void aggr_update_shadow(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200515 struct evlist *evlist)
Jiri Olsa088519f2018-08-30 08:32:52 +0200516{
Ian Rogers6d188042022-01-04 22:13:51 -0800517 int idx, s;
518 struct perf_cpu cpu;
James Clark2760f5a12020-11-26 16:13:20 +0200519 struct aggr_cpu_id s2, id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200520 u64 val;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200521 struct evsel *counter;
Ian Rogersa0232832022-01-04 22:13:07 -0800522 struct perf_cpu_map *cpus;
Jiri Olsa088519f2018-08-30 08:32:52 +0200523
524 for (s = 0; s < config->aggr_map->nr; s++) {
James Clarkff523292020-11-26 16:13:23 +0200525 id = config->aggr_map->map[s];
Jiri Olsa088519f2018-08-30 08:32:52 +0200526 evlist__for_each_entry(evlist, counter) {
Ian Rogersa0232832022-01-04 22:13:07 -0800527 cpus = evsel__cpus(counter);
Jiri Olsa088519f2018-08-30 08:32:52 +0200528 val = 0;
Ian Rogersa0232832022-01-04 22:13:07 -0800529 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Ian Rogers88031a02022-01-04 22:13:08 -0800530 s2 = config->aggr_get_id(config, cpu);
Ian Rogers3ac23d12022-01-04 22:13:16 -0800531 if (!aggr_cpu_id__equal(&s2, &id))
Jiri Olsa088519f2018-08-30 08:32:52 +0200532 continue;
Ian Rogersa0232832022-01-04 22:13:07 -0800533 val += perf_counts(counter->counts, idx, 0)->val;
Jiri Olsa088519f2018-08-30 08:32:52 +0200534 }
535 perf_stat__update_shadow_stats(counter, val,
Ian Rogersce37ab32022-01-04 22:13:50 -0800536 first_shadow_cpu_map_idx(config, counter, &id),
Jiri Olsa088519f2018-08-30 08:32:52 +0200537 &rt_stat);
538 }
539 }
540}
541
Jiri Olsa32dcd022019-07-21 13:23:51 +0200542static void uniquify_event_name(struct evsel *counter)
Jiri Olsa088519f2018-08-30 08:32:52 +0200543{
544 char *new_name;
545 char *config;
Jin Yao12279422021-04-27 15:01:20 +0800546 int ret = 0;
Jiri Olsa088519f2018-08-30 08:32:52 +0200547
Namhyung Kim3cc84392021-06-02 14:22:41 -0700548 if (counter->uniquified_name || counter->use_config_name ||
Jiri Olsa088519f2018-08-30 08:32:52 +0200549 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
550 strlen(counter->pmu_name)))
551 return;
552
553 config = strchr(counter->name, '/');
554 if (config) {
555 if (asprintf(&new_name,
556 "%s%s", counter->pmu_name, config) > 0) {
557 free(counter->name);
558 counter->name = new_name;
559 }
560 } else {
Jin Yao12279422021-04-27 15:01:20 +0800561 if (perf_pmu__has_hybrid()) {
Namhyung Kim3cc84392021-06-02 14:22:41 -0700562 ret = asprintf(&new_name, "%s/%s/",
563 counter->pmu_name, counter->name);
Jin Yao12279422021-04-27 15:01:20 +0800564 } else {
565 ret = asprintf(&new_name, "%s [%s]",
566 counter->name, counter->pmu_name);
567 }
568
569 if (ret) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200570 free(counter->name);
571 counter->name = new_name;
572 }
573 }
574
575 counter->uniquified_name = true;
576}
577
Jiri Olsa32dcd022019-07-21 13:23:51 +0200578static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
579 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200580 bool first),
581 void *data)
582{
Jiri Olsa63503db2019-07-21 13:23:52 +0200583 struct evlist *evlist = counter->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200584 struct evsel *alias;
Jiri Olsa088519f2018-08-30 08:32:52 +0200585
Jiri Olsace9036a2019-07-21 13:24:23 +0200586 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
587 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
Ian Rogersb2b1aa72022-02-04 17:09:41 -0800588 /* Merge events with the same name, etc. but on different PMUs. */
589 if (!strcmp(evsel__name(alias), evsel__name(counter)) &&
590 alias->scale == counter->scale &&
591 alias->cgrp == counter->cgrp &&
592 !strcmp(alias->unit, counter->unit) &&
593 evsel__is_clock(alias) == evsel__is_clock(counter) &&
594 strcmp(alias->pmu_name, counter->pmu_name)) {
595 alias->merged_stat = true;
596 cb(config, alias, data, false);
597 }
Jiri Olsa088519f2018-08-30 08:32:52 +0200598 }
599}
600
Jin Yaoe0a7ef22021-07-07 13:56:52 +0800601static bool is_uncore(struct evsel *evsel)
602{
603 struct perf_pmu *pmu = evsel__find_pmu(evsel);
604
605 return pmu && pmu->is_uncore;
606}
607
608static bool hybrid_uniquify(struct evsel *evsel)
609{
610 return perf_pmu__has_hybrid() && !is_uncore(evsel);
611}
612
Jiri Olsa32dcd022019-07-21 13:23:51 +0200613static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
614 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200615 bool first),
616 void *data)
617{
618 if (counter->merged_stat)
619 return false;
620 cb(config, counter, data, true);
Jin Yaoe0a7ef22021-07-07 13:56:52 +0800621 if (config->no_merge || hybrid_uniquify(counter))
Jiri Olsa088519f2018-08-30 08:32:52 +0200622 uniquify_event_name(counter);
623 else if (counter->auto_merge_stats)
624 collect_all_aliases(config, counter, cb, data);
625 return true;
626}
627
628struct aggr_data {
629 u64 ena, run, val;
James Clark2760f5a12020-11-26 16:13:20 +0200630 struct aggr_cpu_id id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200631 int nr;
Ian Rogersab90caa72022-01-04 22:13:33 -0800632 int cpu_map_idx;
Jiri Olsa088519f2018-08-30 08:32:52 +0200633};
634
635static void aggr_cb(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200636 struct evsel *counter, void *data, bool first)
Jiri Olsa088519f2018-08-30 08:32:52 +0200637{
638 struct aggr_data *ad = data;
Ian Rogers6d188042022-01-04 22:13:51 -0800639 int idx;
640 struct perf_cpu cpu;
Ian Rogersa0232832022-01-04 22:13:07 -0800641 struct perf_cpu_map *cpus;
James Clark2760f5a12020-11-26 16:13:20 +0200642 struct aggr_cpu_id s2;
Jiri Olsa088519f2018-08-30 08:32:52 +0200643
Ian Rogersa0232832022-01-04 22:13:07 -0800644 cpus = evsel__cpus(counter);
645 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200646 struct perf_counts_values *counts;
647
Ian Rogers88031a02022-01-04 22:13:08 -0800648 s2 = config->aggr_get_id(config, cpu);
Ian Rogers3ac23d12022-01-04 22:13:16 -0800649 if (!aggr_cpu_id__equal(&s2, &ad->id))
Jiri Olsa088519f2018-08-30 08:32:52 +0200650 continue;
651 if (first)
652 ad->nr++;
Ian Rogersa0232832022-01-04 22:13:07 -0800653 counts = perf_counts(counter->counts, idx, 0);
Jiri Olsa088519f2018-08-30 08:32:52 +0200654 /*
655 * When any result is bad, make them all to give
656 * consistent output in interval mode.
657 */
658 if (counts->ena == 0 || counts->run == 0 ||
659 counter->counts->scaled == -1) {
660 ad->ena = 0;
661 ad->run = 0;
662 break;
663 }
664 ad->val += counts->val;
665 ad->ena += counts->ena;
666 ad->run += counts->run;
667 }
668}
669
Jin Yao40480a82019-04-12 21:59:48 +0800670static void print_counter_aggrdata(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200671 struct evsel *counter, int s,
Jin Yao40480a82019-04-12 21:59:48 +0800672 char *prefix, bool metric_only,
Ian Rogers6d188042022-01-04 22:13:51 -0800673 bool *first, struct perf_cpu cpu)
Jin Yao40480a82019-04-12 21:59:48 +0800674{
675 struct aggr_data ad;
676 FILE *output = config->output;
677 u64 ena, run, val;
James Clark2760f5a12020-11-26 16:13:20 +0200678 int nr;
679 struct aggr_cpu_id id;
Jin Yao40480a82019-04-12 21:59:48 +0800680 double uval;
681
James Clarkff523292020-11-26 16:13:23 +0200682 ad.id = id = config->aggr_map->map[s];
Jin Yao40480a82019-04-12 21:59:48 +0800683 ad.val = ad.ena = ad.run = 0;
684 ad.nr = 0;
685 if (!collect_data(config, counter, aggr_cb, &ad))
686 return;
687
Jin Yao92637cc2021-04-27 15:01:28 +0800688 if (perf_pmu__has_hybrid() && ad.ena == 0)
689 return;
690
Jin Yao40480a82019-04-12 21:59:48 +0800691 nr = ad.nr;
692 ena = ad.ena;
693 run = ad.run;
694 val = ad.val;
695 if (*first && metric_only) {
696 *first = false;
697 aggr_printout(config, counter, id, nr);
698 }
699 if (prefix && !metric_only)
700 fprintf(output, "%s", prefix);
701
702 uval = val * counter->scale;
Ian Rogers6d188042022-01-04 22:13:51 -0800703 if (cpu.cpu != -1)
Ian Rogers7365f102022-01-04 22:13:28 -0800704 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
705
James Clark2760f5a12020-11-26 16:13:20 +0200706 printout(config, id, nr, counter, uval,
707 prefix, run, ena, 1.0, &rt_stat);
Jin Yao40480a82019-04-12 21:59:48 +0800708 if (!metric_only)
709 fputc('\n', output);
710}
711
Jiri Olsa088519f2018-08-30 08:32:52 +0200712static void print_aggr(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200713 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200714 char *prefix)
715{
716 bool metric_only = config->metric_only;
717 FILE *output = config->output;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200718 struct evsel *counter;
Jin Yao40480a82019-04-12 21:59:48 +0800719 int s;
Jiri Olsa088519f2018-08-30 08:32:52 +0200720 bool first;
721
Hongbo Yaoc0c652f2020-06-05 17:17:40 +0800722 if (!config->aggr_map || !config->aggr_get_id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200723 return;
724
725 aggr_update_shadow(config, evlist);
726
727 /*
728 * With metric_only everything is on a single line.
729 * Without each counter has its own line.
730 */
731 for (s = 0; s < config->aggr_map->nr; s++) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200732 if (prefix && metric_only)
733 fprintf(output, "%s", prefix);
734
Jiri Olsa088519f2018-08-30 08:32:52 +0200735 first = true;
736 evlist__for_each_entry(evlist, counter) {
Jin Yao40480a82019-04-12 21:59:48 +0800737 print_counter_aggrdata(config, counter, s,
Ian Rogers6d188042022-01-04 22:13:51 -0800738 prefix, metric_only,
739 &first, (struct perf_cpu){ .cpu = -1 });
Jiri Olsa088519f2018-08-30 08:32:52 +0200740 }
741 if (metric_only)
742 fputc('\n', output);
743 }
744}
745
746static int cmp_val(const void *a, const void *b)
747{
748 return ((struct perf_aggr_thread_value *)b)->val -
749 ((struct perf_aggr_thread_value *)a)->val;
750}
751
752static struct perf_aggr_thread_value *sort_aggr_thread(
Jiri Olsa32dcd022019-07-21 13:23:51 +0200753 struct evsel *counter,
Jiri Olsa088519f2018-08-30 08:32:52 +0200754 int nthreads, int ncpus,
755 int *ret,
756 struct target *_target)
757{
758 int cpu, thread, i = 0;
759 double uval;
760 struct perf_aggr_thread_value *buf;
761
762 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
763 if (!buf)
764 return NULL;
765
766 for (thread = 0; thread < nthreads; thread++) {
767 u64 ena = 0, run = 0, val = 0;
768
769 for (cpu = 0; cpu < ncpus; cpu++) {
770 val += perf_counts(counter->counts, cpu, thread)->val;
771 ena += perf_counts(counter->counts, cpu, thread)->ena;
772 run += perf_counts(counter->counts, cpu, thread)->run;
773 }
774
775 uval = val * counter->scale;
776
777 /*
778 * Skip value 0 when enabling --per-thread globally,
779 * otherwise too many 0 output.
780 */
781 if (uval == 0.0 && target__has_per_thread(_target))
782 continue;
783
784 buf[i].counter = counter;
Ian Rogers51b826f2022-01-04 22:13:17 -0800785 buf[i].id = aggr_cpu_id__empty();
James Clark8d4852b2020-11-26 16:13:28 +0200786 buf[i].id.thread = thread;
Jiri Olsa088519f2018-08-30 08:32:52 +0200787 buf[i].uval = uval;
788 buf[i].val = val;
789 buf[i].run = run;
790 buf[i].ena = ena;
791 i++;
792 }
793
794 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
795
796 if (ret)
797 *ret = i;
798
799 return buf;
800}
801
802static void print_aggr_thread(struct perf_stat_config *config,
803 struct target *_target,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200804 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200805{
806 FILE *output = config->output;
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200807 int nthreads = perf_thread_map__nr(counter->core.threads);
Jiri Olsa6549cd82019-08-22 13:11:38 +0200808 int ncpus = perf_cpu_map__nr(counter->core.cpus);
James Clark2760f5a12020-11-26 16:13:20 +0200809 int thread, sorted_threads;
810 struct aggr_cpu_id id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200811 struct perf_aggr_thread_value *buf;
812
813 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
814 if (!buf) {
815 perror("cannot sort aggr thread");
816 return;
817 }
818
819 for (thread = 0; thread < sorted_threads; thread++) {
820 if (prefix)
821 fprintf(output, "%s", prefix);
822
823 id = buf[thread].id;
824 if (config->stats)
825 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
826 prefix, buf[thread].run, buf[thread].ena, 1.0,
James Clark8d4852b2020-11-26 16:13:28 +0200827 &config->stats[id.thread]);
Jiri Olsa088519f2018-08-30 08:32:52 +0200828 else
829 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
830 prefix, buf[thread].run, buf[thread].ena, 1.0,
831 &rt_stat);
832 fputc('\n', output);
833 }
834
835 free(buf);
836}
837
838struct caggr_data {
839 double avg, avg_enabled, avg_running;
840};
841
842static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200843 struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200844 bool first __maybe_unused)
845{
846 struct caggr_data *cd = data;
Namhyung Kim07b747f2021-04-22 19:38:33 -0700847 struct perf_counts_values *aggr = &counter->counts->aggr;
Jiri Olsa088519f2018-08-30 08:32:52 +0200848
Namhyung Kim07b747f2021-04-22 19:38:33 -0700849 cd->avg += aggr->val;
850 cd->avg_enabled += aggr->ena;
851 cd->avg_running += aggr->run;
Jiri Olsa088519f2018-08-30 08:32:52 +0200852}
853
854/*
855 * Print out the results of a single counter:
856 * aggregated counts in system-wide mode
857 */
858static void print_counter_aggr(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200859 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200860{
861 bool metric_only = config->metric_only;
862 FILE *output = config->output;
863 double uval;
864 struct caggr_data cd = { .avg = 0.0 };
865
866 if (!collect_data(config, counter, counter_aggr_cb, &cd))
867 return;
868
869 if (prefix && !metric_only)
870 fprintf(output, "%s", prefix);
871
872 uval = cd.avg * counter->scale;
Ian Rogers51b826f2022-01-04 22:13:17 -0800873 printout(config, aggr_cpu_id__empty(), 0, counter, uval, prefix, cd.avg_running,
James Clark2760f5a12020-11-26 16:13:20 +0200874 cd.avg_enabled, cd.avg, &rt_stat);
Jiri Olsa088519f2018-08-30 08:32:52 +0200875 if (!metric_only)
876 fprintf(output, "\n");
877}
878
879static void counter_cb(struct perf_stat_config *config __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200880 struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200881 bool first __maybe_unused)
882{
883 struct aggr_data *ad = data;
884
Ian Rogersab90caa72022-01-04 22:13:33 -0800885 ad->val += perf_counts(counter->counts, ad->cpu_map_idx, 0)->val;
886 ad->ena += perf_counts(counter->counts, ad->cpu_map_idx, 0)->ena;
887 ad->run += perf_counts(counter->counts, ad->cpu_map_idx, 0)->run;
Jiri Olsa088519f2018-08-30 08:32:52 +0200888}
889
890/*
891 * Print out the results of a single counter:
892 * does not use aggregated count in system-wide
893 */
894static void print_counter(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200895 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200896{
897 FILE *output = config->output;
898 u64 ena, run, val;
899 double uval;
Ian Rogers6d188042022-01-04 22:13:51 -0800900 int idx;
901 struct perf_cpu cpu;
James Clark2760f5a12020-11-26 16:13:20 +0200902 struct aggr_cpu_id id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200903
Ian Rogers7ea82fb2022-01-04 22:13:34 -0800904 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
905 struct aggr_data ad = { .cpu_map_idx = idx };
Jiri Olsa088519f2018-08-30 08:32:52 +0200906
907 if (!collect_data(config, counter, counter_cb, &ad))
908 return;
909 val = ad.val;
910 ena = ad.ena;
911 run = ad.run;
912
913 if (prefix)
914 fprintf(output, "%s", prefix);
915
916 uval = val * counter->scale;
Ian Rogers7365f102022-01-04 22:13:28 -0800917 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
James Clark2760f5a12020-11-26 16:13:20 +0200918 printout(config, id, 0, counter, uval, prefix,
919 run, ena, 1.0, &rt_stat);
Jiri Olsa088519f2018-08-30 08:32:52 +0200920
921 fputc('\n', output);
922 }
923}
924
925static void print_no_aggr_metric(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200926 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200927 char *prefix)
928{
Ian Rogers6d188042022-01-04 22:13:51 -0800929 int all_idx;
930 struct perf_cpu cpu;
Jiri Olsa088519f2018-08-30 08:32:52 +0200931
Ian Rogers7365f102022-01-04 22:13:28 -0800932 perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.cpus) {
933 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +0200934 bool first = true;
935
936 if (prefix)
937 fputs(prefix, config->output);
938 evlist__for_each_entry(evlist, counter) {
Ian Rogers7365f102022-01-04 22:13:28 -0800939 u64 ena, run, val;
940 double uval;
941 struct aggr_cpu_id id;
942 int counter_idx = perf_cpu_map__idx(evsel__cpus(counter), cpu);
943
944 if (counter_idx < 0)
945 continue;
946
947 id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
Jiri Olsa088519f2018-08-30 08:32:52 +0200948 if (first) {
James Clark2760f5a12020-11-26 16:13:20 +0200949 aggr_printout(config, counter, id, 0);
Jiri Olsa088519f2018-08-30 08:32:52 +0200950 first = false;
951 }
Ian Rogers7365f102022-01-04 22:13:28 -0800952 val = perf_counts(counter->counts, counter_idx, 0)->val;
953 ena = perf_counts(counter->counts, counter_idx, 0)->ena;
954 run = perf_counts(counter->counts, counter_idx, 0)->run;
Jiri Olsa088519f2018-08-30 08:32:52 +0200955
956 uval = val * counter->scale;
James Clark2760f5a12020-11-26 16:13:20 +0200957 printout(config, id, 0, counter, uval, prefix,
958 run, ena, 1.0, &rt_stat);
Jiri Olsa088519f2018-08-30 08:32:52 +0200959 }
960 fputc('\n', config->output);
961 }
962}
963
964static int aggr_header_lens[] = {
Kan Liangdb5742b2019-06-04 15:50:42 -0700965 [AGGR_CORE] = 24,
966 [AGGR_DIE] = 18,
Jiri Olsa088519f2018-08-30 08:32:52 +0200967 [AGGR_SOCKET] = 12,
968 [AGGR_NONE] = 6,
969 [AGGR_THREAD] = 24,
970 [AGGR_GLOBAL] = 0,
971};
972
973static const char *aggr_header_csv[] = {
974 [AGGR_CORE] = "core,cpus,",
Kan Liangdb5742b2019-06-04 15:50:42 -0700975 [AGGR_DIE] = "die,cpus",
Jiri Olsa088519f2018-08-30 08:32:52 +0200976 [AGGR_SOCKET] = "socket,cpus",
977 [AGGR_NONE] = "cpu,",
978 [AGGR_THREAD] = "comm-pid,",
979 [AGGR_GLOBAL] = ""
980};
981
982static void print_metric_headers(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200983 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200984 const char *prefix, bool no_indent)
985{
986 struct perf_stat_output_ctx out;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200987 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +0200988 struct outstate os = {
989 .fh = config->output
990 };
991
992 if (prefix)
993 fprintf(config->output, "%s", prefix);
994
995 if (!config->csv_output && !no_indent)
996 fprintf(config->output, "%*s",
997 aggr_header_lens[config->aggr_mode], "");
998 if (config->csv_output) {
999 if (config->interval)
1000 fputs("time,", config->output);
Alexander Antonovf07952b2021-04-19 12:41:44 +03001001 if (!config->iostat_run)
1002 fputs(aggr_header_csv[config->aggr_mode], config->output);
Jiri Olsa088519f2018-08-30 08:32:52 +02001003 }
Alexander Antonovf07952b2021-04-19 12:41:44 +03001004 if (config->iostat_run)
1005 iostat_print_header_prefix(config);
Jiri Olsa088519f2018-08-30 08:32:52 +02001006
1007 /* Print metrics headers only */
1008 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +02001009 os.evsel = counter;
1010 out.ctx = &os;
1011 out.print_metric = print_metric_header;
1012 out.new_line = new_line_metric;
1013 out.force_header = true;
Jiri Olsa088519f2018-08-30 08:32:52 +02001014 perf_stat__print_shadow_stats(config, counter, 0,
1015 0,
1016 &out,
1017 &config->metric_events,
1018 &rt_stat);
1019 }
1020 fputc('\n', config->output);
1021}
1022
1023static void print_interval(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +02001024 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +02001025 char *prefix, struct timespec *ts)
1026{
1027 bool metric_only = config->metric_only;
1028 unsigned int unit_width = config->unit_width;
1029 FILE *output = config->output;
1030 static int num_print_interval;
1031
1032 if (config->interval_clear)
1033 puts(CONSOLE_CLEAR);
1034
Alexander Antonovf07952b2021-04-19 12:41:44 +03001035 if (!config->iostat_run)
1036 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
Jiri Olsa088519f2018-08-30 08:32:52 +02001037
1038 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
1039 switch (config->aggr_mode) {
Jiri Olsa86895b42019-08-28 10:17:43 +02001040 case AGGR_NODE:
1041 fprintf(output, "# time node cpus");
1042 if (!metric_only)
1043 fprintf(output, " counts %*s events\n", unit_width, "unit");
1044 break;
Jiri Olsa088519f2018-08-30 08:32:52 +02001045 case AGGR_SOCKET:
1046 fprintf(output, "# time socket cpus");
1047 if (!metric_only)
1048 fprintf(output, " counts %*s events\n", unit_width, "unit");
1049 break;
Kan Liangdb5742b2019-06-04 15:50:42 -07001050 case AGGR_DIE:
1051 fprintf(output, "# time die cpus");
1052 if (!metric_only)
1053 fprintf(output, " counts %*s events\n", unit_width, "unit");
1054 break;
Jiri Olsa088519f2018-08-30 08:32:52 +02001055 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -07001056 fprintf(output, "# time core cpus");
Jiri Olsa088519f2018-08-30 08:32:52 +02001057 if (!metric_only)
1058 fprintf(output, " counts %*s events\n", unit_width, "unit");
1059 break;
1060 case AGGR_NONE:
1061 fprintf(output, "# time CPU ");
1062 if (!metric_only)
1063 fprintf(output, " counts %*s events\n", unit_width, "unit");
1064 break;
1065 case AGGR_THREAD:
1066 fprintf(output, "# time comm-pid");
1067 if (!metric_only)
1068 fprintf(output, " counts %*s events\n", unit_width, "unit");
1069 break;
1070 case AGGR_GLOBAL:
1071 default:
Alexander Antonovf07952b2021-04-19 12:41:44 +03001072 if (!config->iostat_run) {
1073 fprintf(output, "# time");
1074 if (!metric_only)
1075 fprintf(output, " counts %*s events\n", unit_width, "unit");
1076 }
Jiri Olsa088519f2018-08-30 08:32:52 +02001077 case AGGR_UNSET:
1078 break;
1079 }
1080 }
1081
1082 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1083 print_metric_headers(config, evlist, " ", true);
1084 if (++num_print_interval == 25)
1085 num_print_interval = 0;
1086}
1087
1088static void print_header(struct perf_stat_config *config,
1089 struct target *_target,
1090 int argc, const char **argv)
1091{
1092 FILE *output = config->output;
1093 int i;
1094
1095 fflush(stdout);
1096
1097 if (!config->csv_output) {
1098 fprintf(output, "\n");
1099 fprintf(output, " Performance counter stats for ");
Song Liufa853c42020-12-29 13:42:14 -08001100 if (_target->bpf_str)
1101 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1102 else if (_target->system_wide)
Jiri Olsa088519f2018-08-30 08:32:52 +02001103 fprintf(output, "\'system wide");
1104 else if (_target->cpu_list)
1105 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1106 else if (!target__has_task(_target)) {
1107 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1108 for (i = 1; argv && (i < argc); i++)
1109 fprintf(output, " %s", argv[i]);
1110 } else if (_target->pid)
1111 fprintf(output, "process id \'%s", _target->pid);
1112 else
1113 fprintf(output, "thread id \'%s", _target->tid);
1114
1115 fprintf(output, "\'");
1116 if (config->run_count > 1)
1117 fprintf(output, " (%d runs)", config->run_count);
1118 fprintf(output, ":\n\n");
1119 }
1120}
1121
1122static int get_precision(double num)
1123{
1124 if (num > 1)
1125 return 0;
1126
1127 return lround(ceil(-log10(num)));
1128}
1129
1130static void print_table(struct perf_stat_config *config,
1131 FILE *output, int precision, double avg)
1132{
1133 char tmp[64];
1134 int idx, indent = 0;
1135
1136 scnprintf(tmp, 64, " %17.*f", precision, avg);
1137 while (tmp[indent] == ' ')
1138 indent++;
1139
1140 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1141
1142 for (idx = 0; idx < config->run_count; idx++) {
1143 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1144 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1145
1146 fprintf(output, " %17.*f (%+.*f) ",
1147 precision, run, precision, run - avg);
1148
1149 for (h = 0; h < n; h++)
1150 fprintf(output, "#");
1151
1152 fprintf(output, "\n");
1153 }
1154
1155 fprintf(output, "\n%*s# Final result:\n", indent, "");
1156}
1157
1158static double timeval2double(struct timeval *t)
1159{
1160 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1161}
1162
1163static void print_footer(struct perf_stat_config *config)
1164{
1165 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1166 FILE *output = config->output;
Jiri Olsa088519f2018-08-30 08:32:52 +02001167
1168 if (!config->null_run)
1169 fprintf(output, "\n");
1170
1171 if (config->run_count == 1) {
1172 fprintf(output, " %17.9f seconds time elapsed", avg);
1173
1174 if (config->ru_display) {
1175 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1176 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1177
1178 fprintf(output, "\n\n");
1179 fprintf(output, " %17.9f seconds user\n", ru_utime);
1180 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1181 }
1182 } else {
1183 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1184 /*
1185 * Display at most 2 more significant
1186 * digits than the stddev inaccuracy.
1187 */
1188 int precision = get_precision(sd) + 2;
1189
1190 if (config->walltime_run_table)
1191 print_table(config, output, precision, avg);
1192
1193 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1194 precision, avg, precision, sd);
1195
1196 print_noise_pct(config, sd, avg);
1197 }
1198 fprintf(output, "\n\n");
1199
Kan Liang2a14c1b2020-02-24 13:59:22 -08001200 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
Jiri Olsa088519f2018-08-30 08:32:52 +02001201 fprintf(output,
1202"Some events weren't counted. Try disabling the NMI watchdog:\n"
1203" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1204" perf stat ...\n"
1205" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1206
1207 if (config->print_mixed_hw_group_error)
1208 fprintf(output,
1209 "The events in group usually have to be from "
1210 "the same PMU. Try reorganizing the group.\n");
1211}
1212
Jin Yao1af62ce2020-02-14 16:04:52 +08001213static void print_percore_thread(struct perf_stat_config *config,
1214 struct evsel *counter, char *prefix)
1215{
James Clark2760f5a12020-11-26 16:13:20 +02001216 int s;
1217 struct aggr_cpu_id s2, id;
Ian Rogersa0232832022-01-04 22:13:07 -08001218 struct perf_cpu_map *cpus;
Jin Yao1af62ce2020-02-14 16:04:52 +08001219 bool first = true;
Ian Rogers6d188042022-01-04 22:13:51 -08001220 int idx;
1221 struct perf_cpu cpu;
Jin Yao1af62ce2020-02-14 16:04:52 +08001222
Ian Rogersa0232832022-01-04 22:13:07 -08001223 cpus = evsel__cpus(counter);
1224 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Ian Rogers88031a02022-01-04 22:13:08 -08001225 s2 = config->aggr_get_id(config, cpu);
Jin Yao1af62ce2020-02-14 16:04:52 +08001226 for (s = 0; s < config->aggr_map->nr; s++) {
James Clarkff523292020-11-26 16:13:23 +02001227 id = config->aggr_map->map[s];
Ian Rogers3ac23d12022-01-04 22:13:16 -08001228 if (aggr_cpu_id__equal(&s2, &id))
Jin Yao1af62ce2020-02-14 16:04:52 +08001229 break;
1230 }
1231
1232 print_counter_aggrdata(config, counter, s,
1233 prefix, false,
Ian Rogersa0232832022-01-04 22:13:07 -08001234 &first, cpu);
Jin Yao1af62ce2020-02-14 16:04:52 +08001235 }
1236}
1237
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001238static void print_percore(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001239 struct evsel *counter, char *prefix)
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001240{
1241 bool metric_only = config->metric_only;
1242 FILE *output = config->output;
1243 int s;
1244 bool first = true;
1245
Hongbo Yaoc0c652f2020-06-05 17:17:40 +08001246 if (!config->aggr_map || !config->aggr_get_id)
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001247 return;
1248
Jin Yao1af62ce2020-02-14 16:04:52 +08001249 if (config->percore_show_thread)
1250 return print_percore_thread(config, counter, prefix);
1251
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001252 for (s = 0; s < config->aggr_map->nr; s++) {
1253 if (prefix && metric_only)
1254 fprintf(output, "%s", prefix);
1255
1256 print_counter_aggrdata(config, counter, s,
Ian Rogers6d188042022-01-04 22:13:51 -08001257 prefix, metric_only,
1258 &first, (struct perf_cpu){ .cpu = -1 });
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001259 }
1260
1261 if (metric_only)
1262 fputc('\n', output);
1263}
1264
Arnaldo Carvalho de Melo71273722020-11-30 14:55:12 -03001265void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1266 struct target *_target, struct timespec *ts, int argc, const char **argv)
Jiri Olsa088519f2018-08-30 08:32:52 +02001267{
1268 bool metric_only = config->metric_only;
1269 int interval = config->interval;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001270 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +02001271 char buf[64], *prefix = NULL;
1272
Alexander Antonovf07952b2021-04-19 12:41:44 +03001273 if (config->iostat_run)
1274 evlist->selected = evlist__first(evlist);
1275
Jiri Olsa088519f2018-08-30 08:32:52 +02001276 if (interval)
1277 print_interval(config, evlist, prefix = buf, ts);
1278 else
1279 print_header(config, _target, argc, argv);
1280
1281 if (metric_only) {
1282 static int num_print_iv;
1283
1284 if (num_print_iv == 0 && !interval)
1285 print_metric_headers(config, evlist, prefix, false);
1286 if (num_print_iv++ == 25)
1287 num_print_iv = 0;
Alexander Antonovf07952b2021-04-19 12:41:44 +03001288 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
Jiri Olsa088519f2018-08-30 08:32:52 +02001289 fprintf(config->output, "%s", prefix);
1290 }
1291
1292 switch (config->aggr_mode) {
1293 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -07001294 case AGGR_DIE:
Jiri Olsa088519f2018-08-30 08:32:52 +02001295 case AGGR_SOCKET:
Jiri Olsa86895b42019-08-28 10:17:43 +02001296 case AGGR_NODE:
Jiri Olsa088519f2018-08-30 08:32:52 +02001297 print_aggr(config, evlist, prefix);
1298 break;
1299 case AGGR_THREAD:
1300 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +02001301 print_aggr_thread(config, _target, counter, prefix);
1302 }
1303 break;
1304 case AGGR_GLOBAL:
Alexander Antonovf07952b2021-04-19 12:41:44 +03001305 if (config->iostat_run)
1306 iostat_print_counters(evlist, config, ts, prefix = buf,
1307 print_counter_aggr);
1308 else {
1309 evlist__for_each_entry(evlist, counter) {
1310 print_counter_aggr(config, counter, prefix);
1311 }
1312 if (metric_only)
1313 fputc('\n', config->output);
Jiri Olsa088519f2018-08-30 08:32:52 +02001314 }
Jiri Olsa088519f2018-08-30 08:32:52 +02001315 break;
1316 case AGGR_NONE:
1317 if (metric_only)
1318 print_no_aggr_metric(config, evlist, prefix);
1319 else {
1320 evlist__for_each_entry(evlist, counter) {
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001321 if (counter->percore)
1322 print_percore(config, counter, prefix);
1323 else
1324 print_counter(config, counter, prefix);
Jiri Olsa088519f2018-08-30 08:32:52 +02001325 }
1326 }
1327 break;
1328 case AGGR_UNSET:
1329 default:
1330 break;
1331 }
1332
1333 if (!interval && !config->csv_output)
1334 print_footer(config);
1335
1336 fflush(config->output);
1337}