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.
25 #include <sys/types.h>
28 #include <sys/resource.h>
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 */
51 char *proc_stat = "/proc/stat";
52 unsigned int interval_sec = 5; /* set with -i interval_sec */
53 unsigned int verbose; /* set with -v */
54 unsigned int summary_only; /* set with -s */
57 unsigned int do_nhm_cstates;
58 unsigned int do_snb_cstates;
59 unsigned int has_aperf;
60 unsigned int units = 1000000000; /* Ghz etc */
61 unsigned int genuine_intel;
62 unsigned int has_invariant_tsc;
63 unsigned int do_nehalem_platform_info;
64 unsigned int do_nehalem_turbo_ratio_limit;
65 unsigned int extra_msr_offset;
67 unsigned int show_pkg;
68 unsigned int show_core;
69 unsigned int show_cpu;
71 int aperf_mperf_unstable;
80 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 */
95 struct counters *next;
98 struct counters *cnt_even;
99 struct counters *cnt_odd;
100 struct counters *cnt_delta;
101 struct counters *cnt_average;
102 struct timeval tv_even;
103 struct timeval tv_odd;
104 struct timeval tv_delta;
107 * cpu_mask_init(ncpus)
109 * allocate and clear cpu_mask
112 void cpu_mask_init(int ncpus)
114 cpu_mask = CPU_ALLOC(ncpus);
115 if (cpu_mask == NULL) {
119 cpu_mask_size = CPU_ALLOC_SIZE(ncpus);
120 CPU_ZERO_S(cpu_mask_size, cpu_mask);
123 void cpu_mask_uninit()
130 int cpu_migrate(int cpu)
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)
140 int get_msr(int cpu, off_t offset, unsigned long long *msr)
146 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
147 fd = open(pathname, O_RDONLY);
151 retval = pread(fd, msr, sizeof *msr, offset);
154 if (retval != sizeof *msr)
160 void print_header(void)
163 fprintf(stderr, "pk");
165 fprintf(stderr, " ");
167 fprintf(stderr, "cor");
169 fprintf(stderr, " CPU");
170 if (show_pkg || show_core || show_cpu)
171 fprintf(stderr, " ");
173 fprintf(stderr, " %%c0");
175 fprintf(stderr, " GHz");
176 fprintf(stderr, " TSC");
178 fprintf(stderr, " %%c1");
180 fprintf(stderr, " %%c3");
182 fprintf(stderr, " %%c6");
184 fprintf(stderr, " %%c7");
186 fprintf(stderr, " %%pc2");
188 fprintf(stderr, " %%pc3");
190 fprintf(stderr, " %%pc6");
192 fprintf(stderr, " %%pc7");
193 if (extra_msr_offset)
194 fprintf(stderr, " MSR 0x%x ", extra_msr_offset);
199 void dump_cnt(struct counters *cnt)
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);
218 void dump_list(struct counters *cnt)
220 printf("dump_list 0x%p\n", cnt);
222 for (; cnt; cnt = cnt->next)
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
235 void print_cnt(struct counters *p)
237 double interval_float;
239 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
241 /* topology columns, print blanks on 1st (average) line */
242 if (p == cnt_average) {
244 fprintf(stderr, " ");
245 if (show_pkg && show_core)
246 fprintf(stderr, " ");
248 fprintf(stderr, " ");
250 fprintf(stderr, " " " ");
253 fprintf(stderr, "%2d", p->pkg);
254 if (show_pkg && show_core)
255 fprintf(stderr, " ");
257 fprintf(stderr, "%3d", p->core);
259 fprintf(stderr, " %3d", p->cpu);
263 if (do_nhm_cstates) {
264 if (show_pkg || show_core || show_cpu)
265 fprintf(stderr, " ");
267 fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc);
269 fprintf(stderr, " ****");
274 if (!aperf_mperf_unstable) {
275 fprintf(stderr, " %3.2f",
276 1.0 * p->tsc / units * p->aperf /
277 p->mperf / interval_float);
279 if (p->aperf > p->tsc || p->mperf > p->tsc) {
280 fprintf(stderr, " ***");
282 fprintf(stderr, "%3.1f*",
285 p->mperf / interval_float);
291 fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);
293 if (do_nhm_cstates) {
295 fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc);
297 fprintf(stderr, " ****");
300 fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc);
302 fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc);
304 fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc);
306 fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc);
308 fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc);
310 fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc);
312 fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc);
313 if (extra_msr_offset)
314 fprintf(stderr, " 0x%016llx", p->extra_msr);
318 void print_counters(struct counters *counters)
320 struct counters *cnt;
324 if (!printed || !summary_only)
328 print_cnt(cnt_average);
335 for (cnt = counters; cnt != NULL; cnt = cnt->next)
340 #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
342 int compute_delta(struct counters *after,
343 struct counters *before, struct counters *delta)
348 skip_c0 = skip_c1 = 0;
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);
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);
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");
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);
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);
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);
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);
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);
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);
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);
409 perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
411 fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
412 before->cpu, before->aperf, after->aperf);
414 perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
416 fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
417 before->cpu, before->mperf, after->mperf);
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");
425 aperf_mperf_unstable = 1;
428 * mperf delta is likely a huge "positive" number
429 * can not use it for calculating c0 time
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.
440 if (delta->mperf > delta->tsc)
442 else /* normal case, derive c1 */
443 delta->c1 = delta->tsc - delta->mperf
444 - delta->c3 - delta->c6 - delta->c7;
446 if (delta->mperf == 0)
447 delta->mperf = 1; /* divide by 0 protection */
450 * for "extra msr", just copy the latest w/o subtracting
452 delta->extra_msr = after->extra_msr;
454 fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
456 fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
464 void compute_average(struct counters *delta, struct counters *avg)
466 struct counters *sum;
468 sum = calloc(1, sizeof(struct counters));
470 perror("calloc sum");
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;
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;
502 int get_counters(struct counters *cnt)
504 for ( ; cnt; cnt = cnt->next) {
506 if (cpu_migrate(cnt->cpu))
509 if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc))
513 if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf))
515 if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf))
519 if (do_nhm_cstates) {
520 if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3))
522 if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6))
527 if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7))
530 if (do_nhm_cstates) {
531 if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3))
533 if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6))
536 if (do_snb_cstates) {
537 if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2))
539 if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7))
542 if (extra_msr_offset)
543 if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr))
549 void print_nehalem_info(void)
551 unsigned long long msr;
554 if (!do_nehalem_platform_info)
557 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
559 ratio = (msr >> 40) & 0xFF;
560 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
561 ratio, bclk, ratio * bclk);
563 ratio = (msr >> 8) & 0xFF;
564 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
565 ratio, bclk, ratio * bclk);
568 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
570 if (!do_nehalem_turbo_ratio_limit)
573 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
575 ratio = (msr >> 24) & 0xFF;
577 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
578 ratio, bclk, ratio * bclk);
580 ratio = (msr >> 16) & 0xFF;
582 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
583 ratio, bclk, ratio * bclk);
585 ratio = (msr >> 8) & 0xFF;
587 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
588 ratio, bclk, ratio * bclk);
590 ratio = (msr >> 0) & 0xFF;
592 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
593 ratio, bclk, ratio * bclk);
597 void free_counter_list(struct counters *list)
601 for (p = list; p; ) {
602 struct counters *free_me;
610 void free_all_counters(void)
612 free_counter_list(cnt_even);
615 free_counter_list(cnt_odd);
618 free_counter_list(cnt_delta);
621 free_counter_list(cnt_average);
625 void insert_counters(struct counters **list,
626 struct counters *new)
628 struct counters *prev;
640 show_cpu = 1; /* there is more than one CPU */
643 * insert on front of list.
644 * It is sorted by ascending package#, core#, cpu#
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))) {
656 while (prev->next && (prev->next->pkg < new->pkg)) {
659 show_pkg = 1; /* there is more than 1 package */
662 while (prev->next && (prev->next->pkg == new->pkg)
663 && (prev->next->core < new->core)) {
666 show_core = 1; /* there is more than 1 core */
669 while (prev->next && (prev->next->pkg == new->pkg)
670 && (prev->next->core == new->core)
671 && (prev->next->cpu < new->cpu)) {
676 * insert after "prev"
678 new->next = prev->next;
682 void alloc_new_counters(int pkg, int core, int cpu)
684 struct counters *new;
687 printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
689 new = (struct counters *)calloc(1, sizeof(struct counters));
697 insert_counters(&cnt_odd, new);
699 new = (struct counters *)calloc(1,
700 sizeof(struct counters));
708 insert_counters(&cnt_even, new);
710 new = (struct counters *)calloc(1, sizeof(struct counters));
718 insert_counters(&cnt_delta, new);
720 new = (struct counters *)calloc(1, sizeof(struct counters));
731 int get_physical_package_id(int cpu)
737 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
738 filep = fopen(path, "r");
743 fscanf(filep, "%d", &pkg);
748 int get_core_id(int cpu)
754 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
755 filep = fopen(path, "r");
760 fscanf(filep, "%d", &core);
766 * run func(pkg, core, cpu) on every cpu in /proc/stat
769 int for_all_cpus(void (func)(int, int, int))
775 fp = fopen(proc_stat, "r");
781 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
783 perror("/proc/stat format");
787 for (cpu_count = 0; ; cpu_count++) {
790 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
794 func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
800 void re_initialize(void)
803 num_cpus = for_all_cpus(alloc_new_counters);
805 cpu_mask_init(num_cpus);
806 printf("turbostat: re-initialized with num_cpus %d\n", num_cpus);
809 void dummy(int pkg, int core, int cpu) { return; }
811 * check to see if a cpu came on-line
813 int verify_num_cpus(void)
817 new_num_cpus = for_all_cpus(dummy);
819 if (new_num_cpus != num_cpus) {
821 printf("num_cpus was %d, is now %d\n",
822 num_cpus, new_num_cpus);
828 void turbostat_loop()
831 get_counters(cnt_even);
832 gettimeofday(&tv_even, (struct timezone *)NULL);
835 if (verify_num_cpus()) {
840 if (get_counters(cnt_odd)) {
844 gettimeofday(&tv_odd, (struct timezone *)NULL);
845 compute_delta(cnt_odd, cnt_even, cnt_delta);
846 timersub(&tv_odd, &tv_even, &tv_delta);
847 compute_average(cnt_delta, cnt_average);
848 print_counters(cnt_delta);
850 if (get_counters(cnt_even)) {
854 gettimeofday(&tv_even, (struct timezone *)NULL);
855 compute_delta(cnt_even, cnt_odd, cnt_delta);
856 timersub(&tv_even, &tv_odd, &tv_delta);
857 compute_average(cnt_delta, cnt_average);
858 print_counters(cnt_delta);
866 if (stat("/dev/cpu/0/msr", &sb)) {
867 fprintf(stderr, "no /dev/cpu/0/msr\n");
868 fprintf(stderr, "Try \"# modprobe msr\"\n");
873 void check_super_user()
876 fprintf(stderr, "must be root\n");
881 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int 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 */
896 case 0x2D: /* SNB Xeon */
898 case 0x3D: /* IVB Xeon */
900 case 0x2E: /* Nehalem-EX Xeon - Beckton */
901 case 0x2F: /* Westmere-EX Xeon - Eagleton */
907 int is_snb(unsigned int family, unsigned int model)
920 double discover_bclk(unsigned int family, unsigned int model)
922 if (is_snb(family, model))
930 unsigned int eax, ebx, ecx, edx, max_level;
931 unsigned int fms, family, model, stepping;
933 eax = ebx = ecx = edx = 0;
935 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
937 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
941 fprintf(stderr, "%.4s%.4s%.4s ",
942 (char *)&ebx, (char *)&edx, (char *)&ecx);
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;
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);
955 if (!(edx & (1 << 5))) {
956 fprintf(stderr, "CPUID: no MSR\n");
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.
966 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
968 if (max_level < 0x80000007) {
969 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
974 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
975 * this check is valid for both Intel and AMD
977 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
978 has_invariant_tsc = edx & (1 << 8);
980 if (!has_invariant_tsc) {
981 fprintf(stderr, "No invariant TSC\n");
986 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
987 * this check is valid for both Intel and AMD
990 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
991 has_aperf = ecx & (1 << 0);
993 fprintf(stderr, "No APERF MSR\n");
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);
1002 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1008 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1015 * in /dev/cpu/ return success for names that are numbers
1016 * ie. filter out ".", "..", "microcode".
1018 int dir_filter(const struct dirent *dirp)
1020 if (isdigit(dirp->d_name[0]))
1026 int open_dev_cpu_msr(int dummy1)
1031 void turbostat_init()
1038 num_cpus = for_all_cpus(alloc_new_counters);
1039 cpu_mask_init(num_cpus);
1042 print_nehalem_info();
1045 int fork_it(char **argv)
1049 get_counters(cnt_even);
1050 gettimeofday(&tv_even, (struct timezone *)NULL);
1055 execvp(argv[0], argv);
1060 if (child_pid == -1) {
1065 signal(SIGINT, SIG_IGN);
1066 signal(SIGQUIT, SIG_IGN);
1067 if (waitpid(child_pid, &status, 0) == -1) {
1072 get_counters(cnt_odd);
1073 gettimeofday(&tv_odd, (struct timezone *)NULL);
1074 retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
1076 timersub(&tv_odd, &tv_even, &tv_delta);
1077 compute_average(cnt_delta, cnt_average);
1079 print_counters(cnt_delta);
1081 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1086 void cmdline(int argc, char **argv)
1092 while ((opt = getopt(argc, argv, "+svi:M:")) != -1) {
1101 interval_sec = atoi(optarg);
1104 sscanf(optarg, "%x", &extra_msr_offset);
1106 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1114 int main(int argc, char **argv)
1116 cmdline(argc, argv);
1119 fprintf(stderr, "turbostat Dec 6, 2010"
1120 " - Len Brown <lenb@kernel.org>\n");
1122 fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");
1127 * if any params left, it must be a command to fork
1130 return fork_it(argv + optind);