]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - tools/perf/builtin-stat.c
perf tools: Add cpu_map processor socket level functions
[mirror_ubuntu-focal-kernel.git] / tools / perf / builtin-stat.c
CommitLineData
ddcacfa0 1/*
bf9e1876
IM
2 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
ddcacfa0 8
2cba3ffb 9 $ perf stat ./hackbench 10
ddcacfa0 10
2cba3ffb 11 Time: 0.118
ddcacfa0 12
2cba3ffb 13 Performance counter stats for './hackbench 10':
ddcacfa0 14
2cba3ffb
IM
15 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
ddcacfa0 28
5242519b 29 *
2cba3ffb 30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
5242519b
IM
31 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
6e750a8f 39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
5242519b
IM
40 *
41 * Released under the GPL v2. (and only v2, not any later version)
ddcacfa0
IM
42 */
43
1a482f38 44#include "perf.h"
16f762a2 45#include "builtin.h"
148be2c1 46#include "util/util.h"
5242519b
IM
47#include "util/parse-options.h"
48#include "util/parse-events.h"
8f28827a 49#include "util/event.h"
361c99a6 50#include "util/evlist.h"
69aad6f1 51#include "util/evsel.h"
8f28827a 52#include "util/debug.h"
a5d243d0 53#include "util/color.h"
0007ecea 54#include "util/stat.h"
60666c63 55#include "util/header.h"
a12b51c4 56#include "util/cpumap.h"
d6d901c2 57#include "util/thread.h"
fd78260b 58#include "util/thread_map.h"
ddcacfa0 59
1f16c575 60#include <stdlib.h>
ddcacfa0 61#include <sys/prctl.h>
5af52b51 62#include <locale.h>
16c8a109 63
d7470b6a 64#define DEFAULT_SEPARATOR " "
2cee77c4
DA
65#define CNTR_NOT_SUPPORTED "<not supported>"
66#define CNTR_NOT_COUNTED "<not counted>"
d7470b6a 67
13370a9b
SE
68static void print_stat(int argc, const char **argv);
69static void print_counter_aggr(struct perf_evsel *counter, char *prefix);
70static void print_counter(struct perf_evsel *counter, char *prefix);
71
666e6d48 72static struct perf_evlist *evsel_list;
361c99a6 73
77a6f014
NK
74static struct perf_target target = {
75 .uid = UINT_MAX,
76};
ddcacfa0 77
3d632595 78static int run_count = 1;
2e6cdf99 79static bool no_inherit = false;
c0555642 80static bool scale = true;
f5b4a9c3 81static bool no_aggr = false;
933da83a 82static pid_t child_pid = -1;
c0555642 83static bool null_run = false;
2cba3ffb 84static int detailed_run = 0;
201e0b06 85static bool big_num = true;
d7470b6a 86static int big_num_opt = -1;
d7470b6a
SE
87static const char *csv_sep = NULL;
88static bool csv_output = false;
43bece79 89static bool group = false;
4aa9015f 90static FILE *output = NULL;
1f16c575
PZ
91static const char *pre_cmd = NULL;
92static const char *post_cmd = NULL;
93static bool sync_run = false;
13370a9b
SE
94static unsigned int interval = 0;
95static struct timespec ref_time;
5af52b51 96
60666c63
LW
97static volatile int done = 0;
98
69aad6f1
ACM
99struct perf_stat {
100 struct stats res_stats[3];
69aad6f1
ACM
101};
102
13370a9b
SE
103static inline void diff_timespec(struct timespec *r, struct timespec *a,
104 struct timespec *b)
105{
106 r->tv_sec = a->tv_sec - b->tv_sec;
107 if (a->tv_nsec < b->tv_nsec) {
108 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
109 r->tv_sec--;
110 } else {
111 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
112 }
113}
114
115static inline struct cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
116{
117 return (evsel->cpus && !target.cpu_list) ? evsel->cpus : evsel_list->cpus;
118}
119
120static inline int perf_evsel__nr_cpus(struct perf_evsel *evsel)
121{
122 return perf_evsel__cpus(evsel)->nr;
123}
124
c52b12ed 125static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
69aad6f1 126{
c52b12ed 127 evsel->priv = zalloc(sizeof(struct perf_stat));
69aad6f1
ACM
128 return evsel->priv == NULL ? -ENOMEM : 0;
129}
130
131static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
132{
133 free(evsel->priv);
134 evsel->priv = NULL;
135}
136
13370a9b 137static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel)
7ae92e74 138{
13370a9b
SE
139 void *addr;
140 size_t sz;
141
142 sz = sizeof(*evsel->counts) +
143 (perf_evsel__nr_cpus(evsel) * sizeof(struct perf_counts_values));
144
145 addr = zalloc(sz);
146 if (!addr)
147 return -ENOMEM;
148
149 evsel->prev_raw_counts = addr;
150
151 return 0;
7ae92e74
YZ
152}
153
13370a9b 154static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
7ae92e74 155{
13370a9b
SE
156 free(evsel->prev_raw_counts);
157 evsel->prev_raw_counts = NULL;
7ae92e74
YZ
158}
159
666e6d48
RR
160static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
161static struct stats runtime_cycles_stats[MAX_NR_CPUS];
162static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
163static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
164static struct stats runtime_branches_stats[MAX_NR_CPUS];
165static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
166static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
167static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
168static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
169static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
170static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
171static struct stats walltime_nsecs_stats;
be1ac0d8 172
cac21425 173static int create_perf_stat_counter(struct perf_evsel *evsel)
ddcacfa0 174{
69aad6f1 175 struct perf_event_attr *attr = &evsel->attr;
727ab04e 176
ddcacfa0 177 if (scale)
a21ca2ca
IM
178 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
179 PERF_FORMAT_TOTAL_TIME_RUNNING;
ddcacfa0 180
5d2cd909
ACM
181 attr->inherit = !no_inherit;
182
594ac61a
ACM
183 if (perf_target__has_cpu(&target))
184 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
5622c07b 185
07ac002f 186 if (!perf_target__has_task(&target) &&
823254ed 187 perf_evsel__is_group_leader(evsel)) {
48290609
ACM
188 attr->disabled = 1;
189 attr->enable_on_exec = 1;
ddcacfa0 190 }
084ab9f8 191
594ac61a 192 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
ddcacfa0
IM
193}
194
c04f5e5d
IM
195/*
196 * Does the counter have nsecs as a unit?
197 */
daec78a0 198static inline int nsec_counter(struct perf_evsel *evsel)
c04f5e5d 199{
daec78a0
ACM
200 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
201 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
c04f5e5d
IM
202 return 1;
203
204 return 0;
205}
206
dcd9936a
IM
207/*
208 * Update various tracking values we maintain to print
209 * more semantic information such as miss/hit ratios,
210 * instruction rates, etc:
211 */
212static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
213{
214 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
215 update_stats(&runtime_nsecs_stats[0], count[0]);
216 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
217 update_stats(&runtime_cycles_stats[0], count[0]);
d3d1e86d
IM
218 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
219 update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
129c04cb 220 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
d3d1e86d 221 update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
dcd9936a
IM
222 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
223 update_stats(&runtime_branches_stats[0], count[0]);
224 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
225 update_stats(&runtime_cacherefs_stats[0], count[0]);
8bb6c79f
IM
226 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
227 update_stats(&runtime_l1_dcache_stats[0], count[0]);
c3305257
IM
228 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
229 update_stats(&runtime_l1_icache_stats[0], count[0]);
230 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
231 update_stats(&runtime_ll_cache_stats[0], count[0]);
232 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
233 update_stats(&runtime_dtlb_cache_stats[0], count[0]);
234 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
235 update_stats(&runtime_itlb_cache_stats[0], count[0]);
dcd9936a
IM
236}
237
c04f5e5d 238/*
2996f5dd 239 * Read out the results of a single counter:
f5b4a9c3 240 * aggregate counts across CPUs in system-wide mode
c04f5e5d 241 */
c52b12ed 242static int read_counter_aggr(struct perf_evsel *counter)
c04f5e5d 243{
69aad6f1 244 struct perf_stat *ps = counter->priv;
c52b12ed
ACM
245 u64 *count = counter->counts->aggr.values;
246 int i;
2996f5dd 247
7ae92e74 248 if (__perf_evsel__read(counter, perf_evsel__nr_cpus(counter),
7e2ed097 249 evsel_list->threads->nr, scale) < 0)
c52b12ed 250 return -1;
9e9772c4
PZ
251
252 for (i = 0; i < 3; i++)
69aad6f1 253 update_stats(&ps->res_stats[i], count[i]);
9e9772c4
PZ
254
255 if (verbose) {
4aa9015f 256 fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
7289f83c 257 perf_evsel__name(counter), count[0], count[1], count[2]);
9e9772c4
PZ
258 }
259
be1ac0d8
IM
260 /*
261 * Save the full runtime - to allow normalization during printout:
262 */
dcd9936a 263 update_shadow_stats(counter, count);
c52b12ed
ACM
264
265 return 0;
f5b4a9c3
SE
266}
267
268/*
269 * Read out the results of a single counter:
270 * do not aggregate counts across CPUs in system-wide mode
271 */
c52b12ed 272static int read_counter(struct perf_evsel *counter)
f5b4a9c3 273{
c52b12ed 274 u64 *count;
f5b4a9c3 275 int cpu;
f5b4a9c3 276
7ae92e74 277 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
c52b12ed
ACM
278 if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
279 return -1;
f5b4a9c3 280
c52b12ed 281 count = counter->counts->cpu[cpu].values;
f5b4a9c3 282
dcd9936a 283 update_shadow_stats(counter, count);
f5b4a9c3 284 }
c52b12ed
ACM
285
286 return 0;
2996f5dd
IM
287}
288
13370a9b
SE
289static void print_interval(void)
290{
291 static int num_print_interval;
292 struct perf_evsel *counter;
293 struct perf_stat *ps;
294 struct timespec ts, rs;
295 char prefix[64];
296
297 if (no_aggr) {
298 list_for_each_entry(counter, &evsel_list->entries, node) {
299 ps = counter->priv;
300 memset(ps->res_stats, 0, sizeof(ps->res_stats));
301 read_counter(counter);
302 }
303 } else {
304 list_for_each_entry(counter, &evsel_list->entries, node) {
305 ps = counter->priv;
306 memset(ps->res_stats, 0, sizeof(ps->res_stats));
307 read_counter_aggr(counter);
308 }
309 }
310 clock_gettime(CLOCK_MONOTONIC, &ts);
311 diff_timespec(&rs, &ts, &ref_time);
312 sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep);
313
314 if (num_print_interval == 0 && !csv_output) {
315 if (no_aggr)
316 fprintf(output, "# time CPU counts events\n");
317 else
318 fprintf(output, "# time counts events\n");
319 }
320
321 if (++num_print_interval == 25)
322 num_print_interval = 0;
323
324 if (no_aggr) {
325 list_for_each_entry(counter, &evsel_list->entries, node)
326 print_counter(counter, prefix);
327 } else {
328 list_for_each_entry(counter, &evsel_list->entries, node)
329 print_counter_aggr(counter, prefix);
330 }
331}
332
1f16c575 333static int __run_perf_stat(int argc __maybe_unused, const char **argv)
42202dd5 334{
56e52e85 335 char msg[512];
42202dd5 336 unsigned long long t0, t1;
cac21425 337 struct perf_evsel *counter;
13370a9b 338 struct timespec ts;
42202dd5 339 int status = 0;
051ae7f7 340 int child_ready_pipe[2], go_pipe[2];
6be2850e 341 const bool forks = (argc > 0);
051ae7f7 342 char buf;
42202dd5 343
13370a9b
SE
344 if (interval) {
345 ts.tv_sec = interval / 1000;
346 ts.tv_nsec = (interval % 1000) * 1000000;
347 } else {
348 ts.tv_sec = 1;
349 ts.tv_nsec = 0;
350 }
351
60666c63 352 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
051ae7f7 353 perror("failed to create pipes");
fceda7fe 354 return -1;
051ae7f7
PM
355 }
356
60666c63 357 if (forks) {
6be2850e 358 if ((child_pid = fork()) < 0)
60666c63
LW
359 perror("failed to fork");
360
6be2850e 361 if (!child_pid) {
60666c63
LW
362 close(child_ready_pipe[0]);
363 close(go_pipe[1]);
364 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
365
366 /*
367 * Do a dummy execvp to get the PLT entry resolved,
368 * so we avoid the resolver overhead on the real
369 * execvp call.
370 */
371 execvp("", (char **)argv);
372
373 /*
374 * Tell the parent we're ready to go
375 */
376 close(child_ready_pipe[1]);
377
378 /*
379 * Wait until the parent tells us to go.
380 */
381 if (read(go_pipe[0], &buf, 1) == -1)
382 perror("unable to read pipe");
383
384 execvp(argv[0], (char **)argv);
385
386 perror(argv[0]);
387 exit(-1);
388 }
051ae7f7 389
d67356e7 390 if (perf_target__none(&target))
7e2ed097 391 evsel_list->threads->map[0] = child_pid;
d6d901c2 392
051ae7f7 393 /*
60666c63 394 * Wait for the child to be ready to exec.
051ae7f7
PM
395 */
396 close(child_ready_pipe[1]);
60666c63
LW
397 close(go_pipe[0]);
398 if (read(child_ready_pipe[0], &buf, 1) == -1)
a92bef0f 399 perror("unable to read pipe");
60666c63 400 close(child_ready_pipe[0]);
051ae7f7
PM
401 }
402
6a4bb04c 403 if (group)
63dab225 404 perf_evlist__set_leader(evsel_list);
6a4bb04c 405
361c99a6 406 list_for_each_entry(counter, &evsel_list->entries, node) {
cac21425 407 if (create_perf_stat_counter(counter) < 0) {
979987a5
DA
408 /*
409 * PPC returns ENXIO for HW counters until 2.6.37
410 * (behavior changed with commit b0a873e).
411 */
38f6ae1e 412 if (errno == EINVAL || errno == ENOSYS ||
979987a5
DA
413 errno == ENOENT || errno == EOPNOTSUPP ||
414 errno == ENXIO) {
c63ca0c0
DA
415 if (verbose)
416 ui__warning("%s event is not supported by the kernel.\n",
7289f83c 417 perf_evsel__name(counter));
2cee77c4 418 counter->supported = false;
ede70290 419 continue;
c63ca0c0 420 }
ede70290 421
56e52e85
ACM
422 perf_evsel__open_strerror(counter, &target,
423 errno, msg, sizeof(msg));
424 ui__error("%s\n", msg);
425
48290609
ACM
426 if (child_pid != -1)
427 kill(child_pid, SIGTERM);
fceda7fe 428
48290609
ACM
429 return -1;
430 }
2cee77c4 431 counter->supported = true;
084ab9f8 432 }
42202dd5 433
1491a632 434 if (perf_evlist__apply_filters(evsel_list)) {
cfd748ae
FW
435 error("failed to set filter with %d (%s)\n", errno,
436 strerror(errno));
437 return -1;
438 }
439
42202dd5
IM
440 /*
441 * Enable counters and exec the command:
442 */
443 t0 = rdclock();
13370a9b 444 clock_gettime(CLOCK_MONOTONIC, &ref_time);
42202dd5 445
60666c63
LW
446 if (forks) {
447 close(go_pipe[1]);
13370a9b
SE
448 if (interval) {
449 while (!waitpid(child_pid, &status, WNOHANG)) {
450 nanosleep(&ts, NULL);
451 print_interval();
452 }
453 }
60666c63 454 wait(&status);
33e49ea7
AK
455 if (WIFSIGNALED(status))
456 psignal(WTERMSIG(status), argv[0]);
60666c63 457 } else {
13370a9b
SE
458 while (!done) {
459 nanosleep(&ts, NULL);
460 if (interval)
461 print_interval();
462 }
60666c63 463 }
42202dd5 464
42202dd5
IM
465 t1 = rdclock();
466
9e9772c4 467 update_stats(&walltime_nsecs_stats, t1 - t0);
42202dd5 468
f5b4a9c3 469 if (no_aggr) {
361c99a6 470 list_for_each_entry(counter, &evsel_list->entries, node) {
f5b4a9c3 471 read_counter(counter);
7ae92e74 472 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
c52b12ed 473 }
f5b4a9c3 474 } else {
361c99a6 475 list_for_each_entry(counter, &evsel_list->entries, node) {
f5b4a9c3 476 read_counter_aggr(counter);
7ae92e74 477 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
7e2ed097 478 evsel_list->threads->nr);
c52b12ed 479 }
f5b4a9c3 480 }
c52b12ed 481
42202dd5
IM
482 return WEXITSTATUS(status);
483}
484
1f16c575
PZ
485static int run_perf_stat(int argc __maybe_unused, const char **argv)
486{
487 int ret;
488
489 if (pre_cmd) {
490 ret = system(pre_cmd);
491 if (ret)
492 return ret;
493 }
494
495 if (sync_run)
496 sync();
497
498 ret = __run_perf_stat(argc, argv);
499 if (ret)
500 return ret;
501
502 if (post_cmd) {
503 ret = system(post_cmd);
504 if (ret)
505 return ret;
506 }
507
508 return ret;
509}
510
f99844cb
IM
511static void print_noise_pct(double total, double avg)
512{
0007ecea 513 double pct = rel_stddev_stats(total, avg);
f99844cb 514
3ae9a34d 515 if (csv_output)
4aa9015f 516 fprintf(output, "%s%.2f%%", csv_sep, pct);
a1bca6cc 517 else if (pct)
4aa9015f 518 fprintf(output, " ( +-%6.2f%% )", pct);
f99844cb
IM
519}
520
69aad6f1 521static void print_noise(struct perf_evsel *evsel, double avg)
42202dd5 522{
69aad6f1
ACM
523 struct perf_stat *ps;
524
849abde9
PZ
525 if (run_count == 1)
526 return;
527
69aad6f1 528 ps = evsel->priv;
f99844cb 529 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
42202dd5
IM
530}
531
daec78a0 532static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
44175b6f 533{
506d4bc8 534 double msecs = avg / 1e6;
d7470b6a 535 char cpustr[16] = { '\0', };
2cba3ffb 536 const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
44175b6f 537
f5b4a9c3 538 if (no_aggr)
d7470b6a
SE
539 sprintf(cpustr, "CPU%*d%s",
540 csv_output ? 0 : -4,
7ae92e74 541 perf_evsel__cpus(evsel)->map[cpu], csv_sep);
d7470b6a 542
7289f83c 543 fprintf(output, fmt, cpustr, msecs, csv_sep, perf_evsel__name(evsel));
d7470b6a 544
023695d9 545 if (evsel->cgrp)
4aa9015f 546 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
023695d9 547
13370a9b 548 if (csv_output || interval)
d7470b6a 549 return;
44175b6f 550
daec78a0 551 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
4aa9015f
SE
552 fprintf(output, " # %8.3f CPUs utilized ",
553 avg / avg_stats(&walltime_nsecs_stats));
9dac6a29
NK
554 else
555 fprintf(output, " ");
44175b6f
IM
556}
557
15e6392f
NK
558/* used for get_ratio_color() */
559enum grc_type {
560 GRC_STALLED_CYCLES_FE,
561 GRC_STALLED_CYCLES_BE,
562 GRC_CACHE_MISSES,
563 GRC_MAX_NR
564};
565
566static const char *get_ratio_color(enum grc_type type, double ratio)
567{
568 static const double grc_table[GRC_MAX_NR][3] = {
569 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
570 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
571 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
572 };
573 const char *color = PERF_COLOR_NORMAL;
574
575 if (ratio > grc_table[type][0])
576 color = PERF_COLOR_RED;
577 else if (ratio > grc_table[type][1])
578 color = PERF_COLOR_MAGENTA;
579 else if (ratio > grc_table[type][2])
580 color = PERF_COLOR_YELLOW;
581
582 return color;
583}
584
1d037ca1
IT
585static void print_stalled_cycles_frontend(int cpu,
586 struct perf_evsel *evsel
587 __maybe_unused, double avg)
d3d1e86d
IM
588{
589 double total, ratio = 0.0;
590 const char *color;
591
592 total = avg_stats(&runtime_cycles_stats[cpu]);
593
594 if (total)
595 ratio = avg / total * 100.0;
596
15e6392f 597 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
d3d1e86d 598
4aa9015f
SE
599 fprintf(output, " # ");
600 color_fprintf(output, color, "%6.2f%%", ratio);
601 fprintf(output, " frontend cycles idle ");
d3d1e86d
IM
602}
603
1d037ca1
IT
604static void print_stalled_cycles_backend(int cpu,
605 struct perf_evsel *evsel
606 __maybe_unused, double avg)
a5d243d0
IM
607{
608 double total, ratio = 0.0;
609 const char *color;
610
611 total = avg_stats(&runtime_cycles_stats[cpu]);
612
613 if (total)
614 ratio = avg / total * 100.0;
615
15e6392f 616 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
a5d243d0 617
4aa9015f
SE
618 fprintf(output, " # ");
619 color_fprintf(output, color, "%6.2f%%", ratio);
620 fprintf(output, " backend cycles idle ");
a5d243d0
IM
621}
622
1d037ca1
IT
623static void print_branch_misses(int cpu,
624 struct perf_evsel *evsel __maybe_unused,
625 double avg)
c78df6c1
IM
626{
627 double total, ratio = 0.0;
628 const char *color;
629
630 total = avg_stats(&runtime_branches_stats[cpu]);
631
632 if (total)
633 ratio = avg / total * 100.0;
634
15e6392f 635 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
c78df6c1 636
4aa9015f
SE
637 fprintf(output, " # ");
638 color_fprintf(output, color, "%6.2f%%", ratio);
639 fprintf(output, " of all branches ");
c78df6c1
IM
640}
641
1d037ca1
IT
642static void print_l1_dcache_misses(int cpu,
643 struct perf_evsel *evsel __maybe_unused,
644 double avg)
8bb6c79f
IM
645{
646 double total, ratio = 0.0;
647 const char *color;
648
649 total = avg_stats(&runtime_l1_dcache_stats[cpu]);
650
651 if (total)
652 ratio = avg / total * 100.0;
653
15e6392f 654 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
8bb6c79f 655
4aa9015f
SE
656 fprintf(output, " # ");
657 color_fprintf(output, color, "%6.2f%%", ratio);
658 fprintf(output, " of all L1-dcache hits ");
8bb6c79f
IM
659}
660
1d037ca1
IT
661static void print_l1_icache_misses(int cpu,
662 struct perf_evsel *evsel __maybe_unused,
663 double avg)
c3305257
IM
664{
665 double total, ratio = 0.0;
666 const char *color;
667
668 total = avg_stats(&runtime_l1_icache_stats[cpu]);
669
670 if (total)
671 ratio = avg / total * 100.0;
672
15e6392f 673 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
c3305257 674
4aa9015f
SE
675 fprintf(output, " # ");
676 color_fprintf(output, color, "%6.2f%%", ratio);
677 fprintf(output, " of all L1-icache hits ");
c3305257
IM
678}
679
1d037ca1
IT
680static void print_dtlb_cache_misses(int cpu,
681 struct perf_evsel *evsel __maybe_unused,
682 double avg)
c3305257
IM
683{
684 double total, ratio = 0.0;
685 const char *color;
686
687 total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
688
689 if (total)
690 ratio = avg / total * 100.0;
691
15e6392f 692 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
c3305257 693
4aa9015f
SE
694 fprintf(output, " # ");
695 color_fprintf(output, color, "%6.2f%%", ratio);
696 fprintf(output, " of all dTLB cache hits ");
c3305257
IM
697}
698
1d037ca1
IT
699static void print_itlb_cache_misses(int cpu,
700 struct perf_evsel *evsel __maybe_unused,
701 double avg)
c3305257
IM
702{
703 double total, ratio = 0.0;
704 const char *color;
705
706 total = avg_stats(&runtime_itlb_cache_stats[cpu]);
707
708 if (total)
709 ratio = avg / total * 100.0;
710
15e6392f 711 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
c3305257 712
4aa9015f
SE
713 fprintf(output, " # ");
714 color_fprintf(output, color, "%6.2f%%", ratio);
715 fprintf(output, " of all iTLB cache hits ");
c3305257
IM
716}
717
1d037ca1
IT
718static void print_ll_cache_misses(int cpu,
719 struct perf_evsel *evsel __maybe_unused,
720 double avg)
c3305257
IM
721{
722 double total, ratio = 0.0;
723 const char *color;
724
725 total = avg_stats(&runtime_ll_cache_stats[cpu]);
726
727 if (total)
728 ratio = avg / total * 100.0;
729
15e6392f 730 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
c3305257 731
4aa9015f
SE
732 fprintf(output, " # ");
733 color_fprintf(output, color, "%6.2f%%", ratio);
734 fprintf(output, " of all LL-cache hits ");
c3305257
IM
735}
736
daec78a0 737static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
44175b6f 738{
c7f7fea3 739 double total, ratio = 0.0;
f5b4a9c3 740 char cpustr[16] = { '\0', };
d7470b6a
SE
741 const char *fmt;
742
743 if (csv_output)
744 fmt = "%s%.0f%s%s";
745 else if (big_num)
2cba3ffb 746 fmt = "%s%'18.0f%s%-25s";
d7470b6a 747 else
2cba3ffb 748 fmt = "%s%18.0f%s%-25s";
f5b4a9c3
SE
749
750 if (no_aggr)
d7470b6a
SE
751 sprintf(cpustr, "CPU%*d%s",
752 csv_output ? 0 : -4,
7ae92e74 753 perf_evsel__cpus(evsel)->map[cpu], csv_sep);
f5b4a9c3
SE
754 else
755 cpu = 0;
c7f7fea3 756
7289f83c 757 fprintf(output, fmt, cpustr, avg, csv_sep, perf_evsel__name(evsel));
d7470b6a 758
023695d9 759 if (evsel->cgrp)
4aa9015f 760 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
023695d9 761
13370a9b 762 if (csv_output || interval)
d7470b6a 763 return;
44175b6f 764
daec78a0 765 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
f5b4a9c3 766 total = avg_stats(&runtime_cycles_stats[cpu]);
c7f7fea3
IM
767 if (total)
768 ratio = avg / total;
769
4aa9015f 770 fprintf(output, " # %5.2f insns per cycle ", ratio);
481f988a 771
d3d1e86d
IM
772 total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
773 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
481f988a
IM
774
775 if (total && avg) {
776 ratio = total / avg;
4aa9015f 777 fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
481f988a
IM
778 }
779
daec78a0 780 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
f5b4a9c3 781 runtime_branches_stats[cpu].n != 0) {
c78df6c1 782 print_branch_misses(cpu, evsel, avg);
8bb6c79f
IM
783 } else if (
784 evsel->attr.type == PERF_TYPE_HW_CACHE &&
785 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
786 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
787 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
c6264def 788 runtime_l1_dcache_stats[cpu].n != 0) {
8bb6c79f 789 print_l1_dcache_misses(cpu, evsel, avg);
c3305257
IM
790 } else if (
791 evsel->attr.type == PERF_TYPE_HW_CACHE &&
792 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
793 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
794 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
795 runtime_l1_icache_stats[cpu].n != 0) {
796 print_l1_icache_misses(cpu, evsel, avg);
797 } else if (
798 evsel->attr.type == PERF_TYPE_HW_CACHE &&
799 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
800 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
801 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
802 runtime_dtlb_cache_stats[cpu].n != 0) {
803 print_dtlb_cache_misses(cpu, evsel, avg);
804 } else if (
805 evsel->attr.type == PERF_TYPE_HW_CACHE &&
806 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
807 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
808 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
809 runtime_itlb_cache_stats[cpu].n != 0) {
810 print_itlb_cache_misses(cpu, evsel, avg);
811 } else if (
812 evsel->attr.type == PERF_TYPE_HW_CACHE &&
813 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
814 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
815 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
816 runtime_ll_cache_stats[cpu].n != 0) {
817 print_ll_cache_misses(cpu, evsel, avg);
d58f4c82
IM
818 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
819 runtime_cacherefs_stats[cpu].n != 0) {
820 total = avg_stats(&runtime_cacherefs_stats[cpu]);
821
822 if (total)
823 ratio = avg * 100 / total;
824
4aa9015f 825 fprintf(output, " # %8.3f %% of all cache refs ", ratio);
d58f4c82 826
d3d1e86d
IM
827 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
828 print_stalled_cycles_frontend(cpu, evsel, avg);
129c04cb 829 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
d3d1e86d 830 print_stalled_cycles_backend(cpu, evsel, avg);
481f988a 831 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
f5b4a9c3 832 total = avg_stats(&runtime_nsecs_stats[cpu]);
c7f7fea3
IM
833
834 if (total)
481f988a 835 ratio = 1.0 * avg / total;
c7f7fea3 836
4aa9015f 837 fprintf(output, " # %8.3f GHz ", ratio);
481f988a 838 } else if (runtime_nsecs_stats[cpu].n != 0) {
5fde2523
NK
839 char unit = 'M';
840
481f988a 841 total = avg_stats(&runtime_nsecs_stats[cpu]);
11ba2b85
IM
842
843 if (total)
481f988a 844 ratio = 1000.0 * avg / total;
5fde2523
NK
845 if (ratio < 0.001) {
846 ratio *= 1000;
847 unit = 'K';
848 }
11ba2b85 849
5fde2523 850 fprintf(output, " # %8.3f %c/sec ", ratio, unit);
a5d243d0 851 } else {
4aa9015f 852 fprintf(output, " ");
44175b6f 853 }
44175b6f
IM
854}
855
2996f5dd
IM
856/*
857 * Print out the results of a single counter:
f5b4a9c3 858 * aggregated counts in system-wide mode
2996f5dd 859 */
13370a9b 860static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
2996f5dd 861{
69aad6f1
ACM
862 struct perf_stat *ps = counter->priv;
863 double avg = avg_stats(&ps->res_stats[0]);
c52b12ed 864 int scaled = counter->counts->scaled;
2996f5dd 865
13370a9b
SE
866 if (prefix)
867 fprintf(output, "%s", prefix);
868
2996f5dd 869 if (scaled == -1) {
4aa9015f 870 fprintf(output, "%*s%s%*s",
d7470b6a 871 csv_output ? 0 : 18,
2cee77c4 872 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
023695d9
SE
873 csv_sep,
874 csv_output ? 0 : -24,
7289f83c 875 perf_evsel__name(counter));
023695d9
SE
876
877 if (counter->cgrp)
4aa9015f 878 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
023695d9 879
4aa9015f 880 fputc('\n', output);
2996f5dd
IM
881 return;
882 }
c04f5e5d 883
44175b6f 884 if (nsec_counter(counter))
f5b4a9c3 885 nsec_printout(-1, counter, avg);
44175b6f 886 else
f5b4a9c3 887 abs_printout(-1, counter, avg);
849abde9 888
3ae9a34d
ZH
889 print_noise(counter, avg);
890
d7470b6a 891 if (csv_output) {
4aa9015f 892 fputc('\n', output);
d7470b6a
SE
893 return;
894 }
895
506d4bc8
PZ
896 if (scaled) {
897 double avg_enabled, avg_running;
898
69aad6f1
ACM
899 avg_enabled = avg_stats(&ps->res_stats[1]);
900 avg_running = avg_stats(&ps->res_stats[2]);
d7c29318 901
4aa9015f 902 fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
506d4bc8 903 }
4aa9015f 904 fprintf(output, "\n");
c04f5e5d
IM
905}
906
f5b4a9c3
SE
907/*
908 * Print out the results of a single counter:
909 * does not use aggregated count in system-wide
910 */
13370a9b 911static void print_counter(struct perf_evsel *counter, char *prefix)
f5b4a9c3
SE
912{
913 u64 ena, run, val;
914 int cpu;
915
7ae92e74 916 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
c52b12ed
ACM
917 val = counter->counts->cpu[cpu].val;
918 ena = counter->counts->cpu[cpu].ena;
919 run = counter->counts->cpu[cpu].run;
13370a9b
SE
920
921 if (prefix)
922 fprintf(output, "%s", prefix);
923
f5b4a9c3 924 if (run == 0 || ena == 0) {
4aa9015f 925 fprintf(output, "CPU%*d%s%*s%s%*s",
d7470b6a 926 csv_output ? 0 : -4,
7ae92e74 927 perf_evsel__cpus(counter)->map[cpu], csv_sep,
d7470b6a 928 csv_output ? 0 : 18,
2cee77c4
DA
929 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
930 csv_sep,
023695d9 931 csv_output ? 0 : -24,
7289f83c 932 perf_evsel__name(counter));
f5b4a9c3 933
023695d9 934 if (counter->cgrp)
4aa9015f
SE
935 fprintf(output, "%s%s",
936 csv_sep, counter->cgrp->name);
023695d9 937
4aa9015f 938 fputc('\n', output);
f5b4a9c3
SE
939 continue;
940 }
941
942 if (nsec_counter(counter))
943 nsec_printout(cpu, counter, val);
944 else
945 abs_printout(cpu, counter, val);
946
d7470b6a
SE
947 if (!csv_output) {
948 print_noise(counter, 1.0);
f5b4a9c3 949
c6264def 950 if (run != ena)
4aa9015f
SE
951 fprintf(output, " (%.2f%%)",
952 100.0 * run / ena);
f5b4a9c3 953 }
4aa9015f 954 fputc('\n', output);
f5b4a9c3
SE
955 }
956}
957
42202dd5
IM
958static void print_stat(int argc, const char **argv)
959{
69aad6f1
ACM
960 struct perf_evsel *counter;
961 int i;
42202dd5 962
ddcacfa0
IM
963 fflush(stdout);
964
d7470b6a 965 if (!csv_output) {
4aa9015f
SE
966 fprintf(output, "\n");
967 fprintf(output, " Performance counter stats for ");
aa22dd49 968 if (!perf_target__has_task(&target)) {
4aa9015f 969 fprintf(output, "\'%s", argv[0]);
d7470b6a 970 for (i = 1; i < argc; i++)
4aa9015f 971 fprintf(output, " %s", argv[i]);
20f946b4
NK
972 } else if (target.pid)
973 fprintf(output, "process id \'%s", target.pid);
d7470b6a 974 else
20f946b4 975 fprintf(output, "thread id \'%s", target.tid);
44db76c8 976
4aa9015f 977 fprintf(output, "\'");
d7470b6a 978 if (run_count > 1)
4aa9015f
SE
979 fprintf(output, " (%d runs)", run_count);
980 fprintf(output, ":\n\n");
d7470b6a 981 }
2996f5dd 982
f5b4a9c3 983 if (no_aggr) {
361c99a6 984 list_for_each_entry(counter, &evsel_list->entries, node)
13370a9b 985 print_counter(counter, NULL);
f5b4a9c3 986 } else {
361c99a6 987 list_for_each_entry(counter, &evsel_list->entries, node)
13370a9b 988 print_counter_aggr(counter, NULL);
f5b4a9c3 989 }
ddcacfa0 990
d7470b6a 991 if (!csv_output) {
c3305257 992 if (!null_run)
4aa9015f
SE
993 fprintf(output, "\n");
994 fprintf(output, " %17.9f seconds time elapsed",
d7470b6a
SE
995 avg_stats(&walltime_nsecs_stats)/1e9);
996 if (run_count > 1) {
4aa9015f 997 fprintf(output, " ");
f99844cb
IM
998 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
999 avg_stats(&walltime_nsecs_stats));
d7470b6a 1000 }
4aa9015f 1001 fprintf(output, "\n\n");
566747e6 1002 }
ddcacfa0
IM
1003}
1004
f7b7c26e
PZ
1005static volatile int signr = -1;
1006
5242519b 1007static void skip_signal(int signo)
ddcacfa0 1008{
13370a9b 1009 if ((child_pid == -1) || interval)
60666c63
LW
1010 done = 1;
1011
f7b7c26e
PZ
1012 signr = signo;
1013}
1014
1015static void sig_atexit(void)
1016{
933da83a
CW
1017 if (child_pid != -1)
1018 kill(child_pid, SIGTERM);
1019
f7b7c26e
PZ
1020 if (signr == -1)
1021 return;
1022
1023 signal(signr, SIG_DFL);
1024 kill(getpid(), signr);
5242519b
IM
1025}
1026
1d037ca1
IT
1027static int stat__set_big_num(const struct option *opt __maybe_unused,
1028 const char *s __maybe_unused, int unset)
d7470b6a
SE
1029{
1030 big_num_opt = unset ? 0 : 1;
1031 return 0;
1032}
1033
2cba3ffb
IM
1034/*
1035 * Add default attributes, if there were no attributes specified or
1036 * if -d/--detailed, -d -d or -d -d -d is used:
1037 */
1038static int add_default_attributes(void)
1039{
b070a547
ACM
1040 struct perf_event_attr default_attrs[] = {
1041
1042 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1043 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1044 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1045 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1046
1047 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1048 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1049 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1050 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1051 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1052 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1053
1054};
1055
1056/*
1057 * Detailed stats (-d), covering the L1 and last level data caches:
1058 */
1059 struct perf_event_attr detailed_attrs[] = {
1060
1061 { .type = PERF_TYPE_HW_CACHE,
1062 .config =
1063 PERF_COUNT_HW_CACHE_L1D << 0 |
1064 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1065 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1066
1067 { .type = PERF_TYPE_HW_CACHE,
1068 .config =
1069 PERF_COUNT_HW_CACHE_L1D << 0 |
1070 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1071 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1072
1073 { .type = PERF_TYPE_HW_CACHE,
1074 .config =
1075 PERF_COUNT_HW_CACHE_LL << 0 |
1076 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1077 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1078
1079 { .type = PERF_TYPE_HW_CACHE,
1080 .config =
1081 PERF_COUNT_HW_CACHE_LL << 0 |
1082 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1083 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1084};
1085
1086/*
1087 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1088 */
1089 struct perf_event_attr very_detailed_attrs[] = {
1090
1091 { .type = PERF_TYPE_HW_CACHE,
1092 .config =
1093 PERF_COUNT_HW_CACHE_L1I << 0 |
1094 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1095 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1096
1097 { .type = PERF_TYPE_HW_CACHE,
1098 .config =
1099 PERF_COUNT_HW_CACHE_L1I << 0 |
1100 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1101 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1102
1103 { .type = PERF_TYPE_HW_CACHE,
1104 .config =
1105 PERF_COUNT_HW_CACHE_DTLB << 0 |
1106 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1107 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1108
1109 { .type = PERF_TYPE_HW_CACHE,
1110 .config =
1111 PERF_COUNT_HW_CACHE_DTLB << 0 |
1112 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1113 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1114
1115 { .type = PERF_TYPE_HW_CACHE,
1116 .config =
1117 PERF_COUNT_HW_CACHE_ITLB << 0 |
1118 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1119 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1120
1121 { .type = PERF_TYPE_HW_CACHE,
1122 .config =
1123 PERF_COUNT_HW_CACHE_ITLB << 0 |
1124 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1125 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1126
1127};
1128
1129/*
1130 * Very, very detailed stats (-d -d -d), adding prefetch events:
1131 */
1132 struct perf_event_attr very_very_detailed_attrs[] = {
1133
1134 { .type = PERF_TYPE_HW_CACHE,
1135 .config =
1136 PERF_COUNT_HW_CACHE_L1D << 0 |
1137 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1138 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1139
1140 { .type = PERF_TYPE_HW_CACHE,
1141 .config =
1142 PERF_COUNT_HW_CACHE_L1D << 0 |
1143 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1144 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1145};
1146
2cba3ffb
IM
1147 /* Set attrs if no event is selected and !null_run: */
1148 if (null_run)
1149 return 0;
1150
1151 if (!evsel_list->nr_entries) {
79695e1b 1152 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
50d08e47 1153 return -1;
2cba3ffb
IM
1154 }
1155
1156 /* Detailed events get appended to the event list: */
1157
1158 if (detailed_run < 1)
1159 return 0;
1160
1161 /* Append detailed run extra attributes: */
79695e1b 1162 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
50d08e47 1163 return -1;
2cba3ffb
IM
1164
1165 if (detailed_run < 2)
1166 return 0;
1167
1168 /* Append very detailed run extra attributes: */
79695e1b 1169 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
50d08e47 1170 return -1;
2cba3ffb
IM
1171
1172 if (detailed_run < 3)
1173 return 0;
1174
1175 /* Append very, very detailed run extra attributes: */
79695e1b 1176 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2cba3ffb
IM
1177}
1178
1d037ca1 1179int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
5242519b 1180{
1f16c575 1181 bool append_file = false;
b070a547
ACM
1182 int output_fd = 0;
1183 const char *output_name = NULL;
1184 const struct option options[] = {
1185 OPT_CALLBACK('e', "event", &evsel_list, "event",
1186 "event selector. use 'perf list' to list available events",
1187 parse_events_option),
1188 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1189 "event filter", parse_filter),
1190 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1191 "child tasks do not inherit counters"),
1192 OPT_STRING('p', "pid", &target.pid, "pid",
1193 "stat events on existing process id"),
1194 OPT_STRING('t', "tid", &target.tid, "tid",
1195 "stat events on existing thread id"),
1196 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1197 "system-wide collection from all CPUs"),
1198 OPT_BOOLEAN('g', "group", &group,
1199 "put the counters into a counter group"),
1200 OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
1201 OPT_INCR('v', "verbose", &verbose,
1202 "be more verbose (show counter open errors, etc)"),
1203 OPT_INTEGER('r', "repeat", &run_count,
1204 "repeat command and print average + stddev (max: 100)"),
1205 OPT_BOOLEAN('n', "null", &null_run,
1206 "null run - dont start any counters"),
1207 OPT_INCR('d', "detailed", &detailed_run,
1208 "detailed run - start a lot of events"),
1209 OPT_BOOLEAN('S', "sync", &sync_run,
1210 "call sync() before starting a run"),
1211 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1212 "print large numbers with thousands\' separators",
1213 stat__set_big_num),
1214 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1215 "list of cpus to monitor in system-wide"),
1216 OPT_BOOLEAN('A', "no-aggr", &no_aggr, "disable CPU count aggregation"),
1217 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1218 "print counts with custom separator"),
1219 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1220 "monitor event in cgroup name only", parse_cgroups),
1221 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1222 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1223 OPT_INTEGER(0, "log-fd", &output_fd,
1224 "log output to fd, instead of stderr"),
1f16c575
PZ
1225 OPT_STRING(0, "pre", &pre_cmd, "command",
1226 "command to run prior to the measured command"),
1227 OPT_STRING(0, "post", &post_cmd, "command",
1228 "command to run after to the measured command"),
13370a9b
SE
1229 OPT_UINTEGER('I', "interval-print", &interval,
1230 "print counts at regular interval in ms (>= 100)"),
b070a547
ACM
1231 OPT_END()
1232 };
1233 const char * const stat_usage[] = {
1234 "perf stat [<options>] [<command>]",
1235 NULL
1236 };
69aad6f1 1237 struct perf_evsel *pos;
b070a547 1238 int status = -ENOMEM, run_idx;
4aa9015f 1239 const char *mode;
42202dd5 1240
5af52b51
SE
1241 setlocale(LC_ALL, "");
1242
7e2ed097 1243 evsel_list = perf_evlist__new(NULL, NULL);
361c99a6
ACM
1244 if (evsel_list == NULL)
1245 return -ENOMEM;
1246
a0541234
AB
1247 argc = parse_options(argc, argv, options, stat_usage,
1248 PARSE_OPT_STOP_AT_NON_OPTION);
d7470b6a 1249
4aa9015f
SE
1250 output = stderr;
1251 if (output_name && strcmp(output_name, "-"))
1252 output = NULL;
1253
56f3bae7
JC
1254 if (output_name && output_fd) {
1255 fprintf(stderr, "cannot use both --output and --log-fd\n");
1256 usage_with_options(stat_usage, options);
1257 }
fc3e4d07
SE
1258
1259 if (output_fd < 0) {
1260 fprintf(stderr, "argument to --log-fd must be a > 0\n");
1261 usage_with_options(stat_usage, options);
1262 }
1263
4aa9015f
SE
1264 if (!output) {
1265 struct timespec tm;
1266 mode = append_file ? "a" : "w";
1267
1268 output = fopen(output_name, mode);
1269 if (!output) {
1270 perror("failed to create output file");
fceda7fe 1271 return -1;
4aa9015f
SE
1272 }
1273 clock_gettime(CLOCK_REALTIME, &tm);
1274 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
fc3e4d07 1275 } else if (output_fd > 0) {
56f3bae7
JC
1276 mode = append_file ? "a" : "w";
1277 output = fdopen(output_fd, mode);
1278 if (!output) {
1279 perror("Failed opening logfd");
1280 return -errno;
1281 }
4aa9015f
SE
1282 }
1283
d4ffd04d 1284 if (csv_sep) {
d7470b6a 1285 csv_output = true;
d4ffd04d
JC
1286 if (!strcmp(csv_sep, "\\t"))
1287 csv_sep = "\t";
1288 } else
d7470b6a
SE
1289 csv_sep = DEFAULT_SEPARATOR;
1290
1291 /*
1292 * let the spreadsheet do the pretty-printing
1293 */
1294 if (csv_output) {
61a9f324 1295 /* User explicitly passed -B? */
d7470b6a
SE
1296 if (big_num_opt == 1) {
1297 fprintf(stderr, "-B option not supported with -x\n");
1298 usage_with_options(stat_usage, options);
1299 } else /* Nope, so disable big number formatting */
1300 big_num = false;
1301 } else if (big_num_opt == 0) /* User passed --no-big-num */
1302 big_num = false;
1303
aa22dd49 1304 if (!argc && !perf_target__has_task(&target))
5242519b 1305 usage_with_options(stat_usage, options);
9e9772c4 1306 if (run_count <= 0)
42202dd5 1307 usage_with_options(stat_usage, options);
ddcacfa0 1308
023695d9 1309 /* no_aggr, cgroup are for system-wide only */
aa22dd49 1310 if ((no_aggr || nr_cgroups) && !perf_target__has_cpu(&target)) {
023695d9
SE
1311 fprintf(stderr, "both cgroup and no-aggregation "
1312 "modes only available in system-wide mode\n");
1313
f5b4a9c3 1314 usage_with_options(stat_usage, options);
023695d9 1315 }
f5b4a9c3 1316
2cba3ffb
IM
1317 if (add_default_attributes())
1318 goto out;
ddcacfa0 1319
4bd0f2d2 1320 perf_target__validate(&target);
5c98d466 1321
77a6f014 1322 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
aa22dd49 1323 if (perf_target__has_task(&target))
77a6f014 1324 pr_err("Problems finding threads of monitor\n");
aa22dd49 1325 if (perf_target__has_cpu(&target))
77a6f014 1326 perror("failed to parse CPUs map");
ddcacfa0 1327
c45c6ea2 1328 usage_with_options(stat_usage, options);
60d567e2
ACM
1329 return -1;
1330 }
13370a9b
SE
1331 if (interval && interval < 100) {
1332 pr_err("print interval must be >= 100ms\n");
1333 usage_with_options(stat_usage, options);
1334 return -1;
1335 }
c45c6ea2 1336
361c99a6 1337 list_for_each_entry(pos, &evsel_list->entries, node) {
c52b12ed 1338 if (perf_evsel__alloc_stat_priv(pos) < 0 ||
7ae92e74 1339 perf_evsel__alloc_counts(pos, perf_evsel__nr_cpus(pos)) < 0)
69aad6f1 1340 goto out_free_fd;
d6d901c2 1341 }
13370a9b
SE
1342 if (interval) {
1343 list_for_each_entry(pos, &evsel_list->entries, node) {
1344 if (perf_evsel__alloc_prev_raw_counts(pos) < 0)
1345 goto out_free_fd;
1346 }
1347 }
d6d901c2 1348
58d7e993
IM
1349 /*
1350 * We dont want to block the signals - that would cause
1351 * child tasks to inherit that and Ctrl-C would not work.
1352 * What we want is for Ctrl-C to work in the exec()-ed
1353 * task, but being ignored by perf stat itself:
1354 */
f7b7c26e 1355 atexit(sig_atexit);
58d7e993 1356 signal(SIGINT, skip_signal);
13370a9b 1357 signal(SIGCHLD, skip_signal);
58d7e993
IM
1358 signal(SIGALRM, skip_signal);
1359 signal(SIGABRT, skip_signal);
1360
42202dd5
IM
1361 status = 0;
1362 for (run_idx = 0; run_idx < run_count; run_idx++) {
1363 if (run_count != 1 && verbose)
4aa9015f
SE
1364 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1365 run_idx + 1);
f9cef0a9 1366
42202dd5
IM
1367 status = run_perf_stat(argc, argv);
1368 }
1369
13370a9b 1370 if (status != -1 && !interval)
084ab9f8 1371 print_stat(argc, argv);
69aad6f1 1372out_free_fd:
13370a9b 1373 list_for_each_entry(pos, &evsel_list->entries, node) {
69aad6f1 1374 perf_evsel__free_stat_priv(pos);
43f8e76e 1375 perf_evsel__free_counts(pos);
13370a9b
SE
1376 perf_evsel__free_prev_raw_counts(pos);
1377 }
7e2ed097 1378 perf_evlist__delete_maps(evsel_list);
0015e2e1
ACM
1379out:
1380 perf_evlist__delete(evsel_list);
42202dd5 1381 return status;
ddcacfa0 1382}