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) 2021 Intel Corporation.
7 * Len Brown <len.brown@intel.com>
12 #include INTEL_FAMILY_HEADER
17 #include <sys/types.h>
20 #include <sys/select.h>
21 #include <sys/resource.h>
33 #include <sys/capability.h>
36 #include <linux/perf_event.h>
37 #include <asm/unistd.h>
40 char *proc_stat = "/proc/stat";
43 int *fd_instr_count_percpu;
44 struct timeval interval_tv = { 5, 0 };
45 struct timespec interval_ts = { 5, 0 };
47 /* Save original CPU model */
48 unsigned int model_orig;
50 unsigned int num_iterations;
54 unsigned int sums_need_wide_columns;
55 unsigned int rapl_joules;
56 unsigned int summary_only;
57 unsigned int list_header_only;
58 unsigned int dump_only;
59 unsigned int do_snb_cstates;
60 unsigned int do_knl_cstates;
61 unsigned int do_slm_cstates;
62 unsigned int use_c1_residency_msr;
63 unsigned int has_aperf;
65 unsigned int do_irtl_snb;
66 unsigned int do_irtl_hsw;
67 unsigned int units = 1000000; /* MHz etc */
68 unsigned int genuine_intel;
69 unsigned int authentic_amd;
70 unsigned int hygon_genuine;
71 unsigned int max_level, max_extended_level;
72 unsigned int has_invariant_tsc;
73 unsigned int do_nhm_platform_info;
74 unsigned int no_MSR_MISC_PWR_MGMT;
75 unsigned int aperf_mperf_multiplier = 1;
78 unsigned int has_base_hz;
79 double tsc_tweak = 1.0;
80 unsigned int show_pkg_only;
81 unsigned int show_core_only;
82 char *output_buffer, *outp;
87 unsigned long long gfx_cur_rc6_ms;
88 unsigned long long cpuidle_cur_cpu_lpi_us;
89 unsigned long long cpuidle_cur_sys_lpi_us;
90 unsigned int gfx_cur_mhz;
91 unsigned int gfx_act_mhz;
93 unsigned int tj_max_override;
95 double rapl_power_units, rapl_time_units;
96 double rapl_dram_energy_units, rapl_energy_units;
97 double rapl_joule_counter_range;
98 unsigned int do_core_perf_limit_reasons;
99 unsigned int has_automatic_cstate_conversion;
100 unsigned int dis_cstate_prewake;
101 unsigned int do_gfx_perf_limit_reasons;
102 unsigned int do_ring_perf_limit_reasons;
103 unsigned int crystal_hz;
104 unsigned long long tsc_hz;
106 double discover_bclk(unsigned int family, unsigned int model);
107 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
108 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
109 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
110 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
111 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
112 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
113 unsigned int has_misc_feature_control;
114 unsigned int first_counter_read = 1;
117 #define RAPL_PKG (1 << 0)
118 /* 0x610 MSR_PKG_POWER_LIMIT */
119 /* 0x611 MSR_PKG_ENERGY_STATUS */
120 #define RAPL_PKG_PERF_STATUS (1 << 1)
121 /* 0x613 MSR_PKG_PERF_STATUS */
122 #define RAPL_PKG_POWER_INFO (1 << 2)
123 /* 0x614 MSR_PKG_POWER_INFO */
125 #define RAPL_DRAM (1 << 3)
126 /* 0x618 MSR_DRAM_POWER_LIMIT */
127 /* 0x619 MSR_DRAM_ENERGY_STATUS */
128 #define RAPL_DRAM_PERF_STATUS (1 << 4)
129 /* 0x61b MSR_DRAM_PERF_STATUS */
130 #define RAPL_DRAM_POWER_INFO (1 << 5)
131 /* 0x61c MSR_DRAM_POWER_INFO */
133 #define RAPL_CORES_POWER_LIMIT (1 << 6)
134 /* 0x638 MSR_PP0_POWER_LIMIT */
135 #define RAPL_CORE_POLICY (1 << 7)
136 /* 0x63a MSR_PP0_POLICY */
138 #define RAPL_GFX (1 << 8)
139 /* 0x640 MSR_PP1_POWER_LIMIT */
140 /* 0x641 MSR_PP1_ENERGY_STATUS */
141 /* 0x642 MSR_PP1_POLICY */
143 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
144 /* 0x639 MSR_PP0_ENERGY_STATUS */
145 #define RAPL_PER_CORE_ENERGY (1 << 10)
146 /* Indicates cores energy collection is per-core,
147 * not per-package. */
148 #define RAPL_AMD_F17H (1 << 11)
149 /* 0xc0010299 MSR_RAPL_PWR_UNIT */
150 /* 0xc001029a MSR_CORE_ENERGY_STAT */
151 /* 0xc001029b MSR_PKG_ENERGY_STAT */
152 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
153 #define TJMAX_DEFAULT 100
155 /* MSRs that are not yet in the kernel-provided header. */
156 #define MSR_RAPL_PWR_UNIT 0xc0010299
157 #define MSR_CORE_ENERGY_STAT 0xc001029a
158 #define MSR_PKG_ENERGY_STAT 0xc001029b
160 #define MAX(a, b) ((a) > (b) ? (a) : (b))
163 * buffer size used by sscanf() for added column names
164 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
166 #define NAME_BYTES 20
167 #define PATH_BYTES 128
172 #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
173 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
174 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
175 #define MAX_ADDED_COUNTERS 8
176 #define MAX_ADDED_THREAD_COUNTERS 24
177 #define BITMASK_SIZE 32
180 struct timeval tv_begin;
181 struct timeval tv_end;
182 struct timeval tv_delta;
183 unsigned long long tsc;
184 unsigned long long aperf;
185 unsigned long long mperf;
186 unsigned long long c1;
187 unsigned long long instr_count;
188 unsigned long long irq_count;
189 unsigned int smi_count;
191 unsigned int apic_id;
192 unsigned int x2apic_id;
195 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
196 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
197 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
198 } *thread_even, *thread_odd;
201 unsigned long long c3;
202 unsigned long long c6;
203 unsigned long long c7;
204 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
205 unsigned int core_temp_c;
206 unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */
207 unsigned int core_id;
208 unsigned long long counter[MAX_ADDED_COUNTERS];
209 } *core_even, *core_odd;
212 unsigned long long pc2;
213 unsigned long long pc3;
214 unsigned long long pc6;
215 unsigned long long pc7;
216 unsigned long long pc8;
217 unsigned long long pc9;
218 unsigned long long pc10;
219 unsigned long long cpu_lpi;
220 unsigned long long sys_lpi;
221 unsigned long long pkg_wtd_core_c0;
222 unsigned long long pkg_any_core_c0;
223 unsigned long long pkg_any_gfxe_c0;
224 unsigned long long pkg_both_core_gfxe_c0;
225 long long gfx_rc6_ms;
226 unsigned int gfx_mhz;
227 unsigned int gfx_act_mhz;
228 unsigned int package_id;
229 unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */
230 unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */
231 unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */
232 unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */
233 unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
234 unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
235 unsigned int pkg_temp_c;
236 unsigned long long counter[MAX_ADDED_COUNTERS];
237 } *package_even, *package_odd;
239 #define ODD_COUNTERS thread_odd, core_odd, package_odd
240 #define EVEN_COUNTERS thread_even, core_even, package_even
242 #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
245 topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
246 ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
247 ((core_no) * topo.threads_per_core) + \
250 #define GET_CORE(core_base, core_no, node_no, pkg_no) \
252 ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
253 ((node_no) * topo.cores_per_node) + \
256 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
258 enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE };
259 enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC };
260 enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT };
263 unsigned int msr_num;
264 char name[NAME_BYTES];
265 char path[PATH_BYTES];
267 enum counter_type type;
268 enum counter_format format;
269 struct msr_counter *next;
271 #define FLAGS_HIDE (1 << 0)
272 #define FLAGS_SHOW (1 << 1)
273 #define SYSFS_PERCPU (1 << 1)
277 * The accumulated sum of MSR is defined as a monotonic
278 * increasing MSR, it will be accumulated periodically,
279 * despite its register's bit width.
291 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr);
293 struct msr_sum_array {
294 /* get_msr_sum() = sum + (get_msr() - last) */
296 /*The accumulated MSR value is updated by the timer */
297 unsigned long long sum;
298 /*The MSR footprint recorded in last timer */
299 unsigned long long last;
300 } entries[IDX_COUNT];
303 /* The percpu MSR sum array.*/
304 struct msr_sum_array *per_cpu_msr_sum;
306 off_t idx_to_offset(int idx)
312 if (do_rapl & RAPL_AMD_F17H)
313 offset = MSR_PKG_ENERGY_STAT;
315 offset = MSR_PKG_ENERGY_STATUS;
317 case IDX_DRAM_ENERGY:
318 offset = MSR_DRAM_ENERGY_STATUS;
321 offset = MSR_PP0_ENERGY_STATUS;
324 offset = MSR_PP1_ENERGY_STATUS;
327 offset = MSR_PKG_PERF_STATUS;
330 offset = MSR_DRAM_PERF_STATUS;
338 int offset_to_idx(off_t offset)
343 case MSR_PKG_ENERGY_STATUS:
344 case MSR_PKG_ENERGY_STAT:
345 idx = IDX_PKG_ENERGY;
347 case MSR_DRAM_ENERGY_STATUS:
348 idx = IDX_DRAM_ENERGY;
350 case MSR_PP0_ENERGY_STATUS:
351 idx = IDX_PP0_ENERGY;
353 case MSR_PP1_ENERGY_STATUS:
354 idx = IDX_PP1_ENERGY;
356 case MSR_PKG_PERF_STATUS:
359 case MSR_DRAM_PERF_STATUS:
368 int idx_valid(int idx)
372 return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
373 case IDX_DRAM_ENERGY:
374 return do_rapl & RAPL_DRAM;
376 return do_rapl & RAPL_CORES_ENERGY_STATUS;
378 return do_rapl & RAPL_GFX;
380 return do_rapl & RAPL_PKG_PERF_STATUS;
382 return do_rapl & RAPL_DRAM_PERF_STATUS;
388 struct sys_counters {
389 unsigned int added_thread_counters;
390 unsigned int added_core_counters;
391 unsigned int added_package_counters;
392 struct msr_counter *tp;
393 struct msr_counter *cp;
394 struct msr_counter *pp;
397 struct system_summary {
398 struct thread_data threads;
399 struct core_data cores;
400 struct pkg_data packages;
403 struct cpu_topology {
404 int physical_package_id;
407 int physical_node_id;
408 int logical_node_id; /* 0-based count within the package */
409 int physical_core_id;
411 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
423 int threads_per_core;
426 struct timeval tv_even, tv_odd, tv_delta;
428 int *irq_column_2_cpu; /* /proc/interrupts column numbers */
429 int *irqs_per_cpu; /* indexed by cpu_num */
431 void setup_all_buffers(void);
434 char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
435 char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
437 int cpu_is_not_present(int cpu)
439 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
443 * run func(thread, core, package) in topology order
444 * skip non-present cpus
447 int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *),
448 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
450 int retval, pkg_no, core_no, thread_no, node_no;
452 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
453 for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
454 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
455 for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
456 struct thread_data *t;
460 t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
462 if (cpu_is_not_present(t->cpu_id))
465 c = GET_CORE(core_base, core_no, node_no, pkg_no);
466 p = GET_PKG(pkg_base, pkg_no);
468 retval = func(t, c, p);
478 int cpu_migrate(int cpu)
480 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
481 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
482 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
488 int get_msr_fd(int cpu)
498 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
499 fd = open(pathname, O_RDONLY);
501 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
508 static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
510 return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
513 static int perf_instr_count_open(int cpu_num)
515 struct perf_event_attr pea;
518 memset(&pea, 0, sizeof(struct perf_event_attr));
519 pea.type = PERF_TYPE_HARDWARE;
520 pea.size = sizeof(struct perf_event_attr);
521 pea.config = PERF_COUNT_HW_INSTRUCTIONS;
523 /* counter for cpu_num, including user + kernel and all processes */
524 fd = perf_event_open(&pea, -1, cpu_num, -1, 0);
526 err(-1, "cpu%d: perf instruction counter\n", cpu_num);
531 int get_instr_count_fd(int cpu)
533 if (fd_instr_count_percpu[cpu])
534 return fd_instr_count_percpu[cpu];
536 fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu);
538 return fd_instr_count_percpu[cpu];
541 int get_msr(int cpu, off_t offset, unsigned long long *msr)
545 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
547 if (retval != sizeof *msr)
548 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
554 * This list matches the column headers, except
555 * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
556 * 2. Core and CPU are moved to the end, we can't have strings that contain them
557 * matching on them for --show and --hide.
559 struct msr_counter bic[] = {
561 { 0x0, "Time_Of_Day_Seconds" },
569 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL },
615 #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
616 #define BIC_USEC (1ULL << 0)
617 #define BIC_TOD (1ULL << 1)
618 #define BIC_Package (1ULL << 2)
619 #define BIC_Node (1ULL << 3)
620 #define BIC_Avg_MHz (1ULL << 4)
621 #define BIC_Busy (1ULL << 5)
622 #define BIC_Bzy_MHz (1ULL << 6)
623 #define BIC_TSC_MHz (1ULL << 7)
624 #define BIC_IRQ (1ULL << 8)
625 #define BIC_SMI (1ULL << 9)
626 #define BIC_sysfs (1ULL << 10)
627 #define BIC_CPU_c1 (1ULL << 11)
628 #define BIC_CPU_c3 (1ULL << 12)
629 #define BIC_CPU_c6 (1ULL << 13)
630 #define BIC_CPU_c7 (1ULL << 14)
631 #define BIC_ThreadC (1ULL << 15)
632 #define BIC_CoreTmp (1ULL << 16)
633 #define BIC_CoreCnt (1ULL << 17)
634 #define BIC_PkgTmp (1ULL << 18)
635 #define BIC_GFX_rc6 (1ULL << 19)
636 #define BIC_GFXMHz (1ULL << 20)
637 #define BIC_Pkgpc2 (1ULL << 21)
638 #define BIC_Pkgpc3 (1ULL << 22)
639 #define BIC_Pkgpc6 (1ULL << 23)
640 #define BIC_Pkgpc7 (1ULL << 24)
641 #define BIC_Pkgpc8 (1ULL << 25)
642 #define BIC_Pkgpc9 (1ULL << 26)
643 #define BIC_Pkgpc10 (1ULL << 27)
644 #define BIC_CPU_LPI (1ULL << 28)
645 #define BIC_SYS_LPI (1ULL << 29)
646 #define BIC_PkgWatt (1ULL << 30)
647 #define BIC_CorWatt (1ULL << 31)
648 #define BIC_GFXWatt (1ULL << 32)
649 #define BIC_PkgCnt (1ULL << 33)
650 #define BIC_RAMWatt (1ULL << 34)
651 #define BIC_PKG__ (1ULL << 35)
652 #define BIC_RAM__ (1ULL << 36)
653 #define BIC_Pkg_J (1ULL << 37)
654 #define BIC_Cor_J (1ULL << 38)
655 #define BIC_GFX_J (1ULL << 39)
656 #define BIC_RAM_J (1ULL << 40)
657 #define BIC_Mod_c6 (1ULL << 41)
658 #define BIC_Totl_c0 (1ULL << 42)
659 #define BIC_Any_c0 (1ULL << 43)
660 #define BIC_GFX_c0 (1ULL << 44)
661 #define BIC_CPUGFX (1ULL << 45)
662 #define BIC_Core (1ULL << 46)
663 #define BIC_CPU (1ULL << 47)
664 #define BIC_APIC (1ULL << 48)
665 #define BIC_X2APIC (1ULL << 49)
666 #define BIC_Die (1ULL << 50)
667 #define BIC_GFXACTMHz (1ULL << 51)
668 #define BIC_IPC (1ULL << 52)
670 #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die )
671 #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__)
672 #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz )
673 #define BIC_IDLE ( BIC_sysfs | BIC_CPU_c1 | BIC_CPU_c3 | BIC_CPU_c6 | BIC_CPU_c7 | BIC_GFX_rc6 | BIC_Pkgpc2 | BIC_Pkgpc3 | BIC_Pkgpc6 | BIC_Pkgpc7 | BIC_Pkgpc8 | BIC_Pkgpc9 | BIC_Pkgpc10 | BIC_CPU_LPI | BIC_SYS_LPI | BIC_Mod_c6 | BIC_Totl_c0 | BIC_Any_c0 | BIC_GFX_c0 | BIC_CPUGFX)
674 #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC)
676 #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
678 unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
679 unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
681 #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
682 #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
683 #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
684 #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
685 #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
686 #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT)
688 #define MAX_DEFERRED 16
689 char *deferred_skip_names[MAX_DEFERRED];
690 int deferred_skip_index;
693 * HIDE_LIST - hide this list of counters, show the rest [default]
694 * SHOW_LIST - show this list of counters, hide the rest
696 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
701 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
703 "Turbostat forks the specified COMMAND and prints statistics\n"
704 "when COMMAND completes.\n"
705 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
706 "to print statistics, until interrupted.\n"
707 " -a, --add add a counter\n"
708 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
709 " -c, --cpu cpu-set limit output to summary plus cpu-set:\n"
710 " {core | package | j,k,l..m,n-p }\n"
711 " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n"
712 " -D, --Dump displays the raw counter values\n"
713 " -e, --enable [all | column]\n"
714 " shows all or the specified disabled column\n"
715 " -H, --hide [column|column,column,...]\n"
716 " hide the specified column(s)\n"
717 " -i, --interval sec.subsec\n"
718 " Override default 5-second measurement interval\n"
719 " -J, --Joules displays energy in Joules instead of Watts\n"
720 " -l, --list list column headers only\n"
721 " -n, --num_iterations num\n"
722 " number of the measurement iterations\n"
724 " create or truncate \"file\" for all output\n"
725 " -q, --quiet skip decoding system configuration header\n"
726 " -s, --show [column|column,column,...]\n"
727 " show only the specified column(s)\n"
729 " limits output to 1-line system summary per interval\n"
730 " -T, --TCC temperature\n"
731 " sets the Thermal Control Circuit temperature in\n"
733 " -h, --help print this help message\n"
734 " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n");
739 * for all the strings in comma separate name_list,
740 * set the approprate bit in return value.
742 unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
745 unsigned long long retval = 0;
750 comma = strchr(name_list, ',');
755 if (!strcmp(name_list, "all"))
757 if (!strcmp(name_list, "topology"))
759 if (!strcmp(name_list, "power"))
760 return BIC_THERMAL_PWR;
761 if (!strcmp(name_list, "idle"))
763 if (!strcmp(name_list, "frequency"))
764 return BIC_FREQUENCY;
765 if (!strcmp(name_list, "other"))
767 if (!strcmp(name_list, "all"))
770 for (i = 0; i < MAX_BIC; ++i) {
771 if (!strcmp(name_list, bic[i].name)) {
772 retval |= (1ULL << i);
777 if (mode == SHOW_LIST) {
778 fprintf(stderr, "Invalid counter name: %s\n", name_list);
781 deferred_skip_names[deferred_skip_index++] = name_list;
783 fprintf(stderr, "deferred \"%s\"\n", name_list);
784 if (deferred_skip_index >= MAX_DEFERRED) {
785 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
786 MAX_DEFERRED, name_list);
800 void print_header(char *delim)
802 struct msr_counter *mp;
805 if (DO_BIC(BIC_USEC))
806 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
808 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
809 if (DO_BIC(BIC_Package))
810 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
812 outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
813 if (DO_BIC(BIC_Node))
814 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
815 if (DO_BIC(BIC_Core))
816 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
818 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
819 if (DO_BIC(BIC_APIC))
820 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
821 if (DO_BIC(BIC_X2APIC))
822 outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
823 if (DO_BIC(BIC_Avg_MHz))
824 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
825 if (DO_BIC(BIC_Busy))
826 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
827 if (DO_BIC(BIC_Bzy_MHz))
828 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
829 if (DO_BIC(BIC_TSC_MHz))
830 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
833 outp += sprintf(outp, "%sIPC", (printed++ ? delim : ""));
835 if (DO_BIC(BIC_IRQ)) {
836 if (sums_need_wide_columns)
837 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
839 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
843 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
845 for (mp = sys.tp; mp; mp = mp->next) {
847 if (mp->format == FORMAT_RAW) {
849 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
851 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
853 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
854 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
856 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
860 if (DO_BIC(BIC_CPU_c1))
861 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
862 if (DO_BIC(BIC_CPU_c3))
863 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
864 if (DO_BIC(BIC_CPU_c6))
865 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
866 if (DO_BIC(BIC_CPU_c7))
867 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
869 if (DO_BIC(BIC_Mod_c6))
870 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
872 if (DO_BIC(BIC_CoreTmp))
873 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
875 if (do_rapl && !rapl_joules) {
876 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
877 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
878 } else if (do_rapl && rapl_joules) {
879 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
880 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
883 for (mp = sys.cp; mp; mp = mp->next) {
884 if (mp->format == FORMAT_RAW) {
886 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
888 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
890 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
891 outp += sprintf(outp, "%s%8s", delim, mp->name);
893 outp += sprintf(outp, "%s%s", delim, mp->name);
897 if (DO_BIC(BIC_PkgTmp))
898 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
900 if (DO_BIC(BIC_GFX_rc6))
901 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
903 if (DO_BIC(BIC_GFXMHz))
904 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
906 if (DO_BIC(BIC_GFXACTMHz))
907 outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : ""));
909 if (DO_BIC(BIC_Totl_c0))
910 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
911 if (DO_BIC(BIC_Any_c0))
912 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
913 if (DO_BIC(BIC_GFX_c0))
914 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
915 if (DO_BIC(BIC_CPUGFX))
916 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
918 if (DO_BIC(BIC_Pkgpc2))
919 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
920 if (DO_BIC(BIC_Pkgpc3))
921 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
922 if (DO_BIC(BIC_Pkgpc6))
923 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
924 if (DO_BIC(BIC_Pkgpc7))
925 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
926 if (DO_BIC(BIC_Pkgpc8))
927 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
928 if (DO_BIC(BIC_Pkgpc9))
929 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
930 if (DO_BIC(BIC_Pkgpc10))
931 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
932 if (DO_BIC(BIC_CPU_LPI))
933 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
934 if (DO_BIC(BIC_SYS_LPI))
935 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
937 if (do_rapl && !rapl_joules) {
938 if (DO_BIC(BIC_PkgWatt))
939 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
940 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
941 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
942 if (DO_BIC(BIC_GFXWatt))
943 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
944 if (DO_BIC(BIC_RAMWatt))
945 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
946 if (DO_BIC(BIC_PKG__))
947 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
948 if (DO_BIC(BIC_RAM__))
949 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
950 } else if (do_rapl && rapl_joules) {
951 if (DO_BIC(BIC_Pkg_J))
952 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
953 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
954 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
955 if (DO_BIC(BIC_GFX_J))
956 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
957 if (DO_BIC(BIC_RAM_J))
958 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
959 if (DO_BIC(BIC_PKG__))
960 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
961 if (DO_BIC(BIC_RAM__))
962 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
964 for (mp = sys.pp; mp; mp = mp->next) {
965 if (mp->format == FORMAT_RAW) {
967 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
969 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
971 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
972 outp += sprintf(outp, "%s%8s", delim, mp->name);
974 outp += sprintf(outp, "%s%s", delim, mp->name);
978 outp += sprintf(outp, "\n");
981 int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
984 struct msr_counter *mp;
986 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
989 outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
990 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
991 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
992 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
993 outp += sprintf(outp, "c1: %016llX\n", t->c1);
996 outp += sprintf(outp, "IPC: %lld\n", t->instr_count);
999 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
1000 if (DO_BIC(BIC_SMI))
1001 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
1003 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1004 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]);
1009 outp += sprintf(outp, "core: %d\n", c->core_id);
1010 outp += sprintf(outp, "c3: %016llX\n", c->c3);
1011 outp += sprintf(outp, "c6: %016llX\n", c->c6);
1012 outp += sprintf(outp, "c7: %016llX\n", c->c7);
1013 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
1014 outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
1016 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1017 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]);
1019 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
1023 outp += sprintf(outp, "package: %d\n", p->package_id);
1025 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
1026 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
1027 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
1028 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
1030 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
1031 if (DO_BIC(BIC_Pkgpc3))
1032 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
1033 if (DO_BIC(BIC_Pkgpc6))
1034 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
1035 if (DO_BIC(BIC_Pkgpc7))
1036 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
1037 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
1038 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
1039 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
1040 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
1041 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
1042 outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
1043 outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
1044 outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
1045 outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
1046 outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status);
1047 outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status);
1048 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
1050 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1051 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]);
1055 outp += sprintf(outp, "\n");
1061 * column formatting convention & formats
1063 int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1065 double interval_float, tsc;
1068 struct msr_counter *mp;
1072 /* if showing only 1st thread in core and this isn't one, bail out */
1073 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1076 /* if showing only 1st thread in pkg and this isn't one, bail out */
1077 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1080 /*if not summary line and --cpu is used */
1081 if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
1084 if (DO_BIC(BIC_USEC)) {
1085 /* on each row, print how many usec each timestamp took to gather */
1088 timersub(&t->tv_end, &t->tv_begin, &tv);
1089 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
1092 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
1093 if (DO_BIC(BIC_TOD))
1094 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
1096 interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0;
1098 tsc = t->tsc * tsc_tweak;
1100 /* topo columns, print blanks on 1st (average) line */
1101 if (t == &average.threads) {
1102 if (DO_BIC(BIC_Package))
1103 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1104 if (DO_BIC(BIC_Die))
1105 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1106 if (DO_BIC(BIC_Node))
1107 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1108 if (DO_BIC(BIC_Core))
1109 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1110 if (DO_BIC(BIC_CPU))
1111 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1112 if (DO_BIC(BIC_APIC))
1113 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1114 if (DO_BIC(BIC_X2APIC))
1115 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1117 if (DO_BIC(BIC_Package)) {
1119 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
1121 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1123 if (DO_BIC(BIC_Die)) {
1125 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
1127 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1129 if (DO_BIC(BIC_Node)) {
1131 outp += sprintf(outp, "%s%d",
1132 (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id);
1134 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1136 if (DO_BIC(BIC_Core)) {
1138 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
1140 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1142 if (DO_BIC(BIC_CPU))
1143 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
1144 if (DO_BIC(BIC_APIC))
1145 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
1146 if (DO_BIC(BIC_X2APIC))
1147 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
1150 if (DO_BIC(BIC_Avg_MHz))
1151 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float);
1153 if (DO_BIC(BIC_Busy))
1154 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc);
1156 if (DO_BIC(BIC_Bzy_MHz)) {
1159 sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
1161 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1162 tsc / units * t->aperf / t->mperf / interval_float);
1165 if (DO_BIC(BIC_TSC_MHz))
1166 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float);
1168 if (DO_BIC(BIC_IPC))
1169 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf);
1172 if (DO_BIC(BIC_IRQ)) {
1173 if (sums_need_wide_columns)
1174 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
1176 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
1180 if (DO_BIC(BIC_SMI))
1181 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
1183 /* Added counters */
1184 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1185 if (mp->format == FORMAT_RAW) {
1186 if (mp->width == 32)
1188 sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]);
1190 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1191 } else if (mp->format == FORMAT_DELTA) {
1192 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1193 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
1195 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1196 } else if (mp->format == FORMAT_PERCENT) {
1197 if (mp->type == COUNTER_USEC)
1199 sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
1200 t->counter[i] / interval_float / 10000);
1202 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc);
1207 if (DO_BIC(BIC_CPU_c1))
1208 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc);
1210 /* print per-core data only for 1st thread in core */
1211 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1214 if (DO_BIC(BIC_CPU_c3))
1215 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc);
1216 if (DO_BIC(BIC_CPU_c6))
1217 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc);
1218 if (DO_BIC(BIC_CPU_c7))
1219 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc);
1222 if (DO_BIC(BIC_Mod_c6))
1223 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
1225 if (DO_BIC(BIC_CoreTmp))
1226 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1228 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1229 if (mp->format == FORMAT_RAW) {
1230 if (mp->width == 32)
1232 sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]);
1234 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1235 } else if (mp->format == FORMAT_DELTA) {
1236 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1237 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
1239 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1240 } else if (mp->format == FORMAT_PERCENT) {
1241 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc);
1247 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
1249 sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
1250 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
1251 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
1253 /* print per-package data only for 1st core in package */
1254 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1258 if (DO_BIC(BIC_PkgTmp))
1259 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1262 if (DO_BIC(BIC_GFX_rc6)) {
1263 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
1264 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
1266 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
1267 p->gfx_rc6_ms / 10.0 / interval_float);
1272 if (DO_BIC(BIC_GFXMHz))
1273 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
1276 if (DO_BIC(BIC_GFXACTMHz))
1277 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz);
1279 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1280 if (DO_BIC(BIC_Totl_c0))
1281 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc);
1282 if (DO_BIC(BIC_Any_c0))
1283 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc);
1284 if (DO_BIC(BIC_GFX_c0))
1285 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc);
1286 if (DO_BIC(BIC_CPUGFX))
1287 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc);
1289 if (DO_BIC(BIC_Pkgpc2))
1290 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc);
1291 if (DO_BIC(BIC_Pkgpc3))
1292 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc);
1293 if (DO_BIC(BIC_Pkgpc6))
1294 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc);
1295 if (DO_BIC(BIC_Pkgpc7))
1296 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc);
1297 if (DO_BIC(BIC_Pkgpc8))
1298 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc);
1299 if (DO_BIC(BIC_Pkgpc9))
1300 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc);
1301 if (DO_BIC(BIC_Pkgpc10))
1302 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc);
1304 if (DO_BIC(BIC_CPU_LPI))
1306 sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1307 if (DO_BIC(BIC_SYS_LPI))
1309 sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1311 if (DO_BIC(BIC_PkgWatt))
1313 sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1314 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1316 sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1317 if (DO_BIC(BIC_GFXWatt))
1319 sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1320 if (DO_BIC(BIC_RAMWatt))
1322 sprintf(outp, fmt8, (printed++ ? delim : ""),
1323 p->energy_dram * rapl_dram_energy_units / interval_float);
1324 if (DO_BIC(BIC_Pkg_J))
1325 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1326 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1327 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1328 if (DO_BIC(BIC_GFX_J))
1329 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1330 if (DO_BIC(BIC_RAM_J))
1331 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1332 if (DO_BIC(BIC_PKG__))
1334 sprintf(outp, fmt8, (printed++ ? delim : ""),
1335 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1336 if (DO_BIC(BIC_RAM__))
1338 sprintf(outp, fmt8, (printed++ ? delim : ""),
1339 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1341 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1342 if (mp->format == FORMAT_RAW) {
1343 if (mp->width == 32)
1345 sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]);
1347 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1348 } else if (mp->format == FORMAT_DELTA) {
1349 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1350 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
1352 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1353 } else if (mp->format == FORMAT_PERCENT) {
1354 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc);
1359 if (*(outp - 1) != '\n')
1360 outp += sprintf(outp, "\n");
1365 void flush_output_stdout(void)
1374 fputs(output_buffer, filep);
1377 outp = output_buffer;
1380 void flush_output_stderr(void)
1382 fputs(output_buffer, outf);
1384 outp = output_buffer;
1387 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1391 if (!printed || !summary_only)
1394 format_counters(&average.threads, &average.cores, &average.packages);
1401 for_all_cpus(format_counters, t, c, p);
1404 #define DELTA_WRAP32(new, old) \
1405 old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1407 int delta_package(struct pkg_data *new, struct pkg_data *old)
1410 struct msr_counter *mp;
1412 if (DO_BIC(BIC_Totl_c0))
1413 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1414 if (DO_BIC(BIC_Any_c0))
1415 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1416 if (DO_BIC(BIC_GFX_c0))
1417 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1418 if (DO_BIC(BIC_CPUGFX))
1419 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1421 old->pc2 = new->pc2 - old->pc2;
1422 if (DO_BIC(BIC_Pkgpc3))
1423 old->pc3 = new->pc3 - old->pc3;
1424 if (DO_BIC(BIC_Pkgpc6))
1425 old->pc6 = new->pc6 - old->pc6;
1426 if (DO_BIC(BIC_Pkgpc7))
1427 old->pc7 = new->pc7 - old->pc7;
1428 old->pc8 = new->pc8 - old->pc8;
1429 old->pc9 = new->pc9 - old->pc9;
1430 old->pc10 = new->pc10 - old->pc10;
1431 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1432 old->sys_lpi = new->sys_lpi - old->sys_lpi;
1433 old->pkg_temp_c = new->pkg_temp_c;
1435 /* flag an error when rc6 counter resets/wraps */
1436 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1437 old->gfx_rc6_ms = -1;
1439 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1441 old->gfx_mhz = new->gfx_mhz;
1442 old->gfx_act_mhz = new->gfx_act_mhz;
1444 old->energy_pkg = new->energy_pkg - old->energy_pkg;
1445 old->energy_cores = new->energy_cores - old->energy_cores;
1446 old->energy_gfx = new->energy_gfx - old->energy_gfx;
1447 old->energy_dram = new->energy_dram - old->energy_dram;
1448 old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status;
1449 old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status;
1451 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1452 if (mp->format == FORMAT_RAW)
1453 old->counter[i] = new->counter[i];
1455 old->counter[i] = new->counter[i] - old->counter[i];
1461 void delta_core(struct core_data *new, struct core_data *old)
1464 struct msr_counter *mp;
1466 old->c3 = new->c3 - old->c3;
1467 old->c6 = new->c6 - old->c6;
1468 old->c7 = new->c7 - old->c7;
1469 old->core_temp_c = new->core_temp_c;
1470 old->mc6_us = new->mc6_us - old->mc6_us;
1472 DELTA_WRAP32(new->core_energy, old->core_energy);
1474 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1475 if (mp->format == FORMAT_RAW)
1476 old->counter[i] = new->counter[i];
1478 old->counter[i] = new->counter[i] - old->counter[i];
1482 int soft_c1_residency_display(int bic)
1484 if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
1487 return DO_BIC_READ(bic);
1493 int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta)
1496 struct msr_counter *mp;
1498 /* we run cpuid just the 1st time, copy the results */
1499 if (DO_BIC(BIC_APIC))
1500 new->apic_id = old->apic_id;
1501 if (DO_BIC(BIC_X2APIC))
1502 new->x2apic_id = old->x2apic_id;
1505 * the timestamps from start of measurement interval are in "old"
1506 * the timestamp from end of measurement interval are in "new"
1507 * over-write old w/ new so we can print end of interval values
1510 timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
1511 old->tv_begin = new->tv_begin;
1512 old->tv_end = new->tv_end;
1514 old->tsc = new->tsc - old->tsc;
1516 /* check for TSC < 1 Mcycles over interval */
1517 if (old->tsc < (1000 * 1000))
1518 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1519 "You can disable all c-states by booting with \"idle=poll\"\n"
1520 "or just the deep ones with \"processor.max_cstate=1\"");
1522 old->c1 = new->c1 - old->c1;
1524 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
1525 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1526 old->aperf = new->aperf - old->aperf;
1527 old->mperf = new->mperf - old->mperf;
1533 if (use_c1_residency_msr) {
1535 * Some models have a dedicated C1 residency MSR,
1536 * which should be more accurate than the derivation below.
1540 * As counter collection is not atomic,
1541 * it is possible for mperf's non-halted cycles + idle states
1542 * to exceed TSC's all cycles: show c1 = 0% in that case.
1544 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1547 /* normal case, derive c1 */
1548 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1549 - core_delta->c6 - core_delta->c7;
1553 if (old->mperf == 0) {
1555 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1556 old->mperf = 1; /* divide by 0 protection */
1559 if (DO_BIC(BIC_IPC))
1560 old->instr_count = new->instr_count - old->instr_count;
1562 if (DO_BIC(BIC_IRQ))
1563 old->irq_count = new->irq_count - old->irq_count;
1565 if (DO_BIC(BIC_SMI))
1566 old->smi_count = new->smi_count - old->smi_count;
1568 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1569 if (mp->format == FORMAT_RAW)
1570 old->counter[i] = new->counter[i];
1572 old->counter[i] = new->counter[i] - old->counter[i];
1577 int delta_cpu(struct thread_data *t, struct core_data *c,
1578 struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2)
1582 /* calculate core delta only for 1st thread in core */
1583 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1586 /* always calculate thread delta */
1587 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1591 /* calculate package delta only for 1st core in package */
1592 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1593 retval = delta_package(p, p2);
1598 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1601 struct msr_counter *mp;
1603 t->tv_begin.tv_sec = 0;
1604 t->tv_begin.tv_usec = 0;
1605 t->tv_end.tv_sec = 0;
1606 t->tv_end.tv_usec = 0;
1607 t->tv_delta.tv_sec = 0;
1608 t->tv_delta.tv_usec = 0;
1620 /* tells format_counters to dump all fields from this set */
1621 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1630 p->pkg_wtd_core_c0 = 0;
1631 p->pkg_any_core_c0 = 0;
1632 p->pkg_any_gfxe_c0 = 0;
1633 p->pkg_both_core_gfxe_c0 = 0;
1636 if (DO_BIC(BIC_Pkgpc3))
1638 if (DO_BIC(BIC_Pkgpc6))
1640 if (DO_BIC(BIC_Pkgpc7))
1650 p->energy_cores = 0;
1652 p->rapl_pkg_perf_status = 0;
1653 p->rapl_dram_perf_status = 0;
1659 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1662 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1665 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1669 int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1672 struct msr_counter *mp;
1674 /* copy un-changing apic_id's */
1675 if (DO_BIC(BIC_APIC))
1676 average.threads.apic_id = t->apic_id;
1677 if (DO_BIC(BIC_X2APIC))
1678 average.threads.x2apic_id = t->x2apic_id;
1680 /* remember first tv_begin */
1681 if (average.threads.tv_begin.tv_sec == 0)
1682 average.threads.tv_begin = t->tv_begin;
1684 /* remember last tv_end */
1685 average.threads.tv_end = t->tv_end;
1687 average.threads.tsc += t->tsc;
1688 average.threads.aperf += t->aperf;
1689 average.threads.mperf += t->mperf;
1690 average.threads.c1 += t->c1;
1692 average.threads.instr_count += t->instr_count;
1694 average.threads.irq_count += t->irq_count;
1695 average.threads.smi_count += t->smi_count;
1697 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1698 if (mp->format == FORMAT_RAW)
1700 average.threads.counter[i] += t->counter[i];
1703 /* sum per-core values only for 1st thread in core */
1704 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1707 average.cores.c3 += c->c3;
1708 average.cores.c6 += c->c6;
1709 average.cores.c7 += c->c7;
1710 average.cores.mc6_us += c->mc6_us;
1712 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1714 average.cores.core_energy += c->core_energy;
1716 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1717 if (mp->format == FORMAT_RAW)
1719 average.cores.counter[i] += c->counter[i];
1722 /* sum per-pkg values only for 1st core in pkg */
1723 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1726 if (DO_BIC(BIC_Totl_c0))
1727 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1728 if (DO_BIC(BIC_Any_c0))
1729 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1730 if (DO_BIC(BIC_GFX_c0))
1731 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1732 if (DO_BIC(BIC_CPUGFX))
1733 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1735 average.packages.pc2 += p->pc2;
1736 if (DO_BIC(BIC_Pkgpc3))
1737 average.packages.pc3 += p->pc3;
1738 if (DO_BIC(BIC_Pkgpc6))
1739 average.packages.pc6 += p->pc6;
1740 if (DO_BIC(BIC_Pkgpc7))
1741 average.packages.pc7 += p->pc7;
1742 average.packages.pc8 += p->pc8;
1743 average.packages.pc9 += p->pc9;
1744 average.packages.pc10 += p->pc10;
1746 average.packages.cpu_lpi = p->cpu_lpi;
1747 average.packages.sys_lpi = p->sys_lpi;
1749 average.packages.energy_pkg += p->energy_pkg;
1750 average.packages.energy_dram += p->energy_dram;
1751 average.packages.energy_cores += p->energy_cores;
1752 average.packages.energy_gfx += p->energy_gfx;
1754 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1755 average.packages.gfx_mhz = p->gfx_mhz;
1756 average.packages.gfx_act_mhz = p->gfx_act_mhz;
1758 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1760 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1761 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1763 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1764 if (mp->format == FORMAT_RAW)
1766 average.packages.counter[i] += p->counter[i];
1772 * sum the counters for all cpus in the system
1773 * compute the weighted average
1775 void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1778 struct msr_counter *mp;
1780 clear_counters(&average.threads, &average.cores, &average.packages);
1782 for_all_cpus(sum_counters, t, c, p);
1784 /* Use the global time delta for the average. */
1785 average.threads.tv_delta = tv_delta;
1787 average.threads.tsc /= topo.num_cpus;
1788 average.threads.aperf /= topo.num_cpus;
1789 average.threads.mperf /= topo.num_cpus;
1790 average.threads.instr_count /= topo.num_cpus;
1791 average.threads.c1 /= topo.num_cpus;
1793 if (average.threads.irq_count > 9999999)
1794 sums_need_wide_columns = 1;
1796 average.cores.c3 /= topo.num_cores;
1797 average.cores.c6 /= topo.num_cores;
1798 average.cores.c7 /= topo.num_cores;
1799 average.cores.mc6_us /= topo.num_cores;
1801 if (DO_BIC(BIC_Totl_c0))
1802 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1803 if (DO_BIC(BIC_Any_c0))
1804 average.packages.pkg_any_core_c0 /= topo.num_packages;
1805 if (DO_BIC(BIC_GFX_c0))
1806 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1807 if (DO_BIC(BIC_CPUGFX))
1808 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1810 average.packages.pc2 /= topo.num_packages;
1811 if (DO_BIC(BIC_Pkgpc3))
1812 average.packages.pc3 /= topo.num_packages;
1813 if (DO_BIC(BIC_Pkgpc6))
1814 average.packages.pc6 /= topo.num_packages;
1815 if (DO_BIC(BIC_Pkgpc7))
1816 average.packages.pc7 /= topo.num_packages;
1818 average.packages.pc8 /= topo.num_packages;
1819 average.packages.pc9 /= topo.num_packages;
1820 average.packages.pc10 /= topo.num_packages;
1822 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1823 if (mp->format == FORMAT_RAW)
1825 if (mp->type == COUNTER_ITEMS) {
1826 if (average.threads.counter[i] > 9999999)
1827 sums_need_wide_columns = 1;
1830 average.threads.counter[i] /= topo.num_cpus;
1832 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1833 if (mp->format == FORMAT_RAW)
1835 if (mp->type == COUNTER_ITEMS) {
1836 if (average.cores.counter[i] > 9999999)
1837 sums_need_wide_columns = 1;
1839 average.cores.counter[i] /= topo.num_cores;
1841 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1842 if (mp->format == FORMAT_RAW)
1844 if (mp->type == COUNTER_ITEMS) {
1845 if (average.packages.counter[i] > 9999999)
1846 sums_need_wide_columns = 1;
1848 average.packages.counter[i] /= topo.num_packages;
1852 static unsigned long long rdtsc(void)
1854 unsigned int low, high;
1856 asm volatile ("rdtsc":"=a" (low), "=d"(high));
1858 return low | ((unsigned long long)high) << 32;
1862 * Open a file, and exit on failure
1864 FILE *fopen_or_die(const char *path, const char *mode)
1866 FILE *filep = fopen(path, mode);
1869 err(1, "%s: open failed", path);
1874 * snapshot_sysfs_counter()
1876 * return snapshot of given counter
1878 unsigned long long snapshot_sysfs_counter(char *path)
1882 unsigned long long counter;
1884 fp = fopen_or_die(path, "r");
1886 retval = fscanf(fp, "%lld", &counter);
1888 err(1, "snapshot_sysfs_counter(%s)", path);
1895 int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1897 if (mp->msr_num != 0) {
1898 if (get_msr(cpu, mp->msr_num, counterp))
1901 char path[128 + PATH_BYTES];
1903 if (mp->flags & SYSFS_PERCPU) {
1904 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path);
1906 *counterp = snapshot_sysfs_counter(path);
1908 *counterp = snapshot_sysfs_counter(mp->path);
1915 int get_epb(int cpu)
1917 char path[128 + PATH_BYTES];
1918 unsigned long long msr;
1922 sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu);
1924 fp = fopen(path, "r");
1928 ret = fscanf(fp, "%d", &epb);
1930 err(1, "%s(%s)", __func__, path);
1937 get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr);
1942 void get_apic_id(struct thread_data *t)
1944 unsigned int eax, ebx, ecx, edx;
1946 if (DO_BIC(BIC_APIC)) {
1947 eax = ebx = ecx = edx = 0;
1948 __cpuid(1, eax, ebx, ecx, edx);
1950 t->apic_id = (ebx >> 24) & 0xff;
1953 if (!DO_BIC(BIC_X2APIC))
1956 if (authentic_amd || hygon_genuine) {
1957 unsigned int topology_extensions;
1959 if (max_extended_level < 0x8000001e)
1962 eax = ebx = ecx = edx = 0;
1963 __cpuid(0x80000001, eax, ebx, ecx, edx);
1964 topology_extensions = ecx & (1 << 22);
1966 if (topology_extensions == 0)
1969 eax = ebx = ecx = edx = 0;
1970 __cpuid(0x8000001e, eax, ebx, ecx, edx);
1979 if (max_level < 0xb)
1983 __cpuid(0xb, eax, ebx, ecx, edx);
1986 if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
1987 fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
1993 * acquire and record local counters for that cpu
1995 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1997 int cpu = t->cpu_id;
1998 unsigned long long msr;
1999 int aperf_mperf_retry_count = 0;
2000 struct msr_counter *mp;
2003 if (cpu_migrate(cpu)) {
2004 fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu);
2008 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
2010 if (first_counter_read)
2013 t->tsc = rdtsc(); /* we are running on local CPU of interest */
2015 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
2016 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
2019 * The TSC, APERF and MPERF must be read together for
2020 * APERF/MPERF and MPERF/TSC to give accurate results.
2022 * Unfortunately, APERF and MPERF are read by
2023 * individual system call, so delays may occur
2024 * between them. If the time to read them
2025 * varies by a large amount, we re-read them.
2029 * This initial dummy APERF read has been seen to
2030 * reduce jitter in the subsequent reads.
2033 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2036 t->tsc = rdtsc(); /* re-read close to APERF */
2038 tsc_before = t->tsc;
2040 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2043 tsc_between = rdtsc();
2045 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
2048 tsc_after = rdtsc();
2050 aperf_time = tsc_between - tsc_before;
2051 mperf_time = tsc_after - tsc_between;
2054 * If the system call latency to read APERF and MPERF
2055 * differ by more than 2x, then try again.
2057 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
2058 aperf_mperf_retry_count++;
2059 if (aperf_mperf_retry_count < 5)
2062 warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time);
2064 aperf_mperf_retry_count = 0;
2066 t->aperf = t->aperf * aperf_mperf_multiplier;
2067 t->mperf = t->mperf * aperf_mperf_multiplier;
2070 if (DO_BIC(BIC_IPC))
2071 if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long))
2074 if (DO_BIC(BIC_IRQ))
2075 t->irq_count = irqs_per_cpu[cpu];
2076 if (DO_BIC(BIC_SMI)) {
2077 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
2079 t->smi_count = msr & 0xFFFFFFFF;
2081 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
2082 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
2086 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2087 if (get_mp(cpu, mp, &t->counter[i]))
2091 /* collect core counters only for 1st thread in core */
2092 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2095 if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
2096 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
2100 if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
2101 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
2103 } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) {
2104 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
2108 if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) {
2109 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
2111 else if (t->is_atom) {
2113 * For Atom CPUs that has core cstate deeper than c6,
2114 * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper.
2115 * Minus CC7 (and deeper cstates) residency to get
2116 * accturate cc6 residency.
2122 if (DO_BIC(BIC_Mod_c6))
2123 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
2126 if (DO_BIC(BIC_CoreTmp)) {
2127 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2129 c->core_temp_c = tj_max - ((msr >> 16) & 0x7F);
2132 if (do_rapl & RAPL_AMD_F17H) {
2133 if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
2135 c->core_energy = msr & 0xFFFFFFFF;
2138 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2139 if (get_mp(cpu, mp, &c->counter[i]))
2143 /* collect package counters only for 1st core in package */
2144 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2147 if (DO_BIC(BIC_Totl_c0)) {
2148 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
2151 if (DO_BIC(BIC_Any_c0)) {
2152 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
2155 if (DO_BIC(BIC_GFX_c0)) {
2156 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
2159 if (DO_BIC(BIC_CPUGFX)) {
2160 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
2163 if (DO_BIC(BIC_Pkgpc3))
2164 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
2166 if (DO_BIC(BIC_Pkgpc6)) {
2167 if (do_slm_cstates) {
2168 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
2171 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
2176 if (DO_BIC(BIC_Pkgpc2))
2177 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
2179 if (DO_BIC(BIC_Pkgpc7))
2180 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
2182 if (DO_BIC(BIC_Pkgpc8))
2183 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
2185 if (DO_BIC(BIC_Pkgpc9))
2186 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
2188 if (DO_BIC(BIC_Pkgpc10))
2189 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
2192 if (DO_BIC(BIC_CPU_LPI))
2193 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
2194 if (DO_BIC(BIC_SYS_LPI))
2195 p->sys_lpi = cpuidle_cur_sys_lpi_us;
2197 if (do_rapl & RAPL_PKG) {
2198 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr))
2200 p->energy_pkg = msr;
2202 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
2203 if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr))
2205 p->energy_cores = msr;
2207 if (do_rapl & RAPL_DRAM) {
2208 if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
2210 p->energy_dram = msr;
2212 if (do_rapl & RAPL_GFX) {
2213 if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr))
2215 p->energy_gfx = msr;
2217 if (do_rapl & RAPL_PKG_PERF_STATUS) {
2218 if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr))
2220 p->rapl_pkg_perf_status = msr;
2222 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
2223 if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr))
2225 p->rapl_dram_perf_status = msr;
2227 if (do_rapl & RAPL_AMD_F17H) {
2228 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr))
2230 p->energy_pkg = msr;
2232 if (DO_BIC(BIC_PkgTmp)) {
2233 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2235 p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F);
2238 if (DO_BIC(BIC_GFX_rc6))
2239 p->gfx_rc6_ms = gfx_cur_rc6_ms;
2241 if (DO_BIC(BIC_GFXMHz))
2242 p->gfx_mhz = gfx_cur_mhz;
2244 if (DO_BIC(BIC_GFXACTMHz))
2245 p->gfx_act_mhz = gfx_act_mhz;
2247 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2248 if (get_mp(cpu, mp, &p->counter[i]))
2252 gettimeofday(&t->tv_end, (struct timezone *)NULL);
2258 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2259 * If you change the values, note they are used both in comparisons
2260 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2263 #define PCLUKN 0 /* Unknown */
2264 #define PCLRSV 1 /* Reserved */
2265 #define PCL__0 2 /* PC0 */
2266 #define PCL__1 3 /* PC1 */
2267 #define PCL__2 4 /* PC2 */
2268 #define PCL__3 5 /* PC3 */
2269 #define PCL__4 6 /* PC4 */
2270 #define PCL__6 7 /* PC6 */
2271 #define PCL_6N 8 /* PC6 No Retention */
2272 #define PCL_6R 9 /* PC6 Retention */
2273 #define PCL__7 10 /* PC7 */
2274 #define PCL_7S 11 /* PC7 Shrink */
2275 #define PCL__8 12 /* PC8 */
2276 #define PCL__9 13 /* PC9 */
2277 #define PCL_10 14 /* PC10 */
2278 #define PCLUNL 15 /* Unlimited */
2280 int pkg_cstate_limit = PCLUKN;
2281 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2282 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"
2285 int nhm_pkg_cstate_limits[16] =
2286 { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2290 int snb_pkg_cstate_limits[16] =
2291 { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2295 int hsw_pkg_cstate_limits[16] =
2296 { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2300 int slv_pkg_cstate_limits[16] =
2301 { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2305 int amt_pkg_cstate_limits[16] =
2306 { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2310 int phi_pkg_cstate_limits[16] =
2311 { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2315 int glm_pkg_cstate_limits[16] =
2316 { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2320 int skx_pkg_cstate_limits[16] =
2321 { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2325 int icx_pkg_cstate_limits[16] =
2326 { PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2330 static void calculate_tsc_tweak()
2332 tsc_tweak = base_hz / tsc_hz;
2335 void prewake_cstate_probe(unsigned int family, unsigned int model);
2337 static void dump_nhm_platform_info(void)
2339 unsigned long long msr;
2342 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2344 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
2346 ratio = (msr >> 40) & 0xFF;
2347 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk);
2349 ratio = (msr >> 8) & 0xFF;
2350 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
2352 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2353 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2354 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
2356 /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */
2357 if (dis_cstate_prewake)
2358 fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN");
2363 static void dump_hsw_turbo_ratio_limits(void)
2365 unsigned long long msr;
2368 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2370 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2372 ratio = (msr >> 8) & 0xFF;
2374 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk);
2376 ratio = (msr >> 0) & 0xFF;
2378 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk);
2382 static void dump_ivt_turbo_ratio_limits(void)
2384 unsigned long long msr;
2387 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
2389 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
2391 ratio = (msr >> 56) & 0xFF;
2393 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk);
2395 ratio = (msr >> 48) & 0xFF;
2397 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk);
2399 ratio = (msr >> 40) & 0xFF;
2401 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk);
2403 ratio = (msr >> 32) & 0xFF;
2405 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk);
2407 ratio = (msr >> 24) & 0xFF;
2409 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk);
2411 ratio = (msr >> 16) & 0xFF;
2413 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk);
2415 ratio = (msr >> 8) & 0xFF;
2417 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk);
2419 ratio = (msr >> 0) & 0xFF;
2421 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk);
2425 int has_turbo_ratio_group_limits(int family, int model)
2432 case INTEL_FAM6_ATOM_GOLDMONT:
2433 case INTEL_FAM6_SKYLAKE_X:
2434 case INTEL_FAM6_ICELAKE_X:
2435 case INTEL_FAM6_ATOM_GOLDMONT_D:
2436 case INTEL_FAM6_ATOM_TREMONT_D:
2442 static void dump_turbo_ratio_limits(int family, int model)
2444 unsigned long long msr, core_counts;
2445 unsigned int ratio, group_size;
2447 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2448 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2450 if (has_turbo_ratio_group_limits(family, model)) {
2451 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2452 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2454 core_counts = 0x0807060504030201;
2457 ratio = (msr >> 56) & 0xFF;
2458 group_size = (core_counts >> 56) & 0xFF;
2460 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2461 ratio, bclk, ratio * bclk, group_size);
2463 ratio = (msr >> 48) & 0xFF;
2464 group_size = (core_counts >> 48) & 0xFF;
2466 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2467 ratio, bclk, ratio * bclk, group_size);
2469 ratio = (msr >> 40) & 0xFF;
2470 group_size = (core_counts >> 40) & 0xFF;
2472 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2473 ratio, bclk, ratio * bclk, group_size);
2475 ratio = (msr >> 32) & 0xFF;
2476 group_size = (core_counts >> 32) & 0xFF;
2478 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2479 ratio, bclk, ratio * bclk, group_size);
2481 ratio = (msr >> 24) & 0xFF;
2482 group_size = (core_counts >> 24) & 0xFF;
2484 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2485 ratio, bclk, ratio * bclk, group_size);
2487 ratio = (msr >> 16) & 0xFF;
2488 group_size = (core_counts >> 16) & 0xFF;
2490 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2491 ratio, bclk, ratio * bclk, group_size);
2493 ratio = (msr >> 8) & 0xFF;
2494 group_size = (core_counts >> 8) & 0xFF;
2496 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2497 ratio, bclk, ratio * bclk, group_size);
2499 ratio = (msr >> 0) & 0xFF;
2500 group_size = (core_counts >> 0) & 0xFF;
2502 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2503 ratio, bclk, ratio * bclk, group_size);
2507 static void dump_atom_turbo_ratio_limits(void)
2509 unsigned long long msr;
2512 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2513 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2515 ratio = (msr >> 0) & 0x3F;
2517 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk);
2519 ratio = (msr >> 8) & 0x3F;
2521 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk);
2523 ratio = (msr >> 16) & 0x3F;
2525 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
2527 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2528 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2530 ratio = (msr >> 24) & 0x3F;
2532 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk);
2534 ratio = (msr >> 16) & 0x3F;
2536 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk);
2538 ratio = (msr >> 8) & 0x3F;
2540 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk);
2542 ratio = (msr >> 0) & 0x3F;
2544 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk);
2547 static void dump_knl_turbo_ratio_limits(void)
2549 const unsigned int buckets_no = 7;
2551 unsigned long long msr;
2552 int delta_cores, delta_ratio;
2554 unsigned int cores[buckets_no];
2555 unsigned int ratio[buckets_no];
2557 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2559 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2562 * Turbo encoding in KNL is as follows:
2564 * [7:1] -- Base value of number of active cores of bucket 1.
2565 * [15:8] -- Base value of freq ratio of bucket 1.
2566 * [20:16] -- +ve delta of number of active cores of bucket 2.
2567 * i.e. active cores of bucket 2 =
2568 * active cores of bucket 1 + delta
2569 * [23:21] -- Negative delta of freq ratio of bucket 2.
2570 * i.e. freq ratio of bucket 2 =
2571 * freq ratio of bucket 1 - delta
2572 * [28:24]-- +ve delta of number of active cores of bucket 3.
2573 * [31:29]-- -ve delta of freq ratio of bucket 3.
2574 * [36:32]-- +ve delta of number of active cores of bucket 4.
2575 * [39:37]-- -ve delta of freq ratio of bucket 4.
2576 * [44:40]-- +ve delta of number of active cores of bucket 5.
2577 * [47:45]-- -ve delta of freq ratio of bucket 5.
2578 * [52:48]-- +ve delta of number of active cores of bucket 6.
2579 * [55:53]-- -ve delta of freq ratio of bucket 6.
2580 * [60:56]-- +ve delta of number of active cores of bucket 7.
2581 * [63:61]-- -ve delta of freq ratio of bucket 7.
2585 cores[b_nr] = (msr & 0xFF) >> 1;
2586 ratio[b_nr] = (msr >> 8) & 0xFF;
2588 for (i = 16; i < 64; i += 8) {
2589 delta_cores = (msr >> i) & 0x1F;
2590 delta_ratio = (msr >> (i + 5)) & 0x7;
2592 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2593 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2597 for (i = buckets_no - 1; i >= 0; i--)
2598 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2600 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2601 ratio[i], bclk, ratio[i] * bclk, cores[i]);
2604 static void dump_nhm_cst_cfg(void)
2606 unsigned long long msr;
2608 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2610 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2612 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2613 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2614 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2615 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2616 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2617 (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]);
2619 #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2620 if (has_automatic_cstate_conversion) {
2621 fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2624 fprintf(outf, ")\n");
2629 static void dump_config_tdp(void)
2631 unsigned long long msr;
2633 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2634 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2635 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
2637 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2638 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
2640 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2641 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2642 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2643 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
2645 fprintf(outf, ")\n");
2647 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2648 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
2650 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2651 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2652 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2653 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
2655 fprintf(outf, ")\n");
2657 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2658 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
2660 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2661 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2662 fprintf(outf, ")\n");
2664 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2665 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2666 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2667 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2668 fprintf(outf, ")\n");
2671 unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2673 void print_irtl(void)
2675 unsigned long long msr;
2677 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2678 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2679 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2680 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2682 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2683 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2684 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2685 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2687 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2688 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2689 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2690 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2695 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2696 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2697 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2698 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2700 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2701 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2702 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2703 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2705 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2706 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2707 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2708 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2712 void free_fd_percpu(void)
2716 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
2717 if (fd_percpu[i] != 0)
2718 close(fd_percpu[i]);
2724 void free_all_buffers(void)
2728 CPU_FREE(cpu_present_set);
2729 cpu_present_set = NULL;
2730 cpu_present_setsize = 0;
2732 CPU_FREE(cpu_affinity_set);
2733 cpu_affinity_set = NULL;
2734 cpu_affinity_setsize = 0;
2742 package_even = NULL;
2752 free(output_buffer);
2753 output_buffer = NULL;
2758 free(irq_column_2_cpu);
2761 for (i = 0; i <= topo.max_cpu_num; ++i) {
2762 if (cpus[i].put_ids)
2763 CPU_FREE(cpus[i].put_ids);
2769 * Parse a file containing a single int.
2770 * Return 0 if file can not be opened
2771 * Exit if file can be opened, but can not be parsed
2773 int parse_int_file(const char *fmt, ...)
2776 char path[PATH_MAX];
2780 va_start(args, fmt);
2781 vsnprintf(path, sizeof(path), fmt, args);
2783 filep = fopen(path, "r");
2786 if (fscanf(filep, "%d", &value) != 1)
2787 err(1, "%s: failed to parse number from file", path);
2793 * cpu_is_first_core_in_package(cpu)
2794 * return 1 if given CPU is 1st core in package
2796 int cpu_is_first_core_in_package(int cpu)
2798 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2801 int get_physical_package_id(int cpu)
2803 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2806 int get_die_id(int cpu)
2808 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
2811 int get_core_id(int cpu)
2813 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2816 void set_node_data(void)
2818 int pkg, node, lnode, cpu, cpux;
2821 /* initialize logical_node_id */
2822 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
2823 cpus[cpu].logical_node_id = -1;
2826 for (pkg = 0; pkg < topo.num_packages; pkg++) {
2828 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
2829 if (cpus[cpu].physical_package_id != pkg)
2831 /* find a cpu with an unset logical_node_id */
2832 if (cpus[cpu].logical_node_id != -1)
2834 cpus[cpu].logical_node_id = lnode;
2835 node = cpus[cpu].physical_node_id;
2838 * find all matching cpus on this pkg and set
2839 * the logical_node_id
2841 for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
2842 if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) {
2843 cpus[cpux].logical_node_id = lnode;
2848 if (lnode > topo.nodes_per_pkg)
2849 topo.nodes_per_pkg = lnode;
2851 if (cpu_count >= topo.max_cpu_num)
2856 int get_physical_node_id(struct cpu_topology *thiscpu)
2861 int cpu = thiscpu->logical_cpu_id;
2863 for (i = 0; i <= topo.max_cpu_num; i++) {
2864 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i);
2865 filep = fopen(path, "r");
2874 int get_thread_siblings(struct cpu_topology *thiscpu)
2876 char path[80], character;
2879 int so, shift, sib_core;
2880 int cpu = thiscpu->logical_cpu_id;
2881 int offset = topo.max_cpu_num + 1;
2885 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
2886 if (thiscpu->thread_id < 0)
2887 thiscpu->thread_id = thread_id++;
2888 if (!thiscpu->put_ids)
2891 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2892 CPU_ZERO_S(size, thiscpu->put_ids);
2894 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
2895 filep = fopen(path, "r");
2898 warnx("%s: open failed", path);
2902 offset -= BITMASK_SIZE;
2903 if (fscanf(filep, "%lx%c", &map, &character) != 2)
2904 err(1, "%s: failed to parse file", path);
2905 for (shift = 0; shift < BITMASK_SIZE; shift++) {
2906 if ((map >> shift) & 0x1) {
2907 so = shift + offset;
2908 sib_core = get_core_id(so);
2909 if (sib_core == thiscpu->physical_core_id) {
2910 CPU_SET_S(so, size, thiscpu->put_ids);
2911 if ((so != cpu) && (cpus[so].thread_id < 0))
2912 cpus[so].thread_id = thread_id++;
2916 } while (!strncmp(&character, ",", 1));
2919 return CPU_COUNT_S(size, thiscpu->put_ids);
2923 * run func(thread, core, package) in topology order
2924 * skip non-present cpus
2927 int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *,
2928 struct pkg_data *, struct thread_data *, struct core_data *,
2929 struct pkg_data *), struct thread_data *thread_base,
2930 struct core_data *core_base, struct pkg_data *pkg_base,
2931 struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2)
2933 int retval, pkg_no, node_no, core_no, thread_no;
2935 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2936 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
2937 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
2938 for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
2939 struct thread_data *t, *t2;
2940 struct core_data *c, *c2;
2941 struct pkg_data *p, *p2;
2943 t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
2945 if (cpu_is_not_present(t->cpu_id))
2948 t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no);
2950 c = GET_CORE(core_base, core_no, node_no, pkg_no);
2951 c2 = GET_CORE(core_base2, core_no, node_no, pkg_no);
2953 p = GET_PKG(pkg_base, pkg_no);
2954 p2 = GET_PKG(pkg_base2, pkg_no);
2956 retval = func(t, c, p, t2, c2, p2);
2967 * run func(cpu) on every cpu in /proc/stat
2968 * return max_cpu number
2970 int for_all_proc_cpus(int (func) (int))
2976 fp = fopen_or_die(proc_stat, "r");
2978 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2980 err(1, "%s: failed to parse format", proc_stat);
2983 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2987 retval = func(cpu_num);
2997 void re_initialize(void)
3000 setup_all_buffers();
3001 fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
3004 void set_max_cpu_num(void)
3008 unsigned long dummy;
3011 base_cpu = sched_getcpu();
3013 err(1, "cannot find calling cpu ID");
3014 sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu);
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)
3034 int mark_cpu_present(int cpu)
3036 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
3040 int init_thread_id(int cpu)
3042 cpus[cpu].thread_id = -1;
3047 * snapshot_proc_interrupts()
3049 * read and record summary of /proc/interrupts
3051 * return 1 if config change requires a restart, else return 0
3053 int snapshot_proc_interrupts(void)
3059 fp = fopen_or_die("/proc/interrupts", "r");
3063 /* read 1st line of /proc/interrupts to get cpu* name for each column */
3064 for (column = 0; column < topo.num_cpus; ++column) {
3067 retval = fscanf(fp, " CPU%d", &cpu_number);
3071 if (cpu_number > topo.max_cpu_num) {
3072 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
3076 irq_column_2_cpu[column] = cpu_number;
3077 irqs_per_cpu[cpu_number] = 0;
3080 /* read /proc/interrupt count lines and sum up irqs per cpu */
3085 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
3089 /* read the count per cpu */
3090 for (column = 0; column < topo.num_cpus; ++column) {
3092 int cpu_number, irq_count;
3094 retval = fscanf(fp, " %d", &irq_count);
3098 cpu_number = irq_column_2_cpu[column];
3099 irqs_per_cpu[cpu_number] += irq_count;
3103 while (getc(fp) != '\n') ; /* flush interrupt description */
3110 * snapshot_gfx_rc6_ms()
3112 * record snapshot of
3113 * /sys/class/drm/card0/power/rc6_residency_ms
3115 * return 1 if config change requires a restart, else return 0
3117 int snapshot_gfx_rc6_ms(void)
3122 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
3124 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
3134 * snapshot_gfx_mhz()
3136 * record snapshot of
3137 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
3139 * return 1 if config change requires a restart, else return 0
3141 int snapshot_gfx_mhz(void)
3147 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
3153 retval = fscanf(fp, "%d", &gfx_cur_mhz);
3161 * snapshot_gfx_cur_mhz()
3163 * record snapshot of
3164 * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz
3166 * return 1 if config change requires a restart, else return 0
3168 int snapshot_gfx_act_mhz(void)
3174 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r");
3180 retval = fscanf(fp, "%d", &gfx_act_mhz);
3182 err(1, "GFX ACT MHz");
3188 * snapshot_cpu_lpi()
3190 * record snapshot of
3191 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
3193 int snapshot_cpu_lpi_us(void)
3198 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
3200 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
3202 fprintf(stderr, "Disabling Low Power Idle CPU output\n");
3203 BIC_NOT_PRESENT(BIC_CPU_LPI);
3214 * snapshot_sys_lpi()
3216 * record snapshot of sys_lpi_file
3218 int snapshot_sys_lpi_us(void)
3223 fp = fopen_or_die(sys_lpi_file, "r");
3225 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
3227 fprintf(stderr, "Disabling Low Power Idle System output\n");
3228 BIC_NOT_PRESENT(BIC_SYS_LPI);
3238 * snapshot /proc and /sys files
3240 * return 1 if configuration restart needed, else return 0
3242 int snapshot_proc_sysfs_files(void)
3244 if (DO_BIC(BIC_IRQ))
3245 if (snapshot_proc_interrupts())
3248 if (DO_BIC(BIC_GFX_rc6))
3249 snapshot_gfx_rc6_ms();
3251 if (DO_BIC(BIC_GFXMHz))
3254 if (DO_BIC(BIC_GFXACTMHz))
3255 snapshot_gfx_act_mhz();
3257 if (DO_BIC(BIC_CPU_LPI))
3258 snapshot_cpu_lpi_us();
3260 if (DO_BIC(BIC_SYS_LPI))
3261 snapshot_sys_lpi_us();
3268 static void signal_handler(int signal)
3274 fprintf(stderr, " SIGINT\n");
3278 fprintf(stderr, "SIGUSR1\n");
3283 void setup_signal_handler(void)
3285 struct sigaction sa;
3287 memset(&sa, 0, sizeof(sa));
3289 sa.sa_handler = &signal_handler;
3291 if (sigaction(SIGINT, &sa, NULL) < 0)
3292 err(1, "sigaction SIGINT");
3293 if (sigaction(SIGUSR1, &sa, NULL) < 0)
3294 err(1, "sigaction SIGUSR1");
3299 struct timeval tout;
3300 struct timespec rest;
3305 FD_SET(0, &readfds);
3308 nanosleep(&interval_ts, NULL);
3313 retval = select(1, &readfds, NULL, NULL, &tout);
3316 switch (getc(stdin)) {
3322 * 'stdin' is a pipe closed on the other end. There
3323 * won't be any further input.
3326 /* Sleep the rest of the time */
3327 rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3328 rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3329 nanosleep(&rest, NULL);
3334 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr)
3337 unsigned long long msr_cur, msr_last;
3339 if (!per_cpu_msr_sum)
3342 idx = offset_to_idx(offset);
3345 /* get_msr_sum() = sum + (get_msr() - last) */
3346 ret = get_msr(cpu, offset, &msr_cur);
3349 msr_last = per_cpu_msr_sum[cpu].entries[idx].last;
3350 DELTA_WRAP32(msr_cur, msr_last);
3351 *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum;
3358 /* Timer callback, update the sum of MSRs periodically. */
3359 static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3362 int cpu = t->cpu_id;
3364 for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
3365 unsigned long long msr_cur, msr_last;
3370 offset = idx_to_offset(i);
3373 ret = get_msr(cpu, offset, &msr_cur);
3375 fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset);
3379 msr_last = per_cpu_msr_sum[cpu].entries[i].last;
3380 per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff;
3382 DELTA_WRAP32(msr_cur, msr_last);
3383 per_cpu_msr_sum[cpu].entries[i].sum += msr_last;
3388 static void msr_record_handler(union sigval v)
3390 for_all_cpus(update_msr_sum, EVEN_COUNTERS);
3393 void msr_sum_record(void)
3395 struct itimerspec its;
3396 struct sigevent sev;
3398 per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array));
3399 if (!per_cpu_msr_sum) {
3400 fprintf(outf, "Can not allocate memory for long time MSR.\n");
3404 * Signal handler might be restricted, so use thread notifier instead.
3406 memset(&sev, 0, sizeof(struct sigevent));
3407 sev.sigev_notify = SIGEV_THREAD;
3408 sev.sigev_notify_function = msr_record_handler;
3410 sev.sigev_value.sival_ptr = &timerid;
3411 if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
3412 fprintf(outf, "Can not create timer.\n");
3416 its.it_value.tv_sec = 0;
3417 its.it_value.tv_nsec = 1;
3419 * A wraparound time has been calculated early.
3420 * Some sources state that the peak power for a
3421 * microprocessor is usually 1.5 times the TDP rating,
3422 * use 2 * TDP for safety.
3424 its.it_interval.tv_sec = rapl_joule_counter_range / 2;
3425 its.it_interval.tv_nsec = 0;
3427 if (timer_settime(timerid, 0, &its, NULL) == -1) {
3428 fprintf(outf, "Can not set timer.\n");
3434 timer_delete(timerid);
3436 free(per_cpu_msr_sum);
3440 * set_my_sched_priority(pri)
3443 int set_my_sched_priority(int priority)
3446 int original_priority;
3449 original_priority = getpriority(PRIO_PROCESS, 0);
3450 if (errno && (original_priority == -1))
3451 err(errno, "getpriority");
3453 retval = setpriority(PRIO_PROCESS, 0, priority);
3455 err(retval, "setpriority(%d)", priority);
3458 retval = getpriority(PRIO_PROCESS, 0);
3459 if (retval != priority)
3460 err(-1, "getpriority(%d) != setpriority(%d)", retval, priority);
3462 return original_priority;
3465 void turbostat_loop()
3471 setup_signal_handler();
3474 * elevate own priority for interval mode
3476 set_my_sched_priority(-20);
3481 snapshot_proc_sysfs_files();
3482 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3483 first_counter_read = 0;
3486 } else if (retval == -1) {
3487 if (restarted > 10) {
3495 gettimeofday(&tv_even, (struct timezone *)NULL);
3498 if (for_all_proc_cpus(cpu_is_not_present)) {
3503 if (snapshot_proc_sysfs_files())
3505 retval = for_all_cpus(get_counters, ODD_COUNTERS);
3508 } else if (retval == -1) {
3512 gettimeofday(&tv_odd, (struct timezone *)NULL);
3513 timersub(&tv_odd, &tv_even, &tv_delta);
3514 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3518 compute_average(EVEN_COUNTERS);
3519 format_all_counters(EVEN_COUNTERS);
3520 flush_output_stdout();
3523 if (num_iterations && ++done_iters >= num_iterations)
3526 if (snapshot_proc_sysfs_files())
3528 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3531 } else if (retval == -1) {
3535 gettimeofday(&tv_even, (struct timezone *)NULL);
3536 timersub(&tv_even, &tv_odd, &tv_delta);
3537 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3541 compute_average(ODD_COUNTERS);
3542 format_all_counters(ODD_COUNTERS);
3543 flush_output_stdout();
3546 if (num_iterations && ++done_iters >= num_iterations)
3551 void check_dev_msr()
3556 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3557 if (stat(pathname, &sb))
3558 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3559 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3563 * check for CAP_SYS_RAWIO
3564 * return 0 on success
3567 int check_for_cap_sys_rawio(void)
3570 cap_flag_value_t cap_flag_value;
3572 caps = cap_get_proc();
3574 err(-6, "cap_get_proc\n");
3576 if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3577 err(-6, "cap_get\n");
3579 if (cap_flag_value != CAP_SET) {
3580 warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3584 if (cap_free(caps) == -1)
3585 err(-6, "cap_free\n");
3590 void check_permissions(void)
3595 /* check for CAP_SYS_RAWIO */
3596 do_exit += check_for_cap_sys_rawio();
3598 /* test file permissions */
3599 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3600 if (euidaccess(pathname, R_OK)) {
3602 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
3605 /* if all else fails, thell them to be root */
3608 warnx("... or simply run as root");
3615 * NHM adds support for additional MSRs:
3617 * MSR_SMI_COUNT 0x00000034
3619 * MSR_PLATFORM_INFO 0x000000ce
3620 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
3622 * MSR_MISC_PWR_MGMT 0x000001aa
3624 * MSR_PKG_C3_RESIDENCY 0x000003f8
3625 * MSR_PKG_C6_RESIDENCY 0x000003f9
3626 * MSR_CORE_C3_RESIDENCY 0x000003fc
3627 * MSR_CORE_C6_RESIDENCY 0x000003fd
3630 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
3631 * sets has_misc_feature_control
3633 int probe_nhm_msrs(unsigned int family, unsigned int model)
3635 unsigned long long msr;
3636 unsigned int base_ratio;
3637 int *pkg_cstate_limits;
3645 bclk = discover_bclk(family, model);
3648 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3649 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3650 pkg_cstate_limits = nhm_pkg_cstate_limits;
3652 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3653 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3654 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3655 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3656 pkg_cstate_limits = snb_pkg_cstate_limits;
3657 has_misc_feature_control = 1;
3659 case INTEL_FAM6_HASWELL: /* HSW */
3660 case INTEL_FAM6_HASWELL_G: /* HSW */
3661 case INTEL_FAM6_HASWELL_X: /* HSX */
3662 case INTEL_FAM6_HASWELL_L: /* HSW */
3663 case INTEL_FAM6_BROADWELL: /* BDW */
3664 case INTEL_FAM6_BROADWELL_G: /* BDW */
3665 case INTEL_FAM6_BROADWELL_X: /* BDX */
3666 case INTEL_FAM6_SKYLAKE_L: /* SKL */
3667 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3668 pkg_cstate_limits = hsw_pkg_cstate_limits;
3669 has_misc_feature_control = 1;
3671 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3672 pkg_cstate_limits = skx_pkg_cstate_limits;
3673 has_misc_feature_control = 1;
3675 case INTEL_FAM6_ICELAKE_X: /* ICX */
3676 pkg_cstate_limits = icx_pkg_cstate_limits;
3677 has_misc_feature_control = 1;
3679 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
3680 no_MSR_MISC_PWR_MGMT = 1;
3681 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
3682 pkg_cstate_limits = slv_pkg_cstate_limits;
3684 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
3685 pkg_cstate_limits = amt_pkg_cstate_limits;
3686 no_MSR_MISC_PWR_MGMT = 1;
3688 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
3689 pkg_cstate_limits = phi_pkg_cstate_limits;
3691 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3692 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3693 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
3694 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
3695 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
3696 pkg_cstate_limits = glm_pkg_cstate_limits;
3701 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3702 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3704 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
3705 base_ratio = (msr >> 8) & 0xFF;
3707 base_hz = base_ratio * bclk * 1000000;
3713 * SLV client has support for unique MSRs:
3715 * MSR_CC6_DEMOTION_POLICY_CONFIG
3716 * MSR_MC6_DEMOTION_POLICY_CONFIG
3719 int has_slv_msrs(unsigned int family, unsigned int model)
3725 case INTEL_FAM6_ATOM_SILVERMONT:
3726 case INTEL_FAM6_ATOM_SILVERMONT_MID:
3727 case INTEL_FAM6_ATOM_AIRMONT_MID:
3733 int is_dnv(unsigned int family, unsigned int model)
3740 case INTEL_FAM6_ATOM_GOLDMONT_D:
3746 int is_bdx(unsigned int family, unsigned int model)
3753 case INTEL_FAM6_BROADWELL_X:
3759 int is_skx(unsigned int family, unsigned int model)
3766 case INTEL_FAM6_SKYLAKE_X:
3772 int is_icx(unsigned int family, unsigned int model)
3779 case INTEL_FAM6_ICELAKE_X:
3785 int is_ehl(unsigned int family, unsigned int model)
3791 case INTEL_FAM6_ATOM_TREMONT:
3797 int is_jvl(unsigned int family, unsigned int model)
3803 case INTEL_FAM6_ATOM_TREMONT_D:
3809 int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3811 if (has_slv_msrs(family, model))
3815 /* Nehalem compatible, but do not include turbo-ratio limit support */
3816 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3817 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
3824 int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
3826 if (has_slv_msrs(family, model))
3832 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
3841 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3842 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3849 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3858 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3865 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3874 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3881 int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
3890 case INTEL_FAM6_ATOM_GOLDMONT:
3891 case INTEL_FAM6_SKYLAKE_X:
3892 case INTEL_FAM6_ICELAKE_X:
3899 int has_config_tdp(unsigned int family, unsigned int model)
3908 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3909 case INTEL_FAM6_HASWELL: /* HSW */
3910 case INTEL_FAM6_HASWELL_X: /* HSX */
3911 case INTEL_FAM6_HASWELL_L: /* HSW */
3912 case INTEL_FAM6_HASWELL_G: /* HSW */
3913 case INTEL_FAM6_BROADWELL: /* BDW */
3914 case INTEL_FAM6_BROADWELL_G: /* BDW */
3915 case INTEL_FAM6_BROADWELL_X: /* BDX */
3916 case INTEL_FAM6_SKYLAKE_L: /* SKL */
3917 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3918 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3919 case INTEL_FAM6_ICELAKE_X: /* ICX */
3921 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3930 * 0: Tcc Offset not supported (Default)
3931 * 6: Bit 29:24 of MSR_PLATFORM_INFO
3932 * 4: Bit 27:24 of MSR_PLATFORM_INFO
3934 void check_tcc_offset(int model)
3936 unsigned long long msr;
3942 case INTEL_FAM6_SKYLAKE_L:
3943 case INTEL_FAM6_SKYLAKE:
3944 case INTEL_FAM6_KABYLAKE_L:
3945 case INTEL_FAM6_KABYLAKE:
3946 case INTEL_FAM6_ICELAKE_L:
3947 case INTEL_FAM6_ICELAKE:
3948 case INTEL_FAM6_TIGERLAKE_L:
3949 case INTEL_FAM6_TIGERLAKE:
3950 case INTEL_FAM6_COMETLAKE:
3951 if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) {
3952 msr = (msr >> 30) & 1;
3954 tcc_offset_bits = 6;
3962 static void remove_underbar(char *s)
3975 static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3977 if (!do_nhm_platform_info)
3980 dump_nhm_platform_info();
3982 if (has_hsw_turbo_ratio_limit(family, model))
3983 dump_hsw_turbo_ratio_limits();
3985 if (has_ivt_turbo_ratio_limit(family, model))
3986 dump_ivt_turbo_ratio_limits();
3988 if (has_turbo_ratio_limit(family, model))
3989 dump_turbo_ratio_limits(family, model);
3991 if (has_atom_turbo_ratio_limit(family, model))
3992 dump_atom_turbo_ratio_limits();
3994 if (has_knl_turbo_ratio_limit(family, model))
3995 dump_knl_turbo_ratio_limits();
3997 if (has_config_tdp(family, model))
4003 static void dump_sysfs_file(char *path)
4006 char cpuidle_buf[64];
4008 input = fopen(path, "r");
4009 if (input == NULL) {
4011 fprintf(outf, "NSFOD %s\n", path);
4014 if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
4015 err(1, "%s: failed to read file", path);
4018 fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
4021 static void dump_sysfs_cstate_config(void)
4030 if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
4031 fprintf(outf, "cpuidle not loaded\n");
4035 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
4036 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
4037 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
4039 for (state = 0; state < 10; ++state) {
4041 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
4042 input = fopen(path, "r");
4045 if (!fgets(name_buf, sizeof(name_buf), input))
4046 err(1, "%s: failed to read file", path);
4048 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4049 sp = strchr(name_buf, '-');
4051 sp = strchrnul(name_buf, '\n');
4055 remove_underbar(name_buf);
4057 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state);
4058 input = fopen(path, "r");
4061 if (!fgets(desc, sizeof(desc), input))
4062 err(1, "%s: failed to read file", path);
4064 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
4069 static void dump_sysfs_pstate_config(void)
4072 char driver_buf[64];
4073 char governor_buf[64];
4077 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu);
4078 input = fopen(path, "r");
4079 if (input == NULL) {
4080 fprintf(outf, "NSFOD %s\n", path);
4083 if (!fgets(driver_buf, sizeof(driver_buf), input))
4084 err(1, "%s: failed to read file", path);
4087 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu);
4088 input = fopen(path, "r");
4089 if (input == NULL) {
4090 fprintf(outf, "NSFOD %s\n", path);
4093 if (!fgets(governor_buf, sizeof(governor_buf), input))
4094 err(1, "%s: failed to read file", path);
4097 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
4098 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
4100 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
4101 input = fopen(path, "r");
4102 if (input != NULL) {
4103 if (fscanf(input, "%d", &turbo) != 1)
4104 err(1, "%s: failed to parse number from file", path);
4105 fprintf(outf, "cpufreq boost: %d\n", turbo);
4109 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
4110 input = fopen(path, "r");
4111 if (input != NULL) {
4112 if (fscanf(input, "%d", &turbo) != 1)
4113 err(1, "%s: failed to parse number from file", path);
4114 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
4121 * Decode the ENERGY_PERF_BIAS MSR
4123 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4133 /* EPB is per-package */
4134 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4137 if (cpu_migrate(cpu)) {
4138 fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu);
4147 case ENERGY_PERF_BIAS_PERFORMANCE:
4148 epb_string = "performance";
4150 case ENERGY_PERF_BIAS_NORMAL:
4151 epb_string = "balanced";
4153 case ENERGY_PERF_BIAS_POWERSAVE:
4154 epb_string = "powersave";
4157 epb_string = "custom";
4160 fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string);
4167 * Decode the MSR_HWP_CAPABILITIES
4169 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4171 unsigned long long msr;
4179 /* MSR_HWP_CAPABILITIES is per-package */
4180 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4183 if (cpu_migrate(cpu)) {
4184 fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu);
4188 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
4191 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-");
4193 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
4194 if ((msr & (1 << 0)) == 0)
4197 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
4200 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
4201 "(high %d guar %d eff %d low %d)\n",
4203 (unsigned int)HWP_HIGHEST_PERF(msr),
4204 (unsigned int)HWP_GUARANTEED_PERF(msr),
4205 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr));
4207 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
4210 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
4211 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
4213 (unsigned int)(((msr) >> 0) & 0xff),
4214 (unsigned int)(((msr) >> 8) & 0xff),
4215 (unsigned int)(((msr) >> 16) & 0xff),
4216 (unsigned int)(((msr) >> 24) & 0xff),
4217 (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1));
4220 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
4223 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
4224 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
4226 (unsigned int)(((msr) >> 0) & 0xff),
4227 (unsigned int)(((msr) >> 8) & 0xff),
4228 (unsigned int)(((msr) >> 16) & 0xff),
4229 (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3));
4231 if (has_hwp_notify) {
4232 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
4235 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
4236 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
4237 cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis");
4239 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
4242 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
4243 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
4244 cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-");
4250 * print_perf_limit()
4252 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4254 unsigned long long msr;
4260 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4263 if (cpu_migrate(cpu)) {
4264 fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu);
4268 if (do_core_perf_limit_reasons) {
4269 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
4270 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4271 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
4272 (msr & 1 << 15) ? "bit15, " : "",
4273 (msr & 1 << 14) ? "bit14, " : "",
4274 (msr & 1 << 13) ? "Transitions, " : "",
4275 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
4276 (msr & 1 << 11) ? "PkgPwrL2, " : "",
4277 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4278 (msr & 1 << 9) ? "CorePwr, " : "",
4279 (msr & 1 << 8) ? "Amps, " : "",
4280 (msr & 1 << 6) ? "VR-Therm, " : "",
4281 (msr & 1 << 5) ? "Auto-HWP, " : "",
4282 (msr & 1 << 4) ? "Graphics, " : "",
4283 (msr & 1 << 2) ? "bit2, " : "",
4284 (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : "");
4285 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
4286 (msr & 1 << 31) ? "bit31, " : "",
4287 (msr & 1 << 30) ? "bit30, " : "",
4288 (msr & 1 << 29) ? "Transitions, " : "",
4289 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
4290 (msr & 1 << 27) ? "PkgPwrL2, " : "",
4291 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4292 (msr & 1 << 25) ? "CorePwr, " : "",
4293 (msr & 1 << 24) ? "Amps, " : "",
4294 (msr & 1 << 22) ? "VR-Therm, " : "",
4295 (msr & 1 << 21) ? "Auto-HWP, " : "",
4296 (msr & 1 << 20) ? "Graphics, " : "",
4297 (msr & 1 << 18) ? "bit18, " : "",
4298 (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : "");
4301 if (do_gfx_perf_limit_reasons) {
4302 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
4303 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4304 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
4305 (msr & 1 << 0) ? "PROCHOT, " : "",
4306 (msr & 1 << 1) ? "ThermStatus, " : "",
4307 (msr & 1 << 4) ? "Graphics, " : "",
4308 (msr & 1 << 6) ? "VR-Therm, " : "",
4309 (msr & 1 << 8) ? "Amps, " : "",
4310 (msr & 1 << 9) ? "GFXPwr, " : "",
4311 (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4312 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
4313 (msr & 1 << 16) ? "PROCHOT, " : "",
4314 (msr & 1 << 17) ? "ThermStatus, " : "",
4315 (msr & 1 << 20) ? "Graphics, " : "",
4316 (msr & 1 << 22) ? "VR-Therm, " : "",
4317 (msr & 1 << 24) ? "Amps, " : "",
4318 (msr & 1 << 25) ? "GFXPwr, " : "",
4319 (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
4321 if (do_ring_perf_limit_reasons) {
4322 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
4323 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4324 fprintf(outf, " (Active: %s%s%s%s%s%s)",
4325 (msr & 1 << 0) ? "PROCHOT, " : "",
4326 (msr & 1 << 1) ? "ThermStatus, " : "",
4327 (msr & 1 << 6) ? "VR-Therm, " : "",
4328 (msr & 1 << 8) ? "Amps, " : "",
4329 (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4330 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
4331 (msr & 1 << 16) ? "PROCHOT, " : "",
4332 (msr & 1 << 17) ? "ThermStatus, " : "",
4333 (msr & 1 << 22) ? "VR-Therm, " : "",
4334 (msr & 1 << 24) ? "Amps, " : "",
4335 (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
4340 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
4341 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
4343 double get_tdp_intel(unsigned int model)
4345 unsigned long long msr;
4347 if (do_rapl & RAPL_PKG_POWER_INFO)
4348 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
4349 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
4352 case INTEL_FAM6_ATOM_SILVERMONT:
4353 case INTEL_FAM6_ATOM_SILVERMONT_D:
4360 double get_tdp_amd(unsigned int family)
4362 /* This is the max stock TDP of HEDT/Server Fam17h+ chips */
4367 * rapl_dram_energy_units_probe()
4368 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
4370 static double rapl_dram_energy_units_probe(int model, double rapl_energy_units)
4372 /* only called for genuine_intel, family 6 */
4375 case INTEL_FAM6_HASWELL_X: /* HSX */
4376 case INTEL_FAM6_BROADWELL_X: /* BDX */
4377 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4378 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4379 return (rapl_dram_energy_units = 15.3 / 1000000);
4381 return (rapl_energy_units);
4385 void rapl_probe_intel(unsigned int family, unsigned int model)
4387 unsigned long long msr;
4388 unsigned int time_unit;
4395 case INTEL_FAM6_SANDYBRIDGE:
4396 case INTEL_FAM6_IVYBRIDGE:
4397 case INTEL_FAM6_HASWELL: /* HSW */
4398 case INTEL_FAM6_HASWELL_L: /* HSW */
4399 case INTEL_FAM6_HASWELL_G: /* HSW */
4400 case INTEL_FAM6_BROADWELL: /* BDW */
4401 case INTEL_FAM6_BROADWELL_G: /* BDW */
4402 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
4404 BIC_PRESENT(BIC_Pkg_J);
4405 BIC_PRESENT(BIC_Cor_J);
4406 BIC_PRESENT(BIC_GFX_J);
4408 BIC_PRESENT(BIC_PkgWatt);
4409 BIC_PRESENT(BIC_CorWatt);
4410 BIC_PRESENT(BIC_GFXWatt);
4413 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4414 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4415 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
4417 BIC_PRESENT(BIC_Pkg_J);
4419 BIC_PRESENT(BIC_PkgWatt);
4421 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4423 RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
4424 | RAPL_GFX | RAPL_PKG_POWER_INFO;
4426 BIC_PRESENT(BIC_Pkg_J);
4427 BIC_PRESENT(BIC_Cor_J);
4428 BIC_PRESENT(BIC_RAM_J);
4429 BIC_PRESENT(BIC_GFX_J);
4431 BIC_PRESENT(BIC_PkgWatt);
4432 BIC_PRESENT(BIC_CorWatt);
4433 BIC_PRESENT(BIC_RAMWatt);
4434 BIC_PRESENT(BIC_GFXWatt);
4437 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
4438 do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
4439 BIC_PRESENT(BIC_PKG__);
4441 BIC_PRESENT(BIC_Pkg_J);
4443 BIC_PRESENT(BIC_PkgWatt);
4445 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4446 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4448 RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
4449 | RAPL_GFX | RAPL_PKG_POWER_INFO;
4450 BIC_PRESENT(BIC_PKG__);
4451 BIC_PRESENT(BIC_RAM__);
4453 BIC_PRESENT(BIC_Pkg_J);
4454 BIC_PRESENT(BIC_Cor_J);
4455 BIC_PRESENT(BIC_RAM_J);
4456 BIC_PRESENT(BIC_GFX_J);
4458 BIC_PRESENT(BIC_PkgWatt);
4459 BIC_PRESENT(BIC_CorWatt);
4460 BIC_PRESENT(BIC_RAMWatt);
4461 BIC_PRESENT(BIC_GFXWatt);
4464 case INTEL_FAM6_HASWELL_X: /* HSX */
4465 case INTEL_FAM6_BROADWELL_X: /* BDX */
4466 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4467 case INTEL_FAM6_ICELAKE_X: /* ICX */
4468 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4470 RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
4471 RAPL_PKG_POWER_INFO;
4472 BIC_PRESENT(BIC_PKG__);
4473 BIC_PRESENT(BIC_RAM__);
4475 BIC_PRESENT(BIC_Pkg_J);
4476 BIC_PRESENT(BIC_RAM_J);
4478 BIC_PRESENT(BIC_PkgWatt);
4479 BIC_PRESENT(BIC_RAMWatt);
4482 case INTEL_FAM6_SANDYBRIDGE_X:
4483 case INTEL_FAM6_IVYBRIDGE_X:
4485 RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS |
4486 RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
4487 BIC_PRESENT(BIC_PKG__);
4488 BIC_PRESENT(BIC_RAM__);
4490 BIC_PRESENT(BIC_Pkg_J);
4491 BIC_PRESENT(BIC_Cor_J);
4492 BIC_PRESENT(BIC_RAM_J);
4494 BIC_PRESENT(BIC_PkgWatt);
4495 BIC_PRESENT(BIC_CorWatt);
4496 BIC_PRESENT(BIC_RAMWatt);
4499 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4500 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4501 do_rapl = RAPL_PKG | RAPL_CORES;
4503 BIC_PRESENT(BIC_Pkg_J);
4504 BIC_PRESENT(BIC_Cor_J);
4506 BIC_PRESENT(BIC_PkgWatt);
4507 BIC_PRESENT(BIC_CorWatt);
4510 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
4512 RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
4513 RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
4514 BIC_PRESENT(BIC_PKG__);
4515 BIC_PRESENT(BIC_RAM__);
4517 BIC_PRESENT(BIC_Pkg_J);
4518 BIC_PRESENT(BIC_Cor_J);
4519 BIC_PRESENT(BIC_RAM_J);
4521 BIC_PRESENT(BIC_PkgWatt);
4522 BIC_PRESENT(BIC_CorWatt);
4523 BIC_PRESENT(BIC_RAMWatt);
4530 /* units on package 0, verify later other packages match */
4531 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4534 rapl_power_units = 1.0 / (1 << (msr & 0xF));
4535 if (model == INTEL_FAM6_ATOM_SILVERMONT)
4536 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4538 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4540 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
4542 time_unit = msr >> 16 & 0xF;
4546 rapl_time_units = 1.0 / (1 << (time_unit));
4548 tdp = get_tdp_intel(model);
4550 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
4552 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
4555 void rapl_probe_amd(unsigned int family, unsigned int model)
4557 unsigned long long msr;
4558 unsigned int eax, ebx, ecx, edx;
4559 unsigned int has_rapl = 0;
4562 if (max_extended_level >= 0x80000007) {
4563 __cpuid(0x80000007, eax, ebx, ecx, edx);
4564 /* RAPL (Fam 17h+) */
4565 has_rapl = edx & (1 << 14);
4568 if (!has_rapl || family < 0x17)
4571 do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
4573 BIC_PRESENT(BIC_Pkg_J);
4574 BIC_PRESENT(BIC_Cor_J);
4576 BIC_PRESENT(BIC_PkgWatt);
4577 BIC_PRESENT(BIC_CorWatt);
4580 if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
4583 rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
4584 rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
4585 rapl_power_units = ldexp(1.0, -(msr & 0xf));
4587 tdp = get_tdp_amd(family);
4589 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
4591 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
4597 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
4599 void rapl_probe(unsigned int family, unsigned int model)
4602 rapl_probe_intel(family, model);
4603 if (authentic_amd || hygon_genuine)
4604 rapl_probe_amd(family, model);
4607 void perf_limit_reasons_probe(unsigned int family, unsigned int model)
4616 case INTEL_FAM6_HASWELL: /* HSW */
4617 case INTEL_FAM6_HASWELL_L: /* HSW */
4618 case INTEL_FAM6_HASWELL_G: /* HSW */
4619 do_gfx_perf_limit_reasons = 1;
4620 case INTEL_FAM6_HASWELL_X: /* HSX */
4621 do_core_perf_limit_reasons = 1;
4622 do_ring_perf_limit_reasons = 1;
4628 void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4630 if (is_skx(family, model) || is_bdx(family, model) || is_icx(family, model))
4631 has_automatic_cstate_conversion = 1;
4634 void prewake_cstate_probe(unsigned int family, unsigned int model)
4636 if (is_icx(family, model))
4637 dis_cstate_prewake = 1;
4640 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4642 unsigned long long msr;
4643 unsigned int dts, dts2;
4646 if (!(do_dts || do_ptm))
4651 /* DTS is per-core, no need to print for each thread */
4652 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4655 if (cpu_migrate(cpu)) {
4656 fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu);
4660 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4661 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4664 dts = (msr >> 16) & 0x7F;
4665 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts);
4667 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4670 dts = (msr >> 16) & 0x7F;
4671 dts2 = (msr >> 8) & 0x7F;
4672 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4673 cpu, msr, tj_max - dts, tj_max - dts2);
4676 if (do_dts && debug) {
4677 unsigned int resolution;
4679 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4682 dts = (msr >> 16) & 0x7F;
4683 resolution = (msr >> 27) & 0xF;
4684 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4685 cpu, msr, tj_max - dts, resolution);
4687 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4690 dts = (msr >> 16) & 0x7F;
4691 dts2 = (msr >> 8) & 0x7F;
4692 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4693 cpu, msr, tj_max - dts, tj_max - dts2);
4699 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4701 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4703 ((msr >> 15) & 1) ? "EN" : "DIS",
4704 ((msr >> 0) & 0x7FFF) * rapl_power_units,
4705 (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4706 (((msr >> 16) & 1) ? "EN" : "DIS"));
4711 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4713 unsigned long long msr;
4714 const char *msr_name;
4720 /* RAPL counters are per package, so print only for 1st thread/package */
4721 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4725 if (cpu_migrate(cpu)) {
4726 fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu);
4730 if (do_rapl & RAPL_AMD_F17H) {
4731 msr_name = "MSR_RAPL_PWR_UNIT";
4732 if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
4735 msr_name = "MSR_RAPL_POWER_UNIT";
4736 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4740 fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4741 rapl_power_units, rapl_energy_units, rapl_time_units);
4743 if (do_rapl & RAPL_PKG_POWER_INFO) {
4745 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4748 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4750 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4751 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4752 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4753 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4756 if (do_rapl & RAPL_PKG) {
4758 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4761 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4762 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4764 print_power_limit_msr(cpu, msr, "PKG Limit #1");
4765 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4767 ((msr >> 47) & 1) ? "EN" : "DIS",
4768 ((msr >> 32) & 0x7FFF) * rapl_power_units,
4769 (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4770 ((msr >> 48) & 1) ? "EN" : "DIS");
4773 if (do_rapl & RAPL_DRAM_POWER_INFO) {
4774 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4777 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4779 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4780 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4781 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4782 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4784 if (do_rapl & RAPL_DRAM) {
4785 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4787 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4788 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4790 print_power_limit_msr(cpu, msr, "DRAM Limit");
4792 if (do_rapl & RAPL_CORE_POLICY) {
4793 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4796 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4798 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4799 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4801 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4802 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4803 print_power_limit_msr(cpu, msr, "Cores Limit");
4805 if (do_rapl & RAPL_GFX) {
4806 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4809 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4811 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4813 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4814 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4815 print_power_limit_msr(cpu, msr, "GFX Limit");
4821 * SNB adds support for additional MSRs:
4823 * MSR_PKG_C7_RESIDENCY 0x000003fa
4824 * MSR_CORE_C7_RESIDENCY 0x000003fe
4825 * MSR_PKG_C2_RESIDENCY 0x0000060d
4828 int has_snb_msrs(unsigned int family, unsigned int model)
4834 case INTEL_FAM6_SANDYBRIDGE:
4835 case INTEL_FAM6_SANDYBRIDGE_X:
4836 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4837 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4838 case INTEL_FAM6_HASWELL: /* HSW */
4839 case INTEL_FAM6_HASWELL_X: /* HSW */
4840 case INTEL_FAM6_HASWELL_L: /* HSW */
4841 case INTEL_FAM6_HASWELL_G: /* HSW */
4842 case INTEL_FAM6_BROADWELL: /* BDW */
4843 case INTEL_FAM6_BROADWELL_G: /* BDW */
4844 case INTEL_FAM6_BROADWELL_X: /* BDX */
4845 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4846 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4847 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4848 case INTEL_FAM6_ICELAKE_X: /* ICX */
4849 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4850 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4851 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
4852 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4853 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
4860 * HSW ULT added support for C8/C9/C10 MSRs:
4862 * MSR_PKG_C8_RESIDENCY 0x00000630
4863 * MSR_PKG_C9_RESIDENCY 0x00000631
4864 * MSR_PKG_C10_RESIDENCY 0x00000632
4866 * MSR_PKGC8_IRTL 0x00000633
4867 * MSR_PKGC9_IRTL 0x00000634
4868 * MSR_PKGC10_IRTL 0x00000635
4871 int has_c8910_msrs(unsigned int family, unsigned int model)
4877 case INTEL_FAM6_HASWELL_L: /* HSW */
4878 case INTEL_FAM6_BROADWELL: /* BDW */
4879 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4880 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4881 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4882 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4883 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4890 * SKL adds support for additional MSRS:
4892 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
4893 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
4894 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
4895 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
4897 int has_skl_msrs(unsigned int family, unsigned int model)
4903 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4904 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4910 int is_slm(unsigned int family, unsigned int model)
4915 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4916 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4922 int is_knl(unsigned int family, unsigned int model)
4927 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4933 int is_cnl(unsigned int family, unsigned int model)
4939 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4946 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4948 if (is_knl(family, model))
4953 #define SLM_BCLK_FREQS 5
4954 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 };
4956 double slm_bclk(void)
4958 unsigned long long msr = 3;
4962 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4963 fprintf(outf, "SLM BCLK: unknown\n");
4966 if (i >= SLM_BCLK_FREQS) {
4967 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
4970 freq = slm_freq_table[i];
4973 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4978 double discover_bclk(unsigned int family, unsigned int model)
4980 if (has_snb_msrs(family, model) || is_knl(family, model))
4982 else if (is_slm(family, model))
4988 int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4990 unsigned int eax, ebx, ecx, edx;
4995 if (cpu_migrate(t->cpu_id)) {
4996 fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id);
5000 if (max_level < 0x1a)
5003 __cpuid(0x1a, eax, ebx, ecx, edx);
5004 eax = (eax >> 24) & 0xFF;
5011 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
5012 * the Thermal Control Circuit (TCC) activates.
5013 * This is usually equal to tjMax.
5015 * Older processors do not have this MSR, so there we guess,
5016 * but also allow cmdline over-ride with -T.
5018 * Several MSR temperature values are in units of degrees-C
5019 * below this value, including the Digital Thermal Sensor (DTS),
5020 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
5022 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
5024 unsigned long long msr;
5025 unsigned int tcc_default, tcc_offset;
5028 /* tj_max is used only for dts or ptm */
5029 if (!(do_dts || do_ptm))
5032 /* this is a per-package concept */
5033 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
5037 if (cpu_migrate(cpu)) {
5038 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
5042 if (tj_max_override != 0) {
5043 tj_max = tj_max_override;
5044 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max);
5048 /* Temperature Target MSR is Nehalem and newer only */
5049 if (!do_nhm_platform_info)
5052 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
5055 tcc_default = (msr >> 16) & 0xFF;
5058 switch (tcc_offset_bits) {
5060 tcc_offset = (msr >> 24) & 0xF;
5061 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
5062 cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
5065 tcc_offset = (msr >> 24) & 0x3F;
5066 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
5067 cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
5070 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default);
5078 tj_max = tcc_default;
5083 tj_max = TJMAX_DEFAULT;
5084 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max);
5089 void decode_feature_control_msr(void)
5091 unsigned long long msr;
5093 if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
5094 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
5095 base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : "");
5098 void decode_misc_enable_msr(void)
5100 unsigned long long msr;
5105 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
5106 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
5108 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
5109 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
5110 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
5111 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
5112 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
5115 void decode_misc_feature_control(void)
5117 unsigned long long msr;
5119 if (!has_misc_feature_control)
5122 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
5124 "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
5125 base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "",
5126 msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : "");
5130 * Decode MSR_MISC_PWR_MGMT
5132 * Decode the bits according to the Nehalem documentation
5133 * bit[0] seems to continue to have same meaning going forward
5136 void decode_misc_pwr_mgmt_msr(void)
5138 unsigned long long msr;
5140 if (!do_nhm_platform_info)
5143 if (no_MSR_MISC_PWR_MGMT)
5146 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
5147 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
5149 msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS");
5153 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
5155 * This MSRs are present on Silvermont processors,
5156 * Intel Atom processor E3000 series (Baytrail), and friends.
5158 void decode_c6_demotion_policy_msr(void)
5160 unsigned long long msr;
5162 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
5163 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
5164 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5166 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
5167 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
5168 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5172 * When models are the same, for the purpose of turbostat, reuse
5174 unsigned int intel_model_duplicates(unsigned int model)
5178 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
5179 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
5180 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
5181 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
5182 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
5183 return INTEL_FAM6_NEHALEM;
5185 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
5186 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
5187 return INTEL_FAM6_NEHALEM_EX;
5189 case INTEL_FAM6_XEON_PHI_KNM:
5190 return INTEL_FAM6_XEON_PHI_KNL;
5192 case INTEL_FAM6_BROADWELL_X:
5193 case INTEL_FAM6_BROADWELL_D: /* BDX-DE */
5194 return INTEL_FAM6_BROADWELL_X;
5196 case INTEL_FAM6_SKYLAKE_L:
5197 case INTEL_FAM6_SKYLAKE:
5198 case INTEL_FAM6_KABYLAKE_L:
5199 case INTEL_FAM6_KABYLAKE:
5200 case INTEL_FAM6_COMETLAKE_L:
5201 case INTEL_FAM6_COMETLAKE:
5202 return INTEL_FAM6_SKYLAKE_L;
5204 case INTEL_FAM6_ICELAKE_L:
5205 case INTEL_FAM6_ICELAKE_NNPI:
5206 case INTEL_FAM6_TIGERLAKE_L:
5207 case INTEL_FAM6_TIGERLAKE:
5208 case INTEL_FAM6_ROCKETLAKE:
5209 case INTEL_FAM6_LAKEFIELD:
5210 case INTEL_FAM6_ALDERLAKE:
5211 case INTEL_FAM6_ALDERLAKE_L:
5212 return INTEL_FAM6_CANNONLAKE_L;
5214 case INTEL_FAM6_ATOM_TREMONT_L:
5215 return INTEL_FAM6_ATOM_TREMONT;
5217 case INTEL_FAM6_ICELAKE_D:
5218 case INTEL_FAM6_SAPPHIRERAPIDS_X:
5219 return INTEL_FAM6_ICELAKE_X;
5224 void print_dev_latency(void)
5226 char *path = "/dev/cpu_dma_latency";
5231 fd = open(path, O_RDONLY);
5233 warn("fopen %s\n", path);
5237 retval = read(fd, (void *)&value, sizeof(int));
5238 if (retval != sizeof(int)) {
5239 warn("read %s\n", path);
5243 fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained");
5249 * Linux-perf manages the the HW instructions-retired counter
5250 * by enabling when requested, and hiding rollover
5252 void linux_perf_init(void)
5254 if (!BIC_IS_ENABLED(BIC_IPC))
5257 if (access("/proc/sys/kernel/perf_event_paranoid", F_OK))
5260 fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5261 if (fd_instr_count_percpu == NULL)
5262 err(-1, "calloc fd_instr_count_percpu");
5264 BIC_PRESENT(BIC_IPC);
5267 void process_cpuid()
5269 unsigned int eax, ebx, ecx, edx;
5270 unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
5271 unsigned int has_turbo;
5272 unsigned long long ucode_patch = 0;
5274 eax = ebx = ecx = edx = 0;
5276 __cpuid(0, max_level, ebx, ecx, edx);
5278 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
5280 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
5282 else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
5286 fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n",
5287 (char *)&ebx, (char *)&edx, (char *)&ecx, max_level);
5289 __cpuid(1, fms, ebx, ecx, edx);
5290 family = (fms >> 8) & 0xf;
5291 model = (fms >> 4) & 0xf;
5292 stepping = fms & 0xf;
5294 family += (fms >> 20) & 0xff;
5296 model += ((fms >> 16) & 0xf) << 4;
5300 if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
5301 warnx("get_msr(UCODE)\n");
5304 * check max extended function levels of CPUID.
5305 * This is needed to check for invariant TSC.
5306 * This check is valid for both Intel and AMD.
5308 ebx = ecx = edx = 0;
5309 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
5312 fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n",
5313 family, model, stepping, family, model, stepping,
5314 (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
5315 fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
5316 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
5317 ecx_flags & (1 << 0) ? "SSE3" : "-",
5318 ecx_flags & (1 << 3) ? "MONITOR" : "-",
5319 ecx_flags & (1 << 6) ? "SMX" : "-",
5320 ecx_flags & (1 << 7) ? "EIST" : "-",
5321 ecx_flags & (1 << 8) ? "TM2" : "-",
5322 edx_flags & (1 << 4) ? "TSC" : "-",
5323 edx_flags & (1 << 5) ? "MSR" : "-",
5324 edx_flags & (1 << 22) ? "ACPI-TM" : "-",
5325 edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-");
5327 if (genuine_intel) {
5329 model = intel_model_duplicates(model);
5332 if (!(edx_flags & (1 << 5)))
5333 errx(1, "CPUID: no MSR");
5335 if (max_extended_level >= 0x80000007) {
5338 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
5339 * this check is valid for both Intel and AMD
5341 __cpuid(0x80000007, eax, ebx, ecx, edx);
5342 has_invariant_tsc = edx & (1 << 8);
5346 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
5347 * this check is valid for both Intel and AMD
5350 __cpuid(0x6, eax, ebx, ecx, edx);
5351 has_aperf = ecx & (1 << 0);
5353 BIC_PRESENT(BIC_Avg_MHz);
5354 BIC_PRESENT(BIC_Busy);
5355 BIC_PRESENT(BIC_Bzy_MHz);
5357 do_dts = eax & (1 << 0);
5359 BIC_PRESENT(BIC_CoreTmp);
5360 has_turbo = eax & (1 << 1);
5361 do_ptm = eax & (1 << 6);
5363 BIC_PRESENT(BIC_PkgTmp);
5364 has_hwp = eax & (1 << 7);
5365 has_hwp_notify = eax & (1 << 8);
5366 has_hwp_activity_window = eax & (1 << 9);
5367 has_hwp_epp = eax & (1 << 10);
5368 has_hwp_pkg = eax & (1 << 11);
5369 has_epb = ecx & (1 << 3);
5372 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
5373 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
5374 has_aperf ? "" : "No-",
5375 has_turbo ? "" : "No-",
5376 do_dts ? "" : "No-",
5377 do_ptm ? "" : "No-",
5378 has_hwp ? "" : "No-",
5379 has_hwp_notify ? "" : "No-",
5380 has_hwp_activity_window ? "" : "No-",
5381 has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-");
5384 decode_misc_enable_msr();
5386 if (max_level >= 0x7 && !quiet) {
5391 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
5393 has_sgx = ebx & (1 << 2);
5394 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
5397 decode_feature_control_msr();
5400 if (max_level >= 0x15) {
5401 unsigned int eax_crystal;
5402 unsigned int ebx_tsc;
5405 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
5407 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
5408 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
5412 if (!quiet && (ebx != 0))
5413 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
5414 eax_crystal, ebx_tsc, crystal_hz);
5416 if (crystal_hz == 0)
5418 case INTEL_FAM6_SKYLAKE_L: /* SKL */
5419 crystal_hz = 24000000; /* 24.0 MHz */
5421 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
5422 crystal_hz = 25000000; /* 25.0 MHz */
5424 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5425 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5426 crystal_hz = 19200000; /* 19.2 MHz */
5433 tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal;
5435 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
5436 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
5440 if (max_level >= 0x16) {
5441 unsigned int base_mhz, max_mhz, bus_mhz, edx;
5444 * CPUID 16H Base MHz, Max MHz, Bus MHz
5446 base_mhz = max_mhz = bus_mhz = edx = 0;
5448 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
5450 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
5451 base_mhz, max_mhz, bus_mhz);
5455 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
5457 BIC_PRESENT(BIC_IRQ);
5458 BIC_PRESENT(BIC_TSC_MHz);
5460 if (probe_nhm_msrs(family, model)) {
5461 do_nhm_platform_info = 1;
5462 BIC_PRESENT(BIC_CPU_c1);
5463 BIC_PRESENT(BIC_CPU_c3);
5464 BIC_PRESENT(BIC_CPU_c6);
5465 BIC_PRESENT(BIC_SMI);
5467 do_snb_cstates = has_snb_msrs(family, model);
5470 BIC_PRESENT(BIC_CPU_c7);
5472 do_irtl_snb = has_snb_msrs(family, model);
5473 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
5474 BIC_PRESENT(BIC_Pkgpc2);
5475 if (pkg_cstate_limit >= PCL__3)
5476 BIC_PRESENT(BIC_Pkgpc3);
5477 if (pkg_cstate_limit >= PCL__6)
5478 BIC_PRESENT(BIC_Pkgpc6);
5479 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
5480 BIC_PRESENT(BIC_Pkgpc7);
5481 if (has_slv_msrs(family, model)) {
5482 BIC_NOT_PRESENT(BIC_Pkgpc2);
5483 BIC_NOT_PRESENT(BIC_Pkgpc3);
5484 BIC_PRESENT(BIC_Pkgpc6);
5485 BIC_NOT_PRESENT(BIC_Pkgpc7);
5486 BIC_PRESENT(BIC_Mod_c6);
5487 use_c1_residency_msr = 1;
5489 if (is_jvl(family, model)) {
5490 BIC_NOT_PRESENT(BIC_CPU_c3);
5491 BIC_NOT_PRESENT(BIC_CPU_c7);
5492 BIC_NOT_PRESENT(BIC_Pkgpc2);
5493 BIC_NOT_PRESENT(BIC_Pkgpc3);
5494 BIC_NOT_PRESENT(BIC_Pkgpc6);
5495 BIC_NOT_PRESENT(BIC_Pkgpc7);
5497 if (is_dnv(family, model)) {
5498 BIC_PRESENT(BIC_CPU_c1);
5499 BIC_NOT_PRESENT(BIC_CPU_c3);
5500 BIC_NOT_PRESENT(BIC_Pkgpc3);
5501 BIC_NOT_PRESENT(BIC_CPU_c7);
5502 BIC_NOT_PRESENT(BIC_Pkgpc7);
5503 use_c1_residency_msr = 1;
5505 if (is_skx(family, model) || is_icx(family, model)) {
5506 BIC_NOT_PRESENT(BIC_CPU_c3);
5507 BIC_NOT_PRESENT(BIC_Pkgpc3);
5508 BIC_NOT_PRESENT(BIC_CPU_c7);
5509 BIC_NOT_PRESENT(BIC_Pkgpc7);
5511 if (is_bdx(family, model)) {
5512 BIC_NOT_PRESENT(BIC_CPU_c7);
5513 BIC_NOT_PRESENT(BIC_Pkgpc7);
5515 if (has_c8910_msrs(family, model)) {
5516 if (pkg_cstate_limit >= PCL__8)
5517 BIC_PRESENT(BIC_Pkgpc8);
5518 if (pkg_cstate_limit >= PCL__9)
5519 BIC_PRESENT(BIC_Pkgpc9);
5520 if (pkg_cstate_limit >= PCL_10)
5521 BIC_PRESENT(BIC_Pkgpc10);
5523 do_irtl_hsw = has_c8910_msrs(family, model);
5524 if (has_skl_msrs(family, model)) {
5525 BIC_PRESENT(BIC_Totl_c0);
5526 BIC_PRESENT(BIC_Any_c0);
5527 BIC_PRESENT(BIC_GFX_c0);
5528 BIC_PRESENT(BIC_CPUGFX);
5530 do_slm_cstates = is_slm(family, model);
5531 do_knl_cstates = is_knl(family, model);
5533 if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model))
5534 BIC_NOT_PRESENT(BIC_CPU_c3);
5537 decode_misc_pwr_mgmt_msr();
5539 if (!quiet && has_slv_msrs(family, model))
5540 decode_c6_demotion_policy_msr();
5542 rapl_probe(family, model);
5543 perf_limit_reasons_probe(family, model);
5544 automatic_cstate_conversion_probe(family, model);
5546 check_tcc_offset(model_orig);
5549 dump_cstate_pstate_config_info(family, model);
5552 print_dev_latency();
5554 dump_sysfs_cstate_config();
5556 dump_sysfs_pstate_config();
5558 if (has_skl_msrs(family, model) || is_ehl(family, model))
5559 calculate_tsc_tweak();
5561 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
5562 BIC_PRESENT(BIC_GFX_rc6);
5564 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5565 BIC_PRESENT(BIC_GFXMHz);
5567 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK))
5568 BIC_PRESENT(BIC_GFXACTMHz);
5570 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
5571 BIC_PRESENT(BIC_CPU_LPI);
5573 BIC_NOT_PRESENT(BIC_CPU_LPI);
5575 if (!access(sys_lpi_file_sysfs, R_OK)) {
5576 sys_lpi_file = sys_lpi_file_sysfs;
5577 BIC_PRESENT(BIC_SYS_LPI);
5578 } else if (!access(sys_lpi_file_debugfs, R_OK)) {
5579 sys_lpi_file = sys_lpi_file_debugfs;
5580 BIC_PRESENT(BIC_SYS_LPI);
5582 sys_lpi_file_sysfs = NULL;
5583 BIC_NOT_PRESENT(BIC_SYS_LPI);
5587 decode_misc_feature_control();
5593 * in /dev/cpu/ return success for names that are numbers
5594 * ie. filter out ".", "..", "microcode".
5596 int dir_filter(const struct dirent *dirp)
5598 if (isdigit(dirp->d_name[0]))
5604 int open_dev_cpu_msr(int dummy1)
5609 void topology_probe()
5612 int max_core_id = 0;
5613 int max_package_id = 0;
5615 int max_siblings = 0;
5617 /* Initialize num_cpus, max_cpu_num */
5620 for_all_proc_cpus(count_cpus);
5621 if (!summary_only && topo.num_cpus > 1)
5622 BIC_PRESENT(BIC_CPU);
5625 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5627 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
5629 err(1, "calloc cpus");
5632 * Allocate and initialize cpu_present_set
5634 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5635 if (cpu_present_set == NULL)
5636 err(3, "CPU_ALLOC");
5637 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5638 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5639 for_all_proc_cpus(mark_cpu_present);
5642 * Validate that all cpus in cpu_subset are also in cpu_present_set
5644 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
5645 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
5646 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
5647 err(1, "cpu%d not present", i);
5651 * Allocate and initialize cpu_affinity_set
5653 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5654 if (cpu_affinity_set == NULL)
5655 err(3, "CPU_ALLOC");
5656 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5657 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5659 for_all_proc_cpus(init_thread_id);
5663 * find max_core_id, max_package_id
5665 for (i = 0; i <= topo.max_cpu_num; ++i) {
5668 if (cpu_is_not_present(i)) {
5670 fprintf(outf, "cpu%d NOT PRESENT\n", i);
5674 cpus[i].logical_cpu_id = i;
5676 /* get package information */
5677 cpus[i].physical_package_id = get_physical_package_id(i);
5678 if (cpus[i].physical_package_id > max_package_id)
5679 max_package_id = cpus[i].physical_package_id;
5681 /* get die information */
5682 cpus[i].die_id = get_die_id(i);
5683 if (cpus[i].die_id > max_die_id)
5684 max_die_id = cpus[i].die_id;
5686 /* get numa node information */
5687 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5688 if (cpus[i].physical_node_id > topo.max_node_num)
5689 topo.max_node_num = cpus[i].physical_node_id;
5691 /* get core information */
5692 cpus[i].physical_core_id = get_core_id(i);
5693 if (cpus[i].physical_core_id > max_core_id)
5694 max_core_id = cpus[i].physical_core_id;
5696 /* get thread information */
5697 siblings = get_thread_siblings(&cpus[i]);
5698 if (siblings > max_siblings)
5699 max_siblings = siblings;
5700 if (cpus[i].thread_id == 0)
5704 topo.cores_per_node = max_core_id + 1;
5706 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node);
5707 if (!summary_only && topo.cores_per_node > 1)
5708 BIC_PRESENT(BIC_Core);
5710 topo.num_die = max_die_id + 1;
5712 fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die);
5713 if (!summary_only && topo.num_die > 1)
5714 BIC_PRESENT(BIC_Die);
5716 topo.num_packages = max_package_id + 1;
5718 fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages);
5719 if (!summary_only && topo.num_packages > 1)
5720 BIC_PRESENT(BIC_Package);
5724 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
5725 if (!summary_only && topo.nodes_per_pkg > 1)
5726 BIC_PRESENT(BIC_Node);
5728 topo.threads_per_core = max_siblings;
5730 fprintf(outf, "max_siblings %d\n", max_siblings);
5735 for (i = 0; i <= topo.max_cpu_num; ++i) {
5736 if (cpu_is_not_present(i))
5739 "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
5740 i, cpus[i].physical_package_id, cpus[i].die_id,
5741 cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id);
5746 void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
5749 int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages;
5750 int num_threads = topo.threads_per_core * num_cores;
5752 *t = calloc(num_threads, sizeof(struct thread_data));
5756 for (i = 0; i < num_threads; i++)
5757 (*t)[i].cpu_id = -1;
5759 *c = calloc(num_cores, sizeof(struct core_data));
5763 for (i = 0; i < num_cores; i++)
5764 (*c)[i].core_id = -1;
5766 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
5770 for (i = 0; i < topo.num_packages; i++)
5771 (*p)[i].package_id = i;
5775 err(1, "calloc counters");
5781 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5783 void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id)
5785 int pkg_id = cpus[cpu_id].physical_package_id;
5786 int node_id = cpus[cpu_id].logical_node_id;
5787 int core_id = cpus[cpu_id].physical_core_id;
5788 int thread_id = cpus[cpu_id].thread_id;
5789 struct thread_data *t;
5790 struct core_data *c;
5793 /* Workaround for systems where physical_node_id==-1
5794 * and logical_node_id==(-1 - topo.num_cpus)
5799 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
5800 c = GET_CORE(core_base, core_id, node_id, pkg_id);
5801 p = GET_PKG(pkg_base, pkg_id);
5804 if (thread_id == 0) {
5805 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5806 if (cpu_is_first_core_in_package(cpu_id))
5807 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5810 c->core_id = core_id;
5811 p->package_id = pkg_id;
5814 int initialize_counters(int cpu_id)
5816 init_counter(EVEN_COUNTERS, cpu_id);
5817 init_counter(ODD_COUNTERS, cpu_id);
5821 void allocate_output_buffer()
5823 output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
5824 outp = output_buffer;
5826 err(-1, "calloc output buffer");
5829 void allocate_fd_percpu(void)
5831 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5832 if (fd_percpu == NULL)
5833 err(-1, "calloc fd_percpu");
5836 void allocate_irq_buffers(void)
5838 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5839 if (irq_column_2_cpu == NULL)
5840 err(-1, "calloc %d", topo.num_cpus);
5842 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5843 if (irqs_per_cpu == NULL)
5844 err(-1, "calloc %d", topo.max_cpu_num + 1);
5847 void setup_all_buffers(void)
5850 allocate_irq_buffers();
5851 allocate_fd_percpu();
5852 allocate_counters(&thread_even, &core_even, &package_even);
5853 allocate_counters(&thread_odd, &core_odd, &package_odd);
5854 allocate_output_buffer();
5855 for_all_proc_cpus(initialize_counters);
5858 void set_base_cpu(void)
5860 base_cpu = sched_getcpu();
5862 err(-ENODEV, "No valid cpus found");
5865 fprintf(outf, "base_cpu = %d\n", base_cpu);
5868 void turbostat_init()
5870 setup_all_buffers();
5873 check_permissions();
5878 for_all_cpus(print_hwp, ODD_COUNTERS);
5881 for_all_cpus(print_epb, ODD_COUNTERS);
5884 for_all_cpus(print_perf_limit, ODD_COUNTERS);
5887 for_all_cpus(print_rapl, ODD_COUNTERS);
5889 for_all_cpus(set_temperature_target, ODD_COUNTERS);
5891 for_all_cpus(get_cpu_type, ODD_COUNTERS);
5892 for_all_cpus(get_cpu_type, EVEN_COUNTERS);
5895 for_all_cpus(print_thermal, ODD_COUNTERS);
5897 if (!quiet && do_irtl_snb)
5901 int fork_it(char **argv)
5906 snapshot_proc_sysfs_files();
5907 status = for_all_cpus(get_counters, EVEN_COUNTERS);
5908 first_counter_read = 0;
5911 /* clear affinity side-effect of get_counters() */
5912 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5913 gettimeofday(&tv_even, (struct timezone *)NULL);
5918 execvp(argv[0], argv);
5919 err(errno, "exec %s", argv[0]);
5923 if (child_pid == -1)
5926 signal(SIGINT, SIG_IGN);
5927 signal(SIGQUIT, SIG_IGN);
5928 if (waitpid(child_pid, &status, 0) == -1)
5929 err(status, "waitpid");
5931 if (WIFEXITED(status))
5932 status = WEXITSTATUS(status);
5935 * n.b. fork_it() does not check for errors from for_all_cpus()
5936 * because re-starting is problematic when forking
5938 snapshot_proc_sysfs_files();
5939 for_all_cpus(get_counters, ODD_COUNTERS);
5940 gettimeofday(&tv_odd, (struct timezone *)NULL);
5941 timersub(&tv_odd, &tv_even, &tv_delta);
5942 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5943 fprintf(outf, "%s: Counter reset detected\n", progname);
5945 compute_average(EVEN_COUNTERS);
5946 format_all_counters(EVEN_COUNTERS);
5949 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0);
5951 flush_output_stderr();
5956 int get_and_dump_counters(void)
5960 snapshot_proc_sysfs_files();
5961 status = for_all_cpus(get_counters, ODD_COUNTERS);
5965 status = for_all_cpus(dump_counters, ODD_COUNTERS);
5969 flush_output_stdout();
5974 void print_version()
5976 fprintf(outf, "turbostat version 21.05.04" " - Len Brown <lenb@kernel.org>\n");
5979 int add_counter(unsigned int msr_num, char *path, char *name,
5980 unsigned int width, enum counter_scope scope,
5981 enum counter_type type, enum counter_format format, int flags)
5983 struct msr_counter *msrp;
5985 msrp = calloc(1, sizeof(struct msr_counter));
5991 msrp->msr_num = msr_num;
5992 strncpy(msrp->name, name, NAME_BYTES - 1);
5994 strncpy(msrp->path, path, PATH_BYTES - 1);
5995 msrp->width = width;
5997 msrp->format = format;
5998 msrp->flags = flags;
6003 msrp->next = sys.tp;
6005 sys.added_thread_counters++;
6006 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
6007 fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS);
6013 msrp->next = sys.cp;
6015 sys.added_core_counters++;
6016 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
6017 fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS);
6023 msrp->next = sys.pp;
6025 sys.added_package_counters++;
6026 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
6027 fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS);
6036 void parse_add_command(char *add_command)
6040 char name_buffer[NAME_BYTES] = "";
6043 enum counter_scope scope = SCOPE_CPU;
6044 enum counter_type type = COUNTER_CYCLES;
6045 enum counter_format format = FORMAT_DELTA;
6047 while (add_command) {
6049 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
6052 if (sscanf(add_command, "msr%d", &msr_num) == 1)
6055 if (*add_command == '/') {
6060 if (sscanf(add_command, "u%d", &width) == 1) {
6061 if ((width == 32) || (width == 64))
6065 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
6069 if (!strncmp(add_command, "core", strlen("core"))) {
6073 if (!strncmp(add_command, "package", strlen("package"))) {
6074 scope = SCOPE_PACKAGE;
6077 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
6078 type = COUNTER_CYCLES;
6081 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
6082 type = COUNTER_SECONDS;
6085 if (!strncmp(add_command, "usec", strlen("usec"))) {
6086 type = COUNTER_USEC;
6089 if (!strncmp(add_command, "raw", strlen("raw"))) {
6090 format = FORMAT_RAW;
6093 if (!strncmp(add_command, "delta", strlen("delta"))) {
6094 format = FORMAT_DELTA;
6097 if (!strncmp(add_command, "percent", strlen("percent"))) {
6098 format = FORMAT_PERCENT;
6102 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
6105 eos = strchr(name_buffer, ',');
6112 add_command = strchr(add_command, ',');
6114 *add_command = '\0';
6119 if ((msr_num == 0) && (path == NULL)) {
6120 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
6124 /* generate default column header */
6125 if (*name_buffer == '\0') {
6127 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6129 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6132 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
6141 int is_deferred_skip(char *name)
6145 for (i = 0; i < deferred_skip_index; ++i)
6146 if (!strcmp(name, deferred_skip_names[i]))
6151 void probe_sysfs(void)
6159 if (!DO_BIC(BIC_sysfs))
6162 for (state = 10; state >= 0; --state) {
6164 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
6165 input = fopen(path, "r");
6168 if (!fgets(name_buf, sizeof(name_buf), input))
6169 err(1, "%s: failed to read file", path);
6171 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6172 sp = strchr(name_buf, '-');
6174 sp = strchrnul(name_buf, '\n');
6178 remove_underbar(name_buf);
6182 sprintf(path, "cpuidle/state%d/time", state);
6184 if (is_deferred_skip(name_buf))
6187 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU);
6190 for (state = 10; state >= 0; --state) {
6192 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
6193 input = fopen(path, "r");
6196 if (!fgets(name_buf, sizeof(name_buf), input))
6197 err(1, "%s: failed to read file", path);
6198 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6199 sp = strchr(name_buf, '-');
6201 sp = strchrnul(name_buf, '\n');
6205 remove_underbar(name_buf);
6207 sprintf(path, "cpuidle/state%d/usage", state);
6209 if (is_deferred_skip(name_buf))
6212 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU);
6218 * parse cpuset with following syntax
6219 * 1,2,4..6,8-10 and set bits in cpu_subset
6221 void parse_cpu_command(char *optarg)
6223 unsigned int start, end;
6226 if (!strcmp(optarg, "core")) {
6232 if (!strcmp(optarg, "package")) {
6238 if (show_core_only || show_pkg_only)
6241 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
6242 if (cpu_subset == NULL)
6243 err(3, "CPU_ALLOC");
6244 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
6246 CPU_ZERO_S(cpu_subset_size, cpu_subset);
6250 while (next && *next) {
6252 if (*next == '-') /* no negative cpu numbers */
6255 start = strtoul(next, &next, 10);
6257 if (start >= CPU_SUBSET_MAXCPUS)
6259 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6270 next += 1; /* start range */
6271 } else if (*next == '.') {
6274 next += 1; /* start range */
6279 end = strtoul(next, &next, 10);
6283 while (++start <= end) {
6284 if (start >= CPU_SUBSET_MAXCPUS)
6286 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6291 else if (*next != '\0')
6298 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
6303 void cmdline(int argc, char **argv)
6306 int option_index = 0;
6307 static struct option long_options[] = {
6308 { "add", required_argument, 0, 'a' },
6309 { "cpu", required_argument, 0, 'c' },
6310 { "Dump", no_argument, 0, 'D' },
6311 { "debug", no_argument, 0, 'd' }, /* internal, not documented */
6312 { "enable", required_argument, 0, 'e' },
6313 { "interval", required_argument, 0, 'i' },
6314 { "IPC", no_argument, 0, 'I' },
6315 { "num_iterations", required_argument, 0, 'n' },
6316 { "help", no_argument, 0, 'h' },
6317 { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help
6318 { "Joules", no_argument, 0, 'J' },
6319 { "list", no_argument, 0, 'l' },
6320 { "out", required_argument, 0, 'o' },
6321 { "quiet", no_argument, 0, 'q' },
6322 { "show", required_argument, 0, 's' },
6323 { "Summary", no_argument, 0, 'S' },
6324 { "TCC", required_argument, 0, 'T' },
6325 { "version", no_argument, 0, 'v' },
6331 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) {
6334 parse_add_command(optarg);
6337 parse_cpu_command(optarg);
6343 /* --enable specified counter */
6344 bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
6348 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6352 * --hide: do not show those specified
6353 * multiple invocations simply clear more bits in enabled mask
6355 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
6363 double interval = strtod(optarg, NULL);
6365 if (interval < 0.001) {
6366 fprintf(outf, "interval %f seconds is too small\n", interval);
6370 interval_tv.tv_sec = interval_ts.tv_sec = interval;
6371 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
6372 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
6379 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6384 outf = fopen_or_die(optarg, "w");
6390 num_iterations = strtod(optarg, NULL);
6392 if (num_iterations <= 0) {
6393 fprintf(outf, "iterations %d should be positive number\n", num_iterations);
6399 * --show: show only those specified
6400 * The 1st invocation will clear and replace the enabled mask
6401 * subsequent invocations can add to it.
6404 bic_enabled = bic_lookup(optarg, SHOW_LIST);
6406 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
6413 tj_max_override = atoi(optarg);
6423 int main(int argc, char **argv)
6426 cmdline(argc, argv);
6435 /* dump counters and exit */
6437 return get_and_dump_counters();
6439 /* list header and exit */
6440 if (list_header_only) {
6442 flush_output_stdout();
6448 * if any params left, it must be a command to fork
6451 return fork_it(argv + optind);