blob: 0241436bb1fb73ebdf7d70d7a8830c0779bacce4 [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>
Arnaldo Carvalho de Melo8a249c72019-01-22 10:47:38 -02007#include "color.h"
Arnaldo Carvalho de Melobfc49182019-08-21 14:02:05 -03008#include "counts.h"
Jiri Olsa088519f2018-08-30 08:32:52 +02009#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"
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030016#include <linux/ctype.h>
Jiri Olsa088519f2018-08-30 08:32:52 +020017#include "cgroup.h"
Jiri Olsa088519f2018-08-30 08:32:52 +020018#include <api/fs/fs.h>
Kan Liang2a14c1b2020-02-24 13:59:22 -080019#include "util.h"
Alexander Antonovf07952b2021-04-19 12:41:44 +030020#include "iostat.h"
Jin Yao12279422021-04-27 15:01:20 +080021#include "pmu-hybrid.h"
Jin Yao493be702021-06-10 11:45:57 +080022#include "evlist-hybrid.h"
Jiri Olsa088519f2018-08-30 08:32:52 +020023
24#define CNTR_NOT_SUPPORTED "<not supported>"
25#define CNTR_NOT_COUNTED "<not counted>"
26
Jiri Olsa088519f2018-08-30 08:32:52 +020027static void print_running(struct perf_stat_config *config,
28 u64 run, u64 ena)
29{
30 if (config->csv_output) {
31 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
32 config->csv_sep,
33 run,
34 config->csv_sep,
35 ena ? 100.0 * run / ena : 100.0);
36 } else if (run != ena) {
37 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
38 }
39}
40
41static void print_noise_pct(struct perf_stat_config *config,
42 double total, double avg)
43{
44 double pct = rel_stddev_stats(total, avg);
45
46 if (config->csv_output)
47 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
48 else if (pct)
49 fprintf(config->output, " ( +-%6.2f%% )", pct);
50}
51
52static void print_noise(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +020053 struct evsel *evsel, double avg)
Jiri Olsa088519f2018-08-30 08:32:52 +020054{
55 struct perf_stat_evsel *ps;
56
57 if (config->run_count == 1)
58 return;
59
60 ps = evsel->stats;
61 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
62}
63
Jiri Olsa32dcd022019-07-21 13:23:51 +020064static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
Stephane Eranianbc4da382018-11-07 02:50:45 -080065{
66 if (nr_cgroups) {
67 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
68 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
69 }
70}
71
72
Jiri Olsa088519f2018-08-30 08:32:52 +020073static void aggr_printout(struct perf_stat_config *config,
James Clark2760f5a12020-11-26 16:13:20 +020074 struct evsel *evsel, struct aggr_cpu_id id, int nr)
Jiri Olsa088519f2018-08-30 08:32:52 +020075{
76 switch (config->aggr_mode) {
77 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -070078 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
James Clark1a270cb2020-11-26 16:13:25 +020079 id.socket,
James Clarkba2ee162020-11-26 16:13:26 +020080 id.die,
Jiri Olsa088519f2018-08-30 08:32:52 +020081 config->csv_output ? 0 : -8,
James Clarkb9933812020-11-26 16:13:27 +020082 id.core,
Jiri Olsa088519f2018-08-30 08:32:52 +020083 config->csv_sep,
84 config->csv_output ? 0 : 4,
85 nr,
86 config->csv_sep);
87 break;
Kan Liangdb5742b2019-06-04 15:50:42 -070088 case AGGR_DIE:
89 fprintf(config->output, "S%d-D%*d%s%*d%s",
James Clark1a270cb2020-11-26 16:13:25 +020090 id.socket,
Kan Liangdb5742b2019-06-04 15:50:42 -070091 config->csv_output ? 0 : -8,
James Clarkba2ee162020-11-26 16:13:26 +020092 id.die,
Kan Liangdb5742b2019-06-04 15:50:42 -070093 config->csv_sep,
94 config->csv_output ? 0 : 4,
95 nr,
96 config->csv_sep);
97 break;
Jiri Olsa088519f2018-08-30 08:32:52 +020098 case AGGR_SOCKET:
99 fprintf(config->output, "S%*d%s%*d%s",
100 config->csv_output ? 0 : -5,
James Clark1a270cb2020-11-26 16:13:25 +0200101 id.socket,
Jiri Olsa088519f2018-08-30 08:32:52 +0200102 config->csv_sep,
103 config->csv_output ? 0 : 4,
104 nr,
105 config->csv_sep);
106 break;
Jiri Olsa86895b42019-08-28 10:17:43 +0200107 case AGGR_NODE:
108 fprintf(config->output, "N%*d%s%*d%s",
109 config->csv_output ? 0 : -5,
James Clarkfcd83a32020-11-26 16:13:24 +0200110 id.node,
Jiri Olsa86895b42019-08-28 10:17:43 +0200111 config->csv_sep,
112 config->csv_output ? 0 : 4,
113 nr,
114 config->csv_sep);
115 break;
Jiri Olsa088519f2018-08-30 08:32:52 +0200116 case AGGR_NONE:
Jin Yao1af62ce2020-02-14 16:04:52 +0800117 if (evsel->percore && !config->percore_show_thread) {
Kan Liangdb5742b2019-06-04 15:50:42 -0700118 fprintf(config->output, "S%d-D%d-C%*d%s",
James Clark1a270cb2020-11-26 16:13:25 +0200119 id.socket,
James Clarkba2ee162020-11-26 16:13:26 +0200120 id.die,
Jin Yaod13e9e42020-02-18 15:16:14 +0800121 config->csv_output ? 0 : -3,
James Clarkb9933812020-11-26 16:13:27 +0200122 id.core, config->csv_sep);
123 } else if (id.core > -1) {
Jin Yaod13e9e42020-02-18 15:16:14 +0800124 fprintf(config->output, "CPU%*d%s",
125 config->csv_output ? 0 : -7,
James Clarkb9933812020-11-26 16:13:27 +0200126 evsel__cpus(evsel)->map[id.core],
Jin Yao4fc4d8d2019-04-12 21:59:49 +0800127 config->csv_sep);
128 }
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
330static int first_shadow_cpu(struct perf_stat_config *config,
Ian Rogers3ac23d12022-01-04 22:13:16 -0800331 struct evsel *evsel, const struct aggr_cpu_id *id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200332{
Ian Rogersa0232832022-01-04 22:13:07 -0800333 struct perf_cpu_map *cpus;
334 int cpu, idx;
Jiri Olsa088519f2018-08-30 08:32:52 +0200335
Jiri Olsa088519f2018-08-30 08:32:52 +0200336 if (config->aggr_mode == AGGR_NONE)
Ian Rogers3ac23d12022-01-04 22:13:16 -0800337 return id->core;
Jiri Olsa088519f2018-08-30 08:32:52 +0200338
Namhyung Kimc0ee1d5a2020-11-27 13:14:03 +0900339 if (!config->aggr_get_id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200340 return 0;
341
Ian Rogersa0232832022-01-04 22:13:07 -0800342 cpus = evsel__cpus(evsel);
343 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 Rogersa0232832022-01-04 22:13:07 -0800347 return cpu;
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 Rogers3ac23d12022-01-04 22:13:16 -0800506 first_shadow_cpu(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 Rogersa0232832022-01-04 22:13:07 -0800517 int cpu, idx, s;
James Clark2760f5a12020-11-26 16:13:20 +0200518 struct aggr_cpu_id s2, id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200519 u64 val;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200520 struct evsel *counter;
Ian Rogersa0232832022-01-04 22:13:07 -0800521 struct perf_cpu_map *cpus;
Jiri Olsa088519f2018-08-30 08:32:52 +0200522
523 for (s = 0; s < config->aggr_map->nr; s++) {
James Clarkff523292020-11-26 16:13:23 +0200524 id = config->aggr_map->map[s];
Jiri Olsa088519f2018-08-30 08:32:52 +0200525 evlist__for_each_entry(evlist, counter) {
Ian Rogersa0232832022-01-04 22:13:07 -0800526 cpus = evsel__cpus(counter);
Jiri Olsa088519f2018-08-30 08:32:52 +0200527 val = 0;
Ian Rogersa0232832022-01-04 22:13:07 -0800528 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Ian Rogers88031a02022-01-04 22:13:08 -0800529 s2 = config->aggr_get_id(config, cpu);
Ian Rogers3ac23d12022-01-04 22:13:16 -0800530 if (!aggr_cpu_id__equal(&s2, &id))
Jiri Olsa088519f2018-08-30 08:32:52 +0200531 continue;
Ian Rogersa0232832022-01-04 22:13:07 -0800532 val += perf_counts(counter->counts, idx, 0)->val;
Jiri Olsa088519f2018-08-30 08:32:52 +0200533 }
534 perf_stat__update_shadow_stats(counter, val,
Ian Rogers3ac23d12022-01-04 22:13:16 -0800535 first_shadow_cpu(config, counter, &id),
Jiri Olsa088519f2018-08-30 08:32:52 +0200536 &rt_stat);
537 }
538 }
539}
540
Jiri Olsa32dcd022019-07-21 13:23:51 +0200541static void uniquify_event_name(struct evsel *counter)
Jiri Olsa088519f2018-08-30 08:32:52 +0200542{
543 char *new_name;
544 char *config;
Jin Yao12279422021-04-27 15:01:20 +0800545 int ret = 0;
Jiri Olsa088519f2018-08-30 08:32:52 +0200546
Namhyung Kim3cc84392021-06-02 14:22:41 -0700547 if (counter->uniquified_name || counter->use_config_name ||
Jiri Olsa088519f2018-08-30 08:32:52 +0200548 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
549 strlen(counter->pmu_name)))
550 return;
551
552 config = strchr(counter->name, '/');
553 if (config) {
554 if (asprintf(&new_name,
555 "%s%s", counter->pmu_name, config) > 0) {
556 free(counter->name);
557 counter->name = new_name;
558 }
559 } else {
Jin Yao12279422021-04-27 15:01:20 +0800560 if (perf_pmu__has_hybrid()) {
Namhyung Kim3cc84392021-06-02 14:22:41 -0700561 ret = asprintf(&new_name, "%s/%s/",
562 counter->pmu_name, counter->name);
Jin Yao12279422021-04-27 15:01:20 +0800563 } else {
564 ret = asprintf(&new_name, "%s [%s]",
565 counter->name, counter->pmu_name);
566 }
567
568 if (ret) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200569 free(counter->name);
570 counter->name = new_name;
571 }
572 }
573
574 counter->uniquified_name = true;
575}
576
Jiri Olsa32dcd022019-07-21 13:23:51 +0200577static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
578 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200579 bool first),
580 void *data)
581{
Jiri Olsa63503db2019-07-21 13:23:52 +0200582 struct evlist *evlist = counter->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200583 struct evsel *alias;
Jiri Olsa088519f2018-08-30 08:32:52 +0200584
Jiri Olsace9036a2019-07-21 13:24:23 +0200585 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
586 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300587 if (strcmp(evsel__name(alias), evsel__name(counter)) ||
Jiri Olsa088519f2018-08-30 08:32:52 +0200588 alias->scale != counter->scale ||
589 alias->cgrp != counter->cgrp ||
590 strcmp(alias->unit, counter->unit) ||
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300591 evsel__is_clock(alias) != evsel__is_clock(counter) ||
Andi Kleen6c5f4e52019-06-24 12:37:09 -0700592 !strcmp(alias->pmu_name, counter->pmu_name))
Jiri Olsa088519f2018-08-30 08:32:52 +0200593 break;
594 alias->merged_stat = true;
595 cb(config, alias, data, false);
596 }
597}
598
Jin Yaoe0a7ef22021-07-07 13:56:52 +0800599static bool is_uncore(struct evsel *evsel)
600{
601 struct perf_pmu *pmu = evsel__find_pmu(evsel);
602
603 return pmu && pmu->is_uncore;
604}
605
606static bool hybrid_uniquify(struct evsel *evsel)
607{
608 return perf_pmu__has_hybrid() && !is_uncore(evsel);
609}
610
Jiri Olsa32dcd022019-07-21 13:23:51 +0200611static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
612 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200613 bool first),
614 void *data)
615{
616 if (counter->merged_stat)
617 return false;
618 cb(config, counter, data, true);
Jin Yaoe0a7ef22021-07-07 13:56:52 +0800619 if (config->no_merge || hybrid_uniquify(counter))
Jiri Olsa088519f2018-08-30 08:32:52 +0200620 uniquify_event_name(counter);
621 else if (counter->auto_merge_stats)
622 collect_all_aliases(config, counter, cb, data);
623 return true;
624}
625
626struct aggr_data {
627 u64 ena, run, val;
James Clark2760f5a12020-11-26 16:13:20 +0200628 struct aggr_cpu_id id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200629 int nr;
630 int cpu;
631};
632
633static void aggr_cb(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200634 struct evsel *counter, void *data, bool first)
Jiri Olsa088519f2018-08-30 08:32:52 +0200635{
636 struct aggr_data *ad = data;
Ian Rogersa0232832022-01-04 22:13:07 -0800637 int idx, cpu;
638 struct perf_cpu_map *cpus;
James Clark2760f5a12020-11-26 16:13:20 +0200639 struct aggr_cpu_id s2;
Jiri Olsa088519f2018-08-30 08:32:52 +0200640
Ian Rogersa0232832022-01-04 22:13:07 -0800641 cpus = evsel__cpus(counter);
642 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200643 struct perf_counts_values *counts;
644
Ian Rogers88031a02022-01-04 22:13:08 -0800645 s2 = config->aggr_get_id(config, cpu);
Ian Rogers3ac23d12022-01-04 22:13:16 -0800646 if (!aggr_cpu_id__equal(&s2, &ad->id))
Jiri Olsa088519f2018-08-30 08:32:52 +0200647 continue;
648 if (first)
649 ad->nr++;
Ian Rogersa0232832022-01-04 22:13:07 -0800650 counts = perf_counts(counter->counts, idx, 0);
Jiri Olsa088519f2018-08-30 08:32:52 +0200651 /*
652 * When any result is bad, make them all to give
653 * consistent output in interval mode.
654 */
655 if (counts->ena == 0 || counts->run == 0 ||
656 counter->counts->scaled == -1) {
657 ad->ena = 0;
658 ad->run = 0;
659 break;
660 }
661 ad->val += counts->val;
662 ad->ena += counts->ena;
663 ad->run += counts->run;
664 }
665}
666
Jin Yao40480a82019-04-12 21:59:48 +0800667static void print_counter_aggrdata(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200668 struct evsel *counter, int s,
Jin Yao40480a82019-04-12 21:59:48 +0800669 char *prefix, bool metric_only,
Jin Yao1af62ce2020-02-14 16:04:52 +0800670 bool *first, int cpu)
Jin Yao40480a82019-04-12 21:59:48 +0800671{
672 struct aggr_data ad;
673 FILE *output = config->output;
674 u64 ena, run, val;
James Clark2760f5a12020-11-26 16:13:20 +0200675 int nr;
676 struct aggr_cpu_id id;
Jin Yao40480a82019-04-12 21:59:48 +0800677 double uval;
678
James Clarkff523292020-11-26 16:13:23 +0200679 ad.id = id = config->aggr_map->map[s];
Jin Yao40480a82019-04-12 21:59:48 +0800680 ad.val = ad.ena = ad.run = 0;
681 ad.nr = 0;
682 if (!collect_data(config, counter, aggr_cb, &ad))
683 return;
684
Jin Yao92637cc2021-04-27 15:01:28 +0800685 if (perf_pmu__has_hybrid() && ad.ena == 0)
686 return;
687
Jin Yao40480a82019-04-12 21:59:48 +0800688 nr = ad.nr;
689 ena = ad.ena;
690 run = ad.run;
691 val = ad.val;
692 if (*first && metric_only) {
693 *first = false;
694 aggr_printout(config, counter, id, nr);
695 }
696 if (prefix && !metric_only)
697 fprintf(output, "%s", prefix);
698
699 uval = val * counter->scale;
James Clark2760f5a12020-11-26 16:13:20 +0200700 if (cpu != -1) {
701 id = cpu_map__empty_aggr_cpu_id();
James Clarkb9933812020-11-26 16:13:27 +0200702 id.core = cpu;
James Clark2760f5a12020-11-26 16:13:20 +0200703 }
704 printout(config, id, nr, counter, uval,
705 prefix, run, ena, 1.0, &rt_stat);
Jin Yao40480a82019-04-12 21:59:48 +0800706 if (!metric_only)
707 fputc('\n', output);
708}
709
Jiri Olsa088519f2018-08-30 08:32:52 +0200710static void print_aggr(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200711 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200712 char *prefix)
713{
714 bool metric_only = config->metric_only;
715 FILE *output = config->output;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200716 struct evsel *counter;
Jin Yao40480a82019-04-12 21:59:48 +0800717 int s;
Jiri Olsa088519f2018-08-30 08:32:52 +0200718 bool first;
719
Hongbo Yaoc0c652f2020-06-05 17:17:40 +0800720 if (!config->aggr_map || !config->aggr_get_id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200721 return;
722
723 aggr_update_shadow(config, evlist);
724
725 /*
726 * With metric_only everything is on a single line.
727 * Without each counter has its own line.
728 */
729 for (s = 0; s < config->aggr_map->nr; s++) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200730 if (prefix && metric_only)
731 fprintf(output, "%s", prefix);
732
Jiri Olsa088519f2018-08-30 08:32:52 +0200733 first = true;
734 evlist__for_each_entry(evlist, counter) {
Jin Yao40480a82019-04-12 21:59:48 +0800735 print_counter_aggrdata(config, counter, s,
736 prefix, metric_only,
Jin Yao1af62ce2020-02-14 16:04:52 +0800737 &first, -1);
Jiri Olsa088519f2018-08-30 08:32:52 +0200738 }
739 if (metric_only)
740 fputc('\n', output);
741 }
742}
743
744static int cmp_val(const void *a, const void *b)
745{
746 return ((struct perf_aggr_thread_value *)b)->val -
747 ((struct perf_aggr_thread_value *)a)->val;
748}
749
750static struct perf_aggr_thread_value *sort_aggr_thread(
Jiri Olsa32dcd022019-07-21 13:23:51 +0200751 struct evsel *counter,
Jiri Olsa088519f2018-08-30 08:32:52 +0200752 int nthreads, int ncpus,
753 int *ret,
754 struct target *_target)
755{
756 int cpu, thread, i = 0;
757 double uval;
758 struct perf_aggr_thread_value *buf;
759
760 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
761 if (!buf)
762 return NULL;
763
764 for (thread = 0; thread < nthreads; thread++) {
765 u64 ena = 0, run = 0, val = 0;
766
767 for (cpu = 0; cpu < ncpus; cpu++) {
768 val += perf_counts(counter->counts, cpu, thread)->val;
769 ena += perf_counts(counter->counts, cpu, thread)->ena;
770 run += perf_counts(counter->counts, cpu, thread)->run;
771 }
772
773 uval = val * counter->scale;
774
775 /*
776 * Skip value 0 when enabling --per-thread globally,
777 * otherwise too many 0 output.
778 */
779 if (uval == 0.0 && target__has_per_thread(_target))
780 continue;
781
782 buf[i].counter = counter;
James Clark2760f5a12020-11-26 16:13:20 +0200783 buf[i].id = cpu_map__empty_aggr_cpu_id();
James Clark8d4852b2020-11-26 16:13:28 +0200784 buf[i].id.thread = thread;
Jiri Olsa088519f2018-08-30 08:32:52 +0200785 buf[i].uval = uval;
786 buf[i].val = val;
787 buf[i].run = run;
788 buf[i].ena = ena;
789 i++;
790 }
791
792 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
793
794 if (ret)
795 *ret = i;
796
797 return buf;
798}
799
800static void print_aggr_thread(struct perf_stat_config *config,
801 struct target *_target,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200802 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200803{
804 FILE *output = config->output;
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200805 int nthreads = perf_thread_map__nr(counter->core.threads);
Jiri Olsa6549cd82019-08-22 13:11:38 +0200806 int ncpus = perf_cpu_map__nr(counter->core.cpus);
James Clark2760f5a12020-11-26 16:13:20 +0200807 int thread, sorted_threads;
808 struct aggr_cpu_id id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200809 struct perf_aggr_thread_value *buf;
810
811 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
812 if (!buf) {
813 perror("cannot sort aggr thread");
814 return;
815 }
816
817 for (thread = 0; thread < sorted_threads; thread++) {
818 if (prefix)
819 fprintf(output, "%s", prefix);
820
821 id = buf[thread].id;
822 if (config->stats)
823 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
824 prefix, buf[thread].run, buf[thread].ena, 1.0,
James Clark8d4852b2020-11-26 16:13:28 +0200825 &config->stats[id.thread]);
Jiri Olsa088519f2018-08-30 08:32:52 +0200826 else
827 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
828 prefix, buf[thread].run, buf[thread].ena, 1.0,
829 &rt_stat);
830 fputc('\n', output);
831 }
832
833 free(buf);
834}
835
836struct caggr_data {
837 double avg, avg_enabled, avg_running;
838};
839
840static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200841 struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200842 bool first __maybe_unused)
843{
844 struct caggr_data *cd = data;
Namhyung Kim07b747f2021-04-22 19:38:33 -0700845 struct perf_counts_values *aggr = &counter->counts->aggr;
Jiri Olsa088519f2018-08-30 08:32:52 +0200846
Namhyung Kim07b747f2021-04-22 19:38:33 -0700847 cd->avg += aggr->val;
848 cd->avg_enabled += aggr->ena;
849 cd->avg_running += aggr->run;
Jiri Olsa088519f2018-08-30 08:32:52 +0200850}
851
852/*
853 * Print out the results of a single counter:
854 * aggregated counts in system-wide mode
855 */
856static void print_counter_aggr(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200857 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200858{
859 bool metric_only = config->metric_only;
860 FILE *output = config->output;
861 double uval;
862 struct caggr_data cd = { .avg = 0.0 };
863
864 if (!collect_data(config, counter, counter_aggr_cb, &cd))
865 return;
866
867 if (prefix && !metric_only)
868 fprintf(output, "%s", prefix);
869
870 uval = cd.avg * counter->scale;
James Clark2760f5a12020-11-26 16:13:20 +0200871 printout(config, cpu_map__empty_aggr_cpu_id(), 0, counter, uval, prefix, cd.avg_running,
872 cd.avg_enabled, cd.avg, &rt_stat);
Jiri Olsa088519f2018-08-30 08:32:52 +0200873 if (!metric_only)
874 fprintf(output, "\n");
875}
876
877static void counter_cb(struct perf_stat_config *config __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200878 struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200879 bool first __maybe_unused)
880{
881 struct aggr_data *ad = data;
882
883 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
884 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
885 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
886}
887
888/*
889 * Print out the results of a single counter:
890 * does not use aggregated count in system-wide
891 */
892static void print_counter(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200893 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200894{
895 FILE *output = config->output;
896 u64 ena, run, val;
897 double uval;
898 int cpu;
James Clark2760f5a12020-11-26 16:13:20 +0200899 struct aggr_cpu_id id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200900
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -0300901 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200902 struct aggr_data ad = { .cpu = cpu };
903
904 if (!collect_data(config, counter, counter_cb, &ad))
905 return;
906 val = ad.val;
907 ena = ad.ena;
908 run = ad.run;
909
910 if (prefix)
911 fprintf(output, "%s", prefix);
912
913 uval = val * counter->scale;
James Clark2760f5a12020-11-26 16:13:20 +0200914 id = cpu_map__empty_aggr_cpu_id();
James Clarkb9933812020-11-26 16:13:27 +0200915 id.core = cpu;
James Clark2760f5a12020-11-26 16:13:20 +0200916 printout(config, id, 0, counter, uval, prefix,
917 run, ena, 1.0, &rt_stat);
Jiri Olsa088519f2018-08-30 08:32:52 +0200918
919 fputc('\n', output);
920 }
921}
922
923static void print_no_aggr_metric(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200924 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200925 char *prefix)
926{
927 int cpu;
928 int nrcpus = 0;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200929 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +0200930 u64 ena, run, val;
931 double uval;
James Clark2760f5a12020-11-26 16:13:20 +0200932 struct aggr_cpu_id id;
Jiri Olsa088519f2018-08-30 08:32:52 +0200933
Jiri Olsaf72f9012019-07-21 13:24:41 +0200934 nrcpus = evlist->core.cpus->nr;
Jiri Olsa088519f2018-08-30 08:32:52 +0200935 for (cpu = 0; cpu < nrcpus; cpu++) {
936 bool first = true;
937
938 if (prefix)
939 fputs(prefix, config->output);
940 evlist__for_each_entry(evlist, counter) {
James Clark2760f5a12020-11-26 16:13:20 +0200941 id = cpu_map__empty_aggr_cpu_id();
James Clarkb9933812020-11-26 16:13:27 +0200942 id.core = cpu;
Jiri Olsa088519f2018-08-30 08:32:52 +0200943 if (first) {
James Clark2760f5a12020-11-26 16:13:20 +0200944 aggr_printout(config, counter, id, 0);
Jiri Olsa088519f2018-08-30 08:32:52 +0200945 first = false;
946 }
947 val = perf_counts(counter->counts, cpu, 0)->val;
948 ena = perf_counts(counter->counts, cpu, 0)->ena;
949 run = perf_counts(counter->counts, cpu, 0)->run;
950
951 uval = val * counter->scale;
James Clark2760f5a12020-11-26 16:13:20 +0200952 printout(config, id, 0, counter, uval, prefix,
953 run, ena, 1.0, &rt_stat);
Jiri Olsa088519f2018-08-30 08:32:52 +0200954 }
955 fputc('\n', config->output);
956 }
957}
958
959static int aggr_header_lens[] = {
Kan Liangdb5742b2019-06-04 15:50:42 -0700960 [AGGR_CORE] = 24,
961 [AGGR_DIE] = 18,
Jiri Olsa088519f2018-08-30 08:32:52 +0200962 [AGGR_SOCKET] = 12,
963 [AGGR_NONE] = 6,
964 [AGGR_THREAD] = 24,
965 [AGGR_GLOBAL] = 0,
966};
967
968static const char *aggr_header_csv[] = {
969 [AGGR_CORE] = "core,cpus,",
Kan Liangdb5742b2019-06-04 15:50:42 -0700970 [AGGR_DIE] = "die,cpus",
Jiri Olsa088519f2018-08-30 08:32:52 +0200971 [AGGR_SOCKET] = "socket,cpus",
972 [AGGR_NONE] = "cpu,",
973 [AGGR_THREAD] = "comm-pid,",
974 [AGGR_GLOBAL] = ""
975};
976
977static void print_metric_headers(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200978 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200979 const char *prefix, bool no_indent)
980{
981 struct perf_stat_output_ctx out;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200982 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +0200983 struct outstate os = {
984 .fh = config->output
985 };
986
987 if (prefix)
988 fprintf(config->output, "%s", prefix);
989
990 if (!config->csv_output && !no_indent)
991 fprintf(config->output, "%*s",
992 aggr_header_lens[config->aggr_mode], "");
993 if (config->csv_output) {
994 if (config->interval)
995 fputs("time,", config->output);
Alexander Antonovf07952b2021-04-19 12:41:44 +0300996 if (!config->iostat_run)
997 fputs(aggr_header_csv[config->aggr_mode], config->output);
Jiri Olsa088519f2018-08-30 08:32:52 +0200998 }
Alexander Antonovf07952b2021-04-19 12:41:44 +0300999 if (config->iostat_run)
1000 iostat_print_header_prefix(config);
Jiri Olsa088519f2018-08-30 08:32:52 +02001001
1002 /* Print metrics headers only */
1003 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +02001004 os.evsel = counter;
1005 out.ctx = &os;
1006 out.print_metric = print_metric_header;
1007 out.new_line = new_line_metric;
1008 out.force_header = true;
Jiri Olsa088519f2018-08-30 08:32:52 +02001009 perf_stat__print_shadow_stats(config, counter, 0,
1010 0,
1011 &out,
1012 &config->metric_events,
1013 &rt_stat);
1014 }
1015 fputc('\n', config->output);
1016}
1017
1018static void print_interval(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +02001019 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +02001020 char *prefix, struct timespec *ts)
1021{
1022 bool metric_only = config->metric_only;
1023 unsigned int unit_width = config->unit_width;
1024 FILE *output = config->output;
1025 static int num_print_interval;
1026
1027 if (config->interval_clear)
1028 puts(CONSOLE_CLEAR);
1029
Alexander Antonovf07952b2021-04-19 12:41:44 +03001030 if (!config->iostat_run)
1031 sprintf(prefix, "%6lu.%09lu%s", (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
Jiri Olsa088519f2018-08-30 08:32:52 +02001032
1033 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
1034 switch (config->aggr_mode) {
Jiri Olsa86895b42019-08-28 10:17:43 +02001035 case AGGR_NODE:
1036 fprintf(output, "# time node cpus");
1037 if (!metric_only)
1038 fprintf(output, " counts %*s events\n", unit_width, "unit");
1039 break;
Jiri Olsa088519f2018-08-30 08:32:52 +02001040 case AGGR_SOCKET:
1041 fprintf(output, "# time socket cpus");
1042 if (!metric_only)
1043 fprintf(output, " counts %*s events\n", unit_width, "unit");
1044 break;
Kan Liangdb5742b2019-06-04 15:50:42 -07001045 case AGGR_DIE:
1046 fprintf(output, "# time die cpus");
1047 if (!metric_only)
1048 fprintf(output, " counts %*s events\n", unit_width, "unit");
1049 break;
Jiri Olsa088519f2018-08-30 08:32:52 +02001050 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -07001051 fprintf(output, "# time core cpus");
Jiri Olsa088519f2018-08-30 08:32:52 +02001052 if (!metric_only)
1053 fprintf(output, " counts %*s events\n", unit_width, "unit");
1054 break;
1055 case AGGR_NONE:
1056 fprintf(output, "# time CPU ");
1057 if (!metric_only)
1058 fprintf(output, " counts %*s events\n", unit_width, "unit");
1059 break;
1060 case AGGR_THREAD:
1061 fprintf(output, "# time comm-pid");
1062 if (!metric_only)
1063 fprintf(output, " counts %*s events\n", unit_width, "unit");
1064 break;
1065 case AGGR_GLOBAL:
1066 default:
Alexander Antonovf07952b2021-04-19 12:41:44 +03001067 if (!config->iostat_run) {
1068 fprintf(output, "# time");
1069 if (!metric_only)
1070 fprintf(output, " counts %*s events\n", unit_width, "unit");
1071 }
Jiri Olsa088519f2018-08-30 08:32:52 +02001072 case AGGR_UNSET:
1073 break;
1074 }
1075 }
1076
1077 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1078 print_metric_headers(config, evlist, " ", true);
1079 if (++num_print_interval == 25)
1080 num_print_interval = 0;
1081}
1082
1083static void print_header(struct perf_stat_config *config,
1084 struct target *_target,
1085 int argc, const char **argv)
1086{
1087 FILE *output = config->output;
1088 int i;
1089
1090 fflush(stdout);
1091
1092 if (!config->csv_output) {
1093 fprintf(output, "\n");
1094 fprintf(output, " Performance counter stats for ");
Song Liufa853c42020-12-29 13:42:14 -08001095 if (_target->bpf_str)
1096 fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1097 else if (_target->system_wide)
Jiri Olsa088519f2018-08-30 08:32:52 +02001098 fprintf(output, "\'system wide");
1099 else if (_target->cpu_list)
1100 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1101 else if (!target__has_task(_target)) {
1102 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1103 for (i = 1; argv && (i < argc); i++)
1104 fprintf(output, " %s", argv[i]);
1105 } else if (_target->pid)
1106 fprintf(output, "process id \'%s", _target->pid);
1107 else
1108 fprintf(output, "thread id \'%s", _target->tid);
1109
1110 fprintf(output, "\'");
1111 if (config->run_count > 1)
1112 fprintf(output, " (%d runs)", config->run_count);
1113 fprintf(output, ":\n\n");
1114 }
1115}
1116
1117static int get_precision(double num)
1118{
1119 if (num > 1)
1120 return 0;
1121
1122 return lround(ceil(-log10(num)));
1123}
1124
1125static void print_table(struct perf_stat_config *config,
1126 FILE *output, int precision, double avg)
1127{
1128 char tmp[64];
1129 int idx, indent = 0;
1130
1131 scnprintf(tmp, 64, " %17.*f", precision, avg);
1132 while (tmp[indent] == ' ')
1133 indent++;
1134
1135 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1136
1137 for (idx = 0; idx < config->run_count; idx++) {
1138 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1139 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1140
1141 fprintf(output, " %17.*f (%+.*f) ",
1142 precision, run, precision, run - avg);
1143
1144 for (h = 0; h < n; h++)
1145 fprintf(output, "#");
1146
1147 fprintf(output, "\n");
1148 }
1149
1150 fprintf(output, "\n%*s# Final result:\n", indent, "");
1151}
1152
1153static double timeval2double(struct timeval *t)
1154{
1155 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1156}
1157
1158static void print_footer(struct perf_stat_config *config)
1159{
1160 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1161 FILE *output = config->output;
Jiri Olsa088519f2018-08-30 08:32:52 +02001162
1163 if (!config->null_run)
1164 fprintf(output, "\n");
1165
1166 if (config->run_count == 1) {
1167 fprintf(output, " %17.9f seconds time elapsed", avg);
1168
1169 if (config->ru_display) {
1170 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1171 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1172
1173 fprintf(output, "\n\n");
1174 fprintf(output, " %17.9f seconds user\n", ru_utime);
1175 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1176 }
1177 } else {
1178 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1179 /*
1180 * Display at most 2 more significant
1181 * digits than the stddev inaccuracy.
1182 */
1183 int precision = get_precision(sd) + 2;
1184
1185 if (config->walltime_run_table)
1186 print_table(config, output, precision, avg);
1187
1188 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1189 precision, avg, precision, sd);
1190
1191 print_noise_pct(config, sd, avg);
1192 }
1193 fprintf(output, "\n\n");
1194
Kan Liang2a14c1b2020-02-24 13:59:22 -08001195 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
Jiri Olsa088519f2018-08-30 08:32:52 +02001196 fprintf(output,
1197"Some events weren't counted. Try disabling the NMI watchdog:\n"
1198" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1199" perf stat ...\n"
1200" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1201
1202 if (config->print_mixed_hw_group_error)
1203 fprintf(output,
1204 "The events in group usually have to be from "
1205 "the same PMU. Try reorganizing the group.\n");
1206}
1207
Jin Yao1af62ce2020-02-14 16:04:52 +08001208static void print_percore_thread(struct perf_stat_config *config,
1209 struct evsel *counter, char *prefix)
1210{
James Clark2760f5a12020-11-26 16:13:20 +02001211 int s;
1212 struct aggr_cpu_id s2, id;
Ian Rogersa0232832022-01-04 22:13:07 -08001213 struct perf_cpu_map *cpus;
Jin Yao1af62ce2020-02-14 16:04:52 +08001214 bool first = true;
Ian Rogersa0232832022-01-04 22:13:07 -08001215 int idx, cpu;
Jin Yao1af62ce2020-02-14 16:04:52 +08001216
Ian Rogersa0232832022-01-04 22:13:07 -08001217 cpus = evsel__cpus(counter);
1218 perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Ian Rogers88031a02022-01-04 22:13:08 -08001219 s2 = config->aggr_get_id(config, cpu);
Jin Yao1af62ce2020-02-14 16:04:52 +08001220 for (s = 0; s < config->aggr_map->nr; s++) {
James Clarkff523292020-11-26 16:13:23 +02001221 id = config->aggr_map->map[s];
Ian Rogers3ac23d12022-01-04 22:13:16 -08001222 if (aggr_cpu_id__equal(&s2, &id))
Jin Yao1af62ce2020-02-14 16:04:52 +08001223 break;
1224 }
1225
1226 print_counter_aggrdata(config, counter, s,
1227 prefix, false,
Ian Rogersa0232832022-01-04 22:13:07 -08001228 &first, cpu);
Jin Yao1af62ce2020-02-14 16:04:52 +08001229 }
1230}
1231
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001232static void print_percore(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001233 struct evsel *counter, char *prefix)
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001234{
1235 bool metric_only = config->metric_only;
1236 FILE *output = config->output;
1237 int s;
1238 bool first = true;
1239
Hongbo Yaoc0c652f2020-06-05 17:17:40 +08001240 if (!config->aggr_map || !config->aggr_get_id)
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001241 return;
1242
Jin Yao1af62ce2020-02-14 16:04:52 +08001243 if (config->percore_show_thread)
1244 return print_percore_thread(config, counter, prefix);
1245
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001246 for (s = 0; s < config->aggr_map->nr; s++) {
1247 if (prefix && metric_only)
1248 fprintf(output, "%s", prefix);
1249
1250 print_counter_aggrdata(config, counter, s,
1251 prefix, metric_only,
Jin Yao1af62ce2020-02-14 16:04:52 +08001252 &first, -1);
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001253 }
1254
1255 if (metric_only)
1256 fputc('\n', output);
1257}
1258
Arnaldo Carvalho de Melo71273722020-11-30 14:55:12 -03001259void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1260 struct target *_target, struct timespec *ts, int argc, const char **argv)
Jiri Olsa088519f2018-08-30 08:32:52 +02001261{
1262 bool metric_only = config->metric_only;
1263 int interval = config->interval;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001264 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +02001265 char buf[64], *prefix = NULL;
1266
Alexander Antonovf07952b2021-04-19 12:41:44 +03001267 if (config->iostat_run)
1268 evlist->selected = evlist__first(evlist);
1269
Jiri Olsa088519f2018-08-30 08:32:52 +02001270 if (interval)
1271 print_interval(config, evlist, prefix = buf, ts);
1272 else
1273 print_header(config, _target, argc, argv);
1274
1275 if (metric_only) {
1276 static int num_print_iv;
1277
1278 if (num_print_iv == 0 && !interval)
1279 print_metric_headers(config, evlist, prefix, false);
1280 if (num_print_iv++ == 25)
1281 num_print_iv = 0;
Alexander Antonovf07952b2021-04-19 12:41:44 +03001282 if (config->aggr_mode == AGGR_GLOBAL && prefix && !config->iostat_run)
Jiri Olsa088519f2018-08-30 08:32:52 +02001283 fprintf(config->output, "%s", prefix);
1284 }
1285
1286 switch (config->aggr_mode) {
1287 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -07001288 case AGGR_DIE:
Jiri Olsa088519f2018-08-30 08:32:52 +02001289 case AGGR_SOCKET:
Jiri Olsa86895b42019-08-28 10:17:43 +02001290 case AGGR_NODE:
Jiri Olsa088519f2018-08-30 08:32:52 +02001291 print_aggr(config, evlist, prefix);
1292 break;
1293 case AGGR_THREAD:
1294 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +02001295 print_aggr_thread(config, _target, counter, prefix);
1296 }
1297 break;
1298 case AGGR_GLOBAL:
Alexander Antonovf07952b2021-04-19 12:41:44 +03001299 if (config->iostat_run)
1300 iostat_print_counters(evlist, config, ts, prefix = buf,
1301 print_counter_aggr);
1302 else {
1303 evlist__for_each_entry(evlist, counter) {
1304 print_counter_aggr(config, counter, prefix);
1305 }
1306 if (metric_only)
1307 fputc('\n', config->output);
Jiri Olsa088519f2018-08-30 08:32:52 +02001308 }
Jiri Olsa088519f2018-08-30 08:32:52 +02001309 break;
1310 case AGGR_NONE:
1311 if (metric_only)
1312 print_no_aggr_metric(config, evlist, prefix);
1313 else {
1314 evlist__for_each_entry(evlist, counter) {
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001315 if (counter->percore)
1316 print_percore(config, counter, prefix);
1317 else
1318 print_counter(config, counter, prefix);
Jiri Olsa088519f2018-08-30 08:32:52 +02001319 }
1320 }
1321 break;
1322 case AGGR_UNSET:
1323 default:
1324 break;
1325 }
1326
1327 if (!interval && !config->csv_output)
1328 print_footer(config);
1329
1330 fflush(config->output);
1331}