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