blob: ab2f682fd44c6cbc402b4a1ab8cbfd0fa9ce0f2e [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Browne23da032012-02-06 18:37:16 -05005 * Copyright (c) 2012 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Len Brown103a8fe2010-10-22 23:53:03 -040023#include <stdio.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include <sys/stat.h>
28#include <sys/resource.h>
29#include <fcntl.h>
30#include <signal.h>
31#include <sys/time.h>
32#include <stdlib.h>
33#include <dirent.h>
34#include <string.h>
35#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040036#include <sched.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037
38#define MSR_TSC 0x10
39#define MSR_NEHALEM_PLATFORM_INFO 0xCE
40#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD
41#define MSR_APERF 0xE8
42#define MSR_MPERF 0xE7
43#define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */
44#define MSR_PKG_C3_RESIDENCY 0x3F8
45#define MSR_PKG_C6_RESIDENCY 0x3F9
46#define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */
47#define MSR_CORE_C3_RESIDENCY 0x3FC
48#define MSR_CORE_C6_RESIDENCY 0x3FD
49#define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */
50
51char *proc_stat = "/proc/stat";
52unsigned int interval_sec = 5; /* set with -i interval_sec */
53unsigned int verbose; /* set with -v */
Len Browne23da032012-02-06 18:37:16 -050054unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040055unsigned int skip_c0;
56unsigned int skip_c1;
57unsigned int do_nhm_cstates;
58unsigned int do_snb_cstates;
59unsigned int has_aperf;
60unsigned int units = 1000000000; /* Ghz etc */
61unsigned int genuine_intel;
62unsigned int has_invariant_tsc;
63unsigned int do_nehalem_platform_info;
64unsigned int do_nehalem_turbo_ratio_limit;
65unsigned int extra_msr_offset;
66double bclk;
67unsigned int show_pkg;
68unsigned int show_core;
69unsigned int show_cpu;
70
71int aperf_mperf_unstable;
72int backwards_count;
73char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040074
75int num_cpus;
Len Brown88c32812012-03-29 21:44:40 -040076cpu_set_t *cpu_mask;
77size_t cpu_mask_size;
Len Brown103a8fe2010-10-22 23:53:03 -040078
Len Browna829eb42011-02-10 23:36:34 -050079struct counters {
Len Brown103a8fe2010-10-22 23:53:03 -040080 unsigned long long tsc; /* per thread */
81 unsigned long long aperf; /* per thread */
82 unsigned long long mperf; /* per thread */
83 unsigned long long c1; /* per thread (calculated) */
84 unsigned long long c3; /* per core */
85 unsigned long long c6; /* per core */
86 unsigned long long c7; /* per core */
87 unsigned long long pc2; /* per package */
88 unsigned long long pc3; /* per package */
89 unsigned long long pc6; /* per package */
90 unsigned long long pc7; /* per package */
91 unsigned long long extra_msr; /* per thread */
92 int pkg;
93 int core;
94 int cpu;
Len Browna829eb42011-02-10 23:36:34 -050095 struct counters *next;
96};
Len Brown103a8fe2010-10-22 23:53:03 -040097
Len Browna829eb42011-02-10 23:36:34 -050098struct counters *cnt_even;
99struct counters *cnt_odd;
100struct counters *cnt_delta;
101struct counters *cnt_average;
Len Brown103a8fe2010-10-22 23:53:03 -0400102struct timeval tv_even;
103struct timeval tv_odd;
104struct timeval tv_delta;
105
Len Brown88c32812012-03-29 21:44:40 -0400106/*
107 * cpu_mask_init(ncpus)
108 *
109 * allocate and clear cpu_mask
110 * set cpu_mask_size
111 */
112void cpu_mask_init(int ncpus)
113{
114 cpu_mask = CPU_ALLOC(ncpus);
115 if (cpu_mask == NULL) {
116 perror("CPU_ALLOC");
117 exit(3);
118 }
119 cpu_mask_size = CPU_ALLOC_SIZE(ncpus);
120 CPU_ZERO_S(cpu_mask_size, cpu_mask);
121}
122
123void cpu_mask_uninit()
124{
125 CPU_FREE(cpu_mask);
126 cpu_mask = NULL;
127 cpu_mask_size = 0;
128}
129
130int cpu_migrate(int cpu)
131{
132 CPU_ZERO_S(cpu_mask_size, cpu_mask);
133 CPU_SET_S(cpu, cpu_mask_size, cpu_mask);
134 if (sched_setaffinity(0, cpu_mask_size, cpu_mask) == -1)
135 return -1;
136 else
137 return 0;
138}
139
Len Brown15aaa342012-03-29 22:19:58 -0400140int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400141{
142 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400143 char pathname[32];
144 int fd;
145
146 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
147 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400148 if (fd < 0)
149 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400150
Len Brown15aaa342012-03-29 22:19:58 -0400151 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400152 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400153
154 if (retval != sizeof *msr)
155 return -1;
156
157 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400158}
159
Len Browna829eb42011-02-10 23:36:34 -0500160void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400161{
162 if (show_pkg)
Len Brownd30c4b72011-07-31 18:19:33 -0400163 fprintf(stderr, "pk");
Len Browne23da032012-02-06 18:37:16 -0500164 if (show_pkg)
165 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400166 if (show_core)
Len Browne23da032012-02-06 18:37:16 -0500167 fprintf(stderr, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400168 if (show_cpu)
169 fprintf(stderr, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500170 if (show_pkg || show_core || show_cpu)
171 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400172 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500173 fprintf(stderr, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400174 if (has_aperf)
Len Browne23da032012-02-06 18:37:16 -0500175 fprintf(stderr, " GHz");
Len Brown103a8fe2010-10-22 23:53:03 -0400176 fprintf(stderr, " TSC");
177 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400178 fprintf(stderr, " %%c1");
Len Brown103a8fe2010-10-22 23:53:03 -0400179 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400180 fprintf(stderr, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400181 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400182 fprintf(stderr, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400183 if (do_snb_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400184 fprintf(stderr, " %%c7");
Len Brown103a8fe2010-10-22 23:53:03 -0400185 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500186 fprintf(stderr, " %%pc2");
Len Brown103a8fe2010-10-22 23:53:03 -0400187 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500188 fprintf(stderr, " %%pc3");
Len Brown103a8fe2010-10-22 23:53:03 -0400189 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500190 fprintf(stderr, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400191 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500192 fprintf(stderr, " %%pc7");
Len Brown103a8fe2010-10-22 23:53:03 -0400193 if (extra_msr_offset)
Len Brownd30c4b72011-07-31 18:19:33 -0400194 fprintf(stderr, " MSR 0x%x ", extra_msr_offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400195
196 putc('\n', stderr);
197}
198
Len Browna829eb42011-02-10 23:36:34 -0500199void dump_cnt(struct counters *cnt)
Len Brown103a8fe2010-10-22 23:53:03 -0400200{
Len Brownaeae1e92011-07-03 21:41:33 -0400201 if (!cnt)
202 return;
203 if (cnt->pkg) fprintf(stderr, "package: %d ", cnt->pkg);
204 if (cnt->core) fprintf(stderr, "core:: %d ", cnt->core);
205 if (cnt->cpu) fprintf(stderr, "CPU: %d ", cnt->cpu);
206 if (cnt->tsc) fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
207 if (cnt->c3) fprintf(stderr, "c3: %016llX\n", cnt->c3);
208 if (cnt->c6) fprintf(stderr, "c6: %016llX\n", cnt->c6);
209 if (cnt->c7) fprintf(stderr, "c7: %016llX\n", cnt->c7);
210 if (cnt->aperf) fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
211 if (cnt->pc2) fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
212 if (cnt->pc3) fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
213 if (cnt->pc6) fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
214 if (cnt->pc7) fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
215 if (cnt->extra_msr) fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400216}
217
Len Browna829eb42011-02-10 23:36:34 -0500218void dump_list(struct counters *cnt)
Len Brown103a8fe2010-10-22 23:53:03 -0400219{
Len Browna829eb42011-02-10 23:36:34 -0500220 printf("dump_list 0x%p\n", cnt);
Len Brown103a8fe2010-10-22 23:53:03 -0400221
Len Browna829eb42011-02-10 23:36:34 -0500222 for (; cnt; cnt = cnt->next)
223 dump_cnt(cnt);
Len Brown103a8fe2010-10-22 23:53:03 -0400224}
225
Len Browne23da032012-02-06 18:37:16 -0500226/*
227 * column formatting convention & formats
228 * package: "pk" 2 columns %2d
229 * core: "cor" 3 columns %3d
230 * CPU: "CPU" 3 columns %3d
231 * GHz: "GHz" 3 columns %3.2
232 * TSC: "TSC" 3 columns %3.2
233 * percentage " %pc3" %6.2
234 */
Len Browna829eb42011-02-10 23:36:34 -0500235void print_cnt(struct counters *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400236{
237 double interval_float;
238
239 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
240
241 /* topology columns, print blanks on 1st (average) line */
Len Browna829eb42011-02-10 23:36:34 -0500242 if (p == cnt_average) {
Len Brown103a8fe2010-10-22 23:53:03 -0400243 if (show_pkg)
Len Browne23da032012-02-06 18:37:16 -0500244 fprintf(stderr, " ");
245 if (show_pkg && show_core)
Len Brownd30c4b72011-07-31 18:19:33 -0400246 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400247 if (show_core)
Len Browne23da032012-02-06 18:37:16 -0500248 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400249 if (show_cpu)
Len Browne23da032012-02-06 18:37:16 -0500250 fprintf(stderr, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400251 } else {
252 if (show_pkg)
Len Browne23da032012-02-06 18:37:16 -0500253 fprintf(stderr, "%2d", p->pkg);
254 if (show_pkg && show_core)
255 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400256 if (show_core)
Len Browne23da032012-02-06 18:37:16 -0500257 fprintf(stderr, "%3d", p->core);
Len Brown103a8fe2010-10-22 23:53:03 -0400258 if (show_cpu)
Len Browne23da032012-02-06 18:37:16 -0500259 fprintf(stderr, " %3d", p->cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400260 }
261
262 /* %c0 */
263 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500264 if (show_pkg || show_core || show_cpu)
265 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400266 if (!skip_c0)
Len Browne23da032012-02-06 18:37:16 -0500267 fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400268 else
Len Browne23da032012-02-06 18:37:16 -0500269 fprintf(stderr, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400270 }
271
272 /* GHz */
273 if (has_aperf) {
274 if (!aperf_mperf_unstable) {
Len Browne23da032012-02-06 18:37:16 -0500275 fprintf(stderr, " %3.2f",
Len Brown103a8fe2010-10-22 23:53:03 -0400276 1.0 * p->tsc / units * p->aperf /
277 p->mperf / interval_float);
278 } else {
279 if (p->aperf > p->tsc || p->mperf > p->tsc) {
Len Browne23da032012-02-06 18:37:16 -0500280 fprintf(stderr, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400281 } else {
Len Browne23da032012-02-06 18:37:16 -0500282 fprintf(stderr, "%3.1f*",
Len Brown103a8fe2010-10-22 23:53:03 -0400283 1.0 * p->tsc /
284 units * p->aperf /
285 p->mperf / interval_float);
286 }
287 }
288 }
289
290 /* TSC */
291 fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);
292
293 if (do_nhm_cstates) {
294 if (!skip_c1)
Len Browne23da032012-02-06 18:37:16 -0500295 fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400296 else
Len Brownd30c4b72011-07-31 18:19:33 -0400297 fprintf(stderr, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400298 }
299 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400300 fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400301 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400302 fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400303 if (do_snb_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400304 fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400305 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500306 fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400307 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500308 fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400309 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500310 fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400311 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500312 fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400313 if (extra_msr_offset)
314 fprintf(stderr, " 0x%016llx", p->extra_msr);
315 putc('\n', stderr);
316}
317
Len Browna829eb42011-02-10 23:36:34 -0500318void print_counters(struct counters *counters)
Len Brown103a8fe2010-10-22 23:53:03 -0400319{
Len Browna829eb42011-02-10 23:36:34 -0500320 struct counters *cnt;
Len Browne23da032012-02-06 18:37:16 -0500321 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400322
Len Browne23da032012-02-06 18:37:16 -0500323
324 if (!printed || !summary_only)
325 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400326
327 if (num_cpus > 1)
Len Browna829eb42011-02-10 23:36:34 -0500328 print_cnt(cnt_average);
Len Brown103a8fe2010-10-22 23:53:03 -0400329
Len Browne23da032012-02-06 18:37:16 -0500330 printed = 1;
331
332 if (summary_only)
333 return;
334
Len Browna829eb42011-02-10 23:36:34 -0500335 for (cnt = counters; cnt != NULL; cnt = cnt->next)
336 print_cnt(cnt);
Len Brown103a8fe2010-10-22 23:53:03 -0400337
338}
339
340#define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
341
Len Browna829eb42011-02-10 23:36:34 -0500342int compute_delta(struct counters *after,
343 struct counters *before, struct counters *delta)
Len Brown103a8fe2010-10-22 23:53:03 -0400344{
345 int errors = 0;
346 int perf_err = 0;
347
348 skip_c0 = skip_c1 = 0;
349
350 for ( ; after && before && delta;
351 after = after->next, before = before->next, delta = delta->next) {
352 if (before->cpu != after->cpu) {
353 printf("cpu configuration changed: %d != %d\n",
354 before->cpu, after->cpu);
355 return -1;
356 }
357
358 if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
359 fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
360 before->cpu, before->tsc, after->tsc);
361 errors++;
362 }
363 /* check for TSC < 1 Mcycles over interval */
364 if (delta->tsc < (1000 * 1000)) {
365 fprintf(stderr, "Insanely slow TSC rate,"
366 " TSC stops in idle?\n");
367 fprintf(stderr, "You can disable all c-states"
368 " by booting with \"idle=poll\"\n");
369 fprintf(stderr, "or just the deep ones with"
370 " \"processor.max_cstate=1\"\n");
371 exit(-3);
372 }
373 if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
374 fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
375 before->cpu, before->c3, after->c3);
376 errors++;
377 }
378 if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
379 fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
380 before->cpu, before->c6, after->c6);
381 errors++;
382 }
383 if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
384 fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
385 before->cpu, before->c7, after->c7);
386 errors++;
387 }
388 if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
389 fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
390 before->cpu, before->pc2, after->pc2);
391 errors++;
392 }
393 if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
394 fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
395 before->cpu, before->pc3, after->pc3);
396 errors++;
397 }
398 if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
399 fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
400 before->cpu, before->pc6, after->pc6);
401 errors++;
402 }
403 if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
404 fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
405 before->cpu, before->pc7, after->pc7);
406 errors++;
407 }
408
409 perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
410 if (perf_err) {
411 fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
412 before->cpu, before->aperf, after->aperf);
413 }
414 perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
415 if (perf_err) {
416 fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
417 before->cpu, before->mperf, after->mperf);
418 }
419 if (perf_err) {
420 if (!aperf_mperf_unstable) {
421 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
422 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
423 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
424
425 aperf_mperf_unstable = 1;
426 }
427 /*
428 * mperf delta is likely a huge "positive" number
429 * can not use it for calculating c0 time
430 */
431 skip_c0 = 1;
432 skip_c1 = 1;
433 }
434
435 /*
436 * As mperf and tsc collection are not atomic,
437 * it is possible for mperf's non-halted cycles
438 * to exceed TSC's all cycles: show c1 = 0% in that case.
439 */
440 if (delta->mperf > delta->tsc)
441 delta->c1 = 0;
442 else /* normal case, derive c1 */
443 delta->c1 = delta->tsc - delta->mperf
444 - delta->c3 - delta->c6 - delta->c7;
445
446 if (delta->mperf == 0)
447 delta->mperf = 1; /* divide by 0 protection */
448
449 /*
450 * for "extra msr", just copy the latest w/o subtracting
451 */
452 delta->extra_msr = after->extra_msr;
453 if (errors) {
454 fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
Len Browna829eb42011-02-10 23:36:34 -0500455 dump_cnt(before);
Len Brown103a8fe2010-10-22 23:53:03 -0400456 fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
Len Browna829eb42011-02-10 23:36:34 -0500457 dump_cnt(after);
Len Brown103a8fe2010-10-22 23:53:03 -0400458 errors = 0;
459 }
460 }
461 return 0;
462}
463
Len Browna829eb42011-02-10 23:36:34 -0500464void compute_average(struct counters *delta, struct counters *avg)
Len Brown103a8fe2010-10-22 23:53:03 -0400465{
Len Browna829eb42011-02-10 23:36:34 -0500466 struct counters *sum;
Len Brown103a8fe2010-10-22 23:53:03 -0400467
Len Browna829eb42011-02-10 23:36:34 -0500468 sum = calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400469 if (sum == NULL) {
470 perror("calloc sum");
471 exit(1);
472 }
473
474 for (; delta; delta = delta->next) {
475 sum->tsc += delta->tsc;
476 sum->c1 += delta->c1;
477 sum->c3 += delta->c3;
478 sum->c6 += delta->c6;
479 sum->c7 += delta->c7;
480 sum->aperf += delta->aperf;
481 sum->mperf += delta->mperf;
482 sum->pc2 += delta->pc2;
483 sum->pc3 += delta->pc3;
484 sum->pc6 += delta->pc6;
485 sum->pc7 += delta->pc7;
486 }
487 avg->tsc = sum->tsc/num_cpus;
488 avg->c1 = sum->c1/num_cpus;
489 avg->c3 = sum->c3/num_cpus;
490 avg->c6 = sum->c6/num_cpus;
491 avg->c7 = sum->c7/num_cpus;
492 avg->aperf = sum->aperf/num_cpus;
493 avg->mperf = sum->mperf/num_cpus;
494 avg->pc2 = sum->pc2/num_cpus;
495 avg->pc3 = sum->pc3/num_cpus;
496 avg->pc6 = sum->pc6/num_cpus;
497 avg->pc7 = sum->pc7/num_cpus;
498
499 free(sum);
500}
501
Len Brown15aaa342012-03-29 22:19:58 -0400502int get_counters(struct counters *cnt)
Len Brown103a8fe2010-10-22 23:53:03 -0400503{
Len Browna829eb42011-02-10 23:36:34 -0500504 for ( ; cnt; cnt = cnt->next) {
Len Brown15aaa342012-03-29 22:19:58 -0400505
506 if (cpu_migrate(cnt->cpu))
507 return -1;
508
509 if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc))
510 return -1;
511
512 if (has_aperf) {
513 if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf))
514 return -1;
515 if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf))
516 return -1;
Len Brown88c32812012-03-29 21:44:40 -0400517 }
518
Len Brown15aaa342012-03-29 22:19:58 -0400519 if (do_nhm_cstates) {
520 if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3))
521 return -1;
522 if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6))
523 return -1;
524 }
525
Len Brown103a8fe2010-10-22 23:53:03 -0400526 if (do_snb_cstates)
Len Brown15aaa342012-03-29 22:19:58 -0400527 if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7))
528 return -1;
529
530 if (do_nhm_cstates) {
531 if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3))
532 return -1;
533 if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6))
534 return -1;
535 }
536 if (do_snb_cstates) {
537 if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2))
538 return -1;
539 if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7))
540 return -1;
541 }
Len Brown103a8fe2010-10-22 23:53:03 -0400542 if (extra_msr_offset)
Len Brown15aaa342012-03-29 22:19:58 -0400543 if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr))
544 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400545 }
Len Brown15aaa342012-03-29 22:19:58 -0400546 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400547}
548
Len Browna829eb42011-02-10 23:36:34 -0500549void print_nehalem_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400550{
551 unsigned long long msr;
552 unsigned int ratio;
553
554 if (!do_nehalem_platform_info)
555 return;
556
Len Brown15aaa342012-03-29 22:19:58 -0400557 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400558
559 ratio = (msr >> 40) & 0xFF;
560 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
561 ratio, bclk, ratio * bclk);
562
563 ratio = (msr >> 8) & 0xFF;
564 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
565 ratio, bclk, ratio * bclk);
566
567 if (verbose > 1)
568 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
569
570 if (!do_nehalem_turbo_ratio_limit)
571 return;
572
Len Brown15aaa342012-03-29 22:19:58 -0400573 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400574
575 ratio = (msr >> 24) & 0xFF;
576 if (ratio)
577 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
578 ratio, bclk, ratio * bclk);
579
580 ratio = (msr >> 16) & 0xFF;
581 if (ratio)
582 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
583 ratio, bclk, ratio * bclk);
584
585 ratio = (msr >> 8) & 0xFF;
586 if (ratio)
587 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
588 ratio, bclk, ratio * bclk);
589
590 ratio = (msr >> 0) & 0xFF;
591 if (ratio)
592 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
593 ratio, bclk, ratio * bclk);
594
595}
596
Len Browna829eb42011-02-10 23:36:34 -0500597void free_counter_list(struct counters *list)
Len Brown103a8fe2010-10-22 23:53:03 -0400598{
Len Browna829eb42011-02-10 23:36:34 -0500599 struct counters *p;
Len Brown103a8fe2010-10-22 23:53:03 -0400600
601 for (p = list; p; ) {
Len Browna829eb42011-02-10 23:36:34 -0500602 struct counters *free_me;
Len Brown103a8fe2010-10-22 23:53:03 -0400603
604 free_me = p;
605 p = p->next;
606 free(free_me);
607 }
Len Brown103a8fe2010-10-22 23:53:03 -0400608}
609
610void free_all_counters(void)
611{
Len Browna829eb42011-02-10 23:36:34 -0500612 free_counter_list(cnt_even);
613 cnt_even = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400614
Len Browna829eb42011-02-10 23:36:34 -0500615 free_counter_list(cnt_odd);
616 cnt_odd = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400617
Len Browna829eb42011-02-10 23:36:34 -0500618 free_counter_list(cnt_delta);
619 cnt_delta = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400620
Len Browna829eb42011-02-10 23:36:34 -0500621 free_counter_list(cnt_average);
622 cnt_average = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400623}
624
Len Browna829eb42011-02-10 23:36:34 -0500625void insert_counters(struct counters **list,
626 struct counters *new)
Len Brown103a8fe2010-10-22 23:53:03 -0400627{
Len Browna829eb42011-02-10 23:36:34 -0500628 struct counters *prev;
Len Brown103a8fe2010-10-22 23:53:03 -0400629
630 /*
631 * list was empty
632 */
633 if (*list == NULL) {
634 new->next = *list;
635 *list = new;
636 return;
637 }
638
Len Browne23da032012-02-06 18:37:16 -0500639 if (!summary_only)
640 show_cpu = 1; /* there is more than one CPU */
Len Brown103a8fe2010-10-22 23:53:03 -0400641
642 /*
643 * insert on front of list.
644 * It is sorted by ascending package#, core#, cpu#
645 */
646 if (((*list)->pkg > new->pkg) ||
647 (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
648 (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
649 new->next = *list;
650 *list = new;
651 return;
652 }
653
654 prev = *list;
655
656 while (prev->next && (prev->next->pkg < new->pkg)) {
657 prev = prev->next;
Len Browne23da032012-02-06 18:37:16 -0500658 if (!summary_only)
659 show_pkg = 1; /* there is more than 1 package */
Len Brown103a8fe2010-10-22 23:53:03 -0400660 }
661
662 while (prev->next && (prev->next->pkg == new->pkg)
663 && (prev->next->core < new->core)) {
664 prev = prev->next;
Len Browne23da032012-02-06 18:37:16 -0500665 if (!summary_only)
666 show_core = 1; /* there is more than 1 core */
Len Brown103a8fe2010-10-22 23:53:03 -0400667 }
668
669 while (prev->next && (prev->next->pkg == new->pkg)
670 && (prev->next->core == new->core)
671 && (prev->next->cpu < new->cpu)) {
672 prev = prev->next;
673 }
674
675 /*
676 * insert after "prev"
677 */
678 new->next = prev->next;
679 prev->next = new;
Len Brown103a8fe2010-10-22 23:53:03 -0400680}
681
Len Browna829eb42011-02-10 23:36:34 -0500682void alloc_new_counters(int pkg, int core, int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400683{
Len Browna829eb42011-02-10 23:36:34 -0500684 struct counters *new;
Len Brown103a8fe2010-10-22 23:53:03 -0400685
686 if (verbose > 1)
687 printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
688
Len Browna829eb42011-02-10 23:36:34 -0500689 new = (struct counters *)calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400690 if (new == NULL) {
691 perror("calloc");
692 exit(1);
693 }
694 new->pkg = pkg;
695 new->core = core;
696 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500697 insert_counters(&cnt_odd, new);
Len Brown103a8fe2010-10-22 23:53:03 -0400698
Len Browna829eb42011-02-10 23:36:34 -0500699 new = (struct counters *)calloc(1,
700 sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400701 if (new == NULL) {
702 perror("calloc");
703 exit(1);
704 }
705 new->pkg = pkg;
706 new->core = core;
707 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500708 insert_counters(&cnt_even, new);
Len Brown103a8fe2010-10-22 23:53:03 -0400709
Len Browna829eb42011-02-10 23:36:34 -0500710 new = (struct counters *)calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400711 if (new == NULL) {
712 perror("calloc");
713 exit(1);
714 }
715 new->pkg = pkg;
716 new->core = core;
717 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500718 insert_counters(&cnt_delta, new);
Len Brown103a8fe2010-10-22 23:53:03 -0400719
Len Browna829eb42011-02-10 23:36:34 -0500720 new = (struct counters *)calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400721 if (new == NULL) {
722 perror("calloc");
723 exit(1);
724 }
725 new->pkg = pkg;
726 new->core = core;
727 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500728 cnt_average = new;
Len Brown103a8fe2010-10-22 23:53:03 -0400729}
730
731int get_physical_package_id(int cpu)
732{
733 char path[64];
734 FILE *filep;
735 int pkg;
736
737 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
738 filep = fopen(path, "r");
739 if (filep == NULL) {
740 perror(path);
741 exit(1);
742 }
743 fscanf(filep, "%d", &pkg);
744 fclose(filep);
745 return pkg;
746}
747
748int get_core_id(int cpu)
749{
750 char path[64];
751 FILE *filep;
752 int core;
753
754 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
755 filep = fopen(path, "r");
756 if (filep == NULL) {
757 perror(path);
758 exit(1);
759 }
760 fscanf(filep, "%d", &core);
761 fclose(filep);
762 return core;
763}
764
765/*
Len Brown15aaa342012-03-29 22:19:58 -0400766 * run func(pkg, core, cpu) on every cpu in /proc/stat
Len Brown103a8fe2010-10-22 23:53:03 -0400767 */
768
769int for_all_cpus(void (func)(int, int, int))
770{
771 FILE *fp;
772 int cpu_count;
773 int retval;
774
775 fp = fopen(proc_stat, "r");
776 if (fp == NULL) {
777 perror(proc_stat);
778 exit(1);
779 }
780
781 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
782 if (retval != 0) {
783 perror("/proc/stat format");
784 exit(1);
785 }
786
787 for (cpu_count = 0; ; cpu_count++) {
788 int cpu;
789
790 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
791 if (retval != 1)
792 break;
793
794 func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
795 }
796 fclose(fp);
797 return cpu_count;
798}
799
800void re_initialize(void)
801{
Len Brown103a8fe2010-10-22 23:53:03 -0400802 free_all_counters();
Len Browna829eb42011-02-10 23:36:34 -0500803 num_cpus = for_all_cpus(alloc_new_counters);
Len Brown88c32812012-03-29 21:44:40 -0400804 cpu_mask_uninit();
805 cpu_mask_init(num_cpus);
Len Brown15aaa342012-03-29 22:19:58 -0400806 printf("turbostat: re-initialized with num_cpus %d\n", num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -0400807}
808
809void dummy(int pkg, int core, int cpu) { return; }
810/*
811 * check to see if a cpu came on-line
812 */
Len Brown15aaa342012-03-29 22:19:58 -0400813int verify_num_cpus(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400814{
815 int new_num_cpus;
816
817 new_num_cpus = for_all_cpus(dummy);
818
819 if (new_num_cpus != num_cpus) {
820 if (verbose)
821 printf("num_cpus was %d, is now %d\n",
822 num_cpus, new_num_cpus);
Len Brown15aaa342012-03-29 22:19:58 -0400823 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400824 }
Len Brown15aaa342012-03-29 22:19:58 -0400825 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400826}
827
828void turbostat_loop()
829{
830restart:
Len Browna829eb42011-02-10 23:36:34 -0500831 get_counters(cnt_even);
Len Brown103a8fe2010-10-22 23:53:03 -0400832 gettimeofday(&tv_even, (struct timezone *)NULL);
833
834 while (1) {
Len Brown15aaa342012-03-29 22:19:58 -0400835 if (verify_num_cpus()) {
Len Brown103a8fe2010-10-22 23:53:03 -0400836 re_initialize();
837 goto restart;
838 }
839 sleep(interval_sec);
Len Brown15aaa342012-03-29 22:19:58 -0400840 if (get_counters(cnt_odd)) {
841 re_initialize();
842 goto restart;
843 }
Len Brown103a8fe2010-10-22 23:53:03 -0400844 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Browna829eb42011-02-10 23:36:34 -0500845 compute_delta(cnt_odd, cnt_even, cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -0400846 timersub(&tv_odd, &tv_even, &tv_delta);
Len Browna829eb42011-02-10 23:36:34 -0500847 compute_average(cnt_delta, cnt_average);
848 print_counters(cnt_delta);
Len Brown15aaa342012-03-29 22:19:58 -0400849 sleep(interval_sec);
850 if (get_counters(cnt_even)) {
Len Brown103a8fe2010-10-22 23:53:03 -0400851 re_initialize();
852 goto restart;
853 }
Len Brown103a8fe2010-10-22 23:53:03 -0400854 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Browna829eb42011-02-10 23:36:34 -0500855 compute_delta(cnt_even, cnt_odd, cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -0400856 timersub(&tv_even, &tv_odd, &tv_delta);
Len Browna829eb42011-02-10 23:36:34 -0500857 compute_average(cnt_delta, cnt_average);
858 print_counters(cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -0400859 }
860}
861
862void check_dev_msr()
863{
864 struct stat sb;
865
866 if (stat("/dev/cpu/0/msr", &sb)) {
867 fprintf(stderr, "no /dev/cpu/0/msr\n");
868 fprintf(stderr, "Try \"# modprobe msr\"\n");
869 exit(-5);
870 }
871}
872
873void check_super_user()
874{
875 if (getuid() != 0) {
876 fprintf(stderr, "must be root\n");
877 exit(-6);
878 }
879}
880
881int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
882{
883 if (!genuine_intel)
884 return 0;
885
886 if (family != 6)
887 return 0;
888
889 switch (model) {
890 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
891 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
892 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
893 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
894 case 0x2C: /* Westmere EP - Gulftown */
895 case 0x2A: /* SNB */
896 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -0500897 case 0x3A: /* IVB */
898 case 0x3D: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -0400899 return 1;
900 case 0x2E: /* Nehalem-EX Xeon - Beckton */
901 case 0x2F: /* Westmere-EX Xeon - Eagleton */
902 default:
903 return 0;
904 }
905}
906
907int is_snb(unsigned int family, unsigned int model)
908{
909 if (!genuine_intel)
910 return 0;
911
912 switch (model) {
913 case 0x2A:
914 case 0x2D:
915 return 1;
916 }
917 return 0;
918}
919
920double discover_bclk(unsigned int family, unsigned int model)
921{
922 if (is_snb(family, model))
923 return 100.00;
924 else
925 return 133.33;
926}
927
928void check_cpuid()
929{
930 unsigned int eax, ebx, ecx, edx, max_level;
931 unsigned int fms, family, model, stepping;
932
933 eax = ebx = ecx = edx = 0;
934
935 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
936
937 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
938 genuine_intel = 1;
939
940 if (verbose)
941 fprintf(stderr, "%.4s%.4s%.4s ",
942 (char *)&ebx, (char *)&edx, (char *)&ecx);
943
944 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
945 family = (fms >> 8) & 0xf;
946 model = (fms >> 4) & 0xf;
947 stepping = fms & 0xf;
948 if (family == 6 || family == 0xf)
949 model += ((fms >> 16) & 0xf) << 4;
950
951 if (verbose)
952 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
953 max_level, family, model, stepping, family, model, stepping);
954
955 if (!(edx & (1 << 5))) {
956 fprintf(stderr, "CPUID: no MSR\n");
957 exit(1);
958 }
959
960 /*
961 * check max extended function levels of CPUID.
962 * This is needed to check for invariant TSC.
963 * This check is valid for both Intel and AMD.
964 */
965 ebx = ecx = edx = 0;
966 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
967
968 if (max_level < 0x80000007) {
969 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
970 exit(1);
971 }
972
973 /*
974 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
975 * this check is valid for both Intel and AMD
976 */
977 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +0100978 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -0400979
980 if (!has_invariant_tsc) {
981 fprintf(stderr, "No invariant TSC\n");
982 exit(1);
983 }
984
985 /*
986 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
987 * this check is valid for both Intel and AMD
988 */
989
990 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +0100991 has_aperf = ecx & (1 << 0);
Len Brown103a8fe2010-10-22 23:53:03 -0400992 if (!has_aperf) {
993 fprintf(stderr, "No APERF MSR\n");
994 exit(1);
995 }
996
997 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
998 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
999 do_snb_cstates = is_snb(family, model);
1000 bclk = discover_bclk(family, model);
1001
1002 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1003}
1004
1005
1006void usage()
1007{
1008 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1009 progname);
1010 exit(1);
1011}
1012
1013
1014/*
1015 * in /dev/cpu/ return success for names that are numbers
1016 * ie. filter out ".", "..", "microcode".
1017 */
1018int dir_filter(const struct dirent *dirp)
1019{
1020 if (isdigit(dirp->d_name[0]))
1021 return 1;
1022 else
1023 return 0;
1024}
1025
1026int open_dev_cpu_msr(int dummy1)
1027{
1028 return 0;
1029}
1030
1031void turbostat_init()
1032{
1033 check_cpuid();
1034
1035 check_dev_msr();
1036 check_super_user();
1037
Len Browna829eb42011-02-10 23:36:34 -05001038 num_cpus = for_all_cpus(alloc_new_counters);
Len Brown88c32812012-03-29 21:44:40 -04001039 cpu_mask_init(num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001040
1041 if (verbose)
1042 print_nehalem_info();
1043}
1044
1045int fork_it(char **argv)
1046{
1047 int retval;
1048 pid_t child_pid;
Len Browna829eb42011-02-10 23:36:34 -05001049 get_counters(cnt_even);
Len Brown103a8fe2010-10-22 23:53:03 -04001050 gettimeofday(&tv_even, (struct timezone *)NULL);
1051
1052 child_pid = fork();
1053 if (!child_pid) {
1054 /* child */
1055 execvp(argv[0], argv);
1056 } else {
1057 int status;
1058
1059 /* parent */
1060 if (child_pid == -1) {
1061 perror("fork");
1062 exit(1);
1063 }
1064
1065 signal(SIGINT, SIG_IGN);
1066 signal(SIGQUIT, SIG_IGN);
1067 if (waitpid(child_pid, &status, 0) == -1) {
1068 perror("wait");
1069 exit(1);
1070 }
1071 }
Len Browna829eb42011-02-10 23:36:34 -05001072 get_counters(cnt_odd);
Len Brown103a8fe2010-10-22 23:53:03 -04001073 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Browna829eb42011-02-10 23:36:34 -05001074 retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -04001075
1076 timersub(&tv_odd, &tv_even, &tv_delta);
Len Browna829eb42011-02-10 23:36:34 -05001077 compute_average(cnt_delta, cnt_average);
Len Brown103a8fe2010-10-22 23:53:03 -04001078 if (!retval)
Len Browna829eb42011-02-10 23:36:34 -05001079 print_counters(cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -04001080
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001081 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04001082
1083 return 0;
1084}
1085
1086void cmdline(int argc, char **argv)
1087{
1088 int opt;
1089
1090 progname = argv[0];
1091
Len Browne23da032012-02-06 18:37:16 -05001092 while ((opt = getopt(argc, argv, "+svi:M:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001093 switch (opt) {
Len Browne23da032012-02-06 18:37:16 -05001094 case 's':
1095 summary_only++;
1096 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001097 case 'v':
1098 verbose++;
1099 break;
1100 case 'i':
1101 interval_sec = atoi(optarg);
1102 break;
1103 case 'M':
1104 sscanf(optarg, "%x", &extra_msr_offset);
1105 if (verbose > 1)
1106 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1107 break;
1108 default:
1109 usage();
1110 }
1111 }
1112}
1113
1114int main(int argc, char **argv)
1115{
1116 cmdline(argc, argv);
1117
1118 if (verbose > 1)
1119 fprintf(stderr, "turbostat Dec 6, 2010"
1120 " - Len Brown <lenb@kernel.org>\n");
1121 if (verbose > 1)
1122 fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");
1123
1124 turbostat_init();
1125
1126 /*
1127 * if any params left, it must be a command to fork
1128 */
1129 if (argc - optind)
1130 return fork_it(argv + optind);
1131 else
1132 turbostat_loop();
1133
1134 return 0;
1135}