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";
49 struct timespec interval_ts
= {5, 0};
51 unsigned int rapl_joules
;
52 unsigned int summary_only
;
53 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 do_irtl_snb
;
68 unsigned int do_irtl_hsw
;
69 unsigned int units
= 1000000; /* MHz etc */
70 unsigned int genuine_intel
;
71 unsigned int has_invariant_tsc
;
72 unsigned int do_nhm_platform_info
;
73 unsigned int extra_msr_offset32
;
74 unsigned int extra_msr_offset64
;
75 unsigned int extra_delta_offset32
;
76 unsigned int extra_delta_offset64
;
77 unsigned int aperf_mperf_multiplier
= 1;
82 unsigned int has_base_hz
;
83 double tsc_tweak
= 1.0;
84 unsigned int show_pkg
;
85 unsigned int show_core
;
86 unsigned int show_cpu
;
87 unsigned int show_pkg_only
;
88 unsigned int show_core_only
;
89 char *output_buffer
, *outp
;
93 unsigned int do_gfx_rc6_ms
;
94 unsigned long long gfx_cur_rc6_ms
;
95 unsigned int do_gfx_mhz
;
96 unsigned int gfx_cur_mhz
;
97 unsigned int tcc_activation_temp
;
98 unsigned int tcc_activation_temp_override
;
99 double rapl_power_units
, rapl_time_units
;
100 double rapl_dram_energy_units
, rapl_energy_units
;
101 double rapl_joule_counter_range
;
102 unsigned int do_core_perf_limit_reasons
;
103 unsigned int do_gfx_perf_limit_reasons
;
104 unsigned int do_ring_perf_limit_reasons
;
105 unsigned int crystal_hz
;
106 unsigned long long tsc_hz
;
108 double discover_bclk(unsigned int family
, unsigned int model
);
109 unsigned int has_hwp
; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
110 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
111 unsigned int has_hwp_notify
; /* IA32_HWP_INTERRUPT */
112 unsigned int has_hwp_activity_window
; /* IA32_HWP_REQUEST[bits 41:32] */
113 unsigned int has_hwp_epp
; /* IA32_HWP_REQUEST[bits 31:24] */
114 unsigned int has_hwp_pkg
; /* IA32_HWP_REQUEST_PKG */
116 #define RAPL_PKG (1 << 0)
117 /* 0x610 MSR_PKG_POWER_LIMIT */
118 /* 0x611 MSR_PKG_ENERGY_STATUS */
119 #define RAPL_PKG_PERF_STATUS (1 << 1)
120 /* 0x613 MSR_PKG_PERF_STATUS */
121 #define RAPL_PKG_POWER_INFO (1 << 2)
122 /* 0x614 MSR_PKG_POWER_INFO */
124 #define RAPL_DRAM (1 << 3)
125 /* 0x618 MSR_DRAM_POWER_LIMIT */
126 /* 0x619 MSR_DRAM_ENERGY_STATUS */
127 #define RAPL_DRAM_PERF_STATUS (1 << 4)
128 /* 0x61b MSR_DRAM_PERF_STATUS */
129 #define RAPL_DRAM_POWER_INFO (1 << 5)
130 /* 0x61c MSR_DRAM_POWER_INFO */
132 #define RAPL_CORES_POWER_LIMIT (1 << 6)
133 /* 0x638 MSR_PP0_POWER_LIMIT */
134 #define RAPL_CORE_POLICY (1 << 7)
135 /* 0x63a MSR_PP0_POLICY */
137 #define RAPL_GFX (1 << 8)
138 /* 0x640 MSR_PP1_POWER_LIMIT */
139 /* 0x641 MSR_PP1_ENERGY_STATUS */
140 /* 0x642 MSR_PP1_POLICY */
142 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
143 /* 0x639 MSR_PP0_ENERGY_STATUS */
144 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
145 #define TJMAX_DEFAULT 100
147 #define MAX(a, b) ((a) > (b) ? (a) : (b))
152 cpu_set_t
*cpu_present_set
, *cpu_affinity_set
;
153 size_t cpu_present_setsize
, cpu_affinity_setsize
;
156 unsigned long long tsc
;
157 unsigned long long aperf
;
158 unsigned long long mperf
;
159 unsigned long long c1
;
160 unsigned long long extra_msr64
;
161 unsigned long long extra_delta64
;
162 unsigned long long extra_msr32
;
163 unsigned long long extra_delta32
;
164 unsigned int irq_count
;
165 unsigned int smi_count
;
168 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
169 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
170 } *thread_even
, *thread_odd
;
173 unsigned long long c3
;
174 unsigned long long c6
;
175 unsigned long long c7
;
176 unsigned int core_temp_c
;
177 unsigned int core_id
;
178 } *core_even
, *core_odd
;
181 unsigned long long pc2
;
182 unsigned long long pc3
;
183 unsigned long long pc6
;
184 unsigned long long pc7
;
185 unsigned long long pc8
;
186 unsigned long long pc9
;
187 unsigned long long pc10
;
188 unsigned long long pkg_wtd_core_c0
;
189 unsigned long long pkg_any_core_c0
;
190 unsigned long long pkg_any_gfxe_c0
;
191 unsigned long long pkg_both_core_gfxe_c0
;
192 long long gfx_rc6_ms
;
193 unsigned int gfx_mhz
;
194 unsigned int package_id
;
195 unsigned int energy_pkg
; /* MSR_PKG_ENERGY_STATUS */
196 unsigned int energy_dram
; /* MSR_DRAM_ENERGY_STATUS */
197 unsigned int energy_cores
; /* MSR_PP0_ENERGY_STATUS */
198 unsigned int energy_gfx
; /* MSR_PP1_ENERGY_STATUS */
199 unsigned int rapl_pkg_perf_status
; /* MSR_PKG_PERF_STATUS */
200 unsigned int rapl_dram_perf_status
; /* MSR_DRAM_PERF_STATUS */
201 unsigned int pkg_temp_c
;
203 } *package_even
, *package_odd
;
205 #define ODD_COUNTERS thread_odd, core_odd, package_odd
206 #define EVEN_COUNTERS thread_even, core_even, package_even
208 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
209 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
210 topo.num_threads_per_core + \
211 (core_no) * topo.num_threads_per_core + (thread_no))
212 #define GET_CORE(core_base, core_no, pkg_no) \
213 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
214 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
216 struct system_summary
{
217 struct thread_data threads
;
218 struct core_data cores
;
219 struct pkg_data packages
;
228 int num_cores_per_pkg
;
229 int num_threads_per_core
;
232 struct timeval tv_even
, tv_odd
, tv_delta
;
234 int *irq_column_2_cpu
; /* /proc/interrupts column numbers */
235 int *irqs_per_cpu
; /* indexed by cpu_num */
237 void setup_all_buffers(void);
239 int cpu_is_not_present(int cpu
)
241 return !CPU_ISSET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
244 * run func(thread, core, package) in topology order
245 * skip non-present cpus
248 int for_all_cpus(int (func
)(struct thread_data
*, struct core_data
*, struct pkg_data
*),
249 struct thread_data
*thread_base
, struct core_data
*core_base
, struct pkg_data
*pkg_base
)
251 int retval
, pkg_no
, core_no
, thread_no
;
253 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
254 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
255 for (thread_no
= 0; thread_no
<
256 topo
.num_threads_per_core
; ++thread_no
) {
257 struct thread_data
*t
;
261 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
263 if (cpu_is_not_present(t
->cpu_id
))
266 c
= GET_CORE(core_base
, core_no
, pkg_no
);
267 p
= GET_PKG(pkg_base
, pkg_no
);
269 retval
= func(t
, c
, p
);
278 int cpu_migrate(int cpu
)
280 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
281 CPU_SET_S(cpu
, cpu_affinity_setsize
, cpu_affinity_set
);
282 if (sched_setaffinity(0, cpu_affinity_setsize
, cpu_affinity_set
) == -1)
287 int get_msr_fd(int cpu
)
297 sprintf(pathname
, "/dev/cpu/%d/msr", cpu
);
298 fd
= open(pathname
, O_RDONLY
);
300 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname
);
307 int get_msr(int cpu
, off_t offset
, unsigned long long *msr
)
311 retval
= pread(get_msr_fd(cpu
), msr
, sizeof(*msr
), offset
);
313 if (retval
!= sizeof *msr
)
314 err(-1, "msr %d offset 0x%llx read failed", cpu
, (unsigned long long)offset
);
320 * Example Format w/ field column widths:
322 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz IRQ SMI Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp GFXMHz Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
323 * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
326 void print_header(void)
329 outp
+= sprintf(outp
, "\tPackage");
331 outp
+= sprintf(outp
, "\tCore");
333 outp
+= sprintf(outp
, "\tCPU");
335 outp
+= sprintf(outp
, "\tAvg_MHz");
337 outp
+= sprintf(outp
, "\tBusy%%");
339 outp
+= sprintf(outp
, "\tBzy_MHz");
340 outp
+= sprintf(outp
, "\tTSC_MHz");
342 if (extra_delta_offset32
)
343 outp
+= sprintf(outp
, "\tcount 0x%03X", extra_delta_offset32
);
344 if (extra_delta_offset64
)
345 outp
+= sprintf(outp
, "\tCOUNT 0x%03X", extra_delta_offset64
);
346 if (extra_msr_offset32
)
347 outp
+= sprintf(outp
, "\tMSR 0x%03X", extra_msr_offset32
);
348 if (extra_msr_offset64
)
349 outp
+= sprintf(outp
, "\tMSR 0x%03X", extra_msr_offset64
);
355 outp
+= sprintf(outp
, "\tIRQ");
357 outp
+= sprintf(outp
, "\tSMI");
360 outp
+= sprintf(outp
, "\tCPU%%c1");
361 if (do_nhm_cstates
&& !do_slm_cstates
&& !do_knl_cstates
)
362 outp
+= sprintf(outp
, "\tCPU%%c3");
364 outp
+= sprintf(outp
, "\tCPU%%c6");
366 outp
+= sprintf(outp
, "\tCPU%%c7");
369 outp
+= sprintf(outp
, "\tCoreTmp");
371 outp
+= sprintf(outp
, "\tPkgTmp");
374 outp
+= sprintf(outp
, "\tGFX%%rc6");
377 outp
+= sprintf(outp
, "\tGFXMHz");
379 if (do_skl_residency
) {
380 outp
+= sprintf(outp
, "\tTotl%%C0");
381 outp
+= sprintf(outp
, "\tAny%%C0");
382 outp
+= sprintf(outp
, "\tGFX%%C0");
383 outp
+= sprintf(outp
, "\tCPUGFX%%");
387 outp
+= sprintf(outp
, "\tPkg%%pc2");
389 outp
+= sprintf(outp
, "\tPkg%%pc3");
391 outp
+= sprintf(outp
, "\tPkg%%pc6");
393 outp
+= sprintf(outp
, "\tPkg%%pc7");
395 outp
+= sprintf(outp
, "\tPkg%%pc8");
396 outp
+= sprintf(outp
, "\tPkg%%pc9");
397 outp
+= sprintf(outp
, "\tPk%%pc10");
400 if (do_rapl
&& !rapl_joules
) {
401 if (do_rapl
& RAPL_PKG
)
402 outp
+= sprintf(outp
, "\tPkgWatt");
403 if (do_rapl
& RAPL_CORES_ENERGY_STATUS
)
404 outp
+= sprintf(outp
, "\tCorWatt");
405 if (do_rapl
& RAPL_GFX
)
406 outp
+= sprintf(outp
, "\tGFXWatt");
407 if (do_rapl
& RAPL_DRAM
)
408 outp
+= sprintf(outp
, "\tRAMWatt");
409 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
410 outp
+= sprintf(outp
, "\tPKG_%%");
411 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
412 outp
+= sprintf(outp
, "\tRAM_%%");
413 } else if (do_rapl
&& rapl_joules
) {
414 if (do_rapl
& RAPL_PKG
)
415 outp
+= sprintf(outp
, "\tPkg_J");
416 if (do_rapl
& RAPL_CORES_ENERGY_STATUS
)
417 outp
+= sprintf(outp
, "\tCor_J");
418 if (do_rapl
& RAPL_GFX
)
419 outp
+= sprintf(outp
, "\tGFX_J");
420 if (do_rapl
& RAPL_DRAM
)
421 outp
+= sprintf(outp
, "\tRAM_J");
422 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
423 outp
+= sprintf(outp
, "\tPKG_%%");
424 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
425 outp
+= sprintf(outp
, "\tRAM_%%");
428 outp
+= sprintf(outp
, "\n");
431 int dump_counters(struct thread_data
*t
, struct core_data
*c
,
434 outp
+= sprintf(outp
, "t %p, c %p, p %p\n", t
, c
, p
);
437 outp
+= sprintf(outp
, "CPU: %d flags 0x%x\n",
438 t
->cpu_id
, t
->flags
);
439 outp
+= sprintf(outp
, "TSC: %016llX\n", t
->tsc
);
440 outp
+= sprintf(outp
, "aperf: %016llX\n", t
->aperf
);
441 outp
+= sprintf(outp
, "mperf: %016llX\n", t
->mperf
);
442 outp
+= sprintf(outp
, "c1: %016llX\n", t
->c1
);
443 outp
+= sprintf(outp
, "msr0x%x: %08llX\n",
444 extra_delta_offset32
, t
->extra_delta32
);
445 outp
+= sprintf(outp
, "msr0x%x: %016llX\n",
446 extra_delta_offset64
, t
->extra_delta64
);
447 outp
+= sprintf(outp
, "msr0x%x: %08llX\n",
448 extra_msr_offset32
, t
->extra_msr32
);
449 outp
+= sprintf(outp
, "msr0x%x: %016llX\n",
450 extra_msr_offset64
, t
->extra_msr64
);
452 outp
+= sprintf(outp
, "IRQ: %08X\n", t
->irq_count
);
454 outp
+= sprintf(outp
, "SMI: %08X\n", t
->smi_count
);
458 outp
+= sprintf(outp
, "core: %d\n", c
->core_id
);
459 outp
+= sprintf(outp
, "c3: %016llX\n", c
->c3
);
460 outp
+= sprintf(outp
, "c6: %016llX\n", c
->c6
);
461 outp
+= sprintf(outp
, "c7: %016llX\n", c
->c7
);
462 outp
+= sprintf(outp
, "DTS: %dC\n", c
->core_temp_c
);
466 outp
+= sprintf(outp
, "package: %d\n", p
->package_id
);
468 outp
+= sprintf(outp
, "Weighted cores: %016llX\n", p
->pkg_wtd_core_c0
);
469 outp
+= sprintf(outp
, "Any cores: %016llX\n", p
->pkg_any_core_c0
);
470 outp
+= sprintf(outp
, "Any GFX: %016llX\n", p
->pkg_any_gfxe_c0
);
471 outp
+= sprintf(outp
, "CPU + GFX: %016llX\n", p
->pkg_both_core_gfxe_c0
);
473 outp
+= sprintf(outp
, "pc2: %016llX\n", p
->pc2
);
475 outp
+= sprintf(outp
, "pc3: %016llX\n", p
->pc3
);
477 outp
+= sprintf(outp
, "pc6: %016llX\n", p
->pc6
);
479 outp
+= sprintf(outp
, "pc7: %016llX\n", p
->pc7
);
480 outp
+= sprintf(outp
, "pc8: %016llX\n", p
->pc8
);
481 outp
+= sprintf(outp
, "pc9: %016llX\n", p
->pc9
);
482 outp
+= sprintf(outp
, "pc10: %016llX\n", p
->pc10
);
483 outp
+= sprintf(outp
, "Joules PKG: %0X\n", p
->energy_pkg
);
484 outp
+= sprintf(outp
, "Joules COR: %0X\n", p
->energy_cores
);
485 outp
+= sprintf(outp
, "Joules GFX: %0X\n", p
->energy_gfx
);
486 outp
+= sprintf(outp
, "Joules RAM: %0X\n", p
->energy_dram
);
487 outp
+= sprintf(outp
, "Throttle PKG: %0X\n",
488 p
->rapl_pkg_perf_status
);
489 outp
+= sprintf(outp
, "Throttle RAM: %0X\n",
490 p
->rapl_dram_perf_status
);
491 outp
+= sprintf(outp
, "PTM: %dC\n", p
->pkg_temp_c
);
494 outp
+= sprintf(outp
, "\n");
500 * column formatting convention & formats
502 int format_counters(struct thread_data
*t
, struct core_data
*c
,
505 double interval_float
;
508 /* if showing only 1st thread in core and this isn't one, bail out */
509 if (show_core_only
&& !(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
512 /* if showing only 1st thread in pkg and this isn't one, bail out */
513 if (show_pkg_only
&& !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
516 interval_float
= tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0;
518 /* topo columns, print blanks on 1st (average) line */
519 if (t
== &average
.threads
) {
521 outp
+= sprintf(outp
, "\t-");
523 outp
+= sprintf(outp
, "\t-");
525 outp
+= sprintf(outp
, "\t-");
529 outp
+= sprintf(outp
, "\t%d", p
->package_id
);
531 outp
+= sprintf(outp
, "\t-");
535 outp
+= sprintf(outp
, "\t%d", c
->core_id
);
537 outp
+= sprintf(outp
, "\t-");
540 outp
+= sprintf(outp
, "\t%d", t
->cpu_id
);
545 outp
+= sprintf(outp
, "\t%.0f",
546 1.0 / units
* t
->aperf
/ interval_float
);
550 outp
+= sprintf(outp
, "\t%.2f", 100.0 * t
->mperf
/t
->tsc
/tsc_tweak
);
555 outp
+= sprintf(outp
, "\t%.0f", base_hz
/ units
* t
->aperf
/ t
->mperf
);
557 outp
+= sprintf(outp
, "\t%.0f",
558 1.0 * t
->tsc
/ units
* t
->aperf
/ t
->mperf
/ interval_float
);
562 outp
+= sprintf(outp
, "\t%.0f", 1.0 * t
->tsc
/units
/interval_float
);
565 if (extra_delta_offset32
)
566 outp
+= sprintf(outp
, "\t%11llu", t
->extra_delta32
);
569 if (extra_delta_offset64
)
570 outp
+= sprintf(outp
, "\t%11llu", t
->extra_delta64
);
572 if (extra_msr_offset32
)
573 outp
+= sprintf(outp
, "\t0x%08llx", t
->extra_msr32
);
576 if (extra_msr_offset64
)
577 outp
+= sprintf(outp
, "\t0x%016llx", t
->extra_msr64
);
584 outp
+= sprintf(outp
, "\t%d", t
->irq_count
);
588 outp
+= sprintf(outp
, "\t%d", t
->smi_count
);
591 outp
+= sprintf(outp
, "\t%.2f", 100.0 * t
->c1
/t
->tsc
);
593 /* print per-core data only for 1st thread in core */
594 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
597 if (do_nhm_cstates
&& !do_slm_cstates
&& !do_knl_cstates
)
598 outp
+= sprintf(outp
, "\t%.2f", 100.0 * c
->c3
/t
->tsc
);
600 outp
+= sprintf(outp
, "\t%.2f", 100.0 * c
->c6
/t
->tsc
);
602 outp
+= sprintf(outp
, "\t%.2f", 100.0 * c
->c7
/t
->tsc
);
605 outp
+= sprintf(outp
, "\t%d", c
->core_temp_c
);
607 /* print per-package data only for 1st core in package */
608 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
613 outp
+= sprintf(outp
, "\t%d", p
->pkg_temp_c
);
617 if (p
->gfx_rc6_ms
== -1) { /* detect GFX counter reset */
618 outp
+= sprintf(outp
, "\t**.**");
620 outp
+= sprintf(outp
, "\t%.2f",
621 p
->gfx_rc6_ms
/ 10.0 / interval_float
);
627 outp
+= sprintf(outp
, "\t%d", p
->gfx_mhz
);
629 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
630 if (do_skl_residency
) {
631 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pkg_wtd_core_c0
/t
->tsc
);
632 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pkg_any_core_c0
/t
->tsc
);
633 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pkg_any_gfxe_c0
/t
->tsc
);
634 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pkg_both_core_gfxe_c0
/t
->tsc
);
638 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pc2
/t
->tsc
);
640 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pc3
/t
->tsc
);
642 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pc6
/t
->tsc
);
644 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pc7
/t
->tsc
);
646 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pc8
/t
->tsc
);
647 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pc9
/t
->tsc
);
648 outp
+= sprintf(outp
, "\t%.2f", 100.0 * p
->pc10
/t
->tsc
);
652 * If measurement interval exceeds minimum RAPL Joule Counter range,
653 * indicate that results are suspect by printing "**" in fraction place.
655 if (interval_float
< rapl_joule_counter_range
)
660 if (do_rapl
&& !rapl_joules
) {
661 if (do_rapl
& RAPL_PKG
)
662 outp
+= sprintf(outp
, fmt8
, p
->energy_pkg
* rapl_energy_units
/ interval_float
);
663 if (do_rapl
& RAPL_CORES_ENERGY_STATUS
)
664 outp
+= sprintf(outp
, fmt8
, p
->energy_cores
* rapl_energy_units
/ interval_float
);
665 if (do_rapl
& RAPL_GFX
)
666 outp
+= sprintf(outp
, fmt8
, p
->energy_gfx
* rapl_energy_units
/ interval_float
);
667 if (do_rapl
& RAPL_DRAM
)
668 outp
+= sprintf(outp
, fmt8
, p
->energy_dram
* rapl_dram_energy_units
/ interval_float
);
669 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
670 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_pkg_perf_status
* rapl_time_units
/ interval_float
);
671 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
672 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_dram_perf_status
* rapl_time_units
/ interval_float
);
673 } else if (do_rapl
&& rapl_joules
) {
674 if (do_rapl
& RAPL_PKG
)
675 outp
+= sprintf(outp
, fmt8
,
676 p
->energy_pkg
* rapl_energy_units
);
677 if (do_rapl
& RAPL_CORES
)
678 outp
+= sprintf(outp
, fmt8
,
679 p
->energy_cores
* rapl_energy_units
);
680 if (do_rapl
& RAPL_GFX
)
681 outp
+= sprintf(outp
, fmt8
,
682 p
->energy_gfx
* rapl_energy_units
);
683 if (do_rapl
& RAPL_DRAM
)
684 outp
+= sprintf(outp
, fmt8
,
685 p
->energy_dram
* rapl_dram_energy_units
);
686 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
687 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_pkg_perf_status
* rapl_time_units
/ interval_float
);
688 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
689 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_dram_perf_status
* rapl_time_units
/ interval_float
);
692 outp
+= sprintf(outp
, "\n");
697 void flush_output_stdout(void)
706 fputs(output_buffer
, filep
);
709 outp
= output_buffer
;
711 void flush_output_stderr(void)
713 fputs(output_buffer
, outf
);
715 outp
= output_buffer
;
717 void format_all_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
721 if (!printed
|| !summary_only
)
724 if (topo
.num_cpus
> 1)
725 format_counters(&average
.threads
, &average
.cores
,
733 for_all_cpus(format_counters
, t
, c
, p
);
736 #define DELTA_WRAP32(new, old) \
740 old = 0x100000000 + new - old; \
744 delta_package(struct pkg_data
*new, struct pkg_data
*old
)
747 if (do_skl_residency
) {
748 old
->pkg_wtd_core_c0
= new->pkg_wtd_core_c0
- old
->pkg_wtd_core_c0
;
749 old
->pkg_any_core_c0
= new->pkg_any_core_c0
- old
->pkg_any_core_c0
;
750 old
->pkg_any_gfxe_c0
= new->pkg_any_gfxe_c0
- old
->pkg_any_gfxe_c0
;
751 old
->pkg_both_core_gfxe_c0
= new->pkg_both_core_gfxe_c0
- old
->pkg_both_core_gfxe_c0
;
753 old
->pc2
= new->pc2
- old
->pc2
;
755 old
->pc3
= new->pc3
- old
->pc3
;
757 old
->pc6
= new->pc6
- old
->pc6
;
759 old
->pc7
= new->pc7
- old
->pc7
;
760 old
->pc8
= new->pc8
- old
->pc8
;
761 old
->pc9
= new->pc9
- old
->pc9
;
762 old
->pc10
= new->pc10
- old
->pc10
;
763 old
->pkg_temp_c
= new->pkg_temp_c
;
765 /* flag an error when rc6 counter resets/wraps */
766 if (old
->gfx_rc6_ms
> new->gfx_rc6_ms
)
767 old
->gfx_rc6_ms
= -1;
769 old
->gfx_rc6_ms
= new->gfx_rc6_ms
- old
->gfx_rc6_ms
;
771 old
->gfx_mhz
= new->gfx_mhz
;
773 DELTA_WRAP32(new->energy_pkg
, old
->energy_pkg
);
774 DELTA_WRAP32(new->energy_cores
, old
->energy_cores
);
775 DELTA_WRAP32(new->energy_gfx
, old
->energy_gfx
);
776 DELTA_WRAP32(new->energy_dram
, old
->energy_dram
);
777 DELTA_WRAP32(new->rapl_pkg_perf_status
, old
->rapl_pkg_perf_status
);
778 DELTA_WRAP32(new->rapl_dram_perf_status
, old
->rapl_dram_perf_status
);
784 delta_core(struct core_data
*new, struct core_data
*old
)
786 old
->c3
= new->c3
- old
->c3
;
787 old
->c6
= new->c6
- old
->c6
;
788 old
->c7
= new->c7
- old
->c7
;
789 old
->core_temp_c
= new->core_temp_c
;
796 delta_thread(struct thread_data
*new, struct thread_data
*old
,
797 struct core_data
*core_delta
)
799 old
->tsc
= new->tsc
- old
->tsc
;
801 /* check for TSC < 1 Mcycles over interval */
802 if (old
->tsc
< (1000 * 1000))
803 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
804 "You can disable all c-states by booting with \"idle=poll\"\n"
805 "or just the deep ones with \"processor.max_cstate=1\"");
807 old
->c1
= new->c1
- old
->c1
;
810 if ((new->aperf
> old
->aperf
) && (new->mperf
> old
->mperf
)) {
811 old
->aperf
= new->aperf
- old
->aperf
;
812 old
->mperf
= new->mperf
- old
->mperf
;
819 if (use_c1_residency_msr
) {
821 * Some models have a dedicated C1 residency MSR,
822 * which should be more accurate than the derivation below.
826 * As counter collection is not atomic,
827 * it is possible for mperf's non-halted cycles + idle states
828 * to exceed TSC's all cycles: show c1 = 0% in that case.
830 if ((old
->mperf
+ core_delta
->c3
+ core_delta
->c6
+ core_delta
->c7
) > old
->tsc
)
833 /* normal case, derive c1 */
834 old
->c1
= old
->tsc
- old
->mperf
- core_delta
->c3
835 - core_delta
->c6
- core_delta
->c7
;
839 if (old
->mperf
== 0) {
841 fprintf(outf
, "cpu%d MPERF 0!\n", old
->cpu_id
);
842 old
->mperf
= 1; /* divide by 0 protection */
845 old
->extra_delta32
= new->extra_delta32
- old
->extra_delta32
;
846 old
->extra_delta32
&= 0xFFFFFFFF;
848 old
->extra_delta64
= new->extra_delta64
- old
->extra_delta64
;
851 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
853 old
->extra_msr32
= new->extra_msr32
;
854 old
->extra_msr64
= new->extra_msr64
;
857 old
->irq_count
= new->irq_count
- old
->irq_count
;
860 old
->smi_count
= new->smi_count
- old
->smi_count
;
865 int delta_cpu(struct thread_data
*t
, struct core_data
*c
,
866 struct pkg_data
*p
, struct thread_data
*t2
,
867 struct core_data
*c2
, struct pkg_data
*p2
)
871 /* calculate core delta only for 1st thread in core */
872 if (t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
)
875 /* always calculate thread delta */
876 retval
= delta_thread(t
, t2
, c2
); /* c2 is core delta */
880 /* calculate package delta only for 1st core in package */
881 if (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)
882 retval
= delta_package(p
, p2
);
887 void clear_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
894 t
->extra_delta32
= 0;
895 t
->extra_delta64
= 0;
900 /* tells format_counters to dump all fields from this set */
901 t
->flags
= CPU_IS_FIRST_THREAD_IN_CORE
| CPU_IS_FIRST_CORE_IN_PACKAGE
;
908 p
->pkg_wtd_core_c0
= 0;
909 p
->pkg_any_core_c0
= 0;
910 p
->pkg_any_gfxe_c0
= 0;
911 p
->pkg_both_core_gfxe_c0
= 0;
928 p
->rapl_pkg_perf_status
= 0;
929 p
->rapl_dram_perf_status
= 0;
935 int sum_counters(struct thread_data
*t
, struct core_data
*c
,
938 average
.threads
.tsc
+= t
->tsc
;
939 average
.threads
.aperf
+= t
->aperf
;
940 average
.threads
.mperf
+= t
->mperf
;
941 average
.threads
.c1
+= t
->c1
;
943 average
.threads
.extra_delta32
+= t
->extra_delta32
;
944 average
.threads
.extra_delta64
+= t
->extra_delta64
;
946 average
.threads
.irq_count
+= t
->irq_count
;
947 average
.threads
.smi_count
+= t
->smi_count
;
949 /* sum per-core values only for 1st thread in core */
950 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
953 average
.cores
.c3
+= c
->c3
;
954 average
.cores
.c6
+= c
->c6
;
955 average
.cores
.c7
+= c
->c7
;
957 average
.cores
.core_temp_c
= MAX(average
.cores
.core_temp_c
, c
->core_temp_c
);
959 /* sum per-pkg values only for 1st core in pkg */
960 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
963 if (do_skl_residency
) {
964 average
.packages
.pkg_wtd_core_c0
+= p
->pkg_wtd_core_c0
;
965 average
.packages
.pkg_any_core_c0
+= p
->pkg_any_core_c0
;
966 average
.packages
.pkg_any_gfxe_c0
+= p
->pkg_any_gfxe_c0
;
967 average
.packages
.pkg_both_core_gfxe_c0
+= p
->pkg_both_core_gfxe_c0
;
970 average
.packages
.pc2
+= p
->pc2
;
972 average
.packages
.pc3
+= p
->pc3
;
974 average
.packages
.pc6
+= p
->pc6
;
976 average
.packages
.pc7
+= p
->pc7
;
977 average
.packages
.pc8
+= p
->pc8
;
978 average
.packages
.pc9
+= p
->pc9
;
979 average
.packages
.pc10
+= p
->pc10
;
981 average
.packages
.energy_pkg
+= p
->energy_pkg
;
982 average
.packages
.energy_dram
+= p
->energy_dram
;
983 average
.packages
.energy_cores
+= p
->energy_cores
;
984 average
.packages
.energy_gfx
+= p
->energy_gfx
;
986 average
.packages
.gfx_rc6_ms
= p
->gfx_rc6_ms
;
987 average
.packages
.gfx_mhz
= p
->gfx_mhz
;
989 average
.packages
.pkg_temp_c
= MAX(average
.packages
.pkg_temp_c
, p
->pkg_temp_c
);
991 average
.packages
.rapl_pkg_perf_status
+= p
->rapl_pkg_perf_status
;
992 average
.packages
.rapl_dram_perf_status
+= p
->rapl_dram_perf_status
;
996 * sum the counters for all cpus in the system
997 * compute the weighted average
999 void compute_average(struct thread_data
*t
, struct core_data
*c
,
1002 clear_counters(&average
.threads
, &average
.cores
, &average
.packages
);
1004 for_all_cpus(sum_counters
, t
, c
, p
);
1006 average
.threads
.tsc
/= topo
.num_cpus
;
1007 average
.threads
.aperf
/= topo
.num_cpus
;
1008 average
.threads
.mperf
/= topo
.num_cpus
;
1009 average
.threads
.c1
/= topo
.num_cpus
;
1011 average
.threads
.extra_delta32
/= topo
.num_cpus
;
1012 average
.threads
.extra_delta32
&= 0xFFFFFFFF;
1014 average
.threads
.extra_delta64
/= topo
.num_cpus
;
1016 average
.cores
.c3
/= topo
.num_cores
;
1017 average
.cores
.c6
/= topo
.num_cores
;
1018 average
.cores
.c7
/= topo
.num_cores
;
1020 if (do_skl_residency
) {
1021 average
.packages
.pkg_wtd_core_c0
/= topo
.num_packages
;
1022 average
.packages
.pkg_any_core_c0
/= topo
.num_packages
;
1023 average
.packages
.pkg_any_gfxe_c0
/= topo
.num_packages
;
1024 average
.packages
.pkg_both_core_gfxe_c0
/= topo
.num_packages
;
1027 average
.packages
.pc2
/= topo
.num_packages
;
1029 average
.packages
.pc3
/= topo
.num_packages
;
1031 average
.packages
.pc6
/= topo
.num_packages
;
1033 average
.packages
.pc7
/= topo
.num_packages
;
1035 average
.packages
.pc8
/= topo
.num_packages
;
1036 average
.packages
.pc9
/= topo
.num_packages
;
1037 average
.packages
.pc10
/= topo
.num_packages
;
1040 static unsigned long long rdtsc(void)
1042 unsigned int low
, high
;
1044 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
1046 return low
| ((unsigned long long)high
) << 32;
1052 * acquire and record local counters for that cpu
1054 int get_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1056 int cpu
= t
->cpu_id
;
1057 unsigned long long msr
;
1058 int aperf_mperf_retry_count
= 0;
1060 if (cpu_migrate(cpu
)) {
1061 fprintf(outf
, "Could not migrate to CPU %d\n", cpu
);
1066 t
->tsc
= rdtsc(); /* we are running on local CPU of interest */
1069 unsigned long long tsc_before
, tsc_between
, tsc_after
, aperf_time
, mperf_time
;
1072 * The TSC, APERF and MPERF must be read together for
1073 * APERF/MPERF and MPERF/TSC to give accurate results.
1075 * Unfortunately, APERF and MPERF are read by
1076 * individual system call, so delays may occur
1077 * between them. If the time to read them
1078 * varies by a large amount, we re-read them.
1082 * This initial dummy APERF read has been seen to
1083 * reduce jitter in the subsequent reads.
1086 if (get_msr(cpu
, MSR_IA32_APERF
, &t
->aperf
))
1089 t
->tsc
= rdtsc(); /* re-read close to APERF */
1091 tsc_before
= t
->tsc
;
1093 if (get_msr(cpu
, MSR_IA32_APERF
, &t
->aperf
))
1096 tsc_between
= rdtsc();
1098 if (get_msr(cpu
, MSR_IA32_MPERF
, &t
->mperf
))
1101 tsc_after
= rdtsc();
1103 aperf_time
= tsc_between
- tsc_before
;
1104 mperf_time
= tsc_after
- tsc_between
;
1107 * If the system call latency to read APERF and MPERF
1108 * differ by more than 2x, then try again.
1110 if ((aperf_time
> (2 * mperf_time
)) || (mperf_time
> (2 * aperf_time
))) {
1111 aperf_mperf_retry_count
++;
1112 if (aperf_mperf_retry_count
< 5)
1115 warnx("cpu%d jitter %lld %lld",
1116 cpu
, aperf_time
, mperf_time
);
1118 aperf_mperf_retry_count
= 0;
1120 t
->aperf
= t
->aperf
* aperf_mperf_multiplier
;
1121 t
->mperf
= t
->mperf
* aperf_mperf_multiplier
;
1125 t
->irq_count
= irqs_per_cpu
[cpu
];
1127 if (get_msr(cpu
, MSR_SMI_COUNT
, &msr
))
1129 t
->smi_count
= msr
& 0xFFFFFFFF;
1131 if (extra_delta_offset32
) {
1132 if (get_msr(cpu
, extra_delta_offset32
, &msr
))
1134 t
->extra_delta32
= msr
& 0xFFFFFFFF;
1137 if (extra_delta_offset64
)
1138 if (get_msr(cpu
, extra_delta_offset64
, &t
->extra_delta64
))
1141 if (extra_msr_offset32
) {
1142 if (get_msr(cpu
, extra_msr_offset32
, &msr
))
1144 t
->extra_msr32
= msr
& 0xFFFFFFFF;
1147 if (extra_msr_offset64
)
1148 if (get_msr(cpu
, extra_msr_offset64
, &t
->extra_msr64
))
1151 if (use_c1_residency_msr
) {
1152 if (get_msr(cpu
, MSR_CORE_C1_RES
, &t
->c1
))
1156 /* collect core counters only for 1st thread in core */
1157 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
1160 if (do_nhm_cstates
&& !do_slm_cstates
&& !do_knl_cstates
) {
1161 if (get_msr(cpu
, MSR_CORE_C3_RESIDENCY
, &c
->c3
))
1165 if (do_nhm_cstates
&& !do_knl_cstates
) {
1166 if (get_msr(cpu
, MSR_CORE_C6_RESIDENCY
, &c
->c6
))
1168 } else if (do_knl_cstates
) {
1169 if (get_msr(cpu
, MSR_KNL_CORE_C6_RESIDENCY
, &c
->c6
))
1174 if (get_msr(cpu
, MSR_CORE_C7_RESIDENCY
, &c
->c7
))
1178 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
1180 c
->core_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
1184 /* collect package counters only for 1st core in package */
1185 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1188 if (do_skl_residency
) {
1189 if (get_msr(cpu
, MSR_PKG_WEIGHTED_CORE_C0_RES
, &p
->pkg_wtd_core_c0
))
1191 if (get_msr(cpu
, MSR_PKG_ANY_CORE_C0_RES
, &p
->pkg_any_core_c0
))
1193 if (get_msr(cpu
, MSR_PKG_ANY_GFXE_C0_RES
, &p
->pkg_any_gfxe_c0
))
1195 if (get_msr(cpu
, MSR_PKG_BOTH_CORE_GFXE_C0_RES
, &p
->pkg_both_core_gfxe_c0
))
1199 if (get_msr(cpu
, MSR_PKG_C3_RESIDENCY
, &p
->pc3
))
1202 if (get_msr(cpu
, MSR_PKG_C6_RESIDENCY
, &p
->pc6
))
1205 if (get_msr(cpu
, MSR_PKG_C2_RESIDENCY
, &p
->pc2
))
1208 if (get_msr(cpu
, MSR_PKG_C7_RESIDENCY
, &p
->pc7
))
1211 if (get_msr(cpu
, MSR_PKG_C8_RESIDENCY
, &p
->pc8
))
1213 if (get_msr(cpu
, MSR_PKG_C9_RESIDENCY
, &p
->pc9
))
1215 if (get_msr(cpu
, MSR_PKG_C10_RESIDENCY
, &p
->pc10
))
1218 if (do_rapl
& RAPL_PKG
) {
1219 if (get_msr(cpu
, MSR_PKG_ENERGY_STATUS
, &msr
))
1221 p
->energy_pkg
= msr
& 0xFFFFFFFF;
1223 if (do_rapl
& RAPL_CORES_ENERGY_STATUS
) {
1224 if (get_msr(cpu
, MSR_PP0_ENERGY_STATUS
, &msr
))
1226 p
->energy_cores
= msr
& 0xFFFFFFFF;
1228 if (do_rapl
& RAPL_DRAM
) {
1229 if (get_msr(cpu
, MSR_DRAM_ENERGY_STATUS
, &msr
))
1231 p
->energy_dram
= msr
& 0xFFFFFFFF;
1233 if (do_rapl
& RAPL_GFX
) {
1234 if (get_msr(cpu
, MSR_PP1_ENERGY_STATUS
, &msr
))
1236 p
->energy_gfx
= msr
& 0xFFFFFFFF;
1238 if (do_rapl
& RAPL_PKG_PERF_STATUS
) {
1239 if (get_msr(cpu
, MSR_PKG_PERF_STATUS
, &msr
))
1241 p
->rapl_pkg_perf_status
= msr
& 0xFFFFFFFF;
1243 if (do_rapl
& RAPL_DRAM_PERF_STATUS
) {
1244 if (get_msr(cpu
, MSR_DRAM_PERF_STATUS
, &msr
))
1246 p
->rapl_dram_perf_status
= msr
& 0xFFFFFFFF;
1249 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
1251 p
->pkg_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
1255 p
->gfx_rc6_ms
= gfx_cur_rc6_ms
;
1258 p
->gfx_mhz
= gfx_cur_mhz
;
1264 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1265 * If you change the values, note they are used both in comparisons
1266 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1269 #define PCLUKN 0 /* Unknown */
1270 #define PCLRSV 1 /* Reserved */
1271 #define PCL__0 2 /* PC0 */
1272 #define PCL__1 3 /* PC1 */
1273 #define PCL__2 4 /* PC2 */
1274 #define PCL__3 5 /* PC3 */
1275 #define PCL__4 6 /* PC4 */
1276 #define PCL__6 7 /* PC6 */
1277 #define PCL_6N 8 /* PC6 No Retention */
1278 #define PCL_6R 9 /* PC6 Retention */
1279 #define PCL__7 10 /* PC7 */
1280 #define PCL_7S 11 /* PC7 Shrink */
1281 #define PCL__8 12 /* PC8 */
1282 #define PCL__9 13 /* PC9 */
1283 #define PCLUNL 14 /* Unlimited */
1285 int pkg_cstate_limit
= PCLUKN
;
1286 char *pkg_cstate_limit_strings
[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1287 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1289 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
};
1290 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
};
1291 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
};
1292 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
};
1293 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
};
1294 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
};
1295 int bxt_pkg_cstate_limits
[16] = {PCL__0
, PCL__2
, PCLUNL
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
, PCLRSV
};
1299 calculate_tsc_tweak()
1301 tsc_tweak
= base_hz
/ tsc_hz
;
1305 dump_nhm_platform_info(void)
1307 unsigned long long msr
;
1310 get_msr(base_cpu
, MSR_PLATFORM_INFO
, &msr
);
1312 fprintf(outf
, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu
, msr
);
1314 ratio
= (msr
>> 40) & 0xFF;
1315 fprintf(outf
, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1316 ratio
, bclk
, ratio
* bclk
);
1318 ratio
= (msr
>> 8) & 0xFF;
1319 fprintf(outf
, "%d * %.0f = %.0f MHz base frequency\n",
1320 ratio
, bclk
, ratio
* bclk
);
1322 get_msr(base_cpu
, MSR_IA32_POWER_CTL
, &msr
);
1323 fprintf(outf
, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1324 base_cpu
, msr
, msr
& 0x2 ? "EN" : "DIS");
1330 dump_hsw_turbo_ratio_limits(void)
1332 unsigned long long msr
;
1335 get_msr(base_cpu
, MSR_TURBO_RATIO_LIMIT2
, &msr
);
1337 fprintf(outf
, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu
, msr
);
1339 ratio
= (msr
>> 8) & 0xFF;
1341 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1342 ratio
, bclk
, ratio
* bclk
);
1344 ratio
= (msr
>> 0) & 0xFF;
1346 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1347 ratio
, bclk
, ratio
* bclk
);
1352 dump_ivt_turbo_ratio_limits(void)
1354 unsigned long long msr
;
1357 get_msr(base_cpu
, MSR_TURBO_RATIO_LIMIT1
, &msr
);
1359 fprintf(outf
, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu
, msr
);
1361 ratio
= (msr
>> 56) & 0xFF;
1363 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1364 ratio
, bclk
, ratio
* bclk
);
1366 ratio
= (msr
>> 48) & 0xFF;
1368 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1369 ratio
, bclk
, ratio
* bclk
);
1371 ratio
= (msr
>> 40) & 0xFF;
1373 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1374 ratio
, bclk
, ratio
* bclk
);
1376 ratio
= (msr
>> 32) & 0xFF;
1378 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1379 ratio
, bclk
, ratio
* bclk
);
1381 ratio
= (msr
>> 24) & 0xFF;
1383 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1384 ratio
, bclk
, ratio
* bclk
);
1386 ratio
= (msr
>> 16) & 0xFF;
1388 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1389 ratio
, bclk
, ratio
* bclk
);
1391 ratio
= (msr
>> 8) & 0xFF;
1393 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1394 ratio
, bclk
, ratio
* bclk
);
1396 ratio
= (msr
>> 0) & 0xFF;
1398 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1399 ratio
, bclk
, ratio
* bclk
);
1404 dump_nhm_turbo_ratio_limits(void)
1406 unsigned long long msr
;
1409 get_msr(base_cpu
, MSR_TURBO_RATIO_LIMIT
, &msr
);
1411 fprintf(outf
, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu
, msr
);
1413 ratio
= (msr
>> 56) & 0xFF;
1415 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1416 ratio
, bclk
, ratio
* bclk
);
1418 ratio
= (msr
>> 48) & 0xFF;
1420 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1421 ratio
, bclk
, ratio
* bclk
);
1423 ratio
= (msr
>> 40) & 0xFF;
1425 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1426 ratio
, bclk
, ratio
* bclk
);
1428 ratio
= (msr
>> 32) & 0xFF;
1430 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1431 ratio
, bclk
, ratio
* bclk
);
1433 ratio
= (msr
>> 24) & 0xFF;
1435 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1436 ratio
, bclk
, ratio
* bclk
);
1438 ratio
= (msr
>> 16) & 0xFF;
1440 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1441 ratio
, bclk
, ratio
* bclk
);
1443 ratio
= (msr
>> 8) & 0xFF;
1445 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1446 ratio
, bclk
, ratio
* bclk
);
1448 ratio
= (msr
>> 0) & 0xFF;
1450 fprintf(outf
, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1451 ratio
, bclk
, ratio
* bclk
);
1456 dump_knl_turbo_ratio_limits(void)
1458 const unsigned int buckets_no
= 7;
1460 unsigned long long msr
;
1461 int delta_cores
, delta_ratio
;
1463 unsigned int cores
[buckets_no
];
1464 unsigned int ratio
[buckets_no
];
1466 get_msr(base_cpu
, MSR_TURBO_RATIO_LIMIT
, &msr
);
1468 fprintf(outf
, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
1472 * Turbo encoding in KNL is as follows:
1474 * [7:1] -- Base value of number of active cores of bucket 1.
1475 * [15:8] -- Base value of freq ratio of bucket 1.
1476 * [20:16] -- +ve delta of number of active cores of bucket 2.
1477 * i.e. active cores of bucket 2 =
1478 * active cores of bucket 1 + delta
1479 * [23:21] -- Negative delta of freq ratio of bucket 2.
1480 * i.e. freq ratio of bucket 2 =
1481 * freq ratio of bucket 1 - delta
1482 * [28:24]-- +ve delta of number of active cores of bucket 3.
1483 * [31:29]-- -ve delta of freq ratio of bucket 3.
1484 * [36:32]-- +ve delta of number of active cores of bucket 4.
1485 * [39:37]-- -ve delta of freq ratio of bucket 4.
1486 * [44:40]-- +ve delta of number of active cores of bucket 5.
1487 * [47:45]-- -ve delta of freq ratio of bucket 5.
1488 * [52:48]-- +ve delta of number of active cores of bucket 6.
1489 * [55:53]-- -ve delta of freq ratio of bucket 6.
1490 * [60:56]-- +ve delta of number of active cores of bucket 7.
1491 * [63:61]-- -ve delta of freq ratio of bucket 7.
1495 cores
[b_nr
] = (msr
& 0xFF) >> 1;
1496 ratio
[b_nr
] = (msr
>> 8) & 0xFF;
1498 for (i
= 16; i
< 64; i
+= 8) {
1499 delta_cores
= (msr
>> i
) & 0x1F;
1500 delta_ratio
= (msr
>> (i
+ 5)) & 0x7;
1502 cores
[b_nr
+ 1] = cores
[b_nr
] + delta_cores
;
1503 ratio
[b_nr
+ 1] = ratio
[b_nr
] - delta_ratio
;
1507 for (i
= buckets_no
- 1; i
>= 0; i
--)
1508 if (i
> 0 ? ratio
[i
] != ratio
[i
- 1] : 1)
1510 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1511 ratio
[i
], bclk
, ratio
[i
] * bclk
, cores
[i
]);
1515 dump_nhm_cst_cfg(void)
1517 unsigned long long msr
;
1519 get_msr(base_cpu
, MSR_NHM_SNB_PKG_CST_CFG_CTL
, &msr
);
1521 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1522 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1524 fprintf(outf
, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu
, msr
);
1526 fprintf(outf
, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1527 (msr
& SNB_C3_AUTO_UNDEMOTE
) ? "UNdemote-C3, " : "",
1528 (msr
& SNB_C1_AUTO_UNDEMOTE
) ? "UNdemote-C1, " : "",
1529 (msr
& NHM_C3_AUTO_DEMOTE
) ? "demote-C3, " : "",
1530 (msr
& NHM_C1_AUTO_DEMOTE
) ? "demote-C1, " : "",
1531 (msr
& (1 << 15)) ? "" : "UN",
1532 (unsigned int)msr
& 0xF,
1533 pkg_cstate_limit_strings
[pkg_cstate_limit
]);
1538 dump_config_tdp(void)
1540 unsigned long long msr
;
1542 get_msr(base_cpu
, MSR_CONFIG_TDP_NOMINAL
, &msr
);
1543 fprintf(outf
, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu
, msr
);
1544 fprintf(outf
, " (base_ratio=%d)\n", (unsigned int)msr
& 0xFF);
1546 get_msr(base_cpu
, MSR_CONFIG_TDP_LEVEL_1
, &msr
);
1547 fprintf(outf
, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu
, msr
);
1549 fprintf(outf
, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr
>> 48) & 0x7FFF);
1550 fprintf(outf
, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr
>> 32) & 0x7FFF);
1551 fprintf(outf
, "LVL1_RATIO=%d ", (unsigned int)(msr
>> 16) & 0xFF);
1552 fprintf(outf
, "PKG_TDP_LVL1=%d", (unsigned int)(msr
) & 0x7FFF);
1554 fprintf(outf
, ")\n");
1556 get_msr(base_cpu
, MSR_CONFIG_TDP_LEVEL_2
, &msr
);
1557 fprintf(outf
, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu
, msr
);
1559 fprintf(outf
, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr
>> 48) & 0x7FFF);
1560 fprintf(outf
, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr
>> 32) & 0x7FFF);
1561 fprintf(outf
, "LVL2_RATIO=%d ", (unsigned int)(msr
>> 16) & 0xFF);
1562 fprintf(outf
, "PKG_TDP_LVL2=%d", (unsigned int)(msr
) & 0x7FFF);
1564 fprintf(outf
, ")\n");
1566 get_msr(base_cpu
, MSR_CONFIG_TDP_CONTROL
, &msr
);
1567 fprintf(outf
, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu
, msr
);
1569 fprintf(outf
, "TDP_LEVEL=%d ", (unsigned int)(msr
) & 0x3);
1570 fprintf(outf
, " lock=%d", (unsigned int)(msr
>> 31) & 1);
1571 fprintf(outf
, ")\n");
1573 get_msr(base_cpu
, MSR_TURBO_ACTIVATION_RATIO
, &msr
);
1574 fprintf(outf
, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu
, msr
);
1575 fprintf(outf
, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr
) & 0xFF);
1576 fprintf(outf
, " lock=%d", (unsigned int)(msr
>> 31) & 1);
1577 fprintf(outf
, ")\n");
1580 unsigned int irtl_time_units
[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1582 void print_irtl(void)
1584 unsigned long long msr
;
1586 get_msr(base_cpu
, MSR_PKGC3_IRTL
, &msr
);
1587 fprintf(outf
, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu
, msr
);
1588 fprintf(outf
, "%svalid, %lld ns)\n", msr
& (1 << 15) ? "" : "NOT",
1589 (msr
& 0x3FF) * irtl_time_units
[(msr
>> 10) & 0x3]);
1591 get_msr(base_cpu
, MSR_PKGC6_IRTL
, &msr
);
1592 fprintf(outf
, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu
, msr
);
1593 fprintf(outf
, "%svalid, %lld ns)\n", msr
& (1 << 15) ? "" : "NOT",
1594 (msr
& 0x3FF) * irtl_time_units
[(msr
>> 10) & 0x3]);
1596 get_msr(base_cpu
, MSR_PKGC7_IRTL
, &msr
);
1597 fprintf(outf
, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu
, msr
);
1598 fprintf(outf
, "%svalid, %lld ns)\n", msr
& (1 << 15) ? "" : "NOT",
1599 (msr
& 0x3FF) * irtl_time_units
[(msr
>> 10) & 0x3]);
1604 get_msr(base_cpu
, MSR_PKGC8_IRTL
, &msr
);
1605 fprintf(outf
, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu
, msr
);
1606 fprintf(outf
, "%svalid, %lld ns)\n", msr
& (1 << 15) ? "" : "NOT",
1607 (msr
& 0x3FF) * irtl_time_units
[(msr
>> 10) & 0x3]);
1609 get_msr(base_cpu
, MSR_PKGC9_IRTL
, &msr
);
1610 fprintf(outf
, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu
, msr
);
1611 fprintf(outf
, "%svalid, %lld ns)\n", msr
& (1 << 15) ? "" : "NOT",
1612 (msr
& 0x3FF) * irtl_time_units
[(msr
>> 10) & 0x3]);
1614 get_msr(base_cpu
, MSR_PKGC10_IRTL
, &msr
);
1615 fprintf(outf
, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu
, msr
);
1616 fprintf(outf
, "%svalid, %lld ns)\n", msr
& (1 << 15) ? "" : "NOT",
1617 (msr
& 0x3FF) * irtl_time_units
[(msr
>> 10) & 0x3]);
1620 void free_fd_percpu(void)
1624 for (i
= 0; i
< topo
.max_cpu_num
+ 1; ++i
) {
1625 if (fd_percpu
[i
] != 0)
1626 close(fd_percpu
[i
]);
1632 void free_all_buffers(void)
1634 CPU_FREE(cpu_present_set
);
1635 cpu_present_set
= NULL
;
1636 cpu_present_setsize
= 0;
1638 CPU_FREE(cpu_affinity_set
);
1639 cpu_affinity_set
= NULL
;
1640 cpu_affinity_setsize
= 0;
1648 package_even
= NULL
;
1658 free(output_buffer
);
1659 output_buffer
= NULL
;
1664 free(irq_column_2_cpu
);
1669 * Open a file, and exit on failure
1671 FILE *fopen_or_die(const char *path
, const char *mode
)
1673 FILE *filep
= fopen(path
, mode
);
1675 err(1, "%s: open failed", path
);
1680 * Parse a file containing a single int.
1682 int parse_int_file(const char *fmt
, ...)
1685 char path
[PATH_MAX
];
1689 va_start(args
, fmt
);
1690 vsnprintf(path
, sizeof(path
), fmt
, args
);
1692 filep
= fopen_or_die(path
, "r");
1693 if (fscanf(filep
, "%d", &value
) != 1)
1694 err(1, "%s: failed to parse number from file", path
);
1700 * get_cpu_position_in_core(cpu)
1701 * return the position of the CPU among its HT siblings in the core
1702 * return -1 if the sibling is not in list
1704 int get_cpu_position_in_core(int cpu
)
1713 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1715 filep
= fopen(path
, "r");
1716 if (filep
== NULL
) {
1721 for (i
= 0; i
< topo
.num_threads_per_core
; i
++) {
1722 fscanf(filep
, "%d", &this_cpu
);
1723 if (this_cpu
== cpu
) {
1728 /* Account for no separator after last thread*/
1729 if (i
!= (topo
.num_threads_per_core
- 1))
1730 fscanf(filep
, "%c", &character
);
1738 * cpu_is_first_core_in_package(cpu)
1739 * return 1 if given CPU is 1st core in package
1741 int cpu_is_first_core_in_package(int cpu
)
1743 return cpu
== parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu
);
1746 int get_physical_package_id(int cpu
)
1748 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu
);
1751 int get_core_id(int cpu
)
1753 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu
);
1756 int get_num_ht_siblings(int cpu
)
1766 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu
);
1767 filep
= fopen_or_die(path
, "r");
1771 * A ',' separated or '-' separated set of numbers
1772 * (eg 1-2 or 1,3,4,5)
1774 fscanf(filep
, "%d%c\n", &sib1
, &character
);
1775 fseek(filep
, 0, SEEK_SET
);
1776 fgets(str
, 100, filep
);
1777 ch
= strchr(str
, character
);
1778 while (ch
!= NULL
) {
1780 ch
= strchr(ch
+1, character
);
1788 * run func(thread, core, package) in topology order
1789 * skip non-present cpus
1792 int for_all_cpus_2(int (func
)(struct thread_data
*, struct core_data
*,
1793 struct pkg_data
*, struct thread_data
*, struct core_data
*,
1794 struct pkg_data
*), struct thread_data
*thread_base
,
1795 struct core_data
*core_base
, struct pkg_data
*pkg_base
,
1796 struct thread_data
*thread_base2
, struct core_data
*core_base2
,
1797 struct pkg_data
*pkg_base2
)
1799 int retval
, pkg_no
, core_no
, thread_no
;
1801 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
1802 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
1803 for (thread_no
= 0; thread_no
<
1804 topo
.num_threads_per_core
; ++thread_no
) {
1805 struct thread_data
*t
, *t2
;
1806 struct core_data
*c
, *c2
;
1807 struct pkg_data
*p
, *p2
;
1809 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
1811 if (cpu_is_not_present(t
->cpu_id
))
1814 t2
= GET_THREAD(thread_base2
, thread_no
, core_no
, pkg_no
);
1816 c
= GET_CORE(core_base
, core_no
, pkg_no
);
1817 c2
= GET_CORE(core_base2
, core_no
, pkg_no
);
1819 p
= GET_PKG(pkg_base
, pkg_no
);
1820 p2
= GET_PKG(pkg_base2
, pkg_no
);
1822 retval
= func(t
, c
, p
, t2
, c2
, p2
);
1832 * run func(cpu) on every cpu in /proc/stat
1833 * return max_cpu number
1835 int for_all_proc_cpus(int (func
)(int))
1841 fp
= fopen_or_die(proc_stat
, "r");
1843 retval
= fscanf(fp
, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1845 err(1, "%s: failed to parse format", proc_stat
);
1848 retval
= fscanf(fp
, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num
);
1852 retval
= func(cpu_num
);
1862 void re_initialize(void)
1865 setup_all_buffers();
1866 printf("turbostat: re-initialized with num_cpus %d\n", topo
.num_cpus
);
1872 * remember the last one seen, it will be the max
1874 int count_cpus(int cpu
)
1876 if (topo
.max_cpu_num
< cpu
)
1877 topo
.max_cpu_num
= cpu
;
1882 int mark_cpu_present(int cpu
)
1884 CPU_SET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
1889 * snapshot_proc_interrupts()
1891 * read and record summary of /proc/interrupts
1893 * return 1 if config change requires a restart, else return 0
1895 int snapshot_proc_interrupts(void)
1901 fp
= fopen_or_die("/proc/interrupts", "r");
1905 /* read 1st line of /proc/interrupts to get cpu* name for each column */
1906 for (column
= 0; column
< topo
.num_cpus
; ++column
) {
1909 retval
= fscanf(fp
, " CPU%d", &cpu_number
);
1913 if (cpu_number
> topo
.max_cpu_num
) {
1914 warn("/proc/interrupts: cpu%d: > %d", cpu_number
, topo
.max_cpu_num
);
1918 irq_column_2_cpu
[column
] = cpu_number
;
1919 irqs_per_cpu
[cpu_number
] = 0;
1922 /* read /proc/interrupt count lines and sum up irqs per cpu */
1927 retval
= fscanf(fp
, " %s:", buf
); /* flush irq# "N:" */
1931 /* read the count per cpu */
1932 for (column
= 0; column
< topo
.num_cpus
; ++column
) {
1934 int cpu_number
, irq_count
;
1936 retval
= fscanf(fp
, " %d", &irq_count
);
1940 cpu_number
= irq_column_2_cpu
[column
];
1941 irqs_per_cpu
[cpu_number
] += irq_count
;
1945 while (getc(fp
) != '\n')
1946 ; /* flush interrupt description */
1952 * snapshot_gfx_rc6_ms()
1954 * record snapshot of
1955 * /sys/class/drm/card0/power/rc6_residency_ms
1957 * return 1 if config change requires a restart, else return 0
1959 int snapshot_gfx_rc6_ms(void)
1964 fp
= fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
1966 retval
= fscanf(fp
, "%lld", &gfx_cur_rc6_ms
);
1975 * snapshot_gfx_mhz()
1977 * record snapshot of
1978 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
1980 * return 1 if config change requires a restart, else return 0
1982 int snapshot_gfx_mhz(void)
1988 fp
= fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
1992 retval
= fscanf(fp
, "%d", &gfx_cur_mhz
);
2000 * snapshot /proc and /sys files
2002 * return 1 if configuration restart needed, else return 0
2004 int snapshot_proc_sysfs_files(void)
2006 if (snapshot_proc_interrupts())
2010 snapshot_gfx_rc6_ms();
2018 void turbostat_loop()
2026 snapshot_proc_sysfs_files();
2027 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
2030 } else if (retval
== -1) {
2031 if (restarted
> 1) {
2038 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
2041 if (for_all_proc_cpus(cpu_is_not_present
)) {
2045 nanosleep(&interval_ts
, NULL
);
2046 if (snapshot_proc_sysfs_files())
2048 retval
= for_all_cpus(get_counters
, ODD_COUNTERS
);
2051 } else if (retval
== -1) {
2055 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
2056 timersub(&tv_odd
, &tv_even
, &tv_delta
);
2057 if (for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
)) {
2061 compute_average(EVEN_COUNTERS
);
2062 format_all_counters(EVEN_COUNTERS
);
2063 flush_output_stdout();
2064 nanosleep(&interval_ts
, NULL
);
2065 if (snapshot_proc_sysfs_files())
2067 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
2070 } else if (retval
== -1) {
2074 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
2075 timersub(&tv_even
, &tv_odd
, &tv_delta
);
2076 if (for_all_cpus_2(delta_cpu
, EVEN_COUNTERS
, ODD_COUNTERS
)) {
2080 compute_average(ODD_COUNTERS
);
2081 format_all_counters(ODD_COUNTERS
);
2082 flush_output_stdout();
2086 void check_dev_msr()
2091 sprintf(pathname
, "/dev/cpu/%d/msr", base_cpu
);
2092 if (stat(pathname
, &sb
))
2093 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2094 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2097 void check_permissions()
2099 struct __user_cap_header_struct cap_header_data
;
2100 cap_user_header_t cap_header
= &cap_header_data
;
2101 struct __user_cap_data_struct cap_data_data
;
2102 cap_user_data_t cap_data
= &cap_data_data
;
2103 extern int capget(cap_user_header_t hdrp
, cap_user_data_t datap
);
2107 /* check for CAP_SYS_RAWIO */
2108 cap_header
->pid
= getpid();
2109 cap_header
->version
= _LINUX_CAPABILITY_VERSION
;
2110 if (capget(cap_header
, cap_data
) < 0)
2111 err(-6, "capget(2) failed");
2113 if ((cap_data
->effective
& (1 << CAP_SYS_RAWIO
)) == 0) {
2115 warnx("capget(CAP_SYS_RAWIO) failed,"
2116 " try \"# setcap cap_sys_rawio=ep %s\"", progname
);
2119 /* test file permissions */
2120 sprintf(pathname
, "/dev/cpu/%d/msr", base_cpu
);
2121 if (euidaccess(pathname
, R_OK
)) {
2123 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2126 /* if all else fails, thell them to be root */
2129 warnx("... or simply run as root");
2136 * NHM adds support for additional MSRs:
2138 * MSR_SMI_COUNT 0x00000034
2140 * MSR_PLATFORM_INFO 0x000000ce
2141 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
2143 * MSR_PKG_C3_RESIDENCY 0x000003f8
2144 * MSR_PKG_C6_RESIDENCY 0x000003f9
2145 * MSR_CORE_C3_RESIDENCY 0x000003fc
2146 * MSR_CORE_C6_RESIDENCY 0x000003fd
2149 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
2151 int probe_nhm_msrs(unsigned int family
, unsigned int model
)
2153 unsigned long long msr
;
2154 unsigned int base_ratio
;
2155 int *pkg_cstate_limits
;
2163 bclk
= discover_bclk(family
, model
);
2166 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2167 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2168 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
2169 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
2170 case 0x2C: /* Westmere EP - Gulftown */
2171 case 0x2E: /* Nehalem-EX Xeon - Beckton */
2172 case 0x2F: /* Westmere-EX Xeon - Eagleton */
2173 pkg_cstate_limits
= nhm_pkg_cstate_limits
;
2175 case 0x2A: /* SNB */
2176 case 0x2D: /* SNB Xeon */
2177 case 0x3A: /* IVB */
2178 case 0x3E: /* IVB Xeon */
2179 pkg_cstate_limits
= snb_pkg_cstate_limits
;
2181 case 0x3C: /* HSW */
2182 case 0x3F: /* HSX */
2183 case 0x45: /* HSW */
2184 case 0x46: /* HSW */
2185 case 0x3D: /* BDW */
2186 case 0x47: /* BDW */
2187 case 0x4F: /* BDX */
2188 case 0x56: /* BDX-DE */
2189 case 0x4E: /* SKL */
2190 case 0x5E: /* SKL */
2191 case 0x8E: /* KBL */
2192 case 0x9E: /* KBL */
2193 case 0x55: /* SKX */
2194 pkg_cstate_limits
= hsw_pkg_cstate_limits
;
2196 case 0x37: /* BYT */
2197 case 0x4D: /* AVN */
2198 pkg_cstate_limits
= slv_pkg_cstate_limits
;
2200 case 0x4C: /* AMT */
2201 pkg_cstate_limits
= amt_pkg_cstate_limits
;
2203 case 0x57: /* PHI */
2204 pkg_cstate_limits
= phi_pkg_cstate_limits
;
2206 case 0x5C: /* BXT */
2207 case 0x5F: /* DNV */
2208 pkg_cstate_limits
= bxt_pkg_cstate_limits
;
2213 get_msr(base_cpu
, MSR_NHM_SNB_PKG_CST_CFG_CTL
, &msr
);
2214 pkg_cstate_limit
= pkg_cstate_limits
[msr
& 0xF];
2216 get_msr(base_cpu
, MSR_PLATFORM_INFO
, &msr
);
2217 base_ratio
= (msr
>> 8) & 0xFF;
2219 base_hz
= base_ratio
* bclk
* 1000000;
2223 int has_nhm_turbo_ratio_limit(unsigned int family
, unsigned int model
)
2226 /* Nehalem compatible, but do not include turbo-ratio limit support */
2227 case 0x2E: /* Nehalem-EX Xeon - Beckton */
2228 case 0x2F: /* Westmere-EX Xeon - Eagleton */
2229 case 0x57: /* PHI - Knights Landing (different MSR definition) */
2235 int has_ivt_turbo_ratio_limit(unsigned int family
, unsigned int model
)
2244 case 0x3E: /* IVB Xeon */
2245 case 0x3F: /* HSW Xeon */
2251 int has_hsw_turbo_ratio_limit(unsigned int family
, unsigned int model
)
2260 case 0x3F: /* HSW Xeon */
2267 int has_knl_turbo_ratio_limit(unsigned int family
, unsigned int model
)
2276 case 0x57: /* Knights Landing */
2282 int has_config_tdp(unsigned int family
, unsigned int model
)
2291 case 0x3A: /* IVB */
2292 case 0x3C: /* HSW */
2293 case 0x3F: /* HSX */
2294 case 0x45: /* HSW */
2295 case 0x46: /* HSW */
2296 case 0x3D: /* BDW */
2297 case 0x47: /* BDW */
2298 case 0x4F: /* BDX */
2299 case 0x56: /* BDX-DE */
2300 case 0x4E: /* SKL */
2301 case 0x5E: /* SKL */
2302 case 0x8E: /* KBL */
2303 case 0x9E: /* KBL */
2304 case 0x55: /* SKX */
2306 case 0x57: /* Knights Landing */
2314 dump_cstate_pstate_config_info(unsigned int family
, unsigned int model
)
2316 if (!do_nhm_platform_info
)
2319 dump_nhm_platform_info();
2321 if (has_hsw_turbo_ratio_limit(family
, model
))
2322 dump_hsw_turbo_ratio_limits();
2324 if (has_ivt_turbo_ratio_limit(family
, model
))
2325 dump_ivt_turbo_ratio_limits();
2327 if (has_nhm_turbo_ratio_limit(family
, model
))
2328 dump_nhm_turbo_ratio_limits();
2330 if (has_knl_turbo_ratio_limit(family
, model
))
2331 dump_knl_turbo_ratio_limits();
2333 if (has_config_tdp(family
, model
))
2342 * Decode the ENERGY_PERF_BIAS MSR
2344 int print_epb(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
2346 unsigned long long msr
;
2355 /* EPB is per-package */
2356 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
2359 if (cpu_migrate(cpu
)) {
2360 fprintf(outf
, "Could not migrate to CPU %d\n", cpu
);
2364 if (get_msr(cpu
, MSR_IA32_ENERGY_PERF_BIAS
, &msr
))
2367 switch (msr
& 0xF) {
2368 case ENERGY_PERF_BIAS_PERFORMANCE
:
2369 epb_string
= "performance";
2371 case ENERGY_PERF_BIAS_NORMAL
:
2372 epb_string
= "balanced";
2374 case ENERGY_PERF_BIAS_POWERSAVE
:
2375 epb_string
= "powersave";
2378 epb_string
= "custom";
2381 fprintf(outf
, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu
, msr
, epb_string
);
2387 * Decode the MSR_HWP_CAPABILITIES
2389 int print_hwp(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
2391 unsigned long long msr
;
2399 /* MSR_HWP_CAPABILITIES is per-package */
2400 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
2403 if (cpu_migrate(cpu
)) {
2404 fprintf(outf
, "Could not migrate to CPU %d\n", cpu
);
2408 if (get_msr(cpu
, MSR_PM_ENABLE
, &msr
))
2411 fprintf(outf
, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
2412 cpu
, msr
, (msr
& (1 << 0)) ? "" : "No-");
2414 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2415 if ((msr
& (1 << 0)) == 0)
2418 if (get_msr(cpu
, MSR_HWP_CAPABILITIES
, &msr
))
2421 fprintf(outf
, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
2422 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2424 (unsigned int)HWP_HIGHEST_PERF(msr
),
2425 (unsigned int)HWP_GUARANTEED_PERF(msr
),
2426 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr
),
2427 (unsigned int)HWP_LOWEST_PERF(msr
));
2429 if (get_msr(cpu
, MSR_HWP_REQUEST
, &msr
))
2432 fprintf(outf
, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
2433 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2435 (unsigned int)(((msr
) >> 0) & 0xff),
2436 (unsigned int)(((msr
) >> 8) & 0xff),
2437 (unsigned int)(((msr
) >> 16) & 0xff),
2438 (unsigned int)(((msr
) >> 24) & 0xff),
2439 (unsigned int)(((msr
) >> 32) & 0xff3),
2440 (unsigned int)(((msr
) >> 42) & 0x1));
2443 if (get_msr(cpu
, MSR_HWP_REQUEST_PKG
, &msr
))
2446 fprintf(outf
, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
2447 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2449 (unsigned int)(((msr
) >> 0) & 0xff),
2450 (unsigned int)(((msr
) >> 8) & 0xff),
2451 (unsigned int)(((msr
) >> 16) & 0xff),
2452 (unsigned int)(((msr
) >> 24) & 0xff),
2453 (unsigned int)(((msr
) >> 32) & 0xff3));
2455 if (has_hwp_notify
) {
2456 if (get_msr(cpu
, MSR_HWP_INTERRUPT
, &msr
))
2459 fprintf(outf
, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
2460 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2462 ((msr
) & 0x1) ? "EN" : "Dis",
2463 ((msr
) & 0x2) ? "EN" : "Dis");
2465 if (get_msr(cpu
, MSR_HWP_STATUS
, &msr
))
2468 fprintf(outf
, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
2469 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2471 ((msr
) & 0x1) ? "" : "No-",
2472 ((msr
) & 0x2) ? "" : "No-");
2478 * print_perf_limit()
2480 int print_perf_limit(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
2482 unsigned long long msr
;
2488 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
2491 if (cpu_migrate(cpu
)) {
2492 fprintf(outf
, "Could not migrate to CPU %d\n", cpu
);
2496 if (do_core_perf_limit_reasons
) {
2497 get_msr(cpu
, MSR_CORE_PERF_LIMIT_REASONS
, &msr
);
2498 fprintf(outf
, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu
, msr
);
2499 fprintf(outf
, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2500 (msr
& 1 << 15) ? "bit15, " : "",
2501 (msr
& 1 << 14) ? "bit14, " : "",
2502 (msr
& 1 << 13) ? "Transitions, " : "",
2503 (msr
& 1 << 12) ? "MultiCoreTurbo, " : "",
2504 (msr
& 1 << 11) ? "PkgPwrL2, " : "",
2505 (msr
& 1 << 10) ? "PkgPwrL1, " : "",
2506 (msr
& 1 << 9) ? "CorePwr, " : "",
2507 (msr
& 1 << 8) ? "Amps, " : "",
2508 (msr
& 1 << 6) ? "VR-Therm, " : "",
2509 (msr
& 1 << 5) ? "Auto-HWP, " : "",
2510 (msr
& 1 << 4) ? "Graphics, " : "",
2511 (msr
& 1 << 2) ? "bit2, " : "",
2512 (msr
& 1 << 1) ? "ThermStatus, " : "",
2513 (msr
& 1 << 0) ? "PROCHOT, " : "");
2514 fprintf(outf
, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2515 (msr
& 1 << 31) ? "bit31, " : "",
2516 (msr
& 1 << 30) ? "bit30, " : "",
2517 (msr
& 1 << 29) ? "Transitions, " : "",
2518 (msr
& 1 << 28) ? "MultiCoreTurbo, " : "",
2519 (msr
& 1 << 27) ? "PkgPwrL2, " : "",
2520 (msr
& 1 << 26) ? "PkgPwrL1, " : "",
2521 (msr
& 1 << 25) ? "CorePwr, " : "",
2522 (msr
& 1 << 24) ? "Amps, " : "",
2523 (msr
& 1 << 22) ? "VR-Therm, " : "",
2524 (msr
& 1 << 21) ? "Auto-HWP, " : "",
2525 (msr
& 1 << 20) ? "Graphics, " : "",
2526 (msr
& 1 << 18) ? "bit18, " : "",
2527 (msr
& 1 << 17) ? "ThermStatus, " : "",
2528 (msr
& 1 << 16) ? "PROCHOT, " : "");
2531 if (do_gfx_perf_limit_reasons
) {
2532 get_msr(cpu
, MSR_GFX_PERF_LIMIT_REASONS
, &msr
);
2533 fprintf(outf
, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu
, msr
);
2534 fprintf(outf
, " (Active: %s%s%s%s%s%s%s%s)",
2535 (msr
& 1 << 0) ? "PROCHOT, " : "",
2536 (msr
& 1 << 1) ? "ThermStatus, " : "",
2537 (msr
& 1 << 4) ? "Graphics, " : "",
2538 (msr
& 1 << 6) ? "VR-Therm, " : "",
2539 (msr
& 1 << 8) ? "Amps, " : "",
2540 (msr
& 1 << 9) ? "GFXPwr, " : "",
2541 (msr
& 1 << 10) ? "PkgPwrL1, " : "",
2542 (msr
& 1 << 11) ? "PkgPwrL2, " : "");
2543 fprintf(outf
, " (Logged: %s%s%s%s%s%s%s%s)\n",
2544 (msr
& 1 << 16) ? "PROCHOT, " : "",
2545 (msr
& 1 << 17) ? "ThermStatus, " : "",
2546 (msr
& 1 << 20) ? "Graphics, " : "",
2547 (msr
& 1 << 22) ? "VR-Therm, " : "",
2548 (msr
& 1 << 24) ? "Amps, " : "",
2549 (msr
& 1 << 25) ? "GFXPwr, " : "",
2550 (msr
& 1 << 26) ? "PkgPwrL1, " : "",
2551 (msr
& 1 << 27) ? "PkgPwrL2, " : "");
2553 if (do_ring_perf_limit_reasons
) {
2554 get_msr(cpu
, MSR_RING_PERF_LIMIT_REASONS
, &msr
);
2555 fprintf(outf
, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu
, msr
);
2556 fprintf(outf
, " (Active: %s%s%s%s%s%s)",
2557 (msr
& 1 << 0) ? "PROCHOT, " : "",
2558 (msr
& 1 << 1) ? "ThermStatus, " : "",
2559 (msr
& 1 << 6) ? "VR-Therm, " : "",
2560 (msr
& 1 << 8) ? "Amps, " : "",
2561 (msr
& 1 << 10) ? "PkgPwrL1, " : "",
2562 (msr
& 1 << 11) ? "PkgPwrL2, " : "");
2563 fprintf(outf
, " (Logged: %s%s%s%s%s%s)\n",
2564 (msr
& 1 << 16) ? "PROCHOT, " : "",
2565 (msr
& 1 << 17) ? "ThermStatus, " : "",
2566 (msr
& 1 << 22) ? "VR-Therm, " : "",
2567 (msr
& 1 << 24) ? "Amps, " : "",
2568 (msr
& 1 << 26) ? "PkgPwrL1, " : "",
2569 (msr
& 1 << 27) ? "PkgPwrL2, " : "");
2574 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2575 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2577 double get_tdp(unsigned int model
)
2579 unsigned long long msr
;
2581 if (do_rapl
& RAPL_PKG_POWER_INFO
)
2582 if (!get_msr(base_cpu
, MSR_PKG_POWER_INFO
, &msr
))
2583 return ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
;
2595 * rapl_dram_energy_units_probe()
2596 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2599 rapl_dram_energy_units_probe(int model
, double rapl_energy_units
)
2601 /* only called for genuine_intel, family 6 */
2604 case 0x3F: /* HSX */
2605 case 0x4F: /* BDX */
2606 case 0x56: /* BDX-DE */
2607 case 0x57: /* KNL */
2608 return (rapl_dram_energy_units
= 15.3 / 1000000);
2610 return (rapl_energy_units
);
2618 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2620 void rapl_probe(unsigned int family
, unsigned int model
)
2622 unsigned long long msr
;
2623 unsigned int time_unit
;
2635 case 0x3C: /* HSW */
2636 case 0x45: /* HSW */
2637 case 0x46: /* HSW */
2638 case 0x3D: /* BDW */
2639 case 0x47: /* BDW */
2640 do_rapl
= RAPL_PKG
| RAPL_CORES
| RAPL_CORE_POLICY
| RAPL_GFX
| RAPL_PKG_POWER_INFO
;
2642 case 0x5C: /* BXT */
2643 do_rapl
= RAPL_PKG
| RAPL_PKG_POWER_INFO
;
2645 case 0x4E: /* SKL */
2646 case 0x5E: /* SKL */
2647 case 0x8E: /* KBL */
2648 case 0x9E: /* KBL */
2649 do_rapl
= RAPL_PKG
| RAPL_DRAM
| RAPL_DRAM_PERF_STATUS
| RAPL_PKG_PERF_STATUS
| RAPL_PKG_POWER_INFO
;
2651 case 0x3F: /* HSX */
2652 case 0x4F: /* BDX */
2653 case 0x56: /* BDX-DE */
2654 case 0x55: /* SKX */
2655 case 0x57: /* KNL */
2656 do_rapl
= RAPL_PKG
| RAPL_DRAM
| RAPL_DRAM_POWER_INFO
| RAPL_DRAM_PERF_STATUS
| RAPL_PKG_PERF_STATUS
| RAPL_PKG_POWER_INFO
;
2660 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
;
2662 case 0x37: /* BYT */
2663 case 0x4D: /* AVN */
2664 do_rapl
= RAPL_PKG
| RAPL_CORES
;
2666 case 0x5f: /* DNV */
2667 do_rapl
= RAPL_PKG
| RAPL_DRAM
| RAPL_DRAM_POWER_INFO
| RAPL_DRAM_PERF_STATUS
| RAPL_PKG_PERF_STATUS
| RAPL_PKG_POWER_INFO
| RAPL_CORES_ENERGY_STATUS
;
2673 /* units on package 0, verify later other packages match */
2674 if (get_msr(base_cpu
, MSR_RAPL_POWER_UNIT
, &msr
))
2677 rapl_power_units
= 1.0 / (1 << (msr
& 0xF));
2679 rapl_energy_units
= 1.0 * (1 << (msr
>> 8 & 0x1F)) / 1000000;
2681 rapl_energy_units
= 1.0 / (1 << (msr
>> 8 & 0x1F));
2683 rapl_dram_energy_units
= rapl_dram_energy_units_probe(model
, rapl_energy_units
);
2685 time_unit
= msr
>> 16 & 0xF;
2689 rapl_time_units
= 1.0 / (1 << (time_unit
));
2691 tdp
= get_tdp(model
);
2693 rapl_joule_counter_range
= 0xFFFFFFFF * rapl_energy_units
/ tdp
;
2695 fprintf(outf
, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range
, tdp
);
2700 void perf_limit_reasons_probe(unsigned int family
, unsigned int model
)
2709 case 0x3C: /* HSW */
2710 case 0x45: /* HSW */
2711 case 0x46: /* HSW */
2712 do_gfx_perf_limit_reasons
= 1;
2713 case 0x3F: /* HSX */
2714 do_core_perf_limit_reasons
= 1;
2715 do_ring_perf_limit_reasons
= 1;
2721 int print_thermal(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
2723 unsigned long long msr
;
2727 if (!(do_dts
|| do_ptm
))
2732 /* DTS is per-core, no need to print for each thread */
2733 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
2736 if (cpu_migrate(cpu
)) {
2737 fprintf(outf
, "Could not migrate to CPU %d\n", cpu
);
2741 if (do_ptm
&& (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)) {
2742 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
2745 dts
= (msr
>> 16) & 0x7F;
2746 fprintf(outf
, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2747 cpu
, msr
, tcc_activation_temp
- dts
);
2750 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_INTERRUPT
, &msr
))
2753 dts
= (msr
>> 16) & 0x7F;
2754 dts2
= (msr
>> 8) & 0x7F;
2755 fprintf(outf
, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2756 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
2762 unsigned int resolution
;
2764 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
2767 dts
= (msr
>> 16) & 0x7F;
2768 resolution
= (msr
>> 27) & 0xF;
2769 fprintf(outf
, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2770 cpu
, msr
, tcc_activation_temp
- dts
, resolution
);
2773 if (get_msr(cpu
, MSR_IA32_THERM_INTERRUPT
, &msr
))
2776 dts
= (msr
>> 16) & 0x7F;
2777 dts2
= (msr
>> 8) & 0x7F;
2778 fprintf(outf
, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2779 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
2786 void print_power_limit_msr(int cpu
, unsigned long long msr
, char *label
)
2788 fprintf(outf
, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2790 ((msr
>> 15) & 1) ? "EN" : "DIS",
2791 ((msr
>> 0) & 0x7FFF) * rapl_power_units
,
2792 (1.0 + (((msr
>> 22) & 0x3)/4.0)) * (1 << ((msr
>> 17) & 0x1F)) * rapl_time_units
,
2793 (((msr
>> 16) & 1) ? "EN" : "DIS"));
2798 int print_rapl(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
2800 unsigned long long msr
;
2806 /* RAPL counters are per package, so print only for 1st thread/package */
2807 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
2811 if (cpu_migrate(cpu
)) {
2812 fprintf(outf
, "Could not migrate to CPU %d\n", cpu
);
2816 if (get_msr(cpu
, MSR_RAPL_POWER_UNIT
, &msr
))
2820 fprintf(outf
, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2821 "(%f Watts, %f Joules, %f sec.)\n", cpu
, msr
,
2822 rapl_power_units
, rapl_energy_units
, rapl_time_units
);
2824 if (do_rapl
& RAPL_PKG_POWER_INFO
) {
2826 if (get_msr(cpu
, MSR_PKG_POWER_INFO
, &msr
))
2830 fprintf(outf
, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2832 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2833 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2834 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2835 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
2838 if (do_rapl
& RAPL_PKG
) {
2840 if (get_msr(cpu
, MSR_PKG_POWER_LIMIT
, &msr
))
2843 fprintf(outf
, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2844 cpu
, msr
, (msr
>> 63) & 1 ? "": "UN");
2846 print_power_limit_msr(cpu
, msr
, "PKG Limit #1");
2847 fprintf(outf
, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2849 ((msr
>> 47) & 1) ? "EN" : "DIS",
2850 ((msr
>> 32) & 0x7FFF) * rapl_power_units
,
2851 (1.0 + (((msr
>> 54) & 0x3)/4.0)) * (1 << ((msr
>> 49) & 0x1F)) * rapl_time_units
,
2852 ((msr
>> 48) & 1) ? "EN" : "DIS");
2855 if (do_rapl
& RAPL_DRAM_POWER_INFO
) {
2856 if (get_msr(cpu
, MSR_DRAM_POWER_INFO
, &msr
))
2859 fprintf(outf
, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2861 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2862 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2863 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2864 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
2866 if (do_rapl
& RAPL_DRAM
) {
2867 if (get_msr(cpu
, MSR_DRAM_POWER_LIMIT
, &msr
))
2869 fprintf(outf
, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2870 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
2872 print_power_limit_msr(cpu
, msr
, "DRAM Limit");
2874 if (do_rapl
& RAPL_CORE_POLICY
) {
2876 if (get_msr(cpu
, MSR_PP0_POLICY
, &msr
))
2879 fprintf(outf
, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu
, msr
& 0xF);
2882 if (do_rapl
& RAPL_CORES_POWER_LIMIT
) {
2884 if (get_msr(cpu
, MSR_PP0_POWER_LIMIT
, &msr
))
2886 fprintf(outf
, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2887 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
2888 print_power_limit_msr(cpu
, msr
, "Cores Limit");
2891 if (do_rapl
& RAPL_GFX
) {
2893 if (get_msr(cpu
, MSR_PP1_POLICY
, &msr
))
2896 fprintf(outf
, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu
, msr
& 0xF);
2898 if (get_msr(cpu
, MSR_PP1_POWER_LIMIT
, &msr
))
2900 fprintf(outf
, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2901 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
2902 print_power_limit_msr(cpu
, msr
, "GFX Limit");
2909 * SNB adds support for additional MSRs:
2911 * MSR_PKG_C7_RESIDENCY 0x000003fa
2912 * MSR_CORE_C7_RESIDENCY 0x000003fe
2913 * MSR_PKG_C2_RESIDENCY 0x0000060d
2916 int has_snb_msrs(unsigned int family
, unsigned int model
)
2924 case 0x3A: /* IVB */
2925 case 0x3E: /* IVB Xeon */
2926 case 0x3C: /* HSW */
2927 case 0x3F: /* HSW */
2928 case 0x45: /* HSW */
2929 case 0x46: /* HSW */
2930 case 0x3D: /* BDW */
2931 case 0x47: /* BDW */
2932 case 0x4F: /* BDX */
2933 case 0x56: /* BDX-DE */
2934 case 0x4E: /* SKL */
2935 case 0x5E: /* SKL */
2936 case 0x8E: /* KBL */
2937 case 0x9E: /* KBL */
2938 case 0x55: /* SKX */
2939 case 0x5C: /* BXT */
2946 * HSW adds support for additional MSRs:
2948 * MSR_PKG_C8_RESIDENCY 0x00000630
2949 * MSR_PKG_C9_RESIDENCY 0x00000631
2950 * MSR_PKG_C10_RESIDENCY 0x00000632
2952 * MSR_PKGC8_IRTL 0x00000633
2953 * MSR_PKGC9_IRTL 0x00000634
2954 * MSR_PKGC10_IRTL 0x00000635
2957 int has_hsw_msrs(unsigned int family
, unsigned int model
)
2963 case 0x45: /* HSW */
2964 case 0x3D: /* BDW */
2965 case 0x4E: /* SKL */
2966 case 0x5E: /* SKL */
2967 case 0x8E: /* KBL */
2968 case 0x9E: /* KBL */
2969 case 0x5C: /* BXT */
2976 * SKL adds support for additional MSRS:
2978 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2979 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2980 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2981 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2983 int has_skl_msrs(unsigned int family
, unsigned int model
)
2989 case 0x4E: /* SKL */
2990 case 0x5E: /* SKL */
2991 case 0x8E: /* KBL */
2992 case 0x9E: /* KBL */
3000 int is_slm(unsigned int family
, unsigned int model
)
3005 case 0x37: /* BYT */
3006 case 0x4D: /* AVN */
3012 int is_knl(unsigned int family
, unsigned int model
)
3017 case 0x57: /* KNL */
3023 unsigned int get_aperf_mperf_multiplier(unsigned int family
, unsigned int model
)
3025 if (is_knl(family
, model
))
3030 #define SLM_BCLK_FREQS 5
3031 double slm_freq_table
[SLM_BCLK_FREQS
] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3033 double slm_bclk(void)
3035 unsigned long long msr
= 3;
3039 if (get_msr(base_cpu
, MSR_FSB_FREQ
, &msr
))
3040 fprintf(outf
, "SLM BCLK: unknown\n");
3043 if (i
>= SLM_BCLK_FREQS
) {
3044 fprintf(outf
, "SLM BCLK[%d] invalid\n", i
);
3047 freq
= slm_freq_table
[i
];
3049 fprintf(outf
, "SLM BCLK: %.1f Mhz\n", freq
);
3054 double discover_bclk(unsigned int family
, unsigned int model
)
3056 if (has_snb_msrs(family
, model
) || is_knl(family
, model
))
3058 else if (is_slm(family
, model
))
3065 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3066 * the Thermal Control Circuit (TCC) activates.
3067 * This is usually equal to tjMax.
3069 * Older processors do not have this MSR, so there we guess,
3070 * but also allow cmdline over-ride with -T.
3072 * Several MSR temperature values are in units of degrees-C
3073 * below this value, including the Digital Thermal Sensor (DTS),
3074 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3076 int set_temperature_target(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
3078 unsigned long long msr
;
3079 unsigned int target_c_local
;
3082 /* tcc_activation_temp is used only for dts or ptm */
3083 if (!(do_dts
|| do_ptm
))
3086 /* this is a per-package concept */
3087 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
3091 if (cpu_migrate(cpu
)) {
3092 fprintf(outf
, "Could not migrate to CPU %d\n", cpu
);
3096 if (tcc_activation_temp_override
!= 0) {
3097 tcc_activation_temp
= tcc_activation_temp_override
;
3098 fprintf(outf
, "cpu%d: Using cmdline TCC Target (%d C)\n",
3099 cpu
, tcc_activation_temp
);
3103 /* Temperature Target MSR is Nehalem and newer only */
3104 if (!do_nhm_platform_info
)
3107 if (get_msr(base_cpu
, MSR_IA32_TEMPERATURE_TARGET
, &msr
))
3110 target_c_local
= (msr
>> 16) & 0xFF;
3113 fprintf(outf
, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3114 cpu
, msr
, target_c_local
);
3116 if (!target_c_local
)
3119 tcc_activation_temp
= target_c_local
;
3124 tcc_activation_temp
= TJMAX_DEFAULT
;
3125 fprintf(outf
, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3126 cpu
, tcc_activation_temp
);
3131 void decode_feature_control_msr(void)
3133 unsigned long long msr
;
3135 if (!get_msr(base_cpu
, MSR_IA32_FEATURE_CONTROL
, &msr
))
3136 fprintf(outf
, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3138 msr
& FEATURE_CONTROL_LOCKED
? "" : "UN-",
3139 msr
& (1 << 18) ? "SGX" : "");
3142 void decode_misc_enable_msr(void)
3144 unsigned long long msr
;
3146 if (!get_msr(base_cpu
, MSR_IA32_MISC_ENABLE
, &msr
))
3147 fprintf(outf
, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
3149 msr
& (1 << 3) ? "TCC" : "",
3150 msr
& (1 << 16) ? "EIST" : "",
3151 msr
& (1 << 18) ? "MONITOR" : "");
3155 * Decode MSR_MISC_PWR_MGMT
3157 * Decode the bits according to the Nehalem documentation
3158 * bit[0] seems to continue to have same meaning going forward
3161 void decode_misc_pwr_mgmt_msr(void)
3163 unsigned long long msr
;
3165 if (!do_nhm_platform_info
)
3168 if (!get_msr(base_cpu
, MSR_MISC_PWR_MGMT
, &msr
))
3169 fprintf(outf
, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n",
3171 msr
& (1 << 0) ? "DIS" : "EN",
3172 msr
& (1 << 1) ? "EN" : "DIS");
3175 void process_cpuid()
3177 unsigned int eax
, ebx
, ecx
, edx
, max_level
, max_extended_level
;
3178 unsigned int fms
, family
, model
, stepping
;
3180 eax
= ebx
= ecx
= edx
= 0;
3182 __cpuid(0, max_level
, ebx
, ecx
, edx
);
3184 if (ebx
== 0x756e6547 && edx
== 0x49656e69 && ecx
== 0x6c65746e)
3188 fprintf(outf
, "CPUID(0): %.4s%.4s%.4s ",
3189 (char *)&ebx
, (char *)&edx
, (char *)&ecx
);
3191 __cpuid(1, fms
, ebx
, ecx
, edx
);
3192 family
= (fms
>> 8) & 0xf;
3193 model
= (fms
>> 4) & 0xf;
3194 stepping
= fms
& 0xf;
3195 if (family
== 6 || family
== 0xf)
3196 model
+= ((fms
>> 16) & 0xf) << 4;
3199 fprintf(outf
, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
3200 max_level
, family
, model
, stepping
, family
, model
, stepping
);
3201 fprintf(outf
, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
3202 ecx
& (1 << 0) ? "SSE3" : "-",
3203 ecx
& (1 << 3) ? "MONITOR" : "-",
3204 ecx
& (1 << 6) ? "SMX" : "-",
3205 ecx
& (1 << 7) ? "EIST" : "-",
3206 ecx
& (1 << 8) ? "TM2" : "-",
3207 edx
& (1 << 4) ? "TSC" : "-",
3208 edx
& (1 << 5) ? "MSR" : "-",
3209 edx
& (1 << 22) ? "ACPI-TM" : "-",
3210 edx
& (1 << 29) ? "TM" : "-");
3213 if (!(edx
& (1 << 5)))
3214 errx(1, "CPUID: no MSR");
3217 * check max extended function levels of CPUID.
3218 * This is needed to check for invariant TSC.
3219 * This check is valid for both Intel and AMD.
3221 ebx
= ecx
= edx
= 0;
3222 __cpuid(0x80000000, max_extended_level
, ebx
, ecx
, edx
);
3224 if (max_extended_level
>= 0x80000007) {
3227 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3228 * this check is valid for both Intel and AMD
3230 __cpuid(0x80000007, eax
, ebx
, ecx
, edx
);
3231 has_invariant_tsc
= edx
& (1 << 8);
3235 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3236 * this check is valid for both Intel and AMD
3239 __cpuid(0x6, eax
, ebx
, ecx
, edx
);
3240 has_aperf
= ecx
& (1 << 0);
3241 do_dts
= eax
& (1 << 0);
3242 do_ptm
= eax
& (1 << 6);
3243 has_hwp
= eax
& (1 << 7);
3244 has_hwp_notify
= eax
& (1 << 8);
3245 has_hwp_activity_window
= eax
& (1 << 9);
3246 has_hwp_epp
= eax
& (1 << 10);
3247 has_hwp_pkg
= eax
& (1 << 11);
3248 has_epb
= ecx
& (1 << 3);
3251 fprintf(outf
, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
3252 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3253 has_aperf
? "" : "No-",
3254 do_dts
? "" : "No-",
3255 do_ptm
? "" : "No-",
3256 has_hwp
? "" : "No-",
3257 has_hwp_notify
? "" : "No-",
3258 has_hwp_activity_window
? "" : "No-",
3259 has_hwp_epp
? "" : "No-",
3260 has_hwp_pkg
? "" : "No-",
3261 has_epb
? "" : "No-");
3264 decode_misc_enable_msr();
3266 if (max_level
>= 0x7 && debug
) {
3271 __cpuid_count(0x7, 0, eax
, ebx
, ecx
, edx
);
3273 has_sgx
= ebx
& (1 << 2);
3274 fprintf(outf
, "CPUID(7): %sSGX\n", has_sgx
? "" : "No-");
3277 decode_feature_control_msr();
3280 if (max_level
>= 0x15) {
3281 unsigned int eax_crystal
;
3282 unsigned int ebx_tsc
;
3285 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3287 eax_crystal
= ebx_tsc
= crystal_hz
= edx
= 0;
3288 __cpuid(0x15, eax_crystal
, ebx_tsc
, crystal_hz
, edx
);
3292 if (debug
&& (ebx
!= 0))
3293 fprintf(outf
, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
3294 eax_crystal
, ebx_tsc
, crystal_hz
);
3296 if (crystal_hz
== 0)
3298 case 0x4E: /* SKL */
3299 case 0x5E: /* SKL */
3300 case 0x8E: /* KBL */
3301 case 0x9E: /* KBL */
3302 crystal_hz
= 24000000; /* 24.0 MHz */
3304 case 0x55: /* SKX */
3305 crystal_hz
= 25000000; /* 25.0 MHz */
3307 case 0x5C: /* BXT */
3308 case 0x5F: /* DNV */
3309 crystal_hz
= 19200000; /* 19.2 MHz */
3316 tsc_hz
= (unsigned long long) crystal_hz
* ebx_tsc
/ eax_crystal
;
3318 fprintf(outf
, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
3319 tsc_hz
/ 1000000, crystal_hz
, ebx_tsc
, eax_crystal
);
3323 if (max_level
>= 0x16) {
3324 unsigned int base_mhz
, max_mhz
, bus_mhz
, edx
;
3327 * CPUID 16H Base MHz, Max MHz, Bus MHz
3329 base_mhz
= max_mhz
= bus_mhz
= edx
= 0;
3331 __cpuid(0x16, base_mhz
, max_mhz
, bus_mhz
, edx
);
3333 fprintf(outf
, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
3334 base_mhz
, max_mhz
, bus_mhz
);
3338 aperf_mperf_multiplier
= get_aperf_mperf_multiplier(family
, model
);
3340 do_nhm_platform_info
= do_nhm_cstates
= do_smi
= probe_nhm_msrs(family
, model
);
3341 do_snb_cstates
= has_snb_msrs(family
, model
);
3342 do_irtl_snb
= has_snb_msrs(family
, model
);
3343 do_pc2
= do_snb_cstates
&& (pkg_cstate_limit
>= PCL__2
);
3344 do_pc3
= (pkg_cstate_limit
>= PCL__3
);
3345 do_pc6
= (pkg_cstate_limit
>= PCL__6
);
3346 do_pc7
= do_snb_cstates
&& (pkg_cstate_limit
>= PCL__7
);
3347 do_c8_c9_c10
= has_hsw_msrs(family
, model
);
3348 do_irtl_hsw
= has_hsw_msrs(family
, model
);
3349 do_skl_residency
= has_skl_msrs(family
, model
);
3350 do_slm_cstates
= is_slm(family
, model
);
3351 do_knl_cstates
= is_knl(family
, model
);
3354 decode_misc_pwr_mgmt_msr();
3356 rapl_probe(family
, model
);
3357 perf_limit_reasons_probe(family
, model
);
3360 dump_cstate_pstate_config_info(family
, model
);
3362 if (has_skl_msrs(family
, model
))
3363 calculate_tsc_tweak();
3365 do_gfx_rc6_ms
= !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK
);
3367 do_gfx_mhz
= !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK
);
3375 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3377 "Turbostat forks the specified COMMAND and prints statistics\n"
3378 "when COMMAND completes.\n"
3379 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3380 "to print statistics, until interrupted.\n"
3381 "--debug run in \"debug\" mode\n"
3382 "--interval sec Override default 5-second measurement interval\n"
3383 "--help print this help message\n"
3384 "--counter msr print 32-bit counter at address \"msr\"\n"
3385 "--Counter msr print 64-bit Counter at address \"msr\"\n"
3386 "--out file create or truncate \"file\" for all output\n"
3387 "--msr msr print 32-bit value at address \"msr\"\n"
3388 "--MSR msr print 64-bit Value at address \"msr\"\n"
3389 "--version print version information\n"
3391 "For more help, run \"man turbostat\"\n");
3396 * in /dev/cpu/ return success for names that are numbers
3397 * ie. filter out ".", "..", "microcode".
3399 int dir_filter(const struct dirent
*dirp
)
3401 if (isdigit(dirp
->d_name
[0]))
3407 int open_dev_cpu_msr(int dummy1
)
3412 void topology_probe()
3415 int max_core_id
= 0;
3416 int max_package_id
= 0;
3417 int max_siblings
= 0;
3418 struct cpu_topology
{
3420 int physical_package_id
;
3423 /* Initialize num_cpus, max_cpu_num */
3425 topo
.max_cpu_num
= 0;
3426 for_all_proc_cpus(count_cpus
);
3427 if (!summary_only
&& topo
.num_cpus
> 1)
3431 fprintf(outf
, "num_cpus %d max_cpu_num %d\n", topo
.num_cpus
, topo
.max_cpu_num
);
3433 cpus
= calloc(1, (topo
.max_cpu_num
+ 1) * sizeof(struct cpu_topology
));
3435 err(1, "calloc cpus");
3438 * Allocate and initialize cpu_present_set
3440 cpu_present_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
3441 if (cpu_present_set
== NULL
)
3442 err(3, "CPU_ALLOC");
3443 cpu_present_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
3444 CPU_ZERO_S(cpu_present_setsize
, cpu_present_set
);
3445 for_all_proc_cpus(mark_cpu_present
);
3448 * Allocate and initialize cpu_affinity_set
3450 cpu_affinity_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
3451 if (cpu_affinity_set
== NULL
)
3452 err(3, "CPU_ALLOC");
3453 cpu_affinity_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
3454 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
3459 * find max_core_id, max_package_id
3461 for (i
= 0; i
<= topo
.max_cpu_num
; ++i
) {
3464 if (cpu_is_not_present(i
)) {
3466 fprintf(outf
, "cpu%d NOT PRESENT\n", i
);
3469 cpus
[i
].core_id
= get_core_id(i
);
3470 if (cpus
[i
].core_id
> max_core_id
)
3471 max_core_id
= cpus
[i
].core_id
;
3473 cpus
[i
].physical_package_id
= get_physical_package_id(i
);
3474 if (cpus
[i
].physical_package_id
> max_package_id
)
3475 max_package_id
= cpus
[i
].physical_package_id
;
3477 siblings
= get_num_ht_siblings(i
);
3478 if (siblings
> max_siblings
)
3479 max_siblings
= siblings
;
3481 fprintf(outf
, "cpu %d pkg %d core %d\n",
3482 i
, cpus
[i
].physical_package_id
, cpus
[i
].core_id
);
3484 topo
.num_cores_per_pkg
= max_core_id
+ 1;
3486 fprintf(outf
, "max_core_id %d, sizing for %d cores per package\n",
3487 max_core_id
, topo
.num_cores_per_pkg
);
3488 if (debug
&& !summary_only
&& topo
.num_cores_per_pkg
> 1)
3491 topo
.num_packages
= max_package_id
+ 1;
3493 fprintf(outf
, "max_package_id %d, sizing for %d packages\n",
3494 max_package_id
, topo
.num_packages
);
3495 if (debug
&& !summary_only
&& topo
.num_packages
> 1)
3498 topo
.num_threads_per_core
= max_siblings
;
3500 fprintf(outf
, "max_siblings %d\n", max_siblings
);
3506 allocate_counters(struct thread_data
**t
, struct core_data
**c
, struct pkg_data
**p
)
3510 *t
= calloc(topo
.num_threads_per_core
* topo
.num_cores_per_pkg
*
3511 topo
.num_packages
, sizeof(struct thread_data
));
3515 for (i
= 0; i
< topo
.num_threads_per_core
*
3516 topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
3517 (*t
)[i
].cpu_id
= -1;
3519 *c
= calloc(topo
.num_cores_per_pkg
* topo
.num_packages
,
3520 sizeof(struct core_data
));
3524 for (i
= 0; i
< topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
3525 (*c
)[i
].core_id
= -1;
3527 *p
= calloc(topo
.num_packages
, sizeof(struct pkg_data
));
3531 for (i
= 0; i
< topo
.num_packages
; i
++)
3532 (*p
)[i
].package_id
= i
;
3536 err(1, "calloc counters");
3541 * set cpu_id, core_num, pkg_num
3542 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3544 * increment topo.num_cores when 1st core in pkg seen
3546 void init_counter(struct thread_data
*thread_base
, struct core_data
*core_base
,
3547 struct pkg_data
*pkg_base
, int thread_num
, int core_num
,
3548 int pkg_num
, int cpu_id
)
3550 struct thread_data
*t
;
3551 struct core_data
*c
;
3554 t
= GET_THREAD(thread_base
, thread_num
, core_num
, pkg_num
);
3555 c
= GET_CORE(core_base
, core_num
, pkg_num
);
3556 p
= GET_PKG(pkg_base
, pkg_num
);
3559 if (thread_num
== 0) {
3560 t
->flags
|= CPU_IS_FIRST_THREAD_IN_CORE
;
3561 if (cpu_is_first_core_in_package(cpu_id
))
3562 t
->flags
|= CPU_IS_FIRST_CORE_IN_PACKAGE
;
3565 c
->core_id
= core_num
;
3566 p
->package_id
= pkg_num
;
3570 int initialize_counters(int cpu_id
)
3572 int my_thread_id
, my_core_id
, my_package_id
;
3574 my_package_id
= get_physical_package_id(cpu_id
);
3575 my_core_id
= get_core_id(cpu_id
);
3576 my_thread_id
= get_cpu_position_in_core(cpu_id
);
3580 init_counter(EVEN_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
3581 init_counter(ODD_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
3585 void allocate_output_buffer()
3587 output_buffer
= calloc(1, (1 + topo
.num_cpus
) * 1024);
3588 outp
= output_buffer
;
3590 err(-1, "calloc output buffer");
3592 void allocate_fd_percpu(void)
3594 fd_percpu
= calloc(topo
.max_cpu_num
+ 1, sizeof(int));
3595 if (fd_percpu
== NULL
)
3596 err(-1, "calloc fd_percpu");
3598 void allocate_irq_buffers(void)
3600 irq_column_2_cpu
= calloc(topo
.num_cpus
, sizeof(int));
3601 if (irq_column_2_cpu
== NULL
)
3602 err(-1, "calloc %d", topo
.num_cpus
);
3604 irqs_per_cpu
= calloc(topo
.max_cpu_num
+ 1, sizeof(int));
3605 if (irqs_per_cpu
== NULL
)
3606 err(-1, "calloc %d", topo
.max_cpu_num
+ 1);
3608 void setup_all_buffers(void)
3611 allocate_irq_buffers();
3612 allocate_fd_percpu();
3613 allocate_counters(&thread_even
, &core_even
, &package_even
);
3614 allocate_counters(&thread_odd
, &core_odd
, &package_odd
);
3615 allocate_output_buffer();
3616 for_all_proc_cpus(initialize_counters
);
3619 void set_base_cpu(void)
3621 base_cpu
= sched_getcpu();
3623 err(-ENODEV
, "No valid cpus found");
3626 fprintf(outf
, "base_cpu = %d\n", base_cpu
);
3629 void turbostat_init()
3631 setup_all_buffers();
3634 check_permissions();
3639 for_all_cpus(print_hwp
, ODD_COUNTERS
);
3642 for_all_cpus(print_epb
, ODD_COUNTERS
);
3645 for_all_cpus(print_perf_limit
, ODD_COUNTERS
);
3648 for_all_cpus(print_rapl
, ODD_COUNTERS
);
3650 for_all_cpus(set_temperature_target
, ODD_COUNTERS
);
3653 for_all_cpus(print_thermal
, ODD_COUNTERS
);
3655 if (debug
&& do_irtl_snb
)
3659 int fork_it(char **argv
)
3664 status
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
3667 /* clear affinity side-effect of get_counters() */
3668 sched_setaffinity(0, cpu_present_setsize
, cpu_present_set
);
3669 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
3674 execvp(argv
[0], argv
);
3678 if (child_pid
== -1)
3681 signal(SIGINT
, SIG_IGN
);
3682 signal(SIGQUIT
, SIG_IGN
);
3683 if (waitpid(child_pid
, &status
, 0) == -1)
3684 err(status
, "waitpid");
3687 * n.b. fork_it() does not check for errors from for_all_cpus()
3688 * because re-starting is problematic when forking
3690 for_all_cpus(get_counters
, ODD_COUNTERS
);
3691 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
3692 timersub(&tv_odd
, &tv_even
, &tv_delta
);
3693 if (for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
))
3694 fprintf(outf
, "%s: Counter reset detected\n", progname
);
3696 compute_average(EVEN_COUNTERS
);
3697 format_all_counters(EVEN_COUNTERS
);
3700 fprintf(outf
, "%.6f sec\n", tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0);
3702 flush_output_stderr();
3707 int get_and_dump_counters(void)
3711 status
= for_all_cpus(get_counters
, ODD_COUNTERS
);
3715 status
= for_all_cpus(dump_counters
, ODD_COUNTERS
);
3719 flush_output_stdout();
3724 void print_version() {
3725 fprintf(outf
, "turbostat version 4.14 22 Apr 2016"
3726 " - Len Brown <lenb@kernel.org>\n");
3729 void cmdline(int argc
, char **argv
)
3732 int option_index
= 0;
3733 static struct option long_options
[] = {
3734 {"Counter", required_argument
, 0, 'C'},
3735 {"counter", required_argument
, 0, 'c'},
3736 {"Dump", no_argument
, 0, 'D'},
3737 {"debug", no_argument
, 0, 'd'},
3738 {"interval", required_argument
, 0, 'i'},
3739 {"help", no_argument
, 0, 'h'},
3740 {"Joules", no_argument
, 0, 'J'},
3741 {"MSR", required_argument
, 0, 'M'},
3742 {"msr", required_argument
, 0, 'm'},
3743 {"out", required_argument
, 0, 'o'},
3744 {"Package", no_argument
, 0, 'p'},
3745 {"processor", no_argument
, 0, 'p'},
3746 {"Summary", no_argument
, 0, 'S'},
3747 {"TCC", required_argument
, 0, 'T'},
3748 {"version", no_argument
, 0, 'v' },
3754 while ((opt
= getopt_long_only(argc
, argv
, "+C:c:Ddhi:JM:m:o:PpST:v",
3755 long_options
, &option_index
)) != -1) {
3758 sscanf(optarg
, "%x", &extra_delta_offset64
);
3761 sscanf(optarg
, "%x", &extra_delta_offset32
);
3775 double interval
= strtod(optarg
, NULL
);
3777 if (interval
< 0.001) {
3778 fprintf(outf
, "interval %f seconds is too small\n",
3783 interval_ts
.tv_sec
= interval
;
3784 interval_ts
.tv_nsec
= (interval
- interval_ts
.tv_sec
) * 1000000000;
3791 sscanf(optarg
, "%x", &extra_msr_offset64
);
3794 sscanf(optarg
, "%x", &extra_msr_offset32
);
3797 outf
= fopen_or_die(optarg
, "w");
3809 tcc_activation_temp_override
= atoi(optarg
);
3819 int main(int argc
, char **argv
)
3823 cmdline(argc
, argv
);
3830 /* dump counters and exit */
3832 return get_and_dump_counters();
3835 * if any params left, it must be a command to fork
3838 return fork_it(argv
+ optind
);