2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel and AMD 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/select.h>
33 #include <sys/resource.h>
45 #include <linux/capability.h>
48 char *proc_stat = "/proc/stat";
51 struct timeval interval_tv = {5, 0};
52 struct timespec interval_ts = {5, 0};
53 struct timespec one_msec = {0, 1000000};
54 unsigned int num_iterations;
58 unsigned int sums_need_wide_columns;
59 unsigned int rapl_joules;
60 unsigned int summary_only;
61 unsigned int list_header_only;
62 unsigned int dump_only;
63 unsigned int do_snb_cstates;
64 unsigned int do_knl_cstates;
65 unsigned int do_slm_cstates;
66 unsigned int do_cnl_cstates;
67 unsigned int use_c1_residency_msr;
68 unsigned int has_aperf;
70 unsigned int do_irtl_snb;
71 unsigned int do_irtl_hsw;
72 unsigned int units = 1000000; /* MHz etc */
73 unsigned int genuine_intel;
74 unsigned int authentic_amd;
75 unsigned int max_level, max_extended_level;
76 unsigned int has_invariant_tsc;
77 unsigned int do_nhm_platform_info;
78 unsigned int no_MSR_MISC_PWR_MGMT;
79 unsigned int aperf_mperf_multiplier = 1;
82 unsigned int has_base_hz;
83 double tsc_tweak = 1.0;
84 unsigned int show_pkg_only;
85 unsigned int show_core_only;
86 char *output_buffer, *outp;
90 unsigned long long gfx_cur_rc6_ms;
91 unsigned long long cpuidle_cur_cpu_lpi_us;
92 unsigned long long cpuidle_cur_sys_lpi_us;
93 unsigned int gfx_cur_mhz;
94 unsigned int tcc_activation_temp;
95 unsigned int tcc_activation_temp_override;
96 double rapl_power_units, rapl_time_units;
97 double rapl_dram_energy_units, rapl_energy_units;
98 double rapl_joule_counter_range;
99 unsigned int do_core_perf_limit_reasons;
100 unsigned int has_automatic_cstate_conversion;
101 unsigned int do_gfx_perf_limit_reasons;
102 unsigned int do_ring_perf_limit_reasons;
103 unsigned int crystal_hz;
104 unsigned long long tsc_hz;
106 double discover_bclk(unsigned int family, unsigned int model);
107 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
108 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
109 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
110 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
111 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
112 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
113 unsigned int has_misc_feature_control;
114 unsigned int first_counter_read = 1;
116 #define RAPL_PKG (1 << 0)
117 /* 0x610 MSR_PKG_POWER_LIMIT */
118 /* 0x611 MSR_PKG_ENERGY_STATUS */
119 #define RAPL_PKG_PERF_STATUS (1 << 1)
120 /* 0x613 MSR_PKG_PERF_STATUS */
121 #define RAPL_PKG_POWER_INFO (1 << 2)
122 /* 0x614 MSR_PKG_POWER_INFO */
124 #define RAPL_DRAM (1 << 3)
125 /* 0x618 MSR_DRAM_POWER_LIMIT */
126 /* 0x619 MSR_DRAM_ENERGY_STATUS */
127 #define RAPL_DRAM_PERF_STATUS (1 << 4)
128 /* 0x61b MSR_DRAM_PERF_STATUS */
129 #define RAPL_DRAM_POWER_INFO (1 << 5)
130 /* 0x61c MSR_DRAM_POWER_INFO */
132 #define RAPL_CORES_POWER_LIMIT (1 << 6)
133 /* 0x638 MSR_PP0_POWER_LIMIT */
134 #define RAPL_CORE_POLICY (1 << 7)
135 /* 0x63a MSR_PP0_POLICY */
137 #define RAPL_GFX (1 << 8)
138 /* 0x640 MSR_PP1_POWER_LIMIT */
139 /* 0x641 MSR_PP1_ENERGY_STATUS */
140 /* 0x642 MSR_PP1_POLICY */
142 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
143 /* 0x639 MSR_PP0_ENERGY_STATUS */
144 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
145 #define TJMAX_DEFAULT 100
147 #define MAX(a, b) ((a) > (b) ? (a) : (b))
150 * buffer size used by sscanf() for added column names
151 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
153 #define NAME_BYTES 20
154 #define PATH_BYTES 128
159 #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
160 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
161 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
162 #define MAX_ADDED_COUNTERS 8
163 #define MAX_ADDED_THREAD_COUNTERS 24
164 #define BITMASK_SIZE 32
167 struct timeval tv_begin;
168 struct timeval tv_end;
169 unsigned long long tsc;
170 unsigned long long aperf;
171 unsigned long long mperf;
172 unsigned long long c1;
173 unsigned long long irq_count;
174 unsigned int smi_count;
176 unsigned int apic_id;
177 unsigned int x2apic_id;
179 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
180 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
181 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
182 } *thread_even, *thread_odd;
185 unsigned long long c3;
186 unsigned long long c6;
187 unsigned long long c7;
188 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
189 unsigned int core_temp_c;
190 unsigned int core_id;
191 unsigned long long counter[MAX_ADDED_COUNTERS];
192 } *core_even, *core_odd;
195 unsigned long long pc2;
196 unsigned long long pc3;
197 unsigned long long pc6;
198 unsigned long long pc7;
199 unsigned long long pc8;
200 unsigned long long pc9;
201 unsigned long long pc10;
202 unsigned long long cpu_lpi;
203 unsigned long long sys_lpi;
204 unsigned long long pkg_wtd_core_c0;
205 unsigned long long pkg_any_core_c0;
206 unsigned long long pkg_any_gfxe_c0;
207 unsigned long long pkg_both_core_gfxe_c0;
208 long long gfx_rc6_ms;
209 unsigned int gfx_mhz;
210 unsigned int package_id;
211 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
212 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
213 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
214 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
215 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
216 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
217 unsigned int pkg_temp_c;
218 unsigned long long counter[MAX_ADDED_COUNTERS];
219 } *package_even, *package_odd;
221 #define ODD_COUNTERS thread_odd, core_odd, package_odd
222 #define EVEN_COUNTERS thread_even, core_even, package_even
224 #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
227 topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
228 ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
229 ((core_no) * topo.threads_per_core) + \
232 #define GET_CORE(core_base, core_no, node_no, pkg_no) \
234 ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
235 ((node_no) * topo.cores_per_node) + \
239 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
241 enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
242 enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
243 enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
246 unsigned int msr_num;
247 char name[NAME_BYTES];
248 char path[PATH_BYTES];
250 enum counter_type type;
251 enum counter_format format;
252 struct msr_counter *next;
254 #define FLAGS_HIDE (1 << 0)
255 #define FLAGS_SHOW (1 << 1)
256 #define SYSFS_PERCPU (1 << 1)
259 struct sys_counters {
260 unsigned int added_thread_counters;
261 unsigned int added_core_counters;
262 unsigned int added_package_counters;
263 struct msr_counter *tp;
264 struct msr_counter *cp;
265 struct msr_counter *pp;
268 struct system_summary {
269 struct thread_data threads;
270 struct core_data cores;
271 struct pkg_data packages;
274 struct cpu_topology {
275 int physical_package_id;
277 int physical_node_id;
278 int logical_node_id; /* 0-based count within the package */
279 int physical_core_id;
281 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
292 int threads_per_core;
295 struct timeval tv_even, tv_odd, tv_delta;
297 int *irq_column_2_cpu; /* /proc/interrupts column numbers */
298 int *irqs_per_cpu; /* indexed by cpu_num */
300 void setup_all_buffers(void);
303 char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
304 char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
306 int cpu_is_not_present(int cpu)
308 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
311 * run func(thread, core, package) in topology order
312 * skip non-present cpus
315 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
316 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
318 int retval, pkg_no, core_no, thread_no, node_no;
320 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
321 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
322 for (node_no = 0; node_no < topo.nodes_per_pkg;
324 for (thread_no = 0; thread_no <
325 topo.threads_per_core; ++thread_no) {
326 struct thread_data *t;
330 t = GET_THREAD(thread_base, thread_no,
334 if (cpu_is_not_present(t->cpu_id))
337 c = GET_CORE(core_base, core_no,
339 p = GET_PKG(pkg_base, pkg_no);
341 retval = func(t, c, p);
351 int cpu_migrate(int cpu)
353 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
354 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
355 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
360 int get_msr_fd(int cpu)
370 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
371 fd = open(pathname, O_RDONLY);
373 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
380 int get_msr(int cpu, off_t offset, unsigned long long *msr)
384 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
386 if (retval != sizeof *msr)
387 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
393 * This list matches the column headers, except
394 * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
395 * 2. Core and CPU are moved to the end, we can't have strings that contain them
396 * matching on them for --show and --hide.
398 struct msr_counter bic[] = {
400 { 0x0, "Time_Of_Day_Seconds" },
408 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
451 #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
452 #define BIC_USEC (1ULL << 0)
453 #define BIC_TOD (1ULL << 1)
454 #define BIC_Package (1ULL << 2)
455 #define BIC_Node (1ULL << 3)
456 #define BIC_Avg_MHz (1ULL << 4)
457 #define BIC_Busy (1ULL << 5)
458 #define BIC_Bzy_MHz (1ULL << 6)
459 #define BIC_TSC_MHz (1ULL << 7)
460 #define BIC_IRQ (1ULL << 8)
461 #define BIC_SMI (1ULL << 9)
462 #define BIC_sysfs (1ULL << 10)
463 #define BIC_CPU_c1 (1ULL << 11)
464 #define BIC_CPU_c3 (1ULL << 12)
465 #define BIC_CPU_c6 (1ULL << 13)
466 #define BIC_CPU_c7 (1ULL << 14)
467 #define BIC_ThreadC (1ULL << 15)
468 #define BIC_CoreTmp (1ULL << 16)
469 #define BIC_CoreCnt (1ULL << 17)
470 #define BIC_PkgTmp (1ULL << 18)
471 #define BIC_GFX_rc6 (1ULL << 19)
472 #define BIC_GFXMHz (1ULL << 20)
473 #define BIC_Pkgpc2 (1ULL << 21)
474 #define BIC_Pkgpc3 (1ULL << 22)
475 #define BIC_Pkgpc6 (1ULL << 23)
476 #define BIC_Pkgpc7 (1ULL << 24)
477 #define BIC_Pkgpc8 (1ULL << 25)
478 #define BIC_Pkgpc9 (1ULL << 26)
479 #define BIC_Pkgpc10 (1ULL << 27)
480 #define BIC_CPU_LPI (1ULL << 28)
481 #define BIC_SYS_LPI (1ULL << 29)
482 #define BIC_PkgWatt (1ULL << 30)
483 #define BIC_CorWatt (1ULL << 31)
484 #define BIC_GFXWatt (1ULL << 32)
485 #define BIC_PkgCnt (1ULL << 33)
486 #define BIC_RAMWatt (1ULL << 34)
487 #define BIC_PKG__ (1ULL << 35)
488 #define BIC_RAM__ (1ULL << 36)
489 #define BIC_Pkg_J (1ULL << 37)
490 #define BIC_Cor_J (1ULL << 38)
491 #define BIC_GFX_J (1ULL << 39)
492 #define BIC_RAM_J (1ULL << 40)
493 #define BIC_Mod_c6 (1ULL << 41)
494 #define BIC_Totl_c0 (1ULL << 42)
495 #define BIC_Any_c0 (1ULL << 43)
496 #define BIC_GFX_c0 (1ULL << 44)
497 #define BIC_CPUGFX (1ULL << 45)
498 #define BIC_Core (1ULL << 46)
499 #define BIC_CPU (1ULL << 47)
500 #define BIC_APIC (1ULL << 48)
501 #define BIC_X2APIC (1ULL << 49)
503 #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
505 unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
506 unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
508 #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
509 #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
510 #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
511 #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
514 #define MAX_DEFERRED 16
515 char *deferred_skip_names[MAX_DEFERRED];
516 int deferred_skip_index;
519 * HIDE_LIST - hide this list of counters, show the rest [default]
520 * SHOW_LIST - show this list of counters, hide the rest
522 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
527 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
529 "Turbostat forks the specified COMMAND and prints statistics\n"
530 "when COMMAND completes.\n"
531 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
532 "to print statistics, until interrupted.\n"
533 " -a, --add add a counter\n"
534 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
535 " -c, --cpu cpu-set limit output to summary plus cpu-set:\n"
536 " {core | package | j,k,l..m,n-p }\n"
537 " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n"
538 " -D, --Dump displays the raw counter values\n"
539 " -e, --enable [all | column]\n"
540 " shows all or the specified disabled column\n"
541 " -H, --hide [column|column,column,...]\n"
542 " hide the specified column(s)\n"
543 " -i, --interval sec.subsec\n"
544 " Override default 5-second measurement interval\n"
545 " -J, --Joules displays energy in Joules instead of Watts\n"
546 " -l, --list list column headers only\n"
547 " -n, --num_iterations num\n"
548 " number of the measurement iterations\n"
550 " create or truncate \"file\" for all output\n"
551 " -q, --quiet skip decoding system configuration header\n"
552 " -s, --show [column|column,column,...]\n"
553 " show only the specified column(s)\n"
555 " limits output to 1-line system summary per interval\n"
556 " -T, --TCC temperature\n"
557 " sets the Thermal Control Circuit temperature in\n"
559 " -h, --help print this help message\n"
560 " -v, --version print version information\n"
562 "For more help, run \"man turbostat\"\n");
567 * for all the strings in comma separate name_list,
568 * set the approprate bit in return value.
570 unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
573 unsigned long long retval = 0;
578 comma = strchr(name_list, ',');
583 if (!strcmp(name_list, "all"))
586 for (i = 0; i < MAX_BIC; ++i) {
587 if (!strcmp(name_list, bic[i].name)) {
588 retval |= (1ULL << i);
593 if (mode == SHOW_LIST) {
594 fprintf(stderr, "Invalid counter name: %s\n", name_list);
597 deferred_skip_names[deferred_skip_index++] = name_list;
599 fprintf(stderr, "deferred \"%s\"\n", name_list);
600 if (deferred_skip_index >= MAX_DEFERRED) {
601 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
602 MAX_DEFERRED, name_list);
617 void print_header(char *delim)
619 struct msr_counter *mp;
622 if (DO_BIC(BIC_USEC))
623 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
625 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
626 if (DO_BIC(BIC_Package))
627 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
628 if (DO_BIC(BIC_Node))
629 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
630 if (DO_BIC(BIC_Core))
631 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
633 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
634 if (DO_BIC(BIC_APIC))
635 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
636 if (DO_BIC(BIC_X2APIC))
637 outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
638 if (DO_BIC(BIC_Avg_MHz))
639 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
640 if (DO_BIC(BIC_Busy))
641 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
642 if (DO_BIC(BIC_Bzy_MHz))
643 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
644 if (DO_BIC(BIC_TSC_MHz))
645 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
647 if (DO_BIC(BIC_IRQ)) {
648 if (sums_need_wide_columns)
649 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
651 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
655 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
657 for (mp = sys.tp; mp; mp = mp->next) {
659 if (mp->format == FORMAT_RAW) {
661 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
663 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
665 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
666 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
668 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
672 if (DO_BIC(BIC_CPU_c1))
673 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
674 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
675 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
676 if (DO_BIC(BIC_CPU_c6))
677 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
678 if (DO_BIC(BIC_CPU_c7))
679 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
681 if (DO_BIC(BIC_Mod_c6))
682 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
684 if (DO_BIC(BIC_CoreTmp))
685 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
687 for (mp = sys.cp; mp; mp = mp->next) {
688 if (mp->format == FORMAT_RAW) {
690 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
692 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
694 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
695 outp += sprintf(outp, "%s%8s", delim, mp->name);
697 outp += sprintf(outp, "%s%s", delim, mp->name);
701 if (DO_BIC(BIC_PkgTmp))
702 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
704 if (DO_BIC(BIC_GFX_rc6))
705 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
707 if (DO_BIC(BIC_GFXMHz))
708 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
710 if (DO_BIC(BIC_Totl_c0))
711 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
712 if (DO_BIC(BIC_Any_c0))
713 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
714 if (DO_BIC(BIC_GFX_c0))
715 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
716 if (DO_BIC(BIC_CPUGFX))
717 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
719 if (DO_BIC(BIC_Pkgpc2))
720 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
721 if (DO_BIC(BIC_Pkgpc3))
722 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
723 if (DO_BIC(BIC_Pkgpc6))
724 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
725 if (DO_BIC(BIC_Pkgpc7))
726 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
727 if (DO_BIC(BIC_Pkgpc8))
728 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
729 if (DO_BIC(BIC_Pkgpc9))
730 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
731 if (DO_BIC(BIC_Pkgpc10))
732 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
733 if (DO_BIC(BIC_CPU_LPI))
734 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
735 if (DO_BIC(BIC_SYS_LPI))
736 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
738 if (do_rapl && !rapl_joules) {
739 if (DO_BIC(BIC_PkgWatt))
740 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
741 if (DO_BIC(BIC_CorWatt))
742 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
743 if (DO_BIC(BIC_GFXWatt))
744 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
745 if (DO_BIC(BIC_RAMWatt))
746 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
747 if (DO_BIC(BIC_PKG__))
748 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
749 if (DO_BIC(BIC_RAM__))
750 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
751 } else if (do_rapl && rapl_joules) {
752 if (DO_BIC(BIC_Pkg_J))
753 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
754 if (DO_BIC(BIC_Cor_J))
755 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
756 if (DO_BIC(BIC_GFX_J))
757 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
758 if (DO_BIC(BIC_RAM_J))
759 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
760 if (DO_BIC(BIC_PKG__))
761 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
762 if (DO_BIC(BIC_RAM__))
763 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
765 for (mp = sys.pp; mp; mp = mp->next) {
766 if (mp->format == FORMAT_RAW) {
768 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
770 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
772 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
773 outp += sprintf(outp, "%s%8s", delim, mp->name);
775 outp += sprintf(outp, "%s%s", delim, mp->name);
779 outp += sprintf(outp, "\n");
782 int dump_counters(struct thread_data *t, struct core_data *c,
786 struct msr_counter *mp;
788 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
791 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
792 t->cpu_id, t->flags);
793 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
794 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
795 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
796 outp += sprintf(outp, "c1: %016llX\n", t->c1);
799 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
801 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
803 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
804 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
805 i, mp->msr_num, t->counter[i]);
810 outp += sprintf(outp, "core: %d\n", c->core_id);
811 outp += sprintf(outp, "c3: %016llX\n", c->c3);
812 outp += sprintf(outp, "c6: %016llX\n", c->c6);
813 outp += sprintf(outp, "c7: %016llX\n", c->c7);
814 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
816 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
817 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
818 i, mp->msr_num, c->counter[i]);
820 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
824 outp += sprintf(outp, "package: %d\n", p->package_id);
826 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
827 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
828 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
829 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
831 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
832 if (DO_BIC(BIC_Pkgpc3))
833 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
834 if (DO_BIC(BIC_Pkgpc6))
835 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
836 if (DO_BIC(BIC_Pkgpc7))
837 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
838 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
839 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
840 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
841 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
842 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
843 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
844 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
845 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
846 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
847 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
848 outp += sprintf(outp, "Throttle PKG: %0X\n",
849 p->rapl_pkg_perf_status);
850 outp += sprintf(outp, "Throttle RAM: %0X\n",
851 p->rapl_dram_perf_status);
852 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
854 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
855 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
856 i, mp->msr_num, p->counter[i]);
860 outp += sprintf(outp, "\n");
866 * column formatting convention & formats
868 int format_counters(struct thread_data *t, struct core_data *c,
871 double interval_float, tsc;
874 struct msr_counter *mp;
878 /* if showing only 1st thread in core and this isn't one, bail out */
879 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
882 /* if showing only 1st thread in pkg and this isn't one, bail out */
883 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
886 /*if not summary line and --cpu is used */
887 if ((t != &average.threads) &&
888 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
891 if (DO_BIC(BIC_USEC)) {
892 /* on each row, print how many usec each timestamp took to gather */
895 timersub(&t->tv_end, &t->tv_begin, &tv);
896 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
899 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
901 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
903 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
905 tsc = t->tsc * tsc_tweak;
907 /* topo columns, print blanks on 1st (average) line */
908 if (t == &average.threads) {
909 if (DO_BIC(BIC_Package))
910 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
911 if (DO_BIC(BIC_Node))
912 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
913 if (DO_BIC(BIC_Core))
914 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
916 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
917 if (DO_BIC(BIC_APIC))
918 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
919 if (DO_BIC(BIC_X2APIC))
920 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
922 if (DO_BIC(BIC_Package)) {
924 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
926 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
928 if (DO_BIC(BIC_Node)) {
930 outp += sprintf(outp, "%s%d",
931 (printed++ ? delim : ""),
932 cpus[t->cpu_id].physical_node_id);
934 outp += sprintf(outp, "%s-",
935 (printed++ ? delim : ""));
937 if (DO_BIC(BIC_Core)) {
939 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
941 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
944 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
945 if (DO_BIC(BIC_APIC))
946 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
947 if (DO_BIC(BIC_X2APIC))
948 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
951 if (DO_BIC(BIC_Avg_MHz))
952 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
953 1.0 / units * t->aperf / interval_float);
955 if (DO_BIC(BIC_Busy))
956 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
958 if (DO_BIC(BIC_Bzy_MHz)) {
960 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
962 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
963 tsc / units * t->aperf / t->mperf / interval_float);
966 if (DO_BIC(BIC_TSC_MHz))
967 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
970 if (DO_BIC(BIC_IRQ)) {
971 if (sums_need_wide_columns)
972 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
974 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
979 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
982 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
983 if (mp->format == FORMAT_RAW) {
985 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
987 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
988 } else if (mp->format == FORMAT_DELTA) {
989 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
990 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
992 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
993 } else if (mp->format == FORMAT_PERCENT) {
994 if (mp->type == COUNTER_USEC)
995 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
997 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
1002 if (DO_BIC(BIC_CPU_c1))
1003 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
1006 /* print per-core data only for 1st thread in core */
1007 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1010 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
1011 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1012 if (DO_BIC(BIC_CPU_c6))
1013 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1014 if (DO_BIC(BIC_CPU_c7))
1015 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1018 if (DO_BIC(BIC_Mod_c6))
1019 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
1021 if (DO_BIC(BIC_CoreTmp))
1022 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1024 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1025 if (mp->format == FORMAT_RAW) {
1026 if (mp->width == 32)
1027 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1029 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1030 } else if (mp->format == FORMAT_DELTA) {
1031 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1032 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
1034 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1035 } else if (mp->format == FORMAT_PERCENT) {
1036 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1040 /* print per-package data only for 1st core in package */
1041 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1045 if (DO_BIC(BIC_PkgTmp))
1046 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1049 if (DO_BIC(BIC_GFX_rc6)) {
1050 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
1051 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
1053 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
1054 p->gfx_rc6_ms / 10.0 / interval_float);
1059 if (DO_BIC(BIC_GFXMHz))
1060 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
1062 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1063 if (DO_BIC(BIC_Totl_c0))
1064 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1065 if (DO_BIC(BIC_Any_c0))
1066 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1067 if (DO_BIC(BIC_GFX_c0))
1068 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1069 if (DO_BIC(BIC_CPUGFX))
1070 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
1072 if (DO_BIC(BIC_Pkgpc2))
1073 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
1074 if (DO_BIC(BIC_Pkgpc3))
1075 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
1076 if (DO_BIC(BIC_Pkgpc6))
1077 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
1078 if (DO_BIC(BIC_Pkgpc7))
1079 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
1080 if (DO_BIC(BIC_Pkgpc8))
1081 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
1082 if (DO_BIC(BIC_Pkgpc9))
1083 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
1084 if (DO_BIC(BIC_Pkgpc10))
1085 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1087 if (DO_BIC(BIC_CPU_LPI))
1088 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1089 if (DO_BIC(BIC_SYS_LPI))
1090 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1093 * If measurement interval exceeds minimum RAPL Joule Counter range,
1094 * indicate that results are suspect by printing "**" in fraction place.
1096 if (interval_float < rapl_joule_counter_range)
1101 if (DO_BIC(BIC_PkgWatt))
1102 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1103 if (DO_BIC(BIC_CorWatt))
1104 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1105 if (DO_BIC(BIC_GFXWatt))
1106 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1107 if (DO_BIC(BIC_RAMWatt))
1108 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1109 if (DO_BIC(BIC_Pkg_J))
1110 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1111 if (DO_BIC(BIC_Cor_J))
1112 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1113 if (DO_BIC(BIC_GFX_J))
1114 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1115 if (DO_BIC(BIC_RAM_J))
1116 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1117 if (DO_BIC(BIC_PKG__))
1118 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1119 if (DO_BIC(BIC_RAM__))
1120 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1122 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1123 if (mp->format == FORMAT_RAW) {
1124 if (mp->width == 32)
1125 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1127 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1128 } else if (mp->format == FORMAT_DELTA) {
1129 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1130 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
1132 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1133 } else if (mp->format == FORMAT_PERCENT) {
1134 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1139 if (*(outp - 1) != '\n')
1140 outp += sprintf(outp, "\n");
1145 void flush_output_stdout(void)
1154 fputs(output_buffer, filep);
1157 outp = output_buffer;
1159 void flush_output_stderr(void)
1161 fputs(output_buffer, outf);
1163 outp = output_buffer;
1165 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1169 if (!printed || !summary_only)
1172 format_counters(&average.threads, &average.cores, &average.packages);
1179 for_all_cpus(format_counters, t, c, p);
1182 #define DELTA_WRAP32(new, old) \
1186 old = 0x100000000 + new - old; \
1190 delta_package(struct pkg_data *new, struct pkg_data *old)
1193 struct msr_counter *mp;
1196 if (DO_BIC(BIC_Totl_c0))
1197 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1198 if (DO_BIC(BIC_Any_c0))
1199 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1200 if (DO_BIC(BIC_GFX_c0))
1201 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1202 if (DO_BIC(BIC_CPUGFX))
1203 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1205 old->pc2 = new->pc2 - old->pc2;
1206 if (DO_BIC(BIC_Pkgpc3))
1207 old->pc3 = new->pc3 - old->pc3;
1208 if (DO_BIC(BIC_Pkgpc6))
1209 old->pc6 = new->pc6 - old->pc6;
1210 if (DO_BIC(BIC_Pkgpc7))
1211 old->pc7 = new->pc7 - old->pc7;
1212 old->pc8 = new->pc8 - old->pc8;
1213 old->pc9 = new->pc9 - old->pc9;
1214 old->pc10 = new->pc10 - old->pc10;
1215 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1216 old->sys_lpi = new->sys_lpi - old->sys_lpi;
1217 old->pkg_temp_c = new->pkg_temp_c;
1219 /* flag an error when rc6 counter resets/wraps */
1220 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1221 old->gfx_rc6_ms = -1;
1223 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1225 old->gfx_mhz = new->gfx_mhz;
1227 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1228 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1229 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1230 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1231 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1232 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1234 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1235 if (mp->format == FORMAT_RAW)
1236 old->counter[i] = new->counter[i];
1238 old->counter[i] = new->counter[i] - old->counter[i];
1245 delta_core(struct core_data *new, struct core_data *old)
1248 struct msr_counter *mp;
1250 old->c3 = new->c3 - old->c3;
1251 old->c6 = new->c6 - old->c6;
1252 old->c7 = new->c7 - old->c7;
1253 old->core_temp_c = new->core_temp_c;
1254 old->mc6_us = new->mc6_us - old->mc6_us;
1256 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1257 if (mp->format == FORMAT_RAW)
1258 old->counter[i] = new->counter[i];
1260 old->counter[i] = new->counter[i] - old->counter[i];
1268 delta_thread(struct thread_data *new, struct thread_data *old,
1269 struct core_data *core_delta)
1272 struct msr_counter *mp;
1274 /* we run cpuid just the 1st time, copy the results */
1275 if (DO_BIC(BIC_APIC))
1276 new->apic_id = old->apic_id;
1277 if (DO_BIC(BIC_X2APIC))
1278 new->x2apic_id = old->x2apic_id;
1281 * the timestamps from start of measurement interval are in "old"
1282 * the timestamp from end of measurement interval are in "new"
1283 * over-write old w/ new so we can print end of interval values
1286 old->tv_begin = new->tv_begin;
1287 old->tv_end = new->tv_end;
1289 old->tsc = new->tsc - old->tsc;
1291 /* check for TSC < 1 Mcycles over interval */
1292 if (old->tsc < (1000 * 1000))
1293 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1294 "You can disable all c-states by booting with \"idle=poll\"\n"
1295 "or just the deep ones with \"processor.max_cstate=1\"");
1297 old->c1 = new->c1 - old->c1;
1299 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1300 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1301 old->aperf = new->aperf - old->aperf;
1302 old->mperf = new->mperf - old->mperf;
1309 if (use_c1_residency_msr) {
1311 * Some models have a dedicated C1 residency MSR,
1312 * which should be more accurate than the derivation below.
1316 * As counter collection is not atomic,
1317 * it is possible for mperf's non-halted cycles + idle states
1318 * to exceed TSC's all cycles: show c1 = 0% in that case.
1320 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1323 /* normal case, derive c1 */
1324 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1325 - core_delta->c6 - core_delta->c7;
1329 if (old->mperf == 0) {
1331 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1332 old->mperf = 1; /* divide by 0 protection */
1335 if (DO_BIC(BIC_IRQ))
1336 old->irq_count = new->irq_count - old->irq_count;
1338 if (DO_BIC(BIC_SMI))
1339 old->smi_count = new->smi_count - old->smi_count;
1341 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1342 if (mp->format == FORMAT_RAW)
1343 old->counter[i] = new->counter[i];
1345 old->counter[i] = new->counter[i] - old->counter[i];
1350 int delta_cpu(struct thread_data *t, struct core_data *c,
1351 struct pkg_data *p, struct thread_data *t2,
1352 struct core_data *c2, struct pkg_data *p2)
1356 /* calculate core delta only for 1st thread in core */
1357 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1360 /* always calculate thread delta */
1361 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1365 /* calculate package delta only for 1st core in package */
1366 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1367 retval = delta_package(p, p2);
1372 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1375 struct msr_counter *mp;
1377 t->tv_begin.tv_sec = 0;
1378 t->tv_begin.tv_usec = 0;
1379 t->tv_end.tv_sec = 0;
1380 t->tv_end.tv_usec = 0;
1390 /* tells format_counters to dump all fields from this set */
1391 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1399 p->pkg_wtd_core_c0 = 0;
1400 p->pkg_any_core_c0 = 0;
1401 p->pkg_any_gfxe_c0 = 0;
1402 p->pkg_both_core_gfxe_c0 = 0;
1405 if (DO_BIC(BIC_Pkgpc3))
1407 if (DO_BIC(BIC_Pkgpc6))
1409 if (DO_BIC(BIC_Pkgpc7))
1419 p->energy_cores = 0;
1421 p->rapl_pkg_perf_status = 0;
1422 p->rapl_dram_perf_status = 0;
1427 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1430 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1433 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1436 int sum_counters(struct thread_data *t, struct core_data *c,
1440 struct msr_counter *mp;
1442 /* copy un-changing apic_id's */
1443 if (DO_BIC(BIC_APIC))
1444 average.threads.apic_id = t->apic_id;
1445 if (DO_BIC(BIC_X2APIC))
1446 average.threads.x2apic_id = t->x2apic_id;
1448 /* remember first tv_begin */
1449 if (average.threads.tv_begin.tv_sec == 0)
1450 average.threads.tv_begin = t->tv_begin;
1452 /* remember last tv_end */
1453 average.threads.tv_end = t->tv_end;
1455 average.threads.tsc += t->tsc;
1456 average.threads.aperf += t->aperf;
1457 average.threads.mperf += t->mperf;
1458 average.threads.c1 += t->c1;
1460 average.threads.irq_count += t->irq_count;
1461 average.threads.smi_count += t->smi_count;
1463 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1464 if (mp->format == FORMAT_RAW)
1466 average.threads.counter[i] += t->counter[i];
1469 /* sum per-core values only for 1st thread in core */
1470 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1473 average.cores.c3 += c->c3;
1474 average.cores.c6 += c->c6;
1475 average.cores.c7 += c->c7;
1476 average.cores.mc6_us += c->mc6_us;
1478 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1480 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1481 if (mp->format == FORMAT_RAW)
1483 average.cores.counter[i] += c->counter[i];
1486 /* sum per-pkg values only for 1st core in pkg */
1487 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1490 if (DO_BIC(BIC_Totl_c0))
1491 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1492 if (DO_BIC(BIC_Any_c0))
1493 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1494 if (DO_BIC(BIC_GFX_c0))
1495 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1496 if (DO_BIC(BIC_CPUGFX))
1497 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1499 average.packages.pc2 += p->pc2;
1500 if (DO_BIC(BIC_Pkgpc3))
1501 average.packages.pc3 += p->pc3;
1502 if (DO_BIC(BIC_Pkgpc6))
1503 average.packages.pc6 += p->pc6;
1504 if (DO_BIC(BIC_Pkgpc7))
1505 average.packages.pc7 += p->pc7;
1506 average.packages.pc8 += p->pc8;
1507 average.packages.pc9 += p->pc9;
1508 average.packages.pc10 += p->pc10;
1510 average.packages.cpu_lpi = p->cpu_lpi;
1511 average.packages.sys_lpi = p->sys_lpi;
1513 average.packages.energy_pkg += p->energy_pkg;
1514 average.packages.energy_dram += p->energy_dram;
1515 average.packages.energy_cores += p->energy_cores;
1516 average.packages.energy_gfx += p->energy_gfx;
1518 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1519 average.packages.gfx_mhz = p->gfx_mhz;
1521 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1523 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1524 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1526 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1527 if (mp->format == FORMAT_RAW)
1529 average.packages.counter[i] += p->counter[i];
1534 * sum the counters for all cpus in the system
1535 * compute the weighted average
1537 void compute_average(struct thread_data *t, struct core_data *c,
1541 struct msr_counter *mp;
1543 clear_counters(&average.threads, &average.cores, &average.packages);
1545 for_all_cpus(sum_counters, t, c, p);
1547 average.threads.tsc /= topo.num_cpus;
1548 average.threads.aperf /= topo.num_cpus;
1549 average.threads.mperf /= topo.num_cpus;
1550 average.threads.c1 /= topo.num_cpus;
1552 if (average.threads.irq_count > 9999999)
1553 sums_need_wide_columns = 1;
1555 average.cores.c3 /= topo.num_cores;
1556 average.cores.c6 /= topo.num_cores;
1557 average.cores.c7 /= topo.num_cores;
1558 average.cores.mc6_us /= topo.num_cores;
1560 if (DO_BIC(BIC_Totl_c0))
1561 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1562 if (DO_BIC(BIC_Any_c0))
1563 average.packages.pkg_any_core_c0 /= topo.num_packages;
1564 if (DO_BIC(BIC_GFX_c0))
1565 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1566 if (DO_BIC(BIC_CPUGFX))
1567 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1569 average.packages.pc2 /= topo.num_packages;
1570 if (DO_BIC(BIC_Pkgpc3))
1571 average.packages.pc3 /= topo.num_packages;
1572 if (DO_BIC(BIC_Pkgpc6))
1573 average.packages.pc6 /= topo.num_packages;
1574 if (DO_BIC(BIC_Pkgpc7))
1575 average.packages.pc7 /= topo.num_packages;
1577 average.packages.pc8 /= topo.num_packages;
1578 average.packages.pc9 /= topo.num_packages;
1579 average.packages.pc10 /= topo.num_packages;
1581 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1582 if (mp->format == FORMAT_RAW)
1584 if (mp->type == COUNTER_ITEMS) {
1585 if (average.threads.counter[i] > 9999999)
1586 sums_need_wide_columns = 1;
1589 average.threads.counter[i] /= topo.num_cpus;
1591 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1592 if (mp->format == FORMAT_RAW)
1594 if (mp->type == COUNTER_ITEMS) {
1595 if (average.cores.counter[i] > 9999999)
1596 sums_need_wide_columns = 1;
1598 average.cores.counter[i] /= topo.num_cores;
1600 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1601 if (mp->format == FORMAT_RAW)
1603 if (mp->type == COUNTER_ITEMS) {
1604 if (average.packages.counter[i] > 9999999)
1605 sums_need_wide_columns = 1;
1607 average.packages.counter[i] /= topo.num_packages;
1611 static unsigned long long rdtsc(void)
1613 unsigned int low, high;
1615 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1617 return low | ((unsigned long long)high) << 32;
1621 * Open a file, and exit on failure
1623 FILE *fopen_or_die(const char *path, const char *mode)
1625 FILE *filep = fopen(path, mode);
1628 err(1, "%s: open failed", path);
1632 * snapshot_sysfs_counter()
1634 * return snapshot of given counter
1636 unsigned long long snapshot_sysfs_counter(char *path)
1640 unsigned long long counter;
1642 fp = fopen_or_die(path, "r");
1644 retval = fscanf(fp, "%lld", &counter);
1646 err(1, "snapshot_sysfs_counter(%s)", path);
1653 int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1655 if (mp->msr_num != 0) {
1656 if (get_msr(cpu, mp->msr_num, counterp))
1659 char path[128 + PATH_BYTES];
1661 if (mp->flags & SYSFS_PERCPU) {
1662 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1665 *counterp = snapshot_sysfs_counter(path);
1667 *counterp = snapshot_sysfs_counter(mp->path);
1674 void get_apic_id(struct thread_data *t)
1676 unsigned int eax, ebx, ecx, edx;
1678 if (DO_BIC(BIC_APIC)) {
1679 eax = ebx = ecx = edx = 0;
1680 __cpuid(1, eax, ebx, ecx, edx);
1682 t->apic_id = (ebx >> 24) & 0xff;
1685 if (!DO_BIC(BIC_X2APIC))
1688 if (authentic_amd) {
1689 unsigned int topology_extensions;
1691 if (max_extended_level < 0x8000001e)
1694 eax = ebx = ecx = edx = 0;
1695 __cpuid(0x80000001, eax, ebx, ecx, edx);
1696 topology_extensions = ecx & (1 << 22);
1698 if (topology_extensions == 0)
1701 eax = ebx = ecx = edx = 0;
1702 __cpuid(0x8000001e, eax, ebx, ecx, edx);
1711 if (max_level < 0xb)
1715 __cpuid(0xb, eax, ebx, ecx, edx);
1718 if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
1719 fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
1720 t->cpu_id, t->apic_id, t->x2apic_id);
1726 * acquire and record local counters for that cpu
1728 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1730 int cpu = t->cpu_id;
1731 unsigned long long msr;
1732 int aperf_mperf_retry_count = 0;
1733 struct msr_counter *mp;
1736 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1738 if (cpu_migrate(cpu)) {
1739 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
1743 if (first_counter_read)
1746 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1748 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1749 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1752 * The TSC, APERF and MPERF must be read together for
1753 * APERF/MPERF and MPERF/TSC to give accurate results.
1755 * Unfortunately, APERF and MPERF are read by
1756 * individual system call, so delays may occur
1757 * between them. If the time to read them
1758 * varies by a large amount, we re-read them.
1762 * This initial dummy APERF read has been seen to
1763 * reduce jitter in the subsequent reads.
1766 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1769 t->tsc = rdtsc(); /* re-read close to APERF */
1771 tsc_before = t->tsc;
1773 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1776 tsc_between = rdtsc();
1778 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1781 tsc_after = rdtsc();
1783 aperf_time = tsc_between - tsc_before;
1784 mperf_time = tsc_after - tsc_between;
1787 * If the system call latency to read APERF and MPERF
1788 * differ by more than 2x, then try again.
1790 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1791 aperf_mperf_retry_count++;
1792 if (aperf_mperf_retry_count < 5)
1795 warnx("cpu%d jitter %lld %lld",
1796 cpu, aperf_time, mperf_time);
1798 aperf_mperf_retry_count = 0;
1800 t->aperf = t->aperf * aperf_mperf_multiplier;
1801 t->mperf = t->mperf * aperf_mperf_multiplier;
1804 if (DO_BIC(BIC_IRQ))
1805 t->irq_count = irqs_per_cpu[cpu];
1806 if (DO_BIC(BIC_SMI)) {
1807 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1809 t->smi_count = msr & 0xFFFFFFFF;
1811 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1812 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1816 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1817 if (get_mp(cpu, mp, &t->counter[i]))
1821 /* collect core counters only for 1st thread in core */
1822 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1825 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
1826 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1830 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1831 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1833 } else if (do_knl_cstates) {
1834 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1838 if (DO_BIC(BIC_CPU_c7))
1839 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1842 if (DO_BIC(BIC_Mod_c6))
1843 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1846 if (DO_BIC(BIC_CoreTmp)) {
1847 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1849 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1852 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1853 if (get_mp(cpu, mp, &c->counter[i]))
1857 /* collect package counters only for 1st core in package */
1858 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1861 if (DO_BIC(BIC_Totl_c0)) {
1862 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1865 if (DO_BIC(BIC_Any_c0)) {
1866 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1869 if (DO_BIC(BIC_GFX_c0)) {
1870 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1873 if (DO_BIC(BIC_CPUGFX)) {
1874 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1877 if (DO_BIC(BIC_Pkgpc3))
1878 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1880 if (DO_BIC(BIC_Pkgpc6)) {
1881 if (do_slm_cstates) {
1882 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1885 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1890 if (DO_BIC(BIC_Pkgpc2))
1891 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1893 if (DO_BIC(BIC_Pkgpc7))
1894 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1896 if (DO_BIC(BIC_Pkgpc8))
1897 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1899 if (DO_BIC(BIC_Pkgpc9))
1900 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1902 if (DO_BIC(BIC_Pkgpc10))
1903 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1906 if (DO_BIC(BIC_CPU_LPI))
1907 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1908 if (DO_BIC(BIC_SYS_LPI))
1909 p->sys_lpi = cpuidle_cur_sys_lpi_us;
1911 if (do_rapl & RAPL_PKG) {
1912 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1914 p->energy_pkg = msr & 0xFFFFFFFF;
1916 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1917 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1919 p->energy_cores = msr & 0xFFFFFFFF;
1921 if (do_rapl & RAPL_DRAM) {
1922 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1924 p->energy_dram = msr & 0xFFFFFFFF;
1926 if (do_rapl & RAPL_GFX) {
1927 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1929 p->energy_gfx = msr & 0xFFFFFFFF;
1931 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1932 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1934 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1936 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1937 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1939 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1941 if (DO_BIC(BIC_PkgTmp)) {
1942 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1944 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1947 if (DO_BIC(BIC_GFX_rc6))
1948 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1950 if (DO_BIC(BIC_GFXMHz))
1951 p->gfx_mhz = gfx_cur_mhz;
1953 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1954 if (get_mp(cpu, mp, &p->counter[i]))
1958 gettimeofday(&t->tv_end, (struct timezone *)NULL);
1964 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1965 * If you change the values, note they are used both in comparisons
1966 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1969 #define PCLUKN 0 /* Unknown */
1970 #define PCLRSV 1 /* Reserved */
1971 #define PCL__0 2 /* PC0 */
1972 #define PCL__1 3 /* PC1 */
1973 #define PCL__2 4 /* PC2 */
1974 #define PCL__3 5 /* PC3 */
1975 #define PCL__4 6 /* PC4 */
1976 #define PCL__6 7 /* PC6 */
1977 #define PCL_6N 8 /* PC6 No Retention */
1978 #define PCL_6R 9 /* PC6 Retention */
1979 #define PCL__7 10 /* PC7 */
1980 #define PCL_7S 11 /* PC7 Shrink */
1981 #define PCL__8 12 /* PC8 */
1982 #define PCL__9 13 /* PC9 */
1983 #define PCL_10 14 /* PC10 */
1984 #define PCLUNL 15 /* Unlimited */
1986 int pkg_cstate_limit = PCLUKN;
1987 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1988 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"};
1990 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};
1991 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};
1992 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};
1993 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};
1994 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};
1995 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};
1996 int glm_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1997 int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
2001 calculate_tsc_tweak()
2003 tsc_tweak = base_hz / tsc_hz;
2007 dump_nhm_platform_info(void)
2009 unsigned long long msr;
2012 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2014 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
2016 ratio = (msr >> 40) & 0xFF;
2017 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2018 ratio, bclk, ratio * bclk);
2020 ratio = (msr >> 8) & 0xFF;
2021 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2022 ratio, bclk, ratio * bclk);
2024 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2025 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2026 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
2032 dump_hsw_turbo_ratio_limits(void)
2034 unsigned long long msr;
2037 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2039 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2041 ratio = (msr >> 8) & 0xFF;
2043 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2044 ratio, bclk, ratio * bclk);
2046 ratio = (msr >> 0) & 0xFF;
2048 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2049 ratio, bclk, ratio * bclk);
2054 dump_ivt_turbo_ratio_limits(void)
2056 unsigned long long msr;
2059 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
2061 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
2063 ratio = (msr >> 56) & 0xFF;
2065 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
2066 ratio, bclk, ratio * bclk);
2068 ratio = (msr >> 48) & 0xFF;
2070 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
2071 ratio, bclk, ratio * bclk);
2073 ratio = (msr >> 40) & 0xFF;
2075 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
2076 ratio, bclk, ratio * bclk);
2078 ratio = (msr >> 32) & 0xFF;
2080 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
2081 ratio, bclk, ratio * bclk);
2083 ratio = (msr >> 24) & 0xFF;
2085 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
2086 ratio, bclk, ratio * bclk);
2088 ratio = (msr >> 16) & 0xFF;
2090 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
2091 ratio, bclk, ratio * bclk);
2093 ratio = (msr >> 8) & 0xFF;
2095 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
2096 ratio, bclk, ratio * bclk);
2098 ratio = (msr >> 0) & 0xFF;
2100 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
2101 ratio, bclk, ratio * bclk);
2104 int has_turbo_ratio_group_limits(int family, int model)
2111 case INTEL_FAM6_ATOM_GOLDMONT:
2112 case INTEL_FAM6_SKYLAKE_X:
2113 case INTEL_FAM6_ATOM_GOLDMONT_X:
2120 dump_turbo_ratio_limits(int family, int model)
2122 unsigned long long msr, core_counts;
2123 unsigned int ratio, group_size;
2125 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2126 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2128 if (has_turbo_ratio_group_limits(family, model)) {
2129 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2130 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2132 core_counts = 0x0807060504030201;
2135 ratio = (msr >> 56) & 0xFF;
2136 group_size = (core_counts >> 56) & 0xFF;
2138 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2139 ratio, bclk, ratio * bclk, group_size);
2141 ratio = (msr >> 48) & 0xFF;
2142 group_size = (core_counts >> 48) & 0xFF;
2144 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2145 ratio, bclk, ratio * bclk, group_size);
2147 ratio = (msr >> 40) & 0xFF;
2148 group_size = (core_counts >> 40) & 0xFF;
2150 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2151 ratio, bclk, ratio * bclk, group_size);
2153 ratio = (msr >> 32) & 0xFF;
2154 group_size = (core_counts >> 32) & 0xFF;
2156 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2157 ratio, bclk, ratio * bclk, group_size);
2159 ratio = (msr >> 24) & 0xFF;
2160 group_size = (core_counts >> 24) & 0xFF;
2162 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2163 ratio, bclk, ratio * bclk, group_size);
2165 ratio = (msr >> 16) & 0xFF;
2166 group_size = (core_counts >> 16) & 0xFF;
2168 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2169 ratio, bclk, ratio * bclk, group_size);
2171 ratio = (msr >> 8) & 0xFF;
2172 group_size = (core_counts >> 8) & 0xFF;
2174 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2175 ratio, bclk, ratio * bclk, group_size);
2177 ratio = (msr >> 0) & 0xFF;
2178 group_size = (core_counts >> 0) & 0xFF;
2180 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2181 ratio, bclk, ratio * bclk, group_size);
2186 dump_atom_turbo_ratio_limits(void)
2188 unsigned long long msr;
2191 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2192 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2194 ratio = (msr >> 0) & 0x3F;
2196 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2197 ratio, bclk, ratio * bclk);
2199 ratio = (msr >> 8) & 0x3F;
2201 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2202 ratio, bclk, ratio * bclk);
2204 ratio = (msr >> 16) & 0x3F;
2206 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2207 ratio, bclk, ratio * bclk);
2209 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2210 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2212 ratio = (msr >> 24) & 0x3F;
2214 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2215 ratio, bclk, ratio * bclk);
2217 ratio = (msr >> 16) & 0x3F;
2219 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2220 ratio, bclk, ratio * bclk);
2222 ratio = (msr >> 8) & 0x3F;
2224 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2225 ratio, bclk, ratio * bclk);
2227 ratio = (msr >> 0) & 0x3F;
2229 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2230 ratio, bclk, ratio * bclk);
2234 dump_knl_turbo_ratio_limits(void)
2236 const unsigned int buckets_no = 7;
2238 unsigned long long msr;
2239 int delta_cores, delta_ratio;
2241 unsigned int cores[buckets_no];
2242 unsigned int ratio[buckets_no];
2244 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2246 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2250 * Turbo encoding in KNL is as follows:
2252 * [7:1] -- Base value of number of active cores of bucket 1.
2253 * [15:8] -- Base value of freq ratio of bucket 1.
2254 * [20:16] -- +ve delta of number of active cores of bucket 2.
2255 * i.e. active cores of bucket 2 =
2256 * active cores of bucket 1 + delta
2257 * [23:21] -- Negative delta of freq ratio of bucket 2.
2258 * i.e. freq ratio of bucket 2 =
2259 * freq ratio of bucket 1 - delta
2260 * [28:24]-- +ve delta of number of active cores of bucket 3.
2261 * [31:29]-- -ve delta of freq ratio of bucket 3.
2262 * [36:32]-- +ve delta of number of active cores of bucket 4.
2263 * [39:37]-- -ve delta of freq ratio of bucket 4.
2264 * [44:40]-- +ve delta of number of active cores of bucket 5.
2265 * [47:45]-- -ve delta of freq ratio of bucket 5.
2266 * [52:48]-- +ve delta of number of active cores of bucket 6.
2267 * [55:53]-- -ve delta of freq ratio of bucket 6.
2268 * [60:56]-- +ve delta of number of active cores of bucket 7.
2269 * [63:61]-- -ve delta of freq ratio of bucket 7.
2273 cores[b_nr] = (msr & 0xFF) >> 1;
2274 ratio[b_nr] = (msr >> 8) & 0xFF;
2276 for (i = 16; i < 64; i += 8) {
2277 delta_cores = (msr >> i) & 0x1F;
2278 delta_ratio = (msr >> (i + 5)) & 0x7;
2280 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2281 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2285 for (i = buckets_no - 1; i >= 0; i--)
2286 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2288 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2289 ratio[i], bclk, ratio[i] * bclk, cores[i]);
2293 dump_nhm_cst_cfg(void)
2295 unsigned long long msr;
2297 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2299 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2301 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2302 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2303 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2304 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2305 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2306 (msr & (1 << 15)) ? "" : "UN",
2307 (unsigned int)msr & 0xF,
2308 pkg_cstate_limit_strings[pkg_cstate_limit]);
2310 #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2311 if (has_automatic_cstate_conversion) {
2312 fprintf(outf, ", automatic c-state conversion=%s",
2313 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2316 fprintf(outf, ")\n");
2322 dump_config_tdp(void)
2324 unsigned long long msr;
2326 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2327 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2328 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
2330 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2331 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
2333 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2334 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2335 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2336 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
2338 fprintf(outf, ")\n");
2340 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2341 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
2343 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2344 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2345 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2346 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
2348 fprintf(outf, ")\n");
2350 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2351 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
2353 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2354 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2355 fprintf(outf, ")\n");
2357 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2358 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2359 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2360 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2361 fprintf(outf, ")\n");
2364 unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2366 void print_irtl(void)
2368 unsigned long long msr;
2370 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2371 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2372 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2373 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2375 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2376 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2377 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2378 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2380 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2381 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2382 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2383 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2388 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2389 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2390 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2391 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2393 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2394 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2395 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2396 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2398 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2399 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2400 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2401 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2404 void free_fd_percpu(void)
2408 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
2409 if (fd_percpu[i] != 0)
2410 close(fd_percpu[i]);
2416 void free_all_buffers(void)
2420 CPU_FREE(cpu_present_set);
2421 cpu_present_set = NULL;
2422 cpu_present_setsize = 0;
2424 CPU_FREE(cpu_affinity_set);
2425 cpu_affinity_set = NULL;
2426 cpu_affinity_setsize = 0;
2434 package_even = NULL;
2444 free(output_buffer);
2445 output_buffer = NULL;
2450 free(irq_column_2_cpu);
2453 for (i = 0; i <= topo.max_cpu_num; ++i) {
2454 if (cpus[i].put_ids)
2455 CPU_FREE(cpus[i].put_ids);
2462 * Parse a file containing a single int.
2464 int parse_int_file(const char *fmt, ...)
2467 char path[PATH_MAX];
2471 va_start(args, fmt);
2472 vsnprintf(path, sizeof(path), fmt, args);
2474 filep = fopen_or_die(path, "r");
2475 if (fscanf(filep, "%d", &value) != 1)
2476 err(1, "%s: failed to parse number from file", path);
2482 * cpu_is_first_core_in_package(cpu)
2483 * return 1 if given CPU is 1st core in package
2485 int cpu_is_first_core_in_package(int cpu)
2487 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2490 int get_physical_package_id(int cpu)
2492 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2495 int get_core_id(int cpu)
2497 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2500 void set_node_data(void)
2502 int pkg, node, lnode, cpu, cpux;
2505 /* initialize logical_node_id */
2506 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
2507 cpus[cpu].logical_node_id = -1;
2510 for (pkg = 0; pkg < topo.num_packages; pkg++) {
2512 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
2513 if (cpus[cpu].physical_package_id != pkg)
2515 /* find a cpu with an unset logical_node_id */
2516 if (cpus[cpu].logical_node_id != -1)
2518 cpus[cpu].logical_node_id = lnode;
2519 node = cpus[cpu].physical_node_id;
2522 * find all matching cpus on this pkg and set
2523 * the logical_node_id
2525 for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
2526 if ((cpus[cpux].physical_package_id == pkg) &&
2527 (cpus[cpux].physical_node_id == node)) {
2528 cpus[cpux].logical_node_id = lnode;
2533 if (lnode > topo.nodes_per_pkg)
2534 topo.nodes_per_pkg = lnode;
2536 if (cpu_count >= topo.max_cpu_num)
2541 int get_physical_node_id(struct cpu_topology *thiscpu)
2546 int cpu = thiscpu->logical_cpu_id;
2548 for (i = 0; i <= topo.max_cpu_num; i++) {
2549 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2551 filep = fopen(path, "r");
2560 int get_thread_siblings(struct cpu_topology *thiscpu)
2562 char path[80], character;
2565 int so, shift, sib_core;
2566 int cpu = thiscpu->logical_cpu_id;
2567 int offset = topo.max_cpu_num + 1;
2571 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
2572 if (thiscpu->thread_id < 0)
2573 thiscpu->thread_id = thread_id++;
2574 if (!thiscpu->put_ids)
2577 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2578 CPU_ZERO_S(size, thiscpu->put_ids);
2581 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2582 filep = fopen_or_die(path, "r");
2584 offset -= BITMASK_SIZE;
2585 fscanf(filep, "%lx%c", &map, &character);
2586 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2587 if ((map >> shift) & 0x1) {
2588 so = shift + offset;
2589 sib_core = get_core_id(so);
2590 if (sib_core == thiscpu->physical_core_id) {
2591 CPU_SET_S(so, size, thiscpu->put_ids);
2593 (cpus[so].thread_id < 0))
2594 cpus[so].thread_id =
2599 } while (!strncmp(&character, ",", 1));
2602 return CPU_COUNT_S(size, thiscpu->put_ids);
2606 * run func(thread, core, package) in topology order
2607 * skip non-present cpus
2610 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2611 struct pkg_data *, struct thread_data *, struct core_data *,
2612 struct pkg_data *), struct thread_data *thread_base,
2613 struct core_data *core_base, struct pkg_data *pkg_base,
2614 struct thread_data *thread_base2, struct core_data *core_base2,
2615 struct pkg_data *pkg_base2)
2617 int retval, pkg_no, node_no, core_no, thread_no;
2619 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2620 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
2621 for (core_no = 0; core_no < topo.cores_per_node;
2623 for (thread_no = 0; thread_no <
2624 topo.threads_per_core; ++thread_no) {
2625 struct thread_data *t, *t2;
2626 struct core_data *c, *c2;
2627 struct pkg_data *p, *p2;
2629 t = GET_THREAD(thread_base, thread_no,
2633 if (cpu_is_not_present(t->cpu_id))
2636 t2 = GET_THREAD(thread_base2, thread_no,
2640 c = GET_CORE(core_base, core_no,
2642 c2 = GET_CORE(core_base2, core_no,
2646 p = GET_PKG(pkg_base, pkg_no);
2647 p2 = GET_PKG(pkg_base2, pkg_no);
2649 retval = func(t, c, p, t2, c2, p2);
2660 * run func(cpu) on every cpu in /proc/stat
2661 * return max_cpu number
2663 int for_all_proc_cpus(int (func)(int))
2669 fp = fopen_or_die(proc_stat, "r");
2671 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2673 err(1, "%s: failed to parse format", proc_stat);
2676 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2680 retval = func(cpu_num);
2690 void re_initialize(void)
2693 setup_all_buffers();
2694 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2697 void set_max_cpu_num(void)
2700 unsigned long dummy;
2702 topo.max_cpu_num = 0;
2703 filep = fopen_or_die(
2704 "/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2706 while (fscanf(filep, "%lx,", &dummy) == 1)
2707 topo.max_cpu_num += BITMASK_SIZE;
2709 topo.max_cpu_num--; /* 0 based */
2714 * remember the last one seen, it will be the max
2716 int count_cpus(int cpu)
2721 int mark_cpu_present(int cpu)
2723 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
2727 int init_thread_id(int cpu)
2729 cpus[cpu].thread_id = -1;
2734 * snapshot_proc_interrupts()
2736 * read and record summary of /proc/interrupts
2738 * return 1 if config change requires a restart, else return 0
2740 int snapshot_proc_interrupts(void)
2746 fp = fopen_or_die("/proc/interrupts", "r");
2750 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2751 for (column = 0; column < topo.num_cpus; ++column) {
2754 retval = fscanf(fp, " CPU%d", &cpu_number);
2758 if (cpu_number > topo.max_cpu_num) {
2759 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2763 irq_column_2_cpu[column] = cpu_number;
2764 irqs_per_cpu[cpu_number] = 0;
2767 /* read /proc/interrupt count lines and sum up irqs per cpu */
2772 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2776 /* read the count per cpu */
2777 for (column = 0; column < topo.num_cpus; ++column) {
2779 int cpu_number, irq_count;
2781 retval = fscanf(fp, " %d", &irq_count);
2785 cpu_number = irq_column_2_cpu[column];
2786 irqs_per_cpu[cpu_number] += irq_count;
2790 while (getc(fp) != '\n')
2791 ; /* flush interrupt description */
2797 * snapshot_gfx_rc6_ms()
2799 * record snapshot of
2800 * /sys/class/drm/card0/power/rc6_residency_ms
2802 * return 1 if config change requires a restart, else return 0
2804 int snapshot_gfx_rc6_ms(void)
2809 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2811 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2820 * snapshot_gfx_mhz()
2822 * record snapshot of
2823 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2825 * return 1 if config change requires a restart, else return 0
2827 int snapshot_gfx_mhz(void)
2833 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2839 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2847 * snapshot_cpu_lpi()
2849 * record snapshot of
2850 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2852 int snapshot_cpu_lpi_us(void)
2857 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2859 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2868 * snapshot_sys_lpi()
2870 * record snapshot of sys_lpi_file
2872 int snapshot_sys_lpi_us(void)
2877 fp = fopen_or_die(sys_lpi_file, "r");
2879 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2888 * snapshot /proc and /sys files
2890 * return 1 if configuration restart needed, else return 0
2892 int snapshot_proc_sysfs_files(void)
2894 if (DO_BIC(BIC_IRQ))
2895 if (snapshot_proc_interrupts())
2898 if (DO_BIC(BIC_GFX_rc6))
2899 snapshot_gfx_rc6_ms();
2901 if (DO_BIC(BIC_GFXMHz))
2904 if (DO_BIC(BIC_CPU_LPI))
2905 snapshot_cpu_lpi_us();
2907 if (DO_BIC(BIC_SYS_LPI))
2908 snapshot_sys_lpi_us();
2915 static void signal_handler (int signal)
2921 fprintf(stderr, " SIGINT\n");
2925 fprintf(stderr, "SIGUSR1\n");
2928 /* make sure this manually-invoked interval is at least 1ms long */
2929 nanosleep(&one_msec, NULL);
2932 void setup_signal_handler(void)
2934 struct sigaction sa;
2936 memset(&sa, 0, sizeof(sa));
2938 sa.sa_handler = &signal_handler;
2940 if (sigaction(SIGINT, &sa, NULL) < 0)
2941 err(1, "sigaction SIGINT");
2942 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2943 err(1, "sigaction SIGUSR1");
2948 struct timeval select_timeout;
2953 FD_SET(0, &readfds);
2955 if (!isatty(fileno(stdin))) {
2956 nanosleep(&interval_ts, NULL);
2960 select_timeout = interval_tv;
2961 retval = select(1, &readfds, NULL, NULL, &select_timeout);
2964 switch (getc(stdin)) {
2969 /* make sure this manually-invoked interval is at least 1ms long */
2970 nanosleep(&one_msec, NULL);
2975 void turbostat_loop()
2981 setup_signal_handler();
2986 snapshot_proc_sysfs_files();
2987 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2988 first_counter_read = 0;
2991 } else if (retval == -1) {
2992 if (restarted > 1) {
3000 gettimeofday(&tv_even, (struct timezone *)NULL);
3003 if (for_all_proc_cpus(cpu_is_not_present)) {
3008 if (snapshot_proc_sysfs_files())
3010 retval = for_all_cpus(get_counters, ODD_COUNTERS);
3013 } else if (retval == -1) {
3017 gettimeofday(&tv_odd, (struct timezone *)NULL);
3018 timersub(&tv_odd, &tv_even, &tv_delta);
3019 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3023 compute_average(EVEN_COUNTERS);
3024 format_all_counters(EVEN_COUNTERS);
3025 flush_output_stdout();
3028 if (num_iterations && ++done_iters >= num_iterations)
3031 if (snapshot_proc_sysfs_files())
3033 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3036 } else if (retval == -1) {
3040 gettimeofday(&tv_even, (struct timezone *)NULL);
3041 timersub(&tv_even, &tv_odd, &tv_delta);
3042 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3046 compute_average(ODD_COUNTERS);
3047 format_all_counters(ODD_COUNTERS);
3048 flush_output_stdout();
3051 if (num_iterations && ++done_iters >= num_iterations)
3056 void check_dev_msr()
3061 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3062 if (stat(pathname, &sb))
3063 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3064 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3067 void check_permissions()
3069 struct __user_cap_header_struct cap_header_data;
3070 cap_user_header_t cap_header = &cap_header_data;
3071 struct __user_cap_data_struct cap_data_data;
3072 cap_user_data_t cap_data = &cap_data_data;
3073 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
3077 /* check for CAP_SYS_RAWIO */
3078 cap_header->pid = getpid();
3079 cap_header->version = _LINUX_CAPABILITY_VERSION;
3080 if (capget(cap_header, cap_data) < 0)
3081 err(-6, "capget(2) failed");
3083 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
3085 warnx("capget(CAP_SYS_RAWIO) failed,"
3086 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3089 /* test file permissions */
3090 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3091 if (euidaccess(pathname, R_OK)) {
3093 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
3096 /* if all else fails, thell them to be root */
3099 warnx("... or simply run as root");
3106 * NHM adds support for additional MSRs:
3108 * MSR_SMI_COUNT 0x00000034
3110 * MSR_PLATFORM_INFO 0x000000ce
3111 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
3113 * MSR_MISC_PWR_MGMT 0x000001aa
3115 * MSR_PKG_C3_RESIDENCY 0x000003f8
3116 * MSR_PKG_C6_RESIDENCY 0x000003f9
3117 * MSR_CORE_C3_RESIDENCY 0x000003fc
3118 * MSR_CORE_C6_RESIDENCY 0x000003fd
3121 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
3122 * sets has_misc_feature_control
3124 int probe_nhm_msrs(unsigned int family, unsigned int model)
3126 unsigned long long msr;
3127 unsigned int base_ratio;
3128 int *pkg_cstate_limits;
3136 bclk = discover_bclk(family, model);
3139 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3140 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3141 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
3142 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
3143 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
3144 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3145 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3146 pkg_cstate_limits = nhm_pkg_cstate_limits;
3148 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3149 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3150 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3151 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3152 pkg_cstate_limits = snb_pkg_cstate_limits;
3153 has_misc_feature_control = 1;
3155 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3156 case INTEL_FAM6_HASWELL_X: /* HSX */
3157 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3158 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3159 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3160 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3161 case INTEL_FAM6_BROADWELL_X: /* BDX */
3162 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3163 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3164 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3165 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3166 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3167 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
3168 pkg_cstate_limits = hsw_pkg_cstate_limits;
3169 has_misc_feature_control = 1;
3171 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3172 pkg_cstate_limits = skx_pkg_cstate_limits;
3173 has_misc_feature_control = 1;
3175 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
3176 no_MSR_MISC_PWR_MGMT = 1;
3177 case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */
3178 pkg_cstate_limits = slv_pkg_cstate_limits;
3180 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
3181 pkg_cstate_limits = amt_pkg_cstate_limits;
3182 no_MSR_MISC_PWR_MGMT = 1;
3184 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
3185 case INTEL_FAM6_XEON_PHI_KNM:
3186 pkg_cstate_limits = phi_pkg_cstate_limits;
3188 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3189 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3190 case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */
3191 pkg_cstate_limits = glm_pkg_cstate_limits;
3196 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3197 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3199 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
3200 base_ratio = (msr >> 8) & 0xFF;
3202 base_hz = base_ratio * bclk * 1000000;
3207 * SLV client has support for unique MSRs:
3209 * MSR_CC6_DEMOTION_POLICY_CONFIG
3210 * MSR_MC6_DEMOTION_POLICY_CONFIG
3213 int has_slv_msrs(unsigned int family, unsigned int model)
3219 case INTEL_FAM6_ATOM_SILVERMONT:
3220 case INTEL_FAM6_ATOM_SILVERMONT_MID:
3221 case INTEL_FAM6_ATOM_AIRMONT_MID:
3226 int is_dnv(unsigned int family, unsigned int model)
3233 case INTEL_FAM6_ATOM_GOLDMONT_X:
3238 int is_bdx(unsigned int family, unsigned int model)
3245 case INTEL_FAM6_BROADWELL_X:
3246 case INTEL_FAM6_BROADWELL_XEON_D:
3251 int is_skx(unsigned int family, unsigned int model)
3258 case INTEL_FAM6_SKYLAKE_X:
3264 int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3266 if (has_slv_msrs(family, model))
3270 /* Nehalem compatible, but do not include turbo-ratio limit support */
3271 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3272 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
3273 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
3274 case INTEL_FAM6_XEON_PHI_KNM:
3280 int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3282 if (has_slv_msrs(family, model))
3287 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3296 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3297 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3303 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3312 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3319 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3328 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3329 case INTEL_FAM6_XEON_PHI_KNM:
3335 int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3344 case INTEL_FAM6_ATOM_GOLDMONT:
3345 case INTEL_FAM6_SKYLAKE_X:
3351 int has_config_tdp(unsigned int family, unsigned int model)
3360 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3361 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3362 case INTEL_FAM6_HASWELL_X: /* HSX */
3363 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3364 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3365 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3366 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3367 case INTEL_FAM6_BROADWELL_X: /* BDX */
3368 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3369 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3370 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3371 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3372 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3373 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
3374 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3376 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3377 case INTEL_FAM6_XEON_PHI_KNM:
3385 dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3387 if (!do_nhm_platform_info)
3390 dump_nhm_platform_info();
3392 if (has_hsw_turbo_ratio_limit(family, model))
3393 dump_hsw_turbo_ratio_limits();
3395 if (has_ivt_turbo_ratio_limit(family, model))
3396 dump_ivt_turbo_ratio_limits();
3398 if (has_turbo_ratio_limit(family, model))
3399 dump_turbo_ratio_limits(family, model);
3401 if (has_atom_turbo_ratio_limit(family, model))
3402 dump_atom_turbo_ratio_limits();
3404 if (has_knl_turbo_ratio_limit(family, model))
3405 dump_knl_turbo_ratio_limits();
3407 if (has_config_tdp(family, model))
3414 dump_sysfs_cstate_config(void)
3423 if (!DO_BIC(BIC_sysfs))
3426 for (state = 0; state < 10; ++state) {
3428 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3430 input = fopen(path, "r");
3433 fgets(name_buf, sizeof(name_buf), input);
3435 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3436 sp = strchr(name_buf, '-');
3438 sp = strchrnul(name_buf, '\n');
3443 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3445 input = fopen(path, "r");
3448 fgets(desc, sizeof(desc), input);
3450 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3455 dump_sysfs_pstate_config(void)
3458 char driver_buf[64];
3459 char governor_buf[64];
3463 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3465 input = fopen(path, "r");
3466 if (input == NULL) {
3467 fprintf(stderr, "NSFOD %s\n", path);
3470 fgets(driver_buf, sizeof(driver_buf), input);
3473 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3475 input = fopen(path, "r");
3476 if (input == NULL) {
3477 fprintf(stderr, "NSFOD %s\n", path);
3480 fgets(governor_buf, sizeof(governor_buf), input);
3483 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3484 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3486 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3487 input = fopen(path, "r");
3488 if (input != NULL) {
3489 fscanf(input, "%d", &turbo);
3490 fprintf(outf, "cpufreq boost: %d\n", turbo);
3494 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3495 input = fopen(path, "r");
3496 if (input != NULL) {
3497 fscanf(input, "%d", &turbo);
3498 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3506 * Decode the ENERGY_PERF_BIAS MSR
3508 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3510 unsigned long long msr;
3519 /* EPB is per-package */
3520 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3523 if (cpu_migrate(cpu)) {
3524 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3528 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3531 switch (msr & 0xF) {
3532 case ENERGY_PERF_BIAS_PERFORMANCE:
3533 epb_string = "performance";
3535 case ENERGY_PERF_BIAS_NORMAL:
3536 epb_string = "balanced";
3538 case ENERGY_PERF_BIAS_POWERSAVE:
3539 epb_string = "powersave";
3542 epb_string = "custom";
3545 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3551 * Decode the MSR_HWP_CAPABILITIES
3553 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3555 unsigned long long msr;
3563 /* MSR_HWP_CAPABILITIES is per-package */
3564 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3567 if (cpu_migrate(cpu)) {
3568 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3572 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3575 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
3576 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3578 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3579 if ((msr & (1 << 0)) == 0)
3582 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3585 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
3586 "(high %d guar %d eff %d low %d)\n",
3588 (unsigned int)HWP_HIGHEST_PERF(msr),
3589 (unsigned int)HWP_GUARANTEED_PERF(msr),
3590 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3591 (unsigned int)HWP_LOWEST_PERF(msr));
3593 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3596 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
3597 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
3599 (unsigned int)(((msr) >> 0) & 0xff),
3600 (unsigned int)(((msr) >> 8) & 0xff),
3601 (unsigned int)(((msr) >> 16) & 0xff),
3602 (unsigned int)(((msr) >> 24) & 0xff),
3603 (unsigned int)(((msr) >> 32) & 0xff3),
3604 (unsigned int)(((msr) >> 42) & 0x1));
3607 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3610 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
3611 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
3613 (unsigned int)(((msr) >> 0) & 0xff),
3614 (unsigned int)(((msr) >> 8) & 0xff),
3615 (unsigned int)(((msr) >> 16) & 0xff),
3616 (unsigned int)(((msr) >> 24) & 0xff),
3617 (unsigned int)(((msr) >> 32) & 0xff3));
3619 if (has_hwp_notify) {
3620 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3623 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
3624 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3626 ((msr) & 0x1) ? "EN" : "Dis",
3627 ((msr) & 0x2) ? "EN" : "Dis");
3629 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3632 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
3633 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3635 ((msr) & 0x1) ? "" : "No-",
3636 ((msr) & 0x2) ? "" : "No-");
3642 * print_perf_limit()
3644 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3646 unsigned long long msr;
3652 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3655 if (cpu_migrate(cpu)) {
3656 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3660 if (do_core_perf_limit_reasons) {
3661 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3662 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3663 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3664 (msr & 1 << 15) ? "bit15, " : "",
3665 (msr & 1 << 14) ? "bit14, " : "",
3666 (msr & 1 << 13) ? "Transitions, " : "",
3667 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3668 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3669 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3670 (msr & 1 << 9) ? "CorePwr, " : "",
3671 (msr & 1 << 8) ? "Amps, " : "",
3672 (msr & 1 << 6) ? "VR-Therm, " : "",
3673 (msr & 1 << 5) ? "Auto-HWP, " : "",
3674 (msr & 1 << 4) ? "Graphics, " : "",
3675 (msr & 1 << 2) ? "bit2, " : "",
3676 (msr & 1 << 1) ? "ThermStatus, " : "",
3677 (msr & 1 << 0) ? "PROCHOT, " : "");
3678 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3679 (msr & 1 << 31) ? "bit31, " : "",
3680 (msr & 1 << 30) ? "bit30, " : "",
3681 (msr & 1 << 29) ? "Transitions, " : "",
3682 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3683 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3684 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3685 (msr & 1 << 25) ? "CorePwr, " : "",
3686 (msr & 1 << 24) ? "Amps, " : "",
3687 (msr & 1 << 22) ? "VR-Therm, " : "",
3688 (msr & 1 << 21) ? "Auto-HWP, " : "",
3689 (msr & 1 << 20) ? "Graphics, " : "",
3690 (msr & 1 << 18) ? "bit18, " : "",
3691 (msr & 1 << 17) ? "ThermStatus, " : "",
3692 (msr & 1 << 16) ? "PROCHOT, " : "");
3695 if (do_gfx_perf_limit_reasons) {
3696 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3697 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3698 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
3699 (msr & 1 << 0) ? "PROCHOT, " : "",
3700 (msr & 1 << 1) ? "ThermStatus, " : "",
3701 (msr & 1 << 4) ? "Graphics, " : "",
3702 (msr & 1 << 6) ? "VR-Therm, " : "",
3703 (msr & 1 << 8) ? "Amps, " : "",
3704 (msr & 1 << 9) ? "GFXPwr, " : "",
3705 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3706 (msr & 1 << 11) ? "PkgPwrL2, " : "");
3707 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
3708 (msr & 1 << 16) ? "PROCHOT, " : "",
3709 (msr & 1 << 17) ? "ThermStatus, " : "",
3710 (msr & 1 << 20) ? "Graphics, " : "",
3711 (msr & 1 << 22) ? "VR-Therm, " : "",
3712 (msr & 1 << 24) ? "Amps, " : "",
3713 (msr & 1 << 25) ? "GFXPwr, " : "",
3714 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3715 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3717 if (do_ring_perf_limit_reasons) {
3718 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3719 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3720 fprintf(outf, " (Active: %s%s%s%s%s%s)",
3721 (msr & 1 << 0) ? "PROCHOT, " : "",
3722 (msr & 1 << 1) ? "ThermStatus, " : "",
3723 (msr & 1 << 6) ? "VR-Therm, " : "",
3724 (msr & 1 << 8) ? "Amps, " : "",
3725 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3726 (msr & 1 << 11) ? "PkgPwrL2, " : "");
3727 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
3728 (msr & 1 << 16) ? "PROCHOT, " : "",
3729 (msr & 1 << 17) ? "ThermStatus, " : "",
3730 (msr & 1 << 22) ? "VR-Therm, " : "",
3731 (msr & 1 << 24) ? "Amps, " : "",
3732 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3733 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3738 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3739 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3741 double get_tdp(unsigned int model)
3743 unsigned long long msr;
3745 if (do_rapl & RAPL_PKG_POWER_INFO)
3746 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3747 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3750 case INTEL_FAM6_ATOM_SILVERMONT:
3751 case INTEL_FAM6_ATOM_SILVERMONT_X:
3759 * rapl_dram_energy_units_probe()
3760 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3763 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3765 /* only called for genuine_intel, family 6 */
3768 case INTEL_FAM6_HASWELL_X: /* HSX */
3769 case INTEL_FAM6_BROADWELL_X: /* BDX */
3770 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3771 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
3772 case INTEL_FAM6_XEON_PHI_KNM:
3773 return (rapl_dram_energy_units = 15.3 / 1000000);
3775 return (rapl_energy_units);
3783 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3785 void rapl_probe(unsigned int family, unsigned int model)
3787 unsigned long long msr;
3788 unsigned int time_unit;
3798 case INTEL_FAM6_SANDYBRIDGE:
3799 case INTEL_FAM6_IVYBRIDGE:
3800 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3801 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3802 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3803 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3804 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3805 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3807 BIC_PRESENT(BIC_Pkg_J);
3808 BIC_PRESENT(BIC_Cor_J);
3809 BIC_PRESENT(BIC_GFX_J);
3811 BIC_PRESENT(BIC_PkgWatt);
3812 BIC_PRESENT(BIC_CorWatt);
3813 BIC_PRESENT(BIC_GFXWatt);
3816 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3817 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3818 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3820 BIC_PRESENT(BIC_Pkg_J);
3822 BIC_PRESENT(BIC_PkgWatt);
3824 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3825 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3826 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3827 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3828 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
3829 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO;
3830 BIC_PRESENT(BIC_PKG__);
3831 BIC_PRESENT(BIC_RAM__);
3833 BIC_PRESENT(BIC_Pkg_J);
3834 BIC_PRESENT(BIC_Cor_J);
3835 BIC_PRESENT(BIC_RAM_J);
3836 BIC_PRESENT(BIC_GFX_J);
3838 BIC_PRESENT(BIC_PkgWatt);
3839 BIC_PRESENT(BIC_CorWatt);
3840 BIC_PRESENT(BIC_RAMWatt);
3841 BIC_PRESENT(BIC_GFXWatt);
3844 case INTEL_FAM6_HASWELL_X: /* HSX */
3845 case INTEL_FAM6_BROADWELL_X: /* BDX */
3846 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3847 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3848 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
3849 case INTEL_FAM6_XEON_PHI_KNM:
3850 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3851 BIC_PRESENT(BIC_PKG__);
3852 BIC_PRESENT(BIC_RAM__);
3854 BIC_PRESENT(BIC_Pkg_J);
3855 BIC_PRESENT(BIC_RAM_J);
3857 BIC_PRESENT(BIC_PkgWatt);
3858 BIC_PRESENT(BIC_RAMWatt);
3861 case INTEL_FAM6_SANDYBRIDGE_X:
3862 case INTEL_FAM6_IVYBRIDGE_X:
3863 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;
3864 BIC_PRESENT(BIC_PKG__);
3865 BIC_PRESENT(BIC_RAM__);
3867 BIC_PRESENT(BIC_Pkg_J);
3868 BIC_PRESENT(BIC_Cor_J);
3869 BIC_PRESENT(BIC_RAM_J);
3871 BIC_PRESENT(BIC_PkgWatt);
3872 BIC_PRESENT(BIC_CorWatt);
3873 BIC_PRESENT(BIC_RAMWatt);
3876 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
3877 case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */
3878 do_rapl = RAPL_PKG | RAPL_CORES;
3880 BIC_PRESENT(BIC_Pkg_J);
3881 BIC_PRESENT(BIC_Cor_J);
3883 BIC_PRESENT(BIC_PkgWatt);
3884 BIC_PRESENT(BIC_CorWatt);
3887 case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */
3888 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;
3889 BIC_PRESENT(BIC_PKG__);
3890 BIC_PRESENT(BIC_RAM__);
3892 BIC_PRESENT(BIC_Pkg_J);
3893 BIC_PRESENT(BIC_Cor_J);
3894 BIC_PRESENT(BIC_RAM_J);
3896 BIC_PRESENT(BIC_PkgWatt);
3897 BIC_PRESENT(BIC_CorWatt);
3898 BIC_PRESENT(BIC_RAMWatt);
3905 /* units on package 0, verify later other packages match */
3906 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3909 rapl_power_units = 1.0 / (1 << (msr & 0xF));
3910 if (model == INTEL_FAM6_ATOM_SILVERMONT)
3911 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3913 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3915 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3917 time_unit = msr >> 16 & 0xF;
3921 rapl_time_units = 1.0 / (1 << (time_unit));
3923 tdp = get_tdp(model);
3925 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
3927 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3932 void perf_limit_reasons_probe(unsigned int family, unsigned int model)
3941 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3942 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3943 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3944 do_gfx_perf_limit_reasons = 1;
3945 case INTEL_FAM6_HASWELL_X: /* HSX */
3946 do_core_perf_limit_reasons = 1;
3947 do_ring_perf_limit_reasons = 1;
3953 void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3955 if (is_skx(family, model) || is_bdx(family, model))
3956 has_automatic_cstate_conversion = 1;
3959 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3961 unsigned long long msr;
3962 unsigned int dts, dts2;
3965 if (!(do_dts || do_ptm))
3970 /* DTS is per-core, no need to print for each thread */
3971 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3974 if (cpu_migrate(cpu)) {
3975 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3979 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3980 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3983 dts = (msr >> 16) & 0x7F;
3984 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3985 cpu, msr, tcc_activation_temp - dts);
3987 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3990 dts = (msr >> 16) & 0x7F;
3991 dts2 = (msr >> 8) & 0x7F;
3992 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3993 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3997 if (do_dts && debug) {
3998 unsigned int resolution;
4000 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4003 dts = (msr >> 16) & 0x7F;
4004 resolution = (msr >> 27) & 0xF;
4005 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4006 cpu, msr, tcc_activation_temp - dts, resolution);
4008 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4011 dts = (msr >> 16) & 0x7F;
4012 dts2 = (msr >> 8) & 0x7F;
4013 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4014 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4020 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4022 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4024 ((msr >> 15) & 1) ? "EN" : "DIS",
4025 ((msr >> 0) & 0x7FFF) * rapl_power_units,
4026 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4027 (((msr >> 16) & 1) ? "EN" : "DIS"));
4032 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4034 unsigned long long msr;
4040 /* RAPL counters are per package, so print only for 1st thread/package */
4041 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4045 if (cpu_migrate(cpu)) {
4046 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4050 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4053 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
4054 rapl_power_units, rapl_energy_units, rapl_time_units);
4056 if (do_rapl & RAPL_PKG_POWER_INFO) {
4058 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4062 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4064 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4065 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4066 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4067 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4070 if (do_rapl & RAPL_PKG) {
4072 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4075 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4076 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4078 print_power_limit_msr(cpu, msr, "PKG Limit #1");
4079 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4081 ((msr >> 47) & 1) ? "EN" : "DIS",
4082 ((msr >> 32) & 0x7FFF) * rapl_power_units,
4083 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4084 ((msr >> 48) & 1) ? "EN" : "DIS");
4087 if (do_rapl & RAPL_DRAM_POWER_INFO) {
4088 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4091 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4093 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4094 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4095 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4096 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4098 if (do_rapl & RAPL_DRAM) {
4099 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4101 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4102 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4104 print_power_limit_msr(cpu, msr, "DRAM Limit");
4106 if (do_rapl & RAPL_CORE_POLICY) {
4107 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4110 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4112 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4113 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4115 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4116 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4117 print_power_limit_msr(cpu, msr, "Cores Limit");
4119 if (do_rapl & RAPL_GFX) {
4120 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4123 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4125 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4127 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4128 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4129 print_power_limit_msr(cpu, msr, "GFX Limit");
4135 * SNB adds support for additional MSRs:
4137 * MSR_PKG_C7_RESIDENCY 0x000003fa
4138 * MSR_CORE_C7_RESIDENCY 0x000003fe
4139 * MSR_PKG_C2_RESIDENCY 0x0000060d
4142 int has_snb_msrs(unsigned int family, unsigned int model)
4148 case INTEL_FAM6_SANDYBRIDGE:
4149 case INTEL_FAM6_SANDYBRIDGE_X:
4150 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4151 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4152 case INTEL_FAM6_HASWELL_CORE: /* HSW */
4153 case INTEL_FAM6_HASWELL_X: /* HSW */
4154 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4155 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
4156 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4157 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
4158 case INTEL_FAM6_BROADWELL_X: /* BDX */
4159 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
4160 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4161 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4162 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4163 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
4164 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4165 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4166 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4167 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4168 case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */
4175 * HSW adds support for additional MSRs:
4177 * MSR_PKG_C8_RESIDENCY 0x00000630
4178 * MSR_PKG_C9_RESIDENCY 0x00000631
4179 * MSR_PKG_C10_RESIDENCY 0x00000632
4181 * MSR_PKGC8_IRTL 0x00000633
4182 * MSR_PKGC9_IRTL 0x00000634
4183 * MSR_PKGC10_IRTL 0x00000635
4186 int has_hsw_msrs(unsigned int family, unsigned int model)
4192 case INTEL_FAM6_HASWELL_ULT: /* HSW */
4193 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
4194 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4195 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4196 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4197 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
4198 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4199 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4200 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4207 * SKL adds support for additional MSRS:
4209 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4210 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4211 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4212 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4214 int has_skl_msrs(unsigned int family, unsigned int model)
4220 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4221 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4222 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4223 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
4224 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4230 int is_slm(unsigned int family, unsigned int model)
4235 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4236 case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */
4242 int is_knl(unsigned int family, unsigned int model)
4247 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4248 case INTEL_FAM6_XEON_PHI_KNM:
4254 int is_cnl(unsigned int family, unsigned int model)
4260 case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4267 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4269 if (is_knl(family, model))
4274 #define SLM_BCLK_FREQS 5
4275 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4277 double slm_bclk(void)
4279 unsigned long long msr = 3;
4283 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4284 fprintf(outf, "SLM BCLK: unknown\n");
4287 if (i >= SLM_BCLK_FREQS) {
4288 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
4291 freq = slm_freq_table[i];
4294 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4299 double discover_bclk(unsigned int family, unsigned int model)
4301 if (has_snb_msrs(family, model) || is_knl(family, model))
4303 else if (is_slm(family, model))
4310 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4311 * the Thermal Control Circuit (TCC) activates.
4312 * This is usually equal to tjMax.
4314 * Older processors do not have this MSR, so there we guess,
4315 * but also allow cmdline over-ride with -T.
4317 * Several MSR temperature values are in units of degrees-C
4318 * below this value, including the Digital Thermal Sensor (DTS),
4319 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4321 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4323 unsigned long long msr;
4324 unsigned int target_c_local;
4327 /* tcc_activation_temp is used only for dts or ptm */
4328 if (!(do_dts || do_ptm))
4331 /* this is a per-package concept */
4332 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4336 if (cpu_migrate(cpu)) {
4337 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4341 if (tcc_activation_temp_override != 0) {
4342 tcc_activation_temp = tcc_activation_temp_override;
4343 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4344 cpu, tcc_activation_temp);
4348 /* Temperature Target MSR is Nehalem and newer only */
4349 if (!do_nhm_platform_info)
4352 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4355 target_c_local = (msr >> 16) & 0xFF;
4358 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4359 cpu, msr, target_c_local);
4361 if (!target_c_local)
4364 tcc_activation_temp = target_c_local;
4369 tcc_activation_temp = TJMAX_DEFAULT;
4370 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4371 cpu, tcc_activation_temp);
4376 void decode_feature_control_msr(void)
4378 unsigned long long msr;
4380 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4381 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4383 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4384 msr & (1 << 18) ? "SGX" : "");
4387 void decode_misc_enable_msr(void)
4389 unsigned long long msr;
4394 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4395 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
4397 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4398 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4399 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4400 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4401 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
4404 void decode_misc_feature_control(void)
4406 unsigned long long msr;
4408 if (!has_misc_feature_control)
4411 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4412 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4414 msr & (0 << 0) ? "No-" : "",
4415 msr & (1 << 0) ? "No-" : "",
4416 msr & (2 << 0) ? "No-" : "",
4417 msr & (3 << 0) ? "No-" : "");
4420 * Decode MSR_MISC_PWR_MGMT
4422 * Decode the bits according to the Nehalem documentation
4423 * bit[0] seems to continue to have same meaning going forward
4426 void decode_misc_pwr_mgmt_msr(void)
4428 unsigned long long msr;
4430 if (!do_nhm_platform_info)
4433 if (no_MSR_MISC_PWR_MGMT)
4436 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4437 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4439 msr & (1 << 0) ? "DIS" : "EN",
4440 msr & (1 << 1) ? "EN" : "DIS",
4441 msr & (1 << 8) ? "EN" : "DIS");
4444 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4446 * This MSRs are present on Silvermont processors,
4447 * Intel Atom processor E3000 series (Baytrail), and friends.
4449 void decode_c6_demotion_policy_msr(void)
4451 unsigned long long msr;
4453 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4454 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4455 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4457 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4458 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4459 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4462 void process_cpuid()
4464 unsigned int eax, ebx, ecx, edx;
4465 unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
4466 unsigned int has_turbo;
4468 eax = ebx = ecx = edx = 0;
4470 __cpuid(0, max_level, ebx, ecx, edx);
4472 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
4474 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
4478 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4479 (char *)&ebx, (char *)&edx, (char *)&ecx);
4481 __cpuid(1, fms, ebx, ecx, edx);
4482 family = (fms >> 8) & 0xf;
4483 model = (fms >> 4) & 0xf;
4484 stepping = fms & 0xf;
4486 family += (fms >> 20) & 0xff;
4488 model += ((fms >> 16) & 0xf) << 4;
4493 * check max extended function levels of CPUID.
4494 * This is needed to check for invariant TSC.
4495 * This check is valid for both Intel and AMD.
4497 ebx = ecx = edx = 0;
4498 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4501 fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
4502 max_level, max_extended_level, family, model, stepping, family, model, stepping);
4503 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
4504 ecx_flags & (1 << 0) ? "SSE3" : "-",
4505 ecx_flags & (1 << 3) ? "MONITOR" : "-",
4506 ecx_flags & (1 << 6) ? "SMX" : "-",
4507 ecx_flags & (1 << 7) ? "EIST" : "-",
4508 ecx_flags & (1 << 8) ? "TM2" : "-",
4509 edx_flags & (1 << 4) ? "TSC" : "-",
4510 edx_flags & (1 << 5) ? "MSR" : "-",
4511 edx_flags & (1 << 22) ? "ACPI-TM" : "-",
4512 edx_flags & (1 << 28) ? "HT" : "-",
4513 edx_flags & (1 << 29) ? "TM" : "-");
4516 if (!(edx_flags & (1 << 5)))
4517 errx(1, "CPUID: no MSR");
4519 if (max_extended_level >= 0x80000007) {
4522 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4523 * this check is valid for both Intel and AMD
4525 __cpuid(0x80000007, eax, ebx, ecx, edx);
4526 has_invariant_tsc = edx & (1 << 8);
4530 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4531 * this check is valid for both Intel and AMD
4534 __cpuid(0x6, eax, ebx, ecx, edx);
4535 has_aperf = ecx & (1 << 0);
4537 BIC_PRESENT(BIC_Avg_MHz);
4538 BIC_PRESENT(BIC_Busy);
4539 BIC_PRESENT(BIC_Bzy_MHz);
4541 do_dts = eax & (1 << 0);
4543 BIC_PRESENT(BIC_CoreTmp);
4544 has_turbo = eax & (1 << 1);
4545 do_ptm = eax & (1 << 6);
4547 BIC_PRESENT(BIC_PkgTmp);
4548 has_hwp = eax & (1 << 7);
4549 has_hwp_notify = eax & (1 << 8);
4550 has_hwp_activity_window = eax & (1 << 9);
4551 has_hwp_epp = eax & (1 << 10);
4552 has_hwp_pkg = eax & (1 << 11);
4553 has_epb = ecx & (1 << 3);
4556 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
4557 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4558 has_aperf ? "" : "No-",
4559 has_turbo ? "" : "No-",
4560 do_dts ? "" : "No-",
4561 do_ptm ? "" : "No-",
4562 has_hwp ? "" : "No-",
4563 has_hwp_notify ? "" : "No-",
4564 has_hwp_activity_window ? "" : "No-",
4565 has_hwp_epp ? "" : "No-",
4566 has_hwp_pkg ? "" : "No-",
4567 has_epb ? "" : "No-");
4570 decode_misc_enable_msr();
4573 if (max_level >= 0x7 && !quiet) {
4578 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4580 has_sgx = ebx & (1 << 2);
4581 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4584 decode_feature_control_msr();
4587 if (max_level >= 0x15) {
4588 unsigned int eax_crystal;
4589 unsigned int ebx_tsc;
4592 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4594 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
4595 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
4599 if (!quiet && (ebx != 0))
4600 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
4601 eax_crystal, ebx_tsc, crystal_hz);
4603 if (crystal_hz == 0)
4605 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4606 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4607 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4608 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
4609 crystal_hz = 24000000; /* 24.0 MHz */
4611 case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */
4612 crystal_hz = 25000000; /* 25.0 MHz */
4614 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4615 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4616 crystal_hz = 19200000; /* 19.2 MHz */
4623 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
4625 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
4626 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4630 if (max_level >= 0x16) {
4631 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4634 * CPUID 16H Base MHz, Max MHz, Bus MHz
4636 base_mhz = max_mhz = bus_mhz = edx = 0;
4638 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
4640 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
4641 base_mhz, max_mhz, bus_mhz);
4645 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4647 BIC_PRESENT(BIC_IRQ);
4648 BIC_PRESENT(BIC_TSC_MHz);
4650 if (probe_nhm_msrs(family, model)) {
4651 do_nhm_platform_info = 1;
4652 BIC_PRESENT(BIC_CPU_c1);
4653 BIC_PRESENT(BIC_CPU_c3);
4654 BIC_PRESENT(BIC_CPU_c6);
4655 BIC_PRESENT(BIC_SMI);
4657 do_snb_cstates = has_snb_msrs(family, model);
4660 BIC_PRESENT(BIC_CPU_c7);
4662 do_irtl_snb = has_snb_msrs(family, model);
4663 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4664 BIC_PRESENT(BIC_Pkgpc2);
4665 if (pkg_cstate_limit >= PCL__3)
4666 BIC_PRESENT(BIC_Pkgpc3);
4667 if (pkg_cstate_limit >= PCL__6)
4668 BIC_PRESENT(BIC_Pkgpc6);
4669 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4670 BIC_PRESENT(BIC_Pkgpc7);
4671 if (has_slv_msrs(family, model)) {
4672 BIC_NOT_PRESENT(BIC_Pkgpc2);
4673 BIC_NOT_PRESENT(BIC_Pkgpc3);
4674 BIC_PRESENT(BIC_Pkgpc6);
4675 BIC_NOT_PRESENT(BIC_Pkgpc7);
4676 BIC_PRESENT(BIC_Mod_c6);
4677 use_c1_residency_msr = 1;
4679 if (is_dnv(family, model)) {
4680 BIC_PRESENT(BIC_CPU_c1);
4681 BIC_NOT_PRESENT(BIC_CPU_c3);
4682 BIC_NOT_PRESENT(BIC_Pkgpc3);
4683 BIC_NOT_PRESENT(BIC_CPU_c7);
4684 BIC_NOT_PRESENT(BIC_Pkgpc7);
4685 use_c1_residency_msr = 1;
4687 if (is_skx(family, model)) {
4688 BIC_NOT_PRESENT(BIC_CPU_c3);
4689 BIC_NOT_PRESENT(BIC_Pkgpc3);
4690 BIC_NOT_PRESENT(BIC_CPU_c7);
4691 BIC_NOT_PRESENT(BIC_Pkgpc7);
4693 if (is_bdx(family, model)) {
4694 BIC_NOT_PRESENT(BIC_CPU_c7);
4695 BIC_NOT_PRESENT(BIC_Pkgpc7);
4697 if (has_hsw_msrs(family, model)) {
4698 BIC_PRESENT(BIC_Pkgpc8);
4699 BIC_PRESENT(BIC_Pkgpc9);
4700 BIC_PRESENT(BIC_Pkgpc10);
4702 do_irtl_hsw = has_hsw_msrs(family, model);
4703 if (has_skl_msrs(family, model)) {
4704 BIC_PRESENT(BIC_Totl_c0);
4705 BIC_PRESENT(BIC_Any_c0);
4706 BIC_PRESENT(BIC_GFX_c0);
4707 BIC_PRESENT(BIC_CPUGFX);
4709 do_slm_cstates = is_slm(family, model);
4710 do_knl_cstates = is_knl(family, model);
4711 do_cnl_cstates = is_cnl(family, model);
4714 decode_misc_pwr_mgmt_msr();
4716 if (!quiet && has_slv_msrs(family, model))
4717 decode_c6_demotion_policy_msr();
4719 rapl_probe(family, model);
4720 perf_limit_reasons_probe(family, model);
4721 automatic_cstate_conversion_probe(family, model);
4724 dump_cstate_pstate_config_info(family, model);
4727 dump_sysfs_cstate_config();
4729 dump_sysfs_pstate_config();
4731 if (has_skl_msrs(family, model))
4732 calculate_tsc_tweak();
4734 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4735 BIC_PRESENT(BIC_GFX_rc6);
4737 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4738 BIC_PRESENT(BIC_GFXMHz);
4740 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4741 BIC_PRESENT(BIC_CPU_LPI);
4743 BIC_NOT_PRESENT(BIC_CPU_LPI);
4745 if (!access(sys_lpi_file_sysfs, R_OK)) {
4746 sys_lpi_file = sys_lpi_file_sysfs;
4747 BIC_PRESENT(BIC_SYS_LPI);
4748 } else if (!access(sys_lpi_file_debugfs, R_OK)) {
4749 sys_lpi_file = sys_lpi_file_debugfs;
4750 BIC_PRESENT(BIC_SYS_LPI);
4752 sys_lpi_file_sysfs = NULL;
4753 BIC_NOT_PRESENT(BIC_SYS_LPI);
4757 decode_misc_feature_control();
4763 * in /dev/cpu/ return success for names that are numbers
4764 * ie. filter out ".", "..", "microcode".
4766 int dir_filter(const struct dirent *dirp)
4768 if (isdigit(dirp->d_name[0]))
4774 int open_dev_cpu_msr(int dummy1)
4779 void topology_probe()
4782 int max_core_id = 0;
4783 int max_package_id = 0;
4784 int max_siblings = 0;
4786 /* Initialize num_cpus, max_cpu_num */
4789 for_all_proc_cpus(count_cpus);
4790 if (!summary_only && topo.num_cpus > 1)
4791 BIC_PRESENT(BIC_CPU);
4794 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4796 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
4798 err(1, "calloc cpus");
4801 * Allocate and initialize cpu_present_set
4803 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4804 if (cpu_present_set == NULL)
4805 err(3, "CPU_ALLOC");
4806 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4807 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4808 for_all_proc_cpus(mark_cpu_present);
4811 * Validate that all cpus in cpu_subset are also in cpu_present_set
4813 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4814 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4815 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4816 err(1, "cpu%d not present", i);
4820 * Allocate and initialize cpu_affinity_set
4822 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4823 if (cpu_affinity_set == NULL)
4824 err(3, "CPU_ALLOC");
4825 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4826 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4828 for_all_proc_cpus(init_thread_id);
4832 * find max_core_id, max_package_id
4834 for (i = 0; i <= topo.max_cpu_num; ++i) {
4837 if (cpu_is_not_present(i)) {
4839 fprintf(outf, "cpu%d NOT PRESENT\n", i);
4843 cpus[i].logical_cpu_id = i;
4845 /* get package information */
4846 cpus[i].physical_package_id = get_physical_package_id(i);
4847 if (cpus[i].physical_package_id > max_package_id)
4848 max_package_id = cpus[i].physical_package_id;
4850 /* get numa node information */
4851 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4852 if (cpus[i].physical_node_id > topo.max_node_num)
4853 topo.max_node_num = cpus[i].physical_node_id;
4855 /* get core information */
4856 cpus[i].physical_core_id = get_core_id(i);
4857 if (cpus[i].physical_core_id > max_core_id)
4858 max_core_id = cpus[i].physical_core_id;
4860 /* get thread information */
4861 siblings = get_thread_siblings(&cpus[i]);
4862 if (siblings > max_siblings)
4863 max_siblings = siblings;
4864 if (cpus[i].thread_id == 0)
4868 topo.cores_per_node = max_core_id + 1;
4870 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4871 max_core_id, topo.cores_per_node);
4872 if (!summary_only && topo.cores_per_node > 1)
4873 BIC_PRESENT(BIC_Core);
4875 topo.num_packages = max_package_id + 1;
4877 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4878 max_package_id, topo.num_packages);
4879 if (!summary_only && topo.num_packages > 1)
4880 BIC_PRESENT(BIC_Package);
4884 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
4885 if (!summary_only && topo.nodes_per_pkg > 1)
4886 BIC_PRESENT(BIC_Node);
4888 topo.threads_per_core = max_siblings;
4890 fprintf(outf, "max_siblings %d\n", max_siblings);
4895 for (i = 0; i <= topo.max_cpu_num; ++i) {
4897 "cpu %d pkg %d node %d lnode %d core %d thread %d\n",
4898 i, cpus[i].physical_package_id,
4899 cpus[i].physical_node_id,
4900 cpus[i].logical_node_id,
4901 cpus[i].physical_core_id,
4908 allocate_counters(struct thread_data **t, struct core_data **c,
4909 struct pkg_data **p)
4912 int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
4914 int num_threads = topo.threads_per_core * num_cores;
4916 *t = calloc(num_threads, sizeof(struct thread_data));
4920 for (i = 0; i < num_threads; i++)
4921 (*t)[i].cpu_id = -1;
4923 *c = calloc(num_cores, sizeof(struct core_data));
4927 for (i = 0; i < num_cores; i++)
4928 (*c)[i].core_id = -1;
4930 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
4934 for (i = 0; i < topo.num_packages; i++)
4935 (*p)[i].package_id = i;
4939 err(1, "calloc counters");
4944 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4946 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4947 struct pkg_data *pkg_base, int cpu_id)
4949 int pkg_id = cpus[cpu_id].physical_package_id;
4950 int node_id = cpus[cpu_id].logical_node_id;
4951 int core_id = cpus[cpu_id].physical_core_id;
4952 int thread_id = cpus[cpu_id].thread_id;
4953 struct thread_data *t;
4954 struct core_data *c;
4958 /* Workaround for systems where physical_node_id==-1
4959 * and logical_node_id==(-1 - topo.num_cpus)
4964 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
4965 c = GET_CORE(core_base, core_id, node_id, pkg_id);
4966 p = GET_PKG(pkg_base, pkg_id);
4969 if (thread_id == 0) {
4970 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4971 if (cpu_is_first_core_in_package(cpu_id))
4972 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4975 c->core_id = core_id;
4976 p->package_id = pkg_id;
4980 int initialize_counters(int cpu_id)
4982 init_counter(EVEN_COUNTERS, cpu_id);
4983 init_counter(ODD_COUNTERS, cpu_id);
4987 void allocate_output_buffer()
4989 output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
4990 outp = output_buffer;
4992 err(-1, "calloc output buffer");
4994 void allocate_fd_percpu(void)
4996 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4997 if (fd_percpu == NULL)
4998 err(-1, "calloc fd_percpu");
5000 void allocate_irq_buffers(void)
5002 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5003 if (irq_column_2_cpu == NULL)
5004 err(-1, "calloc %d", topo.num_cpus);
5006 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5007 if (irqs_per_cpu == NULL)
5008 err(-1, "calloc %d", topo.max_cpu_num + 1);
5010 void setup_all_buffers(void)
5013 allocate_irq_buffers();
5014 allocate_fd_percpu();
5015 allocate_counters(&thread_even, &core_even, &package_even);
5016 allocate_counters(&thread_odd, &core_odd, &package_odd);
5017 allocate_output_buffer();
5018 for_all_proc_cpus(initialize_counters);
5021 void set_base_cpu(void)
5023 base_cpu = sched_getcpu();
5025 err(-ENODEV, "No valid cpus found");
5028 fprintf(outf, "base_cpu = %d\n", base_cpu);
5031 void turbostat_init()
5033 setup_all_buffers();
5036 check_permissions();
5041 for_all_cpus(print_hwp, ODD_COUNTERS);
5044 for_all_cpus(print_epb, ODD_COUNTERS);
5047 for_all_cpus(print_perf_limit, ODD_COUNTERS);
5050 for_all_cpus(print_rapl, ODD_COUNTERS);
5052 for_all_cpus(set_temperature_target, ODD_COUNTERS);
5055 for_all_cpus(print_thermal, ODD_COUNTERS);
5057 if (!quiet && do_irtl_snb)
5061 int fork_it(char **argv)
5066 snapshot_proc_sysfs_files();
5067 status = for_all_cpus(get_counters, EVEN_COUNTERS);
5068 first_counter_read = 0;
5071 /* clear affinity side-effect of get_counters() */
5072 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5073 gettimeofday(&tv_even, (struct timezone *)NULL);
5078 execvp(argv[0], argv);
5079 err(errno, "exec %s", argv[0]);
5083 if (child_pid == -1)
5086 signal(SIGINT, SIG_IGN);
5087 signal(SIGQUIT, SIG_IGN);
5088 if (waitpid(child_pid, &status, 0) == -1)
5089 err(status, "waitpid");
5091 if (WIFEXITED(status))
5092 status = WEXITSTATUS(status);
5095 * n.b. fork_it() does not check for errors from for_all_cpus()
5096 * because re-starting is problematic when forking
5098 snapshot_proc_sysfs_files();
5099 for_all_cpus(get_counters, ODD_COUNTERS);
5100 gettimeofday(&tv_odd, (struct timezone *)NULL);
5101 timersub(&tv_odd, &tv_even, &tv_delta);
5102 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5103 fprintf(outf, "%s: Counter reset detected\n", progname);
5105 compute_average(EVEN_COUNTERS);
5106 format_all_counters(EVEN_COUNTERS);
5109 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5111 flush_output_stderr();
5116 int get_and_dump_counters(void)
5120 snapshot_proc_sysfs_files();
5121 status = for_all_cpus(get_counters, ODD_COUNTERS);
5125 status = for_all_cpus(dump_counters, ODD_COUNTERS);
5129 flush_output_stdout();
5134 void print_version() {
5135 fprintf(outf, "turbostat version 18.07.27"
5136 " - Len Brown <lenb@kernel.org>\n");
5139 int add_counter(unsigned int msr_num, char *path, char *name,
5140 unsigned int width, enum counter_scope scope,
5141 enum counter_type type, enum counter_format format, int flags)
5143 struct msr_counter *msrp;
5145 msrp = calloc(1, sizeof(struct msr_counter));
5151 msrp->msr_num = msr_num;
5152 strncpy(msrp->name, name, NAME_BYTES - 1);
5154 strncpy(msrp->path, path, PATH_BYTES - 1);
5155 msrp->width = width;
5157 msrp->format = format;
5158 msrp->flags = flags;
5163 msrp->next = sys.tp;
5165 sys.added_thread_counters++;
5166 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5167 fprintf(stderr, "exceeded max %d added thread counters\n",
5168 MAX_ADDED_COUNTERS);
5174 msrp->next = sys.cp;
5176 sys.added_core_counters++;
5177 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5178 fprintf(stderr, "exceeded max %d added core counters\n",
5179 MAX_ADDED_COUNTERS);
5185 msrp->next = sys.pp;
5187 sys.added_package_counters++;
5188 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5189 fprintf(stderr, "exceeded max %d added package counters\n",
5190 MAX_ADDED_COUNTERS);
5199 void parse_add_command(char *add_command)
5203 char name_buffer[NAME_BYTES] = "";
5206 enum counter_scope scope = SCOPE_CPU;
5207 enum counter_type type = COUNTER_CYCLES;
5208 enum counter_format format = FORMAT_DELTA;
5210 while (add_command) {
5212 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5215 if (sscanf(add_command, "msr%d", &msr_num) == 1)
5218 if (*add_command == '/') {
5223 if (sscanf(add_command, "u%d", &width) == 1) {
5224 if ((width == 32) || (width == 64))
5228 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5232 if (!strncmp(add_command, "core", strlen("core"))) {
5236 if (!strncmp(add_command, "package", strlen("package"))) {
5237 scope = SCOPE_PACKAGE;
5240 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5241 type = COUNTER_CYCLES;
5244 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5245 type = COUNTER_SECONDS;
5248 if (!strncmp(add_command, "usec", strlen("usec"))) {
5249 type = COUNTER_USEC;
5252 if (!strncmp(add_command, "raw", strlen("raw"))) {
5253 format = FORMAT_RAW;
5256 if (!strncmp(add_command, "delta", strlen("delta"))) {
5257 format = FORMAT_DELTA;
5260 if (!strncmp(add_command, "percent", strlen("percent"))) {
5261 format = FORMAT_PERCENT;
5265 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
5268 eos = strchr(name_buffer, ',');
5275 add_command = strchr(add_command, ',');
5277 *add_command = '\0';
5282 if ((msr_num == 0) && (path == NULL)) {
5283 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5287 /* generate default column header */
5288 if (*name_buffer == '\0') {
5290 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5292 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5295 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5304 int is_deferred_skip(char *name)
5308 for (i = 0; i < deferred_skip_index; ++i)
5309 if (!strcmp(name, deferred_skip_names[i]))
5314 void probe_sysfs(void)
5322 if (!DO_BIC(BIC_sysfs))
5325 for (state = 10; state >= 0; --state) {
5327 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5329 input = fopen(path, "r");
5332 fgets(name_buf, sizeof(name_buf), input);
5334 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5335 sp = strchr(name_buf, '-');
5337 sp = strchrnul(name_buf, '\n');
5343 sprintf(path, "cpuidle/state%d/time", state);
5345 if (is_deferred_skip(name_buf))
5348 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
5349 FORMAT_PERCENT, SYSFS_PERCPU);
5352 for (state = 10; state >= 0; --state) {
5354 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
5356 input = fopen(path, "r");
5359 fgets(name_buf, sizeof(name_buf), input);
5360 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
5361 sp = strchr(name_buf, '-');
5363 sp = strchrnul(name_buf, '\n');
5367 sprintf(path, "cpuidle/state%d/usage", state);
5369 if (is_deferred_skip(name_buf))
5372 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
5373 FORMAT_DELTA, SYSFS_PERCPU);
5380 * parse cpuset with following syntax
5381 * 1,2,4..6,8-10 and set bits in cpu_subset
5383 void parse_cpu_command(char *optarg)
5385 unsigned int start, end;
5388 if (!strcmp(optarg, "core")) {
5394 if (!strcmp(optarg, "package")) {
5400 if (show_core_only || show_pkg_only)
5403 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
5404 if (cpu_subset == NULL)
5405 err(3, "CPU_ALLOC");
5406 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
5408 CPU_ZERO_S(cpu_subset_size, cpu_subset);
5412 while (next && *next) {
5414 if (*next == '-') /* no negative cpu numbers */
5417 start = strtoul(next, &next, 10);
5419 if (start >= CPU_SUBSET_MAXCPUS)
5421 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5432 next += 1; /* start range */
5433 } else if (*next == '.') {
5436 next += 1; /* start range */
5441 end = strtoul(next, &next, 10);
5445 while (++start <= end) {
5446 if (start >= CPU_SUBSET_MAXCPUS)
5448 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5453 else if (*next != '\0')
5460 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5466 void cmdline(int argc, char **argv)
5469 int option_index = 0;
5470 static struct option long_options[] = {
5471 {"add", required_argument, 0, 'a'},
5472 {"cpu", required_argument, 0, 'c'},
5473 {"Dump", no_argument, 0, 'D'},
5474 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
5475 {"enable", required_argument, 0, 'e'},
5476 {"interval", required_argument, 0, 'i'},
5477 {"num_iterations", required_argument, 0, 'n'},
5478 {"help", no_argument, 0, 'h'},
5479 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
5480 {"Joules", no_argument, 0, 'J'},
5481 {"list", no_argument, 0, 'l'},
5482 {"out", required_argument, 0, 'o'},
5483 {"quiet", no_argument, 0, 'q'},
5484 {"show", required_argument, 0, 's'},
5485 {"Summary", no_argument, 0, 'S'},
5486 {"TCC", required_argument, 0, 'T'},
5487 {"version", no_argument, 0, 'v' },
5493 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5494 long_options, &option_index)) != -1) {
5497 parse_add_command(optarg);
5500 parse_cpu_command(optarg);
5506 /* --enable specified counter */
5507 bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
5511 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5515 * --hide: do not show those specified
5516 * multiple invocations simply clear more bits in enabled mask
5518 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5526 double interval = strtod(optarg, NULL);
5528 if (interval < 0.001) {
5529 fprintf(outf, "interval %f seconds is too small\n",
5534 interval_tv.tv_sec = interval_ts.tv_sec = interval;
5535 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
5536 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
5543 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5548 outf = fopen_or_die(optarg, "w");
5554 num_iterations = strtod(optarg, NULL);
5556 if (num_iterations <= 0) {
5557 fprintf(outf, "iterations %d should be positive number\n",
5564 * --show: show only those specified
5565 * The 1st invocation will clear and replace the enabled mask
5566 * subsequent invocations can add to it.
5569 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5571 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5578 tcc_activation_temp_override = atoi(optarg);
5588 int main(int argc, char **argv)
5591 cmdline(argc, argv);
5600 /* dump counters and exit */
5602 return get_and_dump_counters();
5604 /* list header and exit */
5605 if (list_header_only) {
5607 flush_output_stdout();
5612 * if any params left, it must be a command to fork
5615 return fork_it(argv + optind);