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