1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2103a8feaSLen Brown /*
3103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency
434041551SLen Brown * on modern Intel and AMD processors.
5103a8feaSLen Brown *
6de7839eeSLen Brown * Copyright (c) 2023 Intel Corporation.
7103a8feaSLen Brown * Len Brown <len.brown@intel.com>
8103a8feaSLen Brown */
9103a8feaSLen Brown
1088c3281fSLen Brown #define _GNU_SOURCE
11b731f311SJosh Triplett #include MSRHEADER
12869ce69eSLen Brown #include INTEL_FAMILY_HEADER
1395aebc44SJosh Triplett #include <stdarg.h>
14103a8feaSLen Brown #include <stdio.h>
15b2c95d90SJosh Triplett #include <err.h>
16103a8feaSLen Brown #include <unistd.h>
17103a8feaSLen Brown #include <sys/types.h>
18103a8feaSLen Brown #include <sys/wait.h>
19103a8feaSLen Brown #include <sys/stat.h>
20b9ad8ee0SLen Brown #include <sys/select.h>
21103a8feaSLen Brown #include <sys/resource.h>
22103a8feaSLen Brown #include <fcntl.h>
23103a8feaSLen Brown #include <signal.h>
24103a8feaSLen Brown #include <sys/time.h>
25103a8feaSLen Brown #include <stdlib.h>
26d8af6f5fSLen Brown #include <getopt.h>
27103a8feaSLen Brown #include <dirent.h>
28103a8feaSLen Brown #include <string.h>
29103a8feaSLen Brown #include <ctype.h>
3088c3281fSLen Brown #include <sched.h>
312a0609c0SLen Brown #include <time.h>
322b92865eSJosh Triplett #include <cpuid.h>
33fcaa681cSLen Brown #include <sys/capability.h>
3498481e79SLen Brown #include <errno.h>
359392bd98SCalvin Walton #include <math.h>
362af4f9b8SLen Brown #include <linux/perf_event.h>
372af4f9b8SLen Brown #include <asm/unistd.h>
387ab5ff49SZhang Rui #include <stdbool.h>
39103a8feaSLen Brown
409878bf7aSLen Brown #define UNUSED(x) (void)(x)
419878bf7aSLen Brown
42164d7a96SLen Brown /*
43164d7a96SLen Brown * This list matches the column headers, except
44164d7a96SLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
45164d7a96SLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them
46164d7a96SLen Brown * matching on them for --show and --hide.
47164d7a96SLen Brown */
48164d7a96SLen Brown
49164d7a96SLen Brown /*
50164d7a96SLen Brown * buffer size used by sscanf() for added column names
51164d7a96SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
52164d7a96SLen Brown */
53164d7a96SLen Brown #define NAME_BYTES 20
54164d7a96SLen Brown #define PATH_BYTES 128
55164d7a96SLen Brown
5652b3541bSWyes Karny #define MAX_NOFILE 0x8000
5752b3541bSWyes Karny
58164d7a96SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE };
59164d7a96SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC };
60164d7a96SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT };
61164d7a96SLen Brown
62164d7a96SLen Brown struct msr_counter {
63164d7a96SLen Brown unsigned int msr_num;
64164d7a96SLen Brown char name[NAME_BYTES];
65164d7a96SLen Brown char path[PATH_BYTES];
66164d7a96SLen Brown unsigned int width;
67164d7a96SLen Brown enum counter_type type;
68164d7a96SLen Brown enum counter_format format;
69164d7a96SLen Brown struct msr_counter *next;
70164d7a96SLen Brown unsigned int flags;
71164d7a96SLen Brown #define FLAGS_HIDE (1 << 0)
72164d7a96SLen Brown #define FLAGS_SHOW (1 << 1)
73164d7a96SLen Brown #define SYSFS_PERCPU (1 << 1)
74164d7a96SLen Brown };
75164d7a96SLen Brown
76164d7a96SLen Brown struct msr_counter bic[] = {
779878bf7aSLen Brown { 0x0, "usec", "", 0, 0, 0, NULL, 0 },
789878bf7aSLen Brown { 0x0, "Time_Of_Day_Seconds", "", 0, 0, 0, NULL, 0 },
799878bf7aSLen Brown { 0x0, "Package", "", 0, 0, 0, NULL, 0 },
809878bf7aSLen Brown { 0x0, "Node", "", 0, 0, 0, NULL, 0 },
819878bf7aSLen Brown { 0x0, "Avg_MHz", "", 0, 0, 0, NULL, 0 },
829878bf7aSLen Brown { 0x0, "Busy%", "", 0, 0, 0, NULL, 0 },
839878bf7aSLen Brown { 0x0, "Bzy_MHz", "", 0, 0, 0, NULL, 0 },
849878bf7aSLen Brown { 0x0, "TSC_MHz", "", 0, 0, 0, NULL, 0 },
859878bf7aSLen Brown { 0x0, "IRQ", "", 0, 0, 0, NULL, 0 },
869878bf7aSLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL, 0 },
879878bf7aSLen Brown { 0x0, "sysfs", "", 0, 0, 0, NULL, 0 },
889878bf7aSLen Brown { 0x0, "CPU%c1", "", 0, 0, 0, NULL, 0 },
899878bf7aSLen Brown { 0x0, "CPU%c3", "", 0, 0, 0, NULL, 0 },
909878bf7aSLen Brown { 0x0, "CPU%c6", "", 0, 0, 0, NULL, 0 },
919878bf7aSLen Brown { 0x0, "CPU%c7", "", 0, 0, 0, NULL, 0 },
929878bf7aSLen Brown { 0x0, "ThreadC", "", 0, 0, 0, NULL, 0 },
939878bf7aSLen Brown { 0x0, "CoreTmp", "", 0, 0, 0, NULL, 0 },
949878bf7aSLen Brown { 0x0, "CoreCnt", "", 0, 0, 0, NULL, 0 },
959878bf7aSLen Brown { 0x0, "PkgTmp", "", 0, 0, 0, NULL, 0 },
969878bf7aSLen Brown { 0x0, "GFX%rc6", "", 0, 0, 0, NULL, 0 },
979878bf7aSLen Brown { 0x0, "GFXMHz", "", 0, 0, 0, NULL, 0 },
989878bf7aSLen Brown { 0x0, "Pkg%pc2", "", 0, 0, 0, NULL, 0 },
999878bf7aSLen Brown { 0x0, "Pkg%pc3", "", 0, 0, 0, NULL, 0 },
1009878bf7aSLen Brown { 0x0, "Pkg%pc6", "", 0, 0, 0, NULL, 0 },
1019878bf7aSLen Brown { 0x0, "Pkg%pc7", "", 0, 0, 0, NULL, 0 },
1029878bf7aSLen Brown { 0x0, "Pkg%pc8", "", 0, 0, 0, NULL, 0 },
1039878bf7aSLen Brown { 0x0, "Pkg%pc9", "", 0, 0, 0, NULL, 0 },
1049878bf7aSLen Brown { 0x0, "Pk%pc10", "", 0, 0, 0, NULL, 0 },
1059878bf7aSLen Brown { 0x0, "CPU%LPI", "", 0, 0, 0, NULL, 0 },
1069878bf7aSLen Brown { 0x0, "SYS%LPI", "", 0, 0, 0, NULL, 0 },
1079878bf7aSLen Brown { 0x0, "PkgWatt", "", 0, 0, 0, NULL, 0 },
1089878bf7aSLen Brown { 0x0, "CorWatt", "", 0, 0, 0, NULL, 0 },
1099878bf7aSLen Brown { 0x0, "GFXWatt", "", 0, 0, 0, NULL, 0 },
1109878bf7aSLen Brown { 0x0, "PkgCnt", "", 0, 0, 0, NULL, 0 },
1119878bf7aSLen Brown { 0x0, "RAMWatt", "", 0, 0, 0, NULL, 0 },
1129878bf7aSLen Brown { 0x0, "PKG_%", "", 0, 0, 0, NULL, 0 },
1139878bf7aSLen Brown { 0x0, "RAM_%", "", 0, 0, 0, NULL, 0 },
1149878bf7aSLen Brown { 0x0, "Pkg_J", "", 0, 0, 0, NULL, 0 },
1159878bf7aSLen Brown { 0x0, "Cor_J", "", 0, 0, 0, NULL, 0 },
1169878bf7aSLen Brown { 0x0, "GFX_J", "", 0, 0, 0, NULL, 0 },
1179878bf7aSLen Brown { 0x0, "RAM_J", "", 0, 0, 0, NULL, 0 },
1189878bf7aSLen Brown { 0x0, "Mod%c6", "", 0, 0, 0, NULL, 0 },
1199878bf7aSLen Brown { 0x0, "Totl%C0", "", 0, 0, 0, NULL, 0 },
1209878bf7aSLen Brown { 0x0, "Any%C0", "", 0, 0, 0, NULL, 0 },
1219878bf7aSLen Brown { 0x0, "GFX%C0", "", 0, 0, 0, NULL, 0 },
1229878bf7aSLen Brown { 0x0, "CPUGFX%", "", 0, 0, 0, NULL, 0 },
1239878bf7aSLen Brown { 0x0, "Core", "", 0, 0, 0, NULL, 0 },
1249878bf7aSLen Brown { 0x0, "CPU", "", 0, 0, 0, NULL, 0 },
1259878bf7aSLen Brown { 0x0, "APIC", "", 0, 0, 0, NULL, 0 },
1269878bf7aSLen Brown { 0x0, "X2APIC", "", 0, 0, 0, NULL, 0 },
1279878bf7aSLen Brown { 0x0, "Die", "", 0, 0, 0, NULL, 0 },
1289878bf7aSLen Brown { 0x0, "GFXAMHz", "", 0, 0, 0, NULL, 0 },
1299878bf7aSLen Brown { 0x0, "IPC", "", 0, 0, 0, NULL, 0 },
1309878bf7aSLen Brown { 0x0, "CoreThr", "", 0, 0, 0, NULL, 0 },
131a5c6d65dSLen Brown { 0x0, "UncMHz", "", 0, 0, 0, NULL, 0 },
132164d7a96SLen Brown };
133164d7a96SLen Brown
134164d7a96SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
135164d7a96SLen Brown #define BIC_USEC (1ULL << 0)
136164d7a96SLen Brown #define BIC_TOD (1ULL << 1)
137164d7a96SLen Brown #define BIC_Package (1ULL << 2)
138164d7a96SLen Brown #define BIC_Node (1ULL << 3)
139164d7a96SLen Brown #define BIC_Avg_MHz (1ULL << 4)
140164d7a96SLen Brown #define BIC_Busy (1ULL << 5)
141164d7a96SLen Brown #define BIC_Bzy_MHz (1ULL << 6)
142164d7a96SLen Brown #define BIC_TSC_MHz (1ULL << 7)
143164d7a96SLen Brown #define BIC_IRQ (1ULL << 8)
144164d7a96SLen Brown #define BIC_SMI (1ULL << 9)
145164d7a96SLen Brown #define BIC_sysfs (1ULL << 10)
146164d7a96SLen Brown #define BIC_CPU_c1 (1ULL << 11)
147164d7a96SLen Brown #define BIC_CPU_c3 (1ULL << 12)
148164d7a96SLen Brown #define BIC_CPU_c6 (1ULL << 13)
149164d7a96SLen Brown #define BIC_CPU_c7 (1ULL << 14)
150164d7a96SLen Brown #define BIC_ThreadC (1ULL << 15)
151164d7a96SLen Brown #define BIC_CoreTmp (1ULL << 16)
152164d7a96SLen Brown #define BIC_CoreCnt (1ULL << 17)
153164d7a96SLen Brown #define BIC_PkgTmp (1ULL << 18)
154164d7a96SLen Brown #define BIC_GFX_rc6 (1ULL << 19)
155164d7a96SLen Brown #define BIC_GFXMHz (1ULL << 20)
156164d7a96SLen Brown #define BIC_Pkgpc2 (1ULL << 21)
157164d7a96SLen Brown #define BIC_Pkgpc3 (1ULL << 22)
158164d7a96SLen Brown #define BIC_Pkgpc6 (1ULL << 23)
159164d7a96SLen Brown #define BIC_Pkgpc7 (1ULL << 24)
160164d7a96SLen Brown #define BIC_Pkgpc8 (1ULL << 25)
161164d7a96SLen Brown #define BIC_Pkgpc9 (1ULL << 26)
162164d7a96SLen Brown #define BIC_Pkgpc10 (1ULL << 27)
163164d7a96SLen Brown #define BIC_CPU_LPI (1ULL << 28)
164164d7a96SLen Brown #define BIC_SYS_LPI (1ULL << 29)
165164d7a96SLen Brown #define BIC_PkgWatt (1ULL << 30)
166164d7a96SLen Brown #define BIC_CorWatt (1ULL << 31)
167164d7a96SLen Brown #define BIC_GFXWatt (1ULL << 32)
168164d7a96SLen Brown #define BIC_PkgCnt (1ULL << 33)
169164d7a96SLen Brown #define BIC_RAMWatt (1ULL << 34)
170164d7a96SLen Brown #define BIC_PKG__ (1ULL << 35)
171164d7a96SLen Brown #define BIC_RAM__ (1ULL << 36)
172164d7a96SLen Brown #define BIC_Pkg_J (1ULL << 37)
173164d7a96SLen Brown #define BIC_Cor_J (1ULL << 38)
174164d7a96SLen Brown #define BIC_GFX_J (1ULL << 39)
175164d7a96SLen Brown #define BIC_RAM_J (1ULL << 40)
176164d7a96SLen Brown #define BIC_Mod_c6 (1ULL << 41)
177164d7a96SLen Brown #define BIC_Totl_c0 (1ULL << 42)
178164d7a96SLen Brown #define BIC_Any_c0 (1ULL << 43)
179164d7a96SLen Brown #define BIC_GFX_c0 (1ULL << 44)
180164d7a96SLen Brown #define BIC_CPUGFX (1ULL << 45)
181164d7a96SLen Brown #define BIC_Core (1ULL << 46)
182164d7a96SLen Brown #define BIC_CPU (1ULL << 47)
183164d7a96SLen Brown #define BIC_APIC (1ULL << 48)
184164d7a96SLen Brown #define BIC_X2APIC (1ULL << 49)
185164d7a96SLen Brown #define BIC_Die (1ULL << 50)
186164d7a96SLen Brown #define BIC_GFXACTMHz (1ULL << 51)
187164d7a96SLen Brown #define BIC_IPC (1ULL << 52)
188164d7a96SLen Brown #define BIC_CORE_THROT_CNT (1ULL << 53)
189a5c6d65dSLen Brown #define BIC_UNCORE_MHZ (1ULL << 54)
190164d7a96SLen Brown
191164d7a96SLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die )
192164d7a96SLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__)
193a5c6d65dSLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz | BIC_UNCORE_MHZ)
194164d7a96SLen Brown #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)
195164d7a96SLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC)
196164d7a96SLen Brown
197164d7a96SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
198164d7a96SLen Brown
199164d7a96SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
200164d7a96SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
201164d7a96SLen Brown
202164d7a96SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
203164d7a96SLen Brown #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
204164d7a96SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
205164d7a96SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
206164d7a96SLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
207164d7a96SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT)
208164d7a96SLen Brown
209103a8feaSLen Brown char *proc_stat = "/proc/stat";
210b7d8c148SLen Brown FILE *outf;
21136229897SLen Brown int *fd_percpu;
2122af4f9b8SLen Brown int *fd_instr_count_percpu;
213b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 };
21447936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 };
215e9d3092fSZhang Rui
216e9d3092fSZhang Rui /* Save original CPU model */
217e9d3092fSZhang Rui unsigned int model_orig;
218e9d3092fSZhang Rui
219023fe0acSChen Yu unsigned int num_iterations;
220c7e399f8SZephaniah E. Loss-Cutler-Hull unsigned int header_iterations;
221d8af6f5fSLen Brown unsigned int debug;
22296e47158SLen Brown unsigned int quiet;
2233f44a5c6SLen Brown unsigned int shown;
2240de6c0dfSLen Brown unsigned int sums_need_wide_columns;
225d8af6f5fSLen Brown unsigned int rapl_joules;
226d8af6f5fSLen Brown unsigned int summary_only;
227c8ade361SLen Brown unsigned int list_header_only;
228d8af6f5fSLen Brown unsigned int dump_only;
229103a8feaSLen Brown unsigned int do_snb_cstates;
230fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
231144b44b1SLen Brown unsigned int do_slm_cstates;
232144b44b1SLen Brown unsigned int use_c1_residency_msr;
233103a8feaSLen Brown unsigned int has_aperf;
234889facbeSLen Brown unsigned int has_epb;
2353ea8e52eSArtem Bityutskiy unsigned int has_turbo;
236774627c5SLen Brown unsigned int is_hybrid;
2375a63426eSLen Brown unsigned int do_irtl_snb;
2385a63426eSLen Brown unsigned int do_irtl_hsw;
239fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */
240103a8feaSLen Brown unsigned int genuine_intel;
24134041551SLen Brown unsigned int authentic_amd;
242c1c10cc7SPu Wen unsigned int hygon_genuine;
24334041551SLen Brown unsigned int max_level, max_extended_level;
244103a8feaSLen Brown unsigned int has_invariant_tsc;
245d7899447SLen Brown unsigned int do_nhm_platform_info;
246cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
247b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
248103a8feaSLen Brown double bclk;
249a2b7b749SLen Brown double base_hz;
25021ed5574SLen Brown unsigned int has_base_hz;
251a2b7b749SLen Brown double tsc_tweak = 1.0;
252c98d5d94SLen Brown unsigned int show_pkg_only;
253c98d5d94SLen Brown unsigned int show_core_only;
254c98d5d94SLen Brown char *output_buffer, *outp;
255889facbeSLen Brown unsigned int do_rapl;
256889facbeSLen Brown unsigned int do_dts;
257889facbeSLen Brown unsigned int do_ptm;
2582af4f9b8SLen Brown unsigned int do_ipc;
259fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms;
260be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
261be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
26227d47356SLen Brown unsigned int gfx_cur_mhz;
263b4b91569SRafael Antognolli unsigned int gfx_act_mhz;
26455279aefSZhang Rui unsigned int tj_max;
26555279aefSZhang Rui unsigned int tj_max_override;
2660b9a0b9bSZhang Rui int tcc_offset_bits;
26740ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
26840ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
269889facbeSLen Brown double rapl_joule_counter_range;
2703a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
271ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
272aeb01e6dSChen Yu unsigned int dis_cstate_prewake;
2733a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
2743a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
2758a5bdf41SLen Brown unsigned int crystal_hz;
2768a5bdf41SLen Brown unsigned long long tsc_hz;
2777ce7d5deSPrarit Bhargava int base_cpu;
27821ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
2797f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
2807f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
2817f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
2827f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
2837f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
2847f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
28533148d67SLen Brown unsigned int has_misc_feature_control;
2864c2122d4SLen Brown unsigned int first_counter_read = 1;
287c026c236SArtem Bityutskiy int ignore_stdin;
288889facbeSLen Brown
289889facbeSLen Brown #define RAPL_PKG (1 << 0)
290e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */
291e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */
292e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1)
293e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */
294e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2)
295e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */
296e6f9bb3cSLen Brown
297889facbeSLen Brown #define RAPL_DRAM (1 << 3)
298e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */
299e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */
300e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4)
301e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */
3020b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5)
3030b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */
304e6f9bb3cSLen Brown
3059148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6)
306e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */
3070b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7)
308e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */
309e6f9bb3cSLen Brown
3100b2bb692SLen Brown #define RAPL_GFX (1 << 8)
311e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */
312e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */
313e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */
3149148494cSJacob Pan
3159148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9)
3169148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */
3179392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10)
3189392bd98SCalvin Walton /* Indicates cores energy collection is per-core,
3199392bd98SCalvin Walton * not per-package. */
3209392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11)
3219392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */
3229392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */
3239392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */
3249148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
325889facbeSLen Brown #define TJMAX_DEFAULT 100
326889facbeSLen Brown
3279392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */
3289392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299
3299392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a
3309392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b
3319392bd98SCalvin Walton
332889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
333103a8feaSLen Brown
334103a8feaSLen Brown int backwards_count;
335103a8feaSLen Brown char *progname;
336103a8feaSLen Brown
3371ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
3381ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
3391ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
3400748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8
3410748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24
3420e2d8f05SLen Brown #define BITMASK_SIZE 32
343c98d5d94SLen Brown
344c98d5d94SLen Brown struct thread_data {
345f4fdf2b4SLen Brown struct timeval tv_begin;
346f4fdf2b4SLen Brown struct timeval tv_end;
347d4794f25SYazen Ghannam struct timeval tv_delta;
348c98d5d94SLen Brown unsigned long long tsc;
349c98d5d94SLen Brown unsigned long long aperf;
350c98d5d94SLen Brown unsigned long long mperf;
351144b44b1SLen Brown unsigned long long c1;
3522af4f9b8SLen Brown unsigned long long instr_count;
3530de6c0dfSLen Brown unsigned long long irq_count;
3541ed51011SLen Brown unsigned int smi_count;
355c98d5d94SLen Brown unsigned int cpu_id;
3564c2122d4SLen Brown unsigned int apic_id;
3574c2122d4SLen Brown unsigned int x2apic_id;
358c98d5d94SLen Brown unsigned int flags;
3597ab5ff49SZhang Rui bool is_atom;
360c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
361c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
3620748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
363c98d5d94SLen Brown } *thread_even, *thread_odd;
364c98d5d94SLen Brown
365c98d5d94SLen Brown struct core_data {
366c98d5d94SLen Brown unsigned long long c3;
367c98d5d94SLen Brown unsigned long long c6;
368c98d5d94SLen Brown unsigned long long c7;
3690539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
370889facbeSLen Brown unsigned int core_temp_c;
3719392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */
372c98d5d94SLen Brown unsigned int core_id;
373eae97e05SChen Yu unsigned long long core_throt_cnt;
374678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS];
375c98d5d94SLen Brown } *core_even, *core_odd;
376c98d5d94SLen Brown
377c98d5d94SLen Brown struct pkg_data {
378c98d5d94SLen Brown unsigned long long pc2;
379c98d5d94SLen Brown unsigned long long pc3;
380c98d5d94SLen Brown unsigned long long pc6;
381c98d5d94SLen Brown unsigned long long pc7;
382ca58710fSKristen Carlson Accardi unsigned long long pc8;
383ca58710fSKristen Carlson Accardi unsigned long long pc9;
384ca58710fSKristen Carlson Accardi unsigned long long pc10;
385be0e54c4SLen Brown unsigned long long cpu_lpi;
386be0e54c4SLen Brown unsigned long long sys_lpi;
3870b2bb692SLen Brown unsigned long long pkg_wtd_core_c0;
3880b2bb692SLen Brown unsigned long long pkg_any_core_c0;
3890b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0;
3900b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0;
3919185e988SLen Brown long long gfx_rc6_ms;
39227d47356SLen Brown unsigned int gfx_mhz;
393b4b91569SRafael Antognolli unsigned int gfx_act_mhz;
394c98d5d94SLen Brown unsigned int package_id;
3957c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */
3967c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */
3977c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */
3987c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */
3997c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
4007c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
401889facbeSLen Brown unsigned int pkg_temp_c;
402a5c6d65dSLen Brown unsigned int uncore_mhz;
403678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS];
404c98d5d94SLen Brown } *package_even, *package_odd;
405c98d5d94SLen Brown
406c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
407c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
408c98d5d94SLen Brown
40940f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
41040f5cfe7SPrarit Bhargava ((thread_base) + \
41140f5cfe7SPrarit Bhargava ((pkg_no) * \
41240f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
41340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
41440f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \
41540f5cfe7SPrarit Bhargava (thread_no))
41640f5cfe7SPrarit Bhargava
41740f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \
41840f5cfe7SPrarit Bhargava ((core_base) + \
41940f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
42040f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \
42140f5cfe7SPrarit Bhargava (core_no))
42240f5cfe7SPrarit Bhargava
423c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
424c98d5d94SLen Brown
42587e15da9SChen Yu /*
42687e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic
42787e15da9SChen Yu * increasing MSR, it will be accumulated periodically,
42887e15da9SChen Yu * despite its register's bit width.
42987e15da9SChen Yu */
43087e15da9SChen Yu enum {
43187e15da9SChen Yu IDX_PKG_ENERGY,
43287e15da9SChen Yu IDX_DRAM_ENERGY,
43387e15da9SChen Yu IDX_PP0_ENERGY,
43487e15da9SChen Yu IDX_PP1_ENERGY,
43587e15da9SChen Yu IDX_PKG_PERF,
43687e15da9SChen Yu IDX_DRAM_PERF,
43787e15da9SChen Yu IDX_COUNT,
43887e15da9SChen Yu };
43987e15da9SChen Yu
44087e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr);
44187e15da9SChen Yu
44287e15da9SChen Yu struct msr_sum_array {
44387e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */
44487e15da9SChen Yu struct {
44587e15da9SChen Yu /*The accumulated MSR value is updated by the timer */
44687e15da9SChen Yu unsigned long long sum;
44787e15da9SChen Yu /*The MSR footprint recorded in last timer */
44887e15da9SChen Yu unsigned long long last;
44987e15da9SChen Yu } entries[IDX_COUNT];
45087e15da9SChen Yu };
45187e15da9SChen Yu
45287e15da9SChen Yu /* The percpu MSR sum array.*/
45387e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum;
45487e15da9SChen Yu
idx_to_offset(int idx)45513a779deSCalvin Walton off_t idx_to_offset(int idx)
45687e15da9SChen Yu {
45713a779deSCalvin Walton off_t offset;
45887e15da9SChen Yu
45987e15da9SChen Yu switch (idx) {
46087e15da9SChen Yu case IDX_PKG_ENERGY:
461301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H)
462301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT;
463301b1d3aSBas Nieuwenhuizen else
46487e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS;
46587e15da9SChen Yu break;
46687e15da9SChen Yu case IDX_DRAM_ENERGY:
46787e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS;
46887e15da9SChen Yu break;
46987e15da9SChen Yu case IDX_PP0_ENERGY:
47087e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS;
47187e15da9SChen Yu break;
47287e15da9SChen Yu case IDX_PP1_ENERGY:
47387e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS;
47487e15da9SChen Yu break;
47587e15da9SChen Yu case IDX_PKG_PERF:
47687e15da9SChen Yu offset = MSR_PKG_PERF_STATUS;
47787e15da9SChen Yu break;
47887e15da9SChen Yu case IDX_DRAM_PERF:
47987e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS;
48087e15da9SChen Yu break;
48187e15da9SChen Yu default:
48287e15da9SChen Yu offset = -1;
48387e15da9SChen Yu }
48487e15da9SChen Yu return offset;
48587e15da9SChen Yu }
48687e15da9SChen Yu
offset_to_idx(off_t offset)48713a779deSCalvin Walton int offset_to_idx(off_t offset)
48887e15da9SChen Yu {
48987e15da9SChen Yu int idx;
49087e15da9SChen Yu
49187e15da9SChen Yu switch (offset) {
49287e15da9SChen Yu case MSR_PKG_ENERGY_STATUS:
493301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT:
49487e15da9SChen Yu idx = IDX_PKG_ENERGY;
49587e15da9SChen Yu break;
49687e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS:
49787e15da9SChen Yu idx = IDX_DRAM_ENERGY;
49887e15da9SChen Yu break;
49987e15da9SChen Yu case MSR_PP0_ENERGY_STATUS:
50087e15da9SChen Yu idx = IDX_PP0_ENERGY;
50187e15da9SChen Yu break;
50287e15da9SChen Yu case MSR_PP1_ENERGY_STATUS:
50387e15da9SChen Yu idx = IDX_PP1_ENERGY;
50487e15da9SChen Yu break;
50587e15da9SChen Yu case MSR_PKG_PERF_STATUS:
50687e15da9SChen Yu idx = IDX_PKG_PERF;
50787e15da9SChen Yu break;
50887e15da9SChen Yu case MSR_DRAM_PERF_STATUS:
50987e15da9SChen Yu idx = IDX_DRAM_PERF;
51087e15da9SChen Yu break;
51187e15da9SChen Yu default:
51287e15da9SChen Yu idx = -1;
51387e15da9SChen Yu }
51487e15da9SChen Yu return idx;
51587e15da9SChen Yu }
51687e15da9SChen Yu
idx_valid(int idx)51787e15da9SChen Yu int idx_valid(int idx)
51887e15da9SChen Yu {
51987e15da9SChen Yu switch (idx) {
52087e15da9SChen Yu case IDX_PKG_ENERGY:
521301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
52287e15da9SChen Yu case IDX_DRAM_ENERGY:
52387e15da9SChen Yu return do_rapl & RAPL_DRAM;
52487e15da9SChen Yu case IDX_PP0_ENERGY:
52587e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS;
52687e15da9SChen Yu case IDX_PP1_ENERGY:
52787e15da9SChen Yu return do_rapl & RAPL_GFX;
52887e15da9SChen Yu case IDX_PKG_PERF:
52987e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS;
53087e15da9SChen Yu case IDX_DRAM_PERF:
53187e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS;
53287e15da9SChen Yu default:
53387e15da9SChen Yu return 0;
53487e15da9SChen Yu }
53587e15da9SChen Yu }
5361b439f01SLen Brown
537388e9c81SLen Brown struct sys_counters {
538678a3bd1SLen Brown unsigned int added_thread_counters;
539678a3bd1SLen Brown unsigned int added_core_counters;
540678a3bd1SLen Brown unsigned int added_package_counters;
541388e9c81SLen Brown struct msr_counter *tp;
542388e9c81SLen Brown struct msr_counter *cp;
543388e9c81SLen Brown struct msr_counter *pp;
544388e9c81SLen Brown } sys;
545388e9c81SLen Brown
546c98d5d94SLen Brown struct system_summary {
547c98d5d94SLen Brown struct thread_data threads;
548c98d5d94SLen Brown struct core_data cores;
549c98d5d94SLen Brown struct pkg_data packages;
550388e9c81SLen Brown } average;
551c98d5d94SLen Brown
5520e2d8f05SLen Brown struct cpu_topology {
5530e2d8f05SLen Brown int physical_package_id;
5546de68fe1SLen Brown int die_id;
5550e2d8f05SLen Brown int logical_cpu_id;
556ef605741SPrarit Bhargava int physical_node_id;
557ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */
5580e2d8f05SLen Brown int physical_core_id;
5598cb48b32SPrarit Bhargava int thread_id;
5600e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
5610e2d8f05SLen Brown } *cpus;
562c98d5d94SLen Brown
563c98d5d94SLen Brown struct topo_params {
564c98d5d94SLen Brown int num_packages;
5656de68fe1SLen Brown int num_die;
566103a8feaSLen Brown int num_cpus;
567c98d5d94SLen Brown int num_cores;
568c98d5d94SLen Brown int max_cpu_num;
569*76d7b869SLen Brown int max_die_id;
570ef605741SPrarit Bhargava int max_node_num;
57170a9c6e8SPrarit Bhargava int nodes_per_pkg;
57270a9c6e8SPrarit Bhargava int cores_per_node;
57370a9c6e8SPrarit Bhargava int threads_per_core;
574c98d5d94SLen Brown } topo;
575103a8feaSLen Brown
576c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
577103a8feaSLen Brown
578562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */
579562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */
580562a2d37SLen Brown
581c98d5d94SLen Brown void setup_all_buffers(void);
582103a8feaSLen Brown
5831f81c5efSLen Brown char *sys_lpi_file;
5841f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
5851f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
5861f81c5efSLen Brown
cpu_is_not_present(int cpu)587c98d5d94SLen Brown int cpu_is_not_present(int cpu)
588d15cf7c1SLen Brown {
589c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
590c98d5d94SLen Brown }
5911b439f01SLen Brown
592c98d5d94SLen Brown /*
593c98d5d94SLen Brown * run func(thread, core, package) in topology order
594c98d5d94SLen Brown * skip non-present cpus
595c98d5d94SLen Brown */
596c98d5d94SLen Brown
for_all_cpus(int (func)(struct thread_data *,struct core_data *,struct pkg_data *),struct thread_data * thread_base,struct core_data * core_base,struct pkg_data * pkg_base)597c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *),
598c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
599c98d5d94SLen Brown {
60040f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no;
601c98d5d94SLen Brown
602c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
603df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
60470a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
6051b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
606c98d5d94SLen Brown struct thread_data *t;
607c98d5d94SLen Brown struct core_data *c;
608c98d5d94SLen Brown struct pkg_data *p;
609c98d5d94SLen Brown
6101b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
611c98d5d94SLen Brown
612c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id))
613c98d5d94SLen Brown continue;
614c98d5d94SLen Brown
6151b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no);
616c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no);
617c98d5d94SLen Brown
618c98d5d94SLen Brown retval = func(t, c, p);
619c98d5d94SLen Brown if (retval)
620c98d5d94SLen Brown return retval;
621c98d5d94SLen Brown }
622c98d5d94SLen Brown }
623c98d5d94SLen Brown }
62440f5cfe7SPrarit Bhargava }
625d15cf7c1SLen Brown return 0;
626d15cf7c1SLen Brown }
627d15cf7c1SLen Brown
cpu_migrate(int cpu)62888c3281fSLen Brown int cpu_migrate(int cpu)
62988c3281fSLen Brown {
630c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
631c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
632c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
63388c3281fSLen Brown return -1;
63488c3281fSLen Brown else
63588c3281fSLen Brown return 0;
63688c3281fSLen Brown }
6371b439f01SLen Brown
get_msr_fd(int cpu)63836229897SLen Brown int get_msr_fd(int cpu)
639103a8feaSLen Brown {
640103a8feaSLen Brown char pathname[32];
641103a8feaSLen Brown int fd;
642103a8feaSLen Brown
64336229897SLen Brown fd = fd_percpu[cpu];
64436229897SLen Brown
64536229897SLen Brown if (fd)
64636229897SLen Brown return fd;
64736229897SLen Brown
648103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu);
649103a8feaSLen Brown fd = open(pathname, O_RDONLY);
65015aaa346SLen Brown if (fd < 0)
65198481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
652103a8feaSLen Brown
65336229897SLen Brown fd_percpu[cpu] = fd;
65436229897SLen Brown
65536229897SLen Brown return fd;
65636229897SLen Brown }
65736229897SLen Brown
perf_event_open(struct perf_event_attr * hw_event,pid_t pid,int cpu,int group_fd,unsigned long flags)6582af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
6592af4f9b8SLen Brown {
6602af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
6612af4f9b8SLen Brown }
6622af4f9b8SLen Brown
perf_instr_count_open(int cpu_num)6632af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num)
6642af4f9b8SLen Brown {
6652af4f9b8SLen Brown struct perf_event_attr pea;
6662af4f9b8SLen Brown int fd;
6672af4f9b8SLen Brown
6682af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr));
6692af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE;
6702af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr);
6712af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS;
6722af4f9b8SLen Brown
6732af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */
6742af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0);
675164d7a96SLen Brown if (fd == -1) {
676de7839eeSLen Brown warnx("capget(CAP_PERFMON) failed, try \"# setcap cap_sys_admin=ep %s\"", progname);
677164d7a96SLen Brown BIC_NOT_PRESENT(BIC_IPC);
678164d7a96SLen Brown }
6792af4f9b8SLen Brown
6802af4f9b8SLen Brown return fd;
6812af4f9b8SLen Brown }
6822af4f9b8SLen Brown
get_instr_count_fd(int cpu)6832af4f9b8SLen Brown int get_instr_count_fd(int cpu)
6842af4f9b8SLen Brown {
6852af4f9b8SLen Brown if (fd_instr_count_percpu[cpu])
6862af4f9b8SLen Brown return fd_instr_count_percpu[cpu];
6872af4f9b8SLen Brown
6882af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu);
6892af4f9b8SLen Brown
6902af4f9b8SLen Brown return fd_instr_count_percpu[cpu];
6912af4f9b8SLen Brown }
6922af4f9b8SLen Brown
get_msr(int cpu,off_t offset,unsigned long long * msr)69336229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
69436229897SLen Brown {
69536229897SLen Brown ssize_t retval;
69636229897SLen Brown
69736229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
69815aaa346SLen Brown
69998481e79SLen Brown if (retval != sizeof *msr)
700cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
70115aaa346SLen Brown
70215aaa346SLen Brown return 0;
703103a8feaSLen Brown }
704103a8feaSLen Brown
705dd778a5eSLen Brown #define MAX_DEFERRED 16
7060fc521bcSZephaniah E. Loss-Cutler-Hull char *deferred_add_names[MAX_DEFERRED];
707dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
7080fc521bcSZephaniah E. Loss-Cutler-Hull int deferred_add_index;
709dd778a5eSLen Brown int deferred_skip_index;
710dd778a5eSLen Brown
711dd778a5eSLen Brown /*
712dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default]
713dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest
714dd778a5eSLen Brown */
715dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
716dd778a5eSLen Brown
help(void)717dd778a5eSLen Brown void help(void)
718dd778a5eSLen Brown {
719dd778a5eSLen Brown fprintf(outf,
720dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
721dd778a5eSLen Brown "\n"
722dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n"
723dd778a5eSLen Brown "when COMMAND completes.\n"
724dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
725dd778a5eSLen Brown "to print statistics, until interrupted.\n"
726cc481650SNathan Ciobanu " -a, --add add a counter\n"
727dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
728cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n"
729dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n"
7309ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n"
7319ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n"
7329ce80578SNathan Ciobanu " -e, --enable [all | column]\n"
7339ce80578SNathan Ciobanu " shows all or the specified disabled column\n"
7349ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n"
7359ce80578SNathan Ciobanu " hide the specified column(s)\n"
736cc481650SNathan Ciobanu " -i, --interval sec.subsec\n"
737cc481650SNathan Ciobanu " Override default 5-second measurement interval\n"
7389ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n"
739cc481650SNathan Ciobanu " -l, --list list column headers only\n"
740cc481650SNathan Ciobanu " -n, --num_iterations num\n"
741cc481650SNathan Ciobanu " number of the measurement iterations\n"
742c7e399f8SZephaniah E. Loss-Cutler-Hull " -N, --header_iterations num\n"
743c7e399f8SZephaniah E. Loss-Cutler-Hull " print header every num iterations\n"
744cc481650SNathan Ciobanu " -o, --out file\n"
745cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n"
746cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n"
7479ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n"
7489ce80578SNathan Ciobanu " show only the specified column(s)\n"
7499ce80578SNathan Ciobanu " -S, --Summary\n"
7509ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n"
7519ce80578SNathan Ciobanu " -T, --TCC temperature\n"
7529ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n"
7539ce80578SNathan Ciobanu " degrees Celsius\n"
754cc481650SNathan Ciobanu " -h, --help print this help message\n"
7551b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n");
756dd778a5eSLen Brown }
757dd778a5eSLen Brown
758812db3f7SLen Brown /*
759812db3f7SLen Brown * bic_lookup
760812db3f7SLen Brown * for all the strings in comma separate name_list,
761812db3f7SLen Brown * set the approprate bit in return value.
762812db3f7SLen Brown */
bic_lookup(char * name_list,enum show_hide_mode mode)763dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
764812db3f7SLen Brown {
7659878bf7aSLen Brown unsigned int i;
766812db3f7SLen Brown unsigned long long retval = 0;
767812db3f7SLen Brown
768812db3f7SLen Brown while (name_list) {
769812db3f7SLen Brown char *comma;
770812db3f7SLen Brown
771812db3f7SLen Brown comma = strchr(name_list, ',');
772812db3f7SLen Brown
773812db3f7SLen Brown if (comma)
774812db3f7SLen Brown *comma = '\0';
775812db3f7SLen Brown
776812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) {
777812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) {
778812db3f7SLen Brown retval |= (1ULL << i);
779812db3f7SLen Brown break;
780812db3f7SLen Brown }
7815dc241f2SLen Brown if (!strcmp(name_list, "all")) {
7825dc241f2SLen Brown retval |= ~0;
7835dc241f2SLen Brown break;
7845dc241f2SLen Brown } else if (!strcmp(name_list, "topology")) {
7855dc241f2SLen Brown retval |= BIC_TOPOLOGY;
7865dc241f2SLen Brown break;
7875dc241f2SLen Brown } else if (!strcmp(name_list, "power")) {
7885dc241f2SLen Brown retval |= BIC_THERMAL_PWR;
7895dc241f2SLen Brown break;
7905dc241f2SLen Brown } else if (!strcmp(name_list, "idle")) {
7915dc241f2SLen Brown retval |= BIC_IDLE;
7925dc241f2SLen Brown break;
7935dc241f2SLen Brown } else if (!strcmp(name_list, "frequency")) {
7945dc241f2SLen Brown retval |= BIC_FREQUENCY;
7955dc241f2SLen Brown break;
7965dc241f2SLen Brown } else if (!strcmp(name_list, "other")) {
7975dc241f2SLen Brown retval |= BIC_OTHER;
7985dc241f2SLen Brown break;
7995dc241f2SLen Brown }
8005dc241f2SLen Brown
801812db3f7SLen Brown }
802812db3f7SLen Brown if (i == MAX_BIC) {
803dd778a5eSLen Brown if (mode == SHOW_LIST) {
8040fc521bcSZephaniah E. Loss-Cutler-Hull deferred_add_names[deferred_add_index++] = name_list;
8050fc521bcSZephaniah E. Loss-Cutler-Hull if (deferred_add_index >= MAX_DEFERRED) {
8060fc521bcSZephaniah E. Loss-Cutler-Hull fprintf(stderr, "More than max %d un-recognized --add options '%s'\n",
8070fc521bcSZephaniah E. Loss-Cutler-Hull MAX_DEFERRED, name_list);
8080fc521bcSZephaniah E. Loss-Cutler-Hull help();
8090fc521bcSZephaniah E. Loss-Cutler-Hull exit(1);
810812db3f7SLen Brown }
8110fc521bcSZephaniah E. Loss-Cutler-Hull } else {
812dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list;
813dd778a5eSLen Brown if (debug)
814dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list);
815dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) {
816dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
817dd778a5eSLen Brown MAX_DEFERRED, name_list);
818dd778a5eSLen Brown help();
819dd778a5eSLen Brown exit(1);
820dd778a5eSLen Brown }
821dd778a5eSLen Brown }
8220fc521bcSZephaniah E. Loss-Cutler-Hull }
823812db3f7SLen Brown
824812db3f7SLen Brown name_list = comma;
825812db3f7SLen Brown if (name_list)
826812db3f7SLen Brown name_list++;
827812db3f7SLen Brown
828812db3f7SLen Brown }
829812db3f7SLen Brown return retval;
830812db3f7SLen Brown }
831fc04cc67SLen Brown
print_header(char * delim)832c8ade361SLen Brown void print_header(char *delim)
833103a8feaSLen Brown {
834388e9c81SLen Brown struct msr_counter *mp;
8356168c2e0SLen Brown int printed = 0;
836388e9c81SLen Brown
8373f44a5c6SLen Brown if (DO_BIC(BIC_USEC))
8383f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
8393f44a5c6SLen Brown if (DO_BIC(BIC_TOD))
8403f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
841812db3f7SLen Brown if (DO_BIC(BIC_Package))
8426168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
8436de68fe1SLen Brown if (DO_BIC(BIC_Die))
8446de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
84501235041SPrarit Bhargava if (DO_BIC(BIC_Node))
84601235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
847812db3f7SLen Brown if (DO_BIC(BIC_Core))
8486168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
849812db3f7SLen Brown if (DO_BIC(BIC_CPU))
8506168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
8514c2122d4SLen Brown if (DO_BIC(BIC_APIC))
8524c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
8534c2122d4SLen Brown if (DO_BIC(BIC_X2APIC))
8544c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
855812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz))
8566168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
857812db3f7SLen Brown if (DO_BIC(BIC_Busy))
8586168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
859812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz))
8606168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
861812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz))
8626168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
8631cc21f7bSLen Brown
8642af4f9b8SLen Brown if (DO_BIC(BIC_IPC))
8652af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : ""));
8662af4f9b8SLen Brown
8670de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) {
8680de6c0dfSLen Brown if (sums_need_wide_columns)
8696168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
8700de6c0dfSLen Brown else
8716168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
8720de6c0dfSLen Brown }
8730de6c0dfSLen Brown
874812db3f7SLen Brown if (DO_BIC(BIC_SMI))
8756168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
8761cc21f7bSLen Brown
877388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) {
878dd778a5eSLen Brown
879388e9c81SLen Brown if (mp->format == FORMAT_RAW) {
880388e9c81SLen Brown if (mp->width == 64)
881dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
882388e9c81SLen Brown else
883dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
884388e9c81SLen Brown } else {
8850de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
886dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
8870de6c0dfSLen Brown else
888dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
889388e9c81SLen Brown }
890388e9c81SLen Brown }
891388e9c81SLen Brown
89241618e63SLen Brown if (DO_BIC(BIC_CPU_c1))
8936168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
894562855eeSLen Brown if (DO_BIC(BIC_CPU_c3))
8956168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
896812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6))
8976168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
898812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7))
8996168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
900678a3bd1SLen Brown
9010539ba11SLen Brown if (DO_BIC(BIC_Mod_c6))
9026168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
903678a3bd1SLen Brown
904812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp))
9056168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
906388e9c81SLen Brown
907eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT))
908eae97e05SChen Yu outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : ""));
909eae97e05SChen Yu
9109392bd98SCalvin Walton if (do_rapl && !rapl_joules) {
9119392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
9129392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
9139392bd98SCalvin Walton } else if (do_rapl && rapl_joules) {
9149392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
9159392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
9169392bd98SCalvin Walton }
9179392bd98SCalvin Walton
918388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) {
919388e9c81SLen Brown if (mp->format == FORMAT_RAW) {
920388e9c81SLen Brown if (mp->width == 64)
921c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name);
922388e9c81SLen Brown else
923c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name);
924388e9c81SLen Brown } else {
9250de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9260de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name);
9270de6c0dfSLen Brown else
928c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name);
929388e9c81SLen Brown }
930388e9c81SLen Brown }
931388e9c81SLen Brown
932812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp))
9336168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
934889facbeSLen Brown
935812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6))
9366168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
937fdf676e5SLen Brown
938812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz))
9396168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
94027d47356SLen Brown
941b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz))
942b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : ""));
943b4b91569SRafael Antognolli
944a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0))
9456168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
946a99d8730SLen Brown if (DO_BIC(BIC_Any_c0))
9476168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
948a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0))
9496168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
950a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX))
9516168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
9520b2bb692SLen Brown
9530f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2))
9546168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
9550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
9566168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
9570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6))
9586168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
9590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
9606168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
9610f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8))
9626168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
9630f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9))
9646168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
9650f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10))
9666168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
967be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI))
968be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
969be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI))
970be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
971103a8feaSLen Brown
9725c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) {
973812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt))
9746168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
9759392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
9766168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
977812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt))
9786168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
979812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt))
9806168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
981812db3f7SLen Brown if (DO_BIC(BIC_PKG__))
9826168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
983812db3f7SLen Brown if (DO_BIC(BIC_RAM__))
9846168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
985d7899447SLen Brown } else if (do_rapl && rapl_joules) {
986812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J))
9876168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
9889392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
9896168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
990812db3f7SLen Brown if (DO_BIC(BIC_GFX_J))
9916168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
992812db3f7SLen Brown if (DO_BIC(BIC_RAM_J))
9936168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
994812db3f7SLen Brown if (DO_BIC(BIC_PKG__))
9956168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
996812db3f7SLen Brown if (DO_BIC(BIC_RAM__))
9976168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
9985c56be9aSDirk Brandewie }
999a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ))
1000a5c6d65dSLen Brown outp += sprintf(outp, "%sUncMHz", (printed++ ? delim : ""));
1001a5c6d65dSLen Brown
1002388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) {
1003388e9c81SLen Brown if (mp->format == FORMAT_RAW) {
1004388e9c81SLen Brown if (mp->width == 64)
1005c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name);
1006388e9c81SLen Brown else
1007c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name);
1008388e9c81SLen Brown } else {
10090de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10100de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name);
10110de6c0dfSLen Brown else
1012c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name);
1013388e9c81SLen Brown }
1014388e9c81SLen Brown }
1015388e9c81SLen Brown
1016c98d5d94SLen Brown outp += sprintf(outp, "\n");
1017103a8feaSLen Brown }
1018103a8feaSLen Brown
dump_counters(struct thread_data * t,struct core_data * c,struct pkg_data * p)10191b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1020103a8feaSLen Brown {
1021388e9c81SLen Brown int i;
1022388e9c81SLen Brown struct msr_counter *mp;
1023388e9c81SLen Brown
10243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
1025c98d5d94SLen Brown
1026c98d5d94SLen Brown if (t) {
10271b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
10283b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
10293b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
10303b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
10313b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1);
10326886fee4SLen Brown
10332af4f9b8SLen Brown if (DO_BIC(BIC_IPC))
10342af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count);
10352af4f9b8SLen Brown
1036812db3f7SLen Brown if (DO_BIC(BIC_IRQ))
10370de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
1038812db3f7SLen Brown if (DO_BIC(BIC_SMI))
1039218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count);
1040388e9c81SLen Brown
1041388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
10421b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]);
1043388e9c81SLen Brown }
1044103a8feaSLen Brown }
1045103a8feaSLen Brown
1046c98d5d94SLen Brown if (c) {
10473b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id);
10483b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3);
10493b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6);
10503b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7);
10513b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
1052eae97e05SChen Yu outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt);
10539392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
1054388e9c81SLen Brown
1055388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
10561b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]);
1057388e9c81SLen Brown }
10580539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
1059c98d5d94SLen Brown }
1060103a8feaSLen Brown
1061c98d5d94SLen Brown if (p) {
10623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id);
10630b2bb692SLen Brown
10640b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
10650b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
10660b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
10670b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
10680b2bb692SLen Brown
10693b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
10700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
10713b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
10720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6))
10733b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
10740f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
10753b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
10763b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
10773b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
10783b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
1079be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
1080be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
10817c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
10827c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
10837c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
10847c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
10851b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status);
10861b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status);
10873b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
1088388e9c81SLen Brown
1089388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
10901b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]);
1091388e9c81SLen Brown }
1092c98d5d94SLen Brown }
10933b4d5c7fSAndy Shevchenko
10943b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n");
10953b4d5c7fSAndy Shevchenko
1096c98d5d94SLen Brown return 0;
1097103a8feaSLen Brown }
1098103a8feaSLen Brown
1099e23da037SLen Brown /*
1100e23da037SLen Brown * column formatting convention & formats
1101e23da037SLen Brown */
format_counters(struct thread_data * t,struct core_data * c,struct pkg_data * p)11021b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1103103a8feaSLen Brown {
1104008d396eSLen Brown double interval_float, tsc;
1105fc04cc67SLen Brown char *fmt8;
1106388e9c81SLen Brown int i;
1107388e9c81SLen Brown struct msr_counter *mp;
11086168c2e0SLen Brown char *delim = "\t";
11096168c2e0SLen Brown int printed = 0;
1110103a8feaSLen Brown
1111c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */
1112c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1113c98d5d94SLen Brown return 0;
1114c98d5d94SLen Brown
1115c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */
1116c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1117c98d5d94SLen Brown return 0;
1118c98d5d94SLen Brown
11191ef7d21aSLen Brown /*if not summary line and --cpu is used */
11201b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
11211ef7d21aSLen Brown return 0;
11221ef7d21aSLen Brown
11233f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) {
1124f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */
1125f4fdf2b4SLen Brown struct timeval tv;
1126f4fdf2b4SLen Brown
1127f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv);
1128f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
1129f4fdf2b4SLen Brown }
1130f4fdf2b4SLen Brown
11313f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
11323f44a5c6SLen Brown if (DO_BIC(BIC_TOD))
11333f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
11343f44a5c6SLen Brown
1135d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0;
1136103a8feaSLen Brown
1137008d396eSLen Brown tsc = t->tsc * tsc_tweak;
1138008d396eSLen Brown
1139c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */
1140c98d5d94SLen Brown if (t == &average.threads) {
1141812db3f7SLen Brown if (DO_BIC(BIC_Package))
11426168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11436de68fe1SLen Brown if (DO_BIC(BIC_Die))
11446de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
114501235041SPrarit Bhargava if (DO_BIC(BIC_Node))
114601235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1147812db3f7SLen Brown if (DO_BIC(BIC_Core))
11486168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1149812db3f7SLen Brown if (DO_BIC(BIC_CPU))
11506168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11514c2122d4SLen Brown if (DO_BIC(BIC_APIC))
11524c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11534c2122d4SLen Brown if (DO_BIC(BIC_X2APIC))
11544c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1155103a8feaSLen Brown } else {
1156812db3f7SLen Brown if (DO_BIC(BIC_Package)) {
1157c98d5d94SLen Brown if (p)
11586168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
1159c98d5d94SLen Brown else
11606168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1161c98d5d94SLen Brown }
11626de68fe1SLen Brown if (DO_BIC(BIC_Die)) {
11636de68fe1SLen Brown if (c)
11646de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
11656de68fe1SLen Brown else
11666de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11676de68fe1SLen Brown }
116801235041SPrarit Bhargava if (DO_BIC(BIC_Node)) {
116901235041SPrarit Bhargava if (t)
117001235041SPrarit Bhargava outp += sprintf(outp, "%s%d",
11711b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id);
117201235041SPrarit Bhargava else
11731b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
117401235041SPrarit Bhargava }
1175812db3f7SLen Brown if (DO_BIC(BIC_Core)) {
1176c98d5d94SLen Brown if (c)
11776168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
1178c98d5d94SLen Brown else
11796168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1180c98d5d94SLen Brown }
1181812db3f7SLen Brown if (DO_BIC(BIC_CPU))
11826168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
11834c2122d4SLen Brown if (DO_BIC(BIC_APIC))
11844c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
11854c2122d4SLen Brown if (DO_BIC(BIC_X2APIC))
11864c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
1187103a8feaSLen Brown }
1188fc04cc67SLen Brown
1189812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz))
11901b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float);
1191fc04cc67SLen Brown
1192812db3f7SLen Brown if (DO_BIC(BIC_Busy))
11936168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc);
1194103a8feaSLen Brown
1195812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) {
119621ed5574SLen Brown if (has_base_hz)
11971b439f01SLen Brown outp +=
11981b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
119921ed5574SLen Brown else
12006168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1201008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float);
120221ed5574SLen Brown }
1203103a8feaSLen Brown
1204812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz))
12056168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float);
1206103a8feaSLen Brown
12072af4f9b8SLen Brown if (DO_BIC(BIC_IPC))
12082af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf);
12092af4f9b8SLen Brown
1210562a2d37SLen Brown /* IRQ */
12110de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) {
12120de6c0dfSLen Brown if (sums_need_wide_columns)
12136168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
12140de6c0dfSLen Brown else
12156168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
12160de6c0dfSLen Brown }
1217562a2d37SLen Brown
12181cc21f7bSLen Brown /* SMI */
1219812db3f7SLen Brown if (DO_BIC(BIC_SMI))
12206168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
12211cc21f7bSLen Brown
1222678a3bd1SLen Brown /* Added counters */
1223678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1224678a3bd1SLen Brown if (mp->format == FORMAT_RAW) {
1225678a3bd1SLen Brown if (mp->width == 32)
12261b439f01SLen Brown outp +=
12271b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]);
1228678a3bd1SLen Brown else
12296168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1230678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) {
12310de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
12326168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
12330de6c0dfSLen Brown else
12346168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1235678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) {
123641618e63SLen Brown if (mp->type == COUNTER_USEC)
12371b439f01SLen Brown outp +=
12381b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
12391b439f01SLen Brown t->counter[i] / interval_float / 10000);
124041618e63SLen Brown else
12416168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc);
1242678a3bd1SLen Brown }
1243678a3bd1SLen Brown }
1244678a3bd1SLen Brown
124541618e63SLen Brown /* C1 */
124641618e63SLen Brown if (DO_BIC(BIC_CPU_c1))
12476168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc);
124841618e63SLen Brown
1249c98d5d94SLen Brown /* print per-core data only for 1st thread in core */
1250c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1251c98d5d94SLen Brown goto done;
1252c98d5d94SLen Brown
1253562855eeSLen Brown if (DO_BIC(BIC_CPU_c3))
12546168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc);
1255812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6))
12566168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc);
1257812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7))
12586168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc);
1259c98d5d94SLen Brown
12600539ba11SLen Brown /* Mod%c6 */
12610539ba11SLen Brown if (DO_BIC(BIC_Mod_c6))
12626168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
12630539ba11SLen Brown
1264812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp))
12656168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1266889facbeSLen Brown
1267eae97e05SChen Yu /* Core throttle count */
1268eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT))
1269eae97e05SChen Yu outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt);
1270eae97e05SChen Yu
1271388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1272388e9c81SLen Brown if (mp->format == FORMAT_RAW) {
1273388e9c81SLen Brown if (mp->width == 32)
12741b439f01SLen Brown outp +=
12751b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]);
1276388e9c81SLen Brown else
12776168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1278388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) {
12790de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
12806168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
12810de6c0dfSLen Brown else
12826168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1283388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) {
12846168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc);
1285388e9c81SLen Brown }
1286388e9c81SLen Brown }
1287388e9c81SLen Brown
12889392bd98SCalvin Walton fmt8 = "%s%.2f";
12899392bd98SCalvin Walton
12909392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
12911b439f01SLen Brown outp +=
12921b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
12939392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
12949392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
12959392bd98SCalvin Walton
1296c98d5d94SLen Brown /* print per-package data only for 1st core in package */
1297c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1298c98d5d94SLen Brown goto done;
1299c98d5d94SLen Brown
13000b2bb692SLen Brown /* PkgTmp */
1301812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp))
13026168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1303889facbeSLen Brown
1304fdf676e5SLen Brown /* GFXrc6 */
1305812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) {
1306ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
13076168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
13089185e988SLen Brown } else {
13096168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
13109185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float);
13119185e988SLen Brown }
13129185e988SLen Brown }
1313fdf676e5SLen Brown
131427d47356SLen Brown /* GFXMHz */
1315812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz))
13166168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
131727d47356SLen Brown
1318b4b91569SRafael Antognolli /* GFXACTMHz */
1319b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz))
1320b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz);
1321b4b91569SRafael Antognolli
13220b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1323a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0))
13246168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc);
1325a99d8730SLen Brown if (DO_BIC(BIC_Any_c0))
13266168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc);
1327a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0))
13286168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc);
1329a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX))
13306168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc);
13310b2bb692SLen Brown
13320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2))
13336168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc);
13340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
13356168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc);
13360f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6))
13376168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc);
13380f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
13396168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc);
13400f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8))
13416168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc);
13420f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9))
13436168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc);
13440f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10))
13456168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc);
1346889facbeSLen Brown
1347be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI))
13481b439f01SLen Brown outp +=
13491b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1350be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI))
13511b439f01SLen Brown outp +=
13521b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1353be0e54c4SLen Brown
1354812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt))
13551b439f01SLen Brown outp +=
13561b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1357eae97e05SChen Yu
13589392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
13591b439f01SLen Brown outp +=
13601b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1361812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt))
13621b439f01SLen Brown outp +=
13631b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1364812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt))
13651b439f01SLen Brown outp +=
13661b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""),
13671b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float);
1368812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J))
13696168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
13709392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
13716168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1372812db3f7SLen Brown if (DO_BIC(BIC_GFX_J))
13736168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1374812db3f7SLen Brown if (DO_BIC(BIC_RAM_J))
13756168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1376812db3f7SLen Brown if (DO_BIC(BIC_PKG__))
13771b439f01SLen Brown outp +=
13781b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""),
13791b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1380812db3f7SLen Brown if (DO_BIC(BIC_RAM__))
13811b439f01SLen Brown outp +=
13821b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""),
13831b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1384a5c6d65dSLen Brown /* UncMHz */
1385a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ))
1386a5c6d65dSLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->uncore_mhz);
1387812db3f7SLen Brown
1388388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1389388e9c81SLen Brown if (mp->format == FORMAT_RAW) {
1390388e9c81SLen Brown if (mp->width == 32)
13911b439f01SLen Brown outp +=
13921b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]);
1393388e9c81SLen Brown else
13946168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1395388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) {
13960de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
13976168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
13980de6c0dfSLen Brown else
13996168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1400388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) {
14016168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc);
1402388e9c81SLen Brown }
1403388e9c81SLen Brown }
1404388e9c81SLen Brown
1405c98d5d94SLen Brown done:
140694d6ab4bSLen Brown if (*(outp - 1) != '\n')
1407c98d5d94SLen Brown outp += sprintf(outp, "\n");
1408c98d5d94SLen Brown
1409c98d5d94SLen Brown return 0;
1410103a8feaSLen Brown }
1411103a8feaSLen Brown
flush_output_stdout(void)1412b7d8c148SLen Brown void flush_output_stdout(void)
1413103a8feaSLen Brown {
1414b7d8c148SLen Brown FILE *filep;
1415b7d8c148SLen Brown
1416b7d8c148SLen Brown if (outf == stderr)
1417b7d8c148SLen Brown filep = stdout;
1418b7d8c148SLen Brown else
1419b7d8c148SLen Brown filep = outf;
1420b7d8c148SLen Brown
1421b7d8c148SLen Brown fputs(output_buffer, filep);
1422b7d8c148SLen Brown fflush(filep);
1423b7d8c148SLen Brown
1424c98d5d94SLen Brown outp = output_buffer;
1425c98d5d94SLen Brown }
14261b439f01SLen Brown
flush_output_stderr(void)1427b7d8c148SLen Brown void flush_output_stderr(void)
1428c98d5d94SLen Brown {
1429b7d8c148SLen Brown fputs(output_buffer, outf);
1430b7d8c148SLen Brown fflush(outf);
1431c98d5d94SLen Brown outp = output_buffer;
1432c98d5d94SLen Brown }
14331b439f01SLen Brown
format_all_counters(struct thread_data * t,struct core_data * c,struct pkg_data * p)1434c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1435c98d5d94SLen Brown {
1436c7e399f8SZephaniah E. Loss-Cutler-Hull static int count;
1437103a8feaSLen Brown
1438c7e399f8SZephaniah E. Loss-Cutler-Hull if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only)
1439c8ade361SLen Brown print_header("\t");
1440103a8feaSLen Brown
14419d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages);
1442103a8feaSLen Brown
1443c7e399f8SZephaniah E. Loss-Cutler-Hull count++;
1444e23da037SLen Brown
1445e23da037SLen Brown if (summary_only)
1446e23da037SLen Brown return;
1447e23da037SLen Brown
1448c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p);
1449103a8feaSLen Brown }
1450103a8feaSLen Brown
1451889facbeSLen Brown #define DELTA_WRAP32(new, old) \
14527c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1453889facbeSLen Brown
delta_package(struct pkg_data * new,struct pkg_data * old)14541b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old)
1455103a8feaSLen Brown {
1456388e9c81SLen Brown int i;
1457388e9c81SLen Brown struct msr_counter *mp;
14580b2bb692SLen Brown
1459a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0))
14600b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1461a99d8730SLen Brown if (DO_BIC(BIC_Any_c0))
14620b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1463a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0))
14640b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1465a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX))
14660b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1467a99d8730SLen Brown
1468c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2;
14690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
1470c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3;
14710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6))
1472c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6;
14730f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
1474c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7;
1475ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8;
1476ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9;
1477ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10;
1478be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1479be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi;
1480889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c;
1481889facbeSLen Brown
14829185e988SLen Brown /* flag an error when rc6 counter resets/wraps */
14839185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms)
14849185e988SLen Brown old->gfx_rc6_ms = -1;
14859185e988SLen Brown else
1486fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
14879185e988SLen Brown
1488a5c6d65dSLen Brown old->uncore_mhz = new->uncore_mhz;
148927d47356SLen Brown old->gfx_mhz = new->gfx_mhz;
1490b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz;
149127d47356SLen Brown
149287e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg;
149387e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores;
149487e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx;
149587e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram;
149687e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status;
149787e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status;
1498ba3dec99SLen Brown
1499388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1500388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1501388e9c81SLen Brown old->counter[i] = new->counter[i];
1502388e9c81SLen Brown else
1503388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i];
1504388e9c81SLen Brown }
1505388e9c81SLen Brown
1506ba3dec99SLen Brown return 0;
1507103a8feaSLen Brown }
1508103a8feaSLen Brown
delta_core(struct core_data * new,struct core_data * old)15091b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old)
1510c98d5d94SLen Brown {
1511388e9c81SLen Brown int i;
1512388e9c81SLen Brown struct msr_counter *mp;
1513388e9c81SLen Brown
1514c98d5d94SLen Brown old->c3 = new->c3 - old->c3;
1515c98d5d94SLen Brown old->c6 = new->c6 - old->c6;
1516c98d5d94SLen Brown old->c7 = new->c7 - old->c7;
1517889facbeSLen Brown old->core_temp_c = new->core_temp_c;
1518eae97e05SChen Yu old->core_throt_cnt = new->core_throt_cnt;
15190539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us;
1520388e9c81SLen Brown
15219392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy);
15229392bd98SCalvin Walton
1523388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1524388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1525388e9c81SLen Brown old->counter[i] = new->counter[i];
1526388e9c81SLen Brown else
1527388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i];
1528388e9c81SLen Brown }
1529103a8feaSLen Brown }
1530c98d5d94SLen Brown
soft_c1_residency_display(int bic)15311e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic)
15321e9042b9SSrinivas Pandruvada {
15331e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
15341e9042b9SSrinivas Pandruvada return 0;
15351e9042b9SSrinivas Pandruvada
15361e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic);
15371e9042b9SSrinivas Pandruvada }
15381e9042b9SSrinivas Pandruvada
1539c3ae331dSLen Brown /*
1540c3ae331dSLen Brown * old = new - old
1541c3ae331dSLen Brown */
delta_thread(struct thread_data * new,struct thread_data * old,struct core_data * core_delta)15421b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta)
1543c98d5d94SLen Brown {
1544388e9c81SLen Brown int i;
1545388e9c81SLen Brown struct msr_counter *mp;
1546388e9c81SLen Brown
15474c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */
15484c2122d4SLen Brown if (DO_BIC(BIC_APIC))
15494c2122d4SLen Brown new->apic_id = old->apic_id;
15504c2122d4SLen Brown if (DO_BIC(BIC_X2APIC))
15514c2122d4SLen Brown new->x2apic_id = old->x2apic_id;
15524c2122d4SLen Brown
15533f44a5c6SLen Brown /*
15543f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old"
15553f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new"
15563f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values
15573f44a5c6SLen Brown */
15583f44a5c6SLen Brown
1559d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
15603f44a5c6SLen Brown old->tv_begin = new->tv_begin;
15613f44a5c6SLen Brown old->tv_end = new->tv_end;
15623f44a5c6SLen Brown
1563c98d5d94SLen Brown old->tsc = new->tsc - old->tsc;
1564c98d5d94SLen Brown
1565103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */
1566b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000))
1567b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1568b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n"
1569b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\"");
1570103a8feaSLen Brown
1571c98d5d94SLen Brown old->c1 = new->c1 - old->c1;
1572c98d5d94SLen Brown
15731b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
1574c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1575c98d5d94SLen Brown old->aperf = new->aperf - old->aperf;
1576c98d5d94SLen Brown old->mperf = new->mperf - old->mperf;
1577c98d5d94SLen Brown } else {
1578ba3dec99SLen Brown return -1;
1579103a8feaSLen Brown }
1580a729617cSLen Brown }
1581103a8feaSLen Brown
1582144b44b1SLen Brown if (use_c1_residency_msr) {
1583144b44b1SLen Brown /*
1584144b44b1SLen Brown * Some models have a dedicated C1 residency MSR,
1585144b44b1SLen Brown * which should be more accurate than the derivation below.
1586144b44b1SLen Brown */
1587144b44b1SLen Brown } else {
1588103a8feaSLen Brown /*
1589c3ae331dSLen Brown * As counter collection is not atomic,
1590c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states
1591103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case.
1592103a8feaSLen Brown */
159395149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1594c98d5d94SLen Brown old->c1 = 0;
1595c98d5d94SLen Brown else {
1596c98d5d94SLen Brown /* normal case, derive c1 */
1597008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1598c98d5d94SLen Brown - core_delta->c6 - core_delta->c7;
1599c98d5d94SLen Brown }
1600144b44b1SLen Brown }
1601c3ae331dSLen Brown
1602c98d5d94SLen Brown if (old->mperf == 0) {
1603b7d8c148SLen Brown if (debug > 1)
1604b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1605c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */
1606c98d5d94SLen Brown }
1607103a8feaSLen Brown
16082af4f9b8SLen Brown if (DO_BIC(BIC_IPC))
16092af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count;
16102af4f9b8SLen Brown
1611812db3f7SLen Brown if (DO_BIC(BIC_IRQ))
1612562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count;
1613562a2d37SLen Brown
1614812db3f7SLen Brown if (DO_BIC(BIC_SMI))
16151ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count;
1616ba3dec99SLen Brown
1617388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1618388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1619388e9c81SLen Brown old->counter[i] = new->counter[i];
1620388e9c81SLen Brown else
1621388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i];
1622388e9c81SLen Brown }
1623ba3dec99SLen Brown return 0;
1624103a8feaSLen Brown }
1625c98d5d94SLen Brown
delta_cpu(struct thread_data * t,struct core_data * c,struct pkg_data * p,struct thread_data * t2,struct core_data * c2,struct pkg_data * p2)1626c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
16271b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2)
1628c98d5d94SLen Brown {
1629ba3dec99SLen Brown int retval = 0;
1630ba3dec99SLen Brown
1631c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */
1632c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1633c98d5d94SLen Brown delta_core(c, c2);
1634c98d5d94SLen Brown
1635c98d5d94SLen Brown /* always calculate thread delta */
1636ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */
1637ba3dec99SLen Brown if (retval)
1638ba3dec99SLen Brown return retval;
1639c98d5d94SLen Brown
1640c98d5d94SLen Brown /* calculate package delta only for 1st core in package */
1641c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1642ba3dec99SLen Brown retval = delta_package(p, p2);
1643c98d5d94SLen Brown
1644ba3dec99SLen Brown return retval;
1645103a8feaSLen Brown }
1646103a8feaSLen Brown
clear_counters(struct thread_data * t,struct core_data * c,struct pkg_data * p)1647c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1648103a8feaSLen Brown {
1649388e9c81SLen Brown int i;
1650388e9c81SLen Brown struct msr_counter *mp;
1651388e9c81SLen Brown
16523f44a5c6SLen Brown t->tv_begin.tv_sec = 0;
16533f44a5c6SLen Brown t->tv_begin.tv_usec = 0;
16543f44a5c6SLen Brown t->tv_end.tv_sec = 0;
16553f44a5c6SLen Brown t->tv_end.tv_usec = 0;
1656d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0;
1657d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0;
16583f44a5c6SLen Brown
1659c98d5d94SLen Brown t->tsc = 0;
1660c98d5d94SLen Brown t->aperf = 0;
1661c98d5d94SLen Brown t->mperf = 0;
1662c98d5d94SLen Brown t->c1 = 0;
1663103a8feaSLen Brown
16642af4f9b8SLen Brown t->instr_count = 0;
16652af4f9b8SLen Brown
1666562a2d37SLen Brown t->irq_count = 0;
1667562a2d37SLen Brown t->smi_count = 0;
1668562a2d37SLen Brown
1669c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */
1670c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1671c98d5d94SLen Brown
1672c98d5d94SLen Brown c->c3 = 0;
1673c98d5d94SLen Brown c->c6 = 0;
1674c98d5d94SLen Brown c->c7 = 0;
16750539ba11SLen Brown c->mc6_us = 0;
1676889facbeSLen Brown c->core_temp_c = 0;
16779392bd98SCalvin Walton c->core_energy = 0;
1678eae97e05SChen Yu c->core_throt_cnt = 0;
1679c98d5d94SLen Brown
16800b2bb692SLen Brown p->pkg_wtd_core_c0 = 0;
16810b2bb692SLen Brown p->pkg_any_core_c0 = 0;
16820b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0;
16830b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0;
16840b2bb692SLen Brown
1685c98d5d94SLen Brown p->pc2 = 0;
16860f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
1687c98d5d94SLen Brown p->pc3 = 0;
16880f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6))
1689c98d5d94SLen Brown p->pc6 = 0;
16900f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
1691c98d5d94SLen Brown p->pc7 = 0;
1692ca58710fSKristen Carlson Accardi p->pc8 = 0;
1693ca58710fSKristen Carlson Accardi p->pc9 = 0;
1694ca58710fSKristen Carlson Accardi p->pc10 = 0;
1695be0e54c4SLen Brown p->cpu_lpi = 0;
1696be0e54c4SLen Brown p->sys_lpi = 0;
1697889facbeSLen Brown
1698889facbeSLen Brown p->energy_pkg = 0;
1699889facbeSLen Brown p->energy_dram = 0;
1700889facbeSLen Brown p->energy_cores = 0;
1701889facbeSLen Brown p->energy_gfx = 0;
1702889facbeSLen Brown p->rapl_pkg_perf_status = 0;
1703889facbeSLen Brown p->rapl_dram_perf_status = 0;
1704889facbeSLen Brown p->pkg_temp_c = 0;
170527d47356SLen Brown
1706fdf676e5SLen Brown p->gfx_rc6_ms = 0;
1707a5c6d65dSLen Brown p->uncore_mhz = 0;
170827d47356SLen Brown p->gfx_mhz = 0;
1709b4b91569SRafael Antognolli p->gfx_act_mhz = 0;
1710388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1711388e9c81SLen Brown t->counter[i] = 0;
1712388e9c81SLen Brown
1713388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1714388e9c81SLen Brown c->counter[i] = 0;
1715388e9c81SLen Brown
1716388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1717388e9c81SLen Brown p->counter[i] = 0;
1718103a8feaSLen Brown }
17191b439f01SLen Brown
sum_counters(struct thread_data * t,struct core_data * c,struct pkg_data * p)17201b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1721103a8feaSLen Brown {
1722388e9c81SLen Brown int i;
1723388e9c81SLen Brown struct msr_counter *mp;
1724388e9c81SLen Brown
17254c2122d4SLen Brown /* copy un-changing apic_id's */
17264c2122d4SLen Brown if (DO_BIC(BIC_APIC))
17274c2122d4SLen Brown average.threads.apic_id = t->apic_id;
17284c2122d4SLen Brown if (DO_BIC(BIC_X2APIC))
17294c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id;
17304c2122d4SLen Brown
17313f44a5c6SLen Brown /* remember first tv_begin */
17323f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0)
17333f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin;
17343f44a5c6SLen Brown
17353f44a5c6SLen Brown /* remember last tv_end */
17363f44a5c6SLen Brown average.threads.tv_end = t->tv_end;
17373f44a5c6SLen Brown
1738c98d5d94SLen Brown average.threads.tsc += t->tsc;
1739c98d5d94SLen Brown average.threads.aperf += t->aperf;
1740c98d5d94SLen Brown average.threads.mperf += t->mperf;
1741c98d5d94SLen Brown average.threads.c1 += t->c1;
174215aaa346SLen Brown
17432af4f9b8SLen Brown average.threads.instr_count += t->instr_count;
17442af4f9b8SLen Brown
1745562a2d37SLen Brown average.threads.irq_count += t->irq_count;
1746562a2d37SLen Brown average.threads.smi_count += t->smi_count;
1747562a2d37SLen Brown
1748388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1749388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1750388e9c81SLen Brown continue;
1751388e9c81SLen Brown average.threads.counter[i] += t->counter[i];
1752388e9c81SLen Brown }
1753388e9c81SLen Brown
1754c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */
1755c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1756c98d5d94SLen Brown return 0;
1757c98d5d94SLen Brown
1758c98d5d94SLen Brown average.cores.c3 += c->c3;
1759c98d5d94SLen Brown average.cores.c6 += c->c6;
1760c98d5d94SLen Brown average.cores.c7 += c->c7;
17610539ba11SLen Brown average.cores.mc6_us += c->mc6_us;
1762c98d5d94SLen Brown
1763889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1764eae97e05SChen Yu average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt);
1765889facbeSLen Brown
17669392bd98SCalvin Walton average.cores.core_energy += c->core_energy;
17679392bd98SCalvin Walton
1768388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1769388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1770388e9c81SLen Brown continue;
1771388e9c81SLen Brown average.cores.counter[i] += c->counter[i];
1772388e9c81SLen Brown }
1773388e9c81SLen Brown
1774c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */
1775c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1776c98d5d94SLen Brown return 0;
1777c98d5d94SLen Brown
1778a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0))
17790b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1780a99d8730SLen Brown if (DO_BIC(BIC_Any_c0))
17810b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1782a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0))
17830b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1784a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX))
17850b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
17860b2bb692SLen Brown
1787c98d5d94SLen Brown average.packages.pc2 += p->pc2;
17880f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
1789c98d5d94SLen Brown average.packages.pc3 += p->pc3;
17900f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6))
1791c98d5d94SLen Brown average.packages.pc6 += p->pc6;
17920f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
1793c98d5d94SLen Brown average.packages.pc7 += p->pc7;
1794ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8;
1795ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9;
1796ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10;
1797c98d5d94SLen Brown
1798be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi;
1799be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi;
1800be0e54c4SLen Brown
1801889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg;
1802889facbeSLen Brown average.packages.energy_dram += p->energy_dram;
1803889facbeSLen Brown average.packages.energy_cores += p->energy_cores;
1804889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx;
1805889facbeSLen Brown
1806fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1807a5c6d65dSLen Brown average.packages.uncore_mhz = p->uncore_mhz;
180827d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz;
1809b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz;
181027d47356SLen Brown
1811889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1812889facbeSLen Brown
1813889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1814889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1815388e9c81SLen Brown
1816388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
181795973a9bSDoug Smythies if ((mp->format == FORMAT_RAW) && (topo.num_packages == 0))
181895973a9bSDoug Smythies average.packages.counter[i] = p->counter[i];
181995973a9bSDoug Smythies else
1820388e9c81SLen Brown average.packages.counter[i] += p->counter[i];
1821388e9c81SLen Brown }
1822c98d5d94SLen Brown return 0;
1823c98d5d94SLen Brown }
18241b439f01SLen Brown
1825c98d5d94SLen Brown /*
1826c98d5d94SLen Brown * sum the counters for all cpus in the system
1827c98d5d94SLen Brown * compute the weighted average
1828c98d5d94SLen Brown */
compute_average(struct thread_data * t,struct core_data * c,struct pkg_data * p)18291b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1830c98d5d94SLen Brown {
1831388e9c81SLen Brown int i;
1832388e9c81SLen Brown struct msr_counter *mp;
1833388e9c81SLen Brown
1834c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages);
1835c98d5d94SLen Brown
1836c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p);
1837c98d5d94SLen Brown
1838d4794f25SYazen Ghannam /* Use the global time delta for the average. */
1839d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta;
1840d4794f25SYazen Ghannam
1841c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus;
1842c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus;
1843c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus;
18442af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus;
1845c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus;
1846c98d5d94SLen Brown
18470de6c0dfSLen Brown if (average.threads.irq_count > 9999999)
18480de6c0dfSLen Brown sums_need_wide_columns = 1;
18490de6c0dfSLen Brown
1850c98d5d94SLen Brown average.cores.c3 /= topo.num_cores;
1851c98d5d94SLen Brown average.cores.c6 /= topo.num_cores;
1852c98d5d94SLen Brown average.cores.c7 /= topo.num_cores;
18530539ba11SLen Brown average.cores.mc6_us /= topo.num_cores;
1854c98d5d94SLen Brown
1855a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0))
18560b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1857a99d8730SLen Brown if (DO_BIC(BIC_Any_c0))
18580b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages;
1859a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0))
18600b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1861a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX))
18620b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
18630b2bb692SLen Brown
1864c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages;
18650f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
1866c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages;
18670f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6))
1868c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages;
18690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
1870c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages;
1871ca58710fSKristen Carlson Accardi
1872ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages;
1873ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages;
1874ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages;
1875388e9c81SLen Brown
1876388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1877388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1878388e9c81SLen Brown continue;
18790de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) {
18800de6c0dfSLen Brown if (average.threads.counter[i] > 9999999)
18810de6c0dfSLen Brown sums_need_wide_columns = 1;
188241618e63SLen Brown continue;
18830de6c0dfSLen Brown }
1884388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus;
1885388e9c81SLen Brown }
1886388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1887388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1888388e9c81SLen Brown continue;
18890de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) {
18900de6c0dfSLen Brown if (average.cores.counter[i] > 9999999)
18910de6c0dfSLen Brown sums_need_wide_columns = 1;
18920de6c0dfSLen Brown }
1893388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores;
1894388e9c81SLen Brown }
1895388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1896388e9c81SLen Brown if (mp->format == FORMAT_RAW)
1897388e9c81SLen Brown continue;
18980de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) {
18990de6c0dfSLen Brown if (average.packages.counter[i] > 9999999)
19000de6c0dfSLen Brown sums_need_wide_columns = 1;
19010de6c0dfSLen Brown }
1902388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages;
1903388e9c81SLen Brown }
1904c98d5d94SLen Brown }
1905c98d5d94SLen Brown
rdtsc(void)1906c98d5d94SLen Brown static unsigned long long rdtsc(void)
1907c98d5d94SLen Brown {
1908c98d5d94SLen Brown unsigned int low, high;
1909c98d5d94SLen Brown
1910c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high));
1911c98d5d94SLen Brown
1912c98d5d94SLen Brown return low | ((unsigned long long)high) << 32;
1913c98d5d94SLen Brown }
1914c98d5d94SLen Brown
1915c98d5d94SLen Brown /*
1916495c7654SLen Brown * Open a file, and exit on failure
1917495c7654SLen Brown */
fopen_or_die(const char * path,const char * mode)1918495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1919495c7654SLen Brown {
1920495c7654SLen Brown FILE *filep = fopen(path, mode);
1921495c7654SLen Brown
1922495c7654SLen Brown if (!filep)
1923495c7654SLen Brown err(1, "%s: open failed", path);
1924495c7654SLen Brown return filep;
1925495c7654SLen Brown }
19261b439f01SLen Brown
1927495c7654SLen Brown /*
1928495c7654SLen Brown * snapshot_sysfs_counter()
1929495c7654SLen Brown *
1930495c7654SLen Brown * return snapshot of given counter
1931495c7654SLen Brown */
snapshot_sysfs_counter(char * path)1932495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1933495c7654SLen Brown {
1934495c7654SLen Brown FILE *fp;
1935495c7654SLen Brown int retval;
1936495c7654SLen Brown unsigned long long counter;
1937495c7654SLen Brown
1938495c7654SLen Brown fp = fopen_or_die(path, "r");
1939495c7654SLen Brown
1940495c7654SLen Brown retval = fscanf(fp, "%lld", &counter);
1941495c7654SLen Brown if (retval != 1)
1942495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path);
1943495c7654SLen Brown
1944495c7654SLen Brown fclose(fp);
1945495c7654SLen Brown
1946495c7654SLen Brown return counter;
1947495c7654SLen Brown }
1948495c7654SLen Brown
get_mp(int cpu,struct msr_counter * mp,unsigned long long * counterp)1949495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1950495c7654SLen Brown {
1951495c7654SLen Brown if (mp->msr_num != 0) {
1952495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp))
1953495c7654SLen Brown return -1;
1954495c7654SLen Brown } else {
195546c27978SLen Brown char path[128 + PATH_BYTES];
195641618e63SLen Brown
195741618e63SLen Brown if (mp->flags & SYSFS_PERCPU) {
19581b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path);
195941618e63SLen Brown
196041618e63SLen Brown *counterp = snapshot_sysfs_counter(path);
196141618e63SLen Brown } else {
1962495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path);
1963495c7654SLen Brown }
196441618e63SLen Brown }
1965495c7654SLen Brown
1966495c7654SLen Brown return 0;
1967495c7654SLen Brown }
1968495c7654SLen Brown
get_uncore_mhz(int package,int die)1969a5c6d65dSLen Brown unsigned long long get_uncore_mhz(int package, int die)
1970a5c6d65dSLen Brown {
1971a5c6d65dSLen Brown char path[128];
1972a5c6d65dSLen Brown
1973037e72e7SJustin Ernst sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_%02d_die_%02d/current_freq_khz", package,
1974a5c6d65dSLen Brown die);
1975a5c6d65dSLen Brown
1976a5c6d65dSLen Brown return (snapshot_sysfs_counter(path) / 1000);
1977a5c6d65dSLen Brown }
1978a5c6d65dSLen Brown
get_epb(int cpu)19796d6501d9SBorislav Petkov int get_epb(int cpu)
19806d6501d9SBorislav Petkov {
19816d6501d9SBorislav Petkov char path[128 + PATH_BYTES];
19827f1b11baSBorislav Petkov unsigned long long msr;
19836d6501d9SBorislav Petkov int ret, epb = -1;
19846d6501d9SBorislav Petkov FILE *fp;
19856d6501d9SBorislav Petkov
19866d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu);
19876d6501d9SBorislav Petkov
19887f1b11baSBorislav Petkov fp = fopen(path, "r");
19897f1b11baSBorislav Petkov if (!fp)
19907f1b11baSBorislav Petkov goto msr_fallback;
19916d6501d9SBorislav Petkov
19926d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb);
19936d6501d9SBorislav Petkov if (ret != 1)
19946d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path);
19956d6501d9SBorislav Petkov
19966d6501d9SBorislav Petkov fclose(fp);
19976d6501d9SBorislav Petkov
19986d6501d9SBorislav Petkov return epb;
19997f1b11baSBorislav Petkov
20007f1b11baSBorislav Petkov msr_fallback:
20017f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr);
20027f1b11baSBorislav Petkov
20037f1b11baSBorislav Petkov return msr & 0xf;
20046d6501d9SBorislav Petkov }
20056d6501d9SBorislav Petkov
get_apic_id(struct thread_data * t)20064c2122d4SLen Brown void get_apic_id(struct thread_data *t)
20074c2122d4SLen Brown {
200834041551SLen Brown unsigned int eax, ebx, ecx, edx;
200934041551SLen Brown
201034041551SLen Brown if (DO_BIC(BIC_APIC)) {
201134041551SLen Brown eax = ebx = ecx = edx = 0;
201234041551SLen Brown __cpuid(1, eax, ebx, ecx, edx);
201334041551SLen Brown
201434041551SLen Brown t->apic_id = (ebx >> 24) & 0xff;
201534041551SLen Brown }
201634041551SLen Brown
201734041551SLen Brown if (!DO_BIC(BIC_X2APIC))
201834041551SLen Brown return;
201934041551SLen Brown
2020c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) {
202134041551SLen Brown unsigned int topology_extensions;
202234041551SLen Brown
202334041551SLen Brown if (max_extended_level < 0x8000001e)
202434041551SLen Brown return;
20254c2122d4SLen Brown
20264c2122d4SLen Brown eax = ebx = ecx = edx = 0;
202734041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx);
202834041551SLen Brown topology_extensions = ecx & (1 << 22);
202934041551SLen Brown
203034041551SLen Brown if (topology_extensions == 0)
203134041551SLen Brown return;
203234041551SLen Brown
203334041551SLen Brown eax = ebx = ecx = edx = 0;
203434041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx);
203534041551SLen Brown
203634041551SLen Brown t->x2apic_id = eax;
203734041551SLen Brown return;
203834041551SLen Brown }
20394c2122d4SLen Brown
20404c2122d4SLen Brown if (!genuine_intel)
20414c2122d4SLen Brown return;
20424c2122d4SLen Brown
20434c2122d4SLen Brown if (max_level < 0xb)
20444c2122d4SLen Brown return;
20454c2122d4SLen Brown
20464c2122d4SLen Brown ecx = 0;
20474c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx);
20484c2122d4SLen Brown t->x2apic_id = edx;
20494c2122d4SLen Brown
205034041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
20511b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
20524c2122d4SLen Brown }
20534c2122d4SLen Brown
get_core_throt_cnt(int cpu,unsigned long long * cnt)2054eae97e05SChen Yu int get_core_throt_cnt(int cpu, unsigned long long *cnt)
2055eae97e05SChen Yu {
2056eae97e05SChen Yu char path[128 + PATH_BYTES];
2057eae97e05SChen Yu unsigned long long tmp;
2058eae97e05SChen Yu FILE *fp;
2059eae97e05SChen Yu int ret;
2060eae97e05SChen Yu
2061eae97e05SChen Yu sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu);
2062eae97e05SChen Yu fp = fopen(path, "r");
2063eae97e05SChen Yu if (!fp)
2064eae97e05SChen Yu return -1;
2065eae97e05SChen Yu ret = fscanf(fp, "%lld", &tmp);
20665e5fd36cSColin Ian King fclose(fp);
2067eae97e05SChen Yu if (ret != 1)
2068eae97e05SChen Yu return -1;
2069eae97e05SChen Yu *cnt = tmp;
2070eae97e05SChen Yu
2071eae97e05SChen Yu return 0;
2072eae97e05SChen Yu }
2073eae97e05SChen Yu
2074495c7654SLen Brown /*
2075c98d5d94SLen Brown * get_counters(...)
2076c98d5d94SLen Brown * migrate to cpu
2077c98d5d94SLen Brown * acquire and record local counters for that cpu
2078c98d5d94SLen Brown */
get_counters(struct thread_data * t,struct core_data * c,struct pkg_data * p)2079c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2080c98d5d94SLen Brown {
2081c98d5d94SLen Brown int cpu = t->cpu_id;
2082889facbeSLen Brown unsigned long long msr;
20830102b067SLen Brown int aperf_mperf_retry_count = 0;
2084388e9c81SLen Brown struct msr_counter *mp;
2085388e9c81SLen Brown int i;
2086c98d5d94SLen Brown
2087e52966c0SLen Brown if (cpu_migrate(cpu)) {
20883d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu);
208915aaa346SLen Brown return -1;
2090e52966c0SLen Brown }
209115aaa346SLen Brown
2092d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL);
2093d4794f25SYazen Ghannam
20944c2122d4SLen Brown if (first_counter_read)
20954c2122d4SLen Brown get_apic_id(t);
20960102b067SLen Brown retry:
2097c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */
209815aaa346SLen Brown
20991b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
21000102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
21010102b067SLen Brown
21020102b067SLen Brown /*
21030102b067SLen Brown * The TSC, APERF and MPERF must be read together for
21040102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results.
21050102b067SLen Brown *
21060102b067SLen Brown * Unfortunately, APERF and MPERF are read by
21070102b067SLen Brown * individual system call, so delays may occur
21080102b067SLen Brown * between them. If the time to read them
21090102b067SLen Brown * varies by a large amount, we re-read them.
21100102b067SLen Brown */
21110102b067SLen Brown
21120102b067SLen Brown /*
21130102b067SLen Brown * This initial dummy APERF read has been seen to
21140102b067SLen Brown * reduce jitter in the subsequent reads.
21150102b067SLen Brown */
21160102b067SLen Brown
21179c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2118c98d5d94SLen Brown return -3;
21190102b067SLen Brown
21200102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */
21210102b067SLen Brown
21220102b067SLen Brown tsc_before = t->tsc;
21230102b067SLen Brown
21240102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
21250102b067SLen Brown return -3;
21260102b067SLen Brown
21270102b067SLen Brown tsc_between = rdtsc();
21280102b067SLen Brown
21299c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
2130c98d5d94SLen Brown return -4;
21310102b067SLen Brown
21320102b067SLen Brown tsc_after = rdtsc();
21330102b067SLen Brown
21340102b067SLen Brown aperf_time = tsc_between - tsc_before;
21350102b067SLen Brown mperf_time = tsc_after - tsc_between;
21360102b067SLen Brown
21370102b067SLen Brown /*
21380102b067SLen Brown * If the system call latency to read APERF and MPERF
21390102b067SLen Brown * differ by more than 2x, then try again.
21400102b067SLen Brown */
21410102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
21420102b067SLen Brown aperf_mperf_retry_count++;
21430102b067SLen Brown if (aperf_mperf_retry_count < 5)
21440102b067SLen Brown goto retry;
21450102b067SLen Brown else
21461b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time);
21470102b067SLen Brown }
21480102b067SLen Brown aperf_mperf_retry_count = 0;
21490102b067SLen Brown
2150b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier;
2151b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier;
215288c3281fSLen Brown }
215388c3281fSLen Brown
21542af4f9b8SLen Brown if (DO_BIC(BIC_IPC))
21552af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long))
21562af4f9b8SLen Brown return -4;
21572af4f9b8SLen Brown
2158812db3f7SLen Brown if (DO_BIC(BIC_IRQ))
2159562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu];
2160812db3f7SLen Brown if (DO_BIC(BIC_SMI)) {
21611ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr))
21621ed51011SLen Brown return -5;
21631ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF;
21641ed51011SLen Brown }
21650539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
2166144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
2167144b44b1SLen Brown return -6;
2168144b44b1SLen Brown }
2169144b44b1SLen Brown
2170388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2171495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i]))
2172388e9c81SLen Brown return -10;
2173388e9c81SLen Brown }
2174388e9c81SLen Brown
2175c98d5d94SLen Brown /* collect core counters only for 1st thread in core */
2176c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2177f4fdf2b4SLen Brown goto done;
2178c98d5d94SLen Brown
21791e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
2180c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
2181c98d5d94SLen Brown return -6;
2182144b44b1SLen Brown }
2183144b44b1SLen Brown
21841e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
2185c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
2186c98d5d94SLen Brown return -7;
2187b866371fSZhang Rui } else if (do_knl_cstates && soft_c1_residency_display(BIC_CPU_c6)) {
2188fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
2189fb5d4327SDasaratharaman Chandramouli return -7;
2190103a8feaSLen Brown }
219115aaa346SLen Brown
21927ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) {
2193c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
2194c98d5d94SLen Brown return -8;
21957ab5ff49SZhang Rui else if (t->is_atom) {
21967ab5ff49SZhang Rui /*
21977ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6,
21987ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper.
21997ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get
22007ab5ff49SZhang Rui * accturate cc6 residency.
22017ab5ff49SZhang Rui */
22027ab5ff49SZhang Rui c->c6 -= c->c7;
22037ab5ff49SZhang Rui }
22047ab5ff49SZhang Rui }
2205c98d5d94SLen Brown
22060539ba11SLen Brown if (DO_BIC(BIC_Mod_c6))
22070539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
22080539ba11SLen Brown return -8;
22090539ba11SLen Brown
2210812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) {
2211889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2212889facbeSLen Brown return -9;
221355279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F);
2214889facbeSLen Brown }
2215889facbeSLen Brown
2216eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT))
2217eae97e05SChen Yu get_core_throt_cnt(cpu, &c->core_throt_cnt);
2218eae97e05SChen Yu
22199392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) {
22209392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
22219392bd98SCalvin Walton return -14;
22229392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF;
22239392bd98SCalvin Walton }
22249392bd98SCalvin Walton
2225388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2226495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i]))
2227388e9c81SLen Brown return -10;
2228388e9c81SLen Brown }
2229889facbeSLen Brown
2230c98d5d94SLen Brown /* collect package counters only for 1st core in package */
2231c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2232f4fdf2b4SLen Brown goto done;
223315aaa346SLen Brown
2234a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) {
22350b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
22360b2bb692SLen Brown return -10;
2237a99d8730SLen Brown }
2238a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) {
22390b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
22400b2bb692SLen Brown return -11;
2241a99d8730SLen Brown }
2242a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) {
22430b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
22440b2bb692SLen Brown return -12;
2245a99d8730SLen Brown }
2246a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) {
22470b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
22480b2bb692SLen Brown return -13;
22490b2bb692SLen Brown }
22500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3))
2251c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
2252c98d5d94SLen Brown return -9;
22530f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) {
22540539ba11SLen Brown if (do_slm_cstates) {
22550539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
22560539ba11SLen Brown return -10;
22570539ba11SLen Brown } else {
2258c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
2259c98d5d94SLen Brown return -10;
22600539ba11SLen Brown }
22610539ba11SLen Brown }
22620539ba11SLen Brown
22630f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2))
2264c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
2265c98d5d94SLen Brown return -11;
22660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7))
2267c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
2268c98d5d94SLen Brown return -12;
22690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8))
2270ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
2271ca58710fSKristen Carlson Accardi return -13;
22720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9))
2273ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
2274ca58710fSKristen Carlson Accardi return -13;
22750f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10))
2276ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
2277ca58710fSKristen Carlson Accardi return -13;
22780f47c08dSLen Brown
2279be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI))
2280be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
2281be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI))
2282be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us;
2283be0e54c4SLen Brown
2284889facbeSLen Brown if (do_rapl & RAPL_PKG) {
22859972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr))
2286889facbeSLen Brown return -13;
22879972d5d8SChen Yu p->energy_pkg = msr;
2288889facbeSLen Brown }
22899148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
22909972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr))
2291889facbeSLen Brown return -14;
22929972d5d8SChen Yu p->energy_cores = msr;
2293889facbeSLen Brown }
2294889facbeSLen Brown if (do_rapl & RAPL_DRAM) {
22959972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
2296889facbeSLen Brown return -15;
22979972d5d8SChen Yu p->energy_dram = msr;
2298889facbeSLen Brown }
2299889facbeSLen Brown if (do_rapl & RAPL_GFX) {
23009972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr))
2301889facbeSLen Brown return -16;
23029972d5d8SChen Yu p->energy_gfx = msr;
2303889facbeSLen Brown }
2304889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) {
23059972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr))
2306889facbeSLen Brown return -16;
23079972d5d8SChen Yu p->rapl_pkg_perf_status = msr;
2308889facbeSLen Brown }
2309889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) {
23109972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr))
2311889facbeSLen Brown return -16;
23129972d5d8SChen Yu p->rapl_dram_perf_status = msr;
2313889facbeSLen Brown }
23143316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) {
23159972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr))
23163316f99aSCalvin Walton return -13;
23179972d5d8SChen Yu p->energy_pkg = msr;
23183316f99aSCalvin Walton }
2319812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) {
2320889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2321889facbeSLen Brown return -17;
232255279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F);
2323889facbeSLen Brown }
2324fdf676e5SLen Brown
2325812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6))
2326fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms;
2327fdf676e5SLen Brown
2328a5c6d65dSLen Brown /* n.b. assume die0 uncore frequency applies to whole package */
2329a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ))
2330a5c6d65dSLen Brown p->uncore_mhz = get_uncore_mhz(p->package_id, 0);
2331a5c6d65dSLen Brown
2332812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz))
233327d47356SLen Brown p->gfx_mhz = gfx_cur_mhz;
233427d47356SLen Brown
2335b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz))
2336b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz;
2337b4b91569SRafael Antognolli
2338388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2339495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i]))
2340388e9c81SLen Brown return -10;
2341388e9c81SLen Brown }
2342f4fdf2b4SLen Brown done:
2343f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL);
2344388e9c81SLen Brown
234515aaa346SLen Brown return 0;
2346103a8feaSLen Brown }
2347103a8feaSLen Brown
2348ee7e38e3SLen Brown /*
2349ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2350ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons
2351ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2352ee7e38e3SLen Brown */
2353ee7e38e3SLen Brown
2354ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
2355ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
2356ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
2357ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
2358ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
2359ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
2360ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
2361ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
2362ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
2363ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
2364ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
2365ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
23660b2bb692SLen Brown #define PCL__8 12 /* PC8 */
23670b2bb692SLen Brown #define PCL__9 13 /* PC9 */
2368445640a5SLen Brown #define PCL_10 14 /* PC10 */
2369445640a5SLen Brown #define PCLUNL 15 /* Unlimited */
2370ee7e38e3SLen Brown
2371ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
2372ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
23731b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"
23741b439f01SLen Brown };
2375ee7e38e3SLen Brown
23761b439f01SLen Brown int nhm_pkg_cstate_limits[16] =
23771b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
237838c6663aSLen Brown PCLRSV, PCLRSV
237938c6663aSLen Brown };
238038c6663aSLen Brown
23811b439f01SLen Brown int snb_pkg_cstate_limits[16] =
23821b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
238338c6663aSLen Brown PCLRSV, PCLRSV
238438c6663aSLen Brown };
238538c6663aSLen Brown
23861b439f01SLen Brown int hsw_pkg_cstate_limits[16] =
23871b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
238838c6663aSLen Brown PCLRSV, PCLRSV
238938c6663aSLen Brown };
239038c6663aSLen Brown
23911b439f01SLen Brown int slv_pkg_cstate_limits[16] =
23921b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
239338c6663aSLen Brown PCL__6, PCL__7
239438c6663aSLen Brown };
239538c6663aSLen Brown
23961b439f01SLen Brown int amt_pkg_cstate_limits[16] =
23971b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
239838c6663aSLen Brown PCLRSV, PCLRSV
239938c6663aSLen Brown };
240038c6663aSLen Brown
24011b439f01SLen Brown int phi_pkg_cstate_limits[16] =
24021b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
240338c6663aSLen Brown PCLRSV, PCLRSV
240438c6663aSLen Brown };
240538c6663aSLen Brown
24061b439f01SLen Brown int glm_pkg_cstate_limits[16] =
24071b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
240838c6663aSLen Brown PCLRSV, PCLRSV
240938c6663aSLen Brown };
241038c6663aSLen Brown
24111b439f01SLen Brown int skx_pkg_cstate_limits[16] =
24121b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
241338c6663aSLen Brown PCLRSV, PCLRSV
241438c6663aSLen Brown };
241538c6663aSLen Brown
24161b439f01SLen Brown int icx_pkg_cstate_limits[16] =
2417a1b6f487SArtem Bityutskiy { PCL__0, PCL__2, PCL__6, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
241838c6663aSLen Brown PCLRSV, PCLRSV
241938c6663aSLen Brown };
2420a2b7b749SLen Brown
calculate_tsc_tweak()24211b439f01SLen Brown static void calculate_tsc_tweak()
2422a2b7b749SLen Brown {
2423a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz;
2424a2b7b749SLen Brown }
2425a2b7b749SLen Brown
2426aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model);
2427aeb01e6dSChen Yu
dump_nhm_platform_info(void)24281b439f01SLen Brown static void dump_nhm_platform_info(void)
2429103a8feaSLen Brown {
2430103a8feaSLen Brown unsigned long long msr;
2431103a8feaSLen Brown unsigned int ratio;
2432103a8feaSLen Brown
2433ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2434103a8feaSLen Brown
2435b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
24366574a5d5SLen Brown
2437103a8feaSLen Brown ratio = (msr >> 40) & 0xFF;
24381b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk);
2439103a8feaSLen Brown
2440103a8feaSLen Brown ratio = (msr >> 8) & 0xFF;
24411b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
2442103a8feaSLen Brown
24437ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2444b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2445bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
244667920418SLen Brown
2447aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */
2448aeb01e6dSChen Yu if (dis_cstate_prewake)
24491b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN");
2450aeb01e6dSChen Yu
2451fcd17211SLen Brown return;
2452fcd17211SLen Brown }
2453fcd17211SLen Brown
dump_hsw_turbo_ratio_limits(void)24541b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void)
2455fcd17211SLen Brown {
2456fcd17211SLen Brown unsigned long long msr;
2457fcd17211SLen Brown unsigned int ratio;
2458fcd17211SLen Brown
24597ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2460fcd17211SLen Brown
2461b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2462fcd17211SLen Brown
2463fcd17211SLen Brown ratio = (msr >> 8) & 0xFF;
2464fcd17211SLen Brown if (ratio)
24651b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk);
2466fcd17211SLen Brown
2467fcd17211SLen Brown ratio = (msr >> 0) & 0xFF;
2468fcd17211SLen Brown if (ratio)
24691b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk);
2470fcd17211SLen Brown return;
2471fcd17211SLen Brown }
2472fcd17211SLen Brown
dump_ivt_turbo_ratio_limits(void)24731b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void)
2474fcd17211SLen Brown {
2475fcd17211SLen Brown unsigned long long msr;
2476fcd17211SLen Brown unsigned int ratio;
24776574a5d5SLen Brown
24787ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
24796574a5d5SLen Brown
2480b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
24816574a5d5SLen Brown
24826574a5d5SLen Brown ratio = (msr >> 56) & 0xFF;
24836574a5d5SLen Brown if (ratio)
24841b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk);
24856574a5d5SLen Brown
24866574a5d5SLen Brown ratio = (msr >> 48) & 0xFF;
24876574a5d5SLen Brown if (ratio)
24881b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk);
24896574a5d5SLen Brown
24906574a5d5SLen Brown ratio = (msr >> 40) & 0xFF;
24916574a5d5SLen Brown if (ratio)
24921b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk);
24936574a5d5SLen Brown
24946574a5d5SLen Brown ratio = (msr >> 32) & 0xFF;
24956574a5d5SLen Brown if (ratio)
24961b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk);
24976574a5d5SLen Brown
24986574a5d5SLen Brown ratio = (msr >> 24) & 0xFF;
24996574a5d5SLen Brown if (ratio)
25001b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk);
25016574a5d5SLen Brown
25026574a5d5SLen Brown ratio = (msr >> 16) & 0xFF;
25036574a5d5SLen Brown if (ratio)
25041b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk);
25056574a5d5SLen Brown
25066574a5d5SLen Brown ratio = (msr >> 8) & 0xFF;
25076574a5d5SLen Brown if (ratio)
25081b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk);
25096574a5d5SLen Brown
25106574a5d5SLen Brown ratio = (msr >> 0) & 0xFF;
25116574a5d5SLen Brown if (ratio)
25121b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk);
2513103a8feaSLen Brown return;
2514fcd17211SLen Brown }
25151b439f01SLen Brown
has_turbo_ratio_group_limits(int family,int model)251631e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
251731e07522SLen Brown {
251831e07522SLen Brown
251931e07522SLen Brown if (!genuine_intel)
252031e07522SLen Brown return 0;
252131e07522SLen Brown
25229878bf7aSLen Brown if (family != 6)
25239878bf7aSLen Brown return 0;
25249878bf7aSLen Brown
252531e07522SLen Brown switch (model) {
252631e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT:
252731e07522SLen Brown case INTEL_FAM6_SKYLAKE_X:
252825368d7cSChen Yu case INTEL_FAM6_ICELAKE_X:
2529684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X:
25305ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D:
253120de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D:
253231e07522SLen Brown return 1;
25339878bf7aSLen Brown default:
253431e07522SLen Brown return 0;
253531e07522SLen Brown }
25369878bf7aSLen Brown }
2537fcd17211SLen Brown
dump_turbo_ratio_limits(int trl_msr_offset,int family,int model)25384af184eeSLen Brown static void dump_turbo_ratio_limits(int trl_msr_offset, int family, int model)
2539fcd17211SLen Brown {
254031e07522SLen Brown unsigned long long msr, core_counts;
25415d622845SLen Brown int shift;
2542103a8feaSLen Brown
25434af184eeSLen Brown get_msr(base_cpu, trl_msr_offset, &msr);
25444af184eeSLen Brown fprintf(outf, "cpu%d: MSR_%sTURBO_RATIO_LIMIT: 0x%08llx\n",
2545884a1f95SLen Brown base_cpu, trl_msr_offset == MSR_SECONDARY_TURBO_RATIO_LIMIT ? "SECONDARY_" : "", msr);
25466574a5d5SLen Brown
254731e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) {
254831e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
254931e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
255031e07522SLen Brown } else {
255131e07522SLen Brown core_counts = 0x0807060504030201;
255231e07522SLen Brown }
255331e07522SLen Brown
25545d622845SLen Brown for (shift = 56; shift >= 0; shift -= 8) {
25555d622845SLen Brown unsigned int ratio, group_size;
25566574a5d5SLen Brown
25575d622845SLen Brown ratio = (msr >> shift) & 0xFF;
25585d622845SLen Brown group_size = (core_counts >> shift) & 0xFF;
25596574a5d5SLen Brown if (ratio)
256031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
256131e07522SLen Brown ratio, bclk, ratio * bclk, group_size);
25625d622845SLen Brown }
25636574a5d5SLen Brown
2564fcd17211SLen Brown return;
2565fcd17211SLen Brown }
25663a9a941dSLen Brown
dump_atom_turbo_ratio_limits(void)25671b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void)
25680f7887c4SLen Brown {
25690f7887c4SLen Brown unsigned long long msr;
25700f7887c4SLen Brown unsigned int ratio;
25710f7887c4SLen Brown
25720f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
25730f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
25740f7887c4SLen Brown
25750f7887c4SLen Brown ratio = (msr >> 0) & 0x3F;
25760f7887c4SLen Brown if (ratio)
25771b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk);
25780f7887c4SLen Brown
25790f7887c4SLen Brown ratio = (msr >> 8) & 0x3F;
25800f7887c4SLen Brown if (ratio)
25811b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk);
25820f7887c4SLen Brown
25830f7887c4SLen Brown ratio = (msr >> 16) & 0x3F;
25840f7887c4SLen Brown if (ratio)
25851b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
25860f7887c4SLen Brown
25870f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
25880f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
25890f7887c4SLen Brown
25900f7887c4SLen Brown ratio = (msr >> 24) & 0x3F;
25910f7887c4SLen Brown if (ratio)
25921b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk);
25930f7887c4SLen Brown
25940f7887c4SLen Brown ratio = (msr >> 16) & 0x3F;
25950f7887c4SLen Brown if (ratio)
25961b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk);
25970f7887c4SLen Brown
25980f7887c4SLen Brown ratio = (msr >> 8) & 0x3F;
25990f7887c4SLen Brown if (ratio)
26001b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk);
26010f7887c4SLen Brown
26020f7887c4SLen Brown ratio = (msr >> 0) & 0x3F;
26030f7887c4SLen Brown if (ratio)
26041b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk);
26050f7887c4SLen Brown }
26060f7887c4SLen Brown
dump_knl_turbo_ratio_limits(void)26071b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void)
2608fb5d4327SDasaratharaman Chandramouli {
2609cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7;
2610cbf97abaSHubert Chrzaniuk
2611fb5d4327SDasaratharaman Chandramouli unsigned long long msr;
2612cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio;
2613cbf97abaSHubert Chrzaniuk int i, b_nr;
2614cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no];
2615cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no];
2616fb5d4327SDasaratharaman Chandramouli
2617ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2618fb5d4327SDasaratharaman Chandramouli
26191b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2620fb5d4327SDasaratharaman Chandramouli
26211e3ec5cdSRandy Dunlap /*
2622fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows:
2623cbf97abaSHubert Chrzaniuk * [0] -- Reserved
2624cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1.
2625fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1.
2626fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2.
2627fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 =
2628fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta
2629fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2.
2630fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 =
2631fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta
2632fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3.
2633fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3.
2634fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4.
2635fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4.
2636fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5.
2637fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5.
2638fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6.
2639fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6.
2640fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7.
2641fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7.
2642fb5d4327SDasaratharaman Chandramouli */
2643fb5d4327SDasaratharaman Chandramouli
2644cbf97abaSHubert Chrzaniuk b_nr = 0;
2645cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1;
2646cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF;
2647cbf97abaSHubert Chrzaniuk
2648cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) {
2649fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F;
2650cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7;
2651fb5d4327SDasaratharaman Chandramouli
2652cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores;
2653cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2654cbf97abaSHubert Chrzaniuk b_nr++;
2655fb5d4327SDasaratharaman Chandramouli }
2656cbf97abaSHubert Chrzaniuk
2657cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--)
2658cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2659b7d8c148SLen Brown fprintf(outf,
2660710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2661cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]);
2662fb5d4327SDasaratharaman Chandramouli }
2663fb5d4327SDasaratharaman Chandramouli
dump_nhm_cst_cfg(void)26641b439f01SLen Brown static void dump_nhm_cst_cfg(void)
2665fcd17211SLen Brown {
2666fcd17211SLen Brown unsigned long long msr;
2667fcd17211SLen Brown
26681df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2669fcd17211SLen Brown
26701df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2671fcd17211SLen Brown
26723e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2673fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2674fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2675fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2676fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
26771b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]);
2678ac980e13SArtem Bityutskiy
2679ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2680ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) {
26811b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2682ac980e13SArtem Bityutskiy }
2683ac980e13SArtem Bityutskiy
2684ac980e13SArtem Bityutskiy fprintf(outf, ")\n");
2685ac980e13SArtem Bityutskiy
2686fcd17211SLen Brown return;
2687103a8feaSLen Brown }
2688103a8feaSLen Brown
dump_config_tdp(void)26891b439f01SLen Brown static void dump_config_tdp(void)
26906fb3143bSLen Brown {
26916fb3143bSLen Brown unsigned long long msr;
26926fb3143bSLen Brown
26936fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2694b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2695685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
26966fb3143bSLen Brown
26976fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2698b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
26996fb3143bSLen Brown if (msr) {
2700685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2701685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2702685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2703685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
27046fb3143bSLen Brown }
2705b7d8c148SLen Brown fprintf(outf, ")\n");
27066fb3143bSLen Brown
27076fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2708b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
27096fb3143bSLen Brown if (msr) {
2710685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2711685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2712685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2713685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
27146fb3143bSLen Brown }
2715b7d8c148SLen Brown fprintf(outf, ")\n");
27166fb3143bSLen Brown
27176fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2718b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
27196fb3143bSLen Brown if ((msr) & 0x3)
2720b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2721b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2722b7d8c148SLen Brown fprintf(outf, ")\n");
27236fb3143bSLen Brown
27246fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2725b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2726685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2727b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2728b7d8c148SLen Brown fprintf(outf, ")\n");
27296fb3143bSLen Brown }
27305a63426eSLen Brown
27315a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
27325a63426eSLen Brown
print_irtl(void)27335a63426eSLen Brown void print_irtl(void)
27345a63426eSLen Brown {
27355a63426eSLen Brown unsigned long long msr;
27365a63426eSLen Brown
27375a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
27385a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
27395a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27405a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27415a63426eSLen Brown
27425a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
27435a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
27445a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27455a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27465a63426eSLen Brown
27475a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
27485a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
27495a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27505a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27515a63426eSLen Brown
27525a63426eSLen Brown if (!do_irtl_hsw)
27535a63426eSLen Brown return;
27545a63426eSLen Brown
27555a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
27565a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
27575a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27585a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27595a63426eSLen Brown
27605a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
27615a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
27625a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27635a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27645a63426eSLen Brown
27655a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
27665a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
27675a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27685a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27695a63426eSLen Brown
27705a63426eSLen Brown }
27711b439f01SLen Brown
free_fd_percpu(void)277236229897SLen Brown void free_fd_percpu(void)
277336229897SLen Brown {
277436229897SLen Brown int i;
277536229897SLen Brown
277601a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) {
277736229897SLen Brown if (fd_percpu[i] != 0)
277836229897SLen Brown close(fd_percpu[i]);
277936229897SLen Brown }
278036229897SLen Brown
278136229897SLen Brown free(fd_percpu);
27826fb3143bSLen Brown }
27836fb3143bSLen Brown
free_all_buffers(void)2784c98d5d94SLen Brown void free_all_buffers(void)
2785103a8feaSLen Brown {
27860e2d8f05SLen Brown int i;
27870e2d8f05SLen Brown
2788c98d5d94SLen Brown CPU_FREE(cpu_present_set);
2789c98d5d94SLen Brown cpu_present_set = NULL;
279036229897SLen Brown cpu_present_setsize = 0;
2791103a8feaSLen Brown
2792c98d5d94SLen Brown CPU_FREE(cpu_affinity_set);
2793c98d5d94SLen Brown cpu_affinity_set = NULL;
2794c98d5d94SLen Brown cpu_affinity_setsize = 0;
2795103a8feaSLen Brown
2796c98d5d94SLen Brown free(thread_even);
2797c98d5d94SLen Brown free(core_even);
2798c98d5d94SLen Brown free(package_even);
2799103a8feaSLen Brown
2800c98d5d94SLen Brown thread_even = NULL;
2801c98d5d94SLen Brown core_even = NULL;
2802c98d5d94SLen Brown package_even = NULL;
2803103a8feaSLen Brown
2804c98d5d94SLen Brown free(thread_odd);
2805c98d5d94SLen Brown free(core_odd);
2806c98d5d94SLen Brown free(package_odd);
2807103a8feaSLen Brown
2808c98d5d94SLen Brown thread_odd = NULL;
2809c98d5d94SLen Brown core_odd = NULL;
2810c98d5d94SLen Brown package_odd = NULL;
2811103a8feaSLen Brown
2812c98d5d94SLen Brown free(output_buffer);
2813c98d5d94SLen Brown output_buffer = NULL;
2814c98d5d94SLen Brown outp = NULL;
281536229897SLen Brown
281636229897SLen Brown free_fd_percpu();
2817562a2d37SLen Brown
2818562a2d37SLen Brown free(irq_column_2_cpu);
2819562a2d37SLen Brown free(irqs_per_cpu);
28200e2d8f05SLen Brown
28210e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) {
28220e2d8f05SLen Brown if (cpus[i].put_ids)
28230e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids);
28240e2d8f05SLen Brown }
28250e2d8f05SLen Brown free(cpus);
2826103a8feaSLen Brown }
2827103a8feaSLen Brown
282857a42a34SJosh Triplett /*
282995aebc44SJosh Triplett * Parse a file containing a single int.
28306de68fe1SLen Brown * Return 0 if file can not be opened
28316de68fe1SLen Brown * Exit if file can be opened, but can not be parsed
283295aebc44SJosh Triplett */
parse_int_file(const char * fmt,...)283395aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
283495aebc44SJosh Triplett {
283595aebc44SJosh Triplett va_list args;
283695aebc44SJosh Triplett char path[PATH_MAX];
283795aebc44SJosh Triplett FILE *filep;
283895aebc44SJosh Triplett int value;
283995aebc44SJosh Triplett
284095aebc44SJosh Triplett va_start(args, fmt);
284195aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args);
284295aebc44SJosh Triplett va_end(args);
28436de68fe1SLen Brown filep = fopen(path, "r");
28446de68fe1SLen Brown if (!filep)
28456de68fe1SLen Brown return 0;
2846b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1)
2847b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path);
284895aebc44SJosh Triplett fclose(filep);
284995aebc44SJosh Triplett return value;
285095aebc44SJosh Triplett }
285195aebc44SJosh Triplett
285295aebc44SJosh Triplett /*
2853c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu)
2854c98d5d94SLen Brown * return 1 if given CPU is 1st core in package
2855c98d5d94SLen Brown */
cpu_is_first_core_in_package(int cpu)2856c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2857c98d5d94SLen Brown {
285895aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2859103a8feaSLen Brown }
2860103a8feaSLen Brown
get_physical_package_id(int cpu)2861103a8feaSLen Brown int get_physical_package_id(int cpu)
2862103a8feaSLen Brown {
286395aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2864103a8feaSLen Brown }
2865103a8feaSLen Brown
get_die_id(int cpu)28666de68fe1SLen Brown int get_die_id(int cpu)
28676de68fe1SLen Brown {
28686de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
28696de68fe1SLen Brown }
28706de68fe1SLen Brown
get_core_id(int cpu)2871103a8feaSLen Brown int get_core_id(int cpu)
2872103a8feaSLen Brown {
287395aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2874103a8feaSLen Brown }
2875103a8feaSLen Brown
set_node_data(void)2876ef605741SPrarit Bhargava void set_node_data(void)
2877ef605741SPrarit Bhargava {
28782ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux;
28792ffbb224SPrarit Bhargava int cpu_count;
2880ef605741SPrarit Bhargava
28812ffbb224SPrarit Bhargava /* initialize logical_node_id */
28822ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
28832ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1;
2884ef605741SPrarit Bhargava
28852ffbb224SPrarit Bhargava cpu_count = 0;
28862ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) {
28872ffbb224SPrarit Bhargava lnode = 0;
28882ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
28892ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg)
2890ef605741SPrarit Bhargava continue;
28912ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */
28922ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1)
28932ffbb224SPrarit Bhargava continue;
28942ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode;
2895ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id;
28962ffbb224SPrarit Bhargava cpu_count++;
28972ffbb224SPrarit Bhargava /*
28982ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set
28992ffbb224SPrarit Bhargava * the logical_node_id
29002ffbb224SPrarit Bhargava */
29012ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
29021b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) {
29032ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode;
29042ffbb224SPrarit Bhargava cpu_count++;
2905ef605741SPrarit Bhargava }
29062ffbb224SPrarit Bhargava }
29072ffbb224SPrarit Bhargava lnode++;
29082ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg)
29092ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode;
29102ffbb224SPrarit Bhargava }
29112ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num)
29122ffbb224SPrarit Bhargava break;
29132ffbb224SPrarit Bhargava }
2914ef605741SPrarit Bhargava }
2915ef605741SPrarit Bhargava
get_physical_node_id(struct cpu_topology * thiscpu)2916ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2917c98d5d94SLen Brown {
2918c98d5d94SLen Brown char path[80];
2919c98d5d94SLen Brown FILE *filep;
29200e2d8f05SLen Brown int i;
29210e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id;
2922c98d5d94SLen Brown
29230e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) {
29241b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i);
29250e2d8f05SLen Brown filep = fopen(path, "r");
29260e2d8f05SLen Brown if (!filep)
29270e2d8f05SLen Brown continue;
29280e2d8f05SLen Brown fclose(filep);
29290e2d8f05SLen Brown return i;
29300e2d8f05SLen Brown }
29310e2d8f05SLen Brown return -1;
2932e275b388SDasaratharaman Chandramouli }
2933c98d5d94SLen Brown
get_thread_siblings(struct cpu_topology * thiscpu)29340e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
29350e2d8f05SLen Brown {
29360e2d8f05SLen Brown char path[80], character;
29370e2d8f05SLen Brown FILE *filep;
29380e2d8f05SLen Brown unsigned long map;
29398cb48b32SPrarit Bhargava int so, shift, sib_core;
29400e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id;
29410e2d8f05SLen Brown int offset = topo.max_cpu_num + 1;
29420e2d8f05SLen Brown size_t size;
29438cb48b32SPrarit Bhargava int thread_id = 0;
29440e2d8f05SLen Brown
29450e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
29468cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0)
29478cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++;
29480e2d8f05SLen Brown if (!thiscpu->put_ids)
29490e2d8f05SLen Brown return -1;
29500e2d8f05SLen Brown
29510e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
29520e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids);
29530e2d8f05SLen Brown
29541b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
29553d7772eaSLen Brown filep = fopen(path, "r");
29563d7772eaSLen Brown
29573d7772eaSLen Brown if (!filep) {
29583d7772eaSLen Brown warnx("%s: open failed", path);
29593d7772eaSLen Brown return -1;
29603d7772eaSLen Brown }
29610e2d8f05SLen Brown do {
29620e2d8f05SLen Brown offset -= BITMASK_SIZE;
29638173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2)
29648173c336SBen Hutchings err(1, "%s: failed to parse file", path);
29650e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) {
29660e2d8f05SLen Brown if ((map >> shift) & 0x1) {
29678cb48b32SPrarit Bhargava so = shift + offset;
29688cb48b32SPrarit Bhargava sib_core = get_core_id(so);
29698cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) {
29708cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids);
29711b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0))
29721b439f01SLen Brown cpus[so].thread_id = thread_id++;
29738cb48b32SPrarit Bhargava }
29740e2d8f05SLen Brown }
29750e2d8f05SLen Brown }
2976e13da9a1SColin Ian King } while (character == ',');
2977c98d5d94SLen Brown fclose(filep);
29780e2d8f05SLen Brown
29790e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids);
2980c98d5d94SLen Brown }
2981c98d5d94SLen Brown
2982c98d5d94SLen Brown /*
2983c98d5d94SLen Brown * run func(thread, core, package) in topology order
2984c98d5d94SLen Brown * skip non-present cpus
2985103a8feaSLen Brown */
2986103a8feaSLen Brown
for_all_cpus_2(int (func)(struct thread_data *,struct core_data *,struct pkg_data *,struct thread_data *,struct core_data *,struct pkg_data *),struct thread_data * thread_base,struct core_data * core_base,struct pkg_data * pkg_base,struct thread_data * thread_base2,struct core_data * core_base2,struct pkg_data * pkg_base2)2987c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *,
2988c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *,
2989c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base,
2990c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base,
29911b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2)
2992c98d5d94SLen Brown {
299340f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no;
2994c98d5d94SLen Brown
2995c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
299640f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
29971b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
29981b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
2999c98d5d94SLen Brown struct thread_data *t, *t2;
3000c98d5d94SLen Brown struct core_data *c, *c2;
3001c98d5d94SLen Brown struct pkg_data *p, *p2;
3002c98d5d94SLen Brown
30031b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
3004c98d5d94SLen Brown
3005c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id))
3006c98d5d94SLen Brown continue;
3007c98d5d94SLen Brown
30081b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no);
3009c98d5d94SLen Brown
30101b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no);
30111b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no);
3012c98d5d94SLen Brown
3013c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no);
3014c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no);
3015c98d5d94SLen Brown
3016c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2);
3017c98d5d94SLen Brown if (retval)
3018c98d5d94SLen Brown return retval;
3019c98d5d94SLen Brown }
3020c98d5d94SLen Brown }
3021c98d5d94SLen Brown }
302240f5cfe7SPrarit Bhargava }
3023c98d5d94SLen Brown return 0;
3024c98d5d94SLen Brown }
3025c98d5d94SLen Brown
3026c98d5d94SLen Brown /*
3027c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat
3028c98d5d94SLen Brown * return max_cpu number
3029c98d5d94SLen Brown */
for_all_proc_cpus(int (func)(int))3030c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int))
3031103a8feaSLen Brown {
3032103a8feaSLen Brown FILE *fp;
3033c98d5d94SLen Brown int cpu_num;
3034103a8feaSLen Brown int retval;
3035103a8feaSLen Brown
303657a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r");
3037103a8feaSLen Brown
3038103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
3039b2c95d90SJosh Triplett if (retval != 0)
3040b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat);
3041103a8feaSLen Brown
3042c98d5d94SLen Brown while (1) {
3043c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
3044103a8feaSLen Brown if (retval != 1)
3045103a8feaSLen Brown break;
3046103a8feaSLen Brown
3047c98d5d94SLen Brown retval = func(cpu_num);
3048c98d5d94SLen Brown if (retval) {
3049c98d5d94SLen Brown fclose(fp);
3050c98d5d94SLen Brown return (retval);
3051c98d5d94SLen Brown }
3052103a8feaSLen Brown }
3053103a8feaSLen Brown fclose(fp);
3054c98d5d94SLen Brown return 0;
3055103a8feaSLen Brown }
3056103a8feaSLen Brown
re_initialize(void)3057103a8feaSLen Brown void re_initialize(void)
3058103a8feaSLen Brown {
3059c98d5d94SLen Brown free_all_buffers();
3060c98d5d94SLen Brown setup_all_buffers();
30613d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
3062103a8feaSLen Brown }
3063103a8feaSLen Brown
set_max_cpu_num(void)3064843c5791SPrarit Bhargava void set_max_cpu_num(void)
3065843c5791SPrarit Bhargava {
3066843c5791SPrarit Bhargava FILE *filep;
30678201a028SPrarit Bhargava int base_cpu;
3068843c5791SPrarit Bhargava unsigned long dummy;
30698201a028SPrarit Bhargava char pathname[64];
3070843c5791SPrarit Bhargava
30718201a028SPrarit Bhargava base_cpu = sched_getcpu();
30728201a028SPrarit Bhargava if (base_cpu < 0)
30738201a028SPrarit Bhargava err(1, "cannot find calling cpu ID");
30741b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu);
30758201a028SPrarit Bhargava
30768201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r");
3077843c5791SPrarit Bhargava topo.max_cpu_num = 0;
3078843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1)
30790e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE;
3080843c5791SPrarit Bhargava fclose(filep);
3081843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */
3082843c5791SPrarit Bhargava }
3083c98d5d94SLen Brown
3084103a8feaSLen Brown /*
3085c98d5d94SLen Brown * count_cpus()
3086c98d5d94SLen Brown * remember the last one seen, it will be the max
3087103a8feaSLen Brown */
count_cpus(int cpu)3088c98d5d94SLen Brown int count_cpus(int cpu)
3089103a8feaSLen Brown {
30909878bf7aSLen Brown UNUSED(cpu);
30919878bf7aSLen Brown
3092843c5791SPrarit Bhargava topo.num_cpus++;
3093c98d5d94SLen Brown return 0;
3094103a8feaSLen Brown }
30951b439f01SLen Brown
mark_cpu_present(int cpu)3096c98d5d94SLen Brown int mark_cpu_present(int cpu)
3097c98d5d94SLen Brown {
3098c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
309915aaa346SLen Brown return 0;
3100103a8feaSLen Brown }
3101103a8feaSLen Brown
init_thread_id(int cpu)31028cb48b32SPrarit Bhargava int init_thread_id(int cpu)
31038cb48b32SPrarit Bhargava {
31048cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1;
31058cb48b32SPrarit Bhargava return 0;
31068cb48b32SPrarit Bhargava }
31078cb48b32SPrarit Bhargava
3108562a2d37SLen Brown /*
3109562a2d37SLen Brown * snapshot_proc_interrupts()
3110562a2d37SLen Brown *
3111562a2d37SLen Brown * read and record summary of /proc/interrupts
3112562a2d37SLen Brown *
3113562a2d37SLen Brown * return 1 if config change requires a restart, else return 0
3114562a2d37SLen Brown */
snapshot_proc_interrupts(void)3115562a2d37SLen Brown int snapshot_proc_interrupts(void)
3116562a2d37SLen Brown {
3117562a2d37SLen Brown static FILE *fp;
3118562a2d37SLen Brown int column, retval;
3119562a2d37SLen Brown
3120562a2d37SLen Brown if (fp == NULL)
3121562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r");
3122562a2d37SLen Brown else
3123562a2d37SLen Brown rewind(fp);
3124562a2d37SLen Brown
3125562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */
3126562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) {
3127562a2d37SLen Brown int cpu_number;
3128562a2d37SLen Brown
3129562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number);
3130562a2d37SLen Brown if (retval != 1)
3131562a2d37SLen Brown break;
3132562a2d37SLen Brown
3133562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) {
3134562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
3135562a2d37SLen Brown return 1;
3136562a2d37SLen Brown }
3137562a2d37SLen Brown
3138562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number;
3139562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0;
3140562a2d37SLen Brown }
3141562a2d37SLen Brown
3142562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */
3143562a2d37SLen Brown while (1) {
3144562a2d37SLen Brown int column;
3145562a2d37SLen Brown char buf[64];
3146562a2d37SLen Brown
3147562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
3148562a2d37SLen Brown if (retval != 1)
3149562a2d37SLen Brown break;
3150562a2d37SLen Brown
3151562a2d37SLen Brown /* read the count per cpu */
3152562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) {
3153562a2d37SLen Brown
3154562a2d37SLen Brown int cpu_number, irq_count;
3155562a2d37SLen Brown
3156562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count);
3157562a2d37SLen Brown if (retval != 1)
3158562a2d37SLen Brown break;
3159562a2d37SLen Brown
3160562a2d37SLen Brown cpu_number = irq_column_2_cpu[column];
3161562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count;
3162562a2d37SLen Brown
3163562a2d37SLen Brown }
3164562a2d37SLen Brown
31651b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */
3166562a2d37SLen Brown
3167562a2d37SLen Brown }
3168562a2d37SLen Brown return 0;
3169562a2d37SLen Brown }
31701b439f01SLen Brown
317127d47356SLen Brown /*
3172fdf676e5SLen Brown * snapshot_gfx_rc6_ms()
3173fdf676e5SLen Brown *
3174fdf676e5SLen Brown * record snapshot of
3175fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms
3176fdf676e5SLen Brown *
3177fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0
3178fdf676e5SLen Brown */
snapshot_gfx_rc6_ms(void)3179fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
3180fdf676e5SLen Brown {
3181fdf676e5SLen Brown FILE *fp;
3182fdf676e5SLen Brown int retval;
3183fdf676e5SLen Brown
3184fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
3185fdf676e5SLen Brown
3186fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
3187fdf676e5SLen Brown if (retval != 1)
3188fdf676e5SLen Brown err(1, "GFX rc6");
3189fdf676e5SLen Brown
3190fdf676e5SLen Brown fclose(fp);
3191fdf676e5SLen Brown
3192fdf676e5SLen Brown return 0;
3193fdf676e5SLen Brown }
31941b439f01SLen Brown
3195fdf676e5SLen Brown /*
319627d47356SLen Brown * snapshot_gfx_mhz()
319727d47356SLen Brown *
319827d47356SLen Brown * record snapshot of
319927d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
320027d47356SLen Brown *
320127d47356SLen Brown * return 1 if config change requires a restart, else return 0
320227d47356SLen Brown */
snapshot_gfx_mhz(void)320327d47356SLen Brown int snapshot_gfx_mhz(void)
320427d47356SLen Brown {
320527d47356SLen Brown static FILE *fp;
320627d47356SLen Brown int retval;
320727d47356SLen Brown
320827d47356SLen Brown if (fp == NULL)
320927d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
321022048c54SLen Brown else {
321127d47356SLen Brown rewind(fp);
321222048c54SLen Brown fflush(fp);
321322048c54SLen Brown }
321427d47356SLen Brown
321527d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz);
321627d47356SLen Brown if (retval != 1)
321727d47356SLen Brown err(1, "GFX MHz");
321827d47356SLen Brown
321927d47356SLen Brown return 0;
322027d47356SLen Brown }
3221562a2d37SLen Brown
3222562a2d37SLen Brown /*
3223b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz()
3224b4b91569SRafael Antognolli *
3225b4b91569SRafael Antognolli * record snapshot of
3226b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz
3227b4b91569SRafael Antognolli *
3228b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0
3229b4b91569SRafael Antognolli */
snapshot_gfx_act_mhz(void)3230b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void)
3231b4b91569SRafael Antognolli {
3232b4b91569SRafael Antognolli static FILE *fp;
3233b4b91569SRafael Antognolli int retval;
3234b4b91569SRafael Antognolli
3235b4b91569SRafael Antognolli if (fp == NULL)
3236b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r");
3237b4b91569SRafael Antognolli else {
3238b4b91569SRafael Antognolli rewind(fp);
3239b4b91569SRafael Antognolli fflush(fp);
3240b4b91569SRafael Antognolli }
3241b4b91569SRafael Antognolli
3242b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz);
3243b4b91569SRafael Antognolli if (retval != 1)
3244b4b91569SRafael Antognolli err(1, "GFX ACT MHz");
3245b4b91569SRafael Antognolli
3246b4b91569SRafael Antognolli return 0;
3247b4b91569SRafael Antognolli }
3248b4b91569SRafael Antognolli
3249b4b91569SRafael Antognolli /*
3250be0e54c4SLen Brown * snapshot_cpu_lpi()
3251be0e54c4SLen Brown *
3252be0e54c4SLen Brown * record snapshot of
3253be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
3254be0e54c4SLen Brown */
snapshot_cpu_lpi_us(void)3255be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
3256be0e54c4SLen Brown {
3257be0e54c4SLen Brown FILE *fp;
3258be0e54c4SLen Brown int retval;
3259be0e54c4SLen Brown
3260be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
3261be0e54c4SLen Brown
3262be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
32635ea7647bSPrarit Bhargava if (retval != 1) {
32645ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n");
32655ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI);
3266605736c6SGustavo A. R. Silva fclose(fp);
32675ea7647bSPrarit Bhargava return -1;
32685ea7647bSPrarit Bhargava }
3269be0e54c4SLen Brown
3270be0e54c4SLen Brown fclose(fp);
3271be0e54c4SLen Brown
3272be0e54c4SLen Brown return 0;
3273be0e54c4SLen Brown }
32741b439f01SLen Brown
3275be0e54c4SLen Brown /*
3276be0e54c4SLen Brown * snapshot_sys_lpi()
3277be0e54c4SLen Brown *
32781f81c5efSLen Brown * record snapshot of sys_lpi_file
3279be0e54c4SLen Brown */
snapshot_sys_lpi_us(void)3280be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
3281be0e54c4SLen Brown {
3282be0e54c4SLen Brown FILE *fp;
3283be0e54c4SLen Brown int retval;
3284be0e54c4SLen Brown
32851f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r");
3286be0e54c4SLen Brown
3287be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
32885ea7647bSPrarit Bhargava if (retval != 1) {
32895ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n");
32905ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI);
329115423b95SColin Ian King fclose(fp);
32925ea7647bSPrarit Bhargava return -1;
32935ea7647bSPrarit Bhargava }
3294be0e54c4SLen Brown fclose(fp);
3295be0e54c4SLen Brown
3296be0e54c4SLen Brown return 0;
3297be0e54c4SLen Brown }
32981b439f01SLen Brown
3299be0e54c4SLen Brown /*
3300562a2d37SLen Brown * snapshot /proc and /sys files
3301562a2d37SLen Brown *
3302562a2d37SLen Brown * return 1 if configuration restart needed, else return 0
3303562a2d37SLen Brown */
snapshot_proc_sysfs_files(void)3304562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
3305562a2d37SLen Brown {
3306218f0e8dSLen Brown if (DO_BIC(BIC_IRQ))
3307562a2d37SLen Brown if (snapshot_proc_interrupts())
3308562a2d37SLen Brown return 1;
3309562a2d37SLen Brown
3310812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6))
3311fdf676e5SLen Brown snapshot_gfx_rc6_ms();
3312fdf676e5SLen Brown
3313812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz))
331427d47356SLen Brown snapshot_gfx_mhz();
331527d47356SLen Brown
3316b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz))
3317b4b91569SRafael Antognolli snapshot_gfx_act_mhz();
3318b4b91569SRafael Antognolli
3319be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI))
3320be0e54c4SLen Brown snapshot_cpu_lpi_us();
3321be0e54c4SLen Brown
3322be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI))
3323be0e54c4SLen Brown snapshot_sys_lpi_us();
3324be0e54c4SLen Brown
3325562a2d37SLen Brown return 0;
3326562a2d37SLen Brown }
3327562a2d37SLen Brown
33288aa2ed0bSLen Brown int exit_requested;
33298aa2ed0bSLen Brown
signal_handler(int signal)33308aa2ed0bSLen Brown static void signal_handler(int signal)
33318aa2ed0bSLen Brown {
33328aa2ed0bSLen Brown switch (signal) {
33338aa2ed0bSLen Brown case SIGINT:
33348aa2ed0bSLen Brown exit_requested = 1;
33358aa2ed0bSLen Brown if (debug)
33368aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n");
33378aa2ed0bSLen Brown break;
333807211960SLen Brown case SIGUSR1:
333907211960SLen Brown if (debug > 1)
334007211960SLen Brown fprintf(stderr, "SIGUSR1\n");
334107211960SLen Brown break;
33428aa2ed0bSLen Brown }
33438aa2ed0bSLen Brown }
33448aa2ed0bSLen Brown
setup_signal_handler(void)33458aa2ed0bSLen Brown void setup_signal_handler(void)
33468aa2ed0bSLen Brown {
33478aa2ed0bSLen Brown struct sigaction sa;
33488aa2ed0bSLen Brown
33498aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa));
33508aa2ed0bSLen Brown
33518aa2ed0bSLen Brown sa.sa_handler = &signal_handler;
33528aa2ed0bSLen Brown
33538aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0)
33548aa2ed0bSLen Brown err(1, "sigaction SIGINT");
335507211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0)
335607211960SLen Brown err(1, "sigaction SIGUSR1");
33578aa2ed0bSLen Brown }
3358b9ad8ee0SLen Brown
do_sleep(void)335947936f94SArtem Bityutskiy void do_sleep(void)
3360b9ad8ee0SLen Brown {
3361c026c236SArtem Bityutskiy struct timeval tout;
3362c026c236SArtem Bityutskiy struct timespec rest;
3363b9ad8ee0SLen Brown fd_set readfds;
3364b9ad8ee0SLen Brown int retval;
3365b9ad8ee0SLen Brown
3366b9ad8ee0SLen Brown FD_ZERO(&readfds);
3367b9ad8ee0SLen Brown FD_SET(0, &readfds);
3368b9ad8ee0SLen Brown
3369c026c236SArtem Bityutskiy if (ignore_stdin) {
337047936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL);
337147936f94SArtem Bityutskiy return;
337247936f94SArtem Bityutskiy }
3373b9ad8ee0SLen Brown
3374c026c236SArtem Bityutskiy tout = interval_tv;
3375c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout);
3376b9ad8ee0SLen Brown
3377b9ad8ee0SLen Brown if (retval == 1) {
3378b9ad8ee0SLen Brown switch (getc(stdin)) {
3379b9ad8ee0SLen Brown case 'q':
3380b9ad8ee0SLen Brown exit_requested = 1;
3381b9ad8ee0SLen Brown break;
3382c026c236SArtem Bityutskiy case EOF:
3383c026c236SArtem Bityutskiy /*
3384c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There
3385c026c236SArtem Bityutskiy * won't be any further input.
3386c026c236SArtem Bityutskiy */
3387c026c236SArtem Bityutskiy ignore_stdin = 1;
3388c026c236SArtem Bityutskiy /* Sleep the rest of the time */
3389c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3390c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3391c026c236SArtem Bityutskiy nanosleep(&rest, NULL);
3392b9ad8ee0SLen Brown }
3393b9ad8ee0SLen Brown }
3394b9ad8ee0SLen Brown }
339547936f94SArtem Bityutskiy
get_msr_sum(int cpu,off_t offset,unsigned long long * msr)339687e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr)
339787e15da9SChen Yu {
339887e15da9SChen Yu int ret, idx;
339987e15da9SChen Yu unsigned long long msr_cur, msr_last;
340087e15da9SChen Yu
340187e15da9SChen Yu if (!per_cpu_msr_sum)
340287e15da9SChen Yu return 1;
340387e15da9SChen Yu
340487e15da9SChen Yu idx = offset_to_idx(offset);
340587e15da9SChen Yu if (idx < 0)
340687e15da9SChen Yu return idx;
340787e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */
340887e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur);
340987e15da9SChen Yu if (ret)
341087e15da9SChen Yu return ret;
341187e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last;
341287e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last);
341387e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum;
341487e15da9SChen Yu
341587e15da9SChen Yu return 0;
341687e15da9SChen Yu }
341787e15da9SChen Yu
341887e15da9SChen Yu timer_t timerid;
341987e15da9SChen Yu
342087e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */
update_msr_sum(struct thread_data * t,struct core_data * c,struct pkg_data * p)342187e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p)
342287e15da9SChen Yu {
342387e15da9SChen Yu int i, ret;
342487e15da9SChen Yu int cpu = t->cpu_id;
342587e15da9SChen Yu
34269878bf7aSLen Brown UNUSED(c);
34279878bf7aSLen Brown UNUSED(p);
34289878bf7aSLen Brown
342987e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
343087e15da9SChen Yu unsigned long long msr_cur, msr_last;
343113a779deSCalvin Walton off_t offset;
343287e15da9SChen Yu
343387e15da9SChen Yu if (!idx_valid(i))
343487e15da9SChen Yu continue;
343587e15da9SChen Yu offset = idx_to_offset(i);
343687e15da9SChen Yu if (offset < 0)
343787e15da9SChen Yu continue;
343887e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur);
343987e15da9SChen Yu if (ret) {
34401b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset);
344187e15da9SChen Yu continue;
344287e15da9SChen Yu }
344387e15da9SChen Yu
344487e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last;
344587e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff;
344687e15da9SChen Yu
344787e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last);
344887e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last;
344987e15da9SChen Yu }
345087e15da9SChen Yu return 0;
345187e15da9SChen Yu }
345287e15da9SChen Yu
msr_record_handler(union sigval v)34531b439f01SLen Brown static void msr_record_handler(union sigval v)
345487e15da9SChen Yu {
34559878bf7aSLen Brown UNUSED(v);
34569878bf7aSLen Brown
345787e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS);
345887e15da9SChen Yu }
345987e15da9SChen Yu
msr_sum_record(void)346087e15da9SChen Yu void msr_sum_record(void)
346187e15da9SChen Yu {
346287e15da9SChen Yu struct itimerspec its;
346387e15da9SChen Yu struct sigevent sev;
346487e15da9SChen Yu
346587e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array));
346687e15da9SChen Yu if (!per_cpu_msr_sum) {
346787e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n");
346887e15da9SChen Yu return;
346987e15da9SChen Yu }
347087e15da9SChen Yu /*
347187e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead.
347287e15da9SChen Yu */
347387e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent));
347487e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD;
347587e15da9SChen Yu sev.sigev_notify_function = msr_record_handler;
347687e15da9SChen Yu
347787e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid;
347887e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
347987e15da9SChen Yu fprintf(outf, "Can not create timer.\n");
348087e15da9SChen Yu goto release_msr;
348187e15da9SChen Yu }
348287e15da9SChen Yu
348387e15da9SChen Yu its.it_value.tv_sec = 0;
348487e15da9SChen Yu its.it_value.tv_nsec = 1;
348587e15da9SChen Yu /*
348687e15da9SChen Yu * A wraparound time has been calculated early.
348787e15da9SChen Yu * Some sources state that the peak power for a
348887e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating,
348987e15da9SChen Yu * use 2 * TDP for safety.
349087e15da9SChen Yu */
349187e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2;
349287e15da9SChen Yu its.it_interval.tv_nsec = 0;
349387e15da9SChen Yu
349487e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) {
349587e15da9SChen Yu fprintf(outf, "Can not set timer.\n");
349687e15da9SChen Yu goto release_timer;
349787e15da9SChen Yu }
349887e15da9SChen Yu return;
349987e15da9SChen Yu
350087e15da9SChen Yu release_timer:
350187e15da9SChen Yu timer_delete(timerid);
350287e15da9SChen Yu release_msr:
350387e15da9SChen Yu free(per_cpu_msr_sum);
350487e15da9SChen Yu }
35054c2122d4SLen Brown
350638c6663aSLen Brown /*
350738c6663aSLen Brown * set_my_sched_priority(pri)
350838c6663aSLen Brown * return previous
350938c6663aSLen Brown */
set_my_sched_priority(int priority)351038c6663aSLen Brown int set_my_sched_priority(int priority)
351138c6663aSLen Brown {
351238c6663aSLen Brown int retval;
351338c6663aSLen Brown int original_priority;
351438c6663aSLen Brown
351538c6663aSLen Brown errno = 0;
351638c6663aSLen Brown original_priority = getpriority(PRIO_PROCESS, 0);
351738c6663aSLen Brown if (errno && (original_priority == -1))
351838c6663aSLen Brown err(errno, "getpriority");
351938c6663aSLen Brown
352038c6663aSLen Brown retval = setpriority(PRIO_PROCESS, 0, priority);
352138c6663aSLen Brown if (retval)
3522de7839eeSLen Brown errx(retval, "capget(CAP_SYS_NICE) failed,try \"# setcap cap_sys_nice=ep %s\"", progname);
352338c6663aSLen Brown
352438c6663aSLen Brown errno = 0;
352538c6663aSLen Brown retval = getpriority(PRIO_PROCESS, 0);
352638c6663aSLen Brown if (retval != priority)
3527164d7a96SLen Brown err(retval, "getpriority(%d) != setpriority(%d)", retval, priority);
352838c6663aSLen Brown
352938c6663aSLen Brown return original_priority;
353038c6663aSLen Brown }
353138c6663aSLen Brown
turbostat_loop()3532103a8feaSLen Brown void turbostat_loop()
3533103a8feaSLen Brown {
3534c98d5d94SLen Brown int retval;
3535e52966c0SLen Brown int restarted = 0;
35369878bf7aSLen Brown unsigned int done_iters = 0;
3537c98d5d94SLen Brown
35388aa2ed0bSLen Brown setup_signal_handler();
35398aa2ed0bSLen Brown
354038c6663aSLen Brown /*
354138c6663aSLen Brown * elevate own priority for interval mode
354238c6663aSLen Brown */
354338c6663aSLen Brown set_my_sched_priority(-20);
354438c6663aSLen Brown
3545103a8feaSLen Brown restart:
3546e52966c0SLen Brown restarted++;
3547e52966c0SLen Brown
3548562a2d37SLen Brown snapshot_proc_sysfs_files();
3549c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS);
35504c2122d4SLen Brown first_counter_read = 0;
3551d91bb17cSLen Brown if (retval < -1) {
3552d91bb17cSLen Brown exit(retval);
3553d91bb17cSLen Brown } else if (retval == -1) {
35543d7772eaSLen Brown if (restarted > 10) {
3555e52966c0SLen Brown exit(retval);
3556e52966c0SLen Brown }
3557c98d5d94SLen Brown re_initialize();
3558c98d5d94SLen Brown goto restart;
3559c98d5d94SLen Brown }
3560e52966c0SLen Brown restarted = 0;
3561023fe0acSChen Yu done_iters = 0;
3562103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL);
3563103a8feaSLen Brown
3564103a8feaSLen Brown while (1) {
3565c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) {
3566103a8feaSLen Brown re_initialize();
3567103a8feaSLen Brown goto restart;
3568103a8feaSLen Brown }
3569b9ad8ee0SLen Brown do_sleep();
3570562a2d37SLen Brown if (snapshot_proc_sysfs_files())
3571562a2d37SLen Brown goto restart;
3572c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS);
3573d91bb17cSLen Brown if (retval < -1) {
3574d91bb17cSLen Brown exit(retval);
3575d91bb17cSLen Brown } else if (retval == -1) {
357615aaa346SLen Brown re_initialize();
357715aaa346SLen Brown goto restart;
357815aaa346SLen Brown }
3579103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL);
3580103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta);
3581ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3582ba3dec99SLen Brown re_initialize();
3583ba3dec99SLen Brown goto restart;
3584ba3dec99SLen Brown }
3585c98d5d94SLen Brown compute_average(EVEN_COUNTERS);
3586c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS);
3587b7d8c148SLen Brown flush_output_stdout();
35888aa2ed0bSLen Brown if (exit_requested)
35898aa2ed0bSLen Brown break;
3590023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations)
3591023fe0acSChen Yu break;
3592b9ad8ee0SLen Brown do_sleep();
3593562a2d37SLen Brown if (snapshot_proc_sysfs_files())
3594562a2d37SLen Brown goto restart;
3595c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3596d91bb17cSLen Brown if (retval < -1) {
3597d91bb17cSLen Brown exit(retval);
3598d91bb17cSLen Brown } else if (retval == -1) {
3599103a8feaSLen Brown re_initialize();
3600103a8feaSLen Brown goto restart;
3601103a8feaSLen Brown }
3602103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL);
3603103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta);
3604ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3605ba3dec99SLen Brown re_initialize();
3606ba3dec99SLen Brown goto restart;
3607ba3dec99SLen Brown }
3608c98d5d94SLen Brown compute_average(ODD_COUNTERS);
3609c98d5d94SLen Brown format_all_counters(ODD_COUNTERS);
3610b7d8c148SLen Brown flush_output_stdout();
36118aa2ed0bSLen Brown if (exit_requested)
36128aa2ed0bSLen Brown break;
3613023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations)
3614023fe0acSChen Yu break;
3615103a8feaSLen Brown }
3616103a8feaSLen Brown }
3617103a8feaSLen Brown
check_dev_msr()3618103a8feaSLen Brown void check_dev_msr()
3619103a8feaSLen Brown {
3620103a8feaSLen Brown struct stat sb;
36217ce7d5deSPrarit Bhargava char pathname[32];
3622103a8feaSLen Brown
36237ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
36247ce7d5deSPrarit Bhargava if (stat(pathname, &sb))
3625a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3626d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3627103a8feaSLen Brown }
3628103a8feaSLen Brown
3629fcaa681cSLen Brown /*
3630fcaa681cSLen Brown * check for CAP_SYS_RAWIO
3631fcaa681cSLen Brown * return 0 on success
3632fcaa681cSLen Brown * return 1 on fail
3633fcaa681cSLen Brown */
check_for_cap_sys_rawio(void)3634fcaa681cSLen Brown int check_for_cap_sys_rawio(void)
3635103a8feaSLen Brown {
3636fcaa681cSLen Brown cap_t caps;
3637fcaa681cSLen Brown cap_flag_value_t cap_flag_value;
3638fcaa681cSLen Brown
3639fcaa681cSLen Brown caps = cap_get_proc();
3640fcaa681cSLen Brown if (caps == NULL)
3641fcaa681cSLen Brown err(-6, "cap_get_proc\n");
3642fcaa681cSLen Brown
3643fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3644fcaa681cSLen Brown err(-6, "cap_get\n");
3645fcaa681cSLen Brown
3646fcaa681cSLen Brown if (cap_flag_value != CAP_SET) {
36471b439f01SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3648fcaa681cSLen Brown return 1;
3649fcaa681cSLen Brown }
3650fcaa681cSLen Brown
3651fcaa681cSLen Brown if (cap_free(caps) == -1)
3652fcaa681cSLen Brown err(-6, "cap_free\n");
3653fcaa681cSLen Brown
3654fcaa681cSLen Brown return 0;
3655fcaa681cSLen Brown }
36561b439f01SLen Brown
check_permissions(void)3657fcaa681cSLen Brown void check_permissions(void)
3658fcaa681cSLen Brown {
365998481e79SLen Brown int do_exit = 0;
36607ce7d5deSPrarit Bhargava char pathname[32];
366198481e79SLen Brown
366298481e79SLen Brown /* check for CAP_SYS_RAWIO */
3663fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio();
366498481e79SLen Brown
366598481e79SLen Brown /* test file permissions */
36667ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
36677ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) {
366898481e79SLen Brown do_exit++;
366998481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
367098481e79SLen Brown }
367198481e79SLen Brown
367298481e79SLen Brown /* if all else fails, thell them to be root */
367398481e79SLen Brown if (do_exit)
3674b2c95d90SJosh Triplett if (getuid() != 0)
3675d7899447SLen Brown warnx("... or simply run as root");
367698481e79SLen Brown
367798481e79SLen Brown if (do_exit)
367898481e79SLen Brown exit(-6);
3679103a8feaSLen Brown }
3680103a8feaSLen Brown
3681d7899447SLen Brown /*
3682d7899447SLen Brown * NHM adds support for additional MSRs:
3683d7899447SLen Brown *
3684d7899447SLen Brown * MSR_SMI_COUNT 0x00000034
3685d7899447SLen Brown *
3686ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce
36871df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
3688d7899447SLen Brown *
3689cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa
3690cf4cbe53SLen Brown *
3691d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8
3692d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9
3693d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc
3694d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd
3695d7899447SLen Brown *
3696ee7e38e3SLen Brown * Side effect:
36971df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
369833148d67SLen Brown * sets has_misc_feature_control
3699d7899447SLen Brown */
probe_nhm_msrs(unsigned int family,unsigned int model)3700ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3701103a8feaSLen Brown {
3702ee7e38e3SLen Brown unsigned long long msr;
370321ed5574SLen Brown unsigned int base_ratio;
3704ee7e38e3SLen Brown int *pkg_cstate_limits;
3705ee7e38e3SLen Brown
3706103a8feaSLen Brown if (!genuine_intel)
3707103a8feaSLen Brown return 0;
3708103a8feaSLen Brown
3709103a8feaSLen Brown if (family != 6)
3710103a8feaSLen Brown return 0;
3711103a8feaSLen Brown
371221ed5574SLen Brown bclk = discover_bclk(family, model);
371321ed5574SLen Brown
3714103a8feaSLen Brown switch (model) {
3715869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3716869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3717ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits;
3718ee7e38e3SLen Brown break;
3719869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3720869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3721869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */
3722869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3723ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits;
372433148d67SLen Brown has_misc_feature_control = 1;
3725ee7e38e3SLen Brown break;
3726c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */
37275e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */
3728869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */
372977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */
3730c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */
37315e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */
3732869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */
3733af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */
3734af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3735ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits;
373633148d67SLen Brown has_misc_feature_control = 1;
3737ee7e38e3SLen Brown break;
3738d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */
37390e4d42afSArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
3740d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits;
374133148d67SLen Brown has_misc_feature_control = 1;
3742d8ebb442SLen Brown break;
374325368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */
374425368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits;
374525368d7cSChen Yu has_misc_feature_control = 1;
374625368d7cSChen Yu break;
3747f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
3748cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1;
37499878bf7aSLen Brown /* FALLTHRU */
37505ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
3751ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits;
3752ee7e38e3SLen Brown break;
3753869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
3754ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits;
3755cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1;
3756ee7e38e3SLen Brown break;
3757869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
3758ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits;
3759ee7e38e3SLen Brown break;
3760869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3761f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
37625ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
3763f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */
376420de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
3765445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits;
3766e4085d54SLen Brown break;
3767103a8feaSLen Brown default:
3768103a8feaSLen Brown return 0;
3769103a8feaSLen Brown }
37701df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3771e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3772ee7e38e3SLen Brown
3773ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
377421ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF;
377521ed5574SLen Brown
377621ed5574SLen Brown base_hz = base_ratio * bclk * 1000000;
377721ed5574SLen Brown has_base_hz = 1;
3778ee7e38e3SLen Brown return 1;
3779103a8feaSLen Brown }
37801b439f01SLen Brown
37810f7887c4SLen Brown /*
3782495c7654SLen Brown * SLV client has support for unique MSRs:
37830f7887c4SLen Brown *
37840f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG
37850f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG
37860f7887c4SLen Brown */
37870f7887c4SLen Brown
has_slv_msrs(unsigned int family,unsigned int model)37880f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
37890f7887c4SLen Brown {
37900f7887c4SLen Brown if (!genuine_intel)
37910f7887c4SLen Brown return 0;
37920f7887c4SLen Brown
37939878bf7aSLen Brown if (family != 6)
37949878bf7aSLen Brown return 0;
37959878bf7aSLen Brown
37960f7887c4SLen Brown switch (model) {
3797f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT:
3798f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID:
3799f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID:
38000f7887c4SLen Brown return 1;
38010f7887c4SLen Brown }
38020f7887c4SLen Brown return 0;
38030f7887c4SLen Brown }
38041b439f01SLen Brown
is_dnv(unsigned int family,unsigned int model)38057170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
38067170a374SLen Brown {
38077170a374SLen Brown
38087170a374SLen Brown if (!genuine_intel)
38097170a374SLen Brown return 0;
38107170a374SLen Brown
38119878bf7aSLen Brown if (family != 6)
38129878bf7aSLen Brown return 0;
38139878bf7aSLen Brown
38147170a374SLen Brown switch (model) {
38155ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D:
38167170a374SLen Brown return 1;
38177170a374SLen Brown }
38187170a374SLen Brown return 0;
38197170a374SLen Brown }
38201b439f01SLen Brown
is_bdx(unsigned int family,unsigned int model)3821ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3822ade0ebacSLen Brown {
3823ade0ebacSLen Brown
3824ade0ebacSLen Brown if (!genuine_intel)
3825ade0ebacSLen Brown return 0;
3826ade0ebacSLen Brown
38279878bf7aSLen Brown if (family != 6)
38289878bf7aSLen Brown return 0;
38299878bf7aSLen Brown
3830ade0ebacSLen Brown switch (model) {
3831ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X:
3832ade0ebacSLen Brown return 1;
3833ade0ebacSLen Brown }
3834ade0ebacSLen Brown return 0;
3835ade0ebacSLen Brown }
38361b439f01SLen Brown
is_skx(unsigned int family,unsigned int model)383734c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
383834c76197SLen Brown {
383934c76197SLen Brown
384034c76197SLen Brown if (!genuine_intel)
384134c76197SLen Brown return 0;
384234c76197SLen Brown
38439878bf7aSLen Brown if (family != 6)
38449878bf7aSLen Brown return 0;
38459878bf7aSLen Brown
384634c76197SLen Brown switch (model) {
384734c76197SLen Brown case INTEL_FAM6_SKYLAKE_X:
384834c76197SLen Brown return 1;
384934c76197SLen Brown }
385034c76197SLen Brown return 0;
385134c76197SLen Brown }
385225368d7cSChen Yu
is_icx(unsigned int family,unsigned int model)385325368d7cSChen Yu int is_icx(unsigned int family, unsigned int model)
385425368d7cSChen Yu {
385525368d7cSChen Yu
385625368d7cSChen Yu if (!genuine_intel)
385725368d7cSChen Yu return 0;
385825368d7cSChen Yu
38599878bf7aSLen Brown if (family != 6)
38609878bf7aSLen Brown return 0;
38619878bf7aSLen Brown
386225368d7cSChen Yu switch (model) {
386325368d7cSChen Yu case INTEL_FAM6_ICELAKE_X:
386425368d7cSChen Yu return 1;
386525368d7cSChen Yu }
386625368d7cSChen Yu return 0;
386725368d7cSChen Yu }
386825368d7cSChen Yu
is_spr(unsigned int family,unsigned int model)3869684e40e9SArtem Bityutskiy int is_spr(unsigned int family, unsigned int model)
3870684e40e9SArtem Bityutskiy {
3871684e40e9SArtem Bityutskiy
3872684e40e9SArtem Bityutskiy if (!genuine_intel)
3873684e40e9SArtem Bityutskiy return 0;
3874684e40e9SArtem Bityutskiy
3875684e40e9SArtem Bityutskiy if (family != 6)
3876684e40e9SArtem Bityutskiy return 0;
3877684e40e9SArtem Bityutskiy
3878684e40e9SArtem Bityutskiy switch (model) {
3879684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X:
3880684e40e9SArtem Bityutskiy return 1;
3881684e40e9SArtem Bityutskiy }
3882684e40e9SArtem Bityutskiy return 0;
3883684e40e9SArtem Bityutskiy }
3884684e40e9SArtem Bityutskiy
is_ehl(unsigned int family,unsigned int model)3885f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model)
3886f6708400SChen Yu {
3887f6708400SChen Yu if (!genuine_intel)
3888f6708400SChen Yu return 0;
3889f6708400SChen Yu
38909878bf7aSLen Brown if (family != 6)
38919878bf7aSLen Brown return 0;
38929878bf7aSLen Brown
3893f6708400SChen Yu switch (model) {
3894f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT:
3895f6708400SChen Yu return 1;
3896f6708400SChen Yu }
3897f6708400SChen Yu return 0;
3898f6708400SChen Yu }
38991b439f01SLen Brown
is_jvl(unsigned int family,unsigned int model)390020de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model)
390120de0dabSAntti Laakso {
390220de0dabSAntti Laakso if (!genuine_intel)
390320de0dabSAntti Laakso return 0;
390420de0dabSAntti Laakso
39059878bf7aSLen Brown if (family != 6)
39069878bf7aSLen Brown return 0;
39079878bf7aSLen Brown
390820de0dabSAntti Laakso switch (model) {
390920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D:
391020de0dabSAntti Laakso return 1;
391120de0dabSAntti Laakso }
391220de0dabSAntti Laakso return 0;
391320de0dabSAntti Laakso }
39140f7887c4SLen Brown
has_turbo_ratio_limit(unsigned int family,unsigned int model)391531e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3916d7899447SLen Brown {
39170f7887c4SLen Brown if (has_slv_msrs(family, model))
39180f7887c4SLen Brown return 0;
39190f7887c4SLen Brown
39209878bf7aSLen Brown if (family != 6)
39219878bf7aSLen Brown return 0;
39229878bf7aSLen Brown
3923d7899447SLen Brown switch (model) {
3924d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */
3925869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3926869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
3927d7899447SLen Brown return 0;
3928d7899447SLen Brown default:
3929d7899447SLen Brown return 1;
3930d7899447SLen Brown }
3931d7899447SLen Brown }
39321b439f01SLen Brown
has_atom_turbo_ratio_limit(unsigned int family,unsigned int model)39330f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
39340f7887c4SLen Brown {
39350f7887c4SLen Brown if (has_slv_msrs(family, model))
39360f7887c4SLen Brown return 1;
39370f7887c4SLen Brown
39380f7887c4SLen Brown return 0;
39390f7887c4SLen Brown }
39401b439f01SLen Brown
has_ivt_turbo_ratio_limit(unsigned int family,unsigned int model)39416574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
39426574a5d5SLen Brown {
39436574a5d5SLen Brown if (!genuine_intel)
39446574a5d5SLen Brown return 0;
39456574a5d5SLen Brown
39466574a5d5SLen Brown if (family != 6)
39476574a5d5SLen Brown return 0;
39486574a5d5SLen Brown
39496574a5d5SLen Brown switch (model) {
3950869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3951869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
39526574a5d5SLen Brown return 1;
39536574a5d5SLen Brown default:
39546574a5d5SLen Brown return 0;
39556574a5d5SLen Brown }
39566574a5d5SLen Brown }
39571b439f01SLen Brown
has_hsw_turbo_ratio_limit(unsigned int family,unsigned int model)3958fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3959fcd17211SLen Brown {
3960fcd17211SLen Brown if (!genuine_intel)
3961fcd17211SLen Brown return 0;
3962fcd17211SLen Brown
3963fcd17211SLen Brown if (family != 6)
3964fcd17211SLen Brown return 0;
3965fcd17211SLen Brown
3966fcd17211SLen Brown switch (model) {
3967869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
3968fcd17211SLen Brown return 1;
3969fcd17211SLen Brown default:
3970fcd17211SLen Brown return 0;
3971fcd17211SLen Brown }
3972fcd17211SLen Brown }
3973fcd17211SLen Brown
has_knl_turbo_ratio_limit(unsigned int family,unsigned int model)3974fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3975fb5d4327SDasaratharaman Chandramouli {
3976fb5d4327SDasaratharaman Chandramouli if (!genuine_intel)
3977fb5d4327SDasaratharaman Chandramouli return 0;
3978fb5d4327SDasaratharaman Chandramouli
3979fb5d4327SDasaratharaman Chandramouli if (family != 6)
3980fb5d4327SDasaratharaman Chandramouli return 0;
3981fb5d4327SDasaratharaman Chandramouli
3982fb5d4327SDasaratharaman Chandramouli switch (model) {
3983869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3984fb5d4327SDasaratharaman Chandramouli return 1;
3985fb5d4327SDasaratharaman Chandramouli default:
3986fb5d4327SDasaratharaman Chandramouli return 0;
3987fb5d4327SDasaratharaman Chandramouli }
3988fb5d4327SDasaratharaman Chandramouli }
39891b439f01SLen Brown
has_glm_turbo_ratio_limit(unsigned int family,unsigned int model)399031e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
399131e07522SLen Brown {
399231e07522SLen Brown if (!genuine_intel)
399331e07522SLen Brown return 0;
399431e07522SLen Brown
399531e07522SLen Brown if (family != 6)
399631e07522SLen Brown return 0;
399731e07522SLen Brown
399831e07522SLen Brown switch (model) {
399931e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT:
400031e07522SLen Brown case INTEL_FAM6_SKYLAKE_X:
400125368d7cSChen Yu case INTEL_FAM6_ICELAKE_X:
4002684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X:
400331e07522SLen Brown return 1;
400431e07522SLen Brown default:
400531e07522SLen Brown return 0;
400631e07522SLen Brown }
400731e07522SLen Brown }
40081b439f01SLen Brown
has_config_tdp(unsigned int family,unsigned int model)40096fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
40106fb3143bSLen Brown {
40116fb3143bSLen Brown if (!genuine_intel)
40126fb3143bSLen Brown return 0;
40136fb3143bSLen Brown
40146fb3143bSLen Brown if (family != 6)
40156fb3143bSLen Brown return 0;
40166fb3143bSLen Brown
40176fb3143bSLen Brown switch (model) {
4018869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */
4019c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */
4020869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */
402177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */
40225e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */
4023c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */
40245e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */
4025869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */
4026af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */
4027af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4028869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */
402925368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */
4030684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
4031869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
40326fb3143bSLen Brown return 1;
40336fb3143bSLen Brown default:
40346fb3143bSLen Brown return 0;
40356fb3143bSLen Brown }
40366fb3143bSLen Brown }
40376fb3143bSLen Brown
40380b9a0b9bSZhang Rui /*
40390b9a0b9bSZhang Rui * tcc_offset_bits:
40400b9a0b9bSZhang Rui * 0: Tcc Offset not supported (Default)
40410b9a0b9bSZhang Rui * 6: Bit 29:24 of MSR_PLATFORM_INFO
40420b9a0b9bSZhang Rui * 4: Bit 27:24 of MSR_PLATFORM_INFO
40430b9a0b9bSZhang Rui */
check_tcc_offset(int model)40440b9a0b9bSZhang Rui void check_tcc_offset(int model)
40450b9a0b9bSZhang Rui {
40460b9a0b9bSZhang Rui unsigned long long msr;
40470b9a0b9bSZhang Rui
40480b9a0b9bSZhang Rui if (!genuine_intel)
40490b9a0b9bSZhang Rui return;
40500b9a0b9bSZhang Rui
40510b9a0b9bSZhang Rui switch (model) {
40520b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE_L:
40530b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE:
40540b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE_L:
40550b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE:
40560b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE_L:
40570b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE:
40580b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE_L:
40590b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE:
40600b9a0b9bSZhang Rui case INTEL_FAM6_COMETLAKE:
40610b9a0b9bSZhang Rui if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) {
40620b9a0b9bSZhang Rui msr = (msr >> 30) & 1;
40630b9a0b9bSZhang Rui if (msr)
40640b9a0b9bSZhang Rui tcc_offset_bits = 6;
40650b9a0b9bSZhang Rui }
40660b9a0b9bSZhang Rui return;
40670b9a0b9bSZhang Rui default:
40680b9a0b9bSZhang Rui return;
40690b9a0b9bSZhang Rui }
40700b9a0b9bSZhang Rui }
40710b9a0b9bSZhang Rui
remove_underbar(char * s)40721b439f01SLen Brown static void remove_underbar(char *s)
4073fecb3bc8SDavid Arcari {
4074fecb3bc8SDavid Arcari char *to = s;
4075fecb3bc8SDavid Arcari
4076fecb3bc8SDavid Arcari while (*s) {
4077fecb3bc8SDavid Arcari if (*s != '_')
4078fecb3bc8SDavid Arcari *to++ = *s;
4079fecb3bc8SDavid Arcari s++;
4080fecb3bc8SDavid Arcari }
4081fecb3bc8SDavid Arcari
4082fecb3bc8SDavid Arcari *to = 0;
4083fecb3bc8SDavid Arcari }
4084fecb3bc8SDavid Arcari
dump_turbo_ratio_info(unsigned int family,unsigned int model)40853ea8e52eSArtem Bityutskiy static void dump_turbo_ratio_info(unsigned int family, unsigned int model)
4086fcd17211SLen Brown {
40873ea8e52eSArtem Bityutskiy if (!has_turbo)
4088fcd17211SLen Brown return;
4089fcd17211SLen Brown
4090fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model))
4091fcd17211SLen Brown dump_hsw_turbo_ratio_limits();
4092fcd17211SLen Brown
4093fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model))
4094fcd17211SLen Brown dump_ivt_turbo_ratio_limits();
4095fcd17211SLen Brown
40964af184eeSLen Brown if (has_turbo_ratio_limit(family, model)) {
40974af184eeSLen Brown dump_turbo_ratio_limits(MSR_TURBO_RATIO_LIMIT, family, model);
40984af184eeSLen Brown
40994af184eeSLen Brown if (is_hybrid)
41004af184eeSLen Brown dump_turbo_ratio_limits(MSR_SECONDARY_TURBO_RATIO_LIMIT, family, model);
41014af184eeSLen Brown }
4102fcd17211SLen Brown
41030f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model))
41040f7887c4SLen Brown dump_atom_turbo_ratio_limits();
41050f7887c4SLen Brown
4106fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model))
4107fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits();
4108fb5d4327SDasaratharaman Chandramouli
41096fb3143bSLen Brown if (has_config_tdp(family, model))
41106fb3143bSLen Brown dump_config_tdp();
41113ea8e52eSArtem Bityutskiy }
41126fb3143bSLen Brown
dump_cstate_pstate_config_info(unsigned int family,unsigned int model)41133ea8e52eSArtem Bityutskiy static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
41143ea8e52eSArtem Bityutskiy {
41153ea8e52eSArtem Bityutskiy if (!do_nhm_platform_info)
41163ea8e52eSArtem Bityutskiy return;
41173ea8e52eSArtem Bityutskiy
41183ea8e52eSArtem Bityutskiy dump_nhm_platform_info();
41193ea8e52eSArtem Bityutskiy dump_turbo_ratio_info(family, model);
4120fcd17211SLen Brown dump_nhm_cst_cfg();
4121fcd17211SLen Brown }
4122fcd17211SLen Brown
read_sysfs_int(char * path)4123a5c6d65dSLen Brown static int read_sysfs_int(char *path)
4124a5c6d65dSLen Brown {
4125a5c6d65dSLen Brown FILE *input;
4126a5c6d65dSLen Brown int retval = -1;
4127a5c6d65dSLen Brown
4128a5c6d65dSLen Brown input = fopen(path, "r");
4129a5c6d65dSLen Brown if (input == NULL) {
4130a5c6d65dSLen Brown if (debug)
4131a5c6d65dSLen Brown fprintf(outf, "NSFOD %s\n", path);
4132a5c6d65dSLen Brown return (-1);
4133a5c6d65dSLen Brown }
4134a5c6d65dSLen Brown if (fscanf(input, "%d", &retval) != 1)
4135a5c6d65dSLen Brown err(1, "%s: failed to read int from file", path);
4136a5c6d65dSLen Brown fclose(input);
4137a5c6d65dSLen Brown
4138a5c6d65dSLen Brown return (retval);
4139a5c6d65dSLen Brown }
4140a5c6d65dSLen Brown
dump_sysfs_file(char * path)4141abdcbdb2SLen Brown static void dump_sysfs_file(char *path)
4142abdcbdb2SLen Brown {
4143abdcbdb2SLen Brown FILE *input;
4144abdcbdb2SLen Brown char cpuidle_buf[64];
4145abdcbdb2SLen Brown
4146abdcbdb2SLen Brown input = fopen(path, "r");
4147abdcbdb2SLen Brown if (input == NULL) {
4148abdcbdb2SLen Brown if (debug)
4149abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path);
4150abdcbdb2SLen Brown return;
4151abdcbdb2SLen Brown }
4152abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
4153abdcbdb2SLen Brown err(1, "%s: failed to read file", path);
4154abdcbdb2SLen Brown fclose(input);
4155abdcbdb2SLen Brown
4156abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
4157abdcbdb2SLen Brown }
41581b439f01SLen Brown
intel_uncore_frequency_probe(void)4159a5c6d65dSLen Brown static void intel_uncore_frequency_probe(void)
4160a5c6d65dSLen Brown {
4161a5c6d65dSLen Brown int i, j;
4162a5c6d65dSLen Brown char path[128];
4163a5c6d65dSLen Brown
4164a5c6d65dSLen Brown if (!genuine_intel)
4165a5c6d65dSLen Brown return;
4166a5c6d65dSLen Brown
4167a5c6d65dSLen Brown if (access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00", R_OK))
4168a5c6d65dSLen Brown return;
4169a5c6d65dSLen Brown
4170a5c6d65dSLen Brown if (!access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00/current_freq_khz", R_OK))
4171a5c6d65dSLen Brown BIC_PRESENT(BIC_UNCORE_MHZ);
4172a5c6d65dSLen Brown
4173a5c6d65dSLen Brown if (quiet)
4174a5c6d65dSLen Brown return;
4175a5c6d65dSLen Brown
4176a5c6d65dSLen Brown for (i = 0; i < topo.num_packages; ++i) {
4177a5c6d65dSLen Brown for (j = 0; j < topo.num_die; ++j) {
4178a5c6d65dSLen Brown int k, l;
4179a5c6d65dSLen Brown
4180a5c6d65dSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/min_freq_khz",
4181a5c6d65dSLen Brown i, j);
4182a5c6d65dSLen Brown k = read_sysfs_int(path);
4183a5c6d65dSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/max_freq_khz",
4184a5c6d65dSLen Brown i, j);
4185a5c6d65dSLen Brown l = read_sysfs_int(path);
4186a5c6d65dSLen Brown fprintf(outf, "Uncore Frequency pkg%d die%d: %d - %d MHz ", i, j, k / 1000, l / 1000);
4187a5c6d65dSLen Brown
4188a5c6d65dSLen Brown sprintf(path,
4189a5c6d65dSLen Brown "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_min_freq_khz",
4190a5c6d65dSLen Brown i, j);
4191a5c6d65dSLen Brown k = read_sysfs_int(path);
4192a5c6d65dSLen Brown sprintf(path,
4193a5c6d65dSLen Brown "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_max_freq_khz",
4194a5c6d65dSLen Brown i, j);
4195a5c6d65dSLen Brown l = read_sysfs_int(path);
4196a5c6d65dSLen Brown fprintf(outf, "(%d - %d MHz)\n", k / 1000, l / 1000);
4197a5c6d65dSLen Brown }
4198a5c6d65dSLen Brown }
4199a5c6d65dSLen Brown }
4200a5c6d65dSLen Brown
dump_sysfs_cstate_config(void)42011b439f01SLen Brown static void dump_sysfs_cstate_config(void)
420241618e63SLen Brown {
420341618e63SLen Brown char path[64];
420441618e63SLen Brown char name_buf[16];
420541618e63SLen Brown char desc[64];
420641618e63SLen Brown FILE *input;
420741618e63SLen Brown int state;
420841618e63SLen Brown char *sp;
420941618e63SLen Brown
4210abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
4211abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n");
4212abdcbdb2SLen Brown return;
4213abdcbdb2SLen Brown }
4214abdcbdb2SLen Brown
4215abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
4216abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
4217abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
4218abdcbdb2SLen Brown
421941618e63SLen Brown for (state = 0; state < 10; ++state) {
422041618e63SLen Brown
42211b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
422241618e63SLen Brown input = fopen(path, "r");
422341618e63SLen Brown if (input == NULL)
422441618e63SLen Brown continue;
42258173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input))
42268173c336SBen Hutchings err(1, "%s: failed to read file", path);
422741618e63SLen Brown
422841618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
422941618e63SLen Brown sp = strchr(name_buf, '-');
423041618e63SLen Brown if (!sp)
423141618e63SLen Brown sp = strchrnul(name_buf, '\n');
423241618e63SLen Brown *sp = '\0';
423341618e63SLen Brown fclose(input);
423441618e63SLen Brown
4235fecb3bc8SDavid Arcari remove_underbar(name_buf);
4236fecb3bc8SDavid Arcari
42371b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state);
423841618e63SLen Brown input = fopen(path, "r");
423941618e63SLen Brown if (input == NULL)
424041618e63SLen Brown continue;
42418173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input))
42428173c336SBen Hutchings err(1, "%s: failed to read file", path);
424341618e63SLen Brown
424441618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
424541618e63SLen Brown fclose(input);
424641618e63SLen Brown }
424741618e63SLen Brown }
42481b439f01SLen Brown
dump_sysfs_pstate_config(void)42491b439f01SLen Brown static void dump_sysfs_pstate_config(void)
42507293fccdSLen Brown {
42517293fccdSLen Brown char path[64];
42527293fccdSLen Brown char driver_buf[64];
42537293fccdSLen Brown char governor_buf[64];
42547293fccdSLen Brown FILE *input;
42557293fccdSLen Brown int turbo;
42567293fccdSLen Brown
42571b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu);
42587293fccdSLen Brown input = fopen(path, "r");
42597293fccdSLen Brown if (input == NULL) {
42600a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path);
42617293fccdSLen Brown return;
42627293fccdSLen Brown }
42638173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input))
42648173c336SBen Hutchings err(1, "%s: failed to read file", path);
42657293fccdSLen Brown fclose(input);
42667293fccdSLen Brown
42671b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu);
42687293fccdSLen Brown input = fopen(path, "r");
42697293fccdSLen Brown if (input == NULL) {
42700a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path);
42717293fccdSLen Brown return;
42727293fccdSLen Brown }
42738173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input))
42748173c336SBen Hutchings err(1, "%s: failed to read file", path);
42757293fccdSLen Brown fclose(input);
42767293fccdSLen Brown
42777293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
42787293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
42797293fccdSLen Brown
42807293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
42817293fccdSLen Brown input = fopen(path, "r");
42827293fccdSLen Brown if (input != NULL) {
42838173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1)
42848173c336SBen Hutchings err(1, "%s: failed to parse number from file", path);
42857293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo);
42867293fccdSLen Brown fclose(input);
42877293fccdSLen Brown }
42887293fccdSLen Brown
42897293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
42907293fccdSLen Brown input = fopen(path, "r");
42917293fccdSLen Brown if (input != NULL) {
42928173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1)
42938173c336SBen Hutchings err(1, "%s: failed to parse number from file", path);
42947293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
42957293fccdSLen Brown fclose(input);
42967293fccdSLen Brown }
42977293fccdSLen Brown }
429841618e63SLen Brown
4299889facbeSLen Brown /*
4300889facbeSLen Brown * print_epb()
4301889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR
4302889facbeSLen Brown */
print_epb(struct thread_data * t,struct core_data * c,struct pkg_data * p)4303889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4304889facbeSLen Brown {
4305889facbeSLen Brown char *epb_string;
43066d6501d9SBorislav Petkov int cpu, epb;
4307889facbeSLen Brown
43089878bf7aSLen Brown UNUSED(c);
43099878bf7aSLen Brown UNUSED(p);
43109878bf7aSLen Brown
4311889facbeSLen Brown if (!has_epb)
4312889facbeSLen Brown return 0;
4313889facbeSLen Brown
4314889facbeSLen Brown cpu = t->cpu_id;
4315889facbeSLen Brown
4316889facbeSLen Brown /* EPB is per-package */
4317889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4318889facbeSLen Brown return 0;
4319889facbeSLen Brown
4320889facbeSLen Brown if (cpu_migrate(cpu)) {
43213d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu);
4322889facbeSLen Brown return -1;
4323889facbeSLen Brown }
4324889facbeSLen Brown
43256d6501d9SBorislav Petkov epb = get_epb(cpu);
43266d6501d9SBorislav Petkov if (epb < 0)
4327889facbeSLen Brown return 0;
4328889facbeSLen Brown
43296d6501d9SBorislav Petkov switch (epb) {
4330889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE:
4331889facbeSLen Brown epb_string = "performance";
4332889facbeSLen Brown break;
4333889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL:
4334889facbeSLen Brown epb_string = "balanced";
4335889facbeSLen Brown break;
4336889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE:
4337889facbeSLen Brown epb_string = "powersave";
4338889facbeSLen Brown break;
4339889facbeSLen Brown default:
4340889facbeSLen Brown epb_string = "custom";
4341889facbeSLen Brown break;
4342889facbeSLen Brown }
43436d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string);
4344889facbeSLen Brown
4345889facbeSLen Brown return 0;
4346889facbeSLen Brown }
43471b439f01SLen Brown
43487f5c258eSLen Brown /*
43497f5c258eSLen Brown * print_hwp()
43507f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES
43517f5c258eSLen Brown */
print_hwp(struct thread_data * t,struct core_data * c,struct pkg_data * p)43527f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
43537f5c258eSLen Brown {
43547f5c258eSLen Brown unsigned long long msr;
43557f5c258eSLen Brown int cpu;
43567f5c258eSLen Brown
43579878bf7aSLen Brown UNUSED(c);
43589878bf7aSLen Brown UNUSED(p);
43599878bf7aSLen Brown
43607f5c258eSLen Brown if (!has_hwp)
43617f5c258eSLen Brown return 0;
43627f5c258eSLen Brown
43637f5c258eSLen Brown cpu = t->cpu_id;
43647f5c258eSLen Brown
43657f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */
43667f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
43677f5c258eSLen Brown return 0;
43687f5c258eSLen Brown
43697f5c258eSLen Brown if (cpu_migrate(cpu)) {
43703d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu);
43717f5c258eSLen Brown return -1;
43727f5c258eSLen Brown }
43737f5c258eSLen Brown
43747f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr))
43757f5c258eSLen Brown return 0;
43767f5c258eSLen Brown
43771b439f01SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-");
43787f5c258eSLen Brown
43797f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
43807f5c258eSLen Brown if ((msr & (1 << 0)) == 0)
43817f5c258eSLen Brown return 0;
43827f5c258eSLen Brown
43837f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
43847f5c258eSLen Brown return 0;
43857f5c258eSLen Brown
4386b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
43876dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n",
43887f5c258eSLen Brown cpu, msr,
43897f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr),
43907f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr),
43911b439f01SLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr));
43927f5c258eSLen Brown
43937f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
43947f5c258eSLen Brown return 0;
43957f5c258eSLen Brown
4396b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
43976dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
43987f5c258eSLen Brown cpu, msr,
43997f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff),
44007f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff),
44017f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff),
44027f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff),
44031b439f01SLen Brown (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1));
44047f5c258eSLen Brown
44057f5c258eSLen Brown if (has_hwp_pkg) {
44067f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
44077f5c258eSLen Brown return 0;
44087f5c258eSLen Brown
4409b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
44106dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n",
44117f5c258eSLen Brown cpu, msr,
44127f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff),
44137f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff),
44147f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff),
44151b439f01SLen Brown (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3));
44167f5c258eSLen Brown }
44177f5c258eSLen Brown if (has_hwp_notify) {
44187f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
44197f5c258eSLen Brown return 0;
44207f5c258eSLen Brown
4421b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
44227f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
44231b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis");
44247f5c258eSLen Brown }
44257f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr))
44267f5c258eSLen Brown return 0;
44277f5c258eSLen Brown
4428b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
44297f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
443092c25393SAntti Laakso cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x4) ? "" : "No-");
4431889facbeSLen Brown
4432889facbeSLen Brown return 0;
4433889facbeSLen Brown }
4434889facbeSLen Brown
44353a9a941dSLen Brown /*
44363a9a941dSLen Brown * print_perf_limit()
44373a9a941dSLen Brown */
print_perf_limit(struct thread_data * t,struct core_data * c,struct pkg_data * p)44383a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
44393a9a941dSLen Brown {
44403a9a941dSLen Brown unsigned long long msr;
44413a9a941dSLen Brown int cpu;
44423a9a941dSLen Brown
44439878bf7aSLen Brown UNUSED(c);
44449878bf7aSLen Brown UNUSED(p);
44459878bf7aSLen Brown
44463a9a941dSLen Brown cpu = t->cpu_id;
44473a9a941dSLen Brown
44483a9a941dSLen Brown /* per-package */
44493a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
44503a9a941dSLen Brown return 0;
44513a9a941dSLen Brown
44523a9a941dSLen Brown if (cpu_migrate(cpu)) {
44533d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu);
44543a9a941dSLen Brown return -1;
44553a9a941dSLen Brown }
44563a9a941dSLen Brown
44573a9a941dSLen Brown if (do_core_perf_limit_reasons) {
44583a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
4459b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4460b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
4461e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "",
44623a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "",
4463e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "",
4464e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
4465e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "",
4466e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "",
4467e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "",
4468e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "",
4469e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "",
4470e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "",
4471e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "",
4472e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "",
44731b439f01SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : "");
4474b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
4475e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "",
44763a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "",
4477e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "",
4478e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
4479e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "",
4480e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "",
4481e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "",
4482e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "",
4483e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "",
4484e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "",
4485e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "",
4486e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "",
44871b439f01SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : "");
44883a9a941dSLen Brown
44893a9a941dSLen Brown }
44903a9a941dSLen Brown if (do_gfx_perf_limit_reasons) {
44913a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
4492b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4493b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
44943a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "",
44953a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "",
44963a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "",
44973a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "",
44983a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "",
44993a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "",
45001b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4501b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
45023a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "",
45033a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "",
45043a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "",
45053a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "",
45063a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "",
45073a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "",
45081b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
45093a9a941dSLen Brown }
45103a9a941dSLen Brown if (do_ring_perf_limit_reasons) {
45113a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
4512b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4513b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)",
45143a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "",
45153a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "",
45163a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "",
45173a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "",
45181b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4519b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
45203a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "",
45213a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "",
45223a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "",
45233a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "",
45241b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
45253a9a941dSLen Brown }
45263a9a941dSLen Brown return 0;
45273a9a941dSLen Brown }
45283a9a941dSLen Brown
4529889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
4530889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
4531889facbeSLen Brown
get_tdp_intel(unsigned int model)45329392bd98SCalvin Walton double get_tdp_intel(unsigned int model)
4533144b44b1SLen Brown {
4534144b44b1SLen Brown unsigned long long msr;
4535144b44b1SLen Brown
4536144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO)
45377ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
4538144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
4539144b44b1SLen Brown
4540144b44b1SLen Brown switch (model) {
4541f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT:
45425ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D:
4543144b44b1SLen Brown return 30.0;
4544144b44b1SLen Brown default:
4545144b44b1SLen Brown return 135.0;
4546144b44b1SLen Brown }
4547144b44b1SLen Brown }
4548144b44b1SLen Brown
get_tdp_amd(unsigned int family)45499392bd98SCalvin Walton double get_tdp_amd(unsigned int family)
45509392bd98SCalvin Walton {
45519878bf7aSLen Brown UNUSED(family);
45529878bf7aSLen Brown
455333eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */
455433eb8225SKim Phillips return 280.0;
45559392bd98SCalvin Walton }
45569392bd98SCalvin Walton
455740ee8e3bSAndrey Semin /*
455840ee8e3bSAndrey Semin * rapl_dram_energy_units_probe()
455940ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
456040ee8e3bSAndrey Semin */
rapl_dram_energy_units_probe(int model,double rapl_energy_units)45611b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units)
456240ee8e3bSAndrey Semin {
456340ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */
456440ee8e3bSAndrey Semin
456540ee8e3bSAndrey Semin switch (model) {
4566869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */
4567869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */
4568abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */
4569869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
45706397b641SLen Brown case INTEL_FAM6_ICELAKE_X: /* ICX */
457140ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000);
457240ee8e3bSAndrey Semin default:
457340ee8e3bSAndrey Semin return (rapl_energy_units);
457440ee8e3bSAndrey Semin }
457540ee8e3bSAndrey Semin }
457640ee8e3bSAndrey Semin
rapl_probe_intel(unsigned int family,unsigned int model)45779392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model)
4578889facbeSLen Brown {
4579889facbeSLen Brown unsigned long long msr;
4580144b44b1SLen Brown unsigned int time_unit;
4581889facbeSLen Brown double tdp;
4582889facbeSLen Brown
4583889facbeSLen Brown if (family != 6)
4584889facbeSLen Brown return;
4585889facbeSLen Brown
4586889facbeSLen Brown switch (model) {
4587869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE:
4588869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE:
4589c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */
459077e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */
45915e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */
4592c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */
45935e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */
4594144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
4595812db3f7SLen Brown if (rapl_joules) {
4596812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J);
4597812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J);
4598812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J);
4599812db3f7SLen Brown } else {
4600812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt);
4601812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt);
4602812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt);
4603812db3f7SLen Brown }
4604889facbeSLen Brown break;
4605869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4606f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4607e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
4608812db3f7SLen Brown if (rapl_joules)
4609812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J);
4610812db3f7SLen Brown else
4611812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt);
4612e4085d54SLen Brown break;
4613f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */
46141b439f01SLen Brown do_rapl =
46151b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
46161b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO;
4617f6708400SChen Yu if (rapl_joules) {
4618f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J);
4619f6708400SChen Yu BIC_PRESENT(BIC_Cor_J);
4620f6708400SChen Yu BIC_PRESENT(BIC_RAM_J);
4621f6708400SChen Yu BIC_PRESENT(BIC_GFX_J);
4622f6708400SChen Yu } else {
4623f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt);
4624f6708400SChen Yu BIC_PRESENT(BIC_CorWatt);
4625f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt);
4626f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt);
4627f6708400SChen Yu }
4628f6708400SChen Yu break;
462920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
463020de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
463120de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__);
463220de0dabSAntti Laakso if (rapl_joules)
463320de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J);
463420de0dabSAntti Laakso else
463520de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt);
463620de0dabSAntti Laakso break;
4637af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */
4638af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */
46391b439f01SLen Brown do_rapl =
46401b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
46411b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO;
4642812db3f7SLen Brown BIC_PRESENT(BIC_PKG__);
4643812db3f7SLen Brown BIC_PRESENT(BIC_RAM__);
4644812db3f7SLen Brown if (rapl_joules) {
4645812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J);
4646812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J);
4647812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J);
464881824921SLen Brown BIC_PRESENT(BIC_GFX_J);
4649812db3f7SLen Brown } else {
4650812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt);
4651812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt);
4652812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt);
465381824921SLen Brown BIC_PRESENT(BIC_GFXWatt);
4654812db3f7SLen Brown }
46550b2bb692SLen Brown break;
4656869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */
4657869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */
4658869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */
465925368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */
4660684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
4661869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
46621b439f01SLen Brown do_rapl =
46631b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
46641b439f01SLen Brown RAPL_PKG_POWER_INFO;
4665812db3f7SLen Brown BIC_PRESENT(BIC_PKG__);
4666812db3f7SLen Brown BIC_PRESENT(BIC_RAM__);
4667812db3f7SLen Brown if (rapl_joules) {
4668812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J);
4669812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J);
4670812db3f7SLen Brown } else {
4671812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt);
4672812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt);
4673812db3f7SLen Brown }
4674e6f9bb3cSLen Brown break;
4675869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X:
4676869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X:
46771b439f01SLen Brown do_rapl =
46781b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS |
46791b439f01SLen Brown RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
4680812db3f7SLen Brown BIC_PRESENT(BIC_PKG__);
4681812db3f7SLen Brown BIC_PRESENT(BIC_RAM__);
4682812db3f7SLen Brown if (rapl_joules) {
4683812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J);
4684812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J);
4685812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J);
4686812db3f7SLen Brown } else {
4687812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt);
4688812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt);
4689812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt);
4690812db3f7SLen Brown }
4691144b44b1SLen Brown break;
4692f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
46935ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4694144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES;
4695812db3f7SLen Brown if (rapl_joules) {
4696812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J);
4697812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J);
4698812db3f7SLen Brown } else {
4699812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt);
4700812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt);
4701812db3f7SLen Brown }
4702889facbeSLen Brown break;
47035ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
47041b439f01SLen Brown do_rapl =
47051b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
47061b439f01SLen Brown RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
4707812db3f7SLen Brown BIC_PRESENT(BIC_PKG__);
4708812db3f7SLen Brown BIC_PRESENT(BIC_RAM__);
4709812db3f7SLen Brown if (rapl_joules) {
4710812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J);
4711812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J);
4712812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J);
4713812db3f7SLen Brown } else {
4714812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt);
4715812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt);
4716812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt);
4717812db3f7SLen Brown }
47180f644909SJacob Pan break;
4719889facbeSLen Brown default:
4720889facbeSLen Brown return;
4721889facbeSLen Brown }
4722889facbeSLen Brown
4723889facbeSLen Brown /* units on package 0, verify later other packages match */
47247ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4725889facbeSLen Brown return;
4726889facbeSLen Brown
4727889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF));
4728f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT)
4729144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4730144b44b1SLen Brown else
4731889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4732889facbeSLen Brown
473340ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
473440ee8e3bSAndrey Semin
4735144b44b1SLen Brown time_unit = msr >> 16 & 0xF;
4736144b44b1SLen Brown if (time_unit == 0)
4737144b44b1SLen Brown time_unit = 0xA;
4738889facbeSLen Brown
4739144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit));
4740144b44b1SLen Brown
47419392bd98SCalvin Walton tdp = get_tdp_intel(model);
4742889facbeSLen Brown
4743889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
474496e47158SLen Brown if (!quiet)
4745b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
47469392bd98SCalvin Walton }
4747889facbeSLen Brown
rapl_probe_amd(unsigned int family,unsigned int model)47489392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model)
47499392bd98SCalvin Walton {
47509392bd98SCalvin Walton unsigned long long msr;
47519392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx;
47529392bd98SCalvin Walton unsigned int has_rapl = 0;
47539392bd98SCalvin Walton double tdp;
47549392bd98SCalvin Walton
47559878bf7aSLen Brown UNUSED(model);
47569878bf7aSLen Brown
47579392bd98SCalvin Walton if (max_extended_level >= 0x80000007) {
47589392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx);
475933eb8225SKim Phillips /* RAPL (Fam 17h+) */
47609392bd98SCalvin Walton has_rapl = edx & (1 << 14);
47619392bd98SCalvin Walton }
47629392bd98SCalvin Walton
476333eb8225SKim Phillips if (!has_rapl || family < 0x17)
4764889facbeSLen Brown return;
47659392bd98SCalvin Walton
47669392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
47673316f99aSCalvin Walton if (rapl_joules) {
47683316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J);
47699392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J);
47703316f99aSCalvin Walton } else {
47713316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt);
47729392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt);
47733316f99aSCalvin Walton }
47749392bd98SCalvin Walton
47759392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
47769392bd98SCalvin Walton return;
47779392bd98SCalvin Walton
47789392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
47799392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
47809392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf));
47819392bd98SCalvin Walton
47829cfa8e04SPu Wen tdp = get_tdp_amd(family);
47839392bd98SCalvin Walton
47849392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
47859392bd98SCalvin Walton if (!quiet)
47869392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
47879392bd98SCalvin Walton }
47889392bd98SCalvin Walton
47899392bd98SCalvin Walton /*
47909392bd98SCalvin Walton * rapl_probe()
47919392bd98SCalvin Walton *
47929392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
47939392bd98SCalvin Walton */
rapl_probe(unsigned int family,unsigned int model)47949392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model)
47959392bd98SCalvin Walton {
47969392bd98SCalvin Walton if (genuine_intel)
47979392bd98SCalvin Walton rapl_probe_intel(family, model);
4798c1c10cc7SPu Wen if (authentic_amd || hygon_genuine)
47999392bd98SCalvin Walton rapl_probe_amd(family, model);
4800889facbeSLen Brown }
4801889facbeSLen Brown
perf_limit_reasons_probe(unsigned int family,unsigned int model)48021b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
48033a9a941dSLen Brown {
48043a9a941dSLen Brown if (!genuine_intel)
48053a9a941dSLen Brown return;
48063a9a941dSLen Brown
48073a9a941dSLen Brown if (family != 6)
48083a9a941dSLen Brown return;
48093a9a941dSLen Brown
48103a9a941dSLen Brown switch (model) {
4811c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */
481277e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */
48135e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */
48143a9a941dSLen Brown do_gfx_perf_limit_reasons = 1;
48159878bf7aSLen Brown /* FALLTHRU */
4816869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */
48173a9a941dSLen Brown do_core_perf_limit_reasons = 1;
48183a9a941dSLen Brown do_ring_perf_limit_reasons = 1;
48193a9a941dSLen Brown default:
48203a9a941dSLen Brown return;
48213a9a941dSLen Brown }
48223a9a941dSLen Brown }
48233a9a941dSLen Brown
automatic_cstate_conversion_probe(unsigned int family,unsigned int model)4824ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4825ac980e13SArtem Bityutskiy {
4826eade39b2SArtem Bityutskiy if (family != 6)
4827eade39b2SArtem Bityutskiy return;
4828eade39b2SArtem Bityutskiy
4829eade39b2SArtem Bityutskiy switch (model) {
4830eade39b2SArtem Bityutskiy case INTEL_FAM6_BROADWELL_X:
4831eade39b2SArtem Bityutskiy case INTEL_FAM6_SKYLAKE_X:
4832ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1;
4833ac980e13SArtem Bityutskiy }
4834eade39b2SArtem Bityutskiy }
4835ac980e13SArtem Bityutskiy
prewake_cstate_probe(unsigned int family,unsigned int model)4836aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model)
4837aeb01e6dSChen Yu {
4838684e40e9SArtem Bityutskiy if (is_icx(family, model) || is_spr(family, model))
4839aeb01e6dSChen Yu dis_cstate_prewake = 1;
4840aeb01e6dSChen Yu }
4841aeb01e6dSChen Yu
print_thermal(struct thread_data * t,struct core_data * c,struct pkg_data * p)4842889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4843889facbeSLen Brown {
4844889facbeSLen Brown unsigned long long msr;
4845f4896fa5SLen Brown unsigned int dts, dts2;
4846889facbeSLen Brown int cpu;
4847889facbeSLen Brown
48489878bf7aSLen Brown UNUSED(c);
48499878bf7aSLen Brown UNUSED(p);
48509878bf7aSLen Brown
4851889facbeSLen Brown if (!(do_dts || do_ptm))
4852889facbeSLen Brown return 0;
4853889facbeSLen Brown
4854889facbeSLen Brown cpu = t->cpu_id;
4855889facbeSLen Brown
4856889facbeSLen Brown /* DTS is per-core, no need to print for each thread */
4857889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4858889facbeSLen Brown return 0;
4859889facbeSLen Brown
4860889facbeSLen Brown if (cpu_migrate(cpu)) {
48613d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu);
4862889facbeSLen Brown return -1;
4863889facbeSLen Brown }
4864889facbeSLen Brown
4865889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4866889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4867889facbeSLen Brown return 0;
4868889facbeSLen Brown
4869889facbeSLen Brown dts = (msr >> 16) & 0x7F;
48701b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts);
4871889facbeSLen Brown
4872889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4873889facbeSLen Brown return 0;
4874889facbeSLen Brown
4875889facbeSLen Brown dts = (msr >> 16) & 0x7F;
4876889facbeSLen Brown dts2 = (msr >> 8) & 0x7F;
4877b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
487855279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2);
4879889facbeSLen Brown }
4880889facbeSLen Brown
4881f4896fa5SLen Brown if (do_dts && debug) {
4882889facbeSLen Brown unsigned int resolution;
4883889facbeSLen Brown
4884889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4885889facbeSLen Brown return 0;
4886889facbeSLen Brown
4887889facbeSLen Brown dts = (msr >> 16) & 0x7F;
4888889facbeSLen Brown resolution = (msr >> 27) & 0xF;
4889b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
489055279aefSZhang Rui cpu, msr, tj_max - dts, resolution);
4891889facbeSLen Brown
4892889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4893889facbeSLen Brown return 0;
4894889facbeSLen Brown
4895889facbeSLen Brown dts = (msr >> 16) & 0x7F;
4896889facbeSLen Brown dts2 = (msr >> 8) & 0x7F;
4897b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
489855279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2);
4899889facbeSLen Brown }
4900889facbeSLen Brown
4901889facbeSLen Brown return 0;
4902889facbeSLen Brown }
4903889facbeSLen Brown
print_power_limit_msr(int cpu,unsigned long long msr,char * label)4904889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4905889facbeSLen Brown {
49066b398625SSumeet Pawnikar fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n",
4907889facbeSLen Brown cpu, label,
4908889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS",
4909889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units,
4910889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4911889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS"));
4912889facbeSLen Brown
4913889facbeSLen Brown return;
4914889facbeSLen Brown }
4915889facbeSLen Brown
print_rapl(struct thread_data * t,struct core_data * c,struct pkg_data * p)4916889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4917889facbeSLen Brown {
4918889facbeSLen Brown unsigned long long msr;
49199392bd98SCalvin Walton const char *msr_name;
4920889facbeSLen Brown int cpu;
4921889facbeSLen Brown
49229878bf7aSLen Brown UNUSED(c);
49239878bf7aSLen Brown UNUSED(p);
49249878bf7aSLen Brown
4925889facbeSLen Brown if (!do_rapl)
4926889facbeSLen Brown return 0;
4927889facbeSLen Brown
4928889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */
4929889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4930889facbeSLen Brown return 0;
4931889facbeSLen Brown
4932889facbeSLen Brown cpu = t->cpu_id;
4933889facbeSLen Brown if (cpu_migrate(cpu)) {
49343d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu);
4935889facbeSLen Brown return -1;
4936889facbeSLen Brown }
4937889facbeSLen Brown
49389392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) {
49399392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT";
49409392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
49419392bd98SCalvin Walton return -1;
49429392bd98SCalvin Walton } else {
49439392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT";
4944889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4945889facbeSLen Brown return -1;
49469392bd98SCalvin Walton }
4947889facbeSLen Brown
49489392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4949144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units);
495096e47158SLen Brown
4951144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) {
4952144b44b1SLen Brown
4953889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4954889facbeSLen Brown return -5;
4955889facbeSLen Brown
4956b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4957889facbeSLen Brown cpu, msr,
4958889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4959889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4960889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4961889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4962889facbeSLen Brown
4963144b44b1SLen Brown }
4964144b44b1SLen Brown if (do_rapl & RAPL_PKG) {
4965144b44b1SLen Brown
4966889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4967889facbeSLen Brown return -9;
4968889facbeSLen Brown
4969b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4970889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4971889facbeSLen Brown
4972889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1");
49736b398625SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n",
4974889facbeSLen Brown cpu,
4975889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS",
4976889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units,
4977889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4978889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS");
4979f52ba931SSumeet Pawnikar
4980f52ba931SSumeet Pawnikar if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr))
4981f52ba931SSumeet Pawnikar return -9;
4982f52ba931SSumeet Pawnikar
4983f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr);
4984f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n",
4985164d7a96SLen Brown cpu, ((msr >> 0) & 0x1FFF) * rapl_power_units, (msr >> 31) & 1 ? "" : "UN");
4986889facbeSLen Brown }
4987889facbeSLen Brown
49880b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) {
4989889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4990889facbeSLen Brown return -6;
4991889facbeSLen Brown
4992b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4993889facbeSLen Brown cpu, msr,
4994889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4995889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4996889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4997889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
49980b2bb692SLen Brown }
49990b2bb692SLen Brown if (do_rapl & RAPL_DRAM) {
5000889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
5001889facbeSLen Brown return -9;
5002b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
5003889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5004889facbeSLen Brown
5005889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit");
5006889facbeSLen Brown }
5007144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) {
5008889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr))
5009889facbeSLen Brown return -7;
5010889facbeSLen Brown
5011b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
5012144b44b1SLen Brown }
50139148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) {
5014889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
5015889facbeSLen Brown return -9;
5016b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
5017889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5018889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit");
5019889facbeSLen Brown }
5020889facbeSLen Brown if (do_rapl & RAPL_GFX) {
5021889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr))
5022889facbeSLen Brown return -8;
5023889facbeSLen Brown
5024b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
5025889facbeSLen Brown
5026889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
5027889facbeSLen Brown return -9;
5028b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
5029889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5030889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit");
5031889facbeSLen Brown }
5032889facbeSLen Brown return 0;
5033889facbeSLen Brown }
5034889facbeSLen Brown
5035d7899447SLen Brown /*
5036d7899447SLen Brown * SNB adds support for additional MSRs:
5037d7899447SLen Brown *
5038d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa
5039d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe
5040d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d
5041d7899447SLen Brown */
5042103a8feaSLen Brown
has_snb_msrs(unsigned int family,unsigned int model)5043d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
5044103a8feaSLen Brown {
5045103a8feaSLen Brown if (!genuine_intel)
5046103a8feaSLen Brown return 0;
5047103a8feaSLen Brown
50489878bf7aSLen Brown if (family != 6)
50499878bf7aSLen Brown return 0;
50509878bf7aSLen Brown
5051103a8feaSLen Brown switch (model) {
5052869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE:
5053869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X:
5054869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */
5055869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
5056c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */
5057869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */
505877e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */
50595e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */
5060c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */
50615e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */
5062869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */
5063af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */
5064af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */
5065869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */
506625368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */
5067684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
5068869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5069f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
50705ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
5071f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */
507220de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
5073103a8feaSLen Brown return 1;
5074103a8feaSLen Brown }
5075103a8feaSLen Brown return 0;
5076103a8feaSLen Brown }
5077103a8feaSLen Brown
5078d7899447SLen Brown /*
5079570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs:
5080d7899447SLen Brown *
5081d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630
5082d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631
5083d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632
50845a63426eSLen Brown *
50855a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633
50865a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634
50875a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635
50885a63426eSLen Brown *
5089d7899447SLen Brown */
has_c8910_msrs(unsigned int family,unsigned int model)5090570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model)
5091ca58710fSKristen Carlson Accardi {
5092ca58710fSKristen Carlson Accardi if (!genuine_intel)
5093ca58710fSKristen Carlson Accardi return 0;
5094ca58710fSKristen Carlson Accardi
50959878bf7aSLen Brown if (family != 6)
50969878bf7aSLen Brown return 0;
50979878bf7aSLen Brown
5098ca58710fSKristen Carlson Accardi switch (model) {
509977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */
5100c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */
5101af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */
5102af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */
5103869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5104f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5105f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */
5106ca58710fSKristen Carlson Accardi return 1;
5107ca58710fSKristen Carlson Accardi }
5108ca58710fSKristen Carlson Accardi return 0;
5109ca58710fSKristen Carlson Accardi }
5110ca58710fSKristen Carlson Accardi
51110b2bb692SLen Brown /*
51120b2bb692SLen Brown * SKL adds support for additional MSRS:
51130b2bb692SLen Brown *
51140b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
51150b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659
51160b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
51170b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
51180b2bb692SLen Brown */
has_skl_msrs(unsigned int family,unsigned int model)51190b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
51200b2bb692SLen Brown {
51210b2bb692SLen Brown if (!genuine_intel)
51220b2bb692SLen Brown return 0;
51230b2bb692SLen Brown
51249878bf7aSLen Brown if (family != 6)
51259878bf7aSLen Brown return 0;
51269878bf7aSLen Brown
51270b2bb692SLen Brown switch (model) {
5128af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */
5129af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */
51300b2bb692SLen Brown return 1;
51310b2bb692SLen Brown }
51320b2bb692SLen Brown return 0;
51330b2bb692SLen Brown }
51340b2bb692SLen Brown
is_slm(unsigned int family,unsigned int model)5135144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
5136144b44b1SLen Brown {
5137144b44b1SLen Brown if (!genuine_intel)
5138144b44b1SLen Brown return 0;
51399878bf7aSLen Brown
51409878bf7aSLen Brown if (family != 6)
51419878bf7aSLen Brown return 0;
51429878bf7aSLen Brown
5143144b44b1SLen Brown switch (model) {
5144f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
51455ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
5146144b44b1SLen Brown return 1;
5147144b44b1SLen Brown }
5148144b44b1SLen Brown return 0;
5149144b44b1SLen Brown }
5150144b44b1SLen Brown
is_knl(unsigned int family,unsigned int model)5151fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
5152fb5d4327SDasaratharaman Chandramouli {
5153fb5d4327SDasaratharaman Chandramouli if (!genuine_intel)
5154fb5d4327SDasaratharaman Chandramouli return 0;
51559878bf7aSLen Brown
51569878bf7aSLen Brown if (family != 6)
51579878bf7aSLen Brown return 0;
51589878bf7aSLen Brown
5159fb5d4327SDasaratharaman Chandramouli switch (model) {
5160869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
5161fb5d4327SDasaratharaman Chandramouli return 1;
5162fb5d4327SDasaratharaman Chandramouli }
5163fb5d4327SDasaratharaman Chandramouli return 0;
5164fb5d4327SDasaratharaman Chandramouli }
5165fb5d4327SDasaratharaman Chandramouli
is_cnl(unsigned int family,unsigned int model)5166997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
5167997e5395SSrinivas Pandruvada {
5168997e5395SSrinivas Pandruvada if (!genuine_intel)
5169997e5395SSrinivas Pandruvada return 0;
5170997e5395SSrinivas Pandruvada
51719878bf7aSLen Brown if (family != 6)
51729878bf7aSLen Brown return 0;
51739878bf7aSLen Brown
5174997e5395SSrinivas Pandruvada switch (model) {
5175af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */
5176997e5395SSrinivas Pandruvada return 1;
5177997e5395SSrinivas Pandruvada }
5178997e5395SSrinivas Pandruvada
5179997e5395SSrinivas Pandruvada return 0;
5180997e5395SSrinivas Pandruvada }
5181997e5395SSrinivas Pandruvada
get_aperf_mperf_multiplier(unsigned int family,unsigned int model)5182b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
5183b2b34dfeSHubert Chrzaniuk {
5184b2b34dfeSHubert Chrzaniuk if (is_knl(family, model))
5185b2b34dfeSHubert Chrzaniuk return 1024;
5186b2b34dfeSHubert Chrzaniuk return 1;
5187b2b34dfeSHubert Chrzaniuk }
5188b2b34dfeSHubert Chrzaniuk
5189144b44b1SLen Brown #define SLM_BCLK_FREQS 5
5190144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 };
5191144b44b1SLen Brown
slm_bclk(void)5192144b44b1SLen Brown double slm_bclk(void)
5193144b44b1SLen Brown {
5194144b44b1SLen Brown unsigned long long msr = 3;
5195144b44b1SLen Brown unsigned int i;
5196144b44b1SLen Brown double freq;
5197144b44b1SLen Brown
51987ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
5199b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n");
5200144b44b1SLen Brown
5201144b44b1SLen Brown i = msr & 0xf;
5202144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) {
5203b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i);
52040a91e551SColin Ian King i = 3;
5205144b44b1SLen Brown }
5206144b44b1SLen Brown freq = slm_freq_table[i];
5207144b44b1SLen Brown
520896e47158SLen Brown if (!quiet)
5209b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
5210144b44b1SLen Brown
5211144b44b1SLen Brown return freq;
5212144b44b1SLen Brown }
5213144b44b1SLen Brown
discover_bclk(unsigned int family,unsigned int model)5214103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
5215103a8feaSLen Brown {
5216121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model))
5217103a8feaSLen Brown return 100.00;
5218144b44b1SLen Brown else if (is_slm(family, model))
5219144b44b1SLen Brown return slm_bclk();
5220103a8feaSLen Brown else
5221103a8feaSLen Brown return 133.33;
5222103a8feaSLen Brown }
5223103a8feaSLen Brown
get_cpu_type(struct thread_data * t,struct core_data * c,struct pkg_data * p)52247ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p)
52257ab5ff49SZhang Rui {
52267ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx;
52277ab5ff49SZhang Rui
52289878bf7aSLen Brown UNUSED(c);
52299878bf7aSLen Brown UNUSED(p);
52309878bf7aSLen Brown
52317ab5ff49SZhang Rui if (!genuine_intel)
52327ab5ff49SZhang Rui return 0;
52337ab5ff49SZhang Rui
52347ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) {
52357ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id);
52367ab5ff49SZhang Rui return -1;
52377ab5ff49SZhang Rui }
52387ab5ff49SZhang Rui
52397ab5ff49SZhang Rui if (max_level < 0x1a)
52407ab5ff49SZhang Rui return 0;
52417ab5ff49SZhang Rui
52427ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx);
52437ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF;
52447ab5ff49SZhang Rui if (eax == 0x20)
52457ab5ff49SZhang Rui t->is_atom = true;
52467ab5ff49SZhang Rui return 0;
52477ab5ff49SZhang Rui }
52487ab5ff49SZhang Rui
5249889facbeSLen Brown /*
5250889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
5251889facbeSLen Brown * the Thermal Control Circuit (TCC) activates.
5252889facbeSLen Brown * This is usually equal to tjMax.
5253889facbeSLen Brown *
5254889facbeSLen Brown * Older processors do not have this MSR, so there we guess,
5255889facbeSLen Brown * but also allow cmdline over-ride with -T.
5256889facbeSLen Brown *
5257889facbeSLen Brown * Several MSR temperature values are in units of degrees-C
5258889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS),
5259889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds.
5260889facbeSLen Brown */
set_temperature_target(struct thread_data * t,struct core_data * c,struct pkg_data * p)52616ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
52626ff7cb37SLen Brown {
5263b2b94be7SLen Brown unsigned long long msr;
526455279aefSZhang Rui unsigned int tcc_default, tcc_offset;
5265b2b94be7SLen Brown int cpu;
5266b2b94be7SLen Brown
52679878bf7aSLen Brown UNUSED(c);
52689878bf7aSLen Brown UNUSED(p);
52699878bf7aSLen Brown
527055279aefSZhang Rui /* tj_max is used only for dts or ptm */
5271889facbeSLen Brown if (!(do_dts || do_ptm))
5272889facbeSLen Brown return 0;
5273889facbeSLen Brown
5274889facbeSLen Brown /* this is a per-package concept */
5275889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
5276889facbeSLen Brown return 0;
5277889facbeSLen Brown
5278b2b94be7SLen Brown cpu = t->cpu_id;
5279b2b94be7SLen Brown if (cpu_migrate(cpu)) {
5280b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu);
5281b2b94be7SLen Brown return -1;
5282b2b94be7SLen Brown }
5283b2b94be7SLen Brown
528455279aefSZhang Rui if (tj_max_override != 0) {
528555279aefSZhang Rui tj_max = tj_max_override;
52861b439f01SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max);
5287889facbeSLen Brown return 0;
5288889facbeSLen Brown }
5289889facbeSLen Brown
5290b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */
5291b2b94be7SLen Brown if (!do_nhm_platform_info)
5292b2b94be7SLen Brown goto guess;
5293b2b94be7SLen Brown
5294b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
5295b2b94be7SLen Brown goto guess;
5296b2b94be7SLen Brown
529755279aefSZhang Rui tcc_default = (msr >> 16) & 0xFF;
5298b2b94be7SLen Brown
52990b9a0b9bSZhang Rui if (!quiet) {
53000b9a0b9bSZhang Rui switch (tcc_offset_bits) {
53010b9a0b9bSZhang Rui case 4:
53020b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0xF;
53030b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
530455279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
53050b9a0b9bSZhang Rui break;
53060b9a0b9bSZhang Rui case 6:
53070b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0x3F;
53080b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
530955279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
53100b9a0b9bSZhang Rui break;
53110b9a0b9bSZhang Rui default:
53121b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default);
53130b9a0b9bSZhang Rui break;
53140b9a0b9bSZhang Rui }
53150b9a0b9bSZhang Rui }
5316b2b94be7SLen Brown
531755279aefSZhang Rui if (!tcc_default)
5318b2b94be7SLen Brown goto guess;
5319b2b94be7SLen Brown
532055279aefSZhang Rui tj_max = tcc_default;
5321b2b94be7SLen Brown
5322889facbeSLen Brown return 0;
5323889facbeSLen Brown
5324b2b94be7SLen Brown guess:
532555279aefSZhang Rui tj_max = TJMAX_DEFAULT;
53261b439f01SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max);
5327889facbeSLen Brown
5328889facbeSLen Brown return 0;
5329889facbeSLen Brown }
533069807a63SLen Brown
decode_feature_control_msr(void)5331aa8d8cc7SLen Brown void decode_feature_control_msr(void)
5332aa8d8cc7SLen Brown {
5333aa8d8cc7SLen Brown unsigned long long msr;
5334aa8d8cc7SLen Brown
5335f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
5336aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
53371b439f01SLen Brown base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : "");
5338aa8d8cc7SLen Brown }
5339aa8d8cc7SLen Brown
decode_misc_enable_msr(void)534069807a63SLen Brown void decode_misc_enable_msr(void)
534169807a63SLen Brown {
534269807a63SLen Brown unsigned long long msr;
534369807a63SLen Brown
5344f26b1519SLen Brown if (!genuine_intel)
5345f26b1519SLen Brown return;
5346f26b1519SLen Brown
534769807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
5348e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
534969807a63SLen Brown base_cpu, msr,
5350e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
5351e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
5352fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
5353e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
5354e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
535569807a63SLen Brown }
535669807a63SLen Brown
decode_misc_feature_control(void)535733148d67SLen Brown void decode_misc_feature_control(void)
535833148d67SLen Brown {
535933148d67SLen Brown unsigned long long msr;
536033148d67SLen Brown
536133148d67SLen Brown if (!has_misc_feature_control)
536233148d67SLen Brown return;
536333148d67SLen Brown
536433148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
53651b439f01SLen Brown fprintf(outf,
53661b439f01SLen Brown "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
53671b439f01SLen Brown base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "",
53681b439f01SLen Brown msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : "");
536933148d67SLen Brown }
53701b439f01SLen Brown
5371f0057310SLen Brown /*
5372f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT
5373f0057310SLen Brown *
5374f0057310SLen Brown * Decode the bits according to the Nehalem documentation
5375f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward
5376f0057310SLen Brown * bit[1] less so...
5377f0057310SLen Brown */
decode_misc_pwr_mgmt_msr(void)5378f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
5379f0057310SLen Brown {
5380f0057310SLen Brown unsigned long long msr;
5381f0057310SLen Brown
5382f0057310SLen Brown if (!do_nhm_platform_info)
5383f0057310SLen Brown return;
5384f0057310SLen Brown
5385cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT)
5386cf4cbe53SLen Brown return;
5387cf4cbe53SLen Brown
5388f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
5389ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
5390f0057310SLen Brown base_cpu, msr,
53911b439f01SLen Brown msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS");
5392f0057310SLen Brown }
53931b439f01SLen Brown
539471616c8eSLen Brown /*
539571616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
539671616c8eSLen Brown *
539771616c8eSLen Brown * This MSRs are present on Silvermont processors,
539871616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends.
539971616c8eSLen Brown */
decode_c6_demotion_policy_msr(void)540071616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
540171616c8eSLen Brown {
540271616c8eSLen Brown unsigned long long msr;
540371616c8eSLen Brown
540471616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
540571616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
540671616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
540771616c8eSLen Brown
540871616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
540971616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
541071616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
541171616c8eSLen Brown }
54127f5c258eSLen Brown
5413f5a4c76aSLen Brown /*
5414f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse
5415f5a4c76aSLen Brown */
intel_model_duplicates(unsigned int model)5416f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model)
5417f5a4c76aSLen Brown {
5418f5a4c76aSLen Brown
5419f5a4c76aSLen Brown switch (model) {
5420f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
5421f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
5422f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */
5423f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
5424f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
5425f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM;
5426f5a4c76aSLen Brown
5427f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
5428f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
5429f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX;
5430f5a4c76aSLen Brown
5431f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM:
5432f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL;
5433f5a4c76aSLen Brown
5434f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X:
54355ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */
5436f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X;
5437f5a4c76aSLen Brown
5438af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L:
5439c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE:
5440af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L:
5441c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE:
5442081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L:
5443081c5432SChen Yu case INTEL_FAM6_COMETLAKE:
5444af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L;
5445937807d3SLen Brown
5446af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L:
5447d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI:
54484bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L:
54494bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE:
5450e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE:
5451e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD:
5452e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE:
54535683460bSChen Yu case INTEL_FAM6_ALDERLAKE_L:
5454882cdb06SPeter Zijlstra case INTEL_FAM6_ATOM_GRACEMONT:
5455fb5e29dfSZhang Rui case INTEL_FAM6_RAPTORLAKE:
54566f9cf553SGeorge D Sworo case INTEL_FAM6_RAPTORLAKE_P:
54579b1c2ecfSZhang Rui case INTEL_FAM6_RAPTORLAKE_S:
54588e45a9bfSZhang Rui case INTEL_FAM6_METEORLAKE:
54598e45a9bfSZhang Rui case INTEL_FAM6_METEORLAKE_L:
5460af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L;
5461b62b3184SZhang Rui
5462d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L:
5463d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT;
5464d7814c30SChen Yu
54656c5c6560SChen Yu case INTEL_FAM6_ICELAKE_D:
546625368d7cSChen Yu return INTEL_FAM6_ICELAKE_X;
546793cac415SZhang Rui
546893cac415SZhang Rui case INTEL_FAM6_EMERALDRAPIDS_X:
546993cac415SZhang Rui return INTEL_FAM6_SAPPHIRERAPIDS_X;
5470f5a4c76aSLen Brown }
5471f5a4c76aSLen Brown return model;
5472f5a4c76aSLen Brown }
5473d76bb7a0SLen Brown
print_dev_latency(void)5474d76bb7a0SLen Brown void print_dev_latency(void)
5475d76bb7a0SLen Brown {
5476d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency";
5477d76bb7a0SLen Brown int fd;
5478d76bb7a0SLen Brown int value;
5479d76bb7a0SLen Brown int retval;
5480d76bb7a0SLen Brown
5481d76bb7a0SLen Brown fd = open(path, O_RDONLY);
5482d76bb7a0SLen Brown if (fd < 0) {
54839678a79dSLen Brown if (debug)
54849678a79dSLen Brown warnx("Read %s failed", path);
5485d76bb7a0SLen Brown return;
5486d76bb7a0SLen Brown }
5487d76bb7a0SLen Brown
5488d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int));
5489d76bb7a0SLen Brown if (retval != sizeof(int)) {
54906cbfedc7SLen Brown warn("read failed %s", path);
5491d76bb7a0SLen Brown close(fd);
5492d76bb7a0SLen Brown return;
5493d76bb7a0SLen Brown }
54941b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained");
5495d76bb7a0SLen Brown
5496d76bb7a0SLen Brown close(fd);
5497d76bb7a0SLen Brown }
5498d76bb7a0SLen Brown
54992af4f9b8SLen Brown /*
55002db0e5ebSJiang Jian * Linux-perf manages the HW instructions-retired counter
55012af4f9b8SLen Brown * by enabling when requested, and hiding rollover
55022af4f9b8SLen Brown */
linux_perf_init(void)55032af4f9b8SLen Brown void linux_perf_init(void)
55042af4f9b8SLen Brown {
55052af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC))
55062af4f9b8SLen Brown return;
55072af4f9b8SLen Brown
55082af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK))
55092af4f9b8SLen Brown return;
55102af4f9b8SLen Brown
55112af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
55122af4f9b8SLen Brown if (fd_instr_count_percpu == NULL)
55132af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu");
55142af4f9b8SLen Brown
55152af4f9b8SLen Brown BIC_PRESENT(BIC_IPC);
55162af4f9b8SLen Brown }
55172af4f9b8SLen Brown
process_cpuid()5518fcd17211SLen Brown void process_cpuid()
5519103a8feaSLen Brown {
552034041551SLen Brown unsigned int eax, ebx, ecx, edx;
552134041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
5522ed0757b8SLen Brown unsigned long long ucode_patch = 0;
55238a91872aSPatryk Wlazlyn bool ucode_patch_valid = false;
5524103a8feaSLen Brown
5525103a8feaSLen Brown eax = ebx = ecx = edx = 0;
5526103a8feaSLen Brown
55275aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx);
5528103a8feaSLen Brown
552934041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
5530103a8feaSLen Brown genuine_intel = 1;
553134041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
553234041551SLen Brown authentic_amd = 1;
5533c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
5534c1c10cc7SPu Wen hygon_genuine = 1;
5535103a8feaSLen Brown
553696e47158SLen Brown if (!quiet)
5537ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n",
5538ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level);
5539103a8feaSLen Brown
55405aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx);
5541103a8feaSLen Brown family = (fms >> 8) & 0xf;
5542103a8feaSLen Brown model = (fms >> 4) & 0xf;
5543103a8feaSLen Brown stepping = fms & 0xf;
55445aa3d1a2SCalvin Walton if (family == 0xf)
55455aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff;
55465aa3d1a2SCalvin Walton if (family >= 6)
5547103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4;
554834041551SLen Brown ecx_flags = ecx;
554934041551SLen Brown edx_flags = edx;
5550103a8feaSLen Brown
5551ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
55526cbfedc7SLen Brown warnx("get_msr(UCODE)");
55538a91872aSPatryk Wlazlyn else
55548a91872aSPatryk Wlazlyn ucode_patch_valid = true;
5555ed0757b8SLen Brown
5556103a8feaSLen Brown /*
5557103a8feaSLen Brown * check max extended function levels of CPUID.
5558103a8feaSLen Brown * This is needed to check for invariant TSC.
5559103a8feaSLen Brown * This check is valid for both Intel and AMD.
5560103a8feaSLen Brown */
5561103a8feaSLen Brown ebx = ecx = edx = 0;
55625aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
5563103a8feaSLen Brown
556434041551SLen Brown if (!quiet) {
55658a91872aSPatryk Wlazlyn fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d)",
55668a91872aSPatryk Wlazlyn family, model, stepping, family, model, stepping);
55678a91872aSPatryk Wlazlyn if (ucode_patch_valid)
55688a91872aSPatryk Wlazlyn fprintf(outf, " microcode 0x%x", (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
55698a91872aSPatryk Wlazlyn fputc('\n', outf);
55708a91872aSPatryk Wlazlyn
5571ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
557234041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
557334041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-",
557434041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-",
557534041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-",
557634041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-",
557734041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-",
557834041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-",
557934041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-",
558034041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-",
55811b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-");
558234041551SLen Brown }
558325368d7cSChen Yu if (genuine_intel) {
5584e9d3092fSZhang Rui model_orig = model;
5585f5a4c76aSLen Brown model = intel_model_duplicates(model);
558625368d7cSChen Yu }
558734041551SLen Brown
558834041551SLen Brown if (!(edx_flags & (1 << 5)))
558934041551SLen Brown errx(1, "CPUID: no MSR");
559034041551SLen Brown
559161a87ba7SLen Brown if (max_extended_level >= 0x80000007) {
5592103a8feaSLen Brown
5593103a8feaSLen Brown /*
5594103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
5595103a8feaSLen Brown * this check is valid for both Intel and AMD
5596103a8feaSLen Brown */
55975aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx);
55988209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8);
5599d7899447SLen Brown }
5600103a8feaSLen Brown
5601103a8feaSLen Brown /*
5602103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
5603103a8feaSLen Brown * this check is valid for both Intel and AMD
5604103a8feaSLen Brown */
5605103a8feaSLen Brown
56065aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx);
56078209e054SThomas Renninger has_aperf = ecx & (1 << 0);
5608812db3f7SLen Brown if (has_aperf) {
5609812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz);
5610812db3f7SLen Brown BIC_PRESENT(BIC_Busy);
5611812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz);
5612812db3f7SLen Brown }
5613889facbeSLen Brown do_dts = eax & (1 << 0);
5614812db3f7SLen Brown if (do_dts)
5615812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp);
5616b3a34e93SLen Brown has_turbo = eax & (1 << 1);
5617889facbeSLen Brown do_ptm = eax & (1 << 6);
5618812db3f7SLen Brown if (do_ptm)
5619812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp);
56207f5c258eSLen Brown has_hwp = eax & (1 << 7);
56217f5c258eSLen Brown has_hwp_notify = eax & (1 << 8);
56227f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9);
56237f5c258eSLen Brown has_hwp_epp = eax & (1 << 10);
56247f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11);
5625889facbeSLen Brown has_epb = ecx & (1 << 3);
5626889facbeSLen Brown
562796e47158SLen Brown if (!quiet)
5628b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
56297f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
56307f5c258eSLen Brown has_aperf ? "" : "No-",
5631b3a34e93SLen Brown has_turbo ? "" : "No-",
56327f5c258eSLen Brown do_dts ? "" : "No-",
56337f5c258eSLen Brown do_ptm ? "" : "No-",
56347f5c258eSLen Brown has_hwp ? "" : "No-",
56357f5c258eSLen Brown has_hwp_notify ? "" : "No-",
56367f5c258eSLen Brown has_hwp_activity_window ? "" : "No-",
56371b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-");
5638103a8feaSLen Brown
563996e47158SLen Brown if (!quiet)
564069807a63SLen Brown decode_misc_enable_msr();
564169807a63SLen Brown
564296e47158SLen Brown if (max_level >= 0x7 && !quiet) {
5643aa8d8cc7SLen Brown int has_sgx;
5644aa8d8cc7SLen Brown
5645aa8d8cc7SLen Brown ecx = 0;
5646aa8d8cc7SLen Brown
5647aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
5648aa8d8cc7SLen Brown
5649aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2);
5650774627c5SLen Brown
5651774627c5SLen Brown is_hybrid = edx & (1 << 15);
5652774627c5SLen Brown
5653774627c5SLen Brown fprintf(outf, "CPUID(7): %sSGX %sHybrid\n", has_sgx ? "" : "No-", is_hybrid ? "" : "No-");
5654aa8d8cc7SLen Brown
5655aa8d8cc7SLen Brown if (has_sgx)
5656aa8d8cc7SLen Brown decode_feature_control_msr();
5657aa8d8cc7SLen Brown }
5658aa8d8cc7SLen Brown
565961a87ba7SLen Brown if (max_level >= 0x15) {
56608a5bdf41SLen Brown unsigned int eax_crystal;
56618a5bdf41SLen Brown unsigned int ebx_tsc;
56628a5bdf41SLen Brown
56638a5bdf41SLen Brown /*
56648a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
56658a5bdf41SLen Brown */
56668a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0;
56675aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
56688a5bdf41SLen Brown
56698a5bdf41SLen Brown if (ebx_tsc != 0) {
56708a5bdf41SLen Brown
567196e47158SLen Brown if (!quiet && (ebx != 0))
5672b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
56738a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz);
56748a5bdf41SLen Brown
56758a5bdf41SLen Brown if (crystal_hz == 0)
56768a5bdf41SLen Brown switch (model) {
5677af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */
5678e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */
5679e8efbc80SLen Brown break;
56805ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
5681ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */
5682ec53e594SLen Brown break;
5683869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5684f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5685e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */
56868a5bdf41SLen Brown break;
56878a5bdf41SLen Brown default:
56888a5bdf41SLen Brown crystal_hz = 0;
56898a5bdf41SLen Brown }
56908a5bdf41SLen Brown
56918a5bdf41SLen Brown if (crystal_hz) {
56928a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal;
569396e47158SLen Brown if (!quiet)
5694b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
56958a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
56968a5bdf41SLen Brown }
56978a5bdf41SLen Brown }
56988a5bdf41SLen Brown }
569961a87ba7SLen Brown if (max_level >= 0x16) {
570061a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx;
570161a87ba7SLen Brown
570261a87ba7SLen Brown /*
570361a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz
570461a87ba7SLen Brown */
570561a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0;
570661a87ba7SLen Brown
57075aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
570896e47158SLen Brown if (!quiet)
5709b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
571061a87ba7SLen Brown base_mhz, max_mhz, bus_mhz);
571161a87ba7SLen Brown }
57128a5bdf41SLen Brown
5713b2b34dfeSHubert Chrzaniuk if (has_aperf)
5714b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
5715b2b34dfeSHubert Chrzaniuk
5716812db3f7SLen Brown BIC_PRESENT(BIC_IRQ);
5717812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz);
5718812db3f7SLen Brown
5719812db3f7SLen Brown if (probe_nhm_msrs(family, model)) {
5720812db3f7SLen Brown do_nhm_platform_info = 1;
5721812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1);
5722812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3);
5723812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6);
5724812db3f7SLen Brown BIC_PRESENT(BIC_SMI);
5725812db3f7SLen Brown }
5726d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model);
5727812db3f7SLen Brown
5728812db3f7SLen Brown if (do_snb_cstates)
5729812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7);
5730812db3f7SLen Brown
57315a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model);
57320f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
57330f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2);
57340f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3)
57350f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3);
57360f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6)
57370f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6);
57380f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
57390f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7);
57400539ba11SLen Brown if (has_slv_msrs(family, model)) {
57410f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2);
57420f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3);
57430f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6);
57440f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7);
57450539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6);
57460539ba11SLen Brown use_c1_residency_msr = 1;
57470539ba11SLen Brown }
574820de0dabSAntti Laakso if (is_jvl(family, model)) {
574920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3);
575020de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7);
575120de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2);
575220de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3);
575320de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6);
575420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7);
575520de0dabSAntti Laakso }
57567170a374SLen Brown if (is_dnv(family, model)) {
57577170a374SLen Brown BIC_PRESENT(BIC_CPU_c1);
57587170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3);
57597170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3);
57607170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7);
57617170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7);
57627170a374SLen Brown use_c1_residency_msr = 1;
57637170a374SLen Brown }
5764684e40e9SArtem Bityutskiy if (is_skx(family, model) || is_icx(family, model) || is_spr(family, model)) {
576534c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3);
576634c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3);
576734c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7);
576834c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7);
576934c76197SLen Brown }
5770ade0ebacSLen Brown if (is_bdx(family, model)) {
5771ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7);
5772ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7);
5773ade0ebacSLen Brown }
5774570992fcSLen Brown if (has_c8910_msrs(family, model)) {
5775c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8)
57760f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8);
5777c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9)
57780f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9);
5779c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10)
57800f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10);
57810f47c08dSLen Brown }
5782570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model);
5783a99d8730SLen Brown if (has_skl_msrs(family, model)) {
5784a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0);
5785a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0);
5786a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0);
5787a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX);
5788a99d8730SLen Brown }
5789144b44b1SLen Brown do_slm_cstates = is_slm(family, model);
5790fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model);
5791103a8feaSLen Brown
57921b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model))
5793562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3);
5794103a8feaSLen Brown
579596e47158SLen Brown if (!quiet)
5796f0057310SLen Brown decode_misc_pwr_mgmt_msr();
5797f0057310SLen Brown
579896e47158SLen Brown if (!quiet && has_slv_msrs(family, model))
579971616c8eSLen Brown decode_c6_demotion_policy_msr();
580071616c8eSLen Brown
5801889facbeSLen Brown rapl_probe(family, model);
58023a9a941dSLen Brown perf_limit_reasons_probe(family, model);
5803ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model);
58049b523bf8SChen Yu prewake_cstate_probe(family, model);
5805889facbeSLen Brown
58060b9a0b9bSZhang Rui check_tcc_offset(model_orig);
58070b9a0b9bSZhang Rui
580896e47158SLen Brown if (!quiet)
58091b69317dSColin Ian King dump_cstate_pstate_config_info(family, model);
5810a5c6d65dSLen Brown intel_uncore_frequency_probe();
5811fcd17211SLen Brown
581241618e63SLen Brown if (!quiet)
5813d76bb7a0SLen Brown print_dev_latency();
5814d76bb7a0SLen Brown if (!quiet)
581541618e63SLen Brown dump_sysfs_cstate_config();
58167293fccdSLen Brown if (!quiet)
58177293fccdSLen Brown dump_sysfs_pstate_config();
581841618e63SLen Brown
58198c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model))
5820a2b7b749SLen Brown calculate_tsc_tweak();
5821a2b7b749SLen Brown
5822812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
5823812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6);
5824fdf676e5SLen Brown
5825812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5826812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz);
582727d47356SLen Brown
5828b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK))
5829b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz);
5830b4b91569SRafael Antognolli
5831be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
5832be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI);
5833be0e54c4SLen Brown else
5834be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI);
5835be0e54c4SLen Brown
5836eae97e05SChen Yu if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK))
5837eae97e05SChen Yu BIC_PRESENT(BIC_CORE_THROT_CNT);
5838eae97e05SChen Yu else
5839eae97e05SChen Yu BIC_NOT_PRESENT(BIC_CORE_THROT_CNT);
5840eae97e05SChen Yu
58411f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) {
58421f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs;
5843be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI);
58441f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) {
58451f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs;
58461f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI);
58471f81c5efSLen Brown } else {
58481f81c5efSLen Brown sys_lpi_file_sysfs = NULL;
5849be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI);
58501f81c5efSLen Brown }
5851be0e54c4SLen Brown
585296e47158SLen Brown if (!quiet)
585333148d67SLen Brown decode_misc_feature_control();
585433148d67SLen Brown
5855889facbeSLen Brown return;
5856103a8feaSLen Brown }
5857103a8feaSLen Brown
5858103a8feaSLen Brown /*
5859103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers
5860103a8feaSLen Brown * ie. filter out ".", "..", "microcode".
5861103a8feaSLen Brown */
dir_filter(const struct dirent * dirp)5862103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
5863103a8feaSLen Brown {
5864103a8feaSLen Brown if (isdigit(dirp->d_name[0]))
5865103a8feaSLen Brown return 1;
5866103a8feaSLen Brown else
5867103a8feaSLen Brown return 0;
5868103a8feaSLen Brown }
5869103a8feaSLen Brown
topology_probe()5870c98d5d94SLen Brown void topology_probe()
5871c98d5d94SLen Brown {
5872c98d5d94SLen Brown int i;
5873c98d5d94SLen Brown int max_core_id = 0;
5874c98d5d94SLen Brown int max_package_id = 0;
5875c98d5d94SLen Brown int max_siblings = 0;
5876c98d5d94SLen Brown
5877c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */
5878843c5791SPrarit Bhargava set_max_cpu_num();
5879c98d5d94SLen Brown topo.num_cpus = 0;
5880c98d5d94SLen Brown for_all_proc_cpus(count_cpus);
5881c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1)
5882812db3f7SLen Brown BIC_PRESENT(BIC_CPU);
5883c98d5d94SLen Brown
5884d8af6f5fSLen Brown if (debug > 1)
5885b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5886c98d5d94SLen Brown
5887c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
5888b2c95d90SJosh Triplett if (cpus == NULL)
5889b2c95d90SJosh Triplett err(1, "calloc cpus");
5890c98d5d94SLen Brown
5891c98d5d94SLen Brown /*
5892c98d5d94SLen Brown * Allocate and initialize cpu_present_set
5893c98d5d94SLen Brown */
5894c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5895b2c95d90SJosh Triplett if (cpu_present_set == NULL)
5896b2c95d90SJosh Triplett err(3, "CPU_ALLOC");
5897c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5898c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5899c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present);
5900c98d5d94SLen Brown
5901c98d5d94SLen Brown /*
59021ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set
59031ef7d21aSLen Brown */
59041ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
59051ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
59061ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
59071ef7d21aSLen Brown err(1, "cpu%d not present", i);
59081ef7d21aSLen Brown }
59091ef7d21aSLen Brown
59101ef7d21aSLen Brown /*
5911c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set
5912c98d5d94SLen Brown */
5913c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5914b2c95d90SJosh Triplett if (cpu_affinity_set == NULL)
5915b2c95d90SJosh Triplett err(3, "CPU_ALLOC");
5916c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5917c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5918c98d5d94SLen Brown
59198cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id);
5920c98d5d94SLen Brown
5921c98d5d94SLen Brown /*
5922c98d5d94SLen Brown * For online cpus
5923c98d5d94SLen Brown * find max_core_id, max_package_id
5924c98d5d94SLen Brown */
5925c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) {
5926c98d5d94SLen Brown int siblings;
5927c98d5d94SLen Brown
5928c98d5d94SLen Brown if (cpu_is_not_present(i)) {
5929d8af6f5fSLen Brown if (debug > 1)
5930b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i);
5931c98d5d94SLen Brown continue;
5932c98d5d94SLen Brown }
5933c98d5d94SLen Brown
59340e2d8f05SLen Brown cpus[i].logical_cpu_id = i;
59350e2d8f05SLen Brown
59360e2d8f05SLen Brown /* get package information */
5937c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i);
5938c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id)
5939c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id;
5940c98d5d94SLen Brown
59416de68fe1SLen Brown /* get die information */
59426de68fe1SLen Brown cpus[i].die_id = get_die_id(i);
5943*76d7b869SLen Brown if (cpus[i].die_id > topo.max_die_id)
5944*76d7b869SLen Brown topo.max_die_id = cpus[i].die_id;
59456de68fe1SLen Brown
59460e2d8f05SLen Brown /* get numa node information */
5947ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5948ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num)
5949ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id;
59500e2d8f05SLen Brown
59510e2d8f05SLen Brown /* get core information */
59520e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i);
59530e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id)
59540e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id;
59550e2d8f05SLen Brown
59560e2d8f05SLen Brown /* get thread information */
59570e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]);
5958c98d5d94SLen Brown if (siblings > max_siblings)
5959c98d5d94SLen Brown max_siblings = siblings;
59604f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0)
59618cb48b32SPrarit Bhargava topo.num_cores++;
5962c98d5d94SLen Brown }
5963ef605741SPrarit Bhargava
596470a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1;
5965d8af6f5fSLen Brown if (debug > 1)
59661b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node);
596770a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1)
5968812db3f7SLen Brown BIC_PRESENT(BIC_Core);
5969c98d5d94SLen Brown
5970*76d7b869SLen Brown topo.num_die = topo.max_die_id + 1;
59716de68fe1SLen Brown if (debug > 1)
5972*76d7b869SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", topo.max_die_id, topo.num_die);
59736de68fe1SLen Brown if (!summary_only && topo.num_die > 1)
59746de68fe1SLen Brown BIC_PRESENT(BIC_Die);
59756de68fe1SLen Brown
5976c98d5d94SLen Brown topo.num_packages = max_package_id + 1;
5977d8af6f5fSLen Brown if (debug > 1)
59781b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages);
59797da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1)
5980812db3f7SLen Brown BIC_PRESENT(BIC_Package);
5981c98d5d94SLen Brown
5982ef605741SPrarit Bhargava set_node_data();
5983ef605741SPrarit Bhargava if (debug > 1)
598470a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
598501235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1)
598601235041SPrarit Bhargava BIC_PRESENT(BIC_Node);
5987ef605741SPrarit Bhargava
598870a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings;
5989d8af6f5fSLen Brown if (debug > 1)
5990b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings);
59912ffbb224SPrarit Bhargava
59922ffbb224SPrarit Bhargava if (debug < 1)
59932ffbb224SPrarit Bhargava return;
59942ffbb224SPrarit Bhargava
59952ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) {
59960ec712e3SLen Brown if (cpu_is_not_present(i))
59970ec712e3SLen Brown continue;
59982ffbb224SPrarit Bhargava fprintf(outf,
59996de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
60006de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id,
60011b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id);
60022ffbb224SPrarit Bhargava }
60032ffbb224SPrarit Bhargava
6004c98d5d94SLen Brown }
6005c98d5d94SLen Brown
allocate_counters(struct thread_data ** t,struct core_data ** c,struct pkg_data ** p)60061b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
6007c98d5d94SLen Brown {
6008c98d5d94SLen Brown int i;
60091b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages;
601040f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores;
6011c98d5d94SLen Brown
601240f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data));
6013c98d5d94SLen Brown if (*t == NULL)
6014c98d5d94SLen Brown goto error;
6015c98d5d94SLen Brown
601640f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++)
6017c98d5d94SLen Brown (*t)[i].cpu_id = -1;
6018c98d5d94SLen Brown
601940f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data));
6020c98d5d94SLen Brown if (*c == NULL)
6021c98d5d94SLen Brown goto error;
6022c98d5d94SLen Brown
602340f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++)
6024c98d5d94SLen Brown (*c)[i].core_id = -1;
6025c98d5d94SLen Brown
6026678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data));
6027c98d5d94SLen Brown if (*p == NULL)
6028c98d5d94SLen Brown goto error;
6029c98d5d94SLen Brown
6030c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++)
6031c98d5d94SLen Brown (*p)[i].package_id = i;
6032c98d5d94SLen Brown
6033c98d5d94SLen Brown return;
6034c98d5d94SLen Brown error:
6035b2c95d90SJosh Triplett err(1, "calloc counters");
6036c98d5d94SLen Brown }
60371b439f01SLen Brown
6038c98d5d94SLen Brown /*
6039c98d5d94SLen Brown * init_counter()
6040c98d5d94SLen Brown *
6041c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
6042c98d5d94SLen Brown */
init_counter(struct thread_data * thread_base,struct core_data * core_base,struct pkg_data * pkg_base,int cpu_id)60431b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id)
6044c98d5d94SLen Brown {
60458cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id;
604640f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id;
60478cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id;
60488cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id;
6049c98d5d94SLen Brown struct thread_data *t;
6050c98d5d94SLen Brown struct core_data *c;
6051c98d5d94SLen Brown struct pkg_data *p;
6052c98d5d94SLen Brown
605342dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1
605442dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus)
605542dd4520SNathan Ciobanu */
605642dd4520SNathan Ciobanu if (node_id < 0)
605742dd4520SNathan Ciobanu node_id = 0;
605842dd4520SNathan Ciobanu
605940f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
606040f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id);
60618cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id);
6062c98d5d94SLen Brown
6063c98d5d94SLen Brown t->cpu_id = cpu_id;
60648cb48b32SPrarit Bhargava if (thread_id == 0) {
6065c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
6066c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id))
6067c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
6068c98d5d94SLen Brown }
6069c98d5d94SLen Brown
60708cb48b32SPrarit Bhargava c->core_id = core_id;
60718cb48b32SPrarit Bhargava p->package_id = pkg_id;
6072c98d5d94SLen Brown }
6073c98d5d94SLen Brown
initialize_counters(int cpu_id)6074c98d5d94SLen Brown int initialize_counters(int cpu_id)
6075c98d5d94SLen Brown {
60768cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id);
60778cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id);
6078c98d5d94SLen Brown return 0;
6079c98d5d94SLen Brown }
6080c98d5d94SLen Brown
allocate_output_buffer()6081c98d5d94SLen Brown void allocate_output_buffer()
6082c98d5d94SLen Brown {
6083eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
6084c98d5d94SLen Brown outp = output_buffer;
6085b2c95d90SJosh Triplett if (outp == NULL)
6086b2c95d90SJosh Triplett err(-1, "calloc output buffer");
6087c98d5d94SLen Brown }
60881b439f01SLen Brown
allocate_fd_percpu(void)608936229897SLen Brown void allocate_fd_percpu(void)
609036229897SLen Brown {
609101a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
609236229897SLen Brown if (fd_percpu == NULL)
609336229897SLen Brown err(-1, "calloc fd_percpu");
609436229897SLen Brown }
60951b439f01SLen Brown
allocate_irq_buffers(void)6096562a2d37SLen Brown void allocate_irq_buffers(void)
6097562a2d37SLen Brown {
6098562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
6099562a2d37SLen Brown if (irq_column_2_cpu == NULL)
6100562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus);
6101c98d5d94SLen Brown
610201a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
6103562a2d37SLen Brown if (irqs_per_cpu == NULL)
610401a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1);
6105562a2d37SLen Brown }
61061b439f01SLen Brown
setup_all_buffers(void)6107c98d5d94SLen Brown void setup_all_buffers(void)
6108c98d5d94SLen Brown {
6109c98d5d94SLen Brown topology_probe();
6110562a2d37SLen Brown allocate_irq_buffers();
611136229897SLen Brown allocate_fd_percpu();
6112c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even);
6113c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd);
6114c98d5d94SLen Brown allocate_output_buffer();
6115c98d5d94SLen Brown for_all_proc_cpus(initialize_counters);
6116c98d5d94SLen Brown }
61173b4d5c7fSAndy Shevchenko
set_base_cpu(void)61187ce7d5deSPrarit Bhargava void set_base_cpu(void)
61197ce7d5deSPrarit Bhargava {
61207ce7d5deSPrarit Bhargava base_cpu = sched_getcpu();
61217ce7d5deSPrarit Bhargava if (base_cpu < 0)
61227ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found");
61237ce7d5deSPrarit Bhargava
61247ce7d5deSPrarit Bhargava if (debug > 1)
6125b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu);
61267ce7d5deSPrarit Bhargava }
61277ce7d5deSPrarit Bhargava
turbostat_init()6128103a8feaSLen Brown void turbostat_init()
6129103a8feaSLen Brown {
61307ce7d5deSPrarit Bhargava setup_all_buffers();
61317ce7d5deSPrarit Bhargava set_base_cpu();
6132103a8feaSLen Brown check_dev_msr();
613398481e79SLen Brown check_permissions();
6134fcd17211SLen Brown process_cpuid();
61352af4f9b8SLen Brown linux_perf_init();
6136103a8feaSLen Brown
613796e47158SLen Brown if (!quiet)
61387f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS);
61397f5c258eSLen Brown
614096e47158SLen Brown if (!quiet)
6141889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS);
6142889facbeSLen Brown
614396e47158SLen Brown if (!quiet)
61443a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS);
61453a9a941dSLen Brown
614696e47158SLen Brown if (!quiet)
6147889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS);
6148889facbeSLen Brown
6149889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS);
6150889facbeSLen Brown
61517ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS);
61527ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS);
61537ab5ff49SZhang Rui
615496e47158SLen Brown if (!quiet)
6155889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS);
61565a63426eSLen Brown
615796e47158SLen Brown if (!quiet && do_irtl_snb)
61585a63426eSLen Brown print_irtl();
6159164d7a96SLen Brown
6160164d7a96SLen Brown if (DO_BIC(BIC_IPC))
6161164d7a96SLen Brown (void)get_instr_count_fd(base_cpu);
6162103a8feaSLen Brown }
6163103a8feaSLen Brown
fork_it(char ** argv)6164103a8feaSLen Brown int fork_it(char **argv)
6165103a8feaSLen Brown {
6166103a8feaSLen Brown pid_t child_pid;
6167d91bb17cSLen Brown int status;
6168d15cf7c1SLen Brown
6169218f0e8dSLen Brown snapshot_proc_sysfs_files();
6170d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS);
61714c2122d4SLen Brown first_counter_read = 0;
6172d91bb17cSLen Brown if (status)
6173d91bb17cSLen Brown exit(status);
6174d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */
6175d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
6176103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL);
6177103a8feaSLen Brown
6178103a8feaSLen Brown child_pid = fork();
6179103a8feaSLen Brown if (!child_pid) {
6180103a8feaSLen Brown /* child */
6181103a8feaSLen Brown execvp(argv[0], argv);
61820815a3d0SLen Brown err(errno, "exec %s", argv[0]);
6183103a8feaSLen Brown } else {
6184103a8feaSLen Brown
6185103a8feaSLen Brown /* parent */
6186b2c95d90SJosh Triplett if (child_pid == -1)
6187b2c95d90SJosh Triplett err(1, "fork");
6188103a8feaSLen Brown
6189103a8feaSLen Brown signal(SIGINT, SIG_IGN);
6190103a8feaSLen Brown signal(SIGQUIT, SIG_IGN);
6191b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1)
6192b2c95d90SJosh Triplett err(status, "waitpid");
61932a954966SDavid Arcari
61942a954966SDavid Arcari if (WIFEXITED(status))
61952a954966SDavid Arcari status = WEXITSTATUS(status);
6196103a8feaSLen Brown }
6197c98d5d94SLen Brown /*
6198c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus()
6199c98d5d94SLen Brown * because re-starting is problematic when forking
6200c98d5d94SLen Brown */
6201218f0e8dSLen Brown snapshot_proc_sysfs_files();
6202c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS);
6203103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL);
6204103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta);
6205ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
6206ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname);
6207ba3dec99SLen Brown else {
6208c98d5d94SLen Brown compute_average(EVEN_COUNTERS);
6209c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS);
6210ba3dec99SLen Brown }
6211103a8feaSLen Brown
6212b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0);
6213b7d8c148SLen Brown
6214b7d8c148SLen Brown flush_output_stderr();
6215103a8feaSLen Brown
6216d91bb17cSLen Brown return status;
6217103a8feaSLen Brown }
6218103a8feaSLen Brown
get_and_dump_counters(void)62193b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
62203b4d5c7fSAndy Shevchenko {
62213b4d5c7fSAndy Shevchenko int status;
62223b4d5c7fSAndy Shevchenko
6223218f0e8dSLen Brown snapshot_proc_sysfs_files();
62243b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS);
62253b4d5c7fSAndy Shevchenko if (status)
62263b4d5c7fSAndy Shevchenko return status;
62273b4d5c7fSAndy Shevchenko
62283b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS);
62293b4d5c7fSAndy Shevchenko if (status)
62303b4d5c7fSAndy Shevchenko return status;
62313b4d5c7fSAndy Shevchenko
6232b7d8c148SLen Brown flush_output_stdout();
62333b4d5c7fSAndy Shevchenko
62343b4d5c7fSAndy Shevchenko return status;
62353b4d5c7fSAndy Shevchenko }
62363b4d5c7fSAndy Shevchenko
print_version()62371b439f01SLen Brown void print_version()
62381b439f01SLen Brown {
6239de7839eeSLen Brown fprintf(outf, "turbostat version 2023.03.17 - Len Brown <lenb@kernel.org>\n");
6240d8af6f5fSLen Brown }
6241d8af6f5fSLen Brown
624203331233SChen Yu #define COMMAND_LINE_SIZE 2048
624303331233SChen Yu
print_bootcmd(void)624403331233SChen Yu void print_bootcmd(void)
624503331233SChen Yu {
624603331233SChen Yu char bootcmd[COMMAND_LINE_SIZE];
624703331233SChen Yu FILE *fp;
624803331233SChen Yu int ret;
624903331233SChen Yu
625003331233SChen Yu memset(bootcmd, 0, COMMAND_LINE_SIZE);
625103331233SChen Yu fp = fopen("/proc/cmdline", "r");
625203331233SChen Yu if (!fp)
625303331233SChen Yu return;
625403331233SChen Yu
625503331233SChen Yu ret = fread(bootcmd, sizeof(char), COMMAND_LINE_SIZE - 1, fp);
625603331233SChen Yu if (ret) {
625703331233SChen Yu bootcmd[ret] = '\0';
625803331233SChen Yu /* the last character is already '\n' */
625903331233SChen Yu fprintf(outf, "Kernel command line: %s", bootcmd);
626003331233SChen Yu }
626103331233SChen Yu
626203331233SChen Yu fclose(fp);
626303331233SChen Yu }
626403331233SChen Yu
add_counter(unsigned int msr_num,char * path,char * name,unsigned int width,enum counter_scope scope,enum counter_type type,enum counter_format format,int flags)6265495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
6266495c7654SLen Brown unsigned int width, enum counter_scope scope,
626741618e63SLen Brown enum counter_type type, enum counter_format format, int flags)
6268388e9c81SLen Brown {
6269388e9c81SLen Brown struct msr_counter *msrp;
6270388e9c81SLen Brown
6271388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter));
6272388e9c81SLen Brown if (msrp == NULL) {
6273388e9c81SLen Brown perror("calloc");
6274388e9c81SLen Brown exit(1);
6275388e9c81SLen Brown }
6276388e9c81SLen Brown
6277388e9c81SLen Brown msrp->msr_num = msr_num;
6278d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1);
6279495c7654SLen Brown if (path)
6280d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1);
6281388e9c81SLen Brown msrp->width = width;
6282388e9c81SLen Brown msrp->type = type;
6283388e9c81SLen Brown msrp->format = format;
628441618e63SLen Brown msrp->flags = flags;
6285388e9c81SLen Brown
6286388e9c81SLen Brown switch (scope) {
6287388e9c81SLen Brown
6288388e9c81SLen Brown case SCOPE_CPU:
6289388e9c81SLen Brown msrp->next = sys.tp;
6290388e9c81SLen Brown sys.tp = msrp;
6291678a3bd1SLen Brown sys.added_thread_counters++;
62920748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
62931b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS);
6294678a3bd1SLen Brown exit(-1);
6295678a3bd1SLen Brown }
6296388e9c81SLen Brown break;
6297388e9c81SLen Brown
6298388e9c81SLen Brown case SCOPE_CORE:
6299388e9c81SLen Brown msrp->next = sys.cp;
6300388e9c81SLen Brown sys.cp = msrp;
6301678a3bd1SLen Brown sys.added_core_counters++;
6302678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
63031b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS);
6304678a3bd1SLen Brown exit(-1);
6305678a3bd1SLen Brown }
6306388e9c81SLen Brown break;
6307388e9c81SLen Brown
6308388e9c81SLen Brown case SCOPE_PACKAGE:
6309388e9c81SLen Brown msrp->next = sys.pp;
6310388e9c81SLen Brown sys.pp = msrp;
6311678a3bd1SLen Brown sys.added_package_counters++;
6312678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
63131b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS);
6314678a3bd1SLen Brown exit(-1);
6315678a3bd1SLen Brown }
6316388e9c81SLen Brown break;
6317388e9c81SLen Brown }
6318388e9c81SLen Brown
6319388e9c81SLen Brown return 0;
6320388e9c81SLen Brown }
6321388e9c81SLen Brown
parse_add_command(char * add_command)6322388e9c81SLen Brown void parse_add_command(char *add_command)
6323388e9c81SLen Brown {
6324388e9c81SLen Brown int msr_num = 0;
6325495c7654SLen Brown char *path = NULL;
63260f47c08dSLen Brown char name_buffer[NAME_BYTES] = "";
6327388e9c81SLen Brown int width = 64;
6328388e9c81SLen Brown int fail = 0;
6329388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU;
6330388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES;
6331388e9c81SLen Brown enum counter_format format = FORMAT_DELTA;
6332388e9c81SLen Brown
6333388e9c81SLen Brown while (add_command) {
6334388e9c81SLen Brown
6335388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
6336388e9c81SLen Brown goto next;
6337388e9c81SLen Brown
6338388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1)
6339388e9c81SLen Brown goto next;
6340388e9c81SLen Brown
6341495c7654SLen Brown if (*add_command == '/') {
6342495c7654SLen Brown path = add_command;
6343495c7654SLen Brown goto next;
6344495c7654SLen Brown }
6345495c7654SLen Brown
6346388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) {
6347388e9c81SLen Brown if ((width == 32) || (width == 64))
6348388e9c81SLen Brown goto next;
6349388e9c81SLen Brown width = 64;
6350388e9c81SLen Brown }
6351388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) {
6352388e9c81SLen Brown scope = SCOPE_CPU;
6353388e9c81SLen Brown goto next;
6354388e9c81SLen Brown }
6355388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) {
6356388e9c81SLen Brown scope = SCOPE_CORE;
6357388e9c81SLen Brown goto next;
6358388e9c81SLen Brown }
6359388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) {
6360388e9c81SLen Brown scope = SCOPE_PACKAGE;
6361388e9c81SLen Brown goto next;
6362388e9c81SLen Brown }
6363388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) {
6364388e9c81SLen Brown type = COUNTER_CYCLES;
6365388e9c81SLen Brown goto next;
6366388e9c81SLen Brown }
6367388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) {
6368388e9c81SLen Brown type = COUNTER_SECONDS;
6369388e9c81SLen Brown goto next;
6370388e9c81SLen Brown }
637141618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) {
637241618e63SLen Brown type = COUNTER_USEC;
637341618e63SLen Brown goto next;
637441618e63SLen Brown }
6375388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) {
6376388e9c81SLen Brown format = FORMAT_RAW;
6377388e9c81SLen Brown goto next;
6378388e9c81SLen Brown }
6379388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) {
6380388e9c81SLen Brown format = FORMAT_DELTA;
6381388e9c81SLen Brown goto next;
6382388e9c81SLen Brown }
6383388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) {
6384388e9c81SLen Brown format = FORMAT_PERCENT;
6385388e9c81SLen Brown goto next;
6386388e9c81SLen Brown }
6387388e9c81SLen Brown
6388388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
6389388e9c81SLen Brown char *eos;
6390388e9c81SLen Brown
6391388e9c81SLen Brown eos = strchr(name_buffer, ',');
6392388e9c81SLen Brown if (eos)
6393388e9c81SLen Brown *eos = '\0';
6394388e9c81SLen Brown goto next;
6395388e9c81SLen Brown }
6396388e9c81SLen Brown
6397388e9c81SLen Brown next:
6398388e9c81SLen Brown add_command = strchr(add_command, ',');
6399495c7654SLen Brown if (add_command) {
6400495c7654SLen Brown *add_command = '\0';
6401388e9c81SLen Brown add_command++;
6402495c7654SLen Brown }
6403388e9c81SLen Brown
6404388e9c81SLen Brown }
6405495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) {
6406495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
6407388e9c81SLen Brown fail++;
6408388e9c81SLen Brown }
6409388e9c81SLen Brown
6410388e9c81SLen Brown /* generate default column header */
6411388e9c81SLen Brown if (*name_buffer == '\0') {
6412388e9c81SLen Brown if (width == 32)
64135f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6414388e9c81SLen Brown else
64155f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6416388e9c81SLen Brown }
6417388e9c81SLen Brown
641841618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
6419388e9c81SLen Brown fail++;
6420388e9c81SLen Brown
6421388e9c81SLen Brown if (fail) {
6422388e9c81SLen Brown help();
6423388e9c81SLen Brown exit(1);
6424388e9c81SLen Brown }
6425388e9c81SLen Brown }
642641618e63SLen Brown
is_deferred_add(char * name)64270fc521bcSZephaniah E. Loss-Cutler-Hull int is_deferred_add(char *name)
64280fc521bcSZephaniah E. Loss-Cutler-Hull {
64290fc521bcSZephaniah E. Loss-Cutler-Hull int i;
64300fc521bcSZephaniah E. Loss-Cutler-Hull
64310fc521bcSZephaniah E. Loss-Cutler-Hull for (i = 0; i < deferred_add_index; ++i)
64320fc521bcSZephaniah E. Loss-Cutler-Hull if (!strcmp(name, deferred_add_names[i]))
64330fc521bcSZephaniah E. Loss-Cutler-Hull return 1;
64340fc521bcSZephaniah E. Loss-Cutler-Hull return 0;
64350fc521bcSZephaniah E. Loss-Cutler-Hull }
64360fc521bcSZephaniah E. Loss-Cutler-Hull
is_deferred_skip(char * name)6437dd778a5eSLen Brown int is_deferred_skip(char *name)
6438dd778a5eSLen Brown {
6439dd778a5eSLen Brown int i;
6440dd778a5eSLen Brown
6441dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i)
6442dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i]))
6443dd778a5eSLen Brown return 1;
6444dd778a5eSLen Brown return 0;
6445dd778a5eSLen Brown }
6446dd778a5eSLen Brown
probe_sysfs(void)644741618e63SLen Brown void probe_sysfs(void)
644841618e63SLen Brown {
644941618e63SLen Brown char path[64];
645041618e63SLen Brown char name_buf[16];
645141618e63SLen Brown FILE *input;
645241618e63SLen Brown int state;
645341618e63SLen Brown char *sp;
645441618e63SLen Brown
64550748eaf0SLen Brown for (state = 10; state >= 0; --state) {
645641618e63SLen Brown
64571b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
645841618e63SLen Brown input = fopen(path, "r");
645941618e63SLen Brown if (input == NULL)
646041618e63SLen Brown continue;
64618173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input))
64628173c336SBen Hutchings err(1, "%s: failed to read file", path);
646341618e63SLen Brown
646441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
646541618e63SLen Brown sp = strchr(name_buf, '-');
646641618e63SLen Brown if (!sp)
646741618e63SLen Brown sp = strchrnul(name_buf, '\n');
646841618e63SLen Brown *sp = '%';
646941618e63SLen Brown *(sp + 1) = '\0';
647041618e63SLen Brown
6471fecb3bc8SDavid Arcari remove_underbar(name_buf);
6472fecb3bc8SDavid Arcari
647341618e63SLen Brown fclose(input);
647441618e63SLen Brown
647541618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state);
647641618e63SLen Brown
64770fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
64780fc521bcSZephaniah E. Loss-Cutler-Hull continue;
64790fc521bcSZephaniah E. Loss-Cutler-Hull
6480dd778a5eSLen Brown if (is_deferred_skip(name_buf))
6481dd778a5eSLen Brown continue;
6482dd778a5eSLen Brown
64831b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU);
648441618e63SLen Brown }
648541618e63SLen Brown
64860748eaf0SLen Brown for (state = 10; state >= 0; --state) {
648741618e63SLen Brown
64881b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
648941618e63SLen Brown input = fopen(path, "r");
649041618e63SLen Brown if (input == NULL)
649141618e63SLen Brown continue;
64928173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input))
64938173c336SBen Hutchings err(1, "%s: failed to read file", path);
649441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
649541618e63SLen Brown sp = strchr(name_buf, '-');
649641618e63SLen Brown if (!sp)
649741618e63SLen Brown sp = strchrnul(name_buf, '\n');
649841618e63SLen Brown *sp = '\0';
649941618e63SLen Brown fclose(input);
650041618e63SLen Brown
6501fecb3bc8SDavid Arcari remove_underbar(name_buf);
6502fecb3bc8SDavid Arcari
650341618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state);
650441618e63SLen Brown
65050fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
65060fc521bcSZephaniah E. Loss-Cutler-Hull continue;
65070fc521bcSZephaniah E. Loss-Cutler-Hull
6508dd778a5eSLen Brown if (is_deferred_skip(name_buf))
6509dd778a5eSLen Brown continue;
6510dd778a5eSLen Brown
65111b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU);
651241618e63SLen Brown }
651341618e63SLen Brown
651441618e63SLen Brown }
651541618e63SLen Brown
65161ef7d21aSLen Brown /*
65171ef7d21aSLen Brown * parse cpuset with following syntax
65181ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset
65191ef7d21aSLen Brown */
parse_cpu_command(char * optarg)65201ef7d21aSLen Brown void parse_cpu_command(char *optarg)
65211ef7d21aSLen Brown {
65221ef7d21aSLen Brown unsigned int start, end;
65231ef7d21aSLen Brown char *next;
65241ef7d21aSLen Brown
65254e4e1e7cSLen Brown if (!strcmp(optarg, "core")) {
65264e4e1e7cSLen Brown if (cpu_subset)
65274e4e1e7cSLen Brown goto error;
65284e4e1e7cSLen Brown show_core_only++;
65294e4e1e7cSLen Brown return;
65304e4e1e7cSLen Brown }
65314e4e1e7cSLen Brown if (!strcmp(optarg, "package")) {
65324e4e1e7cSLen Brown if (cpu_subset)
65334e4e1e7cSLen Brown goto error;
65344e4e1e7cSLen Brown show_pkg_only++;
65354e4e1e7cSLen Brown return;
65364e4e1e7cSLen Brown }
65374e4e1e7cSLen Brown if (show_core_only || show_pkg_only)
65384e4e1e7cSLen Brown goto error;
65394e4e1e7cSLen Brown
65401ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
65411ef7d21aSLen Brown if (cpu_subset == NULL)
65421ef7d21aSLen Brown err(3, "CPU_ALLOC");
65431ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
65441ef7d21aSLen Brown
65451ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset);
65461ef7d21aSLen Brown
65471ef7d21aSLen Brown next = optarg;
65481ef7d21aSLen Brown
65491ef7d21aSLen Brown while (next && *next) {
65501ef7d21aSLen Brown
65511ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */
65521ef7d21aSLen Brown goto error;
65531ef7d21aSLen Brown
65541ef7d21aSLen Brown start = strtoul(next, &next, 10);
65551ef7d21aSLen Brown
65561ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS)
65571ef7d21aSLen Brown goto error;
65581ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset);
65591ef7d21aSLen Brown
65601ef7d21aSLen Brown if (*next == '\0')
65611ef7d21aSLen Brown break;
65621ef7d21aSLen Brown
65631ef7d21aSLen Brown if (*next == ',') {
65641ef7d21aSLen Brown next += 1;
65651ef7d21aSLen Brown continue;
65661ef7d21aSLen Brown }
65671ef7d21aSLen Brown
65681ef7d21aSLen Brown if (*next == '-') {
65691ef7d21aSLen Brown next += 1; /* start range */
65701ef7d21aSLen Brown } else if (*next == '.') {
65711ef7d21aSLen Brown next += 1;
65721ef7d21aSLen Brown if (*next == '.')
65731ef7d21aSLen Brown next += 1; /* start range */
65741ef7d21aSLen Brown else
65751ef7d21aSLen Brown goto error;
65761ef7d21aSLen Brown }
65771ef7d21aSLen Brown
65781ef7d21aSLen Brown end = strtoul(next, &next, 10);
65791ef7d21aSLen Brown if (end <= start)
65801ef7d21aSLen Brown goto error;
65811ef7d21aSLen Brown
65821ef7d21aSLen Brown while (++start <= end) {
65831ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS)
65841ef7d21aSLen Brown goto error;
65851ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset);
65861ef7d21aSLen Brown }
65871ef7d21aSLen Brown
65881ef7d21aSLen Brown if (*next == ',')
65891ef7d21aSLen Brown next += 1;
65901ef7d21aSLen Brown else if (*next != '\0')
65911ef7d21aSLen Brown goto error;
65921ef7d21aSLen Brown }
65931ef7d21aSLen Brown
65941ef7d21aSLen Brown return;
65951ef7d21aSLen Brown
65961ef7d21aSLen Brown error:
65974e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
65984e4e1e7cSLen Brown help();
65991ef7d21aSLen Brown exit(-1);
66001ef7d21aSLen Brown }
66011ef7d21aSLen Brown
cmdline(int argc,char ** argv)6602103a8feaSLen Brown void cmdline(int argc, char **argv)
6603103a8feaSLen Brown {
6604103a8feaSLen Brown int opt;
6605d8af6f5fSLen Brown int option_index = 0;
6606d8af6f5fSLen Brown static struct option long_options[] = {
6607388e9c81SLen Brown { "add", required_argument, 0, 'a' },
66081ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' },
6609d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' },
661096e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */
66113f44a5c6SLen Brown { "enable", required_argument, 0, 'e' },
6612d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' },
66132af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' },
6614023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' },
6615c7e399f8SZephaniah E. Loss-Cutler-Hull { "header_iterations", required_argument, 0, 'N' },
6616d8af6f5fSLen Brown { "help", no_argument, 0, 'h' },
6617812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help
6618d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' },
6619c8ade361SLen Brown { "list", no_argument, 0, 'l' },
6620b7d8c148SLen Brown { "out", required_argument, 0, 'o' },
662196e47158SLen Brown { "quiet", no_argument, 0, 'q' },
6622812db3f7SLen Brown { "show", required_argument, 0, 's' },
6623d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' },
6624d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' },
6625d8af6f5fSLen Brown { "version", no_argument, 0, 'v' },
6626d8af6f5fSLen Brown { 0, 0, 0, 0 }
6627d8af6f5fSLen Brown };
6628103a8feaSLen Brown
6629103a8feaSLen Brown progname = argv[0];
6630103a8feaSLen Brown
66311b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) {
6632103a8feaSLen Brown switch (opt) {
6633388e9c81SLen Brown case 'a':
6634388e9c81SLen Brown parse_add_command(optarg);
6635388e9c81SLen Brown break;
66361ef7d21aSLen Brown case 'c':
66371ef7d21aSLen Brown parse_cpu_command(optarg);
66381ef7d21aSLen Brown break;
6639d8af6f5fSLen Brown case 'D':
6640d8af6f5fSLen Brown dump_only++;
66418e180f3cSLen Brown break;
66423f44a5c6SLen Brown case 'e':
66433f44a5c6SLen Brown /* --enable specified counter */
66444c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
66453f44a5c6SLen Brown break;
6646d8af6f5fSLen Brown case 'd':
6647d8af6f5fSLen Brown debug++;
66483f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
66492f32edf1SLen Brown break;
6650812db3f7SLen Brown case 'H':
66513f44a5c6SLen Brown /*
66523f44a5c6SLen Brown * --hide: do not show those specified
66533f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask
66543f44a5c6SLen Brown */
66553f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
6656812db3f7SLen Brown break;
6657d8af6f5fSLen Brown case 'h':
6658d8af6f5fSLen Brown default:
6659d8af6f5fSLen Brown help();
6660d8af6f5fSLen Brown exit(1);
6661d8af6f5fSLen Brown case 'i':
66622a0609c0SLen Brown {
66632a0609c0SLen Brown double interval = strtod(optarg, NULL);
66642a0609c0SLen Brown
66652a0609c0SLen Brown if (interval < 0.001) {
66661b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval);
66672a0609c0SLen Brown exit(2);
66682a0609c0SLen Brown }
66692a0609c0SLen Brown
667047936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval;
6671b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
667247936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
66732a0609c0SLen Brown }
6674889facbeSLen Brown break;
66755c56be9aSDirk Brandewie case 'J':
66765c56be9aSDirk Brandewie rapl_joules++;
66775c56be9aSDirk Brandewie break;
6678c8ade361SLen Brown case 'l':
66793f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6680c8ade361SLen Brown list_header_only++;
6681c8ade361SLen Brown quiet++;
6682c8ade361SLen Brown break;
6683b7d8c148SLen Brown case 'o':
6684b7d8c148SLen Brown outf = fopen_or_die(optarg, "w");
6685b7d8c148SLen Brown break;
668696e47158SLen Brown case 'q':
668796e47158SLen Brown quiet = 1;
668896e47158SLen Brown break;
6689023fe0acSChen Yu case 'n':
6690023fe0acSChen Yu num_iterations = strtod(optarg, NULL);
6691023fe0acSChen Yu
6692023fe0acSChen Yu if (num_iterations <= 0) {
66931b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations);
6694023fe0acSChen Yu exit(2);
6695023fe0acSChen Yu }
6696023fe0acSChen Yu break;
6697c7e399f8SZephaniah E. Loss-Cutler-Hull case 'N':
6698c7e399f8SZephaniah E. Loss-Cutler-Hull header_iterations = strtod(optarg, NULL);
6699c7e399f8SZephaniah E. Loss-Cutler-Hull
6700c7e399f8SZephaniah E. Loss-Cutler-Hull if (header_iterations <= 0) {
6701164d7a96SLen Brown fprintf(outf, "iterations %d should be positive number\n", header_iterations);
6702c7e399f8SZephaniah E. Loss-Cutler-Hull exit(2);
6703c7e399f8SZephaniah E. Loss-Cutler-Hull }
6704c7e399f8SZephaniah E. Loss-Cutler-Hull break;
6705812db3f7SLen Brown case 's':
67063f44a5c6SLen Brown /*
67073f44a5c6SLen Brown * --show: show only those specified
67083f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask
67093f44a5c6SLen Brown * subsequent invocations can add to it.
67103f44a5c6SLen Brown */
67113f44a5c6SLen Brown if (shown == 0)
67123f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST);
67133f44a5c6SLen Brown else
67143f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST);
67153f44a5c6SLen Brown shown = 1;
6716812db3f7SLen Brown break;
6717d8af6f5fSLen Brown case 'S':
6718d8af6f5fSLen Brown summary_only++;
6719d8af6f5fSLen Brown break;
6720d8af6f5fSLen Brown case 'T':
672155279aefSZhang Rui tj_max_override = atoi(optarg);
6722d8af6f5fSLen Brown break;
6723d8af6f5fSLen Brown case 'v':
6724d8af6f5fSLen Brown print_version();
6725d8af6f5fSLen Brown exit(0);
6726d8af6f5fSLen Brown break;
6727103a8feaSLen Brown }
6728103a8feaSLen Brown }
6729103a8feaSLen Brown }
6730103a8feaSLen Brown
set_rlimit(void)673152b3541bSWyes Karny void set_rlimit(void)
673252b3541bSWyes Karny {
673352b3541bSWyes Karny struct rlimit limit;
673452b3541bSWyes Karny
673552b3541bSWyes Karny if (getrlimit(RLIMIT_NOFILE, &limit) < 0)
673652b3541bSWyes Karny err(1, "Failed to get rlimit");
673752b3541bSWyes Karny
673852b3541bSWyes Karny if (limit.rlim_max < MAX_NOFILE)
673952b3541bSWyes Karny limit.rlim_max = MAX_NOFILE;
674052b3541bSWyes Karny if (limit.rlim_cur < MAX_NOFILE)
674152b3541bSWyes Karny limit.rlim_cur = MAX_NOFILE;
674252b3541bSWyes Karny
674352b3541bSWyes Karny if (setrlimit(RLIMIT_NOFILE, &limit) < 0)
674452b3541bSWyes Karny err(1, "Failed to set rlimit");
674552b3541bSWyes Karny }
674652b3541bSWyes Karny
main(int argc,char ** argv)6747103a8feaSLen Brown int main(int argc, char **argv)
6748103a8feaSLen Brown {
6749b7d8c148SLen Brown outf = stderr;
6750103a8feaSLen Brown cmdline(argc, argv);
6751103a8feaSLen Brown
675203331233SChen Yu if (!quiet) {
6753d8af6f5fSLen Brown print_version();
675403331233SChen Yu print_bootcmd();
675503331233SChen Yu }
6756103a8feaSLen Brown
675741618e63SLen Brown probe_sysfs();
675841618e63SLen Brown
675952b3541bSWyes Karny if (!getuid())
676052b3541bSWyes Karny set_rlimit();
676152b3541bSWyes Karny
6762103a8feaSLen Brown turbostat_init();
6763103a8feaSLen Brown
67646799ba84SDan Merillat msr_sum_record();
67656799ba84SDan Merillat
67663b4d5c7fSAndy Shevchenko /* dump counters and exit */
67673b4d5c7fSAndy Shevchenko if (dump_only)
67683b4d5c7fSAndy Shevchenko return get_and_dump_counters();
67693b4d5c7fSAndy Shevchenko
6770c8ade361SLen Brown /* list header and exit */
6771c8ade361SLen Brown if (list_header_only) {
6772c8ade361SLen Brown print_header(",");
6773c8ade361SLen Brown flush_output_stdout();
6774c8ade361SLen Brown return 0;
6775c8ade361SLen Brown }
6776c8ade361SLen Brown
6777103a8feaSLen Brown /*
6778103a8feaSLen Brown * if any params left, it must be a command to fork
6779103a8feaSLen Brown */
6780103a8feaSLen Brown if (argc - optind)
6781103a8feaSLen Brown return fork_it(argv + optind);
6782103a8feaSLen Brown else
6783103a8feaSLen Brown turbostat_loop();
6784103a8feaSLen Brown
6785103a8feaSLen Brown return 0;
6786103a8feaSLen Brown }
6787