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>
40 char *proc_stat
= "/proc/stat";
41 unsigned int interval_sec
= 5; /* set with -i interval_sec */
42 unsigned int verbose
; /* set with -v */
43 unsigned int rapl_verbose
; /* set with -R */
44 unsigned int thermal_verbose
; /* set with -T */
45 unsigned int summary_only
; /* set with -s */
48 unsigned int do_nhm_cstates
;
49 unsigned int do_snb_cstates
;
50 unsigned int do_c8_c9_c10
;
51 unsigned int has_aperf
;
53 unsigned int units
= 1000000000; /* Ghz etc */
54 unsigned int genuine_intel
;
55 unsigned int has_invariant_tsc
;
56 unsigned int do_nehalem_platform_info
;
57 unsigned int do_nehalem_turbo_ratio_limit
;
58 unsigned int do_ivt_turbo_ratio_limit
;
59 unsigned int extra_msr_offset32
;
60 unsigned int extra_msr_offset64
;
61 unsigned int extra_delta_offset32
;
62 unsigned int extra_delta_offset64
;
65 unsigned int show_pkg
;
66 unsigned int show_core
;
67 unsigned int show_cpu
;
68 unsigned int show_pkg_only
;
69 unsigned int show_core_only
;
70 char *output_buffer
, *outp
;
74 unsigned int tcc_activation_temp
;
75 unsigned int tcc_activation_temp_override
;
76 double rapl_power_units
, rapl_energy_units
, rapl_time_units
;
77 double rapl_joule_counter_range
;
79 #define RAPL_PKG (1 << 0)
80 #define RAPL_CORES (1 << 1)
81 #define RAPL_GFX (1 << 2)
82 #define RAPL_DRAM (1 << 3)
83 #define RAPL_PKG_PERF_STATUS (1 << 4)
84 #define RAPL_DRAM_PERF_STATUS (1 << 5)
85 #define TJMAX_DEFAULT 100
87 #define MAX(a, b) ((a) > (b) ? (a) : (b))
89 int aperf_mperf_unstable
;
93 cpu_set_t
*cpu_present_set
, *cpu_affinity_set
;
94 size_t cpu_present_setsize
, cpu_affinity_setsize
;
97 unsigned long long tsc
;
98 unsigned long long aperf
;
99 unsigned long long mperf
;
100 unsigned long long c1
; /* derived */
101 unsigned long long extra_msr64
;
102 unsigned long long extra_delta64
;
103 unsigned long long extra_msr32
;
104 unsigned long long extra_delta32
;
105 unsigned int smi_count
;
108 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
109 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
110 } *thread_even
, *thread_odd
;
113 unsigned long long c3
;
114 unsigned long long c6
;
115 unsigned long long c7
;
116 unsigned int core_temp_c
;
117 unsigned int core_id
;
118 } *core_even
, *core_odd
;
121 unsigned long long pc2
;
122 unsigned long long pc3
;
123 unsigned long long pc6
;
124 unsigned long long pc7
;
125 unsigned long long pc8
;
126 unsigned long long pc9
;
127 unsigned long long pc10
;
128 unsigned int package_id
;
129 unsigned int energy_pkg
; /* MSR_PKG_ENERGY_STATUS */
130 unsigned int energy_dram
; /* MSR_DRAM_ENERGY_STATUS */
131 unsigned int energy_cores
; /* MSR_PP0_ENERGY_STATUS */
132 unsigned int energy_gfx
; /* MSR_PP1_ENERGY_STATUS */
133 unsigned int rapl_pkg_perf_status
; /* MSR_PKG_PERF_STATUS */
134 unsigned int rapl_dram_perf_status
; /* MSR_DRAM_PERF_STATUS */
135 unsigned int pkg_temp_c
;
137 } *package_even
, *package_odd
;
139 #define ODD_COUNTERS thread_odd, core_odd, package_odd
140 #define EVEN_COUNTERS thread_even, core_even, package_even
142 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
143 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
144 topo.num_threads_per_core + \
145 (core_no) * topo.num_threads_per_core + (thread_no))
146 #define GET_CORE(core_base, core_no, pkg_no) \
147 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
148 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
150 struct system_summary
{
151 struct thread_data threads
;
152 struct core_data cores
;
153 struct pkg_data packages
;
162 int num_cores_per_pkg
;
163 int num_threads_per_core
;
166 struct timeval tv_even
, tv_odd
, tv_delta
;
168 void setup_all_buffers(void);
170 int cpu_is_not_present(int cpu
)
172 return !CPU_ISSET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
175 * run func(thread, core, package) in topology order
176 * skip non-present cpus
179 int for_all_cpus(int (func
)(struct thread_data
*, struct core_data
*, struct pkg_data
*),
180 struct thread_data
*thread_base
, struct core_data
*core_base
, struct pkg_data
*pkg_base
)
182 int retval
, pkg_no
, core_no
, thread_no
;
184 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
185 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
186 for (thread_no
= 0; thread_no
<
187 topo
.num_threads_per_core
; ++thread_no
) {
188 struct thread_data
*t
;
192 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
194 if (cpu_is_not_present(t
->cpu_id
))
197 c
= GET_CORE(core_base
, core_no
, pkg_no
);
198 p
= GET_PKG(pkg_base
, pkg_no
);
200 retval
= func(t
, c
, p
);
209 int cpu_migrate(int cpu
)
211 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
212 CPU_SET_S(cpu
, cpu_affinity_setsize
, cpu_affinity_set
);
213 if (sched_setaffinity(0, cpu_affinity_setsize
, cpu_affinity_set
) == -1)
219 int get_msr(int cpu
, off_t offset
, unsigned long long *msr
)
225 sprintf(pathname
, "/dev/cpu/%d/msr", cpu
);
226 fd
= open(pathname
, O_RDONLY
);
230 retval
= pread(fd
, msr
, sizeof *msr
, offset
);
233 if (retval
!= sizeof *msr
) {
234 fprintf(stderr
, "%s offset 0x%zx read failed\n", pathname
, offset
);
241 void print_header(void)
244 outp
+= sprintf(outp
, "pk");
246 outp
+= sprintf(outp
, " ");
248 outp
+= sprintf(outp
, "cor");
250 outp
+= sprintf(outp
, " CPU");
251 if (show_pkg
|| show_core
|| show_cpu
)
252 outp
+= sprintf(outp
, " ");
254 outp
+= sprintf(outp
, " %%c0");
256 outp
+= sprintf(outp
, " GHz");
257 outp
+= sprintf(outp
, " TSC");
259 outp
+= sprintf(outp
, " SMI");
260 if (extra_delta_offset32
)
261 outp
+= sprintf(outp
, " count 0x%03X", extra_delta_offset32
);
262 if (extra_delta_offset64
)
263 outp
+= sprintf(outp
, " COUNT 0x%03X", extra_delta_offset64
);
264 if (extra_msr_offset32
)
265 outp
+= sprintf(outp
, " MSR 0x%03X", extra_msr_offset32
);
266 if (extra_msr_offset64
)
267 outp
+= sprintf(outp
, " MSR 0x%03X", extra_msr_offset64
);
269 outp
+= sprintf(outp
, " %%c1");
271 outp
+= sprintf(outp
, " %%c3");
273 outp
+= sprintf(outp
, " %%c6");
275 outp
+= sprintf(outp
, " %%c7");
278 outp
+= sprintf(outp
, " CTMP");
280 outp
+= sprintf(outp
, " PTMP");
283 outp
+= sprintf(outp
, " %%pc2");
285 outp
+= sprintf(outp
, " %%pc3");
287 outp
+= sprintf(outp
, " %%pc6");
289 outp
+= sprintf(outp
, " %%pc7");
291 outp
+= sprintf(outp
, " %%pc8");
292 outp
+= sprintf(outp
, " %%pc9");
293 outp
+= sprintf(outp
, " %%pc10");
296 if (do_rapl
& RAPL_PKG
)
297 outp
+= sprintf(outp
, " Pkg_W");
298 if (do_rapl
& RAPL_CORES
)
299 outp
+= sprintf(outp
, " Cor_W");
300 if (do_rapl
& RAPL_GFX
)
301 outp
+= sprintf(outp
, " GFX_W");
302 if (do_rapl
& RAPL_DRAM
)
303 outp
+= sprintf(outp
, " RAM_W");
304 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
305 outp
+= sprintf(outp
, " PKG_%%");
306 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
307 outp
+= sprintf(outp
, " RAM_%%");
309 outp
+= sprintf(outp
, "\n");
312 int dump_counters(struct thread_data
*t
, struct core_data
*c
,
315 fprintf(stderr
, "t %p, c %p, p %p\n", t
, c
, p
);
318 fprintf(stderr
, "CPU: %d flags 0x%x\n", t
->cpu_id
, t
->flags
);
319 fprintf(stderr
, "TSC: %016llX\n", t
->tsc
);
320 fprintf(stderr
, "aperf: %016llX\n", t
->aperf
);
321 fprintf(stderr
, "mperf: %016llX\n", t
->mperf
);
322 fprintf(stderr
, "c1: %016llX\n", t
->c1
);
323 fprintf(stderr
, "msr0x%x: %08llX\n",
324 extra_delta_offset32
, t
->extra_delta32
);
325 fprintf(stderr
, "msr0x%x: %016llX\n",
326 extra_delta_offset64
, t
->extra_delta64
);
327 fprintf(stderr
, "msr0x%x: %08llX\n",
328 extra_msr_offset32
, t
->extra_msr32
);
329 fprintf(stderr
, "msr0x%x: %016llX\n",
330 extra_msr_offset64
, t
->extra_msr64
);
332 fprintf(stderr
, "SMI: %08X\n", t
->smi_count
);
336 fprintf(stderr
, "core: %d\n", c
->core_id
);
337 fprintf(stderr
, "c3: %016llX\n", c
->c3
);
338 fprintf(stderr
, "c6: %016llX\n", c
->c6
);
339 fprintf(stderr
, "c7: %016llX\n", c
->c7
);
340 fprintf(stderr
, "DTS: %dC\n", c
->core_temp_c
);
344 fprintf(stderr
, "package: %d\n", p
->package_id
);
345 fprintf(stderr
, "pc2: %016llX\n", p
->pc2
);
346 fprintf(stderr
, "pc3: %016llX\n", p
->pc3
);
347 fprintf(stderr
, "pc6: %016llX\n", p
->pc6
);
348 fprintf(stderr
, "pc7: %016llX\n", p
->pc7
);
349 fprintf(stderr
, "pc8: %016llX\n", p
->pc8
);
350 fprintf(stderr
, "pc9: %016llX\n", p
->pc9
);
351 fprintf(stderr
, "pc10: %016llX\n", p
->pc10
);
352 fprintf(stderr
, "Joules PKG: %0X\n", p
->energy_pkg
);
353 fprintf(stderr
, "Joules COR: %0X\n", p
->energy_cores
);
354 fprintf(stderr
, "Joules GFX: %0X\n", p
->energy_gfx
);
355 fprintf(stderr
, "Joules RAM: %0X\n", p
->energy_dram
);
356 fprintf(stderr
, "Throttle PKG: %0X\n", p
->rapl_pkg_perf_status
);
357 fprintf(stderr
, "Throttle RAM: %0X\n", p
->rapl_dram_perf_status
);
358 fprintf(stderr
, "PTM: %dC\n", p
->pkg_temp_c
);
364 * column formatting convention & formats
365 * package: "pk" 2 columns %2d
366 * core: "cor" 3 columns %3d
367 * CPU: "CPU" 3 columns %3d
372 * GHz: "GHz" 3 columns %3.2
373 * TSC: "TSC" 3 columns %3.2
374 * SMI: "SMI" 4 columns %4d
375 * percentage " %pc3" %6.2
376 * Perf Status percentage: %5.2
377 * "CTMP" 4 columns %4d
379 int format_counters(struct thread_data
*t
, struct core_data
*c
,
382 double interval_float
;
385 /* if showing only 1st thread in core and this isn't one, bail out */
386 if (show_core_only
&& !(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
389 /* if showing only 1st thread in pkg and this isn't one, bail out */
390 if (show_pkg_only
&& !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
393 interval_float
= tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0;
395 /* topo columns, print blanks on 1st (average) line */
396 if (t
== &average
.threads
) {
398 outp
+= sprintf(outp
, " ");
399 if (show_pkg
&& show_core
)
400 outp
+= sprintf(outp
, " ");
402 outp
+= sprintf(outp
, " ");
404 outp
+= sprintf(outp
, " " " ");
408 outp
+= sprintf(outp
, "%2d", p
->package_id
);
410 outp
+= sprintf(outp
, " ");
412 if (show_pkg
&& show_core
)
413 outp
+= sprintf(outp
, " ");
416 outp
+= sprintf(outp
, "%3d", c
->core_id
);
418 outp
+= sprintf(outp
, " ");
421 outp
+= sprintf(outp
, " %3d", t
->cpu_id
);
424 if (do_nhm_cstates
) {
425 if (show_pkg
|| show_core
|| show_cpu
)
426 outp
+= sprintf(outp
, " ");
428 outp
+= sprintf(outp
, "%6.2f", 100.0 * t
->mperf
/t
->tsc
);
430 outp
+= sprintf(outp
, " ****");
435 if (!aperf_mperf_unstable
) {
436 outp
+= sprintf(outp
, " %3.2f",
437 1.0 * t
->tsc
/ units
* t
->aperf
/
438 t
->mperf
/ interval_float
);
440 if (t
->aperf
> t
->tsc
|| t
->mperf
> t
->tsc
) {
441 outp
+= sprintf(outp
, " ***");
443 outp
+= sprintf(outp
, "%3.1f*",
446 t
->mperf
/ interval_float
);
452 outp
+= sprintf(outp
, "%5.2f", 1.0 * t
->tsc
/units
/interval_float
);
456 outp
+= sprintf(outp
, "%4d", t
->smi_count
);
459 if (extra_delta_offset32
)
460 outp
+= sprintf(outp
, " %11llu", t
->extra_delta32
);
463 if (extra_delta_offset64
)
464 outp
+= sprintf(outp
, " %11llu", t
->extra_delta64
);
466 if (extra_msr_offset32
)
467 outp
+= sprintf(outp
, " 0x%08llx", t
->extra_msr32
);
470 if (extra_msr_offset64
)
471 outp
+= sprintf(outp
, " 0x%016llx", t
->extra_msr64
);
473 if (do_nhm_cstates
) {
475 outp
+= sprintf(outp
, " %6.2f", 100.0 * t
->c1
/t
->tsc
);
477 outp
+= sprintf(outp
, " ****");
480 /* print per-core data only for 1st thread in core */
481 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
485 outp
+= sprintf(outp
, " %6.2f", 100.0 * c
->c3
/t
->tsc
);
487 outp
+= sprintf(outp
, " %6.2f", 100.0 * c
->c6
/t
->tsc
);
489 outp
+= sprintf(outp
, " %6.2f", 100.0 * c
->c7
/t
->tsc
);
492 outp
+= sprintf(outp
, " %4d", c
->core_temp_c
);
494 /* print per-package data only for 1st core in package */
495 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
499 outp
+= sprintf(outp
, " %4d", p
->pkg_temp_c
);
502 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc2
/t
->tsc
);
504 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc3
/t
->tsc
);
506 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc6
/t
->tsc
);
508 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc7
/t
->tsc
);
510 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc8
/t
->tsc
);
511 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc9
/t
->tsc
);
512 outp
+= sprintf(outp
, " %6.2f", 100.0 * p
->pc10
/t
->tsc
);
516 * If measurement interval exceeds minimum RAPL Joule Counter range,
517 * indicate that results are suspect by printing "**" in fraction place.
519 if (interval_float
< rapl_joule_counter_range
) {
527 if (do_rapl
& RAPL_PKG
)
528 outp
+= sprintf(outp
, fmt6
, p
->energy_pkg
* rapl_energy_units
/ interval_float
);
529 if (do_rapl
& RAPL_CORES
)
530 outp
+= sprintf(outp
, fmt6
, p
->energy_cores
* rapl_energy_units
/ interval_float
);
531 if (do_rapl
& RAPL_GFX
)
532 outp
+= sprintf(outp
, fmt5
, p
->energy_gfx
* rapl_energy_units
/ interval_float
);
533 if (do_rapl
& RAPL_DRAM
)
534 outp
+= sprintf(outp
, fmt5
, p
->energy_dram
* rapl_energy_units
/ interval_float
);
535 if (do_rapl
& RAPL_PKG_PERF_STATUS
)
536 outp
+= sprintf(outp
, fmt5
, 100.0 * p
->rapl_pkg_perf_status
* rapl_time_units
/ interval_float
);
537 if (do_rapl
& RAPL_DRAM_PERF_STATUS
)
538 outp
+= sprintf(outp
, fmt5
, 100.0 * p
->rapl_dram_perf_status
* rapl_time_units
/ interval_float
);
541 outp
+= sprintf(outp
, "\n");
548 fputs(output_buffer
, stdout
);
550 outp
= output_buffer
;
554 fputs(output_buffer
, stderr
);
555 outp
= output_buffer
;
557 void format_all_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
561 if (!printed
|| !summary_only
)
564 if (topo
.num_cpus
> 1)
565 format_counters(&average
.threads
, &average
.cores
,
573 for_all_cpus(format_counters
, t
, c
, p
);
576 #define DELTA_WRAP32(new, old) \
580 old = 0x100000000 + new - old; \
584 delta_package(struct pkg_data
*new, struct pkg_data
*old
)
586 old
->pc2
= new->pc2
- old
->pc2
;
587 old
->pc3
= new->pc3
- old
->pc3
;
588 old
->pc6
= new->pc6
- old
->pc6
;
589 old
->pc7
= new->pc7
- old
->pc7
;
590 old
->pc8
= new->pc8
- old
->pc8
;
591 old
->pc9
= new->pc9
- old
->pc9
;
592 old
->pc10
= new->pc10
- old
->pc10
;
593 old
->pkg_temp_c
= new->pkg_temp_c
;
595 DELTA_WRAP32(new->energy_pkg
, old
->energy_pkg
);
596 DELTA_WRAP32(new->energy_cores
, old
->energy_cores
);
597 DELTA_WRAP32(new->energy_gfx
, old
->energy_gfx
);
598 DELTA_WRAP32(new->energy_dram
, old
->energy_dram
);
599 DELTA_WRAP32(new->rapl_pkg_perf_status
, old
->rapl_pkg_perf_status
);
600 DELTA_WRAP32(new->rapl_dram_perf_status
, old
->rapl_dram_perf_status
);
604 delta_core(struct core_data
*new, struct core_data
*old
)
606 old
->c3
= new->c3
- old
->c3
;
607 old
->c6
= new->c6
- old
->c6
;
608 old
->c7
= new->c7
- old
->c7
;
609 old
->core_temp_c
= new->core_temp_c
;
616 delta_thread(struct thread_data
*new, struct thread_data
*old
,
617 struct core_data
*core_delta
)
619 old
->tsc
= new->tsc
- old
->tsc
;
621 /* check for TSC < 1 Mcycles over interval */
622 if (old
->tsc
< (1000 * 1000)) {
623 fprintf(stderr
, "Insanely slow TSC rate, TSC stops in idle?\n");
624 fprintf(stderr
, "You can disable all c-states by booting with \"idle=poll\"\n");
625 fprintf(stderr
, "or just the deep ones with \"processor.max_cstate=1\"\n");
629 old
->c1
= new->c1
- old
->c1
;
631 if ((new->aperf
> old
->aperf
) && (new->mperf
> old
->mperf
)) {
632 old
->aperf
= new->aperf
- old
->aperf
;
633 old
->mperf
= new->mperf
- old
->mperf
;
636 if (!aperf_mperf_unstable
) {
637 fprintf(stderr
, "%s: APERF or MPERF went backwards *\n", progname
);
638 fprintf(stderr
, "* Frequency results do not cover entire interval *\n");
639 fprintf(stderr
, "* fix this by running Linux-2.6.30 or later *\n");
641 aperf_mperf_unstable
= 1;
644 * mperf delta is likely a huge "positive" number
645 * can not use it for calculating c0 time
653 * As counter collection is not atomic,
654 * it is possible for mperf's non-halted cycles + idle states
655 * to exceed TSC's all cycles: show c1 = 0% in that case.
657 if ((old
->mperf
+ core_delta
->c3
+ core_delta
->c6
+ core_delta
->c7
) > old
->tsc
)
660 /* normal case, derive c1 */
661 old
->c1
= old
->tsc
- old
->mperf
- core_delta
->c3
662 - core_delta
->c6
- core_delta
->c7
;
665 if (old
->mperf
== 0) {
666 if (verbose
> 1) fprintf(stderr
, "cpu%d MPERF 0!\n", old
->cpu_id
);
667 old
->mperf
= 1; /* divide by 0 protection */
670 old
->extra_delta32
= new->extra_delta32
- old
->extra_delta32
;
671 old
->extra_delta32
&= 0xFFFFFFFF;
673 old
->extra_delta64
= new->extra_delta64
- old
->extra_delta64
;
676 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
678 old
->extra_msr32
= new->extra_msr32
;
679 old
->extra_msr64
= new->extra_msr64
;
682 old
->smi_count
= new->smi_count
- old
->smi_count
;
685 int delta_cpu(struct thread_data
*t
, struct core_data
*c
,
686 struct pkg_data
*p
, struct thread_data
*t2
,
687 struct core_data
*c2
, struct pkg_data
*p2
)
689 /* calculate core delta only for 1st thread in core */
690 if (t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
)
693 /* always calculate thread delta */
694 delta_thread(t
, t2
, c2
); /* c2 is core delta */
696 /* calculate package delta only for 1st core in package */
697 if (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)
698 delta_package(p
, p2
);
703 void clear_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
711 t
->extra_delta32
= 0;
712 t
->extra_delta64
= 0;
714 /* tells format_counters to dump all fields from this set */
715 t
->flags
= CPU_IS_FIRST_THREAD_IN_CORE
| CPU_IS_FIRST_CORE_IN_PACKAGE
;
734 p
->rapl_pkg_perf_status
= 0;
735 p
->rapl_dram_perf_status
= 0;
738 int sum_counters(struct thread_data
*t
, struct core_data
*c
,
741 average
.threads
.tsc
+= t
->tsc
;
742 average
.threads
.aperf
+= t
->aperf
;
743 average
.threads
.mperf
+= t
->mperf
;
744 average
.threads
.c1
+= t
->c1
;
746 average
.threads
.extra_delta32
+= t
->extra_delta32
;
747 average
.threads
.extra_delta64
+= t
->extra_delta64
;
749 /* sum per-core values only for 1st thread in core */
750 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
753 average
.cores
.c3
+= c
->c3
;
754 average
.cores
.c6
+= c
->c6
;
755 average
.cores
.c7
+= c
->c7
;
757 average
.cores
.core_temp_c
= MAX(average
.cores
.core_temp_c
, c
->core_temp_c
);
759 /* sum per-pkg values only for 1st core in pkg */
760 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
763 average
.packages
.pc2
+= p
->pc2
;
764 average
.packages
.pc3
+= p
->pc3
;
765 average
.packages
.pc6
+= p
->pc6
;
766 average
.packages
.pc7
+= p
->pc7
;
767 average
.packages
.pc8
+= p
->pc8
;
768 average
.packages
.pc9
+= p
->pc9
;
769 average
.packages
.pc10
+= p
->pc10
;
771 average
.packages
.energy_pkg
+= p
->energy_pkg
;
772 average
.packages
.energy_dram
+= p
->energy_dram
;
773 average
.packages
.energy_cores
+= p
->energy_cores
;
774 average
.packages
.energy_gfx
+= p
->energy_gfx
;
776 average
.packages
.pkg_temp_c
= MAX(average
.packages
.pkg_temp_c
, p
->pkg_temp_c
);
778 average
.packages
.rapl_pkg_perf_status
+= p
->rapl_pkg_perf_status
;
779 average
.packages
.rapl_dram_perf_status
+= p
->rapl_dram_perf_status
;
783 * sum the counters for all cpus in the system
784 * compute the weighted average
786 void compute_average(struct thread_data
*t
, struct core_data
*c
,
789 clear_counters(&average
.threads
, &average
.cores
, &average
.packages
);
791 for_all_cpus(sum_counters
, t
, c
, p
);
793 average
.threads
.tsc
/= topo
.num_cpus
;
794 average
.threads
.aperf
/= topo
.num_cpus
;
795 average
.threads
.mperf
/= topo
.num_cpus
;
796 average
.threads
.c1
/= topo
.num_cpus
;
798 average
.threads
.extra_delta32
/= topo
.num_cpus
;
799 average
.threads
.extra_delta32
&= 0xFFFFFFFF;
801 average
.threads
.extra_delta64
/= topo
.num_cpus
;
803 average
.cores
.c3
/= topo
.num_cores
;
804 average
.cores
.c6
/= topo
.num_cores
;
805 average
.cores
.c7
/= topo
.num_cores
;
807 average
.packages
.pc2
/= topo
.num_packages
;
808 average
.packages
.pc3
/= topo
.num_packages
;
809 average
.packages
.pc6
/= topo
.num_packages
;
810 average
.packages
.pc7
/= topo
.num_packages
;
812 average
.packages
.pc8
/= topo
.num_packages
;
813 average
.packages
.pc9
/= topo
.num_packages
;
814 average
.packages
.pc10
/= topo
.num_packages
;
817 static unsigned long long rdtsc(void)
819 unsigned int low
, high
;
821 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
823 return low
| ((unsigned long long)high
) << 32;
830 * acquire and record local counters for that cpu
832 int get_counters(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
835 unsigned long long msr
;
837 if (cpu_migrate(cpu
)) {
838 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
842 t
->tsc
= rdtsc(); /* we are running on local CPU of interest */
845 if (get_msr(cpu
, MSR_IA32_APERF
, &t
->aperf
))
847 if (get_msr(cpu
, MSR_IA32_MPERF
, &t
->mperf
))
852 if (get_msr(cpu
, MSR_SMI_COUNT
, &msr
))
854 t
->smi_count
= msr
& 0xFFFFFFFF;
856 if (extra_delta_offset32
) {
857 if (get_msr(cpu
, extra_delta_offset32
, &msr
))
859 t
->extra_delta32
= msr
& 0xFFFFFFFF;
862 if (extra_delta_offset64
)
863 if (get_msr(cpu
, extra_delta_offset64
, &t
->extra_delta64
))
866 if (extra_msr_offset32
) {
867 if (get_msr(cpu
, extra_msr_offset32
, &msr
))
869 t
->extra_msr32
= msr
& 0xFFFFFFFF;
872 if (extra_msr_offset64
)
873 if (get_msr(cpu
, extra_msr_offset64
, &t
->extra_msr64
))
876 /* collect core counters only for 1st thread in core */
877 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
880 if (do_nhm_cstates
) {
881 if (get_msr(cpu
, MSR_CORE_C3_RESIDENCY
, &c
->c3
))
883 if (get_msr(cpu
, MSR_CORE_C6_RESIDENCY
, &c
->c6
))
888 if (get_msr(cpu
, MSR_CORE_C7_RESIDENCY
, &c
->c7
))
892 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
894 c
->core_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
898 /* collect package counters only for 1st core in package */
899 if (!(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
902 if (do_nhm_cstates
) {
903 if (get_msr(cpu
, MSR_PKG_C3_RESIDENCY
, &p
->pc3
))
905 if (get_msr(cpu
, MSR_PKG_C6_RESIDENCY
, &p
->pc6
))
908 if (do_snb_cstates
) {
909 if (get_msr(cpu
, MSR_PKG_C2_RESIDENCY
, &p
->pc2
))
911 if (get_msr(cpu
, MSR_PKG_C7_RESIDENCY
, &p
->pc7
))
915 if (get_msr(cpu
, MSR_PKG_C8_RESIDENCY
, &p
->pc8
))
917 if (get_msr(cpu
, MSR_PKG_C9_RESIDENCY
, &p
->pc9
))
919 if (get_msr(cpu
, MSR_PKG_C10_RESIDENCY
, &p
->pc10
))
922 if (do_rapl
& RAPL_PKG
) {
923 if (get_msr(cpu
, MSR_PKG_ENERGY_STATUS
, &msr
))
925 p
->energy_pkg
= msr
& 0xFFFFFFFF;
927 if (do_rapl
& RAPL_CORES
) {
928 if (get_msr(cpu
, MSR_PP0_ENERGY_STATUS
, &msr
))
930 p
->energy_cores
= msr
& 0xFFFFFFFF;
932 if (do_rapl
& RAPL_DRAM
) {
933 if (get_msr(cpu
, MSR_DRAM_ENERGY_STATUS
, &msr
))
935 p
->energy_dram
= msr
& 0xFFFFFFFF;
937 if (do_rapl
& RAPL_GFX
) {
938 if (get_msr(cpu
, MSR_PP1_ENERGY_STATUS
, &msr
))
940 p
->energy_gfx
= msr
& 0xFFFFFFFF;
942 if (do_rapl
& RAPL_PKG_PERF_STATUS
) {
943 if (get_msr(cpu
, MSR_PKG_PERF_STATUS
, &msr
))
945 p
->rapl_pkg_perf_status
= msr
& 0xFFFFFFFF;
947 if (do_rapl
& RAPL_DRAM_PERF_STATUS
) {
948 if (get_msr(cpu
, MSR_DRAM_PERF_STATUS
, &msr
))
950 p
->rapl_dram_perf_status
= msr
& 0xFFFFFFFF;
953 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
955 p
->pkg_temp_c
= tcc_activation_temp
- ((msr
>> 16) & 0x7F);
960 void print_verbose_header(void)
962 unsigned long long msr
;
965 if (!do_nehalem_platform_info
)
968 get_msr(0, MSR_NHM_PLATFORM_INFO
, &msr
);
970 fprintf(stderr
, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr
);
972 ratio
= (msr
>> 40) & 0xFF;
973 fprintf(stderr
, "%d * %.0f = %.0f MHz max efficiency\n",
974 ratio
, bclk
, ratio
* bclk
);
976 ratio
= (msr
>> 8) & 0xFF;
977 fprintf(stderr
, "%d * %.0f = %.0f MHz TSC frequency\n",
978 ratio
, bclk
, ratio
* bclk
);
980 get_msr(0, MSR_IA32_POWER_CTL
, &msr
);
981 fprintf(stderr
, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E: %sabled)\n",
982 msr
, msr
& 0x2 ? "EN" : "DIS");
984 if (!do_ivt_turbo_ratio_limit
)
985 goto print_nhm_turbo_ratio_limits
;
987 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT
, &msr
);
989 fprintf(stderr
, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr
);
991 ratio
= (msr
>> 56) & 0xFF;
993 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
994 ratio
, bclk
, ratio
* bclk
);
996 ratio
= (msr
>> 48) & 0xFF;
998 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
999 ratio
, bclk
, ratio
* bclk
);
1001 ratio
= (msr
>> 40) & 0xFF;
1003 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1004 ratio
, bclk
, ratio
* bclk
);
1006 ratio
= (msr
>> 32) & 0xFF;
1008 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1009 ratio
, bclk
, ratio
* bclk
);
1011 ratio
= (msr
>> 24) & 0xFF;
1013 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1014 ratio
, bclk
, ratio
* bclk
);
1016 ratio
= (msr
>> 16) & 0xFF;
1018 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1019 ratio
, bclk
, ratio
* bclk
);
1021 ratio
= (msr
>> 8) & 0xFF;
1023 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1024 ratio
, bclk
, ratio
* bclk
);
1026 ratio
= (msr
>> 0) & 0xFF;
1028 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1029 ratio
, bclk
, ratio
* bclk
);
1031 print_nhm_turbo_ratio_limits
:
1032 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL
, &msr
);
1034 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1035 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1037 fprintf(stderr
, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr
);
1039 fprintf(stderr
, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1040 (msr
& SNB_C3_AUTO_UNDEMOTE
) ? "UNdemote-C3, " : "",
1041 (msr
& SNB_C1_AUTO_UNDEMOTE
) ? "UNdemote-C1, " : "",
1042 (msr
& NHM_C3_AUTO_DEMOTE
) ? "demote-C3, " : "",
1043 (msr
& NHM_C1_AUTO_DEMOTE
) ? "demote-C1, " : "",
1044 (msr
& (1 << 15)) ? "" : "UN",
1045 (unsigned int)msr
& 7);
1050 fprintf(stderr
, "pc0");
1053 fprintf(stderr
, do_snb_cstates
? "pc2" : "pc0");
1056 fprintf(stderr
, do_snb_cstates
? "pc6-noret" : "pc3");
1059 fprintf(stderr
, "pc6");
1062 fprintf(stderr
, "pc7");
1065 fprintf(stderr
, do_snb_cstates
? "pc7s" : "invalid");
1068 fprintf(stderr
, "unlimited");
1071 fprintf(stderr
, "invalid");
1073 fprintf(stderr
, ")\n");
1075 if (!do_nehalem_turbo_ratio_limit
)
1078 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT
, &msr
);
1080 fprintf(stderr
, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr
);
1082 ratio
= (msr
>> 56) & 0xFF;
1084 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1085 ratio
, bclk
, ratio
* bclk
);
1087 ratio
= (msr
>> 48) & 0xFF;
1089 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1090 ratio
, bclk
, ratio
* bclk
);
1092 ratio
= (msr
>> 40) & 0xFF;
1094 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1095 ratio
, bclk
, ratio
* bclk
);
1097 ratio
= (msr
>> 32) & 0xFF;
1099 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1100 ratio
, bclk
, ratio
* bclk
);
1102 ratio
= (msr
>> 24) & 0xFF;
1104 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1105 ratio
, bclk
, ratio
* bclk
);
1107 ratio
= (msr
>> 16) & 0xFF;
1109 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1110 ratio
, bclk
, ratio
* bclk
);
1112 ratio
= (msr
>> 8) & 0xFF;
1114 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1115 ratio
, bclk
, ratio
* bclk
);
1117 ratio
= (msr
>> 0) & 0xFF;
1119 fprintf(stderr
, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1120 ratio
, bclk
, ratio
* bclk
);
1123 void free_all_buffers(void)
1125 CPU_FREE(cpu_present_set
);
1126 cpu_present_set
= NULL
;
1127 cpu_present_set
= 0;
1129 CPU_FREE(cpu_affinity_set
);
1130 cpu_affinity_set
= NULL
;
1131 cpu_affinity_setsize
= 0;
1139 package_even
= NULL
;
1149 free(output_buffer
);
1150 output_buffer
= NULL
;
1155 * cpu_is_first_sibling_in_core(cpu)
1156 * return 1 if given CPU is 1st HT sibling in the core
1158 int cpu_is_first_sibling_in_core(int cpu
)
1164 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu
);
1165 filep
= fopen(path
, "r");
1166 if (filep
== NULL
) {
1170 fscanf(filep
, "%d", &first_cpu
);
1172 return (cpu
== first_cpu
);
1176 * cpu_is_first_core_in_package(cpu)
1177 * return 1 if given CPU is 1st core in package
1179 int cpu_is_first_core_in_package(int cpu
)
1185 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu
);
1186 filep
= fopen(path
, "r");
1187 if (filep
== NULL
) {
1191 fscanf(filep
, "%d", &first_cpu
);
1193 return (cpu
== first_cpu
);
1196 int get_physical_package_id(int cpu
)
1202 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu
);
1203 filep
= fopen(path
, "r");
1204 if (filep
== NULL
) {
1208 fscanf(filep
, "%d", &pkg
);
1213 int get_core_id(int cpu
)
1219 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu
);
1220 filep
= fopen(path
, "r");
1221 if (filep
== NULL
) {
1225 fscanf(filep
, "%d", &core
);
1230 int get_num_ht_siblings(int cpu
)
1238 sprintf(path
, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu
);
1239 filep
= fopen(path
, "r");
1240 if (filep
== NULL
) {
1246 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1247 * otherwinse 1 sibling (self).
1249 matches
= fscanf(filep
, "%d%c%d\n", &sib1
, &character
, &sib2
);
1260 * run func(thread, core, package) in topology order
1261 * skip non-present cpus
1264 int for_all_cpus_2(int (func
)(struct thread_data
*, struct core_data
*,
1265 struct pkg_data
*, struct thread_data
*, struct core_data
*,
1266 struct pkg_data
*), struct thread_data
*thread_base
,
1267 struct core_data
*core_base
, struct pkg_data
*pkg_base
,
1268 struct thread_data
*thread_base2
, struct core_data
*core_base2
,
1269 struct pkg_data
*pkg_base2
)
1271 int retval
, pkg_no
, core_no
, thread_no
;
1273 for (pkg_no
= 0; pkg_no
< topo
.num_packages
; ++pkg_no
) {
1274 for (core_no
= 0; core_no
< topo
.num_cores_per_pkg
; ++core_no
) {
1275 for (thread_no
= 0; thread_no
<
1276 topo
.num_threads_per_core
; ++thread_no
) {
1277 struct thread_data
*t
, *t2
;
1278 struct core_data
*c
, *c2
;
1279 struct pkg_data
*p
, *p2
;
1281 t
= GET_THREAD(thread_base
, thread_no
, core_no
, pkg_no
);
1283 if (cpu_is_not_present(t
->cpu_id
))
1286 t2
= GET_THREAD(thread_base2
, thread_no
, core_no
, pkg_no
);
1288 c
= GET_CORE(core_base
, core_no
, pkg_no
);
1289 c2
= GET_CORE(core_base2
, core_no
, pkg_no
);
1291 p
= GET_PKG(pkg_base
, pkg_no
);
1292 p2
= GET_PKG(pkg_base2
, pkg_no
);
1294 retval
= func(t
, c
, p
, t2
, c2
, p2
);
1304 * run func(cpu) on every cpu in /proc/stat
1305 * return max_cpu number
1307 int for_all_proc_cpus(int (func
)(int))
1313 fp
= fopen(proc_stat
, "r");
1319 retval
= fscanf(fp
, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1321 perror("/proc/stat format");
1326 retval
= fscanf(fp
, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num
);
1330 retval
= func(cpu_num
);
1340 void re_initialize(void)
1343 setup_all_buffers();
1344 printf("turbostat: re-initialized with num_cpus %d\n", topo
.num_cpus
);
1350 * remember the last one seen, it will be the max
1352 int count_cpus(int cpu
)
1354 if (topo
.max_cpu_num
< cpu
)
1355 topo
.max_cpu_num
= cpu
;
1360 int mark_cpu_present(int cpu
)
1362 CPU_SET_S(cpu
, cpu_present_setsize
, cpu_present_set
);
1366 void turbostat_loop()
1374 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
1377 } else if (retval
== -1) {
1378 if (restarted
> 1) {
1385 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
1388 if (for_all_proc_cpus(cpu_is_not_present
)) {
1392 sleep(interval_sec
);
1393 retval
= for_all_cpus(get_counters
, ODD_COUNTERS
);
1396 } else if (retval
== -1) {
1400 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
1401 timersub(&tv_odd
, &tv_even
, &tv_delta
);
1402 for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
);
1403 compute_average(EVEN_COUNTERS
);
1404 format_all_counters(EVEN_COUNTERS
);
1406 sleep(interval_sec
);
1407 retval
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
1410 } else if (retval
== -1) {
1414 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
1415 timersub(&tv_even
, &tv_odd
, &tv_delta
);
1416 for_all_cpus_2(delta_cpu
, EVEN_COUNTERS
, ODD_COUNTERS
);
1417 compute_average(ODD_COUNTERS
);
1418 format_all_counters(ODD_COUNTERS
);
1423 void check_dev_msr()
1427 if (stat("/dev/cpu/0/msr", &sb
)) {
1428 fprintf(stderr
, "no /dev/cpu/0/msr\n");
1429 fprintf(stderr
, "Try \"# modprobe msr\"\n");
1434 void check_super_user()
1436 if (getuid() != 0) {
1437 fprintf(stderr
, "must be root\n");
1442 int has_nehalem_turbo_ratio_limit(unsigned int family
, unsigned int model
)
1451 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1452 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1453 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1454 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1455 case 0x2C: /* Westmere EP - Gulftown */
1456 case 0x2A: /* SNB */
1457 case 0x2D: /* SNB Xeon */
1458 case 0x3A: /* IVB */
1459 case 0x3E: /* IVB Xeon */
1460 case 0x3C: /* HSW */
1461 case 0x3F: /* HSW */
1462 case 0x45: /* HSW */
1463 case 0x46: /* HSW */
1465 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1466 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1471 int has_ivt_turbo_ratio_limit(unsigned int family
, unsigned int model
)
1480 case 0x3E: /* IVB Xeon */
1489 * Decode the ENERGY_PERF_BIAS MSR
1491 int print_epb(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1493 unsigned long long msr
;
1502 /* EPB is per-package */
1503 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1506 if (cpu_migrate(cpu
)) {
1507 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1511 if (get_msr(cpu
, MSR_IA32_ENERGY_PERF_BIAS
, &msr
))
1514 switch (msr
& 0x7) {
1515 case ENERGY_PERF_BIAS_PERFORMANCE
:
1516 epb_string
= "performance";
1518 case ENERGY_PERF_BIAS_NORMAL
:
1519 epb_string
= "balanced";
1521 case ENERGY_PERF_BIAS_POWERSAVE
:
1522 epb_string
= "powersave";
1525 epb_string
= "custom";
1528 fprintf(stderr
, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu
, msr
, epb_string
);
1533 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1534 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1541 void rapl_probe(unsigned int family
, unsigned int model
)
1543 unsigned long long msr
;
1555 case 0x3C: /* HSW */
1556 case 0x3F: /* HSW */
1557 case 0x45: /* HSW */
1558 case 0x46: /* HSW */
1559 do_rapl
= RAPL_PKG
| RAPL_CORES
| RAPL_GFX
;
1563 do_rapl
= RAPL_PKG
| RAPL_CORES
| RAPL_DRAM
| RAPL_PKG_PERF_STATUS
| RAPL_DRAM_PERF_STATUS
;
1569 /* units on package 0, verify later other packages match */
1570 if (get_msr(0, MSR_RAPL_POWER_UNIT
, &msr
))
1573 rapl_power_units
= 1.0 / (1 << (msr
& 0xF));
1574 rapl_energy_units
= 1.0 / (1 << (msr
>> 8 & 0x1F));
1575 rapl_time_units
= 1.0 / (1 << (msr
>> 16 & 0xF));
1577 /* get TDP to determine energy counter range */
1578 if (get_msr(0, MSR_PKG_POWER_INFO
, &msr
))
1581 tdp
= ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
;
1583 rapl_joule_counter_range
= 0xFFFFFFFF * rapl_energy_units
/ tdp
;
1586 fprintf(stderr
, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range
);
1591 int print_thermal(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1593 unsigned long long msr
;
1597 if (!(do_dts
|| do_ptm
))
1602 /* DTS is per-core, no need to print for each thread */
1603 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
))
1606 if (cpu_migrate(cpu
)) {
1607 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1611 if (do_ptm
&& (t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
)) {
1612 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
, &msr
))
1615 dts
= (msr
>> 16) & 0x7F;
1616 fprintf(stderr
, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1617 cpu
, msr
, tcc_activation_temp
- dts
);
1620 if (get_msr(cpu
, MSR_IA32_PACKAGE_THERM_INTERRUPT
, &msr
))
1623 dts
= (msr
>> 16) & 0x7F;
1624 dts2
= (msr
>> 8) & 0x7F;
1625 fprintf(stderr
, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1626 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
1632 unsigned int resolution
;
1634 if (get_msr(cpu
, MSR_IA32_THERM_STATUS
, &msr
))
1637 dts
= (msr
>> 16) & 0x7F;
1638 resolution
= (msr
>> 27) & 0xF;
1639 fprintf(stderr
, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1640 cpu
, msr
, tcc_activation_temp
- dts
, resolution
);
1643 if (get_msr(cpu
, MSR_IA32_THERM_INTERRUPT
, &msr
))
1646 dts
= (msr
>> 16) & 0x7F;
1647 dts2
= (msr
>> 8) & 0x7F;
1648 fprintf(stderr
, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1649 cpu
, msr
, tcc_activation_temp
- dts
, tcc_activation_temp
- dts2
);
1656 void print_power_limit_msr(int cpu
, unsigned long long msr
, char *label
)
1658 fprintf(stderr
, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1660 ((msr
>> 15) & 1) ? "EN" : "DIS",
1661 ((msr
>> 0) & 0x7FFF) * rapl_power_units
,
1662 (1.0 + (((msr
>> 22) & 0x3)/4.0)) * (1 << ((msr
>> 17) & 0x1F)) * rapl_time_units
,
1663 (((msr
>> 16) & 1) ? "EN" : "DIS"));
1668 int print_rapl(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1670 unsigned long long msr
;
1672 double local_rapl_power_units
, local_rapl_energy_units
, local_rapl_time_units
;
1677 /* RAPL counters are per package, so print only for 1st thread/package */
1678 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1682 if (cpu_migrate(cpu
)) {
1683 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1687 if (get_msr(cpu
, MSR_RAPL_POWER_UNIT
, &msr
))
1690 local_rapl_power_units
= 1.0 / (1 << (msr
& 0xF));
1691 local_rapl_energy_units
= 1.0 / (1 << (msr
>> 8 & 0x1F));
1692 local_rapl_time_units
= 1.0 / (1 << (msr
>> 16 & 0xF));
1694 if (local_rapl_power_units
!= rapl_power_units
)
1695 fprintf(stderr
, "cpu%d, ERROR: Power units mis-match\n", cpu
);
1696 if (local_rapl_energy_units
!= rapl_energy_units
)
1697 fprintf(stderr
, "cpu%d, ERROR: Energy units mis-match\n", cpu
);
1698 if (local_rapl_time_units
!= rapl_time_units
)
1699 fprintf(stderr
, "cpu%d, ERROR: Time units mis-match\n", cpu
);
1702 fprintf(stderr
, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1703 "(%f Watts, %f Joules, %f sec.)\n", cpu
, msr
,
1704 local_rapl_power_units
, local_rapl_energy_units
, local_rapl_time_units
);
1706 if (do_rapl
& RAPL_PKG
) {
1707 if (get_msr(cpu
, MSR_PKG_POWER_INFO
, &msr
))
1711 fprintf(stderr
, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1713 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1714 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1715 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1716 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
1718 if (get_msr(cpu
, MSR_PKG_POWER_LIMIT
, &msr
))
1721 fprintf(stderr
, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1722 cpu
, msr
, (msr
>> 63) & 1 ? "": "UN");
1724 print_power_limit_msr(cpu
, msr
, "PKG Limit #1");
1725 fprintf(stderr
, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1727 ((msr
>> 47) & 1) ? "EN" : "DIS",
1728 ((msr
>> 32) & 0x7FFF) * rapl_power_units
,
1729 (1.0 + (((msr
>> 54) & 0x3)/4.0)) * (1 << ((msr
>> 49) & 0x1F)) * rapl_time_units
,
1730 ((msr
>> 48) & 1) ? "EN" : "DIS");
1733 if (do_rapl
& RAPL_DRAM
) {
1734 if (get_msr(cpu
, MSR_DRAM_POWER_INFO
, &msr
))
1738 fprintf(stderr
, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1740 ((msr
>> 0) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1741 ((msr
>> 16) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1742 ((msr
>> 32) & RAPL_POWER_GRANULARITY
) * rapl_power_units
,
1743 ((msr
>> 48) & RAPL_TIME_GRANULARITY
) * rapl_time_units
);
1746 if (get_msr(cpu
, MSR_DRAM_POWER_LIMIT
, &msr
))
1748 fprintf(stderr
, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1749 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
1751 print_power_limit_msr(cpu
, msr
, "DRAM Limit");
1753 if (do_rapl
& RAPL_CORES
) {
1755 if (get_msr(cpu
, MSR_PP0_POLICY
, &msr
))
1758 fprintf(stderr
, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu
, msr
& 0xF);
1760 if (get_msr(cpu
, MSR_PP0_POWER_LIMIT
, &msr
))
1762 fprintf(stderr
, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1763 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
1764 print_power_limit_msr(cpu
, msr
, "Cores Limit");
1767 if (do_rapl
& RAPL_GFX
) {
1769 if (get_msr(cpu
, MSR_PP1_POLICY
, &msr
))
1772 fprintf(stderr
, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu
, msr
& 0xF);
1774 if (get_msr(cpu
, MSR_PP1_POWER_LIMIT
, &msr
))
1776 fprintf(stderr
, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1777 cpu
, msr
, (msr
>> 31) & 1 ? "": "UN");
1778 print_power_limit_msr(cpu
, msr
, "GFX Limit");
1785 int is_snb(unsigned int family
, unsigned int model
)
1793 case 0x3A: /* IVB */
1794 case 0x3E: /* IVB Xeon */
1795 case 0x3C: /* HSW */
1796 case 0x3F: /* HSW */
1797 case 0x45: /* HSW */
1798 case 0x46: /* HSW */
1804 int has_c8_c9_c10(unsigned int family
, unsigned int model
)
1817 double discover_bclk(unsigned int family
, unsigned int model
)
1819 if (is_snb(family
, model
))
1826 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1827 * the Thermal Control Circuit (TCC) activates.
1828 * This is usually equal to tjMax.
1830 * Older processors do not have this MSR, so there we guess,
1831 * but also allow cmdline over-ride with -T.
1833 * Several MSR temperature values are in units of degrees-C
1834 * below this value, including the Digital Thermal Sensor (DTS),
1835 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1837 int set_temperature_target(struct thread_data
*t
, struct core_data
*c
, struct pkg_data
*p
)
1839 unsigned long long msr
;
1840 unsigned int target_c_local
;
1843 /* tcc_activation_temp is used only for dts or ptm */
1844 if (!(do_dts
|| do_ptm
))
1847 /* this is a per-package concept */
1848 if (!(t
->flags
& CPU_IS_FIRST_THREAD_IN_CORE
) || !(t
->flags
& CPU_IS_FIRST_CORE_IN_PACKAGE
))
1852 if (cpu_migrate(cpu
)) {
1853 fprintf(stderr
, "Could not migrate to CPU %d\n", cpu
);
1857 if (tcc_activation_temp_override
!= 0) {
1858 tcc_activation_temp
= tcc_activation_temp_override
;
1859 fprintf(stderr
, "cpu%d: Using cmdline TCC Target (%d C)\n",
1860 cpu
, tcc_activation_temp
);
1864 /* Temperature Target MSR is Nehalem and newer only */
1865 if (!do_nehalem_platform_info
)
1868 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET
, &msr
))
1871 target_c_local
= (msr
>> 16) & 0x7F;
1874 fprintf(stderr
, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1875 cpu
, msr
, target_c_local
);
1877 if (target_c_local
< 85 || target_c_local
> 120)
1880 tcc_activation_temp
= target_c_local
;
1885 tcc_activation_temp
= TJMAX_DEFAULT
;
1886 fprintf(stderr
, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1887 cpu
, tcc_activation_temp
);
1893 unsigned int eax
, ebx
, ecx
, edx
, max_level
;
1894 unsigned int fms
, family
, model
, stepping
;
1896 eax
= ebx
= ecx
= edx
= 0;
1898 __get_cpuid(0, &max_level
, &ebx
, &ecx
, &edx
);
1900 if (ebx
== 0x756e6547 && edx
== 0x49656e69 && ecx
== 0x6c65746e)
1904 fprintf(stderr
, "CPUID(0): %.4s%.4s%.4s ",
1905 (char *)&ebx
, (char *)&edx
, (char *)&ecx
);
1907 __get_cpuid(1, &fms
, &ebx
, &ecx
, &edx
);
1908 family
= (fms
>> 8) & 0xf;
1909 model
= (fms
>> 4) & 0xf;
1910 stepping
= fms
& 0xf;
1911 if (family
== 6 || family
== 0xf)
1912 model
+= ((fms
>> 16) & 0xf) << 4;
1915 fprintf(stderr
, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1916 max_level
, family
, model
, stepping
, family
, model
, stepping
);
1918 if (!(edx
& (1 << 5))) {
1919 fprintf(stderr
, "CPUID: no MSR\n");
1924 * check max extended function levels of CPUID.
1925 * This is needed to check for invariant TSC.
1926 * This check is valid for both Intel and AMD.
1928 ebx
= ecx
= edx
= 0;
1929 __get_cpuid(0x80000000, &max_level
, &ebx
, &ecx
, &edx
);
1931 if (max_level
< 0x80000007) {
1932 fprintf(stderr
, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level
);
1937 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1938 * this check is valid for both Intel and AMD
1940 __get_cpuid(0x80000007, &eax
, &ebx
, &ecx
, &edx
);
1941 has_invariant_tsc
= edx
& (1 << 8);
1943 if (!has_invariant_tsc
) {
1944 fprintf(stderr
, "No invariant TSC\n");
1949 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1950 * this check is valid for both Intel and AMD
1953 __get_cpuid(0x6, &eax
, &ebx
, &ecx
, &edx
);
1954 has_aperf
= ecx
& (1 << 0);
1955 do_dts
= eax
& (1 << 0);
1956 do_ptm
= eax
& (1 << 6);
1957 has_epb
= ecx
& (1 << 3);
1960 fprintf(stderr
, "CPUID(6): %s%s%s%s\n",
1961 has_aperf
? "APERF" : "No APERF!",
1962 do_dts
? ", DTS" : "",
1963 do_ptm
? ", PTM": "",
1964 has_epb
? ", EPB": "");
1969 do_nehalem_platform_info
= genuine_intel
&& has_invariant_tsc
;
1970 do_nhm_cstates
= genuine_intel
; /* all Intel w/ non-stop TSC have NHM counters */
1971 do_smi
= do_nhm_cstates
;
1972 do_snb_cstates
= is_snb(family
, model
);
1973 do_c8_c9_c10
= has_c8_c9_c10(family
, model
);
1974 bclk
= discover_bclk(family
, model
);
1976 do_nehalem_turbo_ratio_limit
= has_nehalem_turbo_ratio_limit(family
, model
);
1977 do_ivt_turbo_ratio_limit
= has_ivt_turbo_ratio_limit(family
, model
);
1978 rapl_probe(family
, model
);
1986 fprintf(stderr
, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
1993 * in /dev/cpu/ return success for names that are numbers
1994 * ie. filter out ".", "..", "microcode".
1996 int dir_filter(const struct dirent
*dirp
)
1998 if (isdigit(dirp
->d_name
[0]))
2004 int open_dev_cpu_msr(int dummy1
)
2009 void topology_probe()
2012 int max_core_id
= 0;
2013 int max_package_id
= 0;
2014 int max_siblings
= 0;
2015 struct cpu_topology
{
2017 int physical_package_id
;
2020 /* Initialize num_cpus, max_cpu_num */
2022 topo
.max_cpu_num
= 0;
2023 for_all_proc_cpus(count_cpus
);
2024 if (!summary_only
&& topo
.num_cpus
> 1)
2028 fprintf(stderr
, "num_cpus %d max_cpu_num %d\n", topo
.num_cpus
, topo
.max_cpu_num
);
2030 cpus
= calloc(1, (topo
.max_cpu_num
+ 1) * sizeof(struct cpu_topology
));
2032 perror("calloc cpus");
2037 * Allocate and initialize cpu_present_set
2039 cpu_present_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
2040 if (cpu_present_set
== NULL
) {
2041 perror("CPU_ALLOC");
2044 cpu_present_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
2045 CPU_ZERO_S(cpu_present_setsize
, cpu_present_set
);
2046 for_all_proc_cpus(mark_cpu_present
);
2049 * Allocate and initialize cpu_affinity_set
2051 cpu_affinity_set
= CPU_ALLOC((topo
.max_cpu_num
+ 1));
2052 if (cpu_affinity_set
== NULL
) {
2053 perror("CPU_ALLOC");
2056 cpu_affinity_setsize
= CPU_ALLOC_SIZE((topo
.max_cpu_num
+ 1));
2057 CPU_ZERO_S(cpu_affinity_setsize
, cpu_affinity_set
);
2062 * find max_core_id, max_package_id
2064 for (i
= 0; i
<= topo
.max_cpu_num
; ++i
) {
2067 if (cpu_is_not_present(i
)) {
2069 fprintf(stderr
, "cpu%d NOT PRESENT\n", i
);
2072 cpus
[i
].core_id
= get_core_id(i
);
2073 if (cpus
[i
].core_id
> max_core_id
)
2074 max_core_id
= cpus
[i
].core_id
;
2076 cpus
[i
].physical_package_id
= get_physical_package_id(i
);
2077 if (cpus
[i
].physical_package_id
> max_package_id
)
2078 max_package_id
= cpus
[i
].physical_package_id
;
2080 siblings
= get_num_ht_siblings(i
);
2081 if (siblings
> max_siblings
)
2082 max_siblings
= siblings
;
2084 fprintf(stderr
, "cpu %d pkg %d core %d\n",
2085 i
, cpus
[i
].physical_package_id
, cpus
[i
].core_id
);
2087 topo
.num_cores_per_pkg
= max_core_id
+ 1;
2089 fprintf(stderr
, "max_core_id %d, sizing for %d cores per package\n",
2090 max_core_id
, topo
.num_cores_per_pkg
);
2091 if (!summary_only
&& topo
.num_cores_per_pkg
> 1)
2094 topo
.num_packages
= max_package_id
+ 1;
2096 fprintf(stderr
, "max_package_id %d, sizing for %d packages\n",
2097 max_package_id
, topo
.num_packages
);
2098 if (!summary_only
&& topo
.num_packages
> 1)
2101 topo
.num_threads_per_core
= max_siblings
;
2103 fprintf(stderr
, "max_siblings %d\n", max_siblings
);
2109 allocate_counters(struct thread_data
**t
, struct core_data
**c
, struct pkg_data
**p
)
2113 *t
= calloc(topo
.num_threads_per_core
* topo
.num_cores_per_pkg
*
2114 topo
.num_packages
, sizeof(struct thread_data
));
2118 for (i
= 0; i
< topo
.num_threads_per_core
*
2119 topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
2120 (*t
)[i
].cpu_id
= -1;
2122 *c
= calloc(topo
.num_cores_per_pkg
* topo
.num_packages
,
2123 sizeof(struct core_data
));
2127 for (i
= 0; i
< topo
.num_cores_per_pkg
* topo
.num_packages
; i
++)
2128 (*c
)[i
].core_id
= -1;
2130 *p
= calloc(topo
.num_packages
, sizeof(struct pkg_data
));
2134 for (i
= 0; i
< topo
.num_packages
; i
++)
2135 (*p
)[i
].package_id
= i
;
2139 perror("calloc counters");
2145 * set cpu_id, core_num, pkg_num
2146 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2148 * increment topo.num_cores when 1st core in pkg seen
2150 void init_counter(struct thread_data
*thread_base
, struct core_data
*core_base
,
2151 struct pkg_data
*pkg_base
, int thread_num
, int core_num
,
2152 int pkg_num
, int cpu_id
)
2154 struct thread_data
*t
;
2155 struct core_data
*c
;
2158 t
= GET_THREAD(thread_base
, thread_num
, core_num
, pkg_num
);
2159 c
= GET_CORE(core_base
, core_num
, pkg_num
);
2160 p
= GET_PKG(pkg_base
, pkg_num
);
2163 if (thread_num
== 0) {
2164 t
->flags
|= CPU_IS_FIRST_THREAD_IN_CORE
;
2165 if (cpu_is_first_core_in_package(cpu_id
))
2166 t
->flags
|= CPU_IS_FIRST_CORE_IN_PACKAGE
;
2169 c
->core_id
= core_num
;
2170 p
->package_id
= pkg_num
;
2174 int initialize_counters(int cpu_id
)
2176 int my_thread_id
, my_core_id
, my_package_id
;
2178 my_package_id
= get_physical_package_id(cpu_id
);
2179 my_core_id
= get_core_id(cpu_id
);
2181 if (cpu_is_first_sibling_in_core(cpu_id
)) {
2188 init_counter(EVEN_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
2189 init_counter(ODD_COUNTERS
, my_thread_id
, my_core_id
, my_package_id
, cpu_id
);
2193 void allocate_output_buffer()
2195 output_buffer
= calloc(1, (1 + topo
.num_cpus
) * 256);
2196 outp
= output_buffer
;
2203 void setup_all_buffers(void)
2206 allocate_counters(&thread_even
, &core_even
, &package_even
);
2207 allocate_counters(&thread_odd
, &core_odd
, &package_odd
);
2208 allocate_output_buffer();
2209 for_all_proc_cpus(initialize_counters
);
2211 void turbostat_init()
2218 setup_all_buffers();
2221 print_verbose_header();
2224 for_all_cpus(print_epb
, ODD_COUNTERS
);
2227 for_all_cpus(print_rapl
, ODD_COUNTERS
);
2229 for_all_cpus(set_temperature_target
, ODD_COUNTERS
);
2232 for_all_cpus(print_thermal
, ODD_COUNTERS
);
2235 int fork_it(char **argv
)
2240 status
= for_all_cpus(get_counters
, EVEN_COUNTERS
);
2243 /* clear affinity side-effect of get_counters() */
2244 sched_setaffinity(0, cpu_present_setsize
, cpu_present_set
);
2245 gettimeofday(&tv_even
, (struct timezone
*)NULL
);
2250 execvp(argv
[0], argv
);
2254 if (child_pid
== -1) {
2259 signal(SIGINT
, SIG_IGN
);
2260 signal(SIGQUIT
, SIG_IGN
);
2261 if (waitpid(child_pid
, &status
, 0) == -1) {
2267 * n.b. fork_it() does not check for errors from for_all_cpus()
2268 * because re-starting is problematic when forking
2270 for_all_cpus(get_counters
, ODD_COUNTERS
);
2271 gettimeofday(&tv_odd
, (struct timezone
*)NULL
);
2272 timersub(&tv_odd
, &tv_even
, &tv_delta
);
2273 for_all_cpus_2(delta_cpu
, ODD_COUNTERS
, EVEN_COUNTERS
);
2274 compute_average(EVEN_COUNTERS
);
2275 format_all_counters(EVEN_COUNTERS
);
2278 fprintf(stderr
, "%.6f sec\n", tv_delta
.tv_sec
+ tv_delta
.tv_usec
/1000000.0);
2283 void cmdline(int argc
, char **argv
)
2289 while ((opt
= getopt(argc
, argv
, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
2304 interval_sec
= atoi(optarg
);
2307 sscanf(optarg
, "%x", &extra_delta_offset32
);
2310 sscanf(optarg
, "%x", &extra_delta_offset64
);
2313 sscanf(optarg
, "%x", &extra_msr_offset32
);
2316 sscanf(optarg
, "%x", &extra_msr_offset64
);
2322 tcc_activation_temp_override
= atoi(optarg
);
2330 int main(int argc
, char **argv
)
2332 cmdline(argc
, argv
);
2335 fprintf(stderr
, "turbostat v3.4 April 17, 2013"
2336 " - Len Brown <lenb@kernel.org>\n");
2341 * if any params left, it must be a command to fork
2344 return fork_it(argv
+ optind
);