blob: a963b5b8eb72409152596d6b33f0d961aeb1aa40 [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"
Jiri Olsa088519f2018-08-30 08:32:52 +020020
21#define CNTR_NOT_SUPPORTED "<not supported>"
22#define CNTR_NOT_COUNTED "<not counted>"
23
Jiri Olsa088519f2018-08-30 08:32:52 +020024static void print_running(struct perf_stat_config *config,
25 u64 run, u64 ena)
26{
27 if (config->csv_output) {
28 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
29 config->csv_sep,
30 run,
31 config->csv_sep,
32 ena ? 100.0 * run / ena : 100.0);
33 } else if (run != ena) {
34 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
35 }
36}
37
38static void print_noise_pct(struct perf_stat_config *config,
39 double total, double avg)
40{
41 double pct = rel_stddev_stats(total, avg);
42
43 if (config->csv_output)
44 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
45 else if (pct)
46 fprintf(config->output, " ( +-%6.2f%% )", pct);
47}
48
49static void print_noise(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +020050 struct evsel *evsel, double avg)
Jiri Olsa088519f2018-08-30 08:32:52 +020051{
52 struct perf_stat_evsel *ps;
53
54 if (config->run_count == 1)
55 return;
56
57 ps = evsel->stats;
58 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
59}
60
Jiri Olsa32dcd022019-07-21 13:23:51 +020061static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
Stephane Eranianbc4da382018-11-07 02:50:45 -080062{
63 if (nr_cgroups) {
64 const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name : "";
65 fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
66 }
67}
68
69
Jiri Olsa088519f2018-08-30 08:32:52 +020070static void aggr_printout(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +020071 struct evsel *evsel, int id, int nr)
Jiri Olsa088519f2018-08-30 08:32:52 +020072{
73 switch (config->aggr_mode) {
74 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -070075 fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
Jiri Olsa088519f2018-08-30 08:32:52 +020076 cpu_map__id_to_socket(id),
Kan Liangdb5742b2019-06-04 15:50:42 -070077 cpu_map__id_to_die(id),
Jiri Olsa088519f2018-08-30 08:32:52 +020078 config->csv_output ? 0 : -8,
79 cpu_map__id_to_cpu(id),
80 config->csv_sep,
81 config->csv_output ? 0 : 4,
82 nr,
83 config->csv_sep);
84 break;
Kan Liangdb5742b2019-06-04 15:50:42 -070085 case AGGR_DIE:
86 fprintf(config->output, "S%d-D%*d%s%*d%s",
87 cpu_map__id_to_socket(id << 16),
88 config->csv_output ? 0 : -8,
89 cpu_map__id_to_die(id << 16),
90 config->csv_sep,
91 config->csv_output ? 0 : 4,
92 nr,
93 config->csv_sep);
94 break;
Jiri Olsa088519f2018-08-30 08:32:52 +020095 case AGGR_SOCKET:
96 fprintf(config->output, "S%*d%s%*d%s",
97 config->csv_output ? 0 : -5,
98 id,
99 config->csv_sep,
100 config->csv_output ? 0 : 4,
101 nr,
102 config->csv_sep);
103 break;
Jiri Olsa86895b42019-08-28 10:17:43 +0200104 case AGGR_NODE:
105 fprintf(config->output, "N%*d%s%*d%s",
106 config->csv_output ? 0 : -5,
107 id,
108 config->csv_sep,
109 config->csv_output ? 0 : 4,
110 nr,
111 config->csv_sep);
112 break;
Jiri Olsa088519f2018-08-30 08:32:52 +0200113 case AGGR_NONE:
Jin Yao1af62ce2020-02-14 16:04:52 +0800114 if (evsel->percore && !config->percore_show_thread) {
Kan Liangdb5742b2019-06-04 15:50:42 -0700115 fprintf(config->output, "S%d-D%d-C%*d%s",
Jin Yao4fc4d8d2019-04-12 21:59:49 +0800116 cpu_map__id_to_socket(id),
Kan Liangdb5742b2019-06-04 15:50:42 -0700117 cpu_map__id_to_die(id),
Jin Yaod13e9e42020-02-18 15:16:14 +0800118 config->csv_output ? 0 : -3,
Jin Yao4fc4d8d2019-04-12 21:59:49 +0800119 cpu_map__id_to_cpu(id), config->csv_sep);
Thomas Richter313146a2020-08-25 08:33:04 +0200120 } else if (id > -1) {
Jin Yaod13e9e42020-02-18 15:16:14 +0800121 fprintf(config->output, "CPU%*d%s",
122 config->csv_output ? 0 : -7,
Jiri Olsab49aca32019-07-21 13:24:05 +0200123 evsel__cpus(evsel)->map[id],
Jin Yao4fc4d8d2019-04-12 21:59:49 +0800124 config->csv_sep);
125 }
Jiri Olsa088519f2018-08-30 08:32:52 +0200126 break;
127 case AGGR_THREAD:
128 fprintf(config->output, "%*s-%*d%s",
129 config->csv_output ? 0 : 16,
Jiri Olsaaf663bd2019-07-21 13:24:39 +0200130 perf_thread_map__comm(evsel->core.threads, id),
Jiri Olsa088519f2018-08-30 08:32:52 +0200131 config->csv_output ? 0 : -8,
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200132 perf_thread_map__pid(evsel->core.threads, id),
Jiri Olsa088519f2018-08-30 08:32:52 +0200133 config->csv_sep);
134 break;
135 case AGGR_GLOBAL:
136 case AGGR_UNSET:
137 default:
138 break;
139 }
140}
141
142struct outstate {
143 FILE *fh;
144 bool newline;
145 const char *prefix;
146 int nfields;
147 int id, nr;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200148 struct evsel *evsel;
Jiri Olsa088519f2018-08-30 08:32:52 +0200149};
150
151#define METRIC_LEN 35
152
153static void new_line_std(struct perf_stat_config *config __maybe_unused,
154 void *ctx)
155{
156 struct outstate *os = ctx;
157
158 os->newline = true;
159}
160
161static void do_new_line_std(struct perf_stat_config *config,
162 struct outstate *os)
163{
164 fputc('\n', os->fh);
165 fputs(os->prefix, os->fh);
166 aggr_printout(config, os->evsel, os->id, os->nr);
167 if (config->aggr_mode == AGGR_NONE)
168 fprintf(os->fh, " ");
169 fprintf(os->fh, " ");
170}
171
172static void print_metric_std(struct perf_stat_config *config,
173 void *ctx, const char *color, const char *fmt,
174 const char *unit, double val)
175{
176 struct outstate *os = ctx;
177 FILE *out = os->fh;
178 int n;
179 bool newline = os->newline;
180
181 os->newline = false;
182
183 if (unit == NULL || fmt == NULL) {
184 fprintf(out, "%-*s", METRIC_LEN, "");
185 return;
186 }
187
188 if (newline)
189 do_new_line_std(config, os);
190
191 n = fprintf(out, " # ");
192 if (color)
193 n += color_fprintf(out, color, fmt, val);
194 else
195 n += fprintf(out, fmt, val);
196 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
197}
198
199static void new_line_csv(struct perf_stat_config *config, void *ctx)
200{
201 struct outstate *os = ctx;
202 int i;
203
204 fputc('\n', os->fh);
205 if (os->prefix)
206 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
207 aggr_printout(config, os->evsel, os->id, os->nr);
208 for (i = 0; i < os->nfields; i++)
209 fputs(config->csv_sep, os->fh);
210}
211
212static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
213 void *ctx,
214 const char *color __maybe_unused,
215 const char *fmt, const char *unit, double val)
216{
217 struct outstate *os = ctx;
218 FILE *out = os->fh;
219 char buf[64], *vals, *ends;
220
221 if (unit == NULL || fmt == NULL) {
222 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
223 return;
224 }
225 snprintf(buf, sizeof(buf), fmt, val);
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -0300226 ends = vals = skip_spaces(buf);
Jiri Olsa088519f2018-08-30 08:32:52 +0200227 while (isdigit(*ends) || *ends == '.')
228 ends++;
229 *ends = 0;
Arnaldo Carvalho de Melo810826a2019-06-25 21:28:49 -0300230 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
Jiri Olsa088519f2018-08-30 08:32:52 +0200231}
232
233/* Filter out some columns that don't work well in metrics only mode */
234
235static bool valid_only_metric(const char *unit)
236{
237 if (!unit)
238 return false;
239 if (strstr(unit, "/sec") ||
Jiri Olsa088519f2018-08-30 08:32:52 +0200240 strstr(unit, "CPUs utilized"))
241 return false;
242 return true;
243}
244
Jiri Olsa32dcd022019-07-21 13:23:51 +0200245static const char *fixunit(char *buf, struct evsel *evsel,
Jiri Olsa088519f2018-08-30 08:32:52 +0200246 const char *unit)
247{
248 if (!strncmp(unit, "of all", 6)) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300249 snprintf(buf, 1024, "%s %s", evsel__name(evsel),
Jiri Olsa088519f2018-08-30 08:32:52 +0200250 unit);
251 return buf;
252 }
253 return unit;
254}
255
256static void print_metric_only(struct perf_stat_config *config,
257 void *ctx, const char *color, const char *fmt,
258 const char *unit, double val)
259{
260 struct outstate *os = ctx;
261 FILE *out = os->fh;
262 char buf[1024], str[1024];
263 unsigned mlen = config->metric_only_len;
264
265 if (!valid_only_metric(unit))
266 return;
267 unit = fixunit(buf, os->evsel, unit);
268 if (mlen < strlen(unit))
269 mlen = strlen(unit) + 1;
270
271 if (color)
272 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
273
274 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
275 fprintf(out, "%*s ", mlen, str);
276}
277
278static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
279 void *ctx, const char *color __maybe_unused,
280 const char *fmt,
281 const char *unit, double val)
282{
283 struct outstate *os = ctx;
284 FILE *out = os->fh;
285 char buf[64], *vals, *ends;
286 char tbuf[1024];
287
288 if (!valid_only_metric(unit))
289 return;
290 unit = fixunit(tbuf, os->evsel, unit);
291 snprintf(buf, sizeof buf, fmt, val);
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -0300292 ends = vals = skip_spaces(buf);
Jiri Olsa088519f2018-08-30 08:32:52 +0200293 while (isdigit(*ends) || *ends == '.')
294 ends++;
295 *ends = 0;
296 fprintf(out, "%s%s", vals, config->csv_sep);
297}
298
299static void new_line_metric(struct perf_stat_config *config __maybe_unused,
300 void *ctx __maybe_unused)
301{
302}
303
304static void print_metric_header(struct perf_stat_config *config,
305 void *ctx, const char *color __maybe_unused,
306 const char *fmt __maybe_unused,
307 const char *unit, double val __maybe_unused)
308{
309 struct outstate *os = ctx;
310 char tbuf[1024];
311
312 if (!valid_only_metric(unit))
313 return;
314 unit = fixunit(tbuf, os->evsel, unit);
315 if (config->csv_output)
316 fprintf(os->fh, "%s%s", unit, config->csv_sep);
317 else
318 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
319}
320
321static int first_shadow_cpu(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200322 struct evsel *evsel, int id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200323{
Jiri Olsa63503db2019-07-21 13:23:52 +0200324 struct evlist *evlist = evsel->evlist;
Jiri Olsa088519f2018-08-30 08:32:52 +0200325 int i;
326
Jiri Olsa088519f2018-08-30 08:32:52 +0200327 if (config->aggr_mode == AGGR_NONE)
328 return id;
329
Namhyung Kimc0ee1d5a2020-11-27 13:14:03 +0900330 if (!config->aggr_get_id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200331 return 0;
332
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -0300333 for (i = 0; i < evsel__nr_cpus(evsel); i++) {
Jiri Olsab49aca32019-07-21 13:24:05 +0200334 int cpu2 = evsel__cpus(evsel)->map[i];
Jiri Olsa088519f2018-08-30 08:32:52 +0200335
Jiri Olsaf72f9012019-07-21 13:24:41 +0200336 if (config->aggr_get_id(config, evlist->core.cpus, cpu2) == id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200337 return cpu2;
338 }
339 return 0;
340}
341
342static void abs_printout(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200343 int id, int nr, struct evsel *evsel, double avg)
Jiri Olsa088519f2018-08-30 08:32:52 +0200344{
345 FILE *output = config->output;
346 double sc = evsel->scale;
347 const char *fmt;
348
349 if (config->csv_output) {
350 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
351 } else {
352 if (config->big_num)
353 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
354 else
355 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
356 }
357
358 aggr_printout(config, evsel, id, nr);
359
360 fprintf(output, fmt, avg, config->csv_sep);
361
362 if (evsel->unit)
363 fprintf(output, "%-*s%s",
364 config->csv_output ? 0 : config->unit_width,
365 evsel->unit, config->csv_sep);
366
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300367 fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
Jiri Olsa088519f2018-08-30 08:32:52 +0200368
Stephane Eranianbc4da382018-11-07 02:50:45 -0800369 print_cgroup(config, evsel);
Jiri Olsa088519f2018-08-30 08:32:52 +0200370}
371
Jiri Olsa32dcd022019-07-21 13:23:51 +0200372static bool is_mixed_hw_group(struct evsel *counter)
Jiri Olsa088519f2018-08-30 08:32:52 +0200373{
Jiri Olsa63503db2019-07-21 13:23:52 +0200374 struct evlist *evlist = counter->evlist;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200375 u32 pmu_type = counter->core.attr.type;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200376 struct evsel *pos;
Jiri Olsa088519f2018-08-30 08:32:52 +0200377
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200378 if (counter->core.nr_members < 2)
Jiri Olsa088519f2018-08-30 08:32:52 +0200379 return false;
380
381 evlist__for_each_entry(evlist, pos) {
382 /* software events can be part of any hardware group */
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200383 if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
Jiri Olsa088519f2018-08-30 08:32:52 +0200384 continue;
385 if (pmu_type == PERF_TYPE_SOFTWARE) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200386 pmu_type = pos->core.attr.type;
Jiri Olsa088519f2018-08-30 08:32:52 +0200387 continue;
388 }
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200389 if (pmu_type != pos->core.attr.type)
Jiri Olsa088519f2018-08-30 08:32:52 +0200390 return true;
391 }
392
393 return false;
394}
395
396static void printout(struct perf_stat_config *config, int id, int nr,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200397 struct evsel *counter, double uval,
Jiri Olsa088519f2018-08-30 08:32:52 +0200398 char *prefix, u64 run, u64 ena, double noise,
399 struct runtime_stat *st)
400{
401 struct perf_stat_output_ctx out;
402 struct outstate os = {
403 .fh = config->output,
404 .prefix = prefix ? prefix : "",
405 .id = id,
406 .nr = nr,
407 .evsel = counter,
408 };
409 print_metric_t pm = print_metric_std;
410 new_line_t nl;
411
412 if (config->metric_only) {
413 nl = new_line_metric;
414 if (config->csv_output)
415 pm = print_metric_only_csv;
416 else
417 pm = print_metric_only;
418 } else
419 nl = new_line_std;
420
421 if (config->csv_output && !config->metric_only) {
422 static int aggr_fields[] = {
423 [AGGR_GLOBAL] = 0,
424 [AGGR_THREAD] = 1,
425 [AGGR_NONE] = 1,
426 [AGGR_SOCKET] = 2,
Kan Liangdb5742b2019-06-04 15:50:42 -0700427 [AGGR_DIE] = 2,
Jiri Olsa088519f2018-08-30 08:32:52 +0200428 [AGGR_CORE] = 2,
429 };
430
431 pm = print_metric_csv;
432 nl = new_line_csv;
433 os.nfields = 3;
434 os.nfields += aggr_fields[config->aggr_mode];
435 if (counter->cgrp)
436 os.nfields++;
437 }
438 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
439 if (config->metric_only) {
440 pm(config, &os, NULL, "", "", 0);
441 return;
442 }
443 aggr_printout(config, counter, id, nr);
444
445 fprintf(config->output, "%*s%s",
446 config->csv_output ? 0 : 18,
447 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
448 config->csv_sep);
449
450 if (counter->supported) {
451 config->print_free_counters_hint = 1;
452 if (is_mixed_hw_group(counter))
453 config->print_mixed_hw_group_error = 1;
454 }
455
456 fprintf(config->output, "%-*s%s",
457 config->csv_output ? 0 : config->unit_width,
458 counter->unit, config->csv_sep);
459
460 fprintf(config->output, "%*s",
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300461 config->csv_output ? 0 : -25, evsel__name(counter));
Jiri Olsa088519f2018-08-30 08:32:52 +0200462
Stephane Eranianbc4da382018-11-07 02:50:45 -0800463 print_cgroup(config, counter);
Jiri Olsa088519f2018-08-30 08:32:52 +0200464
465 if (!config->csv_output)
466 pm(config, &os, NULL, NULL, "", 0);
467 print_noise(config, counter, noise);
468 print_running(config, run, ena);
469 if (config->csv_output)
470 pm(config, &os, NULL, NULL, "", 0);
471 return;
472 }
473
474 if (!config->metric_only)
475 abs_printout(config, id, nr, counter, uval);
476
477 out.print_metric = pm;
478 out.new_line = nl;
479 out.ctx = &os;
480 out.force_header = false;
481
482 if (config->csv_output && !config->metric_only) {
483 print_noise(config, counter, noise);
484 print_running(config, run, ena);
485 }
486
487 perf_stat__print_shadow_stats(config, counter, uval,
488 first_shadow_cpu(config, counter, id),
489 &out, &config->metric_events, st);
490 if (!config->csv_output && !config->metric_only) {
491 print_noise(config, counter, noise);
492 print_running(config, run, ena);
493 }
494}
495
496static void aggr_update_shadow(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200497 struct evlist *evlist)
Jiri Olsa088519f2018-08-30 08:32:52 +0200498{
499 int cpu, s2, id, s;
500 u64 val;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200501 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +0200502
503 for (s = 0; s < config->aggr_map->nr; s++) {
504 id = config->aggr_map->map[s];
505 evlist__for_each_entry(evlist, counter) {
506 val = 0;
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -0300507 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
Jiri Olsaf72f9012019-07-21 13:24:41 +0200508 s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
Jiri Olsa088519f2018-08-30 08:32:52 +0200509 if (s2 != id)
510 continue;
511 val += perf_counts(counter->counts, cpu, 0)->val;
512 }
513 perf_stat__update_shadow_stats(counter, val,
514 first_shadow_cpu(config, counter, id),
515 &rt_stat);
516 }
517 }
518}
519
Jiri Olsa32dcd022019-07-21 13:23:51 +0200520static void uniquify_event_name(struct evsel *counter)
Jiri Olsa088519f2018-08-30 08:32:52 +0200521{
522 char *new_name;
523 char *config;
524
525 if (counter->uniquified_name ||
526 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
527 strlen(counter->pmu_name)))
528 return;
529
530 config = strchr(counter->name, '/');
531 if (config) {
532 if (asprintf(&new_name,
533 "%s%s", counter->pmu_name, config) > 0) {
534 free(counter->name);
535 counter->name = new_name;
536 }
537 } else {
538 if (asprintf(&new_name,
539 "%s [%s]", counter->name, counter->pmu_name) > 0) {
540 free(counter->name);
541 counter->name = new_name;
542 }
543 }
544
545 counter->uniquified_name = true;
546}
547
Jiri Olsa32dcd022019-07-21 13:23:51 +0200548static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
549 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200550 bool first),
551 void *data)
552{
Jiri Olsa63503db2019-07-21 13:23:52 +0200553 struct evlist *evlist = counter->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200554 struct evsel *alias;
Jiri Olsa088519f2018-08-30 08:32:52 +0200555
Jiri Olsace9036a2019-07-21 13:24:23 +0200556 alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
557 list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300558 if (strcmp(evsel__name(alias), evsel__name(counter)) ||
Jiri Olsa088519f2018-08-30 08:32:52 +0200559 alias->scale != counter->scale ||
560 alias->cgrp != counter->cgrp ||
561 strcmp(alias->unit, counter->unit) ||
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300562 evsel__is_clock(alias) != evsel__is_clock(counter) ||
Andi Kleen6c5f4e52019-06-24 12:37:09 -0700563 !strcmp(alias->pmu_name, counter->pmu_name))
Jiri Olsa088519f2018-08-30 08:32:52 +0200564 break;
565 alias->merged_stat = true;
566 cb(config, alias, data, false);
567 }
568}
569
Jiri Olsa32dcd022019-07-21 13:23:51 +0200570static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
571 void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200572 bool first),
573 void *data)
574{
575 if (counter->merged_stat)
576 return false;
577 cb(config, counter, data, true);
578 if (config->no_merge)
579 uniquify_event_name(counter);
580 else if (counter->auto_merge_stats)
581 collect_all_aliases(config, counter, cb, data);
582 return true;
583}
584
585struct aggr_data {
586 u64 ena, run, val;
587 int id;
588 int nr;
589 int cpu;
590};
591
592static void aggr_cb(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200593 struct evsel *counter, void *data, bool first)
Jiri Olsa088519f2018-08-30 08:32:52 +0200594{
595 struct aggr_data *ad = data;
596 int cpu, s2;
597
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -0300598 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200599 struct perf_counts_values *counts;
600
Jiri Olsab49aca32019-07-21 13:24:05 +0200601 s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
Jiri Olsa088519f2018-08-30 08:32:52 +0200602 if (s2 != ad->id)
603 continue;
604 if (first)
605 ad->nr++;
606 counts = perf_counts(counter->counts, cpu, 0);
607 /*
608 * When any result is bad, make them all to give
609 * consistent output in interval mode.
610 */
611 if (counts->ena == 0 || counts->run == 0 ||
612 counter->counts->scaled == -1) {
613 ad->ena = 0;
614 ad->run = 0;
615 break;
616 }
617 ad->val += counts->val;
618 ad->ena += counts->ena;
619 ad->run += counts->run;
620 }
621}
622
Jin Yao40480a82019-04-12 21:59:48 +0800623static void print_counter_aggrdata(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200624 struct evsel *counter, int s,
Jin Yao40480a82019-04-12 21:59:48 +0800625 char *prefix, bool metric_only,
Jin Yao1af62ce2020-02-14 16:04:52 +0800626 bool *first, int cpu)
Jin Yao40480a82019-04-12 21:59:48 +0800627{
628 struct aggr_data ad;
629 FILE *output = config->output;
630 u64 ena, run, val;
631 int id, nr;
632 double uval;
633
634 ad.id = id = config->aggr_map->map[s];
635 ad.val = ad.ena = ad.run = 0;
636 ad.nr = 0;
637 if (!collect_data(config, counter, aggr_cb, &ad))
638 return;
639
640 nr = ad.nr;
641 ena = ad.ena;
642 run = ad.run;
643 val = ad.val;
644 if (*first && metric_only) {
645 *first = false;
646 aggr_printout(config, counter, id, nr);
647 }
648 if (prefix && !metric_only)
649 fprintf(output, "%s", prefix);
650
651 uval = val * counter->scale;
Jin Yao1af62ce2020-02-14 16:04:52 +0800652 printout(config, cpu != -1 ? cpu : id, nr, counter, uval, prefix,
Jin Yao40480a82019-04-12 21:59:48 +0800653 run, ena, 1.0, &rt_stat);
654 if (!metric_only)
655 fputc('\n', output);
656}
657
Jiri Olsa088519f2018-08-30 08:32:52 +0200658static void print_aggr(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200659 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200660 char *prefix)
661{
662 bool metric_only = config->metric_only;
663 FILE *output = config->output;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200664 struct evsel *counter;
Jin Yao40480a82019-04-12 21:59:48 +0800665 int s;
Jiri Olsa088519f2018-08-30 08:32:52 +0200666 bool first;
667
Hongbo Yaoc0c652f2020-06-05 17:17:40 +0800668 if (!config->aggr_map || !config->aggr_get_id)
Jiri Olsa088519f2018-08-30 08:32:52 +0200669 return;
670
671 aggr_update_shadow(config, evlist);
672
673 /*
674 * With metric_only everything is on a single line.
675 * Without each counter has its own line.
676 */
677 for (s = 0; s < config->aggr_map->nr; s++) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200678 if (prefix && metric_only)
679 fprintf(output, "%s", prefix);
680
Jiri Olsa088519f2018-08-30 08:32:52 +0200681 first = true;
682 evlist__for_each_entry(evlist, counter) {
Jin Yao40480a82019-04-12 21:59:48 +0800683 print_counter_aggrdata(config, counter, s,
684 prefix, metric_only,
Jin Yao1af62ce2020-02-14 16:04:52 +0800685 &first, -1);
Jiri Olsa088519f2018-08-30 08:32:52 +0200686 }
687 if (metric_only)
688 fputc('\n', output);
689 }
690}
691
692static int cmp_val(const void *a, const void *b)
693{
694 return ((struct perf_aggr_thread_value *)b)->val -
695 ((struct perf_aggr_thread_value *)a)->val;
696}
697
698static struct perf_aggr_thread_value *sort_aggr_thread(
Jiri Olsa32dcd022019-07-21 13:23:51 +0200699 struct evsel *counter,
Jiri Olsa088519f2018-08-30 08:32:52 +0200700 int nthreads, int ncpus,
701 int *ret,
702 struct target *_target)
703{
704 int cpu, thread, i = 0;
705 double uval;
706 struct perf_aggr_thread_value *buf;
707
708 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
709 if (!buf)
710 return NULL;
711
712 for (thread = 0; thread < nthreads; thread++) {
713 u64 ena = 0, run = 0, val = 0;
714
715 for (cpu = 0; cpu < ncpus; cpu++) {
716 val += perf_counts(counter->counts, cpu, thread)->val;
717 ena += perf_counts(counter->counts, cpu, thread)->ena;
718 run += perf_counts(counter->counts, cpu, thread)->run;
719 }
720
721 uval = val * counter->scale;
722
723 /*
724 * Skip value 0 when enabling --per-thread globally,
725 * otherwise too many 0 output.
726 */
727 if (uval == 0.0 && target__has_per_thread(_target))
728 continue;
729
730 buf[i].counter = counter;
731 buf[i].id = thread;
732 buf[i].uval = uval;
733 buf[i].val = val;
734 buf[i].run = run;
735 buf[i].ena = ena;
736 i++;
737 }
738
739 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
740
741 if (ret)
742 *ret = i;
743
744 return buf;
745}
746
747static void print_aggr_thread(struct perf_stat_config *config,
748 struct target *_target,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200749 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200750{
751 FILE *output = config->output;
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200752 int nthreads = perf_thread_map__nr(counter->core.threads);
Jiri Olsa6549cd82019-08-22 13:11:38 +0200753 int ncpus = perf_cpu_map__nr(counter->core.cpus);
Jiri Olsa088519f2018-08-30 08:32:52 +0200754 int thread, sorted_threads, id;
755 struct perf_aggr_thread_value *buf;
756
757 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
758 if (!buf) {
759 perror("cannot sort aggr thread");
760 return;
761 }
762
763 for (thread = 0; thread < sorted_threads; thread++) {
764 if (prefix)
765 fprintf(output, "%s", prefix);
766
767 id = buf[thread].id;
768 if (config->stats)
769 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
770 prefix, buf[thread].run, buf[thread].ena, 1.0,
771 &config->stats[id]);
772 else
773 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
774 prefix, buf[thread].run, buf[thread].ena, 1.0,
775 &rt_stat);
776 fputc('\n', output);
777 }
778
779 free(buf);
780}
781
782struct caggr_data {
783 double avg, avg_enabled, avg_running;
784};
785
786static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200787 struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200788 bool first __maybe_unused)
789{
790 struct caggr_data *cd = data;
791 struct perf_stat_evsel *ps = counter->stats;
792
793 cd->avg += avg_stats(&ps->res_stats[0]);
794 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
795 cd->avg_running += avg_stats(&ps->res_stats[2]);
796}
797
798/*
799 * Print out the results of a single counter:
800 * aggregated counts in system-wide mode
801 */
802static void print_counter_aggr(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200803 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200804{
805 bool metric_only = config->metric_only;
806 FILE *output = config->output;
807 double uval;
808 struct caggr_data cd = { .avg = 0.0 };
809
810 if (!collect_data(config, counter, counter_aggr_cb, &cd))
811 return;
812
813 if (prefix && !metric_only)
814 fprintf(output, "%s", prefix);
815
816 uval = cd.avg * counter->scale;
817 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
818 cd.avg, &rt_stat);
819 if (!metric_only)
820 fprintf(output, "\n");
821}
822
823static void counter_cb(struct perf_stat_config *config __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200824 struct evsel *counter, void *data,
Jiri Olsa088519f2018-08-30 08:32:52 +0200825 bool first __maybe_unused)
826{
827 struct aggr_data *ad = data;
828
829 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
830 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
831 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
832}
833
834/*
835 * Print out the results of a single counter:
836 * does not use aggregated count in system-wide
837 */
838static void print_counter(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200839 struct evsel *counter, char *prefix)
Jiri Olsa088519f2018-08-30 08:32:52 +0200840{
841 FILE *output = config->output;
842 u64 ena, run, val;
843 double uval;
844 int cpu;
845
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -0300846 for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200847 struct aggr_data ad = { .cpu = cpu };
848
849 if (!collect_data(config, counter, counter_cb, &ad))
850 return;
851 val = ad.val;
852 ena = ad.ena;
853 run = ad.run;
854
855 if (prefix)
856 fprintf(output, "%s", prefix);
857
858 uval = val * counter->scale;
859 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
860 &rt_stat);
861
862 fputc('\n', output);
863 }
864}
865
866static void print_no_aggr_metric(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200867 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200868 char *prefix)
869{
870 int cpu;
871 int nrcpus = 0;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200872 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +0200873 u64 ena, run, val;
874 double uval;
875
Jiri Olsaf72f9012019-07-21 13:24:41 +0200876 nrcpus = evlist->core.cpus->nr;
Jiri Olsa088519f2018-08-30 08:32:52 +0200877 for (cpu = 0; cpu < nrcpus; cpu++) {
878 bool first = true;
879
880 if (prefix)
881 fputs(prefix, config->output);
882 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200883 if (first) {
884 aggr_printout(config, counter, cpu, 0);
885 first = false;
886 }
887 val = perf_counts(counter->counts, cpu, 0)->val;
888 ena = perf_counts(counter->counts, cpu, 0)->ena;
889 run = perf_counts(counter->counts, cpu, 0)->run;
890
891 uval = val * counter->scale;
892 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
893 &rt_stat);
894 }
895 fputc('\n', config->output);
896 }
897}
898
899static int aggr_header_lens[] = {
Kan Liangdb5742b2019-06-04 15:50:42 -0700900 [AGGR_CORE] = 24,
901 [AGGR_DIE] = 18,
Jiri Olsa088519f2018-08-30 08:32:52 +0200902 [AGGR_SOCKET] = 12,
903 [AGGR_NONE] = 6,
904 [AGGR_THREAD] = 24,
905 [AGGR_GLOBAL] = 0,
906};
907
908static const char *aggr_header_csv[] = {
909 [AGGR_CORE] = "core,cpus,",
Kan Liangdb5742b2019-06-04 15:50:42 -0700910 [AGGR_DIE] = "die,cpus",
Jiri Olsa088519f2018-08-30 08:32:52 +0200911 [AGGR_SOCKET] = "socket,cpus",
912 [AGGR_NONE] = "cpu,",
913 [AGGR_THREAD] = "comm-pid,",
914 [AGGR_GLOBAL] = ""
915};
916
917static void print_metric_headers(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200918 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200919 const char *prefix, bool no_indent)
920{
921 struct perf_stat_output_ctx out;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200922 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +0200923 struct outstate os = {
924 .fh = config->output
925 };
926
927 if (prefix)
928 fprintf(config->output, "%s", prefix);
929
930 if (!config->csv_output && !no_indent)
931 fprintf(config->output, "%*s",
932 aggr_header_lens[config->aggr_mode], "");
933 if (config->csv_output) {
934 if (config->interval)
935 fputs("time,", config->output);
936 fputs(aggr_header_csv[config->aggr_mode], config->output);
937 }
938
939 /* Print metrics headers only */
940 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +0200941 os.evsel = counter;
942 out.ctx = &os;
943 out.print_metric = print_metric_header;
944 out.new_line = new_line_metric;
945 out.force_header = true;
Jiri Olsa088519f2018-08-30 08:32:52 +0200946 perf_stat__print_shadow_stats(config, counter, 0,
947 0,
948 &out,
949 &config->metric_events,
950 &rt_stat);
951 }
952 fputc('\n', config->output);
953}
954
955static void print_interval(struct perf_stat_config *config,
Jiri Olsa63503db2019-07-21 13:23:52 +0200956 struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +0200957 char *prefix, struct timespec *ts)
958{
959 bool metric_only = config->metric_only;
960 unsigned int unit_width = config->unit_width;
961 FILE *output = config->output;
962 static int num_print_interval;
963
964 if (config->interval_clear)
965 puts(CONSOLE_CLEAR);
966
967 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
968
969 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
970 switch (config->aggr_mode) {
Jiri Olsa86895b42019-08-28 10:17:43 +0200971 case AGGR_NODE:
972 fprintf(output, "# time node cpus");
973 if (!metric_only)
974 fprintf(output, " counts %*s events\n", unit_width, "unit");
975 break;
Jiri Olsa088519f2018-08-30 08:32:52 +0200976 case AGGR_SOCKET:
977 fprintf(output, "# time socket cpus");
978 if (!metric_only)
979 fprintf(output, " counts %*s events\n", unit_width, "unit");
980 break;
Kan Liangdb5742b2019-06-04 15:50:42 -0700981 case AGGR_DIE:
982 fprintf(output, "# time die cpus");
983 if (!metric_only)
984 fprintf(output, " counts %*s events\n", unit_width, "unit");
985 break;
Jiri Olsa088519f2018-08-30 08:32:52 +0200986 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -0700987 fprintf(output, "# time core cpus");
Jiri Olsa088519f2018-08-30 08:32:52 +0200988 if (!metric_only)
989 fprintf(output, " counts %*s events\n", unit_width, "unit");
990 break;
991 case AGGR_NONE:
992 fprintf(output, "# time CPU ");
993 if (!metric_only)
994 fprintf(output, " counts %*s events\n", unit_width, "unit");
995 break;
996 case AGGR_THREAD:
997 fprintf(output, "# time comm-pid");
998 if (!metric_only)
999 fprintf(output, " counts %*s events\n", unit_width, "unit");
1000 break;
1001 case AGGR_GLOBAL:
1002 default:
1003 fprintf(output, "# time");
1004 if (!metric_only)
1005 fprintf(output, " counts %*s events\n", unit_width, "unit");
1006 case AGGR_UNSET:
1007 break;
1008 }
1009 }
1010
1011 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1012 print_metric_headers(config, evlist, " ", true);
1013 if (++num_print_interval == 25)
1014 num_print_interval = 0;
1015}
1016
1017static void print_header(struct perf_stat_config *config,
1018 struct target *_target,
1019 int argc, const char **argv)
1020{
1021 FILE *output = config->output;
1022 int i;
1023
1024 fflush(stdout);
1025
1026 if (!config->csv_output) {
1027 fprintf(output, "\n");
1028 fprintf(output, " Performance counter stats for ");
1029 if (_target->system_wide)
1030 fprintf(output, "\'system wide");
1031 else if (_target->cpu_list)
1032 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1033 else if (!target__has_task(_target)) {
1034 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1035 for (i = 1; argv && (i < argc); i++)
1036 fprintf(output, " %s", argv[i]);
1037 } else if (_target->pid)
1038 fprintf(output, "process id \'%s", _target->pid);
1039 else
1040 fprintf(output, "thread id \'%s", _target->tid);
1041
1042 fprintf(output, "\'");
1043 if (config->run_count > 1)
1044 fprintf(output, " (%d runs)", config->run_count);
1045 fprintf(output, ":\n\n");
1046 }
1047}
1048
1049static int get_precision(double num)
1050{
1051 if (num > 1)
1052 return 0;
1053
1054 return lround(ceil(-log10(num)));
1055}
1056
1057static void print_table(struct perf_stat_config *config,
1058 FILE *output, int precision, double avg)
1059{
1060 char tmp[64];
1061 int idx, indent = 0;
1062
1063 scnprintf(tmp, 64, " %17.*f", precision, avg);
1064 while (tmp[indent] == ' ')
1065 indent++;
1066
1067 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1068
1069 for (idx = 0; idx < config->run_count; idx++) {
1070 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1071 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1072
1073 fprintf(output, " %17.*f (%+.*f) ",
1074 precision, run, precision, run - avg);
1075
1076 for (h = 0; h < n; h++)
1077 fprintf(output, "#");
1078
1079 fprintf(output, "\n");
1080 }
1081
1082 fprintf(output, "\n%*s# Final result:\n", indent, "");
1083}
1084
1085static double timeval2double(struct timeval *t)
1086{
1087 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1088}
1089
1090static void print_footer(struct perf_stat_config *config)
1091{
1092 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1093 FILE *output = config->output;
Jiri Olsa088519f2018-08-30 08:32:52 +02001094
1095 if (!config->null_run)
1096 fprintf(output, "\n");
1097
1098 if (config->run_count == 1) {
1099 fprintf(output, " %17.9f seconds time elapsed", avg);
1100
1101 if (config->ru_display) {
1102 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1103 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1104
1105 fprintf(output, "\n\n");
1106 fprintf(output, " %17.9f seconds user\n", ru_utime);
1107 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1108 }
1109 } else {
1110 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1111 /*
1112 * Display at most 2 more significant
1113 * digits than the stddev inaccuracy.
1114 */
1115 int precision = get_precision(sd) + 2;
1116
1117 if (config->walltime_run_table)
1118 print_table(config, output, precision, avg);
1119
1120 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1121 precision, avg, precision, sd);
1122
1123 print_noise_pct(config, sd, avg);
1124 }
1125 fprintf(output, "\n\n");
1126
Kan Liang2a14c1b2020-02-24 13:59:22 -08001127 if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
Jiri Olsa088519f2018-08-30 08:32:52 +02001128 fprintf(output,
1129"Some events weren't counted. Try disabling the NMI watchdog:\n"
1130" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1131" perf stat ...\n"
1132" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1133
1134 if (config->print_mixed_hw_group_error)
1135 fprintf(output,
1136 "The events in group usually have to be from "
1137 "the same PMU. Try reorganizing the group.\n");
1138}
1139
Jin Yao1af62ce2020-02-14 16:04:52 +08001140static void print_percore_thread(struct perf_stat_config *config,
1141 struct evsel *counter, char *prefix)
1142{
1143 int s, s2, id;
1144 bool first = true;
1145
Arnaldo Carvalho de Melo5eb88f02020-04-29 15:45:09 -03001146 for (int i = 0; i < evsel__nr_cpus(counter); i++) {
Jin Yao1af62ce2020-02-14 16:04:52 +08001147 s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1148 for (s = 0; s < config->aggr_map->nr; s++) {
1149 id = config->aggr_map->map[s];
1150 if (s2 == id)
1151 break;
1152 }
1153
1154 print_counter_aggrdata(config, counter, s,
1155 prefix, false,
1156 &first, i);
1157 }
1158}
1159
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001160static void print_percore(struct perf_stat_config *config,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001161 struct evsel *counter, char *prefix)
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001162{
1163 bool metric_only = config->metric_only;
1164 FILE *output = config->output;
1165 int s;
1166 bool first = true;
1167
Hongbo Yaoc0c652f2020-06-05 17:17:40 +08001168 if (!config->aggr_map || !config->aggr_get_id)
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001169 return;
1170
Jin Yao1af62ce2020-02-14 16:04:52 +08001171 if (config->percore_show_thread)
1172 return print_percore_thread(config, counter, prefix);
1173
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001174 for (s = 0; s < config->aggr_map->nr; s++) {
1175 if (prefix && metric_only)
1176 fprintf(output, "%s", prefix);
1177
1178 print_counter_aggrdata(config, counter, s,
1179 prefix, metric_only,
Jin Yao1af62ce2020-02-14 16:04:52 +08001180 &first, -1);
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001181 }
1182
1183 if (metric_only)
1184 fputc('\n', output);
1185}
1186
Jiri Olsa088519f2018-08-30 08:32:52 +02001187void
Jiri Olsa63503db2019-07-21 13:23:52 +02001188perf_evlist__print_counters(struct evlist *evlist,
Jiri Olsa088519f2018-08-30 08:32:52 +02001189 struct perf_stat_config *config,
1190 struct target *_target,
1191 struct timespec *ts,
1192 int argc, const char **argv)
1193{
1194 bool metric_only = config->metric_only;
1195 int interval = config->interval;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001196 struct evsel *counter;
Jiri Olsa088519f2018-08-30 08:32:52 +02001197 char buf[64], *prefix = NULL;
1198
1199 if (interval)
1200 print_interval(config, evlist, prefix = buf, ts);
1201 else
1202 print_header(config, _target, argc, argv);
1203
1204 if (metric_only) {
1205 static int num_print_iv;
1206
1207 if (num_print_iv == 0 && !interval)
1208 print_metric_headers(config, evlist, prefix, false);
1209 if (num_print_iv++ == 25)
1210 num_print_iv = 0;
1211 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1212 fprintf(config->output, "%s", prefix);
1213 }
1214
1215 switch (config->aggr_mode) {
1216 case AGGR_CORE:
Kan Liangdb5742b2019-06-04 15:50:42 -07001217 case AGGR_DIE:
Jiri Olsa088519f2018-08-30 08:32:52 +02001218 case AGGR_SOCKET:
Jiri Olsa86895b42019-08-28 10:17:43 +02001219 case AGGR_NODE:
Jiri Olsa088519f2018-08-30 08:32:52 +02001220 print_aggr(config, evlist, prefix);
1221 break;
1222 case AGGR_THREAD:
1223 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +02001224 print_aggr_thread(config, _target, counter, prefix);
1225 }
1226 break;
1227 case AGGR_GLOBAL:
1228 evlist__for_each_entry(evlist, counter) {
Jiri Olsa088519f2018-08-30 08:32:52 +02001229 print_counter_aggr(config, counter, prefix);
1230 }
1231 if (metric_only)
1232 fputc('\n', config->output);
1233 break;
1234 case AGGR_NONE:
1235 if (metric_only)
1236 print_no_aggr_metric(config, evlist, prefix);
1237 else {
1238 evlist__for_each_entry(evlist, counter) {
Jin Yao4fc4d8d2019-04-12 21:59:49 +08001239 if (counter->percore)
1240 print_percore(config, counter, prefix);
1241 else
1242 print_counter(config, counter, prefix);
Jiri Olsa088519f2018-08-30 08:32:52 +02001243 }
1244 }
1245 break;
1246 case AGGR_UNSET:
1247 default:
1248 break;
1249 }
1250
1251 if (!interval && !config->csv_output)
1252 print_footer(config);
1253
1254 fflush(config->output);
1255}