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;
69 unsigned int show_pkg;
70 unsigned int show_core;
71 unsigned int show_cpu;
72 unsigned int show_pkg_only;
73 unsigned int show_core_only;
74 char *output_buffer, *outp;
76 int aperf_mperf_unstable;
80 cpu_set_t *cpu_present_set, *cpu_affinity_set;
81 size_t cpu_present_setsize, cpu_affinity_setsize;
84 unsigned long long tsc;
85 unsigned long long aperf;
86 unsigned long long mperf;
87 unsigned long long c1; /* derived */
88 unsigned long long extra_msr64;
89 unsigned int extra_msr32;
92 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
93 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
94 } *thread_even, *thread_odd;
97 unsigned long long c3;
98 unsigned long long c6;
99 unsigned long long c7;
100 unsigned int core_id;
101 } *core_even, *core_odd;
104 unsigned long long pc2;
105 unsigned long long pc3;
106 unsigned long long pc6;
107 unsigned long long pc7;
108 unsigned int package_id;
109 } *package_even, *package_odd;
111 #define ODD_COUNTERS thread_odd, core_odd, package_odd
112 #define EVEN_COUNTERS thread_even, core_even, package_even
114 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
115 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
116 topo.num_threads_per_core + \
117 (core_no) * topo.num_threads_per_core + (thread_no))
118 #define GET_CORE(core_base, core_no, pkg_no) \
119 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
120 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
122 struct system_summary {
123 struct thread_data threads;
124 struct core_data cores;
125 struct pkg_data packages;
134 int num_cores_per_pkg;
135 int num_threads_per_core;
138 struct timeval tv_even, tv_odd, tv_delta;
140 void setup_all_buffers(void);
142 int cpu_is_not_present(int cpu)
144 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
147 * run func(thread, core, package) in topology order
148 * skip non-present cpus
151 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
152 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
154 int retval, pkg_no, core_no, thread_no;
156 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
157 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
158 for (thread_no = 0; thread_no <
159 topo.num_threads_per_core; ++thread_no) {
160 struct thread_data *t;
164 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
166 if (cpu_is_not_present(t->cpu_id))
169 c = GET_CORE(core_base, core_no, pkg_no);
170 p = GET_PKG(pkg_base, pkg_no);
172 retval = func(t, c, p);
181 int cpu_migrate(int cpu)
183 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
184 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
185 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
191 int get_msr(int cpu, off_t offset, unsigned long long *msr)
197 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
198 fd = open(pathname, O_RDONLY);
202 retval = pread(fd, msr, sizeof *msr, offset);
205 if (retval != sizeof *msr)
212 * Truncate the 8 bytes we read from /dev/cpu/.../msr
213 * to the 4 bytes requested
216 int get_msr32(int cpu, off_t offset, unsigned int *msr)
220 unsigned long long msr64;
222 retval = get_msr(cpu, offset, &msr64);
223 *msr = (unsigned int) msr64;
229 void print_header(void)
232 outp += sprintf(outp, "pk");
234 outp += sprintf(outp, " ");
236 outp += sprintf(outp, "cor");
238 outp += sprintf(outp, " CPU");
239 if (show_pkg || show_core || show_cpu)
240 outp += sprintf(outp, " ");
242 outp += sprintf(outp, " %%c0");
244 outp += sprintf(outp, " GHz");
245 outp += sprintf(outp, " TSC");
246 if (extra_msr_offset32)
247 outp += sprintf(outp, " MSR 0x%04X", extra_msr_offset32);
248 if (extra_msr_offset64)
249 outp += sprintf(outp, " MSR 0x%04X", extra_msr_offset64);
251 outp += sprintf(outp, " %%c1");
253 outp += sprintf(outp, " %%c3");
255 outp += sprintf(outp, " %%c6");
257 outp += sprintf(outp, " %%c7");
259 outp += sprintf(outp, " %%pc2");
261 outp += sprintf(outp, " %%pc3");
263 outp += sprintf(outp, " %%pc6");
265 outp += sprintf(outp, " %%pc7");
267 outp += sprintf(outp, "\n");
270 int dump_counters(struct thread_data *t, struct core_data *c,
273 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
276 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
277 fprintf(stderr, "TSC: %016llX\n", t->tsc);
278 fprintf(stderr, "aperf: %016llX\n", t->aperf);
279 fprintf(stderr, "mperf: %016llX\n", t->mperf);
280 fprintf(stderr, "c1: %016llX\n", t->c1);
281 fprintf(stderr, "msr0x%x: %08X\n",
282 extra_msr_offset32, t->extra_msr32);
283 fprintf(stderr, "msr0x%x: %016llX\n",
284 extra_msr_offset64, t->extra_msr64);
288 fprintf(stderr, "core: %d\n", c->core_id);
289 fprintf(stderr, "c3: %016llX\n", c->c3);
290 fprintf(stderr, "c6: %016llX\n", c->c6);
291 fprintf(stderr, "c7: %016llX\n", c->c7);
295 fprintf(stderr, "package: %d\n", p->package_id);
296 fprintf(stderr, "pc2: %016llX\n", p->pc2);
297 fprintf(stderr, "pc3: %016llX\n", p->pc3);
298 fprintf(stderr, "pc6: %016llX\n", p->pc6);
299 fprintf(stderr, "pc7: %016llX\n", p->pc7);
305 * column formatting convention & formats
306 * package: "pk" 2 columns %2d
307 * core: "cor" 3 columns %3d
308 * CPU: "CPU" 3 columns %3d
309 * GHz: "GHz" 3 columns %3.2
310 * TSC: "TSC" 3 columns %3.2
311 * percentage " %pc3" %6.2
313 int format_counters(struct thread_data *t, struct core_data *c,
316 double interval_float;
318 /* if showing only 1st thread in core and this isn't one, bail out */
319 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
322 /* if showing only 1st thread in pkg and this isn't one, bail out */
323 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
326 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
328 /* topo columns, print blanks on 1st (average) line */
329 if (t == &average.threads) {
331 outp += sprintf(outp, " ");
332 if (show_pkg && show_core)
333 outp += sprintf(outp, " ");
335 outp += sprintf(outp, " ");
337 outp += sprintf(outp, " " " ");
341 outp += sprintf(outp, "%2d", p->package_id);
343 outp += sprintf(outp, " ");
345 if (show_pkg && show_core)
346 outp += sprintf(outp, " ");
349 outp += sprintf(outp, "%3d", c->core_id);
351 outp += sprintf(outp, " ");
354 outp += sprintf(outp, " %3d", t->cpu_id);
358 if (do_nhm_cstates) {
359 if (show_pkg || show_core || show_cpu)
360 outp += sprintf(outp, " ");
362 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
364 outp += sprintf(outp, " ****");
369 if (!aperf_mperf_unstable) {
370 outp += sprintf(outp, " %3.2f",
371 1.0 * t->tsc / units * t->aperf /
372 t->mperf / interval_float);
374 if (t->aperf > t->tsc || t->mperf > t->tsc) {
375 outp += sprintf(outp, " ***");
377 outp += sprintf(outp, "%3.1f*",
380 t->mperf / interval_float);
386 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
389 if (extra_msr_offset32)
390 outp += sprintf(outp, " 0x%08x", t->extra_msr32);
393 if (extra_msr_offset64)
394 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
396 if (do_nhm_cstates) {
398 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
400 outp += sprintf(outp, " ****");
403 /* print per-core data only for 1st thread in core */
404 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
408 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
410 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
412 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
414 /* print per-package data only for 1st core in package */
415 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
419 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
421 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
423 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
425 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
427 outp += sprintf(outp, "\n");
434 fputs(output_buffer, stdout);
435 outp = output_buffer;
439 fputs(output_buffer, stderr);
440 outp = output_buffer;
442 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
446 if (!printed || !summary_only)
449 if (topo.num_cpus > 1)
450 format_counters(&average.threads, &average.cores,
458 for_all_cpus(format_counters, t, c, p);
462 delta_package(struct pkg_data *new, struct pkg_data *old)
464 old->pc2 = new->pc2 - old->pc2;
465 old->pc3 = new->pc3 - old->pc3;
466 old->pc6 = new->pc6 - old->pc6;
467 old->pc7 = new->pc7 - old->pc7;
471 delta_core(struct core_data *new, struct core_data *old)
473 old->c3 = new->c3 - old->c3;
474 old->c6 = new->c6 - old->c6;
475 old->c7 = new->c7 - old->c7;
482 delta_thread(struct thread_data *new, struct thread_data *old,
483 struct core_data *core_delta)
485 old->tsc = new->tsc - old->tsc;
487 /* check for TSC < 1 Mcycles over interval */
488 if (old->tsc < (1000 * 1000)) {
489 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
490 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
491 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
495 old->c1 = new->c1 - old->c1;
497 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
498 old->aperf = new->aperf - old->aperf;
499 old->mperf = new->mperf - old->mperf;
502 if (!aperf_mperf_unstable) {
503 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
504 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
505 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
507 aperf_mperf_unstable = 1;
510 * mperf delta is likely a huge "positive" number
511 * can not use it for calculating c0 time
519 * As counter collection is not atomic,
520 * it is possible for mperf's non-halted cycles + idle states
521 * to exceed TSC's all cycles: show c1 = 0% in that case.
523 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
526 /* normal case, derive c1 */
527 old->c1 = old->tsc - old->mperf - core_delta->c3
528 - core_delta->c6 - core_delta->c7;
531 if (old->mperf == 0) {
532 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
533 old->mperf = 1; /* divide by 0 protection */
537 * Extra MSR is a snapshot, simply copy latest w/o subtracting
539 old->extra_msr32 = new->extra_msr32;
540 old->extra_msr64 = new->extra_msr64;
543 int delta_cpu(struct thread_data *t, struct core_data *c,
544 struct pkg_data *p, struct thread_data *t2,
545 struct core_data *c2, struct pkg_data *p2)
547 /* calculate core delta only for 1st thread in core */
548 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
551 /* always calculate thread delta */
552 delta_thread(t, t2, c2); /* c2 is core delta */
554 /* calculate package delta only for 1st core in package */
555 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
556 delta_package(p, p2);
561 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
568 /* tells format_counters to dump all fields from this set */
569 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
580 int sum_counters(struct thread_data *t, struct core_data *c,
583 average.threads.tsc += t->tsc;
584 average.threads.aperf += t->aperf;
585 average.threads.mperf += t->mperf;
586 average.threads.c1 += t->c1;
588 /* sum per-core values only for 1st thread in core */
589 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
592 average.cores.c3 += c->c3;
593 average.cores.c6 += c->c6;
594 average.cores.c7 += c->c7;
596 /* sum per-pkg values only for 1st core in pkg */
597 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
600 average.packages.pc2 += p->pc2;
601 average.packages.pc3 += p->pc3;
602 average.packages.pc6 += p->pc6;
603 average.packages.pc7 += p->pc7;
608 * sum the counters for all cpus in the system
609 * compute the weighted average
611 void compute_average(struct thread_data *t, struct core_data *c,
614 clear_counters(&average.threads, &average.cores, &average.packages);
616 for_all_cpus(sum_counters, t, c, p);
618 average.threads.tsc /= topo.num_cpus;
619 average.threads.aperf /= topo.num_cpus;
620 average.threads.mperf /= topo.num_cpus;
621 average.threads.c1 /= topo.num_cpus;
623 average.cores.c3 /= topo.num_cores;
624 average.cores.c6 /= topo.num_cores;
625 average.cores.c7 /= topo.num_cores;
627 average.packages.pc2 /= topo.num_packages;
628 average.packages.pc3 /= topo.num_packages;
629 average.packages.pc6 /= topo.num_packages;
630 average.packages.pc7 /= topo.num_packages;
633 static unsigned long long rdtsc(void)
635 unsigned int low, high;
637 asm volatile("rdtsc" : "=a" (low), "=d" (high));
639 return low | ((unsigned long long)high) << 32;
646 * acquire and record local counters for that cpu
648 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
652 if (cpu_migrate(cpu))
655 t->tsc = rdtsc(); /* we are running on local CPU of interest */
658 if (get_msr(cpu, MSR_APERF, &t->aperf))
660 if (get_msr(cpu, MSR_MPERF, &t->mperf))
664 if (extra_msr_offset32)
665 if (get_msr32(cpu, extra_msr_offset32, &t->extra_msr32))
668 if (extra_msr_offset64)
669 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
672 /* collect core counters only for 1st thread in core */
673 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
676 if (do_nhm_cstates) {
677 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
679 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
684 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
687 /* collect package counters only for 1st core in package */
688 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
691 if (do_nhm_cstates) {
692 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
694 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
697 if (do_snb_cstates) {
698 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
700 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
706 void print_verbose_header(void)
708 unsigned long long msr;
711 if (!do_nehalem_platform_info)
714 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
717 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
719 ratio = (msr >> 40) & 0xFF;
720 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
721 ratio, bclk, ratio * bclk);
723 ratio = (msr >> 8) & 0xFF;
724 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
725 ratio, bclk, ratio * bclk);
727 if (!do_ivt_turbo_ratio_limit)
728 goto print_nhm_turbo_ratio_limits;
730 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
733 fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
735 ratio = (msr >> 56) & 0xFF;
737 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
738 ratio, bclk, ratio * bclk);
740 ratio = (msr >> 48) & 0xFF;
742 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
743 ratio, bclk, ratio * bclk);
745 ratio = (msr >> 40) & 0xFF;
747 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
748 ratio, bclk, ratio * bclk);
750 ratio = (msr >> 32) & 0xFF;
752 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
753 ratio, bclk, ratio * bclk);
755 ratio = (msr >> 24) & 0xFF;
757 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
758 ratio, bclk, ratio * bclk);
760 ratio = (msr >> 16) & 0xFF;
762 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
763 ratio, bclk, ratio * bclk);
765 ratio = (msr >> 8) & 0xFF;
767 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
768 ratio, bclk, ratio * bclk);
770 ratio = (msr >> 0) & 0xFF;
772 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
773 ratio, bclk, ratio * bclk);
775 print_nhm_turbo_ratio_limits:
777 if (!do_nehalem_turbo_ratio_limit)
780 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
783 fprintf(stderr, "MSR_NEHALEM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
785 ratio = (msr >> 56) & 0xFF;
787 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
788 ratio, bclk, ratio * bclk);
790 ratio = (msr >> 48) & 0xFF;
792 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
793 ratio, bclk, ratio * bclk);
795 ratio = (msr >> 40) & 0xFF;
797 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
798 ratio, bclk, ratio * bclk);
800 ratio = (msr >> 32) & 0xFF;
802 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
803 ratio, bclk, ratio * bclk);
805 ratio = (msr >> 24) & 0xFF;
807 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
808 ratio, bclk, ratio * bclk);
810 ratio = (msr >> 16) & 0xFF;
812 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
813 ratio, bclk, ratio * bclk);
815 ratio = (msr >> 8) & 0xFF;
817 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
818 ratio, bclk, ratio * bclk);
820 ratio = (msr >> 0) & 0xFF;
822 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
823 ratio, bclk, ratio * bclk);
826 void free_all_buffers(void)
828 CPU_FREE(cpu_present_set);
829 cpu_present_set = NULL;
832 CPU_FREE(cpu_affinity_set);
833 cpu_affinity_set = NULL;
834 cpu_affinity_setsize = 0;
853 output_buffer = NULL;
858 * cpu_is_first_sibling_in_core(cpu)
859 * return 1 if given CPU is 1st HT sibling in the core
861 int cpu_is_first_sibling_in_core(int cpu)
867 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
868 filep = fopen(path, "r");
873 fscanf(filep, "%d", &first_cpu);
875 return (cpu == first_cpu);
879 * cpu_is_first_core_in_package(cpu)
880 * return 1 if given CPU is 1st core in package
882 int cpu_is_first_core_in_package(int cpu)
888 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
889 filep = fopen(path, "r");
894 fscanf(filep, "%d", &first_cpu);
896 return (cpu == first_cpu);
899 int get_physical_package_id(int cpu)
905 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
906 filep = fopen(path, "r");
911 fscanf(filep, "%d", &pkg);
916 int get_core_id(int cpu)
922 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
923 filep = fopen(path, "r");
928 fscanf(filep, "%d", &core);
933 int get_num_ht_siblings(int cpu)
941 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
942 filep = fopen(path, "r");
949 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
950 * otherwinse 1 sibling (self).
952 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
963 * run func(thread, core, package) in topology order
964 * skip non-present cpus
967 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
968 struct pkg_data *, struct thread_data *, struct core_data *,
969 struct pkg_data *), struct thread_data *thread_base,
970 struct core_data *core_base, struct pkg_data *pkg_base,
971 struct thread_data *thread_base2, struct core_data *core_base2,
972 struct pkg_data *pkg_base2)
974 int retval, pkg_no, core_no, thread_no;
976 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
977 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
978 for (thread_no = 0; thread_no <
979 topo.num_threads_per_core; ++thread_no) {
980 struct thread_data *t, *t2;
981 struct core_data *c, *c2;
982 struct pkg_data *p, *p2;
984 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
986 if (cpu_is_not_present(t->cpu_id))
989 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
991 c = GET_CORE(core_base, core_no, pkg_no);
992 c2 = GET_CORE(core_base2, core_no, pkg_no);
994 p = GET_PKG(pkg_base, pkg_no);
995 p2 = GET_PKG(pkg_base2, pkg_no);
997 retval = func(t, c, p, t2, c2, p2);
1007 * run func(cpu) on every cpu in /proc/stat
1008 * return max_cpu number
1010 int for_all_proc_cpus(int (func)(int))
1016 fp = fopen(proc_stat, "r");
1022 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1024 perror("/proc/stat format");
1029 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1033 retval = func(cpu_num);
1043 void re_initialize(void)
1046 setup_all_buffers();
1047 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1053 * remember the last one seen, it will be the max
1055 int count_cpus(int cpu)
1057 if (topo.max_cpu_num < cpu)
1058 topo.max_cpu_num = cpu;
1063 int mark_cpu_present(int cpu)
1065 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1069 void turbostat_loop()
1074 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1079 gettimeofday(&tv_even, (struct timezone *)NULL);
1082 if (for_all_proc_cpus(cpu_is_not_present)) {
1086 sleep(interval_sec);
1087 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1092 gettimeofday(&tv_odd, (struct timezone *)NULL);
1093 timersub(&tv_odd, &tv_even, &tv_delta);
1094 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1095 compute_average(EVEN_COUNTERS);
1096 format_all_counters(EVEN_COUNTERS);
1098 sleep(interval_sec);
1099 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1104 gettimeofday(&tv_even, (struct timezone *)NULL);
1105 timersub(&tv_even, &tv_odd, &tv_delta);
1106 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1107 compute_average(ODD_COUNTERS);
1108 format_all_counters(ODD_COUNTERS);
1113 void check_dev_msr()
1117 if (stat("/dev/cpu/0/msr", &sb)) {
1118 fprintf(stderr, "no /dev/cpu/0/msr\n");
1119 fprintf(stderr, "Try \"# modprobe msr\"\n");
1124 void check_super_user()
1126 if (getuid() != 0) {
1127 fprintf(stderr, "must be root\n");
1132 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1141 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1142 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1143 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1144 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1145 case 0x2C: /* Westmere EP - Gulftown */
1146 case 0x2A: /* SNB */
1147 case 0x2D: /* SNB Xeon */
1148 case 0x3A: /* IVB */
1149 case 0x3E: /* IVB Xeon */
1151 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1152 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1157 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1166 case 0x3E: /* IVB Xeon */
1174 int is_snb(unsigned int family, unsigned int model)
1182 case 0x3A: /* IVB */
1183 case 0x3E: /* IVB Xeon */
1189 double discover_bclk(unsigned int family, unsigned int model)
1191 if (is_snb(family, model))
1199 unsigned int eax, ebx, ecx, edx, max_level;
1200 unsigned int fms, family, model, stepping;
1202 eax = ebx = ecx = edx = 0;
1204 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1206 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1210 fprintf(stderr, "%.4s%.4s%.4s ",
1211 (char *)&ebx, (char *)&edx, (char *)&ecx);
1213 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1214 family = (fms >> 8) & 0xf;
1215 model = (fms >> 4) & 0xf;
1216 stepping = fms & 0xf;
1217 if (family == 6 || family == 0xf)
1218 model += ((fms >> 16) & 0xf) << 4;
1221 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1222 max_level, family, model, stepping, family, model, stepping);
1224 if (!(edx & (1 << 5))) {
1225 fprintf(stderr, "CPUID: no MSR\n");
1230 * check max extended function levels of CPUID.
1231 * This is needed to check for invariant TSC.
1232 * This check is valid for both Intel and AMD.
1234 ebx = ecx = edx = 0;
1235 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1237 if (max_level < 0x80000007) {
1238 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1243 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1244 * this check is valid for both Intel and AMD
1246 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
1247 has_invariant_tsc = edx & (1 << 8);
1249 if (!has_invariant_tsc) {
1250 fprintf(stderr, "No invariant TSC\n");
1255 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1256 * this check is valid for both Intel and AMD
1259 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
1260 has_aperf = ecx & (1 << 0);
1262 fprintf(stderr, "No APERF MSR\n");
1266 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1267 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1268 do_snb_cstates = is_snb(family, model);
1269 bclk = discover_bclk(family, model);
1271 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1272 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
1278 fprintf(stderr, "%s: [-v] [-m msr#] [-M MSR#] [-i interval_sec | command ...]\n",
1285 * in /dev/cpu/ return success for names that are numbers
1286 * ie. filter out ".", "..", "microcode".
1288 int dir_filter(const struct dirent *dirp)
1290 if (isdigit(dirp->d_name[0]))
1296 int open_dev_cpu_msr(int dummy1)
1301 void topology_probe()
1304 int max_core_id = 0;
1305 int max_package_id = 0;
1306 int max_siblings = 0;
1307 struct cpu_topology {
1309 int physical_package_id;
1312 /* Initialize num_cpus, max_cpu_num */
1314 topo.max_cpu_num = 0;
1315 for_all_proc_cpus(count_cpus);
1316 if (!summary_only && topo.num_cpus > 1)
1320 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1322 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1324 perror("calloc cpus");
1329 * Allocate and initialize cpu_present_set
1331 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1332 if (cpu_present_set == NULL) {
1333 perror("CPU_ALLOC");
1336 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1337 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1338 for_all_proc_cpus(mark_cpu_present);
1341 * Allocate and initialize cpu_affinity_set
1343 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1344 if (cpu_affinity_set == NULL) {
1345 perror("CPU_ALLOC");
1348 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1349 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1354 * find max_core_id, max_package_id
1356 for (i = 0; i <= topo.max_cpu_num; ++i) {
1359 if (cpu_is_not_present(i)) {
1361 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1364 cpus[i].core_id = get_core_id(i);
1365 if (cpus[i].core_id > max_core_id)
1366 max_core_id = cpus[i].core_id;
1368 cpus[i].physical_package_id = get_physical_package_id(i);
1369 if (cpus[i].physical_package_id > max_package_id)
1370 max_package_id = cpus[i].physical_package_id;
1372 siblings = get_num_ht_siblings(i);
1373 if (siblings > max_siblings)
1374 max_siblings = siblings;
1376 fprintf(stderr, "cpu %d pkg %d core %d\n",
1377 i, cpus[i].physical_package_id, cpus[i].core_id);
1379 topo.num_cores_per_pkg = max_core_id + 1;
1381 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1382 max_core_id, topo.num_cores_per_pkg);
1383 if (!summary_only && topo.num_cores_per_pkg > 1)
1386 topo.num_packages = max_package_id + 1;
1388 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1389 max_package_id, topo.num_packages);
1390 if (!summary_only && topo.num_packages > 1)
1393 topo.num_threads_per_core = max_siblings;
1395 fprintf(stderr, "max_siblings %d\n", max_siblings);
1401 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1405 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1406 topo.num_packages, sizeof(struct thread_data));
1410 for (i = 0; i < topo.num_threads_per_core *
1411 topo.num_cores_per_pkg * topo.num_packages; i++)
1412 (*t)[i].cpu_id = -1;
1414 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1415 sizeof(struct core_data));
1419 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1420 (*c)[i].core_id = -1;
1422 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1426 for (i = 0; i < topo.num_packages; i++)
1427 (*p)[i].package_id = i;
1431 perror("calloc counters");
1437 * set cpu_id, core_num, pkg_num
1438 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1440 * increment topo.num_cores when 1st core in pkg seen
1442 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1443 struct pkg_data *pkg_base, int thread_num, int core_num,
1444 int pkg_num, int cpu_id)
1446 struct thread_data *t;
1447 struct core_data *c;
1450 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1451 c = GET_CORE(core_base, core_num, pkg_num);
1452 p = GET_PKG(pkg_base, pkg_num);
1455 if (thread_num == 0) {
1456 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1457 if (cpu_is_first_core_in_package(cpu_id))
1458 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1461 c->core_id = core_num;
1462 p->package_id = pkg_num;
1466 int initialize_counters(int cpu_id)
1468 int my_thread_id, my_core_id, my_package_id;
1470 my_package_id = get_physical_package_id(cpu_id);
1471 my_core_id = get_core_id(cpu_id);
1473 if (cpu_is_first_sibling_in_core(cpu_id)) {
1480 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1481 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1485 void allocate_output_buffer()
1487 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1488 outp = output_buffer;
1495 void setup_all_buffers(void)
1498 allocate_counters(&thread_even, &core_even, &package_even);
1499 allocate_counters(&thread_odd, &core_odd, &package_odd);
1500 allocate_output_buffer();
1501 for_all_proc_cpus(initialize_counters);
1503 void turbostat_init()
1510 setup_all_buffers();
1513 print_verbose_header();
1516 int fork_it(char **argv)
1520 for_all_cpus(get_counters, EVEN_COUNTERS);
1521 /* clear affinity side-effect of get_counters() */
1522 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
1523 gettimeofday(&tv_even, (struct timezone *)NULL);
1528 execvp(argv[0], argv);
1533 if (child_pid == -1) {
1538 signal(SIGINT, SIG_IGN);
1539 signal(SIGQUIT, SIG_IGN);
1540 if (waitpid(child_pid, &status, 0) == -1) {
1546 * n.b. fork_it() does not check for errors from for_all_cpus()
1547 * because re-starting is problematic when forking
1549 for_all_cpus(get_counters, ODD_COUNTERS);
1550 gettimeofday(&tv_odd, (struct timezone *)NULL);
1551 timersub(&tv_odd, &tv_even, &tv_delta);
1552 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1553 compute_average(EVEN_COUNTERS);
1554 format_all_counters(EVEN_COUNTERS);
1557 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
1562 void cmdline(int argc, char **argv)
1568 while ((opt = getopt(argc, argv, "+cpsvi:m:M:")) != -1) {
1583 interval_sec = atoi(optarg);
1586 sscanf(optarg, "%x", &extra_msr_offset32);
1588 fprintf(stderr, "msr 0x%X\n", extra_msr_offset32);
1591 sscanf(optarg, "%x", &extra_msr_offset64);
1593 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset64);
1601 int main(int argc, char **argv)
1603 cmdline(argc, argv);
1606 fprintf(stderr, "turbostat v2.0 May 16, 2012"
1607 " - Len Brown <lenb@kernel.org>\n");
1612 * if any params left, it must be a command to fork
1615 return fork_it(argv + optind);