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;
76 cpu_set_t *cpu_present_set, *cpu_mask;
77 size_t cpu_present_setsize, cpu_mask_size;
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;
106 int mark_cpu_present(int pkg, int core, int cpu)
108 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
113 * cpu_mask_init(ncpus)
115 * allocate and clear cpu_mask
118 void cpu_mask_init(int ncpus)
120 cpu_mask = CPU_ALLOC(ncpus);
121 if (cpu_mask == NULL) {
125 cpu_mask_size = CPU_ALLOC_SIZE(ncpus);
126 CPU_ZERO_S(cpu_mask_size, cpu_mask);
129 * Allocate and initialize cpu_present_set
131 cpu_present_set = CPU_ALLOC(ncpus);
132 if (cpu_present_set == NULL) {
136 cpu_present_setsize = CPU_ALLOC_SIZE(ncpus);
137 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
138 for_all_cpus(mark_cpu_present);
141 void cpu_mask_uninit()
146 CPU_FREE(cpu_present_set);
147 cpu_present_set = NULL;
148 cpu_present_setsize = 0;
151 int cpu_migrate(int cpu)
153 CPU_ZERO_S(cpu_mask_size, cpu_mask);
154 CPU_SET_S(cpu, cpu_mask_size, cpu_mask);
155 if (sched_setaffinity(0, cpu_mask_size, cpu_mask) == -1)
161 int get_msr(int cpu, off_t offset, unsigned long long *msr)
167 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
168 fd = open(pathname, O_RDONLY);
172 retval = pread(fd, msr, sizeof *msr, offset);
175 if (retval != sizeof *msr)
181 void print_header(void)
184 fprintf(stderr, "pk");
186 fprintf(stderr, " ");
188 fprintf(stderr, "cor");
190 fprintf(stderr, " CPU");
191 if (show_pkg || show_core || show_cpu)
192 fprintf(stderr, " ");
194 fprintf(stderr, " %%c0");
196 fprintf(stderr, " GHz");
197 fprintf(stderr, " TSC");
199 fprintf(stderr, " %%c1");
201 fprintf(stderr, " %%c3");
203 fprintf(stderr, " %%c6");
205 fprintf(stderr, " %%c7");
207 fprintf(stderr, " %%pc2");
209 fprintf(stderr, " %%pc3");
211 fprintf(stderr, " %%pc6");
213 fprintf(stderr, " %%pc7");
214 if (extra_msr_offset)
215 fprintf(stderr, " MSR 0x%x ", extra_msr_offset);
220 void dump_cnt(struct counters *cnt)
224 if (cnt->pkg) fprintf(stderr, "package: %d ", cnt->pkg);
225 if (cnt->core) fprintf(stderr, "core:: %d ", cnt->core);
226 if (cnt->cpu) fprintf(stderr, "CPU: %d ", cnt->cpu);
227 if (cnt->tsc) fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
228 if (cnt->c3) fprintf(stderr, "c3: %016llX\n", cnt->c3);
229 if (cnt->c6) fprintf(stderr, "c6: %016llX\n", cnt->c6);
230 if (cnt->c7) fprintf(stderr, "c7: %016llX\n", cnt->c7);
231 if (cnt->aperf) fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
232 if (cnt->pc2) fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
233 if (cnt->pc3) fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
234 if (cnt->pc6) fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
235 if (cnt->pc7) fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
236 if (cnt->extra_msr) fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
239 void dump_list(struct counters *cnt)
241 printf("dump_list 0x%p\n", cnt);
243 for (; cnt; cnt = cnt->next)
248 * column formatting convention & formats
249 * package: "pk" 2 columns %2d
250 * core: "cor" 3 columns %3d
251 * CPU: "CPU" 3 columns %3d
252 * GHz: "GHz" 3 columns %3.2
253 * TSC: "TSC" 3 columns %3.2
254 * percentage " %pc3" %6.2
256 void print_cnt(struct counters *p)
258 double interval_float;
260 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
262 /* topology columns, print blanks on 1st (average) line */
263 if (p == cnt_average) {
265 fprintf(stderr, " ");
266 if (show_pkg && show_core)
267 fprintf(stderr, " ");
269 fprintf(stderr, " ");
271 fprintf(stderr, " " " ");
274 fprintf(stderr, "%2d", p->pkg);
275 if (show_pkg && show_core)
276 fprintf(stderr, " ");
278 fprintf(stderr, "%3d", p->core);
280 fprintf(stderr, " %3d", p->cpu);
284 if (do_nhm_cstates) {
285 if (show_pkg || show_core || show_cpu)
286 fprintf(stderr, " ");
288 fprintf(stderr, "%6.2f", 100.0 * p->mperf/p->tsc);
290 fprintf(stderr, " ****");
295 if (!aperf_mperf_unstable) {
296 fprintf(stderr, " %3.2f",
297 1.0 * p->tsc / units * p->aperf /
298 p->mperf / interval_float);
300 if (p->aperf > p->tsc || p->mperf > p->tsc) {
301 fprintf(stderr, " ***");
303 fprintf(stderr, "%3.1f*",
306 p->mperf / interval_float);
312 fprintf(stderr, "%5.2f", 1.0 * p->tsc/units/interval_float);
314 if (do_nhm_cstates) {
316 fprintf(stderr, " %6.2f", 100.0 * p->c1/p->tsc);
318 fprintf(stderr, " ****");
321 fprintf(stderr, " %6.2f", 100.0 * p->c3/p->tsc);
323 fprintf(stderr, " %6.2f", 100.0 * p->c6/p->tsc);
325 fprintf(stderr, " %6.2f", 100.0 * p->c7/p->tsc);
327 fprintf(stderr, " %6.2f", 100.0 * p->pc2/p->tsc);
329 fprintf(stderr, " %6.2f", 100.0 * p->pc3/p->tsc);
331 fprintf(stderr, " %6.2f", 100.0 * p->pc6/p->tsc);
333 fprintf(stderr, " %6.2f", 100.0 * p->pc7/p->tsc);
334 if (extra_msr_offset)
335 fprintf(stderr, " 0x%016llx", p->extra_msr);
339 void print_counters(struct counters *counters)
341 struct counters *cnt;
345 if (!printed || !summary_only)
349 print_cnt(cnt_average);
356 for (cnt = counters; cnt != NULL; cnt = cnt->next)
361 #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
363 int compute_delta(struct counters *after,
364 struct counters *before, struct counters *delta)
369 skip_c0 = skip_c1 = 0;
371 for ( ; after && before && delta;
372 after = after->next, before = before->next, delta = delta->next) {
373 if (before->cpu != after->cpu) {
374 printf("cpu configuration changed: %d != %d\n",
375 before->cpu, after->cpu);
379 if (SUBTRACT_COUNTER(after->tsc, before->tsc, delta->tsc)) {
380 fprintf(stderr, "cpu%d TSC went backwards %llX to %llX\n",
381 before->cpu, before->tsc, after->tsc);
384 /* check for TSC < 1 Mcycles over interval */
385 if (delta->tsc < (1000 * 1000)) {
386 fprintf(stderr, "Insanely slow TSC rate,"
387 " TSC stops in idle?\n");
388 fprintf(stderr, "You can disable all c-states"
389 " by booting with \"idle=poll\"\n");
390 fprintf(stderr, "or just the deep ones with"
391 " \"processor.max_cstate=1\"\n");
394 if (SUBTRACT_COUNTER(after->c3, before->c3, delta->c3)) {
395 fprintf(stderr, "cpu%d c3 counter went backwards %llX to %llX\n",
396 before->cpu, before->c3, after->c3);
399 if (SUBTRACT_COUNTER(after->c6, before->c6, delta->c6)) {
400 fprintf(stderr, "cpu%d c6 counter went backwards %llX to %llX\n",
401 before->cpu, before->c6, after->c6);
404 if (SUBTRACT_COUNTER(after->c7, before->c7, delta->c7)) {
405 fprintf(stderr, "cpu%d c7 counter went backwards %llX to %llX\n",
406 before->cpu, before->c7, after->c7);
409 if (SUBTRACT_COUNTER(after->pc2, before->pc2, delta->pc2)) {
410 fprintf(stderr, "cpu%d pc2 counter went backwards %llX to %llX\n",
411 before->cpu, before->pc2, after->pc2);
414 if (SUBTRACT_COUNTER(after->pc3, before->pc3, delta->pc3)) {
415 fprintf(stderr, "cpu%d pc3 counter went backwards %llX to %llX\n",
416 before->cpu, before->pc3, after->pc3);
419 if (SUBTRACT_COUNTER(after->pc6, before->pc6, delta->pc6)) {
420 fprintf(stderr, "cpu%d pc6 counter went backwards %llX to %llX\n",
421 before->cpu, before->pc6, after->pc6);
424 if (SUBTRACT_COUNTER(after->pc7, before->pc7, delta->pc7)) {
425 fprintf(stderr, "cpu%d pc7 counter went backwards %llX to %llX\n",
426 before->cpu, before->pc7, after->pc7);
430 perf_err = SUBTRACT_COUNTER(after->aperf, before->aperf, delta->aperf);
432 fprintf(stderr, "cpu%d aperf counter went backwards %llX to %llX\n",
433 before->cpu, before->aperf, after->aperf);
435 perf_err |= SUBTRACT_COUNTER(after->mperf, before->mperf, delta->mperf);
437 fprintf(stderr, "cpu%d mperf counter went backwards %llX to %llX\n",
438 before->cpu, before->mperf, after->mperf);
441 if (!aperf_mperf_unstable) {
442 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
443 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
444 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
446 aperf_mperf_unstable = 1;
449 * mperf delta is likely a huge "positive" number
450 * can not use it for calculating c0 time
457 * As mperf and tsc collection are not atomic,
458 * it is possible for mperf's non-halted cycles
459 * to exceed TSC's all cycles: show c1 = 0% in that case.
461 if (delta->mperf > delta->tsc)
463 else /* normal case, derive c1 */
464 delta->c1 = delta->tsc - delta->mperf
465 - delta->c3 - delta->c6 - delta->c7;
467 if (delta->mperf == 0)
468 delta->mperf = 1; /* divide by 0 protection */
471 * for "extra msr", just copy the latest w/o subtracting
473 delta->extra_msr = after->extra_msr;
475 fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
477 fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
485 void compute_average(struct counters *delta, struct counters *avg)
487 struct counters *sum;
489 sum = calloc(1, sizeof(struct counters));
491 perror("calloc sum");
495 for (; delta; delta = delta->next) {
496 sum->tsc += delta->tsc;
497 sum->c1 += delta->c1;
498 sum->c3 += delta->c3;
499 sum->c6 += delta->c6;
500 sum->c7 += delta->c7;
501 sum->aperf += delta->aperf;
502 sum->mperf += delta->mperf;
503 sum->pc2 += delta->pc2;
504 sum->pc3 += delta->pc3;
505 sum->pc6 += delta->pc6;
506 sum->pc7 += delta->pc7;
508 avg->tsc = sum->tsc/num_cpus;
509 avg->c1 = sum->c1/num_cpus;
510 avg->c3 = sum->c3/num_cpus;
511 avg->c6 = sum->c6/num_cpus;
512 avg->c7 = sum->c7/num_cpus;
513 avg->aperf = sum->aperf/num_cpus;
514 avg->mperf = sum->mperf/num_cpus;
515 avg->pc2 = sum->pc2/num_cpus;
516 avg->pc3 = sum->pc3/num_cpus;
517 avg->pc6 = sum->pc6/num_cpus;
518 avg->pc7 = sum->pc7/num_cpus;
523 int get_counters(struct counters *cnt)
525 for ( ; cnt; cnt = cnt->next) {
527 if (cpu_migrate(cnt->cpu))
530 if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc))
534 if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf))
536 if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf))
540 if (do_nhm_cstates) {
541 if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3))
543 if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6))
548 if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7))
551 if (do_nhm_cstates) {
552 if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3))
554 if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6))
557 if (do_snb_cstates) {
558 if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2))
560 if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7))
563 if (extra_msr_offset)
564 if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr))
570 void print_nehalem_info(void)
572 unsigned long long msr;
575 if (!do_nehalem_platform_info)
578 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
580 ratio = (msr >> 40) & 0xFF;
581 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
582 ratio, bclk, ratio * bclk);
584 ratio = (msr >> 8) & 0xFF;
585 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
586 ratio, bclk, ratio * bclk);
589 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
591 if (!do_nehalem_turbo_ratio_limit)
594 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
596 ratio = (msr >> 24) & 0xFF;
598 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
599 ratio, bclk, ratio * bclk);
601 ratio = (msr >> 16) & 0xFF;
603 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
604 ratio, bclk, ratio * bclk);
606 ratio = (msr >> 8) & 0xFF;
608 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
609 ratio, bclk, ratio * bclk);
611 ratio = (msr >> 0) & 0xFF;
613 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
614 ratio, bclk, ratio * bclk);
618 void free_counter_list(struct counters *list)
622 for (p = list; p; ) {
623 struct counters *free_me;
631 void free_all_counters(void)
633 free_counter_list(cnt_even);
636 free_counter_list(cnt_odd);
639 free_counter_list(cnt_delta);
642 free_counter_list(cnt_average);
646 void insert_counters(struct counters **list,
647 struct counters *new)
649 struct counters *prev;
661 show_cpu = 1; /* there is more than one CPU */
664 * insert on front of list.
665 * It is sorted by ascending package#, core#, cpu#
667 if (((*list)->pkg > new->pkg) ||
668 (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
669 (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
677 while (prev->next && (prev->next->pkg < new->pkg)) {
680 show_pkg = 1; /* there is more than 1 package */
683 while (prev->next && (prev->next->pkg == new->pkg)
684 && (prev->next->core < new->core)) {
687 show_core = 1; /* there is more than 1 core */
690 while (prev->next && (prev->next->pkg == new->pkg)
691 && (prev->next->core == new->core)
692 && (prev->next->cpu < new->cpu)) {
697 * insert after "prev"
699 new->next = prev->next;
703 void alloc_new_counters(int pkg, int core, int cpu)
705 struct counters *new;
708 printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
710 new = (struct counters *)calloc(1, sizeof(struct counters));
718 insert_counters(&cnt_odd, new);
720 new = (struct counters *)calloc(1,
721 sizeof(struct counters));
729 insert_counters(&cnt_even, new);
731 new = (struct counters *)calloc(1, sizeof(struct counters));
739 insert_counters(&cnt_delta, new);
741 new = (struct counters *)calloc(1, sizeof(struct counters));
752 int get_physical_package_id(int cpu)
758 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
759 filep = fopen(path, "r");
764 fscanf(filep, "%d", &pkg);
769 int get_core_id(int cpu)
775 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
776 filep = fopen(path, "r");
781 fscanf(filep, "%d", &core);
787 * run func(pkg, core, cpu) on every cpu in /proc/stat
790 int for_all_cpus(void (func)(int, int, int))
796 fp = fopen(proc_stat, "r");
802 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
804 perror("/proc/stat format");
808 for (cpu_count = 0; ; cpu_count++) {
811 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
815 func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
821 void re_initialize(void)
824 num_cpus = for_all_cpus(alloc_new_counters);
826 cpu_mask_init(num_cpus);
827 printf("turbostat: re-initialized with num_cpus %d\n", num_cpus);
830 void dummy(int pkg, int core, int cpu) { return; }
832 * check to see if a cpu came on-line
834 int verify_num_cpus(void)
838 new_num_cpus = for_all_cpus(dummy);
840 if (new_num_cpus != num_cpus) {
842 printf("num_cpus was %d, is now %d\n",
843 num_cpus, new_num_cpus);
849 void turbostat_loop()
852 get_counters(cnt_even);
853 gettimeofday(&tv_even, (struct timezone *)NULL);
856 if (verify_num_cpus()) {
861 if (get_counters(cnt_odd)) {
865 gettimeofday(&tv_odd, (struct timezone *)NULL);
866 compute_delta(cnt_odd, cnt_even, cnt_delta);
867 timersub(&tv_odd, &tv_even, &tv_delta);
868 compute_average(cnt_delta, cnt_average);
869 print_counters(cnt_delta);
871 if (get_counters(cnt_even)) {
875 gettimeofday(&tv_even, (struct timezone *)NULL);
876 compute_delta(cnt_even, cnt_odd, cnt_delta);
877 timersub(&tv_even, &tv_odd, &tv_delta);
878 compute_average(cnt_delta, cnt_average);
879 print_counters(cnt_delta);
887 if (stat("/dev/cpu/0/msr", &sb)) {
888 fprintf(stderr, "no /dev/cpu/0/msr\n");
889 fprintf(stderr, "Try \"# modprobe msr\"\n");
894 void check_super_user()
897 fprintf(stderr, "must be root\n");
902 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
911 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
912 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
913 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
914 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
915 case 0x2C: /* Westmere EP - Gulftown */
917 case 0x2D: /* SNB Xeon */
919 case 0x3D: /* IVB Xeon */
921 case 0x2E: /* Nehalem-EX Xeon - Beckton */
922 case 0x2F: /* Westmere-EX Xeon - Eagleton */
928 int is_snb(unsigned int family, unsigned int model)
937 case 0x3D: /* IVB Xeon */
943 double discover_bclk(unsigned int family, unsigned int model)
945 if (is_snb(family, model))
953 unsigned int eax, ebx, ecx, edx, max_level;
954 unsigned int fms, family, model, stepping;
956 eax = ebx = ecx = edx = 0;
958 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
960 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
964 fprintf(stderr, "%.4s%.4s%.4s ",
965 (char *)&ebx, (char *)&edx, (char *)&ecx);
967 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
968 family = (fms >> 8) & 0xf;
969 model = (fms >> 4) & 0xf;
970 stepping = fms & 0xf;
971 if (family == 6 || family == 0xf)
972 model += ((fms >> 16) & 0xf) << 4;
975 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
976 max_level, family, model, stepping, family, model, stepping);
978 if (!(edx & (1 << 5))) {
979 fprintf(stderr, "CPUID: no MSR\n");
984 * check max extended function levels of CPUID.
985 * This is needed to check for invariant TSC.
986 * This check is valid for both Intel and AMD.
989 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
991 if (max_level < 0x80000007) {
992 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
997 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
998 * this check is valid for both Intel and AMD
1000 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
1001 has_invariant_tsc = edx & (1 << 8);
1003 if (!has_invariant_tsc) {
1004 fprintf(stderr, "No invariant TSC\n");
1009 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1010 * this check is valid for both Intel and AMD
1013 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
1014 has_aperf = ecx & (1 << 0);
1016 fprintf(stderr, "No APERF MSR\n");
1020 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1021 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1022 do_snb_cstates = is_snb(family, model);
1023 bclk = discover_bclk(family, model);
1025 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1031 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1038 * in /dev/cpu/ return success for names that are numbers
1039 * ie. filter out ".", "..", "microcode".
1041 int dir_filter(const struct dirent *dirp)
1043 if (isdigit(dirp->d_name[0]))
1049 int open_dev_cpu_msr(int dummy1)
1054 void turbostat_init()
1061 num_cpus = for_all_cpus(alloc_new_counters);
1062 cpu_mask_init(num_cpus);
1065 print_nehalem_info();
1068 int fork_it(char **argv)
1072 get_counters(cnt_even);
1074 /* clear affinity side-effect of get_counters() */
1075 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
1076 gettimeofday(&tv_even, (struct timezone *)NULL);
1081 execvp(argv[0], argv);
1086 if (child_pid == -1) {
1091 signal(SIGINT, SIG_IGN);
1092 signal(SIGQUIT, SIG_IGN);
1093 if (waitpid(child_pid, &status, 0) == -1) {
1098 get_counters(cnt_odd);
1099 gettimeofday(&tv_odd, (struct timezone *)NULL);
1100 retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
1102 timersub(&tv_odd, &tv_even, &tv_delta);
1103 compute_average(cnt_delta, cnt_average);
1105 print_counters(cnt_delta);
1107 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1112 void cmdline(int argc, char **argv)
1118 while ((opt = getopt(argc, argv, "+svi:M:")) != -1) {
1127 interval_sec = atoi(optarg);
1130 sscanf(optarg, "%x", &extra_msr_offset);
1132 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1140 int main(int argc, char **argv)
1142 cmdline(argc, argv);
1145 fprintf(stderr, "turbostat Dec 6, 2010"
1146 " - Len Brown <lenb@kernel.org>\n");
1148 fprintf(stderr, "http://userweb.kernel.org/~lenb/acpi/utils/pmtools/turbostat/\n");
1153 * if any params left, it must be a command to fork
1156 return fork_it(argv + optind);