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.
26 #include <sys/types.h>
29 #include <sys/resource.h>
39 char *proc_stat = "/proc/stat";
40 unsigned int interval_sec = 5; /* set with -i interval_sec */
41 unsigned int verbose; /* set with -v */
42 unsigned int summary_only; /* set with -s */
45 unsigned int do_nhm_cstates;
46 unsigned int do_snb_cstates;
47 unsigned int has_aperf;
48 unsigned int units = 1000000000; /* Ghz etc */
49 unsigned int genuine_intel;
50 unsigned int has_invariant_tsc;
51 unsigned int do_nehalem_platform_info;
52 unsigned int do_nehalem_turbo_ratio_limit;
53 unsigned int do_ivt_turbo_ratio_limit;
54 unsigned int extra_msr_offset32;
55 unsigned int extra_msr_offset64;
56 unsigned int extra_delta_offset32;
57 unsigned int extra_delta_offset64;
59 unsigned int show_pkg;
60 unsigned int show_core;
61 unsigned int show_cpu;
62 unsigned int show_pkg_only;
63 unsigned int show_core_only;
64 char *output_buffer, *outp;
66 int aperf_mperf_unstable;
70 cpu_set_t *cpu_present_set, *cpu_affinity_set;
71 size_t cpu_present_setsize, cpu_affinity_setsize;
74 unsigned long long tsc;
75 unsigned long long aperf;
76 unsigned long long mperf;
77 unsigned long long c1; /* derived */
78 unsigned long long extra_msr64;
79 unsigned long long extra_delta64;
80 unsigned long long extra_msr32;
81 unsigned long long extra_delta32;
84 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
85 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
86 } *thread_even, *thread_odd;
89 unsigned long long c3;
90 unsigned long long c6;
91 unsigned long long c7;
93 } *core_even, *core_odd;
96 unsigned long long pc2;
97 unsigned long long pc3;
98 unsigned long long pc6;
99 unsigned long long pc7;
100 unsigned int package_id;
101 } *package_even, *package_odd;
103 #define ODD_COUNTERS thread_odd, core_odd, package_odd
104 #define EVEN_COUNTERS thread_even, core_even, package_even
106 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
107 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
108 topo.num_threads_per_core + \
109 (core_no) * topo.num_threads_per_core + (thread_no))
110 #define GET_CORE(core_base, core_no, pkg_no) \
111 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
112 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
114 struct system_summary {
115 struct thread_data threads;
116 struct core_data cores;
117 struct pkg_data packages;
126 int num_cores_per_pkg;
127 int num_threads_per_core;
130 struct timeval tv_even, tv_odd, tv_delta;
132 void setup_all_buffers(void);
134 int cpu_is_not_present(int cpu)
136 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
139 * run func(thread, core, package) in topology order
140 * skip non-present cpus
143 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
144 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
146 int retval, pkg_no, core_no, thread_no;
148 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
149 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
150 for (thread_no = 0; thread_no <
151 topo.num_threads_per_core; ++thread_no) {
152 struct thread_data *t;
156 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
158 if (cpu_is_not_present(t->cpu_id))
161 c = GET_CORE(core_base, core_no, pkg_no);
162 p = GET_PKG(pkg_base, pkg_no);
164 retval = func(t, c, p);
173 int cpu_migrate(int cpu)
175 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
176 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
177 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
183 int get_msr(int cpu, off_t offset, unsigned long long *msr)
189 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
190 fd = open(pathname, O_RDONLY);
194 retval = pread(fd, msr, sizeof *msr, offset);
197 if (retval != sizeof *msr) {
198 fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
205 void print_header(void)
208 outp += sprintf(outp, "pk");
210 outp += sprintf(outp, " ");
212 outp += sprintf(outp, "cor");
214 outp += sprintf(outp, " CPU");
215 if (show_pkg || show_core || show_cpu)
216 outp += sprintf(outp, " ");
218 outp += sprintf(outp, " %%c0");
220 outp += sprintf(outp, " GHz");
221 outp += sprintf(outp, " TSC");
222 if (extra_delta_offset32)
223 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
224 if (extra_delta_offset64)
225 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
226 if (extra_msr_offset32)
227 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
228 if (extra_msr_offset64)
229 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
231 outp += sprintf(outp, " %%c1");
233 outp += sprintf(outp, " %%c3");
235 outp += sprintf(outp, " %%c6");
237 outp += sprintf(outp, " %%c7");
239 outp += sprintf(outp, " %%pc2");
241 outp += sprintf(outp, " %%pc3");
243 outp += sprintf(outp, " %%pc6");
245 outp += sprintf(outp, " %%pc7");
247 outp += sprintf(outp, "\n");
250 int dump_counters(struct thread_data *t, struct core_data *c,
253 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
256 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
257 fprintf(stderr, "TSC: %016llX\n", t->tsc);
258 fprintf(stderr, "aperf: %016llX\n", t->aperf);
259 fprintf(stderr, "mperf: %016llX\n", t->mperf);
260 fprintf(stderr, "c1: %016llX\n", t->c1);
261 fprintf(stderr, "msr0x%x: %08llX\n",
262 extra_delta_offset32, t->extra_delta32);
263 fprintf(stderr, "msr0x%x: %016llX\n",
264 extra_delta_offset64, t->extra_delta64);
265 fprintf(stderr, "msr0x%x: %08llX\n",
266 extra_msr_offset32, t->extra_msr32);
267 fprintf(stderr, "msr0x%x: %016llX\n",
268 extra_msr_offset64, t->extra_msr64);
272 fprintf(stderr, "core: %d\n", c->core_id);
273 fprintf(stderr, "c3: %016llX\n", c->c3);
274 fprintf(stderr, "c6: %016llX\n", c->c6);
275 fprintf(stderr, "c7: %016llX\n", c->c7);
279 fprintf(stderr, "package: %d\n", p->package_id);
280 fprintf(stderr, "pc2: %016llX\n", p->pc2);
281 fprintf(stderr, "pc3: %016llX\n", p->pc3);
282 fprintf(stderr, "pc6: %016llX\n", p->pc6);
283 fprintf(stderr, "pc7: %016llX\n", p->pc7);
289 * column formatting convention & formats
290 * package: "pk" 2 columns %2d
291 * core: "cor" 3 columns %3d
292 * CPU: "CPU" 3 columns %3d
293 * GHz: "GHz" 3 columns %3.2
294 * TSC: "TSC" 3 columns %3.2
295 * percentage " %pc3" %6.2
297 int format_counters(struct thread_data *t, struct core_data *c,
300 double interval_float;
302 /* if showing only 1st thread in core and this isn't one, bail out */
303 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
306 /* if showing only 1st thread in pkg and this isn't one, bail out */
307 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
310 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
312 /* topo columns, print blanks on 1st (average) line */
313 if (t == &average.threads) {
315 outp += sprintf(outp, " ");
316 if (show_pkg && show_core)
317 outp += sprintf(outp, " ");
319 outp += sprintf(outp, " ");
321 outp += sprintf(outp, " " " ");
325 outp += sprintf(outp, "%2d", p->package_id);
327 outp += sprintf(outp, " ");
329 if (show_pkg && show_core)
330 outp += sprintf(outp, " ");
333 outp += sprintf(outp, "%3d", c->core_id);
335 outp += sprintf(outp, " ");
338 outp += sprintf(outp, " %3d", t->cpu_id);
342 if (do_nhm_cstates) {
343 if (show_pkg || show_core || show_cpu)
344 outp += sprintf(outp, " ");
346 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
348 outp += sprintf(outp, " ****");
353 if (!aperf_mperf_unstable) {
354 outp += sprintf(outp, " %3.2f",
355 1.0 * t->tsc / units * t->aperf /
356 t->mperf / interval_float);
358 if (t->aperf > t->tsc || t->mperf > t->tsc) {
359 outp += sprintf(outp, " ***");
361 outp += sprintf(outp, "%3.1f*",
364 t->mperf / interval_float);
370 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
373 if (extra_delta_offset32)
374 outp += sprintf(outp, " %11llu", t->extra_delta32);
377 if (extra_delta_offset64)
378 outp += sprintf(outp, " %11llu", t->extra_delta64);
380 if (extra_msr_offset32)
381 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
384 if (extra_msr_offset64)
385 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
387 if (do_nhm_cstates) {
389 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
391 outp += sprintf(outp, " ****");
394 /* print per-core data only for 1st thread in core */
395 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
399 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
401 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
403 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
405 /* print per-package data only for 1st core in package */
406 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
410 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
412 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
414 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
416 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
418 outp += sprintf(outp, "\n");
425 fputs(output_buffer, stdout);
426 outp = output_buffer;
430 fputs(output_buffer, stderr);
431 outp = output_buffer;
433 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
437 if (!printed || !summary_only)
440 if (topo.num_cpus > 1)
441 format_counters(&average.threads, &average.cores,
449 for_all_cpus(format_counters, t, c, p);
453 delta_package(struct pkg_data *new, struct pkg_data *old)
455 old->pc2 = new->pc2 - old->pc2;
456 old->pc3 = new->pc3 - old->pc3;
457 old->pc6 = new->pc6 - old->pc6;
458 old->pc7 = new->pc7 - old->pc7;
462 delta_core(struct core_data *new, struct core_data *old)
464 old->c3 = new->c3 - old->c3;
465 old->c6 = new->c6 - old->c6;
466 old->c7 = new->c7 - old->c7;
473 delta_thread(struct thread_data *new, struct thread_data *old,
474 struct core_data *core_delta)
476 old->tsc = new->tsc - old->tsc;
478 /* check for TSC < 1 Mcycles over interval */
479 if (old->tsc < (1000 * 1000)) {
480 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
481 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
482 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
486 old->c1 = new->c1 - old->c1;
488 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
489 old->aperf = new->aperf - old->aperf;
490 old->mperf = new->mperf - old->mperf;
493 if (!aperf_mperf_unstable) {
494 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
495 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
496 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
498 aperf_mperf_unstable = 1;
501 * mperf delta is likely a huge "positive" number
502 * can not use it for calculating c0 time
510 * As counter collection is not atomic,
511 * it is possible for mperf's non-halted cycles + idle states
512 * to exceed TSC's all cycles: show c1 = 0% in that case.
514 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
517 /* normal case, derive c1 */
518 old->c1 = old->tsc - old->mperf - core_delta->c3
519 - core_delta->c6 - core_delta->c7;
522 if (old->mperf == 0) {
523 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
524 old->mperf = 1; /* divide by 0 protection */
527 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
528 old->extra_delta32 &= 0xFFFFFFFF;
530 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
533 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
535 old->extra_msr32 = new->extra_msr32;
536 old->extra_msr64 = new->extra_msr64;
539 int delta_cpu(struct thread_data *t, struct core_data *c,
540 struct pkg_data *p, struct thread_data *t2,
541 struct core_data *c2, struct pkg_data *p2)
543 /* calculate core delta only for 1st thread in core */
544 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
547 /* always calculate thread delta */
548 delta_thread(t, t2, c2); /* c2 is core delta */
550 /* calculate package delta only for 1st core in package */
551 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
552 delta_package(p, p2);
557 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
564 t->extra_delta32 = 0;
565 t->extra_delta64 = 0;
567 /* tells format_counters to dump all fields from this set */
568 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
579 int sum_counters(struct thread_data *t, struct core_data *c,
582 average.threads.tsc += t->tsc;
583 average.threads.aperf += t->aperf;
584 average.threads.mperf += t->mperf;
585 average.threads.c1 += t->c1;
587 average.threads.extra_delta32 += t->extra_delta32;
588 average.threads.extra_delta64 += t->extra_delta64;
590 /* sum per-core values only for 1st thread in core */
591 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
594 average.cores.c3 += c->c3;
595 average.cores.c6 += c->c6;
596 average.cores.c7 += c->c7;
598 /* sum per-pkg values only for 1st core in pkg */
599 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
602 average.packages.pc2 += p->pc2;
603 average.packages.pc3 += p->pc3;
604 average.packages.pc6 += p->pc6;
605 average.packages.pc7 += p->pc7;
610 * sum the counters for all cpus in the system
611 * compute the weighted average
613 void compute_average(struct thread_data *t, struct core_data *c,
616 clear_counters(&average.threads, &average.cores, &average.packages);
618 for_all_cpus(sum_counters, t, c, p);
620 average.threads.tsc /= topo.num_cpus;
621 average.threads.aperf /= topo.num_cpus;
622 average.threads.mperf /= topo.num_cpus;
623 average.threads.c1 /= topo.num_cpus;
625 average.threads.extra_delta32 /= topo.num_cpus;
626 average.threads.extra_delta32 &= 0xFFFFFFFF;
628 average.threads.extra_delta64 /= topo.num_cpus;
630 average.cores.c3 /= topo.num_cores;
631 average.cores.c6 /= topo.num_cores;
632 average.cores.c7 /= topo.num_cores;
634 average.packages.pc2 /= topo.num_packages;
635 average.packages.pc3 /= topo.num_packages;
636 average.packages.pc6 /= topo.num_packages;
637 average.packages.pc7 /= topo.num_packages;
640 static unsigned long long rdtsc(void)
642 unsigned int low, high;
644 asm volatile("rdtsc" : "=a" (low), "=d" (high));
646 return low | ((unsigned long long)high) << 32;
653 * acquire and record local counters for that cpu
655 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
659 if (cpu_migrate(cpu))
662 t->tsc = rdtsc(); /* we are running on local CPU of interest */
665 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
667 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
671 if (extra_delta_offset32) {
672 if (get_msr(cpu, extra_delta_offset32, &t->extra_delta32))
674 t->extra_delta32 &= 0xFFFFFFFF;
677 if (extra_delta_offset64)
678 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
681 if (extra_msr_offset32) {
682 if (get_msr(cpu, extra_msr_offset32, &t->extra_msr32))
684 t->extra_msr32 &= 0xFFFFFFFF;
687 if (extra_msr_offset64)
688 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
691 /* collect core counters only for 1st thread in core */
692 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
695 if (do_nhm_cstates) {
696 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
698 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
703 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
706 /* collect package counters only for 1st core in package */
707 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
710 if (do_nhm_cstates) {
711 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
713 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
716 if (do_snb_cstates) {
717 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
719 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
725 void print_verbose_header(void)
727 unsigned long long msr;
730 if (!do_nehalem_platform_info)
733 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
736 fprintf(stderr, "MSR_NHM_PLATFORM_INFO: 0x%llx\n", msr);
738 ratio = (msr >> 40) & 0xFF;
739 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
740 ratio, bclk, ratio * bclk);
742 ratio = (msr >> 8) & 0xFF;
743 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
744 ratio, bclk, ratio * bclk);
746 if (!do_ivt_turbo_ratio_limit)
747 goto print_nhm_turbo_ratio_limits;
749 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
752 fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
754 ratio = (msr >> 56) & 0xFF;
756 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
757 ratio, bclk, ratio * bclk);
759 ratio = (msr >> 48) & 0xFF;
761 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
762 ratio, bclk, ratio * bclk);
764 ratio = (msr >> 40) & 0xFF;
766 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
767 ratio, bclk, ratio * bclk);
769 ratio = (msr >> 32) & 0xFF;
771 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
772 ratio, bclk, ratio * bclk);
774 ratio = (msr >> 24) & 0xFF;
776 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
777 ratio, bclk, ratio * bclk);
779 ratio = (msr >> 16) & 0xFF;
781 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
782 ratio, bclk, ratio * bclk);
784 ratio = (msr >> 8) & 0xFF;
786 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
787 ratio, bclk, ratio * bclk);
789 ratio = (msr >> 0) & 0xFF;
791 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
792 ratio, bclk, ratio * bclk);
794 print_nhm_turbo_ratio_limits:
796 if (!do_nehalem_turbo_ratio_limit)
799 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
802 fprintf(stderr, "MSR_NHM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
804 ratio = (msr >> 56) & 0xFF;
806 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
807 ratio, bclk, ratio * bclk);
809 ratio = (msr >> 48) & 0xFF;
811 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
812 ratio, bclk, ratio * bclk);
814 ratio = (msr >> 40) & 0xFF;
816 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
817 ratio, bclk, ratio * bclk);
819 ratio = (msr >> 32) & 0xFF;
821 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
822 ratio, bclk, ratio * bclk);
824 ratio = (msr >> 24) & 0xFF;
826 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
827 ratio, bclk, ratio * bclk);
829 ratio = (msr >> 16) & 0xFF;
831 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
832 ratio, bclk, ratio * bclk);
834 ratio = (msr >> 8) & 0xFF;
836 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
837 ratio, bclk, ratio * bclk);
839 ratio = (msr >> 0) & 0xFF;
841 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
842 ratio, bclk, ratio * bclk);
845 void free_all_buffers(void)
847 CPU_FREE(cpu_present_set);
848 cpu_present_set = NULL;
851 CPU_FREE(cpu_affinity_set);
852 cpu_affinity_set = NULL;
853 cpu_affinity_setsize = 0;
872 output_buffer = NULL;
877 * cpu_is_first_sibling_in_core(cpu)
878 * return 1 if given CPU is 1st HT sibling in the core
880 int cpu_is_first_sibling_in_core(int cpu)
886 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
887 filep = fopen(path, "r");
892 fscanf(filep, "%d", &first_cpu);
894 return (cpu == first_cpu);
898 * cpu_is_first_core_in_package(cpu)
899 * return 1 if given CPU is 1st core in package
901 int cpu_is_first_core_in_package(int cpu)
907 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
908 filep = fopen(path, "r");
913 fscanf(filep, "%d", &first_cpu);
915 return (cpu == first_cpu);
918 int get_physical_package_id(int cpu)
924 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
925 filep = fopen(path, "r");
930 fscanf(filep, "%d", &pkg);
935 int get_core_id(int cpu)
941 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
942 filep = fopen(path, "r");
947 fscanf(filep, "%d", &core);
952 int get_num_ht_siblings(int cpu)
960 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
961 filep = fopen(path, "r");
968 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
969 * otherwinse 1 sibling (self).
971 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
982 * run func(thread, core, package) in topology order
983 * skip non-present cpus
986 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
987 struct pkg_data *, struct thread_data *, struct core_data *,
988 struct pkg_data *), struct thread_data *thread_base,
989 struct core_data *core_base, struct pkg_data *pkg_base,
990 struct thread_data *thread_base2, struct core_data *core_base2,
991 struct pkg_data *pkg_base2)
993 int retval, pkg_no, core_no, thread_no;
995 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
996 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
997 for (thread_no = 0; thread_no <
998 topo.num_threads_per_core; ++thread_no) {
999 struct thread_data *t, *t2;
1000 struct core_data *c, *c2;
1001 struct pkg_data *p, *p2;
1003 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1005 if (cpu_is_not_present(t->cpu_id))
1008 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1010 c = GET_CORE(core_base, core_no, pkg_no);
1011 c2 = GET_CORE(core_base2, core_no, pkg_no);
1013 p = GET_PKG(pkg_base, pkg_no);
1014 p2 = GET_PKG(pkg_base2, pkg_no);
1016 retval = func(t, c, p, t2, c2, p2);
1026 * run func(cpu) on every cpu in /proc/stat
1027 * return max_cpu number
1029 int for_all_proc_cpus(int (func)(int))
1035 fp = fopen(proc_stat, "r");
1041 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1043 perror("/proc/stat format");
1048 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1052 retval = func(cpu_num);
1062 void re_initialize(void)
1065 setup_all_buffers();
1066 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1072 * remember the last one seen, it will be the max
1074 int count_cpus(int cpu)
1076 if (topo.max_cpu_num < cpu)
1077 topo.max_cpu_num = cpu;
1082 int mark_cpu_present(int cpu)
1084 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1088 void turbostat_loop()
1093 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1096 } else if (retval == -1) {
1100 gettimeofday(&tv_even, (struct timezone *)NULL);
1103 if (for_all_proc_cpus(cpu_is_not_present)) {
1107 sleep(interval_sec);
1108 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1111 } else if (retval == -1) {
1115 gettimeofday(&tv_odd, (struct timezone *)NULL);
1116 timersub(&tv_odd, &tv_even, &tv_delta);
1117 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1118 compute_average(EVEN_COUNTERS);
1119 format_all_counters(EVEN_COUNTERS);
1121 sleep(interval_sec);
1122 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1125 } else if (retval == -1) {
1129 gettimeofday(&tv_even, (struct timezone *)NULL);
1130 timersub(&tv_even, &tv_odd, &tv_delta);
1131 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1132 compute_average(ODD_COUNTERS);
1133 format_all_counters(ODD_COUNTERS);
1138 void check_dev_msr()
1142 if (stat("/dev/cpu/0/msr", &sb)) {
1143 fprintf(stderr, "no /dev/cpu/0/msr\n");
1144 fprintf(stderr, "Try \"# modprobe msr\"\n");
1149 void check_super_user()
1151 if (getuid() != 0) {
1152 fprintf(stderr, "must be root\n");
1157 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1166 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1167 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1168 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1169 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1170 case 0x2C: /* Westmere EP - Gulftown */
1171 case 0x2A: /* SNB */
1172 case 0x2D: /* SNB Xeon */
1173 case 0x3A: /* IVB */
1174 case 0x3E: /* IVB Xeon */
1176 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1177 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1182 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1191 case 0x3E: /* IVB Xeon */
1199 int is_snb(unsigned int family, unsigned int model)
1207 case 0x3A: /* IVB */
1208 case 0x3E: /* IVB Xeon */
1214 double discover_bclk(unsigned int family, unsigned int model)
1216 if (is_snb(family, model))
1224 unsigned int eax, ebx, ecx, edx, max_level;
1225 unsigned int fms, family, model, stepping;
1227 eax = ebx = ecx = edx = 0;
1229 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1231 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1235 fprintf(stderr, "%.4s%.4s%.4s ",
1236 (char *)&ebx, (char *)&edx, (char *)&ecx);
1238 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1239 family = (fms >> 8) & 0xf;
1240 model = (fms >> 4) & 0xf;
1241 stepping = fms & 0xf;
1242 if (family == 6 || family == 0xf)
1243 model += ((fms >> 16) & 0xf) << 4;
1246 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1247 max_level, family, model, stepping, family, model, stepping);
1249 if (!(edx & (1 << 5))) {
1250 fprintf(stderr, "CPUID: no MSR\n");
1255 * check max extended function levels of CPUID.
1256 * This is needed to check for invariant TSC.
1257 * This check is valid for both Intel and AMD.
1259 ebx = ecx = edx = 0;
1260 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1262 if (max_level < 0x80000007) {
1263 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1268 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1269 * this check is valid for both Intel and AMD
1271 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
1272 has_invariant_tsc = edx & (1 << 8);
1274 if (!has_invariant_tsc) {
1275 fprintf(stderr, "No invariant TSC\n");
1280 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1281 * this check is valid for both Intel and AMD
1284 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
1285 has_aperf = ecx & (1 << 0);
1287 fprintf(stderr, "No APERF MSR\n");
1291 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1292 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1293 do_snb_cstates = is_snb(family, model);
1294 bclk = discover_bclk(family, model);
1296 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1297 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
1303 fprintf(stderr, "%s: [-v][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
1310 * in /dev/cpu/ return success for names that are numbers
1311 * ie. filter out ".", "..", "microcode".
1313 int dir_filter(const struct dirent *dirp)
1315 if (isdigit(dirp->d_name[0]))
1321 int open_dev_cpu_msr(int dummy1)
1326 void topology_probe()
1329 int max_core_id = 0;
1330 int max_package_id = 0;
1331 int max_siblings = 0;
1332 struct cpu_topology {
1334 int physical_package_id;
1337 /* Initialize num_cpus, max_cpu_num */
1339 topo.max_cpu_num = 0;
1340 for_all_proc_cpus(count_cpus);
1341 if (!summary_only && topo.num_cpus > 1)
1345 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1347 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1349 perror("calloc cpus");
1354 * Allocate and initialize cpu_present_set
1356 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1357 if (cpu_present_set == NULL) {
1358 perror("CPU_ALLOC");
1361 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1362 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1363 for_all_proc_cpus(mark_cpu_present);
1366 * Allocate and initialize cpu_affinity_set
1368 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1369 if (cpu_affinity_set == NULL) {
1370 perror("CPU_ALLOC");
1373 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1374 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1379 * find max_core_id, max_package_id
1381 for (i = 0; i <= topo.max_cpu_num; ++i) {
1384 if (cpu_is_not_present(i)) {
1386 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1389 cpus[i].core_id = get_core_id(i);
1390 if (cpus[i].core_id > max_core_id)
1391 max_core_id = cpus[i].core_id;
1393 cpus[i].physical_package_id = get_physical_package_id(i);
1394 if (cpus[i].physical_package_id > max_package_id)
1395 max_package_id = cpus[i].physical_package_id;
1397 siblings = get_num_ht_siblings(i);
1398 if (siblings > max_siblings)
1399 max_siblings = siblings;
1401 fprintf(stderr, "cpu %d pkg %d core %d\n",
1402 i, cpus[i].physical_package_id, cpus[i].core_id);
1404 topo.num_cores_per_pkg = max_core_id + 1;
1406 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1407 max_core_id, topo.num_cores_per_pkg);
1408 if (!summary_only && topo.num_cores_per_pkg > 1)
1411 topo.num_packages = max_package_id + 1;
1413 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1414 max_package_id, topo.num_packages);
1415 if (!summary_only && topo.num_packages > 1)
1418 topo.num_threads_per_core = max_siblings;
1420 fprintf(stderr, "max_siblings %d\n", max_siblings);
1426 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1430 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1431 topo.num_packages, sizeof(struct thread_data));
1435 for (i = 0; i < topo.num_threads_per_core *
1436 topo.num_cores_per_pkg * topo.num_packages; i++)
1437 (*t)[i].cpu_id = -1;
1439 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1440 sizeof(struct core_data));
1444 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1445 (*c)[i].core_id = -1;
1447 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1451 for (i = 0; i < topo.num_packages; i++)
1452 (*p)[i].package_id = i;
1456 perror("calloc counters");
1462 * set cpu_id, core_num, pkg_num
1463 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1465 * increment topo.num_cores when 1st core in pkg seen
1467 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1468 struct pkg_data *pkg_base, int thread_num, int core_num,
1469 int pkg_num, int cpu_id)
1471 struct thread_data *t;
1472 struct core_data *c;
1475 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1476 c = GET_CORE(core_base, core_num, pkg_num);
1477 p = GET_PKG(pkg_base, pkg_num);
1480 if (thread_num == 0) {
1481 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1482 if (cpu_is_first_core_in_package(cpu_id))
1483 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1486 c->core_id = core_num;
1487 p->package_id = pkg_num;
1491 int initialize_counters(int cpu_id)
1493 int my_thread_id, my_core_id, my_package_id;
1495 my_package_id = get_physical_package_id(cpu_id);
1496 my_core_id = get_core_id(cpu_id);
1498 if (cpu_is_first_sibling_in_core(cpu_id)) {
1505 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1506 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1510 void allocate_output_buffer()
1512 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1513 outp = output_buffer;
1520 void setup_all_buffers(void)
1523 allocate_counters(&thread_even, &core_even, &package_even);
1524 allocate_counters(&thread_odd, &core_odd, &package_odd);
1525 allocate_output_buffer();
1526 for_all_proc_cpus(initialize_counters);
1528 void turbostat_init()
1535 setup_all_buffers();
1538 print_verbose_header();
1541 int fork_it(char **argv)
1546 status = for_all_cpus(get_counters, EVEN_COUNTERS);
1549 /* clear affinity side-effect of get_counters() */
1550 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
1551 gettimeofday(&tv_even, (struct timezone *)NULL);
1556 execvp(argv[0], argv);
1560 if (child_pid == -1) {
1565 signal(SIGINT, SIG_IGN);
1566 signal(SIGQUIT, SIG_IGN);
1567 if (waitpid(child_pid, &status, 0) == -1) {
1573 * n.b. fork_it() does not check for errors from for_all_cpus()
1574 * because re-starting is problematic when forking
1576 for_all_cpus(get_counters, ODD_COUNTERS);
1577 gettimeofday(&tv_odd, (struct timezone *)NULL);
1578 timersub(&tv_odd, &tv_even, &tv_delta);
1579 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1580 compute_average(EVEN_COUNTERS);
1581 format_all_counters(EVEN_COUNTERS);
1584 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1589 void cmdline(int argc, char **argv)
1595 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:")) != -1) {
1610 interval_sec = atoi(optarg);
1613 sscanf(optarg, "%x", &extra_delta_offset32);
1616 extra_delta_offset32 = 0x34; /* SMI counter */
1619 sscanf(optarg, "%x", &extra_delta_offset64);
1622 sscanf(optarg, "%x", &extra_msr_offset32);
1625 sscanf(optarg, "%x", &extra_msr_offset64);
1633 int main(int argc, char **argv)
1635 cmdline(argc, argv);
1638 fprintf(stderr, "turbostat v2.1 October 6, 2012"
1639 " - Len Brown <lenb@kernel.org>\n");
1644 * if any params left, it must be a command to fork
1647 return fork_it(argv + optind);