2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
5 * Copyright (c) 2012 Intel Corporation.
6 * Len Brown <len.brown@intel.com>
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.
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
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.
24 #include <sys/types.h>
27 #include <sys/resource.h>
37 #define MSR_NEHALEM_PLATFORM_INFO 0xCE
38 #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD
39 #define MSR_APERF 0xE8
40 #define MSR_MPERF 0xE7
41 #define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */
42 #define MSR_PKG_C3_RESIDENCY 0x3F8
43 #define MSR_PKG_C6_RESIDENCY 0x3F9
44 #define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */
45 #define MSR_CORE_C3_RESIDENCY 0x3FC
46 #define MSR_CORE_C6_RESIDENCY 0x3FD
47 #define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */
49 char *proc_stat = "/proc/stat";
50 unsigned int interval_sec = 5; /* set with -i interval_sec */
51 unsigned int verbose; /* set with -v */
52 unsigned int summary_only; /* set with -s */
55 unsigned int do_nhm_cstates;
56 unsigned int do_snb_cstates;
57 unsigned int has_aperf;
58 unsigned int units = 1000000000; /* Ghz etc */
59 unsigned int genuine_intel;
60 unsigned int has_invariant_tsc;
61 unsigned int do_nehalem_platform_info;
62 unsigned int do_nehalem_turbo_ratio_limit;
63 unsigned int extra_msr_offset;
65 unsigned int show_pkg;
66 unsigned int show_core;
67 unsigned int show_cpu;
69 int aperf_mperf_unstable;
72 int need_reinitialize;
77 unsigned long long tsc; /* per thread */
78 unsigned long long aperf; /* per thread */
79 unsigned long long mperf; /* per thread */
80 unsigned long long c1; /* per thread (calculated) */
81 unsigned long long c3; /* per core */
82 unsigned long long c6; /* per core */
83 unsigned long long c7; /* per core */
84 unsigned long long pc2; /* per package */
85 unsigned long long pc3; /* per package */
86 unsigned long long pc6; /* per package */
87 unsigned long long pc7; /* per package */
88 unsigned long long extra_msr; /* per thread */
92 struct counters *next;
95 struct counters *cnt_even;
96 struct counters *cnt_odd;
97 struct counters *cnt_delta;
98 struct counters *cnt_average;
99 struct timeval tv_even;
100 struct timeval tv_odd;
101 struct timeval tv_delta;
103 unsigned long long get_msr(int cpu, off_t offset)
106 unsigned long long msr;
110 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
111 fd = open(pathname, O_RDONLY);
114 need_reinitialize = 1;
118 retval = pread(fd, &msr, sizeof msr, offset);
119 if (retval != sizeof msr) {
120 fprintf(stderr, "cpu%d pread(..., 0x%zx) = %jd\n",
121 cpu, offset, retval);
129 void print_header(void)
132 fprintf(stderr, "pk");
134 fprintf(stderr, " ");
136 fprintf(stderr, "cor");
138 fprintf(stderr, " CPU");
139 if (show_pkg || show_core || show_cpu)
140 fprintf(stderr, " ");
142 fprintf(stderr, " %%c0");
144 fprintf(stderr, " GHz");
145 fprintf(stderr, " TSC");
147 fprintf(stderr, " %%c1");
149 fprintf(stderr, " %%c3");
151 fprintf(stderr, " %%c6");
153 fprintf(stderr, " %%c7");
155 fprintf(stderr, " %%pc2");
157 fprintf(stderr, " %%pc3");
159 fprintf(stderr, " %%pc6");
161 fprintf(stderr, " %%pc7");
162 if (extra_msr_offset)
163 fprintf(stderr, " MSR 0x%x ", extra_msr_offset);
168 void dump_cnt(struct counters *cnt)
172 if (cnt->pkg) fprintf(stderr, "package: %d ", cnt->pkg);
173 if (cnt->core) fprintf(stderr, "core:: %d ", cnt->core);
174 if (cnt->cpu) fprintf(stderr, "CPU: %d ", cnt->cpu);
175 if (cnt->tsc) fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
176 if (cnt->c3) fprintf(stderr, "c3: %016llX\n", cnt->c3);
177 if (cnt->c6) fprintf(stderr, "c6: %016llX\n", cnt->c6);
178 if (cnt->c7) fprintf(stderr, "c7: %016llX\n", cnt->c7);
179 if (cnt->aperf) fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
180 if (cnt->pc2) fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
181 if (cnt->pc3) fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
182 if (cnt->pc6) fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
183 if (cnt->pc7) fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
184 if (cnt->extra_msr) fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
187 void dump_list(struct counters *cnt)
189 printf("dump_list 0x%p\n", cnt);
191 for (; cnt; cnt = cnt->next)
196 * column formatting convention & formats
197 * package: "pk" 2 columns %2d
198 * core: "cor" 3 columns %3d
199 * CPU: "CPU" 3 columns %3d
200 * GHz: "GHz" 3 columns %3.2
201 * TSC: "TSC" 3 columns %3.2
202 * percentage " %pc3" %6.2
204 void print_cnt(struct counters *p)
206 double interval_float;
208 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
210 /* topology columns, print blanks on 1st (average) line */
211 if (p == cnt_average) {
213 fprintf(stderr, " ");
214 if (show_pkg && show_core)
215 fprintf(stderr, " ");
217 fprintf(stderr, " ");
219 fprintf(stderr, " " " ");
222 fprintf(stderr, "%2d", p->pkg);
223 if (show_pkg && show_core)
224 fprintf(stderr, " ");
226 fprintf(stderr, "%3d", p->core);
228 fprintf(stderr, " %3d", p->cpu);
232 if (do_nhm_cstates) {
233 if (show_pkg || show_core || show_cpu)
234 fprintf(stderr, " ");
236 fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc);
238 fprintf(stderr, " ****");
243 if (!aperf_mperf_unstable) {
244 fprintf(stderr, " %3.2f",
245 1.0 * p->tsc / units * p->aperf /
246 p->mperf / interval_float);
248 if (p->aperf > p->tsc || p->mperf > p->tsc) {
249 fprintf(stderr, " ***");
251 fprintf(stderr, "%3.1f*",
254 p->mperf / interval_float);
260 fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);
262 if (do_nhm_cstates) {
264 fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc);
266 fprintf(stderr, " ****");
269 fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc);
271 fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc);
273 fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc);
275 fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc);
277 fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc);
279 fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc);
281 fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc);
282 if (extra_msr_offset)
283 fprintf(stderr, " 0x%016llx", p->extra_msr);
287 void print_counters(struct counters *counters)
289 struct counters *cnt;
293 if (!printed || !summary_only)
297 print_cnt(cnt_average);
304 for (cnt = counters; cnt != NULL; cnt = cnt->next)
309 #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
311 int compute_delta(struct counters *after,
312 struct counters *before, struct counters *delta)
317 skip_c0 = skip_c1 = 0;
319 for ( ; after && before && delta;
320 after = after->next, before = before->next, delta = delta->next) {
321 if (before->cpu != after->cpu) {
322 printf("cpu configuration changed: %d != %d\n",
323 before->cpu, after->cpu);
327 if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
328 fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
329 before->cpu, before->tsc, after->tsc);
332 /* check for TSC < 1 Mcycles over interval */
333 if (delta->tsc < (1000 * 1000)) {
334 fprintf(stderr, "Insanely slow TSC rate,"
335 " TSC stops in idle?\n");
336 fprintf(stderr, "You can disable all c-states"
337 " by booting with \"idle=poll\"\n");
338 fprintf(stderr, "or just the deep ones with"
339 " \"processor.max_cstate=1\"\n");
342 if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
343 fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
344 before->cpu, before->c3, after->c3);
347 if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
348 fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
349 before->cpu, before->c6, after->c6);
352 if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
353 fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
354 before->cpu, before->c7, after->c7);
357 if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
358 fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
359 before->cpu, before->pc2, after->pc2);
362 if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
363 fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
364 before->cpu, before->pc3, after->pc3);
367 if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
368 fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
369 before->cpu, before->pc6, after->pc6);
372 if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
373 fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
374 before->cpu, before->pc7, after->pc7);
378 perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
380 fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
381 before->cpu, before->aperf, after->aperf);
383 perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
385 fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
386 before->cpu, before->mperf, after->mperf);
389 if (!aperf_mperf_unstable) {
390 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
391 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
392 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
394 aperf_mperf_unstable = 1;
397 * mperf delta is likely a huge "positive" number
398 * can not use it for calculating c0 time
405 * As mperf and tsc collection are not atomic,
406 * it is possible for mperf's non-halted cycles
407 * to exceed TSC's all cycles: show c1 = 0% in that case.
409 if (delta->mperf > delta->tsc)
411 else /* normal case, derive c1 */
412 delta->c1 = delta->tsc - delta->mperf
413 - delta->c3 - delta->c6 - delta->c7;
415 if (delta->mperf == 0)
416 delta->mperf = 1; /* divide by 0 protection */
419 * for "extra msr", just copy the latest w/o subtracting
421 delta->extra_msr = after->extra_msr;
423 fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
425 fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
433 void compute_average(struct counters *delta, struct counters *avg)
435 struct counters *sum;
437 sum = calloc(1, sizeof(struct counters));
439 perror("calloc sum");
443 for (; delta; delta = delta->next) {
444 sum->tsc += delta->tsc;
445 sum->c1 += delta->c1;
446 sum->c3 += delta->c3;
447 sum->c6 += delta->c6;
448 sum->c7 += delta->c7;
449 sum->aperf += delta->aperf;
450 sum->mperf += delta->mperf;
451 sum->pc2 += delta->pc2;
452 sum->pc3 += delta->pc3;
453 sum->pc6 += delta->pc6;
454 sum->pc7 += delta->pc7;
456 avg->tsc = sum->tsc/num_cpus;
457 avg->c1 = sum->c1/num_cpus;
458 avg->c3 = sum->c3/num_cpus;
459 avg->c6 = sum->c6/num_cpus;
460 avg->c7 = sum->c7/num_cpus;
461 avg->aperf = sum->aperf/num_cpus;
462 avg->mperf = sum->mperf/num_cpus;
463 avg->pc2 = sum->pc2/num_cpus;
464 avg->pc3 = sum->pc3/num_cpus;
465 avg->pc6 = sum->pc6/num_cpus;
466 avg->pc7 = sum->pc7/num_cpus;
471 void get_counters(struct counters *cnt)
473 for ( ; cnt; cnt = cnt->next) {
474 cnt->tsc = get_msr(cnt->cpu, MSR_TSC);
476 cnt->c3 = get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY);
478 cnt->c6 = get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY);
480 cnt->c7 = get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY);
482 cnt->aperf = get_msr(cnt->cpu, MSR_APERF);
484 cnt->mperf = get_msr(cnt->cpu, MSR_MPERF);
486 cnt->pc2 = get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY);
488 cnt->pc3 = get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY);
490 cnt->pc6 = get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY);
492 cnt->pc7 = get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY);
493 if (extra_msr_offset)
494 cnt->extra_msr = get_msr(cnt->cpu, extra_msr_offset);
498 void print_nehalem_info(void)
500 unsigned long long msr;
503 if (!do_nehalem_platform_info)
506 msr = get_msr(0, MSR_NEHALEM_PLATFORM_INFO);
508 ratio = (msr >> 40) & 0xFF;
509 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
510 ratio, bclk, ratio * bclk);
512 ratio = (msr >> 8) & 0xFF;
513 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
514 ratio, bclk, ratio * bclk);
517 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
519 if (!do_nehalem_turbo_ratio_limit)
522 msr = get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT);
524 ratio = (msr >> 24) & 0xFF;
526 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
527 ratio, bclk, ratio * bclk);
529 ratio = (msr >> 16) & 0xFF;
531 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
532 ratio, bclk, ratio * bclk);
534 ratio = (msr >> 8) & 0xFF;
536 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
537 ratio, bclk, ratio * bclk);
539 ratio = (msr >> 0) & 0xFF;
541 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
542 ratio, bclk, ratio * bclk);
546 void free_counter_list(struct counters *list)
550 for (p = list; p; ) {
551 struct counters *free_me;
559 void free_all_counters(void)
561 free_counter_list(cnt_even);
564 free_counter_list(cnt_odd);
567 free_counter_list(cnt_delta);
570 free_counter_list(cnt_average);
574 void insert_counters(struct counters **list,
575 struct counters *new)
577 struct counters *prev;
589 show_cpu = 1; /* there is more than one CPU */
592 * insert on front of list.
593 * It is sorted by ascending package#, core#, cpu#
595 if (((*list)->pkg > new->pkg) ||
596 (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
597 (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
605 while (prev->next && (prev->next->pkg < new->pkg)) {
608 show_pkg = 1; /* there is more than 1 package */
611 while (prev->next && (prev->next->pkg == new->pkg)
612 && (prev->next->core < new->core)) {
615 show_core = 1; /* there is more than 1 core */
618 while (prev->next && (prev->next->pkg == new->pkg)
619 && (prev->next->core == new->core)
620 && (prev->next->cpu < new->cpu)) {
625 * insert after "prev"
627 new->next = prev->next;
631 void alloc_new_counters(int pkg, int core, int cpu)
633 struct counters *new;
636 printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
638 new = (struct counters *)calloc(1, sizeof(struct counters));
646 insert_counters(&cnt_odd, new);
648 new = (struct counters *)calloc(1,
649 sizeof(struct counters));
657 insert_counters(&cnt_even, new);
659 new = (struct counters *)calloc(1, sizeof(struct counters));
667 insert_counters(&cnt_delta, new);
669 new = (struct counters *)calloc(1, sizeof(struct counters));
680 int get_physical_package_id(int cpu)
686 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
687 filep = fopen(path, "r");
692 fscanf(filep, "%d", &pkg);
697 int get_core_id(int cpu)
703 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
704 filep = fopen(path, "r");
709 fscanf(filep, "%d", &core);
715 * run func(index, cpu) on every cpu in /proc/stat
718 int for_all_cpus(void (func)(int, int, int))
724 fp = fopen(proc_stat, "r");
730 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
732 perror("/proc/stat format");
736 for (cpu_count = 0; ; cpu_count++) {
739 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
743 func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
749 void re_initialize(void)
751 printf("turbostat: topology changed, re-initializing.\n");
753 num_cpus = for_all_cpus(alloc_new_counters);
754 need_reinitialize = 0;
755 printf("num_cpus is now %d\n", num_cpus);
758 void dummy(int pkg, int core, int cpu) { return; }
760 * check to see if a cpu came on-line
762 void verify_num_cpus(void)
766 new_num_cpus = for_all_cpus(dummy);
768 if (new_num_cpus != num_cpus) {
770 printf("num_cpus was %d, is now %d\n",
771 num_cpus, new_num_cpus);
772 need_reinitialize = 1;
776 void turbostat_loop()
779 get_counters(cnt_even);
780 gettimeofday(&tv_even, (struct timezone *)NULL);
784 if (need_reinitialize) {
789 get_counters(cnt_odd);
790 gettimeofday(&tv_odd, (struct timezone *)NULL);
792 compute_delta(cnt_odd, cnt_even, cnt_delta);
793 timersub(&tv_odd, &tv_even, &tv_delta);
794 compute_average(cnt_delta, cnt_average);
795 print_counters(cnt_delta);
796 if (need_reinitialize) {
801 get_counters(cnt_even);
802 gettimeofday(&tv_even, (struct timezone *)NULL);
803 compute_delta(cnt_even, cnt_odd, cnt_delta);
804 timersub(&tv_even, &tv_odd, &tv_delta);
805 compute_average(cnt_delta, cnt_average);
806 print_counters(cnt_delta);
814 if (stat("/dev/cpu/0/msr", &sb)) {
815 fprintf(stderr, "no /dev/cpu/0/msr\n");
816 fprintf(stderr, "Try \"# modprobe msr\"\n");
821 void check_super_user()
824 fprintf(stderr, "must be root\n");
829 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
838 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
839 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
840 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
841 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
842 case 0x2C: /* Westmere EP - Gulftown */
844 case 0x2D: /* SNB Xeon */
846 case 0x3D: /* IVB Xeon */
848 case 0x2E: /* Nehalem-EX Xeon - Beckton */
849 case 0x2F: /* Westmere-EX Xeon - Eagleton */
855 int is_snb(unsigned int family, unsigned int model)
868 double discover_bclk(unsigned int family, unsigned int model)
870 if (is_snb(family, model))
878 unsigned int eax, ebx, ecx, edx, max_level;
879 unsigned int fms, family, model, stepping;
881 eax = ebx = ecx = edx = 0;
883 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
885 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
889 fprintf(stderr, "%.4s%.4s%.4s ",
890 (char *)&ebx, (char *)&edx, (char *)&ecx);
892 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
893 family = (fms >> 8) & 0xf;
894 model = (fms >> 4) & 0xf;
895 stepping = fms & 0xf;
896 if (family == 6 || family == 0xf)
897 model += ((fms >> 16) & 0xf) << 4;
900 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
901 max_level, family, model, stepping, family, model, stepping);
903 if (!(edx & (1 << 5))) {
904 fprintf(stderr, "CPUID: no MSR\n");
909 * check max extended function levels of CPUID.
910 * This is needed to check for invariant TSC.
911 * This check is valid for both Intel and AMD.
914 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
916 if (max_level < 0x80000007) {
917 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
922 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
923 * this check is valid for both Intel and AMD
925 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
926 has_invariant_tsc = edx & (1 << 8);
928 if (!has_invariant_tsc) {
929 fprintf(stderr, "No invariant TSC\n");
934 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
935 * this check is valid for both Intel and AMD
938 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
939 has_aperf = ecx & (1 << 0);
941 fprintf(stderr, "No APERF MSR\n");
945 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
946 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
947 do_snb_cstates = is_snb(family, model);
948 bclk = discover_bclk(family, model);
950 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
956 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
963 * in /dev/cpu/ return success for names that are numbers
964 * ie. filter out ".", "..", "microcode".
966 int dir_filter(const struct dirent *dirp)
968 if (isdigit(dirp->d_name[0]))
974 int open_dev_cpu_msr(int dummy1)
979 void turbostat_init()
986 num_cpus = for_all_cpus(alloc_new_counters);
989 print_nehalem_info();
992 int fork_it(char **argv)
996 get_counters(cnt_even);
997 gettimeofday(&tv_even, (struct timezone *)NULL);
1002 execvp(argv[0], argv);
1007 if (child_pid == -1) {
1012 signal(SIGINT, SIG_IGN);
1013 signal(SIGQUIT, SIG_IGN);
1014 if (waitpid(child_pid, &status, 0) == -1) {
1019 get_counters(cnt_odd);
1020 gettimeofday(&tv_odd, (struct timezone *)NULL);
1021 retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
1023 timersub(&tv_odd, &tv_even, &tv_delta);
1024 compute_average(cnt_delta, cnt_average);
1026 print_counters(cnt_delta);
1028 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1033 void cmdline(int argc, char **argv)
1039 while ((opt = getopt(argc, argv, "+svi:M:")) != -1) {
1048 interval_sec = atoi(optarg);
1051 sscanf(optarg, "%x", &extra_msr_offset);
1053 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1061 int main(int argc, char **argv)
1063 cmdline(argc, argv);
1066 fprintf(stderr, "turbostat Dec 6, 2010"
1067 " - Len Brown <lenb@kernel.org>\n");
1069 fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");
1074 * if any params left, it must be a command to fork
1077 return fork_it(argv + optind);