2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
5 * Copyright (c) 2013 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 rapl_verbose; /* set with -R */
43 unsigned int thermal_verbose; /* set with -T */
44 unsigned int summary_only; /* set with -s */
47 unsigned int do_nhm_cstates;
48 unsigned int do_snb_cstates;
49 unsigned int do_c8_c9_c10;
50 unsigned int do_slm_cstates;
51 unsigned int use_c1_residency_msr;
52 unsigned int has_aperf;
54 unsigned int units = 1000000000; /* Ghz etc */
55 unsigned int genuine_intel;
56 unsigned int has_invariant_tsc;
57 unsigned int do_nehalem_platform_info;
58 unsigned int do_nehalem_turbo_ratio_limit;
59 unsigned int do_ivt_turbo_ratio_limit;
60 unsigned int extra_msr_offset32;
61 unsigned int extra_msr_offset64;
62 unsigned int extra_delta_offset32;
63 unsigned int extra_delta_offset64;
66 unsigned int show_pkg;
67 unsigned int show_core;
68 unsigned int show_cpu;
69 unsigned int show_pkg_only;
70 unsigned int show_core_only;
71 char *output_buffer, *outp;
75 unsigned int tcc_activation_temp;
76 unsigned int tcc_activation_temp_override;
77 double rapl_power_units, rapl_energy_units, rapl_time_units;
78 double rapl_joule_counter_range;
80 #define RAPL_PKG (1 << 0)
81 #define RAPL_CORES (1 << 1)
82 #define RAPL_GFX (1 << 2)
83 #define RAPL_DRAM (1 << 3)
84 #define RAPL_PKG_PERF_STATUS (1 << 4)
85 #define RAPL_DRAM_PERF_STATUS (1 << 5)
86 #define RAPL_PKG_POWER_INFO (1 << 6)
87 #define RAPL_CORE_POLICY (1 << 7)
88 #define TJMAX_DEFAULT 100
90 #define MAX(a, b) ((a) > (b) ? (a) : (b))
92 int aperf_mperf_unstable;
96 cpu_set_t *cpu_present_set, *cpu_affinity_set;
97 size_t cpu_present_setsize, cpu_affinity_setsize;
100 unsigned long long tsc;
101 unsigned long long aperf;
102 unsigned long long mperf;
103 unsigned long long c1;
104 unsigned long long extra_msr64;
105 unsigned long long extra_delta64;
106 unsigned long long extra_msr32;
107 unsigned long long extra_delta32;
108 unsigned int smi_count;
111 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
112 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
113 } *thread_even, *thread_odd;
116 unsigned long long c3;
117 unsigned long long c6;
118 unsigned long long c7;
119 unsigned int core_temp_c;
120 unsigned int core_id;
121 } *core_even, *core_odd;
124 unsigned long long pc2;
125 unsigned long long pc3;
126 unsigned long long pc6;
127 unsigned long long pc7;
128 unsigned long long pc8;
129 unsigned long long pc9;
130 unsigned long long pc10;
131 unsigned int package_id;
132 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
133 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
134 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
135 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
136 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
137 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
138 unsigned int pkg_temp_c;
140 } *package_even, *package_odd;
142 #define ODD_COUNTERS thread_odd, core_odd, package_odd
143 #define EVEN_COUNTERS thread_even, core_even, package_even
145 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
146 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
147 topo.num_threads_per_core + \
148 (core_no) * topo.num_threads_per_core + (thread_no))
149 #define GET_CORE(core_base, core_no, pkg_no) \
150 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
151 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
153 struct system_summary {
154 struct thread_data threads;
155 struct core_data cores;
156 struct pkg_data packages;
165 int num_cores_per_pkg;
166 int num_threads_per_core;
169 struct timeval tv_even, tv_odd, tv_delta;
171 void setup_all_buffers(void);
173 int cpu_is_not_present(int cpu)
175 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
178 * run func(thread, core, package) in topology order
179 * skip non-present cpus
182 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
183 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
185 int retval, pkg_no, core_no, thread_no;
187 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
188 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
189 for (thread_no = 0; thread_no <
190 topo.num_threads_per_core; ++thread_no) {
191 struct thread_data *t;
195 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
197 if (cpu_is_not_present(t->cpu_id))
200 c = GET_CORE(core_base, core_no, pkg_no);
201 p = GET_PKG(pkg_base, pkg_no);
203 retval = func(t, c, p);
212 int cpu_migrate(int cpu)
214 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
215 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
216 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
222 int get_msr(int cpu, off_t offset, unsigned long long *msr)
228 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
229 fd = open(pathname, O_RDONLY);
233 retval = pread(fd, msr, sizeof *msr, offset);
236 if (retval != sizeof *msr) {
237 fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
244 void print_header(void)
247 outp += sprintf(outp, "pk");
249 outp += sprintf(outp, " ");
251 outp += sprintf(outp, "cor");
253 outp += sprintf(outp, " CPU");
254 if (show_pkg || show_core || show_cpu)
255 outp += sprintf(outp, " ");
257 outp += sprintf(outp, " %%c0");
259 outp += sprintf(outp, " GHz");
260 outp += sprintf(outp, " TSC");
262 outp += sprintf(outp, " SMI");
263 if (extra_delta_offset32)
264 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
265 if (extra_delta_offset64)
266 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
267 if (extra_msr_offset32)
268 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
269 if (extra_msr_offset64)
270 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
272 outp += sprintf(outp, " %%c1");
273 if (do_nhm_cstates && !do_slm_cstates)
274 outp += sprintf(outp, " %%c3");
276 outp += sprintf(outp, " %%c6");
278 outp += sprintf(outp, " %%c7");
281 outp += sprintf(outp, " CTMP");
283 outp += sprintf(outp, " PTMP");
286 outp += sprintf(outp, " %%pc2");
287 if (do_nhm_cstates && !do_slm_cstates)
288 outp += sprintf(outp, " %%pc3");
289 if (do_nhm_cstates && !do_slm_cstates)
290 outp += sprintf(outp, " %%pc6");
292 outp += sprintf(outp, " %%pc7");
294 outp += sprintf(outp, " %%pc8");
295 outp += sprintf(outp, " %%pc9");
296 outp += sprintf(outp, " %%pc10");
299 if (do_rapl & RAPL_PKG)
300 outp += sprintf(outp, " Pkg_W");
301 if (do_rapl & RAPL_CORES)
302 outp += sprintf(outp, " Cor_W");
303 if (do_rapl & RAPL_GFX)
304 outp += sprintf(outp, " GFX_W");
305 if (do_rapl & RAPL_DRAM)
306 outp += sprintf(outp, " RAM_W");
307 if (do_rapl & RAPL_PKG_PERF_STATUS)
308 outp += sprintf(outp, " PKG_%%");
309 if (do_rapl & RAPL_DRAM_PERF_STATUS)
310 outp += sprintf(outp, " RAM_%%");
312 outp += sprintf(outp, "\n");
315 int dump_counters(struct thread_data *t, struct core_data *c,
318 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
321 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
322 fprintf(stderr, "TSC: %016llX\n", t->tsc);
323 fprintf(stderr, "aperf: %016llX\n", t->aperf);
324 fprintf(stderr, "mperf: %016llX\n", t->mperf);
325 fprintf(stderr, "c1: %016llX\n", t->c1);
326 fprintf(stderr, "msr0x%x: %08llX\n",
327 extra_delta_offset32, t->extra_delta32);
328 fprintf(stderr, "msr0x%x: %016llX\n",
329 extra_delta_offset64, t->extra_delta64);
330 fprintf(stderr, "msr0x%x: %08llX\n",
331 extra_msr_offset32, t->extra_msr32);
332 fprintf(stderr, "msr0x%x: %016llX\n",
333 extra_msr_offset64, t->extra_msr64);
335 fprintf(stderr, "SMI: %08X\n", t->smi_count);
339 fprintf(stderr, "core: %d\n", c->core_id);
340 fprintf(stderr, "c3: %016llX\n", c->c3);
341 fprintf(stderr, "c6: %016llX\n", c->c6);
342 fprintf(stderr, "c7: %016llX\n", c->c7);
343 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
347 fprintf(stderr, "package: %d\n", p->package_id);
348 fprintf(stderr, "pc2: %016llX\n", p->pc2);
349 fprintf(stderr, "pc3: %016llX\n", p->pc3);
350 fprintf(stderr, "pc6: %016llX\n", p->pc6);
351 fprintf(stderr, "pc7: %016llX\n", p->pc7);
352 fprintf(stderr, "pc8: %016llX\n", p->pc8);
353 fprintf(stderr, "pc9: %016llX\n", p->pc9);
354 fprintf(stderr, "pc10: %016llX\n", p->pc10);
355 fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
356 fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
357 fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
358 fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
359 fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
360 fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
361 fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
367 * column formatting convention & formats
368 * package: "pk" 2 columns %2d
369 * core: "cor" 3 columns %3d
370 * CPU: "CPU" 3 columns %3d
375 * GHz: "GHz" 3 columns %3.2
376 * TSC: "TSC" 3 columns %3.2
377 * SMI: "SMI" 4 columns %4d
378 * percentage " %pc3" %6.2
379 * Perf Status percentage: %5.2
380 * "CTMP" 4 columns %4d
382 int format_counters(struct thread_data *t, struct core_data *c,
385 double interval_float;
388 /* if showing only 1st thread in core and this isn't one, bail out */
389 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
392 /* if showing only 1st thread in pkg and this isn't one, bail out */
393 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
396 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
398 /* topo columns, print blanks on 1st (average) line */
399 if (t == &average.threads) {
401 outp += sprintf(outp, " ");
402 if (show_pkg && show_core)
403 outp += sprintf(outp, " ");
405 outp += sprintf(outp, " ");
407 outp += sprintf(outp, " " " ");
411 outp += sprintf(outp, "%2d", p->package_id);
413 outp += sprintf(outp, " ");
415 if (show_pkg && show_core)
416 outp += sprintf(outp, " ");
419 outp += sprintf(outp, "%3d", c->core_id);
421 outp += sprintf(outp, " ");
424 outp += sprintf(outp, " %3d", t->cpu_id);
427 if (do_nhm_cstates) {
428 if (show_pkg || show_core || show_cpu)
429 outp += sprintf(outp, " ");
431 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
433 outp += sprintf(outp, " ****");
438 if (!aperf_mperf_unstable) {
439 outp += sprintf(outp, " %3.2f",
440 1.0 * t->tsc / units * t->aperf /
441 t->mperf / interval_float);
443 if (t->aperf > t->tsc || t->mperf > t->tsc) {
444 outp += sprintf(outp, " ***");
446 outp += sprintf(outp, "%3.1f*",
449 t->mperf / interval_float);
455 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
459 outp += sprintf(outp, "%4d", t->smi_count);
462 if (extra_delta_offset32)
463 outp += sprintf(outp, " %11llu", t->extra_delta32);
466 if (extra_delta_offset64)
467 outp += sprintf(outp, " %11llu", t->extra_delta64);
469 if (extra_msr_offset32)
470 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
473 if (extra_msr_offset64)
474 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
476 if (do_nhm_cstates) {
478 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
480 outp += sprintf(outp, " ****");
483 /* print per-core data only for 1st thread in core */
484 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
487 if (do_nhm_cstates && !do_slm_cstates)
488 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
490 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
492 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
495 outp += sprintf(outp, " %4d", c->core_temp_c);
497 /* print per-package data only for 1st core in package */
498 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
502 outp += sprintf(outp, " %4d", p->pkg_temp_c);
505 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
506 if (do_nhm_cstates && !do_slm_cstates)
507 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
508 if (do_nhm_cstates && !do_slm_cstates)
509 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
511 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
513 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
514 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
515 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
519 * If measurement interval exceeds minimum RAPL Joule Counter range,
520 * indicate that results are suspect by printing "**" in fraction place.
522 if (interval_float < rapl_joule_counter_range) {
530 if (do_rapl & RAPL_PKG)
531 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
532 if (do_rapl & RAPL_CORES)
533 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
534 if (do_rapl & RAPL_GFX)
535 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
536 if (do_rapl & RAPL_DRAM)
537 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
538 if (do_rapl & RAPL_PKG_PERF_STATUS )
539 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
540 if (do_rapl & RAPL_DRAM_PERF_STATUS )
541 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
544 outp += sprintf(outp, "\n");
551 fputs(output_buffer, stdout);
553 outp = output_buffer;
557 fputs(output_buffer, stderr);
558 outp = output_buffer;
560 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
564 if (!printed || !summary_only)
567 if (topo.num_cpus > 1)
568 format_counters(&average.threads, &average.cores,
576 for_all_cpus(format_counters, t, c, p);
579 #define DELTA_WRAP32(new, old) \
583 old = 0x100000000 + new - old; \
587 delta_package(struct pkg_data *new, struct pkg_data *old)
589 old->pc2 = new->pc2 - old->pc2;
590 old->pc3 = new->pc3 - old->pc3;
591 old->pc6 = new->pc6 - old->pc6;
592 old->pc7 = new->pc7 - old->pc7;
593 old->pc8 = new->pc8 - old->pc8;
594 old->pc9 = new->pc9 - old->pc9;
595 old->pc10 = new->pc10 - old->pc10;
596 old->pkg_temp_c = new->pkg_temp_c;
598 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
599 DELTA_WRAP32(new->energy_cores, old->energy_cores);
600 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
601 DELTA_WRAP32(new->energy_dram, old->energy_dram);
602 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
603 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
607 delta_core(struct core_data *new, struct core_data *old)
609 old->c3 = new->c3 - old->c3;
610 old->c6 = new->c6 - old->c6;
611 old->c7 = new->c7 - old->c7;
612 old->core_temp_c = new->core_temp_c;
619 delta_thread(struct thread_data *new, struct thread_data *old,
620 struct core_data *core_delta)
622 old->tsc = new->tsc - old->tsc;
624 /* check for TSC < 1 Mcycles over interval */
625 if (old->tsc < (1000 * 1000)) {
626 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
627 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
628 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
632 old->c1 = new->c1 - old->c1;
634 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
635 old->aperf = new->aperf - old->aperf;
636 old->mperf = new->mperf - old->mperf;
639 if (!aperf_mperf_unstable) {
640 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
641 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
642 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
644 aperf_mperf_unstable = 1;
647 * mperf delta is likely a huge "positive" number
648 * can not use it for calculating c0 time
655 if (use_c1_residency_msr) {
657 * Some models have a dedicated C1 residency MSR,
658 * which should be more accurate than the derivation below.
662 * As counter collection is not atomic,
663 * it is possible for mperf's non-halted cycles + idle states
664 * to exceed TSC's all cycles: show c1 = 0% in that case.
666 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
669 /* normal case, derive c1 */
670 old->c1 = old->tsc - old->mperf - core_delta->c3
671 - core_delta->c6 - core_delta->c7;
675 if (old->mperf == 0) {
676 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
677 old->mperf = 1; /* divide by 0 protection */
680 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
681 old->extra_delta32 &= 0xFFFFFFFF;
683 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
686 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
688 old->extra_msr32 = new->extra_msr32;
689 old->extra_msr64 = new->extra_msr64;
692 old->smi_count = new->smi_count - old->smi_count;
695 int delta_cpu(struct thread_data *t, struct core_data *c,
696 struct pkg_data *p, struct thread_data *t2,
697 struct core_data *c2, struct pkg_data *p2)
699 /* calculate core delta only for 1st thread in core */
700 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
703 /* always calculate thread delta */
704 delta_thread(t, t2, c2); /* c2 is core delta */
706 /* calculate package delta only for 1st core in package */
707 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
708 delta_package(p, p2);
713 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
721 t->extra_delta32 = 0;
722 t->extra_delta64 = 0;
724 /* tells format_counters to dump all fields from this set */
725 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
744 p->rapl_pkg_perf_status = 0;
745 p->rapl_dram_perf_status = 0;
748 int sum_counters(struct thread_data *t, struct core_data *c,
751 average.threads.tsc += t->tsc;
752 average.threads.aperf += t->aperf;
753 average.threads.mperf += t->mperf;
754 average.threads.c1 += t->c1;
756 average.threads.extra_delta32 += t->extra_delta32;
757 average.threads.extra_delta64 += t->extra_delta64;
759 /* sum per-core values only for 1st thread in core */
760 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
763 average.cores.c3 += c->c3;
764 average.cores.c6 += c->c6;
765 average.cores.c7 += c->c7;
767 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
769 /* sum per-pkg values only for 1st core in pkg */
770 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
773 average.packages.pc2 += p->pc2;
774 average.packages.pc3 += p->pc3;
775 average.packages.pc6 += p->pc6;
776 average.packages.pc7 += p->pc7;
777 average.packages.pc8 += p->pc8;
778 average.packages.pc9 += p->pc9;
779 average.packages.pc10 += p->pc10;
781 average.packages.energy_pkg += p->energy_pkg;
782 average.packages.energy_dram += p->energy_dram;
783 average.packages.energy_cores += p->energy_cores;
784 average.packages.energy_gfx += p->energy_gfx;
786 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
788 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
789 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
793 * sum the counters for all cpus in the system
794 * compute the weighted average
796 void compute_average(struct thread_data *t, struct core_data *c,
799 clear_counters(&average.threads, &average.cores, &average.packages);
801 for_all_cpus(sum_counters, t, c, p);
803 average.threads.tsc /= topo.num_cpus;
804 average.threads.aperf /= topo.num_cpus;
805 average.threads.mperf /= topo.num_cpus;
806 average.threads.c1 /= topo.num_cpus;
808 average.threads.extra_delta32 /= topo.num_cpus;
809 average.threads.extra_delta32 &= 0xFFFFFFFF;
811 average.threads.extra_delta64 /= topo.num_cpus;
813 average.cores.c3 /= topo.num_cores;
814 average.cores.c6 /= topo.num_cores;
815 average.cores.c7 /= topo.num_cores;
817 average.packages.pc2 /= topo.num_packages;
818 average.packages.pc3 /= topo.num_packages;
819 average.packages.pc6 /= topo.num_packages;
820 average.packages.pc7 /= topo.num_packages;
822 average.packages.pc8 /= topo.num_packages;
823 average.packages.pc9 /= topo.num_packages;
824 average.packages.pc10 /= topo.num_packages;
827 static unsigned long long rdtsc(void)
829 unsigned int low, high;
831 asm volatile("rdtsc" : "=a" (low), "=d" (high));
833 return low | ((unsigned long long)high) << 32;
840 * acquire and record local counters for that cpu
842 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
845 unsigned long long msr;
847 if (cpu_migrate(cpu)) {
848 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
852 t->tsc = rdtsc(); /* we are running on local CPU of interest */
855 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
857 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
862 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
864 t->smi_count = msr & 0xFFFFFFFF;
866 if (extra_delta_offset32) {
867 if (get_msr(cpu, extra_delta_offset32, &msr))
869 t->extra_delta32 = msr & 0xFFFFFFFF;
872 if (extra_delta_offset64)
873 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
876 if (extra_msr_offset32) {
877 if (get_msr(cpu, extra_msr_offset32, &msr))
879 t->extra_msr32 = msr & 0xFFFFFFFF;
882 if (extra_msr_offset64)
883 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
886 if (use_c1_residency_msr) {
887 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
891 /* collect core counters only for 1st thread in core */
892 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
895 if (do_nhm_cstates && !do_slm_cstates) {
896 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
900 if (do_nhm_cstates) {
901 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
906 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
910 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
912 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
916 /* collect package counters only for 1st core in package */
917 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
920 if (do_nhm_cstates && !do_slm_cstates) {
921 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
923 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
926 if (do_snb_cstates) {
927 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
929 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
933 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
935 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
937 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
940 if (do_rapl & RAPL_PKG) {
941 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
943 p->energy_pkg = msr & 0xFFFFFFFF;
945 if (do_rapl & RAPL_CORES) {
946 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
948 p->energy_cores = msr & 0xFFFFFFFF;
950 if (do_rapl & RAPL_DRAM) {
951 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
953 p->energy_dram = msr & 0xFFFFFFFF;
955 if (do_rapl & RAPL_GFX) {
956 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
958 p->energy_gfx = msr & 0xFFFFFFFF;
960 if (do_rapl & RAPL_PKG_PERF_STATUS) {
961 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
963 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
965 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
966 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
968 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
971 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
973 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
978 void print_verbose_header(void)
980 unsigned long long msr;
983 if (!do_nehalem_platform_info)
986 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
988 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
990 ratio = (msr >> 40) & 0xFF;
991 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
992 ratio, bclk, ratio * bclk);
994 ratio = (msr >> 8) & 0xFF;
995 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
996 ratio, bclk, ratio * bclk);
998 get_msr(0, MSR_IA32_POWER_CTL, &msr);
999 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1000 msr, msr & 0x2 ? "EN" : "DIS");
1002 if (!do_ivt_turbo_ratio_limit)
1003 goto print_nhm_turbo_ratio_limits;
1005 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1007 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1009 ratio = (msr >> 56) & 0xFF;
1011 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1012 ratio, bclk, ratio * bclk);
1014 ratio = (msr >> 48) & 0xFF;
1016 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1017 ratio, bclk, ratio * bclk);
1019 ratio = (msr >> 40) & 0xFF;
1021 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1022 ratio, bclk, ratio * bclk);
1024 ratio = (msr >> 32) & 0xFF;
1026 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1027 ratio, bclk, ratio * bclk);
1029 ratio = (msr >> 24) & 0xFF;
1031 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1032 ratio, bclk, ratio * bclk);
1034 ratio = (msr >> 16) & 0xFF;
1036 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1037 ratio, bclk, ratio * bclk);
1039 ratio = (msr >> 8) & 0xFF;
1041 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1042 ratio, bclk, ratio * bclk);
1044 ratio = (msr >> 0) & 0xFF;
1046 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1047 ratio, bclk, ratio * bclk);
1049 print_nhm_turbo_ratio_limits:
1050 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1052 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1053 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1055 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1057 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1058 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1059 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1060 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1061 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1062 (msr & (1 << 15)) ? "" : "UN",
1063 (unsigned int)msr & 7);
1068 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
1071 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
1074 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
1077 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
1080 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
1083 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1086 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
1089 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
1092 fprintf(stderr, "invalid");
1094 fprintf(stderr, ")\n");
1096 if (!do_nehalem_turbo_ratio_limit)
1099 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1101 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1103 ratio = (msr >> 56) & 0xFF;
1105 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1106 ratio, bclk, ratio * bclk);
1108 ratio = (msr >> 48) & 0xFF;
1110 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1111 ratio, bclk, ratio * bclk);
1113 ratio = (msr >> 40) & 0xFF;
1115 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1116 ratio, bclk, ratio * bclk);
1118 ratio = (msr >> 32) & 0xFF;
1120 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1121 ratio, bclk, ratio * bclk);
1123 ratio = (msr >> 24) & 0xFF;
1125 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1126 ratio, bclk, ratio * bclk);
1128 ratio = (msr >> 16) & 0xFF;
1130 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1131 ratio, bclk, ratio * bclk);
1133 ratio = (msr >> 8) & 0xFF;
1135 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1136 ratio, bclk, ratio * bclk);
1138 ratio = (msr >> 0) & 0xFF;
1140 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1141 ratio, bclk, ratio * bclk);
1144 void free_all_buffers(void)
1146 CPU_FREE(cpu_present_set);
1147 cpu_present_set = NULL;
1148 cpu_present_set = 0;
1150 CPU_FREE(cpu_affinity_set);
1151 cpu_affinity_set = NULL;
1152 cpu_affinity_setsize = 0;
1160 package_even = NULL;
1170 free(output_buffer);
1171 output_buffer = NULL;
1176 * cpu_is_first_sibling_in_core(cpu)
1177 * return 1 if given CPU is 1st HT sibling in the core
1179 int cpu_is_first_sibling_in_core(int cpu)
1185 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1186 filep = fopen(path, "r");
1187 if (filep == NULL) {
1191 fscanf(filep, "%d", &first_cpu);
1193 return (cpu == first_cpu);
1197 * cpu_is_first_core_in_package(cpu)
1198 * return 1 if given CPU is 1st core in package
1200 int cpu_is_first_core_in_package(int cpu)
1206 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1207 filep = fopen(path, "r");
1208 if (filep == NULL) {
1212 fscanf(filep, "%d", &first_cpu);
1214 return (cpu == first_cpu);
1217 int get_physical_package_id(int cpu)
1223 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1224 filep = fopen(path, "r");
1225 if (filep == NULL) {
1229 fscanf(filep, "%d", &pkg);
1234 int get_core_id(int cpu)
1240 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1241 filep = fopen(path, "r");
1242 if (filep == NULL) {
1246 fscanf(filep, "%d", &core);
1251 int get_num_ht_siblings(int cpu)
1259 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1260 filep = fopen(path, "r");
1261 if (filep == NULL) {
1267 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1268 * otherwinse 1 sibling (self).
1270 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1281 * run func(thread, core, package) in topology order
1282 * skip non-present cpus
1285 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1286 struct pkg_data *, struct thread_data *, struct core_data *,
1287 struct pkg_data *), struct thread_data *thread_base,
1288 struct core_data *core_base, struct pkg_data *pkg_base,
1289 struct thread_data *thread_base2, struct core_data *core_base2,
1290 struct pkg_data *pkg_base2)
1292 int retval, pkg_no, core_no, thread_no;
1294 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1295 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1296 for (thread_no = 0; thread_no <
1297 topo.num_threads_per_core; ++thread_no) {
1298 struct thread_data *t, *t2;
1299 struct core_data *c, *c2;
1300 struct pkg_data *p, *p2;
1302 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1304 if (cpu_is_not_present(t->cpu_id))
1307 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1309 c = GET_CORE(core_base, core_no, pkg_no);
1310 c2 = GET_CORE(core_base2, core_no, pkg_no);
1312 p = GET_PKG(pkg_base, pkg_no);
1313 p2 = GET_PKG(pkg_base2, pkg_no);
1315 retval = func(t, c, p, t2, c2, p2);
1325 * run func(cpu) on every cpu in /proc/stat
1326 * return max_cpu number
1328 int for_all_proc_cpus(int (func)(int))
1334 fp = fopen(proc_stat, "r");
1340 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1342 perror("/proc/stat format");
1347 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1351 retval = func(cpu_num);
1361 void re_initialize(void)
1364 setup_all_buffers();
1365 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1371 * remember the last one seen, it will be the max
1373 int count_cpus(int cpu)
1375 if (topo.max_cpu_num < cpu)
1376 topo.max_cpu_num = cpu;
1381 int mark_cpu_present(int cpu)
1383 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1387 void turbostat_loop()
1395 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1398 } else if (retval == -1) {
1399 if (restarted > 1) {
1406 gettimeofday(&tv_even, (struct timezone *)NULL);
1409 if (for_all_proc_cpus(cpu_is_not_present)) {
1413 sleep(interval_sec);
1414 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1417 } else if (retval == -1) {
1421 gettimeofday(&tv_odd, (struct timezone *)NULL);
1422 timersub(&tv_odd, &tv_even, &tv_delta);
1423 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1424 compute_average(EVEN_COUNTERS);
1425 format_all_counters(EVEN_COUNTERS);
1427 sleep(interval_sec);
1428 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1431 } else if (retval == -1) {
1435 gettimeofday(&tv_even, (struct timezone *)NULL);
1436 timersub(&tv_even, &tv_odd, &tv_delta);
1437 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1438 compute_average(ODD_COUNTERS);
1439 format_all_counters(ODD_COUNTERS);
1444 void check_dev_msr()
1448 if (stat("/dev/cpu/0/msr", &sb)) {
1449 fprintf(stderr, "no /dev/cpu/0/msr\n");
1450 fprintf(stderr, "Try \"# modprobe msr\"\n");
1455 void check_super_user()
1457 if (getuid() != 0) {
1458 fprintf(stderr, "must be root\n");
1463 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1472 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1473 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1474 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1475 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1476 case 0x2C: /* Westmere EP - Gulftown */
1477 case 0x2A: /* SNB */
1478 case 0x2D: /* SNB Xeon */
1479 case 0x3A: /* IVB */
1480 case 0x3E: /* IVB Xeon */
1481 case 0x3C: /* HSW */
1482 case 0x3F: /* HSW */
1483 case 0x45: /* HSW */
1484 case 0x46: /* HSW */
1485 case 0x37: /* BYT */
1486 case 0x4D: /* AVN */
1488 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1489 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1494 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1503 case 0x3E: /* IVB Xeon */
1512 * Decode the ENERGY_PERF_BIAS MSR
1514 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1516 unsigned long long msr;
1525 /* EPB is per-package */
1526 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1529 if (cpu_migrate(cpu)) {
1530 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1534 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1537 switch (msr & 0x7) {
1538 case ENERGY_PERF_BIAS_PERFORMANCE:
1539 epb_string = "performance";
1541 case ENERGY_PERF_BIAS_NORMAL:
1542 epb_string = "balanced";
1544 case ENERGY_PERF_BIAS_POWERSAVE:
1545 epb_string = "powersave";
1548 epb_string = "custom";
1551 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1556 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1557 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1559 double get_tdp(model)
1561 unsigned long long msr;
1563 if (do_rapl & RAPL_PKG_POWER_INFO)
1564 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1565 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1580 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
1582 void rapl_probe(unsigned int family, unsigned int model)
1584 unsigned long long msr;
1585 unsigned int time_unit;
1597 case 0x3C: /* HSW */
1598 case 0x3F: /* HSW */
1599 case 0x45: /* HSW */
1600 case 0x46: /* HSW */
1601 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1605 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1607 case 0x37: /* BYT */
1608 case 0x4D: /* AVN */
1609 do_rapl = RAPL_PKG | RAPL_CORES ;
1615 /* units on package 0, verify later other packages match */
1616 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1619 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1621 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1623 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1625 time_unit = msr >> 16 & 0xF;
1629 rapl_time_units = 1.0 / (1 << (time_unit));
1631 tdp = get_tdp(model);
1633 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1635 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1640 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1642 unsigned long long msr;
1646 if (!(do_dts || do_ptm))
1651 /* DTS is per-core, no need to print for each thread */
1652 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1655 if (cpu_migrate(cpu)) {
1656 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1660 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1661 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1664 dts = (msr >> 16) & 0x7F;
1665 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1666 cpu, msr, tcc_activation_temp - dts);
1669 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1672 dts = (msr >> 16) & 0x7F;
1673 dts2 = (msr >> 8) & 0x7F;
1674 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1675 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1681 unsigned int resolution;
1683 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1686 dts = (msr >> 16) & 0x7F;
1687 resolution = (msr >> 27) & 0xF;
1688 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1689 cpu, msr, tcc_activation_temp - dts, resolution);
1692 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1695 dts = (msr >> 16) & 0x7F;
1696 dts2 = (msr >> 8) & 0x7F;
1697 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1698 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1705 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1707 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1709 ((msr >> 15) & 1) ? "EN" : "DIS",
1710 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1711 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1712 (((msr >> 16) & 1) ? "EN" : "DIS"));
1717 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1719 unsigned long long msr;
1725 /* RAPL counters are per package, so print only for 1st thread/package */
1726 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1730 if (cpu_migrate(cpu)) {
1731 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1735 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1739 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1740 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1741 rapl_power_units, rapl_energy_units, rapl_time_units);
1743 if (do_rapl & RAPL_PKG_POWER_INFO) {
1745 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1749 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1751 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1752 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1753 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1754 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1757 if (do_rapl & RAPL_PKG) {
1759 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1762 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1763 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1765 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1766 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1768 ((msr >> 47) & 1) ? "EN" : "DIS",
1769 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1770 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1771 ((msr >> 48) & 1) ? "EN" : "DIS");
1774 if (do_rapl & RAPL_DRAM) {
1775 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1779 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1781 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1782 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1783 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1784 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1787 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1789 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1790 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1792 print_power_limit_msr(cpu, msr, "DRAM Limit");
1794 if (do_rapl & RAPL_CORE_POLICY) {
1796 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1799 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1802 if (do_rapl & RAPL_CORES) {
1805 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1807 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1808 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1809 print_power_limit_msr(cpu, msr, "Cores Limit");
1812 if (do_rapl & RAPL_GFX) {
1814 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1817 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1819 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1821 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1822 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1823 print_power_limit_msr(cpu, msr, "GFX Limit");
1830 int is_snb(unsigned int family, unsigned int model)
1838 case 0x3A: /* IVB */
1839 case 0x3E: /* IVB Xeon */
1840 case 0x3C: /* HSW */
1841 case 0x3F: /* HSW */
1842 case 0x45: /* HSW */
1843 case 0x46: /* HSW */
1849 int has_c8_c9_c10(unsigned int family, unsigned int model)
1862 int is_slm(unsigned int family, unsigned int model)
1867 case 0x37: /* BYT */
1868 case 0x4D: /* AVN */
1874 #define SLM_BCLK_FREQS 5
1875 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1877 double slm_bclk(void)
1879 unsigned long long msr = 3;
1883 if (get_msr(0, MSR_FSB_FREQ, &msr))
1884 fprintf(stderr, "SLM BCLK: unknown\n");
1887 if (i >= SLM_BCLK_FREQS) {
1888 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1891 freq = slm_freq_table[i];
1893 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1898 double discover_bclk(unsigned int family, unsigned int model)
1900 if (is_snb(family, model))
1902 else if (is_slm(family, model))
1909 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1910 * the Thermal Control Circuit (TCC) activates.
1911 * This is usually equal to tjMax.
1913 * Older processors do not have this MSR, so there we guess,
1914 * but also allow cmdline over-ride with -T.
1916 * Several MSR temperature values are in units of degrees-C
1917 * below this value, including the Digital Thermal Sensor (DTS),
1918 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1920 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1922 unsigned long long msr;
1923 unsigned int target_c_local;
1926 /* tcc_activation_temp is used only for dts or ptm */
1927 if (!(do_dts || do_ptm))
1930 /* this is a per-package concept */
1931 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1935 if (cpu_migrate(cpu)) {
1936 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1940 if (tcc_activation_temp_override != 0) {
1941 tcc_activation_temp = tcc_activation_temp_override;
1942 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1943 cpu, tcc_activation_temp);
1947 /* Temperature Target MSR is Nehalem and newer only */
1948 if (!do_nehalem_platform_info)
1951 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1954 target_c_local = (msr >> 16) & 0x7F;
1957 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1958 cpu, msr, target_c_local);
1960 if (target_c_local < 85 || target_c_local > 127)
1963 tcc_activation_temp = target_c_local;
1968 tcc_activation_temp = TJMAX_DEFAULT;
1969 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1970 cpu, tcc_activation_temp);
1976 unsigned int eax, ebx, ecx, edx, max_level;
1977 unsigned int fms, family, model, stepping;
1979 eax = ebx = ecx = edx = 0;
1981 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1983 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1987 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
1988 (char *)&ebx, (char *)&edx, (char *)&ecx);
1990 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1991 family = (fms >> 8) & 0xf;
1992 model = (fms >> 4) & 0xf;
1993 stepping = fms & 0xf;
1994 if (family == 6 || family == 0xf)
1995 model += ((fms >> 16) & 0xf) << 4;
1998 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1999 max_level, family, model, stepping, family, model, stepping);
2001 if (!(edx & (1 << 5))) {
2002 fprintf(stderr, "CPUID: no MSR\n");
2007 * check max extended function levels of CPUID.
2008 * This is needed to check for invariant TSC.
2009 * This check is valid for both Intel and AMD.
2011 ebx = ecx = edx = 0;
2012 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
2014 if (max_level < 0x80000007) {
2015 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
2020 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2021 * this check is valid for both Intel and AMD
2023 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
2024 has_invariant_tsc = edx & (1 << 8);
2026 if (!has_invariant_tsc) {
2027 fprintf(stderr, "No invariant TSC\n");
2032 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2033 * this check is valid for both Intel and AMD
2036 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
2037 has_aperf = ecx & (1 << 0);
2038 do_dts = eax & (1 << 0);
2039 do_ptm = eax & (1 << 6);
2040 has_epb = ecx & (1 << 3);
2043 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2044 has_aperf ? "APERF" : "No APERF!",
2045 do_dts ? ", DTS" : "",
2046 do_ptm ? ", PTM": "",
2047 has_epb ? ", EPB": "");
2052 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2053 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
2054 do_smi = do_nhm_cstates;
2055 do_snb_cstates = is_snb(family, model);
2056 do_c8_c9_c10 = has_c8_c9_c10(family, model);
2057 do_slm_cstates = is_slm(family, model);
2058 bclk = discover_bclk(family, model);
2060 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
2061 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
2062 rapl_probe(family, model);
2070 fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
2077 * in /dev/cpu/ return success for names that are numbers
2078 * ie. filter out ".", "..", "microcode".
2080 int dir_filter(const struct dirent *dirp)
2082 if (isdigit(dirp->d_name[0]))
2088 int open_dev_cpu_msr(int dummy1)
2093 void topology_probe()
2096 int max_core_id = 0;
2097 int max_package_id = 0;
2098 int max_siblings = 0;
2099 struct cpu_topology {
2101 int physical_package_id;
2104 /* Initialize num_cpus, max_cpu_num */
2106 topo.max_cpu_num = 0;
2107 for_all_proc_cpus(count_cpus);
2108 if (!summary_only && topo.num_cpus > 1)
2112 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2114 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2116 perror("calloc cpus");
2121 * Allocate and initialize cpu_present_set
2123 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2124 if (cpu_present_set == NULL) {
2125 perror("CPU_ALLOC");
2128 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2129 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2130 for_all_proc_cpus(mark_cpu_present);
2133 * Allocate and initialize cpu_affinity_set
2135 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2136 if (cpu_affinity_set == NULL) {
2137 perror("CPU_ALLOC");
2140 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2141 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2146 * find max_core_id, max_package_id
2148 for (i = 0; i <= topo.max_cpu_num; ++i) {
2151 if (cpu_is_not_present(i)) {
2153 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2156 cpus[i].core_id = get_core_id(i);
2157 if (cpus[i].core_id > max_core_id)
2158 max_core_id = cpus[i].core_id;
2160 cpus[i].physical_package_id = get_physical_package_id(i);
2161 if (cpus[i].physical_package_id > max_package_id)
2162 max_package_id = cpus[i].physical_package_id;
2164 siblings = get_num_ht_siblings(i);
2165 if (siblings > max_siblings)
2166 max_siblings = siblings;
2168 fprintf(stderr, "cpu %d pkg %d core %d\n",
2169 i, cpus[i].physical_package_id, cpus[i].core_id);
2171 topo.num_cores_per_pkg = max_core_id + 1;
2173 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2174 max_core_id, topo.num_cores_per_pkg);
2175 if (!summary_only && topo.num_cores_per_pkg > 1)
2178 topo.num_packages = max_package_id + 1;
2180 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2181 max_package_id, topo.num_packages);
2182 if (!summary_only && topo.num_packages > 1)
2185 topo.num_threads_per_core = max_siblings;
2187 fprintf(stderr, "max_siblings %d\n", max_siblings);
2193 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2197 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2198 topo.num_packages, sizeof(struct thread_data));
2202 for (i = 0; i < topo.num_threads_per_core *
2203 topo.num_cores_per_pkg * topo.num_packages; i++)
2204 (*t)[i].cpu_id = -1;
2206 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2207 sizeof(struct core_data));
2211 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2212 (*c)[i].core_id = -1;
2214 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2218 for (i = 0; i < topo.num_packages; i++)
2219 (*p)[i].package_id = i;
2223 perror("calloc counters");
2229 * set cpu_id, core_num, pkg_num
2230 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2232 * increment topo.num_cores when 1st core in pkg seen
2234 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2235 struct pkg_data *pkg_base, int thread_num, int core_num,
2236 int pkg_num, int cpu_id)
2238 struct thread_data *t;
2239 struct core_data *c;
2242 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2243 c = GET_CORE(core_base, core_num, pkg_num);
2244 p = GET_PKG(pkg_base, pkg_num);
2247 if (thread_num == 0) {
2248 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2249 if (cpu_is_first_core_in_package(cpu_id))
2250 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2253 c->core_id = core_num;
2254 p->package_id = pkg_num;
2258 int initialize_counters(int cpu_id)
2260 int my_thread_id, my_core_id, my_package_id;
2262 my_package_id = get_physical_package_id(cpu_id);
2263 my_core_id = get_core_id(cpu_id);
2265 if (cpu_is_first_sibling_in_core(cpu_id)) {
2272 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2273 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2277 void allocate_output_buffer()
2279 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
2280 outp = output_buffer;
2287 void setup_all_buffers(void)
2290 allocate_counters(&thread_even, &core_even, &package_even);
2291 allocate_counters(&thread_odd, &core_odd, &package_odd);
2292 allocate_output_buffer();
2293 for_all_proc_cpus(initialize_counters);
2295 void turbostat_init()
2302 setup_all_buffers();
2305 print_verbose_header();
2308 for_all_cpus(print_epb, ODD_COUNTERS);
2311 for_all_cpus(print_rapl, ODD_COUNTERS);
2313 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2316 for_all_cpus(print_thermal, ODD_COUNTERS);
2319 int fork_it(char **argv)
2324 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2327 /* clear affinity side-effect of get_counters() */
2328 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2329 gettimeofday(&tv_even, (struct timezone *)NULL);
2334 execvp(argv[0], argv);
2338 if (child_pid == -1) {
2343 signal(SIGINT, SIG_IGN);
2344 signal(SIGQUIT, SIG_IGN);
2345 if (waitpid(child_pid, &status, 0) == -1) {
2351 * n.b. fork_it() does not check for errors from for_all_cpus()
2352 * because re-starting is problematic when forking
2354 for_all_cpus(get_counters, ODD_COUNTERS);
2355 gettimeofday(&tv_odd, (struct timezone *)NULL);
2356 timersub(&tv_odd, &tv_even, &tv_delta);
2357 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2358 compute_average(EVEN_COUNTERS);
2359 format_all_counters(EVEN_COUNTERS);
2362 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2367 void cmdline(int argc, char **argv)
2373 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
2388 interval_sec = atoi(optarg);
2391 sscanf(optarg, "%x", &extra_delta_offset32);
2394 sscanf(optarg, "%x", &extra_delta_offset64);
2397 sscanf(optarg, "%x", &extra_msr_offset32);
2400 sscanf(optarg, "%x", &extra_msr_offset64);
2406 tcc_activation_temp_override = atoi(optarg);
2414 int main(int argc, char **argv)
2416 cmdline(argc, argv);
2419 fprintf(stderr, "turbostat v3.5 April 26, 2013"
2420 " - Len Brown <lenb@kernel.org>\n");
2425 * if any params left, it must be a command to fork
2428 return fork_it(argv + optind);