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};
46 unsigned int num_iterations;
50 unsigned int sums_need_wide_columns;
51 unsigned int rapl_joules;
52 unsigned int summary_only;
53 unsigned int list_header_only;
54 unsigned int dump_only;
55 unsigned int do_snb_cstates;
56 unsigned int do_knl_cstates;
57 unsigned int do_slm_cstates;
58 unsigned int use_c1_residency_msr;
59 unsigned int has_aperf;
61 unsigned int do_irtl_snb;
62 unsigned int do_irtl_hsw;
63 unsigned int units = 1000000; /* MHz etc */
64 unsigned int genuine_intel;
65 unsigned int authentic_amd;
66 unsigned int hygon_genuine;
67 unsigned int max_level, max_extended_level;
68 unsigned int has_invariant_tsc;
69 unsigned int do_nhm_platform_info;
70 unsigned int no_MSR_MISC_PWR_MGMT;
71 unsigned int aperf_mperf_multiplier = 1;
74 unsigned int has_base_hz;
75 double tsc_tweak = 1.0;
76 unsigned int show_pkg_only;
77 unsigned int show_core_only;
78 char *output_buffer, *outp;
83 unsigned long long gfx_cur_rc6_ms;
84 unsigned long long cpuidle_cur_cpu_lpi_us;
85 unsigned long long cpuidle_cur_sys_lpi_us;
86 unsigned int gfx_cur_mhz;
87 unsigned int gfx_act_mhz;
88 unsigned int tcc_activation_temp;
89 unsigned int tcc_activation_temp_override;
90 double rapl_power_units, rapl_time_units;
91 double rapl_dram_energy_units, rapl_energy_units;
92 double rapl_joule_counter_range;
93 unsigned int do_core_perf_limit_reasons;
94 unsigned int has_automatic_cstate_conversion;
95 unsigned int dis_cstate_prewake;
96 unsigned int do_gfx_perf_limit_reasons;
97 unsigned int do_ring_perf_limit_reasons;
98 unsigned int crystal_hz;
99 unsigned long long tsc_hz;
101 double discover_bclk(unsigned int family, unsigned int model);
102 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
103 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
104 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
105 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
106 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
107 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
108 unsigned int has_misc_feature_control;
109 unsigned int first_counter_read = 1;
112 #define RAPL_PKG (1 << 0)
113 /* 0x610 MSR_PKG_POWER_LIMIT */
114 /* 0x611 MSR_PKG_ENERGY_STATUS */
115 #define RAPL_PKG_PERF_STATUS (1 << 1)
116 /* 0x613 MSR_PKG_PERF_STATUS */
117 #define RAPL_PKG_POWER_INFO (1 << 2)
118 /* 0x614 MSR_PKG_POWER_INFO */
120 #define RAPL_DRAM (1 << 3)
121 /* 0x618 MSR_DRAM_POWER_LIMIT */
122 /* 0x619 MSR_DRAM_ENERGY_STATUS */
123 #define RAPL_DRAM_PERF_STATUS (1 << 4)
124 /* 0x61b MSR_DRAM_PERF_STATUS */
125 #define RAPL_DRAM_POWER_INFO (1 << 5)
126 /* 0x61c MSR_DRAM_POWER_INFO */
128 #define RAPL_CORES_POWER_LIMIT (1 << 6)
129 /* 0x638 MSR_PP0_POWER_LIMIT */
130 #define RAPL_CORE_POLICY (1 << 7)
131 /* 0x63a MSR_PP0_POLICY */
133 #define RAPL_GFX (1 << 8)
134 /* 0x640 MSR_PP1_POWER_LIMIT */
135 /* 0x641 MSR_PP1_ENERGY_STATUS */
136 /* 0x642 MSR_PP1_POLICY */
138 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
139 /* 0x639 MSR_PP0_ENERGY_STATUS */
140 #define RAPL_PER_CORE_ENERGY (1 << 10)
141 /* Indicates cores energy collection is per-core,
142 * not per-package. */
143 #define RAPL_AMD_F17H (1 << 11)
144 /* 0xc0010299 MSR_RAPL_PWR_UNIT */
145 /* 0xc001029a MSR_CORE_ENERGY_STAT */
146 /* 0xc001029b MSR_PKG_ENERGY_STAT */
147 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
148 #define TJMAX_DEFAULT 100
150 /* MSRs that are not yet in the kernel-provided header. */
151 #define MSR_RAPL_PWR_UNIT 0xc0010299
152 #define MSR_CORE_ENERGY_STAT 0xc001029a
153 #define MSR_PKG_ENERGY_STAT 0xc001029b
155 #define MAX(a, b) ((a) > (b) ? (a) : (b))
158 * buffer size used by sscanf() for added column names
159 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
161 #define NAME_BYTES 20
162 #define PATH_BYTES 128
167 #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
168 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
169 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
170 #define MAX_ADDED_COUNTERS 8
171 #define MAX_ADDED_THREAD_COUNTERS 24
172 #define BITMASK_SIZE 32
175 struct timeval tv_begin;
176 struct timeval tv_end;
177 struct timeval tv_delta;
178 unsigned long long tsc;
179 unsigned long long aperf;
180 unsigned long long mperf;
181 unsigned long long c1;
182 unsigned long long instr_count;
183 unsigned long long irq_count;
184 unsigned int smi_count;
186 unsigned int apic_id;
187 unsigned int x2apic_id;
190 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
191 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
192 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
193 } *thread_even, *thread_odd;
196 unsigned long long c3;
197 unsigned long long c6;
198 unsigned long long c7;
199 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
200 unsigned int core_temp_c;
201 unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */
202 unsigned int core_id;
203 unsigned long long counter[MAX_ADDED_COUNTERS];
204 } *core_even, *core_odd;
207 unsigned long long pc2;
208 unsigned long long pc3;
209 unsigned long long pc6;
210 unsigned long long pc7;
211 unsigned long long pc8;
212 unsigned long long pc9;
213 unsigned long long pc10;
214 unsigned long long cpu_lpi;
215 unsigned long long sys_lpi;
216 unsigned long long pkg_wtd_core_c0;
217 unsigned long long pkg_any_core_c0;
218 unsigned long long pkg_any_gfxe_c0;
219 unsigned long long pkg_both_core_gfxe_c0;
220 long long gfx_rc6_ms;
221 unsigned int gfx_mhz;
222 unsigned int gfx_act_mhz;
223 unsigned int package_id;
224 unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */
225 unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */
226 unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */
227 unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */
228 unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
229 unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
230 unsigned int pkg_temp_c;
231 unsigned long long counter[MAX_ADDED_COUNTERS];
232 } *package_even, *package_odd;
234 #define ODD_COUNTERS thread_odd, core_odd, package_odd
235 #define EVEN_COUNTERS thread_even, core_even, package_even
237 #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
240 topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
241 ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
242 ((core_no) * topo.threads_per_core) + \
245 #define GET_CORE(core_base, core_no, node_no, pkg_no) \
247 ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
248 ((node_no) * topo.cores_per_node) + \
252 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
254 enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
255 enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
256 enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
259 unsigned int msr_num;
260 char name[NAME_BYTES];
261 char path[PATH_BYTES];
263 enum counter_type type;
264 enum counter_format format;
265 struct msr_counter *next;
267 #define FLAGS_HIDE (1 << 0)
268 #define FLAGS_SHOW (1 << 1)
269 #define SYSFS_PERCPU (1 << 1)
273 * The accumulated sum of MSR is defined as a monotonic
274 * increasing MSR, it will be accumulated periodically,
275 * despite its register's bit width.
287 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr);
289 struct msr_sum_array {
290 /* get_msr_sum() = sum + (get_msr() - last) */
292 /*The accumulated MSR value is updated by the timer*/
293 unsigned long long sum;
294 /*The MSR footprint recorded in last timer*/
295 unsigned long long last;
296 } entries[IDX_COUNT];
299 /* The percpu MSR sum array.*/
300 struct msr_sum_array *per_cpu_msr_sum;
302 off_t idx_to_offset(int idx)
308 if (do_rapl & RAPL_AMD_F17H)
309 offset = MSR_PKG_ENERGY_STAT;
311 offset = MSR_PKG_ENERGY_STATUS;
313 case IDX_DRAM_ENERGY:
314 offset = MSR_DRAM_ENERGY_STATUS;
317 offset = MSR_PP0_ENERGY_STATUS;
320 offset = MSR_PP1_ENERGY_STATUS;
323 offset = MSR_PKG_PERF_STATUS;
326 offset = MSR_DRAM_PERF_STATUS;
334 int offset_to_idx(off_t offset)
339 case MSR_PKG_ENERGY_STATUS:
340 case MSR_PKG_ENERGY_STAT:
341 idx = IDX_PKG_ENERGY;
343 case MSR_DRAM_ENERGY_STATUS:
344 idx = IDX_DRAM_ENERGY;
346 case MSR_PP0_ENERGY_STATUS:
347 idx = IDX_PP0_ENERGY;
349 case MSR_PP1_ENERGY_STATUS:
350 idx = IDX_PP1_ENERGY;
352 case MSR_PKG_PERF_STATUS:
355 case MSR_DRAM_PERF_STATUS:
364 int idx_valid(int idx)
368 return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
369 case IDX_DRAM_ENERGY:
370 return do_rapl & RAPL_DRAM;
372 return do_rapl & RAPL_CORES_ENERGY_STATUS;
374 return do_rapl & RAPL_GFX;
376 return do_rapl & RAPL_PKG_PERF_STATUS;
378 return do_rapl & RAPL_DRAM_PERF_STATUS;
383 struct sys_counters {
384 unsigned int added_thread_counters;
385 unsigned int added_core_counters;
386 unsigned int added_package_counters;
387 struct msr_counter *tp;
388 struct msr_counter *cp;
389 struct msr_counter *pp;
392 struct system_summary {
393 struct thread_data threads;
394 struct core_data cores;
395 struct pkg_data packages;
398 struct cpu_topology {
399 int physical_package_id;
402 int physical_node_id;
403 int logical_node_id; /* 0-based count within the package */
404 int physical_core_id;
406 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
418 int threads_per_core;
421 struct timeval tv_even, tv_odd, tv_delta;
423 int *irq_column_2_cpu; /* /proc/interrupts column numbers */
424 int *irqs_per_cpu; /* indexed by cpu_num */
426 void setup_all_buffers(void);
429 char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
430 char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
432 int cpu_is_not_present(int cpu)
434 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
437 * run func(thread, core, package) in topology order
438 * skip non-present cpus
441 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
442 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
444 int retval, pkg_no, core_no, thread_no, node_no;
446 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
447 for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
448 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
449 for (thread_no = 0; thread_no <
450 topo.threads_per_core; ++thread_no) {
451 struct thread_data *t;
455 t = GET_THREAD(thread_base, thread_no,
459 if (cpu_is_not_present(t->cpu_id))
462 c = GET_CORE(core_base, core_no,
464 p = GET_PKG(pkg_base, pkg_no);
466 retval = func(t, c, p);
476 int cpu_migrate(int cpu)
478 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
479 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
480 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
485 int get_msr_fd(int cpu)
495 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
496 fd = open(pathname, O_RDONLY);
498 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
505 static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
507 return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
510 static int perf_instr_count_open(int cpu_num)
512 struct perf_event_attr pea;
515 memset(&pea, 0, sizeof(struct perf_event_attr));
516 pea.type = PERF_TYPE_HARDWARE;
517 pea.size = sizeof(struct perf_event_attr);
518 pea.config = PERF_COUNT_HW_INSTRUCTIONS;
520 /* counter for cpu_num, including user + kernel and all processes */
521 fd = perf_event_open(&pea, -1, cpu_num, -1, 0);
523 err(-1, "cpu%d: perf instruction counter\n", cpu_num);
528 int get_instr_count_fd(int cpu)
530 if (fd_instr_count_percpu[cpu])
531 return fd_instr_count_percpu[cpu];
533 fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu);
535 return fd_instr_count_percpu[cpu];
538 int get_msr(int cpu, off_t offset, unsigned long long *msr)
542 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
544 if (retval != sizeof *msr)
545 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
551 * This list matches the column headers, except
552 * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
553 * 2. Core and CPU are moved to the end, we can't have strings that contain them
554 * matching on them for --show and --hide.
556 struct msr_counter bic[] = {
558 { 0x0, "Time_Of_Day_Seconds" },
566 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
612 #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
613 #define BIC_USEC (1ULL << 0)
614 #define BIC_TOD (1ULL << 1)
615 #define BIC_Package (1ULL << 2)
616 #define BIC_Node (1ULL << 3)
617 #define BIC_Avg_MHz (1ULL << 4)
618 #define BIC_Busy (1ULL << 5)
619 #define BIC_Bzy_MHz (1ULL << 6)
620 #define BIC_TSC_MHz (1ULL << 7)
621 #define BIC_IRQ (1ULL << 8)
622 #define BIC_SMI (1ULL << 9)
623 #define BIC_sysfs (1ULL << 10)
624 #define BIC_CPU_c1 (1ULL << 11)
625 #define BIC_CPU_c3 (1ULL << 12)
626 #define BIC_CPU_c6 (1ULL << 13)
627 #define BIC_CPU_c7 (1ULL << 14)
628 #define BIC_ThreadC (1ULL << 15)
629 #define BIC_CoreTmp (1ULL << 16)
630 #define BIC_CoreCnt (1ULL << 17)
631 #define BIC_PkgTmp (1ULL << 18)
632 #define BIC_GFX_rc6 (1ULL << 19)
633 #define BIC_GFXMHz (1ULL << 20)
634 #define BIC_Pkgpc2 (1ULL << 21)
635 #define BIC_Pkgpc3 (1ULL << 22)
636 #define BIC_Pkgpc6 (1ULL << 23)
637 #define BIC_Pkgpc7 (1ULL << 24)
638 #define BIC_Pkgpc8 (1ULL << 25)
639 #define BIC_Pkgpc9 (1ULL << 26)
640 #define BIC_Pkgpc10 (1ULL << 27)
641 #define BIC_CPU_LPI (1ULL << 28)
642 #define BIC_SYS_LPI (1ULL << 29)
643 #define BIC_PkgWatt (1ULL << 30)
644 #define BIC_CorWatt (1ULL << 31)
645 #define BIC_GFXWatt (1ULL << 32)
646 #define BIC_PkgCnt (1ULL << 33)
647 #define BIC_RAMWatt (1ULL << 34)
648 #define BIC_PKG__ (1ULL << 35)
649 #define BIC_RAM__ (1ULL << 36)
650 #define BIC_Pkg_J (1ULL << 37)
651 #define BIC_Cor_J (1ULL << 38)
652 #define BIC_GFX_J (1ULL << 39)
653 #define BIC_RAM_J (1ULL << 40)
654 #define BIC_Mod_c6 (1ULL << 41)
655 #define BIC_Totl_c0 (1ULL << 42)
656 #define BIC_Any_c0 (1ULL << 43)
657 #define BIC_GFX_c0 (1ULL << 44)
658 #define BIC_CPUGFX (1ULL << 45)
659 #define BIC_Core (1ULL << 46)
660 #define BIC_CPU (1ULL << 47)
661 #define BIC_APIC (1ULL << 48)
662 #define BIC_X2APIC (1ULL << 49)
663 #define BIC_Die (1ULL << 50)
664 #define BIC_GFXACTMHz (1ULL << 51)
665 #define BIC_IPC (1ULL << 52)
667 #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
669 unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
670 unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
672 #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
673 #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
674 #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
675 #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
676 #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
677 #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT)
680 #define MAX_DEFERRED 16
681 char *deferred_skip_names[MAX_DEFERRED];
682 int deferred_skip_index;
685 * HIDE_LIST - hide this list of counters, show the rest [default]
686 * SHOW_LIST - show this list of counters, hide the rest
688 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
693 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
695 "Turbostat forks the specified COMMAND and prints statistics\n"
696 "when COMMAND completes.\n"
697 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
698 "to print statistics, until interrupted.\n"
699 " -a, --add add a counter\n"
700 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
701 " -c, --cpu cpu-set limit output to summary plus cpu-set:\n"
702 " {core | package | j,k,l..m,n-p }\n"
703 " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n"
704 " -D, --Dump displays the raw counter values\n"
705 " -e, --enable [all | column]\n"
706 " shows all or the specified disabled column\n"
707 " -H, --hide [column|column,column,...]\n"
708 " hide the specified column(s)\n"
709 " -i, --interval sec.subsec\n"
710 " Override default 5-second measurement interval\n"
711 " -J, --Joules displays energy in Joules instead of Watts\n"
712 " -l, --list list column headers only\n"
713 " -n, --num_iterations num\n"
714 " number of the measurement iterations\n"
716 " create or truncate \"file\" for all output\n"
717 " -q, --quiet skip decoding system configuration header\n"
718 " -s, --show [column|column,column,...]\n"
719 " show only the specified column(s)\n"
721 " limits output to 1-line system summary per interval\n"
722 " -T, --TCC temperature\n"
723 " sets the Thermal Control Circuit temperature in\n"
725 " -h, --help print this help message\n"
726 " -v, --version print version information\n"
728 "For more help, run \"man turbostat\"\n");
733 * for all the strings in comma separate name_list,
734 * set the approprate bit in return value.
736 unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
739 unsigned long long retval = 0;
744 comma = strchr(name_list, ',');
749 if (!strcmp(name_list, "all"))
752 for (i = 0; i < MAX_BIC; ++i) {
753 if (!strcmp(name_list, bic[i].name)) {
754 retval |= (1ULL << i);
759 if (mode == SHOW_LIST) {
760 fprintf(stderr, "Invalid counter name: %s\n", name_list);
763 deferred_skip_names[deferred_skip_index++] = name_list;
765 fprintf(stderr, "deferred \"%s\"\n", name_list);
766 if (deferred_skip_index >= MAX_DEFERRED) {
767 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
768 MAX_DEFERRED, name_list);
783 void print_header(char *delim)
785 struct msr_counter *mp;
788 if (DO_BIC(BIC_USEC))
789 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
791 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
792 if (DO_BIC(BIC_Package))
793 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
795 outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
796 if (DO_BIC(BIC_Node))
797 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
798 if (DO_BIC(BIC_Core))
799 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
801 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
802 if (DO_BIC(BIC_APIC))
803 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
804 if (DO_BIC(BIC_X2APIC))
805 outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
806 if (DO_BIC(BIC_Avg_MHz))
807 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
808 if (DO_BIC(BIC_Busy))
809 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
810 if (DO_BIC(BIC_Bzy_MHz))
811 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
812 if (DO_BIC(BIC_TSC_MHz))
813 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
816 outp += sprintf(outp, "%sIPC", (printed++ ? delim : ""));
818 if (DO_BIC(BIC_IRQ)) {
819 if (sums_need_wide_columns)
820 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
822 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
826 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
828 for (mp = sys.tp; mp; mp = mp->next) {
830 if (mp->format == FORMAT_RAW) {
832 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
834 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
836 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
837 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
839 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
843 if (DO_BIC(BIC_CPU_c1))
844 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
845 if (DO_BIC(BIC_CPU_c3))
846 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
847 if (DO_BIC(BIC_CPU_c6))
848 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
849 if (DO_BIC(BIC_CPU_c7))
850 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
852 if (DO_BIC(BIC_Mod_c6))
853 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
855 if (DO_BIC(BIC_CoreTmp))
856 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
858 if (do_rapl && !rapl_joules) {
859 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
860 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
861 } else if (do_rapl && rapl_joules) {
862 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
863 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
866 for (mp = sys.cp; mp; mp = mp->next) {
867 if (mp->format == FORMAT_RAW) {
869 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
871 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
873 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
874 outp += sprintf(outp, "%s%8s", delim, mp->name);
876 outp += sprintf(outp, "%s%s", delim, mp->name);
880 if (DO_BIC(BIC_PkgTmp))
881 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
883 if (DO_BIC(BIC_GFX_rc6))
884 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
886 if (DO_BIC(BIC_GFXMHz))
887 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
889 if (DO_BIC(BIC_GFXACTMHz))
890 outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : ""));
892 if (DO_BIC(BIC_Totl_c0))
893 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
894 if (DO_BIC(BIC_Any_c0))
895 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
896 if (DO_BIC(BIC_GFX_c0))
897 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
898 if (DO_BIC(BIC_CPUGFX))
899 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
901 if (DO_BIC(BIC_Pkgpc2))
902 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
903 if (DO_BIC(BIC_Pkgpc3))
904 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
905 if (DO_BIC(BIC_Pkgpc6))
906 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
907 if (DO_BIC(BIC_Pkgpc7))
908 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
909 if (DO_BIC(BIC_Pkgpc8))
910 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
911 if (DO_BIC(BIC_Pkgpc9))
912 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
913 if (DO_BIC(BIC_Pkgpc10))
914 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
915 if (DO_BIC(BIC_CPU_LPI))
916 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
917 if (DO_BIC(BIC_SYS_LPI))
918 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
920 if (do_rapl && !rapl_joules) {
921 if (DO_BIC(BIC_PkgWatt))
922 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
923 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
924 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
925 if (DO_BIC(BIC_GFXWatt))
926 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
927 if (DO_BIC(BIC_RAMWatt))
928 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
929 if (DO_BIC(BIC_PKG__))
930 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
931 if (DO_BIC(BIC_RAM__))
932 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
933 } else if (do_rapl && rapl_joules) {
934 if (DO_BIC(BIC_Pkg_J))
935 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
936 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
937 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
938 if (DO_BIC(BIC_GFX_J))
939 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
940 if (DO_BIC(BIC_RAM_J))
941 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
942 if (DO_BIC(BIC_PKG__))
943 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
944 if (DO_BIC(BIC_RAM__))
945 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
947 for (mp = sys.pp; mp; mp = mp->next) {
948 if (mp->format == FORMAT_RAW) {
950 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
952 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
954 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
955 outp += sprintf(outp, "%s%8s", delim, mp->name);
957 outp += sprintf(outp, "%s%s", delim, mp->name);
961 outp += sprintf(outp, "\n");
964 int dump_counters(struct thread_data *t, struct core_data *c,
968 struct msr_counter *mp;
970 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
973 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
974 t->cpu_id, t->flags);
975 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
976 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
977 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
978 outp += sprintf(outp, "c1: %016llX\n", t->c1);
981 outp += sprintf(outp, "IPC: %lld\n", t->instr_count);
984 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
986 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
988 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
989 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
990 i, mp->msr_num, t->counter[i]);
995 outp += sprintf(outp, "core: %d\n", c->core_id);
996 outp += sprintf(outp, "c3: %016llX\n", c->c3);
997 outp += sprintf(outp, "c6: %016llX\n", c->c6);
998 outp += sprintf(outp, "c7: %016llX\n", c->c7);
999 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
1000 outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
1002 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1003 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
1004 i, mp->msr_num, c->counter[i]);
1006 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
1010 outp += sprintf(outp, "package: %d\n", p->package_id);
1012 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
1013 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
1014 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
1015 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
1017 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
1018 if (DO_BIC(BIC_Pkgpc3))
1019 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
1020 if (DO_BIC(BIC_Pkgpc6))
1021 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
1022 if (DO_BIC(BIC_Pkgpc7))
1023 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
1024 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
1025 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
1026 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
1027 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
1028 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
1029 outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
1030 outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
1031 outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
1032 outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
1033 outp += sprintf(outp, "Throttle PKG: %0llX\n",
1034 p->rapl_pkg_perf_status);
1035 outp += sprintf(outp, "Throttle RAM: %0llX\n",
1036 p->rapl_dram_perf_status);
1037 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
1039 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1040 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
1041 i, mp->msr_num, p->counter[i]);
1045 outp += sprintf(outp, "\n");
1051 * column formatting convention & formats
1053 int format_counters(struct thread_data *t, struct core_data *c,
1056 double interval_float, tsc;
1059 struct msr_counter *mp;
1063 /* if showing only 1st thread in core and this isn't one, bail out */
1064 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1067 /* if showing only 1st thread in pkg and this isn't one, bail out */
1068 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1071 /*if not summary line and --cpu is used */
1072 if ((t != &average.threads) &&
1073 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
1076 if (DO_BIC(BIC_USEC)) {
1077 /* on each row, print how many usec each timestamp took to gather */
1080 timersub(&t->tv_end, &t->tv_begin, &tv);
1081 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
1084 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
1085 if (DO_BIC(BIC_TOD))
1086 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
1088 interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0;
1090 tsc = t->tsc * tsc_tweak;
1092 /* topo columns, print blanks on 1st (average) line */
1093 if (t == &average.threads) {
1094 if (DO_BIC(BIC_Package))
1095 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1096 if (DO_BIC(BIC_Die))
1097 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1098 if (DO_BIC(BIC_Node))
1099 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1100 if (DO_BIC(BIC_Core))
1101 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1102 if (DO_BIC(BIC_CPU))
1103 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1104 if (DO_BIC(BIC_APIC))
1105 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1106 if (DO_BIC(BIC_X2APIC))
1107 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1109 if (DO_BIC(BIC_Package)) {
1111 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
1113 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1115 if (DO_BIC(BIC_Die)) {
1117 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
1119 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1121 if (DO_BIC(BIC_Node)) {
1123 outp += sprintf(outp, "%s%d",
1124 (printed++ ? delim : ""),
1125 cpus[t->cpu_id].physical_node_id);
1127 outp += sprintf(outp, "%s-",
1128 (printed++ ? delim : ""));
1130 if (DO_BIC(BIC_Core)) {
1132 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
1134 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1136 if (DO_BIC(BIC_CPU))
1137 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
1138 if (DO_BIC(BIC_APIC))
1139 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
1140 if (DO_BIC(BIC_X2APIC))
1141 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
1144 if (DO_BIC(BIC_Avg_MHz))
1145 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1146 1.0 / units * t->aperf / interval_float);
1148 if (DO_BIC(BIC_Busy))
1149 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
1151 if (DO_BIC(BIC_Bzy_MHz)) {
1153 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
1155 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1156 tsc / units * t->aperf / t->mperf / interval_float);
1159 if (DO_BIC(BIC_TSC_MHz))
1160 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
1162 if (DO_BIC(BIC_IPC))
1163 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf);
1166 if (DO_BIC(BIC_IRQ)) {
1167 if (sums_need_wide_columns)
1168 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
1170 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
1174 if (DO_BIC(BIC_SMI))
1175 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
1177 /* Added counters */
1178 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1179 if (mp->format == FORMAT_RAW) {
1180 if (mp->width == 32)
1181 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
1183 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1184 } else if (mp->format == FORMAT_DELTA) {
1185 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1186 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
1188 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1189 } else if (mp->format == FORMAT_PERCENT) {
1190 if (mp->type == COUNTER_USEC)
1191 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
1193 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
1198 if (DO_BIC(BIC_CPU_c1))
1199 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
1202 /* print per-core data only for 1st thread in core */
1203 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1206 if (DO_BIC(BIC_CPU_c3))
1207 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1208 if (DO_BIC(BIC_CPU_c6))
1209 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1210 if (DO_BIC(BIC_CPU_c7))
1211 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1214 if (DO_BIC(BIC_Mod_c6))
1215 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
1217 if (DO_BIC(BIC_CoreTmp))
1218 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1220 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1221 if (mp->format == FORMAT_RAW) {
1222 if (mp->width == 32)
1223 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1225 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1226 } else if (mp->format == FORMAT_DELTA) {
1227 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1228 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
1230 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1231 } else if (mp->format == FORMAT_PERCENT) {
1232 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1238 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
1239 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
1240 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
1241 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
1243 /* print per-package data only for 1st core in package */
1244 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1248 if (DO_BIC(BIC_PkgTmp))
1249 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1252 if (DO_BIC(BIC_GFX_rc6)) {
1253 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
1254 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
1256 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
1257 p->gfx_rc6_ms / 10.0 / interval_float);
1262 if (DO_BIC(BIC_GFXMHz))
1263 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
1266 if (DO_BIC(BIC_GFXACTMHz))
1267 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz);
1269 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1270 if (DO_BIC(BIC_Totl_c0))
1271 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1272 if (DO_BIC(BIC_Any_c0))
1273 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1274 if (DO_BIC(BIC_GFX_c0))
1275 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1276 if (DO_BIC(BIC_CPUGFX))
1277 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
1279 if (DO_BIC(BIC_Pkgpc2))
1280 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
1281 if (DO_BIC(BIC_Pkgpc3))
1282 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
1283 if (DO_BIC(BIC_Pkgpc6))
1284 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
1285 if (DO_BIC(BIC_Pkgpc7))
1286 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
1287 if (DO_BIC(BIC_Pkgpc8))
1288 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
1289 if (DO_BIC(BIC_Pkgpc9))
1290 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
1291 if (DO_BIC(BIC_Pkgpc10))
1292 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1294 if (DO_BIC(BIC_CPU_LPI))
1295 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1296 if (DO_BIC(BIC_SYS_LPI))
1297 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1299 if (DO_BIC(BIC_PkgWatt))
1300 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1301 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1302 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1303 if (DO_BIC(BIC_GFXWatt))
1304 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1305 if (DO_BIC(BIC_RAMWatt))
1306 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1307 if (DO_BIC(BIC_Pkg_J))
1308 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1309 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1310 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1311 if (DO_BIC(BIC_GFX_J))
1312 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1313 if (DO_BIC(BIC_RAM_J))
1314 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1315 if (DO_BIC(BIC_PKG__))
1316 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1317 if (DO_BIC(BIC_RAM__))
1318 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1320 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1321 if (mp->format == FORMAT_RAW) {
1322 if (mp->width == 32)
1323 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1325 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1326 } else if (mp->format == FORMAT_DELTA) {
1327 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1328 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
1330 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1331 } else if (mp->format == FORMAT_PERCENT) {
1332 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1337 if (*(outp - 1) != '\n')
1338 outp += sprintf(outp, "\n");
1343 void flush_output_stdout(void)
1352 fputs(output_buffer, filep);
1355 outp = output_buffer;
1357 void flush_output_stderr(void)
1359 fputs(output_buffer, outf);
1361 outp = output_buffer;
1363 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1367 if (!printed || !summary_only)
1370 format_counters(&average.threads, &average.cores, &average.packages);
1377 for_all_cpus(format_counters, t, c, p);
1380 #define DELTA_WRAP32(new, old) \
1381 old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1384 delta_package(struct pkg_data *new, struct pkg_data *old)
1387 struct msr_counter *mp;
1390 if (DO_BIC(BIC_Totl_c0))
1391 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1392 if (DO_BIC(BIC_Any_c0))
1393 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1394 if (DO_BIC(BIC_GFX_c0))
1395 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1396 if (DO_BIC(BIC_CPUGFX))
1397 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1399 old->pc2 = new->pc2 - old->pc2;
1400 if (DO_BIC(BIC_Pkgpc3))
1401 old->pc3 = new->pc3 - old->pc3;
1402 if (DO_BIC(BIC_Pkgpc6))
1403 old->pc6 = new->pc6 - old->pc6;
1404 if (DO_BIC(BIC_Pkgpc7))
1405 old->pc7 = new->pc7 - old->pc7;
1406 old->pc8 = new->pc8 - old->pc8;
1407 old->pc9 = new->pc9 - old->pc9;
1408 old->pc10 = new->pc10 - old->pc10;
1409 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1410 old->sys_lpi = new->sys_lpi - old->sys_lpi;
1411 old->pkg_temp_c = new->pkg_temp_c;
1413 /* flag an error when rc6 counter resets/wraps */
1414 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1415 old->gfx_rc6_ms = -1;
1417 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1419 old->gfx_mhz = new->gfx_mhz;
1420 old->gfx_act_mhz = new->gfx_act_mhz;
1422 old->energy_pkg = new->energy_pkg - old->energy_pkg;
1423 old->energy_cores = new->energy_cores - old->energy_cores;
1424 old->energy_gfx = new->energy_gfx - old->energy_gfx;
1425 old->energy_dram = new->energy_dram - old->energy_dram;
1426 old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status;
1427 old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status;
1429 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1430 if (mp->format == FORMAT_RAW)
1431 old->counter[i] = new->counter[i];
1433 old->counter[i] = new->counter[i] - old->counter[i];
1440 delta_core(struct core_data *new, struct core_data *old)
1443 struct msr_counter *mp;
1445 old->c3 = new->c3 - old->c3;
1446 old->c6 = new->c6 - old->c6;
1447 old->c7 = new->c7 - old->c7;
1448 old->core_temp_c = new->core_temp_c;
1449 old->mc6_us = new->mc6_us - old->mc6_us;
1451 DELTA_WRAP32(new->core_energy, old->core_energy);
1453 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1454 if (mp->format == FORMAT_RAW)
1455 old->counter[i] = new->counter[i];
1457 old->counter[i] = new->counter[i] - old->counter[i];
1461 int soft_c1_residency_display(int bic)
1463 if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
1466 return DO_BIC_READ(bic);
1473 delta_thread(struct thread_data *new, struct thread_data *old,
1474 struct core_data *core_delta)
1477 struct msr_counter *mp;
1479 /* we run cpuid just the 1st time, copy the results */
1480 if (DO_BIC(BIC_APIC))
1481 new->apic_id = old->apic_id;
1482 if (DO_BIC(BIC_X2APIC))
1483 new->x2apic_id = old->x2apic_id;
1486 * the timestamps from start of measurement interval are in "old"
1487 * the timestamp from end of measurement interval are in "new"
1488 * over-write old w/ new so we can print end of interval values
1491 timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
1492 old->tv_begin = new->tv_begin;
1493 old->tv_end = new->tv_end;
1495 old->tsc = new->tsc - old->tsc;
1497 /* check for TSC < 1 Mcycles over interval */
1498 if (old->tsc < (1000 * 1000))
1499 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1500 "You can disable all c-states by booting with \"idle=poll\"\n"
1501 "or just the deep ones with \"processor.max_cstate=1\"");
1503 old->c1 = new->c1 - old->c1;
1505 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
1506 soft_c1_residency_display(BIC_Avg_MHz)) {
1507 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1508 old->aperf = new->aperf - old->aperf;
1509 old->mperf = new->mperf - old->mperf;
1516 if (use_c1_residency_msr) {
1518 * Some models have a dedicated C1 residency MSR,
1519 * which should be more accurate than the derivation below.
1523 * As counter collection is not atomic,
1524 * it is possible for mperf's non-halted cycles + idle states
1525 * to exceed TSC's all cycles: show c1 = 0% in that case.
1527 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1530 /* normal case, derive c1 */
1531 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1532 - core_delta->c6 - core_delta->c7;
1536 if (old->mperf == 0) {
1538 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1539 old->mperf = 1; /* divide by 0 protection */
1542 if (DO_BIC(BIC_IPC))
1543 old->instr_count = new->instr_count - old->instr_count;
1545 if (DO_BIC(BIC_IRQ))
1546 old->irq_count = new->irq_count - old->irq_count;
1548 if (DO_BIC(BIC_SMI))
1549 old->smi_count = new->smi_count - old->smi_count;
1551 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1552 if (mp->format == FORMAT_RAW)
1553 old->counter[i] = new->counter[i];
1555 old->counter[i] = new->counter[i] - old->counter[i];
1560 int delta_cpu(struct thread_data *t, struct core_data *c,
1561 struct pkg_data *p, struct thread_data *t2,
1562 struct core_data *c2, struct pkg_data *p2)
1566 /* calculate core delta only for 1st thread in core */
1567 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1570 /* always calculate thread delta */
1571 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1575 /* calculate package delta only for 1st core in package */
1576 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1577 retval = delta_package(p, p2);
1582 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1585 struct msr_counter *mp;
1587 t->tv_begin.tv_sec = 0;
1588 t->tv_begin.tv_usec = 0;
1589 t->tv_end.tv_sec = 0;
1590 t->tv_end.tv_usec = 0;
1591 t->tv_delta.tv_sec = 0;
1592 t->tv_delta.tv_usec = 0;
1604 /* tells format_counters to dump all fields from this set */
1605 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1614 p->pkg_wtd_core_c0 = 0;
1615 p->pkg_any_core_c0 = 0;
1616 p->pkg_any_gfxe_c0 = 0;
1617 p->pkg_both_core_gfxe_c0 = 0;
1620 if (DO_BIC(BIC_Pkgpc3))
1622 if (DO_BIC(BIC_Pkgpc6))
1624 if (DO_BIC(BIC_Pkgpc7))
1634 p->energy_cores = 0;
1636 p->rapl_pkg_perf_status = 0;
1637 p->rapl_dram_perf_status = 0;
1643 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1646 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1649 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1652 int sum_counters(struct thread_data *t, struct core_data *c,
1656 struct msr_counter *mp;
1658 /* copy un-changing apic_id's */
1659 if (DO_BIC(BIC_APIC))
1660 average.threads.apic_id = t->apic_id;
1661 if (DO_BIC(BIC_X2APIC))
1662 average.threads.x2apic_id = t->x2apic_id;
1664 /* remember first tv_begin */
1665 if (average.threads.tv_begin.tv_sec == 0)
1666 average.threads.tv_begin = t->tv_begin;
1668 /* remember last tv_end */
1669 average.threads.tv_end = t->tv_end;
1671 average.threads.tsc += t->tsc;
1672 average.threads.aperf += t->aperf;
1673 average.threads.mperf += t->mperf;
1674 average.threads.c1 += t->c1;
1676 average.threads.instr_count += t->instr_count;
1678 average.threads.irq_count += t->irq_count;
1679 average.threads.smi_count += t->smi_count;
1681 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1682 if (mp->format == FORMAT_RAW)
1684 average.threads.counter[i] += t->counter[i];
1687 /* sum per-core values only for 1st thread in core */
1688 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1691 average.cores.c3 += c->c3;
1692 average.cores.c6 += c->c6;
1693 average.cores.c7 += c->c7;
1694 average.cores.mc6_us += c->mc6_us;
1696 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1698 average.cores.core_energy += c->core_energy;
1700 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1701 if (mp->format == FORMAT_RAW)
1703 average.cores.counter[i] += c->counter[i];
1706 /* sum per-pkg values only for 1st core in pkg */
1707 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1710 if (DO_BIC(BIC_Totl_c0))
1711 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1712 if (DO_BIC(BIC_Any_c0))
1713 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1714 if (DO_BIC(BIC_GFX_c0))
1715 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1716 if (DO_BIC(BIC_CPUGFX))
1717 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1719 average.packages.pc2 += p->pc2;
1720 if (DO_BIC(BIC_Pkgpc3))
1721 average.packages.pc3 += p->pc3;
1722 if (DO_BIC(BIC_Pkgpc6))
1723 average.packages.pc6 += p->pc6;
1724 if (DO_BIC(BIC_Pkgpc7))
1725 average.packages.pc7 += p->pc7;
1726 average.packages.pc8 += p->pc8;
1727 average.packages.pc9 += p->pc9;
1728 average.packages.pc10 += p->pc10;
1730 average.packages.cpu_lpi = p->cpu_lpi;
1731 average.packages.sys_lpi = p->sys_lpi;
1733 average.packages.energy_pkg += p->energy_pkg;
1734 average.packages.energy_dram += p->energy_dram;
1735 average.packages.energy_cores += p->energy_cores;
1736 average.packages.energy_gfx += p->energy_gfx;
1738 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1739 average.packages.gfx_mhz = p->gfx_mhz;
1740 average.packages.gfx_act_mhz = p->gfx_act_mhz;
1742 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1744 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1745 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1747 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1748 if (mp->format == FORMAT_RAW)
1750 average.packages.counter[i] += p->counter[i];
1755 * sum the counters for all cpus in the system
1756 * compute the weighted average
1758 void compute_average(struct thread_data *t, struct core_data *c,
1762 struct msr_counter *mp;
1764 clear_counters(&average.threads, &average.cores, &average.packages);
1766 for_all_cpus(sum_counters, t, c, p);
1768 /* Use the global time delta for the average. */
1769 average.threads.tv_delta = tv_delta;
1771 average.threads.tsc /= topo.num_cpus;
1772 average.threads.aperf /= topo.num_cpus;
1773 average.threads.mperf /= topo.num_cpus;
1774 average.threads.instr_count /= topo.num_cpus;
1775 average.threads.c1 /= topo.num_cpus;
1777 if (average.threads.irq_count > 9999999)
1778 sums_need_wide_columns = 1;
1780 average.cores.c3 /= topo.num_cores;
1781 average.cores.c6 /= topo.num_cores;
1782 average.cores.c7 /= topo.num_cores;
1783 average.cores.mc6_us /= topo.num_cores;
1785 if (DO_BIC(BIC_Totl_c0))
1786 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1787 if (DO_BIC(BIC_Any_c0))
1788 average.packages.pkg_any_core_c0 /= topo.num_packages;
1789 if (DO_BIC(BIC_GFX_c0))
1790 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1791 if (DO_BIC(BIC_CPUGFX))
1792 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1794 average.packages.pc2 /= topo.num_packages;
1795 if (DO_BIC(BIC_Pkgpc3))
1796 average.packages.pc3 /= topo.num_packages;
1797 if (DO_BIC(BIC_Pkgpc6))
1798 average.packages.pc6 /= topo.num_packages;
1799 if (DO_BIC(BIC_Pkgpc7))
1800 average.packages.pc7 /= topo.num_packages;
1802 average.packages.pc8 /= topo.num_packages;
1803 average.packages.pc9 /= topo.num_packages;
1804 average.packages.pc10 /= topo.num_packages;
1806 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1807 if (mp->format == FORMAT_RAW)
1809 if (mp->type == COUNTER_ITEMS) {
1810 if (average.threads.counter[i] > 9999999)
1811 sums_need_wide_columns = 1;
1814 average.threads.counter[i] /= topo.num_cpus;
1816 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1817 if (mp->format == FORMAT_RAW)
1819 if (mp->type == COUNTER_ITEMS) {
1820 if (average.cores.counter[i] > 9999999)
1821 sums_need_wide_columns = 1;
1823 average.cores.counter[i] /= topo.num_cores;
1825 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1826 if (mp->format == FORMAT_RAW)
1828 if (mp->type == COUNTER_ITEMS) {
1829 if (average.packages.counter[i] > 9999999)
1830 sums_need_wide_columns = 1;
1832 average.packages.counter[i] /= topo.num_packages;
1836 static unsigned long long rdtsc(void)
1838 unsigned int low, high;
1840 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1842 return low | ((unsigned long long)high) << 32;
1846 * Open a file, and exit on failure
1848 FILE *fopen_or_die(const char *path, const char *mode)
1850 FILE *filep = fopen(path, mode);
1853 err(1, "%s: open failed", path);
1857 * snapshot_sysfs_counter()
1859 * return snapshot of given counter
1861 unsigned long long snapshot_sysfs_counter(char *path)
1865 unsigned long long counter;
1867 fp = fopen_or_die(path, "r");
1869 retval = fscanf(fp, "%lld", &counter);
1871 err(1, "snapshot_sysfs_counter(%s)", path);
1878 int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1880 if (mp->msr_num != 0) {
1881 if (get_msr(cpu, mp->msr_num, counterp))
1884 char path[128 + PATH_BYTES];
1886 if (mp->flags & SYSFS_PERCPU) {
1887 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1890 *counterp = snapshot_sysfs_counter(path);
1892 *counterp = snapshot_sysfs_counter(mp->path);
1899 int get_epb(int cpu)
1901 char path[128 + PATH_BYTES];
1902 unsigned long long msr;
1906 sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu);
1908 fp = fopen(path, "r");
1912 ret = fscanf(fp, "%d", &epb);
1914 err(1, "%s(%s)", __func__, path);
1921 get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr);
1926 void get_apic_id(struct thread_data *t)
1928 unsigned int eax, ebx, ecx, edx;
1930 if (DO_BIC(BIC_APIC)) {
1931 eax = ebx = ecx = edx = 0;
1932 __cpuid(1, eax, ebx, ecx, edx);
1934 t->apic_id = (ebx >> 24) & 0xff;
1937 if (!DO_BIC(BIC_X2APIC))
1940 if (authentic_amd || hygon_genuine) {
1941 unsigned int topology_extensions;
1943 if (max_extended_level < 0x8000001e)
1946 eax = ebx = ecx = edx = 0;
1947 __cpuid(0x80000001, eax, ebx, ecx, edx);
1948 topology_extensions = ecx & (1 << 22);
1950 if (topology_extensions == 0)
1953 eax = ebx = ecx = edx = 0;
1954 __cpuid(0x8000001e, eax, ebx, ecx, edx);
1963 if (max_level < 0xb)
1967 __cpuid(0xb, eax, ebx, ecx, edx);
1970 if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
1971 fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
1972 t->cpu_id, t->apic_id, t->x2apic_id);
1978 * acquire and record local counters for that cpu
1980 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1982 int cpu = t->cpu_id;
1983 unsigned long long msr;
1984 int aperf_mperf_retry_count = 0;
1985 struct msr_counter *mp;
1988 if (cpu_migrate(cpu)) {
1989 fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu);
1993 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1995 if (first_counter_read)
1998 t->tsc = rdtsc(); /* we are running on local CPU of interest */
2000 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
2001 soft_c1_residency_display(BIC_Avg_MHz)) {
2002 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
2005 * The TSC, APERF and MPERF must be read together for
2006 * APERF/MPERF and MPERF/TSC to give accurate results.
2008 * Unfortunately, APERF and MPERF are read by
2009 * individual system call, so delays may occur
2010 * between them. If the time to read them
2011 * varies by a large amount, we re-read them.
2015 * This initial dummy APERF read has been seen to
2016 * reduce jitter in the subsequent reads.
2019 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2022 t->tsc = rdtsc(); /* re-read close to APERF */
2024 tsc_before = t->tsc;
2026 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2029 tsc_between = rdtsc();
2031 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
2034 tsc_after = rdtsc();
2036 aperf_time = tsc_between - tsc_before;
2037 mperf_time = tsc_after - tsc_between;
2040 * If the system call latency to read APERF and MPERF
2041 * differ by more than 2x, then try again.
2043 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
2044 aperf_mperf_retry_count++;
2045 if (aperf_mperf_retry_count < 5)
2048 warnx("cpu%d jitter %lld %lld",
2049 cpu, aperf_time, mperf_time);
2051 aperf_mperf_retry_count = 0;
2053 t->aperf = t->aperf * aperf_mperf_multiplier;
2054 t->mperf = t->mperf * aperf_mperf_multiplier;
2057 if (DO_BIC(BIC_IPC))
2058 if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long))
2061 if (DO_BIC(BIC_IRQ))
2062 t->irq_count = irqs_per_cpu[cpu];
2063 if (DO_BIC(BIC_SMI)) {
2064 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
2066 t->smi_count = msr & 0xFFFFFFFF;
2068 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
2069 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
2073 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2074 if (get_mp(cpu, mp, &t->counter[i]))
2078 /* collect core counters only for 1st thread in core */
2079 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2082 if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
2083 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
2087 if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
2088 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
2090 } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) {
2091 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
2095 if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) {
2096 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
2098 else if (t->is_atom) {
2100 * For Atom CPUs that has core cstate deeper than c6,
2101 * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper.
2102 * Minus CC7 (and deeper cstates) residency to get
2103 * accturate cc6 residency.
2109 if (DO_BIC(BIC_Mod_c6))
2110 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
2113 if (DO_BIC(BIC_CoreTmp)) {
2114 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2116 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
2119 if (do_rapl & RAPL_AMD_F17H) {
2120 if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
2122 c->core_energy = msr & 0xFFFFFFFF;
2125 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2126 if (get_mp(cpu, mp, &c->counter[i]))
2130 /* collect package counters only for 1st core in package */
2131 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2134 if (DO_BIC(BIC_Totl_c0)) {
2135 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
2138 if (DO_BIC(BIC_Any_c0)) {
2139 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
2142 if (DO_BIC(BIC_GFX_c0)) {
2143 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
2146 if (DO_BIC(BIC_CPUGFX)) {
2147 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
2150 if (DO_BIC(BIC_Pkgpc3))
2151 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
2153 if (DO_BIC(BIC_Pkgpc6)) {
2154 if (do_slm_cstates) {
2155 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
2158 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
2163 if (DO_BIC(BIC_Pkgpc2))
2164 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
2166 if (DO_BIC(BIC_Pkgpc7))
2167 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
2169 if (DO_BIC(BIC_Pkgpc8))
2170 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
2172 if (DO_BIC(BIC_Pkgpc9))
2173 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
2175 if (DO_BIC(BIC_Pkgpc10))
2176 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
2179 if (DO_BIC(BIC_CPU_LPI))
2180 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
2181 if (DO_BIC(BIC_SYS_LPI))
2182 p->sys_lpi = cpuidle_cur_sys_lpi_us;
2184 if (do_rapl & RAPL_PKG) {
2185 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr))
2187 p->energy_pkg = msr;
2189 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
2190 if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr))
2192 p->energy_cores = msr;
2194 if (do_rapl & RAPL_DRAM) {
2195 if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
2197 p->energy_dram = msr;
2199 if (do_rapl & RAPL_GFX) {
2200 if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr))
2202 p->energy_gfx = msr;
2204 if (do_rapl & RAPL_PKG_PERF_STATUS) {
2205 if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr))
2207 p->rapl_pkg_perf_status = msr;
2209 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
2210 if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr))
2212 p->rapl_dram_perf_status = msr;
2214 if (do_rapl & RAPL_AMD_F17H) {
2215 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr))
2217 p->energy_pkg = msr;
2219 if (DO_BIC(BIC_PkgTmp)) {
2220 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2222 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
2225 if (DO_BIC(BIC_GFX_rc6))
2226 p->gfx_rc6_ms = gfx_cur_rc6_ms;
2228 if (DO_BIC(BIC_GFXMHz))
2229 p->gfx_mhz = gfx_cur_mhz;
2231 if (DO_BIC(BIC_GFXACTMHz))
2232 p->gfx_act_mhz = gfx_act_mhz;
2234 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2235 if (get_mp(cpu, mp, &p->counter[i]))
2239 gettimeofday(&t->tv_end, (struct timezone *)NULL);
2245 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2246 * If you change the values, note they are used both in comparisons
2247 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2250 #define PCLUKN 0 /* Unknown */
2251 #define PCLRSV 1 /* Reserved */
2252 #define PCL__0 2 /* PC0 */
2253 #define PCL__1 3 /* PC1 */
2254 #define PCL__2 4 /* PC2 */
2255 #define PCL__3 5 /* PC3 */
2256 #define PCL__4 6 /* PC4 */
2257 #define PCL__6 7 /* PC6 */
2258 #define PCL_6N 8 /* PC6 No Retention */
2259 #define PCL_6R 9 /* PC6 Retention */
2260 #define PCL__7 10 /* PC7 */
2261 #define PCL_7S 11 /* PC7 Shrink */
2262 #define PCL__8 12 /* PC8 */
2263 #define PCL__9 13 /* PC9 */
2264 #define PCL_10 14 /* PC10 */
2265 #define PCLUNL 15 /* Unlimited */
2267 int pkg_cstate_limit = PCLUKN;
2268 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2269 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"};
2271 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};
2272 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};
2273 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};
2274 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};
2275 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};
2276 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};
2277 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};
2278 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};
2279 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};
2282 calculate_tsc_tweak()
2284 tsc_tweak = base_hz / tsc_hz;
2287 void prewake_cstate_probe(unsigned int family, unsigned int model);
2290 dump_nhm_platform_info(void)
2292 unsigned long long msr;
2295 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2297 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
2299 ratio = (msr >> 40) & 0xFF;
2300 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2301 ratio, bclk, ratio * bclk);
2303 ratio = (msr >> 8) & 0xFF;
2304 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2305 ratio, bclk, ratio * bclk);
2307 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2308 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2309 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
2311 /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */
2312 if (dis_cstate_prewake)
2313 fprintf(outf, "C-state Pre-wake: %sabled\n",
2314 msr & 0x40000000 ? "DIS" : "EN");
2320 dump_hsw_turbo_ratio_limits(void)
2322 unsigned long long msr;
2325 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2327 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2329 ratio = (msr >> 8) & 0xFF;
2331 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2332 ratio, bclk, ratio * bclk);
2334 ratio = (msr >> 0) & 0xFF;
2336 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2337 ratio, bclk, ratio * bclk);
2342 dump_ivt_turbo_ratio_limits(void)
2344 unsigned long long msr;
2347 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
2349 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
2351 ratio = (msr >> 56) & 0xFF;
2353 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
2354 ratio, bclk, ratio * bclk);
2356 ratio = (msr >> 48) & 0xFF;
2358 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
2359 ratio, bclk, ratio * bclk);
2361 ratio = (msr >> 40) & 0xFF;
2363 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
2364 ratio, bclk, ratio * bclk);
2366 ratio = (msr >> 32) & 0xFF;
2368 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
2369 ratio, bclk, ratio * bclk);
2371 ratio = (msr >> 24) & 0xFF;
2373 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
2374 ratio, bclk, ratio * bclk);
2376 ratio = (msr >> 16) & 0xFF;
2378 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
2379 ratio, bclk, ratio * bclk);
2381 ratio = (msr >> 8) & 0xFF;
2383 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
2384 ratio, bclk, ratio * bclk);
2386 ratio = (msr >> 0) & 0xFF;
2388 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
2389 ratio, bclk, ratio * bclk);
2392 int has_turbo_ratio_group_limits(int family, int model)
2399 case INTEL_FAM6_ATOM_GOLDMONT:
2400 case INTEL_FAM6_SKYLAKE_X:
2401 case INTEL_FAM6_ICELAKE_X:
2402 case INTEL_FAM6_ATOM_GOLDMONT_D:
2403 case INTEL_FAM6_ATOM_TREMONT_D:
2410 dump_turbo_ratio_limits(int family, int model)
2412 unsigned long long msr, core_counts;
2413 unsigned int ratio, group_size;
2415 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2416 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2418 if (has_turbo_ratio_group_limits(family, model)) {
2419 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2420 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2422 core_counts = 0x0807060504030201;
2425 ratio = (msr >> 56) & 0xFF;
2426 group_size = (core_counts >> 56) & 0xFF;
2428 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2429 ratio, bclk, ratio * bclk, group_size);
2431 ratio = (msr >> 48) & 0xFF;
2432 group_size = (core_counts >> 48) & 0xFF;
2434 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2435 ratio, bclk, ratio * bclk, group_size);
2437 ratio = (msr >> 40) & 0xFF;
2438 group_size = (core_counts >> 40) & 0xFF;
2440 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2441 ratio, bclk, ratio * bclk, group_size);
2443 ratio = (msr >> 32) & 0xFF;
2444 group_size = (core_counts >> 32) & 0xFF;
2446 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2447 ratio, bclk, ratio * bclk, group_size);
2449 ratio = (msr >> 24) & 0xFF;
2450 group_size = (core_counts >> 24) & 0xFF;
2452 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2453 ratio, bclk, ratio * bclk, group_size);
2455 ratio = (msr >> 16) & 0xFF;
2456 group_size = (core_counts >> 16) & 0xFF;
2458 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2459 ratio, bclk, ratio * bclk, group_size);
2461 ratio = (msr >> 8) & 0xFF;
2462 group_size = (core_counts >> 8) & 0xFF;
2464 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2465 ratio, bclk, ratio * bclk, group_size);
2467 ratio = (msr >> 0) & 0xFF;
2468 group_size = (core_counts >> 0) & 0xFF;
2470 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2471 ratio, bclk, ratio * bclk, group_size);
2476 dump_atom_turbo_ratio_limits(void)
2478 unsigned long long msr;
2481 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2482 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2484 ratio = (msr >> 0) & 0x3F;
2486 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2487 ratio, bclk, ratio * bclk);
2489 ratio = (msr >> 8) & 0x3F;
2491 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2492 ratio, bclk, ratio * bclk);
2494 ratio = (msr >> 16) & 0x3F;
2496 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2497 ratio, bclk, ratio * bclk);
2499 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2500 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2502 ratio = (msr >> 24) & 0x3F;
2504 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2505 ratio, bclk, ratio * bclk);
2507 ratio = (msr >> 16) & 0x3F;
2509 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2510 ratio, bclk, ratio * bclk);
2512 ratio = (msr >> 8) & 0x3F;
2514 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2515 ratio, bclk, ratio * bclk);
2517 ratio = (msr >> 0) & 0x3F;
2519 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2520 ratio, bclk, ratio * bclk);
2524 dump_knl_turbo_ratio_limits(void)
2526 const unsigned int buckets_no = 7;
2528 unsigned long long msr;
2529 int delta_cores, delta_ratio;
2531 unsigned int cores[buckets_no];
2532 unsigned int ratio[buckets_no];
2534 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2536 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2540 * Turbo encoding in KNL is as follows:
2542 * [7:1] -- Base value of number of active cores of bucket 1.
2543 * [15:8] -- Base value of freq ratio of bucket 1.
2544 * [20:16] -- +ve delta of number of active cores of bucket 2.
2545 * i.e. active cores of bucket 2 =
2546 * active cores of bucket 1 + delta
2547 * [23:21] -- Negative delta of freq ratio of bucket 2.
2548 * i.e. freq ratio of bucket 2 =
2549 * freq ratio of bucket 1 - delta
2550 * [28:24]-- +ve delta of number of active cores of bucket 3.
2551 * [31:29]-- -ve delta of freq ratio of bucket 3.
2552 * [36:32]-- +ve delta of number of active cores of bucket 4.
2553 * [39:37]-- -ve delta of freq ratio of bucket 4.
2554 * [44:40]-- +ve delta of number of active cores of bucket 5.
2555 * [47:45]-- -ve delta of freq ratio of bucket 5.
2556 * [52:48]-- +ve delta of number of active cores of bucket 6.
2557 * [55:53]-- -ve delta of freq ratio of bucket 6.
2558 * [60:56]-- +ve delta of number of active cores of bucket 7.
2559 * [63:61]-- -ve delta of freq ratio of bucket 7.
2563 cores[b_nr] = (msr & 0xFF) >> 1;
2564 ratio[b_nr] = (msr >> 8) & 0xFF;
2566 for (i = 16; i < 64; i += 8) {
2567 delta_cores = (msr >> i) & 0x1F;
2568 delta_ratio = (msr >> (i + 5)) & 0x7;
2570 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2571 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2575 for (i = buckets_no - 1; i >= 0; i--)
2576 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2578 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2579 ratio[i], bclk, ratio[i] * bclk, cores[i]);
2583 dump_nhm_cst_cfg(void)
2585 unsigned long long msr;
2587 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2589 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2591 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2592 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2593 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2594 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2595 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2596 (msr & (1 << 15)) ? "" : "UN",
2597 (unsigned int)msr & 0xF,
2598 pkg_cstate_limit_strings[pkg_cstate_limit]);
2600 #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2601 if (has_automatic_cstate_conversion) {
2602 fprintf(outf, ", automatic c-state conversion=%s",
2603 (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2606 fprintf(outf, ")\n");
2612 dump_config_tdp(void)
2614 unsigned long long msr;
2616 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2617 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2618 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
2620 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2621 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
2623 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2624 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2625 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2626 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
2628 fprintf(outf, ")\n");
2630 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2631 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
2633 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2634 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2635 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2636 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
2638 fprintf(outf, ")\n");
2640 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2641 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
2643 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2644 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2645 fprintf(outf, ")\n");
2647 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2648 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2649 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2650 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2651 fprintf(outf, ")\n");
2654 unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2656 void print_irtl(void)
2658 unsigned long long msr;
2660 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2661 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2662 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2663 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2665 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2666 fprintf(outf, "cpu%d: MSR_PKGC6_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_PKGC7_IRTL, &msr);
2671 fprintf(outf, "cpu%d: MSR_PKGC7_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]);
2678 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2679 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2680 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2681 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2683 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2684 fprintf(outf, "cpu%d: MSR_PKGC9_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_PKGC10_IRTL, &msr);
2689 fprintf(outf, "cpu%d: MSR_PKGC10_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]);
2694 void free_fd_percpu(void)
2698 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
2699 if (fd_percpu[i] != 0)
2700 close(fd_percpu[i]);
2706 void free_all_buffers(void)
2710 CPU_FREE(cpu_present_set);
2711 cpu_present_set = NULL;
2712 cpu_present_setsize = 0;
2714 CPU_FREE(cpu_affinity_set);
2715 cpu_affinity_set = NULL;
2716 cpu_affinity_setsize = 0;
2724 package_even = NULL;
2734 free(output_buffer);
2735 output_buffer = NULL;
2740 free(irq_column_2_cpu);
2743 for (i = 0; i <= topo.max_cpu_num; ++i) {
2744 if (cpus[i].put_ids)
2745 CPU_FREE(cpus[i].put_ids);
2752 * Parse a file containing a single int.
2753 * Return 0 if file can not be opened
2754 * Exit if file can be opened, but can not be parsed
2756 int parse_int_file(const char *fmt, ...)
2759 char path[PATH_MAX];
2763 va_start(args, fmt);
2764 vsnprintf(path, sizeof(path), fmt, args);
2766 filep = fopen(path, "r");
2769 if (fscanf(filep, "%d", &value) != 1)
2770 err(1, "%s: failed to parse number from file", path);
2776 * cpu_is_first_core_in_package(cpu)
2777 * return 1 if given CPU is 1st core in package
2779 int cpu_is_first_core_in_package(int cpu)
2781 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2784 int get_physical_package_id(int cpu)
2786 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2789 int get_die_id(int cpu)
2791 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
2794 int get_core_id(int cpu)
2796 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2799 void set_node_data(void)
2801 int pkg, node, lnode, cpu, cpux;
2804 /* initialize logical_node_id */
2805 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
2806 cpus[cpu].logical_node_id = -1;
2809 for (pkg = 0; pkg < topo.num_packages; pkg++) {
2811 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
2812 if (cpus[cpu].physical_package_id != pkg)
2814 /* find a cpu with an unset logical_node_id */
2815 if (cpus[cpu].logical_node_id != -1)
2817 cpus[cpu].logical_node_id = lnode;
2818 node = cpus[cpu].physical_node_id;
2821 * find all matching cpus on this pkg and set
2822 * the logical_node_id
2824 for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
2825 if ((cpus[cpux].physical_package_id == pkg) &&
2826 (cpus[cpux].physical_node_id == node)) {
2827 cpus[cpux].logical_node_id = lnode;
2832 if (lnode > topo.nodes_per_pkg)
2833 topo.nodes_per_pkg = lnode;
2835 if (cpu_count >= topo.max_cpu_num)
2840 int get_physical_node_id(struct cpu_topology *thiscpu)
2845 int cpu = thiscpu->logical_cpu_id;
2847 for (i = 0; i <= topo.max_cpu_num; i++) {
2848 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
2850 filep = fopen(path, "r");
2859 int get_thread_siblings(struct cpu_topology *thiscpu)
2861 char path[80], character;
2864 int so, shift, sib_core;
2865 int cpu = thiscpu->logical_cpu_id;
2866 int offset = topo.max_cpu_num + 1;
2870 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
2871 if (thiscpu->thread_id < 0)
2872 thiscpu->thread_id = thread_id++;
2873 if (!thiscpu->put_ids)
2876 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2877 CPU_ZERO_S(size, thiscpu->put_ids);
2880 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2881 filep = fopen(path, "r");
2884 warnx("%s: open failed", path);
2888 offset -= BITMASK_SIZE;
2889 if (fscanf(filep, "%lx%c", &map, &character) != 2)
2890 err(1, "%s: failed to parse file", path);
2891 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2892 if ((map >> shift) & 0x1) {
2893 so = shift + offset;
2894 sib_core = get_core_id(so);
2895 if (sib_core == thiscpu->physical_core_id) {
2896 CPU_SET_S(so, size, thiscpu->put_ids);
2898 (cpus[so].thread_id < 0))
2899 cpus[so].thread_id =
2904 } while (!strncmp(&character, ",", 1));
2907 return CPU_COUNT_S(size, thiscpu->put_ids);
2911 * run func(thread, core, package) in topology order
2912 * skip non-present cpus
2915 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2916 struct pkg_data *, struct thread_data *, struct core_data *,
2917 struct pkg_data *), struct thread_data *thread_base,
2918 struct core_data *core_base, struct pkg_data *pkg_base,
2919 struct thread_data *thread_base2, struct core_data *core_base2,
2920 struct pkg_data *pkg_base2)
2922 int retval, pkg_no, node_no, core_no, thread_no;
2924 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2925 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
2926 for (core_no = 0; core_no < topo.cores_per_node;
2928 for (thread_no = 0; thread_no <
2929 topo.threads_per_core; ++thread_no) {
2930 struct thread_data *t, *t2;
2931 struct core_data *c, *c2;
2932 struct pkg_data *p, *p2;
2934 t = GET_THREAD(thread_base, thread_no,
2938 if (cpu_is_not_present(t->cpu_id))
2941 t2 = GET_THREAD(thread_base2, thread_no,
2945 c = GET_CORE(core_base, core_no,
2947 c2 = GET_CORE(core_base2, core_no,
2951 p = GET_PKG(pkg_base, pkg_no);
2952 p2 = GET_PKG(pkg_base2, pkg_no);
2954 retval = func(t, c, p, t2, c2, p2);
2965 * run func(cpu) on every cpu in /proc/stat
2966 * return max_cpu number
2968 int for_all_proc_cpus(int (func)(int))
2974 fp = fopen_or_die(proc_stat, "r");
2976 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2978 err(1, "%s: failed to parse format", proc_stat);
2981 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2985 retval = func(cpu_num);
2995 void re_initialize(void)
2998 setup_all_buffers();
2999 fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
3002 void set_max_cpu_num(void)
3006 unsigned long dummy;
3009 base_cpu = sched_getcpu();
3011 err(1, "cannot find calling cpu ID");
3013 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings",
3016 filep = fopen_or_die(pathname, "r");
3017 topo.max_cpu_num = 0;
3018 while (fscanf(filep, "%lx,", &dummy) == 1)
3019 topo.max_cpu_num += BITMASK_SIZE;
3021 topo.max_cpu_num--; /* 0 based */
3026 * remember the last one seen, it will be the max
3028 int count_cpus(int cpu)
3033 int mark_cpu_present(int cpu)
3035 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
3039 int init_thread_id(int cpu)
3041 cpus[cpu].thread_id = -1;
3046 * snapshot_proc_interrupts()
3048 * read and record summary of /proc/interrupts
3050 * return 1 if config change requires a restart, else return 0
3052 int snapshot_proc_interrupts(void)
3058 fp = fopen_or_die("/proc/interrupts", "r");
3062 /* read 1st line of /proc/interrupts to get cpu* name for each column */
3063 for (column = 0; column < topo.num_cpus; ++column) {
3066 retval = fscanf(fp, " CPU%d", &cpu_number);
3070 if (cpu_number > topo.max_cpu_num) {
3071 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
3075 irq_column_2_cpu[column] = cpu_number;
3076 irqs_per_cpu[cpu_number] = 0;
3079 /* read /proc/interrupt count lines and sum up irqs per cpu */
3084 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
3088 /* read the count per cpu */
3089 for (column = 0; column < topo.num_cpus; ++column) {
3091 int cpu_number, irq_count;
3093 retval = fscanf(fp, " %d", &irq_count);
3097 cpu_number = irq_column_2_cpu[column];
3098 irqs_per_cpu[cpu_number] += irq_count;
3102 while (getc(fp) != '\n')
3103 ; /* flush interrupt description */
3109 * snapshot_gfx_rc6_ms()
3111 * record snapshot of
3112 * /sys/class/drm/card0/power/rc6_residency_ms
3114 * return 1 if config change requires a restart, else return 0
3116 int snapshot_gfx_rc6_ms(void)
3121 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
3123 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
3132 * snapshot_gfx_mhz()
3134 * record snapshot of
3135 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
3137 * return 1 if config change requires a restart, else return 0
3139 int snapshot_gfx_mhz(void)
3145 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
3151 retval = fscanf(fp, "%d", &gfx_cur_mhz);
3159 * snapshot_gfx_cur_mhz()
3161 * record snapshot of
3162 * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz
3164 * return 1 if config change requires a restart, else return 0
3166 int snapshot_gfx_act_mhz(void)
3172 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r");
3178 retval = fscanf(fp, "%d", &gfx_act_mhz);
3180 err(1, "GFX ACT MHz");
3186 * snapshot_cpu_lpi()
3188 * record snapshot of
3189 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
3191 int snapshot_cpu_lpi_us(void)
3196 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
3198 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
3200 fprintf(stderr, "Disabling Low Power Idle CPU output\n");
3201 BIC_NOT_PRESENT(BIC_CPU_LPI);
3211 * snapshot_sys_lpi()
3213 * record snapshot of sys_lpi_file
3215 int snapshot_sys_lpi_us(void)
3220 fp = fopen_or_die(sys_lpi_file, "r");
3222 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
3224 fprintf(stderr, "Disabling Low Power Idle System output\n");
3225 BIC_NOT_PRESENT(BIC_SYS_LPI);
3234 * snapshot /proc and /sys files
3236 * return 1 if configuration restart needed, else return 0
3238 int snapshot_proc_sysfs_files(void)
3240 if (DO_BIC(BIC_IRQ))
3241 if (snapshot_proc_interrupts())
3244 if (DO_BIC(BIC_GFX_rc6))
3245 snapshot_gfx_rc6_ms();
3247 if (DO_BIC(BIC_GFXMHz))
3250 if (DO_BIC(BIC_GFXACTMHz))
3251 snapshot_gfx_act_mhz();
3253 if (DO_BIC(BIC_CPU_LPI))
3254 snapshot_cpu_lpi_us();
3256 if (DO_BIC(BIC_SYS_LPI))
3257 snapshot_sys_lpi_us();
3264 static void signal_handler (int signal)
3270 fprintf(stderr, " SIGINT\n");
3274 fprintf(stderr, "SIGUSR1\n");
3279 void setup_signal_handler(void)
3281 struct sigaction sa;
3283 memset(&sa, 0, sizeof(sa));
3285 sa.sa_handler = &signal_handler;
3287 if (sigaction(SIGINT, &sa, NULL) < 0)
3288 err(1, "sigaction SIGINT");
3289 if (sigaction(SIGUSR1, &sa, NULL) < 0)
3290 err(1, "sigaction SIGUSR1");
3295 struct timeval tout;
3296 struct timespec rest;
3301 FD_SET(0, &readfds);
3304 nanosleep(&interval_ts, NULL);
3309 retval = select(1, &readfds, NULL, NULL, &tout);
3312 switch (getc(stdin)) {
3318 * 'stdin' is a pipe closed on the other end. There
3319 * won't be any further input.
3322 /* Sleep the rest of the time */
3323 rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3324 rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3325 nanosleep(&rest, NULL);
3330 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr)
3333 unsigned long long msr_cur, msr_last;
3335 if (!per_cpu_msr_sum)
3338 idx = offset_to_idx(offset);
3341 /* get_msr_sum() = sum + (get_msr() - last) */
3342 ret = get_msr(cpu, offset, &msr_cur);
3345 msr_last = per_cpu_msr_sum[cpu].entries[idx].last;
3346 DELTA_WRAP32(msr_cur, msr_last);
3347 *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum;
3354 /* Timer callback, update the sum of MSRs periodically. */
3355 static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3358 int cpu = t->cpu_id;
3360 for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
3361 unsigned long long msr_cur, msr_last;
3366 offset = idx_to_offset(i);
3369 ret = get_msr(cpu, offset, &msr_cur);
3371 fprintf(outf, "Can not update msr(0x%llx)\n",
3372 (unsigned long long)offset);
3376 msr_last = per_cpu_msr_sum[cpu].entries[i].last;
3377 per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff;
3379 DELTA_WRAP32(msr_cur, msr_last);
3380 per_cpu_msr_sum[cpu].entries[i].sum += msr_last;
3386 msr_record_handler(union sigval v)
3388 for_all_cpus(update_msr_sum, EVEN_COUNTERS);
3391 void msr_sum_record(void)
3393 struct itimerspec its;
3394 struct sigevent sev;
3396 per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array));
3397 if (!per_cpu_msr_sum) {
3398 fprintf(outf, "Can not allocate memory for long time MSR.\n");
3402 * Signal handler might be restricted, so use thread notifier instead.
3404 memset(&sev, 0, sizeof(struct sigevent));
3405 sev.sigev_notify = SIGEV_THREAD;
3406 sev.sigev_notify_function = msr_record_handler;
3408 sev.sigev_value.sival_ptr = &timerid;
3409 if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
3410 fprintf(outf, "Can not create timer.\n");
3414 its.it_value.tv_sec = 0;
3415 its.it_value.tv_nsec = 1;
3417 * A wraparound time has been calculated early.
3418 * Some sources state that the peak power for a
3419 * microprocessor is usually 1.5 times the TDP rating,
3420 * use 2 * TDP for safety.
3422 its.it_interval.tv_sec = rapl_joule_counter_range / 2;
3423 its.it_interval.tv_nsec = 0;
3425 if (timer_settime(timerid, 0, &its, NULL) == -1) {
3426 fprintf(outf, "Can not set timer.\n");
3432 timer_delete(timerid);
3434 free(per_cpu_msr_sum);
3437 void turbostat_loop()
3443 setup_signal_handler();
3448 snapshot_proc_sysfs_files();
3449 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3450 first_counter_read = 0;
3453 } else if (retval == -1) {
3454 if (restarted > 10) {
3462 gettimeofday(&tv_even, (struct timezone *)NULL);
3465 if (for_all_proc_cpus(cpu_is_not_present)) {
3470 if (snapshot_proc_sysfs_files())
3472 retval = for_all_cpus(get_counters, ODD_COUNTERS);
3475 } else if (retval == -1) {
3479 gettimeofday(&tv_odd, (struct timezone *)NULL);
3480 timersub(&tv_odd, &tv_even, &tv_delta);
3481 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3485 compute_average(EVEN_COUNTERS);
3486 format_all_counters(EVEN_COUNTERS);
3487 flush_output_stdout();
3490 if (num_iterations && ++done_iters >= num_iterations)
3493 if (snapshot_proc_sysfs_files())
3495 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3498 } else if (retval == -1) {
3502 gettimeofday(&tv_even, (struct timezone *)NULL);
3503 timersub(&tv_even, &tv_odd, &tv_delta);
3504 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3508 compute_average(ODD_COUNTERS);
3509 format_all_counters(ODD_COUNTERS);
3510 flush_output_stdout();
3513 if (num_iterations && ++done_iters >= num_iterations)
3518 void check_dev_msr()
3523 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3524 if (stat(pathname, &sb))
3525 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3526 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3530 * check for CAP_SYS_RAWIO
3531 * return 0 on success
3534 int check_for_cap_sys_rawio(void)
3537 cap_flag_value_t cap_flag_value;
3539 caps = cap_get_proc();
3541 err(-6, "cap_get_proc\n");
3543 if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3544 err(-6, "cap_get\n");
3546 if (cap_flag_value != CAP_SET) {
3547 warnx("capget(CAP_SYS_RAWIO) failed,"
3548 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3552 if (cap_free(caps) == -1)
3553 err(-6, "cap_free\n");
3557 void check_permissions(void)
3562 /* check for CAP_SYS_RAWIO */
3563 do_exit += check_for_cap_sys_rawio();
3565 /* test file permissions */
3566 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3567 if (euidaccess(pathname, R_OK)) {
3569 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
3572 /* if all else fails, thell them to be root */
3575 warnx("... or simply run as root");
3582 * NHM adds support for additional MSRs:
3584 * MSR_SMI_COUNT 0x00000034
3586 * MSR_PLATFORM_INFO 0x000000ce
3587 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
3589 * MSR_MISC_PWR_MGMT 0x000001aa
3591 * MSR_PKG_C3_RESIDENCY 0x000003f8
3592 * MSR_PKG_C6_RESIDENCY 0x000003f9
3593 * MSR_CORE_C3_RESIDENCY 0x000003fc
3594 * MSR_CORE_C6_RESIDENCY 0x000003fd
3597 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
3598 * sets has_misc_feature_control
3600 int probe_nhm_msrs(unsigned int family, unsigned int model)
3602 unsigned long long msr;
3603 unsigned int base_ratio;
3604 int *pkg_cstate_limits;
3612 bclk = discover_bclk(family, model);
3615 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3616 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3617 pkg_cstate_limits = nhm_pkg_cstate_limits;
3619 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3620 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3621 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3622 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3623 pkg_cstate_limits = snb_pkg_cstate_limits;
3624 has_misc_feature_control = 1;
3626 case INTEL_FAM6_HASWELL: /* HSW */
3627 case INTEL_FAM6_HASWELL_G: /* HSW */
3628 case INTEL_FAM6_HASWELL_X: /* HSX */
3629 case INTEL_FAM6_HASWELL_L: /* HSW */
3630 case INTEL_FAM6_BROADWELL: /* BDW */
3631 case INTEL_FAM6_BROADWELL_G: /* BDW */
3632 case INTEL_FAM6_BROADWELL_X: /* BDX */
3633 case INTEL_FAM6_SKYLAKE_L: /* SKL */
3634 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3635 pkg_cstate_limits = hsw_pkg_cstate_limits;
3636 has_misc_feature_control = 1;
3638 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3639 pkg_cstate_limits = skx_pkg_cstate_limits;
3640 has_misc_feature_control = 1;
3642 case INTEL_FAM6_ICELAKE_X: /* ICX */
3643 pkg_cstate_limits = icx_pkg_cstate_limits;
3644 has_misc_feature_control = 1;
3646 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
3647 no_MSR_MISC_PWR_MGMT = 1;
3648 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
3649 pkg_cstate_limits = slv_pkg_cstate_limits;
3651 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
3652 pkg_cstate_limits = amt_pkg_cstate_limits;
3653 no_MSR_MISC_PWR_MGMT = 1;
3655 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
3656 pkg_cstate_limits = phi_pkg_cstate_limits;
3658 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3659 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3660 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
3661 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
3662 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
3663 pkg_cstate_limits = glm_pkg_cstate_limits;
3668 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3669 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3671 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
3672 base_ratio = (msr >> 8) & 0xFF;
3674 base_hz = base_ratio * bclk * 1000000;
3679 * SLV client has support for unique MSRs:
3681 * MSR_CC6_DEMOTION_POLICY_CONFIG
3682 * MSR_MC6_DEMOTION_POLICY_CONFIG
3685 int has_slv_msrs(unsigned int family, unsigned int model)
3691 case INTEL_FAM6_ATOM_SILVERMONT:
3692 case INTEL_FAM6_ATOM_SILVERMONT_MID:
3693 case INTEL_FAM6_ATOM_AIRMONT_MID:
3698 int is_dnv(unsigned int family, unsigned int model)
3705 case INTEL_FAM6_ATOM_GOLDMONT_D:
3710 int is_bdx(unsigned int family, unsigned int model)
3717 case INTEL_FAM6_BROADWELL_X:
3722 int is_skx(unsigned int family, unsigned int model)
3729 case INTEL_FAM6_SKYLAKE_X:
3735 int is_icx(unsigned int family, unsigned int model)
3742 case INTEL_FAM6_ICELAKE_X:
3748 int is_ehl(unsigned int family, unsigned int model)
3754 case INTEL_FAM6_ATOM_TREMONT:
3759 int is_jvl(unsigned int family, unsigned int model)
3765 case INTEL_FAM6_ATOM_TREMONT_D:
3771 int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3773 if (has_slv_msrs(family, model))
3777 /* Nehalem compatible, but do not include turbo-ratio limit support */
3778 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3779 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
3785 int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3787 if (has_slv_msrs(family, model))
3792 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3801 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3802 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3808 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3817 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3824 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3833 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3839 int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3848 case INTEL_FAM6_ATOM_GOLDMONT:
3849 case INTEL_FAM6_SKYLAKE_X:
3850 case INTEL_FAM6_ICELAKE_X:
3856 int has_config_tdp(unsigned int family, unsigned int model)
3865 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3866 case INTEL_FAM6_HASWELL: /* HSW */
3867 case INTEL_FAM6_HASWELL_X: /* HSX */
3868 case INTEL_FAM6_HASWELL_L: /* HSW */
3869 case INTEL_FAM6_HASWELL_G: /* HSW */
3870 case INTEL_FAM6_BROADWELL: /* BDW */
3871 case INTEL_FAM6_BROADWELL_G: /* BDW */
3872 case INTEL_FAM6_BROADWELL_X: /* BDX */
3873 case INTEL_FAM6_SKYLAKE_L: /* SKL */
3874 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3875 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3876 case INTEL_FAM6_ICELAKE_X: /* ICX */
3878 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3886 remove_underbar(char *s)
3900 dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3902 if (!do_nhm_platform_info)
3905 dump_nhm_platform_info();
3907 if (has_hsw_turbo_ratio_limit(family, model))
3908 dump_hsw_turbo_ratio_limits();
3910 if (has_ivt_turbo_ratio_limit(family, model))
3911 dump_ivt_turbo_ratio_limits();
3913 if (has_turbo_ratio_limit(family, model))
3914 dump_turbo_ratio_limits(family, model);
3916 if (has_atom_turbo_ratio_limit(family, model))
3917 dump_atom_turbo_ratio_limits();
3919 if (has_knl_turbo_ratio_limit(family, model))
3920 dump_knl_turbo_ratio_limits();
3922 if (has_config_tdp(family, model))
3928 static void dump_sysfs_file(char *path)
3931 char cpuidle_buf[64];
3933 input = fopen(path, "r");
3934 if (input == NULL) {
3936 fprintf(outf, "NSFOD %s\n", path);
3939 if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
3940 err(1, "%s: failed to read file", path);
3943 fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
3946 dump_sysfs_cstate_config(void)
3955 if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
3956 fprintf(outf, "cpuidle not loaded\n");
3960 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
3961 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
3962 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
3964 for (state = 0; state < 10; ++state) {
3966 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3968 input = fopen(path, "r");
3971 if (!fgets(name_buf, sizeof(name_buf), input))
3972 err(1, "%s: failed to read file", path);
3974 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3975 sp = strchr(name_buf, '-');
3977 sp = strchrnul(name_buf, '\n');
3981 remove_underbar(name_buf);
3983 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3985 input = fopen(path, "r");
3988 if (!fgets(desc, sizeof(desc), input))
3989 err(1, "%s: failed to read file", path);
3991 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3996 dump_sysfs_pstate_config(void)
3999 char driver_buf[64];
4000 char governor_buf[64];
4004 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
4006 input = fopen(path, "r");
4007 if (input == NULL) {
4008 fprintf(outf, "NSFOD %s\n", path);
4011 if (!fgets(driver_buf, sizeof(driver_buf), input))
4012 err(1, "%s: failed to read file", path);
4015 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
4017 input = fopen(path, "r");
4018 if (input == NULL) {
4019 fprintf(outf, "NSFOD %s\n", path);
4022 if (!fgets(governor_buf, sizeof(governor_buf), input))
4023 err(1, "%s: failed to read file", path);
4026 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
4027 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
4029 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
4030 input = fopen(path, "r");
4031 if (input != NULL) {
4032 if (fscanf(input, "%d", &turbo) != 1)
4033 err(1, "%s: failed to parse number from file", path);
4034 fprintf(outf, "cpufreq boost: %d\n", turbo);
4038 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
4039 input = fopen(path, "r");
4040 if (input != NULL) {
4041 if (fscanf(input, "%d", &turbo) != 1)
4042 err(1, "%s: failed to parse number from file", path);
4043 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
4051 * Decode the ENERGY_PERF_BIAS MSR
4053 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4063 /* EPB is per-package */
4064 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4067 if (cpu_migrate(cpu)) {
4068 fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu);
4077 case ENERGY_PERF_BIAS_PERFORMANCE:
4078 epb_string = "performance";
4080 case ENERGY_PERF_BIAS_NORMAL:
4081 epb_string = "balanced";
4083 case ENERGY_PERF_BIAS_POWERSAVE:
4084 epb_string = "powersave";
4087 epb_string = "custom";
4090 fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string);
4096 * Decode the MSR_HWP_CAPABILITIES
4098 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4100 unsigned long long msr;
4108 /* MSR_HWP_CAPABILITIES is per-package */
4109 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4112 if (cpu_migrate(cpu)) {
4113 fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu);
4117 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
4120 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
4121 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
4123 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
4124 if ((msr & (1 << 0)) == 0)
4127 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
4130 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
4131 "(high %d guar %d eff %d low %d)\n",
4133 (unsigned int)HWP_HIGHEST_PERF(msr),
4134 (unsigned int)HWP_GUARANTEED_PERF(msr),
4135 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
4136 (unsigned int)HWP_LOWEST_PERF(msr));
4138 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
4141 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
4142 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
4144 (unsigned int)(((msr) >> 0) & 0xff),
4145 (unsigned int)(((msr) >> 8) & 0xff),
4146 (unsigned int)(((msr) >> 16) & 0xff),
4147 (unsigned int)(((msr) >> 24) & 0xff),
4148 (unsigned int)(((msr) >> 32) & 0xff3),
4149 (unsigned int)(((msr) >> 42) & 0x1));
4152 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
4155 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
4156 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
4158 (unsigned int)(((msr) >> 0) & 0xff),
4159 (unsigned int)(((msr) >> 8) & 0xff),
4160 (unsigned int)(((msr) >> 16) & 0xff),
4161 (unsigned int)(((msr) >> 24) & 0xff),
4162 (unsigned int)(((msr) >> 32) & 0xff3));
4164 if (has_hwp_notify) {
4165 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
4168 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
4169 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
4171 ((msr) & 0x1) ? "EN" : "Dis",
4172 ((msr) & 0x2) ? "EN" : "Dis");
4174 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
4177 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
4178 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
4180 ((msr) & 0x1) ? "" : "No-",
4181 ((msr) & 0x2) ? "" : "No-");
4187 * print_perf_limit()
4189 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4191 unsigned long long msr;
4197 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4200 if (cpu_migrate(cpu)) {
4201 fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu);
4205 if (do_core_perf_limit_reasons) {
4206 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
4207 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4208 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
4209 (msr & 1 << 15) ? "bit15, " : "",
4210 (msr & 1 << 14) ? "bit14, " : "",
4211 (msr & 1 << 13) ? "Transitions, " : "",
4212 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
4213 (msr & 1 << 11) ? "PkgPwrL2, " : "",
4214 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4215 (msr & 1 << 9) ? "CorePwr, " : "",
4216 (msr & 1 << 8) ? "Amps, " : "",
4217 (msr & 1 << 6) ? "VR-Therm, " : "",
4218 (msr & 1 << 5) ? "Auto-HWP, " : "",
4219 (msr & 1 << 4) ? "Graphics, " : "",
4220 (msr & 1 << 2) ? "bit2, " : "",
4221 (msr & 1 << 1) ? "ThermStatus, " : "",
4222 (msr & 1 << 0) ? "PROCHOT, " : "");
4223 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
4224 (msr & 1 << 31) ? "bit31, " : "",
4225 (msr & 1 << 30) ? "bit30, " : "",
4226 (msr & 1 << 29) ? "Transitions, " : "",
4227 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
4228 (msr & 1 << 27) ? "PkgPwrL2, " : "",
4229 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4230 (msr & 1 << 25) ? "CorePwr, " : "",
4231 (msr & 1 << 24) ? "Amps, " : "",
4232 (msr & 1 << 22) ? "VR-Therm, " : "",
4233 (msr & 1 << 21) ? "Auto-HWP, " : "",
4234 (msr & 1 << 20) ? "Graphics, " : "",
4235 (msr & 1 << 18) ? "bit18, " : "",
4236 (msr & 1 << 17) ? "ThermStatus, " : "",
4237 (msr & 1 << 16) ? "PROCHOT, " : "");
4240 if (do_gfx_perf_limit_reasons) {
4241 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
4242 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4243 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
4244 (msr & 1 << 0) ? "PROCHOT, " : "",
4245 (msr & 1 << 1) ? "ThermStatus, " : "",
4246 (msr & 1 << 4) ? "Graphics, " : "",
4247 (msr & 1 << 6) ? "VR-Therm, " : "",
4248 (msr & 1 << 8) ? "Amps, " : "",
4249 (msr & 1 << 9) ? "GFXPwr, " : "",
4250 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4251 (msr & 1 << 11) ? "PkgPwrL2, " : "");
4252 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
4253 (msr & 1 << 16) ? "PROCHOT, " : "",
4254 (msr & 1 << 17) ? "ThermStatus, " : "",
4255 (msr & 1 << 20) ? "Graphics, " : "",
4256 (msr & 1 << 22) ? "VR-Therm, " : "",
4257 (msr & 1 << 24) ? "Amps, " : "",
4258 (msr & 1 << 25) ? "GFXPwr, " : "",
4259 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4260 (msr & 1 << 27) ? "PkgPwrL2, " : "");
4262 if (do_ring_perf_limit_reasons) {
4263 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
4264 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4265 fprintf(outf, " (Active: %s%s%s%s%s%s)",
4266 (msr & 1 << 0) ? "PROCHOT, " : "",
4267 (msr & 1 << 1) ? "ThermStatus, " : "",
4268 (msr & 1 << 6) ? "VR-Therm, " : "",
4269 (msr & 1 << 8) ? "Amps, " : "",
4270 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4271 (msr & 1 << 11) ? "PkgPwrL2, " : "");
4272 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
4273 (msr & 1 << 16) ? "PROCHOT, " : "",
4274 (msr & 1 << 17) ? "ThermStatus, " : "",
4275 (msr & 1 << 22) ? "VR-Therm, " : "",
4276 (msr & 1 << 24) ? "Amps, " : "",
4277 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4278 (msr & 1 << 27) ? "PkgPwrL2, " : "");
4283 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
4284 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
4286 double get_tdp_intel(unsigned int model)
4288 unsigned long long msr;
4290 if (do_rapl & RAPL_PKG_POWER_INFO)
4291 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
4292 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
4295 case INTEL_FAM6_ATOM_SILVERMONT:
4296 case INTEL_FAM6_ATOM_SILVERMONT_D:
4303 double get_tdp_amd(unsigned int family)
4305 /* This is the max stock TDP of HEDT/Server Fam17h+ chips */
4310 * rapl_dram_energy_units_probe()
4311 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
4314 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
4316 /* only called for genuine_intel, family 6 */
4319 case INTEL_FAM6_HASWELL_X: /* HSX */
4320 case INTEL_FAM6_BROADWELL_X: /* BDX */
4321 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4322 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4323 return (rapl_dram_energy_units = 15.3 / 1000000);
4325 return (rapl_energy_units);
4329 void rapl_probe_intel(unsigned int family, unsigned int model)
4331 unsigned long long msr;
4332 unsigned int time_unit;
4339 case INTEL_FAM6_SANDYBRIDGE:
4340 case INTEL_FAM6_IVYBRIDGE:
4341 case INTEL_FAM6_HASWELL: /* HSW */
4342 case INTEL_FAM6_HASWELL_L: /* HSW */
4343 case INTEL_FAM6_HASWELL_G: /* HSW */
4344 case INTEL_FAM6_BROADWELL: /* BDW */
4345 case INTEL_FAM6_BROADWELL_G: /* BDW */
4346 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
4348 BIC_PRESENT(BIC_Pkg_J);
4349 BIC_PRESENT(BIC_Cor_J);
4350 BIC_PRESENT(BIC_GFX_J);
4352 BIC_PRESENT(BIC_PkgWatt);
4353 BIC_PRESENT(BIC_CorWatt);
4354 BIC_PRESENT(BIC_GFXWatt);
4357 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4358 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4359 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
4361 BIC_PRESENT(BIC_Pkg_J);
4363 BIC_PRESENT(BIC_PkgWatt);
4365 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4366 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;
4368 BIC_PRESENT(BIC_Pkg_J);
4369 BIC_PRESENT(BIC_Cor_J);
4370 BIC_PRESENT(BIC_RAM_J);
4371 BIC_PRESENT(BIC_GFX_J);
4373 BIC_PRESENT(BIC_PkgWatt);
4374 BIC_PRESENT(BIC_CorWatt);
4375 BIC_PRESENT(BIC_RAMWatt);
4376 BIC_PRESENT(BIC_GFXWatt);
4379 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
4380 do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
4381 BIC_PRESENT(BIC_PKG__);
4383 BIC_PRESENT(BIC_Pkg_J);
4385 BIC_PRESENT(BIC_PkgWatt);
4387 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4388 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4389 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;
4390 BIC_PRESENT(BIC_PKG__);
4391 BIC_PRESENT(BIC_RAM__);
4393 BIC_PRESENT(BIC_Pkg_J);
4394 BIC_PRESENT(BIC_Cor_J);
4395 BIC_PRESENT(BIC_RAM_J);
4396 BIC_PRESENT(BIC_GFX_J);
4398 BIC_PRESENT(BIC_PkgWatt);
4399 BIC_PRESENT(BIC_CorWatt);
4400 BIC_PRESENT(BIC_RAMWatt);
4401 BIC_PRESENT(BIC_GFXWatt);
4404 case INTEL_FAM6_HASWELL_X: /* HSX */
4405 case INTEL_FAM6_BROADWELL_X: /* BDX */
4406 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4407 case INTEL_FAM6_ICELAKE_X: /* ICX */
4408 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4409 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
4410 BIC_PRESENT(BIC_PKG__);
4411 BIC_PRESENT(BIC_RAM__);
4413 BIC_PRESENT(BIC_Pkg_J);
4414 BIC_PRESENT(BIC_RAM_J);
4416 BIC_PRESENT(BIC_PkgWatt);
4417 BIC_PRESENT(BIC_RAMWatt);
4420 case INTEL_FAM6_SANDYBRIDGE_X:
4421 case INTEL_FAM6_IVYBRIDGE_X:
4422 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;
4423 BIC_PRESENT(BIC_PKG__);
4424 BIC_PRESENT(BIC_RAM__);
4426 BIC_PRESENT(BIC_Pkg_J);
4427 BIC_PRESENT(BIC_Cor_J);
4428 BIC_PRESENT(BIC_RAM_J);
4430 BIC_PRESENT(BIC_PkgWatt);
4431 BIC_PRESENT(BIC_CorWatt);
4432 BIC_PRESENT(BIC_RAMWatt);
4435 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4436 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4437 do_rapl = RAPL_PKG | RAPL_CORES;
4439 BIC_PRESENT(BIC_Pkg_J);
4440 BIC_PRESENT(BIC_Cor_J);
4442 BIC_PRESENT(BIC_PkgWatt);
4443 BIC_PRESENT(BIC_CorWatt);
4446 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
4447 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;
4448 BIC_PRESENT(BIC_PKG__);
4449 BIC_PRESENT(BIC_RAM__);
4451 BIC_PRESENT(BIC_Pkg_J);
4452 BIC_PRESENT(BIC_Cor_J);
4453 BIC_PRESENT(BIC_RAM_J);
4455 BIC_PRESENT(BIC_PkgWatt);
4456 BIC_PRESENT(BIC_CorWatt);
4457 BIC_PRESENT(BIC_RAMWatt);
4464 /* units on package 0, verify later other packages match */
4465 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4468 rapl_power_units = 1.0 / (1 << (msr & 0xF));
4469 if (model == INTEL_FAM6_ATOM_SILVERMONT)
4470 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4472 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4474 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
4476 time_unit = msr >> 16 & 0xF;
4480 rapl_time_units = 1.0 / (1 << (time_unit));
4482 tdp = get_tdp_intel(model);
4484 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
4486 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
4489 void rapl_probe_amd(unsigned int family, unsigned int model)
4491 unsigned long long msr;
4492 unsigned int eax, ebx, ecx, edx;
4493 unsigned int has_rapl = 0;
4496 if (max_extended_level >= 0x80000007) {
4497 __cpuid(0x80000007, eax, ebx, ecx, edx);
4498 /* RAPL (Fam 17h+) */
4499 has_rapl = edx & (1 << 14);
4502 if (!has_rapl || family < 0x17)
4505 do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
4507 BIC_PRESENT(BIC_Pkg_J);
4508 BIC_PRESENT(BIC_Cor_J);
4510 BIC_PRESENT(BIC_PkgWatt);
4511 BIC_PRESENT(BIC_CorWatt);
4514 if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
4517 rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
4518 rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
4519 rapl_power_units = ldexp(1.0, -(msr & 0xf));
4521 tdp = get_tdp_amd(family);
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);
4531 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
4533 void rapl_probe(unsigned int family, unsigned int model)
4536 rapl_probe_intel(family, model);
4537 if (authentic_amd || hygon_genuine)
4538 rapl_probe_amd(family, model);
4541 void perf_limit_reasons_probe(unsigned int family, unsigned int model)
4550 case INTEL_FAM6_HASWELL: /* HSW */
4551 case INTEL_FAM6_HASWELL_L: /* HSW */
4552 case INTEL_FAM6_HASWELL_G: /* HSW */
4553 do_gfx_perf_limit_reasons = 1;
4554 case INTEL_FAM6_HASWELL_X: /* HSX */
4555 do_core_perf_limit_reasons = 1;
4556 do_ring_perf_limit_reasons = 1;
4562 void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4564 if (is_skx(family, model) || is_bdx(family, model) ||
4565 is_icx(family, model))
4566 has_automatic_cstate_conversion = 1;
4569 void prewake_cstate_probe(unsigned int family, unsigned int model)
4571 if (is_icx(family, model))
4572 dis_cstate_prewake = 1;
4575 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4577 unsigned long long msr;
4578 unsigned int dts, dts2;
4581 if (!(do_dts || do_ptm))
4586 /* DTS is per-core, no need to print for each thread */
4587 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4590 if (cpu_migrate(cpu)) {
4591 fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu);
4595 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4596 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4599 dts = (msr >> 16) & 0x7F;
4600 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
4601 cpu, msr, tcc_activation_temp - dts);
4603 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4606 dts = (msr >> 16) & 0x7F;
4607 dts2 = (msr >> 8) & 0x7F;
4608 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4609 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4613 if (do_dts && debug) {
4614 unsigned int resolution;
4616 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4619 dts = (msr >> 16) & 0x7F;
4620 resolution = (msr >> 27) & 0xF;
4621 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4622 cpu, msr, tcc_activation_temp - dts, resolution);
4624 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4627 dts = (msr >> 16) & 0x7F;
4628 dts2 = (msr >> 8) & 0x7F;
4629 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4630 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4636 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4638 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4640 ((msr >> 15) & 1) ? "EN" : "DIS",
4641 ((msr >> 0) & 0x7FFF) * rapl_power_units,
4642 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4643 (((msr >> 16) & 1) ? "EN" : "DIS"));
4648 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4650 unsigned long long msr;
4651 const char *msr_name;
4657 /* RAPL counters are per package, so print only for 1st thread/package */
4658 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4662 if (cpu_migrate(cpu)) {
4663 fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu);
4667 if (do_rapl & RAPL_AMD_F17H) {
4668 msr_name = "MSR_RAPL_PWR_UNIT";
4669 if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
4672 msr_name = "MSR_RAPL_POWER_UNIT";
4673 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4677 fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4678 rapl_power_units, rapl_energy_units, rapl_time_units);
4680 if (do_rapl & RAPL_PKG_POWER_INFO) {
4682 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4686 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4688 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4689 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4690 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4691 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4694 if (do_rapl & RAPL_PKG) {
4696 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4699 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4700 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4702 print_power_limit_msr(cpu, msr, "PKG Limit #1");
4703 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4705 ((msr >> 47) & 1) ? "EN" : "DIS",
4706 ((msr >> 32) & 0x7FFF) * rapl_power_units,
4707 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4708 ((msr >> 48) & 1) ? "EN" : "DIS");
4711 if (do_rapl & RAPL_DRAM_POWER_INFO) {
4712 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4715 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4717 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4718 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4719 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4720 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4722 if (do_rapl & RAPL_DRAM) {
4723 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4725 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4726 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4728 print_power_limit_msr(cpu, msr, "DRAM Limit");
4730 if (do_rapl & RAPL_CORE_POLICY) {
4731 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4734 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4736 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4737 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4739 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4740 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4741 print_power_limit_msr(cpu, msr, "Cores Limit");
4743 if (do_rapl & RAPL_GFX) {
4744 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4747 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4749 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4751 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4752 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4753 print_power_limit_msr(cpu, msr, "GFX Limit");
4759 * SNB adds support for additional MSRs:
4761 * MSR_PKG_C7_RESIDENCY 0x000003fa
4762 * MSR_CORE_C7_RESIDENCY 0x000003fe
4763 * MSR_PKG_C2_RESIDENCY 0x0000060d
4766 int has_snb_msrs(unsigned int family, unsigned int model)
4772 case INTEL_FAM6_SANDYBRIDGE:
4773 case INTEL_FAM6_SANDYBRIDGE_X:
4774 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4775 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4776 case INTEL_FAM6_HASWELL: /* HSW */
4777 case INTEL_FAM6_HASWELL_X: /* HSW */
4778 case INTEL_FAM6_HASWELL_L: /* HSW */
4779 case INTEL_FAM6_HASWELL_G: /* HSW */
4780 case INTEL_FAM6_BROADWELL: /* BDW */
4781 case INTEL_FAM6_BROADWELL_G: /* BDW */
4782 case INTEL_FAM6_BROADWELL_X: /* BDX */
4783 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4784 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4785 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4786 case INTEL_FAM6_ICELAKE_X: /* ICX */
4787 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4788 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4789 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
4790 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4791 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
4798 * HSW ULT added support for C8/C9/C10 MSRs:
4800 * MSR_PKG_C8_RESIDENCY 0x00000630
4801 * MSR_PKG_C9_RESIDENCY 0x00000631
4802 * MSR_PKG_C10_RESIDENCY 0x00000632
4804 * MSR_PKGC8_IRTL 0x00000633
4805 * MSR_PKGC9_IRTL 0x00000634
4806 * MSR_PKGC10_IRTL 0x00000635
4809 int has_c8910_msrs(unsigned int family, unsigned int model)
4815 case INTEL_FAM6_HASWELL_L: /* HSW */
4816 case INTEL_FAM6_BROADWELL: /* BDW */
4817 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4818 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4819 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4820 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4821 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4828 * SKL adds support for additional MSRS:
4830 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4831 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4832 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4833 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4835 int has_skl_msrs(unsigned int family, unsigned int model)
4841 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4842 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4848 int is_slm(unsigned int family, unsigned int model)
4853 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4854 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4860 int is_knl(unsigned int family, unsigned int model)
4865 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4871 int is_cnl(unsigned int family, unsigned int model)
4877 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4884 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4886 if (is_knl(family, model))
4891 #define SLM_BCLK_FREQS 5
4892 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4894 double slm_bclk(void)
4896 unsigned long long msr = 3;
4900 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4901 fprintf(outf, "SLM BCLK: unknown\n");
4904 if (i >= SLM_BCLK_FREQS) {
4905 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
4908 freq = slm_freq_table[i];
4911 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4916 double discover_bclk(unsigned int family, unsigned int model)
4918 if (has_snb_msrs(family, model) || is_knl(family, model))
4920 else if (is_slm(family, model))
4926 int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4928 unsigned int eax, ebx, ecx, edx;
4933 if (cpu_migrate(t->cpu_id)) {
4934 fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id);
4938 if (max_level < 0x1a)
4941 __cpuid(0x1a, eax, ebx, ecx, edx);
4942 eax = (eax >> 24) & 0xFF;
4949 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4950 * the Thermal Control Circuit (TCC) activates.
4951 * This is usually equal to tjMax.
4953 * Older processors do not have this MSR, so there we guess,
4954 * but also allow cmdline over-ride with -T.
4956 * Several MSR temperature values are in units of degrees-C
4957 * below this value, including the Digital Thermal Sensor (DTS),
4958 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4960 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4962 unsigned long long msr;
4963 unsigned int target_c_local;
4966 /* tcc_activation_temp is used only for dts or ptm */
4967 if (!(do_dts || do_ptm))
4970 /* this is a per-package concept */
4971 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4975 if (cpu_migrate(cpu)) {
4976 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4980 if (tcc_activation_temp_override != 0) {
4981 tcc_activation_temp = tcc_activation_temp_override;
4982 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4983 cpu, tcc_activation_temp);
4987 /* Temperature Target MSR is Nehalem and newer only */
4988 if (!do_nhm_platform_info)
4991 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4994 target_c_local = (msr >> 16) & 0xFF;
4997 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4998 cpu, msr, target_c_local);
5000 if (!target_c_local)
5003 tcc_activation_temp = target_c_local;
5008 tcc_activation_temp = TJMAX_DEFAULT;
5009 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
5010 cpu, tcc_activation_temp);
5015 void decode_feature_control_msr(void)
5017 unsigned long long msr;
5019 if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
5020 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
5022 msr & FEAT_CTL_LOCKED ? "" : "UN-",
5023 msr & (1 << 18) ? "SGX" : "");
5026 void decode_misc_enable_msr(void)
5028 unsigned long long msr;
5033 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
5034 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
5036 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
5037 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
5038 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
5039 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
5040 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
5043 void decode_misc_feature_control(void)
5045 unsigned long long msr;
5047 if (!has_misc_feature_control)
5050 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
5051 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
5053 msr & (0 << 0) ? "No-" : "",
5054 msr & (1 << 0) ? "No-" : "",
5055 msr & (2 << 0) ? "No-" : "",
5056 msr & (3 << 0) ? "No-" : "");
5059 * Decode MSR_MISC_PWR_MGMT
5061 * Decode the bits according to the Nehalem documentation
5062 * bit[0] seems to continue to have same meaning going forward
5065 void decode_misc_pwr_mgmt_msr(void)
5067 unsigned long long msr;
5069 if (!do_nhm_platform_info)
5072 if (no_MSR_MISC_PWR_MGMT)
5075 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
5076 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
5078 msr & (1 << 0) ? "DIS" : "EN",
5079 msr & (1 << 1) ? "EN" : "DIS",
5080 msr & (1 << 8) ? "EN" : "DIS");
5083 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
5085 * This MSRs are present on Silvermont processors,
5086 * Intel Atom processor E3000 series (Baytrail), and friends.
5088 void decode_c6_demotion_policy_msr(void)
5090 unsigned long long msr;
5092 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
5093 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
5094 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5096 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
5097 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
5098 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5102 * When models are the same, for the purpose of turbostat, reuse
5104 unsigned int intel_model_duplicates(unsigned int model)
5108 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
5109 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
5110 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
5111 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
5112 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
5113 return INTEL_FAM6_NEHALEM;
5115 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
5116 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
5117 return INTEL_FAM6_NEHALEM_EX;
5119 case INTEL_FAM6_XEON_PHI_KNM:
5120 return INTEL_FAM6_XEON_PHI_KNL;
5122 case INTEL_FAM6_BROADWELL_X:
5123 case INTEL_FAM6_BROADWELL_D: /* BDX-DE */
5124 return INTEL_FAM6_BROADWELL_X;
5126 case INTEL_FAM6_SKYLAKE_L:
5127 case INTEL_FAM6_SKYLAKE:
5128 case INTEL_FAM6_KABYLAKE_L:
5129 case INTEL_FAM6_KABYLAKE:
5130 case INTEL_FAM6_COMETLAKE_L:
5131 case INTEL_FAM6_COMETLAKE:
5132 return INTEL_FAM6_SKYLAKE_L;
5134 case INTEL_FAM6_ICELAKE_L:
5135 case INTEL_FAM6_ICELAKE_NNPI:
5136 case INTEL_FAM6_TIGERLAKE_L:
5137 case INTEL_FAM6_TIGERLAKE:
5138 case INTEL_FAM6_ROCKETLAKE:
5139 case INTEL_FAM6_LAKEFIELD:
5140 case INTEL_FAM6_ALDERLAKE:
5141 case INTEL_FAM6_ALDERLAKE_L:
5142 return INTEL_FAM6_CANNONLAKE_L;
5144 case INTEL_FAM6_ATOM_TREMONT_L:
5145 return INTEL_FAM6_ATOM_TREMONT;
5147 case INTEL_FAM6_ICELAKE_D:
5148 case INTEL_FAM6_SAPPHIRERAPIDS_X:
5149 return INTEL_FAM6_ICELAKE_X;
5154 void print_dev_latency(void)
5156 char *path = "/dev/cpu_dma_latency";
5161 fd = open(path, O_RDONLY);
5163 warn("fopen %s\n", path);
5167 retval = read(fd, (void *)&value, sizeof(int));
5168 if (retval != sizeof(int)) {
5169 warn("read %s\n", path);
5173 fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n",
5174 value, value == 2000000000 ? "default" : "constrained");
5181 * Linux-perf manages the the HW instructions-retired counter
5182 * by enabling when requested, and hiding rollover
5184 void linux_perf_init(void)
5186 if (!BIC_IS_ENABLED(BIC_IPC))
5189 if (access("/proc/sys/kernel/perf_event_paranoid", F_OK))
5192 fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5193 if (fd_instr_count_percpu == NULL)
5194 err(-1, "calloc fd_instr_count_percpu");
5196 BIC_PRESENT(BIC_IPC);
5199 void process_cpuid()
5201 unsigned int eax, ebx, ecx, edx;
5202 unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
5203 unsigned int has_turbo;
5204 unsigned long long ucode_patch = 0;
5206 eax = ebx = ecx = edx = 0;
5208 __cpuid(0, max_level, ebx, ecx, edx);
5210 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
5212 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
5214 else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
5218 fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n",
5219 (char *)&ebx, (char *)&edx, (char *)&ecx, max_level);
5221 __cpuid(1, fms, ebx, ecx, edx);
5222 family = (fms >> 8) & 0xf;
5223 model = (fms >> 4) & 0xf;
5224 stepping = fms & 0xf;
5226 family += (fms >> 20) & 0xff;
5228 model += ((fms >> 16) & 0xf) << 4;
5232 if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
5233 warnx("get_msr(UCODE)\n");
5236 * check max extended function levels of CPUID.
5237 * This is needed to check for invariant TSC.
5238 * This check is valid for both Intel and AMD.
5240 ebx = ecx = edx = 0;
5241 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
5244 fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n",
5245 family, model, stepping, family, model, stepping, (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
5246 fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
5247 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
5248 ecx_flags & (1 << 0) ? "SSE3" : "-",
5249 ecx_flags & (1 << 3) ? "MONITOR" : "-",
5250 ecx_flags & (1 << 6) ? "SMX" : "-",
5251 ecx_flags & (1 << 7) ? "EIST" : "-",
5252 ecx_flags & (1 << 8) ? "TM2" : "-",
5253 edx_flags & (1 << 4) ? "TSC" : "-",
5254 edx_flags & (1 << 5) ? "MSR" : "-",
5255 edx_flags & (1 << 22) ? "ACPI-TM" : "-",
5256 edx_flags & (1 << 28) ? "HT" : "-",
5257 edx_flags & (1 << 29) ? "TM" : "-");
5259 if (genuine_intel) {
5260 model = intel_model_duplicates(model);
5263 if (!(edx_flags & (1 << 5)))
5264 errx(1, "CPUID: no MSR");
5266 if (max_extended_level >= 0x80000007) {
5269 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
5270 * this check is valid for both Intel and AMD
5272 __cpuid(0x80000007, eax, ebx, ecx, edx);
5273 has_invariant_tsc = edx & (1 << 8);
5277 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
5278 * this check is valid for both Intel and AMD
5281 __cpuid(0x6, eax, ebx, ecx, edx);
5282 has_aperf = ecx & (1 << 0);
5284 BIC_PRESENT(BIC_Avg_MHz);
5285 BIC_PRESENT(BIC_Busy);
5286 BIC_PRESENT(BIC_Bzy_MHz);
5288 do_dts = eax & (1 << 0);
5290 BIC_PRESENT(BIC_CoreTmp);
5291 has_turbo = eax & (1 << 1);
5292 do_ptm = eax & (1 << 6);
5294 BIC_PRESENT(BIC_PkgTmp);
5295 has_hwp = eax & (1 << 7);
5296 has_hwp_notify = eax & (1 << 8);
5297 has_hwp_activity_window = eax & (1 << 9);
5298 has_hwp_epp = eax & (1 << 10);
5299 has_hwp_pkg = eax & (1 << 11);
5300 has_epb = ecx & (1 << 3);
5303 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
5304 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
5305 has_aperf ? "" : "No-",
5306 has_turbo ? "" : "No-",
5307 do_dts ? "" : "No-",
5308 do_ptm ? "" : "No-",
5309 has_hwp ? "" : "No-",
5310 has_hwp_notify ? "" : "No-",
5311 has_hwp_activity_window ? "" : "No-",
5312 has_hwp_epp ? "" : "No-",
5313 has_hwp_pkg ? "" : "No-",
5314 has_epb ? "" : "No-");
5317 decode_misc_enable_msr();
5320 if (max_level >= 0x7 && !quiet) {
5325 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
5327 has_sgx = ebx & (1 << 2);
5328 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
5331 decode_feature_control_msr();
5334 if (max_level >= 0x15) {
5335 unsigned int eax_crystal;
5336 unsigned int ebx_tsc;
5339 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
5341 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
5342 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
5346 if (!quiet && (ebx != 0))
5347 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
5348 eax_crystal, ebx_tsc, crystal_hz);
5350 if (crystal_hz == 0)
5352 case INTEL_FAM6_SKYLAKE_L: /* SKL */
5353 crystal_hz = 24000000; /* 24.0 MHz */
5355 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
5356 crystal_hz = 25000000; /* 25.0 MHz */
5358 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5359 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5360 crystal_hz = 19200000; /* 19.2 MHz */
5367 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
5369 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
5370 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
5374 if (max_level >= 0x16) {
5375 unsigned int base_mhz, max_mhz, bus_mhz, edx;
5378 * CPUID 16H Base MHz, Max MHz, Bus MHz
5380 base_mhz = max_mhz = bus_mhz = edx = 0;
5382 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
5384 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
5385 base_mhz, max_mhz, bus_mhz);
5389 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
5391 BIC_PRESENT(BIC_IRQ);
5392 BIC_PRESENT(BIC_TSC_MHz);
5394 if (probe_nhm_msrs(family, model)) {
5395 do_nhm_platform_info = 1;
5396 BIC_PRESENT(BIC_CPU_c1);
5397 BIC_PRESENT(BIC_CPU_c3);
5398 BIC_PRESENT(BIC_CPU_c6);
5399 BIC_PRESENT(BIC_SMI);
5401 do_snb_cstates = has_snb_msrs(family, model);
5404 BIC_PRESENT(BIC_CPU_c7);
5406 do_irtl_snb = has_snb_msrs(family, model);
5407 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
5408 BIC_PRESENT(BIC_Pkgpc2);
5409 if (pkg_cstate_limit >= PCL__3)
5410 BIC_PRESENT(BIC_Pkgpc3);
5411 if (pkg_cstate_limit >= PCL__6)
5412 BIC_PRESENT(BIC_Pkgpc6);
5413 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
5414 BIC_PRESENT(BIC_Pkgpc7);
5415 if (has_slv_msrs(family, model)) {
5416 BIC_NOT_PRESENT(BIC_Pkgpc2);
5417 BIC_NOT_PRESENT(BIC_Pkgpc3);
5418 BIC_PRESENT(BIC_Pkgpc6);
5419 BIC_NOT_PRESENT(BIC_Pkgpc7);
5420 BIC_PRESENT(BIC_Mod_c6);
5421 use_c1_residency_msr = 1;
5423 if (is_jvl(family, model)) {
5424 BIC_NOT_PRESENT(BIC_CPU_c3);
5425 BIC_NOT_PRESENT(BIC_CPU_c7);
5426 BIC_NOT_PRESENT(BIC_Pkgpc2);
5427 BIC_NOT_PRESENT(BIC_Pkgpc3);
5428 BIC_NOT_PRESENT(BIC_Pkgpc6);
5429 BIC_NOT_PRESENT(BIC_Pkgpc7);
5431 if (is_dnv(family, model)) {
5432 BIC_PRESENT(BIC_CPU_c1);
5433 BIC_NOT_PRESENT(BIC_CPU_c3);
5434 BIC_NOT_PRESENT(BIC_Pkgpc3);
5435 BIC_NOT_PRESENT(BIC_CPU_c7);
5436 BIC_NOT_PRESENT(BIC_Pkgpc7);
5437 use_c1_residency_msr = 1;
5439 if (is_skx(family, model) || is_icx(family, model)) {
5440 BIC_NOT_PRESENT(BIC_CPU_c3);
5441 BIC_NOT_PRESENT(BIC_Pkgpc3);
5442 BIC_NOT_PRESENT(BIC_CPU_c7);
5443 BIC_NOT_PRESENT(BIC_Pkgpc7);
5445 if (is_bdx(family, model)) {
5446 BIC_NOT_PRESENT(BIC_CPU_c7);
5447 BIC_NOT_PRESENT(BIC_Pkgpc7);
5449 if (has_c8910_msrs(family, model)) {
5450 if (pkg_cstate_limit >= PCL__8)
5451 BIC_PRESENT(BIC_Pkgpc8);
5452 if (pkg_cstate_limit >= PCL__9)
5453 BIC_PRESENT(BIC_Pkgpc9);
5454 if (pkg_cstate_limit >= PCL_10)
5455 BIC_PRESENT(BIC_Pkgpc10);
5457 do_irtl_hsw = has_c8910_msrs(family, model);
5458 if (has_skl_msrs(family, model)) {
5459 BIC_PRESENT(BIC_Totl_c0);
5460 BIC_PRESENT(BIC_Any_c0);
5461 BIC_PRESENT(BIC_GFX_c0);
5462 BIC_PRESENT(BIC_CPUGFX);
5464 do_slm_cstates = is_slm(family, model);
5465 do_knl_cstates = is_knl(family, model);
5467 if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) ||
5468 is_ehl(family, model))
5469 BIC_NOT_PRESENT(BIC_CPU_c3);
5472 decode_misc_pwr_mgmt_msr();
5474 if (!quiet && has_slv_msrs(family, model))
5475 decode_c6_demotion_policy_msr();
5477 rapl_probe(family, model);
5478 perf_limit_reasons_probe(family, model);
5479 automatic_cstate_conversion_probe(family, model);
5482 dump_cstate_pstate_config_info(family, model);
5485 print_dev_latency();
5487 dump_sysfs_cstate_config();
5489 dump_sysfs_pstate_config();
5491 if (has_skl_msrs(family, model) || is_ehl(family, model))
5492 calculate_tsc_tweak();
5494 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
5495 BIC_PRESENT(BIC_GFX_rc6);
5497 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5498 BIC_PRESENT(BIC_GFXMHz);
5500 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK))
5501 BIC_PRESENT(BIC_GFXACTMHz);
5503 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
5504 BIC_PRESENT(BIC_CPU_LPI);
5506 BIC_NOT_PRESENT(BIC_CPU_LPI);
5508 if (!access(sys_lpi_file_sysfs, R_OK)) {
5509 sys_lpi_file = sys_lpi_file_sysfs;
5510 BIC_PRESENT(BIC_SYS_LPI);
5511 } else if (!access(sys_lpi_file_debugfs, R_OK)) {
5512 sys_lpi_file = sys_lpi_file_debugfs;
5513 BIC_PRESENT(BIC_SYS_LPI);
5515 sys_lpi_file_sysfs = NULL;
5516 BIC_NOT_PRESENT(BIC_SYS_LPI);
5520 decode_misc_feature_control();
5526 * in /dev/cpu/ return success for names that are numbers
5527 * ie. filter out ".", "..", "microcode".
5529 int dir_filter(const struct dirent *dirp)
5531 if (isdigit(dirp->d_name[0]))
5537 int open_dev_cpu_msr(int dummy1)
5542 void topology_probe()
5545 int max_core_id = 0;
5546 int max_package_id = 0;
5548 int max_siblings = 0;
5550 /* Initialize num_cpus, max_cpu_num */
5553 for_all_proc_cpus(count_cpus);
5554 if (!summary_only && topo.num_cpus > 1)
5555 BIC_PRESENT(BIC_CPU);
5558 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5560 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
5562 err(1, "calloc cpus");
5565 * Allocate and initialize cpu_present_set
5567 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5568 if (cpu_present_set == NULL)
5569 err(3, "CPU_ALLOC");
5570 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5571 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5572 for_all_proc_cpus(mark_cpu_present);
5575 * Validate that all cpus in cpu_subset are also in cpu_present_set
5577 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
5578 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
5579 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
5580 err(1, "cpu%d not present", i);
5584 * Allocate and initialize cpu_affinity_set
5586 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5587 if (cpu_affinity_set == NULL)
5588 err(3, "CPU_ALLOC");
5589 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5590 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5592 for_all_proc_cpus(init_thread_id);
5596 * find max_core_id, max_package_id
5598 for (i = 0; i <= topo.max_cpu_num; ++i) {
5601 if (cpu_is_not_present(i)) {
5603 fprintf(outf, "cpu%d NOT PRESENT\n", i);
5607 cpus[i].logical_cpu_id = i;
5609 /* get package information */
5610 cpus[i].physical_package_id = get_physical_package_id(i);
5611 if (cpus[i].physical_package_id > max_package_id)
5612 max_package_id = cpus[i].physical_package_id;
5614 /* get die information */
5615 cpus[i].die_id = get_die_id(i);
5616 if (cpus[i].die_id > max_die_id)
5617 max_die_id = cpus[i].die_id;
5619 /* get numa node information */
5620 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5621 if (cpus[i].physical_node_id > topo.max_node_num)
5622 topo.max_node_num = cpus[i].physical_node_id;
5624 /* get core information */
5625 cpus[i].physical_core_id = get_core_id(i);
5626 if (cpus[i].physical_core_id > max_core_id)
5627 max_core_id = cpus[i].physical_core_id;
5629 /* get thread information */
5630 siblings = get_thread_siblings(&cpus[i]);
5631 if (siblings > max_siblings)
5632 max_siblings = siblings;
5633 if (cpus[i].thread_id == 0)
5637 topo.cores_per_node = max_core_id + 1;
5639 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
5640 max_core_id, topo.cores_per_node);
5641 if (!summary_only && topo.cores_per_node > 1)
5642 BIC_PRESENT(BIC_Core);
5644 topo.num_die = max_die_id + 1;
5646 fprintf(outf, "max_die_id %d, sizing for %d die\n",
5647 max_die_id, topo.num_die);
5648 if (!summary_only && topo.num_die > 1)
5649 BIC_PRESENT(BIC_Die);
5651 topo.num_packages = max_package_id + 1;
5653 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
5654 max_package_id, topo.num_packages);
5655 if (!summary_only && topo.num_packages > 1)
5656 BIC_PRESENT(BIC_Package);
5660 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
5661 if (!summary_only && topo.nodes_per_pkg > 1)
5662 BIC_PRESENT(BIC_Node);
5664 topo.threads_per_core = max_siblings;
5666 fprintf(outf, "max_siblings %d\n", max_siblings);
5671 for (i = 0; i <= topo.max_cpu_num; ++i) {
5672 if (cpu_is_not_present(i))
5675 "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
5676 i, cpus[i].physical_package_id, cpus[i].die_id,
5677 cpus[i].physical_node_id,
5678 cpus[i].logical_node_id,
5679 cpus[i].physical_core_id,
5686 allocate_counters(struct thread_data **t, struct core_data **c,
5687 struct pkg_data **p)
5690 int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
5692 int num_threads = topo.threads_per_core * num_cores;
5694 *t = calloc(num_threads, sizeof(struct thread_data));
5698 for (i = 0; i < num_threads; i++)
5699 (*t)[i].cpu_id = -1;
5701 *c = calloc(num_cores, sizeof(struct core_data));
5705 for (i = 0; i < num_cores; i++)
5706 (*c)[i].core_id = -1;
5708 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
5712 for (i = 0; i < topo.num_packages; i++)
5713 (*p)[i].package_id = i;
5717 err(1, "calloc counters");
5722 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5724 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
5725 struct pkg_data *pkg_base, int cpu_id)
5727 int pkg_id = cpus[cpu_id].physical_package_id;
5728 int node_id = cpus[cpu_id].logical_node_id;
5729 int core_id = cpus[cpu_id].physical_core_id;
5730 int thread_id = cpus[cpu_id].thread_id;
5731 struct thread_data *t;
5732 struct core_data *c;
5736 /* Workaround for systems where physical_node_id==-1
5737 * and logical_node_id==(-1 - topo.num_cpus)
5742 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
5743 c = GET_CORE(core_base, core_id, node_id, pkg_id);
5744 p = GET_PKG(pkg_base, pkg_id);
5747 if (thread_id == 0) {
5748 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5749 if (cpu_is_first_core_in_package(cpu_id))
5750 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5753 c->core_id = core_id;
5754 p->package_id = pkg_id;
5758 int initialize_counters(int cpu_id)
5760 init_counter(EVEN_COUNTERS, cpu_id);
5761 init_counter(ODD_COUNTERS, cpu_id);
5765 void allocate_output_buffer()
5767 output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
5768 outp = output_buffer;
5770 err(-1, "calloc output buffer");
5772 void allocate_fd_percpu(void)
5774 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5775 if (fd_percpu == NULL)
5776 err(-1, "calloc fd_percpu");
5778 void allocate_irq_buffers(void)
5780 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5781 if (irq_column_2_cpu == NULL)
5782 err(-1, "calloc %d", topo.num_cpus);
5784 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5785 if (irqs_per_cpu == NULL)
5786 err(-1, "calloc %d", topo.max_cpu_num + 1);
5788 void setup_all_buffers(void)
5791 allocate_irq_buffers();
5792 allocate_fd_percpu();
5793 allocate_counters(&thread_even, &core_even, &package_even);
5794 allocate_counters(&thread_odd, &core_odd, &package_odd);
5795 allocate_output_buffer();
5796 for_all_proc_cpus(initialize_counters);
5799 void set_base_cpu(void)
5801 base_cpu = sched_getcpu();
5803 err(-ENODEV, "No valid cpus found");
5806 fprintf(outf, "base_cpu = %d\n", base_cpu);
5809 void turbostat_init()
5811 setup_all_buffers();
5814 check_permissions();
5820 for_all_cpus(print_hwp, ODD_COUNTERS);
5823 for_all_cpus(print_epb, ODD_COUNTERS);
5826 for_all_cpus(print_perf_limit, ODD_COUNTERS);
5829 for_all_cpus(print_rapl, ODD_COUNTERS);
5831 for_all_cpus(set_temperature_target, ODD_COUNTERS);
5833 for_all_cpus(get_cpu_type, ODD_COUNTERS);
5834 for_all_cpus(get_cpu_type, EVEN_COUNTERS);
5837 for_all_cpus(print_thermal, ODD_COUNTERS);
5839 if (!quiet && do_irtl_snb)
5843 int fork_it(char **argv)
5848 snapshot_proc_sysfs_files();
5849 status = for_all_cpus(get_counters, EVEN_COUNTERS);
5850 first_counter_read = 0;
5853 /* clear affinity side-effect of get_counters() */
5854 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5855 gettimeofday(&tv_even, (struct timezone *)NULL);
5860 execvp(argv[0], argv);
5861 err(errno, "exec %s", argv[0]);
5865 if (child_pid == -1)
5868 signal(SIGINT, SIG_IGN);
5869 signal(SIGQUIT, SIG_IGN);
5870 if (waitpid(child_pid, &status, 0) == -1)
5871 err(status, "waitpid");
5873 if (WIFEXITED(status))
5874 status = WEXITSTATUS(status);
5877 * n.b. fork_it() does not check for errors from for_all_cpus()
5878 * because re-starting is problematic when forking
5880 snapshot_proc_sysfs_files();
5881 for_all_cpus(get_counters, ODD_COUNTERS);
5882 gettimeofday(&tv_odd, (struct timezone *)NULL);
5883 timersub(&tv_odd, &tv_even, &tv_delta);
5884 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5885 fprintf(outf, "%s: Counter reset detected\n", progname);
5887 compute_average(EVEN_COUNTERS);
5888 format_all_counters(EVEN_COUNTERS);
5891 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5893 flush_output_stderr();
5898 int get_and_dump_counters(void)
5902 snapshot_proc_sysfs_files();
5903 status = for_all_cpus(get_counters, ODD_COUNTERS);
5907 status = for_all_cpus(dump_counters, ODD_COUNTERS);
5911 flush_output_stdout();
5916 void print_version() {
5917 fprintf(outf, "turbostat version 21.03.12"
5918 " - Len Brown <lenb@kernel.org>\n");
5921 int add_counter(unsigned int msr_num, char *path, char *name,
5922 unsigned int width, enum counter_scope scope,
5923 enum counter_type type, enum counter_format format, int flags)
5925 struct msr_counter *msrp;
5927 msrp = calloc(1, sizeof(struct msr_counter));
5933 msrp->msr_num = msr_num;
5934 strncpy(msrp->name, name, NAME_BYTES - 1);
5936 strncpy(msrp->path, path, PATH_BYTES - 1);
5937 msrp->width = width;
5939 msrp->format = format;
5940 msrp->flags = flags;
5945 msrp->next = sys.tp;
5947 sys.added_thread_counters++;
5948 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5949 fprintf(stderr, "exceeded max %d added thread counters\n",
5950 MAX_ADDED_COUNTERS);
5956 msrp->next = sys.cp;
5958 sys.added_core_counters++;
5959 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5960 fprintf(stderr, "exceeded max %d added core counters\n",
5961 MAX_ADDED_COUNTERS);
5967 msrp->next = sys.pp;
5969 sys.added_package_counters++;
5970 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5971 fprintf(stderr, "exceeded max %d added package counters\n",
5972 MAX_ADDED_COUNTERS);
5981 void parse_add_command(char *add_command)
5985 char name_buffer[NAME_BYTES] = "";
5988 enum counter_scope scope = SCOPE_CPU;
5989 enum counter_type type = COUNTER_CYCLES;
5990 enum counter_format format = FORMAT_DELTA;
5992 while (add_command) {
5994 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5997 if (sscanf(add_command, "msr%d", &msr_num) == 1)
6000 if (*add_command == '/') {
6005 if (sscanf(add_command, "u%d", &width) == 1) {
6006 if ((width == 32) || (width == 64))
6010 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
6014 if (!strncmp(add_command, "core", strlen("core"))) {
6018 if (!strncmp(add_command, "package", strlen("package"))) {
6019 scope = SCOPE_PACKAGE;
6022 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
6023 type = COUNTER_CYCLES;
6026 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
6027 type = COUNTER_SECONDS;
6030 if (!strncmp(add_command, "usec", strlen("usec"))) {
6031 type = COUNTER_USEC;
6034 if (!strncmp(add_command, "raw", strlen("raw"))) {
6035 format = FORMAT_RAW;
6038 if (!strncmp(add_command, "delta", strlen("delta"))) {
6039 format = FORMAT_DELTA;
6042 if (!strncmp(add_command, "percent", strlen("percent"))) {
6043 format = FORMAT_PERCENT;
6047 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
6050 eos = strchr(name_buffer, ',');
6057 add_command = strchr(add_command, ',');
6059 *add_command = '\0';
6064 if ((msr_num == 0) && (path == NULL)) {
6065 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
6069 /* generate default column header */
6070 if (*name_buffer == '\0') {
6072 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6074 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6077 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
6086 int is_deferred_skip(char *name)
6090 for (i = 0; i < deferred_skip_index; ++i)
6091 if (!strcmp(name, deferred_skip_names[i]))
6096 void probe_sysfs(void)
6104 if (!DO_BIC(BIC_sysfs))
6107 for (state = 10; state >= 0; --state) {
6109 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
6111 input = fopen(path, "r");
6114 if (!fgets(name_buf, sizeof(name_buf), input))
6115 err(1, "%s: failed to read file", path);
6117 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6118 sp = strchr(name_buf, '-');
6120 sp = strchrnul(name_buf, '\n');
6124 remove_underbar(name_buf);
6128 sprintf(path, "cpuidle/state%d/time", state);
6130 if (is_deferred_skip(name_buf))
6133 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
6134 FORMAT_PERCENT, SYSFS_PERCPU);
6137 for (state = 10; state >= 0; --state) {
6139 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
6141 input = fopen(path, "r");
6144 if (!fgets(name_buf, sizeof(name_buf), input))
6145 err(1, "%s: failed to read file", path);
6146 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6147 sp = strchr(name_buf, '-');
6149 sp = strchrnul(name_buf, '\n');
6153 remove_underbar(name_buf);
6155 sprintf(path, "cpuidle/state%d/usage", state);
6157 if (is_deferred_skip(name_buf))
6160 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
6161 FORMAT_DELTA, SYSFS_PERCPU);
6168 * parse cpuset with following syntax
6169 * 1,2,4..6,8-10 and set bits in cpu_subset
6171 void parse_cpu_command(char *optarg)
6173 unsigned int start, end;
6176 if (!strcmp(optarg, "core")) {
6182 if (!strcmp(optarg, "package")) {
6188 if (show_core_only || show_pkg_only)
6191 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
6192 if (cpu_subset == NULL)
6193 err(3, "CPU_ALLOC");
6194 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
6196 CPU_ZERO_S(cpu_subset_size, cpu_subset);
6200 while (next && *next) {
6202 if (*next == '-') /* no negative cpu numbers */
6205 start = strtoul(next, &next, 10);
6207 if (start >= CPU_SUBSET_MAXCPUS)
6209 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6220 next += 1; /* start range */
6221 } else if (*next == '.') {
6224 next += 1; /* start range */
6229 end = strtoul(next, &next, 10);
6233 while (++start <= end) {
6234 if (start >= CPU_SUBSET_MAXCPUS)
6236 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6241 else if (*next != '\0')
6248 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
6254 void cmdline(int argc, char **argv)
6257 int option_index = 0;
6258 static struct option long_options[] = {
6259 {"add", required_argument, 0, 'a'},
6260 {"cpu", required_argument, 0, 'c'},
6261 {"Dump", no_argument, 0, 'D'},
6262 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
6263 {"enable", required_argument, 0, 'e'},
6264 {"interval", required_argument, 0, 'i'},
6265 {"IPC", no_argument, 0, 'I'},
6266 {"num_iterations", required_argument, 0, 'n'},
6267 {"help", no_argument, 0, 'h'},
6268 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
6269 {"Joules", no_argument, 0, 'J'},
6270 {"list", no_argument, 0, 'l'},
6271 {"out", required_argument, 0, 'o'},
6272 {"quiet", no_argument, 0, 'q'},
6273 {"show", required_argument, 0, 's'},
6274 {"Summary", no_argument, 0, 'S'},
6275 {"TCC", required_argument, 0, 'T'},
6276 {"version", no_argument, 0, 'v' },
6282 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
6283 long_options, &option_index)) != -1) {
6286 parse_add_command(optarg);
6289 parse_cpu_command(optarg);
6295 /* --enable specified counter */
6296 bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
6300 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6304 * --hide: do not show those specified
6305 * multiple invocations simply clear more bits in enabled mask
6307 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
6315 double interval = strtod(optarg, NULL);
6317 if (interval < 0.001) {
6318 fprintf(outf, "interval %f seconds is too small\n",
6323 interval_tv.tv_sec = interval_ts.tv_sec = interval;
6324 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
6325 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
6332 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6337 outf = fopen_or_die(optarg, "w");
6343 num_iterations = strtod(optarg, NULL);
6345 if (num_iterations <= 0) {
6346 fprintf(outf, "iterations %d should be positive number\n",
6353 * --show: show only those specified
6354 * The 1st invocation will clear and replace the enabled mask
6355 * subsequent invocations can add to it.
6358 bic_enabled = bic_lookup(optarg, SHOW_LIST);
6360 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
6367 tcc_activation_temp_override = atoi(optarg);
6377 int main(int argc, char **argv)
6380 cmdline(argc, argv);
6389 /* dump counters and exit */
6391 return get_and_dump_counters();
6393 /* list header and exit */
6394 if (list_header_only) {
6396 flush_output_stdout();
6402 * if any params left, it must be a command to fork
6405 return fork_it(argv + optind);