25 #include <sys/types.h>
28 #include <sys/resource.h>
38 #define MSR_NEHALEM_PLATFORM_INFO 0xCE
39 #define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD
40 #define MSR_IVT_TURBO_RATIO_LIMIT 0x1AE
41 #define MSR_APERF 0xE8
42 #define MSR_MPERF 0xE7
43 #define MSR_PKG_C2_RESIDENCY 0x60D
44 #define MSR_PKG_C3_RESIDENCY 0x3F8
45 #define MSR_PKG_C6_RESIDENCY 0x3F9
46 #define MSR_PKG_C7_RESIDENCY 0x3FA
47 #define MSR_CORE_C3_RESIDENCY 0x3FC
48 #define MSR_CORE_C6_RESIDENCY 0x3FD
49 #define MSR_CORE_C7_RESIDENCY 0x3FE
60 unsigned int units = 1000000000;
86 unsigned long long tsc;
89 unsigned long long c1;
96 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
97 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
101 unsigned long long c3;
102 unsigned long long c6;
103 unsigned long long c7;
115 #define ODD_COUNTERS thread_odd, core_odd, package_odd
116 #define EVEN_COUNTERS thread_even, core_even, package_even
118 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
119 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
120 topo.num_threads_per_core + \
121 (core_no) * topo.num_threads_per_core + (thread_no))
122 #define GET_CORE(core_base, core_no, pkg_no) \
123 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
124 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
158 int retval, pkg_no, core_no, thread_no;
160 for (pkg_no = 0; pkg_no <
topo.num_packages; ++pkg_no) {
161 for (core_no = 0; core_no <
topo.num_cores_per_pkg; ++core_no) {
162 for (thread_no = 0; thread_no <
163 topo.num_threads_per_core; ++thread_no) {
168 t =
GET_THREAD(thread_base, thread_no, core_no, pkg_no);
173 c =
GET_CORE(core_base, core_no, pkg_no);
176 retval =
func(t, c, p);
201 sprintf(pathname,
"/dev/cpu/%d/msr", cpu);
206 retval = pread(fd, msr,
sizeof *msr, offset);
209 if (retval !=
sizeof *msr) {
210 fprintf(stderr,
"%s offset 0x%zx read failed\n", pathname, offset);
265 fprintf(stderr,
"t %p, c %p, p %p\n", t, c, p);
273 fprintf(stderr,
"msr0x%x: %08llX\n",
275 fprintf(stderr,
"msr0x%x: %016llX\n",
277 fprintf(stderr,
"msr0x%x: %08llX\n",
279 fprintf(stderr,
"msr0x%x: %016llX\n",
312 double interval_float;
325 if (t == &average.threads) {
368 t->
mperf / interval_float);
376 t->
mperf / interval_float);
452 if (
topo.num_cpus > 1)
467 old->
pc2 =
new->pc2 - old->
pc2;
468 old->
pc3 =
new->pc3 - old->
pc3;
469 old->
pc6 =
new->pc6 - old->
pc6;
470 old->
pc7 =
new->pc7 - old->
pc7;
476 old->
c3 =
new->c3 - old->
c3;
477 old->
c6 =
new->c6 - old->
c6;
478 old->
c7 =
new->c7 - old->
c7;
488 old->
tsc =
new->tsc - old->
tsc;
491 if (old->
tsc < (1000 * 1000)) {
492 fprintf(stderr,
"Insanely slow TSC rate, TSC stops in idle?\n");
493 fprintf(stderr,
"You can disable all c-states by booting with \"idle=poll\"\n");
494 fprintf(stderr,
"or just the deep ones with \"processor.max_cstate=1\"\n");
498 old->
c1 =
new->c1 - old->
c1;
500 if ((new->aperf > old->
aperf) && (new->mperf > old->
mperf)) {
507 fprintf(stderr,
"* Frequency results do not cover entire interval *\n");
508 fprintf(stderr,
"* fix this by running Linux-2.6.30 or later *\n");
526 if ((old->
mperf + core_delta->
c3 + core_delta->
c6 + core_delta->
c7) > old->
tsc)
531 - core_delta->
c6 - core_delta->
c7;
534 if (old->
mperf == 0) {
594 average.threads.tsc += t->
tsc;
595 average.threads.aperf += t->
aperf;
596 average.threads.mperf += t->
mperf;
597 average.threads.c1 += t->
c1;
606 average.cores.c3 += c->
c3;
607 average.cores.c6 += c->
c6;
608 average.cores.c7 += c->
c7;
614 average.packages.pc2 += p->
pc2;
615 average.packages.pc3 += p->
pc3;
616 average.packages.pc6 += p->
pc6;
617 average.packages.pc7 += p->
pc7;
628 clear_counters(&average.threads, &average.cores, &average.packages);
632 average.threads.tsc /=
topo.num_cpus;
633 average.threads.aperf /=
topo.num_cpus;
634 average.threads.mperf /=
topo.num_cpus;
635 average.threads.c1 /=
topo.num_cpus;
637 average.threads.extra_delta32 /=
topo.num_cpus;
638 average.threads.extra_delta32 &= 0xFFFFFFFF;
640 average.threads.extra_delta64 /=
topo.num_cpus;
642 average.cores.c3 /=
topo.num_cores;
643 average.cores.c6 /=
topo.num_cores;
644 average.cores.c7 /=
topo.num_cores;
646 average.packages.pc2 /=
topo.num_packages;
647 average.packages.pc3 /=
topo.num_packages;
648 average.packages.pc6 /=
topo.num_packages;
649 average.packages.pc7 /=
topo.num_packages;
652 static unsigned long long rdtsc(
void)
656 asm volatile(
"rdtsc" :
"=a" (
low),
"=d" (high));
658 return low | ((
unsigned long long)high) << 32;
739 unsigned long long msr;
748 fprintf(stderr,
"MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
750 ratio = (msr >> 40) & 0xFF;
751 fprintf(stderr,
"%d * %.0f = %.0f MHz max efficiency\n",
754 ratio = (msr >> 8) & 0xFF;
755 fprintf(stderr,
"%d * %.0f = %.0f MHz TSC frequency\n",
756 ratio, bclk, ratio * bclk);
759 goto print_nhm_turbo_ratio_limits;
764 fprintf(stderr,
"MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
766 ratio = (msr >> 56) & 0xFF;
768 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 16 active cores\n",
769 ratio, bclk, ratio * bclk);
771 ratio = (msr >> 48) & 0xFF;
773 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 15 active cores\n",
774 ratio, bclk, ratio * bclk);
776 ratio = (msr >> 40) & 0xFF;
778 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 14 active cores\n",
779 ratio, bclk, ratio * bclk);
781 ratio = (msr >> 32) & 0xFF;
783 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 13 active cores\n",
784 ratio, bclk, ratio * bclk);
786 ratio = (msr >> 24) & 0xFF;
788 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 12 active cores\n",
789 ratio, bclk, ratio * bclk);
791 ratio = (msr >> 16) & 0xFF;
793 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 11 active cores\n",
794 ratio, bclk, ratio * bclk);
796 ratio = (msr >> 8) & 0xFF;
798 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 10 active cores\n",
799 ratio, bclk, ratio * bclk);
801 ratio = (msr >> 0) & 0xFF;
803 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 9 active cores\n",
804 ratio, bclk, ratio * bclk);
806 print_nhm_turbo_ratio_limits:
814 fprintf(stderr,
"MSR_NEHALEM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
816 ratio = (msr >> 56) & 0xFF;
818 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 8 active cores\n",
819 ratio, bclk, ratio * bclk);
821 ratio = (msr >> 48) & 0xFF;
823 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 7 active cores\n",
824 ratio, bclk, ratio * bclk);
826 ratio = (msr >> 40) & 0xFF;
828 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 6 active cores\n",
829 ratio, bclk, ratio * bclk);
831 ratio = (msr >> 32) & 0xFF;
833 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 5 active cores\n",
834 ratio, bclk, ratio * bclk);
836 ratio = (msr >> 24) & 0xFF;
838 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 4 active cores\n",
839 ratio, bclk, ratio * bclk);
841 ratio = (msr >> 16) & 0xFF;
843 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 3 active cores\n",
844 ratio, bclk, ratio * bclk);
846 ratio = (msr >> 8) & 0xFF;
848 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 2 active cores\n",
849 ratio, bclk, ratio * bclk);
851 ratio = (msr >> 0) & 0xFF;
853 fprintf(stderr,
"%d * %.0f = %.0f MHz max turbo 1 active cores\n",
854 ratio, bclk, ratio * bclk);
898 sprintf(path,
"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
899 filep = fopen(path,
"r");
904 fscanf(filep,
"%d", &first_cpu);
906 return (cpu == first_cpu);
919 sprintf(path,
"/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
920 filep = fopen(path,
"r");
925 fscanf(filep,
"%d", &first_cpu);
927 return (cpu == first_cpu);
936 sprintf(path,
"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
937 filep = fopen(path,
"r");
942 fscanf(filep,
"%d", &pkg);
953 sprintf(path,
"/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
954 filep = fopen(path,
"r");
959 fscanf(filep,
"%d", &core);
972 sprintf(path,
"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
973 filep = fopen(path,
"r");
983 matches = fscanf(filep,
"%d%c%d\n", &sib1, &character, &sib2);
1005 int retval, pkg_no, core_no, thread_no;
1007 for (pkg_no = 0; pkg_no <
topo.num_packages; ++pkg_no) {
1008 for (core_no = 0; core_no <
topo.num_cores_per_pkg; ++core_no) {
1009 for (thread_no = 0; thread_no <
1010 topo.num_threads_per_core; ++thread_no) {
1015 t =
GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1020 t2 =
GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1022 c =
GET_CORE(core_base, core_no, pkg_no);
1023 c2 =
GET_CORE(core_base2, core_no, pkg_no);
1025 p =
GET_PKG(pkg_base, pkg_no);
1026 p2 =
GET_PKG(pkg_base2, pkg_no);
1028 retval =
func(t, c, p, t2, c2, p2);
1053 retval = fscanf(fp,
"cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1055 perror(
"/proc/stat format");
1060 retval = fscanf(fp,
"cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1064 retval =
func(cpu_num);
1078 printf(
"turbostat: re-initialized with num_cpus %d\n",
topo.num_cpus);
1088 if (
topo.max_cpu_num < cpu)
1108 }
else if (retval == -1) {
1123 }
else if (retval == -1) {
1128 timersub(&tv_odd, &tv_even, &
tv_delta);
1137 }
else if (retval == -1) {
1142 timersub(&tv_even, &tv_odd, &
tv_delta);
1154 if (
stat(
"/dev/cpu/0/msr", &sb)) {
1155 fprintf(stderr,
"no /dev/cpu/0/msr\n");
1156 fprintf(stderr,
"Try \"# modprobe msr\"\n");
1163 if (getuid() != 0) {
1164 fprintf(stderr,
"must be root\n");
1228 if (
is_snb(family, model))
1239 eax = ebx = ecx = edx = 0;
1241 asm(
"cpuid" :
"=a" (
max_level),
"=b" (ebx),
"=c" (
ecx),
"=d" (edx) :
"a" (0));
1243 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1247 fprintf(stderr,
"%.4s%.4s%.4s ",
1248 (
char *)&ebx, (
char *)&edx, (
char *)&ecx);
1250 asm(
"cpuid" :
"=a" (fms),
"=c" (ecx),
"=d" (edx) :
"a" (1) :
"ebx");
1251 family = (fms >> 8) & 0xf;
1252 model = (fms >> 4) & 0xf;
1253 stepping = fms & 0xf;
1254 if (family == 6 || family == 0xf)
1255 model += ((fms >> 16) & 0xf) << 4;
1258 fprintf(stderr,
"%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1259 max_level, family, model, stepping, family, model, stepping);
1261 if (!(edx & (1 << 5))) {
1262 fprintf(stderr,
"CPUID: no MSR\n");
1271 ebx = ecx = edx = 0;
1272 asm(
"cpuid" :
"=a" (
max_level),
"=b" (ebx),
"=c" (
ecx),
"=d" (edx) :
"a" (0x80000000));
1274 if (max_level < 0x80000007) {
1275 fprintf(stderr,
"CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1283 asm(
"cpuid" :
"=a" (eax),
"=b" (ebx),
"=c" (
ecx),
"=d" (edx) :
"a" (0x80000007));
1287 fprintf(stderr,
"No invariant TSC\n");
1296 asm(
"cpuid" :
"=a" (eax),
"=b" (ebx),
"=c" (
ecx),
"=d" (edx) :
"a" (0x6));
1299 fprintf(stderr,
"No APERF MSR\n");
1315 fprintf(stderr,
"%s: [-v][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
1341 int max_core_id = 0;
1342 int max_package_id = 0;
1343 int max_siblings = 0;
1346 int physical_package_id;
1351 topo.max_cpu_num = 0;
1357 fprintf(stderr,
"num_cpus %d max_cpu_num %d\n",
topo.num_cpus,
topo.max_cpu_num);
1361 perror(
"calloc cpus");
1370 perror(
"CPU_ALLOC");
1382 perror(
"CPU_ALLOC");
1393 for (i = 0; i <=
topo.max_cpu_num; ++
i) {
1398 fprintf(stderr,
"cpu%d NOT PRESENT\n", i);
1402 if (
cpus[i].core_id > max_core_id)
1403 max_core_id =
cpus[
i].core_id;
1406 if (
cpus[i].physical_package_id > max_package_id)
1407 max_package_id =
cpus[
i].physical_package_id;
1410 if (siblings > max_siblings)
1413 fprintf(stderr,
"cpu %d pkg %d core %d\n",
1414 i,
cpus[i].physical_package_id,
cpus[i].core_id);
1416 topo.num_cores_per_pkg = max_core_id + 1;
1418 fprintf(stderr,
"max_core_id %d, sizing for %d cores per package\n",
1419 max_core_id,
topo.num_cores_per_pkg);
1423 topo.num_packages = max_package_id + 1;
1425 fprintf(stderr,
"max_package_id %d, sizing for %d packages\n",
1426 max_package_id,
topo.num_packages);
1430 topo.num_threads_per_core = max_siblings;
1432 fprintf(stderr,
"max_siblings %d\n", max_siblings);
1442 *t = calloc(
topo.num_threads_per_core *
topo.num_cores_per_pkg *
1447 for (i = 0; i <
topo.num_threads_per_core *
1448 topo.num_cores_per_pkg *
topo.num_packages; i++)
1449 (*t)[
i].cpu_id = -1;
1451 *c = calloc(
topo.num_cores_per_pkg *
topo.num_packages,
1456 for (i = 0; i <
topo.num_cores_per_pkg *
topo.num_packages; i++)
1457 (*c)[
i].core_id = -1;
1459 *p = calloc(
topo.num_packages,
sizeof(
struct pkg_data));
1463 for (i = 0; i <
topo.num_packages; i++)
1464 (*p)[
i].package_id =
i;
1468 perror(
"calloc counters");
1480 struct pkg_data *pkg_base,
int thread_num,
int core_num,
1487 t =
GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1488 c =
GET_CORE(core_base, core_num, pkg_num);
1489 p =
GET_PKG(pkg_base, pkg_num);
1492 if (thread_num == 0) {
1505 int my_thread_id, my_core_id, my_package_id;
1568 execvp(argv[0], argv);
1572 if (child_pid == -1) {
1579 if (waitpid(child_pid, &status, 0) == -1) {
1590 timersub(&tv_odd, &tv_even, &
tv_delta);
1607 while ((opt = getopt(argc, argv,
"+pPSvi:sc:sC:m:M:")) != -1) {
1650 fprintf(stderr,
"turbostat v2.1 October 6, 2012"
1659 return fork_it(argv + optind);