1 // SPDX-License-Identifier: GPL-2.0-only
3 * turbostat -- show CPU frequency and C-state residency
4 * on modern Intel and AMD processors.
6 * Copyright (c) 2013 Intel Corporation.
7 * Len Brown <len.brown@intel.com>
12 #include INTEL_FAMILY_HEADER
17 #include <sys/types.h>
20 #include <sys/select.h>
21 #include <sys/resource.h>
33 #include <sys/capability.h>
36 #include <linux/perf_event.h>
37 #include <asm/unistd.h>
40 char *proc_stat = "/proc/stat";
43 int *fd_instr_count_percpu;
44 struct timeval interval_tv = {5, 0};
45 struct timespec interval_ts = {5, 0};
47 /* Save original CPU model */
48 unsigned int model_orig;
50 unsigned int num_iterations;
54 unsigned int sums_need_wide_columns;
55 unsigned int rapl_joules;
56 unsigned int summary_only;
57 unsigned int list_header_only;
58 unsigned int dump_only;
59 unsigned int do_snb_cstates;
60 unsigned int do_knl_cstates;
61 unsigned int do_slm_cstates;
62 unsigned int use_c1_residency_msr;
63 unsigned int has_aperf;
65 unsigned int do_irtl_snb;
66 unsigned int do_irtl_hsw;
67 unsigned int units = 1000000; /* MHz etc */
68 unsigned int genuine_intel;
69 unsigned int authentic_amd;
70 unsigned int hygon_genuine;
71 unsigned int max_level, max_extended_level;
72 unsigned int has_invariant_tsc;
73 unsigned int do_nhm_platform_info;
74 unsigned int no_MSR_MISC_PWR_MGMT;
75 unsigned int aperf_mperf_multiplier = 1;
78 unsigned int has_base_hz;
79 double tsc_tweak = 1.0;
80 unsigned int show_pkg_only;
81 unsigned int show_core_only;
82 char *output_buffer, *outp;
87 unsigned long long gfx_cur_rc6_ms;
88 unsigned long long cpuidle_cur_cpu_lpi_us;
89 unsigned long long cpuidle_cur_sys_lpi_us;
90 unsigned int gfx_cur_mhz;
91 unsigned int gfx_act_mhz;
92 unsigned int tcc_activation_temp;
93 unsigned int tcc_activation_temp_override;
95 double rapl_power_units, rapl_time_units;
96 double rapl_dram_energy_units, rapl_energy_units;
97 double rapl_joule_counter_range;
98 unsigned int do_core_perf_limit_reasons;
99 unsigned int has_automatic_cstate_conversion;
100 unsigned int dis_cstate_prewake;
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;
117 #define RAPL_PKG (1 << 0)
118 /* 0x610 MSR_PKG_POWER_LIMIT */
119 /* 0x611 MSR_PKG_ENERGY_STATUS */
120 #define RAPL_PKG_PERF_STATUS (1 << 1)
121 /* 0x613 MSR_PKG_PERF_STATUS */
122 #define RAPL_PKG_POWER_INFO (1 << 2)
123 /* 0x614 MSR_PKG_POWER_INFO */
125 #define RAPL_DRAM (1 << 3)
126 /* 0x618 MSR_DRAM_POWER_LIMIT */
127 /* 0x619 MSR_DRAM_ENERGY_STATUS */
128 #define RAPL_DRAM_PERF_STATUS (1 << 4)
129 /* 0x61b MSR_DRAM_PERF_STATUS */
130 #define RAPL_DRAM_POWER_INFO (1 << 5)
131 /* 0x61c MSR_DRAM_POWER_INFO */
133 #define RAPL_CORES_POWER_LIMIT (1 << 6)
134 /* 0x638 MSR_PP0_POWER_LIMIT */
135 #define RAPL_CORE_POLICY (1 << 7)
136 /* 0x63a MSR_PP0_POLICY */
138 #define RAPL_GFX (1 << 8)
139 /* 0x640 MSR_PP1_POWER_LIMIT */
140 /* 0x641 MSR_PP1_ENERGY_STATUS */
141 /* 0x642 MSR_PP1_POLICY */
143 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
144 /* 0x639 MSR_PP0_ENERGY_STATUS */
145 #define RAPL_PER_CORE_ENERGY (1 << 10)
146 /* Indicates cores energy collection is per-core,
147 * not per-package. */
148 #define RAPL_AMD_F17H (1 << 11)
149 /* 0xc0010299 MSR_RAPL_PWR_UNIT */
150 /* 0xc001029a MSR_CORE_ENERGY_STAT */
151 /* 0xc001029b MSR_PKG_ENERGY_STAT */
152 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
153 #define TJMAX_DEFAULT 100
155 /* MSRs that are not yet in the kernel-provided header. */
156 #define MSR_RAPL_PWR_UNIT 0xc0010299
157 #define MSR_CORE_ENERGY_STAT 0xc001029a
158 #define MSR_PKG_ENERGY_STAT 0xc001029b
160 #define MAX(a, b) ((a) > (b) ? (a) : (b))
163 * buffer size used by sscanf() for added column names
164 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
166 #define NAME_BYTES 20
167 #define PATH_BYTES 128
172 #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
173 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
174 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
175 #define MAX_ADDED_COUNTERS 8
176 #define MAX_ADDED_THREAD_COUNTERS 24
177 #define BITMASK_SIZE 32
180 struct timeval tv_begin;
181 struct timeval tv_end;
182 struct timeval tv_delta;
183 unsigned long long tsc;
184 unsigned long long aperf;
185 unsigned long long mperf;
186 unsigned long long c1;
187 unsigned long long instr_count;
188 unsigned long long irq_count;
189 unsigned int smi_count;
191 unsigned int apic_id;
192 unsigned int x2apic_id;
195 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
196 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
197 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
198 } *thread_even, *thread_odd;
201 unsigned long long c3;
202 unsigned long long c6;
203 unsigned long long c7;
204 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
205 unsigned int core_temp_c;
206 unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */
207 unsigned int core_id;
208 unsigned long long counter[MAX_ADDED_COUNTERS];
209 } *core_even, *core_odd;
212 unsigned long long pc2;
213 unsigned long long pc3;
214 unsigned long long pc6;
215 unsigned long long pc7;
216 unsigned long long pc8;
217 unsigned long long pc9;
218 unsigned long long pc10;
219 unsigned long long cpu_lpi;
220 unsigned long long sys_lpi;
221 unsigned long long pkg_wtd_core_c0;
222 unsigned long long pkg_any_core_c0;
223 unsigned long long pkg_any_gfxe_c0;
224 unsigned long long pkg_both_core_gfxe_c0;
225 long long gfx_rc6_ms;
226 unsigned int gfx_mhz;
227 unsigned int gfx_act_mhz;
228 unsigned int package_id;
229 unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */
230 unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */
231 unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */
232 unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */
233 unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
234 unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
235 unsigned int pkg_temp_c;
236 unsigned long long counter[MAX_ADDED_COUNTERS];
237 } *package_even, *package_odd;
239 #define ODD_COUNTERS thread_odd, core_odd, package_odd
240 #define EVEN_COUNTERS thread_even, core_even, package_even
242 #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
245 topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
246 ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
247 ((core_no) * topo.threads_per_core) + \
250 #define GET_CORE(core_base, core_no, node_no, pkg_no) \
252 ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
253 ((node_no) * topo.cores_per_node) + \
257 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
259 enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
260 enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
261 enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
264 unsigned int msr_num;
265 char name[NAME_BYTES];
266 char path[PATH_BYTES];
268 enum counter_type type;
269 enum counter_format format;
270 struct msr_counter *next;
272 #define FLAGS_HIDE (1 << 0)
273 #define FLAGS_SHOW (1 << 1)
274 #define SYSFS_PERCPU (1 << 1)
278 * The accumulated sum of MSR is defined as a monotonic
279 * increasing MSR, it will be accumulated periodically,
280 * despite its register's bit width.
292 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr);
294 struct msr_sum_array {
295 /* get_msr_sum() = sum + (get_msr() - last) */
297 /*The accumulated MSR value is updated by the timer*/
298 unsigned long long sum;
299 /*The MSR footprint recorded in last timer*/
300 unsigned long long last;
301 } entries[IDX_COUNT];
304 /* The percpu MSR sum array.*/
305 struct msr_sum_array *per_cpu_msr_sum;
307 off_t idx_to_offset(int idx)
313 if (do_rapl & RAPL_AMD_F17H)
314 offset = MSR_PKG_ENERGY_STAT;
316 offset = MSR_PKG_ENERGY_STATUS;
318 case IDX_DRAM_ENERGY:
319 offset = MSR_DRAM_ENERGY_STATUS;
322 offset = MSR_PP0_ENERGY_STATUS;
325 offset = MSR_PP1_ENERGY_STATUS;
328 offset = MSR_PKG_PERF_STATUS;
331 offset = MSR_DRAM_PERF_STATUS;
339 int offset_to_idx(off_t offset)
344 case MSR_PKG_ENERGY_STATUS:
345 case MSR_PKG_ENERGY_STAT:
346 idx = IDX_PKG_ENERGY;
348 case MSR_DRAM_ENERGY_STATUS:
349 idx = IDX_DRAM_ENERGY;
351 case MSR_PP0_ENERGY_STATUS:
352 idx = IDX_PP0_ENERGY;
354 case MSR_PP1_ENERGY_STATUS:
355 idx = IDX_PP1_ENERGY;
357 case MSR_PKG_PERF_STATUS:
360 case MSR_DRAM_PERF_STATUS:
369 int idx_valid(int idx)
373 return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
374 case IDX_DRAM_ENERGY:
375 return do_rapl & RAPL_DRAM;
377 return do_rapl & RAPL_CORES_ENERGY_STATUS;
379 return do_rapl & RAPL_GFX;
381 return do_rapl & RAPL_PKG_PERF_STATUS;
383 return do_rapl & RAPL_DRAM_PERF_STATUS;
388 struct sys_counters {
389 unsigned int added_thread_counters;
390 unsigned int added_core_counters;
391 unsigned int added_package_counters;
392 struct msr_counter *tp;
393 struct msr_counter *cp;
394 struct msr_counter *pp;
397 struct system_summary {
398 struct thread_data threads;
399 struct core_data cores;
400 struct pkg_data packages;
403 struct cpu_topology {
404 int physical_package_id;
407 int physical_node_id;
408 int logical_node_id; /* 0-based count within the package */
409 int physical_core_id;
411 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
423 int threads_per_core;
426 struct timeval tv_even, tv_odd, tv_delta;
428 int *irq_column_2_cpu; /* /proc/interrupts column numbers */
429 int *irqs_per_cpu; /* indexed by cpu_num */
431 void setup_all_buffers(void);
434 char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
435 char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
437 int cpu_is_not_present(int cpu)
439 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
442 * run func(thread, core, package) in topology order
443 * skip non-present cpus
446 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
447 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
449 int retval, pkg_no, core_no, thread_no, node_no;
451 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
452 for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
453 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
454 for (thread_no = 0; thread_no <
455 topo.threads_per_core; ++thread_no) {
456 struct thread_data *t;
460 t = GET_THREAD(thread_base, thread_no,
464 if (cpu_is_not_present(t->cpu_id))
467 c = GET_CORE(core_base, core_no,
469 p = GET_PKG(pkg_base, pkg_no);
471 retval = func(t, c, p);
481 int cpu_migrate(int cpu)
483 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
484 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
485 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
490 int get_msr_fd(int cpu)
500 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
501 fd = open(pathname, O_RDONLY);
503 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
510 static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
512 return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
515 static int perf_instr_count_open(int cpu_num)
517 struct perf_event_attr pea;
520 memset(&pea, 0, sizeof(struct perf_event_attr));
521 pea.type = PERF_TYPE_HARDWARE;
522 pea.size = sizeof(struct perf_event_attr);
523 pea.config = PERF_COUNT_HW_INSTRUCTIONS;
525 /* counter for cpu_num, including user + kernel and all processes */
526 fd = perf_event_open(&pea, -1, cpu_num, -1, 0);
528 err(-1, "cpu%d: perf instruction counter\n", cpu_num);
533 int get_instr_count_fd(int cpu)
535 if (fd_instr_count_percpu[cpu])
536 return fd_instr_count_percpu[cpu];
538 fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu);
540 return fd_instr_count_percpu[cpu];
543 int get_msr(int cpu, off_t offset, unsigned long long *msr)
547 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
549 if (retval != sizeof *msr)
550 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
556 * This list matches the column headers, except
557 * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
558 * 2. Core and CPU are moved to the end, we can't have strings that contain them
559 * matching on them for --show and --hide.
561 struct msr_counter bic[] = {
563 { 0x0, "Time_Of_Day_Seconds" },
571 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
617 #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
618 #define BIC_USEC (1ULL << 0)
619 #define BIC_TOD (1ULL << 1)
620 #define BIC_Package (1ULL << 2)
621 #define BIC_Node (1ULL << 3)
622 #define BIC_Avg_MHz (1ULL << 4)
623 #define BIC_Busy (1ULL << 5)
624 #define BIC_Bzy_MHz (1ULL << 6)
625 #define BIC_TSC_MHz (1ULL << 7)
626 #define BIC_IRQ (1ULL << 8)
627 #define BIC_SMI (1ULL << 9)
628 #define BIC_sysfs (1ULL << 10)
629 #define BIC_CPU_c1 (1ULL << 11)
630 #define BIC_CPU_c3 (1ULL << 12)
631 #define BIC_CPU_c6 (1ULL << 13)
632 #define BIC_CPU_c7 (1ULL << 14)
633 #define BIC_ThreadC (1ULL << 15)
634 #define BIC_CoreTmp (1ULL << 16)
635 #define BIC_CoreCnt (1ULL << 17)
636 #define BIC_PkgTmp (1ULL << 18)
637 #define BIC_GFX_rc6 (1ULL << 19)
638 #define BIC_GFXMHz (1ULL << 20)
639 #define BIC_Pkgpc2 (1ULL << 21)
640 #define BIC_Pkgpc3 (1ULL << 22)
641 #define BIC_Pkgpc6 (1ULL << 23)
642 #define BIC_Pkgpc7 (1ULL << 24)
643 #define BIC_Pkgpc8 (1ULL << 25)
644 #define BIC_Pkgpc9 (1ULL << 26)
645 #define BIC_Pkgpc10 (1ULL << 27)
646 #define BIC_CPU_LPI (1ULL << 28)
647 #define BIC_SYS_LPI (1ULL << 29)
648 #define BIC_PkgWatt (1ULL << 30)
649 #define BIC_CorWatt (1ULL << 31)
650 #define BIC_GFXWatt (1ULL << 32)
651 #define BIC_PkgCnt (1ULL << 33)
652 #define BIC_RAMWatt (1ULL << 34)
653 #define BIC_PKG__ (1ULL << 35)
654 #define BIC_RAM__ (1ULL << 36)
655 #define BIC_Pkg_J (1ULL << 37)
656 #define BIC_Cor_J (1ULL << 38)
657 #define BIC_GFX_J (1ULL << 39)
658 #define BIC_RAM_J (1ULL << 40)
659 #define BIC_Mod_c6 (1ULL << 41)
660 #define BIC_Totl_c0 (1ULL << 42)
661 #define BIC_Any_c0 (1ULL << 43)
662 #define BIC_GFX_c0 (1ULL << 44)
663 #define BIC_CPUGFX (1ULL << 45)
664 #define BIC_Core (1ULL << 46)
665 #define BIC_CPU (1ULL << 47)
666 #define BIC_APIC (1ULL << 48)
667 #define BIC_X2APIC (1ULL << 49)
668 #define BIC_Die (1ULL << 50)
669 #define BIC_GFXACTMHz (1ULL << 51)
670 #define BIC_IPC (1ULL << 52)
672 #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
674 unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
675 unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
677 #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
678 #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
679 #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
680 #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
681 #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
682 #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT)
685 #define MAX_DEFERRED 16
686 char *deferred_skip_names[MAX_DEFERRED];
687 int deferred_skip_index;
690 * HIDE_LIST - hide this list of counters, show the rest [default]
691 * SHOW_LIST - show this list of counters, hide the rest
693 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
698 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
700 "Turbostat forks the specified COMMAND and prints statistics\n"
701 "when COMMAND completes.\n"
702 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
703 "to print statistics, until interrupted.\n"
704 " -a, --add add a counter\n"
705 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
706 " -c, --cpu cpu-set limit output to summary plus cpu-set:\n"
707 " {core | package | j,k,l..m,n-p }\n"
708 " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n"
709 " -D, --Dump displays the raw counter values\n"
710 " -e, --enable [all | column]\n"
711 " shows all or the specified disabled column\n"
712 " -H, --hide [column|column,column,...]\n"
713 " hide the specified column(s)\n"
714 " -i, --interval sec.subsec\n"
715 " Override default 5-second measurement interval\n"
716 " -J, --Joules displays energy in Joules instead of Watts\n"
717 " -l, --list list column headers only\n"
718 " -n, --num_iterations num\n"
719 " number of the measurement iterations\n"
721 " create or truncate \"file\" for all output\n"
722 " -q, --quiet skip decoding system configuration header\n"
723 " -s, --show [column|column,column,...]\n"
724 " show only the specified column(s)\n"
726 " limits output to 1-line system summary per interval\n"
727 " -T, --TCC temperature\n"
728 " sets the Thermal Control Circuit temperature in\n"
730 " -h, --help print this help message\n"
731 " -v, --version print version information\n"
733 "For more help, run \"man turbostat\"\n");
738 * for all the strings in comma separate name_list,
739 * set the approprate bit in return value.
741 unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
744 unsigned long long retval = 0;
749 comma = strchr(name_list, ',');
754 if (!strcmp(name_list, "all"))
757 for (i = 0; i < MAX_BIC; ++i) {
758 if (!strcmp(name_list, bic[i].name)) {
759 retval |= (1ULL << i);
764 if (mode == SHOW_LIST) {
765 fprintf(stderr, "Invalid counter name: %s\n", name_list);
768 deferred_skip_names[deferred_skip_index++] = name_list;
770 fprintf(stderr, "deferred \"%s\"\n", name_list);
771 if (deferred_skip_index >= MAX_DEFERRED) {
772 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
773 MAX_DEFERRED, name_list);
788 void print_header(char *delim)
790 struct msr_counter *mp;
793 if (DO_BIC(BIC_USEC))
794 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
796 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
797 if (DO_BIC(BIC_Package))
798 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
800 outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
801 if (DO_BIC(BIC_Node))
802 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
803 if (DO_BIC(BIC_Core))
804 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
806 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
807 if (DO_BIC(BIC_APIC))
808 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
809 if (DO_BIC(BIC_X2APIC))
810 outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
811 if (DO_BIC(BIC_Avg_MHz))
812 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
813 if (DO_BIC(BIC_Busy))
814 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
815 if (DO_BIC(BIC_Bzy_MHz))
816 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
817 if (DO_BIC(BIC_TSC_MHz))
818 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
821 outp += sprintf(outp, "%sIPC", (printed++ ? delim : ""));
823 if (DO_BIC(BIC_IRQ)) {
824 if (sums_need_wide_columns)
825 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
827 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
831 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
833 for (mp = sys.tp; mp; mp = mp->next) {
835 if (mp->format == FORMAT_RAW) {
837 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
839 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
841 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
842 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
844 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
848 if (DO_BIC(BIC_CPU_c1))
849 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
850 if (DO_BIC(BIC_CPU_c3))
851 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
852 if (DO_BIC(BIC_CPU_c6))
853 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
854 if (DO_BIC(BIC_CPU_c7))
855 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
857 if (DO_BIC(BIC_Mod_c6))
858 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
860 if (DO_BIC(BIC_CoreTmp))
861 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
863 if (do_rapl && !rapl_joules) {
864 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
865 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
866 } else if (do_rapl && rapl_joules) {
867 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
868 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
871 for (mp = sys.cp; mp; mp = mp->next) {
872 if (mp->format == FORMAT_RAW) {
874 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
876 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
878 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
879 outp += sprintf(outp, "%s%8s", delim, mp->name);
881 outp += sprintf(outp, "%s%s", delim, mp->name);
885 if (DO_BIC(BIC_PkgTmp))
886 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
888 if (DO_BIC(BIC_GFX_rc6))
889 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
891 if (DO_BIC(BIC_GFXMHz))
892 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
894 if (DO_BIC(BIC_GFXACTMHz))
895 outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : ""));
897 if (DO_BIC(BIC_Totl_c0))
898 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
899 if (DO_BIC(BIC_Any_c0))
900 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
901 if (DO_BIC(BIC_GFX_c0))
902 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
903 if (DO_BIC(BIC_CPUGFX))
904 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
906 if (DO_BIC(BIC_Pkgpc2))
907 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
908 if (DO_BIC(BIC_Pkgpc3))
909 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
910 if (DO_BIC(BIC_Pkgpc6))
911 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
912 if (DO_BIC(BIC_Pkgpc7))
913 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
914 if (DO_BIC(BIC_Pkgpc8))
915 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
916 if (DO_BIC(BIC_Pkgpc9))
917 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
918 if (DO_BIC(BIC_Pkgpc10))
919 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
920 if (DO_BIC(BIC_CPU_LPI))
921 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
922 if (DO_BIC(BIC_SYS_LPI))
923 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
925 if (do_rapl && !rapl_joules) {
926 if (DO_BIC(BIC_PkgWatt))
927 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
928 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
929 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
930 if (DO_BIC(BIC_GFXWatt))
931 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
932 if (DO_BIC(BIC_RAMWatt))
933 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
934 if (DO_BIC(BIC_PKG__))
935 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
936 if (DO_BIC(BIC_RAM__))
937 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
938 } else if (do_rapl && rapl_joules) {
939 if (DO_BIC(BIC_Pkg_J))
940 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
941 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
942 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
943 if (DO_BIC(BIC_GFX_J))
944 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
945 if (DO_BIC(BIC_RAM_J))
946 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
947 if (DO_BIC(BIC_PKG__))
948 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
949 if (DO_BIC(BIC_RAM__))
950 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
952 for (mp = sys.pp; mp; mp = mp->next) {
953 if (mp->format == FORMAT_RAW) {
955 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
957 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
959 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
960 outp += sprintf(outp, "%s%8s", delim, mp->name);
962 outp += sprintf(outp, "%s%s", delim, mp->name);
966 outp += sprintf(outp, "\n");
969 int dump_counters(struct thread_data *t, struct core_data *c,
973 struct msr_counter *mp;
975 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
978 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
979 t->cpu_id, t->flags);
980 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
981 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
982 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
983 outp += sprintf(outp, "c1: %016llX\n", t->c1);
986 outp += sprintf(outp, "IPC: %lld\n", t->instr_count);
989 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
991 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
993 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
994 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
995 i, mp->msr_num, t->counter[i]);
1000 outp += sprintf(outp, "core: %d\n", c->core_id);
1001 outp += sprintf(outp, "c3: %016llX\n", c->c3);
1002 outp += sprintf(outp, "c6: %016llX\n", c->c6);
1003 outp += sprintf(outp, "c7: %016llX\n", c->c7);
1004 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
1005 outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
1007 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1008 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
1009 i, mp->msr_num, c->counter[i]);
1011 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
1015 outp += sprintf(outp, "package: %d\n", p->package_id);
1017 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
1018 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
1019 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
1020 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
1022 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
1023 if (DO_BIC(BIC_Pkgpc3))
1024 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
1025 if (DO_BIC(BIC_Pkgpc6))
1026 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
1027 if (DO_BIC(BIC_Pkgpc7))
1028 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
1029 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
1030 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
1031 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
1032 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
1033 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
1034 outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
1035 outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
1036 outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
1037 outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
1038 outp += sprintf(outp, "Throttle PKG: %0llX\n",
1039 p->rapl_pkg_perf_status);
1040 outp += sprintf(outp, "Throttle RAM: %0llX\n",
1041 p->rapl_dram_perf_status);
1042 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
1044 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1045 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
1046 i, mp->msr_num, p->counter[i]);
1050 outp += sprintf(outp, "\n");
1056 * column formatting convention & formats
1058 int format_counters(struct thread_data *t, struct core_data *c,
1061 double interval_float, tsc;
1064 struct msr_counter *mp;
1068 /* if showing only 1st thread in core and this isn't one, bail out */
1069 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1072 /* if showing only 1st thread in pkg and this isn't one, bail out */
1073 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1076 /*if not summary line and --cpu is used */
1077 if ((t != &average.threads) &&
1078 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
1081 if (DO_BIC(BIC_USEC)) {
1082 /* on each row, print how many usec each timestamp took to gather */
1085 timersub(&t->tv_end, &t->tv_begin, &tv);
1086 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
1089 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
1090 if (DO_BIC(BIC_TOD))
1091 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
1093 interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0;
1095 tsc = t->tsc * tsc_tweak;
1097 /* topo columns, print blanks on 1st (average) line */
1098 if (t == &average.threads) {
1099 if (DO_BIC(BIC_Package))
1100 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1101 if (DO_BIC(BIC_Die))
1102 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1103 if (DO_BIC(BIC_Node))
1104 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1105 if (DO_BIC(BIC_Core))
1106 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1107 if (DO_BIC(BIC_CPU))
1108 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1109 if (DO_BIC(BIC_APIC))
1110 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1111 if (DO_BIC(BIC_X2APIC))
1112 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1114 if (DO_BIC(BIC_Package)) {
1116 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
1118 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1120 if (DO_BIC(BIC_Die)) {
1122 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
1124 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1126 if (DO_BIC(BIC_Node)) {
1128 outp += sprintf(outp, "%s%d",
1129 (printed++ ? delim : ""),
1130 cpus[t->cpu_id].physical_node_id);
1132 outp += sprintf(outp, "%s-",
1133 (printed++ ? delim : ""));
1135 if (DO_BIC(BIC_Core)) {
1137 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
1139 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1141 if (DO_BIC(BIC_CPU))
1142 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
1143 if (DO_BIC(BIC_APIC))
1144 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
1145 if (DO_BIC(BIC_X2APIC))
1146 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
1149 if (DO_BIC(BIC_Avg_MHz))
1150 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1151 1.0 / units * t->aperf / interval_float);
1153 if (DO_BIC(BIC_Busy))
1154 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
1156 if (DO_BIC(BIC_Bzy_MHz)) {
1158 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
1160 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1161 tsc / units * t->aperf / t->mperf / interval_float);
1164 if (DO_BIC(BIC_TSC_MHz))
1165 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
1167 if (DO_BIC(BIC_IPC))
1168 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf);
1171 if (DO_BIC(BIC_IRQ)) {
1172 if (sums_need_wide_columns)
1173 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
1175 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
1179 if (DO_BIC(BIC_SMI))
1180 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
1182 /* Added counters */
1183 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1184 if (mp->format == FORMAT_RAW) {
1185 if (mp->width == 32)
1186 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
1188 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1189 } else if (mp->format == FORMAT_DELTA) {
1190 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1191 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
1193 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1194 } else if (mp->format == FORMAT_PERCENT) {
1195 if (mp->type == COUNTER_USEC)
1196 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
1198 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
1203 if (DO_BIC(BIC_CPU_c1))
1204 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
1207 /* print per-core data only for 1st thread in core */
1208 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1211 if (DO_BIC(BIC_CPU_c3))
1212 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1213 if (DO_BIC(BIC_CPU_c6))
1214 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1215 if (DO_BIC(BIC_CPU_c7))
1216 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1219 if (DO_BIC(BIC_Mod_c6))
1220 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
1222 if (DO_BIC(BIC_CoreTmp))
1223 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1225 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1226 if (mp->format == FORMAT_RAW) {
1227 if (mp->width == 32)
1228 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1230 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1231 } else if (mp->format == FORMAT_DELTA) {
1232 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1233 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
1235 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1236 } else if (mp->format == FORMAT_PERCENT) {
1237 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1243 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
1244 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
1245 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
1246 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
1248 /* print per-package data only for 1st core in package */
1249 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1253 if (DO_BIC(BIC_PkgTmp))
1254 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1257 if (DO_BIC(BIC_GFX_rc6)) {
1258 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
1259 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
1261 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
1262 p->gfx_rc6_ms / 10.0 / interval_float);
1267 if (DO_BIC(BIC_GFXMHz))
1268 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
1271 if (DO_BIC(BIC_GFXACTMHz))
1272 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz);
1274 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1275 if (DO_BIC(BIC_Totl_c0))
1276 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1277 if (DO_BIC(BIC_Any_c0))
1278 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1279 if (DO_BIC(BIC_GFX_c0))
1280 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1281 if (DO_BIC(BIC_CPUGFX))
1282 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
1284 if (DO_BIC(BIC_Pkgpc2))
1285 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
1286 if (DO_BIC(BIC_Pkgpc3))
1287 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
1288 if (DO_BIC(BIC_Pkgpc6))
1289 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
1290 if (DO_BIC(BIC_Pkgpc7))
1291 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
1292 if (DO_BIC(BIC_Pkgpc8))
1293 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
1294 if (DO_BIC(BIC_Pkgpc9))
1295 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
1296 if (DO_BIC(BIC_Pkgpc10))
1297 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1299 if (DO_BIC(BIC_CPU_LPI))
1300 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1301 if (DO_BIC(BIC_SYS_LPI))
1302 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1304 if (DO_BIC(BIC_PkgWatt))
1305 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1306 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1307 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1308 if (DO_BIC(BIC_GFXWatt))
1309 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1310 if (DO_BIC(BIC_RAMWatt))
1311 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1312 if (DO_BIC(BIC_Pkg_J))
1313 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1314 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1315 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1316 if (DO_BIC(BIC_GFX_J))
1317 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1318 if (DO_BIC(BIC_RAM_J))
1319 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1320 if (DO_BIC(BIC_PKG__))
1321 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1322 if (DO_BIC(BIC_RAM__))
1323 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1325 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1326 if (mp->format == FORMAT_RAW) {
1327 if (mp->width == 32)
1328 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1330 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1331 } else if (mp->format == FORMAT_DELTA) {
1332 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1333 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
1335 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1336 } else if (mp->format == FORMAT_PERCENT) {
1337 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1342 if (*(outp - 1) != '\n')
1343 outp += sprintf(outp, "\n");
1348 void flush_output_stdout(void)
1357 fputs(output_buffer, filep);
1360 outp = output_buffer;
1362 void flush_output_stderr(void)
1364 fputs(output_buffer, outf);
1366 outp = output_buffer;
1368 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1372 if (!printed || !summary_only)
1375 format_counters(&average.threads, &average.cores, &average.packages);
1382 for_all_cpus(format_counters, t, c, p);
1385 #define DELTA_WRAP32(new, old) \
1386 old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1389 delta_package(struct pkg_data *new, struct pkg_data *old)
1392 struct msr_counter *mp;
1395 if (DO_BIC(BIC_Totl_c0))
1396 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1397 if (DO_BIC(BIC_Any_c0))
1398 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1399 if (DO_BIC(BIC_GFX_c0))
1400 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1401 if (DO_BIC(BIC_CPUGFX))
1402 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1404 old->pc2 = new->pc2 - old->pc2;
1405 if (DO_BIC(BIC_Pkgpc3))
1406 old->pc3 = new->pc3 - old->pc3;
1407 if (DO_BIC(BIC_Pkgpc6))
1408 old->pc6 = new->pc6 - old->pc6;
1409 if (DO_BIC(BIC_Pkgpc7))
1410 old->pc7 = new->pc7 - old->pc7;
1411 old->pc8 = new->pc8 - old->pc8;
1412 old->pc9 = new->pc9 - old->pc9;
1413 old->pc10 = new->pc10 - old->pc10;
1414 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1415 old->sys_lpi = new->sys_lpi - old->sys_lpi;
1416 old->pkg_temp_c = new->pkg_temp_c;
1418 /* flag an error when rc6 counter resets/wraps */
1419 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1420 old->gfx_rc6_ms = -1;
1422 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1424 old->gfx_mhz = new->gfx_mhz;
1425 old->gfx_act_mhz = new->gfx_act_mhz;
1427 old->energy_pkg = new->energy_pkg - old->energy_pkg;
1428 old->energy_cores = new->energy_cores - old->energy_cores;
1429 old->energy_gfx = new->energy_gfx - old->energy_gfx;
1430 old->energy_dram = new->energy_dram - old->energy_dram;
1431 old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status;
1432 old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status;
1434 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1435 if (mp->format == FORMAT_RAW)
1436 old->counter[i] = new->counter[i];
1438 old->counter[i] = new->counter[i] - old->counter[i];
1445 delta_core(struct core_data *new, struct core_data *old)
1448 struct msr_counter *mp;
1450 old->c3 = new->c3 - old->c3;
1451 old->c6 = new->c6 - old->c6;
1452 old->c7 = new->c7 - old->c7;
1453 old->core_temp_c = new->core_temp_c;
1454 old->mc6_us = new->mc6_us - old->mc6_us;
1456 DELTA_WRAP32(new->core_energy, old->core_energy);
1458 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1459 if (mp->format == FORMAT_RAW)
1460 old->counter[i] = new->counter[i];
1462 old->counter[i] = new->counter[i] - old->counter[i];
1466 int soft_c1_residency_display(int bic)
1468 if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
1471 return DO_BIC_READ(bic);
1478 delta_thread(struct thread_data *new, struct thread_data *old,
1479 struct core_data *core_delta)
1482 struct msr_counter *mp;
1484 /* we run cpuid just the 1st time, copy the results */
1485 if (DO_BIC(BIC_APIC))
1486 new->apic_id = old->apic_id;
1487 if (DO_BIC(BIC_X2APIC))
1488 new->x2apic_id = old->x2apic_id;
1491 * the timestamps from start of measurement interval are in "old"
1492 * the timestamp from end of measurement interval are in "new"
1493 * over-write old w/ new so we can print end of interval values
1496 timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
1497 old->tv_begin = new->tv_begin;
1498 old->tv_end = new->tv_end;
1500 old->tsc = new->tsc - old->tsc;
1502 /* check for TSC < 1 Mcycles over interval */
1503 if (old->tsc < (1000 * 1000))
1504 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1505 "You can disable all c-states by booting with \"idle=poll\"\n"
1506 "or just the deep ones with \"processor.max_cstate=1\"");
1508 old->c1 = new->c1 - old->c1;
1510 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
1511 soft_c1_residency_display(BIC_Avg_MHz)) {
1512 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1513 old->aperf = new->aperf - old->aperf;
1514 old->mperf = new->mperf - old->mperf;
1521 if (use_c1_residency_msr) {
1523 * Some models have a dedicated C1 residency MSR,
1524 * which should be more accurate than the derivation below.
1528 * As counter collection is not atomic,
1529 * it is possible for mperf's non-halted cycles + idle states
1530 * to exceed TSC's all cycles: show c1 = 0% in that case.
1532 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1535 /* normal case, derive c1 */
1536 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1537 - core_delta->c6 - core_delta->c7;
1541 if (old->mperf == 0) {
1543 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1544 old->mperf = 1; /* divide by 0 protection */
1547 if (DO_BIC(BIC_IPC))
1548 old->instr_count = new->instr_count - old->instr_count;
1550 if (DO_BIC(BIC_IRQ))
1551 old->irq_count = new->irq_count - old->irq_count;
1553 if (DO_BIC(BIC_SMI))
1554 old->smi_count = new->smi_count - old->smi_count;
1556 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1557 if (mp->format == FORMAT_RAW)
1558 old->counter[i] = new->counter[i];
1560 old->counter[i] = new->counter[i] - old->counter[i];
1565 int delta_cpu(struct thread_data *t, struct core_data *c,
1566 struct pkg_data *p, struct thread_data *t2,
1567 struct core_data *c2, struct pkg_data *p2)
1571 /* calculate core delta only for 1st thread in core */
1572 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1575 /* always calculate thread delta */
1576 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1580 /* calculate package delta only for 1st core in package */
1581 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1582 retval = delta_package(p, p2);
1587 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1590 struct msr_counter *mp;
1592 t->tv_begin.tv_sec = 0;
1593 t->tv_begin.tv_usec = 0;
1594 t->tv_end.tv_sec = 0;
1595 t->tv_end.tv_usec = 0;
1596 t->tv_delta.tv_sec = 0;
1597 t->tv_delta.tv_usec = 0;
1609 /* tells format_counters to dump all fields from this set */
1610 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1619 p->pkg_wtd_core_c0 = 0;
1620 p->pkg_any_core_c0 = 0;
1621 p->pkg_any_gfxe_c0 = 0;
1622 p->pkg_both_core_gfxe_c0 = 0;
1625 if (DO_BIC(BIC_Pkgpc3))
1627 if (DO_BIC(BIC_Pkgpc6))
1629 if (DO_BIC(BIC_Pkgpc7))
1639 p->energy_cores = 0;
1641 p->rapl_pkg_perf_status = 0;
1642 p->rapl_dram_perf_status = 0;
1648 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1651 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1654 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1657 int sum_counters(struct thread_data *t, struct core_data *c,
1661 struct msr_counter *mp;
1663 /* copy un-changing apic_id's */
1664 if (DO_BIC(BIC_APIC))
1665 average.threads.apic_id = t->apic_id;
1666 if (DO_BIC(BIC_X2APIC))
1667 average.threads.x2apic_id = t->x2apic_id;
1669 /* remember first tv_begin */
1670 if (average.threads.tv_begin.tv_sec == 0)
1671 average.threads.tv_begin = t->tv_begin;
1673 /* remember last tv_end */
1674 average.threads.tv_end = t->tv_end;
1676 average.threads.tsc += t->tsc;
1677 average.threads.aperf += t->aperf;
1678 average.threads.mperf += t->mperf;
1679 average.threads.c1 += t->c1;
1681 average.threads.instr_count += t->instr_count;
1683 average.threads.irq_count += t->irq_count;
1684 average.threads.smi_count += t->smi_count;
1686 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1687 if (mp->format == FORMAT_RAW)
1689 average.threads.counter[i] += t->counter[i];
1692 /* sum per-core values only for 1st thread in core */
1693 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1696 average.cores.c3 += c->c3;
1697 average.cores.c6 += c->c6;
1698 average.cores.c7 += c->c7;
1699 average.cores.mc6_us += c->mc6_us;
1701 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1703 average.cores.core_energy += c->core_energy;
1705 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1706 if (mp->format == FORMAT_RAW)
1708 average.cores.counter[i] += c->counter[i];
1711 /* sum per-pkg values only for 1st core in pkg */
1712 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1715 if (DO_BIC(BIC_Totl_c0))
1716 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1717 if (DO_BIC(BIC_Any_c0))
1718 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1719 if (DO_BIC(BIC_GFX_c0))
1720 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1721 if (DO_BIC(BIC_CPUGFX))
1722 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1724 average.packages.pc2 += p->pc2;
1725 if (DO_BIC(BIC_Pkgpc3))
1726 average.packages.pc3 += p->pc3;
1727 if (DO_BIC(BIC_Pkgpc6))
1728 average.packages.pc6 += p->pc6;
1729 if (DO_BIC(BIC_Pkgpc7))
1730 average.packages.pc7 += p->pc7;
1731 average.packages.pc8 += p->pc8;
1732 average.packages.pc9 += p->pc9;
1733 average.packages.pc10 += p->pc10;
1735 average.packages.cpu_lpi = p->cpu_lpi;
1736 average.packages.sys_lpi = p->sys_lpi;
1738 average.packages.energy_pkg += p->energy_pkg;
1739 average.packages.energy_dram += p->energy_dram;
1740 average.packages.energy_cores += p->energy_cores;
1741 average.packages.energy_gfx += p->energy_gfx;
1743 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1744 average.packages.gfx_mhz = p->gfx_mhz;
1745 average.packages.gfx_act_mhz = p->gfx_act_mhz;
1747 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1749 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1750 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1752 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1753 if (mp->format == FORMAT_RAW)
1755 average.packages.counter[i] += p->counter[i];
1760 * sum the counters for all cpus in the system
1761 * compute the weighted average
1763 void compute_average(struct thread_data *t, struct core_data *c,
1767 struct msr_counter *mp;
1769 clear_counters(&average.threads, &average.cores, &average.packages);
1771 for_all_cpus(sum_counters, t, c, p);
1773 /* Use the global time delta for the average. */
1774 average.threads.tv_delta = tv_delta;
1776 average.threads.tsc /= topo.num_cpus;
1777 average.threads.aperf /= topo.num_cpus;
1778 average.threads.mperf /= topo.num_cpus;
1779 average.threads.instr_count /= topo.num_cpus;
1780 average.threads.c1 /= topo.num_cpus;
1782 if (average.threads.irq_count > 9999999)
1783 sums_need_wide_columns = 1;
1785 average.cores.c3 /= topo.num_cores;
1786 average.cores.c6 /= topo.num_cores;
1787 average.cores.c7 /= topo.num_cores;
1788 average.cores.mc6_us /= topo.num_cores;
1790 if (DO_BIC(BIC_Totl_c0))
1791 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1792 if (DO_BIC(BIC_Any_c0))
1793 average.packages.pkg_any_core_c0 /= topo.num_packages;
1794 if (DO_BIC(BIC_GFX_c0))
1795 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1796 if (DO_BIC(BIC_CPUGFX))
1797 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1799 average.packages.pc2 /= topo.num_packages;
1800 if (DO_BIC(BIC_Pkgpc3))
1801 average.packages.pc3 /= topo.num_packages;
1802 if (DO_BIC(BIC_Pkgpc6))
1803 average.packages.pc6 /= topo.num_packages;
1804 if (DO_BIC(BIC_Pkgpc7))
1805 average.packages.pc7 /= topo.num_packages;
1807 average.packages.pc8 /= topo.num_packages;
1808 average.packages.pc9 /= topo.num_packages;
1809 average.packages.pc10 /= topo.num_packages;
1811 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1812 if (mp->format == FORMAT_RAW)
1814 if (mp->type == COUNTER_ITEMS) {
1815 if (average.threads.counter[i] > 9999999)
1816 sums_need_wide_columns = 1;
1819 average.threads.counter[i] /= topo.num_cpus;
1821 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1822 if (mp->format == FORMAT_RAW)
1824 if (mp->type == COUNTER_ITEMS) {
1825 if (average.cores.counter[i] > 9999999)
1826 sums_need_wide_columns = 1;
1828 average.cores.counter[i] /= topo.num_cores;
1830 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1831 if (mp->format == FORMAT_RAW)
1833 if (mp->type == COUNTER_ITEMS) {
1834 if (average.packages.counter[i] > 9999999)
1835 sums_need_wide_columns = 1;
1837 average.packages.counter[i] /= topo.num_packages;
1841 static unsigned long long rdtsc(void)
1843 unsigned int low, high;
1845 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1847 return low | ((unsigned long long)high) << 32;
1851 * Open a file, and exit on failure
1853 FILE *fopen_or_die(const char *path, const char *mode)
1855 FILE *filep = fopen(path, mode);
1858 err(1, "%s: open failed", path);
1862 * snapshot_sysfs_counter()
1864 * return snapshot of given counter
1866 unsigned long long snapshot_sysfs_counter(char *path)
1870 unsigned long long counter;
1872 fp = fopen_or_die(path, "r");
1874 retval = fscanf(fp, "%lld", &counter);
1876 err(1, "snapshot_sysfs_counter(%s)", path);
1883 int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1885 if (mp->msr_num != 0) {
1886 if (get_msr(cpu, mp->msr_num, counterp))
1889 char path[128 + PATH_BYTES];
1891 if (mp->flags & SYSFS_PERCPU) {
1892 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1895 *counterp = snapshot_sysfs_counter(path);
1897 *counterp = snapshot_sysfs_counter(mp->path);
1904 int get_epb(int cpu)
1906 char path[128 + PATH_BYTES];
1907 unsigned long long msr;
1911 sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu);
1913 fp = fopen(path, "r");
1917 ret = fscanf(fp, "%d", &epb);
1919 err(1, "%s(%s)", __func__, path);
1926 get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr);
1931 void get_apic_id(struct thread_data *t)
1933 unsigned int eax, ebx, ecx, edx;
1935 if (DO_BIC(BIC_APIC)) {
1936 eax = ebx = ecx = edx = 0;
1937 __cpuid(1, eax, ebx, ecx, edx);
1939 t->apic_id = (ebx >> 24) & 0xff;
1942 if (!DO_BIC(BIC_X2APIC))
1945 if (authentic_amd || hygon_genuine) {
1946 unsigned int topology_extensions;
1948 if (max_extended_level < 0x8000001e)
1951 eax = ebx = ecx = edx = 0;
1952 __cpuid(0x80000001, eax, ebx, ecx, edx);
1953 topology_extensions = ecx & (1 << 22);
1955 if (topology_extensions == 0)
1958 eax = ebx = ecx = edx = 0;
1959 __cpuid(0x8000001e, eax, ebx, ecx, edx);
1968 if (max_level < 0xb)
1972 __cpuid(0xb, eax, ebx, ecx, edx);
1975 if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
1976 fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
1977 t->cpu_id, t->apic_id, t->x2apic_id);
1983 * acquire and record local counters for that cpu
1985 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1987 int cpu = t->cpu_id;
1988 unsigned long long msr;
1989 int aperf_mperf_retry_count = 0;
1990 struct msr_counter *mp;
1993 if (cpu_migrate(cpu)) {
1994 fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu);
1998 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
2000 if (first_counter_read)
2003 t->tsc = rdtsc(); /* we are running on local CPU of interest */
2005 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
2006 soft_c1_residency_display(BIC_Avg_MHz)) {
2007 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
2010 * The TSC, APERF and MPERF must be read together for
2011 * APERF/MPERF and MPERF/TSC to give accurate results.
2013 * Unfortunately, APERF and MPERF are read by
2014 * individual system call, so delays may occur
2015 * between them. If the time to read them
2016 * varies by a large amount, we re-read them.
2020 * This initial dummy APERF read has been seen to
2021 * reduce jitter in the subsequent reads.
2024 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2027 t->tsc = rdtsc(); /* re-read close to APERF */
2029 tsc_before = t->tsc;
2031 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2034 tsc_between = rdtsc();
2036 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
2039 tsc_after = rdtsc();
2041 aperf_time = tsc_between - tsc_before;
2042 mperf_time = tsc_after - tsc_between;
2045 * If the system call latency to read APERF and MPERF
2046 * differ by more than 2x, then try again.
2048 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
2049 aperf_mperf_retry_count++;
2050 if (aperf_mperf_retry_count < 5)
2053 warnx("cpu%d jitter %lld %lld",
2054 cpu, aperf_time, mperf_time);
2056 aperf_mperf_retry_count = 0;
2058 t->aperf = t->aperf * aperf_mperf_multiplier;
2059 t->mperf = t->mperf * aperf_mperf_multiplier;
2062 if (DO_BIC(BIC_IPC))
2063 if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long))
2066 if (DO_BIC(BIC_IRQ))
2067 t->irq_count = irqs_per_cpu[cpu];
2068 if (DO_BIC(BIC_SMI)) {
2069 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
2071 t->smi_count = msr & 0xFFFFFFFF;
2073 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
2074 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
2078 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2079 if (get_mp(cpu, mp, &t->counter[i]))
2083 /* collect core counters only for 1st thread in core */
2084 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2087 if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
2088 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
2092 if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
2093 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
2095 } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) {
2096 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
2100 if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) {
2101 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
2103 else if (t->is_atom) {
2105 * For Atom CPUs that has core cstate deeper than c6,
2106 * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper.
2107 * Minus CC7 (and deeper cstates) residency to get
2108 * accturate cc6 residency.
2114 if (DO_BIC(BIC_Mod_c6))
2115 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
2118 if (DO_BIC(BIC_CoreTmp)) {
2119 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2121 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
2124 if (do_rapl & RAPL_AMD_F17H) {
2125 if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
2127 c->core_energy = msr & 0xFFFFFFFF;
2130 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2131 if (get_mp(cpu, mp, &c->counter[i]))
2135 /* collect package counters only for 1st core in package */
2136 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2139 if (DO_BIC(BIC_Totl_c0)) {
2140 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
2143 if (DO_BIC(BIC_Any_c0)) {
2144 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
2147 if (DO_BIC(BIC_GFX_c0)) {
2148 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
2151 if (DO_BIC(BIC_CPUGFX)) {
2152 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
2155 if (DO_BIC(BIC_Pkgpc3))
2156 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
2158 if (DO_BIC(BIC_Pkgpc6)) {
2159 if (do_slm_cstates) {
2160 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
2163 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
2168 if (DO_BIC(BIC_Pkgpc2))
2169 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
2171 if (DO_BIC(BIC_Pkgpc7))
2172 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
2174 if (DO_BIC(BIC_Pkgpc8))
2175 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
2177 if (DO_BIC(BIC_Pkgpc9))
2178 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
2180 if (DO_BIC(BIC_Pkgpc10))
2181 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
2184 if (DO_BIC(BIC_CPU_LPI))
2185 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
2186 if (DO_BIC(BIC_SYS_LPI))
2187 p->sys_lpi = cpuidle_cur_sys_lpi_us;
2189 if (do_rapl & RAPL_PKG) {
2190 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr))
2192 p->energy_pkg = msr;
2194 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
2195 if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr))
2197 p->energy_cores = msr;
2199 if (do_rapl & RAPL_DRAM) {
2200 if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
2202 p->energy_dram = msr;
2204 if (do_rapl & RAPL_GFX) {
2205 if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr))
2207 p->energy_gfx = msr;
2209 if (do_rapl & RAPL_PKG_PERF_STATUS) {
2210 if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr))
2212 p->rapl_pkg_perf_status = msr;
2214 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
2215 if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr))
2217 p->rapl_dram_perf_status = msr;
2219 if (do_rapl & RAPL_AMD_F17H) {
2220 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr))
2222 p->energy_pkg = msr;
2224 if (DO_BIC(BIC_PkgTmp)) {
2225 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2227 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
2230 if (DO_BIC(BIC_GFX_rc6))
2231 p->gfx_rc6_ms = gfx_cur_rc6_ms;
2233 if (DO_BIC(BIC_GFXMHz))
2234 p->gfx_mhz = gfx_cur_mhz;
2236 if (DO_BIC(BIC_GFXACTMHz))
2237 p->gfx_act_mhz = gfx_act_mhz;
2239 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2240 if (get_mp(cpu, mp, &p->counter[i]))
2244 gettimeofday(&t->tv_end, (struct timezone *)NULL);
2250 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2251 * If you change the values, note they are used both in comparisons
2252 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2255 #define PCLUKN 0 /* Unknown */
2256 #define PCLRSV 1 /* Reserved */
2257 #define PCL__0 2 /* PC0 */
2258 #define PCL__1 3 /* PC1 */
2259 #define PCL__2 4 /* PC2 */
2260 #define PCL__3 5 /* PC3 */
2261 #define PCL__4 6 /* PC4 */
2262 #define PCL__6 7 /* PC6 */
2263 #define PCL_6N 8 /* PC6 No Retention */
2264 #define PCL_6R 9 /* PC6 Retention */
2265 #define PCL__7 10 /* PC7 */
2266 #define PCL_7S 11 /* PC7 Shrink */
2267 #define PCL__8 12 /* PC8 */
2268 #define PCL__9 13 /* PC9 */
2269 #define PCL_10 14 /* PC10 */
2270 #define PCLUNL 15 /* Unlimited */
2272 int pkg_cstate_limit = PCLUKN;
2273 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2274 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"};
2276 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};
2277 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};
2278 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};
2279 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};
2280 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};
2281 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};
2282 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};
2283 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};
2284 int icx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
2287 calculate_tsc_tweak()
2289 tsc_tweak = base_hz / tsc_hz;
2292 void prewake_cstate_probe(unsigned int family, unsigned int model);
2295 dump_nhm_platform_info(void)
2297 unsigned long long msr;
2300 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2302 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
2304 ratio = (msr >> 40) & 0xFF;
2305 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2306 ratio, bclk, ratio * bclk);
2308 ratio = (msr >> 8) & 0xFF;
2309 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2310 ratio, bclk, ratio * bclk);
2312 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2313 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2314 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
2316 /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */
2317 if (dis_cstate_prewake)
2318 fprintf(outf, "C-state Pre-wake: %sabled\n",
2319 msr & 0x40000000 ? "DIS" : "EN");
2325 dump_hsw_turbo_ratio_limits(void)
2327 unsigned long long msr;
2330 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2332 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2334 ratio = (msr >> 8) & 0xFF;
2336 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2337 ratio, bclk, ratio * bclk);
2339 ratio = (msr >> 0) & 0xFF;
2341 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2342 ratio, bclk, ratio * bclk);
2347 dump_ivt_turbo_ratio_limits(void)
2349 unsigned long long msr;
2352 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
2354 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
2356 ratio = (msr >> 56) & 0xFF;
2358 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
2359 ratio, bclk, ratio * bclk);
2361 ratio = (msr >> 48) & 0xFF;
2363 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
2364 ratio, bclk, ratio * bclk);
2366 ratio = (msr >> 40) & 0xFF;
2368 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
2369 ratio, bclk, ratio * bclk);
2371 ratio = (msr >> 32) & 0xFF;
2373 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
2374 ratio, bclk, ratio * bclk);
2376 ratio = (msr >> 24) & 0xFF;
2378 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
2379 ratio, bclk, ratio * bclk);
2381 ratio = (msr >> 16) & 0xFF;
2383 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
2384 ratio, bclk, ratio * bclk);
2386 ratio = (msr >> 8) & 0xFF;
2388 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
2389 ratio, bclk, ratio * bclk);
2391 ratio = (msr >> 0) & 0xFF;
2393 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
2394 ratio, bclk, ratio * bclk);
2397 int has_turbo_ratio_group_limits(int family, int model)
2404 case INTEL_FAM6_ATOM_GOLDMONT:
2405 case INTEL_FAM6_SKYLAKE_X:
2406 case INTEL_FAM6_ICELAKE_X:
2407 case INTEL_FAM6_ATOM_GOLDMONT_D:
2408 case INTEL_FAM6_ATOM_TREMONT_D:
2415 dump_turbo_ratio_limits(int family, int model)
2417 unsigned long long msr, core_counts;
2418 unsigned int ratio, group_size;
2420 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2421 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2423 if (has_turbo_ratio_group_limits(family, model)) {
2424 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2425 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2427 core_counts = 0x0807060504030201;
2430 ratio = (msr >> 56) & 0xFF;
2431 group_size = (core_counts >> 56) & 0xFF;
2433 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2434 ratio, bclk, ratio * bclk, group_size);
2436 ratio = (msr >> 48) & 0xFF;
2437 group_size = (core_counts >> 48) & 0xFF;
2439 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2440 ratio, bclk, ratio * bclk, group_size);
2442 ratio = (msr >> 40) & 0xFF;
2443 group_size = (core_counts >> 40) & 0xFF;
2445 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2446 ratio, bclk, ratio * bclk, group_size);
2448 ratio = (msr >> 32) & 0xFF;
2449 group_size = (core_counts >> 32) & 0xFF;
2451 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2452 ratio, bclk, ratio * bclk, group_size);
2454 ratio = (msr >> 24) & 0xFF;
2455 group_size = (core_counts >> 24) & 0xFF;
2457 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2458 ratio, bclk, ratio * bclk, group_size);
2460 ratio = (msr >> 16) & 0xFF;
2461 group_size = (core_counts >> 16) & 0xFF;
2463 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2464 ratio, bclk, ratio * bclk, group_size);
2466 ratio = (msr >> 8) & 0xFF;
2467 group_size = (core_counts >> 8) & 0xFF;
2469 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2470 ratio, bclk, ratio * bclk, group_size);
2472 ratio = (msr >> 0) & 0xFF;
2473 group_size = (core_counts >> 0) & 0xFF;
2475 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2476 ratio, bclk, ratio * bclk, group_size);
2481 dump_atom_turbo_ratio_limits(void)
2483 unsigned long long msr;
2486 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2487 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2489 ratio = (msr >> 0) & 0x3F;
2491 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2492 ratio, bclk, ratio * bclk);
2494 ratio = (msr >> 8) & 0x3F;
2496 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2497 ratio, bclk, ratio * bclk);
2499 ratio = (msr >> 16) & 0x3F;
2501 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2502 ratio, bclk, ratio * bclk);
2504 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2505 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2507 ratio = (msr >> 24) & 0x3F;
2509 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2510 ratio, bclk, ratio * bclk);
2512 ratio = (msr >> 16) & 0x3F;
2514 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2515 ratio, bclk, ratio * bclk);
2517 ratio = (msr >> 8) & 0x3F;
2519 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2520 ratio, bclk, ratio * bclk);
2522 ratio = (msr >> 0) & 0x3F;
2524 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2525 ratio, bclk, ratio * bclk);
2529 dump_knl_turbo_ratio_limits(void)
2531 const unsigned int buckets_no = 7;
2533 unsigned long long msr;
2534 int delta_cores, delta_ratio;
2536 unsigned int cores[buckets_no];
2537 unsigned int ratio[buckets_no];
2539 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2541 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2545 * Turbo encoding in KNL is as follows:
2547 * [7:1] -- Base value of number of active cores of bucket 1.
2548 * [15:8] -- Base value of freq ratio of bucket 1.
2549 * [20:16] -- +ve delta of number of active cores of bucket 2.
2550 * i.e. active cores of bucket 2 =
2551 * active cores of bucket 1 + delta
2552 * [23:21] -- Negative delta of freq ratio of bucket 2.
2553 * i.e. freq ratio of bucket 2 =
2554 * freq ratio of bucket 1 - delta
2555 * [28:24]-- +ve delta of number of active cores of bucket 3.
2556 * [31:29]-- -ve delta of freq ratio of bucket 3.
2557 * [36:32]-- +ve delta of number of active cores of bucket 4.
2558 * [39:37]-- -ve delta of freq ratio of bucket 4.
2559 * [44:40]-- +ve delta of number of active cores of bucket 5.
2560 * [47:45]-- -ve delta of freq ratio of bucket 5.
2561 * [52:48]-- +ve delta of number of active cores of bucket 6.
2562 * [55:53]-- -ve delta of freq ratio of bucket 6.
2563 * [60:56]-- +ve delta of number of active cores of bucket 7.
2564 * [63:61]-- -ve delta of freq ratio of bucket 7.
2568 cores[b_nr] = (msr & 0xFF) >> 1;
2569 ratio[b_nr] = (msr >> 8) & 0xFF;
2571 for (i = 16; i < 64; i += 8) {
2572 delta_cores = (msr >> i) & 0x1F;
2573 delta_ratio = (msr >> (i + 5)) & 0x7;
2575 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2576 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2580 for (i = buckets_no - 1; i >= 0; i--)
2581 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2583 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2584 ratio[i], bclk, ratio[i] * bclk, cores[i]);
2588 dump_nhm_cst_cfg(void)
2590 unsigned long long msr;
2592 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2594 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2596 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2597 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2598 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2599 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2600 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2601 (msr & (1 << 15)) ? "" : "UN",
2602 (unsigned int)msr & 0xF,
2603 pkg_cstate_limit_strings[pkg_cstate_limit]);
2605 #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2606 if (has_automatic_cstate_conversion) {
2607 fprintf(outf, ", automatic c-state conversion=%s",
2608 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2611 fprintf(outf, ")\n");
2617 dump_config_tdp(void)
2619 unsigned long long msr;
2621 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2622 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2623 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
2625 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2626 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
2628 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2629 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2630 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2631 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
2633 fprintf(outf, ")\n");
2635 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2636 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
2638 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2639 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2640 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2641 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
2643 fprintf(outf, ")\n");
2645 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2646 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
2648 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2649 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2650 fprintf(outf, ")\n");
2652 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2653 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2654 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2655 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2656 fprintf(outf, ")\n");
2659 unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2661 void print_irtl(void)
2663 unsigned long long msr;
2665 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2666 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2667 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2668 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2670 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2671 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2672 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2673 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2675 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2676 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2677 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2678 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2683 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2684 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2685 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2686 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2688 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2689 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2690 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2691 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2693 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2694 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2695 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2696 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2699 void free_fd_percpu(void)
2703 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
2704 if (fd_percpu[i] != 0)
2705 close(fd_percpu[i]);
2711 void free_all_buffers(void)
2715 CPU_FREE(cpu_present_set);
2716 cpu_present_set = NULL;
2717 cpu_present_setsize = 0;
2719 CPU_FREE(cpu_affinity_set);
2720 cpu_affinity_set = NULL;
2721 cpu_affinity_setsize = 0;
2729 package_even = NULL;
2739 free(output_buffer);
2740 output_buffer = NULL;
2745 free(irq_column_2_cpu);
2748 for (i = 0; i <= topo.max_cpu_num; ++i) {
2749 if (cpus[i].put_ids)
2750 CPU_FREE(cpus[i].put_ids);
2757 * Parse a file containing a single int.
2758 * Return 0 if file can not be opened
2759 * Exit if file can be opened, but can not be parsed
2761 int parse_int_file(const char *fmt, ...)
2764 char path[PATH_MAX];
2768 va_start(args, fmt);
2769 vsnprintf(path, sizeof(path), fmt, args);
2771 filep = fopen(path, "r");
2774 if (fscanf(filep, "%d", &value) != 1)
2775 err(1, "%s: failed to parse number from file", path);
2781 * cpu_is_first_core_in_package(cpu)
2782 * return 1 if given CPU is 1st core in package
2784 int cpu_is_first_core_in_package(int cpu)
2786 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2789 int get_physical_package_id(int cpu)
2791 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2794 int get_die_id(int cpu)
2796 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
2799 int get_core_id(int cpu)
2801 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2804 void set_node_data(void)
2806 int pkg, node, lnode, cpu, cpux;
2809 /* initialize logical_node_id */
2810 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
2811 cpus[cpu].logical_node_id = -1;
2814 for (pkg = 0; pkg < topo.num_packages; pkg++) {
2816 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
2817 if (cpus[cpu].physical_package_id != pkg)
2819 /* find a cpu with an unset logical_node_id */
2820 if (cpus[cpu].logical_node_id != -1)
2822 cpus[cpu].logical_node_id = lnode;
2823 node = cpus[cpu].physical_node_id;
2826 * find all matching cpus on this pkg and set
2827 * the logical_node_id
2829 for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
2830 if ((cpus[cpux].physical_package_id == pkg) &&
2831 (cpus[cpux].physical_node_id == node)) {
2832 cpus[cpux].logical_node_id = lnode;
2837 if (lnode > topo.nodes_per_pkg)
2838 topo.nodes_per_pkg = lnode;
2840 if (cpu_count >= topo.max_cpu_num)
2845 int get_physical_node_id(struct cpu_topology *thiscpu)
2850 int cpu = thiscpu->logical_cpu_id;
2852 for (i = 0; i <= topo.max_cpu_num; i++) {
2853 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2855 filep = fopen(path, "r");
2864 int get_thread_siblings(struct cpu_topology *thiscpu)
2866 char path[80], character;
2869 int so, shift, sib_core;
2870 int cpu = thiscpu->logical_cpu_id;
2871 int offset = topo.max_cpu_num + 1;
2875 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
2876 if (thiscpu->thread_id < 0)
2877 thiscpu->thread_id = thread_id++;
2878 if (!thiscpu->put_ids)
2881 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2882 CPU_ZERO_S(size, thiscpu->put_ids);
2885 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2886 filep = fopen(path, "r");
2889 warnx("%s: open failed", path);
2893 offset -= BITMASK_SIZE;
2894 if (fscanf(filep, "%lx%c", &map, &character) != 2)
2895 err(1, "%s: failed to parse file", path);
2896 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2897 if ((map >> shift) & 0x1) {
2898 so = shift + offset;
2899 sib_core = get_core_id(so);
2900 if (sib_core == thiscpu->physical_core_id) {
2901 CPU_SET_S(so, size, thiscpu->put_ids);
2903 (cpus[so].thread_id < 0))
2904 cpus[so].thread_id =
2909 } while (!strncmp(&character, ",", 1));
2912 return CPU_COUNT_S(size, thiscpu->put_ids);
2916 * run func(thread, core, package) in topology order
2917 * skip non-present cpus
2920 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2921 struct pkg_data *, struct thread_data *, struct core_data *,
2922 struct pkg_data *), struct thread_data *thread_base,
2923 struct core_data *core_base, struct pkg_data *pkg_base,
2924 struct thread_data *thread_base2, struct core_data *core_base2,
2925 struct pkg_data *pkg_base2)
2927 int retval, pkg_no, node_no, core_no, thread_no;
2929 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2930 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
2931 for (core_no = 0; core_no < topo.cores_per_node;
2933 for (thread_no = 0; thread_no <
2934 topo.threads_per_core; ++thread_no) {
2935 struct thread_data *t, *t2;
2936 struct core_data *c, *c2;
2937 struct pkg_data *p, *p2;
2939 t = GET_THREAD(thread_base, thread_no,
2943 if (cpu_is_not_present(t->cpu_id))
2946 t2 = GET_THREAD(thread_base2, thread_no,
2950 c = GET_CORE(core_base, core_no,
2952 c2 = GET_CORE(core_base2, core_no,
2956 p = GET_PKG(pkg_base, pkg_no);
2957 p2 = GET_PKG(pkg_base2, pkg_no);
2959 retval = func(t, c, p, t2, c2, p2);
2970 * run func(cpu) on every cpu in /proc/stat
2971 * return max_cpu number
2973 int for_all_proc_cpus(int (func)(int))
2979 fp = fopen_or_die(proc_stat, "r");
2981 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2983 err(1, "%s: failed to parse format", proc_stat);
2986 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2990 retval = func(cpu_num);
3000 void re_initialize(void)
3003 setup_all_buffers();
3004 fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
3007 void set_max_cpu_num(void)
3011 unsigned long dummy;
3014 base_cpu = sched_getcpu();
3016 err(1, "cannot find calling cpu ID");
3018 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings",
3021 filep = fopen_or_die(pathname, "r");
3022 topo.max_cpu_num = 0;
3023 while (fscanf(filep, "%lx,", &dummy) == 1)
3024 topo.max_cpu_num += BITMASK_SIZE;
3026 topo.max_cpu_num--; /* 0 based */
3031 * remember the last one seen, it will be the max
3033 int count_cpus(int cpu)
3038 int mark_cpu_present(int cpu)
3040 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
3044 int init_thread_id(int cpu)
3046 cpus[cpu].thread_id = -1;
3051 * snapshot_proc_interrupts()
3053 * read and record summary of /proc/interrupts
3055 * return 1 if config change requires a restart, else return 0
3057 int snapshot_proc_interrupts(void)
3063 fp = fopen_or_die("/proc/interrupts", "r");
3067 /* read 1st line of /proc/interrupts to get cpu* name for each column */
3068 for (column = 0; column < topo.num_cpus; ++column) {
3071 retval = fscanf(fp, " CPU%d", &cpu_number);
3075 if (cpu_number > topo.max_cpu_num) {
3076 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
3080 irq_column_2_cpu[column] = cpu_number;
3081 irqs_per_cpu[cpu_number] = 0;
3084 /* read /proc/interrupt count lines and sum up irqs per cpu */
3089 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
3093 /* read the count per cpu */
3094 for (column = 0; column < topo.num_cpus; ++column) {
3096 int cpu_number, irq_count;
3098 retval = fscanf(fp, " %d", &irq_count);
3102 cpu_number = irq_column_2_cpu[column];
3103 irqs_per_cpu[cpu_number] += irq_count;
3107 while (getc(fp) != '\n')
3108 ; /* flush interrupt description */
3114 * snapshot_gfx_rc6_ms()
3116 * record snapshot of
3117 * /sys/class/drm/card0/power/rc6_residency_ms
3119 * return 1 if config change requires a restart, else return 0
3121 int snapshot_gfx_rc6_ms(void)
3126 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
3128 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
3137 * snapshot_gfx_mhz()
3139 * record snapshot of
3140 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
3142 * return 1 if config change requires a restart, else return 0
3144 int snapshot_gfx_mhz(void)
3150 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
3156 retval = fscanf(fp, "%d", &gfx_cur_mhz);
3164 * snapshot_gfx_cur_mhz()
3166 * record snapshot of
3167 * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz
3169 * return 1 if config change requires a restart, else return 0
3171 int snapshot_gfx_act_mhz(void)
3177 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r");
3183 retval = fscanf(fp, "%d", &gfx_act_mhz);
3185 err(1, "GFX ACT MHz");
3191 * snapshot_cpu_lpi()
3193 * record snapshot of
3194 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
3196 int snapshot_cpu_lpi_us(void)
3201 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
3203 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
3205 fprintf(stderr, "Disabling Low Power Idle CPU output\n");
3206 BIC_NOT_PRESENT(BIC_CPU_LPI);
3216 * snapshot_sys_lpi()
3218 * record snapshot of sys_lpi_file
3220 int snapshot_sys_lpi_us(void)
3225 fp = fopen_or_die(sys_lpi_file, "r");
3227 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
3229 fprintf(stderr, "Disabling Low Power Idle System output\n");
3230 BIC_NOT_PRESENT(BIC_SYS_LPI);
3239 * snapshot /proc and /sys files
3241 * return 1 if configuration restart needed, else return 0
3243 int snapshot_proc_sysfs_files(void)
3245 if (DO_BIC(BIC_IRQ))
3246 if (snapshot_proc_interrupts())
3249 if (DO_BIC(BIC_GFX_rc6))
3250 snapshot_gfx_rc6_ms();
3252 if (DO_BIC(BIC_GFXMHz))
3255 if (DO_BIC(BIC_GFXACTMHz))
3256 snapshot_gfx_act_mhz();
3258 if (DO_BIC(BIC_CPU_LPI))
3259 snapshot_cpu_lpi_us();
3261 if (DO_BIC(BIC_SYS_LPI))
3262 snapshot_sys_lpi_us();
3269 static void signal_handler (int signal)
3275 fprintf(stderr, " SIGINT\n");
3279 fprintf(stderr, "SIGUSR1\n");
3284 void setup_signal_handler(void)
3286 struct sigaction sa;
3288 memset(&sa, 0, sizeof(sa));
3290 sa.sa_handler = &signal_handler;
3292 if (sigaction(SIGINT, &sa, NULL) < 0)
3293 err(1, "sigaction SIGINT");
3294 if (sigaction(SIGUSR1, &sa, NULL) < 0)
3295 err(1, "sigaction SIGUSR1");
3300 struct timeval tout;
3301 struct timespec rest;
3306 FD_SET(0, &readfds);
3309 nanosleep(&interval_ts, NULL);
3314 retval = select(1, &readfds, NULL, NULL, &tout);
3317 switch (getc(stdin)) {
3323 * 'stdin' is a pipe closed on the other end. There
3324 * won't be any further input.
3327 /* Sleep the rest of the time */
3328 rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3329 rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3330 nanosleep(&rest, NULL);
3335 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr)
3338 unsigned long long msr_cur, msr_last;
3340 if (!per_cpu_msr_sum)
3343 idx = offset_to_idx(offset);
3346 /* get_msr_sum() = sum + (get_msr() - last) */
3347 ret = get_msr(cpu, offset, &msr_cur);
3350 msr_last = per_cpu_msr_sum[cpu].entries[idx].last;
3351 DELTA_WRAP32(msr_cur, msr_last);
3352 *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum;
3359 /* Timer callback, update the sum of MSRs periodically. */
3360 static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3363 int cpu = t->cpu_id;
3365 for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
3366 unsigned long long msr_cur, msr_last;
3371 offset = idx_to_offset(i);
3374 ret = get_msr(cpu, offset, &msr_cur);
3376 fprintf(outf, "Can not update msr(0x%llx)\n",
3377 (unsigned long long)offset);
3381 msr_last = per_cpu_msr_sum[cpu].entries[i].last;
3382 per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff;
3384 DELTA_WRAP32(msr_cur, msr_last);
3385 per_cpu_msr_sum[cpu].entries[i].sum += msr_last;
3391 msr_record_handler(union sigval v)
3393 for_all_cpus(update_msr_sum, EVEN_COUNTERS);
3396 void msr_sum_record(void)
3398 struct itimerspec its;
3399 struct sigevent sev;
3401 per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array));
3402 if (!per_cpu_msr_sum) {
3403 fprintf(outf, "Can not allocate memory for long time MSR.\n");
3407 * Signal handler might be restricted, so use thread notifier instead.
3409 memset(&sev, 0, sizeof(struct sigevent));
3410 sev.sigev_notify = SIGEV_THREAD;
3411 sev.sigev_notify_function = msr_record_handler;
3413 sev.sigev_value.sival_ptr = &timerid;
3414 if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
3415 fprintf(outf, "Can not create timer.\n");
3419 its.it_value.tv_sec = 0;
3420 its.it_value.tv_nsec = 1;
3422 * A wraparound time has been calculated early.
3423 * Some sources state that the peak power for a
3424 * microprocessor is usually 1.5 times the TDP rating,
3425 * use 2 * TDP for safety.
3427 its.it_interval.tv_sec = rapl_joule_counter_range / 2;
3428 its.it_interval.tv_nsec = 0;
3430 if (timer_settime(timerid, 0, &its, NULL) == -1) {
3431 fprintf(outf, "Can not set timer.\n");
3437 timer_delete(timerid);
3439 free(per_cpu_msr_sum);
3442 void turbostat_loop()
3448 setup_signal_handler();
3453 snapshot_proc_sysfs_files();
3454 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3455 first_counter_read = 0;
3458 } else if (retval == -1) {
3459 if (restarted > 10) {
3467 gettimeofday(&tv_even, (struct timezone *)NULL);
3470 if (for_all_proc_cpus(cpu_is_not_present)) {
3475 if (snapshot_proc_sysfs_files())
3477 retval = for_all_cpus(get_counters, ODD_COUNTERS);
3480 } else if (retval == -1) {
3484 gettimeofday(&tv_odd, (struct timezone *)NULL);
3485 timersub(&tv_odd, &tv_even, &tv_delta);
3486 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3490 compute_average(EVEN_COUNTERS);
3491 format_all_counters(EVEN_COUNTERS);
3492 flush_output_stdout();
3495 if (num_iterations && ++done_iters >= num_iterations)
3498 if (snapshot_proc_sysfs_files())
3500 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3503 } else if (retval == -1) {
3507 gettimeofday(&tv_even, (struct timezone *)NULL);
3508 timersub(&tv_even, &tv_odd, &tv_delta);
3509 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3513 compute_average(ODD_COUNTERS);
3514 format_all_counters(ODD_COUNTERS);
3515 flush_output_stdout();
3518 if (num_iterations && ++done_iters >= num_iterations)
3523 void check_dev_msr()
3528 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3529 if (stat(pathname, &sb))
3530 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3531 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3535 * check for CAP_SYS_RAWIO
3536 * return 0 on success
3539 int check_for_cap_sys_rawio(void)
3542 cap_flag_value_t cap_flag_value;
3544 caps = cap_get_proc();
3546 err(-6, "cap_get_proc\n");
3548 if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3549 err(-6, "cap_get\n");
3551 if (cap_flag_value != CAP_SET) {
3552 warnx("capget(CAP_SYS_RAWIO) failed,"
3553 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3557 if (cap_free(caps) == -1)
3558 err(-6, "cap_free\n");
3562 void check_permissions(void)
3567 /* check for CAP_SYS_RAWIO */
3568 do_exit += check_for_cap_sys_rawio();
3570 /* test file permissions */
3571 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3572 if (euidaccess(pathname, R_OK)) {
3574 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
3577 /* if all else fails, thell them to be root */
3580 warnx("... or simply run as root");
3587 * NHM adds support for additional MSRs:
3589 * MSR_SMI_COUNT 0x00000034
3591 * MSR_PLATFORM_INFO 0x000000ce
3592 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
3594 * MSR_MISC_PWR_MGMT 0x000001aa
3596 * MSR_PKG_C3_RESIDENCY 0x000003f8
3597 * MSR_PKG_C6_RESIDENCY 0x000003f9
3598 * MSR_CORE_C3_RESIDENCY 0x000003fc
3599 * MSR_CORE_C6_RESIDENCY 0x000003fd
3602 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
3603 * sets has_misc_feature_control
3605 int probe_nhm_msrs(unsigned int family, unsigned int model)
3607 unsigned long long msr;
3608 unsigned int base_ratio;
3609 int *pkg_cstate_limits;
3617 bclk = discover_bclk(family, model);
3620 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3621 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3622 pkg_cstate_limits = nhm_pkg_cstate_limits;
3624 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3625 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3626 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3627 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3628 pkg_cstate_limits = snb_pkg_cstate_limits;
3629 has_misc_feature_control = 1;
3631 case INTEL_FAM6_HASWELL: /* HSW */
3632 case INTEL_FAM6_HASWELL_G: /* HSW */
3633 case INTEL_FAM6_HASWELL_X: /* HSX */
3634 case INTEL_FAM6_HASWELL_L: /* HSW */
3635 case INTEL_FAM6_BROADWELL: /* BDW */
3636 case INTEL_FAM6_BROADWELL_G: /* BDW */
3637 case INTEL_FAM6_BROADWELL_X: /* BDX */
3638 case INTEL_FAM6_SKYLAKE_L: /* SKL */
3639 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3640 pkg_cstate_limits = hsw_pkg_cstate_limits;
3641 has_misc_feature_control = 1;
3643 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3644 pkg_cstate_limits = skx_pkg_cstate_limits;
3645 has_misc_feature_control = 1;
3647 case INTEL_FAM6_ICELAKE_X: /* ICX */
3648 pkg_cstate_limits = icx_pkg_cstate_limits;
3649 has_misc_feature_control = 1;
3651 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
3652 no_MSR_MISC_PWR_MGMT = 1;
3653 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
3654 pkg_cstate_limits = slv_pkg_cstate_limits;
3656 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
3657 pkg_cstate_limits = amt_pkg_cstate_limits;
3658 no_MSR_MISC_PWR_MGMT = 1;
3660 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
3661 pkg_cstate_limits = phi_pkg_cstate_limits;
3663 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3664 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3665 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
3666 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
3667 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
3668 pkg_cstate_limits = glm_pkg_cstate_limits;
3673 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3674 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3676 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
3677 base_ratio = (msr >> 8) & 0xFF;
3679 base_hz = base_ratio * bclk * 1000000;
3684 * SLV client has support for unique MSRs:
3686 * MSR_CC6_DEMOTION_POLICY_CONFIG
3687 * MSR_MC6_DEMOTION_POLICY_CONFIG
3690 int has_slv_msrs(unsigned int family, unsigned int model)
3696 case INTEL_FAM6_ATOM_SILVERMONT:
3697 case INTEL_FAM6_ATOM_SILVERMONT_MID:
3698 case INTEL_FAM6_ATOM_AIRMONT_MID:
3703 int is_dnv(unsigned int family, unsigned int model)
3710 case INTEL_FAM6_ATOM_GOLDMONT_D:
3715 int is_bdx(unsigned int family, unsigned int model)
3722 case INTEL_FAM6_BROADWELL_X:
3727 int is_skx(unsigned int family, unsigned int model)
3734 case INTEL_FAM6_SKYLAKE_X:
3740 int is_icx(unsigned int family, unsigned int model)
3747 case INTEL_FAM6_ICELAKE_X:
3753 int is_ehl(unsigned int family, unsigned int model)
3759 case INTEL_FAM6_ATOM_TREMONT:
3764 int is_jvl(unsigned int family, unsigned int model)
3770 case INTEL_FAM6_ATOM_TREMONT_D:
3776 int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3778 if (has_slv_msrs(family, model))
3782 /* Nehalem compatible, but do not include turbo-ratio limit support */
3783 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3784 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
3790 int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3792 if (has_slv_msrs(family, model))
3797 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3806 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3807 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3813 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3822 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3829 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3838 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3844 int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3853 case INTEL_FAM6_ATOM_GOLDMONT:
3854 case INTEL_FAM6_SKYLAKE_X:
3855 case INTEL_FAM6_ICELAKE_X:
3861 int has_config_tdp(unsigned int family, unsigned int model)
3870 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3871 case INTEL_FAM6_HASWELL: /* HSW */
3872 case INTEL_FAM6_HASWELL_X: /* HSX */
3873 case INTEL_FAM6_HASWELL_L: /* HSW */
3874 case INTEL_FAM6_HASWELL_G: /* HSW */
3875 case INTEL_FAM6_BROADWELL: /* BDW */
3876 case INTEL_FAM6_BROADWELL_G: /* BDW */
3877 case INTEL_FAM6_BROADWELL_X: /* BDX */
3878 case INTEL_FAM6_SKYLAKE_L: /* SKL */
3879 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3880 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3881 case INTEL_FAM6_ICELAKE_X: /* ICX */
3883 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3892 * 0: Tcc Offset not supported (Default)
3893 * 6: Bit 29:24 of MSR_PLATFORM_INFO
3894 * 4: Bit 27:24 of MSR_PLATFORM_INFO
3896 void check_tcc_offset(int model)
3898 unsigned long long msr;
3904 case INTEL_FAM6_SKYLAKE_L:
3905 case INTEL_FAM6_SKYLAKE:
3906 case INTEL_FAM6_KABYLAKE_L:
3907 case INTEL_FAM6_KABYLAKE:
3908 case INTEL_FAM6_ICELAKE_L:
3909 case INTEL_FAM6_ICELAKE:
3910 case INTEL_FAM6_TIGERLAKE_L:
3911 case INTEL_FAM6_TIGERLAKE:
3912 case INTEL_FAM6_COMETLAKE:
3913 if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) {
3914 msr = (msr >> 30) & 1;
3916 tcc_offset_bits = 6;
3925 remove_underbar(char *s)
3939 dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3941 if (!do_nhm_platform_info)
3944 dump_nhm_platform_info();
3946 if (has_hsw_turbo_ratio_limit(family, model))
3947 dump_hsw_turbo_ratio_limits();
3949 if (has_ivt_turbo_ratio_limit(family, model))
3950 dump_ivt_turbo_ratio_limits();
3952 if (has_turbo_ratio_limit(family, model))
3953 dump_turbo_ratio_limits(family, model);
3955 if (has_atom_turbo_ratio_limit(family, model))
3956 dump_atom_turbo_ratio_limits();
3958 if (has_knl_turbo_ratio_limit(family, model))
3959 dump_knl_turbo_ratio_limits();
3961 if (has_config_tdp(family, model))
3967 static void dump_sysfs_file(char *path)
3970 char cpuidle_buf[64];
3972 input = fopen(path, "r");
3973 if (input == NULL) {
3975 fprintf(outf, "NSFOD %s\n", path);
3978 if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
3979 err(1, "%s: failed to read file", path);
3982 fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
3985 dump_sysfs_cstate_config(void)
3994 if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
3995 fprintf(outf, "cpuidle not loaded\n");
3999 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
4000 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
4001 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
4003 for (state = 0; state < 10; ++state) {
4005 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4007 input = fopen(path, "r");
4010 if (!fgets(name_buf, sizeof(name_buf), input))
4011 err(1, "%s: failed to read file", path);
4013 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4014 sp = strchr(name_buf, '-');
4016 sp = strchrnul(name_buf, '\n');
4020 remove_underbar(name_buf);
4022 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
4024 input = fopen(path, "r");
4027 if (!fgets(desc, sizeof(desc), input))
4028 err(1, "%s: failed to read file", path);
4030 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
4035 dump_sysfs_pstate_config(void)
4038 char driver_buf[64];
4039 char governor_buf[64];
4043 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
4045 input = fopen(path, "r");
4046 if (input == NULL) {
4047 fprintf(outf, "NSFOD %s\n", path);
4050 if (!fgets(driver_buf, sizeof(driver_buf), input))
4051 err(1, "%s: failed to read file", path);
4054 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
4056 input = fopen(path, "r");
4057 if (input == NULL) {
4058 fprintf(outf, "NSFOD %s\n", path);
4061 if (!fgets(governor_buf, sizeof(governor_buf), input))
4062 err(1, "%s: failed to read file", path);
4065 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
4066 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
4068 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
4069 input = fopen(path, "r");
4070 if (input != NULL) {
4071 if (fscanf(input, "%d", &turbo) != 1)
4072 err(1, "%s: failed to parse number from file", path);
4073 fprintf(outf, "cpufreq boost: %d\n", turbo);
4077 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
4078 input = fopen(path, "r");
4079 if (input != NULL) {
4080 if (fscanf(input, "%d", &turbo) != 1)
4081 err(1, "%s: failed to parse number from file", path);
4082 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
4090 * Decode the ENERGY_PERF_BIAS MSR
4092 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4102 /* EPB is per-package */
4103 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4106 if (cpu_migrate(cpu)) {
4107 fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu);
4116 case ENERGY_PERF_BIAS_PERFORMANCE:
4117 epb_string = "performance";
4119 case ENERGY_PERF_BIAS_NORMAL:
4120 epb_string = "balanced";
4122 case ENERGY_PERF_BIAS_POWERSAVE:
4123 epb_string = "powersave";
4126 epb_string = "custom";
4129 fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string);
4135 * Decode the MSR_HWP_CAPABILITIES
4137 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4139 unsigned long long msr;
4147 /* MSR_HWP_CAPABILITIES is per-package */
4148 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4151 if (cpu_migrate(cpu)) {
4152 fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu);
4156 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
4159 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
4160 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
4162 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
4163 if ((msr & (1 << 0)) == 0)
4166 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
4169 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
4170 "(high %d guar %d eff %d low %d)\n",
4172 (unsigned int)HWP_HIGHEST_PERF(msr),
4173 (unsigned int)HWP_GUARANTEED_PERF(msr),
4174 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
4175 (unsigned int)HWP_LOWEST_PERF(msr));
4177 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
4180 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
4181 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
4183 (unsigned int)(((msr) >> 0) & 0xff),
4184 (unsigned int)(((msr) >> 8) & 0xff),
4185 (unsigned int)(((msr) >> 16) & 0xff),
4186 (unsigned int)(((msr) >> 24) & 0xff),
4187 (unsigned int)(((msr) >> 32) & 0xff3),
4188 (unsigned int)(((msr) >> 42) & 0x1));
4191 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
4194 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
4195 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
4197 (unsigned int)(((msr) >> 0) & 0xff),
4198 (unsigned int)(((msr) >> 8) & 0xff),
4199 (unsigned int)(((msr) >> 16) & 0xff),
4200 (unsigned int)(((msr) >> 24) & 0xff),
4201 (unsigned int)(((msr) >> 32) & 0xff3));
4203 if (has_hwp_notify) {
4204 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
4207 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
4208 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
4210 ((msr) & 0x1) ? "EN" : "Dis",
4211 ((msr) & 0x2) ? "EN" : "Dis");
4213 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
4216 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
4217 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
4219 ((msr) & 0x1) ? "" : "No-",
4220 ((msr) & 0x2) ? "" : "No-");
4226 * print_perf_limit()
4228 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4230 unsigned long long msr;
4236 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4239 if (cpu_migrate(cpu)) {
4240 fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu);
4244 if (do_core_perf_limit_reasons) {
4245 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
4246 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4247 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
4248 (msr & 1 << 15) ? "bit15, " : "",
4249 (msr & 1 << 14) ? "bit14, " : "",
4250 (msr & 1 << 13) ? "Transitions, " : "",
4251 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
4252 (msr & 1 << 11) ? "PkgPwrL2, " : "",
4253 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4254 (msr & 1 << 9) ? "CorePwr, " : "",
4255 (msr & 1 << 8) ? "Amps, " : "",
4256 (msr & 1 << 6) ? "VR-Therm, " : "",
4257 (msr & 1 << 5) ? "Auto-HWP, " : "",
4258 (msr & 1 << 4) ? "Graphics, " : "",
4259 (msr & 1 << 2) ? "bit2, " : "",
4260 (msr & 1 << 1) ? "ThermStatus, " : "",
4261 (msr & 1 << 0) ? "PROCHOT, " : "");
4262 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
4263 (msr & 1 << 31) ? "bit31, " : "",
4264 (msr & 1 << 30) ? "bit30, " : "",
4265 (msr & 1 << 29) ? "Transitions, " : "",
4266 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
4267 (msr & 1 << 27) ? "PkgPwrL2, " : "",
4268 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4269 (msr & 1 << 25) ? "CorePwr, " : "",
4270 (msr & 1 << 24) ? "Amps, " : "",
4271 (msr & 1 << 22) ? "VR-Therm, " : "",
4272 (msr & 1 << 21) ? "Auto-HWP, " : "",
4273 (msr & 1 << 20) ? "Graphics, " : "",
4274 (msr & 1 << 18) ? "bit18, " : "",
4275 (msr & 1 << 17) ? "ThermStatus, " : "",
4276 (msr & 1 << 16) ? "PROCHOT, " : "");
4279 if (do_gfx_perf_limit_reasons) {
4280 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
4281 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4282 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
4283 (msr & 1 << 0) ? "PROCHOT, " : "",
4284 (msr & 1 << 1) ? "ThermStatus, " : "",
4285 (msr & 1 << 4) ? "Graphics, " : "",
4286 (msr & 1 << 6) ? "VR-Therm, " : "",
4287 (msr & 1 << 8) ? "Amps, " : "",
4288 (msr & 1 << 9) ? "GFXPwr, " : "",
4289 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4290 (msr & 1 << 11) ? "PkgPwrL2, " : "");
4291 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
4292 (msr & 1 << 16) ? "PROCHOT, " : "",
4293 (msr & 1 << 17) ? "ThermStatus, " : "",
4294 (msr & 1 << 20) ? "Graphics, " : "",
4295 (msr & 1 << 22) ? "VR-Therm, " : "",
4296 (msr & 1 << 24) ? "Amps, " : "",
4297 (msr & 1 << 25) ? "GFXPwr, " : "",
4298 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4299 (msr & 1 << 27) ? "PkgPwrL2, " : "");
4301 if (do_ring_perf_limit_reasons) {
4302 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
4303 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4304 fprintf(outf, " (Active: %s%s%s%s%s%s)",
4305 (msr & 1 << 0) ? "PROCHOT, " : "",
4306 (msr & 1 << 1) ? "ThermStatus, " : "",
4307 (msr & 1 << 6) ? "VR-Therm, " : "",
4308 (msr & 1 << 8) ? "Amps, " : "",
4309 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4310 (msr & 1 << 11) ? "PkgPwrL2, " : "");
4311 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
4312 (msr & 1 << 16) ? "PROCHOT, " : "",
4313 (msr & 1 << 17) ? "ThermStatus, " : "",
4314 (msr & 1 << 22) ? "VR-Therm, " : "",
4315 (msr & 1 << 24) ? "Amps, " : "",
4316 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4317 (msr & 1 << 27) ? "PkgPwrL2, " : "");
4322 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
4323 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
4325 double get_tdp_intel(unsigned int model)
4327 unsigned long long msr;
4329 if (do_rapl & RAPL_PKG_POWER_INFO)
4330 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
4331 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
4334 case INTEL_FAM6_ATOM_SILVERMONT:
4335 case INTEL_FAM6_ATOM_SILVERMONT_D:
4342 double get_tdp_amd(unsigned int family)
4344 /* This is the max stock TDP of HEDT/Server Fam17h+ chips */
4349 * rapl_dram_energy_units_probe()
4350 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
4353 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
4355 /* only called for genuine_intel, family 6 */
4358 case INTEL_FAM6_HASWELL_X: /* HSX */
4359 case INTEL_FAM6_BROADWELL_X: /* BDX */
4360 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4361 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4362 return (rapl_dram_energy_units = 15.3 / 1000000);
4364 return (rapl_energy_units);
4368 void rapl_probe_intel(unsigned int family, unsigned int model)
4370 unsigned long long msr;
4371 unsigned int time_unit;
4378 case INTEL_FAM6_SANDYBRIDGE:
4379 case INTEL_FAM6_IVYBRIDGE:
4380 case INTEL_FAM6_HASWELL: /* HSW */
4381 case INTEL_FAM6_HASWELL_L: /* HSW */
4382 case INTEL_FAM6_HASWELL_G: /* HSW */
4383 case INTEL_FAM6_BROADWELL: /* BDW */
4384 case INTEL_FAM6_BROADWELL_G: /* BDW */
4385 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
4387 BIC_PRESENT(BIC_Pkg_J);
4388 BIC_PRESENT(BIC_Cor_J);
4389 BIC_PRESENT(BIC_GFX_J);
4391 BIC_PRESENT(BIC_PkgWatt);
4392 BIC_PRESENT(BIC_CorWatt);
4393 BIC_PRESENT(BIC_GFXWatt);
4396 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4397 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4398 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
4400 BIC_PRESENT(BIC_Pkg_J);
4402 BIC_PRESENT(BIC_PkgWatt);
4404 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4405 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;
4407 BIC_PRESENT(BIC_Pkg_J);
4408 BIC_PRESENT(BIC_Cor_J);
4409 BIC_PRESENT(BIC_RAM_J);
4410 BIC_PRESENT(BIC_GFX_J);
4412 BIC_PRESENT(BIC_PkgWatt);
4413 BIC_PRESENT(BIC_CorWatt);
4414 BIC_PRESENT(BIC_RAMWatt);
4415 BIC_PRESENT(BIC_GFXWatt);
4418 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
4419 do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
4420 BIC_PRESENT(BIC_PKG__);
4422 BIC_PRESENT(BIC_Pkg_J);
4424 BIC_PRESENT(BIC_PkgWatt);
4426 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4427 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4428 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;
4429 BIC_PRESENT(BIC_PKG__);
4430 BIC_PRESENT(BIC_RAM__);
4432 BIC_PRESENT(BIC_Pkg_J);
4433 BIC_PRESENT(BIC_Cor_J);
4434 BIC_PRESENT(BIC_RAM_J);
4435 BIC_PRESENT(BIC_GFX_J);
4437 BIC_PRESENT(BIC_PkgWatt);
4438 BIC_PRESENT(BIC_CorWatt);
4439 BIC_PRESENT(BIC_RAMWatt);
4440 BIC_PRESENT(BIC_GFXWatt);
4443 case INTEL_FAM6_HASWELL_X: /* HSX */
4444 case INTEL_FAM6_BROADWELL_X: /* BDX */
4445 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4446 case INTEL_FAM6_ICELAKE_X: /* ICX */
4447 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4448 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
4449 BIC_PRESENT(BIC_PKG__);
4450 BIC_PRESENT(BIC_RAM__);
4452 BIC_PRESENT(BIC_Pkg_J);
4453 BIC_PRESENT(BIC_RAM_J);
4455 BIC_PRESENT(BIC_PkgWatt);
4456 BIC_PRESENT(BIC_RAMWatt);
4459 case INTEL_FAM6_SANDYBRIDGE_X:
4460 case INTEL_FAM6_IVYBRIDGE_X:
4461 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;
4462 BIC_PRESENT(BIC_PKG__);
4463 BIC_PRESENT(BIC_RAM__);
4465 BIC_PRESENT(BIC_Pkg_J);
4466 BIC_PRESENT(BIC_Cor_J);
4467 BIC_PRESENT(BIC_RAM_J);
4469 BIC_PRESENT(BIC_PkgWatt);
4470 BIC_PRESENT(BIC_CorWatt);
4471 BIC_PRESENT(BIC_RAMWatt);
4474 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4475 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4476 do_rapl = RAPL_PKG | RAPL_CORES;
4478 BIC_PRESENT(BIC_Pkg_J);
4479 BIC_PRESENT(BIC_Cor_J);
4481 BIC_PRESENT(BIC_PkgWatt);
4482 BIC_PRESENT(BIC_CorWatt);
4485 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
4486 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;
4487 BIC_PRESENT(BIC_PKG__);
4488 BIC_PRESENT(BIC_RAM__);
4490 BIC_PRESENT(BIC_Pkg_J);
4491 BIC_PRESENT(BIC_Cor_J);
4492 BIC_PRESENT(BIC_RAM_J);
4494 BIC_PRESENT(BIC_PkgWatt);
4495 BIC_PRESENT(BIC_CorWatt);
4496 BIC_PRESENT(BIC_RAMWatt);
4503 /* units on package 0, verify later other packages match */
4504 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4507 rapl_power_units = 1.0 / (1 << (msr & 0xF));
4508 if (model == INTEL_FAM6_ATOM_SILVERMONT)
4509 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4511 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4513 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
4515 time_unit = msr >> 16 & 0xF;
4519 rapl_time_units = 1.0 / (1 << (time_unit));
4521 tdp = get_tdp_intel(model);
4523 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
4525 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
4528 void rapl_probe_amd(unsigned int family, unsigned int model)
4530 unsigned long long msr;
4531 unsigned int eax, ebx, ecx, edx;
4532 unsigned int has_rapl = 0;
4535 if (max_extended_level >= 0x80000007) {
4536 __cpuid(0x80000007, eax, ebx, ecx, edx);
4537 /* RAPL (Fam 17h+) */
4538 has_rapl = edx & (1 << 14);
4541 if (!has_rapl || family < 0x17)
4544 do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
4546 BIC_PRESENT(BIC_Pkg_J);
4547 BIC_PRESENT(BIC_Cor_J);
4549 BIC_PRESENT(BIC_PkgWatt);
4550 BIC_PRESENT(BIC_CorWatt);
4553 if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
4556 rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
4557 rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
4558 rapl_power_units = ldexp(1.0, -(msr & 0xf));
4560 tdp = get_tdp_amd(family);
4562 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
4564 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
4570 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
4572 void rapl_probe(unsigned int family, unsigned int model)
4575 rapl_probe_intel(family, model);
4576 if (authentic_amd || hygon_genuine)
4577 rapl_probe_amd(family, model);
4580 void perf_limit_reasons_probe(unsigned int family, unsigned int model)
4589 case INTEL_FAM6_HASWELL: /* HSW */
4590 case INTEL_FAM6_HASWELL_L: /* HSW */
4591 case INTEL_FAM6_HASWELL_G: /* HSW */
4592 do_gfx_perf_limit_reasons = 1;
4593 case INTEL_FAM6_HASWELL_X: /* HSX */
4594 do_core_perf_limit_reasons = 1;
4595 do_ring_perf_limit_reasons = 1;
4601 void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4603 if (is_skx(family, model) || is_bdx(family, model) ||
4604 is_icx(family, model))
4605 has_automatic_cstate_conversion = 1;
4608 void prewake_cstate_probe(unsigned int family, unsigned int model)
4610 if (is_icx(family, model))
4611 dis_cstate_prewake = 1;
4614 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4616 unsigned long long msr;
4617 unsigned int dts, dts2;
4620 if (!(do_dts || do_ptm))
4625 /* DTS is per-core, no need to print for each thread */
4626 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4629 if (cpu_migrate(cpu)) {
4630 fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu);
4634 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4635 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4638 dts = (msr >> 16) & 0x7F;
4639 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
4640 cpu, msr, tcc_activation_temp - dts);
4642 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4645 dts = (msr >> 16) & 0x7F;
4646 dts2 = (msr >> 8) & 0x7F;
4647 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4648 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4652 if (do_dts && debug) {
4653 unsigned int resolution;
4655 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4658 dts = (msr >> 16) & 0x7F;
4659 resolution = (msr >> 27) & 0xF;
4660 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4661 cpu, msr, tcc_activation_temp - dts, resolution);
4663 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4666 dts = (msr >> 16) & 0x7F;
4667 dts2 = (msr >> 8) & 0x7F;
4668 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4669 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4675 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4677 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4679 ((msr >> 15) & 1) ? "EN" : "DIS",
4680 ((msr >> 0) & 0x7FFF) * rapl_power_units,
4681 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4682 (((msr >> 16) & 1) ? "EN" : "DIS"));
4687 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4689 unsigned long long msr;
4690 const char *msr_name;
4696 /* RAPL counters are per package, so print only for 1st thread/package */
4697 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4701 if (cpu_migrate(cpu)) {
4702 fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu);
4706 if (do_rapl & RAPL_AMD_F17H) {
4707 msr_name = "MSR_RAPL_PWR_UNIT";
4708 if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
4711 msr_name = "MSR_RAPL_POWER_UNIT";
4712 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4716 fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4717 rapl_power_units, rapl_energy_units, rapl_time_units);
4719 if (do_rapl & RAPL_PKG_POWER_INFO) {
4721 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4725 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4727 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4728 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4729 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4730 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4733 if (do_rapl & RAPL_PKG) {
4735 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4738 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4739 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4741 print_power_limit_msr(cpu, msr, "PKG Limit #1");
4742 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4744 ((msr >> 47) & 1) ? "EN" : "DIS",
4745 ((msr >> 32) & 0x7FFF) * rapl_power_units,
4746 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4747 ((msr >> 48) & 1) ? "EN" : "DIS");
4750 if (do_rapl & RAPL_DRAM_POWER_INFO) {
4751 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4754 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4756 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4757 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4758 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4759 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4761 if (do_rapl & RAPL_DRAM) {
4762 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4764 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4765 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4767 print_power_limit_msr(cpu, msr, "DRAM Limit");
4769 if (do_rapl & RAPL_CORE_POLICY) {
4770 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4773 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4775 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4776 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4778 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4779 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4780 print_power_limit_msr(cpu, msr, "Cores Limit");
4782 if (do_rapl & RAPL_GFX) {
4783 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4786 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4788 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4790 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4791 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4792 print_power_limit_msr(cpu, msr, "GFX Limit");
4798 * SNB adds support for additional MSRs:
4800 * MSR_PKG_C7_RESIDENCY 0x000003fa
4801 * MSR_CORE_C7_RESIDENCY 0x000003fe
4802 * MSR_PKG_C2_RESIDENCY 0x0000060d
4805 int has_snb_msrs(unsigned int family, unsigned int model)
4811 case INTEL_FAM6_SANDYBRIDGE:
4812 case INTEL_FAM6_SANDYBRIDGE_X:
4813 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4814 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4815 case INTEL_FAM6_HASWELL: /* HSW */
4816 case INTEL_FAM6_HASWELL_X: /* HSW */
4817 case INTEL_FAM6_HASWELL_L: /* HSW */
4818 case INTEL_FAM6_HASWELL_G: /* HSW */
4819 case INTEL_FAM6_BROADWELL: /* BDW */
4820 case INTEL_FAM6_BROADWELL_G: /* BDW */
4821 case INTEL_FAM6_BROADWELL_X: /* BDX */
4822 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4823 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4824 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4825 case INTEL_FAM6_ICELAKE_X: /* ICX */
4826 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4827 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4828 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
4829 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4830 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
4837 * HSW ULT added support for C8/C9/C10 MSRs:
4839 * MSR_PKG_C8_RESIDENCY 0x00000630
4840 * MSR_PKG_C9_RESIDENCY 0x00000631
4841 * MSR_PKG_C10_RESIDENCY 0x00000632
4843 * MSR_PKGC8_IRTL 0x00000633
4844 * MSR_PKGC9_IRTL 0x00000634
4845 * MSR_PKGC10_IRTL 0x00000635
4848 int has_c8910_msrs(unsigned int family, unsigned int model)
4854 case INTEL_FAM6_HASWELL_L: /* HSW */
4855 case INTEL_FAM6_BROADWELL: /* BDW */
4856 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4857 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4858 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4859 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4860 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4867 * SKL adds support for additional MSRS:
4869 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4870 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4871 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4872 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4874 int has_skl_msrs(unsigned int family, unsigned int model)
4880 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4881 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4887 int is_slm(unsigned int family, unsigned int model)
4892 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4893 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4899 int is_knl(unsigned int family, unsigned int model)
4904 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4910 int is_cnl(unsigned int family, unsigned int model)
4916 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4923 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4925 if (is_knl(family, model))
4930 #define SLM_BCLK_FREQS 5
4931 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4933 double slm_bclk(void)
4935 unsigned long long msr = 3;
4939 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4940 fprintf(outf, "SLM BCLK: unknown\n");
4943 if (i >= SLM_BCLK_FREQS) {
4944 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
4947 freq = slm_freq_table[i];
4950 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4955 double discover_bclk(unsigned int family, unsigned int model)
4957 if (has_snb_msrs(family, model) || is_knl(family, model))
4959 else if (is_slm(family, model))
4965 int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4967 unsigned int eax, ebx, ecx, edx;
4972 if (cpu_migrate(t->cpu_id)) {
4973 fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id);
4977 if (max_level < 0x1a)
4980 __cpuid(0x1a, eax, ebx, ecx, edx);
4981 eax = (eax >> 24) & 0xFF;
4988 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4989 * the Thermal Control Circuit (TCC) activates.
4990 * This is usually equal to tjMax.
4992 * Older processors do not have this MSR, so there we guess,
4993 * but also allow cmdline over-ride with -T.
4995 * Several MSR temperature values are in units of degrees-C
4996 * below this value, including the Digital Thermal Sensor (DTS),
4997 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4999 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
5001 unsigned long long msr;
5002 unsigned int target_c_local, tcc_offset;
5005 /* tcc_activation_temp is used only for dts or ptm */
5006 if (!(do_dts || do_ptm))
5009 /* this is a per-package concept */
5010 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
5014 if (cpu_migrate(cpu)) {
5015 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
5019 if (tcc_activation_temp_override != 0) {
5020 tcc_activation_temp = tcc_activation_temp_override;
5021 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
5022 cpu, tcc_activation_temp);
5026 /* Temperature Target MSR is Nehalem and newer only */
5027 if (!do_nhm_platform_info)
5030 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
5033 target_c_local = (msr >> 16) & 0xFF;
5036 switch (tcc_offset_bits) {
5038 tcc_offset = (msr >> 24) & 0xF;
5039 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
5040 cpu, msr, target_c_local - tcc_offset, target_c_local, tcc_offset);
5043 tcc_offset = (msr >> 24) & 0x3F;
5044 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
5045 cpu, msr, target_c_local - tcc_offset, target_c_local, tcc_offset);
5048 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
5049 cpu, msr, target_c_local);
5054 if (!target_c_local)
5057 tcc_activation_temp = target_c_local;
5062 tcc_activation_temp = TJMAX_DEFAULT;
5063 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
5064 cpu, tcc_activation_temp);
5069 void decode_feature_control_msr(void)
5071 unsigned long long msr;
5073 if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
5074 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
5076 msr & FEAT_CTL_LOCKED ? "" : "UN-",
5077 msr & (1 << 18) ? "SGX" : "");
5080 void decode_misc_enable_msr(void)
5082 unsigned long long msr;
5087 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
5088 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
5090 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
5091 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
5092 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
5093 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
5094 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
5097 void decode_misc_feature_control(void)
5099 unsigned long long msr;
5101 if (!has_misc_feature_control)
5104 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
5105 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
5107 msr & (0 << 0) ? "No-" : "",
5108 msr & (1 << 0) ? "No-" : "",
5109 msr & (2 << 0) ? "No-" : "",
5110 msr & (3 << 0) ? "No-" : "");
5113 * Decode MSR_MISC_PWR_MGMT
5115 * Decode the bits according to the Nehalem documentation
5116 * bit[0] seems to continue to have same meaning going forward
5119 void decode_misc_pwr_mgmt_msr(void)
5121 unsigned long long msr;
5123 if (!do_nhm_platform_info)
5126 if (no_MSR_MISC_PWR_MGMT)
5129 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
5130 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
5132 msr & (1 << 0) ? "DIS" : "EN",
5133 msr & (1 << 1) ? "EN" : "DIS",
5134 msr & (1 << 8) ? "EN" : "DIS");
5137 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
5139 * This MSRs are present on Silvermont processors,
5140 * Intel Atom processor E3000 series (Baytrail), and friends.
5142 void decode_c6_demotion_policy_msr(void)
5144 unsigned long long msr;
5146 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
5147 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
5148 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5150 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
5151 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
5152 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5156 * When models are the same, for the purpose of turbostat, reuse
5158 unsigned int intel_model_duplicates(unsigned int model)
5162 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
5163 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
5164 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
5165 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
5166 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
5167 return INTEL_FAM6_NEHALEM;
5169 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
5170 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
5171 return INTEL_FAM6_NEHALEM_EX;
5173 case INTEL_FAM6_XEON_PHI_KNM:
5174 return INTEL_FAM6_XEON_PHI_KNL;
5176 case INTEL_FAM6_BROADWELL_X:
5177 case INTEL_FAM6_BROADWELL_D: /* BDX-DE */
5178 return INTEL_FAM6_BROADWELL_X;
5180 case INTEL_FAM6_SKYLAKE_L:
5181 case INTEL_FAM6_SKYLAKE:
5182 case INTEL_FAM6_KABYLAKE_L:
5183 case INTEL_FAM6_KABYLAKE:
5184 case INTEL_FAM6_COMETLAKE_L:
5185 case INTEL_FAM6_COMETLAKE:
5186 return INTEL_FAM6_SKYLAKE_L;
5188 case INTEL_FAM6_ICELAKE_L:
5189 case INTEL_FAM6_ICELAKE_NNPI:
5190 case INTEL_FAM6_TIGERLAKE_L:
5191 case INTEL_FAM6_TIGERLAKE:
5192 case INTEL_FAM6_ROCKETLAKE:
5193 case INTEL_FAM6_LAKEFIELD:
5194 case INTEL_FAM6_ALDERLAKE:
5195 case INTEL_FAM6_ALDERLAKE_L:
5196 return INTEL_FAM6_CANNONLAKE_L;
5198 case INTEL_FAM6_ATOM_TREMONT_L:
5199 return INTEL_FAM6_ATOM_TREMONT;
5201 case INTEL_FAM6_ICELAKE_D:
5202 case INTEL_FAM6_SAPPHIRERAPIDS_X:
5203 return INTEL_FAM6_ICELAKE_X;
5208 void print_dev_latency(void)
5210 char *path = "/dev/cpu_dma_latency";
5215 fd = open(path, O_RDONLY);
5217 warn("fopen %s\n", path);
5221 retval = read(fd, (void *)&value, sizeof(int));
5222 if (retval != sizeof(int)) {
5223 warn("read %s\n", path);
5227 fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n",
5228 value, value == 2000000000 ? "default" : "constrained");
5235 * Linux-perf manages the the HW instructions-retired counter
5236 * by enabling when requested, and hiding rollover
5238 void linux_perf_init(void)
5240 if (!BIC_IS_ENABLED(BIC_IPC))
5243 if (access("/proc/sys/kernel/perf_event_paranoid", F_OK))
5246 fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5247 if (fd_instr_count_percpu == NULL)
5248 err(-1, "calloc fd_instr_count_percpu");
5250 BIC_PRESENT(BIC_IPC);
5253 void process_cpuid()
5255 unsigned int eax, ebx, ecx, edx;
5256 unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
5257 unsigned int has_turbo;
5258 unsigned long long ucode_patch = 0;
5260 eax = ebx = ecx = edx = 0;
5262 __cpuid(0, max_level, ebx, ecx, edx);
5264 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
5266 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
5268 else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
5272 fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n",
5273 (char *)&ebx, (char *)&edx, (char *)&ecx, max_level);
5275 __cpuid(1, fms, ebx, ecx, edx);
5276 family = (fms >> 8) & 0xf;
5277 model = (fms >> 4) & 0xf;
5278 stepping = fms & 0xf;
5280 family += (fms >> 20) & 0xff;
5282 model += ((fms >> 16) & 0xf) << 4;
5286 if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
5287 warnx("get_msr(UCODE)\n");
5290 * check max extended function levels of CPUID.
5291 * This is needed to check for invariant TSC.
5292 * This check is valid for both Intel and AMD.
5294 ebx = ecx = edx = 0;
5295 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
5298 fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n",
5299 family, model, stepping, family, model, stepping, (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
5300 fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
5301 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
5302 ecx_flags & (1 << 0) ? "SSE3" : "-",
5303 ecx_flags & (1 << 3) ? "MONITOR" : "-",
5304 ecx_flags & (1 << 6) ? "SMX" : "-",
5305 ecx_flags & (1 << 7) ? "EIST" : "-",
5306 ecx_flags & (1 << 8) ? "TM2" : "-",
5307 edx_flags & (1 << 4) ? "TSC" : "-",
5308 edx_flags & (1 << 5) ? "MSR" : "-",
5309 edx_flags & (1 << 22) ? "ACPI-TM" : "-",
5310 edx_flags & (1 << 28) ? "HT" : "-",
5311 edx_flags & (1 << 29) ? "TM" : "-");
5313 if (genuine_intel) {
5315 model = intel_model_duplicates(model);
5318 if (!(edx_flags & (1 << 5)))
5319 errx(1, "CPUID: no MSR");
5321 if (max_extended_level >= 0x80000007) {
5324 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
5325 * this check is valid for both Intel and AMD
5327 __cpuid(0x80000007, eax, ebx, ecx, edx);
5328 has_invariant_tsc = edx & (1 << 8);
5332 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
5333 * this check is valid for both Intel and AMD
5336 __cpuid(0x6, eax, ebx, ecx, edx);
5337 has_aperf = ecx & (1 << 0);
5339 BIC_PRESENT(BIC_Avg_MHz);
5340 BIC_PRESENT(BIC_Busy);
5341 BIC_PRESENT(BIC_Bzy_MHz);
5343 do_dts = eax & (1 << 0);
5345 BIC_PRESENT(BIC_CoreTmp);
5346 has_turbo = eax & (1 << 1);
5347 do_ptm = eax & (1 << 6);
5349 BIC_PRESENT(BIC_PkgTmp);
5350 has_hwp = eax & (1 << 7);
5351 has_hwp_notify = eax & (1 << 8);
5352 has_hwp_activity_window = eax & (1 << 9);
5353 has_hwp_epp = eax & (1 << 10);
5354 has_hwp_pkg = eax & (1 << 11);
5355 has_epb = ecx & (1 << 3);
5358 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
5359 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
5360 has_aperf ? "" : "No-",
5361 has_turbo ? "" : "No-",
5362 do_dts ? "" : "No-",
5363 do_ptm ? "" : "No-",
5364 has_hwp ? "" : "No-",
5365 has_hwp_notify ? "" : "No-",
5366 has_hwp_activity_window ? "" : "No-",
5367 has_hwp_epp ? "" : "No-",
5368 has_hwp_pkg ? "" : "No-",
5369 has_epb ? "" : "No-");
5372 decode_misc_enable_msr();
5375 if (max_level >= 0x7 && !quiet) {
5380 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
5382 has_sgx = ebx & (1 << 2);
5383 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
5386 decode_feature_control_msr();
5389 if (max_level >= 0x15) {
5390 unsigned int eax_crystal;
5391 unsigned int ebx_tsc;
5394 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
5396 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
5397 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
5401 if (!quiet && (ebx != 0))
5402 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
5403 eax_crystal, ebx_tsc, crystal_hz);
5405 if (crystal_hz == 0)
5407 case INTEL_FAM6_SKYLAKE_L: /* SKL */
5408 crystal_hz = 24000000; /* 24.0 MHz */
5410 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
5411 crystal_hz = 25000000; /* 25.0 MHz */
5413 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5414 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5415 crystal_hz = 19200000; /* 19.2 MHz */
5422 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
5424 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
5425 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
5429 if (max_level >= 0x16) {
5430 unsigned int base_mhz, max_mhz, bus_mhz, edx;
5433 * CPUID 16H Base MHz, Max MHz, Bus MHz
5435 base_mhz = max_mhz = bus_mhz = edx = 0;
5437 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
5439 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
5440 base_mhz, max_mhz, bus_mhz);
5444 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
5446 BIC_PRESENT(BIC_IRQ);
5447 BIC_PRESENT(BIC_TSC_MHz);
5449 if (probe_nhm_msrs(family, model)) {
5450 do_nhm_platform_info = 1;
5451 BIC_PRESENT(BIC_CPU_c1);
5452 BIC_PRESENT(BIC_CPU_c3);
5453 BIC_PRESENT(BIC_CPU_c6);
5454 BIC_PRESENT(BIC_SMI);
5456 do_snb_cstates = has_snb_msrs(family, model);
5459 BIC_PRESENT(BIC_CPU_c7);
5461 do_irtl_snb = has_snb_msrs(family, model);
5462 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
5463 BIC_PRESENT(BIC_Pkgpc2);
5464 if (pkg_cstate_limit >= PCL__3)
5465 BIC_PRESENT(BIC_Pkgpc3);
5466 if (pkg_cstate_limit >= PCL__6)
5467 BIC_PRESENT(BIC_Pkgpc6);
5468 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
5469 BIC_PRESENT(BIC_Pkgpc7);
5470 if (has_slv_msrs(family, model)) {
5471 BIC_NOT_PRESENT(BIC_Pkgpc2);
5472 BIC_NOT_PRESENT(BIC_Pkgpc3);
5473 BIC_PRESENT(BIC_Pkgpc6);
5474 BIC_NOT_PRESENT(BIC_Pkgpc7);
5475 BIC_PRESENT(BIC_Mod_c6);
5476 use_c1_residency_msr = 1;
5478 if (is_jvl(family, model)) {
5479 BIC_NOT_PRESENT(BIC_CPU_c3);
5480 BIC_NOT_PRESENT(BIC_CPU_c7);
5481 BIC_NOT_PRESENT(BIC_Pkgpc2);
5482 BIC_NOT_PRESENT(BIC_Pkgpc3);
5483 BIC_NOT_PRESENT(BIC_Pkgpc6);
5484 BIC_NOT_PRESENT(BIC_Pkgpc7);
5486 if (is_dnv(family, model)) {
5487 BIC_PRESENT(BIC_CPU_c1);
5488 BIC_NOT_PRESENT(BIC_CPU_c3);
5489 BIC_NOT_PRESENT(BIC_Pkgpc3);
5490 BIC_NOT_PRESENT(BIC_CPU_c7);
5491 BIC_NOT_PRESENT(BIC_Pkgpc7);
5492 use_c1_residency_msr = 1;
5494 if (is_skx(family, model) || is_icx(family, model)) {
5495 BIC_NOT_PRESENT(BIC_CPU_c3);
5496 BIC_NOT_PRESENT(BIC_Pkgpc3);
5497 BIC_NOT_PRESENT(BIC_CPU_c7);
5498 BIC_NOT_PRESENT(BIC_Pkgpc7);
5500 if (is_bdx(family, model)) {
5501 BIC_NOT_PRESENT(BIC_CPU_c7);
5502 BIC_NOT_PRESENT(BIC_Pkgpc7);
5504 if (has_c8910_msrs(family, model)) {
5505 if (pkg_cstate_limit >= PCL__8)
5506 BIC_PRESENT(BIC_Pkgpc8);
5507 if (pkg_cstate_limit >= PCL__9)
5508 BIC_PRESENT(BIC_Pkgpc9);
5509 if (pkg_cstate_limit >= PCL_10)
5510 BIC_PRESENT(BIC_Pkgpc10);
5512 do_irtl_hsw = has_c8910_msrs(family, model);
5513 if (has_skl_msrs(family, model)) {
5514 BIC_PRESENT(BIC_Totl_c0);
5515 BIC_PRESENT(BIC_Any_c0);
5516 BIC_PRESENT(BIC_GFX_c0);
5517 BIC_PRESENT(BIC_CPUGFX);
5519 do_slm_cstates = is_slm(family, model);
5520 do_knl_cstates = is_knl(family, model);
5522 if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) ||
5523 is_ehl(family, model))
5524 BIC_NOT_PRESENT(BIC_CPU_c3);
5527 decode_misc_pwr_mgmt_msr();
5529 if (!quiet && has_slv_msrs(family, model))
5530 decode_c6_demotion_policy_msr();
5532 rapl_probe(family, model);
5533 perf_limit_reasons_probe(family, model);
5534 automatic_cstate_conversion_probe(family, model);
5536 check_tcc_offset(model_orig);
5539 dump_cstate_pstate_config_info(family, model);
5542 print_dev_latency();
5544 dump_sysfs_cstate_config();
5546 dump_sysfs_pstate_config();
5548 if (has_skl_msrs(family, model) || is_ehl(family, model))
5549 calculate_tsc_tweak();
5551 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
5552 BIC_PRESENT(BIC_GFX_rc6);
5554 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5555 BIC_PRESENT(BIC_GFXMHz);
5557 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK))
5558 BIC_PRESENT(BIC_GFXACTMHz);
5560 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
5561 BIC_PRESENT(BIC_CPU_LPI);
5563 BIC_NOT_PRESENT(BIC_CPU_LPI);
5565 if (!access(sys_lpi_file_sysfs, R_OK)) {
5566 sys_lpi_file = sys_lpi_file_sysfs;
5567 BIC_PRESENT(BIC_SYS_LPI);
5568 } else if (!access(sys_lpi_file_debugfs, R_OK)) {
5569 sys_lpi_file = sys_lpi_file_debugfs;
5570 BIC_PRESENT(BIC_SYS_LPI);
5572 sys_lpi_file_sysfs = NULL;
5573 BIC_NOT_PRESENT(BIC_SYS_LPI);
5577 decode_misc_feature_control();
5583 * in /dev/cpu/ return success for names that are numbers
5584 * ie. filter out ".", "..", "microcode".
5586 int dir_filter(const struct dirent *dirp)
5588 if (isdigit(dirp->d_name[0]))
5594 int open_dev_cpu_msr(int dummy1)
5599 void topology_probe()
5602 int max_core_id = 0;
5603 int max_package_id = 0;
5605 int max_siblings = 0;
5607 /* Initialize num_cpus, max_cpu_num */
5610 for_all_proc_cpus(count_cpus);
5611 if (!summary_only && topo.num_cpus > 1)
5612 BIC_PRESENT(BIC_CPU);
5615 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5617 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
5619 err(1, "calloc cpus");
5622 * Allocate and initialize cpu_present_set
5624 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5625 if (cpu_present_set == NULL)
5626 err(3, "CPU_ALLOC");
5627 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5628 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5629 for_all_proc_cpus(mark_cpu_present);
5632 * Validate that all cpus in cpu_subset are also in cpu_present_set
5634 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
5635 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
5636 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
5637 err(1, "cpu%d not present", i);
5641 * Allocate and initialize cpu_affinity_set
5643 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5644 if (cpu_affinity_set == NULL)
5645 err(3, "CPU_ALLOC");
5646 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5647 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5649 for_all_proc_cpus(init_thread_id);
5653 * find max_core_id, max_package_id
5655 for (i = 0; i <= topo.max_cpu_num; ++i) {
5658 if (cpu_is_not_present(i)) {
5660 fprintf(outf, "cpu%d NOT PRESENT\n", i);
5664 cpus[i].logical_cpu_id = i;
5666 /* get package information */
5667 cpus[i].physical_package_id = get_physical_package_id(i);
5668 if (cpus[i].physical_package_id > max_package_id)
5669 max_package_id = cpus[i].physical_package_id;
5671 /* get die information */
5672 cpus[i].die_id = get_die_id(i);
5673 if (cpus[i].die_id > max_die_id)
5674 max_die_id = cpus[i].die_id;
5676 /* get numa node information */
5677 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5678 if (cpus[i].physical_node_id > topo.max_node_num)
5679 topo.max_node_num = cpus[i].physical_node_id;
5681 /* get core information */
5682 cpus[i].physical_core_id = get_core_id(i);
5683 if (cpus[i].physical_core_id > max_core_id)
5684 max_core_id = cpus[i].physical_core_id;
5686 /* get thread information */
5687 siblings = get_thread_siblings(&cpus[i]);
5688 if (siblings > max_siblings)
5689 max_siblings = siblings;
5690 if (cpus[i].thread_id == 0)
5694 topo.cores_per_node = max_core_id + 1;
5696 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
5697 max_core_id, topo.cores_per_node);
5698 if (!summary_only && topo.cores_per_node > 1)
5699 BIC_PRESENT(BIC_Core);
5701 topo.num_die = max_die_id + 1;
5703 fprintf(outf, "max_die_id %d, sizing for %d die\n",
5704 max_die_id, topo.num_die);
5705 if (!summary_only && topo.num_die > 1)
5706 BIC_PRESENT(BIC_Die);
5708 topo.num_packages = max_package_id + 1;
5710 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
5711 max_package_id, topo.num_packages);
5712 if (!summary_only && topo.num_packages > 1)
5713 BIC_PRESENT(BIC_Package);
5717 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
5718 if (!summary_only && topo.nodes_per_pkg > 1)
5719 BIC_PRESENT(BIC_Node);
5721 topo.threads_per_core = max_siblings;
5723 fprintf(outf, "max_siblings %d\n", max_siblings);
5728 for (i = 0; i <= topo.max_cpu_num; ++i) {
5729 if (cpu_is_not_present(i))
5732 "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
5733 i, cpus[i].physical_package_id, cpus[i].die_id,
5734 cpus[i].physical_node_id,
5735 cpus[i].logical_node_id,
5736 cpus[i].physical_core_id,
5743 allocate_counters(struct thread_data **t, struct core_data **c,
5744 struct pkg_data **p)
5747 int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
5749 int num_threads = topo.threads_per_core * num_cores;
5751 *t = calloc(num_threads, sizeof(struct thread_data));
5755 for (i = 0; i < num_threads; i++)
5756 (*t)[i].cpu_id = -1;
5758 *c = calloc(num_cores, sizeof(struct core_data));
5762 for (i = 0; i < num_cores; i++)
5763 (*c)[i].core_id = -1;
5765 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
5769 for (i = 0; i < topo.num_packages; i++)
5770 (*p)[i].package_id = i;
5774 err(1, "calloc counters");
5779 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5781 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
5782 struct pkg_data *pkg_base, int cpu_id)
5784 int pkg_id = cpus[cpu_id].physical_package_id;
5785 int node_id = cpus[cpu_id].logical_node_id;
5786 int core_id = cpus[cpu_id].physical_core_id;
5787 int thread_id = cpus[cpu_id].thread_id;
5788 struct thread_data *t;
5789 struct core_data *c;
5793 /* Workaround for systems where physical_node_id==-1
5794 * and logical_node_id==(-1 - topo.num_cpus)
5799 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
5800 c = GET_CORE(core_base, core_id, node_id, pkg_id);
5801 p = GET_PKG(pkg_base, pkg_id);
5804 if (thread_id == 0) {
5805 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5806 if (cpu_is_first_core_in_package(cpu_id))
5807 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5810 c->core_id = core_id;
5811 p->package_id = pkg_id;
5815 int initialize_counters(int cpu_id)
5817 init_counter(EVEN_COUNTERS, cpu_id);
5818 init_counter(ODD_COUNTERS, cpu_id);
5822 void allocate_output_buffer()
5824 output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
5825 outp = output_buffer;
5827 err(-1, "calloc output buffer");
5829 void allocate_fd_percpu(void)
5831 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5832 if (fd_percpu == NULL)
5833 err(-1, "calloc fd_percpu");
5835 void allocate_irq_buffers(void)
5837 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5838 if (irq_column_2_cpu == NULL)
5839 err(-1, "calloc %d", topo.num_cpus);
5841 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5842 if (irqs_per_cpu == NULL)
5843 err(-1, "calloc %d", topo.max_cpu_num + 1);
5845 void setup_all_buffers(void)
5848 allocate_irq_buffers();
5849 allocate_fd_percpu();
5850 allocate_counters(&thread_even, &core_even, &package_even);
5851 allocate_counters(&thread_odd, &core_odd, &package_odd);
5852 allocate_output_buffer();
5853 for_all_proc_cpus(initialize_counters);
5856 void set_base_cpu(void)
5858 base_cpu = sched_getcpu();
5860 err(-ENODEV, "No valid cpus found");
5863 fprintf(outf, "base_cpu = %d\n", base_cpu);
5866 void turbostat_init()
5868 setup_all_buffers();
5871 check_permissions();
5877 for_all_cpus(print_hwp, ODD_COUNTERS);
5880 for_all_cpus(print_epb, ODD_COUNTERS);
5883 for_all_cpus(print_perf_limit, ODD_COUNTERS);
5886 for_all_cpus(print_rapl, ODD_COUNTERS);
5888 for_all_cpus(set_temperature_target, ODD_COUNTERS);
5890 for_all_cpus(get_cpu_type, ODD_COUNTERS);
5891 for_all_cpus(get_cpu_type, EVEN_COUNTERS);
5894 for_all_cpus(print_thermal, ODD_COUNTERS);
5896 if (!quiet && do_irtl_snb)
5900 int fork_it(char **argv)
5905 snapshot_proc_sysfs_files();
5906 status = for_all_cpus(get_counters, EVEN_COUNTERS);
5907 first_counter_read = 0;
5910 /* clear affinity side-effect of get_counters() */
5911 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5912 gettimeofday(&tv_even, (struct timezone *)NULL);
5917 execvp(argv[0], argv);
5918 err(errno, "exec %s", argv[0]);
5922 if (child_pid == -1)
5925 signal(SIGINT, SIG_IGN);
5926 signal(SIGQUIT, SIG_IGN);
5927 if (waitpid(child_pid, &status, 0) == -1)
5928 err(status, "waitpid");
5930 if (WIFEXITED(status))
5931 status = WEXITSTATUS(status);
5934 * n.b. fork_it() does not check for errors from for_all_cpus()
5935 * because re-starting is problematic when forking
5937 snapshot_proc_sysfs_files();
5938 for_all_cpus(get_counters, ODD_COUNTERS);
5939 gettimeofday(&tv_odd, (struct timezone *)NULL);
5940 timersub(&tv_odd, &tv_even, &tv_delta);
5941 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5942 fprintf(outf, "%s: Counter reset detected\n", progname);
5944 compute_average(EVEN_COUNTERS);
5945 format_all_counters(EVEN_COUNTERS);
5948 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5950 flush_output_stderr();
5955 int get_and_dump_counters(void)
5959 snapshot_proc_sysfs_files();
5960 status = for_all_cpus(get_counters, ODD_COUNTERS);
5964 status = for_all_cpus(dump_counters, ODD_COUNTERS);
5968 flush_output_stdout();
5973 void print_version() {
5974 fprintf(outf, "turbostat version 21.03.12"
5975 " - Len Brown <lenb@kernel.org>\n");
5978 int add_counter(unsigned int msr_num, char *path, char *name,
5979 unsigned int width, enum counter_scope scope,
5980 enum counter_type type, enum counter_format format, int flags)
5982 struct msr_counter *msrp;
5984 msrp = calloc(1, sizeof(struct msr_counter));
5990 msrp->msr_num = msr_num;
5991 strncpy(msrp->name, name, NAME_BYTES - 1);
5993 strncpy(msrp->path, path, PATH_BYTES - 1);
5994 msrp->width = width;
5996 msrp->format = format;
5997 msrp->flags = flags;
6002 msrp->next = sys.tp;
6004 sys.added_thread_counters++;
6005 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
6006 fprintf(stderr, "exceeded max %d added thread counters\n",
6007 MAX_ADDED_COUNTERS);
6013 msrp->next = sys.cp;
6015 sys.added_core_counters++;
6016 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
6017 fprintf(stderr, "exceeded max %d added core counters\n",
6018 MAX_ADDED_COUNTERS);
6024 msrp->next = sys.pp;
6026 sys.added_package_counters++;
6027 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
6028 fprintf(stderr, "exceeded max %d added package counters\n",
6029 MAX_ADDED_COUNTERS);
6038 void parse_add_command(char *add_command)
6042 char name_buffer[NAME_BYTES] = "";
6045 enum counter_scope scope = SCOPE_CPU;
6046 enum counter_type type = COUNTER_CYCLES;
6047 enum counter_format format = FORMAT_DELTA;
6049 while (add_command) {
6051 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
6054 if (sscanf(add_command, "msr%d", &msr_num) == 1)
6057 if (*add_command == '/') {
6062 if (sscanf(add_command, "u%d", &width) == 1) {
6063 if ((width == 32) || (width == 64))
6067 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
6071 if (!strncmp(add_command, "core", strlen("core"))) {
6075 if (!strncmp(add_command, "package", strlen("package"))) {
6076 scope = SCOPE_PACKAGE;
6079 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
6080 type = COUNTER_CYCLES;
6083 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
6084 type = COUNTER_SECONDS;
6087 if (!strncmp(add_command, "usec", strlen("usec"))) {
6088 type = COUNTER_USEC;
6091 if (!strncmp(add_command, "raw", strlen("raw"))) {
6092 format = FORMAT_RAW;
6095 if (!strncmp(add_command, "delta", strlen("delta"))) {
6096 format = FORMAT_DELTA;
6099 if (!strncmp(add_command, "percent", strlen("percent"))) {
6100 format = FORMAT_PERCENT;
6104 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
6107 eos = strchr(name_buffer, ',');
6114 add_command = strchr(add_command, ',');
6116 *add_command = '\0';
6121 if ((msr_num == 0) && (path == NULL)) {
6122 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
6126 /* generate default column header */
6127 if (*name_buffer == '\0') {
6129 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6131 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6134 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
6143 int is_deferred_skip(char *name)
6147 for (i = 0; i < deferred_skip_index; ++i)
6148 if (!strcmp(name, deferred_skip_names[i]))
6153 void probe_sysfs(void)
6161 if (!DO_BIC(BIC_sysfs))
6164 for (state = 10; state >= 0; --state) {
6166 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
6168 input = fopen(path, "r");
6171 if (!fgets(name_buf, sizeof(name_buf), input))
6172 err(1, "%s: failed to read file", path);
6174 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6175 sp = strchr(name_buf, '-');
6177 sp = strchrnul(name_buf, '\n');
6181 remove_underbar(name_buf);
6185 sprintf(path, "cpuidle/state%d/time", state);
6187 if (is_deferred_skip(name_buf))
6190 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
6191 FORMAT_PERCENT, SYSFS_PERCPU);
6194 for (state = 10; state >= 0; --state) {
6196 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
6198 input = fopen(path, "r");
6201 if (!fgets(name_buf, sizeof(name_buf), input))
6202 err(1, "%s: failed to read file", path);
6203 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6204 sp = strchr(name_buf, '-');
6206 sp = strchrnul(name_buf, '\n');
6210 remove_underbar(name_buf);
6212 sprintf(path, "cpuidle/state%d/usage", state);
6214 if (is_deferred_skip(name_buf))
6217 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
6218 FORMAT_DELTA, SYSFS_PERCPU);
6225 * parse cpuset with following syntax
6226 * 1,2,4..6,8-10 and set bits in cpu_subset
6228 void parse_cpu_command(char *optarg)
6230 unsigned int start, end;
6233 if (!strcmp(optarg, "core")) {
6239 if (!strcmp(optarg, "package")) {
6245 if (show_core_only || show_pkg_only)
6248 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
6249 if (cpu_subset == NULL)
6250 err(3, "CPU_ALLOC");
6251 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
6253 CPU_ZERO_S(cpu_subset_size, cpu_subset);
6257 while (next && *next) {
6259 if (*next == '-') /* no negative cpu numbers */
6262 start = strtoul(next, &next, 10);
6264 if (start >= CPU_SUBSET_MAXCPUS)
6266 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6277 next += 1; /* start range */
6278 } else if (*next == '.') {
6281 next += 1; /* start range */
6286 end = strtoul(next, &next, 10);
6290 while (++start <= end) {
6291 if (start >= CPU_SUBSET_MAXCPUS)
6293 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6298 else if (*next != '\0')
6305 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
6311 void cmdline(int argc, char **argv)
6314 int option_index = 0;
6315 static struct option long_options[] = {
6316 {"add", required_argument, 0, 'a'},
6317 {"cpu", required_argument, 0, 'c'},
6318 {"Dump", no_argument, 0, 'D'},
6319 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
6320 {"enable", required_argument, 0, 'e'},
6321 {"interval", required_argument, 0, 'i'},
6322 {"IPC", no_argument, 0, 'I'},
6323 {"num_iterations", required_argument, 0, 'n'},
6324 {"help", no_argument, 0, 'h'},
6325 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
6326 {"Joules", no_argument, 0, 'J'},
6327 {"list", no_argument, 0, 'l'},
6328 {"out", required_argument, 0, 'o'},
6329 {"quiet", no_argument, 0, 'q'},
6330 {"show", required_argument, 0, 's'},
6331 {"Summary", no_argument, 0, 'S'},
6332 {"TCC", required_argument, 0, 'T'},
6333 {"version", no_argument, 0, 'v' },
6339 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
6340 long_options, &option_index)) != -1) {
6343 parse_add_command(optarg);
6346 parse_cpu_command(optarg);
6352 /* --enable specified counter */
6353 bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
6357 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6361 * --hide: do not show those specified
6362 * multiple invocations simply clear more bits in enabled mask
6364 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
6372 double interval = strtod(optarg, NULL);
6374 if (interval < 0.001) {
6375 fprintf(outf, "interval %f seconds is too small\n",
6380 interval_tv.tv_sec = interval_ts.tv_sec = interval;
6381 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
6382 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
6389 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6394 outf = fopen_or_die(optarg, "w");
6400 num_iterations = strtod(optarg, NULL);
6402 if (num_iterations <= 0) {
6403 fprintf(outf, "iterations %d should be positive number\n",
6410 * --show: show only those specified
6411 * The 1st invocation will clear and replace the enabled mask
6412 * subsequent invocations can add to it.
6415 bic_enabled = bic_lookup(optarg, SHOW_LIST);
6417 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
6424 tcc_activation_temp_override = atoi(optarg);
6434 int main(int argc, char **argv)
6437 cmdline(argc, argv);
6446 /* dump counters and exit */
6448 return get_and_dump_counters();
6450 /* list header and exit */
6451 if (list_header_only) {
6453 flush_output_stdout();
6459 * if any params left, it must be a command to fork
6462 return fork_it(argv + optind);