2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
5 * Copyright (c) 2013 Intel Corporation.
6 * Len Brown <len.brown@intel.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
31 #include <sys/resource.h>
42 #include <linux/capability.h>
45 char *proc_stat = "/proc/stat";
46 unsigned int interval_sec = 5;
48 unsigned int rapl_joules;
49 unsigned int summary_only;
50 unsigned int dump_only;
53 unsigned int do_nhm_cstates;
54 unsigned int do_snb_cstates;
55 unsigned int do_knl_cstates;
60 unsigned int do_c8_c9_c10;
61 unsigned int do_skl_residency;
62 unsigned int do_slm_cstates;
63 unsigned int use_c1_residency_msr;
64 unsigned int has_aperf;
66 unsigned int units = 1000000; /* MHz etc */
67 unsigned int genuine_intel;
68 unsigned int has_invariant_tsc;
69 unsigned int do_nhm_platform_info;
70 unsigned int extra_msr_offset32;
71 unsigned int extra_msr_offset64;
72 unsigned int extra_delta_offset32;
73 unsigned int extra_delta_offset64;
76 unsigned int show_pkg;
77 unsigned int show_core;
78 unsigned int show_cpu;
79 unsigned int show_pkg_only;
80 unsigned int show_core_only;
81 char *output_buffer, *outp;
85 unsigned int tcc_activation_temp;
86 unsigned int tcc_activation_temp_override;
87 double rapl_power_units, rapl_time_units;
88 double rapl_dram_energy_units, rapl_energy_units;
89 double rapl_joule_counter_range;
90 unsigned int do_core_perf_limit_reasons;
91 unsigned int do_gfx_perf_limit_reasons;
92 unsigned int do_ring_perf_limit_reasons;
93 unsigned int crystal_hz;
94 unsigned long long tsc_hz;
96 #define RAPL_PKG (1 << 0)
97 /* 0x610 MSR_PKG_POWER_LIMIT */
98 /* 0x611 MSR_PKG_ENERGY_STATUS */
99 #define RAPL_PKG_PERF_STATUS (1 << 1)
100 /* 0x613 MSR_PKG_PERF_STATUS */
101 #define RAPL_PKG_POWER_INFO (1 << 2)
102 /* 0x614 MSR_PKG_POWER_INFO */
104 #define RAPL_DRAM (1 << 3)
105 /* 0x618 MSR_DRAM_POWER_LIMIT */
106 /* 0x619 MSR_DRAM_ENERGY_STATUS */
107 #define RAPL_DRAM_PERF_STATUS (1 << 4)
108 /* 0x61b MSR_DRAM_PERF_STATUS */
109 #define RAPL_DRAM_POWER_INFO (1 << 5)
110 /* 0x61c MSR_DRAM_POWER_INFO */
112 #define RAPL_CORES (1 << 6)
113 /* 0x638 MSR_PP0_POWER_LIMIT */
114 /* 0x639 MSR_PP0_ENERGY_STATUS */
115 #define RAPL_CORE_POLICY (1 << 7)
116 /* 0x63a MSR_PP0_POLICY */
118 #define RAPL_GFX (1 << 8)
119 /* 0x640 MSR_PP1_POWER_LIMIT */
120 /* 0x641 MSR_PP1_ENERGY_STATUS */
121 /* 0x642 MSR_PP1_POLICY */
122 #define TJMAX_DEFAULT 100
124 #define MAX(a, b) ((a) > (b) ? (a) : (b))
126 int aperf_mperf_unstable;
130 cpu_set_t *cpu_present_set, *cpu_affinity_set;
131 size_t cpu_present_setsize, cpu_affinity_setsize;
134 unsigned long long tsc;
135 unsigned long long aperf;
136 unsigned long long mperf;
137 unsigned long long c1;
138 unsigned long long extra_msr64;
139 unsigned long long extra_delta64;
140 unsigned long long extra_msr32;
141 unsigned long long extra_delta32;
142 unsigned int smi_count;
145 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
146 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
147 } *thread_even, *thread_odd;
150 unsigned long long c3;
151 unsigned long long c6;
152 unsigned long long c7;
153 unsigned int core_temp_c;
154 unsigned int core_id;
155 } *core_even, *core_odd;
158 unsigned long long pc2;
159 unsigned long long pc3;
160 unsigned long long pc6;
161 unsigned long long pc7;
162 unsigned long long pc8;
163 unsigned long long pc9;
164 unsigned long long pc10;
165 unsigned long long pkg_wtd_core_c0;
166 unsigned long long pkg_any_core_c0;
167 unsigned long long pkg_any_gfxe_c0;
168 unsigned long long pkg_both_core_gfxe_c0;
169 unsigned int package_id;
170 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
171 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
172 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
173 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
174 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
175 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
176 unsigned int pkg_temp_c;
178 } *package_even, *package_odd;
180 #define ODD_COUNTERS thread_odd, core_odd, package_odd
181 #define EVEN_COUNTERS thread_even, core_even, package_even
183 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
184 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
185 topo.num_threads_per_core + \
186 (core_no) * topo.num_threads_per_core + (thread_no))
187 #define GET_CORE(core_base, core_no, pkg_no) \
188 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
189 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
191 struct system_summary {
192 struct thread_data threads;
193 struct core_data cores;
194 struct pkg_data packages;
203 int num_cores_per_pkg;
204 int num_threads_per_core;
207 struct timeval tv_even, tv_odd, tv_delta;
209 void setup_all_buffers(void);
211 int cpu_is_not_present(int cpu)
213 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
216 * run func(thread, core, package) in topology order
217 * skip non-present cpus
220 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
221 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
223 int retval, pkg_no, core_no, thread_no;
225 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
226 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
227 for (thread_no = 0; thread_no <
228 topo.num_threads_per_core; ++thread_no) {
229 struct thread_data *t;
233 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
235 if (cpu_is_not_present(t->cpu_id))
238 c = GET_CORE(core_base, core_no, pkg_no);
239 p = GET_PKG(pkg_base, pkg_no);
241 retval = func(t, c, p);
250 int cpu_migrate(int cpu)
252 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
253 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
254 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
260 int get_msr(int cpu, off_t offset, unsigned long long *msr)
266 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
267 fd = open(pathname, O_RDONLY);
269 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
271 retval = pread(fd, msr, sizeof *msr, offset);
274 if (retval != sizeof *msr)
275 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
281 * Example Format w/ field column widths:
283 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
284 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
287 void print_header(void)
290 outp += sprintf(outp, " Package");
292 outp += sprintf(outp, " Core");
294 outp += sprintf(outp, " CPU");
296 outp += sprintf(outp, " Avg_MHz");
298 outp += sprintf(outp, " %%Busy");
300 outp += sprintf(outp, " Bzy_MHz");
301 outp += sprintf(outp, " TSC_MHz");
303 if (extra_delta_offset32)
304 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
305 if (extra_delta_offset64)
306 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
307 if (extra_msr_offset32)
308 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
309 if (extra_msr_offset64)
310 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
316 outp += sprintf(outp, " SMI");
319 outp += sprintf(outp, " CPU%%c1");
320 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
321 outp += sprintf(outp, " CPU%%c3");
323 outp += sprintf(outp, " CPU%%c6");
325 outp += sprintf(outp, " CPU%%c7");
328 outp += sprintf(outp, " CoreTmp");
330 outp += sprintf(outp, " PkgTmp");
332 if (do_skl_residency) {
333 outp += sprintf(outp, " Totl%%C0");
334 outp += sprintf(outp, " Any%%C0");
335 outp += sprintf(outp, " GFX%%C0");
336 outp += sprintf(outp, " CPUGFX%%");
340 outp += sprintf(outp, " Pkg%%pc2");
342 outp += sprintf(outp, " Pkg%%pc3");
344 outp += sprintf(outp, " Pkg%%pc6");
346 outp += sprintf(outp, " Pkg%%pc7");
348 outp += sprintf(outp, " Pkg%%pc8");
349 outp += sprintf(outp, " Pkg%%pc9");
350 outp += sprintf(outp, " Pk%%pc10");
353 if (do_rapl && !rapl_joules) {
354 if (do_rapl & RAPL_PKG)
355 outp += sprintf(outp, " PkgWatt");
356 if (do_rapl & RAPL_CORES)
357 outp += sprintf(outp, " CorWatt");
358 if (do_rapl & RAPL_GFX)
359 outp += sprintf(outp, " GFXWatt");
360 if (do_rapl & RAPL_DRAM)
361 outp += sprintf(outp, " RAMWatt");
362 if (do_rapl & RAPL_PKG_PERF_STATUS)
363 outp += sprintf(outp, " PKG_%%");
364 if (do_rapl & RAPL_DRAM_PERF_STATUS)
365 outp += sprintf(outp, " RAM_%%");
366 } else if (do_rapl && rapl_joules) {
367 if (do_rapl & RAPL_PKG)
368 outp += sprintf(outp, " Pkg_J");
369 if (do_rapl & RAPL_CORES)
370 outp += sprintf(outp, " Cor_J");
371 if (do_rapl & RAPL_GFX)
372 outp += sprintf(outp, " GFX_J");
373 if (do_rapl & RAPL_DRAM)
374 outp += sprintf(outp, " RAM_W");
375 if (do_rapl & RAPL_PKG_PERF_STATUS)
376 outp += sprintf(outp, " PKG_%%");
377 if (do_rapl & RAPL_DRAM_PERF_STATUS)
378 outp += sprintf(outp, " RAM_%%");
379 outp += sprintf(outp, " time");
383 outp += sprintf(outp, "\n");
386 int dump_counters(struct thread_data *t, struct core_data *c,
389 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
392 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
393 t->cpu_id, t->flags);
394 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
395 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
396 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
397 outp += sprintf(outp, "c1: %016llX\n", t->c1);
398 outp += sprintf(outp, "msr0x%x: %08llX\n",
399 extra_delta_offset32, t->extra_delta32);
400 outp += sprintf(outp, "msr0x%x: %016llX\n",
401 extra_delta_offset64, t->extra_delta64);
402 outp += sprintf(outp, "msr0x%x: %08llX\n",
403 extra_msr_offset32, t->extra_msr32);
404 outp += sprintf(outp, "msr0x%x: %016llX\n",
405 extra_msr_offset64, t->extra_msr64);
407 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
411 outp += sprintf(outp, "core: %d\n", c->core_id);
412 outp += sprintf(outp, "c3: %016llX\n", c->c3);
413 outp += sprintf(outp, "c6: %016llX\n", c->c6);
414 outp += sprintf(outp, "c7: %016llX\n", c->c7);
415 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
419 outp += sprintf(outp, "package: %d\n", p->package_id);
421 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
422 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
423 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
424 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
426 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
428 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
430 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
432 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
433 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
434 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
435 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
436 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
437 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
438 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
439 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
440 outp += sprintf(outp, "Throttle PKG: %0X\n",
441 p->rapl_pkg_perf_status);
442 outp += sprintf(outp, "Throttle RAM: %0X\n",
443 p->rapl_dram_perf_status);
444 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
447 outp += sprintf(outp, "\n");
453 * column formatting convention & formats
455 int format_counters(struct thread_data *t, struct core_data *c,
458 double interval_float;
461 /* if showing only 1st thread in core and this isn't one, bail out */
462 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
465 /* if showing only 1st thread in pkg and this isn't one, bail out */
466 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
469 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
471 /* topo columns, print blanks on 1st (average) line */
472 if (t == &average.threads) {
474 outp += sprintf(outp, " -");
476 outp += sprintf(outp, " -");
478 outp += sprintf(outp, " -");
482 outp += sprintf(outp, "%8d", p->package_id);
484 outp += sprintf(outp, " -");
488 outp += sprintf(outp, "%8d", c->core_id);
490 outp += sprintf(outp, " -");
493 outp += sprintf(outp, "%8d", t->cpu_id);
498 outp += sprintf(outp, "%8.0f",
499 1.0 / units * t->aperf / interval_float);
504 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
506 outp += sprintf(outp, "********");
511 outp += sprintf(outp, "%8.0f",
512 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
515 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
518 if (extra_delta_offset32)
519 outp += sprintf(outp, " %11llu", t->extra_delta32);
522 if (extra_delta_offset64)
523 outp += sprintf(outp, " %11llu", t->extra_delta64);
525 if (extra_msr_offset32)
526 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
529 if (extra_msr_offset64)
530 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
537 outp += sprintf(outp, "%8d", t->smi_count);
539 if (do_nhm_cstates) {
541 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
543 outp += sprintf(outp, "********");
546 /* print per-core data only for 1st thread in core */
547 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
550 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
551 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
553 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
555 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
558 outp += sprintf(outp, "%8d", c->core_temp_c);
560 /* print per-package data only for 1st core in package */
561 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
566 outp += sprintf(outp, "%8d", p->pkg_temp_c);
568 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
569 if (do_skl_residency) {
570 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
571 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
572 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
573 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
577 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
579 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
581 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
583 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
585 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
586 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
587 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
591 * If measurement interval exceeds minimum RAPL Joule Counter range,
592 * indicate that results are suspect by printing "**" in fraction place.
594 if (interval_float < rapl_joule_counter_range)
599 if (do_rapl && !rapl_joules) {
600 if (do_rapl & RAPL_PKG)
601 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
602 if (do_rapl & RAPL_CORES)
603 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
604 if (do_rapl & RAPL_GFX)
605 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
606 if (do_rapl & RAPL_DRAM)
607 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
608 if (do_rapl & RAPL_PKG_PERF_STATUS)
609 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
610 if (do_rapl & RAPL_DRAM_PERF_STATUS)
611 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
612 } else if (do_rapl && rapl_joules) {
613 if (do_rapl & RAPL_PKG)
614 outp += sprintf(outp, fmt8,
615 p->energy_pkg * rapl_energy_units);
616 if (do_rapl & RAPL_CORES)
617 outp += sprintf(outp, fmt8,
618 p->energy_cores * rapl_energy_units);
619 if (do_rapl & RAPL_GFX)
620 outp += sprintf(outp, fmt8,
621 p->energy_gfx * rapl_energy_units);
622 if (do_rapl & RAPL_DRAM)
623 outp += sprintf(outp, fmt8,
624 p->energy_dram * rapl_dram_energy_units);
625 if (do_rapl & RAPL_PKG_PERF_STATUS)
626 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
627 if (do_rapl & RAPL_DRAM_PERF_STATUS)
628 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
630 outp += sprintf(outp, fmt8, interval_float);
633 outp += sprintf(outp, "\n");
640 fputs(output_buffer, stdout);
642 outp = output_buffer;
646 fputs(output_buffer, stderr);
647 outp = output_buffer;
649 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
653 if (!printed || !summary_only)
656 if (topo.num_cpus > 1)
657 format_counters(&average.threads, &average.cores,
665 for_all_cpus(format_counters, t, c, p);
668 #define DELTA_WRAP32(new, old) \
672 old = 0x100000000 + new - old; \
676 delta_package(struct pkg_data *new, struct pkg_data *old)
679 if (do_skl_residency) {
680 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
681 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
682 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
683 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
685 old->pc2 = new->pc2 - old->pc2;
687 old->pc3 = new->pc3 - old->pc3;
689 old->pc6 = new->pc6 - old->pc6;
691 old->pc7 = new->pc7 - old->pc7;
692 old->pc8 = new->pc8 - old->pc8;
693 old->pc9 = new->pc9 - old->pc9;
694 old->pc10 = new->pc10 - old->pc10;
695 old->pkg_temp_c = new->pkg_temp_c;
697 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
698 DELTA_WRAP32(new->energy_cores, old->energy_cores);
699 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
700 DELTA_WRAP32(new->energy_dram, old->energy_dram);
701 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
702 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
706 delta_core(struct core_data *new, struct core_data *old)
708 old->c3 = new->c3 - old->c3;
709 old->c6 = new->c6 - old->c6;
710 old->c7 = new->c7 - old->c7;
711 old->core_temp_c = new->core_temp_c;
718 delta_thread(struct thread_data *new, struct thread_data *old,
719 struct core_data *core_delta)
721 old->tsc = new->tsc - old->tsc;
723 /* check for TSC < 1 Mcycles over interval */
724 if (old->tsc < (1000 * 1000))
725 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
726 "You can disable all c-states by booting with \"idle=poll\"\n"
727 "or just the deep ones with \"processor.max_cstate=1\"");
729 old->c1 = new->c1 - old->c1;
732 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
733 old->aperf = new->aperf - old->aperf;
734 old->mperf = new->mperf - old->mperf;
737 if (!aperf_mperf_unstable) {
738 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
739 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
740 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
742 aperf_mperf_unstable = 1;
745 * mperf delta is likely a huge "positive" number
746 * can not use it for calculating c0 time
754 if (use_c1_residency_msr) {
756 * Some models have a dedicated C1 residency MSR,
757 * which should be more accurate than the derivation below.
761 * As counter collection is not atomic,
762 * it is possible for mperf's non-halted cycles + idle states
763 * to exceed TSC's all cycles: show c1 = 0% in that case.
765 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
768 /* normal case, derive c1 */
769 old->c1 = old->tsc - old->mperf - core_delta->c3
770 - core_delta->c6 - core_delta->c7;
774 if (old->mperf == 0) {
775 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
776 old->mperf = 1; /* divide by 0 protection */
779 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
780 old->extra_delta32 &= 0xFFFFFFFF;
782 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
785 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
787 old->extra_msr32 = new->extra_msr32;
788 old->extra_msr64 = new->extra_msr64;
791 old->smi_count = new->smi_count - old->smi_count;
794 int delta_cpu(struct thread_data *t, struct core_data *c,
795 struct pkg_data *p, struct thread_data *t2,
796 struct core_data *c2, struct pkg_data *p2)
798 /* calculate core delta only for 1st thread in core */
799 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
802 /* always calculate thread delta */
803 delta_thread(t, t2, c2); /* c2 is core delta */
805 /* calculate package delta only for 1st core in package */
806 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
807 delta_package(p, p2);
812 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
820 t->extra_delta32 = 0;
821 t->extra_delta64 = 0;
823 /* tells format_counters to dump all fields from this set */
824 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
831 p->pkg_wtd_core_c0 = 0;
832 p->pkg_any_core_c0 = 0;
833 p->pkg_any_gfxe_c0 = 0;
834 p->pkg_both_core_gfxe_c0 = 0;
851 p->rapl_pkg_perf_status = 0;
852 p->rapl_dram_perf_status = 0;
855 int sum_counters(struct thread_data *t, struct core_data *c,
858 average.threads.tsc += t->tsc;
859 average.threads.aperf += t->aperf;
860 average.threads.mperf += t->mperf;
861 average.threads.c1 += t->c1;
863 average.threads.extra_delta32 += t->extra_delta32;
864 average.threads.extra_delta64 += t->extra_delta64;
866 /* sum per-core values only for 1st thread in core */
867 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
870 average.cores.c3 += c->c3;
871 average.cores.c6 += c->c6;
872 average.cores.c7 += c->c7;
874 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
876 /* sum per-pkg values only for 1st core in pkg */
877 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
880 if (do_skl_residency) {
881 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
882 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
883 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
884 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
887 average.packages.pc2 += p->pc2;
889 average.packages.pc3 += p->pc3;
891 average.packages.pc6 += p->pc6;
893 average.packages.pc7 += p->pc7;
894 average.packages.pc8 += p->pc8;
895 average.packages.pc9 += p->pc9;
896 average.packages.pc10 += p->pc10;
898 average.packages.energy_pkg += p->energy_pkg;
899 average.packages.energy_dram += p->energy_dram;
900 average.packages.energy_cores += p->energy_cores;
901 average.packages.energy_gfx += p->energy_gfx;
903 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
905 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
906 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
910 * sum the counters for all cpus in the system
911 * compute the weighted average
913 void compute_average(struct thread_data *t, struct core_data *c,
916 clear_counters(&average.threads, &average.cores, &average.packages);
918 for_all_cpus(sum_counters, t, c, p);
920 average.threads.tsc /= topo.num_cpus;
921 average.threads.aperf /= topo.num_cpus;
922 average.threads.mperf /= topo.num_cpus;
923 average.threads.c1 /= topo.num_cpus;
925 average.threads.extra_delta32 /= topo.num_cpus;
926 average.threads.extra_delta32 &= 0xFFFFFFFF;
928 average.threads.extra_delta64 /= topo.num_cpus;
930 average.cores.c3 /= topo.num_cores;
931 average.cores.c6 /= topo.num_cores;
932 average.cores.c7 /= topo.num_cores;
934 if (do_skl_residency) {
935 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
936 average.packages.pkg_any_core_c0 /= topo.num_packages;
937 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
938 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
941 average.packages.pc2 /= topo.num_packages;
943 average.packages.pc3 /= topo.num_packages;
945 average.packages.pc6 /= topo.num_packages;
947 average.packages.pc7 /= topo.num_packages;
949 average.packages.pc8 /= topo.num_packages;
950 average.packages.pc9 /= topo.num_packages;
951 average.packages.pc10 /= topo.num_packages;
954 static unsigned long long rdtsc(void)
956 unsigned int low, high;
958 asm volatile("rdtsc" : "=a" (low), "=d" (high));
960 return low | ((unsigned long long)high) << 32;
967 * acquire and record local counters for that cpu
969 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
972 unsigned long long msr;
974 if (cpu_migrate(cpu)) {
975 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
979 t->tsc = rdtsc(); /* we are running on local CPU of interest */
982 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
984 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
989 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
991 t->smi_count = msr & 0xFFFFFFFF;
993 if (extra_delta_offset32) {
994 if (get_msr(cpu, extra_delta_offset32, &msr))
996 t->extra_delta32 = msr & 0xFFFFFFFF;
999 if (extra_delta_offset64)
1000 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1003 if (extra_msr_offset32) {
1004 if (get_msr(cpu, extra_msr_offset32, &msr))
1006 t->extra_msr32 = msr & 0xFFFFFFFF;
1009 if (extra_msr_offset64)
1010 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
1013 if (use_c1_residency_msr) {
1014 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1018 /* collect core counters only for 1st thread in core */
1019 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1022 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
1023 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1027 if (do_nhm_cstates && !do_knl_cstates) {
1028 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1030 } else if (do_knl_cstates) {
1031 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1036 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1040 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1042 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1046 /* collect package counters only for 1st core in package */
1047 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1050 if (do_skl_residency) {
1051 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1053 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1055 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1057 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1061 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1064 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1067 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1070 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1073 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1075 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1077 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1080 if (do_rapl & RAPL_PKG) {
1081 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1083 p->energy_pkg = msr & 0xFFFFFFFF;
1085 if (do_rapl & RAPL_CORES) {
1086 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1088 p->energy_cores = msr & 0xFFFFFFFF;
1090 if (do_rapl & RAPL_DRAM) {
1091 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1093 p->energy_dram = msr & 0xFFFFFFFF;
1095 if (do_rapl & RAPL_GFX) {
1096 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1098 p->energy_gfx = msr & 0xFFFFFFFF;
1100 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1101 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1103 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1105 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1106 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1108 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1111 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1113 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1119 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1120 * If you change the values, note they are used both in comparisons
1121 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1124 #define PCLUKN 0 /* Unknown */
1125 #define PCLRSV 1 /* Reserved */
1126 #define PCL__0 2 /* PC0 */
1127 #define PCL__1 3 /* PC1 */
1128 #define PCL__2 4 /* PC2 */
1129 #define PCL__3 5 /* PC3 */
1130 #define PCL__4 6 /* PC4 */
1131 #define PCL__6 7 /* PC6 */
1132 #define PCL_6N 8 /* PC6 No Retention */
1133 #define PCL_6R 9 /* PC6 Retention */
1134 #define PCL__7 10 /* PC7 */
1135 #define PCL_7S 11 /* PC7 Shrink */
1136 #define PCL__8 12 /* PC8 */
1137 #define PCL__9 13 /* PC9 */
1138 #define PCLUNL 14 /* Unlimited */
1140 int pkg_cstate_limit = PCLUKN;
1141 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1142 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1144 int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1145 int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1146 int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1147 int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1148 int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1149 int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1152 dump_nhm_platform_info(void)
1154 unsigned long long msr;
1157 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
1159 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
1161 ratio = (msr >> 40) & 0xFF;
1162 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1163 ratio, bclk, ratio * bclk);
1165 ratio = (msr >> 8) & 0xFF;
1166 fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
1167 ratio, bclk, ratio * bclk);
1169 get_msr(0, MSR_IA32_POWER_CTL, &msr);
1170 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1171 msr, msr & 0x2 ? "EN" : "DIS");
1177 dump_hsw_turbo_ratio_limits(void)
1179 unsigned long long msr;
1182 get_msr(0, MSR_TURBO_RATIO_LIMIT2, &msr);
1184 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", msr);
1186 ratio = (msr >> 8) & 0xFF;
1188 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1189 ratio, bclk, ratio * bclk);
1191 ratio = (msr >> 0) & 0xFF;
1193 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1194 ratio, bclk, ratio * bclk);
1199 dump_ivt_turbo_ratio_limits(void)
1201 unsigned long long msr;
1204 get_msr(0, MSR_TURBO_RATIO_LIMIT1, &msr);
1206 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", msr);
1208 ratio = (msr >> 56) & 0xFF;
1210 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1211 ratio, bclk, ratio * bclk);
1213 ratio = (msr >> 48) & 0xFF;
1215 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1216 ratio, bclk, ratio * bclk);
1218 ratio = (msr >> 40) & 0xFF;
1220 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1221 ratio, bclk, ratio * bclk);
1223 ratio = (msr >> 32) & 0xFF;
1225 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1226 ratio, bclk, ratio * bclk);
1228 ratio = (msr >> 24) & 0xFF;
1230 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1231 ratio, bclk, ratio * bclk);
1233 ratio = (msr >> 16) & 0xFF;
1235 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1236 ratio, bclk, ratio * bclk);
1238 ratio = (msr >> 8) & 0xFF;
1240 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1241 ratio, bclk, ratio * bclk);
1243 ratio = (msr >> 0) & 0xFF;
1245 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1246 ratio, bclk, ratio * bclk);
1251 dump_nhm_turbo_ratio_limits(void)
1253 unsigned long long msr;
1256 get_msr(0, MSR_TURBO_RATIO_LIMIT, &msr);
1258 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1260 ratio = (msr >> 56) & 0xFF;
1262 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1263 ratio, bclk, ratio * bclk);
1265 ratio = (msr >> 48) & 0xFF;
1267 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1268 ratio, bclk, ratio * bclk);
1270 ratio = (msr >> 40) & 0xFF;
1272 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1273 ratio, bclk, ratio * bclk);
1275 ratio = (msr >> 32) & 0xFF;
1277 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1278 ratio, bclk, ratio * bclk);
1280 ratio = (msr >> 24) & 0xFF;
1282 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1283 ratio, bclk, ratio * bclk);
1285 ratio = (msr >> 16) & 0xFF;
1287 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1288 ratio, bclk, ratio * bclk);
1290 ratio = (msr >> 8) & 0xFF;
1292 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1293 ratio, bclk, ratio * bclk);
1295 ratio = (msr >> 0) & 0xFF;
1297 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1298 ratio, bclk, ratio * bclk);
1303 dump_knl_turbo_ratio_limits(void)
1307 unsigned long long msr;
1312 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1314 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
1318 * Turbo encoding in KNL is as follows:
1319 * [7:0] -- Base value of number of active cores of bucket 1.
1320 * [15:8] -- Base value of freq ratio of bucket 1.
1321 * [20:16] -- +ve delta of number of active cores of bucket 2.
1322 * i.e. active cores of bucket 2 =
1323 * active cores of bucket 1 + delta
1324 * [23:21] -- Negative delta of freq ratio of bucket 2.
1325 * i.e. freq ratio of bucket 2 =
1326 * freq ratio of bucket 1 - delta
1327 * [28:24]-- +ve delta of number of active cores of bucket 3.
1328 * [31:29]-- -ve delta of freq ratio of bucket 3.
1329 * [36:32]-- +ve delta of number of active cores of bucket 4.
1330 * [39:37]-- -ve delta of freq ratio of bucket 4.
1331 * [44:40]-- +ve delta of number of active cores of bucket 5.
1332 * [47:45]-- -ve delta of freq ratio of bucket 5.
1333 * [52:48]-- +ve delta of number of active cores of bucket 6.
1334 * [55:53]-- -ve delta of freq ratio of bucket 6.
1335 * [60:56]-- +ve delta of number of active cores of bucket 7.
1336 * [63:61]-- -ve delta of freq ratio of bucket 7.
1339 ratio = (msr >> 8) && 0xFF;
1342 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1343 ratio, bclk, ratio * bclk, cores);
1345 for (i = 16; i < 64; i = i + 8) {
1346 delta_cores = (msr >> i) & 0x1F;
1347 delta_ratio = (msr >> (i + 5)) && 0x7;
1348 if (!delta_cores || !delta_ratio)
1350 cores = cores + delta_cores;
1351 ratio = ratio - delta_ratio;
1353 /** -ve ratios will make successive ratio calculations
1354 * negative. Hence return instead of carrying on.
1358 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1359 ratio, bclk, ratio * bclk, cores);
1364 dump_nhm_cst_cfg(void)
1366 unsigned long long msr;
1368 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1370 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1371 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1373 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1375 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1376 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1377 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1378 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1379 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1380 (msr & (1 << 15)) ? "" : "UN",
1381 (unsigned int)msr & 7,
1382 pkg_cstate_limit_strings[pkg_cstate_limit]);
1386 void free_all_buffers(void)
1388 CPU_FREE(cpu_present_set);
1389 cpu_present_set = NULL;
1390 cpu_present_set = 0;
1392 CPU_FREE(cpu_affinity_set);
1393 cpu_affinity_set = NULL;
1394 cpu_affinity_setsize = 0;
1402 package_even = NULL;
1412 free(output_buffer);
1413 output_buffer = NULL;
1418 * Open a file, and exit on failure
1420 FILE *fopen_or_die(const char *path, const char *mode)
1422 FILE *filep = fopen(path, "r");
1424 err(1, "%s: open failed", path);
1429 * Parse a file containing a single int.
1431 int parse_int_file(const char *fmt, ...)
1434 char path[PATH_MAX];
1438 va_start(args, fmt);
1439 vsnprintf(path, sizeof(path), fmt, args);
1441 filep = fopen_or_die(path, "r");
1442 if (fscanf(filep, "%d", &value) != 1)
1443 err(1, "%s: failed to parse number from file", path);
1449 * get_cpu_position_in_core(cpu)
1450 * return the position of the CPU among its HT siblings in the core
1451 * return -1 if the sibling is not in list
1453 int get_cpu_position_in_core(int cpu)
1462 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1464 filep = fopen(path, "r");
1465 if (filep == NULL) {
1470 for (i = 0; i < topo.num_threads_per_core; i++) {
1471 fscanf(filep, "%d", &this_cpu);
1472 if (this_cpu == cpu) {
1477 /* Account for no separator after last thread*/
1478 if (i != (topo.num_threads_per_core - 1))
1479 fscanf(filep, "%c", &character);
1487 * cpu_is_first_core_in_package(cpu)
1488 * return 1 if given CPU is 1st core in package
1490 int cpu_is_first_core_in_package(int cpu)
1492 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1495 int get_physical_package_id(int cpu)
1497 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1500 int get_core_id(int cpu)
1502 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1505 int get_num_ht_siblings(int cpu)
1515 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1516 filep = fopen_or_die(path, "r");
1520 * A ',' separated or '-' separated set of numbers
1521 * (eg 1-2 or 1,3,4,5)
1523 fscanf(filep, "%d%c\n", &sib1, &character);
1524 fseek(filep, 0, SEEK_SET);
1525 fgets(str, 100, filep);
1526 ch = strchr(str, character);
1527 while (ch != NULL) {
1529 ch = strchr(ch+1, character);
1537 * run func(thread, core, package) in topology order
1538 * skip non-present cpus
1541 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1542 struct pkg_data *, struct thread_data *, struct core_data *,
1543 struct pkg_data *), struct thread_data *thread_base,
1544 struct core_data *core_base, struct pkg_data *pkg_base,
1545 struct thread_data *thread_base2, struct core_data *core_base2,
1546 struct pkg_data *pkg_base2)
1548 int retval, pkg_no, core_no, thread_no;
1550 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1551 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1552 for (thread_no = 0; thread_no <
1553 topo.num_threads_per_core; ++thread_no) {
1554 struct thread_data *t, *t2;
1555 struct core_data *c, *c2;
1556 struct pkg_data *p, *p2;
1558 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1560 if (cpu_is_not_present(t->cpu_id))
1563 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1565 c = GET_CORE(core_base, core_no, pkg_no);
1566 c2 = GET_CORE(core_base2, core_no, pkg_no);
1568 p = GET_PKG(pkg_base, pkg_no);
1569 p2 = GET_PKG(pkg_base2, pkg_no);
1571 retval = func(t, c, p, t2, c2, p2);
1581 * run func(cpu) on every cpu in /proc/stat
1582 * return max_cpu number
1584 int for_all_proc_cpus(int (func)(int))
1590 fp = fopen_or_die(proc_stat, "r");
1592 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1594 err(1, "%s: failed to parse format", proc_stat);
1597 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1601 retval = func(cpu_num);
1611 void re_initialize(void)
1614 setup_all_buffers();
1615 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1621 * remember the last one seen, it will be the max
1623 int count_cpus(int cpu)
1625 if (topo.max_cpu_num < cpu)
1626 topo.max_cpu_num = cpu;
1631 int mark_cpu_present(int cpu)
1633 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1637 void turbostat_loop()
1645 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1648 } else if (retval == -1) {
1649 if (restarted > 1) {
1656 gettimeofday(&tv_even, (struct timezone *)NULL);
1659 if (for_all_proc_cpus(cpu_is_not_present)) {
1663 sleep(interval_sec);
1664 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1667 } else if (retval == -1) {
1671 gettimeofday(&tv_odd, (struct timezone *)NULL);
1672 timersub(&tv_odd, &tv_even, &tv_delta);
1673 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1674 compute_average(EVEN_COUNTERS);
1675 format_all_counters(EVEN_COUNTERS);
1677 sleep(interval_sec);
1678 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1681 } else if (retval == -1) {
1685 gettimeofday(&tv_even, (struct timezone *)NULL);
1686 timersub(&tv_even, &tv_odd, &tv_delta);
1687 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1688 compute_average(ODD_COUNTERS);
1689 format_all_counters(ODD_COUNTERS);
1694 void check_dev_msr()
1698 if (stat("/dev/cpu/0/msr", &sb))
1699 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1700 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1703 void check_permissions()
1705 struct __user_cap_header_struct cap_header_data;
1706 cap_user_header_t cap_header = &cap_header_data;
1707 struct __user_cap_data_struct cap_data_data;
1708 cap_user_data_t cap_data = &cap_data_data;
1709 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1712 /* check for CAP_SYS_RAWIO */
1713 cap_header->pid = getpid();
1714 cap_header->version = _LINUX_CAPABILITY_VERSION;
1715 if (capget(cap_header, cap_data) < 0)
1716 err(-6, "capget(2) failed");
1718 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1720 warnx("capget(CAP_SYS_RAWIO) failed,"
1721 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1724 /* test file permissions */
1725 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1727 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1730 /* if all else fails, thell them to be root */
1733 warnx("... or simply run as root");
1740 * NHM adds support for additional MSRs:
1742 * MSR_SMI_COUNT 0x00000034
1744 * MSR_NHM_PLATFORM_INFO 0x000000ce
1745 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1747 * MSR_PKG_C3_RESIDENCY 0x000003f8
1748 * MSR_PKG_C6_RESIDENCY 0x000003f9
1749 * MSR_CORE_C3_RESIDENCY 0x000003fc
1750 * MSR_CORE_C6_RESIDENCY 0x000003fd
1753 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
1755 int probe_nhm_msrs(unsigned int family, unsigned int model)
1757 unsigned long long msr;
1758 int *pkg_cstate_limits;
1767 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1768 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1769 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1770 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1771 case 0x2C: /* Westmere EP - Gulftown */
1772 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1773 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1774 pkg_cstate_limits = nhm_pkg_cstate_limits;
1776 case 0x2A: /* SNB */
1777 case 0x2D: /* SNB Xeon */
1778 case 0x3A: /* IVB */
1779 case 0x3E: /* IVB Xeon */
1780 pkg_cstate_limits = snb_pkg_cstate_limits;
1782 case 0x3C: /* HSW */
1783 case 0x3F: /* HSX */
1784 case 0x45: /* HSW */
1785 case 0x46: /* HSW */
1786 case 0x3D: /* BDW */
1787 case 0x47: /* BDW */
1788 case 0x4F: /* BDX */
1789 case 0x56: /* BDX-DE */
1790 case 0x4E: /* SKL */
1791 case 0x5E: /* SKL */
1792 pkg_cstate_limits = hsw_pkg_cstate_limits;
1794 case 0x37: /* BYT */
1795 case 0x4D: /* AVN */
1796 pkg_cstate_limits = slv_pkg_cstate_limits;
1798 case 0x4C: /* AMT */
1799 pkg_cstate_limits = amt_pkg_cstate_limits;
1801 case 0x57: /* PHI */
1802 pkg_cstate_limits = phi_pkg_cstate_limits;
1807 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1809 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
1813 int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1816 /* Nehalem compatible, but do not include turbo-ratio limit support */
1817 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1818 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1824 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1833 case 0x3E: /* IVB Xeon */
1834 case 0x3F: /* HSW Xeon */
1840 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1849 case 0x3F: /* HSW Xeon */
1856 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1865 case 0x57: /* Knights Landing */
1872 dump_cstate_pstate_config_info(family, model)
1874 if (!do_nhm_platform_info)
1877 dump_nhm_platform_info();
1879 if (has_hsw_turbo_ratio_limit(family, model))
1880 dump_hsw_turbo_ratio_limits();
1882 if (has_ivt_turbo_ratio_limit(family, model))
1883 dump_ivt_turbo_ratio_limits();
1885 if (has_nhm_turbo_ratio_limit(family, model))
1886 dump_nhm_turbo_ratio_limits();
1888 if (has_knl_turbo_ratio_limit(family, model))
1889 dump_knl_turbo_ratio_limits();
1897 * Decode the ENERGY_PERF_BIAS MSR
1899 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1901 unsigned long long msr;
1910 /* EPB is per-package */
1911 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1914 if (cpu_migrate(cpu)) {
1915 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1919 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1922 switch (msr & 0x7) {
1923 case ENERGY_PERF_BIAS_PERFORMANCE:
1924 epb_string = "performance";
1926 case ENERGY_PERF_BIAS_NORMAL:
1927 epb_string = "balanced";
1929 case ENERGY_PERF_BIAS_POWERSAVE:
1930 epb_string = "powersave";
1933 epb_string = "custom";
1936 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1942 * print_perf_limit()
1944 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1946 unsigned long long msr;
1952 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1955 if (cpu_migrate(cpu)) {
1956 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1960 if (do_core_perf_limit_reasons) {
1961 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
1962 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1963 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1964 (msr & 1 << 15) ? "bit15, " : "",
1965 (msr & 1 << 14) ? "bit14, " : "",
1966 (msr & 1 << 13) ? "Transitions, " : "",
1967 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1968 (msr & 1 << 11) ? "PkgPwrL2, " : "",
1969 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1970 (msr & 1 << 9) ? "CorePwr, " : "",
1971 (msr & 1 << 8) ? "Amps, " : "",
1972 (msr & 1 << 6) ? "VR-Therm, " : "",
1973 (msr & 1 << 5) ? "Auto-HWP, " : "",
1974 (msr & 1 << 4) ? "Graphics, " : "",
1975 (msr & 1 << 2) ? "bit2, " : "",
1976 (msr & 1 << 1) ? "ThermStatus, " : "",
1977 (msr & 1 << 0) ? "PROCHOT, " : "");
1978 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
1979 (msr & 1 << 31) ? "bit31, " : "",
1980 (msr & 1 << 30) ? "bit30, " : "",
1981 (msr & 1 << 29) ? "Transitions, " : "",
1982 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1983 (msr & 1 << 27) ? "PkgPwrL2, " : "",
1984 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1985 (msr & 1 << 25) ? "CorePwr, " : "",
1986 (msr & 1 << 24) ? "Amps, " : "",
1987 (msr & 1 << 22) ? "VR-Therm, " : "",
1988 (msr & 1 << 21) ? "Auto-HWP, " : "",
1989 (msr & 1 << 20) ? "Graphics, " : "",
1990 (msr & 1 << 18) ? "bit18, " : "",
1991 (msr & 1 << 17) ? "ThermStatus, " : "",
1992 (msr & 1 << 16) ? "PROCHOT, " : "");
1995 if (do_gfx_perf_limit_reasons) {
1996 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
1997 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1998 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
1999 (msr & 1 << 0) ? "PROCHOT, " : "",
2000 (msr & 1 << 1) ? "ThermStatus, " : "",
2001 (msr & 1 << 4) ? "Graphics, " : "",
2002 (msr & 1 << 6) ? "VR-Therm, " : "",
2003 (msr & 1 << 8) ? "Amps, " : "",
2004 (msr & 1 << 9) ? "GFXPwr, " : "",
2005 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2006 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2007 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
2008 (msr & 1 << 16) ? "PROCHOT, " : "",
2009 (msr & 1 << 17) ? "ThermStatus, " : "",
2010 (msr & 1 << 20) ? "Graphics, " : "",
2011 (msr & 1 << 22) ? "VR-Therm, " : "",
2012 (msr & 1 << 24) ? "Amps, " : "",
2013 (msr & 1 << 25) ? "GFXPwr, " : "",
2014 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2015 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2017 if (do_ring_perf_limit_reasons) {
2018 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2019 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2020 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
2021 (msr & 1 << 0) ? "PROCHOT, " : "",
2022 (msr & 1 << 1) ? "ThermStatus, " : "",
2023 (msr & 1 << 6) ? "VR-Therm, " : "",
2024 (msr & 1 << 8) ? "Amps, " : "",
2025 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2026 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2027 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
2028 (msr & 1 << 16) ? "PROCHOT, " : "",
2029 (msr & 1 << 17) ? "ThermStatus, " : "",
2030 (msr & 1 << 22) ? "VR-Therm, " : "",
2031 (msr & 1 << 24) ? "Amps, " : "",
2032 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2033 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2038 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2039 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2041 double get_tdp(model)
2043 unsigned long long msr;
2045 if (do_rapl & RAPL_PKG_POWER_INFO)
2046 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
2047 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2059 * rapl_dram_energy_units_probe()
2060 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2063 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2065 /* only called for genuine_intel, family 6 */
2068 case 0x3F: /* HSX */
2069 case 0x4F: /* BDX */
2070 case 0x56: /* BDX-DE */
2071 case 0x57: /* KNL */
2072 return (rapl_dram_energy_units = 15.3 / 1000000);
2074 return (rapl_energy_units);
2082 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2084 void rapl_probe(unsigned int family, unsigned int model)
2086 unsigned long long msr;
2087 unsigned int time_unit;
2099 case 0x3C: /* HSW */
2100 case 0x45: /* HSW */
2101 case 0x46: /* HSW */
2102 case 0x3D: /* BDW */
2103 case 0x47: /* BDW */
2104 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2106 case 0x4E: /* SKL */
2107 case 0x5E: /* SKL */
2108 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2110 case 0x3F: /* HSX */
2111 case 0x4F: /* BDX */
2112 case 0x56: /* BDX-DE */
2113 case 0x57: /* KNL */
2114 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2118 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
2120 case 0x37: /* BYT */
2121 case 0x4D: /* AVN */
2122 do_rapl = RAPL_PKG | RAPL_CORES ;
2128 /* units on package 0, verify later other packages match */
2129 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
2132 rapl_power_units = 1.0 / (1 << (msr & 0xF));
2134 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2136 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2138 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2140 time_unit = msr >> 16 & 0xF;
2144 rapl_time_units = 1.0 / (1 << (time_unit));
2146 tdp = get_tdp(model);
2148 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2150 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2155 void perf_limit_reasons_probe(family, model)
2164 case 0x3C: /* HSW */
2165 case 0x45: /* HSW */
2166 case 0x46: /* HSW */
2167 do_gfx_perf_limit_reasons = 1;
2168 case 0x3F: /* HSX */
2169 do_core_perf_limit_reasons = 1;
2170 do_ring_perf_limit_reasons = 1;
2176 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2178 unsigned long long msr;
2182 if (!(do_dts || do_ptm))
2187 /* DTS is per-core, no need to print for each thread */
2188 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2191 if (cpu_migrate(cpu)) {
2192 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2196 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2197 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2200 dts = (msr >> 16) & 0x7F;
2201 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2202 cpu, msr, tcc_activation_temp - dts);
2205 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2208 dts = (msr >> 16) & 0x7F;
2209 dts2 = (msr >> 8) & 0x7F;
2210 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2211 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2217 unsigned int resolution;
2219 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2222 dts = (msr >> 16) & 0x7F;
2223 resolution = (msr >> 27) & 0xF;
2224 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2225 cpu, msr, tcc_activation_temp - dts, resolution);
2228 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2231 dts = (msr >> 16) & 0x7F;
2232 dts2 = (msr >> 8) & 0x7F;
2233 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2234 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2241 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2243 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2245 ((msr >> 15) & 1) ? "EN" : "DIS",
2246 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2247 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2248 (((msr >> 16) & 1) ? "EN" : "DIS"));
2253 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2255 unsigned long long msr;
2261 /* RAPL counters are per package, so print only for 1st thread/package */
2262 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2266 if (cpu_migrate(cpu)) {
2267 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2271 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2275 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2276 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2277 rapl_power_units, rapl_energy_units, rapl_time_units);
2279 if (do_rapl & RAPL_PKG_POWER_INFO) {
2281 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2285 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2287 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2288 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2289 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2290 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2293 if (do_rapl & RAPL_PKG) {
2295 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2298 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2299 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2301 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2302 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2304 ((msr >> 47) & 1) ? "EN" : "DIS",
2305 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2306 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2307 ((msr >> 48) & 1) ? "EN" : "DIS");
2310 if (do_rapl & RAPL_DRAM_POWER_INFO) {
2311 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2314 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2316 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2317 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2318 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2319 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2321 if (do_rapl & RAPL_DRAM) {
2322 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2324 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2325 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2327 print_power_limit_msr(cpu, msr, "DRAM Limit");
2329 if (do_rapl & RAPL_CORE_POLICY) {
2331 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2334 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
2337 if (do_rapl & RAPL_CORES) {
2340 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2342 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2343 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2344 print_power_limit_msr(cpu, msr, "Cores Limit");
2347 if (do_rapl & RAPL_GFX) {
2349 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2352 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2354 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2356 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2357 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2358 print_power_limit_msr(cpu, msr, "GFX Limit");
2365 * SNB adds support for additional MSRs:
2367 * MSR_PKG_C7_RESIDENCY 0x000003fa
2368 * MSR_CORE_C7_RESIDENCY 0x000003fe
2369 * MSR_PKG_C2_RESIDENCY 0x0000060d
2372 int has_snb_msrs(unsigned int family, unsigned int model)
2380 case 0x3A: /* IVB */
2381 case 0x3E: /* IVB Xeon */
2382 case 0x3C: /* HSW */
2383 case 0x3F: /* HSW */
2384 case 0x45: /* HSW */
2385 case 0x46: /* HSW */
2386 case 0x3D: /* BDW */
2387 case 0x47: /* BDW */
2388 case 0x4F: /* BDX */
2389 case 0x56: /* BDX-DE */
2390 case 0x4E: /* SKL */
2391 case 0x5E: /* SKL */
2398 * HSW adds support for additional MSRs:
2400 * MSR_PKG_C8_RESIDENCY 0x00000630
2401 * MSR_PKG_C9_RESIDENCY 0x00000631
2402 * MSR_PKG_C10_RESIDENCY 0x00000632
2404 int has_hsw_msrs(unsigned int family, unsigned int model)
2410 case 0x45: /* HSW */
2411 case 0x3D: /* BDW */
2412 case 0x4E: /* SKL */
2413 case 0x5E: /* SKL */
2420 * SKL adds support for additional MSRS:
2422 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2423 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2424 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2425 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2427 int has_skl_msrs(unsigned int family, unsigned int model)
2433 case 0x4E: /* SKL */
2434 case 0x5E: /* SKL */
2442 int is_slm(unsigned int family, unsigned int model)
2447 case 0x37: /* BYT */
2448 case 0x4D: /* AVN */
2454 int is_knl(unsigned int family, unsigned int model)
2459 case 0x57: /* KNL */
2465 #define SLM_BCLK_FREQS 5
2466 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2468 double slm_bclk(void)
2470 unsigned long long msr = 3;
2474 if (get_msr(0, MSR_FSB_FREQ, &msr))
2475 fprintf(stderr, "SLM BCLK: unknown\n");
2478 if (i >= SLM_BCLK_FREQS) {
2479 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2482 freq = slm_freq_table[i];
2484 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2489 double discover_bclk(unsigned int family, unsigned int model)
2491 if (has_snb_msrs(family, model))
2493 else if (is_slm(family, model))
2500 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2501 * the Thermal Control Circuit (TCC) activates.
2502 * This is usually equal to tjMax.
2504 * Older processors do not have this MSR, so there we guess,
2505 * but also allow cmdline over-ride with -T.
2507 * Several MSR temperature values are in units of degrees-C
2508 * below this value, including the Digital Thermal Sensor (DTS),
2509 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2511 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2513 unsigned long long msr;
2514 unsigned int target_c_local;
2517 /* tcc_activation_temp is used only for dts or ptm */
2518 if (!(do_dts || do_ptm))
2521 /* this is a per-package concept */
2522 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2526 if (cpu_migrate(cpu)) {
2527 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2531 if (tcc_activation_temp_override != 0) {
2532 tcc_activation_temp = tcc_activation_temp_override;
2533 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2534 cpu, tcc_activation_temp);
2538 /* Temperature Target MSR is Nehalem and newer only */
2539 if (!do_nhm_platform_info)
2542 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2545 target_c_local = (msr >> 16) & 0xFF;
2548 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2549 cpu, msr, target_c_local);
2551 if (!target_c_local)
2554 tcc_activation_temp = target_c_local;
2559 tcc_activation_temp = TJMAX_DEFAULT;
2560 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2561 cpu, tcc_activation_temp);
2565 void process_cpuid()
2567 unsigned int eax, ebx, ecx, edx, max_level;
2568 unsigned int fms, family, model, stepping;
2570 eax = ebx = ecx = edx = 0;
2572 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2574 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2578 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2579 (char *)&ebx, (char *)&edx, (char *)&ecx);
2581 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
2582 family = (fms >> 8) & 0xf;
2583 model = (fms >> 4) & 0xf;
2584 stepping = fms & 0xf;
2585 if (family == 6 || family == 0xf)
2586 model += ((fms >> 16) & 0xf) << 4;
2589 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2590 max_level, family, model, stepping, family, model, stepping);
2592 if (!(edx & (1 << 5)))
2593 errx(1, "CPUID: no MSR");
2596 * check max extended function levels of CPUID.
2597 * This is needed to check for invariant TSC.
2598 * This check is valid for both Intel and AMD.
2600 ebx = ecx = edx = 0;
2601 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2603 if (max_level >= 0x80000007) {
2606 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2607 * this check is valid for both Intel and AMD
2609 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2610 has_invariant_tsc = edx & (1 << 8);
2614 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2615 * this check is valid for both Intel and AMD
2618 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
2619 has_aperf = ecx & (1 << 0);
2620 do_dts = eax & (1 << 0);
2621 do_ptm = eax & (1 << 6);
2622 has_epb = ecx & (1 << 3);
2625 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2626 has_aperf ? "" : "No ",
2627 do_dts ? "" : "No ",
2628 do_ptm ? "" : "No ",
2629 has_epb ? "" : "No ");
2631 if (max_level > 0x15) {
2632 unsigned int eax_crystal;
2633 unsigned int ebx_tsc;
2636 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2638 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2639 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2643 if (debug && (ebx != 0))
2644 fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
2645 eax_crystal, ebx_tsc, crystal_hz);
2647 if (crystal_hz == 0)
2649 case 0x4E: /* SKL */
2650 case 0x5E: /* SKL */
2651 crystal_hz = 24000000; /* 24 MHz */
2658 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2660 fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
2661 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
2666 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
2667 do_snb_cstates = has_snb_msrs(family, model);
2668 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2669 do_pc3 = (pkg_cstate_limit >= PCL__3);
2670 do_pc6 = (pkg_cstate_limit >= PCL__6);
2671 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
2672 do_c8_c9_c10 = has_hsw_msrs(family, model);
2673 do_skl_residency = has_skl_msrs(family, model);
2674 do_slm_cstates = is_slm(family, model);
2675 do_knl_cstates = is_knl(family, model);
2676 bclk = discover_bclk(family, model);
2678 rapl_probe(family, model);
2679 perf_limit_reasons_probe(family, model);
2682 dump_cstate_pstate_config_info();
2690 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2692 "Turbostat forks the specified COMMAND and prints statistics\n"
2693 "when COMMAND completes.\n"
2694 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2695 "to print statistics, until interrupted.\n"
2696 "--debug run in \"debug\" mode\n"
2697 "--interval sec Override default 5-second measurement interval\n"
2698 "--help print this help message\n"
2699 "--counter msr print 32-bit counter at address \"msr\"\n"
2700 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2701 "--msr msr print 32-bit value at address \"msr\"\n"
2702 "--MSR msr print 64-bit Value at address \"msr\"\n"
2703 "--version print version information\n"
2705 "For more help, run \"man turbostat\"\n");
2710 * in /dev/cpu/ return success for names that are numbers
2711 * ie. filter out ".", "..", "microcode".
2713 int dir_filter(const struct dirent *dirp)
2715 if (isdigit(dirp->d_name[0]))
2721 int open_dev_cpu_msr(int dummy1)
2726 void topology_probe()
2729 int max_core_id = 0;
2730 int max_package_id = 0;
2731 int max_siblings = 0;
2732 struct cpu_topology {
2734 int physical_package_id;
2737 /* Initialize num_cpus, max_cpu_num */
2739 topo.max_cpu_num = 0;
2740 for_all_proc_cpus(count_cpus);
2741 if (!summary_only && topo.num_cpus > 1)
2745 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2747 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2749 err(1, "calloc cpus");
2752 * Allocate and initialize cpu_present_set
2754 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2755 if (cpu_present_set == NULL)
2756 err(3, "CPU_ALLOC");
2757 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2758 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2759 for_all_proc_cpus(mark_cpu_present);
2762 * Allocate and initialize cpu_affinity_set
2764 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2765 if (cpu_affinity_set == NULL)
2766 err(3, "CPU_ALLOC");
2767 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2768 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2773 * find max_core_id, max_package_id
2775 for (i = 0; i <= topo.max_cpu_num; ++i) {
2778 if (cpu_is_not_present(i)) {
2780 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2783 cpus[i].core_id = get_core_id(i);
2784 if (cpus[i].core_id > max_core_id)
2785 max_core_id = cpus[i].core_id;
2787 cpus[i].physical_package_id = get_physical_package_id(i);
2788 if (cpus[i].physical_package_id > max_package_id)
2789 max_package_id = cpus[i].physical_package_id;
2791 siblings = get_num_ht_siblings(i);
2792 if (siblings > max_siblings)
2793 max_siblings = siblings;
2795 fprintf(stderr, "cpu %d pkg %d core %d\n",
2796 i, cpus[i].physical_package_id, cpus[i].core_id);
2798 topo.num_cores_per_pkg = max_core_id + 1;
2800 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2801 max_core_id, topo.num_cores_per_pkg);
2802 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
2805 topo.num_packages = max_package_id + 1;
2807 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2808 max_package_id, topo.num_packages);
2809 if (debug && !summary_only && topo.num_packages > 1)
2812 topo.num_threads_per_core = max_siblings;
2814 fprintf(stderr, "max_siblings %d\n", max_siblings);
2820 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2824 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2825 topo.num_packages, sizeof(struct thread_data));
2829 for (i = 0; i < topo.num_threads_per_core *
2830 topo.num_cores_per_pkg * topo.num_packages; i++)
2831 (*t)[i].cpu_id = -1;
2833 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2834 sizeof(struct core_data));
2838 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2839 (*c)[i].core_id = -1;
2841 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2845 for (i = 0; i < topo.num_packages; i++)
2846 (*p)[i].package_id = i;
2850 err(1, "calloc counters");
2855 * set cpu_id, core_num, pkg_num
2856 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2858 * increment topo.num_cores when 1st core in pkg seen
2860 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2861 struct pkg_data *pkg_base, int thread_num, int core_num,
2862 int pkg_num, int cpu_id)
2864 struct thread_data *t;
2865 struct core_data *c;
2868 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2869 c = GET_CORE(core_base, core_num, pkg_num);
2870 p = GET_PKG(pkg_base, pkg_num);
2873 if (thread_num == 0) {
2874 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2875 if (cpu_is_first_core_in_package(cpu_id))
2876 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2879 c->core_id = core_num;
2880 p->package_id = pkg_num;
2884 int initialize_counters(int cpu_id)
2886 int my_thread_id, my_core_id, my_package_id;
2888 my_package_id = get_physical_package_id(cpu_id);
2889 my_core_id = get_core_id(cpu_id);
2890 my_thread_id = get_cpu_position_in_core(cpu_id);
2894 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2895 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2899 void allocate_output_buffer()
2901 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2902 outp = output_buffer;
2904 err(-1, "calloc output buffer");
2907 void setup_all_buffers(void)
2910 allocate_counters(&thread_even, &core_even, &package_even);
2911 allocate_counters(&thread_odd, &core_odd, &package_odd);
2912 allocate_output_buffer();
2913 for_all_proc_cpus(initialize_counters);
2916 void turbostat_init()
2919 check_permissions();
2922 setup_all_buffers();
2925 for_all_cpus(print_epb, ODD_COUNTERS);
2928 for_all_cpus(print_perf_limit, ODD_COUNTERS);
2931 for_all_cpus(print_rapl, ODD_COUNTERS);
2933 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2936 for_all_cpus(print_thermal, ODD_COUNTERS);
2939 int fork_it(char **argv)
2944 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2947 /* clear affinity side-effect of get_counters() */
2948 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2949 gettimeofday(&tv_even, (struct timezone *)NULL);
2954 execvp(argv[0], argv);
2958 if (child_pid == -1)
2961 signal(SIGINT, SIG_IGN);
2962 signal(SIGQUIT, SIG_IGN);
2963 if (waitpid(child_pid, &status, 0) == -1)
2964 err(status, "waitpid");
2967 * n.b. fork_it() does not check for errors from for_all_cpus()
2968 * because re-starting is problematic when forking
2970 for_all_cpus(get_counters, ODD_COUNTERS);
2971 gettimeofday(&tv_odd, (struct timezone *)NULL);
2972 timersub(&tv_odd, &tv_even, &tv_delta);
2973 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2974 compute_average(EVEN_COUNTERS);
2975 format_all_counters(EVEN_COUNTERS);
2978 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2983 int get_and_dump_counters(void)
2987 status = for_all_cpus(get_counters, ODD_COUNTERS);
2991 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3000 void print_version() {
3001 fprintf(stderr, "turbostat version 4.5 2 Apr, 2015"
3002 " - Len Brown <lenb@kernel.org>\n");
3005 void cmdline(int argc, char **argv)
3008 int option_index = 0;
3009 static struct option long_options[] = {
3010 {"Counter", required_argument, 0, 'C'},
3011 {"counter", required_argument, 0, 'c'},
3012 {"Dump", no_argument, 0, 'D'},
3013 {"debug", no_argument, 0, 'd'},
3014 {"interval", required_argument, 0, 'i'},
3015 {"help", no_argument, 0, 'h'},
3016 {"Joules", no_argument, 0, 'J'},
3017 {"MSR", required_argument, 0, 'M'},
3018 {"msr", required_argument, 0, 'm'},
3019 {"Package", no_argument, 0, 'p'},
3020 {"processor", no_argument, 0, 'p'},
3021 {"Summary", no_argument, 0, 'S'},
3022 {"TCC", required_argument, 0, 'T'},
3023 {"version", no_argument, 0, 'v' },
3029 while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v",
3030 long_options, &option_index)) != -1) {
3033 sscanf(optarg, "%x", &extra_delta_offset64);
3036 sscanf(optarg, "%x", &extra_delta_offset32);
3049 interval_sec = atoi(optarg);
3055 sscanf(optarg, "%x", &extra_msr_offset64);
3058 sscanf(optarg, "%x", &extra_msr_offset32);
3070 tcc_activation_temp_override = atoi(optarg);
3080 int main(int argc, char **argv)
3082 cmdline(argc, argv);
3089 /* dump counters and exit */
3091 return get_and_dump_counters();
3094 * if any params left, it must be a command to fork
3097 return fork_it(argv + optind);