blob: 00772661894c58dfa24e22588d8b69889448a89b [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>
Josh Triplett4c6544f2013-08-20 17:20:14 -070037#include <cpuid.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038
39#define MSR_TSC 0x10
40#define MSR_NEHALEM_PLATFORM_INFO 0xCE
41#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD
42#define MSR_APERF 0xE8
43#define MSR_MPERF 0xE7
44#define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */
45#define MSR_PKG_C3_RESIDENCY 0x3F8
46#define MSR_PKG_C6_RESIDENCY 0x3F9
47#define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */
48#define MSR_CORE_C3_RESIDENCY 0x3FC
49#define MSR_CORE_C6_RESIDENCY 0x3FD
50#define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */
51
52char *proc_stat = "/proc/stat";
53unsigned int interval_sec = 5; /* set with -i interval_sec */
54unsigned int verbose; /* set with -v */
Len Browne23da032012-02-06 18:37:16 -050055unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040056unsigned int skip_c0;
57unsigned int skip_c1;
58unsigned int do_nhm_cstates;
59unsigned int do_snb_cstates;
60unsigned int has_aperf;
61unsigned int units = 1000000000; /* Ghz etc */
62unsigned int genuine_intel;
63unsigned int has_invariant_tsc;
64unsigned int do_nehalem_platform_info;
65unsigned int do_nehalem_turbo_ratio_limit;
66unsigned int extra_msr_offset;
67double bclk;
68unsigned int show_pkg;
69unsigned int show_core;
70unsigned int show_cpu;
71
72int aperf_mperf_unstable;
73int backwards_count;
74char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040075
76int num_cpus;
Len Brown88c32812012-03-29 21:44:40 -040077cpu_set_t *cpu_mask;
78size_t cpu_mask_size;
Len Brown103a8fe2010-10-22 23:53:03 -040079
Len Browna829eb42011-02-10 23:36:34 -050080struct counters {
Len Brown103a8fe2010-10-22 23:53:03 -040081 unsigned long long tsc; /* per thread */
82 unsigned long long aperf; /* per thread */
83 unsigned long long mperf; /* per thread */
84 unsigned long long c1; /* per thread (calculated) */
85 unsigned long long c3; /* per core */
86 unsigned long long c6; /* per core */
87 unsigned long long c7; /* per core */
88 unsigned long long pc2; /* per package */
89 unsigned long long pc3; /* per package */
90 unsigned long long pc6; /* per package */
91 unsigned long long pc7; /* per package */
92 unsigned long long extra_msr; /* per thread */
93 int pkg;
94 int core;
95 int cpu;
Len Browna829eb42011-02-10 23:36:34 -050096 struct counters *next;
97};
Len Brown103a8fe2010-10-22 23:53:03 -040098
Len Browna829eb42011-02-10 23:36:34 -050099struct counters *cnt_even;
100struct counters *cnt_odd;
101struct counters *cnt_delta;
102struct counters *cnt_average;
Len Brown103a8fe2010-10-22 23:53:03 -0400103struct timeval tv_even;
104struct timeval tv_odd;
105struct timeval tv_delta;
106
Len Brown88c32812012-03-29 21:44:40 -0400107/*
108 * cpu_mask_init(ncpus)
109 *
110 * allocate and clear cpu_mask
111 * set cpu_mask_size
112 */
113void cpu_mask_init(int ncpus)
114{
115 cpu_mask = CPU_ALLOC(ncpus);
116 if (cpu_mask == NULL) {
117 perror("CPU_ALLOC");
118 exit(3);
119 }
120 cpu_mask_size = CPU_ALLOC_SIZE(ncpus);
121 CPU_ZERO_S(cpu_mask_size, cpu_mask);
122}
123
124void cpu_mask_uninit()
125{
126 CPU_FREE(cpu_mask);
127 cpu_mask = NULL;
128 cpu_mask_size = 0;
129}
130
131int cpu_migrate(int cpu)
132{
133 CPU_ZERO_S(cpu_mask_size, cpu_mask);
134 CPU_SET_S(cpu, cpu_mask_size, cpu_mask);
135 if (sched_setaffinity(0, cpu_mask_size, cpu_mask) == -1)
136 return -1;
137 else
138 return 0;
139}
140
Len Brown15aaa342012-03-29 22:19:58 -0400141int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400142{
143 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400144 char pathname[32];
145 int fd;
146
147 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
148 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400149 if (fd < 0)
150 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400151
Len Brown15aaa342012-03-29 22:19:58 -0400152 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400153 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400154
155 if (retval != sizeof *msr)
156 return -1;
157
158 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400159}
160
Len Browna829eb42011-02-10 23:36:34 -0500161void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400162{
163 if (show_pkg)
Len Brownd30c4b72011-07-31 18:19:33 -0400164 fprintf(stderr, "pk");
Len Browne23da032012-02-06 18:37:16 -0500165 if (show_pkg)
166 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400167 if (show_core)
Len Browne23da032012-02-06 18:37:16 -0500168 fprintf(stderr, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400169 if (show_cpu)
170 fprintf(stderr, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500171 if (show_pkg || show_core || show_cpu)
172 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400173 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500174 fprintf(stderr, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400175 if (has_aperf)
Len Browne23da032012-02-06 18:37:16 -0500176 fprintf(stderr, " GHz");
Len Brown103a8fe2010-10-22 23:53:03 -0400177 fprintf(stderr, " TSC");
178 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400179 fprintf(stderr, " %%c1");
Len Brown103a8fe2010-10-22 23:53:03 -0400180 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400181 fprintf(stderr, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400182 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400183 fprintf(stderr, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400184 if (do_snb_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400185 fprintf(stderr, " %%c7");
Len Brown103a8fe2010-10-22 23:53:03 -0400186 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500187 fprintf(stderr, " %%pc2");
Len Brown103a8fe2010-10-22 23:53:03 -0400188 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500189 fprintf(stderr, " %%pc3");
Len Brown103a8fe2010-10-22 23:53:03 -0400190 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500191 fprintf(stderr, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400192 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500193 fprintf(stderr, " %%pc7");
Len Brown103a8fe2010-10-22 23:53:03 -0400194 if (extra_msr_offset)
Len Brownd30c4b72011-07-31 18:19:33 -0400195 fprintf(stderr, " MSR 0x%x ", extra_msr_offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400196
197 putc('\n', stderr);
198}
199
Len Browna829eb42011-02-10 23:36:34 -0500200void dump_cnt(struct counters *cnt)
Len Brown103a8fe2010-10-22 23:53:03 -0400201{
Len Brownaeae1e92011-07-03 21:41:33 -0400202 if (!cnt)
203 return;
204 if (cnt->pkg) fprintf(stderr, "package: %d ", cnt->pkg);
205 if (cnt->core) fprintf(stderr, "core:: %d ", cnt->core);
206 if (cnt->cpu) fprintf(stderr, "CPU: %d ", cnt->cpu);
207 if (cnt->tsc) fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
208 if (cnt->c3) fprintf(stderr, "c3: %016llX\n", cnt->c3);
209 if (cnt->c6) fprintf(stderr, "c6: %016llX\n", cnt->c6);
210 if (cnt->c7) fprintf(stderr, "c7: %016llX\n", cnt->c7);
211 if (cnt->aperf) fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
212 if (cnt->pc2) fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
213 if (cnt->pc3) fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
214 if (cnt->pc6) fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
215 if (cnt->pc7) fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
216 if (cnt->extra_msr) fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400217}
218
Len Browna829eb42011-02-10 23:36:34 -0500219void dump_list(struct counters *cnt)
Len Brown103a8fe2010-10-22 23:53:03 -0400220{
Len Browna829eb42011-02-10 23:36:34 -0500221 printf("dump_list 0x%p\n", cnt);
Len Brown103a8fe2010-10-22 23:53:03 -0400222
Len Browna829eb42011-02-10 23:36:34 -0500223 for (; cnt; cnt = cnt->next)
224 dump_cnt(cnt);
Len Brown103a8fe2010-10-22 23:53:03 -0400225}
226
Len Browne23da032012-02-06 18:37:16 -0500227/*
228 * column formatting convention & formats
229 * package: "pk" 2 columns %2d
230 * core: "cor" 3 columns %3d
231 * CPU: "CPU" 3 columns %3d
232 * GHz: "GHz" 3 columns %3.2
233 * TSC: "TSC" 3 columns %3.2
234 * percentage " %pc3" %6.2
235 */
Len Browna829eb42011-02-10 23:36:34 -0500236void print_cnt(struct counters *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400237{
238 double interval_float;
239
240 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
241
242 /* topology columns, print blanks on 1st (average) line */
Len Browna829eb42011-02-10 23:36:34 -0500243 if (p == cnt_average) {
Len Brown103a8fe2010-10-22 23:53:03 -0400244 if (show_pkg)
Len Browne23da032012-02-06 18:37:16 -0500245 fprintf(stderr, " ");
246 if (show_pkg && show_core)
Len Brownd30c4b72011-07-31 18:19:33 -0400247 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400248 if (show_core)
Len Browne23da032012-02-06 18:37:16 -0500249 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400250 if (show_cpu)
Len Browne23da032012-02-06 18:37:16 -0500251 fprintf(stderr, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400252 } else {
253 if (show_pkg)
Len Browne23da032012-02-06 18:37:16 -0500254 fprintf(stderr, "%2d", p->pkg);
255 if (show_pkg && show_core)
256 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400257 if (show_core)
Len Browne23da032012-02-06 18:37:16 -0500258 fprintf(stderr, "%3d", p->core);
Len Brown103a8fe2010-10-22 23:53:03 -0400259 if (show_cpu)
Len Browne23da032012-02-06 18:37:16 -0500260 fprintf(stderr, " %3d", p->cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400261 }
262
263 /* %c0 */
264 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500265 if (show_pkg || show_core || show_cpu)
266 fprintf(stderr, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400267 if (!skip_c0)
Len Browne23da032012-02-06 18:37:16 -0500268 fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400269 else
Len Browne23da032012-02-06 18:37:16 -0500270 fprintf(stderr, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400271 }
272
273 /* GHz */
274 if (has_aperf) {
275 if (!aperf_mperf_unstable) {
Len Browne23da032012-02-06 18:37:16 -0500276 fprintf(stderr, " %3.2f",
Len Brown103a8fe2010-10-22 23:53:03 -0400277 1.0 * p->tsc / units * p->aperf /
278 p->mperf / interval_float);
279 } else {
280 if (p->aperf > p->tsc || p->mperf > p->tsc) {
Len Browne23da032012-02-06 18:37:16 -0500281 fprintf(stderr, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400282 } else {
Len Browne23da032012-02-06 18:37:16 -0500283 fprintf(stderr, "%3.1f*",
Len Brown103a8fe2010-10-22 23:53:03 -0400284 1.0 * p->tsc /
285 units * p->aperf /
286 p->mperf / interval_float);
287 }
288 }
289 }
290
291 /* TSC */
292 fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);
293
294 if (do_nhm_cstates) {
295 if (!skip_c1)
Len Browne23da032012-02-06 18:37:16 -0500296 fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400297 else
Len Brownd30c4b72011-07-31 18:19:33 -0400298 fprintf(stderr, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400299 }
300 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400301 fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400302 if (do_nhm_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400303 fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400304 if (do_snb_cstates)
Len Brownd30c4b72011-07-31 18:19:33 -0400305 fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400306 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500307 fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400308 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500309 fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400310 if (do_nhm_cstates)
Len Browne23da032012-02-06 18:37:16 -0500311 fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400312 if (do_snb_cstates)
Len Browne23da032012-02-06 18:37:16 -0500313 fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400314 if (extra_msr_offset)
315 fprintf(stderr, " 0x%016llx", p->extra_msr);
316 putc('\n', stderr);
317}
318
Len Browna829eb42011-02-10 23:36:34 -0500319void print_counters(struct counters *counters)
Len Brown103a8fe2010-10-22 23:53:03 -0400320{
Len Browna829eb42011-02-10 23:36:34 -0500321 struct counters *cnt;
Len Browne23da032012-02-06 18:37:16 -0500322 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400323
Len Browne23da032012-02-06 18:37:16 -0500324
325 if (!printed || !summary_only)
326 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400327
328 if (num_cpus > 1)
Len Browna829eb42011-02-10 23:36:34 -0500329 print_cnt(cnt_average);
Len Brown103a8fe2010-10-22 23:53:03 -0400330
Len Browne23da032012-02-06 18:37:16 -0500331 printed = 1;
332
333 if (summary_only)
334 return;
335
Len Browna829eb42011-02-10 23:36:34 -0500336 for (cnt = counters; cnt != NULL; cnt = cnt->next)
337 print_cnt(cnt);
Len Brown103a8fe2010-10-22 23:53:03 -0400338
339}
340
341#define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
342
Len Browna829eb42011-02-10 23:36:34 -0500343int compute_delta(struct counters *after,
344 struct counters *before, struct counters *delta)
Len Brown103a8fe2010-10-22 23:53:03 -0400345{
346 int errors = 0;
347 int perf_err = 0;
348
349 skip_c0 = skip_c1 = 0;
350
351 for ( ; after && before && delta;
352 after = after->next, before = before->next, delta = delta->next) {
353 if (before->cpu != after->cpu) {
354 printf("cpu configuration changed: %d != %d\n",
355 before->cpu, after->cpu);
356 return -1;
357 }
358
359 if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
360 fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
361 before->cpu, before->tsc, after->tsc);
362 errors++;
363 }
364 /* check for TSC < 1 Mcycles over interval */
365 if (delta->tsc < (1000 * 1000)) {
366 fprintf(stderr, "Insanely slow TSC rate,"
367 " TSC stops in idle?\n");
368 fprintf(stderr, "You can disable all c-states"
369 " by booting with \"idle=poll\"\n");
370 fprintf(stderr, "or just the deep ones with"
371 " \"processor.max_cstate=1\"\n");
372 exit(-3);
373 }
374 if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
375 fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
376 before->cpu, before->c3, after->c3);
377 errors++;
378 }
379 if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
380 fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
381 before->cpu, before->c6, after->c6);
382 errors++;
383 }
384 if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
385 fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
386 before->cpu, before->c7, after->c7);
387 errors++;
388 }
389 if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
390 fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
391 before->cpu, before->pc2, after->pc2);
392 errors++;
393 }
394 if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
395 fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
396 before->cpu, before->pc3, after->pc3);
397 errors++;
398 }
399 if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
400 fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
401 before->cpu, before->pc6, after->pc6);
402 errors++;
403 }
404 if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
405 fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
406 before->cpu, before->pc7, after->pc7);
407 errors++;
408 }
409
410 perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
411 if (perf_err) {
412 fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
413 before->cpu, before->aperf, after->aperf);
414 }
415 perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
416 if (perf_err) {
417 fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
418 before->cpu, before->mperf, after->mperf);
419 }
420 if (perf_err) {
421 if (!aperf_mperf_unstable) {
422 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
423 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
424 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
425
426 aperf_mperf_unstable = 1;
427 }
428 /*
429 * mperf delta is likely a huge "positive" number
430 * can not use it for calculating c0 time
431 */
432 skip_c0 = 1;
433 skip_c1 = 1;
434 }
435
436 /*
437 * As mperf and tsc collection are not atomic,
438 * it is possible for mperf's non-halted cycles
439 * to exceed TSC's all cycles: show c1 = 0% in that case.
440 */
441 if (delta->mperf > delta->tsc)
442 delta->c1 = 0;
443 else /* normal case, derive c1 */
444 delta->c1 = delta->tsc - delta->mperf
445 - delta->c3 - delta->c6 - delta->c7;
446
447 if (delta->mperf == 0)
448 delta->mperf = 1; /* divide by 0 protection */
449
450 /*
451 * for "extra msr", just copy the latest w/o subtracting
452 */
453 delta->extra_msr = after->extra_msr;
454 if (errors) {
455 fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
Len Browna829eb42011-02-10 23:36:34 -0500456 dump_cnt(before);
Len Brown103a8fe2010-10-22 23:53:03 -0400457 fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
Len Browna829eb42011-02-10 23:36:34 -0500458 dump_cnt(after);
Len Brown103a8fe2010-10-22 23:53:03 -0400459 errors = 0;
460 }
461 }
462 return 0;
463}
464
Len Browna829eb42011-02-10 23:36:34 -0500465void compute_average(struct counters *delta, struct counters *avg)
Len Brown103a8fe2010-10-22 23:53:03 -0400466{
Len Browna829eb42011-02-10 23:36:34 -0500467 struct counters *sum;
Len Brown103a8fe2010-10-22 23:53:03 -0400468
Len Browna829eb42011-02-10 23:36:34 -0500469 sum = calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400470 if (sum == NULL) {
471 perror("calloc sum");
472 exit(1);
473 }
474
475 for (; delta; delta = delta->next) {
476 sum->tsc += delta->tsc;
477 sum->c1 += delta->c1;
478 sum->c3 += delta->c3;
479 sum->c6 += delta->c6;
480 sum->c7 += delta->c7;
481 sum->aperf += delta->aperf;
482 sum->mperf += delta->mperf;
483 sum->pc2 += delta->pc2;
484 sum->pc3 += delta->pc3;
485 sum->pc6 += delta->pc6;
486 sum->pc7 += delta->pc7;
487 }
488 avg->tsc = sum->tsc/num_cpus;
489 avg->c1 = sum->c1/num_cpus;
490 avg->c3 = sum->c3/num_cpus;
491 avg->c6 = sum->c6/num_cpus;
492 avg->c7 = sum->c7/num_cpus;
493 avg->aperf = sum->aperf/num_cpus;
494 avg->mperf = sum->mperf/num_cpus;
495 avg->pc2 = sum->pc2/num_cpus;
496 avg->pc3 = sum->pc3/num_cpus;
497 avg->pc6 = sum->pc6/num_cpus;
498 avg->pc7 = sum->pc7/num_cpus;
499
500 free(sum);
501}
502
Len Brown15aaa342012-03-29 22:19:58 -0400503int get_counters(struct counters *cnt)
Len Brown103a8fe2010-10-22 23:53:03 -0400504{
Len Browna829eb42011-02-10 23:36:34 -0500505 for ( ; cnt; cnt = cnt->next) {
Len Brown15aaa342012-03-29 22:19:58 -0400506
507 if (cpu_migrate(cnt->cpu))
508 return -1;
509
510 if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc))
511 return -1;
512
513 if (has_aperf) {
514 if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf))
515 return -1;
516 if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf))
517 return -1;
Len Brown88c32812012-03-29 21:44:40 -0400518 }
519
Len Brown15aaa342012-03-29 22:19:58 -0400520 if (do_nhm_cstates) {
521 if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3))
522 return -1;
523 if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6))
524 return -1;
525 }
526
Len Brown103a8fe2010-10-22 23:53:03 -0400527 if (do_snb_cstates)
Len Brown15aaa342012-03-29 22:19:58 -0400528 if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7))
529 return -1;
530
531 if (do_nhm_cstates) {
532 if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3))
533 return -1;
534 if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6))
535 return -1;
536 }
537 if (do_snb_cstates) {
538 if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2))
539 return -1;
540 if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7))
541 return -1;
542 }
Len Brown103a8fe2010-10-22 23:53:03 -0400543 if (extra_msr_offset)
Len Brown15aaa342012-03-29 22:19:58 -0400544 if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr))
545 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400546 }
Len Brown15aaa342012-03-29 22:19:58 -0400547 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400548}
549
Len Browna829eb42011-02-10 23:36:34 -0500550void print_nehalem_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400551{
552 unsigned long long msr;
553 unsigned int ratio;
554
555 if (!do_nehalem_platform_info)
556 return;
557
Len Brown15aaa342012-03-29 22:19:58 -0400558 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400559
560 ratio = (msr >> 40) & 0xFF;
561 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
562 ratio, bclk, ratio * bclk);
563
564 ratio = (msr >> 8) & 0xFF;
565 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
566 ratio, bclk, ratio * bclk);
567
568 if (verbose > 1)
569 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
570
571 if (!do_nehalem_turbo_ratio_limit)
572 return;
573
Len Brown15aaa342012-03-29 22:19:58 -0400574 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400575
576 ratio = (msr >> 24) & 0xFF;
577 if (ratio)
578 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
579 ratio, bclk, ratio * bclk);
580
581 ratio = (msr >> 16) & 0xFF;
582 if (ratio)
583 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
584 ratio, bclk, ratio * bclk);
585
586 ratio = (msr >> 8) & 0xFF;
587 if (ratio)
588 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
589 ratio, bclk, ratio * bclk);
590
591 ratio = (msr >> 0) & 0xFF;
592 if (ratio)
593 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
594 ratio, bclk, ratio * bclk);
595
596}
597
Len Browna829eb42011-02-10 23:36:34 -0500598void free_counter_list(struct counters *list)
Len Brown103a8fe2010-10-22 23:53:03 -0400599{
Len Browna829eb42011-02-10 23:36:34 -0500600 struct counters *p;
Len Brown103a8fe2010-10-22 23:53:03 -0400601
602 for (p = list; p; ) {
Len Browna829eb42011-02-10 23:36:34 -0500603 struct counters *free_me;
Len Brown103a8fe2010-10-22 23:53:03 -0400604
605 free_me = p;
606 p = p->next;
607 free(free_me);
608 }
Len Brown103a8fe2010-10-22 23:53:03 -0400609}
610
611void free_all_counters(void)
612{
Len Browna829eb42011-02-10 23:36:34 -0500613 free_counter_list(cnt_even);
614 cnt_even = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400615
Len Browna829eb42011-02-10 23:36:34 -0500616 free_counter_list(cnt_odd);
617 cnt_odd = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400618
Len Browna829eb42011-02-10 23:36:34 -0500619 free_counter_list(cnt_delta);
620 cnt_delta = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400621
Len Browna829eb42011-02-10 23:36:34 -0500622 free_counter_list(cnt_average);
623 cnt_average = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400624}
625
Len Browna829eb42011-02-10 23:36:34 -0500626void insert_counters(struct counters **list,
627 struct counters *new)
Len Brown103a8fe2010-10-22 23:53:03 -0400628{
Len Browna829eb42011-02-10 23:36:34 -0500629 struct counters *prev;
Len Brown103a8fe2010-10-22 23:53:03 -0400630
631 /*
632 * list was empty
633 */
634 if (*list == NULL) {
635 new->next = *list;
636 *list = new;
637 return;
638 }
639
Len Browne23da032012-02-06 18:37:16 -0500640 if (!summary_only)
641 show_cpu = 1; /* there is more than one CPU */
Len Brown103a8fe2010-10-22 23:53:03 -0400642
643 /*
644 * insert on front of list.
645 * It is sorted by ascending package#, core#, cpu#
646 */
647 if (((*list)->pkg > new->pkg) ||
648 (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
649 (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
650 new->next = *list;
651 *list = new;
652 return;
653 }
654
655 prev = *list;
656
657 while (prev->next && (prev->next->pkg < new->pkg)) {
658 prev = prev->next;
Len Browne23da032012-02-06 18:37:16 -0500659 if (!summary_only)
660 show_pkg = 1; /* there is more than 1 package */
Len Brown103a8fe2010-10-22 23:53:03 -0400661 }
662
663 while (prev->next && (prev->next->pkg == new->pkg)
664 && (prev->next->core < new->core)) {
665 prev = prev->next;
Len Browne23da032012-02-06 18:37:16 -0500666 if (!summary_only)
667 show_core = 1; /* there is more than 1 core */
Len Brown103a8fe2010-10-22 23:53:03 -0400668 }
669
670 while (prev->next && (prev->next->pkg == new->pkg)
671 && (prev->next->core == new->core)
672 && (prev->next->cpu < new->cpu)) {
673 prev = prev->next;
674 }
675
676 /*
677 * insert after "prev"
678 */
679 new->next = prev->next;
680 prev->next = new;
Len Brown103a8fe2010-10-22 23:53:03 -0400681}
682
Len Browna829eb42011-02-10 23:36:34 -0500683void alloc_new_counters(int pkg, int core, int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400684{
Len Browna829eb42011-02-10 23:36:34 -0500685 struct counters *new;
Len Brown103a8fe2010-10-22 23:53:03 -0400686
687 if (verbose > 1)
688 printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
689
Len Browna829eb42011-02-10 23:36:34 -0500690 new = (struct counters *)calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400691 if (new == NULL) {
692 perror("calloc");
693 exit(1);
694 }
695 new->pkg = pkg;
696 new->core = core;
697 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500698 insert_counters(&cnt_odd, new);
Len Brown103a8fe2010-10-22 23:53:03 -0400699
Len Browna829eb42011-02-10 23:36:34 -0500700 new = (struct counters *)calloc(1,
701 sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400702 if (new == NULL) {
703 perror("calloc");
704 exit(1);
705 }
706 new->pkg = pkg;
707 new->core = core;
708 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500709 insert_counters(&cnt_even, new);
Len Brown103a8fe2010-10-22 23:53:03 -0400710
Len Browna829eb42011-02-10 23:36:34 -0500711 new = (struct counters *)calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400712 if (new == NULL) {
713 perror("calloc");
714 exit(1);
715 }
716 new->pkg = pkg;
717 new->core = core;
718 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500719 insert_counters(&cnt_delta, new);
Len Brown103a8fe2010-10-22 23:53:03 -0400720
Len Browna829eb42011-02-10 23:36:34 -0500721 new = (struct counters *)calloc(1, sizeof(struct counters));
Len Brown103a8fe2010-10-22 23:53:03 -0400722 if (new == NULL) {
723 perror("calloc");
724 exit(1);
725 }
726 new->pkg = pkg;
727 new->core = core;
728 new->cpu = cpu;
Len Browna829eb42011-02-10 23:36:34 -0500729 cnt_average = new;
Len Brown103a8fe2010-10-22 23:53:03 -0400730}
731
732int get_physical_package_id(int cpu)
733{
734 char path[64];
735 FILE *filep;
736 int pkg;
737
738 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
739 filep = fopen(path, "r");
740 if (filep == NULL) {
741 perror(path);
742 exit(1);
743 }
744 fscanf(filep, "%d", &pkg);
745 fclose(filep);
746 return pkg;
747}
748
749int get_core_id(int cpu)
750{
751 char path[64];
752 FILE *filep;
753 int core;
754
755 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
756 filep = fopen(path, "r");
757 if (filep == NULL) {
758 perror(path);
759 exit(1);
760 }
761 fscanf(filep, "%d", &core);
762 fclose(filep);
763 return core;
764}
765
766/*
Len Brown15aaa342012-03-29 22:19:58 -0400767 * run func(pkg, core, cpu) on every cpu in /proc/stat
Len Brown103a8fe2010-10-22 23:53:03 -0400768 */
769
770int for_all_cpus(void (func)(int, int, int))
771{
772 FILE *fp;
773 int cpu_count;
774 int retval;
775
776 fp = fopen(proc_stat, "r");
777 if (fp == NULL) {
778 perror(proc_stat);
779 exit(1);
780 }
781
782 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
783 if (retval != 0) {
784 perror("/proc/stat format");
785 exit(1);
786 }
787
788 for (cpu_count = 0; ; cpu_count++) {
789 int cpu;
790
791 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
792 if (retval != 1)
793 break;
794
795 func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
796 }
797 fclose(fp);
798 return cpu_count;
799}
800
801void re_initialize(void)
802{
Len Brown103a8fe2010-10-22 23:53:03 -0400803 free_all_counters();
Len Browna829eb42011-02-10 23:36:34 -0500804 num_cpus = for_all_cpus(alloc_new_counters);
Len Brown88c32812012-03-29 21:44:40 -0400805 cpu_mask_uninit();
806 cpu_mask_init(num_cpus);
Len Brown15aaa342012-03-29 22:19:58 -0400807 printf("turbostat: re-initialized with num_cpus %d\n", num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -0400808}
809
810void dummy(int pkg, int core, int cpu) { return; }
811/*
812 * check to see if a cpu came on-line
813 */
Len Brown15aaa342012-03-29 22:19:58 -0400814int verify_num_cpus(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400815{
816 int new_num_cpus;
817
818 new_num_cpus = for_all_cpus(dummy);
819
820 if (new_num_cpus != num_cpus) {
821 if (verbose)
822 printf("num_cpus was %d, is now %d\n",
823 num_cpus, new_num_cpus);
Len Brown15aaa342012-03-29 22:19:58 -0400824 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400825 }
Len Brown15aaa342012-03-29 22:19:58 -0400826 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400827}
828
829void turbostat_loop()
830{
831restart:
Len Browna829eb42011-02-10 23:36:34 -0500832 get_counters(cnt_even);
Len Brown103a8fe2010-10-22 23:53:03 -0400833 gettimeofday(&tv_even, (struct timezone *)NULL);
834
835 while (1) {
Len Brown15aaa342012-03-29 22:19:58 -0400836 if (verify_num_cpus()) {
Len Brown103a8fe2010-10-22 23:53:03 -0400837 re_initialize();
838 goto restart;
839 }
840 sleep(interval_sec);
Len Brown15aaa342012-03-29 22:19:58 -0400841 if (get_counters(cnt_odd)) {
842 re_initialize();
843 goto restart;
844 }
Len Brown103a8fe2010-10-22 23:53:03 -0400845 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Browna829eb42011-02-10 23:36:34 -0500846 compute_delta(cnt_odd, cnt_even, cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -0400847 timersub(&tv_odd, &tv_even, &tv_delta);
Len Browna829eb42011-02-10 23:36:34 -0500848 compute_average(cnt_delta, cnt_average);
849 print_counters(cnt_delta);
Len Brown15aaa342012-03-29 22:19:58 -0400850 sleep(interval_sec);
851 if (get_counters(cnt_even)) {
Len Brown103a8fe2010-10-22 23:53:03 -0400852 re_initialize();
853 goto restart;
854 }
Len Brown103a8fe2010-10-22 23:53:03 -0400855 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Browna829eb42011-02-10 23:36:34 -0500856 compute_delta(cnt_even, cnt_odd, cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -0400857 timersub(&tv_even, &tv_odd, &tv_delta);
Len Browna829eb42011-02-10 23:36:34 -0500858 compute_average(cnt_delta, cnt_average);
859 print_counters(cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -0400860 }
861}
862
863void check_dev_msr()
864{
865 struct stat sb;
866
867 if (stat("/dev/cpu/0/msr", &sb)) {
868 fprintf(stderr, "no /dev/cpu/0/msr\n");
869 fprintf(stderr, "Try \"# modprobe msr\"\n");
870 exit(-5);
871 }
872}
873
874void check_super_user()
875{
876 if (getuid() != 0) {
877 fprintf(stderr, "must be root\n");
878 exit(-6);
879 }
880}
881
882int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
883{
884 if (!genuine_intel)
885 return 0;
886
887 if (family != 6)
888 return 0;
889
890 switch (model) {
891 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
892 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
893 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
894 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
895 case 0x2C: /* Westmere EP - Gulftown */
896 case 0x2A: /* SNB */
897 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -0500898 case 0x3A: /* IVB */
899 case 0x3D: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -0400900 return 1;
901 case 0x2E: /* Nehalem-EX Xeon - Beckton */
902 case 0x2F: /* Westmere-EX Xeon - Eagleton */
903 default:
904 return 0;
905 }
906}
907
908int is_snb(unsigned int family, unsigned int model)
909{
910 if (!genuine_intel)
911 return 0;
912
913 switch (model) {
914 case 0x2A:
915 case 0x2D:
916 return 1;
917 }
918 return 0;
919}
920
921double discover_bclk(unsigned int family, unsigned int model)
922{
923 if (is_snb(family, model))
924 return 100.00;
925 else
926 return 133.33;
927}
928
929void check_cpuid()
930{
931 unsigned int eax, ebx, ecx, edx, max_level;
932 unsigned int fms, family, model, stepping;
933
934 eax = ebx = ecx = edx = 0;
935
Josh Triplett4c6544f2013-08-20 17:20:14 -0700936 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -0400937
938 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
939 genuine_intel = 1;
940
941 if (verbose)
942 fprintf(stderr, "%.4s%.4s%.4s ",
943 (char *)&ebx, (char *)&edx, (char *)&ecx);
944
Josh Triplett4c6544f2013-08-20 17:20:14 -0700945 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -0400946 family = (fms >> 8) & 0xf;
947 model = (fms >> 4) & 0xf;
948 stepping = fms & 0xf;
949 if (family == 6 || family == 0xf)
950 model += ((fms >> 16) & 0xf) << 4;
951
952 if (verbose)
953 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
954 max_level, family, model, stepping, family, model, stepping);
955
956 if (!(edx & (1 << 5))) {
957 fprintf(stderr, "CPUID: no MSR\n");
958 exit(1);
959 }
960
961 /*
962 * check max extended function levels of CPUID.
963 * This is needed to check for invariant TSC.
964 * This check is valid for both Intel and AMD.
965 */
966 ebx = ecx = edx = 0;
Josh Triplett4c6544f2013-08-20 17:20:14 -0700967 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -0400968
969 if (max_level < 0x80000007) {
970 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
971 exit(1);
972 }
973
974 /*
975 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
976 * this check is valid for both Intel and AMD
977 */
Josh Triplett4c6544f2013-08-20 17:20:14 -0700978 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +0100979 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -0400980
981 if (!has_invariant_tsc) {
982 fprintf(stderr, "No invariant TSC\n");
983 exit(1);
984 }
985
986 /*
987 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
988 * this check is valid for both Intel and AMD
989 */
990
Josh Triplett4c6544f2013-08-20 17:20:14 -0700991 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +0100992 has_aperf = ecx & (1 << 0);
Len Brown103a8fe2010-10-22 23:53:03 -0400993 if (!has_aperf) {
994 fprintf(stderr, "No APERF MSR\n");
995 exit(1);
996 }
997
998 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
999 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1000 do_snb_cstates = is_snb(family, model);
1001 bclk = discover_bclk(family, model);
1002
1003 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1004}
1005
1006
1007void usage()
1008{
1009 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1010 progname);
1011 exit(1);
1012}
1013
1014
1015/*
1016 * in /dev/cpu/ return success for names that are numbers
1017 * ie. filter out ".", "..", "microcode".
1018 */
1019int dir_filter(const struct dirent *dirp)
1020{
1021 if (isdigit(dirp->d_name[0]))
1022 return 1;
1023 else
1024 return 0;
1025}
1026
1027int open_dev_cpu_msr(int dummy1)
1028{
1029 return 0;
1030}
1031
1032void turbostat_init()
1033{
1034 check_cpuid();
1035
1036 check_dev_msr();
1037 check_super_user();
1038
Len Browna829eb42011-02-10 23:36:34 -05001039 num_cpus = for_all_cpus(alloc_new_counters);
Len Brown88c32812012-03-29 21:44:40 -04001040 cpu_mask_init(num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001041
1042 if (verbose)
1043 print_nehalem_info();
1044}
1045
1046int fork_it(char **argv)
1047{
1048 int retval;
1049 pid_t child_pid;
Len Browna829eb42011-02-10 23:36:34 -05001050 get_counters(cnt_even);
Len Brown103a8fe2010-10-22 23:53:03 -04001051 gettimeofday(&tv_even, (struct timezone *)NULL);
1052
1053 child_pid = fork();
1054 if (!child_pid) {
1055 /* child */
1056 execvp(argv[0], argv);
1057 } else {
1058 int status;
1059
1060 /* parent */
1061 if (child_pid == -1) {
1062 perror("fork");
1063 exit(1);
1064 }
1065
1066 signal(SIGINT, SIG_IGN);
1067 signal(SIGQUIT, SIG_IGN);
1068 if (waitpid(child_pid, &status, 0) == -1) {
1069 perror("wait");
1070 exit(1);
1071 }
1072 }
Len Browna829eb42011-02-10 23:36:34 -05001073 get_counters(cnt_odd);
Len Brown103a8fe2010-10-22 23:53:03 -04001074 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Browna829eb42011-02-10 23:36:34 -05001075 retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -04001076
1077 timersub(&tv_odd, &tv_even, &tv_delta);
Len Browna829eb42011-02-10 23:36:34 -05001078 compute_average(cnt_delta, cnt_average);
Len Brown103a8fe2010-10-22 23:53:03 -04001079 if (!retval)
Len Browna829eb42011-02-10 23:36:34 -05001080 print_counters(cnt_delta);
Len Brown103a8fe2010-10-22 23:53:03 -04001081
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001082 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04001083
1084 return 0;
1085}
1086
1087void cmdline(int argc, char **argv)
1088{
1089 int opt;
1090
1091 progname = argv[0];
1092
Len Browne23da032012-02-06 18:37:16 -05001093 while ((opt = getopt(argc, argv, "+svi:M:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001094 switch (opt) {
Len Browne23da032012-02-06 18:37:16 -05001095 case 's':
1096 summary_only++;
1097 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001098 case 'v':
1099 verbose++;
1100 break;
1101 case 'i':
1102 interval_sec = atoi(optarg);
1103 break;
1104 case 'M':
1105 sscanf(optarg, "%x", &extra_msr_offset);
1106 if (verbose > 1)
1107 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1108 break;
1109 default:
1110 usage();
1111 }
1112 }
1113}
1114
1115int main(int argc, char **argv)
1116{
1117 cmdline(argc, argv);
1118
1119 if (verbose > 1)
1120 fprintf(stderr, "turbostat Dec 6, 2010"
1121 " - Len Brown <lenb@kernel.org>\n");
1122 if (verbose > 1)
1123 fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");
1124
1125 turbostat_init();
1126
1127 /*
1128 * if any params left, it must be a command to fork
1129 */
1130 if (argc - optind)
1131 return fork_it(argv + optind);
1132 else
1133 turbostat_loop();
1134
1135 return 0;
1136}