2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
5 * Copyright (c) 2012 Intel Corporation.
6 * Len Brown <len.brown@intel.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
29 #include <sys/resource.h>
39 char *proc_stat
= "/proc/stat";
40 unsigned int interval_sec
= 5; /* set with -i interval_sec */
41 unsigned int verbose
; /* set with -v */
42 unsigned int rapl_verbose
; /* set with -R */
43 unsigned int thermal_verbose
; /* set with -T */
44 unsigned int summary_only
; /* set with -s */
47 unsigned int do_nhm_cstates
;
48 unsigned int do_snb_cstates
;
49 unsigned int has_aperf
;
51 unsigned int units
= 1000000000; /* Ghz etc */
52 unsigned int genuine_intel
;
53 unsigned int has_invariant_tsc
;
54 unsigned int do_nehalem_platform_info
;
55 unsigned int do_nehalem_turbo_ratio_limit
;
56 unsigned int do_ivt_turbo_ratio_limit
;
57 unsigned int extra_msr_offset32
;
58 unsigned int extra_msr_offset64
;
59 unsigned int extra_delta_offset32
;
60 unsigned int extra_delta_offset64
;
63 unsigned int show_pkg
;
64 unsigned int show_core
;
65 unsigned int show_cpu
;
66 unsigned int show_pkg_only
;
67 unsigned int show_core_only
;
68 char *output_buffer
, *outp
;
72 unsigned int tcc_activation_temp
;
73 unsigned int tcc_activation_temp_override
;
74 double rapl_power_units
, rapl_energy_units
, rapl_time_units
;
75 double rapl_joule_counter_range
;
77 #define RAPL_PKG (1 << 0)
78 #define RAPL_CORES (1 << 1)
79 #define RAPL_GFX (1 << 2)
80 #define RAPL_DRAM (1 << 3)
81 #define RAPL_PKG_PERF_STATUS (1 << 4)
82 #define RAPL_DRAM_PERF_STATUS (1 << 5)
83 #define TJMAX_DEFAULT 100
85 #define MAX(a, b) ((a) > (b) ? (a) : (b))
87 int aperf_mperf_unstable
;
91 cpu_set_t
*cpu_present_set
, *cpu_affinity_set
;
92 size_t cpu_present_setsize
, cpu_affinity_setsize
;
95 unsigned long long tsc
;
96 unsigned long long aperf
;
97 unsigned long long mperf
;
98 unsigned long long c1
; /* derived */
99 unsigned long long extra_msr64
;
100 unsigned long long extra_delta64
;
101 unsigned long long extra_msr32
;
102 unsigned long long extra_delta32
;
103 unsigned int smi_count
;
106 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
107 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
108 } *thread_even
, *thread_odd
;
111 unsigned long long c3
;
112 unsigned long long c6
;
113 unsigned long long c7
;
114 unsigned int core_temp_c
;
115 unsigned int core_id
;
116 } *core_even
, *core_odd
;
119 unsigned long long pc2
;
120 unsigned long long pc3
;
121 unsigned long long pc6
;
122 unsigned long long pc7
;
123 unsigned int package_id
;
124 unsigned int energy_pkg
; /* MSR_PKG_ENERGY_STATUS */
125 unsigned int energy_dram
; /* MSR_DRAM_ENERGY_STATUS */
126 unsigned int energy_cores
; /* MSR_PP0_ENERGY_STATUS */
127 unsigned int energy_gfx
; /* MSR_PP1_ENERGY_STATUS */
128 unsigned int rapl_pkg_perf_status
; /* MSR_PKG_PERF_STATUS */
129 unsigned int rapl_dram_perf_status
; /* MSR_DRAM_PERF_STATUS */
130 unsigned int pkg_temp_c
;
132 } *package_even
, *package_odd
;
134 #define ODD_COUNTERS thread_odd, core_odd, package_odd
135 #define EVEN_COUNTERS thread_even, core_even, package_even
137 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
138 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
139 topo.num_threads_per_core + \
140 (core_no) * topo.num_threads_per_core + (thread_no))
141 #define GET_CORE(core_base, core_no, pkg_no) \
142 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
143 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
145 struct system_summary
{
146 struct thread_data threads
;
147 struct core_data cores
;
148 struct pkg_data packages
;
157 int num_cores_per_pkg
;
158 int num_threads_per_core
;
161 struct timeval tv_even
, tv_odd
, tv_delta
;
163 void setup_all_buffers(void);
165 int cpu_is_not_present(int cpu
)
167 return !CPU_ISSET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
170 * run func(thread, core, package) in topology order
171 * skip non-present cpus
174 int for_all_cpus(int (func
)(struct thread_data
*, struct core_data
*, struct pkg_data
*),
175 struct thread_data
*thread_base
, struct core_data
*core_base
, struct pkg_data
*pkg_base
)
177 int retval
, pkg_no
, core_no
, thread_no
;
179 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
180 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
181 for (thread_no
= 0; thread_no
<
182 topo
.num_threads_per_core
; ++thread_no
) {
183 struct thread_data
*t
;
187 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
189 if (cpu_is_not_present(t
->cpu_id
))
192 c
= GET_CORE(core_base
, core_no
, pkg_no
);
193 p
= GET_PKG(pkg_base
, pkg_no
);
195 retval
= func(t
, c
, p
);
204 int cpu_migrate(int cpu
)
206 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
207 CPU_SET_S(cpu
, cpu_affinity_setsize
, cpu_affinity_set
);
208 if (sched_setaffinity(0, cpu_affinity_setsize
, cpu_affinity_set
) == -1)
214 int get_msr(int cpu
, off_t offset
, unsigned long long *msr
)
220 sprintf(pathname
, "/dev/cpu/%d/msr", cpu
);
221 fd
= open(pathname
, O_RDONLY
);
225 retval
= pread(fd
, msr
, sizeof *msr
, offset
);
228 if (retval
!= sizeof *msr
) {
229 fprintf(stderr
, "%s offset 0x%zx read failed\n", pathname
, offset
);
236 void print_header(void)
239 outp
+= sprintf(outp
, "pk");
241 outp
+= sprintf(outp
, " ");
243 outp
+= sprintf(outp
, "cor");
245 outp
+= sprintf(outp
, " CPU");
246 if (show_pkg
|| show_core
|| show_cpu
)
247 outp
+= sprintf(outp
, " ");
249 outp
+= sprintf(outp
, " %%c0");
251 outp
+= sprintf(outp
, " GHz");
252 outp
+= sprintf(outp
, " TSC");
254 outp
+= sprintf(outp
, " SMI");
255 if (extra_delta_offset32
)
256 outp
+= sprintf(outp
, " count 0x%03X", extra_delta_offset32
);
257 if (extra_delta_offset64
)
258 outp
+= sprintf(outp
, " COUNT 0x%03X", extra_delta_offset64
);
259 if (extra_msr_offset32
)
260 outp
+= sprintf(outp
, " MSR 0x%03X", extra_msr_offset32
);
261 if (extra_msr_offset64
)
262 outp
+= sprintf(outp
, " MSR 0x%03X", extra_msr_offset64
);
264 outp
+= sprintf(outp
, " %%c1");
266 outp
+= sprintf(outp
, " %%c3");
268 outp
+= sprintf(outp
, " %%c6");
270 outp
+= sprintf(outp
, " %%c7");
273 outp
+= sprintf(outp
, " CTMP");
275 outp
+= sprintf(outp
, " PTMP");
278 outp
+= sprintf(outp
, " %%pc2");
280 outp
+= sprintf(outp
, " %%pc3");
282 outp
+= sprintf(outp
, " %%pc6");
284 outp
+= sprintf(outp
, " %%pc7");
286 if (do_rapl
& RAPL_PKG
)
287 outp
+= sprintf(outp
, " Pkg_W");
288 if (do_rapl
& RAPL_CORES
)
289 outp
+= sprintf(outp
, " Cor_W");
290 if (do_rapl
& RAPL_GFX
)
291 outp
+= sprintf(outp
, " GFX_W");
292 if (do_rapl
& RAPL_DRAM
)
293 outp
+= sprintf(outp
, " RAM_W");
294 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
295 outp
+= sprintf(outp
, " PKG_%%");
296 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
297 outp
+= sprintf(outp
, " RAM_%%");
299 outp
+= sprintf(outp
, "\n");
302 int dump_counters(struct thread_data
*t
, struct core_data
*c
,
305 fprintf(stderr
, "t %p, c %p, p %p\n", t
, c
, p
);
308 fprintf(stderr
, "CPU: %d flags 0x%x\n", t
->cpu_id
, t
->flags
);
309 fprintf(stderr
, "TSC: %016llX\n", t
->tsc
);
310 fprintf(stderr
, "aperf: %016llX\n", t
->aperf
);
311 fprintf(stderr
, "mperf: %016llX\n", t
->mperf
);
312 fprintf(stderr
, "c1: %016llX\n", t
->c1
);
313 fprintf(stderr
, "msr0x%x: %08llX\n",
314 extra_delta_offset32
, t
->extra_delta32
);
315 fprintf(stderr
, "msr0x%x: %016llX\n",
316 extra_delta_offset64
, t
->extra_delta64
);
317 fprintf(stderr
, "msr0x%x: %08llX\n",
318 extra_msr_offset32
, t
->extra_msr32
);
319 fprintf(stderr
, "msr0x%x: %016llX\n",
320 extra_msr_offset64
, t
->extra_msr64
);
322 fprintf(stderr
, "SMI: %08X\n", t
->smi_count
);
326 fprintf(stderr
, "core: %d\n", c
->core_id
);
327 fprintf(stderr
, "c3: %016llX\n", c
->c3
);
328 fprintf(stderr
, "c6: %016llX\n", c
->c6
);
329 fprintf(stderr
, "c7: %016llX\n", c
->c7
);
330 fprintf(stderr
, "DTS: %dC\n", c
->core_temp_c
);
334 fprintf(stderr
, "package: %d\n", p
->package_id
);
335 fprintf(stderr
, "pc2: %016llX\n", p
->pc2
);
336 fprintf(stderr
, "pc3: %016llX\n", p
->pc3
);
337 fprintf(stderr
, "pc6: %016llX\n", p
->pc6
);
338 fprintf(stderr
, "pc7: %016llX\n", p
->pc7
);
339 fprintf(stderr
, "Joules PKG: %0X\n", p
->energy_pkg
);
340 fprintf(stderr
, "Joules COR: %0X\n", p
->energy_cores
);
341 fprintf(stderr
, "Joules GFX: %0X\n", p
->energy_gfx
);
342 fprintf(stderr
, "Joules RAM: %0X\n", p
->energy_dram
);
343 fprintf(stderr
, "Throttle PKG: %0X\n", p
->rapl_pkg_perf_status
);
344 fprintf(stderr
, "Throttle RAM: %0X\n", p
->rapl_dram_perf_status
);
345 fprintf(stderr
, "PTM: %dC\n", p
->pkg_temp_c
);
351 * column formatting convention & formats
352 * package: "pk" 2 columns %2d
353 * core: "cor" 3 columns %3d
354 * CPU: "CPU" 3 columns %3d
359 * GHz: "GHz" 3 columns %3.2
360 * TSC: "TSC" 3 columns %3.2
361 * SMI: "SMI" 4 columns %4d
362 * percentage " %pc3" %6.2
363 * Perf Status percentage: %5.2
364 * "CTMP" 4 columns %4d
366 int format_counters(struct thread_data
*t
, struct core_data
*c
,
369 double interval_float
;
372 /* if showing only 1st thread in core and this isn't one, bail out */
373 if (show_core_only
&& !(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
376 /* if showing only 1st thread in pkg and this isn't one, bail out */
377 if (show_pkg_only
&& !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
380 interval_float
= tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0;
382 /* topo columns, print blanks on 1st (average) line */
383 if (t
== &average
.threads
) {
385 outp
+= sprintf(outp
, " ");
386 if (show_pkg
&& show_core
)
387 outp
+= sprintf(outp
, " ");
389 outp
+= sprintf(outp
, " ");
391 outp
+= sprintf(outp
, " " " ");
395 outp
+= sprintf(outp
, "%2d", p
->package_id
);
397 outp
+= sprintf(outp
, " ");
399 if (show_pkg
&& show_core
)
400 outp
+= sprintf(outp
, " ");
403 outp
+= sprintf(outp
, "%3d", c
->core_id
);
405 outp
+= sprintf(outp
, " ");
408 outp
+= sprintf(outp
, " %3d", t
->cpu_id
);
411 if (do_nhm_cstates
) {
412 if (show_pkg
|| show_core
|| show_cpu
)
413 outp
+= sprintf(outp
, " ");
415 outp
+= sprintf(outp
, "%6.2f", 100.0 * t
->mperf
/t
->tsc
);
417 outp
+= sprintf(outp
, " ****");
422 if (!aperf_mperf_unstable
) {
423 outp
+= sprintf(outp
, " %3.2f",
424 1.0 * t
->tsc
/ units
* t
->aperf
/
425 t
->mperf
/ interval_float
);
427 if (t
->aperf
> t
->tsc
|| t
->mperf
> t
->tsc
) {
428 outp
+= sprintf(outp
, " ***");
430 outp
+= sprintf(outp
, "%3.1f*",
433 t
->mperf
/ interval_float
);
439 outp
+= sprintf(outp
, "%5.2f", 1.0 * t
->tsc
/units
/interval_float
);
443 outp
+= sprintf(outp
, "%4d", t
->smi_count
);
446 if (extra_delta_offset32
)
447 outp
+= sprintf(outp
, " %11llu", t
->extra_delta32
);
450 if (extra_delta_offset64
)
451 outp
+= sprintf(outp
, " %11llu", t
->extra_delta64
);
453 if (extra_msr_offset32
)
454 outp
+= sprintf(outp
, " 0x%08llx", t
->extra_msr32
);
457 if (extra_msr_offset64
)
458 outp
+= sprintf(outp
, " 0x%016llx", t
->extra_msr64
);
460 if (do_nhm_cstates
) {
462 outp
+= sprintf(outp
, " %6.2f", 100.0 * t
->c1
/t
->tsc
);
464 outp
+= sprintf(outp
, " ****");
467 /* print per-core data only for 1st thread in core */
468 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
472 outp
+= sprintf(outp
, " %6.2f", 100.0 * c
->c3
/t
->tsc
);
474 outp
+= sprintf(outp
, " %6.2f", 100.0 * c
->c6
/t
->tsc
);
476 outp
+= sprintf(outp
, " %6.2f", 100.0 * c
->c7
/t
->tsc
);
479 outp
+= sprintf(outp
, " %4d", c
->core_temp_c
);
481 /* print per-package data only for 1st core in package */
482 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
486 outp
+= sprintf(outp
, " %4d", p
->pkg_temp_c
);
489 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc2
/t
->tsc
);
491 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc3
/t
->tsc
);
493 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc6
/t
->tsc
);
495 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc7
/t
->tsc
);
498 * If measurement interval exceeds minimum RAPL Joule Counter range,
499 * indicate that results are suspect by printing "**" in fraction place.
501 if (interval_float
< rapl_joule_counter_range
) {
509 if (do_rapl
& RAPL_PKG
)
510 outp
+= sprintf(outp
, fmt6
, p
->energy_pkg
* rapl_energy_units
/ interval_float
);
511 if (do_rapl
& RAPL_CORES
)
512 outp
+= sprintf(outp
, fmt6
, p
->energy_cores
* rapl_energy_units
/ interval_float
);
513 if (do_rapl
& RAPL_GFX
)
514 outp
+= sprintf(outp
, fmt5
, p
->energy_gfx
* rapl_energy_units
/ interval_float
);
515 if (do_rapl
& RAPL_DRAM
)
516 outp
+= sprintf(outp
, fmt5
, p
->energy_dram
* rapl_energy_units
/ interval_float
);
517 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
518 outp
+= sprintf(outp
, fmt5
, 100.0 * p
->rapl_pkg_perf_status
* rapl_time_units
/ interval_float
);
519 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
520 outp
+= sprintf(outp
, fmt5
, 100.0 * p
->rapl_dram_perf_status
* rapl_time_units
/ interval_float
);
523 outp
+= sprintf(outp
, "\n");
530 fputs(output_buffer
, stdout
);
532 outp
= output_buffer
;
536 fputs(output_buffer
, stderr
);
537 outp
= output_buffer
;
539 void format_all_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
543 if (!printed
|| !summary_only
)
546 if (topo
.num_cpus
> 1)
547 format_counters(&average
.threads
, &average
.cores
,
555 for_all_cpus(format_counters
, t
, c
, p
);
558 #define DELTA_WRAP32(new, old) \
562 old = 0x100000000 + new - old; \
566 delta_package(struct pkg_data
*new, struct pkg_data
*old
)
568 old
->pc2
= new->pc2
- old
->pc2
;
569 old
->pc3
= new->pc3
- old
->pc3
;
570 old
->pc6
= new->pc6
- old
->pc6
;
571 old
->pc7
= new->pc7
- old
->pc7
;
572 old
->pkg_temp_c
= new->pkg_temp_c
;
574 DELTA_WRAP32(new->energy_pkg
, old
->energy_pkg
);
575 DELTA_WRAP32(new->energy_cores
, old
->energy_cores
);
576 DELTA_WRAP32(new->energy_gfx
, old
->energy_gfx
);
577 DELTA_WRAP32(new->energy_dram
, old
->energy_dram
);
578 DELTA_WRAP32(new->rapl_pkg_perf_status
, old
->rapl_pkg_perf_status
);
579 DELTA_WRAP32(new->rapl_dram_perf_status
, old
->rapl_dram_perf_status
);
583 delta_core(struct core_data
*new, struct core_data
*old
)
585 old
->c3
= new->c3
- old
->c3
;
586 old
->c6
= new->c6
- old
->c6
;
587 old
->c7
= new->c7
- old
->c7
;
588 old
->core_temp_c
= new->core_temp_c
;
595 delta_thread(struct thread_data
*new, struct thread_data
*old
,
596 struct core_data
*core_delta
)
598 old
->tsc
= new->tsc
- old
->tsc
;
600 /* check for TSC < 1 Mcycles over interval */
601 if (old
->tsc
< (1000 * 1000)) {
602 fprintf(stderr
, "Insanely slow TSC rate, TSC stops in idle?\n");
603 fprintf(stderr
, "You can disable all c-states by booting with \"idle=poll\"\n");
604 fprintf(stderr
, "or just the deep ones with \"processor.max_cstate=1\"\n");
608 old
->c1
= new->c1
- old
->c1
;
610 if ((new->aperf
> old
->aperf
) && (new->mperf
> old
->mperf
)) {
611 old
->aperf
= new->aperf
- old
->aperf
;
612 old
->mperf
= new->mperf
- old
->mperf
;
615 if (!aperf_mperf_unstable
) {
616 fprintf(stderr
, "%s: APERF or MPERF went backwards *\n", progname
);
617 fprintf(stderr
, "* Frequency results do not cover entire interval *\n");
618 fprintf(stderr
, "* fix this by running Linux-2.6.30 or later *\n");
620 aperf_mperf_unstable
= 1;
623 * mperf delta is likely a huge "positive" number
624 * can not use it for calculating c0 time
632 * As counter collection is not atomic,
633 * it is possible for mperf's non-halted cycles + idle states
634 * to exceed TSC's all cycles: show c1 = 0% in that case.
636 if ((old
->mperf
+ core_delta
->c3
+ core_delta
->c6
+ core_delta
->c7
) > old
->tsc
)
639 /* normal case, derive c1 */
640 old
->c1
= old
->tsc
- old
->mperf
- core_delta
->c3
641 - core_delta
->c6
- core_delta
->c7
;
644 if (old
->mperf
== 0) {
645 if (verbose
> 1) fprintf(stderr
, "cpu%d MPERF 0!\n", old
->cpu_id
);
646 old
->mperf
= 1; /* divide by 0 protection */
649 old
->extra_delta32
= new->extra_delta32
- old
->extra_delta32
;
650 old
->extra_delta32
&= 0xFFFFFFFF;
652 old
->extra_delta64
= new->extra_delta64
- old
->extra_delta64
;
655 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
657 old
->extra_msr32
= new->extra_msr32
;
658 old
->extra_msr64
= new->extra_msr64
;
661 old
->smi_count
= new->smi_count
- old
->smi_count
;
664 int delta_cpu(struct thread_data
*t
, struct core_data
*c
,
665 struct pkg_data
*p
, struct thread_data
*t2
,
666 struct core_data
*c2
, struct pkg_data
*p2
)
668 /* calculate core delta only for 1st thread in core */
669 if (t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
)
672 /* always calculate thread delta */
673 delta_thread(t
, t2
, c2
); /* c2 is core delta */
675 /* calculate package delta only for 1st core in package */
676 if (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)
677 delta_package(p
, p2
);
682 void clear_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
690 t
->extra_delta32
= 0;
691 t
->extra_delta64
= 0;
693 /* tells format_counters to dump all fields from this set */
694 t
->flags
= CPU_IS_FIRST_THREAD_IN_CORE
| CPU_IS_FIRST_CORE_IN_PACKAGE
;
710 p
->rapl_pkg_perf_status
= 0;
711 p
->rapl_dram_perf_status
= 0;
714 int sum_counters(struct thread_data
*t
, struct core_data
*c
,
717 average
.threads
.tsc
+= t
->tsc
;
718 average
.threads
.aperf
+= t
->aperf
;
719 average
.threads
.mperf
+= t
->mperf
;
720 average
.threads
.c1
+= t
->c1
;
722 average
.threads
.extra_delta32
+= t
->extra_delta32
;
723 average
.threads
.extra_delta64
+= t
->extra_delta64
;
725 /* sum per-core values only for 1st thread in core */
726 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
729 average
.cores
.c3
+= c
->c3
;
730 average
.cores
.c6
+= c
->c6
;
731 average
.cores
.c7
+= c
->c7
;
733 average
.cores
.core_temp_c
= MAX(average
.cores
.core_temp_c
, c
->core_temp_c
);
735 /* sum per-pkg values only for 1st core in pkg */
736 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
739 average
.packages
.pc2
+= p
->pc2
;
740 average
.packages
.pc3
+= p
->pc3
;
741 average
.packages
.pc6
+= p
->pc6
;
742 average
.packages
.pc7
+= p
->pc7
;
744 average
.packages
.energy_pkg
+= p
->energy_pkg
;
745 average
.packages
.energy_dram
+= p
->energy_dram
;
746 average
.packages
.energy_cores
+= p
->energy_cores
;
747 average
.packages
.energy_gfx
+= p
->energy_gfx
;
749 average
.packages
.pkg_temp_c
= MAX(average
.packages
.pkg_temp_c
, p
->pkg_temp_c
);
751 average
.packages
.rapl_pkg_perf_status
+= p
->rapl_pkg_perf_status
;
752 average
.packages
.rapl_dram_perf_status
+= p
->rapl_dram_perf_status
;
756 * sum the counters for all cpus in the system
757 * compute the weighted average
759 void compute_average(struct thread_data
*t
, struct core_data
*c
,
762 clear_counters(&average
.threads
, &average
.cores
, &average
.packages
);
764 for_all_cpus(sum_counters
, t
, c
, p
);
766 average
.threads
.tsc
/= topo
.num_cpus
;
767 average
.threads
.aperf
/= topo
.num_cpus
;
768 average
.threads
.mperf
/= topo
.num_cpus
;
769 average
.threads
.c1
/= topo
.num_cpus
;
771 average
.threads
.extra_delta32
/= topo
.num_cpus
;
772 average
.threads
.extra_delta32
&= 0xFFFFFFFF;
774 average
.threads
.extra_delta64
/= topo
.num_cpus
;
776 average
.cores
.c3
/= topo
.num_cores
;
777 average
.cores
.c6
/= topo
.num_cores
;
778 average
.cores
.c7
/= topo
.num_cores
;
780 average
.packages
.pc2
/= topo
.num_packages
;
781 average
.packages
.pc3
/= topo
.num_packages
;
782 average
.packages
.pc6
/= topo
.num_packages
;
783 average
.packages
.pc7
/= topo
.num_packages
;
786 static unsigned long long rdtsc(void)
788 unsigned int low
, high
;
790 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
792 return low
| ((unsigned long long)high
) << 32;
799 * acquire and record local counters for that cpu
801 int get_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
804 unsigned long long msr
;
806 if (cpu_migrate(cpu
)) {
807 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
811 t
->tsc
= rdtsc(); /* we are running on local CPU of interest */
814 if (get_msr(cpu
, MSR_IA32_APERF
, &t
->aperf
))
816 if (get_msr(cpu
, MSR_IA32_MPERF
, &t
->mperf
))
821 if (get_msr(cpu
, MSR_SMI_COUNT
, &msr
))
823 t
->smi_count
= msr
& 0xFFFFFFFF;
825 if (extra_delta_offset32
) {
826 if (get_msr(cpu
, extra_delta_offset32
, &msr
))
828 t
->extra_delta32
= msr
& 0xFFFFFFFF;
831 if (extra_delta_offset64
)
832 if (get_msr(cpu
, extra_delta_offset64
, &t
->extra_delta64
))
835 if (extra_msr_offset32
) {
836 if (get_msr(cpu
, extra_msr_offset32
, &msr
))
838 t
->extra_msr32
= msr
& 0xFFFFFFFF;
841 if (extra_msr_offset64
)
842 if (get_msr(cpu
, extra_msr_offset64
, &t
->extra_msr64
))
845 /* collect core counters only for 1st thread in core */
846 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
849 if (do_nhm_cstates
) {
850 if (get_msr(cpu
, MSR_CORE_C3_RESIDENCY
, &c
->c3
))
852 if (get_msr(cpu
, MSR_CORE_C6_RESIDENCY
, &c
->c6
))
857 if (get_msr(cpu
, MSR_CORE_C7_RESIDENCY
, &c
->c7
))
861 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
863 c
->core_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
867 /* collect package counters only for 1st core in package */
868 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
871 if (do_nhm_cstates
) {
872 if (get_msr(cpu
, MSR_PKG_C3_RESIDENCY
, &p
->pc3
))
874 if (get_msr(cpu
, MSR_PKG_C6_RESIDENCY
, &p
->pc6
))
877 if (do_snb_cstates
) {
878 if (get_msr(cpu
, MSR_PKG_C2_RESIDENCY
, &p
->pc2
))
880 if (get_msr(cpu
, MSR_PKG_C7_RESIDENCY
, &p
->pc7
))
883 if (do_rapl
& RAPL_PKG
) {
884 if (get_msr(cpu
, MSR_PKG_ENERGY_STATUS
, &msr
))
886 p
->energy_pkg
= msr
& 0xFFFFFFFF;
888 if (do_rapl
& RAPL_CORES
) {
889 if (get_msr(cpu
, MSR_PP0_ENERGY_STATUS
, &msr
))
891 p
->energy_cores
= msr
& 0xFFFFFFFF;
893 if (do_rapl
& RAPL_DRAM
) {
894 if (get_msr(cpu
, MSR_DRAM_ENERGY_STATUS
, &msr
))
896 p
->energy_dram
= msr
& 0xFFFFFFFF;
898 if (do_rapl
& RAPL_GFX
) {
899 if (get_msr(cpu
, MSR_PP1_ENERGY_STATUS
, &msr
))
901 p
->energy_gfx
= msr
& 0xFFFFFFFF;
903 if (do_rapl
& RAPL_PKG_PERF_STATUS
) {
904 if (get_msr(cpu
, MSR_PKG_PERF_STATUS
, &msr
))
906 p
->rapl_pkg_perf_status
= msr
& 0xFFFFFFFF;
908 if (do_rapl
& RAPL_DRAM_PERF_STATUS
) {
909 if (get_msr(cpu
, MSR_DRAM_PERF_STATUS
, &msr
))
911 p
->rapl_dram_perf_status
= msr
& 0xFFFFFFFF;
914 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
916 p
->pkg_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
921 void print_verbose_header(void)
923 unsigned long long msr
;
926 if (!do_nehalem_platform_info
)
929 get_msr(0, MSR_NHM_PLATFORM_INFO
, &msr
);
931 fprintf(stderr
, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr
);
933 ratio
= (msr
>> 40) & 0xFF;
934 fprintf(stderr
, "%d * %.0f = %.0f MHz max efficiency\n",
935 ratio
, bclk
, ratio
* bclk
);
937 ratio
= (msr
>> 8) & 0xFF;
938 fprintf(stderr
, "%d * %.0f = %.0f MHz TSC frequency\n",
939 ratio
, bclk
, ratio
* bclk
);
941 get_msr(0, MSR_IA32_POWER_CTL
, &msr
);
942 fprintf(stderr
, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E: %sabled)\n",
943 msr
, msr
& 0x2 ? "EN" : "DIS");
945 if (!do_ivt_turbo_ratio_limit
)
946 goto print_nhm_turbo_ratio_limits
;
948 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT
, &msr
);
950 fprintf(stderr
, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr
);
952 ratio
= (msr
>> 56) & 0xFF;
954 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
955 ratio
, bclk
, ratio
* bclk
);
957 ratio
= (msr
>> 48) & 0xFF;
959 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
960 ratio
, bclk
, ratio
* bclk
);
962 ratio
= (msr
>> 40) & 0xFF;
964 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
965 ratio
, bclk
, ratio
* bclk
);
967 ratio
= (msr
>> 32) & 0xFF;
969 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
970 ratio
, bclk
, ratio
* bclk
);
972 ratio
= (msr
>> 24) & 0xFF;
974 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
975 ratio
, bclk
, ratio
* bclk
);
977 ratio
= (msr
>> 16) & 0xFF;
979 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
980 ratio
, bclk
, ratio
* bclk
);
982 ratio
= (msr
>> 8) & 0xFF;
984 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
985 ratio
, bclk
, ratio
* bclk
);
987 ratio
= (msr
>> 0) & 0xFF;
989 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
990 ratio
, bclk
, ratio
* bclk
);
992 print_nhm_turbo_ratio_limits
:
993 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL
, &msr
);
995 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
996 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
998 fprintf(stderr
, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr
);
1000 fprintf(stderr
, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1001 (msr
& SNB_C3_AUTO_UNDEMOTE
) ? "UNdemote-C3, " : "",
1002 (msr
& SNB_C1_AUTO_UNDEMOTE
) ? "UNdemote-C1, " : "",
1003 (msr
& NHM_C3_AUTO_DEMOTE
) ? "demote-C3, " : "",
1004 (msr
& NHM_C1_AUTO_DEMOTE
) ? "demote-C1, " : "",
1005 (msr
& (1 << 15)) ? "" : "UN",
1006 (unsigned int)msr
& 7);
1011 fprintf(stderr
, "pc0");
1014 fprintf(stderr
, do_snb_cstates
? "pc2" : "pc0");
1017 fprintf(stderr
, do_snb_cstates
? "pc6-noret" : "pc3");
1020 fprintf(stderr
, "pc6");
1023 fprintf(stderr
, "pc7");
1026 fprintf(stderr
, do_snb_cstates
? "pc7s" : "invalid");
1029 fprintf(stderr
, "unlimited");
1032 fprintf(stderr
, "invalid");
1034 fprintf(stderr
, ")\n");
1036 if (!do_nehalem_turbo_ratio_limit
)
1039 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT
, &msr
);
1041 fprintf(stderr
, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr
);
1043 ratio
= (msr
>> 56) & 0xFF;
1045 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1046 ratio
, bclk
, ratio
* bclk
);
1048 ratio
= (msr
>> 48) & 0xFF;
1050 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1051 ratio
, bclk
, ratio
* bclk
);
1053 ratio
= (msr
>> 40) & 0xFF;
1055 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1056 ratio
, bclk
, ratio
* bclk
);
1058 ratio
= (msr
>> 32) & 0xFF;
1060 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1061 ratio
, bclk
, ratio
* bclk
);
1063 ratio
= (msr
>> 24) & 0xFF;
1065 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1066 ratio
, bclk
, ratio
* bclk
);
1068 ratio
= (msr
>> 16) & 0xFF;
1070 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1071 ratio
, bclk
, ratio
* bclk
);
1073 ratio
= (msr
>> 8) & 0xFF;
1075 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1076 ratio
, bclk
, ratio
* bclk
);
1078 ratio
= (msr
>> 0) & 0xFF;
1080 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1081 ratio
, bclk
, ratio
* bclk
);
1084 void free_all_buffers(void)
1086 CPU_FREE(cpu_present_set
);
1087 cpu_present_set
= NULL
;
1088 cpu_present_set
= 0;
1090 CPU_FREE(cpu_affinity_set
);
1091 cpu_affinity_set
= NULL
;
1092 cpu_affinity_setsize
= 0;
1100 package_even
= NULL
;
1110 free(output_buffer
);
1111 output_buffer
= NULL
;
1116 * cpu_is_first_sibling_in_core(cpu)
1117 * return 1 if given CPU is 1st HT sibling in the core
1119 int cpu_is_first_sibling_in_core(int cpu
)
1125 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu
);
1126 filep
= fopen(path
, "r");
1127 if (filep
== NULL
) {
1131 fscanf(filep
, "%d", &first_cpu
);
1133 return (cpu
== first_cpu
);
1137 * cpu_is_first_core_in_package(cpu)
1138 * return 1 if given CPU is 1st core in package
1140 int cpu_is_first_core_in_package(int cpu
)
1146 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu
);
1147 filep
= fopen(path
, "r");
1148 if (filep
== NULL
) {
1152 fscanf(filep
, "%d", &first_cpu
);
1154 return (cpu
== first_cpu
);
1157 int get_physical_package_id(int cpu
)
1163 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu
);
1164 filep
= fopen(path
, "r");
1165 if (filep
== NULL
) {
1169 fscanf(filep
, "%d", &pkg
);
1174 int get_core_id(int cpu
)
1180 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu
);
1181 filep
= fopen(path
, "r");
1182 if (filep
== NULL
) {
1186 fscanf(filep
, "%d", &core
);
1191 int get_num_ht_siblings(int cpu
)
1199 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu
);
1200 filep
= fopen(path
, "r");
1201 if (filep
== NULL
) {
1207 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1208 * otherwinse 1 sibling (self).
1210 matches
= fscanf(filep
, "%d%c%d\n", &sib1
, &character
, &sib2
);
1221 * run func(thread, core, package) in topology order
1222 * skip non-present cpus
1225 int for_all_cpus_2(int (func
)(struct thread_data
*, struct core_data
*,
1226 struct pkg_data
*, struct thread_data
*, struct core_data
*,
1227 struct pkg_data
*), struct thread_data
*thread_base
,
1228 struct core_data
*core_base
, struct pkg_data
*pkg_base
,
1229 struct thread_data
*thread_base2
, struct core_data
*core_base2
,
1230 struct pkg_data
*pkg_base2
)
1232 int retval
, pkg_no
, core_no
, thread_no
;
1234 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
1235 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
1236 for (thread_no
= 0; thread_no
<
1237 topo
.num_threads_per_core
; ++thread_no
) {
1238 struct thread_data
*t
, *t2
;
1239 struct core_data
*c
, *c2
;
1240 struct pkg_data
*p
, *p2
;
1242 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
1244 if (cpu_is_not_present(t
->cpu_id
))
1247 t2
= GET_THREAD(thread_base2
, thread_no
, core_no
, pkg_no
);
1249 c
= GET_CORE(core_base
, core_no
, pkg_no
);
1250 c2
= GET_CORE(core_base2
, core_no
, pkg_no
);
1252 p
= GET_PKG(pkg_base
, pkg_no
);
1253 p2
= GET_PKG(pkg_base2
, pkg_no
);
1255 retval
= func(t
, c
, p
, t2
, c2
, p2
);
1265 * run func(cpu) on every cpu in /proc/stat
1266 * return max_cpu number
1268 int for_all_proc_cpus(int (func
)(int))
1274 fp
= fopen(proc_stat
, "r");
1280 retval
= fscanf(fp
, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1282 perror("/proc/stat format");
1287 retval
= fscanf(fp
, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num
);
1291 retval
= func(cpu_num
);
1301 void re_initialize(void)
1304 setup_all_buffers();
1305 printf("turbostat: re-initialized with num_cpus %d\n", topo
.num_cpus
);
1311 * remember the last one seen, it will be the max
1313 int count_cpus(int cpu
)
1315 if (topo
.max_cpu_num
< cpu
)
1316 topo
.max_cpu_num
= cpu
;
1321 int mark_cpu_present(int cpu
)
1323 CPU_SET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
1327 void turbostat_loop()
1335 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
1338 } else if (retval
== -1) {
1339 if (restarted
> 1) {
1346 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
1349 if (for_all_proc_cpus(cpu_is_not_present
)) {
1353 sleep(interval_sec
);
1354 retval
= for_all_cpus(get_counters
, ODD_COUNTERS
);
1357 } else if (retval
== -1) {
1361 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
1362 timersub(&tv_odd
, &tv_even
, &tv_delta
);
1363 for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
);
1364 compute_average(EVEN_COUNTERS
);
1365 format_all_counters(EVEN_COUNTERS
);
1367 sleep(interval_sec
);
1368 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
1371 } else if (retval
== -1) {
1375 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
1376 timersub(&tv_even
, &tv_odd
, &tv_delta
);
1377 for_all_cpus_2(delta_cpu
, EVEN_COUNTERS
, ODD_COUNTERS
);
1378 compute_average(ODD_COUNTERS
);
1379 format_all_counters(ODD_COUNTERS
);
1384 void check_dev_msr()
1388 if (stat("/dev/cpu/0/msr", &sb
)) {
1389 fprintf(stderr
, "no /dev/cpu/0/msr\n");
1390 fprintf(stderr
, "Try \"# modprobe msr\"\n");
1395 void check_super_user()
1397 if (getuid() != 0) {
1398 fprintf(stderr
, "must be root\n");
1403 int has_nehalem_turbo_ratio_limit(unsigned int family
, unsigned int model
)
1412 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1413 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1414 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1415 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1416 case 0x2C: /* Westmere EP - Gulftown */
1417 case 0x2A: /* SNB */
1418 case 0x2D: /* SNB Xeon */
1419 case 0x3A: /* IVB */
1420 case 0x3E: /* IVB Xeon */
1421 case 0x3C: /* HSW */
1422 case 0x3F: /* HSW */
1423 case 0x45: /* HSW */
1424 case 0x46: /* HSW */
1426 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1427 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1432 int has_ivt_turbo_ratio_limit(unsigned int family
, unsigned int model
)
1441 case 0x3E: /* IVB Xeon */
1450 * Decode the ENERGY_PERF_BIAS MSR
1452 int print_epb(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1454 unsigned long long msr
;
1463 /* EPB is per-package */
1464 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1467 if (cpu_migrate(cpu
)) {
1468 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1472 if (get_msr(cpu
, MSR_IA32_ENERGY_PERF_BIAS
, &msr
))
1475 switch (msr
& 0x7) {
1476 case ENERGY_PERF_BIAS_PERFORMANCE
:
1477 epb_string
= "performance";
1479 case ENERGY_PERF_BIAS_NORMAL
:
1480 epb_string
= "balanced";
1482 case ENERGY_PERF_BIAS_POWERSAVE
:
1483 epb_string
= "powersave";
1486 epb_string
= "custom";
1489 fprintf(stderr
, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu
, msr
, epb_string
);
1494 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1495 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1502 void rapl_probe(unsigned int family
, unsigned int model
)
1504 unsigned long long msr
;
1516 case 0x3C: /* HSW */
1517 case 0x3F: /* HSW */
1518 case 0x45: /* HSW */
1519 case 0x46: /* HSW */
1520 do_rapl
= RAPL_PKG
| RAPL_CORES
| RAPL_GFX
;
1524 do_rapl
= RAPL_PKG
| RAPL_CORES
| RAPL_DRAM
| RAPL_PKG_PERF_STATUS
| RAPL_DRAM_PERF_STATUS
;
1530 /* units on package 0, verify later other packages match */
1531 if (get_msr(0, MSR_RAPL_POWER_UNIT
, &msr
))
1534 rapl_power_units
= 1.0 / (1 << (msr
& 0xF));
1535 rapl_energy_units
= 1.0 / (1 << (msr
>> 8 & 0x1F));
1536 rapl_time_units
= 1.0 / (1 << (msr
>> 16 & 0xF));
1538 /* get TDP to determine energy counter range */
1539 if (get_msr(0, MSR_PKG_POWER_INFO
, &msr
))
1542 tdp
= ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
;
1544 rapl_joule_counter_range
= 0xFFFFFFFF * rapl_energy_units
/ tdp
;
1547 fprintf(stderr
, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range
);
1552 int print_thermal(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1554 unsigned long long msr
;
1558 if (!(do_dts
|| do_ptm
))
1563 /* DTS is per-core, no need to print for each thread */
1564 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
1567 if (cpu_migrate(cpu
)) {
1568 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1572 if (do_ptm
&& (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)) {
1573 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
1576 dts
= (msr
>> 16) & 0x7F;
1577 fprintf(stderr
, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1578 cpu
, msr
, tcc_activation_temp
- dts
);
1581 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_INTERRUPT
, &msr
))
1584 dts
= (msr
>> 16) & 0x7F;
1585 dts2
= (msr
>> 8) & 0x7F;
1586 fprintf(stderr
, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1587 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
1593 unsigned int resolution
;
1595 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
1598 dts
= (msr
>> 16) & 0x7F;
1599 resolution
= (msr
>> 27) & 0xF;
1600 fprintf(stderr
, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1601 cpu
, msr
, tcc_activation_temp
- dts
, resolution
);
1604 if (get_msr(cpu
, MSR_IA32_THERM_INTERRUPT
, &msr
))
1607 dts
= (msr
>> 16) & 0x7F;
1608 dts2
= (msr
>> 8) & 0x7F;
1609 fprintf(stderr
, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1610 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
1617 void print_power_limit_msr(int cpu
, unsigned long long msr
, char *label
)
1619 fprintf(stderr
, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1621 ((msr
>> 15) & 1) ? "EN" : "DIS",
1622 ((msr
>> 0) & 0x7FFF) * rapl_power_units
,
1623 (1.0 + (((msr
>> 22) & 0x3)/4.0)) * (1 << ((msr
>> 17) & 0x1F)) * rapl_time_units
,
1624 (((msr
>> 16) & 1) ? "EN" : "DIS"));
1629 int print_rapl(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1631 unsigned long long msr
;
1633 double local_rapl_power_units
, local_rapl_energy_units
, local_rapl_time_units
;
1638 /* RAPL counters are per package, so print only for 1st thread/package */
1639 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1643 if (cpu_migrate(cpu
)) {
1644 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1648 if (get_msr(cpu
, MSR_RAPL_POWER_UNIT
, &msr
))
1651 local_rapl_power_units
= 1.0 / (1 << (msr
& 0xF));
1652 local_rapl_energy_units
= 1.0 / (1 << (msr
>> 8 & 0x1F));
1653 local_rapl_time_units
= 1.0 / (1 << (msr
>> 16 & 0xF));
1655 if (local_rapl_power_units
!= rapl_power_units
)
1656 fprintf(stderr
, "cpu%d, ERROR: Power units mis-match\n", cpu
);
1657 if (local_rapl_energy_units
!= rapl_energy_units
)
1658 fprintf(stderr
, "cpu%d, ERROR: Energy units mis-match\n", cpu
);
1659 if (local_rapl_time_units
!= rapl_time_units
)
1660 fprintf(stderr
, "cpu%d, ERROR: Time units mis-match\n", cpu
);
1663 fprintf(stderr
, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1664 "(%f Watts, %f Joules, %f sec.)\n", cpu
, msr
,
1665 local_rapl_power_units
, local_rapl_energy_units
, local_rapl_time_units
);
1667 if (do_rapl
& RAPL_PKG
) {
1668 if (get_msr(cpu
, MSR_PKG_POWER_INFO
, &msr
))
1672 fprintf(stderr
, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1674 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1675 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1676 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1677 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
1679 if (get_msr(cpu
, MSR_PKG_POWER_LIMIT
, &msr
))
1682 fprintf(stderr
, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1683 cpu
, msr
, (msr
>> 63) & 1 ? "": "UN");
1685 print_power_limit_msr(cpu
, msr
, "PKG Limit #1");
1686 fprintf(stderr
, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1688 ((msr
>> 47) & 1) ? "EN" : "DIS",
1689 ((msr
>> 32) & 0x7FFF) * rapl_power_units
,
1690 (1.0 + (((msr
>> 54) & 0x3)/4.0)) * (1 << ((msr
>> 49) & 0x1F)) * rapl_time_units
,
1691 ((msr
>> 48) & 1) ? "EN" : "DIS");
1694 if (do_rapl
& RAPL_DRAM
) {
1695 if (get_msr(cpu
, MSR_DRAM_POWER_INFO
, &msr
))
1699 fprintf(stderr
, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1701 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1702 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1703 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1704 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
1707 if (get_msr(cpu
, MSR_DRAM_POWER_LIMIT
, &msr
))
1709 fprintf(stderr
, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1710 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
1712 print_power_limit_msr(cpu
, msr
, "DRAM Limit");
1714 if (do_rapl
& RAPL_CORES
) {
1716 if (get_msr(cpu
, MSR_PP0_POLICY
, &msr
))
1719 fprintf(stderr
, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu
, msr
& 0xF);
1721 if (get_msr(cpu
, MSR_PP0_POWER_LIMIT
, &msr
))
1723 fprintf(stderr
, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1724 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
1725 print_power_limit_msr(cpu
, msr
, "Cores Limit");
1728 if (do_rapl
& RAPL_GFX
) {
1730 if (get_msr(cpu
, MSR_PP1_POLICY
, &msr
))
1733 fprintf(stderr
, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu
, msr
& 0xF);
1735 if (get_msr(cpu
, MSR_PP1_POWER_LIMIT
, &msr
))
1737 fprintf(stderr
, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1738 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
1739 print_power_limit_msr(cpu
, msr
, "GFX Limit");
1746 int is_snb(unsigned int family
, unsigned int model
)
1754 case 0x3A: /* IVB */
1755 case 0x3E: /* IVB Xeon */
1756 case 0x3C: /* HSW */
1757 case 0x3F: /* HSW */
1758 case 0x45: /* HSW */
1759 case 0x46: /* HSW */
1765 double discover_bclk(unsigned int family
, unsigned int model
)
1767 if (is_snb(family
, model
))
1774 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1775 * the Thermal Control Circuit (TCC) activates.
1776 * This is usually equal to tjMax.
1778 * Older processors do not have this MSR, so there we guess,
1779 * but also allow cmdline over-ride with -T.
1781 * Several MSR temperature values are in units of degrees-C
1782 * below this value, including the Digital Thermal Sensor (DTS),
1783 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1785 int set_temperature_target(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1787 unsigned long long msr
;
1788 unsigned int target_c_local
;
1791 /* tcc_activation_temp is used only for dts or ptm */
1792 if (!(do_dts
|| do_ptm
))
1795 /* this is a per-package concept */
1796 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1800 if (cpu_migrate(cpu
)) {
1801 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1805 if (tcc_activation_temp_override
!= 0) {
1806 tcc_activation_temp
= tcc_activation_temp_override
;
1807 fprintf(stderr
, "cpu%d: Using cmdline TCC Target (%d C)\n",
1808 cpu
, tcc_activation_temp
);
1812 /* Temperature Target MSR is Nehalem and newer only */
1813 if (!do_nehalem_platform_info
)
1816 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET
, &msr
))
1819 target_c_local
= (msr
>> 16) & 0x7F;
1822 fprintf(stderr
, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1823 cpu
, msr
, target_c_local
);
1825 if (target_c_local
< 85 || target_c_local
> 120)
1828 tcc_activation_temp
= target_c_local
;
1833 tcc_activation_temp
= TJMAX_DEFAULT
;
1834 fprintf(stderr
, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1835 cpu
, tcc_activation_temp
);
1841 unsigned int eax
, ebx
, ecx
, edx
, max_level
;
1842 unsigned int fms
, family
, model
, stepping
;
1844 eax
= ebx
= ecx
= edx
= 0;
1846 asm("cpuid" : "=a" (max_level
), "=b" (ebx
), "=c" (ecx
), "=d" (edx
) : "a" (0));
1848 if (ebx
== 0x756e6547 && edx
== 0x49656e69 && ecx
== 0x6c65746e)
1852 fprintf(stderr
, "CPUID(0): %.4s%.4s%.4s ",
1853 (char *)&ebx
, (char *)&edx
, (char *)&ecx
);
1855 asm("cpuid" : "=a" (fms
), "=c" (ecx
), "=d" (edx
) : "a" (1) : "ebx");
1856 family
= (fms
>> 8) & 0xf;
1857 model
= (fms
>> 4) & 0xf;
1858 stepping
= fms
& 0xf;
1859 if (family
== 6 || family
== 0xf)
1860 model
+= ((fms
>> 16) & 0xf) << 4;
1863 fprintf(stderr
, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1864 max_level
, family
, model
, stepping
, family
, model
, stepping
);
1866 if (!(edx
& (1 << 5))) {
1867 fprintf(stderr
, "CPUID: no MSR\n");
1872 * check max extended function levels of CPUID.
1873 * This is needed to check for invariant TSC.
1874 * This check is valid for both Intel and AMD.
1876 ebx
= ecx
= edx
= 0;
1877 asm("cpuid" : "=a" (max_level
), "=b" (ebx
), "=c" (ecx
), "=d" (edx
) : "a" (0x80000000));
1879 if (max_level
< 0x80000007) {
1880 fprintf(stderr
, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level
);
1885 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1886 * this check is valid for both Intel and AMD
1888 asm("cpuid" : "=a" (eax
), "=b" (ebx
), "=c" (ecx
), "=d" (edx
) : "a" (0x80000007));
1889 has_invariant_tsc
= edx
& (1 << 8);
1891 if (!has_invariant_tsc
) {
1892 fprintf(stderr
, "No invariant TSC\n");
1897 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1898 * this check is valid for both Intel and AMD
1901 asm("cpuid" : "=a" (eax
), "=b" (ebx
), "=c" (ecx
), "=d" (edx
) : "a" (0x6));
1902 has_aperf
= ecx
& (1 << 0);
1903 do_dts
= eax
& (1 << 0);
1904 do_ptm
= eax
& (1 << 6);
1905 has_epb
= ecx
& (1 << 3);
1908 fprintf(stderr
, "CPUID(6): %s%s%s%s\n",
1909 has_aperf
? "APERF" : "No APERF!",
1910 do_dts
? ", DTS" : "",
1911 do_ptm
? ", PTM": "",
1912 has_epb
? ", EPB": "");
1917 do_nehalem_platform_info
= genuine_intel
&& has_invariant_tsc
;
1918 do_nhm_cstates
= genuine_intel
; /* all Intel w/ non-stop TSC have NHM counters */
1919 do_smi
= do_nhm_cstates
;
1920 do_snb_cstates
= is_snb(family
, model
);
1921 bclk
= discover_bclk(family
, model
);
1923 do_nehalem_turbo_ratio_limit
= has_nehalem_turbo_ratio_limit(family
, model
);
1924 do_ivt_turbo_ratio_limit
= has_ivt_turbo_ratio_limit(family
, model
);
1925 rapl_probe(family
, model
);
1933 fprintf(stderr
, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
1940 * in /dev/cpu/ return success for names that are numbers
1941 * ie. filter out ".", "..", "microcode".
1943 int dir_filter(const struct dirent
*dirp
)
1945 if (isdigit(dirp
->d_name
[0]))
1951 int open_dev_cpu_msr(int dummy1
)
1956 void topology_probe()
1959 int max_core_id
= 0;
1960 int max_package_id
= 0;
1961 int max_siblings
= 0;
1962 struct cpu_topology
{
1964 int physical_package_id
;
1967 /* Initialize num_cpus, max_cpu_num */
1969 topo
.max_cpu_num
= 0;
1970 for_all_proc_cpus(count_cpus
);
1971 if (!summary_only
&& topo
.num_cpus
> 1)
1975 fprintf(stderr
, "num_cpus %d max_cpu_num %d\n", topo
.num_cpus
, topo
.max_cpu_num
);
1977 cpus
= calloc(1, (topo
.max_cpu_num
+ 1) * sizeof(struct cpu_topology
));
1979 perror("calloc cpus");
1984 * Allocate and initialize cpu_present_set
1986 cpu_present_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
1987 if (cpu_present_set
== NULL
) {
1988 perror("CPU_ALLOC");
1991 cpu_present_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
1992 CPU_ZERO_S(cpu_present_setsize
, cpu_present_set
);
1993 for_all_proc_cpus(mark_cpu_present
);
1996 * Allocate and initialize cpu_affinity_set
1998 cpu_affinity_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
1999 if (cpu_affinity_set
== NULL
) {
2000 perror("CPU_ALLOC");
2003 cpu_affinity_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
2004 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
2009 * find max_core_id, max_package_id
2011 for (i
= 0; i
<= topo
.max_cpu_num
; ++i
) {
2014 if (cpu_is_not_present(i
)) {
2016 fprintf(stderr
, "cpu%d NOT PRESENT\n", i
);
2019 cpus
[i
].core_id
= get_core_id(i
);
2020 if (cpus
[i
].core_id
> max_core_id
)
2021 max_core_id
= cpus
[i
].core_id
;
2023 cpus
[i
].physical_package_id
= get_physical_package_id(i
);
2024 if (cpus
[i
].physical_package_id
> max_package_id
)
2025 max_package_id
= cpus
[i
].physical_package_id
;
2027 siblings
= get_num_ht_siblings(i
);
2028 if (siblings
> max_siblings
)
2029 max_siblings
= siblings
;
2031 fprintf(stderr
, "cpu %d pkg %d core %d\n",
2032 i
, cpus
[i
].physical_package_id
, cpus
[i
].core_id
);
2034 topo
.num_cores_per_pkg
= max_core_id
+ 1;
2036 fprintf(stderr
, "max_core_id %d, sizing for %d cores per package\n",
2037 max_core_id
, topo
.num_cores_per_pkg
);
2038 if (!summary_only
&& topo
.num_cores_per_pkg
> 1)
2041 topo
.num_packages
= max_package_id
+ 1;
2043 fprintf(stderr
, "max_package_id %d, sizing for %d packages\n",
2044 max_package_id
, topo
.num_packages
);
2045 if (!summary_only
&& topo
.num_packages
> 1)
2048 topo
.num_threads_per_core
= max_siblings
;
2050 fprintf(stderr
, "max_siblings %d\n", max_siblings
);
2056 allocate_counters(struct thread_data
**t
, struct core_data
**c
, struct pkg_data
**p
)
2060 *t
= calloc(topo
.num_threads_per_core
* topo
.num_cores_per_pkg
*
2061 topo
.num_packages
, sizeof(struct thread_data
));
2065 for (i
= 0; i
< topo
.num_threads_per_core
*
2066 topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
2067 (*t
)[i
].cpu_id
= -1;
2069 *c
= calloc(topo
.num_cores_per_pkg
* topo
.num_packages
,
2070 sizeof(struct core_data
));
2074 for (i
= 0; i
< topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
2075 (*c
)[i
].core_id
= -1;
2077 *p
= calloc(topo
.num_packages
, sizeof(struct pkg_data
));
2081 for (i
= 0; i
< topo
.num_packages
; i
++)
2082 (*p
)[i
].package_id
= i
;
2086 perror("calloc counters");
2092 * set cpu_id, core_num, pkg_num
2093 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2095 * increment topo.num_cores when 1st core in pkg seen
2097 void init_counter(struct thread_data
*thread_base
, struct core_data
*core_base
,
2098 struct pkg_data
*pkg_base
, int thread_num
, int core_num
,
2099 int pkg_num
, int cpu_id
)
2101 struct thread_data
*t
;
2102 struct core_data
*c
;
2105 t
= GET_THREAD(thread_base
, thread_num
, core_num
, pkg_num
);
2106 c
= GET_CORE(core_base
, core_num
, pkg_num
);
2107 p
= GET_PKG(pkg_base
, pkg_num
);
2110 if (thread_num
== 0) {
2111 t
->flags
|= CPU_IS_FIRST_THREAD_IN_CORE
;
2112 if (cpu_is_first_core_in_package(cpu_id
))
2113 t
->flags
|= CPU_IS_FIRST_CORE_IN_PACKAGE
;
2116 c
->core_id
= core_num
;
2117 p
->package_id
= pkg_num
;
2121 int initialize_counters(int cpu_id
)
2123 int my_thread_id
, my_core_id
, my_package_id
;
2125 my_package_id
= get_physical_package_id(cpu_id
);
2126 my_core_id
= get_core_id(cpu_id
);
2128 if (cpu_is_first_sibling_in_core(cpu_id
)) {
2135 init_counter(EVEN_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
2136 init_counter(ODD_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
2140 void allocate_output_buffer()
2142 output_buffer
= calloc(1, (1 + topo
.num_cpus
) * 128);
2143 outp
= output_buffer
;
2150 void setup_all_buffers(void)
2153 allocate_counters(&thread_even
, &core_even
, &package_even
);
2154 allocate_counters(&thread_odd
, &core_odd
, &package_odd
);
2155 allocate_output_buffer();
2156 for_all_proc_cpus(initialize_counters
);
2158 void turbostat_init()
2165 setup_all_buffers();
2168 print_verbose_header();
2171 for_all_cpus(print_epb
, ODD_COUNTERS
);
2174 for_all_cpus(print_rapl
, ODD_COUNTERS
);
2176 for_all_cpus(set_temperature_target
, ODD_COUNTERS
);
2179 for_all_cpus(print_thermal
, ODD_COUNTERS
);
2182 int fork_it(char **argv
)
2187 status
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
2190 /* clear affinity side-effect of get_counters() */
2191 sched_setaffinity(0, cpu_present_setsize
, cpu_present_set
);
2192 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
2197 execvp(argv
[0], argv
);
2201 if (child_pid
== -1) {
2206 signal(SIGINT
, SIG_IGN
);
2207 signal(SIGQUIT
, SIG_IGN
);
2208 if (waitpid(child_pid
, &status
, 0) == -1) {
2214 * n.b. fork_it() does not check for errors from for_all_cpus()
2215 * because re-starting is problematic when forking
2217 for_all_cpus(get_counters
, ODD_COUNTERS
);
2218 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
2219 timersub(&tv_odd
, &tv_even
, &tv_delta
);
2220 for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
);
2221 compute_average(EVEN_COUNTERS
);
2222 format_all_counters(EVEN_COUNTERS
);
2225 fprintf(stderr
, "%.6f sec\n", tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0);
2230 void cmdline(int argc
, char **argv
)
2236 while ((opt
= getopt(argc
, argv
, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
2251 interval_sec
= atoi(optarg
);
2254 sscanf(optarg
, "%x", &extra_delta_offset32
);
2257 sscanf(optarg
, "%x", &extra_delta_offset64
);
2260 sscanf(optarg
, "%x", &extra_msr_offset32
);
2263 sscanf(optarg
, "%x", &extra_msr_offset64
);
2269 tcc_activation_temp_override
= atoi(optarg
);
2277 int main(int argc
, char **argv
)
2279 cmdline(argc
, argv
);
2282 fprintf(stderr
, "turbostat v3.3 March 15, 2013"
2283 " - Len Brown <lenb@kernel.org>\n");
2288 * if any params left, it must be a command to fork
2291 return fork_it(argv
+ optind
);