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