]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - Documentation/perf_counter/builtin-stat.c
perf top: Fix zero or negative refresh delay
[mirror_ubuntu-focal-kernel.git] / Documentation / perf_counter / 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
bf9e1876
IM
9 $ perf stat ~/hackbench 10
10 Time: 0.104
ddcacfa0 11
bf9e1876 12 Performance counter stats for '/home/mingo/hackbench':
ddcacfa0 13
bf9e1876
IM
14 1255.538611 task clock ticks # 10.143 CPU utilization factor
15 54011 context switches # 0.043 M/sec
16 385 CPU migrations # 0.000 M/sec
17 17755 pagefaults # 0.014 M/sec
18 3808323185 CPU cycles # 3033.219 M/sec
19 1575111190 instructions # 1254.530 M/sec
20 17367895 cache references # 13.833 M/sec
21 7674421 cache misses # 6.112 M/sec
ddcacfa0 22
bf9e1876 23 Wall-clock time elapsed: 123.786620 msecs
ddcacfa0 24
5242519b
IM
25 *
26 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
27 *
28 * Improvements and fixes by:
29 *
30 * Arjan van de Ven <arjan@linux.intel.com>
31 * Yanmin Zhang <yanmin.zhang@intel.com>
32 * Wu Fengguang <fengguang.wu@intel.com>
33 * Mike Galbraith <efault@gmx.de>
34 * Paul Mackerras <paulus@samba.org>
35 *
36 * Released under the GPL v2. (and only v2, not any later version)
ddcacfa0
IM
37 */
38
1a482f38 39#include "perf.h"
16f762a2 40#include "builtin.h"
148be2c1 41#include "util/util.h"
5242519b
IM
42#include "util/parse-options.h"
43#include "util/parse-events.h"
ddcacfa0 44
ddcacfa0 45#include <sys/prctl.h>
16c8a109 46
ddcacfa0 47static int system_wide = 0;
5242519b 48static int inherit = 1;
ddcacfa0 49
5242519b 50static __u64 default_event_id[MAX_COUNTERS] = {
ddcacfa0
IM
51 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
52 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
53 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
54 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
55
56 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
57 EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
58 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
59 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
60};
5242519b 61
ddcacfa0
IM
62static int default_interval = 100000;
63static int event_count[MAX_COUNTERS];
64static int fd[MAX_NR_CPUS][MAX_COUNTERS];
65
5242519b 66static int target_pid = -1;
ddcacfa0 67static int nr_cpus = 0;
ddcacfa0
IM
68static unsigned int page_size;
69
66cf7829 70static int scale = 1;
ddcacfa0
IM
71
72static const unsigned int default_count[] = {
73 1000000,
74 1000000,
75 10000,
76 10000,
77 1000000,
78 10000,
79};
80
2996f5dd
IM
81static __u64 event_res[MAX_COUNTERS][3];
82static __u64 event_scaled[MAX_COUNTERS];
83
be1ac0d8 84static __u64 runtime_nsecs;
d7c29318 85static __u64 walltime_nsecs;
be1ac0d8 86
ddcacfa0
IM
87static void create_perfstat_counter(int counter)
88{
c70975bc 89 struct perf_counter_attr attr;
ddcacfa0 90
c70975bc
PZ
91 memset(&attr, 0, sizeof(attr));
92 attr.config = event_id[counter];
93 attr.sample_type = 0;
94 attr.exclude_kernel = event_mask[counter] & EVENT_MASK_KERNEL;
95 attr.exclude_user = event_mask[counter] & EVENT_MASK_USER;
16c8a109 96
ddcacfa0 97 if (scale)
c70975bc 98 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
ddcacfa0
IM
99 PERF_FORMAT_TOTAL_TIME_RUNNING;
100
101 if (system_wide) {
102 int cpu;
103 for (cpu = 0; cpu < nr_cpus; cpu ++) {
c70975bc 104 fd[cpu][counter] = sys_perf_counter_open(&attr, -1, cpu, -1, 0);
ddcacfa0
IM
105 if (fd[cpu][counter] < 0) {
106 printf("perfstat error: syscall returned with %d (%s)\n",
107 fd[cpu][counter], strerror(errno));
108 exit(-1);
109 }
110 }
111 } else {
c70975bc
PZ
112 attr.inherit = inherit;
113 attr.disabled = 1;
ddcacfa0 114
c70975bc 115 fd[0][counter] = sys_perf_counter_open(&attr, 0, -1, -1, 0);
ddcacfa0
IM
116 if (fd[0][counter] < 0) {
117 printf("perfstat error: syscall returned with %d (%s)\n",
118 fd[0][counter], strerror(errno));
119 exit(-1);
120 }
121 }
122}
123
c04f5e5d
IM
124/*
125 * Does the counter have nsecs as a unit?
126 */
127static inline int nsec_counter(int counter)
128{
129 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK))
130 return 1;
131 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
132 return 1;
133
134 return 0;
135}
136
137/*
2996f5dd 138 * Read out the results of a single counter:
c04f5e5d 139 */
2996f5dd 140static void read_counter(int counter)
c04f5e5d 141{
2996f5dd 142 __u64 *count, single_count[3];
c04f5e5d
IM
143 ssize_t res;
144 int cpu, nv;
145 int scaled;
146
2996f5dd
IM
147 count = event_res[counter];
148
c04f5e5d 149 count[0] = count[1] = count[2] = 0;
2996f5dd 150
c04f5e5d
IM
151 nv = scale ? 3 : 1;
152 for (cpu = 0; cpu < nr_cpus; cpu ++) {
153 res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
154 assert(res == nv * sizeof(__u64));
155
156 count[0] += single_count[0];
157 if (scale) {
158 count[1] += single_count[1];
159 count[2] += single_count[2];
160 }
161 }
162
163 scaled = 0;
164 if (scale) {
165 if (count[2] == 0) {
2996f5dd
IM
166 event_scaled[counter] = -1;
167 count[0] = 0;
c04f5e5d
IM
168 return;
169 }
2996f5dd 170
c04f5e5d 171 if (count[2] < count[1]) {
2996f5dd 172 event_scaled[counter] = 1;
c04f5e5d
IM
173 count[0] = (unsigned long long)
174 ((double)count[0] * count[1] / count[2] + 0.5);
175 }
176 }
be1ac0d8
IM
177 /*
178 * Save the full runtime - to allow normalization during printout:
179 */
180 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
181 runtime_nsecs = count[0];
2996f5dd
IM
182}
183
184/*
185 * Print out the results of a single counter:
186 */
187static void print_counter(int counter)
188{
189 __u64 *count;
190 int scaled;
191
192 count = event_res[counter];
193 scaled = event_scaled[counter];
194
195 if (scaled == -1) {
196 fprintf(stderr, " %14s %-20s\n",
197 "<not counted>", event_name(counter));
198 return;
199 }
c04f5e5d
IM
200
201 if (nsec_counter(counter)) {
202 double msecs = (double)count[0] / 1000000;
203
d7c29318 204 fprintf(stderr, " %14.6f %-20s",
c04f5e5d 205 msecs, event_name(counter));
d7c29318
IM
206 if (event_id[counter] ==
207 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK)) {
208
209 fprintf(stderr, " # %11.3f CPU utilization factor",
210 (double)count[0] / (double)walltime_nsecs);
211 }
c04f5e5d 212 } else {
be1ac0d8 213 fprintf(stderr, " %14Ld %-20s",
c04f5e5d 214 count[0], event_name(counter));
be1ac0d8 215 if (runtime_nsecs)
d7c29318 216 fprintf(stderr, " # %11.3f M/sec",
be1ac0d8 217 (double)count[0]/runtime_nsecs*1000.0);
c04f5e5d
IM
218 }
219 if (scaled)
220 fprintf(stderr, " (scaled from %.2f%%)",
221 (double) count[2] / count[1] * 100);
222 fprintf(stderr, "\n");
223}
224
16f762a2 225static int do_perfstat(int argc, const char **argv)
ddcacfa0
IM
226{
227 unsigned long long t0, t1;
228 int counter;
ddcacfa0
IM
229 int status;
230 int pid;
44db76c8 231 int i;
ddcacfa0
IM
232
233 if (!system_wide)
234 nr_cpus = 1;
235
236 for (counter = 0; counter < nr_counters; counter++)
237 create_perfstat_counter(counter);
238
ddcacfa0
IM
239 /*
240 * Enable counters and exec the command:
241 */
242 t0 = rdclock();
243 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
244
245 if ((pid = fork()) < 0)
246 perror("failed to fork");
44db76c8 247
ddcacfa0 248 if (!pid) {
5242519b 249 if (execvp(argv[0], (char **)argv)) {
ddcacfa0
IM
250 perror(argv[0]);
251 exit(-1);
252 }
253 }
44db76c8 254
ddcacfa0
IM
255 while (wait(&status) >= 0)
256 ;
44db76c8 257
ddcacfa0
IM
258 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
259 t1 = rdclock();
260
d7c29318
IM
261 walltime_nsecs = t1 - t0;
262
ddcacfa0
IM
263 fflush(stdout);
264
265 fprintf(stderr, "\n");
44db76c8
IM
266 fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
267
268 for (i = 1; i < argc; i++)
269 fprintf(stderr, " %s", argv[i]);
270
271 fprintf(stderr, "\':\n");
ddcacfa0
IM
272 fprintf(stderr, "\n");
273
2996f5dd
IM
274 for (counter = 0; counter < nr_counters; counter++)
275 read_counter(counter);
276
c04f5e5d
IM
277 for (counter = 0; counter < nr_counters; counter++)
278 print_counter(counter);
ddcacfa0 279
ddcacfa0 280
ddcacfa0
IM
281 fprintf(stderr, "\n");
282 fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
283 (double)(t1-t0)/1e6);
284 fprintf(stderr, "\n");
285
286 return 0;
287}
288
5242519b 289static void skip_signal(int signo)
ddcacfa0 290{
5242519b
IM
291}
292
293static const char * const stat_usage[] = {
294 "perf stat [<options>] <command>",
295 NULL
296};
297
298static char events_help_msg[EVENTS_HELP_MAX];
299
300static const struct option options[] = {
301 OPT_CALLBACK('e', "event", NULL, "event",
302 events_help_msg, parse_events),
303 OPT_INTEGER('c', "count", &default_interval,
304 "event period to sample"),
305 OPT_BOOLEAN('i', "inherit", &inherit,
306 "child tasks inherit counters"),
307 OPT_INTEGER('p', "pid", &target_pid,
308 "stat events on existing pid"),
309 OPT_BOOLEAN('a', "all-cpus", &system_wide,
310 "system-wide collection from all CPUs"),
311 OPT_BOOLEAN('l', "scale", &scale,
312 "scale/normalize counters"),
313 OPT_END()
314};
315
316int cmd_stat(int argc, const char **argv, const char *prefix)
317{
318 int counter;
319
320 page_size = sysconf(_SC_PAGE_SIZE);
321
322 create_events_help(events_help_msg);
323 memcpy(event_id, default_event_id, sizeof(default_event_id));
324
325 argc = parse_options(argc, argv, options, stat_usage, 0);
326 if (!argc)
327 usage_with_options(stat_usage, options);
ddcacfa0
IM
328
329 if (!nr_counters) {
330 nr_counters = 8;
331 }
332
333 for (counter = 0; counter < nr_counters; counter++) {
334 if (event_count[counter])
335 continue;
336
337 event_count[counter] = default_interval;
338 }
ddcacfa0
IM
339 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
340 assert(nr_cpus <= MAX_NR_CPUS);
341 assert(nr_cpus >= 0);
342
58d7e993
IM
343 /*
344 * We dont want to block the signals - that would cause
345 * child tasks to inherit that and Ctrl-C would not work.
346 * What we want is for Ctrl-C to work in the exec()-ed
347 * task, but being ignored by perf stat itself:
348 */
349 signal(SIGINT, skip_signal);
350 signal(SIGALRM, skip_signal);
351 signal(SIGABRT, skip_signal);
352
ddcacfa0
IM
353 return do_perfstat(argc, argv);
354}