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>
42 #include <linux/capability.h>
45 char *proc_stat
= "/proc/stat";
46 unsigned int interval_sec
= 5;
48 unsigned int rapl_joules
;
49 unsigned int summary_only
;
50 unsigned int dump_only
;
53 unsigned int do_nhm_cstates
;
54 unsigned int do_snb_cstates
;
59 unsigned int do_c8_c9_c10
;
60 unsigned int do_slm_cstates
;
61 unsigned int use_c1_residency_msr
;
62 unsigned int has_aperf
;
64 unsigned int units
= 1000000; /* MHz etc */
65 unsigned int genuine_intel
;
66 unsigned int has_invariant_tsc
;
67 unsigned int do_nhm_platform_info
;
68 unsigned int do_nhm_turbo_ratio_limit
;
69 unsigned int do_ivt_turbo_ratio_limit
;
70 unsigned int extra_msr_offset32
;
71 unsigned int extra_msr_offset64
;
72 unsigned int extra_delta_offset32
;
73 unsigned int extra_delta_offset64
;
76 unsigned int show_pkg
;
77 unsigned int show_core
;
78 unsigned int show_cpu
;
79 unsigned int show_pkg_only
;
80 unsigned int show_core_only
;
81 char *output_buffer
, *outp
;
85 unsigned int tcc_activation_temp
;
86 unsigned int tcc_activation_temp_override
;
87 double rapl_power_units
, rapl_energy_units
, rapl_time_units
;
88 double rapl_joule_counter_range
;
89 unsigned int do_core_perf_limit_reasons
;
90 unsigned int do_gfx_perf_limit_reasons
;
91 unsigned int do_ring_perf_limit_reasons
;
93 #define RAPL_PKG (1 << 0)
94 /* 0x610 MSR_PKG_POWER_LIMIT */
95 /* 0x611 MSR_PKG_ENERGY_STATUS */
96 #define RAPL_PKG_PERF_STATUS (1 << 1)
97 /* 0x613 MSR_PKG_PERF_STATUS */
98 #define RAPL_PKG_POWER_INFO (1 << 2)
99 /* 0x614 MSR_PKG_POWER_INFO */
101 #define RAPL_DRAM (1 << 3)
102 /* 0x618 MSR_DRAM_POWER_LIMIT */
103 /* 0x619 MSR_DRAM_ENERGY_STATUS */
104 /* 0x61c MSR_DRAM_POWER_INFO */
105 #define RAPL_DRAM_PERF_STATUS (1 << 4)
106 /* 0x61b MSR_DRAM_PERF_STATUS */
108 #define RAPL_CORES (1 << 5)
109 /* 0x638 MSR_PP0_POWER_LIMIT */
110 /* 0x639 MSR_PP0_ENERGY_STATUS */
111 #define RAPL_CORE_POLICY (1 << 6)
112 /* 0x63a MSR_PP0_POLICY */
115 #define RAPL_GFX (1 << 7)
116 /* 0x640 MSR_PP1_POWER_LIMIT */
117 /* 0x641 MSR_PP1_ENERGY_STATUS */
118 /* 0x642 MSR_PP1_POLICY */
119 #define TJMAX_DEFAULT 100
121 #define MAX(a, b) ((a) > (b) ? (a) : (b))
123 int aperf_mperf_unstable
;
127 cpu_set_t
*cpu_present_set
, *cpu_affinity_set
;
128 size_t cpu_present_setsize
, cpu_affinity_setsize
;
131 unsigned long long tsc
;
132 unsigned long long aperf
;
133 unsigned long long mperf
;
134 unsigned long long c1
;
135 unsigned long long extra_msr64
;
136 unsigned long long extra_delta64
;
137 unsigned long long extra_msr32
;
138 unsigned long long extra_delta32
;
139 unsigned int smi_count
;
142 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
143 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
144 } *thread_even
, *thread_odd
;
147 unsigned long long c3
;
148 unsigned long long c6
;
149 unsigned long long c7
;
150 unsigned int core_temp_c
;
151 unsigned int core_id
;
152 } *core_even
, *core_odd
;
155 unsigned long long pc2
;
156 unsigned long long pc3
;
157 unsigned long long pc6
;
158 unsigned long long pc7
;
159 unsigned long long pc8
;
160 unsigned long long pc9
;
161 unsigned long long pc10
;
162 unsigned int package_id
;
163 unsigned int energy_pkg
; /* MSR_PKG_ENERGY_STATUS */
164 unsigned int energy_dram
; /* MSR_DRAM_ENERGY_STATUS */
165 unsigned int energy_cores
; /* MSR_PP0_ENERGY_STATUS */
166 unsigned int energy_gfx
; /* MSR_PP1_ENERGY_STATUS */
167 unsigned int rapl_pkg_perf_status
; /* MSR_PKG_PERF_STATUS */
168 unsigned int rapl_dram_perf_status
; /* MSR_DRAM_PERF_STATUS */
169 unsigned int pkg_temp_c
;
171 } *package_even
, *package_odd
;
173 #define ODD_COUNTERS thread_odd, core_odd, package_odd
174 #define EVEN_COUNTERS thread_even, core_even, package_even
176 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
177 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
178 topo.num_threads_per_core + \
179 (core_no) * topo.num_threads_per_core + (thread_no))
180 #define GET_CORE(core_base, core_no, pkg_no) \
181 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
182 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
184 struct system_summary
{
185 struct thread_data threads
;
186 struct core_data cores
;
187 struct pkg_data packages
;
196 int num_cores_per_pkg
;
197 int num_threads_per_core
;
200 struct timeval tv_even
, tv_odd
, tv_delta
;
202 void setup_all_buffers(void);
204 int cpu_is_not_present(int cpu
)
206 return !CPU_ISSET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
209 * run func(thread, core, package) in topology order
210 * skip non-present cpus
213 int for_all_cpus(int (func
)(struct thread_data
*, struct core_data
*, struct pkg_data
*),
214 struct thread_data
*thread_base
, struct core_data
*core_base
, struct pkg_data
*pkg_base
)
216 int retval
, pkg_no
, core_no
, thread_no
;
218 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
219 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
220 for (thread_no
= 0; thread_no
<
221 topo
.num_threads_per_core
; ++thread_no
) {
222 struct thread_data
*t
;
226 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
228 if (cpu_is_not_present(t
->cpu_id
))
231 c
= GET_CORE(core_base
, core_no
, pkg_no
);
232 p
= GET_PKG(pkg_base
, pkg_no
);
234 retval
= func(t
, c
, p
);
243 int cpu_migrate(int cpu
)
245 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
246 CPU_SET_S(cpu
, cpu_affinity_setsize
, cpu_affinity_set
);
247 if (sched_setaffinity(0, cpu_affinity_setsize
, cpu_affinity_set
) == -1)
253 int get_msr(int cpu
, off_t offset
, unsigned long long *msr
)
259 sprintf(pathname
, "/dev/cpu/%d/msr", cpu
);
260 fd
= open(pathname
, O_RDONLY
);
262 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname
);
264 retval
= pread(fd
, msr
, sizeof *msr
, offset
);
267 if (retval
!= sizeof *msr
)
268 err(-1, "%s offset 0x%llx read failed", pathname
, (unsigned long long)offset
);
274 * Example Format w/ field column widths:
276 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
277 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
280 void print_header(void)
283 outp
+= sprintf(outp
, " Package");
285 outp
+= sprintf(outp
, " Core");
287 outp
+= sprintf(outp
, " CPU");
289 outp
+= sprintf(outp
, " Avg_MHz");
291 outp
+= sprintf(outp
, " %%Busy");
293 outp
+= sprintf(outp
, " Bzy_MHz");
294 outp
+= sprintf(outp
, " TSC_MHz");
296 if (extra_delta_offset32
)
297 outp
+= sprintf(outp
, " count 0x%03X", extra_delta_offset32
);
298 if (extra_delta_offset64
)
299 outp
+= sprintf(outp
, " COUNT 0x%03X", extra_delta_offset64
);
300 if (extra_msr_offset32
)
301 outp
+= sprintf(outp
, " MSR 0x%03X", extra_msr_offset32
);
302 if (extra_msr_offset64
)
303 outp
+= sprintf(outp
, " MSR 0x%03X", extra_msr_offset64
);
309 outp
+= sprintf(outp
, " SMI");
312 outp
+= sprintf(outp
, " CPU%%c1");
313 if (do_nhm_cstates
&& !do_slm_cstates
)
314 outp
+= sprintf(outp
, " CPU%%c3");
316 outp
+= sprintf(outp
, " CPU%%c6");
318 outp
+= sprintf(outp
, " CPU%%c7");
321 outp
+= sprintf(outp
, " CoreTmp");
323 outp
+= sprintf(outp
, " PkgTmp");
326 outp
+= sprintf(outp
, " Pkg%%pc2");
328 outp
+= sprintf(outp
, " Pkg%%pc3");
330 outp
+= sprintf(outp
, " Pkg%%pc6");
332 outp
+= sprintf(outp
, " Pkg%%pc7");
334 outp
+= sprintf(outp
, " Pkg%%pc8");
335 outp
+= sprintf(outp
, " Pkg%%pc9");
336 outp
+= sprintf(outp
, " Pk%%pc10");
339 if (do_rapl
&& !rapl_joules
) {
340 if (do_rapl
& RAPL_PKG
)
341 outp
+= sprintf(outp
, " PkgWatt");
342 if (do_rapl
& RAPL_CORES
)
343 outp
+= sprintf(outp
, " CorWatt");
344 if (do_rapl
& RAPL_GFX
)
345 outp
+= sprintf(outp
, " GFXWatt");
346 if (do_rapl
& RAPL_DRAM
)
347 outp
+= sprintf(outp
, " RAMWatt");
348 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
349 outp
+= sprintf(outp
, " PKG_%%");
350 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
351 outp
+= sprintf(outp
, " RAM_%%");
352 } else if (do_rapl
&& rapl_joules
) {
353 if (do_rapl
& RAPL_PKG
)
354 outp
+= sprintf(outp
, " Pkg_J");
355 if (do_rapl
& RAPL_CORES
)
356 outp
+= sprintf(outp
, " Cor_J");
357 if (do_rapl
& RAPL_GFX
)
358 outp
+= sprintf(outp
, " GFX_J");
359 if (do_rapl
& RAPL_DRAM
)
360 outp
+= sprintf(outp
, " RAM_W");
361 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
362 outp
+= sprintf(outp
, " PKG_%%");
363 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
364 outp
+= sprintf(outp
, " RAM_%%");
365 outp
+= sprintf(outp
, " time");
369 outp
+= sprintf(outp
, "\n");
372 int dump_counters(struct thread_data
*t
, struct core_data
*c
,
375 outp
+= sprintf(outp
, "t %p, c %p, p %p\n", t
, c
, p
);
378 outp
+= sprintf(outp
, "CPU: %d flags 0x%x\n",
379 t
->cpu_id
, t
->flags
);
380 outp
+= sprintf(outp
, "TSC: %016llX\n", t
->tsc
);
381 outp
+= sprintf(outp
, "aperf: %016llX\n", t
->aperf
);
382 outp
+= sprintf(outp
, "mperf: %016llX\n", t
->mperf
);
383 outp
+= sprintf(outp
, "c1: %016llX\n", t
->c1
);
384 outp
+= sprintf(outp
, "msr0x%x: %08llX\n",
385 extra_delta_offset32
, t
->extra_delta32
);
386 outp
+= sprintf(outp
, "msr0x%x: %016llX\n",
387 extra_delta_offset64
, t
->extra_delta64
);
388 outp
+= sprintf(outp
, "msr0x%x: %08llX\n",
389 extra_msr_offset32
, t
->extra_msr32
);
390 outp
+= sprintf(outp
, "msr0x%x: %016llX\n",
391 extra_msr_offset64
, t
->extra_msr64
);
393 outp
+= sprintf(outp
, "SMI: %08X\n", t
->smi_count
);
397 outp
+= sprintf(outp
, "core: %d\n", c
->core_id
);
398 outp
+= sprintf(outp
, "c3: %016llX\n", c
->c3
);
399 outp
+= sprintf(outp
, "c6: %016llX\n", c
->c6
);
400 outp
+= sprintf(outp
, "c7: %016llX\n", c
->c7
);
401 outp
+= sprintf(outp
, "DTS: %dC\n", c
->core_temp_c
);
405 outp
+= sprintf(outp
, "package: %d\n", p
->package_id
);
406 outp
+= sprintf(outp
, "pc2: %016llX\n", p
->pc2
);
408 outp
+= sprintf(outp
, "pc3: %016llX\n", p
->pc3
);
410 outp
+= sprintf(outp
, "pc6: %016llX\n", p
->pc6
);
412 outp
+= sprintf(outp
, "pc7: %016llX\n", p
->pc7
);
413 outp
+= sprintf(outp
, "pc8: %016llX\n", p
->pc8
);
414 outp
+= sprintf(outp
, "pc9: %016llX\n", p
->pc9
);
415 outp
+= sprintf(outp
, "pc10: %016llX\n", p
->pc10
);
416 outp
+= sprintf(outp
, "Joules PKG: %0X\n", p
->energy_pkg
);
417 outp
+= sprintf(outp
, "Joules COR: %0X\n", p
->energy_cores
);
418 outp
+= sprintf(outp
, "Joules GFX: %0X\n", p
->energy_gfx
);
419 outp
+= sprintf(outp
, "Joules RAM: %0X\n", p
->energy_dram
);
420 outp
+= sprintf(outp
, "Throttle PKG: %0X\n",
421 p
->rapl_pkg_perf_status
);
422 outp
+= sprintf(outp
, "Throttle RAM: %0X\n",
423 p
->rapl_dram_perf_status
);
424 outp
+= sprintf(outp
, "PTM: %dC\n", p
->pkg_temp_c
);
427 outp
+= sprintf(outp
, "\n");
433 * column formatting convention & formats
435 int format_counters(struct thread_data
*t
, struct core_data
*c
,
438 double interval_float
;
441 /* if showing only 1st thread in core and this isn't one, bail out */
442 if (show_core_only
&& !(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
445 /* if showing only 1st thread in pkg and this isn't one, bail out */
446 if (show_pkg_only
&& !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
449 interval_float
= tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0;
451 /* topo columns, print blanks on 1st (average) line */
452 if (t
== &average
.threads
) {
454 outp
+= sprintf(outp
, " -");
456 outp
+= sprintf(outp
, " -");
458 outp
+= sprintf(outp
, " -");
462 outp
+= sprintf(outp
, "%8d", p
->package_id
);
464 outp
+= sprintf(outp
, " -");
468 outp
+= sprintf(outp
, "%8d", c
->core_id
);
470 outp
+= sprintf(outp
, " -");
473 outp
+= sprintf(outp
, "%8d", t
->cpu_id
);
478 outp
+= sprintf(outp
, "%8.0f",
479 1.0 / units
* t
->aperf
/ interval_float
);
484 outp
+= sprintf(outp
, "%8.2f", 100.0 * t
->mperf
/t
->tsc
);
486 outp
+= sprintf(outp
, "********");
491 outp
+= sprintf(outp
, "%8.0f",
492 1.0 * t
->tsc
/ units
* t
->aperf
/ t
->mperf
/ interval_float
);
495 outp
+= sprintf(outp
, "%8.0f", 1.0 * t
->tsc
/units
/interval_float
);
498 if (extra_delta_offset32
)
499 outp
+= sprintf(outp
, " %11llu", t
->extra_delta32
);
502 if (extra_delta_offset64
)
503 outp
+= sprintf(outp
, " %11llu", t
->extra_delta64
);
505 if (extra_msr_offset32
)
506 outp
+= sprintf(outp
, " 0x%08llx", t
->extra_msr32
);
509 if (extra_msr_offset64
)
510 outp
+= sprintf(outp
, " 0x%016llx", t
->extra_msr64
);
517 outp
+= sprintf(outp
, "%8d", t
->smi_count
);
519 if (do_nhm_cstates
) {
521 outp
+= sprintf(outp
, "%8.2f", 100.0 * t
->c1
/t
->tsc
);
523 outp
+= sprintf(outp
, "********");
526 /* print per-core data only for 1st thread in core */
527 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
530 if (do_nhm_cstates
&& !do_slm_cstates
)
531 outp
+= sprintf(outp
, "%8.2f", 100.0 * c
->c3
/t
->tsc
);
533 outp
+= sprintf(outp
, "%8.2f", 100.0 * c
->c6
/t
->tsc
);
535 outp
+= sprintf(outp
, "%8.2f", 100.0 * c
->c7
/t
->tsc
);
538 outp
+= sprintf(outp
, "%8d", c
->core_temp_c
);
540 /* print per-package data only for 1st core in package */
541 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
545 outp
+= sprintf(outp
, "%8d", p
->pkg_temp_c
);
548 outp
+= sprintf(outp
, "%8.2f", 100.0 * p
->pc2
/t
->tsc
);
550 outp
+= sprintf(outp
, "%8.2f", 100.0 * p
->pc3
/t
->tsc
);
552 outp
+= sprintf(outp
, "%8.2f", 100.0 * p
->pc6
/t
->tsc
);
554 outp
+= sprintf(outp
, "%8.2f", 100.0 * p
->pc7
/t
->tsc
);
556 outp
+= sprintf(outp
, "%8.2f", 100.0 * p
->pc8
/t
->tsc
);
557 outp
+= sprintf(outp
, "%8.2f", 100.0 * p
->pc9
/t
->tsc
);
558 outp
+= sprintf(outp
, "%8.2f", 100.0 * p
->pc10
/t
->tsc
);
562 * If measurement interval exceeds minimum RAPL Joule Counter range,
563 * indicate that results are suspect by printing "**" in fraction place.
565 if (interval_float
< rapl_joule_counter_range
)
570 if (do_rapl
&& !rapl_joules
) {
571 if (do_rapl
& RAPL_PKG
)
572 outp
+= sprintf(outp
, fmt8
, p
->energy_pkg
* rapl_energy_units
/ interval_float
);
573 if (do_rapl
& RAPL_CORES
)
574 outp
+= sprintf(outp
, fmt8
, p
->energy_cores
* rapl_energy_units
/ interval_float
);
575 if (do_rapl
& RAPL_GFX
)
576 outp
+= sprintf(outp
, fmt8
, p
->energy_gfx
* rapl_energy_units
/ interval_float
);
577 if (do_rapl
& RAPL_DRAM
)
578 outp
+= sprintf(outp
, fmt8
, p
->energy_dram
* rapl_energy_units
/ interval_float
);
579 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
580 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_pkg_perf_status
* rapl_time_units
/ interval_float
);
581 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
582 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_dram_perf_status
* rapl_time_units
/ interval_float
);
583 } else if (do_rapl
&& rapl_joules
) {
584 if (do_rapl
& RAPL_PKG
)
585 outp
+= sprintf(outp
, fmt8
,
586 p
->energy_pkg
* rapl_energy_units
);
587 if (do_rapl
& RAPL_CORES
)
588 outp
+= sprintf(outp
, fmt8
,
589 p
->energy_cores
* rapl_energy_units
);
590 if (do_rapl
& RAPL_GFX
)
591 outp
+= sprintf(outp
, fmt8
,
592 p
->energy_gfx
* rapl_energy_units
);
593 if (do_rapl
& RAPL_DRAM
)
594 outp
+= sprintf(outp
, fmt8
,
595 p
->energy_dram
* rapl_energy_units
);
596 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
597 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_pkg_perf_status
* rapl_time_units
/ interval_float
);
598 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
599 outp
+= sprintf(outp
, fmt8
, 100.0 * p
->rapl_dram_perf_status
* rapl_time_units
/ interval_float
);
601 outp
+= sprintf(outp
, fmt8
, interval_float
);
604 outp
+= sprintf(outp
, "\n");
611 fputs(output_buffer
, stdout
);
613 outp
= output_buffer
;
617 fputs(output_buffer
, stderr
);
618 outp
= output_buffer
;
620 void format_all_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
624 if (!printed
|| !summary_only
)
627 if (topo
.num_cpus
> 1)
628 format_counters(&average
.threads
, &average
.cores
,
636 for_all_cpus(format_counters
, t
, c
, p
);
639 #define DELTA_WRAP32(new, old) \
643 old = 0x100000000 + new - old; \
647 delta_package(struct pkg_data
*new, struct pkg_data
*old
)
649 old
->pc2
= new->pc2
- old
->pc2
;
651 old
->pc3
= new->pc3
- old
->pc3
;
653 old
->pc6
= new->pc6
- old
->pc6
;
655 old
->pc7
= new->pc7
- old
->pc7
;
656 old
->pc8
= new->pc8
- old
->pc8
;
657 old
->pc9
= new->pc9
- old
->pc9
;
658 old
->pc10
= new->pc10
- old
->pc10
;
659 old
->pkg_temp_c
= new->pkg_temp_c
;
661 DELTA_WRAP32(new->energy_pkg
, old
->energy_pkg
);
662 DELTA_WRAP32(new->energy_cores
, old
->energy_cores
);
663 DELTA_WRAP32(new->energy_gfx
, old
->energy_gfx
);
664 DELTA_WRAP32(new->energy_dram
, old
->energy_dram
);
665 DELTA_WRAP32(new->rapl_pkg_perf_status
, old
->rapl_pkg_perf_status
);
666 DELTA_WRAP32(new->rapl_dram_perf_status
, old
->rapl_dram_perf_status
);
670 delta_core(struct core_data
*new, struct core_data
*old
)
672 old
->c3
= new->c3
- old
->c3
;
673 old
->c6
= new->c6
- old
->c6
;
674 old
->c7
= new->c7
- old
->c7
;
675 old
->core_temp_c
= new->core_temp_c
;
682 delta_thread(struct thread_data
*new, struct thread_data
*old
,
683 struct core_data
*core_delta
)
685 old
->tsc
= new->tsc
- old
->tsc
;
687 /* check for TSC < 1 Mcycles over interval */
688 if (old
->tsc
< (1000 * 1000))
689 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
690 "You can disable all c-states by booting with \"idle=poll\"\n"
691 "or just the deep ones with \"processor.max_cstate=1\"");
693 old
->c1
= new->c1
- old
->c1
;
696 if ((new->aperf
> old
->aperf
) && (new->mperf
> old
->mperf
)) {
697 old
->aperf
= new->aperf
- old
->aperf
;
698 old
->mperf
= new->mperf
- old
->mperf
;
701 if (!aperf_mperf_unstable
) {
702 fprintf(stderr
, "%s: APERF or MPERF went backwards *\n", progname
);
703 fprintf(stderr
, "* Frequency results do not cover entire interval *\n");
704 fprintf(stderr
, "* fix this by running Linux-2.6.30 or later *\n");
706 aperf_mperf_unstable
= 1;
709 * mperf delta is likely a huge "positive" number
710 * can not use it for calculating c0 time
718 if (use_c1_residency_msr
) {
720 * Some models have a dedicated C1 residency MSR,
721 * which should be more accurate than the derivation below.
725 * As counter collection is not atomic,
726 * it is possible for mperf's non-halted cycles + idle states
727 * to exceed TSC's all cycles: show c1 = 0% in that case.
729 if ((old
->mperf
+ core_delta
->c3
+ core_delta
->c6
+ core_delta
->c7
) > old
->tsc
)
732 /* normal case, derive c1 */
733 old
->c1
= old
->tsc
- old
->mperf
- core_delta
->c3
734 - core_delta
->c6
- core_delta
->c7
;
738 if (old
->mperf
== 0) {
739 if (debug
> 1) fprintf(stderr
, "cpu%d MPERF 0!\n", old
->cpu_id
);
740 old
->mperf
= 1; /* divide by 0 protection */
743 old
->extra_delta32
= new->extra_delta32
- old
->extra_delta32
;
744 old
->extra_delta32
&= 0xFFFFFFFF;
746 old
->extra_delta64
= new->extra_delta64
- old
->extra_delta64
;
749 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
751 old
->extra_msr32
= new->extra_msr32
;
752 old
->extra_msr64
= new->extra_msr64
;
755 old
->smi_count
= new->smi_count
- old
->smi_count
;
758 int delta_cpu(struct thread_data
*t
, struct core_data
*c
,
759 struct pkg_data
*p
, struct thread_data
*t2
,
760 struct core_data
*c2
, struct pkg_data
*p2
)
762 /* calculate core delta only for 1st thread in core */
763 if (t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
)
766 /* always calculate thread delta */
767 delta_thread(t
, t2
, c2
); /* c2 is core delta */
769 /* calculate package delta only for 1st core in package */
770 if (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)
771 delta_package(p
, p2
);
776 void clear_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
784 t
->extra_delta32
= 0;
785 t
->extra_delta64
= 0;
787 /* tells format_counters to dump all fields from this set */
788 t
->flags
= CPU_IS_FIRST_THREAD_IN_CORE
| CPU_IS_FIRST_CORE_IN_PACKAGE
;
810 p
->rapl_pkg_perf_status
= 0;
811 p
->rapl_dram_perf_status
= 0;
814 int sum_counters(struct thread_data
*t
, struct core_data
*c
,
817 average
.threads
.tsc
+= t
->tsc
;
818 average
.threads
.aperf
+= t
->aperf
;
819 average
.threads
.mperf
+= t
->mperf
;
820 average
.threads
.c1
+= t
->c1
;
822 average
.threads
.extra_delta32
+= t
->extra_delta32
;
823 average
.threads
.extra_delta64
+= t
->extra_delta64
;
825 /* sum per-core values only for 1st thread in core */
826 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
829 average
.cores
.c3
+= c
->c3
;
830 average
.cores
.c6
+= c
->c6
;
831 average
.cores
.c7
+= c
->c7
;
833 average
.cores
.core_temp_c
= MAX(average
.cores
.core_temp_c
, c
->core_temp_c
);
835 /* sum per-pkg values only for 1st core in pkg */
836 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
839 average
.packages
.pc2
+= p
->pc2
;
841 average
.packages
.pc3
+= p
->pc3
;
843 average
.packages
.pc6
+= p
->pc6
;
845 average
.packages
.pc7
+= p
->pc7
;
846 average
.packages
.pc8
+= p
->pc8
;
847 average
.packages
.pc9
+= p
->pc9
;
848 average
.packages
.pc10
+= p
->pc10
;
850 average
.packages
.energy_pkg
+= p
->energy_pkg
;
851 average
.packages
.energy_dram
+= p
->energy_dram
;
852 average
.packages
.energy_cores
+= p
->energy_cores
;
853 average
.packages
.energy_gfx
+= p
->energy_gfx
;
855 average
.packages
.pkg_temp_c
= MAX(average
.packages
.pkg_temp_c
, p
->pkg_temp_c
);
857 average
.packages
.rapl_pkg_perf_status
+= p
->rapl_pkg_perf_status
;
858 average
.packages
.rapl_dram_perf_status
+= p
->rapl_dram_perf_status
;
862 * sum the counters for all cpus in the system
863 * compute the weighted average
865 void compute_average(struct thread_data
*t
, struct core_data
*c
,
868 clear_counters(&average
.threads
, &average
.cores
, &average
.packages
);
870 for_all_cpus(sum_counters
, t
, c
, p
);
872 average
.threads
.tsc
/= topo
.num_cpus
;
873 average
.threads
.aperf
/= topo
.num_cpus
;
874 average
.threads
.mperf
/= topo
.num_cpus
;
875 average
.threads
.c1
/= topo
.num_cpus
;
877 average
.threads
.extra_delta32
/= topo
.num_cpus
;
878 average
.threads
.extra_delta32
&= 0xFFFFFFFF;
880 average
.threads
.extra_delta64
/= topo
.num_cpus
;
882 average
.cores
.c3
/= topo
.num_cores
;
883 average
.cores
.c6
/= topo
.num_cores
;
884 average
.cores
.c7
/= topo
.num_cores
;
886 average
.packages
.pc2
/= topo
.num_packages
;
888 average
.packages
.pc3
/= topo
.num_packages
;
890 average
.packages
.pc6
/= topo
.num_packages
;
892 average
.packages
.pc7
/= topo
.num_packages
;
894 average
.packages
.pc8
/= topo
.num_packages
;
895 average
.packages
.pc9
/= topo
.num_packages
;
896 average
.packages
.pc10
/= topo
.num_packages
;
899 static unsigned long long rdtsc(void)
901 unsigned int low
, high
;
903 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
905 return low
| ((unsigned long long)high
) << 32;
912 * acquire and record local counters for that cpu
914 int get_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
917 unsigned long long msr
;
919 if (cpu_migrate(cpu
)) {
920 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
924 t
->tsc
= rdtsc(); /* we are running on local CPU of interest */
927 if (get_msr(cpu
, MSR_IA32_APERF
, &t
->aperf
))
929 if (get_msr(cpu
, MSR_IA32_MPERF
, &t
->mperf
))
934 if (get_msr(cpu
, MSR_SMI_COUNT
, &msr
))
936 t
->smi_count
= msr
& 0xFFFFFFFF;
938 if (extra_delta_offset32
) {
939 if (get_msr(cpu
, extra_delta_offset32
, &msr
))
941 t
->extra_delta32
= msr
& 0xFFFFFFFF;
944 if (extra_delta_offset64
)
945 if (get_msr(cpu
, extra_delta_offset64
, &t
->extra_delta64
))
948 if (extra_msr_offset32
) {
949 if (get_msr(cpu
, extra_msr_offset32
, &msr
))
951 t
->extra_msr32
= msr
& 0xFFFFFFFF;
954 if (extra_msr_offset64
)
955 if (get_msr(cpu
, extra_msr_offset64
, &t
->extra_msr64
))
958 if (use_c1_residency_msr
) {
959 if (get_msr(cpu
, MSR_CORE_C1_RES
, &t
->c1
))
963 /* collect core counters only for 1st thread in core */
964 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
967 if (do_nhm_cstates
&& !do_slm_cstates
) {
968 if (get_msr(cpu
, MSR_CORE_C3_RESIDENCY
, &c
->c3
))
972 if (do_nhm_cstates
) {
973 if (get_msr(cpu
, MSR_CORE_C6_RESIDENCY
, &c
->c6
))
978 if (get_msr(cpu
, MSR_CORE_C7_RESIDENCY
, &c
->c7
))
982 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
984 c
->core_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
988 /* collect package counters only for 1st core in package */
989 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
993 if (get_msr(cpu
, MSR_PKG_C3_RESIDENCY
, &p
->pc3
))
996 if (get_msr(cpu
, MSR_PKG_C6_RESIDENCY
, &p
->pc6
))
999 if (get_msr(cpu
, MSR_PKG_C2_RESIDENCY
, &p
->pc2
))
1002 if (get_msr(cpu
, MSR_PKG_C7_RESIDENCY
, &p
->pc7
))
1005 if (get_msr(cpu
, MSR_PKG_C8_RESIDENCY
, &p
->pc8
))
1007 if (get_msr(cpu
, MSR_PKG_C9_RESIDENCY
, &p
->pc9
))
1009 if (get_msr(cpu
, MSR_PKG_C10_RESIDENCY
, &p
->pc10
))
1012 if (do_rapl
& RAPL_PKG
) {
1013 if (get_msr(cpu
, MSR_PKG_ENERGY_STATUS
, &msr
))
1015 p
->energy_pkg
= msr
& 0xFFFFFFFF;
1017 if (do_rapl
& RAPL_CORES
) {
1018 if (get_msr(cpu
, MSR_PP0_ENERGY_STATUS
, &msr
))
1020 p
->energy_cores
= msr
& 0xFFFFFFFF;
1022 if (do_rapl
& RAPL_DRAM
) {
1023 if (get_msr(cpu
, MSR_DRAM_ENERGY_STATUS
, &msr
))
1025 p
->energy_dram
= msr
& 0xFFFFFFFF;
1027 if (do_rapl
& RAPL_GFX
) {
1028 if (get_msr(cpu
, MSR_PP1_ENERGY_STATUS
, &msr
))
1030 p
->energy_gfx
= msr
& 0xFFFFFFFF;
1032 if (do_rapl
& RAPL_PKG_PERF_STATUS
) {
1033 if (get_msr(cpu
, MSR_PKG_PERF_STATUS
, &msr
))
1035 p
->rapl_pkg_perf_status
= msr
& 0xFFFFFFFF;
1037 if (do_rapl
& RAPL_DRAM_PERF_STATUS
) {
1038 if (get_msr(cpu
, MSR_DRAM_PERF_STATUS
, &msr
))
1040 p
->rapl_dram_perf_status
= msr
& 0xFFFFFFFF;
1043 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
1045 p
->pkg_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
1051 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1052 * If you change the values, note they are used both in comparisons
1053 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1056 #define PCLUKN 0 /* Unknown */
1057 #define PCLRSV 1 /* Reserved */
1058 #define PCL__0 2 /* PC0 */
1059 #define PCL__1 3 /* PC1 */
1060 #define PCL__2 4 /* PC2 */
1061 #define PCL__3 5 /* PC3 */
1062 #define PCL__4 6 /* PC4 */
1063 #define PCL__6 7 /* PC6 */
1064 #define PCL_6N 8 /* PC6 No Retention */
1065 #define PCL_6R 9 /* PC6 Retention */
1066 #define PCL__7 10 /* PC7 */
1067 #define PCL_7S 11 /* PC7 Shrink */
1068 #define PCLUNL 12 /* Unlimited */
1070 int pkg_cstate_limit
= PCLUKN
;
1071 char *pkg_cstate_limit_strings
[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1072 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "unlimited"};
1074 int nhm_pkg_cstate_limits
[8] = {PCL__0
, PCL__1
, PCL__3
, PCL__6
, PCL__7
, PCLRSV
, PCLRSV
, PCLUNL
};
1075 int snb_pkg_cstate_limits
[8] = {PCL__0
, PCL__2
, PCL_6N
, PCL_6R
, PCL__7
, PCL_7S
, PCLRSV
, PCLUNL
};
1076 int hsw_pkg_cstate_limits
[8] = {PCL__0
, PCL__2
, PCL__3
, PCL__6
, PCL__7
, PCL_7S
, PCLRSV
, PCLUNL
};
1077 int slv_pkg_cstate_limits
[8] = {PCL__0
, PCL__1
, PCLRSV
, PCLRSV
, PCL__4
, PCLRSV
, PCL__6
, PCL__7
};
1078 int amt_pkg_cstate_limits
[8] = {PCL__0
, PCL__1
, PCL__2
, PCLRSV
, PCLRSV
, PCLRSV
, PCL__6
, PCL__7
};
1079 int phi_pkg_cstate_limits
[8] = {PCL__0
, PCL__2
, PCL_6N
, PCL_6R
, PCLRSV
, PCLRSV
, PCLRSV
, PCLUNL
};
1081 void dump_system_config_info(void)
1083 unsigned long long msr
;
1086 if (!do_nhm_platform_info
)
1089 get_msr(0, MSR_NHM_PLATFORM_INFO
, &msr
);
1091 fprintf(stderr
, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr
);
1093 ratio
= (msr
>> 40) & 0xFF;
1094 fprintf(stderr
, "%d * %.0f = %.0f MHz max efficiency\n",
1095 ratio
, bclk
, ratio
* bclk
);
1097 ratio
= (msr
>> 8) & 0xFF;
1098 fprintf(stderr
, "%d * %.0f = %.0f MHz TSC frequency\n",
1099 ratio
, bclk
, ratio
* bclk
);
1101 get_msr(0, MSR_IA32_POWER_CTL
, &msr
);
1102 fprintf(stderr
, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1103 msr
, msr
& 0x2 ? "EN" : "DIS");
1105 if (!do_ivt_turbo_ratio_limit
)
1106 goto print_nhm_turbo_ratio_limits
;
1108 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT
, &msr
);
1110 fprintf(stderr
, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr
);
1112 ratio
= (msr
>> 56) & 0xFF;
1114 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1115 ratio
, bclk
, ratio
* bclk
);
1117 ratio
= (msr
>> 48) & 0xFF;
1119 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1120 ratio
, bclk
, ratio
* bclk
);
1122 ratio
= (msr
>> 40) & 0xFF;
1124 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1125 ratio
, bclk
, ratio
* bclk
);
1127 ratio
= (msr
>> 32) & 0xFF;
1129 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1130 ratio
, bclk
, ratio
* bclk
);
1132 ratio
= (msr
>> 24) & 0xFF;
1134 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1135 ratio
, bclk
, ratio
* bclk
);
1137 ratio
= (msr
>> 16) & 0xFF;
1139 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1140 ratio
, bclk
, ratio
* bclk
);
1142 ratio
= (msr
>> 8) & 0xFF;
1144 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1145 ratio
, bclk
, ratio
* bclk
);
1147 ratio
= (msr
>> 0) & 0xFF;
1149 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1150 ratio
, bclk
, ratio
* bclk
);
1152 print_nhm_turbo_ratio_limits
:
1153 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL
, &msr
);
1155 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1156 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1158 fprintf(stderr
, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr
);
1160 fprintf(stderr
, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1161 (msr
& SNB_C3_AUTO_UNDEMOTE
) ? "UNdemote-C3, " : "",
1162 (msr
& SNB_C1_AUTO_UNDEMOTE
) ? "UNdemote-C1, " : "",
1163 (msr
& NHM_C3_AUTO_DEMOTE
) ? "demote-C3, " : "",
1164 (msr
& NHM_C1_AUTO_DEMOTE
) ? "demote-C1, " : "",
1165 (msr
& (1 << 15)) ? "" : "UN",
1166 (unsigned int)msr
& 7,
1167 pkg_cstate_limit_strings
[pkg_cstate_limit
]);
1169 if (!do_nhm_turbo_ratio_limit
)
1172 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT
, &msr
);
1174 fprintf(stderr
, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr
);
1176 ratio
= (msr
>> 56) & 0xFF;
1178 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1179 ratio
, bclk
, ratio
* bclk
);
1181 ratio
= (msr
>> 48) & 0xFF;
1183 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1184 ratio
, bclk
, ratio
* bclk
);
1186 ratio
= (msr
>> 40) & 0xFF;
1188 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1189 ratio
, bclk
, ratio
* bclk
);
1191 ratio
= (msr
>> 32) & 0xFF;
1193 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1194 ratio
, bclk
, ratio
* bclk
);
1196 ratio
= (msr
>> 24) & 0xFF;
1198 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1199 ratio
, bclk
, ratio
* bclk
);
1201 ratio
= (msr
>> 16) & 0xFF;
1203 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1204 ratio
, bclk
, ratio
* bclk
);
1206 ratio
= (msr
>> 8) & 0xFF;
1208 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1209 ratio
, bclk
, ratio
* bclk
);
1211 ratio
= (msr
>> 0) & 0xFF;
1213 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1214 ratio
, bclk
, ratio
* bclk
);
1218 void free_all_buffers(void)
1220 CPU_FREE(cpu_present_set
);
1221 cpu_present_set
= NULL
;
1222 cpu_present_set
= 0;
1224 CPU_FREE(cpu_affinity_set
);
1225 cpu_affinity_set
= NULL
;
1226 cpu_affinity_setsize
= 0;
1234 package_even
= NULL
;
1244 free(output_buffer
);
1245 output_buffer
= NULL
;
1250 * Open a file, and exit on failure
1252 FILE *fopen_or_die(const char *path
, const char *mode
)
1254 FILE *filep
= fopen(path
, "r");
1256 err(1, "%s: open failed", path
);
1261 * Parse a file containing a single int.
1263 int parse_int_file(const char *fmt
, ...)
1266 char path
[PATH_MAX
];
1270 va_start(args
, fmt
);
1271 vsnprintf(path
, sizeof(path
), fmt
, args
);
1273 filep
= fopen_or_die(path
, "r");
1274 if (fscanf(filep
, "%d", &value
) != 1)
1275 err(1, "%s: failed to parse number from file", path
);
1281 * cpu_is_first_sibling_in_core(cpu)
1282 * return 1 if given CPU is 1st HT sibling in the core
1284 int cpu_is_first_sibling_in_core(int cpu
)
1286 return cpu
== parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu
);
1290 * cpu_is_first_core_in_package(cpu)
1291 * return 1 if given CPU is 1st core in package
1293 int cpu_is_first_core_in_package(int cpu
)
1295 return cpu
== parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu
);
1298 int get_physical_package_id(int cpu
)
1300 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu
);
1303 int get_core_id(int cpu
)
1305 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu
);
1308 int get_num_ht_siblings(int cpu
)
1316 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu
);
1317 filep
= fopen_or_die(path
, "r");
1320 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1321 * otherwinse 1 sibling (self).
1323 matches
= fscanf(filep
, "%d%c%d\n", &sib1
, &character
, &sib2
);
1334 * run func(thread, core, package) in topology order
1335 * skip non-present cpus
1338 int for_all_cpus_2(int (func
)(struct thread_data
*, struct core_data
*,
1339 struct pkg_data
*, struct thread_data
*, struct core_data
*,
1340 struct pkg_data
*), struct thread_data
*thread_base
,
1341 struct core_data
*core_base
, struct pkg_data
*pkg_base
,
1342 struct thread_data
*thread_base2
, struct core_data
*core_base2
,
1343 struct pkg_data
*pkg_base2
)
1345 int retval
, pkg_no
, core_no
, thread_no
;
1347 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
1348 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
1349 for (thread_no
= 0; thread_no
<
1350 topo
.num_threads_per_core
; ++thread_no
) {
1351 struct thread_data
*t
, *t2
;
1352 struct core_data
*c
, *c2
;
1353 struct pkg_data
*p
, *p2
;
1355 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
1357 if (cpu_is_not_present(t
->cpu_id
))
1360 t2
= GET_THREAD(thread_base2
, thread_no
, core_no
, pkg_no
);
1362 c
= GET_CORE(core_base
, core_no
, pkg_no
);
1363 c2
= GET_CORE(core_base2
, core_no
, pkg_no
);
1365 p
= GET_PKG(pkg_base
, pkg_no
);
1366 p2
= GET_PKG(pkg_base2
, pkg_no
);
1368 retval
= func(t
, c
, p
, t2
, c2
, p2
);
1378 * run func(cpu) on every cpu in /proc/stat
1379 * return max_cpu number
1381 int for_all_proc_cpus(int (func
)(int))
1387 fp
= fopen_or_die(proc_stat
, "r");
1389 retval
= fscanf(fp
, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1391 err(1, "%s: failed to parse format", proc_stat
);
1394 retval
= fscanf(fp
, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num
);
1398 retval
= func(cpu_num
);
1408 void re_initialize(void)
1411 setup_all_buffers();
1412 printf("turbostat: re-initialized with num_cpus %d\n", topo
.num_cpus
);
1418 * remember the last one seen, it will be the max
1420 int count_cpus(int cpu
)
1422 if (topo
.max_cpu_num
< cpu
)
1423 topo
.max_cpu_num
= cpu
;
1428 int mark_cpu_present(int cpu
)
1430 CPU_SET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
1434 void turbostat_loop()
1442 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
1445 } else if (retval
== -1) {
1446 if (restarted
> 1) {
1453 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
1456 if (for_all_proc_cpus(cpu_is_not_present
)) {
1460 sleep(interval_sec
);
1461 retval
= for_all_cpus(get_counters
, ODD_COUNTERS
);
1464 } else if (retval
== -1) {
1468 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
1469 timersub(&tv_odd
, &tv_even
, &tv_delta
);
1470 for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
);
1471 compute_average(EVEN_COUNTERS
);
1472 format_all_counters(EVEN_COUNTERS
);
1474 sleep(interval_sec
);
1475 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
1478 } else if (retval
== -1) {
1482 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
1483 timersub(&tv_even
, &tv_odd
, &tv_delta
);
1484 for_all_cpus_2(delta_cpu
, EVEN_COUNTERS
, ODD_COUNTERS
);
1485 compute_average(ODD_COUNTERS
);
1486 format_all_counters(ODD_COUNTERS
);
1491 void check_dev_msr()
1495 if (stat("/dev/cpu/0/msr", &sb
))
1496 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1499 void check_permissions()
1501 struct __user_cap_header_struct cap_header_data
;
1502 cap_user_header_t cap_header
= &cap_header_data
;
1503 struct __user_cap_data_struct cap_data_data
;
1504 cap_user_data_t cap_data
= &cap_data_data
;
1505 extern int capget(cap_user_header_t hdrp
, cap_user_data_t datap
);
1508 /* check for CAP_SYS_RAWIO */
1509 cap_header
->pid
= getpid();
1510 cap_header
->version
= _LINUX_CAPABILITY_VERSION
;
1511 if (capget(cap_header
, cap_data
) < 0)
1512 err(-6, "capget(2) failed");
1514 if ((cap_data
->effective
& (1 << CAP_SYS_RAWIO
)) == 0) {
1516 warnx("capget(CAP_SYS_RAWIO) failed,"
1517 " try \"# setcap cap_sys_rawio=ep %s\"", progname
);
1520 /* test file permissions */
1521 if (euidaccess("/dev/cpu/0/msr", R_OK
)) {
1523 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1526 /* if all else fails, thell them to be root */
1529 warnx("... or simply run as root");
1536 * NHM adds support for additional MSRs:
1538 * MSR_SMI_COUNT 0x00000034
1540 * MSR_NHM_PLATFORM_INFO 0x000000ce
1541 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1543 * MSR_PKG_C3_RESIDENCY 0x000003f8
1544 * MSR_PKG_C6_RESIDENCY 0x000003f9
1545 * MSR_CORE_C3_RESIDENCY 0x000003fc
1546 * MSR_CORE_C6_RESIDENCY 0x000003fd
1549 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
1551 int probe_nhm_msrs(unsigned int family
, unsigned int model
)
1553 unsigned long long msr
;
1554 int *pkg_cstate_limits
;
1563 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1564 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1565 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1566 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1567 case 0x2C: /* Westmere EP - Gulftown */
1568 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1569 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1570 pkg_cstate_limits
= nhm_pkg_cstate_limits
;
1572 case 0x2A: /* SNB */
1573 case 0x2D: /* SNB Xeon */
1574 case 0x3A: /* IVB */
1575 case 0x3E: /* IVB Xeon */
1576 pkg_cstate_limits
= snb_pkg_cstate_limits
;
1578 case 0x3C: /* HSW */
1579 case 0x3F: /* HSX */
1580 case 0x45: /* HSW */
1581 case 0x46: /* HSW */
1582 case 0x3D: /* BDW */
1583 case 0x47: /* BDW */
1584 case 0x4F: /* BDX */
1585 case 0x56: /* BDX-DE */
1586 pkg_cstate_limits
= hsw_pkg_cstate_limits
;
1588 case 0x37: /* BYT */
1589 case 0x4D: /* AVN */
1590 pkg_cstate_limits
= slv_pkg_cstate_limits
;
1592 case 0x4C: /* AMT */
1593 pkg_cstate_limits
= amt_pkg_cstate_limits
;
1595 case 0x57: /* PHI */
1596 pkg_cstate_limits
= phi_pkg_cstate_limits
;
1601 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL
, &msr
);
1603 pkg_cstate_limit
= pkg_cstate_limits
[msr
& 0x7];
1607 int has_nhm_turbo_ratio_limit(unsigned int family
, unsigned int model
)
1610 /* Nehalem compatible, but do not include turbo-ratio limit support */
1611 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1612 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1618 int has_ivt_turbo_ratio_limit(unsigned int family
, unsigned int model
)
1627 case 0x3E: /* IVB Xeon */
1636 * Decode the ENERGY_PERF_BIAS MSR
1638 int print_epb(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1640 unsigned long long msr
;
1649 /* EPB is per-package */
1650 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1653 if (cpu_migrate(cpu
)) {
1654 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1658 if (get_msr(cpu
, MSR_IA32_ENERGY_PERF_BIAS
, &msr
))
1661 switch (msr
& 0x7) {
1662 case ENERGY_PERF_BIAS_PERFORMANCE
:
1663 epb_string
= "performance";
1665 case ENERGY_PERF_BIAS_NORMAL
:
1666 epb_string
= "balanced";
1668 case ENERGY_PERF_BIAS_POWERSAVE
:
1669 epb_string
= "powersave";
1672 epb_string
= "custom";
1675 fprintf(stderr
, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu
, msr
, epb_string
);
1681 * print_perf_limit()
1683 int print_perf_limit(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1685 unsigned long long msr
;
1691 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1694 if (cpu_migrate(cpu
)) {
1695 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1699 if (do_core_perf_limit_reasons
) {
1700 get_msr(cpu
, MSR_CORE_PERF_LIMIT_REASONS
, &msr
);
1701 fprintf(stderr
, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu
, msr
);
1702 fprintf(stderr
, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1703 (msr
& 1 << 15) ? "bit15, " : "",
1704 (msr
& 1 << 14) ? "bit14, " : "",
1705 (msr
& 1 << 13) ? "Transitions, " : "",
1706 (msr
& 1 << 12) ? "MultiCoreTurbo, " : "",
1707 (msr
& 1 << 11) ? "PkgPwrL2, " : "",
1708 (msr
& 1 << 10) ? "PkgPwrL1, " : "",
1709 (msr
& 1 << 9) ? "CorePwr, " : "",
1710 (msr
& 1 << 8) ? "Amps, " : "",
1711 (msr
& 1 << 6) ? "VR-Therm, " : "",
1712 (msr
& 1 << 5) ? "Auto-HWP, " : "",
1713 (msr
& 1 << 4) ? "Graphics, " : "",
1714 (msr
& 1 << 2) ? "bit2, " : "",
1715 (msr
& 1 << 1) ? "ThermStatus, " : "",
1716 (msr
& 1 << 0) ? "PROCHOT, " : "");
1717 fprintf(stderr
, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
1718 (msr
& 1 << 31) ? "bit31, " : "",
1719 (msr
& 1 << 30) ? "bit30, " : "",
1720 (msr
& 1 << 29) ? "Transitions, " : "",
1721 (msr
& 1 << 28) ? "MultiCoreTurbo, " : "",
1722 (msr
& 1 << 27) ? "PkgPwrL2, " : "",
1723 (msr
& 1 << 26) ? "PkgPwrL1, " : "",
1724 (msr
& 1 << 25) ? "CorePwr, " : "",
1725 (msr
& 1 << 24) ? "Amps, " : "",
1726 (msr
& 1 << 22) ? "VR-Therm, " : "",
1727 (msr
& 1 << 21) ? "Auto-HWP, " : "",
1728 (msr
& 1 << 20) ? "Graphics, " : "",
1729 (msr
& 1 << 18) ? "bit18, " : "",
1730 (msr
& 1 << 17) ? "ThermStatus, " : "",
1731 (msr
& 1 << 16) ? "PROCHOT, " : "");
1734 if (do_gfx_perf_limit_reasons
) {
1735 get_msr(cpu
, MSR_GFX_PERF_LIMIT_REASONS
, &msr
);
1736 fprintf(stderr
, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu
, msr
);
1737 fprintf(stderr
, " (Active: %s%s%s%s%s%s%s%s)",
1738 (msr
& 1 << 0) ? "PROCHOT, " : "",
1739 (msr
& 1 << 1) ? "ThermStatus, " : "",
1740 (msr
& 1 << 4) ? "Graphics, " : "",
1741 (msr
& 1 << 6) ? "VR-Therm, " : "",
1742 (msr
& 1 << 8) ? "Amps, " : "",
1743 (msr
& 1 << 9) ? "GFXPwr, " : "",
1744 (msr
& 1 << 10) ? "PkgPwrL1, " : "",
1745 (msr
& 1 << 11) ? "PkgPwrL2, " : "");
1746 fprintf(stderr
, " (Logged: %s%s%s%s%s%s%s%s)\n",
1747 (msr
& 1 << 16) ? "PROCHOT, " : "",
1748 (msr
& 1 << 17) ? "ThermStatus, " : "",
1749 (msr
& 1 << 20) ? "Graphics, " : "",
1750 (msr
& 1 << 22) ? "VR-Therm, " : "",
1751 (msr
& 1 << 24) ? "Amps, " : "",
1752 (msr
& 1 << 25) ? "GFXPwr, " : "",
1753 (msr
& 1 << 26) ? "PkgPwrL1, " : "",
1754 (msr
& 1 << 27) ? "PkgPwrL2, " : "");
1756 if (do_ring_perf_limit_reasons
) {
1757 get_msr(cpu
, MSR_RING_PERF_LIMIT_REASONS
, &msr
);
1758 fprintf(stderr
, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu
, msr
);
1759 fprintf(stderr
, " (Active: %s%s%s%s%s%s)",
1760 (msr
& 1 << 0) ? "PROCHOT, " : "",
1761 (msr
& 1 << 1) ? "ThermStatus, " : "",
1762 (msr
& 1 << 6) ? "VR-Therm, " : "",
1763 (msr
& 1 << 8) ? "Amps, " : "",
1764 (msr
& 1 << 10) ? "PkgPwrL1, " : "",
1765 (msr
& 1 << 11) ? "PkgPwrL2, " : "");
1766 fprintf(stderr
, " (Logged: %s%s%s%s%s%s)\n",
1767 (msr
& 1 << 16) ? "PROCHOT, " : "",
1768 (msr
& 1 << 17) ? "ThermStatus, " : "",
1769 (msr
& 1 << 22) ? "VR-Therm, " : "",
1770 (msr
& 1 << 24) ? "Amps, " : "",
1771 (msr
& 1 << 26) ? "PkgPwrL1, " : "",
1772 (msr
& 1 << 27) ? "PkgPwrL2, " : "");
1777 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1778 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1780 double get_tdp(model
)
1782 unsigned long long msr
;
1784 if (do_rapl
& RAPL_PKG_POWER_INFO
)
1785 if (!get_msr(0, MSR_PKG_POWER_INFO
, &msr
))
1786 return ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
;
1801 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
1803 void rapl_probe(unsigned int family
, unsigned int model
)
1805 unsigned long long msr
;
1806 unsigned int time_unit
;
1818 case 0x3C: /* HSW */
1819 case 0x45: /* HSW */
1820 case 0x46: /* HSW */
1821 case 0x3D: /* BDW */
1822 case 0x47: /* BDW */
1823 do_rapl
= RAPL_PKG
| RAPL_CORES
| RAPL_CORE_POLICY
| RAPL_GFX
| RAPL_PKG_POWER_INFO
;
1825 case 0x3F: /* HSX */
1826 case 0x4F: /* BDX */
1827 case 0x56: /* BDX-DE */
1828 do_rapl
= RAPL_PKG
| RAPL_DRAM
| RAPL_DRAM_PERF_STATUS
| RAPL_PKG_PERF_STATUS
| RAPL_PKG_POWER_INFO
;
1832 do_rapl
= RAPL_PKG
| RAPL_CORES
| RAPL_CORE_POLICY
| RAPL_DRAM
| RAPL_PKG_PERF_STATUS
| RAPL_DRAM_PERF_STATUS
| RAPL_PKG_POWER_INFO
;
1834 case 0x37: /* BYT */
1835 case 0x4D: /* AVN */
1836 do_rapl
= RAPL_PKG
| RAPL_CORES
;
1842 /* units on package 0, verify later other packages match */
1843 if (get_msr(0, MSR_RAPL_POWER_UNIT
, &msr
))
1846 rapl_power_units
= 1.0 / (1 << (msr
& 0xF));
1848 rapl_energy_units
= 1.0 * (1 << (msr
>> 8 & 0x1F)) / 1000000;
1850 rapl_energy_units
= 1.0 / (1 << (msr
>> 8 & 0x1F));
1852 time_unit
= msr
>> 16 & 0xF;
1856 rapl_time_units
= 1.0 / (1 << (time_unit
));
1858 tdp
= get_tdp(model
);
1860 rapl_joule_counter_range
= 0xFFFFFFFF * rapl_energy_units
/ tdp
;
1862 fprintf(stderr
, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range
, tdp
);
1867 void perf_limit_reasons_probe(family
, model
)
1876 case 0x3C: /* HSW */
1877 case 0x45: /* HSW */
1878 case 0x46: /* HSW */
1879 do_gfx_perf_limit_reasons
= 1;
1880 case 0x3F: /* HSX */
1881 do_core_perf_limit_reasons
= 1;
1882 do_ring_perf_limit_reasons
= 1;
1888 int print_thermal(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1890 unsigned long long msr
;
1894 if (!(do_dts
|| do_ptm
))
1899 /* DTS is per-core, no need to print for each thread */
1900 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
1903 if (cpu_migrate(cpu
)) {
1904 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1908 if (do_ptm
&& (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)) {
1909 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
1912 dts
= (msr
>> 16) & 0x7F;
1913 fprintf(stderr
, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1914 cpu
, msr
, tcc_activation_temp
- dts
);
1917 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_INTERRUPT
, &msr
))
1920 dts
= (msr
>> 16) & 0x7F;
1921 dts2
= (msr
>> 8) & 0x7F;
1922 fprintf(stderr
, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1923 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
1929 unsigned int resolution
;
1931 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
1934 dts
= (msr
>> 16) & 0x7F;
1935 resolution
= (msr
>> 27) & 0xF;
1936 fprintf(stderr
, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1937 cpu
, msr
, tcc_activation_temp
- dts
, resolution
);
1940 if (get_msr(cpu
, MSR_IA32_THERM_INTERRUPT
, &msr
))
1943 dts
= (msr
>> 16) & 0x7F;
1944 dts2
= (msr
>> 8) & 0x7F;
1945 fprintf(stderr
, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1946 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
1953 void print_power_limit_msr(int cpu
, unsigned long long msr
, char *label
)
1955 fprintf(stderr
, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1957 ((msr
>> 15) & 1) ? "EN" : "DIS",
1958 ((msr
>> 0) & 0x7FFF) * rapl_power_units
,
1959 (1.0 + (((msr
>> 22) & 0x3)/4.0)) * (1 << ((msr
>> 17) & 0x1F)) * rapl_time_units
,
1960 (((msr
>> 16) & 1) ? "EN" : "DIS"));
1965 int print_rapl(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1967 unsigned long long msr
;
1973 /* RAPL counters are per package, so print only for 1st thread/package */
1974 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1978 if (cpu_migrate(cpu
)) {
1979 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1983 if (get_msr(cpu
, MSR_RAPL_POWER_UNIT
, &msr
))
1987 fprintf(stderr
, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1988 "(%f Watts, %f Joules, %f sec.)\n", cpu
, msr
,
1989 rapl_power_units
, rapl_energy_units
, rapl_time_units
);
1991 if (do_rapl
& RAPL_PKG_POWER_INFO
) {
1993 if (get_msr(cpu
, MSR_PKG_POWER_INFO
, &msr
))
1997 fprintf(stderr
, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1999 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2000 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2001 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2002 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
2005 if (do_rapl
& RAPL_PKG
) {
2007 if (get_msr(cpu
, MSR_PKG_POWER_LIMIT
, &msr
))
2010 fprintf(stderr
, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2011 cpu
, msr
, (msr
>> 63) & 1 ? "": "UN");
2013 print_power_limit_msr(cpu
, msr
, "PKG Limit #1");
2014 fprintf(stderr
, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2016 ((msr
>> 47) & 1) ? "EN" : "DIS",
2017 ((msr
>> 32) & 0x7FFF) * rapl_power_units
,
2018 (1.0 + (((msr
>> 54) & 0x3)/4.0)) * (1 << ((msr
>> 49) & 0x1F)) * rapl_time_units
,
2019 ((msr
>> 48) & 1) ? "EN" : "DIS");
2022 if (do_rapl
& RAPL_DRAM
) {
2023 if (get_msr(cpu
, MSR_DRAM_POWER_INFO
, &msr
))
2027 fprintf(stderr
, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2029 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2030 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2031 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
2032 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
2035 if (get_msr(cpu
, MSR_DRAM_POWER_LIMIT
, &msr
))
2037 fprintf(stderr
, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2038 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
2040 print_power_limit_msr(cpu
, msr
, "DRAM Limit");
2042 if (do_rapl
& RAPL_CORE_POLICY
) {
2044 if (get_msr(cpu
, MSR_PP0_POLICY
, &msr
))
2047 fprintf(stderr
, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu
, msr
& 0xF);
2050 if (do_rapl
& RAPL_CORES
) {
2053 if (get_msr(cpu
, MSR_PP0_POWER_LIMIT
, &msr
))
2055 fprintf(stderr
, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2056 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
2057 print_power_limit_msr(cpu
, msr
, "Cores Limit");
2060 if (do_rapl
& RAPL_GFX
) {
2062 if (get_msr(cpu
, MSR_PP1_POLICY
, &msr
))
2065 fprintf(stderr
, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu
, msr
& 0xF);
2067 if (get_msr(cpu
, MSR_PP1_POWER_LIMIT
, &msr
))
2069 fprintf(stderr
, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2070 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
2071 print_power_limit_msr(cpu
, msr
, "GFX Limit");
2078 * SNB adds support for additional MSRs:
2080 * MSR_PKG_C7_RESIDENCY 0x000003fa
2081 * MSR_CORE_C7_RESIDENCY 0x000003fe
2082 * MSR_PKG_C2_RESIDENCY 0x0000060d
2085 int has_snb_msrs(unsigned int family
, unsigned int model
)
2093 case 0x3A: /* IVB */
2094 case 0x3E: /* IVB Xeon */
2095 case 0x3C: /* HSW */
2096 case 0x3F: /* HSW */
2097 case 0x45: /* HSW */
2098 case 0x46: /* HSW */
2099 case 0x3D: /* BDW */
2100 case 0x47: /* BDW */
2101 case 0x4F: /* BDX */
2102 case 0x56: /* BDX-DE */
2109 * HSW adds support for additional MSRs:
2111 * MSR_PKG_C8_RESIDENCY 0x00000630
2112 * MSR_PKG_C9_RESIDENCY 0x00000631
2113 * MSR_PKG_C10_RESIDENCY 0x00000632
2115 int has_hsw_msrs(unsigned int family
, unsigned int model
)
2121 case 0x45: /* HSW */
2122 case 0x3D: /* BDW */
2129 int is_slm(unsigned int family
, unsigned int model
)
2134 case 0x37: /* BYT */
2135 case 0x4D: /* AVN */
2141 #define SLM_BCLK_FREQS 5
2142 double slm_freq_table
[SLM_BCLK_FREQS
] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2144 double slm_bclk(void)
2146 unsigned long long msr
= 3;
2150 if (get_msr(0, MSR_FSB_FREQ
, &msr
))
2151 fprintf(stderr
, "SLM BCLK: unknown\n");
2154 if (i
>= SLM_BCLK_FREQS
) {
2155 fprintf(stderr
, "SLM BCLK[%d] invalid\n", i
);
2158 freq
= slm_freq_table
[i
];
2160 fprintf(stderr
, "SLM BCLK: %.1f Mhz\n", freq
);
2165 double discover_bclk(unsigned int family
, unsigned int model
)
2167 if (has_snb_msrs(family
, model
))
2169 else if (is_slm(family
, model
))
2176 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2177 * the Thermal Control Circuit (TCC) activates.
2178 * This is usually equal to tjMax.
2180 * Older processors do not have this MSR, so there we guess,
2181 * but also allow cmdline over-ride with -T.
2183 * Several MSR temperature values are in units of degrees-C
2184 * below this value, including the Digital Thermal Sensor (DTS),
2185 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2187 int set_temperature_target(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
2189 unsigned long long msr
;
2190 unsigned int target_c_local
;
2193 /* tcc_activation_temp is used only for dts or ptm */
2194 if (!(do_dts
|| do_ptm
))
2197 /* this is a per-package concept */
2198 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
2202 if (cpu_migrate(cpu
)) {
2203 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
2207 if (tcc_activation_temp_override
!= 0) {
2208 tcc_activation_temp
= tcc_activation_temp_override
;
2209 fprintf(stderr
, "cpu%d: Using cmdline TCC Target (%d C)\n",
2210 cpu
, tcc_activation_temp
);
2214 /* Temperature Target MSR is Nehalem and newer only */
2215 if (!do_nhm_platform_info
)
2218 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET
, &msr
))
2221 target_c_local
= (msr
>> 16) & 0xFF;
2224 fprintf(stderr
, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2225 cpu
, msr
, target_c_local
);
2227 if (!target_c_local
)
2230 tcc_activation_temp
= target_c_local
;
2235 tcc_activation_temp
= TJMAX_DEFAULT
;
2236 fprintf(stderr
, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2237 cpu
, tcc_activation_temp
);
2243 unsigned int eax
, ebx
, ecx
, edx
, max_level
;
2244 unsigned int fms
, family
, model
, stepping
;
2246 eax
= ebx
= ecx
= edx
= 0;
2248 __get_cpuid(0, &max_level
, &ebx
, &ecx
, &edx
);
2250 if (ebx
== 0x756e6547 && edx
== 0x49656e69 && ecx
== 0x6c65746e)
2254 fprintf(stderr
, "CPUID(0): %.4s%.4s%.4s ",
2255 (char *)&ebx
, (char *)&edx
, (char *)&ecx
);
2257 __get_cpuid(1, &fms
, &ebx
, &ecx
, &edx
);
2258 family
= (fms
>> 8) & 0xf;
2259 model
= (fms
>> 4) & 0xf;
2260 stepping
= fms
& 0xf;
2261 if (family
== 6 || family
== 0xf)
2262 model
+= ((fms
>> 16) & 0xf) << 4;
2265 fprintf(stderr
, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2266 max_level
, family
, model
, stepping
, family
, model
, stepping
);
2268 if (!(edx
& (1 << 5)))
2269 errx(1, "CPUID: no MSR");
2272 * check max extended function levels of CPUID.
2273 * This is needed to check for invariant TSC.
2274 * This check is valid for both Intel and AMD.
2276 ebx
= ecx
= edx
= 0;
2277 __get_cpuid(0x80000000, &max_level
, &ebx
, &ecx
, &edx
);
2279 if (max_level
>= 0x80000007) {
2282 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2283 * this check is valid for both Intel and AMD
2285 __get_cpuid(0x80000007, &eax
, &ebx
, &ecx
, &edx
);
2286 has_invariant_tsc
= edx
& (1 << 8);
2290 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2291 * this check is valid for both Intel and AMD
2294 __get_cpuid(0x6, &eax
, &ebx
, &ecx
, &edx
);
2295 has_aperf
= ecx
& (1 << 0);
2296 do_dts
= eax
& (1 << 0);
2297 do_ptm
= eax
& (1 << 6);
2298 has_epb
= ecx
& (1 << 3);
2301 fprintf(stderr
, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2302 has_aperf
? "" : "No ",
2303 do_dts
? "" : "No ",
2304 do_ptm
? "" : "No ",
2305 has_epb
? "" : "No ");
2307 do_nhm_platform_info
= do_nhm_cstates
= do_smi
= probe_nhm_msrs(family
, model
);
2308 do_snb_cstates
= has_snb_msrs(family
, model
);
2309 do_pc2
= do_snb_cstates
&& (pkg_cstate_limit
>= PCL__2
);
2310 do_pc3
= (pkg_cstate_limit
>= PCL__3
);
2311 do_pc6
= (pkg_cstate_limit
>= PCL__6
);
2312 do_pc7
= do_snb_cstates
&& (pkg_cstate_limit
>= PCL__7
);
2313 do_c8_c9_c10
= has_hsw_msrs(family
, model
);
2314 do_slm_cstates
= is_slm(family
, model
);
2315 bclk
= discover_bclk(family
, model
);
2317 do_nhm_turbo_ratio_limit
= do_nhm_platform_info
&& has_nhm_turbo_ratio_limit(family
, model
);
2318 do_ivt_turbo_ratio_limit
= has_ivt_turbo_ratio_limit(family
, model
);
2319 rapl_probe(family
, model
);
2320 perf_limit_reasons_probe(family
, model
);
2329 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2331 "Turbostat forks the specified COMMAND and prints statistics\n"
2332 "when COMMAND completes.\n"
2333 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2334 "to print statistics, until interrupted.\n"
2335 "--debug run in \"debug\" mode\n"
2336 "--interval sec Override default 5-second measurement interval\n"
2337 "--help print this help message\n"
2338 "--counter msr print 32-bit counter at address \"msr\"\n"
2339 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2340 "--msr msr print 32-bit value at address \"msr\"\n"
2341 "--MSR msr print 64-bit Value at address \"msr\"\n"
2342 "--version print version information\n"
2344 "For more help, run \"man turbostat\"\n");
2349 * in /dev/cpu/ return success for names that are numbers
2350 * ie. filter out ".", "..", "microcode".
2352 int dir_filter(const struct dirent
*dirp
)
2354 if (isdigit(dirp
->d_name
[0]))
2360 int open_dev_cpu_msr(int dummy1
)
2365 void topology_probe()
2368 int max_core_id
= 0;
2369 int max_package_id
= 0;
2370 int max_siblings
= 0;
2371 struct cpu_topology
{
2373 int physical_package_id
;
2376 /* Initialize num_cpus, max_cpu_num */
2378 topo
.max_cpu_num
= 0;
2379 for_all_proc_cpus(count_cpus
);
2380 if (!summary_only
&& topo
.num_cpus
> 1)
2384 fprintf(stderr
, "num_cpus %d max_cpu_num %d\n", topo
.num_cpus
, topo
.max_cpu_num
);
2386 cpus
= calloc(1, (topo
.max_cpu_num
+ 1) * sizeof(struct cpu_topology
));
2388 err(1, "calloc cpus");
2391 * Allocate and initialize cpu_present_set
2393 cpu_present_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
2394 if (cpu_present_set
== NULL
)
2395 err(3, "CPU_ALLOC");
2396 cpu_present_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
2397 CPU_ZERO_S(cpu_present_setsize
, cpu_present_set
);
2398 for_all_proc_cpus(mark_cpu_present
);
2401 * Allocate and initialize cpu_affinity_set
2403 cpu_affinity_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
2404 if (cpu_affinity_set
== NULL
)
2405 err(3, "CPU_ALLOC");
2406 cpu_affinity_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
2407 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
2412 * find max_core_id, max_package_id
2414 for (i
= 0; i
<= topo
.max_cpu_num
; ++i
) {
2417 if (cpu_is_not_present(i
)) {
2419 fprintf(stderr
, "cpu%d NOT PRESENT\n", i
);
2422 cpus
[i
].core_id
= get_core_id(i
);
2423 if (cpus
[i
].core_id
> max_core_id
)
2424 max_core_id
= cpus
[i
].core_id
;
2426 cpus
[i
].physical_package_id
= get_physical_package_id(i
);
2427 if (cpus
[i
].physical_package_id
> max_package_id
)
2428 max_package_id
= cpus
[i
].physical_package_id
;
2430 siblings
= get_num_ht_siblings(i
);
2431 if (siblings
> max_siblings
)
2432 max_siblings
= siblings
;
2434 fprintf(stderr
, "cpu %d pkg %d core %d\n",
2435 i
, cpus
[i
].physical_package_id
, cpus
[i
].core_id
);
2437 topo
.num_cores_per_pkg
= max_core_id
+ 1;
2439 fprintf(stderr
, "max_core_id %d, sizing for %d cores per package\n",
2440 max_core_id
, topo
.num_cores_per_pkg
);
2441 if (debug
&& !summary_only
&& topo
.num_cores_per_pkg
> 1)
2444 topo
.num_packages
= max_package_id
+ 1;
2446 fprintf(stderr
, "max_package_id %d, sizing for %d packages\n",
2447 max_package_id
, topo
.num_packages
);
2448 if (debug
&& !summary_only
&& topo
.num_packages
> 1)
2451 topo
.num_threads_per_core
= max_siblings
;
2453 fprintf(stderr
, "max_siblings %d\n", max_siblings
);
2459 allocate_counters(struct thread_data
**t
, struct core_data
**c
, struct pkg_data
**p
)
2463 *t
= calloc(topo
.num_threads_per_core
* topo
.num_cores_per_pkg
*
2464 topo
.num_packages
, sizeof(struct thread_data
));
2468 for (i
= 0; i
< topo
.num_threads_per_core
*
2469 topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
2470 (*t
)[i
].cpu_id
= -1;
2472 *c
= calloc(topo
.num_cores_per_pkg
* topo
.num_packages
,
2473 sizeof(struct core_data
));
2477 for (i
= 0; i
< topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
2478 (*c
)[i
].core_id
= -1;
2480 *p
= calloc(topo
.num_packages
, sizeof(struct pkg_data
));
2484 for (i
= 0; i
< topo
.num_packages
; i
++)
2485 (*p
)[i
].package_id
= i
;
2489 err(1, "calloc counters");
2494 * set cpu_id, core_num, pkg_num
2495 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2497 * increment topo.num_cores when 1st core in pkg seen
2499 void init_counter(struct thread_data
*thread_base
, struct core_data
*core_base
,
2500 struct pkg_data
*pkg_base
, int thread_num
, int core_num
,
2501 int pkg_num
, int cpu_id
)
2503 struct thread_data
*t
;
2504 struct core_data
*c
;
2507 t
= GET_THREAD(thread_base
, thread_num
, core_num
, pkg_num
);
2508 c
= GET_CORE(core_base
, core_num
, pkg_num
);
2509 p
= GET_PKG(pkg_base
, pkg_num
);
2512 if (thread_num
== 0) {
2513 t
->flags
|= CPU_IS_FIRST_THREAD_IN_CORE
;
2514 if (cpu_is_first_core_in_package(cpu_id
))
2515 t
->flags
|= CPU_IS_FIRST_CORE_IN_PACKAGE
;
2518 c
->core_id
= core_num
;
2519 p
->package_id
= pkg_num
;
2523 int initialize_counters(int cpu_id
)
2525 int my_thread_id
, my_core_id
, my_package_id
;
2527 my_package_id
= get_physical_package_id(cpu_id
);
2528 my_core_id
= get_core_id(cpu_id
);
2530 if (cpu_is_first_sibling_in_core(cpu_id
)) {
2537 init_counter(EVEN_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
2538 init_counter(ODD_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
2542 void allocate_output_buffer()
2544 output_buffer
= calloc(1, (1 + topo
.num_cpus
) * 1024);
2545 outp
= output_buffer
;
2547 err(-1, "calloc output buffer");
2550 void setup_all_buffers(void)
2553 allocate_counters(&thread_even
, &core_even
, &package_even
);
2554 allocate_counters(&thread_odd
, &core_odd
, &package_odd
);
2555 allocate_output_buffer();
2556 for_all_proc_cpus(initialize_counters
);
2559 void turbostat_init()
2562 check_permissions();
2565 setup_all_buffers();
2568 dump_system_config_info();
2571 for_all_cpus(print_epb
, ODD_COUNTERS
);
2574 for_all_cpus(print_perf_limit
, ODD_COUNTERS
);
2577 for_all_cpus(print_rapl
, ODD_COUNTERS
);
2579 for_all_cpus(set_temperature_target
, ODD_COUNTERS
);
2582 for_all_cpus(print_thermal
, ODD_COUNTERS
);
2585 int fork_it(char **argv
)
2590 status
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
2593 /* clear affinity side-effect of get_counters() */
2594 sched_setaffinity(0, cpu_present_setsize
, cpu_present_set
);
2595 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
2600 execvp(argv
[0], argv
);
2604 if (child_pid
== -1)
2607 signal(SIGINT
, SIG_IGN
);
2608 signal(SIGQUIT
, SIG_IGN
);
2609 if (waitpid(child_pid
, &status
, 0) == -1)
2610 err(status
, "waitpid");
2613 * n.b. fork_it() does not check for errors from for_all_cpus()
2614 * because re-starting is problematic when forking
2616 for_all_cpus(get_counters
, ODD_COUNTERS
);
2617 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
2618 timersub(&tv_odd
, &tv_even
, &tv_delta
);
2619 for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
);
2620 compute_average(EVEN_COUNTERS
);
2621 format_all_counters(EVEN_COUNTERS
);
2624 fprintf(stderr
, "%.6f sec\n", tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0);
2629 int get_and_dump_counters(void)
2633 status
= for_all_cpus(get_counters
, ODD_COUNTERS
);
2637 status
= for_all_cpus(dump_counters
, ODD_COUNTERS
);
2646 void print_version() {
2647 fprintf(stderr
, "turbostat version 4.1 10-Feb, 2015"
2648 " - Len Brown <lenb@kernel.org>\n");
2651 void cmdline(int argc
, char **argv
)
2654 int option_index
= 0;
2655 static struct option long_options
[] = {
2656 {"Counter", required_argument
, 0, 'C'},
2657 {"counter", required_argument
, 0, 'c'},
2658 {"Dump", no_argument
, 0, 'D'},
2659 {"debug", no_argument
, 0, 'd'},
2660 {"interval", required_argument
, 0, 'i'},
2661 {"help", no_argument
, 0, 'h'},
2662 {"Joules", no_argument
, 0, 'J'},
2663 {"MSR", required_argument
, 0, 'M'},
2664 {"msr", required_argument
, 0, 'm'},
2665 {"Package", no_argument
, 0, 'p'},
2666 {"processor", no_argument
, 0, 'p'},
2667 {"Summary", no_argument
, 0, 'S'},
2668 {"TCC", required_argument
, 0, 'T'},
2669 {"version", no_argument
, 0, 'v' },
2675 while ((opt
= getopt_long_only(argc
, argv
, "C:c:Ddhi:JM:m:PpST:v",
2676 long_options
, &option_index
)) != -1) {
2679 sscanf(optarg
, "%x", &extra_delta_offset64
);
2682 sscanf(optarg
, "%x", &extra_delta_offset32
);
2695 interval_sec
= atoi(optarg
);
2701 sscanf(optarg
, "%x", &extra_msr_offset64
);
2704 sscanf(optarg
, "%x", &extra_msr_offset32
);
2716 tcc_activation_temp_override
= atoi(optarg
);
2726 int main(int argc
, char **argv
)
2728 cmdline(argc
, argv
);
2735 /* dump counters and exit */
2737 return get_and_dump_counters();
2740 * if any params left, it must be a command to fork
2743 return fork_it(argv
+ optind
);