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>
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 /* 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 do_ivt_turbo_ratio_limit;
66 unsigned int extra_msr_offset32;
67 unsigned int extra_msr_offset64;
68 unsigned int extra_delta_offset32;
69 unsigned int extra_delta_offset64;
71 unsigned int show_pkg;
72 unsigned int show_core;
73 unsigned int show_cpu;
74 unsigned int show_pkg_only;
75 unsigned int show_core_only;
76 char *output_buffer, *outp;
78 int aperf_mperf_unstable;
82 cpu_set_t *cpu_present_set, *cpu_affinity_set;
83 size_t cpu_present_setsize, cpu_affinity_setsize;
86 unsigned long long tsc;
87 unsigned long long aperf;
88 unsigned long long mperf;
89 unsigned long long c1; /* derived */
90 unsigned long long extra_msr64;
91 unsigned long long extra_delta64;
92 unsigned long long extra_msr32;
93 unsigned long long extra_delta32;
96 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
97 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
98 } *thread_even, *thread_odd;
101 unsigned long long c3;
102 unsigned long long c6;
103 unsigned long long c7;
104 unsigned int core_id;
105 } *core_even, *core_odd;
108 unsigned long long pc2;
109 unsigned long long pc3;
110 unsigned long long pc6;
111 unsigned long long pc7;
112 unsigned int package_id;
113 } *package_even, *package_odd;
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)
126 struct system_summary {
127 struct thread_data threads;
128 struct core_data cores;
129 struct pkg_data packages;
138 int num_cores_per_pkg;
139 int num_threads_per_core;
142 struct timeval tv_even, tv_odd, tv_delta;
144 void setup_all_buffers(void);
146 int cpu_is_not_present(int cpu)
148 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
151 * run func(thread, core, package) in topology order
152 * skip non-present cpus
155 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
156 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
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) {
164 struct thread_data *t;
168 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
170 if (cpu_is_not_present(t->cpu_id))
173 c = GET_CORE(core_base, core_no, pkg_no);
174 p = GET_PKG(pkg_base, pkg_no);
176 retval = func(t, c, p);
185 int cpu_migrate(int cpu)
187 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
188 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
189 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
195 int get_msr(int cpu, off_t offset, unsigned long long *msr)
201 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
202 fd = open(pathname, O_RDONLY);
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);
217 void print_header(void)
220 outp += sprintf(outp, "pk");
222 outp += sprintf(outp, " ");
224 outp += sprintf(outp, "cor");
226 outp += sprintf(outp, " CPU");
227 if (show_pkg || show_core || show_cpu)
228 outp += sprintf(outp, " ");
230 outp += sprintf(outp, " %%c0");
232 outp += sprintf(outp, " GHz");
233 outp += sprintf(outp, " TSC");
234 if (extra_delta_offset32)
235 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
236 if (extra_delta_offset64)
237 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
238 if (extra_msr_offset32)
239 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
240 if (extra_msr_offset64)
241 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
243 outp += sprintf(outp, " %%c1");
245 outp += sprintf(outp, " %%c3");
247 outp += sprintf(outp, " %%c6");
249 outp += sprintf(outp, " %%c7");
251 outp += sprintf(outp, " %%pc2");
253 outp += sprintf(outp, " %%pc3");
255 outp += sprintf(outp, " %%pc6");
257 outp += sprintf(outp, " %%pc7");
259 outp += sprintf(outp, "\n");
262 int dump_counters(struct thread_data *t, struct core_data *c,
265 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
268 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
269 fprintf(stderr, "TSC: %016llX\n", t->tsc);
270 fprintf(stderr, "aperf: %016llX\n", t->aperf);
271 fprintf(stderr, "mperf: %016llX\n", t->mperf);
272 fprintf(stderr, "c1: %016llX\n", t->c1);
273 fprintf(stderr, "msr0x%x: %08llX\n",
274 extra_delta_offset32, t->extra_delta32);
275 fprintf(stderr, "msr0x%x: %016llX\n",
276 extra_delta_offset64, t->extra_delta64);
277 fprintf(stderr, "msr0x%x: %08llX\n",
278 extra_msr_offset32, t->extra_msr32);
279 fprintf(stderr, "msr0x%x: %016llX\n",
280 extra_msr_offset64, t->extra_msr64);
284 fprintf(stderr, "core: %d\n", c->core_id);
285 fprintf(stderr, "c3: %016llX\n", c->c3);
286 fprintf(stderr, "c6: %016llX\n", c->c6);
287 fprintf(stderr, "c7: %016llX\n", c->c7);
291 fprintf(stderr, "package: %d\n", p->package_id);
292 fprintf(stderr, "pc2: %016llX\n", p->pc2);
293 fprintf(stderr, "pc3: %016llX\n", p->pc3);
294 fprintf(stderr, "pc6: %016llX\n", p->pc6);
295 fprintf(stderr, "pc7: %016llX\n", p->pc7);
301 * column formatting convention & formats
302 * package: "pk" 2 columns %2d
303 * core: "cor" 3 columns %3d
304 * CPU: "CPU" 3 columns %3d
305 * GHz: "GHz" 3 columns %3.2
306 * TSC: "TSC" 3 columns %3.2
307 * percentage " %pc3" %6.2
309 int format_counters(struct thread_data *t, struct core_data *c,
312 double interval_float;
314 /* if showing only 1st thread in core and this isn't one, bail out */
315 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
318 /* if showing only 1st thread in pkg and this isn't one, bail out */
319 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
322 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
324 /* topo columns, print blanks on 1st (average) line */
325 if (t == &average.threads) {
327 outp += sprintf(outp, " ");
328 if (show_pkg && show_core)
329 outp += sprintf(outp, " ");
331 outp += sprintf(outp, " ");
333 outp += sprintf(outp, " " " ");
337 outp += sprintf(outp, "%2d", p->package_id);
339 outp += sprintf(outp, " ");
341 if (show_pkg && show_core)
342 outp += sprintf(outp, " ");
345 outp += sprintf(outp, "%3d", c->core_id);
347 outp += sprintf(outp, " ");
350 outp += sprintf(outp, " %3d", t->cpu_id);
354 if (do_nhm_cstates) {
355 if (show_pkg || show_core || show_cpu)
356 outp += sprintf(outp, " ");
358 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
360 outp += sprintf(outp, " ****");
365 if (!aperf_mperf_unstable) {
366 outp += sprintf(outp, " %3.2f",
367 1.0 * t->tsc / units * t->aperf /
368 t->mperf / interval_float);
370 if (t->aperf > t->tsc || t->mperf > t->tsc) {
371 outp += sprintf(outp, " ***");
373 outp += sprintf(outp, "%3.1f*",
376 t->mperf / interval_float);
382 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
385 if (extra_delta_offset32)
386 outp += sprintf(outp, " %11llu", t->extra_delta32);
389 if (extra_delta_offset64)
390 outp += sprintf(outp, " %11llu", t->extra_delta64);
392 if (extra_msr_offset32)
393 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
396 if (extra_msr_offset64)
397 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
399 if (do_nhm_cstates) {
401 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
403 outp += sprintf(outp, " ****");
406 /* print per-core data only for 1st thread in core */
407 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
411 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
413 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
415 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
417 /* print per-package data only for 1st core in package */
418 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
422 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
424 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
426 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
428 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
430 outp += sprintf(outp, "\n");
437 fputs(output_buffer, stdout);
438 outp = output_buffer;
442 fputs(output_buffer, stderr);
443 outp = output_buffer;
445 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
449 if (!printed || !summary_only)
452 if (topo.num_cpus > 1)
453 format_counters(&average.threads, &average.cores,
461 for_all_cpus(format_counters, t, c, p);
465 delta_package(struct pkg_data *new, struct pkg_data *old)
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;
474 delta_core(struct core_data *new, struct core_data *old)
476 old->c3 = new->c3 - old->c3;
477 old->c6 = new->c6 - old->c6;
478 old->c7 = new->c7 - old->c7;
485 delta_thread(struct thread_data *new, struct thread_data *old,
486 struct core_data *core_delta)
488 old->tsc = new->tsc - old->tsc;
490 /* check for TSC < 1 Mcycles over interval */
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)) {
501 old->aperf = new->aperf - old->aperf;
502 old->mperf = new->mperf - old->mperf;
505 if (!aperf_mperf_unstable) {
506 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
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");
510 aperf_mperf_unstable = 1;
513 * mperf delta is likely a huge "positive" number
514 * can not use it for calculating c0 time
522 * As counter collection is not atomic,
523 * it is possible for mperf's non-halted cycles + idle states
524 * to exceed TSC's all cycles: show c1 = 0% in that case.
526 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
529 /* normal case, derive c1 */
530 old->c1 = old->tsc - old->mperf - core_delta->c3
531 - core_delta->c6 - core_delta->c7;
534 if (old->mperf == 0) {
535 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
536 old->mperf = 1; /* divide by 0 protection */
539 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
540 old->extra_delta32 &= 0xFFFFFFFF;
542 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
545 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
547 old->extra_msr32 = new->extra_msr32;
548 old->extra_msr64 = new->extra_msr64;
551 int delta_cpu(struct thread_data *t, struct core_data *c,
552 struct pkg_data *p, struct thread_data *t2,
553 struct core_data *c2, struct pkg_data *p2)
555 /* calculate core delta only for 1st thread in core */
556 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
559 /* always calculate thread delta */
560 delta_thread(t, t2, c2); /* c2 is core delta */
562 /* calculate package delta only for 1st core in package */
563 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
564 delta_package(p, p2);
569 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
576 t->extra_delta32 = 0;
577 t->extra_delta64 = 0;
579 /* tells format_counters to dump all fields from this set */
580 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
591 int sum_counters(struct thread_data *t, struct core_data *c,
594 average.threads.tsc += t->tsc;
595 average.threads.aperf += t->aperf;
596 average.threads.mperf += t->mperf;
597 average.threads.c1 += t->c1;
599 average.threads.extra_delta32 += t->extra_delta32;
600 average.threads.extra_delta64 += t->extra_delta64;
602 /* sum per-core values only for 1st thread in core */
603 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
606 average.cores.c3 += c->c3;
607 average.cores.c6 += c->c6;
608 average.cores.c7 += c->c7;
610 /* sum per-pkg values only for 1st core in pkg */
611 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
614 average.packages.pc2 += p->pc2;
615 average.packages.pc3 += p->pc3;
616 average.packages.pc6 += p->pc6;
617 average.packages.pc7 += p->pc7;
622 * sum the counters for all cpus in the system
623 * compute the weighted average
625 void compute_average(struct thread_data *t, struct core_data *c,
628 clear_counters(&average.threads, &average.cores, &average.packages);
630 for_all_cpus(sum_counters, t, c, p);
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)
654 unsigned int low, high;
656 asm volatile("rdtsc" : "=a" (low), "=d" (high));
658 return low | ((unsigned long long)high) << 32;
665 * acquire and record local counters for that cpu
667 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
671 if (cpu_migrate(cpu))
674 t->tsc = rdtsc(); /* we are running on local CPU of interest */
677 if (get_msr(cpu, MSR_APERF, &t->aperf))
679 if (get_msr(cpu, MSR_MPERF, &t->mperf))
683 if (extra_delta_offset32) {
684 if (get_msr(cpu, extra_delta_offset32, &t->extra_delta32))
686 t->extra_delta32 &= 0xFFFFFFFF;
689 if (extra_delta_offset64)
690 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
693 if (extra_msr_offset32) {
694 if (get_msr(cpu, extra_msr_offset32, &t->extra_msr32))
696 t->extra_msr32 &= 0xFFFFFFFF;
699 if (extra_msr_offset64)
700 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
703 /* collect core counters only for 1st thread in core */
704 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
707 if (do_nhm_cstates) {
708 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
710 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
715 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
718 /* collect package counters only for 1st core in package */
719 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
722 if (do_nhm_cstates) {
723 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
725 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
728 if (do_snb_cstates) {
729 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
731 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
737 void print_verbose_header(void)
739 unsigned long long msr;
742 if (!do_nehalem_platform_info)
745 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &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",
752 ratio, bclk, ratio * bclk);
754 ratio = (msr >> 8) & 0xFF;
755 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
756 ratio, bclk, ratio * bclk);
758 if (!do_ivt_turbo_ratio_limit)
759 goto print_nhm_turbo_ratio_limits;
761 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
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:
808 if (!do_nehalem_turbo_ratio_limit)
811 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
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);
857 void free_all_buffers(void)
859 CPU_FREE(cpu_present_set);
860 cpu_present_set = NULL;
863 CPU_FREE(cpu_affinity_set);
864 cpu_affinity_set = NULL;
865 cpu_affinity_setsize = 0;
884 output_buffer = NULL;
889 * cpu_is_first_sibling_in_core(cpu)
890 * return 1 if given CPU is 1st HT sibling in the core
892 int cpu_is_first_sibling_in_core(int cpu)
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);
910 * cpu_is_first_core_in_package(cpu)
911 * return 1 if given CPU is 1st core in package
913 int cpu_is_first_core_in_package(int 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);
930 int get_physical_package_id(int 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);
947 int get_core_id(int cpu)
953 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
954 filep = fopen(path, "r");
959 fscanf(filep, "%d", &core);
964 int get_num_ht_siblings(int cpu)
972 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
973 filep = fopen(path, "r");
980 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
981 * otherwinse 1 sibling (self).
983 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
994 * run func(thread, core, package) in topology order
995 * skip non-present cpus
998 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
999 struct pkg_data *, struct thread_data *, struct core_data *,
1000 struct pkg_data *), struct thread_data *thread_base,
1001 struct core_data *core_base, struct pkg_data *pkg_base,
1002 struct thread_data *thread_base2, struct core_data *core_base2,
1003 struct pkg_data *pkg_base2)
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) {
1011 struct thread_data *t, *t2;
1012 struct core_data *c, *c2;
1013 struct pkg_data *p, *p2;
1015 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1017 if (cpu_is_not_present(t->cpu_id))
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);
1038 * run func(cpu) on every cpu in /proc/stat
1039 * return max_cpu number
1041 int for_all_proc_cpus(int (func)(int))
1047 fp = fopen(proc_stat, "r");
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);
1074 void re_initialize(void)
1077 setup_all_buffers();
1078 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1084 * remember the last one seen, it will be the max
1086 int count_cpus(int cpu)
1088 if (topo.max_cpu_num < cpu)
1089 topo.max_cpu_num = cpu;
1094 int mark_cpu_present(int cpu)
1096 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1100 void turbostat_loop()
1105 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1108 } else if (retval == -1) {
1112 gettimeofday(&tv_even, (struct timezone *)NULL);
1115 if (for_all_proc_cpus(cpu_is_not_present)) {
1119 sleep(interval_sec);
1120 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1123 } else if (retval == -1) {
1127 gettimeofday(&tv_odd, (struct timezone *)NULL);
1128 timersub(&tv_odd, &tv_even, &tv_delta);
1129 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1130 compute_average(EVEN_COUNTERS);
1131 format_all_counters(EVEN_COUNTERS);
1133 sleep(interval_sec);
1134 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1137 } else if (retval == -1) {
1141 gettimeofday(&tv_even, (struct timezone *)NULL);
1142 timersub(&tv_even, &tv_odd, &tv_delta);
1143 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1144 compute_average(ODD_COUNTERS);
1145 format_all_counters(ODD_COUNTERS);
1150 void check_dev_msr()
1154 if (stat("/dev/cpu/0/msr", &sb)) {
1155 fprintf(stderr, "no /dev/cpu/0/msr\n");
1156 fprintf(stderr, "Try \"# modprobe msr\"\n");
1161 void check_super_user()
1163 if (getuid() != 0) {
1164 fprintf(stderr, "must be root\n");
1169 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1178 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1179 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1180 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1181 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1182 case 0x2C: /* Westmere EP - Gulftown */
1183 case 0x2A: /* SNB */
1184 case 0x2D: /* SNB Xeon */
1185 case 0x3A: /* IVB */
1186 case 0x3E: /* IVB Xeon */
1188 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1189 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1194 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1203 case 0x3E: /* IVB Xeon */
1211 int is_snb(unsigned int family, unsigned int model)
1219 case 0x3A: /* IVB */
1220 case 0x3E: /* IVB Xeon */
1226 double discover_bclk(unsigned int family, unsigned int model)
1228 if (is_snb(family, model))
1236 unsigned int eax, ebx, ecx, edx, max_level;
1237 unsigned int fms, family, model, stepping;
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");
1267 * check max extended function levels of CPUID.
1268 * This is needed to check for invariant TSC.
1269 * This check is valid for both Intel and AMD.
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);
1280 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1281 * this check is valid for both Intel and AMD
1283 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
1284 has_invariant_tsc = edx & (1 << 8);
1286 if (!has_invariant_tsc) {
1287 fprintf(stderr, "No invariant TSC\n");
1292 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1293 * this check is valid for both Intel and AMD
1296 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
1297 has_aperf = ecx & (1 << 0);
1299 fprintf(stderr, "No APERF MSR\n");
1303 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1304 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1305 do_snb_cstates = is_snb(family, model);
1306 bclk = discover_bclk(family, model);
1308 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1309 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
1315 fprintf(stderr, "%s: [-v][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
1322 * in /dev/cpu/ return success for names that are numbers
1323 * ie. filter out ".", "..", "microcode".
1325 int dir_filter(const struct dirent *dirp)
1327 if (isdigit(dirp->d_name[0]))
1333 int open_dev_cpu_msr(int dummy1)
1338 void topology_probe()
1341 int max_core_id = 0;
1342 int max_package_id = 0;
1343 int max_siblings = 0;
1344 struct cpu_topology {
1346 int physical_package_id;
1349 /* Initialize num_cpus, max_cpu_num */
1351 topo.max_cpu_num = 0;
1352 for_all_proc_cpus(count_cpus);
1353 if (!summary_only && topo.num_cpus > 1)
1357 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1359 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1361 perror("calloc cpus");
1366 * Allocate and initialize cpu_present_set
1368 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1369 if (cpu_present_set == NULL) {
1370 perror("CPU_ALLOC");
1373 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1374 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1375 for_all_proc_cpus(mark_cpu_present);
1378 * Allocate and initialize cpu_affinity_set
1380 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1381 if (cpu_affinity_set == NULL) {
1382 perror("CPU_ALLOC");
1385 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1386 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1391 * find max_core_id, max_package_id
1393 for (i = 0; i <= topo.max_cpu_num; ++i) {
1396 if (cpu_is_not_present(i)) {
1398 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1401 cpus[i].core_id = get_core_id(i);
1402 if (cpus[i].core_id > max_core_id)
1403 max_core_id = cpus[i].core_id;
1405 cpus[i].physical_package_id = get_physical_package_id(i);
1406 if (cpus[i].physical_package_id > max_package_id)
1407 max_package_id = cpus[i].physical_package_id;
1409 siblings = get_num_ht_siblings(i);
1410 if (siblings > max_siblings)
1411 max_siblings = 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);
1420 if (!summary_only && topo.num_cores_per_pkg > 1)
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);
1427 if (!summary_only && topo.num_packages > 1)
1430 topo.num_threads_per_core = max_siblings;
1432 fprintf(stderr, "max_siblings %d\n", max_siblings);
1438 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1442 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1443 topo.num_packages, sizeof(struct thread_data));
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,
1452 sizeof(struct core_data));
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");
1474 * set cpu_id, core_num, pkg_num
1475 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1477 * increment topo.num_cores when 1st core in pkg seen
1479 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1480 struct pkg_data *pkg_base, int thread_num, int core_num,
1481 int pkg_num, int cpu_id)
1483 struct thread_data *t;
1484 struct core_data *c;
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) {
1493 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1494 if (cpu_is_first_core_in_package(cpu_id))
1495 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1498 c->core_id = core_num;
1499 p->package_id = pkg_num;
1503 int initialize_counters(int cpu_id)
1505 int my_thread_id, my_core_id, my_package_id;
1507 my_package_id = get_physical_package_id(cpu_id);
1508 my_core_id = get_core_id(cpu_id);
1510 if (cpu_is_first_sibling_in_core(cpu_id)) {
1517 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1518 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1522 void allocate_output_buffer()
1524 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1525 outp = output_buffer;
1532 void setup_all_buffers(void)
1535 allocate_counters(&thread_even, &core_even, &package_even);
1536 allocate_counters(&thread_odd, &core_odd, &package_odd);
1537 allocate_output_buffer();
1538 for_all_proc_cpus(initialize_counters);
1540 void turbostat_init()
1547 setup_all_buffers();
1550 print_verbose_header();
1553 int fork_it(char **argv)
1558 status = for_all_cpus(get_counters, EVEN_COUNTERS);
1561 /* clear affinity side-effect of get_counters() */
1562 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
1563 gettimeofday(&tv_even, (struct timezone *)NULL);
1568 execvp(argv[0], argv);
1572 if (child_pid == -1) {
1577 signal(SIGINT, SIG_IGN);
1578 signal(SIGQUIT, SIG_IGN);
1579 if (waitpid(child_pid, &status, 0) == -1) {
1585 * n.b. fork_it() does not check for errors from for_all_cpus()
1586 * because re-starting is problematic when forking
1588 for_all_cpus(get_counters, ODD_COUNTERS);
1589 gettimeofday(&tv_odd, (struct timezone *)NULL);
1590 timersub(&tv_odd, &tv_even, &tv_delta);
1591 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1592 compute_average(EVEN_COUNTERS);
1593 format_all_counters(EVEN_COUNTERS);
1596 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1601 void cmdline(int argc, char **argv)
1607 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:")) != -1) {
1622 interval_sec = atoi(optarg);
1625 sscanf(optarg, "%x", &extra_delta_offset32);
1628 extra_delta_offset32 = 0x34; /* SMI counter */
1631 sscanf(optarg, "%x", &extra_delta_offset64);
1634 sscanf(optarg, "%x", &extra_msr_offset32);
1637 sscanf(optarg, "%x", &extra_msr_offset64);
1645 int main(int argc, char **argv)
1647 cmdline(argc, argv);
1650 fprintf(stderr, "turbostat v2.1 October 6, 2012"
1651 " - Len Brown <lenb@kernel.org>\n");
1656 * if any params left, it must be a command to fork
1659 return fork_it(argv + optind);