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.
24 #include INTEL_FAMILY_HEADER
29 #include <sys/types.h>
32 #include <sys/resource.h>
44 #include <linux/capability.h>
47 char *proc_stat = "/proc/stat";
50 struct timespec interval_ts = {5, 0};
52 unsigned int rapl_joules;
53 unsigned int summary_only;
54 unsigned int dump_only;
55 unsigned int do_snb_cstates;
56 unsigned int do_knl_cstates;
61 unsigned int do_c8_c9_c10;
62 unsigned int do_skl_residency;
63 unsigned int do_slm_cstates;
64 unsigned int use_c1_residency_msr;
65 unsigned int has_aperf;
67 unsigned int do_irtl_snb;
68 unsigned int do_irtl_hsw;
69 unsigned int units = 1000000; /* MHz etc */
70 unsigned int genuine_intel;
71 unsigned int has_invariant_tsc;
72 unsigned int do_nhm_platform_info;
73 unsigned int no_MSR_MISC_PWR_MGMT;
74 unsigned int aperf_mperf_multiplier = 1;
77 unsigned int has_base_hz;
78 double tsc_tweak = 1.0;
79 unsigned int show_pkg_only;
80 unsigned int show_core_only;
81 char *output_buffer, *outp;
85 unsigned long long gfx_cur_rc6_ms;
86 unsigned int gfx_cur_mhz;
87 unsigned int tcc_activation_temp;
88 unsigned int tcc_activation_temp_override;
89 double rapl_power_units, rapl_time_units;
90 double rapl_dram_energy_units, rapl_energy_units;
91 double rapl_joule_counter_range;
92 unsigned int do_core_perf_limit_reasons;
93 unsigned int do_gfx_perf_limit_reasons;
94 unsigned int do_ring_perf_limit_reasons;
95 unsigned int crystal_hz;
96 unsigned long long tsc_hz;
98 double discover_bclk(unsigned int family, unsigned int model);
99 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
100 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
101 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
102 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
103 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
104 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
106 #define RAPL_PKG (1 << 0)
107 /* 0x610 MSR_PKG_POWER_LIMIT */
108 /* 0x611 MSR_PKG_ENERGY_STATUS */
109 #define RAPL_PKG_PERF_STATUS (1 << 1)
110 /* 0x613 MSR_PKG_PERF_STATUS */
111 #define RAPL_PKG_POWER_INFO (1 << 2)
112 /* 0x614 MSR_PKG_POWER_INFO */
114 #define RAPL_DRAM (1 << 3)
115 /* 0x618 MSR_DRAM_POWER_LIMIT */
116 /* 0x619 MSR_DRAM_ENERGY_STATUS */
117 #define RAPL_DRAM_PERF_STATUS (1 << 4)
118 /* 0x61b MSR_DRAM_PERF_STATUS */
119 #define RAPL_DRAM_POWER_INFO (1 << 5)
120 /* 0x61c MSR_DRAM_POWER_INFO */
122 #define RAPL_CORES_POWER_LIMIT (1 << 6)
123 /* 0x638 MSR_PP0_POWER_LIMIT */
124 #define RAPL_CORE_POLICY (1 << 7)
125 /* 0x63a MSR_PP0_POLICY */
127 #define RAPL_GFX (1 << 8)
128 /* 0x640 MSR_PP1_POWER_LIMIT */
129 /* 0x641 MSR_PP1_ENERGY_STATUS */
130 /* 0x642 MSR_PP1_POLICY */
132 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
133 /* 0x639 MSR_PP0_ENERGY_STATUS */
134 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
135 #define TJMAX_DEFAULT 100
137 #define MAX(a, b) ((a) > (b) ? (a) : (b))
140 * buffer size used by sscanf() for added column names
141 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
143 #define NAME_BYTES 20
148 cpu_set_t *cpu_present_set, *cpu_affinity_set;
149 size_t cpu_present_setsize, cpu_affinity_setsize;
150 #define MAX_ADDED_COUNTERS 16
153 unsigned long long tsc;
154 unsigned long long aperf;
155 unsigned long long mperf;
156 unsigned long long c1;
157 unsigned int irq_count;
158 unsigned int smi_count;
161 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
162 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
163 unsigned long long counter[MAX_ADDED_COUNTERS];
164 } *thread_even, *thread_odd;
167 unsigned long long c3;
168 unsigned long long c6;
169 unsigned long long c7;
170 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
171 unsigned int core_temp_c;
172 unsigned int core_id;
173 unsigned long long counter[MAX_ADDED_COUNTERS];
174 } *core_even, *core_odd;
177 unsigned long long pc2;
178 unsigned long long pc3;
179 unsigned long long pc6;
180 unsigned long long pc7;
181 unsigned long long pc8;
182 unsigned long long pc9;
183 unsigned long long pc10;
184 unsigned long long pkg_wtd_core_c0;
185 unsigned long long pkg_any_core_c0;
186 unsigned long long pkg_any_gfxe_c0;
187 unsigned long long pkg_both_core_gfxe_c0;
188 long long gfx_rc6_ms;
189 unsigned int gfx_mhz;
190 unsigned int package_id;
191 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
192 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
193 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
194 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
195 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
196 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
197 unsigned int pkg_temp_c;
198 unsigned long long counter[MAX_ADDED_COUNTERS];
199 } *package_even, *package_odd;
201 #define ODD_COUNTERS thread_odd, core_odd, package_odd
202 #define EVEN_COUNTERS thread_even, core_even, package_even
204 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
205 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
206 topo.num_threads_per_core + \
207 (core_no) * topo.num_threads_per_core + (thread_no))
208 #define GET_CORE(core_base, core_no, pkg_no) \
209 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
210 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
212 enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
213 enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
214 enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
217 unsigned int msr_num;
218 char name[NAME_BYTES];
220 enum counter_type type;
221 enum counter_format format;
222 struct msr_counter *next;
224 #define FLAGS_HIDE (1 << 0)
225 #define FLAGS_SHOW (1 << 1)
228 struct sys_counters {
229 unsigned int added_thread_counters;
230 unsigned int added_core_counters;
231 unsigned int added_package_counters;
232 struct msr_counter *tp;
233 struct msr_counter *cp;
234 struct msr_counter *pp;
237 struct system_summary {
238 struct thread_data threads;
239 struct core_data cores;
240 struct pkg_data packages;
249 int num_cores_per_pkg;
250 int num_threads_per_core;
253 struct timeval tv_even, tv_odd, tv_delta;
255 int *irq_column_2_cpu; /* /proc/interrupts column numbers */
256 int *irqs_per_cpu; /* indexed by cpu_num */
258 void setup_all_buffers(void);
260 int cpu_is_not_present(int cpu)
262 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
265 * run func(thread, core, package) in topology order
266 * skip non-present cpus
269 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
270 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
272 int retval, pkg_no, core_no, thread_no;
274 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
275 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
276 for (thread_no = 0; thread_no <
277 topo.num_threads_per_core; ++thread_no) {
278 struct thread_data *t;
282 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
284 if (cpu_is_not_present(t->cpu_id))
287 c = GET_CORE(core_base, core_no, pkg_no);
288 p = GET_PKG(pkg_base, pkg_no);
290 retval = func(t, c, p);
299 int cpu_migrate(int cpu)
301 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
302 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
303 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
308 int get_msr_fd(int cpu)
318 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
319 fd = open(pathname, O_RDONLY);
321 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
328 int get_msr(int cpu, off_t offset, unsigned long long *msr)
332 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
334 if (retval != sizeof *msr)
335 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
341 * Each string in this array is compared in --show and --hide cmdline.
342 * Thus, strings that are proper sub-sets must follow their more specific peers.
344 struct msr_counter bic[] = {
350 { 0x0, "SMI", 32, 0, FORMAT_DELTA, NULL},
382 #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
383 #define BIC_Package (1ULL << 0)
384 #define BIC_Avg_MHz (1ULL << 1)
385 #define BIC_Bzy_MHz (1ULL << 2)
386 #define BIC_TSC_MHz (1ULL << 3)
387 #define BIC_IRQ (1ULL << 4)
388 #define BIC_SMI (1ULL << 5)
389 #define BIC_Busy (1ULL << 6)
390 #define BIC_CPU_c1 (1ULL << 7)
391 #define BIC_CPU_c3 (1ULL << 8)
392 #define BIC_CPU_c6 (1ULL << 9)
393 #define BIC_CPU_c7 (1ULL << 10)
394 #define BIC_ThreadC (1ULL << 11)
395 #define BIC_CoreTmp (1ULL << 12)
396 #define BIC_CoreCnt (1ULL << 13)
397 #define BIC_PkgTmp (1ULL << 14)
398 #define BIC_GFX_rc6 (1ULL << 15)
399 #define BIC_GFXMHz (1ULL << 16)
400 #define BIC_Pkgpc2 (1ULL << 17)
401 #define BIC_Pkgpc3 (1ULL << 18)
402 #define BIC_Pkgpc6 (1ULL << 19)
403 #define BIC_Pkgpc7 (1ULL << 20)
404 #define BIC_PkgWatt (1ULL << 21)
405 #define BIC_CorWatt (1ULL << 22)
406 #define BIC_GFXWatt (1ULL << 23)
407 #define BIC_PkgCnt (1ULL << 24)
408 #define BIC_RAMWatt (1ULL << 27)
409 #define BIC_PKG__ (1ULL << 28)
410 #define BIC_RAM__ (1ULL << 29)
411 #define BIC_Pkg_J (1ULL << 30)
412 #define BIC_Cor_J (1ULL << 31)
413 #define BIC_GFX_J (1ULL << 30)
414 #define BIC_RAM_J (1ULL << 31)
415 #define BIC_Core (1ULL << 32)
416 #define BIC_CPU (1ULL << 33)
417 #define BIC_Mod_c6 (1ULL << 34)
419 unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
420 unsigned long long bic_present;
422 #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
423 #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
427 * for all the strings in comma separate name_list,
428 * set the approprate bit in return value.
430 unsigned long long bic_lookup(char *name_list)
433 unsigned long long retval = 0;
438 comma = strchr(name_list, ',');
443 for (i = 0; i < MAX_BIC; ++i) {
444 if (!strcmp(name_list, bic[i].name)) {
445 retval |= (1ULL << i);
450 fprintf(stderr, "Invalid counter name: %s\n", name_list);
462 void print_header(void)
464 struct msr_counter *mp;
466 if (DO_BIC(BIC_Package))
467 outp += sprintf(outp, "\tPackage");
468 if (DO_BIC(BIC_Core))
469 outp += sprintf(outp, "\tCore");
471 outp += sprintf(outp, "\tCPU");
472 if (DO_BIC(BIC_Avg_MHz))
473 outp += sprintf(outp, "\tAvg_MHz");
474 if (DO_BIC(BIC_Busy))
475 outp += sprintf(outp, "\tBusy%%");
476 if (DO_BIC(BIC_Bzy_MHz))
477 outp += sprintf(outp, "\tBzy_MHz");
478 if (DO_BIC(BIC_TSC_MHz))
479 outp += sprintf(outp, "\tTSC_MHz");
485 outp += sprintf(outp, "\tIRQ");
487 outp += sprintf(outp, "\tSMI");
489 if (DO_BIC(BIC_CPU_c1))
490 outp += sprintf(outp, "\tCPU%%c1");
492 for (mp = sys.tp; mp; mp = mp->next) {
493 if (mp->format == FORMAT_RAW) {
495 outp += sprintf(outp, "\t%18.18s", mp->name);
497 outp += sprintf(outp, "\t%10.10s", mp->name);
499 outp += sprintf(outp, "\t%-7.7s", mp->name);
503 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
504 outp += sprintf(outp, "\tCPU%%c3");
505 if (DO_BIC(BIC_CPU_c6))
506 outp += sprintf(outp, "\tCPU%%c6");
507 if (DO_BIC(BIC_CPU_c7))
508 outp += sprintf(outp, "\tCPU%%c7");
510 if (DO_BIC(BIC_Mod_c6))
511 outp += sprintf(outp, "\tMod%%c6");
513 if (DO_BIC(BIC_CoreTmp))
514 outp += sprintf(outp, "\tCoreTmp");
516 for (mp = sys.cp; mp; mp = mp->next) {
517 if (mp->format == FORMAT_RAW) {
519 outp += sprintf(outp, "\t%18.18s", mp->name);
521 outp += sprintf(outp, "\t%10.10s", mp->name);
523 outp += sprintf(outp, "\t%-7.7s", mp->name);
527 if (DO_BIC(BIC_PkgTmp))
528 outp += sprintf(outp, "\tPkgTmp");
530 if (DO_BIC(BIC_GFX_rc6))
531 outp += sprintf(outp, "\tGFX%%rc6");
533 if (DO_BIC(BIC_GFXMHz))
534 outp += sprintf(outp, "\tGFXMHz");
536 if (do_skl_residency) {
537 outp += sprintf(outp, "\tTotl%%C0");
538 outp += sprintf(outp, "\tAny%%C0");
539 outp += sprintf(outp, "\tGFX%%C0");
540 outp += sprintf(outp, "\tCPUGFX%%");
544 outp += sprintf(outp, "\tPkg%%pc2");
546 outp += sprintf(outp, "\tPkg%%pc3");
548 outp += sprintf(outp, "\tPkg%%pc6");
550 outp += sprintf(outp, "\tPkg%%pc7");
552 outp += sprintf(outp, "\tPkg%%pc8");
553 outp += sprintf(outp, "\tPkg%%pc9");
554 outp += sprintf(outp, "\tPk%%pc10");
557 if (do_rapl && !rapl_joules) {
558 if (DO_BIC(BIC_PkgWatt))
559 outp += sprintf(outp, "\tPkgWatt");
560 if (DO_BIC(BIC_CorWatt))
561 outp += sprintf(outp, "\tCorWatt");
562 if (DO_BIC(BIC_GFXWatt))
563 outp += sprintf(outp, "\tGFXWatt");
564 if (DO_BIC(BIC_RAMWatt))
565 outp += sprintf(outp, "\tRAMWatt");
566 if (DO_BIC(BIC_PKG__))
567 outp += sprintf(outp, "\tPKG_%%");
568 if (DO_BIC(BIC_RAM__))
569 outp += sprintf(outp, "\tRAM_%%");
570 } else if (do_rapl && rapl_joules) {
571 if (DO_BIC(BIC_Pkg_J))
572 outp += sprintf(outp, "\tPkg_J");
573 if (DO_BIC(BIC_Cor_J))
574 outp += sprintf(outp, "\tCor_J");
575 if (DO_BIC(BIC_GFX_J))
576 outp += sprintf(outp, "\tGFX_J");
577 if (DO_BIC(BIC_RAM_J))
578 outp += sprintf(outp, "\tRAM_J");
579 if (DO_BIC(BIC_PKG__))
580 outp += sprintf(outp, "\tPKG_%%");
581 if (DO_BIC(BIC_RAM__))
582 outp += sprintf(outp, "\tRAM_%%");
584 for (mp = sys.pp; mp; mp = mp->next) {
585 if (mp->format == FORMAT_RAW) {
587 outp += sprintf(outp, "\t%18.18s", mp->name);
589 outp += sprintf(outp, "\t%10.10s", mp->name);
591 outp += sprintf(outp, "\t%-7.7s", mp->name);
596 outp += sprintf(outp, "\n");
599 int dump_counters(struct thread_data *t, struct core_data *c,
603 struct msr_counter *mp;
605 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
608 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
609 t->cpu_id, t->flags);
610 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
611 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
612 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
613 outp += sprintf(outp, "c1: %016llX\n", t->c1);
616 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
618 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
620 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
621 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
622 i, mp->msr_num, t->counter[i]);
627 outp += sprintf(outp, "core: %d\n", c->core_id);
628 outp += sprintf(outp, "c3: %016llX\n", c->c3);
629 outp += sprintf(outp, "c6: %016llX\n", c->c6);
630 outp += sprintf(outp, "c7: %016llX\n", c->c7);
631 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
633 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
634 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
635 i, mp->msr_num, c->counter[i]);
637 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
641 outp += sprintf(outp, "package: %d\n", p->package_id);
643 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
644 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
645 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
646 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
648 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
650 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
652 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
654 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
655 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
656 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
657 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
658 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
659 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
660 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
661 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
662 outp += sprintf(outp, "Throttle PKG: %0X\n",
663 p->rapl_pkg_perf_status);
664 outp += sprintf(outp, "Throttle RAM: %0X\n",
665 p->rapl_dram_perf_status);
666 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
668 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
669 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
670 i, mp->msr_num, p->counter[i]);
674 outp += sprintf(outp, "\n");
680 * column formatting convention & formats
682 int format_counters(struct thread_data *t, struct core_data *c,
685 double interval_float;
688 struct msr_counter *mp;
690 /* if showing only 1st thread in core and this isn't one, bail out */
691 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
694 /* if showing only 1st thread in pkg and this isn't one, bail out */
695 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
698 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
700 /* topo columns, print blanks on 1st (average) line */
701 if (t == &average.threads) {
702 if (DO_BIC(BIC_Package))
703 outp += sprintf(outp, "\t-");
704 if (DO_BIC(BIC_Core))
705 outp += sprintf(outp, "\t-");
707 outp += sprintf(outp, "\t-");
709 if (DO_BIC(BIC_Package)) {
711 outp += sprintf(outp, "\t%d", p->package_id);
713 outp += sprintf(outp, "\t-");
715 if (DO_BIC(BIC_Core)) {
717 outp += sprintf(outp, "\t%d", c->core_id);
719 outp += sprintf(outp, "\t-");
722 outp += sprintf(outp, "\t%d", t->cpu_id);
725 if (DO_BIC(BIC_Avg_MHz))
726 outp += sprintf(outp, "\t%.0f",
727 1.0 / units * t->aperf / interval_float);
729 if (DO_BIC(BIC_Busy))
730 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
732 if (DO_BIC(BIC_Bzy_MHz)) {
734 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
736 outp += sprintf(outp, "\t%.0f",
737 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
740 if (DO_BIC(BIC_TSC_MHz))
741 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
748 outp += sprintf(outp, "\t%d", t->irq_count);
752 outp += sprintf(outp, "\t%d", t->smi_count);
755 if (DO_BIC(BIC_CPU_c1))
756 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
759 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
760 if (mp->format == FORMAT_RAW) {
762 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
764 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
765 } else if (mp->format == FORMAT_DELTA) {
766 outp += sprintf(outp, "\t%lld", t->counter[i]);
767 } else if (mp->format == FORMAT_PERCENT) {
768 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
772 /* print per-core data only for 1st thread in core */
773 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
776 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
777 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
778 if (DO_BIC(BIC_CPU_c6))
779 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
780 if (DO_BIC(BIC_CPU_c7))
781 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
784 if (DO_BIC(BIC_Mod_c6))
785 outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / t->tsc);
787 if (DO_BIC(BIC_CoreTmp))
788 outp += sprintf(outp, "\t%d", c->core_temp_c);
790 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
791 if (mp->format == FORMAT_RAW) {
793 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
795 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
796 } else if (mp->format == FORMAT_DELTA) {
797 outp += sprintf(outp, "\t%lld", c->counter[i]);
798 } else if (mp->format == FORMAT_PERCENT) {
799 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
803 /* print per-package data only for 1st core in package */
804 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
808 if (DO_BIC(BIC_PkgTmp))
809 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
812 if (DO_BIC(BIC_GFX_rc6)) {
813 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
814 outp += sprintf(outp, "\t**.**");
816 outp += sprintf(outp, "\t%.2f",
817 p->gfx_rc6_ms / 10.0 / interval_float);
822 if (DO_BIC(BIC_GFXMHz))
823 outp += sprintf(outp, "\t%d", p->gfx_mhz);
825 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
826 if (do_skl_residency) {
827 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
828 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
829 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
830 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
834 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
836 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
838 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
840 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
842 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
843 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
844 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
848 * If measurement interval exceeds minimum RAPL Joule Counter range,
849 * indicate that results are suspect by printing "**" in fraction place.
851 if (interval_float < rapl_joule_counter_range)
856 if (DO_BIC(BIC_PkgWatt))
857 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
858 if (DO_BIC(BIC_CorWatt))
859 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
860 if (DO_BIC(BIC_GFXWatt))
861 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
862 if (DO_BIC(BIC_RAMWatt))
863 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
864 if (DO_BIC(BIC_Pkg_J))
865 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
866 if (DO_BIC(BIC_Cor_J))
867 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
868 if (DO_BIC(BIC_GFX_J))
869 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
870 if (DO_BIC(BIC_RAM_J))
871 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
872 if (DO_BIC(BIC_PKG__))
873 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
874 if (DO_BIC(BIC_RAM__))
875 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
877 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
878 if (mp->format == FORMAT_RAW) {
880 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
882 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
883 } else if (mp->format == FORMAT_DELTA) {
884 outp += sprintf(outp, "\t%lld", p->counter[i]);
885 } else if (mp->format == FORMAT_PERCENT) {
886 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
891 outp += sprintf(outp, "\n");
896 void flush_output_stdout(void)
905 fputs(output_buffer, filep);
908 outp = output_buffer;
910 void flush_output_stderr(void)
912 fputs(output_buffer, outf);
914 outp = output_buffer;
916 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
920 if (!printed || !summary_only)
923 if (topo.num_cpus > 1)
924 format_counters(&average.threads, &average.cores,
932 for_all_cpus(format_counters, t, c, p);
935 #define DELTA_WRAP32(new, old) \
939 old = 0x100000000 + new - old; \
943 delta_package(struct pkg_data *new, struct pkg_data *old)
946 struct msr_counter *mp;
948 if (do_skl_residency) {
949 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
950 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
951 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
952 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
954 old->pc2 = new->pc2 - old->pc2;
956 old->pc3 = new->pc3 - old->pc3;
958 old->pc6 = new->pc6 - old->pc6;
960 old->pc7 = new->pc7 - old->pc7;
961 old->pc8 = new->pc8 - old->pc8;
962 old->pc9 = new->pc9 - old->pc9;
963 old->pc10 = new->pc10 - old->pc10;
964 old->pkg_temp_c = new->pkg_temp_c;
966 /* flag an error when rc6 counter resets/wraps */
967 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
968 old->gfx_rc6_ms = -1;
970 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
972 old->gfx_mhz = new->gfx_mhz;
974 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
975 DELTA_WRAP32(new->energy_cores, old->energy_cores);
976 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
977 DELTA_WRAP32(new->energy_dram, old->energy_dram);
978 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
979 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
981 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
982 if (mp->format == FORMAT_RAW)
983 old->counter[i] = new->counter[i];
985 old->counter[i] = new->counter[i] - old->counter[i];
992 delta_core(struct core_data *new, struct core_data *old)
995 struct msr_counter *mp;
997 old->c3 = new->c3 - old->c3;
998 old->c6 = new->c6 - old->c6;
999 old->c7 = new->c7 - old->c7;
1000 old->core_temp_c = new->core_temp_c;
1001 old->mc6_us = new->mc6_us - old->mc6_us;
1003 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1004 if (mp->format == FORMAT_RAW)
1005 old->counter[i] = new->counter[i];
1007 old->counter[i] = new->counter[i] - old->counter[i];
1015 delta_thread(struct thread_data *new, struct thread_data *old,
1016 struct core_data *core_delta)
1019 struct msr_counter *mp;
1021 old->tsc = new->tsc - old->tsc;
1023 /* check for TSC < 1 Mcycles over interval */
1024 if (old->tsc < (1000 * 1000))
1025 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1026 "You can disable all c-states by booting with \"idle=poll\"\n"
1027 "or just the deep ones with \"processor.max_cstate=1\"");
1029 old->c1 = new->c1 - old->c1;
1031 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1032 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1033 old->aperf = new->aperf - old->aperf;
1034 old->mperf = new->mperf - old->mperf;
1041 if (use_c1_residency_msr) {
1043 * Some models have a dedicated C1 residency MSR,
1044 * which should be more accurate than the derivation below.
1048 * As counter collection is not atomic,
1049 * it is possible for mperf's non-halted cycles + idle states
1050 * to exceed TSC's all cycles: show c1 = 0% in that case.
1052 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1055 /* normal case, derive c1 */
1056 old->c1 = old->tsc - old->mperf - core_delta->c3
1057 - core_delta->c6 - core_delta->c7;
1061 if (old->mperf == 0) {
1063 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1064 old->mperf = 1; /* divide by 0 protection */
1067 if (DO_BIC(BIC_IRQ))
1068 old->irq_count = new->irq_count - old->irq_count;
1070 if (DO_BIC(BIC_SMI))
1071 old->smi_count = new->smi_count - old->smi_count;
1073 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1074 if (mp->format == FORMAT_RAW)
1075 old->counter[i] = new->counter[i];
1077 old->counter[i] = new->counter[i] - old->counter[i];
1082 int delta_cpu(struct thread_data *t, struct core_data *c,
1083 struct pkg_data *p, struct thread_data *t2,
1084 struct core_data *c2, struct pkg_data *p2)
1088 /* calculate core delta only for 1st thread in core */
1089 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1092 /* always calculate thread delta */
1093 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1097 /* calculate package delta only for 1st core in package */
1098 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1099 retval = delta_package(p, p2);
1104 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1107 struct msr_counter *mp;
1117 /* tells format_counters to dump all fields from this set */
1118 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1126 p->pkg_wtd_core_c0 = 0;
1127 p->pkg_any_core_c0 = 0;
1128 p->pkg_any_gfxe_c0 = 0;
1129 p->pkg_both_core_gfxe_c0 = 0;
1144 p->energy_cores = 0;
1146 p->rapl_pkg_perf_status = 0;
1147 p->rapl_dram_perf_status = 0;
1152 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1155 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1158 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1161 int sum_counters(struct thread_data *t, struct core_data *c,
1165 struct msr_counter *mp;
1167 average.threads.tsc += t->tsc;
1168 average.threads.aperf += t->aperf;
1169 average.threads.mperf += t->mperf;
1170 average.threads.c1 += t->c1;
1172 average.threads.irq_count += t->irq_count;
1173 average.threads.smi_count += t->smi_count;
1175 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1176 if (mp->format == FORMAT_RAW)
1178 average.threads.counter[i] += t->counter[i];
1181 /* sum per-core values only for 1st thread in core */
1182 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1185 average.cores.c3 += c->c3;
1186 average.cores.c6 += c->c6;
1187 average.cores.c7 += c->c7;
1188 average.cores.mc6_us += c->mc6_us;
1190 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1192 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1193 if (mp->format == FORMAT_RAW)
1195 average.cores.counter[i] += c->counter[i];
1198 /* sum per-pkg values only for 1st core in pkg */
1199 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1202 if (do_skl_residency) {
1203 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1204 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1205 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1206 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1209 average.packages.pc2 += p->pc2;
1211 average.packages.pc3 += p->pc3;
1213 average.packages.pc6 += p->pc6;
1215 average.packages.pc7 += p->pc7;
1216 average.packages.pc8 += p->pc8;
1217 average.packages.pc9 += p->pc9;
1218 average.packages.pc10 += p->pc10;
1220 average.packages.energy_pkg += p->energy_pkg;
1221 average.packages.energy_dram += p->energy_dram;
1222 average.packages.energy_cores += p->energy_cores;
1223 average.packages.energy_gfx += p->energy_gfx;
1225 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1226 average.packages.gfx_mhz = p->gfx_mhz;
1228 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1230 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1231 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1233 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1234 if (mp->format == FORMAT_RAW)
1236 average.packages.counter[i] += p->counter[i];
1241 * sum the counters for all cpus in the system
1242 * compute the weighted average
1244 void compute_average(struct thread_data *t, struct core_data *c,
1248 struct msr_counter *mp;
1250 clear_counters(&average.threads, &average.cores, &average.packages);
1252 for_all_cpus(sum_counters, t, c, p);
1254 average.threads.tsc /= topo.num_cpus;
1255 average.threads.aperf /= topo.num_cpus;
1256 average.threads.mperf /= topo.num_cpus;
1257 average.threads.c1 /= topo.num_cpus;
1259 average.cores.c3 /= topo.num_cores;
1260 average.cores.c6 /= topo.num_cores;
1261 average.cores.c7 /= topo.num_cores;
1262 average.cores.mc6_us /= topo.num_cores;
1264 if (do_skl_residency) {
1265 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1266 average.packages.pkg_any_core_c0 /= topo.num_packages;
1267 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1268 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1271 average.packages.pc2 /= topo.num_packages;
1273 average.packages.pc3 /= topo.num_packages;
1275 average.packages.pc6 /= topo.num_packages;
1277 average.packages.pc7 /= topo.num_packages;
1279 average.packages.pc8 /= topo.num_packages;
1280 average.packages.pc9 /= topo.num_packages;
1281 average.packages.pc10 /= topo.num_packages;
1283 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1284 if (mp->format == FORMAT_RAW)
1286 average.threads.counter[i] /= topo.num_cpus;
1288 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1289 if (mp->format == FORMAT_RAW)
1291 average.cores.counter[i] /= topo.num_cores;
1293 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1294 if (mp->format == FORMAT_RAW)
1296 average.packages.counter[i] /= topo.num_packages;
1300 static unsigned long long rdtsc(void)
1302 unsigned int low, high;
1304 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1306 return low | ((unsigned long long)high) << 32;
1312 * acquire and record local counters for that cpu
1314 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1316 int cpu = t->cpu_id;
1317 unsigned long long msr;
1318 int aperf_mperf_retry_count = 0;
1319 struct msr_counter *mp;
1322 if (cpu_migrate(cpu)) {
1323 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
1328 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1330 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1331 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1334 * The TSC, APERF and MPERF must be read together for
1335 * APERF/MPERF and MPERF/TSC to give accurate results.
1337 * Unfortunately, APERF and MPERF are read by
1338 * individual system call, so delays may occur
1339 * between them. If the time to read them
1340 * varies by a large amount, we re-read them.
1344 * This initial dummy APERF read has been seen to
1345 * reduce jitter in the subsequent reads.
1348 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1351 t->tsc = rdtsc(); /* re-read close to APERF */
1353 tsc_before = t->tsc;
1355 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1358 tsc_between = rdtsc();
1360 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1363 tsc_after = rdtsc();
1365 aperf_time = tsc_between - tsc_before;
1366 mperf_time = tsc_after - tsc_between;
1369 * If the system call latency to read APERF and MPERF
1370 * differ by more than 2x, then try again.
1372 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1373 aperf_mperf_retry_count++;
1374 if (aperf_mperf_retry_count < 5)
1377 warnx("cpu%d jitter %lld %lld",
1378 cpu, aperf_time, mperf_time);
1380 aperf_mperf_retry_count = 0;
1382 t->aperf = t->aperf * aperf_mperf_multiplier;
1383 t->mperf = t->mperf * aperf_mperf_multiplier;
1386 if (DO_BIC(BIC_IRQ))
1387 t->irq_count = irqs_per_cpu[cpu];
1388 if (DO_BIC(BIC_SMI)) {
1389 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1391 t->smi_count = msr & 0xFFFFFFFF;
1393 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1394 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1398 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1399 if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1404 /* collect core counters only for 1st thread in core */
1405 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1408 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
1409 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1413 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1414 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1416 } else if (do_knl_cstates) {
1417 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1421 if (DO_BIC(BIC_CPU_c7))
1422 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1425 if (DO_BIC(BIC_Mod_c6))
1426 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1429 if (DO_BIC(BIC_CoreTmp)) {
1430 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1432 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1435 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1436 if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1440 /* collect package counters only for 1st core in package */
1441 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1444 if (do_skl_residency) {
1445 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1447 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1449 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1451 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1455 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1458 if (do_slm_cstates) {
1459 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1462 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1468 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1471 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1474 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1476 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1478 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1481 if (do_rapl & RAPL_PKG) {
1482 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1484 p->energy_pkg = msr & 0xFFFFFFFF;
1486 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1487 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1489 p->energy_cores = msr & 0xFFFFFFFF;
1491 if (do_rapl & RAPL_DRAM) {
1492 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1494 p->energy_dram = msr & 0xFFFFFFFF;
1496 if (do_rapl & RAPL_GFX) {
1497 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1499 p->energy_gfx = msr & 0xFFFFFFFF;
1501 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1502 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1504 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1506 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1507 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1509 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1511 if (DO_BIC(BIC_PkgTmp)) {
1512 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1514 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1517 if (DO_BIC(BIC_GFX_rc6))
1518 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1520 if (DO_BIC(BIC_GFXMHz))
1521 p->gfx_mhz = gfx_cur_mhz;
1523 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1524 if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1532 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1533 * If you change the values, note they are used both in comparisons
1534 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1537 #define PCLUKN 0 /* Unknown */
1538 #define PCLRSV 1 /* Reserved */
1539 #define PCL__0 2 /* PC0 */
1540 #define PCL__1 3 /* PC1 */
1541 #define PCL__2 4 /* PC2 */
1542 #define PCL__3 5 /* PC3 */
1543 #define PCL__4 6 /* PC4 */
1544 #define PCL__6 7 /* PC6 */
1545 #define PCL_6N 8 /* PC6 No Retention */
1546 #define PCL_6R 9 /* PC6 Retention */
1547 #define PCL__7 10 /* PC7 */
1548 #define PCL_7S 11 /* PC7 Shrink */
1549 #define PCL__8 12 /* PC8 */
1550 #define PCL__9 13 /* PC9 */
1551 #define PCLUNL 14 /* Unlimited */
1553 int pkg_cstate_limit = PCLUKN;
1554 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1555 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1557 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};
1558 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};
1559 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};
1560 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, PCL__6, PCL__7};
1561 int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1562 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};
1563 int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1564 int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1568 calculate_tsc_tweak()
1570 tsc_tweak = base_hz / tsc_hz;
1574 dump_nhm_platform_info(void)
1576 unsigned long long msr;
1579 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1581 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
1583 ratio = (msr >> 40) & 0xFF;
1584 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1585 ratio, bclk, ratio * bclk);
1587 ratio = (msr >> 8) & 0xFF;
1588 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1589 ratio, bclk, ratio * bclk);
1591 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1592 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1593 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
1599 dump_hsw_turbo_ratio_limits(void)
1601 unsigned long long msr;
1604 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1606 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1608 ratio = (msr >> 8) & 0xFF;
1610 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
1611 ratio, bclk, ratio * bclk);
1613 ratio = (msr >> 0) & 0xFF;
1615 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
1616 ratio, bclk, ratio * bclk);
1621 dump_ivt_turbo_ratio_limits(void)
1623 unsigned long long msr;
1626 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
1628 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
1630 ratio = (msr >> 56) & 0xFF;
1632 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
1633 ratio, bclk, ratio * bclk);
1635 ratio = (msr >> 48) & 0xFF;
1637 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
1638 ratio, bclk, ratio * bclk);
1640 ratio = (msr >> 40) & 0xFF;
1642 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
1643 ratio, bclk, ratio * bclk);
1645 ratio = (msr >> 32) & 0xFF;
1647 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
1648 ratio, bclk, ratio * bclk);
1650 ratio = (msr >> 24) & 0xFF;
1652 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
1653 ratio, bclk, ratio * bclk);
1655 ratio = (msr >> 16) & 0xFF;
1657 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
1658 ratio, bclk, ratio * bclk);
1660 ratio = (msr >> 8) & 0xFF;
1662 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
1663 ratio, bclk, ratio * bclk);
1665 ratio = (msr >> 0) & 0xFF;
1667 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
1668 ratio, bclk, ratio * bclk);
1673 dump_nhm_turbo_ratio_limits(void)
1675 unsigned long long msr;
1678 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1680 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
1682 ratio = (msr >> 56) & 0xFF;
1684 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 8 active cores\n",
1685 ratio, bclk, ratio * bclk);
1687 ratio = (msr >> 48) & 0xFF;
1689 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 7 active cores\n",
1690 ratio, bclk, ratio * bclk);
1692 ratio = (msr >> 40) & 0xFF;
1694 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 6 active cores\n",
1695 ratio, bclk, ratio * bclk);
1697 ratio = (msr >> 32) & 0xFF;
1699 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 5 active cores\n",
1700 ratio, bclk, ratio * bclk);
1702 ratio = (msr >> 24) & 0xFF;
1704 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1705 ratio, bclk, ratio * bclk);
1707 ratio = (msr >> 16) & 0xFF;
1709 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1710 ratio, bclk, ratio * bclk);
1712 ratio = (msr >> 8) & 0xFF;
1714 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1715 ratio, bclk, ratio * bclk);
1717 ratio = (msr >> 0) & 0xFF;
1719 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active cores\n",
1720 ratio, bclk, ratio * bclk);
1725 dump_atom_turbo_ratio_limits(void)
1727 unsigned long long msr;
1730 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1731 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1733 ratio = (msr >> 0) & 0x3F;
1735 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1736 ratio, bclk, ratio * bclk);
1738 ratio = (msr >> 8) & 0x3F;
1740 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1741 ratio, bclk, ratio * bclk);
1743 ratio = (msr >> 16) & 0x3F;
1745 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1746 ratio, bclk, ratio * bclk);
1748 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1749 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1751 ratio = (msr >> 24) & 0x3F;
1753 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1754 ratio, bclk, ratio * bclk);
1756 ratio = (msr >> 16) & 0x3F;
1758 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1759 ratio, bclk, ratio * bclk);
1761 ratio = (msr >> 8) & 0x3F;
1763 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1764 ratio, bclk, ratio * bclk);
1766 ratio = (msr >> 0) & 0x3F;
1768 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1769 ratio, bclk, ratio * bclk);
1773 dump_knl_turbo_ratio_limits(void)
1775 const unsigned int buckets_no = 7;
1777 unsigned long long msr;
1778 int delta_cores, delta_ratio;
1780 unsigned int cores[buckets_no];
1781 unsigned int ratio[buckets_no];
1783 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1785 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
1789 * Turbo encoding in KNL is as follows:
1791 * [7:1] -- Base value of number of active cores of bucket 1.
1792 * [15:8] -- Base value of freq ratio of bucket 1.
1793 * [20:16] -- +ve delta of number of active cores of bucket 2.
1794 * i.e. active cores of bucket 2 =
1795 * active cores of bucket 1 + delta
1796 * [23:21] -- Negative delta of freq ratio of bucket 2.
1797 * i.e. freq ratio of bucket 2 =
1798 * freq ratio of bucket 1 - delta
1799 * [28:24]-- +ve delta of number of active cores of bucket 3.
1800 * [31:29]-- -ve delta of freq ratio of bucket 3.
1801 * [36:32]-- +ve delta of number of active cores of bucket 4.
1802 * [39:37]-- -ve delta of freq ratio of bucket 4.
1803 * [44:40]-- +ve delta of number of active cores of bucket 5.
1804 * [47:45]-- -ve delta of freq ratio of bucket 5.
1805 * [52:48]-- +ve delta of number of active cores of bucket 6.
1806 * [55:53]-- -ve delta of freq ratio of bucket 6.
1807 * [60:56]-- +ve delta of number of active cores of bucket 7.
1808 * [63:61]-- -ve delta of freq ratio of bucket 7.
1812 cores[b_nr] = (msr & 0xFF) >> 1;
1813 ratio[b_nr] = (msr >> 8) & 0xFF;
1815 for (i = 16; i < 64; i += 8) {
1816 delta_cores = (msr >> i) & 0x1F;
1817 delta_ratio = (msr >> (i + 5)) & 0x7;
1819 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1820 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1824 for (i = buckets_no - 1; i >= 0; i--)
1825 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
1827 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1828 ratio[i], bclk, ratio[i] * bclk, cores[i]);
1832 dump_nhm_cst_cfg(void)
1834 unsigned long long msr;
1836 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
1838 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1839 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1841 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
1843 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1844 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1845 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1846 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1847 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1848 (msr & (1 << 15)) ? "" : "UN",
1849 (unsigned int)msr & 0xF,
1850 pkg_cstate_limit_strings[pkg_cstate_limit]);
1855 dump_config_tdp(void)
1857 unsigned long long msr;
1859 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1860 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1861 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
1863 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1864 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
1866 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1867 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1868 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1869 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
1871 fprintf(outf, ")\n");
1873 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1874 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
1876 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1877 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1878 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1879 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
1881 fprintf(outf, ")\n");
1883 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1884 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
1886 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1887 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1888 fprintf(outf, ")\n");
1890 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1891 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1892 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
1893 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1894 fprintf(outf, ")\n");
1897 unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1899 void print_irtl(void)
1901 unsigned long long msr;
1903 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1904 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1905 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1906 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1908 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1909 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1910 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1911 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1913 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1914 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
1915 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1916 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1921 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1922 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
1923 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1924 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1926 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
1927 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
1928 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1929 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1931 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
1932 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
1933 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1934 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1937 void free_fd_percpu(void)
1941 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
1942 if (fd_percpu[i] != 0)
1943 close(fd_percpu[i]);
1949 void free_all_buffers(void)
1951 CPU_FREE(cpu_present_set);
1952 cpu_present_set = NULL;
1953 cpu_present_setsize = 0;
1955 CPU_FREE(cpu_affinity_set);
1956 cpu_affinity_set = NULL;
1957 cpu_affinity_setsize = 0;
1965 package_even = NULL;
1975 free(output_buffer);
1976 output_buffer = NULL;
1981 free(irq_column_2_cpu);
1986 * Open a file, and exit on failure
1988 FILE *fopen_or_die(const char *path, const char *mode)
1990 FILE *filep = fopen(path, mode);
1992 err(1, "%s: open failed", path);
1997 * Parse a file containing a single int.
1999 int parse_int_file(const char *fmt, ...)
2002 char path[PATH_MAX];
2006 va_start(args, fmt);
2007 vsnprintf(path, sizeof(path), fmt, args);
2009 filep = fopen_or_die(path, "r");
2010 if (fscanf(filep, "%d", &value) != 1)
2011 err(1, "%s: failed to parse number from file", path);
2017 * get_cpu_position_in_core(cpu)
2018 * return the position of the CPU among its HT siblings in the core
2019 * return -1 if the sibling is not in list
2021 int get_cpu_position_in_core(int cpu)
2030 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2032 filep = fopen(path, "r");
2033 if (filep == NULL) {
2038 for (i = 0; i < topo.num_threads_per_core; i++) {
2039 fscanf(filep, "%d", &this_cpu);
2040 if (this_cpu == cpu) {
2045 /* Account for no separator after last thread*/
2046 if (i != (topo.num_threads_per_core - 1))
2047 fscanf(filep, "%c", &character);
2055 * cpu_is_first_core_in_package(cpu)
2056 * return 1 if given CPU is 1st core in package
2058 int cpu_is_first_core_in_package(int cpu)
2060 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2063 int get_physical_package_id(int cpu)
2065 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2068 int get_core_id(int cpu)
2070 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2073 int get_num_ht_siblings(int cpu)
2083 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
2084 filep = fopen_or_die(path, "r");
2088 * A ',' separated or '-' separated set of numbers
2089 * (eg 1-2 or 1,3,4,5)
2091 fscanf(filep, "%d%c\n", &sib1, &character);
2092 fseek(filep, 0, SEEK_SET);
2093 fgets(str, 100, filep);
2094 ch = strchr(str, character);
2095 while (ch != NULL) {
2097 ch = strchr(ch+1, character);
2105 * run func(thread, core, package) in topology order
2106 * skip non-present cpus
2109 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2110 struct pkg_data *, struct thread_data *, struct core_data *,
2111 struct pkg_data *), struct thread_data *thread_base,
2112 struct core_data *core_base, struct pkg_data *pkg_base,
2113 struct thread_data *thread_base2, struct core_data *core_base2,
2114 struct pkg_data *pkg_base2)
2116 int retval, pkg_no, core_no, thread_no;
2118 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2119 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2120 for (thread_no = 0; thread_no <
2121 topo.num_threads_per_core; ++thread_no) {
2122 struct thread_data *t, *t2;
2123 struct core_data *c, *c2;
2124 struct pkg_data *p, *p2;
2126 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2128 if (cpu_is_not_present(t->cpu_id))
2131 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2133 c = GET_CORE(core_base, core_no, pkg_no);
2134 c2 = GET_CORE(core_base2, core_no, pkg_no);
2136 p = GET_PKG(pkg_base, pkg_no);
2137 p2 = GET_PKG(pkg_base2, pkg_no);
2139 retval = func(t, c, p, t2, c2, p2);
2149 * run func(cpu) on every cpu in /proc/stat
2150 * return max_cpu number
2152 int for_all_proc_cpus(int (func)(int))
2158 fp = fopen_or_die(proc_stat, "r");
2160 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2162 err(1, "%s: failed to parse format", proc_stat);
2165 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2169 retval = func(cpu_num);
2179 void re_initialize(void)
2182 setup_all_buffers();
2183 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2189 * remember the last one seen, it will be the max
2191 int count_cpus(int cpu)
2193 if (topo.max_cpu_num < cpu)
2194 topo.max_cpu_num = cpu;
2199 int mark_cpu_present(int cpu)
2201 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
2206 * snapshot_proc_interrupts()
2208 * read and record summary of /proc/interrupts
2210 * return 1 if config change requires a restart, else return 0
2212 int snapshot_proc_interrupts(void)
2218 fp = fopen_or_die("/proc/interrupts", "r");
2222 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2223 for (column = 0; column < topo.num_cpus; ++column) {
2226 retval = fscanf(fp, " CPU%d", &cpu_number);
2230 if (cpu_number > topo.max_cpu_num) {
2231 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2235 irq_column_2_cpu[column] = cpu_number;
2236 irqs_per_cpu[cpu_number] = 0;
2239 /* read /proc/interrupt count lines and sum up irqs per cpu */
2244 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2248 /* read the count per cpu */
2249 for (column = 0; column < topo.num_cpus; ++column) {
2251 int cpu_number, irq_count;
2253 retval = fscanf(fp, " %d", &irq_count);
2257 cpu_number = irq_column_2_cpu[column];
2258 irqs_per_cpu[cpu_number] += irq_count;
2262 while (getc(fp) != '\n')
2263 ; /* flush interrupt description */
2269 * snapshot_gfx_rc6_ms()
2271 * record snapshot of
2272 * /sys/class/drm/card0/power/rc6_residency_ms
2274 * return 1 if config change requires a restart, else return 0
2276 int snapshot_gfx_rc6_ms(void)
2281 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2283 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2292 * snapshot_gfx_mhz()
2294 * record snapshot of
2295 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2297 * return 1 if config change requires a restart, else return 0
2299 int snapshot_gfx_mhz(void)
2305 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2309 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2317 * snapshot /proc and /sys files
2319 * return 1 if configuration restart needed, else return 0
2321 int snapshot_proc_sysfs_files(void)
2323 if (snapshot_proc_interrupts())
2326 if (DO_BIC(BIC_GFX_rc6))
2327 snapshot_gfx_rc6_ms();
2329 if (DO_BIC(BIC_GFXMHz))
2335 void turbostat_loop()
2343 snapshot_proc_sysfs_files();
2344 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2347 } else if (retval == -1) {
2348 if (restarted > 1) {
2355 gettimeofday(&tv_even, (struct timezone *)NULL);
2358 if (for_all_proc_cpus(cpu_is_not_present)) {
2362 nanosleep(&interval_ts, NULL);
2363 if (snapshot_proc_sysfs_files())
2365 retval = for_all_cpus(get_counters, ODD_COUNTERS);
2368 } else if (retval == -1) {
2372 gettimeofday(&tv_odd, (struct timezone *)NULL);
2373 timersub(&tv_odd, &tv_even, &tv_delta);
2374 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2378 compute_average(EVEN_COUNTERS);
2379 format_all_counters(EVEN_COUNTERS);
2380 flush_output_stdout();
2381 nanosleep(&interval_ts, NULL);
2382 if (snapshot_proc_sysfs_files())
2384 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2387 } else if (retval == -1) {
2391 gettimeofday(&tv_even, (struct timezone *)NULL);
2392 timersub(&tv_even, &tv_odd, &tv_delta);
2393 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2397 compute_average(ODD_COUNTERS);
2398 format_all_counters(ODD_COUNTERS);
2399 flush_output_stdout();
2403 void check_dev_msr()
2408 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2409 if (stat(pathname, &sb))
2410 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2411 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2414 void check_permissions()
2416 struct __user_cap_header_struct cap_header_data;
2417 cap_user_header_t cap_header = &cap_header_data;
2418 struct __user_cap_data_struct cap_data_data;
2419 cap_user_data_t cap_data = &cap_data_data;
2420 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2424 /* check for CAP_SYS_RAWIO */
2425 cap_header->pid = getpid();
2426 cap_header->version = _LINUX_CAPABILITY_VERSION;
2427 if (capget(cap_header, cap_data) < 0)
2428 err(-6, "capget(2) failed");
2430 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2432 warnx("capget(CAP_SYS_RAWIO) failed,"
2433 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2436 /* test file permissions */
2437 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2438 if (euidaccess(pathname, R_OK)) {
2440 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2443 /* if all else fails, thell them to be root */
2446 warnx("... or simply run as root");
2453 * NHM adds support for additional MSRs:
2455 * MSR_SMI_COUNT 0x00000034
2457 * MSR_PLATFORM_INFO 0x000000ce
2458 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
2460 * MSR_MISC_PWR_MGMT 0x000001aa
2462 * MSR_PKG_C3_RESIDENCY 0x000003f8
2463 * MSR_PKG_C6_RESIDENCY 0x000003f9
2464 * MSR_CORE_C3_RESIDENCY 0x000003fc
2465 * MSR_CORE_C6_RESIDENCY 0x000003fd
2468 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
2470 int probe_nhm_msrs(unsigned int family, unsigned int model)
2472 unsigned long long msr;
2473 unsigned int base_ratio;
2474 int *pkg_cstate_limits;
2482 bclk = discover_bclk(family, model);
2485 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2486 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2487 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
2488 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2489 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2490 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2491 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2492 pkg_cstate_limits = nhm_pkg_cstate_limits;
2494 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2495 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2496 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2497 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2498 pkg_cstate_limits = snb_pkg_cstate_limits;
2500 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2501 case INTEL_FAM6_HASWELL_X: /* HSX */
2502 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2503 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2504 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2505 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2506 case INTEL_FAM6_BROADWELL_X: /* BDX */
2507 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2508 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2509 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2510 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2511 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2512 pkg_cstate_limits = hsw_pkg_cstate_limits;
2514 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2515 pkg_cstate_limits = skx_pkg_cstate_limits;
2517 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
2518 no_MSR_MISC_PWR_MGMT = 1;
2519 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
2520 pkg_cstate_limits = slv_pkg_cstate_limits;
2522 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
2523 pkg_cstate_limits = amt_pkg_cstate_limits;
2524 no_MSR_MISC_PWR_MGMT = 1;
2526 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
2527 case INTEL_FAM6_XEON_PHI_KNM:
2528 pkg_cstate_limits = phi_pkg_cstate_limits;
2530 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
2531 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
2532 pkg_cstate_limits = bxt_pkg_cstate_limits;
2537 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2538 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2540 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2541 base_ratio = (msr >> 8) & 0xFF;
2543 base_hz = base_ratio * bclk * 1000000;
2548 * SLV client has supporet for unique MSRs:
2550 * MSR_CC6_DEMOTION_POLICY_CONFIG
2551 * MSR_MC6_DEMOTION_POLICY_CONFIG
2554 int has_slv_msrs(unsigned int family, unsigned int model)
2560 case INTEL_FAM6_ATOM_SILVERMONT1:
2561 case INTEL_FAM6_ATOM_MERRIFIELD:
2562 case INTEL_FAM6_ATOM_MOOREFIELD:
2568 int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2570 if (has_slv_msrs(family, model))
2574 /* Nehalem compatible, but do not include turbo-ratio limit support */
2575 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2576 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2577 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
2578 case INTEL_FAM6_XEON_PHI_KNM:
2584 int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2586 if (has_slv_msrs(family, model))
2591 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2600 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2601 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
2607 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2616 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
2623 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2632 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
2633 case INTEL_FAM6_XEON_PHI_KNM:
2639 int has_config_tdp(unsigned int family, unsigned int model)
2648 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2649 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2650 case INTEL_FAM6_HASWELL_X: /* HSX */
2651 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2652 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2653 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2654 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2655 case INTEL_FAM6_BROADWELL_X: /* BDX */
2656 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2657 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2658 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2659 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2660 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2661 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2663 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
2664 case INTEL_FAM6_XEON_PHI_KNM:
2672 dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
2674 if (!do_nhm_platform_info)
2677 dump_nhm_platform_info();
2679 if (has_hsw_turbo_ratio_limit(family, model))
2680 dump_hsw_turbo_ratio_limits();
2682 if (has_ivt_turbo_ratio_limit(family, model))
2683 dump_ivt_turbo_ratio_limits();
2685 if (has_nhm_turbo_ratio_limit(family, model))
2686 dump_nhm_turbo_ratio_limits();
2688 if (has_atom_turbo_ratio_limit(family, model))
2689 dump_atom_turbo_ratio_limits();
2691 if (has_knl_turbo_ratio_limit(family, model))
2692 dump_knl_turbo_ratio_limits();
2694 if (has_config_tdp(family, model))
2703 * Decode the ENERGY_PERF_BIAS MSR
2705 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2707 unsigned long long msr;
2716 /* EPB is per-package */
2717 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2720 if (cpu_migrate(cpu)) {
2721 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2725 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2728 switch (msr & 0xF) {
2729 case ENERGY_PERF_BIAS_PERFORMANCE:
2730 epb_string = "performance";
2732 case ENERGY_PERF_BIAS_NORMAL:
2733 epb_string = "balanced";
2735 case ENERGY_PERF_BIAS_POWERSAVE:
2736 epb_string = "powersave";
2739 epb_string = "custom";
2742 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2748 * Decode the MSR_HWP_CAPABILITIES
2750 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2752 unsigned long long msr;
2760 /* MSR_HWP_CAPABILITIES is per-package */
2761 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2764 if (cpu_migrate(cpu)) {
2765 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2769 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2772 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
2773 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2775 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2776 if ((msr & (1 << 0)) == 0)
2779 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2782 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
2783 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2785 (unsigned int)HWP_HIGHEST_PERF(msr),
2786 (unsigned int)HWP_GUARANTEED_PERF(msr),
2787 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2788 (unsigned int)HWP_LOWEST_PERF(msr));
2790 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2793 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
2794 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2796 (unsigned int)(((msr) >> 0) & 0xff),
2797 (unsigned int)(((msr) >> 8) & 0xff),
2798 (unsigned int)(((msr) >> 16) & 0xff),
2799 (unsigned int)(((msr) >> 24) & 0xff),
2800 (unsigned int)(((msr) >> 32) & 0xff3),
2801 (unsigned int)(((msr) >> 42) & 0x1));
2804 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2807 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
2808 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2810 (unsigned int)(((msr) >> 0) & 0xff),
2811 (unsigned int)(((msr) >> 8) & 0xff),
2812 (unsigned int)(((msr) >> 16) & 0xff),
2813 (unsigned int)(((msr) >> 24) & 0xff),
2814 (unsigned int)(((msr) >> 32) & 0xff3));
2816 if (has_hwp_notify) {
2817 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2820 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
2821 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2823 ((msr) & 0x1) ? "EN" : "Dis",
2824 ((msr) & 0x2) ? "EN" : "Dis");
2826 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2829 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
2830 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2832 ((msr) & 0x1) ? "" : "No-",
2833 ((msr) & 0x2) ? "" : "No-");
2839 * print_perf_limit()
2841 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2843 unsigned long long msr;
2849 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2852 if (cpu_migrate(cpu)) {
2853 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2857 if (do_core_perf_limit_reasons) {
2858 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2859 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2860 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2861 (msr & 1 << 15) ? "bit15, " : "",
2862 (msr & 1 << 14) ? "bit14, " : "",
2863 (msr & 1 << 13) ? "Transitions, " : "",
2864 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2865 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2866 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2867 (msr & 1 << 9) ? "CorePwr, " : "",
2868 (msr & 1 << 8) ? "Amps, " : "",
2869 (msr & 1 << 6) ? "VR-Therm, " : "",
2870 (msr & 1 << 5) ? "Auto-HWP, " : "",
2871 (msr & 1 << 4) ? "Graphics, " : "",
2872 (msr & 1 << 2) ? "bit2, " : "",
2873 (msr & 1 << 1) ? "ThermStatus, " : "",
2874 (msr & 1 << 0) ? "PROCHOT, " : "");
2875 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2876 (msr & 1 << 31) ? "bit31, " : "",
2877 (msr & 1 << 30) ? "bit30, " : "",
2878 (msr & 1 << 29) ? "Transitions, " : "",
2879 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2880 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2881 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2882 (msr & 1 << 25) ? "CorePwr, " : "",
2883 (msr & 1 << 24) ? "Amps, " : "",
2884 (msr & 1 << 22) ? "VR-Therm, " : "",
2885 (msr & 1 << 21) ? "Auto-HWP, " : "",
2886 (msr & 1 << 20) ? "Graphics, " : "",
2887 (msr & 1 << 18) ? "bit18, " : "",
2888 (msr & 1 << 17) ? "ThermStatus, " : "",
2889 (msr & 1 << 16) ? "PROCHOT, " : "");
2892 if (do_gfx_perf_limit_reasons) {
2893 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2894 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2895 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
2896 (msr & 1 << 0) ? "PROCHOT, " : "",
2897 (msr & 1 << 1) ? "ThermStatus, " : "",
2898 (msr & 1 << 4) ? "Graphics, " : "",
2899 (msr & 1 << 6) ? "VR-Therm, " : "",
2900 (msr & 1 << 8) ? "Amps, " : "",
2901 (msr & 1 << 9) ? "GFXPwr, " : "",
2902 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2903 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2904 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
2905 (msr & 1 << 16) ? "PROCHOT, " : "",
2906 (msr & 1 << 17) ? "ThermStatus, " : "",
2907 (msr & 1 << 20) ? "Graphics, " : "",
2908 (msr & 1 << 22) ? "VR-Therm, " : "",
2909 (msr & 1 << 24) ? "Amps, " : "",
2910 (msr & 1 << 25) ? "GFXPwr, " : "",
2911 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2912 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2914 if (do_ring_perf_limit_reasons) {
2915 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2916 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2917 fprintf(outf, " (Active: %s%s%s%s%s%s)",
2918 (msr & 1 << 0) ? "PROCHOT, " : "",
2919 (msr & 1 << 1) ? "ThermStatus, " : "",
2920 (msr & 1 << 6) ? "VR-Therm, " : "",
2921 (msr & 1 << 8) ? "Amps, " : "",
2922 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2923 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2924 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
2925 (msr & 1 << 16) ? "PROCHOT, " : "",
2926 (msr & 1 << 17) ? "ThermStatus, " : "",
2927 (msr & 1 << 22) ? "VR-Therm, " : "",
2928 (msr & 1 << 24) ? "Amps, " : "",
2929 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2930 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2935 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2936 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2938 double get_tdp(unsigned int model)
2940 unsigned long long msr;
2942 if (do_rapl & RAPL_PKG_POWER_INFO)
2943 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
2944 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2947 case INTEL_FAM6_ATOM_SILVERMONT1:
2948 case INTEL_FAM6_ATOM_SILVERMONT2:
2956 * rapl_dram_energy_units_probe()
2957 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2960 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2962 /* only called for genuine_intel, family 6 */
2965 case INTEL_FAM6_HASWELL_X: /* HSX */
2966 case INTEL_FAM6_BROADWELL_X: /* BDX */
2967 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2968 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
2969 case INTEL_FAM6_XEON_PHI_KNM:
2970 return (rapl_dram_energy_units = 15.3 / 1000000);
2972 return (rapl_energy_units);
2980 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2982 void rapl_probe(unsigned int family, unsigned int model)
2984 unsigned long long msr;
2985 unsigned int time_unit;
2995 case INTEL_FAM6_SANDYBRIDGE:
2996 case INTEL_FAM6_IVYBRIDGE:
2997 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2998 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2999 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3000 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3001 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3002 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3004 BIC_PRESENT(BIC_Pkg_J);
3005 BIC_PRESENT(BIC_Cor_J);
3006 BIC_PRESENT(BIC_GFX_J);
3008 BIC_PRESENT(BIC_PkgWatt);
3009 BIC_PRESENT(BIC_CorWatt);
3010 BIC_PRESENT(BIC_GFXWatt);
3013 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3014 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3016 BIC_PRESENT(BIC_Pkg_J);
3018 BIC_PRESENT(BIC_PkgWatt);
3020 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3021 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3022 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3023 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3024 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3025 BIC_PRESENT(BIC_PKG__);
3026 BIC_PRESENT(BIC_RAM__);
3028 BIC_PRESENT(BIC_Pkg_J);
3029 BIC_PRESENT(BIC_Cor_J);
3030 BIC_PRESENT(BIC_RAM_J);
3032 BIC_PRESENT(BIC_PkgWatt);
3033 BIC_PRESENT(BIC_CorWatt);
3034 BIC_PRESENT(BIC_RAMWatt);
3037 case INTEL_FAM6_HASWELL_X: /* HSX */
3038 case INTEL_FAM6_BROADWELL_X: /* BDX */
3039 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3040 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3041 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
3042 case INTEL_FAM6_XEON_PHI_KNM:
3043 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3044 BIC_PRESENT(BIC_PKG__);
3045 BIC_PRESENT(BIC_RAM__);
3047 BIC_PRESENT(BIC_Pkg_J);
3048 BIC_PRESENT(BIC_RAM_J);
3050 BIC_PRESENT(BIC_PkgWatt);
3051 BIC_PRESENT(BIC_RAMWatt);
3054 case INTEL_FAM6_SANDYBRIDGE_X:
3055 case INTEL_FAM6_IVYBRIDGE_X:
3056 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;
3057 BIC_PRESENT(BIC_PKG__);
3058 BIC_PRESENT(BIC_RAM__);
3060 BIC_PRESENT(BIC_Pkg_J);
3061 BIC_PRESENT(BIC_Cor_J);
3062 BIC_PRESENT(BIC_RAM_J);
3064 BIC_PRESENT(BIC_PkgWatt);
3065 BIC_PRESENT(BIC_CorWatt);
3066 BIC_PRESENT(BIC_RAMWatt);
3069 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3070 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
3071 do_rapl = RAPL_PKG | RAPL_CORES;
3073 BIC_PRESENT(BIC_Pkg_J);
3074 BIC_PRESENT(BIC_Cor_J);
3076 BIC_PRESENT(BIC_PkgWatt);
3077 BIC_PRESENT(BIC_CorWatt);
3080 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
3081 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
3082 BIC_PRESENT(BIC_PKG__);
3083 BIC_PRESENT(BIC_RAM__);
3085 BIC_PRESENT(BIC_Pkg_J);
3086 BIC_PRESENT(BIC_Cor_J);
3087 BIC_PRESENT(BIC_RAM_J);
3089 BIC_PRESENT(BIC_PkgWatt);
3090 BIC_PRESENT(BIC_CorWatt);
3091 BIC_PRESENT(BIC_RAMWatt);
3098 /* units on package 0, verify later other packages match */
3099 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3102 rapl_power_units = 1.0 / (1 << (msr & 0xF));
3103 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3104 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3106 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3108 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3110 time_unit = msr >> 16 & 0xF;
3114 rapl_time_units = 1.0 / (1 << (time_unit));
3116 tdp = get_tdp(model);
3118 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
3120 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3125 void perf_limit_reasons_probe(unsigned int family, unsigned int model)
3134 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3135 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3136 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3137 do_gfx_perf_limit_reasons = 1;
3138 case INTEL_FAM6_HASWELL_X: /* HSX */
3139 do_core_perf_limit_reasons = 1;
3140 do_ring_perf_limit_reasons = 1;
3146 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3148 unsigned long long msr;
3152 if (!(do_dts || do_ptm))
3157 /* DTS is per-core, no need to print for each thread */
3158 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3161 if (cpu_migrate(cpu)) {
3162 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3166 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3167 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3170 dts = (msr >> 16) & 0x7F;
3171 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3172 cpu, msr, tcc_activation_temp - dts);
3175 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3178 dts = (msr >> 16) & 0x7F;
3179 dts2 = (msr >> 8) & 0x7F;
3180 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3181 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3187 unsigned int resolution;
3189 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3192 dts = (msr >> 16) & 0x7F;
3193 resolution = (msr >> 27) & 0xF;
3194 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3195 cpu, msr, tcc_activation_temp - dts, resolution);
3198 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3201 dts = (msr >> 16) & 0x7F;
3202 dts2 = (msr >> 8) & 0x7F;
3203 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3204 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3211 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3213 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3215 ((msr >> 15) & 1) ? "EN" : "DIS",
3216 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3217 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3218 (((msr >> 16) & 1) ? "EN" : "DIS"));
3223 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3225 unsigned long long msr;
3231 /* RAPL counters are per package, so print only for 1st thread/package */
3232 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3236 if (cpu_migrate(cpu)) {
3237 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3241 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3245 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
3246 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3247 rapl_power_units, rapl_energy_units, rapl_time_units);
3249 if (do_rapl & RAPL_PKG_POWER_INFO) {
3251 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3255 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3257 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3258 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3259 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3260 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3263 if (do_rapl & RAPL_PKG) {
3265 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3268 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3269 cpu, msr, (msr >> 63) & 1 ? "": "UN");
3271 print_power_limit_msr(cpu, msr, "PKG Limit #1");
3272 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3274 ((msr >> 47) & 1) ? "EN" : "DIS",
3275 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3276 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3277 ((msr >> 48) & 1) ? "EN" : "DIS");
3280 if (do_rapl & RAPL_DRAM_POWER_INFO) {
3281 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3284 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3286 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3287 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3288 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3289 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3291 if (do_rapl & RAPL_DRAM) {
3292 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3294 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3295 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3297 print_power_limit_msr(cpu, msr, "DRAM Limit");
3299 if (do_rapl & RAPL_CORE_POLICY) {
3301 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3304 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3307 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3309 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3311 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3312 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3313 print_power_limit_msr(cpu, msr, "Cores Limit");
3316 if (do_rapl & RAPL_GFX) {
3318 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3321 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3323 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3325 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3326 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3327 print_power_limit_msr(cpu, msr, "GFX Limit");
3334 * SNB adds support for additional MSRs:
3336 * MSR_PKG_C7_RESIDENCY 0x000003fa
3337 * MSR_CORE_C7_RESIDENCY 0x000003fe
3338 * MSR_PKG_C2_RESIDENCY 0x0000060d
3341 int has_snb_msrs(unsigned int family, unsigned int model)
3347 case INTEL_FAM6_SANDYBRIDGE:
3348 case INTEL_FAM6_SANDYBRIDGE_X:
3349 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3350 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3351 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3352 case INTEL_FAM6_HASWELL_X: /* HSW */
3353 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3354 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3355 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3356 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3357 case INTEL_FAM6_BROADWELL_X: /* BDX */
3358 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3359 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3360 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3361 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3362 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3363 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3364 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3365 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
3372 * HSW adds support for additional MSRs:
3374 * MSR_PKG_C8_RESIDENCY 0x00000630
3375 * MSR_PKG_C9_RESIDENCY 0x00000631
3376 * MSR_PKG_C10_RESIDENCY 0x00000632
3378 * MSR_PKGC8_IRTL 0x00000633
3379 * MSR_PKGC9_IRTL 0x00000634
3380 * MSR_PKGC10_IRTL 0x00000635
3383 int has_hsw_msrs(unsigned int family, unsigned int model)
3389 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3390 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3391 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3392 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3393 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3394 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3395 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3402 * SKL adds support for additional MSRS:
3404 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3405 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3406 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3407 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3409 int has_skl_msrs(unsigned int family, unsigned int model)
3415 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3416 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3417 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3418 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3424 int is_slm(unsigned int family, unsigned int model)
3429 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3430 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
3436 int is_knl(unsigned int family, unsigned int model)
3441 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
3442 case INTEL_FAM6_XEON_PHI_KNM:
3448 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3450 if (is_knl(family, model))
3455 #define SLM_BCLK_FREQS 5
3456 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3458 double slm_bclk(void)
3460 unsigned long long msr = 3;
3464 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3465 fprintf(outf, "SLM BCLK: unknown\n");
3468 if (i >= SLM_BCLK_FREQS) {
3469 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
3472 freq = slm_freq_table[i];
3475 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3480 double discover_bclk(unsigned int family, unsigned int model)
3482 if (has_snb_msrs(family, model) || is_knl(family, model))
3484 else if (is_slm(family, model))
3491 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3492 * the Thermal Control Circuit (TCC) activates.
3493 * This is usually equal to tjMax.
3495 * Older processors do not have this MSR, so there we guess,
3496 * but also allow cmdline over-ride with -T.
3498 * Several MSR temperature values are in units of degrees-C
3499 * below this value, including the Digital Thermal Sensor (DTS),
3500 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3502 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3504 unsigned long long msr;
3505 unsigned int target_c_local;
3508 /* tcc_activation_temp is used only for dts or ptm */
3509 if (!(do_dts || do_ptm))
3512 /* this is a per-package concept */
3513 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3517 if (cpu_migrate(cpu)) {
3518 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3522 if (tcc_activation_temp_override != 0) {
3523 tcc_activation_temp = tcc_activation_temp_override;
3524 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3525 cpu, tcc_activation_temp);
3529 /* Temperature Target MSR is Nehalem and newer only */
3530 if (!do_nhm_platform_info)
3533 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3536 target_c_local = (msr >> 16) & 0xFF;
3539 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3540 cpu, msr, target_c_local);
3542 if (!target_c_local)
3545 tcc_activation_temp = target_c_local;
3550 tcc_activation_temp = TJMAX_DEFAULT;
3551 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3552 cpu, tcc_activation_temp);
3557 void decode_feature_control_msr(void)
3559 unsigned long long msr;
3561 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3562 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3564 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3565 msr & (1 << 18) ? "SGX" : "");
3568 void decode_misc_enable_msr(void)
3570 unsigned long long msr;
3572 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
3573 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
3575 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3576 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3577 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3578 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3579 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
3583 * Decode MSR_MISC_PWR_MGMT
3585 * Decode the bits according to the Nehalem documentation
3586 * bit[0] seems to continue to have same meaning going forward
3589 void decode_misc_pwr_mgmt_msr(void)
3591 unsigned long long msr;
3593 if (!do_nhm_platform_info)
3596 if (no_MSR_MISC_PWR_MGMT)
3599 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
3600 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
3602 msr & (1 << 0) ? "DIS" : "EN",
3603 msr & (1 << 1) ? "EN" : "DIS",
3604 msr & (1 << 8) ? "EN" : "DIS");
3607 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3609 * This MSRs are present on Silvermont processors,
3610 * Intel Atom processor E3000 series (Baytrail), and friends.
3612 void decode_c6_demotion_policy_msr(void)
3614 unsigned long long msr;
3616 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3617 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3618 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3620 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3621 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3622 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3625 void process_cpuid()
3627 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
3628 unsigned int fms, family, model, stepping;
3630 eax = ebx = ecx = edx = 0;
3632 __cpuid(0, max_level, ebx, ecx, edx);
3634 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3638 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
3639 (char *)&ebx, (char *)&edx, (char *)&ecx);
3641 __cpuid(1, fms, ebx, ecx, edx);
3642 family = (fms >> 8) & 0xf;
3643 model = (fms >> 4) & 0xf;
3644 stepping = fms & 0xf;
3645 if (family == 6 || family == 0xf)
3646 model += ((fms >> 16) & 0xf) << 4;
3649 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
3650 max_level, family, model, stepping, family, model, stepping);
3651 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
3652 ecx & (1 << 0) ? "SSE3" : "-",
3653 ecx & (1 << 3) ? "MONITOR" : "-",
3654 ecx & (1 << 6) ? "SMX" : "-",
3655 ecx & (1 << 7) ? "EIST" : "-",
3656 ecx & (1 << 8) ? "TM2" : "-",
3657 edx & (1 << 4) ? "TSC" : "-",
3658 edx & (1 << 5) ? "MSR" : "-",
3659 edx & (1 << 22) ? "ACPI-TM" : "-",
3660 edx & (1 << 29) ? "TM" : "-");
3663 if (!(edx & (1 << 5)))
3664 errx(1, "CPUID: no MSR");
3667 * check max extended function levels of CPUID.
3668 * This is needed to check for invariant TSC.
3669 * This check is valid for both Intel and AMD.
3671 ebx = ecx = edx = 0;
3672 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
3674 if (max_extended_level >= 0x80000007) {
3677 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3678 * this check is valid for both Intel and AMD
3680 __cpuid(0x80000007, eax, ebx, ecx, edx);
3681 has_invariant_tsc = edx & (1 << 8);
3685 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3686 * this check is valid for both Intel and AMD
3689 __cpuid(0x6, eax, ebx, ecx, edx);
3690 has_aperf = ecx & (1 << 0);
3692 BIC_PRESENT(BIC_Avg_MHz);
3693 BIC_PRESENT(BIC_Busy);
3694 BIC_PRESENT(BIC_Bzy_MHz);
3696 do_dts = eax & (1 << 0);
3698 BIC_PRESENT(BIC_CoreTmp);
3699 do_ptm = eax & (1 << 6);
3701 BIC_PRESENT(BIC_PkgTmp);
3702 has_hwp = eax & (1 << 7);
3703 has_hwp_notify = eax & (1 << 8);
3704 has_hwp_activity_window = eax & (1 << 9);
3705 has_hwp_epp = eax & (1 << 10);
3706 has_hwp_pkg = eax & (1 << 11);
3707 has_epb = ecx & (1 << 3);
3710 fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
3711 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3712 has_aperf ? "" : "No-",
3713 do_dts ? "" : "No-",
3714 do_ptm ? "" : "No-",
3715 has_hwp ? "" : "No-",
3716 has_hwp_notify ? "" : "No-",
3717 has_hwp_activity_window ? "" : "No-",
3718 has_hwp_epp ? "" : "No-",
3719 has_hwp_pkg ? "" : "No-",
3720 has_epb ? "" : "No-");
3723 decode_misc_enable_msr();
3725 if (max_level >= 0x7 && debug) {
3730 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3732 has_sgx = ebx & (1 << 2);
3733 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3736 decode_feature_control_msr();
3739 if (max_level >= 0x15) {
3740 unsigned int eax_crystal;
3741 unsigned int ebx_tsc;
3744 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3746 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
3747 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
3751 if (debug && (ebx != 0))
3752 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
3753 eax_crystal, ebx_tsc, crystal_hz);
3755 if (crystal_hz == 0)
3757 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3758 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3759 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3760 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3761 crystal_hz = 24000000; /* 24.0 MHz */
3763 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3764 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
3765 crystal_hz = 25000000; /* 25.0 MHz */
3767 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3768 crystal_hz = 19200000; /* 19.2 MHz */
3775 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
3777 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
3778 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3782 if (max_level >= 0x16) {
3783 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3786 * CPUID 16H Base MHz, Max MHz, Bus MHz
3788 base_mhz = max_mhz = bus_mhz = edx = 0;
3790 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
3792 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
3793 base_mhz, max_mhz, bus_mhz);
3797 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3799 BIC_PRESENT(BIC_IRQ);
3800 BIC_PRESENT(BIC_TSC_MHz);
3802 if (probe_nhm_msrs(family, model)) {
3803 do_nhm_platform_info = 1;
3804 BIC_PRESENT(BIC_CPU_c1);
3805 BIC_PRESENT(BIC_CPU_c3);
3806 BIC_PRESENT(BIC_CPU_c6);
3807 BIC_PRESENT(BIC_SMI);
3809 do_snb_cstates = has_snb_msrs(family, model);
3812 BIC_PRESENT(BIC_CPU_c7);
3814 do_irtl_snb = has_snb_msrs(family, model);
3815 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3816 do_pc3 = (pkg_cstate_limit >= PCL__3);
3817 do_pc6 = (pkg_cstate_limit >= PCL__6);
3818 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
3819 if (has_slv_msrs(family, model)) {
3820 do_pc2 = do_pc3 = do_pc7 = 0;
3822 BIC_PRESENT(BIC_Mod_c6);
3823 use_c1_residency_msr = 1;
3825 do_c8_c9_c10 = has_hsw_msrs(family, model);
3826 do_irtl_hsw = has_hsw_msrs(family, model);
3827 do_skl_residency = has_skl_msrs(family, model);
3828 do_slm_cstates = is_slm(family, model);
3829 do_knl_cstates = is_knl(family, model);
3832 decode_misc_pwr_mgmt_msr();
3834 if (debug && has_slv_msrs(family, model))
3835 decode_c6_demotion_policy_msr();
3837 rapl_probe(family, model);
3838 perf_limit_reasons_probe(family, model);
3841 dump_cstate_pstate_config_info(family, model);
3843 if (has_skl_msrs(family, model))
3844 calculate_tsc_tweak();
3846 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
3847 BIC_PRESENT(BIC_GFX_rc6);
3849 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
3850 BIC_PRESENT(BIC_GFXMHz);
3858 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3860 "Turbostat forks the specified COMMAND and prints statistics\n"
3861 "when COMMAND completes.\n"
3862 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3863 "to print statistics, until interrupted.\n"
3864 "--add add a counter\n"
3865 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
3866 "--debug run in \"debug\" mode\n"
3867 "--interval sec Override default 5-second measurement interval\n"
3868 "--help print this help message\n"
3869 "--out file create or truncate \"file\" for all output\n"
3870 "--version print version information\n"
3872 "For more help, run \"man turbostat\"\n");
3877 * in /dev/cpu/ return success for names that are numbers
3878 * ie. filter out ".", "..", "microcode".
3880 int dir_filter(const struct dirent *dirp)
3882 if (isdigit(dirp->d_name[0]))
3888 int open_dev_cpu_msr(int dummy1)
3893 void topology_probe()
3896 int max_core_id = 0;
3897 int max_package_id = 0;
3898 int max_siblings = 0;
3899 struct cpu_topology {
3901 int physical_package_id;
3904 /* Initialize num_cpus, max_cpu_num */
3906 topo.max_cpu_num = 0;
3907 for_all_proc_cpus(count_cpus);
3908 if (!summary_only && topo.num_cpus > 1)
3909 BIC_PRESENT(BIC_CPU);
3912 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
3914 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
3916 err(1, "calloc cpus");
3919 * Allocate and initialize cpu_present_set
3921 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
3922 if (cpu_present_set == NULL)
3923 err(3, "CPU_ALLOC");
3924 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3925 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3926 for_all_proc_cpus(mark_cpu_present);
3929 * Allocate and initialize cpu_affinity_set
3931 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
3932 if (cpu_affinity_set == NULL)
3933 err(3, "CPU_ALLOC");
3934 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3935 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3940 * find max_core_id, max_package_id
3942 for (i = 0; i <= topo.max_cpu_num; ++i) {
3945 if (cpu_is_not_present(i)) {
3947 fprintf(outf, "cpu%d NOT PRESENT\n", i);
3950 cpus[i].core_id = get_core_id(i);
3951 if (cpus[i].core_id > max_core_id)
3952 max_core_id = cpus[i].core_id;
3954 cpus[i].physical_package_id = get_physical_package_id(i);
3955 if (cpus[i].physical_package_id > max_package_id)
3956 max_package_id = cpus[i].physical_package_id;
3958 siblings = get_num_ht_siblings(i);
3959 if (siblings > max_siblings)
3960 max_siblings = siblings;
3962 fprintf(outf, "cpu %d pkg %d core %d\n",
3963 i, cpus[i].physical_package_id, cpus[i].core_id);
3965 topo.num_cores_per_pkg = max_core_id + 1;
3967 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
3968 max_core_id, topo.num_cores_per_pkg);
3969 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
3970 BIC_PRESENT(BIC_Core);
3972 topo.num_packages = max_package_id + 1;
3974 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
3975 max_package_id, topo.num_packages);
3976 if (debug && !summary_only && topo.num_packages > 1)
3977 BIC_PRESENT(BIC_Package);
3979 topo.num_threads_per_core = max_siblings;
3981 fprintf(outf, "max_siblings %d\n", max_siblings);
3987 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3991 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3992 topo.num_packages, sizeof(struct thread_data));
3996 for (i = 0; i < topo.num_threads_per_core *
3997 topo.num_cores_per_pkg * topo.num_packages; i++)
3998 (*t)[i].cpu_id = -1;
4000 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
4001 sizeof(struct core_data));
4005 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4006 (*c)[i].core_id = -1;
4008 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
4012 for (i = 0; i < topo.num_packages; i++)
4013 (*p)[i].package_id = i;
4017 err(1, "calloc counters");
4022 * set cpu_id, core_num, pkg_num
4023 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4025 * increment topo.num_cores when 1st core in pkg seen
4027 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4028 struct pkg_data *pkg_base, int thread_num, int core_num,
4029 int pkg_num, int cpu_id)
4031 struct thread_data *t;
4032 struct core_data *c;
4035 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4036 c = GET_CORE(core_base, core_num, pkg_num);
4037 p = GET_PKG(pkg_base, pkg_num);
4040 if (thread_num == 0) {
4041 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4042 if (cpu_is_first_core_in_package(cpu_id))
4043 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4046 c->core_id = core_num;
4047 p->package_id = pkg_num;
4051 int initialize_counters(int cpu_id)
4053 int my_thread_id, my_core_id, my_package_id;
4055 my_package_id = get_physical_package_id(cpu_id);
4056 my_core_id = get_core_id(cpu_id);
4057 my_thread_id = get_cpu_position_in_core(cpu_id);
4061 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4062 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4066 void allocate_output_buffer()
4068 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4069 outp = output_buffer;
4071 err(-1, "calloc output buffer");
4073 void allocate_fd_percpu(void)
4075 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4076 if (fd_percpu == NULL)
4077 err(-1, "calloc fd_percpu");
4079 void allocate_irq_buffers(void)
4081 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4082 if (irq_column_2_cpu == NULL)
4083 err(-1, "calloc %d", topo.num_cpus);
4085 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4086 if (irqs_per_cpu == NULL)
4087 err(-1, "calloc %d", topo.max_cpu_num + 1);
4089 void setup_all_buffers(void)
4092 allocate_irq_buffers();
4093 allocate_fd_percpu();
4094 allocate_counters(&thread_even, &core_even, &package_even);
4095 allocate_counters(&thread_odd, &core_odd, &package_odd);
4096 allocate_output_buffer();
4097 for_all_proc_cpus(initialize_counters);
4100 void set_base_cpu(void)
4102 base_cpu = sched_getcpu();
4104 err(-ENODEV, "No valid cpus found");
4107 fprintf(outf, "base_cpu = %d\n", base_cpu);
4110 void turbostat_init()
4112 setup_all_buffers();
4115 check_permissions();
4120 for_all_cpus(print_hwp, ODD_COUNTERS);
4123 for_all_cpus(print_epb, ODD_COUNTERS);
4126 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4129 for_all_cpus(print_rapl, ODD_COUNTERS);
4131 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4134 for_all_cpus(print_thermal, ODD_COUNTERS);
4136 if (debug && do_irtl_snb)
4140 int fork_it(char **argv)
4145 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4148 /* clear affinity side-effect of get_counters() */
4149 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
4150 gettimeofday(&tv_even, (struct timezone *)NULL);
4155 execvp(argv[0], argv);
4159 if (child_pid == -1)
4162 signal(SIGINT, SIG_IGN);
4163 signal(SIGQUIT, SIG_IGN);
4164 if (waitpid(child_pid, &status, 0) == -1)
4165 err(status, "waitpid");
4168 * n.b. fork_it() does not check for errors from for_all_cpus()
4169 * because re-starting is problematic when forking
4171 for_all_cpus(get_counters, ODD_COUNTERS);
4172 gettimeofday(&tv_odd, (struct timezone *)NULL);
4173 timersub(&tv_odd, &tv_even, &tv_delta);
4174 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4175 fprintf(outf, "%s: Counter reset detected\n", progname);
4177 compute_average(EVEN_COUNTERS);
4178 format_all_counters(EVEN_COUNTERS);
4181 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4183 flush_output_stderr();
4188 int get_and_dump_counters(void)
4192 status = for_all_cpus(get_counters, ODD_COUNTERS);
4196 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4200 flush_output_stdout();
4205 void print_version() {
4206 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
4207 " - Len Brown <lenb@kernel.org>\n");
4210 int add_counter(unsigned int msr_num, char *name, unsigned int width,
4211 enum counter_scope scope, enum counter_type type,
4212 enum counter_format format)
4214 struct msr_counter *msrp;
4216 msrp = calloc(1, sizeof(struct msr_counter));
4222 msrp->msr_num = msr_num;
4223 strncpy(msrp->name, name, NAME_BYTES);
4224 msrp->width = width;
4226 msrp->format = format;
4231 msrp->next = sys.tp;
4233 sys.added_thread_counters++;
4234 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4235 fprintf(stderr, "exceeded max %d added thread counters\n",
4236 MAX_ADDED_COUNTERS);
4242 msrp->next = sys.cp;
4244 sys.added_core_counters++;
4245 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4246 fprintf(stderr, "exceeded max %d added core counters\n",
4247 MAX_ADDED_COUNTERS);
4253 msrp->next = sys.pp;
4255 sys.added_package_counters++;
4256 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4257 fprintf(stderr, "exceeded max %d added package counters\n",
4258 MAX_ADDED_COUNTERS);
4267 void parse_add_command(char *add_command)
4270 char name_buffer[NAME_BYTES];
4273 enum counter_scope scope = SCOPE_CPU;
4274 enum counter_type type = COUNTER_CYCLES;
4275 enum counter_format format = FORMAT_DELTA;
4277 while (add_command) {
4279 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4282 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4285 if (sscanf(add_command, "u%d", &width) == 1) {
4286 if ((width == 32) || (width == 64))
4290 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4294 if (!strncmp(add_command, "core", strlen("core"))) {
4298 if (!strncmp(add_command, "package", strlen("package"))) {
4299 scope = SCOPE_PACKAGE;
4302 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4303 type = COUNTER_CYCLES;
4306 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4307 type = COUNTER_SECONDS;
4310 if (!strncmp(add_command, "raw", strlen("raw"))) {
4311 format = FORMAT_RAW;
4314 if (!strncmp(add_command, "delta", strlen("delta"))) {
4315 format = FORMAT_DELTA;
4318 if (!strncmp(add_command, "percent", strlen("percent"))) {
4319 format = FORMAT_PERCENT;
4323 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4326 eos = strchr(name_buffer, ',');
4333 add_command = strchr(add_command, ',');
4339 fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4343 /* generate default column header */
4344 if (*name_buffer == '\0') {
4345 if (format == FORMAT_RAW) {
4347 sprintf(name_buffer, "msr%d", msr_num);
4349 sprintf(name_buffer, "MSR%d", msr_num);
4350 } else if (format == FORMAT_DELTA) {
4352 sprintf(name_buffer, "cnt%d", msr_num);
4354 sprintf(name_buffer, "CNT%d", msr_num);
4355 } else if (format == FORMAT_PERCENT) {
4357 sprintf(name_buffer, "msr%d%%", msr_num);
4359 sprintf(name_buffer, "MSR%d%%", msr_num);
4363 if (add_counter(msr_num, name_buffer, width, scope, type, format))
4372 * HIDE_LIST - hide this list of counters, show the rest [default]
4373 * SHOW_LIST - show this list of counters, hide the rest
4375 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4379 * parse_show_hide() - process cmdline to set default counter action
4381 void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4384 * --show: show only those specified
4385 * The 1st invocation will clear and replace the enabled mask
4386 * subsequent invocations can add to it.
4388 if (new_mode == SHOW_LIST) {
4390 bic_enabled = bic_lookup(optarg);
4392 bic_enabled |= bic_lookup(optarg);
4399 * --hide: do not show those specified
4400 * multiple invocations simply clear more bits in enabled mask
4402 bic_enabled &= ~bic_lookup(optarg);
4405 void cmdline(int argc, char **argv)
4408 int option_index = 0;
4409 static struct option long_options[] = {
4410 {"add", required_argument, 0, 'a'},
4411 {"Dump", no_argument, 0, 'D'},
4412 {"debug", no_argument, 0, 'd'},
4413 {"interval", required_argument, 0, 'i'},
4414 {"help", no_argument, 0, 'h'},
4415 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
4416 {"Joules", no_argument, 0, 'J'},
4417 {"out", required_argument, 0, 'o'},
4418 {"Package", no_argument, 0, 'p'},
4419 {"processor", no_argument, 0, 'p'},
4420 {"show", required_argument, 0, 's'},
4421 {"Summary", no_argument, 0, 'S'},
4422 {"TCC", required_argument, 0, 'T'},
4423 {"version", no_argument, 0, 'v' },
4429 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
4430 long_options, &option_index)) != -1) {
4433 parse_add_command(optarg);
4442 parse_show_hide(optarg, HIDE_LIST);
4450 double interval = strtod(optarg, NULL);
4452 if (interval < 0.001) {
4453 fprintf(outf, "interval %f seconds is too small\n",
4458 interval_ts.tv_sec = interval;
4459 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4466 outf = fopen_or_die(optarg, "w");
4475 parse_show_hide(optarg, SHOW_LIST);
4481 tcc_activation_temp_override = atoi(optarg);
4491 int main(int argc, char **argv)
4495 cmdline(argc, argv);
4502 /* dump counters and exit */
4504 return get_and_dump_counters();
4507 * if any params left, it must be a command to fork
4510 return fork_it(argv + optind);