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.
28 #include <sys/types.h>
31 #include <sys/resource.h>
43 #include <linux/capability.h>
46 char *proc_stat = "/proc/stat";
47 struct timespec interval_ts = {5, 0};
49 unsigned int rapl_joules;
50 unsigned int summary_only;
51 unsigned int dump_only;
54 unsigned int do_nhm_cstates;
55 unsigned int do_snb_cstates;
56 unsigned int do_knl_cstates;
61 unsigned int do_c8_c9_c10;
62 unsigned int do_skl_residency;
63 unsigned int do_slm_cstates;
64 unsigned int use_c1_residency_msr;
65 unsigned int has_aperf;
67 unsigned int units = 1000000; /* MHz etc */
68 unsigned int genuine_intel;
69 unsigned int has_invariant_tsc;
70 unsigned int do_nhm_platform_info;
71 unsigned int extra_msr_offset32;
72 unsigned int extra_msr_offset64;
73 unsigned int extra_delta_offset32;
74 unsigned int extra_delta_offset64;
75 unsigned int aperf_mperf_multiplier = 1;
79 unsigned int has_base_hz;
80 double tsc_tweak = 1.0;
81 unsigned int show_pkg;
82 unsigned int show_core;
83 unsigned int show_cpu;
84 unsigned int show_pkg_only;
85 unsigned int show_core_only;
86 char *output_buffer, *outp;
90 unsigned int tcc_activation_temp;
91 unsigned int tcc_activation_temp_override;
92 double rapl_power_units, rapl_time_units;
93 double rapl_dram_energy_units, rapl_energy_units;
94 double rapl_joule_counter_range;
95 unsigned int do_core_perf_limit_reasons;
96 unsigned int do_gfx_perf_limit_reasons;
97 unsigned int do_ring_perf_limit_reasons;
98 unsigned int crystal_hz;
99 unsigned long long tsc_hz;
101 double discover_bclk(unsigned int family, unsigned int model);
102 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
103 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
104 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
105 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
106 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
107 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
109 #define RAPL_PKG (1 << 0)
110 /* 0x610 MSR_PKG_POWER_LIMIT */
111 /* 0x611 MSR_PKG_ENERGY_STATUS */
112 #define RAPL_PKG_PERF_STATUS (1 << 1)
113 /* 0x613 MSR_PKG_PERF_STATUS */
114 #define RAPL_PKG_POWER_INFO (1 << 2)
115 /* 0x614 MSR_PKG_POWER_INFO */
117 #define RAPL_DRAM (1 << 3)
118 /* 0x618 MSR_DRAM_POWER_LIMIT */
119 /* 0x619 MSR_DRAM_ENERGY_STATUS */
120 #define RAPL_DRAM_PERF_STATUS (1 << 4)
121 /* 0x61b MSR_DRAM_PERF_STATUS */
122 #define RAPL_DRAM_POWER_INFO (1 << 5)
123 /* 0x61c MSR_DRAM_POWER_INFO */
125 #define RAPL_CORES (1 << 6)
126 /* 0x638 MSR_PP0_POWER_LIMIT */
127 /* 0x639 MSR_PP0_ENERGY_STATUS */
128 #define RAPL_CORE_POLICY (1 << 7)
129 /* 0x63a MSR_PP0_POLICY */
131 #define RAPL_GFX (1 << 8)
132 /* 0x640 MSR_PP1_POWER_LIMIT */
133 /* 0x641 MSR_PP1_ENERGY_STATUS */
134 /* 0x642 MSR_PP1_POLICY */
135 #define TJMAX_DEFAULT 100
137 #define MAX(a, b) ((a) > (b) ? (a) : (b))
139 int aperf_mperf_unstable;
143 cpu_set_t *cpu_present_set, *cpu_affinity_set;
144 size_t cpu_present_setsize, cpu_affinity_setsize;
147 unsigned long long tsc;
148 unsigned long long aperf;
149 unsigned long long mperf;
150 unsigned long long c1;
151 unsigned long long extra_msr64;
152 unsigned long long extra_delta64;
153 unsigned long long extra_msr32;
154 unsigned long long extra_delta32;
155 unsigned int smi_count;
158 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
159 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
160 } *thread_even, *thread_odd;
163 unsigned long long c3;
164 unsigned long long c6;
165 unsigned long long c7;
166 unsigned int core_temp_c;
167 unsigned int core_id;
168 } *core_even, *core_odd;
171 unsigned long long pc2;
172 unsigned long long pc3;
173 unsigned long long pc6;
174 unsigned long long pc7;
175 unsigned long long pc8;
176 unsigned long long pc9;
177 unsigned long long pc10;
178 unsigned long long pkg_wtd_core_c0;
179 unsigned long long pkg_any_core_c0;
180 unsigned long long pkg_any_gfxe_c0;
181 unsigned long long pkg_both_core_gfxe_c0;
182 unsigned int package_id;
183 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
184 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
185 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
186 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
187 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
188 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
189 unsigned int pkg_temp_c;
191 } *package_even, *package_odd;
193 #define ODD_COUNTERS thread_odd, core_odd, package_odd
194 #define EVEN_COUNTERS thread_even, core_even, package_even
196 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
197 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
198 topo.num_threads_per_core + \
199 (core_no) * topo.num_threads_per_core + (thread_no))
200 #define GET_CORE(core_base, core_no, pkg_no) \
201 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
202 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
204 struct system_summary {
205 struct thread_data threads;
206 struct core_data cores;
207 struct pkg_data packages;
216 int num_cores_per_pkg;
217 int num_threads_per_core;
220 struct timeval tv_even, tv_odd, tv_delta;
222 void setup_all_buffers(void);
224 int cpu_is_not_present(int cpu)
226 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
229 * run func(thread, core, package) in topology order
230 * skip non-present cpus
233 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
234 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
236 int retval, pkg_no, core_no, thread_no;
238 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
239 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
240 for (thread_no = 0; thread_no <
241 topo.num_threads_per_core; ++thread_no) {
242 struct thread_data *t;
246 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
248 if (cpu_is_not_present(t->cpu_id))
251 c = GET_CORE(core_base, core_no, pkg_no);
252 p = GET_PKG(pkg_base, pkg_no);
254 retval = func(t, c, p);
263 int cpu_migrate(int cpu)
265 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
266 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
267 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
273 int get_msr(int cpu, off_t offset, unsigned long long *msr)
279 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
280 fd = open(pathname, O_RDONLY);
282 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
284 retval = pread(fd, msr, sizeof *msr, offset);
287 if (retval != sizeof *msr)
288 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
294 * Example Format w/ field column widths:
296 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
297 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
300 void print_header(void)
303 outp += sprintf(outp, " Package");
305 outp += sprintf(outp, " Core");
307 outp += sprintf(outp, " CPU");
309 outp += sprintf(outp, " Avg_MHz");
311 outp += sprintf(outp, " %%Busy");
313 outp += sprintf(outp, " Bzy_MHz");
314 outp += sprintf(outp, " TSC_MHz");
316 if (extra_delta_offset32)
317 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
318 if (extra_delta_offset64)
319 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
320 if (extra_msr_offset32)
321 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
322 if (extra_msr_offset64)
323 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
329 outp += sprintf(outp, " SMI");
332 outp += sprintf(outp, " CPU%%c1");
333 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
334 outp += sprintf(outp, " CPU%%c3");
336 outp += sprintf(outp, " CPU%%c6");
338 outp += sprintf(outp, " CPU%%c7");
341 outp += sprintf(outp, " CoreTmp");
343 outp += sprintf(outp, " PkgTmp");
345 if (do_skl_residency) {
346 outp += sprintf(outp, " Totl%%C0");
347 outp += sprintf(outp, " Any%%C0");
348 outp += sprintf(outp, " GFX%%C0");
349 outp += sprintf(outp, " CPUGFX%%");
353 outp += sprintf(outp, " Pkg%%pc2");
355 outp += sprintf(outp, " Pkg%%pc3");
357 outp += sprintf(outp, " Pkg%%pc6");
359 outp += sprintf(outp, " Pkg%%pc7");
361 outp += sprintf(outp, " Pkg%%pc8");
362 outp += sprintf(outp, " Pkg%%pc9");
363 outp += sprintf(outp, " Pk%%pc10");
366 if (do_rapl && !rapl_joules) {
367 if (do_rapl & RAPL_PKG)
368 outp += sprintf(outp, " PkgWatt");
369 if (do_rapl & RAPL_CORES)
370 outp += sprintf(outp, " CorWatt");
371 if (do_rapl & RAPL_GFX)
372 outp += sprintf(outp, " GFXWatt");
373 if (do_rapl & RAPL_DRAM)
374 outp += sprintf(outp, " RAMWatt");
375 if (do_rapl & RAPL_PKG_PERF_STATUS)
376 outp += sprintf(outp, " PKG_%%");
377 if (do_rapl & RAPL_DRAM_PERF_STATUS)
378 outp += sprintf(outp, " RAM_%%");
379 } else if (do_rapl && rapl_joules) {
380 if (do_rapl & RAPL_PKG)
381 outp += sprintf(outp, " Pkg_J");
382 if (do_rapl & RAPL_CORES)
383 outp += sprintf(outp, " Cor_J");
384 if (do_rapl & RAPL_GFX)
385 outp += sprintf(outp, " GFX_J");
386 if (do_rapl & RAPL_DRAM)
387 outp += sprintf(outp, " RAM_J");
388 if (do_rapl & RAPL_PKG_PERF_STATUS)
389 outp += sprintf(outp, " PKG_%%");
390 if (do_rapl & RAPL_DRAM_PERF_STATUS)
391 outp += sprintf(outp, " RAM_%%");
392 outp += sprintf(outp, " time");
396 outp += sprintf(outp, "\n");
399 int dump_counters(struct thread_data *t, struct core_data *c,
402 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
405 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
406 t->cpu_id, t->flags);
407 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
408 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
409 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
410 outp += sprintf(outp, "c1: %016llX\n", t->c1);
411 outp += sprintf(outp, "msr0x%x: %08llX\n",
412 extra_delta_offset32, t->extra_delta32);
413 outp += sprintf(outp, "msr0x%x: %016llX\n",
414 extra_delta_offset64, t->extra_delta64);
415 outp += sprintf(outp, "msr0x%x: %08llX\n",
416 extra_msr_offset32, t->extra_msr32);
417 outp += sprintf(outp, "msr0x%x: %016llX\n",
418 extra_msr_offset64, t->extra_msr64);
420 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
424 outp += sprintf(outp, "core: %d\n", c->core_id);
425 outp += sprintf(outp, "c3: %016llX\n", c->c3);
426 outp += sprintf(outp, "c6: %016llX\n", c->c6);
427 outp += sprintf(outp, "c7: %016llX\n", c->c7);
428 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
432 outp += sprintf(outp, "package: %d\n", p->package_id);
434 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
435 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
436 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
437 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
439 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
441 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
443 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
445 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
446 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
447 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
448 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
449 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
450 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
451 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
452 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
453 outp += sprintf(outp, "Throttle PKG: %0X\n",
454 p->rapl_pkg_perf_status);
455 outp += sprintf(outp, "Throttle RAM: %0X\n",
456 p->rapl_dram_perf_status);
457 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
460 outp += sprintf(outp, "\n");
466 * column formatting convention & formats
468 int format_counters(struct thread_data *t, struct core_data *c,
471 double interval_float;
474 /* if showing only 1st thread in core and this isn't one, bail out */
475 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
478 /* if showing only 1st thread in pkg and this isn't one, bail out */
479 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
482 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
484 /* topo columns, print blanks on 1st (average) line */
485 if (t == &average.threads) {
487 outp += sprintf(outp, " -");
489 outp += sprintf(outp, " -");
491 outp += sprintf(outp, " -");
495 outp += sprintf(outp, "%8d", p->package_id);
497 outp += sprintf(outp, " -");
501 outp += sprintf(outp, "%8d", c->core_id);
503 outp += sprintf(outp, " -");
506 outp += sprintf(outp, "%8d", t->cpu_id);
511 outp += sprintf(outp, "%8.0f",
512 1.0 / units * t->aperf / interval_float);
517 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
519 outp += sprintf(outp, "********");
525 outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf);
527 outp += sprintf(outp, "%8.0f",
528 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
532 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
535 if (extra_delta_offset32)
536 outp += sprintf(outp, " %11llu", t->extra_delta32);
539 if (extra_delta_offset64)
540 outp += sprintf(outp, " %11llu", t->extra_delta64);
542 if (extra_msr_offset32)
543 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
546 if (extra_msr_offset64)
547 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
554 outp += sprintf(outp, "%8d", t->smi_count);
556 if (do_nhm_cstates) {
558 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
560 outp += sprintf(outp, "********");
563 /* print per-core data only for 1st thread in core */
564 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
567 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
568 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
570 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
572 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
575 outp += sprintf(outp, "%8d", c->core_temp_c);
577 /* print per-package data only for 1st core in package */
578 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
583 outp += sprintf(outp, "%8d", p->pkg_temp_c);
585 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
586 if (do_skl_residency) {
587 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
588 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
589 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
590 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
594 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
596 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
598 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
600 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
602 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
603 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
604 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
608 * If measurement interval exceeds minimum RAPL Joule Counter range,
609 * indicate that results are suspect by printing "**" in fraction place.
611 if (interval_float < rapl_joule_counter_range)
616 if (do_rapl && !rapl_joules) {
617 if (do_rapl & RAPL_PKG)
618 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
619 if (do_rapl & RAPL_CORES)
620 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
621 if (do_rapl & RAPL_GFX)
622 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
623 if (do_rapl & RAPL_DRAM)
624 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
625 if (do_rapl & RAPL_PKG_PERF_STATUS)
626 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
627 if (do_rapl & RAPL_DRAM_PERF_STATUS)
628 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
629 } else if (do_rapl && rapl_joules) {
630 if (do_rapl & RAPL_PKG)
631 outp += sprintf(outp, fmt8,
632 p->energy_pkg * rapl_energy_units);
633 if (do_rapl & RAPL_CORES)
634 outp += sprintf(outp, fmt8,
635 p->energy_cores * rapl_energy_units);
636 if (do_rapl & RAPL_GFX)
637 outp += sprintf(outp, fmt8,
638 p->energy_gfx * rapl_energy_units);
639 if (do_rapl & RAPL_DRAM)
640 outp += sprintf(outp, fmt8,
641 p->energy_dram * rapl_dram_energy_units);
642 if (do_rapl & RAPL_PKG_PERF_STATUS)
643 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
644 if (do_rapl & RAPL_DRAM_PERF_STATUS)
645 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
647 outp += sprintf(outp, fmt8, interval_float);
650 outp += sprintf(outp, "\n");
657 fputs(output_buffer, stdout);
659 outp = output_buffer;
663 fputs(output_buffer, stderr);
664 outp = output_buffer;
666 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
670 if (!printed || !summary_only)
673 if (topo.num_cpus > 1)
674 format_counters(&average.threads, &average.cores,
682 for_all_cpus(format_counters, t, c, p);
685 #define DELTA_WRAP32(new, old) \
689 old = 0x100000000 + new - old; \
693 delta_package(struct pkg_data *new, struct pkg_data *old)
696 if (do_skl_residency) {
697 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
698 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
699 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
700 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
702 old->pc2 = new->pc2 - old->pc2;
704 old->pc3 = new->pc3 - old->pc3;
706 old->pc6 = new->pc6 - old->pc6;
708 old->pc7 = new->pc7 - old->pc7;
709 old->pc8 = new->pc8 - old->pc8;
710 old->pc9 = new->pc9 - old->pc9;
711 old->pc10 = new->pc10 - old->pc10;
712 old->pkg_temp_c = new->pkg_temp_c;
714 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
715 DELTA_WRAP32(new->energy_cores, old->energy_cores);
716 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
717 DELTA_WRAP32(new->energy_dram, old->energy_dram);
718 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
719 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
723 delta_core(struct core_data *new, struct core_data *old)
725 old->c3 = new->c3 - old->c3;
726 old->c6 = new->c6 - old->c6;
727 old->c7 = new->c7 - old->c7;
728 old->core_temp_c = new->core_temp_c;
735 delta_thread(struct thread_data *new, struct thread_data *old,
736 struct core_data *core_delta)
738 old->tsc = new->tsc - old->tsc;
740 /* check for TSC < 1 Mcycles over interval */
741 if (old->tsc < (1000 * 1000))
742 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
743 "You can disable all c-states by booting with \"idle=poll\"\n"
744 "or just the deep ones with \"processor.max_cstate=1\"");
746 old->c1 = new->c1 - old->c1;
749 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
750 old->aperf = new->aperf - old->aperf;
751 old->mperf = new->mperf - old->mperf;
754 if (!aperf_mperf_unstable) {
755 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
756 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
757 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
759 aperf_mperf_unstable = 1;
762 * mperf delta is likely a huge "positive" number
763 * can not use it for calculating c0 time
771 if (use_c1_residency_msr) {
773 * Some models have a dedicated C1 residency MSR,
774 * which should be more accurate than the derivation below.
778 * As counter collection is not atomic,
779 * it is possible for mperf's non-halted cycles + idle states
780 * to exceed TSC's all cycles: show c1 = 0% in that case.
782 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
785 /* normal case, derive c1 */
786 old->c1 = old->tsc - old->mperf - core_delta->c3
787 - core_delta->c6 - core_delta->c7;
791 if (old->mperf == 0) {
792 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
793 old->mperf = 1; /* divide by 0 protection */
796 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
797 old->extra_delta32 &= 0xFFFFFFFF;
799 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
802 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
804 old->extra_msr32 = new->extra_msr32;
805 old->extra_msr64 = new->extra_msr64;
808 old->smi_count = new->smi_count - old->smi_count;
811 int delta_cpu(struct thread_data *t, struct core_data *c,
812 struct pkg_data *p, struct thread_data *t2,
813 struct core_data *c2, struct pkg_data *p2)
815 /* calculate core delta only for 1st thread in core */
816 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
819 /* always calculate thread delta */
820 delta_thread(t, t2, c2); /* c2 is core delta */
822 /* calculate package delta only for 1st core in package */
823 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
824 delta_package(p, p2);
829 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
837 t->extra_delta32 = 0;
838 t->extra_delta64 = 0;
840 /* tells format_counters to dump all fields from this set */
841 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
848 p->pkg_wtd_core_c0 = 0;
849 p->pkg_any_core_c0 = 0;
850 p->pkg_any_gfxe_c0 = 0;
851 p->pkg_both_core_gfxe_c0 = 0;
868 p->rapl_pkg_perf_status = 0;
869 p->rapl_dram_perf_status = 0;
872 int sum_counters(struct thread_data *t, struct core_data *c,
875 average.threads.tsc += t->tsc;
876 average.threads.aperf += t->aperf;
877 average.threads.mperf += t->mperf;
878 average.threads.c1 += t->c1;
880 average.threads.extra_delta32 += t->extra_delta32;
881 average.threads.extra_delta64 += t->extra_delta64;
883 /* sum per-core values only for 1st thread in core */
884 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
887 average.cores.c3 += c->c3;
888 average.cores.c6 += c->c6;
889 average.cores.c7 += c->c7;
891 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
893 /* sum per-pkg values only for 1st core in pkg */
894 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
897 if (do_skl_residency) {
898 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
899 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
900 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
901 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
904 average.packages.pc2 += p->pc2;
906 average.packages.pc3 += p->pc3;
908 average.packages.pc6 += p->pc6;
910 average.packages.pc7 += p->pc7;
911 average.packages.pc8 += p->pc8;
912 average.packages.pc9 += p->pc9;
913 average.packages.pc10 += p->pc10;
915 average.packages.energy_pkg += p->energy_pkg;
916 average.packages.energy_dram += p->energy_dram;
917 average.packages.energy_cores += p->energy_cores;
918 average.packages.energy_gfx += p->energy_gfx;
920 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
922 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
923 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
927 * sum the counters for all cpus in the system
928 * compute the weighted average
930 void compute_average(struct thread_data *t, struct core_data *c,
933 clear_counters(&average.threads, &average.cores, &average.packages);
935 for_all_cpus(sum_counters, t, c, p);
937 average.threads.tsc /= topo.num_cpus;
938 average.threads.aperf /= topo.num_cpus;
939 average.threads.mperf /= topo.num_cpus;
940 average.threads.c1 /= topo.num_cpus;
942 average.threads.extra_delta32 /= topo.num_cpus;
943 average.threads.extra_delta32 &= 0xFFFFFFFF;
945 average.threads.extra_delta64 /= topo.num_cpus;
947 average.cores.c3 /= topo.num_cores;
948 average.cores.c6 /= topo.num_cores;
949 average.cores.c7 /= topo.num_cores;
951 if (do_skl_residency) {
952 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
953 average.packages.pkg_any_core_c0 /= topo.num_packages;
954 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
955 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
958 average.packages.pc2 /= topo.num_packages;
960 average.packages.pc3 /= topo.num_packages;
962 average.packages.pc6 /= topo.num_packages;
964 average.packages.pc7 /= topo.num_packages;
966 average.packages.pc8 /= topo.num_packages;
967 average.packages.pc9 /= topo.num_packages;
968 average.packages.pc10 /= topo.num_packages;
971 static unsigned long long rdtsc(void)
973 unsigned int low, high;
975 asm volatile("rdtsc" : "=a" (low), "=d" (high));
977 return low | ((unsigned long long)high) << 32;
984 * acquire and record local counters for that cpu
986 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
989 unsigned long long msr;
991 if (cpu_migrate(cpu)) {
992 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
996 t->tsc = rdtsc(); /* we are running on local CPU of interest */
999 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1001 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1003 t->aperf = t->aperf * aperf_mperf_multiplier;
1004 t->mperf = t->mperf * aperf_mperf_multiplier;
1008 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1010 t->smi_count = msr & 0xFFFFFFFF;
1012 if (extra_delta_offset32) {
1013 if (get_msr(cpu, extra_delta_offset32, &msr))
1015 t->extra_delta32 = msr & 0xFFFFFFFF;
1018 if (extra_delta_offset64)
1019 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1022 if (extra_msr_offset32) {
1023 if (get_msr(cpu, extra_msr_offset32, &msr))
1025 t->extra_msr32 = msr & 0xFFFFFFFF;
1028 if (extra_msr_offset64)
1029 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
1032 if (use_c1_residency_msr) {
1033 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1037 /* collect core counters only for 1st thread in core */
1038 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1041 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
1042 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1046 if (do_nhm_cstates && !do_knl_cstates) {
1047 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1049 } else if (do_knl_cstates) {
1050 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1055 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1059 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1061 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1065 /* collect package counters only for 1st core in package */
1066 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1069 if (do_skl_residency) {
1070 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1072 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1074 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1076 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1080 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1083 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1086 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1089 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1092 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1094 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1096 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1099 if (do_rapl & RAPL_PKG) {
1100 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1102 p->energy_pkg = msr & 0xFFFFFFFF;
1104 if (do_rapl & RAPL_CORES) {
1105 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1107 p->energy_cores = msr & 0xFFFFFFFF;
1109 if (do_rapl & RAPL_DRAM) {
1110 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1112 p->energy_dram = msr & 0xFFFFFFFF;
1114 if (do_rapl & RAPL_GFX) {
1115 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1117 p->energy_gfx = msr & 0xFFFFFFFF;
1119 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1120 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1122 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1124 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1125 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1127 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1130 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1132 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1138 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1139 * If you change the values, note they are used both in comparisons
1140 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1143 #define PCLUKN 0 /* Unknown */
1144 #define PCLRSV 1 /* Reserved */
1145 #define PCL__0 2 /* PC0 */
1146 #define PCL__1 3 /* PC1 */
1147 #define PCL__2 4 /* PC2 */
1148 #define PCL__3 5 /* PC3 */
1149 #define PCL__4 6 /* PC4 */
1150 #define PCL__6 7 /* PC6 */
1151 #define PCL_6N 8 /* PC6 No Retention */
1152 #define PCL_6R 9 /* PC6 Retention */
1153 #define PCL__7 10 /* PC7 */
1154 #define PCL_7S 11 /* PC7 Shrink */
1155 #define PCL__8 12 /* PC8 */
1156 #define PCL__9 13 /* PC9 */
1157 #define PCLUNL 14 /* Unlimited */
1159 int pkg_cstate_limit = PCLUKN;
1160 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1161 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1163 int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1164 int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1165 int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1166 int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1167 int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1168 int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1172 calculate_tsc_tweak()
1174 tsc_tweak = base_hz / tsc_hz;
1178 dump_nhm_platform_info(void)
1180 unsigned long long msr;
1183 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1185 fprintf(stderr, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
1187 ratio = (msr >> 40) & 0xFF;
1188 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1189 ratio, bclk, ratio * bclk);
1191 ratio = (msr >> 8) & 0xFF;
1192 fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
1193 ratio, bclk, ratio * bclk);
1195 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1196 fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1197 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
1203 dump_hsw_turbo_ratio_limits(void)
1205 unsigned long long msr;
1208 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1210 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1212 ratio = (msr >> 8) & 0xFF;
1214 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1215 ratio, bclk, ratio * bclk);
1217 ratio = (msr >> 0) & 0xFF;
1219 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1220 ratio, bclk, ratio * bclk);
1225 dump_ivt_turbo_ratio_limits(void)
1227 unsigned long long msr;
1230 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
1232 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
1234 ratio = (msr >> 56) & 0xFF;
1236 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1237 ratio, bclk, ratio * bclk);
1239 ratio = (msr >> 48) & 0xFF;
1241 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1242 ratio, bclk, ratio * bclk);
1244 ratio = (msr >> 40) & 0xFF;
1246 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1247 ratio, bclk, ratio * bclk);
1249 ratio = (msr >> 32) & 0xFF;
1251 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1252 ratio, bclk, ratio * bclk);
1254 ratio = (msr >> 24) & 0xFF;
1256 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1257 ratio, bclk, ratio * bclk);
1259 ratio = (msr >> 16) & 0xFF;
1261 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1262 ratio, bclk, ratio * bclk);
1264 ratio = (msr >> 8) & 0xFF;
1266 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1267 ratio, bclk, ratio * bclk);
1269 ratio = (msr >> 0) & 0xFF;
1271 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1272 ratio, bclk, ratio * bclk);
1277 dump_nhm_turbo_ratio_limits(void)
1279 unsigned long long msr;
1282 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1284 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
1286 ratio = (msr >> 56) & 0xFF;
1288 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1289 ratio, bclk, ratio * bclk);
1291 ratio = (msr >> 48) & 0xFF;
1293 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1294 ratio, bclk, ratio * bclk);
1296 ratio = (msr >> 40) & 0xFF;
1298 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1299 ratio, bclk, ratio * bclk);
1301 ratio = (msr >> 32) & 0xFF;
1303 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1304 ratio, bclk, ratio * bclk);
1306 ratio = (msr >> 24) & 0xFF;
1308 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1309 ratio, bclk, ratio * bclk);
1311 ratio = (msr >> 16) & 0xFF;
1313 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1314 ratio, bclk, ratio * bclk);
1316 ratio = (msr >> 8) & 0xFF;
1318 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1319 ratio, bclk, ratio * bclk);
1321 ratio = (msr >> 0) & 0xFF;
1323 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1324 ratio, bclk, ratio * bclk);
1329 dump_knl_turbo_ratio_limits(void)
1331 const unsigned int buckets_no = 7;
1333 unsigned long long msr;
1334 int delta_cores, delta_ratio;
1336 unsigned int cores[buckets_no];
1337 unsigned int ratio[buckets_no];
1339 get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1341 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
1345 * Turbo encoding in KNL is as follows:
1347 * [7:1] -- Base value of number of active cores of bucket 1.
1348 * [15:8] -- Base value of freq ratio of bucket 1.
1349 * [20:16] -- +ve delta of number of active cores of bucket 2.
1350 * i.e. active cores of bucket 2 =
1351 * active cores of bucket 1 + delta
1352 * [23:21] -- Negative delta of freq ratio of bucket 2.
1353 * i.e. freq ratio of bucket 2 =
1354 * freq ratio of bucket 1 - delta
1355 * [28:24]-- +ve delta of number of active cores of bucket 3.
1356 * [31:29]-- -ve delta of freq ratio of bucket 3.
1357 * [36:32]-- +ve delta of number of active cores of bucket 4.
1358 * [39:37]-- -ve delta of freq ratio of bucket 4.
1359 * [44:40]-- +ve delta of number of active cores of bucket 5.
1360 * [47:45]-- -ve delta of freq ratio of bucket 5.
1361 * [52:48]-- +ve delta of number of active cores of bucket 6.
1362 * [55:53]-- -ve delta of freq ratio of bucket 6.
1363 * [60:56]-- +ve delta of number of active cores of bucket 7.
1364 * [63:61]-- -ve delta of freq ratio of bucket 7.
1368 cores[b_nr] = (msr & 0xFF) >> 1;
1369 ratio[b_nr] = (msr >> 8) & 0xFF;
1371 for (i = 16; i < 64; i += 8) {
1372 delta_cores = (msr >> i) & 0x1F;
1373 delta_ratio = (msr >> (i + 5)) & 0x7;
1375 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1376 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1380 for (i = buckets_no - 1; i >= 0; i--)
1381 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
1383 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1384 ratio[i], bclk, ratio[i] * bclk, cores[i]);
1388 dump_nhm_cst_cfg(void)
1390 unsigned long long msr;
1392 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1394 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1395 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1397 fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
1399 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1400 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1401 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1402 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1403 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1404 (msr & (1 << 15)) ? "" : "UN",
1405 (unsigned int)msr & 7,
1406 pkg_cstate_limit_strings[pkg_cstate_limit]);
1411 dump_config_tdp(void)
1413 unsigned long long msr;
1415 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1416 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1417 fprintf(stderr, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF);
1419 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1420 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
1422 fprintf(stderr, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1423 fprintf(stderr, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1424 fprintf(stderr, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1425 fprintf(stderr, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF);
1427 fprintf(stderr, ")\n");
1429 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1430 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
1432 fprintf(stderr, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1433 fprintf(stderr, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1434 fprintf(stderr, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1435 fprintf(stderr, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF);
1437 fprintf(stderr, ")\n");
1439 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1440 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
1442 fprintf(stderr, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1443 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1444 fprintf(stderr, ")\n");
1446 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1447 fprintf(stderr, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1448 fprintf(stderr, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F);
1449 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1450 fprintf(stderr, ")\n");
1453 void free_all_buffers(void)
1455 CPU_FREE(cpu_present_set);
1456 cpu_present_set = NULL;
1457 cpu_present_set = 0;
1459 CPU_FREE(cpu_affinity_set);
1460 cpu_affinity_set = NULL;
1461 cpu_affinity_setsize = 0;
1469 package_even = NULL;
1479 free(output_buffer);
1480 output_buffer = NULL;
1485 * Open a file, and exit on failure
1487 FILE *fopen_or_die(const char *path, const char *mode)
1489 FILE *filep = fopen(path, "r");
1491 err(1, "%s: open failed", path);
1496 * Parse a file containing a single int.
1498 int parse_int_file(const char *fmt, ...)
1501 char path[PATH_MAX];
1505 va_start(args, fmt);
1506 vsnprintf(path, sizeof(path), fmt, args);
1508 filep = fopen_or_die(path, "r");
1509 if (fscanf(filep, "%d", &value) != 1)
1510 err(1, "%s: failed to parse number from file", path);
1516 * get_cpu_position_in_core(cpu)
1517 * return the position of the CPU among its HT siblings in the core
1518 * return -1 if the sibling is not in list
1520 int get_cpu_position_in_core(int cpu)
1529 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1531 filep = fopen(path, "r");
1532 if (filep == NULL) {
1537 for (i = 0; i < topo.num_threads_per_core; i++) {
1538 fscanf(filep, "%d", &this_cpu);
1539 if (this_cpu == cpu) {
1544 /* Account for no separator after last thread*/
1545 if (i != (topo.num_threads_per_core - 1))
1546 fscanf(filep, "%c", &character);
1554 * cpu_is_first_core_in_package(cpu)
1555 * return 1 if given CPU is 1st core in package
1557 int cpu_is_first_core_in_package(int cpu)
1559 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1562 int get_physical_package_id(int cpu)
1564 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1567 int get_core_id(int cpu)
1569 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1572 int get_num_ht_siblings(int cpu)
1582 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1583 filep = fopen_or_die(path, "r");
1587 * A ',' separated or '-' separated set of numbers
1588 * (eg 1-2 or 1,3,4,5)
1590 fscanf(filep, "%d%c\n", &sib1, &character);
1591 fseek(filep, 0, SEEK_SET);
1592 fgets(str, 100, filep);
1593 ch = strchr(str, character);
1594 while (ch != NULL) {
1596 ch = strchr(ch+1, character);
1604 * run func(thread, core, package) in topology order
1605 * skip non-present cpus
1608 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1609 struct pkg_data *, struct thread_data *, struct core_data *,
1610 struct pkg_data *), struct thread_data *thread_base,
1611 struct core_data *core_base, struct pkg_data *pkg_base,
1612 struct thread_data *thread_base2, struct core_data *core_base2,
1613 struct pkg_data *pkg_base2)
1615 int retval, pkg_no, core_no, thread_no;
1617 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1618 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1619 for (thread_no = 0; thread_no <
1620 topo.num_threads_per_core; ++thread_no) {
1621 struct thread_data *t, *t2;
1622 struct core_data *c, *c2;
1623 struct pkg_data *p, *p2;
1625 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1627 if (cpu_is_not_present(t->cpu_id))
1630 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1632 c = GET_CORE(core_base, core_no, pkg_no);
1633 c2 = GET_CORE(core_base2, core_no, pkg_no);
1635 p = GET_PKG(pkg_base, pkg_no);
1636 p2 = GET_PKG(pkg_base2, pkg_no);
1638 retval = func(t, c, p, t2, c2, p2);
1648 * run func(cpu) on every cpu in /proc/stat
1649 * return max_cpu number
1651 int for_all_proc_cpus(int (func)(int))
1657 fp = fopen_or_die(proc_stat, "r");
1659 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1661 err(1, "%s: failed to parse format", proc_stat);
1664 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1668 retval = func(cpu_num);
1678 void re_initialize(void)
1681 setup_all_buffers();
1682 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1688 * remember the last one seen, it will be the max
1690 int count_cpus(int cpu)
1692 if (topo.max_cpu_num < cpu)
1693 topo.max_cpu_num = cpu;
1698 int mark_cpu_present(int cpu)
1700 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1704 void turbostat_loop()
1712 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1715 } else if (retval == -1) {
1716 if (restarted > 1) {
1723 gettimeofday(&tv_even, (struct timezone *)NULL);
1726 if (for_all_proc_cpus(cpu_is_not_present)) {
1730 nanosleep(&interval_ts, NULL);
1731 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1734 } else if (retval == -1) {
1738 gettimeofday(&tv_odd, (struct timezone *)NULL);
1739 timersub(&tv_odd, &tv_even, &tv_delta);
1740 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1741 compute_average(EVEN_COUNTERS);
1742 format_all_counters(EVEN_COUNTERS);
1744 nanosleep(&interval_ts, NULL);
1745 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1748 } else if (retval == -1) {
1752 gettimeofday(&tv_even, (struct timezone *)NULL);
1753 timersub(&tv_even, &tv_odd, &tv_delta);
1754 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1755 compute_average(ODD_COUNTERS);
1756 format_all_counters(ODD_COUNTERS);
1761 void check_dev_msr()
1766 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1767 if (stat(pathname, &sb))
1768 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1769 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1772 void check_permissions()
1774 struct __user_cap_header_struct cap_header_data;
1775 cap_user_header_t cap_header = &cap_header_data;
1776 struct __user_cap_data_struct cap_data_data;
1777 cap_user_data_t cap_data = &cap_data_data;
1778 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1782 /* check for CAP_SYS_RAWIO */
1783 cap_header->pid = getpid();
1784 cap_header->version = _LINUX_CAPABILITY_VERSION;
1785 if (capget(cap_header, cap_data) < 0)
1786 err(-6, "capget(2) failed");
1788 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1790 warnx("capget(CAP_SYS_RAWIO) failed,"
1791 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1794 /* test file permissions */
1795 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1796 if (euidaccess(pathname, R_OK)) {
1798 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1801 /* if all else fails, thell them to be root */
1804 warnx("... or simply run as root");
1811 * NHM adds support for additional MSRs:
1813 * MSR_SMI_COUNT 0x00000034
1815 * MSR_PLATFORM_INFO 0x000000ce
1816 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1818 * MSR_PKG_C3_RESIDENCY 0x000003f8
1819 * MSR_PKG_C6_RESIDENCY 0x000003f9
1820 * MSR_CORE_C3_RESIDENCY 0x000003fc
1821 * MSR_CORE_C6_RESIDENCY 0x000003fd
1824 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
1826 int probe_nhm_msrs(unsigned int family, unsigned int model)
1828 unsigned long long msr;
1829 unsigned int base_ratio;
1830 int *pkg_cstate_limits;
1838 bclk = discover_bclk(family, model);
1841 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1842 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1843 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1844 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1845 case 0x2C: /* Westmere EP - Gulftown */
1846 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1847 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1848 pkg_cstate_limits = nhm_pkg_cstate_limits;
1850 case 0x2A: /* SNB */
1851 case 0x2D: /* SNB Xeon */
1852 case 0x3A: /* IVB */
1853 case 0x3E: /* IVB Xeon */
1854 pkg_cstate_limits = snb_pkg_cstate_limits;
1856 case 0x3C: /* HSW */
1857 case 0x3F: /* HSX */
1858 case 0x45: /* HSW */
1859 case 0x46: /* HSW */
1860 case 0x3D: /* BDW */
1861 case 0x47: /* BDW */
1862 case 0x4F: /* BDX */
1863 case 0x56: /* BDX-DE */
1864 case 0x4E: /* SKL */
1865 case 0x5E: /* SKL */
1866 pkg_cstate_limits = hsw_pkg_cstate_limits;
1868 case 0x37: /* BYT */
1869 case 0x4D: /* AVN */
1870 pkg_cstate_limits = slv_pkg_cstate_limits;
1872 case 0x4C: /* AMT */
1873 pkg_cstate_limits = amt_pkg_cstate_limits;
1875 case 0x57: /* PHI */
1876 pkg_cstate_limits = phi_pkg_cstate_limits;
1881 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1882 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
1884 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1885 base_ratio = (msr >> 8) & 0xFF;
1887 base_hz = base_ratio * bclk * 1000000;
1891 int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1894 /* Nehalem compatible, but do not include turbo-ratio limit support */
1895 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1896 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1897 case 0x57: /* PHI - Knights Landing (different MSR definition) */
1903 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1912 case 0x3E: /* IVB Xeon */
1913 case 0x3F: /* HSW Xeon */
1919 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1928 case 0x3F: /* HSW Xeon */
1935 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1944 case 0x57: /* Knights Landing */
1950 int has_config_tdp(unsigned int family, unsigned int model)
1959 case 0x3A: /* IVB */
1960 case 0x3C: /* HSW */
1961 case 0x3F: /* HSX */
1962 case 0x45: /* HSW */
1963 case 0x46: /* HSW */
1964 case 0x3D: /* BDW */
1965 case 0x47: /* BDW */
1966 case 0x4F: /* BDX */
1967 case 0x56: /* BDX-DE */
1968 case 0x4E: /* SKL */
1969 case 0x5E: /* SKL */
1971 case 0x57: /* Knights Landing */
1979 dump_cstate_pstate_config_info(family, model)
1981 if (!do_nhm_platform_info)
1984 dump_nhm_platform_info();
1986 if (has_hsw_turbo_ratio_limit(family, model))
1987 dump_hsw_turbo_ratio_limits();
1989 if (has_ivt_turbo_ratio_limit(family, model))
1990 dump_ivt_turbo_ratio_limits();
1992 if (has_nhm_turbo_ratio_limit(family, model))
1993 dump_nhm_turbo_ratio_limits();
1995 if (has_knl_turbo_ratio_limit(family, model))
1996 dump_knl_turbo_ratio_limits();
1998 if (has_config_tdp(family, model))
2007 * Decode the ENERGY_PERF_BIAS MSR
2009 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2011 unsigned long long msr;
2020 /* EPB is per-package */
2021 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2024 if (cpu_migrate(cpu)) {
2025 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2029 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2032 switch (msr & 0xF) {
2033 case ENERGY_PERF_BIAS_PERFORMANCE:
2034 epb_string = "performance";
2036 case ENERGY_PERF_BIAS_NORMAL:
2037 epb_string = "balanced";
2039 case ENERGY_PERF_BIAS_POWERSAVE:
2040 epb_string = "powersave";
2043 epb_string = "custom";
2046 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2052 * Decode the MSR_HWP_CAPABILITIES
2054 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2056 unsigned long long msr;
2064 /* MSR_HWP_CAPABILITIES is per-package */
2065 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2068 if (cpu_migrate(cpu)) {
2069 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2073 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2076 fprintf(stderr, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
2077 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2079 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2080 if ((msr & (1 << 0)) == 0)
2083 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2086 fprintf(stderr, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
2087 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2089 (unsigned int)HWP_HIGHEST_PERF(msr),
2090 (unsigned int)HWP_GUARANTEED_PERF(msr),
2091 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2092 (unsigned int)HWP_LOWEST_PERF(msr));
2094 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2097 fprintf(stderr, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
2098 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2100 (unsigned int)(((msr) >> 0) & 0xff),
2101 (unsigned int)(((msr) >> 8) & 0xff),
2102 (unsigned int)(((msr) >> 16) & 0xff),
2103 (unsigned int)(((msr) >> 24) & 0xff),
2104 (unsigned int)(((msr) >> 32) & 0xff3),
2105 (unsigned int)(((msr) >> 42) & 0x1));
2108 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2111 fprintf(stderr, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
2112 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2114 (unsigned int)(((msr) >> 0) & 0xff),
2115 (unsigned int)(((msr) >> 8) & 0xff),
2116 (unsigned int)(((msr) >> 16) & 0xff),
2117 (unsigned int)(((msr) >> 24) & 0xff),
2118 (unsigned int)(((msr) >> 32) & 0xff3));
2120 if (has_hwp_notify) {
2121 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2124 fprintf(stderr, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
2125 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2127 ((msr) & 0x1) ? "EN" : "Dis",
2128 ((msr) & 0x2) ? "EN" : "Dis");
2130 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2133 fprintf(stderr, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
2134 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2136 ((msr) & 0x1) ? "" : "No-",
2137 ((msr) & 0x2) ? "" : "No-");
2143 * print_perf_limit()
2145 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2147 unsigned long long msr;
2153 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2156 if (cpu_migrate(cpu)) {
2157 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2161 if (do_core_perf_limit_reasons) {
2162 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2163 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2164 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2165 (msr & 1 << 15) ? "bit15, " : "",
2166 (msr & 1 << 14) ? "bit14, " : "",
2167 (msr & 1 << 13) ? "Transitions, " : "",
2168 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2169 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2170 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2171 (msr & 1 << 9) ? "CorePwr, " : "",
2172 (msr & 1 << 8) ? "Amps, " : "",
2173 (msr & 1 << 6) ? "VR-Therm, " : "",
2174 (msr & 1 << 5) ? "Auto-HWP, " : "",
2175 (msr & 1 << 4) ? "Graphics, " : "",
2176 (msr & 1 << 2) ? "bit2, " : "",
2177 (msr & 1 << 1) ? "ThermStatus, " : "",
2178 (msr & 1 << 0) ? "PROCHOT, " : "");
2179 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2180 (msr & 1 << 31) ? "bit31, " : "",
2181 (msr & 1 << 30) ? "bit30, " : "",
2182 (msr & 1 << 29) ? "Transitions, " : "",
2183 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2184 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2185 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2186 (msr & 1 << 25) ? "CorePwr, " : "",
2187 (msr & 1 << 24) ? "Amps, " : "",
2188 (msr & 1 << 22) ? "VR-Therm, " : "",
2189 (msr & 1 << 21) ? "Auto-HWP, " : "",
2190 (msr & 1 << 20) ? "Graphics, " : "",
2191 (msr & 1 << 18) ? "bit18, " : "",
2192 (msr & 1 << 17) ? "ThermStatus, " : "",
2193 (msr & 1 << 16) ? "PROCHOT, " : "");
2196 if (do_gfx_perf_limit_reasons) {
2197 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2198 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2199 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
2200 (msr & 1 << 0) ? "PROCHOT, " : "",
2201 (msr & 1 << 1) ? "ThermStatus, " : "",
2202 (msr & 1 << 4) ? "Graphics, " : "",
2203 (msr & 1 << 6) ? "VR-Therm, " : "",
2204 (msr & 1 << 8) ? "Amps, " : "",
2205 (msr & 1 << 9) ? "GFXPwr, " : "",
2206 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2207 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2208 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
2209 (msr & 1 << 16) ? "PROCHOT, " : "",
2210 (msr & 1 << 17) ? "ThermStatus, " : "",
2211 (msr & 1 << 20) ? "Graphics, " : "",
2212 (msr & 1 << 22) ? "VR-Therm, " : "",
2213 (msr & 1 << 24) ? "Amps, " : "",
2214 (msr & 1 << 25) ? "GFXPwr, " : "",
2215 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2216 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2218 if (do_ring_perf_limit_reasons) {
2219 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2220 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2221 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
2222 (msr & 1 << 0) ? "PROCHOT, " : "",
2223 (msr & 1 << 1) ? "ThermStatus, " : "",
2224 (msr & 1 << 6) ? "VR-Therm, " : "",
2225 (msr & 1 << 8) ? "Amps, " : "",
2226 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2227 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2228 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
2229 (msr & 1 << 16) ? "PROCHOT, " : "",
2230 (msr & 1 << 17) ? "ThermStatus, " : "",
2231 (msr & 1 << 22) ? "VR-Therm, " : "",
2232 (msr & 1 << 24) ? "Amps, " : "",
2233 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2234 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2239 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2240 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2242 double get_tdp(model)
2244 unsigned long long msr;
2246 if (do_rapl & RAPL_PKG_POWER_INFO)
2247 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
2248 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2260 * rapl_dram_energy_units_probe()
2261 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2264 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2266 /* only called for genuine_intel, family 6 */
2269 case 0x3F: /* HSX */
2270 case 0x4F: /* BDX */
2271 case 0x56: /* BDX-DE */
2272 case 0x57: /* KNL */
2273 return (rapl_dram_energy_units = 15.3 / 1000000);
2275 return (rapl_energy_units);
2283 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2285 void rapl_probe(unsigned int family, unsigned int model)
2287 unsigned long long msr;
2288 unsigned int time_unit;
2300 case 0x3C: /* HSW */
2301 case 0x45: /* HSW */
2302 case 0x46: /* HSW */
2303 case 0x3D: /* BDW */
2304 case 0x47: /* BDW */
2305 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2307 case 0x4E: /* SKL */
2308 case 0x5E: /* SKL */
2309 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2311 case 0x3F: /* HSX */
2312 case 0x4F: /* BDX */
2313 case 0x56: /* BDX-DE */
2314 case 0x57: /* KNL */
2315 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2319 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
2321 case 0x37: /* BYT */
2322 case 0x4D: /* AVN */
2323 do_rapl = RAPL_PKG | RAPL_CORES ;
2329 /* units on package 0, verify later other packages match */
2330 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
2333 rapl_power_units = 1.0 / (1 << (msr & 0xF));
2335 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2337 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2339 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2341 time_unit = msr >> 16 & 0xF;
2345 rapl_time_units = 1.0 / (1 << (time_unit));
2347 tdp = get_tdp(model);
2349 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2351 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2356 void perf_limit_reasons_probe(family, model)
2365 case 0x3C: /* HSW */
2366 case 0x45: /* HSW */
2367 case 0x46: /* HSW */
2368 do_gfx_perf_limit_reasons = 1;
2369 case 0x3F: /* HSX */
2370 do_core_perf_limit_reasons = 1;
2371 do_ring_perf_limit_reasons = 1;
2377 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2379 unsigned long long msr;
2383 if (!(do_dts || do_ptm))
2388 /* DTS is per-core, no need to print for each thread */
2389 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2392 if (cpu_migrate(cpu)) {
2393 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2397 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2398 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2401 dts = (msr >> 16) & 0x7F;
2402 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2403 cpu, msr, tcc_activation_temp - dts);
2406 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2409 dts = (msr >> 16) & 0x7F;
2410 dts2 = (msr >> 8) & 0x7F;
2411 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2412 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2418 unsigned int resolution;
2420 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2423 dts = (msr >> 16) & 0x7F;
2424 resolution = (msr >> 27) & 0xF;
2425 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2426 cpu, msr, tcc_activation_temp - dts, resolution);
2429 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2432 dts = (msr >> 16) & 0x7F;
2433 dts2 = (msr >> 8) & 0x7F;
2434 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2435 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2442 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2444 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2446 ((msr >> 15) & 1) ? "EN" : "DIS",
2447 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2448 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2449 (((msr >> 16) & 1) ? "EN" : "DIS"));
2454 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2456 unsigned long long msr;
2462 /* RAPL counters are per package, so print only for 1st thread/package */
2463 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2467 if (cpu_migrate(cpu)) {
2468 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2472 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2476 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2477 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2478 rapl_power_units, rapl_energy_units, rapl_time_units);
2480 if (do_rapl & RAPL_PKG_POWER_INFO) {
2482 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2486 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2488 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2489 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2490 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2491 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2494 if (do_rapl & RAPL_PKG) {
2496 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2499 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2500 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2502 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2503 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2505 ((msr >> 47) & 1) ? "EN" : "DIS",
2506 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2507 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2508 ((msr >> 48) & 1) ? "EN" : "DIS");
2511 if (do_rapl & RAPL_DRAM_POWER_INFO) {
2512 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2515 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2517 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2518 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2519 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2520 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2522 if (do_rapl & RAPL_DRAM) {
2523 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2525 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2526 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2528 print_power_limit_msr(cpu, msr, "DRAM Limit");
2530 if (do_rapl & RAPL_CORE_POLICY) {
2532 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2535 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
2538 if (do_rapl & RAPL_CORES) {
2541 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2543 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2544 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2545 print_power_limit_msr(cpu, msr, "Cores Limit");
2548 if (do_rapl & RAPL_GFX) {
2550 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2553 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2555 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2557 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2558 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2559 print_power_limit_msr(cpu, msr, "GFX Limit");
2566 * SNB adds support for additional MSRs:
2568 * MSR_PKG_C7_RESIDENCY 0x000003fa
2569 * MSR_CORE_C7_RESIDENCY 0x000003fe
2570 * MSR_PKG_C2_RESIDENCY 0x0000060d
2573 int has_snb_msrs(unsigned int family, unsigned int model)
2581 case 0x3A: /* IVB */
2582 case 0x3E: /* IVB Xeon */
2583 case 0x3C: /* HSW */
2584 case 0x3F: /* HSW */
2585 case 0x45: /* HSW */
2586 case 0x46: /* HSW */
2587 case 0x3D: /* BDW */
2588 case 0x47: /* BDW */
2589 case 0x4F: /* BDX */
2590 case 0x56: /* BDX-DE */
2591 case 0x4E: /* SKL */
2592 case 0x5E: /* SKL */
2599 * HSW adds support for additional MSRs:
2601 * MSR_PKG_C8_RESIDENCY 0x00000630
2602 * MSR_PKG_C9_RESIDENCY 0x00000631
2603 * MSR_PKG_C10_RESIDENCY 0x00000632
2605 int has_hsw_msrs(unsigned int family, unsigned int model)
2611 case 0x45: /* HSW */
2612 case 0x3D: /* BDW */
2613 case 0x4E: /* SKL */
2614 case 0x5E: /* SKL */
2621 * SKL adds support for additional MSRS:
2623 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2624 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2625 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2626 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2628 int has_skl_msrs(unsigned int family, unsigned int model)
2634 case 0x4E: /* SKL */
2635 case 0x5E: /* SKL */
2643 int is_slm(unsigned int family, unsigned int model)
2648 case 0x37: /* BYT */
2649 case 0x4D: /* AVN */
2655 int is_knl(unsigned int family, unsigned int model)
2660 case 0x57: /* KNL */
2666 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
2668 if (is_knl(family, model))
2673 #define SLM_BCLK_FREQS 5
2674 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2676 double slm_bclk(void)
2678 unsigned long long msr = 3;
2682 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
2683 fprintf(stderr, "SLM BCLK: unknown\n");
2686 if (i >= SLM_BCLK_FREQS) {
2687 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2690 freq = slm_freq_table[i];
2692 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2697 double discover_bclk(unsigned int family, unsigned int model)
2699 if (has_snb_msrs(family, model) || is_knl(family, model))
2701 else if (is_slm(family, model))
2708 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2709 * the Thermal Control Circuit (TCC) activates.
2710 * This is usually equal to tjMax.
2712 * Older processors do not have this MSR, so there we guess,
2713 * but also allow cmdline over-ride with -T.
2715 * Several MSR temperature values are in units of degrees-C
2716 * below this value, including the Digital Thermal Sensor (DTS),
2717 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2719 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2721 unsigned long long msr;
2722 unsigned int target_c_local;
2725 /* tcc_activation_temp is used only for dts or ptm */
2726 if (!(do_dts || do_ptm))
2729 /* this is a per-package concept */
2730 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2734 if (cpu_migrate(cpu)) {
2735 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2739 if (tcc_activation_temp_override != 0) {
2740 tcc_activation_temp = tcc_activation_temp_override;
2741 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2742 cpu, tcc_activation_temp);
2746 /* Temperature Target MSR is Nehalem and newer only */
2747 if (!do_nhm_platform_info)
2750 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
2753 target_c_local = (msr >> 16) & 0xFF;
2756 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2757 cpu, msr, target_c_local);
2759 if (!target_c_local)
2762 tcc_activation_temp = target_c_local;
2767 tcc_activation_temp = TJMAX_DEFAULT;
2768 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2769 cpu, tcc_activation_temp);
2774 void decode_misc_enable_msr(void)
2776 unsigned long long msr;
2778 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
2779 fprintf(stderr, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
2781 msr & (1 << 3) ? "TCC" : "",
2782 msr & (1 << 16) ? "EIST" : "",
2783 msr & (1 << 18) ? "MONITOR" : "");
2787 * Decode MSR_MISC_PWR_MGMT
2789 * Decode the bits according to the Nehalem documentation
2790 * bit[0] seems to continue to have same meaning going forward
2793 void decode_misc_pwr_mgmt_msr(void)
2795 unsigned long long msr;
2797 if (!do_nhm_platform_info)
2800 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
2801 fprintf(stderr, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n",
2803 msr & (1 << 0) ? "DIS" : "EN",
2804 msr & (1 << 1) ? "EN" : "DIS");
2807 void process_cpuid()
2809 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
2810 unsigned int fms, family, model, stepping;
2812 eax = ebx = ecx = edx = 0;
2814 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2816 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2820 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2821 (char *)&ebx, (char *)&edx, (char *)&ecx);
2823 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
2824 family = (fms >> 8) & 0xf;
2825 model = (fms >> 4) & 0xf;
2826 stepping = fms & 0xf;
2827 if (family == 6 || family == 0xf)
2828 model += ((fms >> 16) & 0xf) << 4;
2831 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2832 max_level, family, model, stepping, family, model, stepping);
2833 fprintf(stderr, "CPUID(1): %s %s %s %s %s %s %s %s\n",
2834 ecx & (1 << 0) ? "SSE3" : "-",
2835 ecx & (1 << 3) ? "MONITOR" : "-",
2836 ecx & (1 << 7) ? "EIST" : "-",
2837 ecx & (1 << 8) ? "TM2" : "-",
2838 edx & (1 << 4) ? "TSC" : "-",
2839 edx & (1 << 5) ? "MSR" : "-",
2840 edx & (1 << 22) ? "ACPI-TM" : "-",
2841 edx & (1 << 29) ? "TM" : "-");
2844 if (!(edx & (1 << 5)))
2845 errx(1, "CPUID: no MSR");
2848 * check max extended function levels of CPUID.
2849 * This is needed to check for invariant TSC.
2850 * This check is valid for both Intel and AMD.
2852 ebx = ecx = edx = 0;
2853 __get_cpuid(0x80000000, &max_extended_level, &ebx, &ecx, &edx);
2855 if (max_extended_level >= 0x80000007) {
2858 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2859 * this check is valid for both Intel and AMD
2861 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2862 has_invariant_tsc = edx & (1 << 8);
2866 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2867 * this check is valid for both Intel and AMD
2870 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
2871 has_aperf = ecx & (1 << 0);
2872 do_dts = eax & (1 << 0);
2873 do_ptm = eax & (1 << 6);
2874 has_hwp = eax & (1 << 7);
2875 has_hwp_notify = eax & (1 << 8);
2876 has_hwp_activity_window = eax & (1 << 9);
2877 has_hwp_epp = eax & (1 << 10);
2878 has_hwp_pkg = eax & (1 << 11);
2879 has_epb = ecx & (1 << 3);
2882 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
2883 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
2884 has_aperf ? "" : "No-",
2885 do_dts ? "" : "No-",
2886 do_ptm ? "" : "No-",
2887 has_hwp ? "" : "No-",
2888 has_hwp_notify ? "" : "No-",
2889 has_hwp_activity_window ? "" : "No-",
2890 has_hwp_epp ? "" : "No-",
2891 has_hwp_pkg ? "" : "No-",
2892 has_epb ? "" : "No-");
2895 decode_misc_enable_msr();
2897 if (max_level >= 0x15) {
2898 unsigned int eax_crystal;
2899 unsigned int ebx_tsc;
2902 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2904 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2905 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2909 if (debug && (ebx != 0))
2910 fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
2911 eax_crystal, ebx_tsc, crystal_hz);
2913 if (crystal_hz == 0)
2915 case 0x4E: /* SKL */
2916 case 0x5E: /* SKL */
2917 crystal_hz = 24000000; /* 24 MHz */
2924 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2926 fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
2927 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
2931 if (max_level >= 0x16) {
2932 unsigned int base_mhz, max_mhz, bus_mhz, edx;
2935 * CPUID 16H Base MHz, Max MHz, Bus MHz
2937 base_mhz = max_mhz = bus_mhz = edx = 0;
2939 __get_cpuid(0x16, &base_mhz, &max_mhz, &bus_mhz, &edx);
2941 fprintf(stderr, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
2942 base_mhz, max_mhz, bus_mhz);
2946 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
2948 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
2949 do_snb_cstates = has_snb_msrs(family, model);
2950 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2951 do_pc3 = (pkg_cstate_limit >= PCL__3);
2952 do_pc6 = (pkg_cstate_limit >= PCL__6);
2953 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
2954 do_c8_c9_c10 = has_hsw_msrs(family, model);
2955 do_skl_residency = has_skl_msrs(family, model);
2956 do_slm_cstates = is_slm(family, model);
2957 do_knl_cstates = is_knl(family, model);
2960 decode_misc_pwr_mgmt_msr();
2962 rapl_probe(family, model);
2963 perf_limit_reasons_probe(family, model);
2966 dump_cstate_pstate_config_info();
2968 if (has_skl_msrs(family, model))
2969 calculate_tsc_tweak();
2977 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2979 "Turbostat forks the specified COMMAND and prints statistics\n"
2980 "when COMMAND completes.\n"
2981 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2982 "to print statistics, until interrupted.\n"
2983 "--debug run in \"debug\" mode\n"
2984 "--interval sec Override default 5-second measurement interval\n"
2985 "--help print this help message\n"
2986 "--counter msr print 32-bit counter at address \"msr\"\n"
2987 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2988 "--msr msr print 32-bit value at address \"msr\"\n"
2989 "--MSR msr print 64-bit Value at address \"msr\"\n"
2990 "--version print version information\n"
2992 "For more help, run \"man turbostat\"\n");
2997 * in /dev/cpu/ return success for names that are numbers
2998 * ie. filter out ".", "..", "microcode".
3000 int dir_filter(const struct dirent *dirp)
3002 if (isdigit(dirp->d_name[0]))
3008 int open_dev_cpu_msr(int dummy1)
3013 void topology_probe()
3016 int max_core_id = 0;
3017 int max_package_id = 0;
3018 int max_siblings = 0;
3019 struct cpu_topology {
3021 int physical_package_id;
3024 /* Initialize num_cpus, max_cpu_num */
3026 topo.max_cpu_num = 0;
3027 for_all_proc_cpus(count_cpus);
3028 if (!summary_only && topo.num_cpus > 1)
3032 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
3034 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
3036 err(1, "calloc cpus");
3039 * Allocate and initialize cpu_present_set
3041 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
3042 if (cpu_present_set == NULL)
3043 err(3, "CPU_ALLOC");
3044 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3045 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3046 for_all_proc_cpus(mark_cpu_present);
3049 * Allocate and initialize cpu_affinity_set
3051 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
3052 if (cpu_affinity_set == NULL)
3053 err(3, "CPU_ALLOC");
3054 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3055 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3060 * find max_core_id, max_package_id
3062 for (i = 0; i <= topo.max_cpu_num; ++i) {
3065 if (cpu_is_not_present(i)) {
3067 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
3070 cpus[i].core_id = get_core_id(i);
3071 if (cpus[i].core_id > max_core_id)
3072 max_core_id = cpus[i].core_id;
3074 cpus[i].physical_package_id = get_physical_package_id(i);
3075 if (cpus[i].physical_package_id > max_package_id)
3076 max_package_id = cpus[i].physical_package_id;
3078 siblings = get_num_ht_siblings(i);
3079 if (siblings > max_siblings)
3080 max_siblings = siblings;
3082 fprintf(stderr, "cpu %d pkg %d core %d\n",
3083 i, cpus[i].physical_package_id, cpus[i].core_id);
3085 topo.num_cores_per_pkg = max_core_id + 1;
3087 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
3088 max_core_id, topo.num_cores_per_pkg);
3089 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
3092 topo.num_packages = max_package_id + 1;
3094 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
3095 max_package_id, topo.num_packages);
3096 if (debug && !summary_only && topo.num_packages > 1)
3099 topo.num_threads_per_core = max_siblings;
3101 fprintf(stderr, "max_siblings %d\n", max_siblings);
3107 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3111 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3112 topo.num_packages, sizeof(struct thread_data));
3116 for (i = 0; i < topo.num_threads_per_core *
3117 topo.num_cores_per_pkg * topo.num_packages; i++)
3118 (*t)[i].cpu_id = -1;
3120 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3121 sizeof(struct core_data));
3125 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3126 (*c)[i].core_id = -1;
3128 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
3132 for (i = 0; i < topo.num_packages; i++)
3133 (*p)[i].package_id = i;
3137 err(1, "calloc counters");
3142 * set cpu_id, core_num, pkg_num
3143 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3145 * increment topo.num_cores when 1st core in pkg seen
3147 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3148 struct pkg_data *pkg_base, int thread_num, int core_num,
3149 int pkg_num, int cpu_id)
3151 struct thread_data *t;
3152 struct core_data *c;
3155 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3156 c = GET_CORE(core_base, core_num, pkg_num);
3157 p = GET_PKG(pkg_base, pkg_num);
3160 if (thread_num == 0) {
3161 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3162 if (cpu_is_first_core_in_package(cpu_id))
3163 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3166 c->core_id = core_num;
3167 p->package_id = pkg_num;
3171 int initialize_counters(int cpu_id)
3173 int my_thread_id, my_core_id, my_package_id;
3175 my_package_id = get_physical_package_id(cpu_id);
3176 my_core_id = get_core_id(cpu_id);
3177 my_thread_id = get_cpu_position_in_core(cpu_id);
3181 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3182 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3186 void allocate_output_buffer()
3188 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
3189 outp = output_buffer;
3191 err(-1, "calloc output buffer");
3194 void setup_all_buffers(void)
3197 allocate_counters(&thread_even, &core_even, &package_even);
3198 allocate_counters(&thread_odd, &core_odd, &package_odd);
3199 allocate_output_buffer();
3200 for_all_proc_cpus(initialize_counters);
3203 void set_base_cpu(void)
3205 base_cpu = sched_getcpu();
3207 err(-ENODEV, "No valid cpus found");
3210 fprintf(stderr, "base_cpu = %d\n", base_cpu);
3213 void turbostat_init()
3215 setup_all_buffers();
3218 check_permissions();
3223 for_all_cpus(print_hwp, ODD_COUNTERS);
3226 for_all_cpus(print_epb, ODD_COUNTERS);
3229 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3232 for_all_cpus(print_rapl, ODD_COUNTERS);
3234 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3237 for_all_cpus(print_thermal, ODD_COUNTERS);
3240 int fork_it(char **argv)
3245 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3248 /* clear affinity side-effect of get_counters() */
3249 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
3250 gettimeofday(&tv_even, (struct timezone *)NULL);
3255 execvp(argv[0], argv);
3259 if (child_pid == -1)
3262 signal(SIGINT, SIG_IGN);
3263 signal(SIGQUIT, SIG_IGN);
3264 if (waitpid(child_pid, &status, 0) == -1)
3265 err(status, "waitpid");
3268 * n.b. fork_it() does not check for errors from for_all_cpus()
3269 * because re-starting is problematic when forking
3271 for_all_cpus(get_counters, ODD_COUNTERS);
3272 gettimeofday(&tv_odd, (struct timezone *)NULL);
3273 timersub(&tv_odd, &tv_even, &tv_delta);
3274 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
3275 compute_average(EVEN_COUNTERS);
3276 format_all_counters(EVEN_COUNTERS);
3279 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3284 int get_and_dump_counters(void)
3288 status = for_all_cpus(get_counters, ODD_COUNTERS);
3292 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3301 void print_version() {
3302 fprintf(stderr, "turbostat version 4.10 10 Dec, 2015"
3303 " - Len Brown <lenb@kernel.org>\n");
3306 void cmdline(int argc, char **argv)
3309 int option_index = 0;
3310 static struct option long_options[] = {
3311 {"Counter", required_argument, 0, 'C'},
3312 {"counter", required_argument, 0, 'c'},
3313 {"Dump", no_argument, 0, 'D'},
3314 {"debug", no_argument, 0, 'd'},
3315 {"interval", required_argument, 0, 'i'},
3316 {"help", no_argument, 0, 'h'},
3317 {"Joules", no_argument, 0, 'J'},
3318 {"MSR", required_argument, 0, 'M'},
3319 {"msr", required_argument, 0, 'm'},
3320 {"Package", no_argument, 0, 'p'},
3321 {"processor", no_argument, 0, 'p'},
3322 {"Summary", no_argument, 0, 'S'},
3323 {"TCC", required_argument, 0, 'T'},
3324 {"version", no_argument, 0, 'v' },
3330 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:PpST:v",
3331 long_options, &option_index)) != -1) {
3334 sscanf(optarg, "%x", &extra_delta_offset64);
3337 sscanf(optarg, "%x", &extra_delta_offset32);
3351 double interval = strtod(optarg, NULL);
3353 if (interval < 0.001) {
3354 fprintf(stderr, "interval %f seconds is too small\n",
3359 interval_ts.tv_sec = interval;
3360 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
3367 sscanf(optarg, "%x", &extra_msr_offset64);
3370 sscanf(optarg, "%x", &extra_msr_offset32);
3382 tcc_activation_temp_override = atoi(optarg);
3392 int main(int argc, char **argv)
3394 cmdline(argc, argv);
3401 /* dump counters and exit */
3403 return get_and_dump_counters();
3406 * if any params left, it must be a command to fork
3409 return fork_it(argv + optind);