1 // SPDX-License-Identifier: GPL-2.0-only
3 * turbostat -- show CPU frequency and C-state residency
4 * on modern Intel and AMD processors.
6 * Copyright (c) 2023 Intel Corporation.
7 * Len Brown <len.brown@intel.com>
12 #include INTEL_FAMILY_HEADER
17 #include <sys/types.h>
20 #include <sys/select.h>
21 #include <sys/resource.h>
33 #include <sys/capability.h>
36 #include <linux/perf_event.h>
37 #include <asm/unistd.h>
40 #define UNUSED(x) (void)(x)
43 * This list matches the column headers, except
44 * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
45 * 2. Core and CPU are moved to the end, we can't have strings that contain them
46 * matching on them for --show and --hide.
50 * buffer size used by sscanf() for added column names
51 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
54 #define PATH_BYTES 128
56 enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE };
57 enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC };
58 enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT };
62 char name[NAME_BYTES];
63 char path[PATH_BYTES];
65 enum counter_type type;
66 enum counter_format format;
67 struct msr_counter *next;
69 #define FLAGS_HIDE (1 << 0)
70 #define FLAGS_SHOW (1 << 1)
71 #define SYSFS_PERCPU (1 << 1)
74 struct msr_counter bic[] = {
75 { 0x0, "usec", "", 0, 0, 0, NULL, 0 },
76 { 0x0, "Time_Of_Day_Seconds", "", 0, 0, 0, NULL, 0 },
77 { 0x0, "Package", "", 0, 0, 0, NULL, 0 },
78 { 0x0, "Node", "", 0, 0, 0, NULL, 0 },
79 { 0x0, "Avg_MHz", "", 0, 0, 0, NULL, 0 },
80 { 0x0, "Busy%", "", 0, 0, 0, NULL, 0 },
81 { 0x0, "Bzy_MHz", "", 0, 0, 0, NULL, 0 },
82 { 0x0, "TSC_MHz", "", 0, 0, 0, NULL, 0 },
83 { 0x0, "IRQ", "", 0, 0, 0, NULL, 0 },
84 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL, 0 },
85 { 0x0, "sysfs", "", 0, 0, 0, NULL, 0 },
86 { 0x0, "CPU%c1", "", 0, 0, 0, NULL, 0 },
87 { 0x0, "CPU%c3", "", 0, 0, 0, NULL, 0 },
88 { 0x0, "CPU%c6", "", 0, 0, 0, NULL, 0 },
89 { 0x0, "CPU%c7", "", 0, 0, 0, NULL, 0 },
90 { 0x0, "ThreadC", "", 0, 0, 0, NULL, 0 },
91 { 0x0, "CoreTmp", "", 0, 0, 0, NULL, 0 },
92 { 0x0, "CoreCnt", "", 0, 0, 0, NULL, 0 },
93 { 0x0, "PkgTmp", "", 0, 0, 0, NULL, 0 },
94 { 0x0, "GFX%rc6", "", 0, 0, 0, NULL, 0 },
95 { 0x0, "GFXMHz", "", 0, 0, 0, NULL, 0 },
96 { 0x0, "Pkg%pc2", "", 0, 0, 0, NULL, 0 },
97 { 0x0, "Pkg%pc3", "", 0, 0, 0, NULL, 0 },
98 { 0x0, "Pkg%pc6", "", 0, 0, 0, NULL, 0 },
99 { 0x0, "Pkg%pc7", "", 0, 0, 0, NULL, 0 },
100 { 0x0, "Pkg%pc8", "", 0, 0, 0, NULL, 0 },
101 { 0x0, "Pkg%pc9", "", 0, 0, 0, NULL, 0 },
102 { 0x0, "Pk%pc10", "", 0, 0, 0, NULL, 0 },
103 { 0x0, "CPU%LPI", "", 0, 0, 0, NULL, 0 },
104 { 0x0, "SYS%LPI", "", 0, 0, 0, NULL, 0 },
105 { 0x0, "PkgWatt", "", 0, 0, 0, NULL, 0 },
106 { 0x0, "CorWatt", "", 0, 0, 0, NULL, 0 },
107 { 0x0, "GFXWatt", "", 0, 0, 0, NULL, 0 },
108 { 0x0, "PkgCnt", "", 0, 0, 0, NULL, 0 },
109 { 0x0, "RAMWatt", "", 0, 0, 0, NULL, 0 },
110 { 0x0, "PKG_%", "", 0, 0, 0, NULL, 0 },
111 { 0x0, "RAM_%", "", 0, 0, 0, NULL, 0 },
112 { 0x0, "Pkg_J", "", 0, 0, 0, NULL, 0 },
113 { 0x0, "Cor_J", "", 0, 0, 0, NULL, 0 },
114 { 0x0, "GFX_J", "", 0, 0, 0, NULL, 0 },
115 { 0x0, "RAM_J", "", 0, 0, 0, NULL, 0 },
116 { 0x0, "Mod%c6", "", 0, 0, 0, NULL, 0 },
117 { 0x0, "Totl%C0", "", 0, 0, 0, NULL, 0 },
118 { 0x0, "Any%C0", "", 0, 0, 0, NULL, 0 },
119 { 0x0, "GFX%C0", "", 0, 0, 0, NULL, 0 },
120 { 0x0, "CPUGFX%", "", 0, 0, 0, NULL, 0 },
121 { 0x0, "Core", "", 0, 0, 0, NULL, 0 },
122 { 0x0, "CPU", "", 0, 0, 0, NULL, 0 },
123 { 0x0, "APIC", "", 0, 0, 0, NULL, 0 },
124 { 0x0, "X2APIC", "", 0, 0, 0, NULL, 0 },
125 { 0x0, "Die", "", 0, 0, 0, NULL, 0 },
126 { 0x0, "GFXAMHz", "", 0, 0, 0, NULL, 0 },
127 { 0x0, "IPC", "", 0, 0, 0, NULL, 0 },
128 { 0x0, "CoreThr", "", 0, 0, 0, NULL, 0 },
129 { 0x0, "UncMHz", "", 0, 0, 0, NULL, 0 },
132 #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
133 #define BIC_USEC (1ULL << 0)
134 #define BIC_TOD (1ULL << 1)
135 #define BIC_Package (1ULL << 2)
136 #define BIC_Node (1ULL << 3)
137 #define BIC_Avg_MHz (1ULL << 4)
138 #define BIC_Busy (1ULL << 5)
139 #define BIC_Bzy_MHz (1ULL << 6)
140 #define BIC_TSC_MHz (1ULL << 7)
141 #define BIC_IRQ (1ULL << 8)
142 #define BIC_SMI (1ULL << 9)
143 #define BIC_sysfs (1ULL << 10)
144 #define BIC_CPU_c1 (1ULL << 11)
145 #define BIC_CPU_c3 (1ULL << 12)
146 #define BIC_CPU_c6 (1ULL << 13)
147 #define BIC_CPU_c7 (1ULL << 14)
148 #define BIC_ThreadC (1ULL << 15)
149 #define BIC_CoreTmp (1ULL << 16)
150 #define BIC_CoreCnt (1ULL << 17)
151 #define BIC_PkgTmp (1ULL << 18)
152 #define BIC_GFX_rc6 (1ULL << 19)
153 #define BIC_GFXMHz (1ULL << 20)
154 #define BIC_Pkgpc2 (1ULL << 21)
155 #define BIC_Pkgpc3 (1ULL << 22)
156 #define BIC_Pkgpc6 (1ULL << 23)
157 #define BIC_Pkgpc7 (1ULL << 24)
158 #define BIC_Pkgpc8 (1ULL << 25)
159 #define BIC_Pkgpc9 (1ULL << 26)
160 #define BIC_Pkgpc10 (1ULL << 27)
161 #define BIC_CPU_LPI (1ULL << 28)
162 #define BIC_SYS_LPI (1ULL << 29)
163 #define BIC_PkgWatt (1ULL << 30)
164 #define BIC_CorWatt (1ULL << 31)
165 #define BIC_GFXWatt (1ULL << 32)
166 #define BIC_PkgCnt (1ULL << 33)
167 #define BIC_RAMWatt (1ULL << 34)
168 #define BIC_PKG__ (1ULL << 35)
169 #define BIC_RAM__ (1ULL << 36)
170 #define BIC_Pkg_J (1ULL << 37)
171 #define BIC_Cor_J (1ULL << 38)
172 #define BIC_GFX_J (1ULL << 39)
173 #define BIC_RAM_J (1ULL << 40)
174 #define BIC_Mod_c6 (1ULL << 41)
175 #define BIC_Totl_c0 (1ULL << 42)
176 #define BIC_Any_c0 (1ULL << 43)
177 #define BIC_GFX_c0 (1ULL << 44)
178 #define BIC_CPUGFX (1ULL << 45)
179 #define BIC_Core (1ULL << 46)
180 #define BIC_CPU (1ULL << 47)
181 #define BIC_APIC (1ULL << 48)
182 #define BIC_X2APIC (1ULL << 49)
183 #define BIC_Die (1ULL << 50)
184 #define BIC_GFXACTMHz (1ULL << 51)
185 #define BIC_IPC (1ULL << 52)
186 #define BIC_CORE_THROT_CNT (1ULL << 53)
187 #define BIC_UNCORE_MHZ (1ULL << 54)
189 #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die )
190 #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__)
191 #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz | BIC_UNCORE_MHZ)
192 #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)
193 #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC)
195 #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
197 unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
198 unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
200 #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
201 #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
202 #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
203 #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
204 #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
205 #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT)
207 char *proc_stat = "/proc/stat";
210 int *fd_instr_count_percpu;
211 struct timeval interval_tv = { 5, 0 };
212 struct timespec interval_ts = { 5, 0 };
214 unsigned int num_iterations;
215 unsigned int header_iterations;
219 unsigned int sums_need_wide_columns;
220 unsigned int rapl_joules;
221 unsigned int summary_only;
222 unsigned int list_header_only;
223 unsigned int dump_only;
224 unsigned int do_snb_cstates;
225 unsigned int do_knl_cstates;
226 unsigned int do_slm_cstates;
227 unsigned int use_c1_residency_msr;
228 unsigned int has_aperf;
229 unsigned int has_epb;
230 unsigned int has_turbo;
231 unsigned int is_hybrid;
232 unsigned int do_irtl_snb;
233 unsigned int do_irtl_hsw;
234 unsigned int units = 1000000; /* MHz etc */
235 unsigned int genuine_intel;
236 unsigned int authentic_amd;
237 unsigned int hygon_genuine;
238 unsigned int max_level, max_extended_level;
239 unsigned int has_invariant_tsc;
240 unsigned int do_nhm_platform_info;
241 unsigned int no_MSR_MISC_PWR_MGMT;
242 unsigned int aperf_mperf_multiplier = 1;
245 unsigned int has_base_hz;
246 double tsc_tweak = 1.0;
247 unsigned int show_pkg_only;
248 unsigned int show_core_only;
249 char *output_buffer, *outp;
250 unsigned int do_rapl;
254 unsigned long long gfx_cur_rc6_ms;
255 unsigned long long cpuidle_cur_cpu_lpi_us;
256 unsigned long long cpuidle_cur_sys_lpi_us;
257 unsigned int gfx_cur_mhz;
258 unsigned int gfx_act_mhz;
260 unsigned int tj_max_override;
262 double rapl_power_units, rapl_time_units;
263 double rapl_dram_energy_units, rapl_energy_units;
264 double rapl_joule_counter_range;
265 unsigned int do_core_perf_limit_reasons;
266 unsigned int has_automatic_cstate_conversion;
267 unsigned int dis_cstate_prewake;
268 unsigned int do_gfx_perf_limit_reasons;
269 unsigned int do_ring_perf_limit_reasons;
270 unsigned int crystal_hz;
271 unsigned long long tsc_hz;
273 double discover_bclk(unsigned int family, unsigned int model);
274 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
275 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
276 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
277 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
278 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
279 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
280 unsigned int has_misc_feature_control;
281 unsigned int first_counter_read = 1;
284 /* Model specific support Start */
286 /* List of features that may diverge among different platforms */
287 struct platform_features {
290 struct platform_data {
292 const struct platform_features *features;
295 static const struct platform_features nhm_features = {
298 static const struct platform_features nhx_features = {
301 static const struct platform_features snb_features = {
304 static const struct platform_features snx_features = {
307 static const struct platform_features ivb_features = {
310 static const struct platform_features ivx_features = {
313 static const struct platform_features hsw_features = {
316 static const struct platform_features hsx_features = {
319 static const struct platform_features hswl_features = {
322 static const struct platform_features hswg_features = {
325 static const struct platform_features bdw_features = {
328 static const struct platform_features bdwg_features = {
331 static const struct platform_features bdx_features = {
334 static const struct platform_features skl_features = {
337 static const struct platform_features cnl_features = {
340 static const struct platform_features skx_features = {
343 static const struct platform_features icx_features = {
346 static const struct platform_features spr_features = {
349 static const struct platform_features slv_features = {
352 static const struct platform_features slvd_features = {
355 static const struct platform_features amt_features = {
358 static const struct platform_features gmt_features = {
361 static const struct platform_features gmtd_features = {
364 static const struct platform_features gmtp_features = {
367 static const struct platform_features tmt_features = {
370 static const struct platform_features tmtd_features = {
373 static const struct platform_features knl_features = {
376 static const struct platform_features default_features = {
379 static const struct platform_features amd_features = {
382 static const struct platform_data turbostat_pdata[] = {
383 { INTEL_FAM6_NEHALEM, &nhm_features },
384 { INTEL_FAM6_NEHALEM_G, &nhm_features },
385 { INTEL_FAM6_NEHALEM_EP, &nhm_features },
386 { INTEL_FAM6_NEHALEM_EX, &nhx_features },
387 { INTEL_FAM6_WESTMERE, &nhm_features },
388 { INTEL_FAM6_WESTMERE_EP, &nhm_features },
389 { INTEL_FAM6_WESTMERE_EX, &nhx_features },
390 { INTEL_FAM6_SANDYBRIDGE, &snb_features },
391 { INTEL_FAM6_SANDYBRIDGE_X, &snx_features },
392 { INTEL_FAM6_IVYBRIDGE, &ivb_features },
393 { INTEL_FAM6_IVYBRIDGE_X, &ivx_features },
394 { INTEL_FAM6_HASWELL, &hsw_features },
395 { INTEL_FAM6_HASWELL_X, &hsx_features },
396 { INTEL_FAM6_HASWELL_L, &hswl_features },
397 { INTEL_FAM6_HASWELL_G, &hswg_features },
398 { INTEL_FAM6_BROADWELL, &bdw_features },
399 { INTEL_FAM6_BROADWELL_G, &bdwg_features },
400 { INTEL_FAM6_BROADWELL_X, &bdx_features },
401 { INTEL_FAM6_BROADWELL_D, &bdx_features },
402 { INTEL_FAM6_SKYLAKE_L, &skl_features },
403 { INTEL_FAM6_SKYLAKE, &skl_features },
404 { INTEL_FAM6_SKYLAKE_X, &skx_features },
405 { INTEL_FAM6_KABYLAKE_L, &skl_features },
406 { INTEL_FAM6_KABYLAKE, &skl_features },
407 { INTEL_FAM6_COMETLAKE, &skl_features },
408 { INTEL_FAM6_COMETLAKE_L, &skl_features },
409 { INTEL_FAM6_CANNONLAKE_L, &cnl_features },
410 { INTEL_FAM6_ICELAKE_X, &icx_features },
411 { INTEL_FAM6_ICELAKE_D, &icx_features },
412 { INTEL_FAM6_ICELAKE_L, &cnl_features },
413 { INTEL_FAM6_ICELAKE_NNPI, &cnl_features },
414 { INTEL_FAM6_ROCKETLAKE, &cnl_features },
415 { INTEL_FAM6_TIGERLAKE_L, &cnl_features },
416 { INTEL_FAM6_TIGERLAKE, &cnl_features },
417 { INTEL_FAM6_SAPPHIRERAPIDS_X, &spr_features },
418 { INTEL_FAM6_EMERALDRAPIDS_X, &spr_features },
419 { INTEL_FAM6_LAKEFIELD, &cnl_features },
420 { INTEL_FAM6_ALDERLAKE, &cnl_features },
421 { INTEL_FAM6_ALDERLAKE_L, &cnl_features },
422 { INTEL_FAM6_RAPTORLAKE, &cnl_features },
423 { INTEL_FAM6_RAPTORLAKE_P, &cnl_features },
424 { INTEL_FAM6_RAPTORLAKE_S, &cnl_features },
425 { INTEL_FAM6_METEORLAKE, &cnl_features },
426 { INTEL_FAM6_METEORLAKE_L, &cnl_features },
427 { INTEL_FAM6_ATOM_SILVERMONT, &slv_features },
428 { INTEL_FAM6_ATOM_SILVERMONT_D, &slvd_features },
429 { INTEL_FAM6_ATOM_AIRMONT, &amt_features },
430 { INTEL_FAM6_ATOM_GOLDMONT, &gmt_features },
431 { INTEL_FAM6_ATOM_GOLDMONT_D, &gmtd_features },
432 { INTEL_FAM6_ATOM_GOLDMONT_PLUS, &gmtp_features },
433 { INTEL_FAM6_ATOM_TREMONT_D, &tmtd_features },
434 { INTEL_FAM6_ATOM_TREMONT, &tmt_features },
435 { INTEL_FAM6_ATOM_TREMONT_L, &tmt_features },
436 { INTEL_FAM6_ATOM_GRACEMONT, &cnl_features },
437 { INTEL_FAM6_XEON_PHI_KNL, &knl_features },
438 { INTEL_FAM6_XEON_PHI_KNM, &knl_features },
440 * Missing support for
442 * INTEL_FAM6_ATOM_SILVERMONT_MID
443 * INTEL_FAM6_ATOM_AIRMONT_MID
444 * INTEL_FAM6_ATOM_AIRMONT_NP
449 static const struct platform_features *platform;
451 void probe_platform_features(unsigned int family, unsigned int model)
455 if (authentic_amd || hygon_genuine) {
456 platform = &amd_features;
460 platform = &default_features;
462 if (!genuine_intel || family != 6)
465 for (i = 0; turbostat_pdata[i].features; i++) {
466 if (turbostat_pdata[i].model == model) {
467 platform = turbostat_pdata[i].features;
473 /* Model specific support End */
475 #define RAPL_PKG (1 << 0)
476 /* 0x610 MSR_PKG_POWER_LIMIT */
477 /* 0x611 MSR_PKG_ENERGY_STATUS */
478 #define RAPL_PKG_PERF_STATUS (1 << 1)
479 /* 0x613 MSR_PKG_PERF_STATUS */
480 #define RAPL_PKG_POWER_INFO (1 << 2)
481 /* 0x614 MSR_PKG_POWER_INFO */
483 #define RAPL_DRAM (1 << 3)
484 /* 0x618 MSR_DRAM_POWER_LIMIT */
485 /* 0x619 MSR_DRAM_ENERGY_STATUS */
486 #define RAPL_DRAM_PERF_STATUS (1 << 4)
487 /* 0x61b MSR_DRAM_PERF_STATUS */
488 #define RAPL_DRAM_POWER_INFO (1 << 5)
489 /* 0x61c MSR_DRAM_POWER_INFO */
491 #define RAPL_CORES_POWER_LIMIT (1 << 6)
492 /* 0x638 MSR_PP0_POWER_LIMIT */
493 #define RAPL_CORE_POLICY (1 << 7)
494 /* 0x63a MSR_PP0_POLICY */
496 #define RAPL_GFX (1 << 8)
497 /* 0x640 MSR_PP1_POWER_LIMIT */
498 /* 0x641 MSR_PP1_ENERGY_STATUS */
499 /* 0x642 MSR_PP1_POLICY */
501 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
502 /* 0x639 MSR_PP0_ENERGY_STATUS */
503 #define RAPL_PER_CORE_ENERGY (1 << 10)
504 /* Indicates cores energy collection is per-core,
505 * not per-package. */
506 #define RAPL_AMD_F17H (1 << 11)
507 /* 0xc0010299 MSR_RAPL_PWR_UNIT */
508 /* 0xc001029a MSR_CORE_ENERGY_STAT */
509 /* 0xc001029b MSR_PKG_ENERGY_STAT */
510 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
511 #define TJMAX_DEFAULT 100
513 /* MSRs that are not yet in the kernel-provided header. */
514 #define MSR_RAPL_PWR_UNIT 0xc0010299
515 #define MSR_CORE_ENERGY_STAT 0xc001029a
516 #define MSR_PKG_ENERGY_STAT 0xc001029b
518 #define MAX(a, b) ((a) > (b) ? (a) : (b))
523 #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
524 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
525 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
526 #define MAX_ADDED_COUNTERS 8
527 #define MAX_ADDED_THREAD_COUNTERS 24
528 #define BITMASK_SIZE 32
531 struct timeval tv_begin;
532 struct timeval tv_end;
533 struct timeval tv_delta;
534 unsigned long long tsc;
535 unsigned long long aperf;
536 unsigned long long mperf;
537 unsigned long long c1;
538 unsigned long long instr_count;
539 unsigned long long irq_count;
540 unsigned int smi_count;
542 unsigned int apic_id;
543 unsigned int x2apic_id;
546 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
547 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
548 unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
549 } *thread_even, *thread_odd;
552 unsigned long long c3;
553 unsigned long long c6;
554 unsigned long long c7;
555 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
556 unsigned int core_temp_c;
557 unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */
558 unsigned int core_id;
559 unsigned long long core_throt_cnt;
560 unsigned long long counter[MAX_ADDED_COUNTERS];
561 } *core_even, *core_odd;
564 unsigned long long pc2;
565 unsigned long long pc3;
566 unsigned long long pc6;
567 unsigned long long pc7;
568 unsigned long long pc8;
569 unsigned long long pc9;
570 unsigned long long pc10;
571 unsigned long long cpu_lpi;
572 unsigned long long sys_lpi;
573 unsigned long long pkg_wtd_core_c0;
574 unsigned long long pkg_any_core_c0;
575 unsigned long long pkg_any_gfxe_c0;
576 unsigned long long pkg_both_core_gfxe_c0;
577 long long gfx_rc6_ms;
578 unsigned int gfx_mhz;
579 unsigned int gfx_act_mhz;
580 unsigned int package_id;
581 unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */
582 unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */
583 unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */
584 unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */
585 unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
586 unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
587 unsigned int pkg_temp_c;
588 unsigned int uncore_mhz;
589 unsigned long long counter[MAX_ADDED_COUNTERS];
590 } *package_even, *package_odd;
592 #define ODD_COUNTERS thread_odd, core_odd, package_odd
593 #define EVEN_COUNTERS thread_even, core_even, package_even
595 #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \
598 topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
599 ((node_no) * topo.cores_per_node * topo.threads_per_core) + \
600 ((core_no) * topo.threads_per_core) + \
603 #define GET_CORE(core_base, core_no, node_no, pkg_no) \
605 ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \
606 ((node_no) * topo.cores_per_node) + \
609 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
612 * The accumulated sum of MSR is defined as a monotonic
613 * increasing MSR, it will be accumulated periodically,
614 * despite its register's bit width.
626 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr);
628 struct msr_sum_array {
629 /* get_msr_sum() = sum + (get_msr() - last) */
631 /*The accumulated MSR value is updated by the timer */
632 unsigned long long sum;
633 /*The MSR footprint recorded in last timer */
634 unsigned long long last;
635 } entries[IDX_COUNT];
638 /* The percpu MSR sum array.*/
639 struct msr_sum_array *per_cpu_msr_sum;
641 off_t idx_to_offset(int idx)
647 if (do_rapl & RAPL_AMD_F17H)
648 offset = MSR_PKG_ENERGY_STAT;
650 offset = MSR_PKG_ENERGY_STATUS;
652 case IDX_DRAM_ENERGY:
653 offset = MSR_DRAM_ENERGY_STATUS;
656 offset = MSR_PP0_ENERGY_STATUS;
659 offset = MSR_PP1_ENERGY_STATUS;
662 offset = MSR_PKG_PERF_STATUS;
665 offset = MSR_DRAM_PERF_STATUS;
673 int offset_to_idx(off_t offset)
678 case MSR_PKG_ENERGY_STATUS:
679 case MSR_PKG_ENERGY_STAT:
680 idx = IDX_PKG_ENERGY;
682 case MSR_DRAM_ENERGY_STATUS:
683 idx = IDX_DRAM_ENERGY;
685 case MSR_PP0_ENERGY_STATUS:
686 idx = IDX_PP0_ENERGY;
688 case MSR_PP1_ENERGY_STATUS:
689 idx = IDX_PP1_ENERGY;
691 case MSR_PKG_PERF_STATUS:
694 case MSR_DRAM_PERF_STATUS:
703 int idx_valid(int idx)
707 return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
708 case IDX_DRAM_ENERGY:
709 return do_rapl & RAPL_DRAM;
711 return do_rapl & RAPL_CORES_ENERGY_STATUS;
713 return do_rapl & RAPL_GFX;
715 return do_rapl & RAPL_PKG_PERF_STATUS;
717 return do_rapl & RAPL_DRAM_PERF_STATUS;
723 struct sys_counters {
724 unsigned int added_thread_counters;
725 unsigned int added_core_counters;
726 unsigned int added_package_counters;
727 struct msr_counter *tp;
728 struct msr_counter *cp;
729 struct msr_counter *pp;
732 struct system_summary {
733 struct thread_data threads;
734 struct core_data cores;
735 struct pkg_data packages;
738 struct cpu_topology {
739 int physical_package_id;
742 int physical_node_id;
743 int logical_node_id; /* 0-based count within the package */
744 int physical_core_id;
746 cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
758 int threads_per_core;
761 struct timeval tv_even, tv_odd, tv_delta;
763 int *irq_column_2_cpu; /* /proc/interrupts column numbers */
764 int *irqs_per_cpu; /* indexed by cpu_num */
766 void setup_all_buffers(void);
769 char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
770 char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
772 int cpu_is_not_present(int cpu)
774 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
778 * run func(thread, core, package) in topology order
779 * skip non-present cpus
782 int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *),
783 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
785 int retval, pkg_no, core_no, thread_no, node_no;
787 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
788 for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
789 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
790 for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
791 struct thread_data *t;
795 t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
797 if (cpu_is_not_present(t->cpu_id))
800 c = GET_CORE(core_base, core_no, node_no, pkg_no);
801 p = GET_PKG(pkg_base, pkg_no);
803 retval = func(t, c, p);
813 int cpu_migrate(int cpu)
815 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
816 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
817 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
823 int get_msr_fd(int cpu)
833 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
834 fd = open(pathname, O_RDONLY);
836 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
843 static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
845 return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
848 static int perf_instr_count_open(int cpu_num)
850 struct perf_event_attr pea;
853 memset(&pea, 0, sizeof(struct perf_event_attr));
854 pea.type = PERF_TYPE_HARDWARE;
855 pea.size = sizeof(struct perf_event_attr);
856 pea.config = PERF_COUNT_HW_INSTRUCTIONS;
858 /* counter for cpu_num, including user + kernel and all processes */
859 fd = perf_event_open(&pea, -1, cpu_num, -1, 0);
861 warnx("capget(CAP_PERFMON) failed, try \"# setcap cap_sys_admin=ep %s\"", progname);
862 BIC_NOT_PRESENT(BIC_IPC);
868 int get_instr_count_fd(int cpu)
870 if (fd_instr_count_percpu[cpu])
871 return fd_instr_count_percpu[cpu];
873 fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu);
875 return fd_instr_count_percpu[cpu];
878 int get_msr(int cpu, off_t offset, unsigned long long *msr)
882 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
884 if (retval != sizeof *msr)
885 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
890 #define MAX_DEFERRED 16
891 char *deferred_add_names[MAX_DEFERRED];
892 char *deferred_skip_names[MAX_DEFERRED];
893 int deferred_add_index;
894 int deferred_skip_index;
897 * HIDE_LIST - hide this list of counters, show the rest [default]
898 * SHOW_LIST - show this list of counters, hide the rest
900 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
905 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
907 "Turbostat forks the specified COMMAND and prints statistics\n"
908 "when COMMAND completes.\n"
909 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
910 "to print statistics, until interrupted.\n"
911 " -a, --add add a counter\n"
912 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
913 " -c, --cpu cpu-set limit output to summary plus cpu-set:\n"
914 " {core | package | j,k,l..m,n-p }\n"
915 " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n"
916 " -D, --Dump displays the raw counter values\n"
917 " -e, --enable [all | column]\n"
918 " shows all or the specified disabled column\n"
919 " -H, --hide [column|column,column,...]\n"
920 " hide the specified column(s)\n"
921 " -i, --interval sec.subsec\n"
922 " Override default 5-second measurement interval\n"
923 " -J, --Joules displays energy in Joules instead of Watts\n"
924 " -l, --list list column headers only\n"
925 " -n, --num_iterations num\n"
926 " number of the measurement iterations\n"
927 " -N, --header_iterations num\n"
928 " print header every num iterations\n"
930 " create or truncate \"file\" for all output\n"
931 " -q, --quiet skip decoding system configuration header\n"
932 " -s, --show [column|column,column,...]\n"
933 " show only the specified column(s)\n"
935 " limits output to 1-line system summary per interval\n"
936 " -T, --TCC temperature\n"
937 " sets the Thermal Control Circuit temperature in\n"
939 " -h, --help print this help message\n"
940 " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n");
945 * for all the strings in comma separate name_list,
946 * set the approprate bit in return value.
948 unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
951 unsigned long long retval = 0;
956 comma = strchr(name_list, ',');
961 for (i = 0; i < MAX_BIC; ++i) {
962 if (!strcmp(name_list, bic[i].name)) {
963 retval |= (1ULL << i);
966 if (!strcmp(name_list, "all")) {
969 } else if (!strcmp(name_list, "topology")) {
970 retval |= BIC_TOPOLOGY;
972 } else if (!strcmp(name_list, "power")) {
973 retval |= BIC_THERMAL_PWR;
975 } else if (!strcmp(name_list, "idle")) {
978 } else if (!strcmp(name_list, "frequency")) {
979 retval |= BIC_FREQUENCY;
981 } else if (!strcmp(name_list, "other")) {
988 if (mode == SHOW_LIST) {
989 deferred_add_names[deferred_add_index++] = name_list;
990 if (deferred_add_index >= MAX_DEFERRED) {
991 fprintf(stderr, "More than max %d un-recognized --add options '%s'\n",
992 MAX_DEFERRED, name_list);
997 deferred_skip_names[deferred_skip_index++] = name_list;
999 fprintf(stderr, "deferred \"%s\"\n", name_list);
1000 if (deferred_skip_index >= MAX_DEFERRED) {
1001 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
1002 MAX_DEFERRED, name_list);
1017 void print_header(char *delim)
1019 struct msr_counter *mp;
1022 if (DO_BIC(BIC_USEC))
1023 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
1024 if (DO_BIC(BIC_TOD))
1025 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
1026 if (DO_BIC(BIC_Package))
1027 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
1028 if (DO_BIC(BIC_Die))
1029 outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
1030 if (DO_BIC(BIC_Node))
1031 outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
1032 if (DO_BIC(BIC_Core))
1033 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
1034 if (DO_BIC(BIC_CPU))
1035 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
1036 if (DO_BIC(BIC_APIC))
1037 outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
1038 if (DO_BIC(BIC_X2APIC))
1039 outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
1040 if (DO_BIC(BIC_Avg_MHz))
1041 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
1042 if (DO_BIC(BIC_Busy))
1043 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
1044 if (DO_BIC(BIC_Bzy_MHz))
1045 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
1046 if (DO_BIC(BIC_TSC_MHz))
1047 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
1049 if (DO_BIC(BIC_IPC))
1050 outp += sprintf(outp, "%sIPC", (printed++ ? delim : ""));
1052 if (DO_BIC(BIC_IRQ)) {
1053 if (sums_need_wide_columns)
1054 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
1056 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
1059 if (DO_BIC(BIC_SMI))
1060 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
1062 for (mp = sys.tp; mp; mp = mp->next) {
1064 if (mp->format == FORMAT_RAW) {
1065 if (mp->width == 64)
1066 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
1068 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
1070 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1071 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
1073 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
1077 if (DO_BIC(BIC_CPU_c1))
1078 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
1079 if (DO_BIC(BIC_CPU_c3))
1080 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
1081 if (DO_BIC(BIC_CPU_c6))
1082 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
1083 if (DO_BIC(BIC_CPU_c7))
1084 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
1086 if (DO_BIC(BIC_Mod_c6))
1087 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
1089 if (DO_BIC(BIC_CoreTmp))
1090 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
1092 if (DO_BIC(BIC_CORE_THROT_CNT))
1093 outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : ""));
1095 if (do_rapl && !rapl_joules) {
1096 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
1097 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
1098 } else if (do_rapl && rapl_joules) {
1099 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
1100 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
1103 for (mp = sys.cp; mp; mp = mp->next) {
1104 if (mp->format == FORMAT_RAW) {
1105 if (mp->width == 64)
1106 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
1108 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
1110 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1111 outp += sprintf(outp, "%s%8s", delim, mp->name);
1113 outp += sprintf(outp, "%s%s", delim, mp->name);
1117 if (DO_BIC(BIC_PkgTmp))
1118 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
1120 if (DO_BIC(BIC_GFX_rc6))
1121 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
1123 if (DO_BIC(BIC_GFXMHz))
1124 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
1126 if (DO_BIC(BIC_GFXACTMHz))
1127 outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : ""));
1129 if (DO_BIC(BIC_Totl_c0))
1130 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
1131 if (DO_BIC(BIC_Any_c0))
1132 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
1133 if (DO_BIC(BIC_GFX_c0))
1134 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
1135 if (DO_BIC(BIC_CPUGFX))
1136 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
1138 if (DO_BIC(BIC_Pkgpc2))
1139 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
1140 if (DO_BIC(BIC_Pkgpc3))
1141 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
1142 if (DO_BIC(BIC_Pkgpc6))
1143 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
1144 if (DO_BIC(BIC_Pkgpc7))
1145 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
1146 if (DO_BIC(BIC_Pkgpc8))
1147 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
1148 if (DO_BIC(BIC_Pkgpc9))
1149 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
1150 if (DO_BIC(BIC_Pkgpc10))
1151 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
1152 if (DO_BIC(BIC_CPU_LPI))
1153 outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
1154 if (DO_BIC(BIC_SYS_LPI))
1155 outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
1157 if (do_rapl && !rapl_joules) {
1158 if (DO_BIC(BIC_PkgWatt))
1159 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
1160 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1161 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
1162 if (DO_BIC(BIC_GFXWatt))
1163 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
1164 if (DO_BIC(BIC_RAMWatt))
1165 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
1166 if (DO_BIC(BIC_PKG__))
1167 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
1168 if (DO_BIC(BIC_RAM__))
1169 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
1170 } else if (do_rapl && rapl_joules) {
1171 if (DO_BIC(BIC_Pkg_J))
1172 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
1173 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1174 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
1175 if (DO_BIC(BIC_GFX_J))
1176 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
1177 if (DO_BIC(BIC_RAM_J))
1178 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
1179 if (DO_BIC(BIC_PKG__))
1180 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
1181 if (DO_BIC(BIC_RAM__))
1182 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
1184 if (DO_BIC(BIC_UNCORE_MHZ))
1185 outp += sprintf(outp, "%sUncMHz", (printed++ ? delim : ""));
1187 for (mp = sys.pp; mp; mp = mp->next) {
1188 if (mp->format == FORMAT_RAW) {
1189 if (mp->width == 64)
1190 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
1192 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
1194 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1195 outp += sprintf(outp, "%s%8s", delim, mp->name);
1197 outp += sprintf(outp, "%s%s", delim, mp->name);
1201 outp += sprintf(outp, "\n");
1204 int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1207 struct msr_counter *mp;
1209 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
1212 outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
1213 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
1214 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
1215 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
1216 outp += sprintf(outp, "c1: %016llX\n", t->c1);
1218 if (DO_BIC(BIC_IPC))
1219 outp += sprintf(outp, "IPC: %lld\n", t->instr_count);
1221 if (DO_BIC(BIC_IRQ))
1222 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
1223 if (DO_BIC(BIC_SMI))
1224 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
1226 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1227 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]);
1232 outp += sprintf(outp, "core: %d\n", c->core_id);
1233 outp += sprintf(outp, "c3: %016llX\n", c->c3);
1234 outp += sprintf(outp, "c6: %016llX\n", c->c6);
1235 outp += sprintf(outp, "c7: %016llX\n", c->c7);
1236 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
1237 outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt);
1238 outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
1240 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1241 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]);
1243 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
1247 outp += sprintf(outp, "package: %d\n", p->package_id);
1249 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
1250 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
1251 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
1252 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
1254 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
1255 if (DO_BIC(BIC_Pkgpc3))
1256 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
1257 if (DO_BIC(BIC_Pkgpc6))
1258 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
1259 if (DO_BIC(BIC_Pkgpc7))
1260 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
1261 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
1262 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
1263 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
1264 outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
1265 outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
1266 outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
1267 outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
1268 outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
1269 outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
1270 outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status);
1271 outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status);
1272 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
1274 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1275 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]);
1279 outp += sprintf(outp, "\n");
1285 * column formatting convention & formats
1287 int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1289 double interval_float, tsc;
1292 struct msr_counter *mp;
1296 /* if showing only 1st thread in core and this isn't one, bail out */
1297 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1300 /* if showing only 1st thread in pkg and this isn't one, bail out */
1301 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1304 /*if not summary line and --cpu is used */
1305 if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
1308 if (DO_BIC(BIC_USEC)) {
1309 /* on each row, print how many usec each timestamp took to gather */
1312 timersub(&t->tv_end, &t->tv_begin, &tv);
1313 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
1316 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
1317 if (DO_BIC(BIC_TOD))
1318 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
1320 interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0;
1322 tsc = t->tsc * tsc_tweak;
1324 /* topo columns, print blanks on 1st (average) line */
1325 if (t == &average.threads) {
1326 if (DO_BIC(BIC_Package))
1327 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1328 if (DO_BIC(BIC_Die))
1329 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1330 if (DO_BIC(BIC_Node))
1331 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1332 if (DO_BIC(BIC_Core))
1333 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1334 if (DO_BIC(BIC_CPU))
1335 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1336 if (DO_BIC(BIC_APIC))
1337 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1338 if (DO_BIC(BIC_X2APIC))
1339 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1341 if (DO_BIC(BIC_Package)) {
1343 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
1345 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1347 if (DO_BIC(BIC_Die)) {
1349 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
1351 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1353 if (DO_BIC(BIC_Node)) {
1355 outp += sprintf(outp, "%s%d",
1356 (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id);
1358 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1360 if (DO_BIC(BIC_Core)) {
1362 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
1364 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1366 if (DO_BIC(BIC_CPU))
1367 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
1368 if (DO_BIC(BIC_APIC))
1369 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
1370 if (DO_BIC(BIC_X2APIC))
1371 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
1374 if (DO_BIC(BIC_Avg_MHz))
1375 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float);
1377 if (DO_BIC(BIC_Busy))
1378 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc);
1380 if (DO_BIC(BIC_Bzy_MHz)) {
1383 sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
1385 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1386 tsc / units * t->aperf / t->mperf / interval_float);
1389 if (DO_BIC(BIC_TSC_MHz))
1390 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float);
1392 if (DO_BIC(BIC_IPC))
1393 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf);
1396 if (DO_BIC(BIC_IRQ)) {
1397 if (sums_need_wide_columns)
1398 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
1400 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
1404 if (DO_BIC(BIC_SMI))
1405 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
1407 /* Added counters */
1408 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1409 if (mp->format == FORMAT_RAW) {
1410 if (mp->width == 32)
1412 sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]);
1414 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1415 } else if (mp->format == FORMAT_DELTA) {
1416 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1417 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
1419 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1420 } else if (mp->format == FORMAT_PERCENT) {
1421 if (mp->type == COUNTER_USEC)
1423 sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
1424 t->counter[i] / interval_float / 10000);
1426 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc);
1431 if (DO_BIC(BIC_CPU_c1))
1432 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc);
1434 /* print per-core data only for 1st thread in core */
1435 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1438 if (DO_BIC(BIC_CPU_c3))
1439 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc);
1440 if (DO_BIC(BIC_CPU_c6))
1441 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc);
1442 if (DO_BIC(BIC_CPU_c7))
1443 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc);
1446 if (DO_BIC(BIC_Mod_c6))
1447 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
1449 if (DO_BIC(BIC_CoreTmp))
1450 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1452 /* Core throttle count */
1453 if (DO_BIC(BIC_CORE_THROT_CNT))
1454 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt);
1456 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1457 if (mp->format == FORMAT_RAW) {
1458 if (mp->width == 32)
1460 sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]);
1462 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1463 } else if (mp->format == FORMAT_DELTA) {
1464 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1465 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
1467 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1468 } else if (mp->format == FORMAT_PERCENT) {
1469 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc);
1475 if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
1477 sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
1478 if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
1479 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
1481 /* print per-package data only for 1st core in package */
1482 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1486 if (DO_BIC(BIC_PkgTmp))
1487 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1490 if (DO_BIC(BIC_GFX_rc6)) {
1491 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
1492 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
1494 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
1495 p->gfx_rc6_ms / 10.0 / interval_float);
1500 if (DO_BIC(BIC_GFXMHz))
1501 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
1504 if (DO_BIC(BIC_GFXACTMHz))
1505 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz);
1507 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1508 if (DO_BIC(BIC_Totl_c0))
1509 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc);
1510 if (DO_BIC(BIC_Any_c0))
1511 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc);
1512 if (DO_BIC(BIC_GFX_c0))
1513 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc);
1514 if (DO_BIC(BIC_CPUGFX))
1515 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc);
1517 if (DO_BIC(BIC_Pkgpc2))
1518 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc);
1519 if (DO_BIC(BIC_Pkgpc3))
1520 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc);
1521 if (DO_BIC(BIC_Pkgpc6))
1522 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc);
1523 if (DO_BIC(BIC_Pkgpc7))
1524 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc);
1525 if (DO_BIC(BIC_Pkgpc8))
1526 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc);
1527 if (DO_BIC(BIC_Pkgpc9))
1528 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc);
1529 if (DO_BIC(BIC_Pkgpc10))
1530 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc);
1532 if (DO_BIC(BIC_CPU_LPI))
1534 sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1535 if (DO_BIC(BIC_SYS_LPI))
1537 sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1539 if (DO_BIC(BIC_PkgWatt))
1541 sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1543 if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1545 sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1546 if (DO_BIC(BIC_GFXWatt))
1548 sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1549 if (DO_BIC(BIC_RAMWatt))
1551 sprintf(outp, fmt8, (printed++ ? delim : ""),
1552 p->energy_dram * rapl_dram_energy_units / interval_float);
1553 if (DO_BIC(BIC_Pkg_J))
1554 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1555 if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
1556 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1557 if (DO_BIC(BIC_GFX_J))
1558 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1559 if (DO_BIC(BIC_RAM_J))
1560 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1561 if (DO_BIC(BIC_PKG__))
1563 sprintf(outp, fmt8, (printed++ ? delim : ""),
1564 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1565 if (DO_BIC(BIC_RAM__))
1567 sprintf(outp, fmt8, (printed++ ? delim : ""),
1568 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1570 if (DO_BIC(BIC_UNCORE_MHZ))
1571 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->uncore_mhz);
1573 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1574 if (mp->format == FORMAT_RAW) {
1575 if (mp->width == 32)
1577 sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]);
1579 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1580 } else if (mp->format == FORMAT_DELTA) {
1581 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1582 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
1584 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1585 } else if (mp->format == FORMAT_PERCENT) {
1586 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc);
1591 if (*(outp - 1) != '\n')
1592 outp += sprintf(outp, "\n");
1597 void flush_output_stdout(void)
1606 fputs(output_buffer, filep);
1609 outp = output_buffer;
1612 void flush_output_stderr(void)
1614 fputs(output_buffer, outf);
1616 outp = output_buffer;
1619 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1623 if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only)
1626 format_counters(&average.threads, &average.cores, &average.packages);
1633 for_all_cpus(format_counters, t, c, p);
1636 #define DELTA_WRAP32(new, old) \
1637 old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1639 int delta_package(struct pkg_data *new, struct pkg_data *old)
1642 struct msr_counter *mp;
1644 if (DO_BIC(BIC_Totl_c0))
1645 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1646 if (DO_BIC(BIC_Any_c0))
1647 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1648 if (DO_BIC(BIC_GFX_c0))
1649 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1650 if (DO_BIC(BIC_CPUGFX))
1651 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1653 old->pc2 = new->pc2 - old->pc2;
1654 if (DO_BIC(BIC_Pkgpc3))
1655 old->pc3 = new->pc3 - old->pc3;
1656 if (DO_BIC(BIC_Pkgpc6))
1657 old->pc6 = new->pc6 - old->pc6;
1658 if (DO_BIC(BIC_Pkgpc7))
1659 old->pc7 = new->pc7 - old->pc7;
1660 old->pc8 = new->pc8 - old->pc8;
1661 old->pc9 = new->pc9 - old->pc9;
1662 old->pc10 = new->pc10 - old->pc10;
1663 old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1664 old->sys_lpi = new->sys_lpi - old->sys_lpi;
1665 old->pkg_temp_c = new->pkg_temp_c;
1667 /* flag an error when rc6 counter resets/wraps */
1668 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1669 old->gfx_rc6_ms = -1;
1671 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1673 old->uncore_mhz = new->uncore_mhz;
1674 old->gfx_mhz = new->gfx_mhz;
1675 old->gfx_act_mhz = new->gfx_act_mhz;
1677 old->energy_pkg = new->energy_pkg - old->energy_pkg;
1678 old->energy_cores = new->energy_cores - old->energy_cores;
1679 old->energy_gfx = new->energy_gfx - old->energy_gfx;
1680 old->energy_dram = new->energy_dram - old->energy_dram;
1681 old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status;
1682 old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status;
1684 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1685 if (mp->format == FORMAT_RAW)
1686 old->counter[i] = new->counter[i];
1688 old->counter[i] = new->counter[i] - old->counter[i];
1694 void delta_core(struct core_data *new, struct core_data *old)
1697 struct msr_counter *mp;
1699 old->c3 = new->c3 - old->c3;
1700 old->c6 = new->c6 - old->c6;
1701 old->c7 = new->c7 - old->c7;
1702 old->core_temp_c = new->core_temp_c;
1703 old->core_throt_cnt = new->core_throt_cnt;
1704 old->mc6_us = new->mc6_us - old->mc6_us;
1706 DELTA_WRAP32(new->core_energy, old->core_energy);
1708 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1709 if (mp->format == FORMAT_RAW)
1710 old->counter[i] = new->counter[i];
1712 old->counter[i] = new->counter[i] - old->counter[i];
1716 int soft_c1_residency_display(int bic)
1718 if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
1721 return DO_BIC_READ(bic);
1727 int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta)
1730 struct msr_counter *mp;
1732 /* we run cpuid just the 1st time, copy the results */
1733 if (DO_BIC(BIC_APIC))
1734 new->apic_id = old->apic_id;
1735 if (DO_BIC(BIC_X2APIC))
1736 new->x2apic_id = old->x2apic_id;
1739 * the timestamps from start of measurement interval are in "old"
1740 * the timestamp from end of measurement interval are in "new"
1741 * over-write old w/ new so we can print end of interval values
1744 timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
1745 old->tv_begin = new->tv_begin;
1746 old->tv_end = new->tv_end;
1748 old->tsc = new->tsc - old->tsc;
1750 /* check for TSC < 1 Mcycles over interval */
1751 if (old->tsc < (1000 * 1000))
1752 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1753 "You can disable all c-states by booting with \"idle=poll\"\n"
1754 "or just the deep ones with \"processor.max_cstate=1\"");
1756 old->c1 = new->c1 - old->c1;
1758 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
1759 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1760 old->aperf = new->aperf - old->aperf;
1761 old->mperf = new->mperf - old->mperf;
1767 if (use_c1_residency_msr) {
1769 * Some models have a dedicated C1 residency MSR,
1770 * which should be more accurate than the derivation below.
1774 * As counter collection is not atomic,
1775 * it is possible for mperf's non-halted cycles + idle states
1776 * to exceed TSC's all cycles: show c1 = 0% in that case.
1778 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1781 /* normal case, derive c1 */
1782 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1783 - core_delta->c6 - core_delta->c7;
1787 if (old->mperf == 0) {
1789 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1790 old->mperf = 1; /* divide by 0 protection */
1793 if (DO_BIC(BIC_IPC))
1794 old->instr_count = new->instr_count - old->instr_count;
1796 if (DO_BIC(BIC_IRQ))
1797 old->irq_count = new->irq_count - old->irq_count;
1799 if (DO_BIC(BIC_SMI))
1800 old->smi_count = new->smi_count - old->smi_count;
1802 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1803 if (mp->format == FORMAT_RAW)
1804 old->counter[i] = new->counter[i];
1806 old->counter[i] = new->counter[i] - old->counter[i];
1811 int delta_cpu(struct thread_data *t, struct core_data *c,
1812 struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2)
1816 /* calculate core delta only for 1st thread in core */
1817 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1820 /* always calculate thread delta */
1821 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1825 /* calculate package delta only for 1st core in package */
1826 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1827 retval = delta_package(p, p2);
1832 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1835 struct msr_counter *mp;
1837 t->tv_begin.tv_sec = 0;
1838 t->tv_begin.tv_usec = 0;
1839 t->tv_end.tv_sec = 0;
1840 t->tv_end.tv_usec = 0;
1841 t->tv_delta.tv_sec = 0;
1842 t->tv_delta.tv_usec = 0;
1854 /* tells format_counters to dump all fields from this set */
1855 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1863 c->core_throt_cnt = 0;
1865 p->pkg_wtd_core_c0 = 0;
1866 p->pkg_any_core_c0 = 0;
1867 p->pkg_any_gfxe_c0 = 0;
1868 p->pkg_both_core_gfxe_c0 = 0;
1871 if (DO_BIC(BIC_Pkgpc3))
1873 if (DO_BIC(BIC_Pkgpc6))
1875 if (DO_BIC(BIC_Pkgpc7))
1885 p->energy_cores = 0;
1887 p->rapl_pkg_perf_status = 0;
1888 p->rapl_dram_perf_status = 0;
1895 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1898 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1901 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1905 int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1908 struct msr_counter *mp;
1910 /* copy un-changing apic_id's */
1911 if (DO_BIC(BIC_APIC))
1912 average.threads.apic_id = t->apic_id;
1913 if (DO_BIC(BIC_X2APIC))
1914 average.threads.x2apic_id = t->x2apic_id;
1916 /* remember first tv_begin */
1917 if (average.threads.tv_begin.tv_sec == 0)
1918 average.threads.tv_begin = t->tv_begin;
1920 /* remember last tv_end */
1921 average.threads.tv_end = t->tv_end;
1923 average.threads.tsc += t->tsc;
1924 average.threads.aperf += t->aperf;
1925 average.threads.mperf += t->mperf;
1926 average.threads.c1 += t->c1;
1928 average.threads.instr_count += t->instr_count;
1930 average.threads.irq_count += t->irq_count;
1931 average.threads.smi_count += t->smi_count;
1933 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1934 if (mp->format == FORMAT_RAW)
1936 average.threads.counter[i] += t->counter[i];
1939 /* sum per-core values only for 1st thread in core */
1940 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1943 average.cores.c3 += c->c3;
1944 average.cores.c6 += c->c6;
1945 average.cores.c7 += c->c7;
1946 average.cores.mc6_us += c->mc6_us;
1948 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1949 average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt);
1951 average.cores.core_energy += c->core_energy;
1953 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1954 if (mp->format == FORMAT_RAW)
1956 average.cores.counter[i] += c->counter[i];
1959 /* sum per-pkg values only for 1st core in pkg */
1960 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1963 if (DO_BIC(BIC_Totl_c0))
1964 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1965 if (DO_BIC(BIC_Any_c0))
1966 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1967 if (DO_BIC(BIC_GFX_c0))
1968 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1969 if (DO_BIC(BIC_CPUGFX))
1970 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1972 average.packages.pc2 += p->pc2;
1973 if (DO_BIC(BIC_Pkgpc3))
1974 average.packages.pc3 += p->pc3;
1975 if (DO_BIC(BIC_Pkgpc6))
1976 average.packages.pc6 += p->pc6;
1977 if (DO_BIC(BIC_Pkgpc7))
1978 average.packages.pc7 += p->pc7;
1979 average.packages.pc8 += p->pc8;
1980 average.packages.pc9 += p->pc9;
1981 average.packages.pc10 += p->pc10;
1983 average.packages.cpu_lpi = p->cpu_lpi;
1984 average.packages.sys_lpi = p->sys_lpi;
1986 average.packages.energy_pkg += p->energy_pkg;
1987 average.packages.energy_dram += p->energy_dram;
1988 average.packages.energy_cores += p->energy_cores;
1989 average.packages.energy_gfx += p->energy_gfx;
1991 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1992 average.packages.uncore_mhz = p->uncore_mhz;
1993 average.packages.gfx_mhz = p->gfx_mhz;
1994 average.packages.gfx_act_mhz = p->gfx_act_mhz;
1996 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1998 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1999 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
2001 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2002 if (mp->format == FORMAT_RAW)
2004 average.packages.counter[i] += p->counter[i];
2010 * sum the counters for all cpus in the system
2011 * compute the weighted average
2013 void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2016 struct msr_counter *mp;
2018 clear_counters(&average.threads, &average.cores, &average.packages);
2020 for_all_cpus(sum_counters, t, c, p);
2022 /* Use the global time delta for the average. */
2023 average.threads.tv_delta = tv_delta;
2025 average.threads.tsc /= topo.num_cpus;
2026 average.threads.aperf /= topo.num_cpus;
2027 average.threads.mperf /= topo.num_cpus;
2028 average.threads.instr_count /= topo.num_cpus;
2029 average.threads.c1 /= topo.num_cpus;
2031 if (average.threads.irq_count > 9999999)
2032 sums_need_wide_columns = 1;
2034 average.cores.c3 /= topo.num_cores;
2035 average.cores.c6 /= topo.num_cores;
2036 average.cores.c7 /= topo.num_cores;
2037 average.cores.mc6_us /= topo.num_cores;
2039 if (DO_BIC(BIC_Totl_c0))
2040 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
2041 if (DO_BIC(BIC_Any_c0))
2042 average.packages.pkg_any_core_c0 /= topo.num_packages;
2043 if (DO_BIC(BIC_GFX_c0))
2044 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
2045 if (DO_BIC(BIC_CPUGFX))
2046 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
2048 average.packages.pc2 /= topo.num_packages;
2049 if (DO_BIC(BIC_Pkgpc3))
2050 average.packages.pc3 /= topo.num_packages;
2051 if (DO_BIC(BIC_Pkgpc6))
2052 average.packages.pc6 /= topo.num_packages;
2053 if (DO_BIC(BIC_Pkgpc7))
2054 average.packages.pc7 /= topo.num_packages;
2056 average.packages.pc8 /= topo.num_packages;
2057 average.packages.pc9 /= topo.num_packages;
2058 average.packages.pc10 /= topo.num_packages;
2060 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2061 if (mp->format == FORMAT_RAW)
2063 if (mp->type == COUNTER_ITEMS) {
2064 if (average.threads.counter[i] > 9999999)
2065 sums_need_wide_columns = 1;
2068 average.threads.counter[i] /= topo.num_cpus;
2070 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2071 if (mp->format == FORMAT_RAW)
2073 if (mp->type == COUNTER_ITEMS) {
2074 if (average.cores.counter[i] > 9999999)
2075 sums_need_wide_columns = 1;
2077 average.cores.counter[i] /= topo.num_cores;
2079 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2080 if (mp->format == FORMAT_RAW)
2082 if (mp->type == COUNTER_ITEMS) {
2083 if (average.packages.counter[i] > 9999999)
2084 sums_need_wide_columns = 1;
2086 average.packages.counter[i] /= topo.num_packages;
2090 static unsigned long long rdtsc(void)
2092 unsigned int low, high;
2094 asm volatile ("rdtsc":"=a" (low), "=d"(high));
2096 return low | ((unsigned long long)high) << 32;
2100 * Open a file, and exit on failure
2102 FILE *fopen_or_die(const char *path, const char *mode)
2104 FILE *filep = fopen(path, mode);
2107 err(1, "%s: open failed", path);
2112 * snapshot_sysfs_counter()
2114 * return snapshot of given counter
2116 unsigned long long snapshot_sysfs_counter(char *path)
2120 unsigned long long counter;
2122 fp = fopen_or_die(path, "r");
2124 retval = fscanf(fp, "%lld", &counter);
2126 err(1, "snapshot_sysfs_counter(%s)", path);
2133 int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
2135 if (mp->msr_num != 0) {
2136 if (get_msr(cpu, mp->msr_num, counterp))
2139 char path[128 + PATH_BYTES];
2141 if (mp->flags & SYSFS_PERCPU) {
2142 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path);
2144 *counterp = snapshot_sysfs_counter(path);
2146 *counterp = snapshot_sysfs_counter(mp->path);
2153 unsigned long long get_uncore_mhz(int package, int die)
2157 sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/current_freq_khz", package,
2160 return (snapshot_sysfs_counter(path) / 1000);
2163 int get_epb(int cpu)
2165 char path[128 + PATH_BYTES];
2166 unsigned long long msr;
2170 sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu);
2172 fp = fopen(path, "r");
2176 ret = fscanf(fp, "%d", &epb);
2178 err(1, "%s(%s)", __func__, path);
2185 get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr);
2190 void get_apic_id(struct thread_data *t)
2192 unsigned int eax, ebx, ecx, edx;
2194 if (DO_BIC(BIC_APIC)) {
2195 eax = ebx = ecx = edx = 0;
2196 __cpuid(1, eax, ebx, ecx, edx);
2198 t->apic_id = (ebx >> 24) & 0xff;
2201 if (!DO_BIC(BIC_X2APIC))
2204 if (authentic_amd || hygon_genuine) {
2205 unsigned int topology_extensions;
2207 if (max_extended_level < 0x8000001e)
2210 eax = ebx = ecx = edx = 0;
2211 __cpuid(0x80000001, eax, ebx, ecx, edx);
2212 topology_extensions = ecx & (1 << 22);
2214 if (topology_extensions == 0)
2217 eax = ebx = ecx = edx = 0;
2218 __cpuid(0x8000001e, eax, ebx, ecx, edx);
2227 if (max_level < 0xb)
2231 __cpuid(0xb, eax, ebx, ecx, edx);
2234 if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
2235 fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
2238 int get_core_throt_cnt(int cpu, unsigned long long *cnt)
2240 char path[128 + PATH_BYTES];
2241 unsigned long long tmp;
2245 sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu);
2246 fp = fopen(path, "r");
2249 ret = fscanf(fp, "%lld", &tmp);
2261 * acquire and record local counters for that cpu
2263 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2265 int cpu = t->cpu_id;
2266 unsigned long long msr;
2267 int aperf_mperf_retry_count = 0;
2268 struct msr_counter *mp;
2271 if (cpu_migrate(cpu)) {
2272 fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu);
2276 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
2278 if (first_counter_read)
2281 t->tsc = rdtsc(); /* we are running on local CPU of interest */
2283 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
2284 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
2287 * The TSC, APERF and MPERF must be read together for
2288 * APERF/MPERF and MPERF/TSC to give accurate results.
2290 * Unfortunately, APERF and MPERF are read by
2291 * individual system call, so delays may occur
2292 * between them. If the time to read them
2293 * varies by a large amount, we re-read them.
2297 * This initial dummy APERF read has been seen to
2298 * reduce jitter in the subsequent reads.
2301 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2304 t->tsc = rdtsc(); /* re-read close to APERF */
2306 tsc_before = t->tsc;
2308 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2311 tsc_between = rdtsc();
2313 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
2316 tsc_after = rdtsc();
2318 aperf_time = tsc_between - tsc_before;
2319 mperf_time = tsc_after - tsc_between;
2322 * If the system call latency to read APERF and MPERF
2323 * differ by more than 2x, then try again.
2325 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
2326 aperf_mperf_retry_count++;
2327 if (aperf_mperf_retry_count < 5)
2330 warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time);
2332 aperf_mperf_retry_count = 0;
2334 t->aperf = t->aperf * aperf_mperf_multiplier;
2335 t->mperf = t->mperf * aperf_mperf_multiplier;
2338 if (DO_BIC(BIC_IPC))
2339 if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long))
2342 if (DO_BIC(BIC_IRQ))
2343 t->irq_count = irqs_per_cpu[cpu];
2344 if (DO_BIC(BIC_SMI)) {
2345 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
2347 t->smi_count = msr & 0xFFFFFFFF;
2349 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
2350 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
2354 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2355 if (get_mp(cpu, mp, &t->counter[i]))
2359 /* collect core counters only for 1st thread in core */
2360 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2363 if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
2364 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
2368 if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
2369 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
2371 } else if (do_knl_cstates && soft_c1_residency_display(BIC_CPU_c6)) {
2372 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
2376 if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) {
2377 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
2379 else if (t->is_atom) {
2381 * For Atom CPUs that has core cstate deeper than c6,
2382 * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper.
2383 * Minus CC7 (and deeper cstates) residency to get
2384 * accturate cc6 residency.
2390 if (DO_BIC(BIC_Mod_c6))
2391 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
2394 if (DO_BIC(BIC_CoreTmp)) {
2395 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2397 c->core_temp_c = tj_max - ((msr >> 16) & 0x7F);
2400 if (DO_BIC(BIC_CORE_THROT_CNT))
2401 get_core_throt_cnt(cpu, &c->core_throt_cnt);
2403 if (do_rapl & RAPL_AMD_F17H) {
2404 if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
2406 c->core_energy = msr & 0xFFFFFFFF;
2409 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2410 if (get_mp(cpu, mp, &c->counter[i]))
2414 /* collect package counters only for 1st core in package */
2415 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2418 if (DO_BIC(BIC_Totl_c0)) {
2419 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
2422 if (DO_BIC(BIC_Any_c0)) {
2423 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
2426 if (DO_BIC(BIC_GFX_c0)) {
2427 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
2430 if (DO_BIC(BIC_CPUGFX)) {
2431 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
2434 if (DO_BIC(BIC_Pkgpc3))
2435 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
2437 if (DO_BIC(BIC_Pkgpc6)) {
2438 if (do_slm_cstates) {
2439 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
2442 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
2447 if (DO_BIC(BIC_Pkgpc2))
2448 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
2450 if (DO_BIC(BIC_Pkgpc7))
2451 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
2453 if (DO_BIC(BIC_Pkgpc8))
2454 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
2456 if (DO_BIC(BIC_Pkgpc9))
2457 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
2459 if (DO_BIC(BIC_Pkgpc10))
2460 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
2463 if (DO_BIC(BIC_CPU_LPI))
2464 p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
2465 if (DO_BIC(BIC_SYS_LPI))
2466 p->sys_lpi = cpuidle_cur_sys_lpi_us;
2468 if (do_rapl & RAPL_PKG) {
2469 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr))
2471 p->energy_pkg = msr;
2473 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
2474 if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr))
2476 p->energy_cores = msr;
2478 if (do_rapl & RAPL_DRAM) {
2479 if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
2481 p->energy_dram = msr;
2483 if (do_rapl & RAPL_GFX) {
2484 if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr))
2486 p->energy_gfx = msr;
2488 if (do_rapl & RAPL_PKG_PERF_STATUS) {
2489 if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr))
2491 p->rapl_pkg_perf_status = msr;
2493 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
2494 if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr))
2496 p->rapl_dram_perf_status = msr;
2498 if (do_rapl & RAPL_AMD_F17H) {
2499 if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr))
2501 p->energy_pkg = msr;
2503 if (DO_BIC(BIC_PkgTmp)) {
2504 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2506 p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F);
2509 if (DO_BIC(BIC_GFX_rc6))
2510 p->gfx_rc6_ms = gfx_cur_rc6_ms;
2512 /* n.b. assume die0 uncore frequency applies to whole package */
2513 if (DO_BIC(BIC_UNCORE_MHZ))
2514 p->uncore_mhz = get_uncore_mhz(p->package_id, 0);
2516 if (DO_BIC(BIC_GFXMHz))
2517 p->gfx_mhz = gfx_cur_mhz;
2519 if (DO_BIC(BIC_GFXACTMHz))
2520 p->gfx_act_mhz = gfx_act_mhz;
2522 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2523 if (get_mp(cpu, mp, &p->counter[i]))
2527 gettimeofday(&t->tv_end, (struct timezone *)NULL);
2533 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2534 * If you change the values, note they are used both in comparisons
2535 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2538 #define PCLUKN 0 /* Unknown */
2539 #define PCLRSV 1 /* Reserved */
2540 #define PCL__0 2 /* PC0 */
2541 #define PCL__1 3 /* PC1 */
2542 #define PCL__2 4 /* PC2 */
2543 #define PCL__3 5 /* PC3 */
2544 #define PCL__4 6 /* PC4 */
2545 #define PCL__6 7 /* PC6 */
2546 #define PCL_6N 8 /* PC6 No Retention */
2547 #define PCL_6R 9 /* PC6 Retention */
2548 #define PCL__7 10 /* PC7 */
2549 #define PCL_7S 11 /* PC7 Shrink */
2550 #define PCL__8 12 /* PC8 */
2551 #define PCL__9 13 /* PC9 */
2552 #define PCL_10 14 /* PC10 */
2553 #define PCLUNL 15 /* Unlimited */
2555 int pkg_cstate_limit = PCLUKN;
2556 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2557 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"
2560 int nhm_pkg_cstate_limits[16] =
2561 { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2565 int snb_pkg_cstate_limits[16] =
2566 { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2570 int hsw_pkg_cstate_limits[16] =
2571 { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2575 int slv_pkg_cstate_limits[16] =
2576 { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2580 int amt_pkg_cstate_limits[16] =
2581 { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2585 int phi_pkg_cstate_limits[16] =
2586 { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2590 int glm_pkg_cstate_limits[16] =
2591 { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2595 int skx_pkg_cstate_limits[16] =
2596 { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2600 int icx_pkg_cstate_limits[16] =
2601 { PCL__0, PCL__2, PCL__6, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
2605 static void calculate_tsc_tweak()
2607 tsc_tweak = base_hz / tsc_hz;
2610 void prewake_cstate_probe(unsigned int family, unsigned int model);
2612 static void dump_nhm_platform_info(void)
2614 unsigned long long msr;
2617 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2619 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
2621 ratio = (msr >> 40) & 0xFF;
2622 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk);
2624 ratio = (msr >> 8) & 0xFF;
2625 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
2627 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2628 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2629 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
2631 /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */
2632 if (dis_cstate_prewake)
2633 fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN");
2638 static void dump_hsw_turbo_ratio_limits(void)
2640 unsigned long long msr;
2643 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2645 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2647 ratio = (msr >> 8) & 0xFF;
2649 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk);
2651 ratio = (msr >> 0) & 0xFF;
2653 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk);
2657 static void dump_ivt_turbo_ratio_limits(void)
2659 unsigned long long msr;
2662 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
2664 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
2666 ratio = (msr >> 56) & 0xFF;
2668 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk);
2670 ratio = (msr >> 48) & 0xFF;
2672 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk);
2674 ratio = (msr >> 40) & 0xFF;
2676 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk);
2678 ratio = (msr >> 32) & 0xFF;
2680 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk);
2682 ratio = (msr >> 24) & 0xFF;
2684 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk);
2686 ratio = (msr >> 16) & 0xFF;
2688 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk);
2690 ratio = (msr >> 8) & 0xFF;
2692 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk);
2694 ratio = (msr >> 0) & 0xFF;
2696 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk);
2700 int has_turbo_ratio_group_limits(int family, int model)
2710 case INTEL_FAM6_ATOM_GOLDMONT:
2711 case INTEL_FAM6_SKYLAKE_X:
2712 case INTEL_FAM6_ICELAKE_X:
2713 case INTEL_FAM6_SAPPHIRERAPIDS_X:
2714 case INTEL_FAM6_ATOM_GOLDMONT_D:
2715 case INTEL_FAM6_ATOM_TREMONT_D:
2722 static void dump_turbo_ratio_limits(int trl_msr_offset, int family, int model)
2724 unsigned long long msr, core_counts;
2727 get_msr(base_cpu, trl_msr_offset, &msr);
2728 fprintf(outf, "cpu%d: MSR_%sTURBO_RATIO_LIMIT: 0x%08llx\n",
2729 base_cpu, trl_msr_offset == MSR_SECONDARY_TURBO_RATIO_LIMIT ? "SECONDARY_" : "", msr);
2731 if (has_turbo_ratio_group_limits(family, model)) {
2732 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
2733 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
2735 core_counts = 0x0807060504030201;
2738 for (shift = 56; shift >= 0; shift -= 8) {
2739 unsigned int ratio, group_size;
2741 ratio = (msr >> shift) & 0xFF;
2742 group_size = (core_counts >> shift) & 0xFF;
2744 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2745 ratio, bclk, ratio * bclk, group_size);
2751 static void dump_atom_turbo_ratio_limits(void)
2753 unsigned long long msr;
2756 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2757 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2759 ratio = (msr >> 0) & 0x3F;
2761 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk);
2763 ratio = (msr >> 8) & 0x3F;
2765 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk);
2767 ratio = (msr >> 16) & 0x3F;
2769 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
2771 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2772 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2774 ratio = (msr >> 24) & 0x3F;
2776 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk);
2778 ratio = (msr >> 16) & 0x3F;
2780 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk);
2782 ratio = (msr >> 8) & 0x3F;
2784 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk);
2786 ratio = (msr >> 0) & 0x3F;
2788 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk);
2791 static void dump_knl_turbo_ratio_limits(void)
2793 const unsigned int buckets_no = 7;
2795 unsigned long long msr;
2796 int delta_cores, delta_ratio;
2798 unsigned int cores[buckets_no];
2799 unsigned int ratio[buckets_no];
2801 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2803 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2806 * Turbo encoding in KNL is as follows:
2808 * [7:1] -- Base value of number of active cores of bucket 1.
2809 * [15:8] -- Base value of freq ratio of bucket 1.
2810 * [20:16] -- +ve delta of number of active cores of bucket 2.
2811 * i.e. active cores of bucket 2 =
2812 * active cores of bucket 1 + delta
2813 * [23:21] -- Negative delta of freq ratio of bucket 2.
2814 * i.e. freq ratio of bucket 2 =
2815 * freq ratio of bucket 1 - delta
2816 * [28:24]-- +ve delta of number of active cores of bucket 3.
2817 * [31:29]-- -ve delta of freq ratio of bucket 3.
2818 * [36:32]-- +ve delta of number of active cores of bucket 4.
2819 * [39:37]-- -ve delta of freq ratio of bucket 4.
2820 * [44:40]-- +ve delta of number of active cores of bucket 5.
2821 * [47:45]-- -ve delta of freq ratio of bucket 5.
2822 * [52:48]-- +ve delta of number of active cores of bucket 6.
2823 * [55:53]-- -ve delta of freq ratio of bucket 6.
2824 * [60:56]-- +ve delta of number of active cores of bucket 7.
2825 * [63:61]-- -ve delta of freq ratio of bucket 7.
2829 cores[b_nr] = (msr & 0xFF) >> 1;
2830 ratio[b_nr] = (msr >> 8) & 0xFF;
2832 for (i = 16; i < 64; i += 8) {
2833 delta_cores = (msr >> i) & 0x1F;
2834 delta_ratio = (msr >> (i + 5)) & 0x7;
2836 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2837 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2841 for (i = buckets_no - 1; i >= 0; i--)
2842 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2844 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2845 ratio[i], bclk, ratio[i] * bclk, cores[i]);
2848 static void dump_nhm_cst_cfg(void)
2850 unsigned long long msr;
2852 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2854 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2856 fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2857 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2858 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2859 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2860 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2861 (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]);
2863 #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16)
2864 if (has_automatic_cstate_conversion) {
2865 fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2868 fprintf(outf, ")\n");
2873 static void dump_config_tdp(void)
2875 unsigned long long msr;
2877 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2878 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2879 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
2881 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2882 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
2884 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2885 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2886 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2887 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
2889 fprintf(outf, ")\n");
2891 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2892 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
2894 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2895 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2896 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2897 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
2899 fprintf(outf, ")\n");
2901 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2902 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
2904 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2905 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2906 fprintf(outf, ")\n");
2908 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2909 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2910 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2911 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2912 fprintf(outf, ")\n");
2915 unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2917 void print_irtl(void)
2919 unsigned long long msr;
2921 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2922 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2923 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2924 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2926 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2927 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2928 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2929 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2931 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2932 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2933 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2934 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2939 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2940 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2941 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2942 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2944 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2945 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2946 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2947 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2949 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2950 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2951 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2952 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2956 void free_fd_percpu(void)
2960 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
2961 if (fd_percpu[i] != 0)
2962 close(fd_percpu[i]);
2968 void free_all_buffers(void)
2972 CPU_FREE(cpu_present_set);
2973 cpu_present_set = NULL;
2974 cpu_present_setsize = 0;
2976 CPU_FREE(cpu_affinity_set);
2977 cpu_affinity_set = NULL;
2978 cpu_affinity_setsize = 0;
2986 package_even = NULL;
2996 free(output_buffer);
2997 output_buffer = NULL;
3002 free(irq_column_2_cpu);
3005 for (i = 0; i <= topo.max_cpu_num; ++i) {
3006 if (cpus[i].put_ids)
3007 CPU_FREE(cpus[i].put_ids);
3013 * Parse a file containing a single int.
3014 * Return 0 if file can not be opened
3015 * Exit if file can be opened, but can not be parsed
3017 int parse_int_file(const char *fmt, ...)
3020 char path[PATH_MAX];
3024 va_start(args, fmt);
3025 vsnprintf(path, sizeof(path), fmt, args);
3027 filep = fopen(path, "r");
3030 if (fscanf(filep, "%d", &value) != 1)
3031 err(1, "%s: failed to parse number from file", path);
3037 * cpu_is_first_core_in_package(cpu)
3038 * return 1 if given CPU is 1st core in package
3040 int cpu_is_first_core_in_package(int cpu)
3042 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
3045 int get_physical_package_id(int cpu)
3047 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
3050 int get_die_id(int cpu)
3052 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
3055 int get_core_id(int cpu)
3057 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
3060 void set_node_data(void)
3062 int pkg, node, lnode, cpu, cpux;
3065 /* initialize logical_node_id */
3066 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
3067 cpus[cpu].logical_node_id = -1;
3070 for (pkg = 0; pkg < topo.num_packages; pkg++) {
3072 for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
3073 if (cpus[cpu].physical_package_id != pkg)
3075 /* find a cpu with an unset logical_node_id */
3076 if (cpus[cpu].logical_node_id != -1)
3078 cpus[cpu].logical_node_id = lnode;
3079 node = cpus[cpu].physical_node_id;
3082 * find all matching cpus on this pkg and set
3083 * the logical_node_id
3085 for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
3086 if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) {
3087 cpus[cpux].logical_node_id = lnode;
3092 if (lnode > topo.nodes_per_pkg)
3093 topo.nodes_per_pkg = lnode;
3095 if (cpu_count >= topo.max_cpu_num)
3100 int get_physical_node_id(struct cpu_topology *thiscpu)
3105 int cpu = thiscpu->logical_cpu_id;
3107 for (i = 0; i <= topo.max_cpu_num; i++) {
3108 sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i);
3109 filep = fopen(path, "r");
3118 int get_thread_siblings(struct cpu_topology *thiscpu)
3120 char path[80], character;
3123 int so, shift, sib_core;
3124 int cpu = thiscpu->logical_cpu_id;
3125 int offset = topo.max_cpu_num + 1;
3129 thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
3130 if (thiscpu->thread_id < 0)
3131 thiscpu->thread_id = thread_id++;
3132 if (!thiscpu->put_ids)
3135 size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3136 CPU_ZERO_S(size, thiscpu->put_ids);
3138 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
3139 filep = fopen(path, "r");
3142 warnx("%s: open failed", path);
3146 offset -= BITMASK_SIZE;
3147 if (fscanf(filep, "%lx%c", &map, &character) != 2)
3148 err(1, "%s: failed to parse file", path);
3149 for (shift = 0; shift < BITMASK_SIZE; shift++) {
3150 if ((map >> shift) & 0x1) {
3151 so = shift + offset;
3152 sib_core = get_core_id(so);
3153 if (sib_core == thiscpu->physical_core_id) {
3154 CPU_SET_S(so, size, thiscpu->put_ids);
3155 if ((so != cpu) && (cpus[so].thread_id < 0))
3156 cpus[so].thread_id = thread_id++;
3160 } while (character == ',');
3163 return CPU_COUNT_S(size, thiscpu->put_ids);
3167 * run func(thread, core, package) in topology order
3168 * skip non-present cpus
3171 int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *,
3172 struct pkg_data *, struct thread_data *, struct core_data *,
3173 struct pkg_data *), struct thread_data *thread_base,
3174 struct core_data *core_base, struct pkg_data *pkg_base,
3175 struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2)
3177 int retval, pkg_no, node_no, core_no, thread_no;
3179 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
3180 for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
3181 for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
3182 for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
3183 struct thread_data *t, *t2;
3184 struct core_data *c, *c2;
3185 struct pkg_data *p, *p2;
3187 t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
3189 if (cpu_is_not_present(t->cpu_id))
3192 t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no);
3194 c = GET_CORE(core_base, core_no, node_no, pkg_no);
3195 c2 = GET_CORE(core_base2, core_no, node_no, pkg_no);
3197 p = GET_PKG(pkg_base, pkg_no);
3198 p2 = GET_PKG(pkg_base2, pkg_no);
3200 retval = func(t, c, p, t2, c2, p2);
3211 * run func(cpu) on every cpu in /proc/stat
3212 * return max_cpu number
3214 int for_all_proc_cpus(int (func) (int))
3220 fp = fopen_or_die(proc_stat, "r");
3222 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
3224 err(1, "%s: failed to parse format", proc_stat);
3227 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
3231 retval = func(cpu_num);
3241 void re_initialize(void)
3244 setup_all_buffers();
3245 fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
3248 void set_max_cpu_num(void)
3252 unsigned long dummy;
3255 base_cpu = sched_getcpu();
3257 err(1, "cannot find calling cpu ID");
3258 sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu);
3260 filep = fopen_or_die(pathname, "r");
3261 topo.max_cpu_num = 0;
3262 while (fscanf(filep, "%lx,", &dummy) == 1)
3263 topo.max_cpu_num += BITMASK_SIZE;
3265 topo.max_cpu_num--; /* 0 based */
3270 * remember the last one seen, it will be the max
3272 int count_cpus(int cpu)
3280 int mark_cpu_present(int cpu)
3282 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
3286 int init_thread_id(int cpu)
3288 cpus[cpu].thread_id = -1;
3293 * snapshot_proc_interrupts()
3295 * read and record summary of /proc/interrupts
3297 * return 1 if config change requires a restart, else return 0
3299 int snapshot_proc_interrupts(void)
3305 fp = fopen_or_die("/proc/interrupts", "r");
3309 /* read 1st line of /proc/interrupts to get cpu* name for each column */
3310 for (column = 0; column < topo.num_cpus; ++column) {
3313 retval = fscanf(fp, " CPU%d", &cpu_number);
3317 if (cpu_number > topo.max_cpu_num) {
3318 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
3322 irq_column_2_cpu[column] = cpu_number;
3323 irqs_per_cpu[cpu_number] = 0;
3326 /* read /proc/interrupt count lines and sum up irqs per cpu */
3331 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
3335 /* read the count per cpu */
3336 for (column = 0; column < topo.num_cpus; ++column) {
3338 int cpu_number, irq_count;
3340 retval = fscanf(fp, " %d", &irq_count);
3344 cpu_number = irq_column_2_cpu[column];
3345 irqs_per_cpu[cpu_number] += irq_count;
3349 while (getc(fp) != '\n') ; /* flush interrupt description */
3356 * snapshot_gfx_rc6_ms()
3358 * record snapshot of
3359 * /sys/class/drm/card0/power/rc6_residency_ms
3361 * return 1 if config change requires a restart, else return 0
3363 int snapshot_gfx_rc6_ms(void)
3368 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
3370 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
3380 * snapshot_gfx_mhz()
3382 * fall back to /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
3383 * when /sys/class/drm/card0/gt_cur_freq_mhz is not available.
3385 * return 1 if config change requires a restart, else return 0
3387 int snapshot_gfx_mhz(void)
3393 fp = fopen("/sys/class/drm/card0/gt_cur_freq_mhz", "r");
3395 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
3401 retval = fscanf(fp, "%d", &gfx_cur_mhz);
3409 * snapshot_gfx_cur_mhz()
3411 * fall back to /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz
3412 * when /sys/class/drm/card0/gt_act_freq_mhz is not available.
3414 * return 1 if config change requires a restart, else return 0
3416 int snapshot_gfx_act_mhz(void)
3422 fp = fopen("/sys/class/drm/card0/gt_act_freq_mhz", "r");
3424 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r");
3430 retval = fscanf(fp, "%d", &gfx_act_mhz);
3432 err(1, "GFX ACT MHz");
3438 * snapshot_cpu_lpi()
3440 * record snapshot of
3441 * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
3443 int snapshot_cpu_lpi_us(void)
3448 fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
3450 retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
3452 fprintf(stderr, "Disabling Low Power Idle CPU output\n");
3453 BIC_NOT_PRESENT(BIC_CPU_LPI);
3464 * snapshot_sys_lpi()
3466 * record snapshot of sys_lpi_file
3468 int snapshot_sys_lpi_us(void)
3473 fp = fopen_or_die(sys_lpi_file, "r");
3475 retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
3477 fprintf(stderr, "Disabling Low Power Idle System output\n");
3478 BIC_NOT_PRESENT(BIC_SYS_LPI);
3488 * snapshot /proc and /sys files
3490 * return 1 if configuration restart needed, else return 0
3492 int snapshot_proc_sysfs_files(void)
3494 if (DO_BIC(BIC_IRQ))
3495 if (snapshot_proc_interrupts())
3498 if (DO_BIC(BIC_GFX_rc6))
3499 snapshot_gfx_rc6_ms();
3501 if (DO_BIC(BIC_GFXMHz))
3504 if (DO_BIC(BIC_GFXACTMHz))
3505 snapshot_gfx_act_mhz();
3507 if (DO_BIC(BIC_CPU_LPI))
3508 snapshot_cpu_lpi_us();
3510 if (DO_BIC(BIC_SYS_LPI))
3511 snapshot_sys_lpi_us();
3518 static void signal_handler(int signal)
3524 fprintf(stderr, " SIGINT\n");
3528 fprintf(stderr, "SIGUSR1\n");
3533 void setup_signal_handler(void)
3535 struct sigaction sa;
3537 memset(&sa, 0, sizeof(sa));
3539 sa.sa_handler = &signal_handler;
3541 if (sigaction(SIGINT, &sa, NULL) < 0)
3542 err(1, "sigaction SIGINT");
3543 if (sigaction(SIGUSR1, &sa, NULL) < 0)
3544 err(1, "sigaction SIGUSR1");
3549 struct timeval tout;
3550 struct timespec rest;
3555 FD_SET(0, &readfds);
3558 nanosleep(&interval_ts, NULL);
3563 retval = select(1, &readfds, NULL, NULL, &tout);
3566 switch (getc(stdin)) {
3572 * 'stdin' is a pipe closed on the other end. There
3573 * won't be any further input.
3576 /* Sleep the rest of the time */
3577 rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3578 rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3579 nanosleep(&rest, NULL);
3584 int get_msr_sum(int cpu, off_t offset, unsigned long long *msr)
3587 unsigned long long msr_cur, msr_last;
3589 if (!per_cpu_msr_sum)
3592 idx = offset_to_idx(offset);
3595 /* get_msr_sum() = sum + (get_msr() - last) */
3596 ret = get_msr(cpu, offset, &msr_cur);
3599 msr_last = per_cpu_msr_sum[cpu].entries[idx].last;
3600 DELTA_WRAP32(msr_cur, msr_last);
3601 *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum;
3608 /* Timer callback, update the sum of MSRs periodically. */
3609 static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3612 int cpu = t->cpu_id;
3617 for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
3618 unsigned long long msr_cur, msr_last;
3623 offset = idx_to_offset(i);
3626 ret = get_msr(cpu, offset, &msr_cur);
3628 fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset);
3632 msr_last = per_cpu_msr_sum[cpu].entries[i].last;
3633 per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff;
3635 DELTA_WRAP32(msr_cur, msr_last);
3636 per_cpu_msr_sum[cpu].entries[i].sum += msr_last;
3641 static void msr_record_handler(union sigval v)
3645 for_all_cpus(update_msr_sum, EVEN_COUNTERS);
3648 void msr_sum_record(void)
3650 struct itimerspec its;
3651 struct sigevent sev;
3653 per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array));
3654 if (!per_cpu_msr_sum) {
3655 fprintf(outf, "Can not allocate memory for long time MSR.\n");
3659 * Signal handler might be restricted, so use thread notifier instead.
3661 memset(&sev, 0, sizeof(struct sigevent));
3662 sev.sigev_notify = SIGEV_THREAD;
3663 sev.sigev_notify_function = msr_record_handler;
3665 sev.sigev_value.sival_ptr = &timerid;
3666 if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
3667 fprintf(outf, "Can not create timer.\n");
3671 its.it_value.tv_sec = 0;
3672 its.it_value.tv_nsec = 1;
3674 * A wraparound time has been calculated early.
3675 * Some sources state that the peak power for a
3676 * microprocessor is usually 1.5 times the TDP rating,
3677 * use 2 * TDP for safety.
3679 its.it_interval.tv_sec = rapl_joule_counter_range / 2;
3680 its.it_interval.tv_nsec = 0;
3682 if (timer_settime(timerid, 0, &its, NULL) == -1) {
3683 fprintf(outf, "Can not set timer.\n");
3689 timer_delete(timerid);
3691 free(per_cpu_msr_sum);
3695 * set_my_sched_priority(pri)
3698 int set_my_sched_priority(int priority)
3701 int original_priority;
3704 original_priority = getpriority(PRIO_PROCESS, 0);
3705 if (errno && (original_priority == -1))
3706 err(errno, "getpriority");
3708 retval = setpriority(PRIO_PROCESS, 0, priority);
3710 errx(retval, "capget(CAP_SYS_NICE) failed,try \"# setcap cap_sys_nice=ep %s\"", progname);
3713 retval = getpriority(PRIO_PROCESS, 0);
3714 if (retval != priority)
3715 err(retval, "getpriority(%d) != setpriority(%d)", retval, priority);
3717 return original_priority;
3720 void turbostat_loop()
3724 unsigned int done_iters = 0;
3726 setup_signal_handler();
3729 * elevate own priority for interval mode
3731 set_my_sched_priority(-20);
3736 snapshot_proc_sysfs_files();
3737 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3738 first_counter_read = 0;
3741 } else if (retval == -1) {
3742 if (restarted > 10) {
3750 gettimeofday(&tv_even, (struct timezone *)NULL);
3753 if (for_all_proc_cpus(cpu_is_not_present)) {
3758 if (snapshot_proc_sysfs_files())
3760 retval = for_all_cpus(get_counters, ODD_COUNTERS);
3763 } else if (retval == -1) {
3767 gettimeofday(&tv_odd, (struct timezone *)NULL);
3768 timersub(&tv_odd, &tv_even, &tv_delta);
3769 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3773 compute_average(EVEN_COUNTERS);
3774 format_all_counters(EVEN_COUNTERS);
3775 flush_output_stdout();
3778 if (num_iterations && ++done_iters >= num_iterations)
3781 if (snapshot_proc_sysfs_files())
3783 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3786 } else if (retval == -1) {
3790 gettimeofday(&tv_even, (struct timezone *)NULL);
3791 timersub(&tv_even, &tv_odd, &tv_delta);
3792 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3796 compute_average(ODD_COUNTERS);
3797 format_all_counters(ODD_COUNTERS);
3798 flush_output_stdout();
3801 if (num_iterations && ++done_iters >= num_iterations)
3806 void check_dev_msr()
3811 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3812 if (stat(pathname, &sb))
3813 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3814 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3818 * check for CAP_SYS_RAWIO
3819 * return 0 on success
3822 int check_for_cap_sys_rawio(void)
3825 cap_flag_value_t cap_flag_value;
3827 caps = cap_get_proc();
3829 err(-6, "cap_get_proc\n");
3831 if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3832 err(-6, "cap_get\n");
3834 if (cap_flag_value != CAP_SET) {
3835 warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3839 if (cap_free(caps) == -1)
3840 err(-6, "cap_free\n");
3845 void check_permissions(void)
3850 /* check for CAP_SYS_RAWIO */
3851 do_exit += check_for_cap_sys_rawio();
3853 /* test file permissions */
3854 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
3855 if (euidaccess(pathname, R_OK)) {
3857 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
3860 /* if all else fails, thell them to be root */
3863 warnx("... or simply run as root");
3870 * NHM adds support for additional MSRs:
3872 * MSR_SMI_COUNT 0x00000034
3874 * MSR_PLATFORM_INFO 0x000000ce
3875 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
3877 * MSR_MISC_PWR_MGMT 0x000001aa
3879 * MSR_PKG_C3_RESIDENCY 0x000003f8
3880 * MSR_PKG_C6_RESIDENCY 0x000003f9
3881 * MSR_CORE_C3_RESIDENCY 0x000003fc
3882 * MSR_CORE_C6_RESIDENCY 0x000003fd
3885 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
3886 * sets has_misc_feature_control
3888 int probe_nhm_msrs(unsigned int family, unsigned int model)
3890 unsigned long long msr;
3891 unsigned int base_ratio;
3892 int *pkg_cstate_limits;
3900 bclk = discover_bclk(family, model);
3903 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3904 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
3905 pkg_cstate_limits = nhm_pkg_cstate_limits;
3907 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
3908 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
3909 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3910 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3911 pkg_cstate_limits = snb_pkg_cstate_limits;
3912 has_misc_feature_control = 1;
3914 case INTEL_FAM6_HASWELL: /* HSW */
3915 case INTEL_FAM6_HASWELL_G: /* HSW */
3916 case INTEL_FAM6_HASWELL_X: /* HSX */
3917 case INTEL_FAM6_HASWELL_L: /* HSW */
3918 case INTEL_FAM6_BROADWELL: /* BDW */
3919 case INTEL_FAM6_BROADWELL_G: /* BDW */
3920 case INTEL_FAM6_BROADWELL_X: /* BDX */
3921 case INTEL_FAM6_SKYLAKE_L: /* SKL */
3922 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
3923 pkg_cstate_limits = hsw_pkg_cstate_limits;
3924 has_misc_feature_control = 1;
3926 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3927 case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
3928 pkg_cstate_limits = skx_pkg_cstate_limits;
3929 has_misc_feature_control = 1;
3931 case INTEL_FAM6_ICELAKE_X: /* ICX */
3932 pkg_cstate_limits = icx_pkg_cstate_limits;
3933 has_misc_feature_control = 1;
3935 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
3936 no_MSR_MISC_PWR_MGMT = 1;
3938 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
3939 pkg_cstate_limits = slv_pkg_cstate_limits;
3941 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
3942 pkg_cstate_limits = amt_pkg_cstate_limits;
3943 no_MSR_MISC_PWR_MGMT = 1;
3945 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
3946 pkg_cstate_limits = phi_pkg_cstate_limits;
3948 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3949 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3950 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
3951 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
3952 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
3953 pkg_cstate_limits = glm_pkg_cstate_limits;
3958 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3959 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3961 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
3962 base_ratio = (msr >> 8) & 0xFF;
3964 base_hz = base_ratio * bclk * 1000000;
3970 * SLV client has support for unique MSRs:
3972 * MSR_CC6_DEMOTION_POLICY_CONFIG
3973 * MSR_MC6_DEMOTION_POLICY_CONFIG
3976 int has_slv_msrs(unsigned int family, unsigned int model)
3985 case INTEL_FAM6_ATOM_SILVERMONT:
3991 int is_dnv(unsigned int family, unsigned int model)
4001 case INTEL_FAM6_ATOM_GOLDMONT_D:
4007 int is_bdx(unsigned int family, unsigned int model)
4017 case INTEL_FAM6_BROADWELL_X:
4023 int is_skx(unsigned int family, unsigned int model)
4033 case INTEL_FAM6_SKYLAKE_X:
4039 int is_icx(unsigned int family, unsigned int model)
4049 case INTEL_FAM6_ICELAKE_X:
4055 int is_spr(unsigned int family, unsigned int model)
4065 case INTEL_FAM6_SAPPHIRERAPIDS_X:
4071 int is_ehl(unsigned int family, unsigned int model)
4080 case INTEL_FAM6_ATOM_TREMONT:
4086 int is_jvl(unsigned int family, unsigned int model)
4095 case INTEL_FAM6_ATOM_TREMONT_D:
4101 int has_turbo_ratio_limit(unsigned int family, unsigned int model)
4103 if (has_slv_msrs(family, model))
4110 /* Nehalem compatible, but do not include turbo-ratio limit support */
4111 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
4112 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
4119 int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
4121 if (has_slv_msrs(family, model))
4127 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
4136 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
4137 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
4144 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
4153 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
4160 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
4169 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
4176 int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
4185 case INTEL_FAM6_ATOM_GOLDMONT:
4186 case INTEL_FAM6_SKYLAKE_X:
4187 case INTEL_FAM6_ICELAKE_X:
4188 case INTEL_FAM6_SAPPHIRERAPIDS_X:
4195 int has_config_tdp(unsigned int family, unsigned int model)
4204 case INTEL_FAM6_IVYBRIDGE: /* IVB */
4205 case INTEL_FAM6_HASWELL: /* HSW */
4206 case INTEL_FAM6_HASWELL_X: /* HSX */
4207 case INTEL_FAM6_HASWELL_L: /* HSW */
4208 case INTEL_FAM6_HASWELL_G: /* HSW */
4209 case INTEL_FAM6_BROADWELL: /* BDW */
4210 case INTEL_FAM6_BROADWELL_G: /* BDW */
4211 case INTEL_FAM6_BROADWELL_X: /* BDX */
4212 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4213 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4214 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4215 case INTEL_FAM6_ICELAKE_X: /* ICX */
4216 case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
4217 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
4226 * 0: Tcc Offset not supported (Default)
4227 * 6: Bit 29:24 of MSR_PLATFORM_INFO
4228 * 4: Bit 27:24 of MSR_PLATFORM_INFO
4230 void check_tcc_offset(int model)
4232 unsigned long long msr;
4238 case INTEL_FAM6_SKYLAKE_L:
4239 case INTEL_FAM6_CANNONLAKE_L:
4240 if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) {
4241 msr = (msr >> 30) & 1;
4243 tcc_offset_bits = 6;
4251 static void remove_underbar(char *s)
4264 static void dump_turbo_ratio_info(unsigned int family, unsigned int model)
4269 if (has_hsw_turbo_ratio_limit(family, model))
4270 dump_hsw_turbo_ratio_limits();
4272 if (has_ivt_turbo_ratio_limit(family, model))
4273 dump_ivt_turbo_ratio_limits();
4275 if (has_turbo_ratio_limit(family, model)) {
4276 dump_turbo_ratio_limits(MSR_TURBO_RATIO_LIMIT, family, model);
4279 dump_turbo_ratio_limits(MSR_SECONDARY_TURBO_RATIO_LIMIT, family, model);
4282 if (has_atom_turbo_ratio_limit(family, model))
4283 dump_atom_turbo_ratio_limits();
4285 if (has_knl_turbo_ratio_limit(family, model))
4286 dump_knl_turbo_ratio_limits();
4288 if (has_config_tdp(family, model))
4292 static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
4294 if (!do_nhm_platform_info)
4297 dump_nhm_platform_info();
4298 dump_turbo_ratio_info(family, model);
4302 static int read_sysfs_int(char *path)
4307 input = fopen(path, "r");
4308 if (input == NULL) {
4310 fprintf(outf, "NSFOD %s\n", path);
4313 if (fscanf(input, "%d", &retval) != 1)
4314 err(1, "%s: failed to read int from file", path);
4320 static void dump_sysfs_file(char *path)
4323 char cpuidle_buf[64];
4325 input = fopen(path, "r");
4326 if (input == NULL) {
4328 fprintf(outf, "NSFOD %s\n", path);
4331 if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
4332 err(1, "%s: failed to read file", path);
4335 fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
4338 static void intel_uncore_frequency_probe(void)
4346 if (access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00", R_OK))
4349 /* Cluster level sysfs not supported yet. */
4350 if (!access("/sys/devices/system/cpu/intel_uncore_frequency/uncore00", R_OK))
4353 if (!access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00/current_freq_khz", R_OK))
4354 BIC_PRESENT(BIC_UNCORE_MHZ);
4359 for (i = 0; i < topo.num_packages; ++i) {
4360 for (j = 0; j < topo.num_die; ++j) {
4363 sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/min_freq_khz",
4365 k = read_sysfs_int(path);
4366 sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/max_freq_khz",
4368 l = read_sysfs_int(path);
4369 fprintf(outf, "Uncore Frequency pkg%d die%d: %d - %d MHz ", i, j, k / 1000, l / 1000);
4372 "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_min_freq_khz",
4374 k = read_sysfs_int(path);
4376 "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_max_freq_khz",
4378 l = read_sysfs_int(path);
4379 fprintf(outf, "(%d - %d MHz)\n", k / 1000, l / 1000);
4384 static void dump_sysfs_cstate_config(void)
4393 if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
4394 fprintf(outf, "cpuidle not loaded\n");
4398 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
4399 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
4400 dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
4402 for (state = 0; state < 10; ++state) {
4404 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
4405 input = fopen(path, "r");
4408 if (!fgets(name_buf, sizeof(name_buf), input))
4409 err(1, "%s: failed to read file", path);
4411 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4412 sp = strchr(name_buf, '-');
4414 sp = strchrnul(name_buf, '\n');
4418 remove_underbar(name_buf);
4420 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state);
4421 input = fopen(path, "r");
4424 if (!fgets(desc, sizeof(desc), input))
4425 err(1, "%s: failed to read file", path);
4427 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
4432 static void dump_sysfs_pstate_config(void)
4435 char driver_buf[64];
4436 char governor_buf[64];
4440 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu);
4441 input = fopen(path, "r");
4442 if (input == NULL) {
4443 fprintf(outf, "NSFOD %s\n", path);
4446 if (!fgets(driver_buf, sizeof(driver_buf), input))
4447 err(1, "%s: failed to read file", path);
4450 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu);
4451 input = fopen(path, "r");
4452 if (input == NULL) {
4453 fprintf(outf, "NSFOD %s\n", path);
4456 if (!fgets(governor_buf, sizeof(governor_buf), input))
4457 err(1, "%s: failed to read file", path);
4460 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
4461 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
4463 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
4464 input = fopen(path, "r");
4465 if (input != NULL) {
4466 if (fscanf(input, "%d", &turbo) != 1)
4467 err(1, "%s: failed to parse number from file", path);
4468 fprintf(outf, "cpufreq boost: %d\n", turbo);
4472 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
4473 input = fopen(path, "r");
4474 if (input != NULL) {
4475 if (fscanf(input, "%d", &turbo) != 1)
4476 err(1, "%s: failed to parse number from file", path);
4477 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
4484 * Decode the ENERGY_PERF_BIAS MSR
4486 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4499 /* EPB is per-package */
4500 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4503 if (cpu_migrate(cpu)) {
4504 fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu);
4513 case ENERGY_PERF_BIAS_PERFORMANCE:
4514 epb_string = "performance";
4516 case ENERGY_PERF_BIAS_NORMAL:
4517 epb_string = "balanced";
4519 case ENERGY_PERF_BIAS_POWERSAVE:
4520 epb_string = "powersave";
4523 epb_string = "custom";
4526 fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string);
4533 * Decode the MSR_HWP_CAPABILITIES
4535 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4537 unsigned long long msr;
4548 /* MSR_HWP_CAPABILITIES is per-package */
4549 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4552 if (cpu_migrate(cpu)) {
4553 fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu);
4557 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
4560 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-");
4562 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
4563 if ((msr & (1 << 0)) == 0)
4566 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
4569 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
4570 "(high %d guar %d eff %d low %d)\n",
4572 (unsigned int)HWP_HIGHEST_PERF(msr),
4573 (unsigned int)HWP_GUARANTEED_PERF(msr),
4574 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr));
4576 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
4579 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
4580 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
4582 (unsigned int)(((msr) >> 0) & 0xff),
4583 (unsigned int)(((msr) >> 8) & 0xff),
4584 (unsigned int)(((msr) >> 16) & 0xff),
4585 (unsigned int)(((msr) >> 24) & 0xff),
4586 (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1));
4589 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
4592 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
4593 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
4595 (unsigned int)(((msr) >> 0) & 0xff),
4596 (unsigned int)(((msr) >> 8) & 0xff),
4597 (unsigned int)(((msr) >> 16) & 0xff),
4598 (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3));
4600 if (has_hwp_notify) {
4601 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
4604 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
4605 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
4606 cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis");
4608 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
4611 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
4612 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
4613 cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x4) ? "" : "No-");
4619 * print_perf_limit()
4621 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4623 unsigned long long msr;
4632 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4635 if (cpu_migrate(cpu)) {
4636 fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu);
4640 if (do_core_perf_limit_reasons) {
4641 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
4642 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4643 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
4644 (msr & 1 << 15) ? "bit15, " : "",
4645 (msr & 1 << 14) ? "bit14, " : "",
4646 (msr & 1 << 13) ? "Transitions, " : "",
4647 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
4648 (msr & 1 << 11) ? "PkgPwrL2, " : "",
4649 (msr & 1 << 10) ? "PkgPwrL1, " : "",
4650 (msr & 1 << 9) ? "CorePwr, " : "",
4651 (msr & 1 << 8) ? "Amps, " : "",
4652 (msr & 1 << 6) ? "VR-Therm, " : "",
4653 (msr & 1 << 5) ? "Auto-HWP, " : "",
4654 (msr & 1 << 4) ? "Graphics, " : "",
4655 (msr & 1 << 2) ? "bit2, " : "",
4656 (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : "");
4657 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
4658 (msr & 1 << 31) ? "bit31, " : "",
4659 (msr & 1 << 30) ? "bit30, " : "",
4660 (msr & 1 << 29) ? "Transitions, " : "",
4661 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
4662 (msr & 1 << 27) ? "PkgPwrL2, " : "",
4663 (msr & 1 << 26) ? "PkgPwrL1, " : "",
4664 (msr & 1 << 25) ? "CorePwr, " : "",
4665 (msr & 1 << 24) ? "Amps, " : "",
4666 (msr & 1 << 22) ? "VR-Therm, " : "",
4667 (msr & 1 << 21) ? "Auto-HWP, " : "",
4668 (msr & 1 << 20) ? "Graphics, " : "",
4669 (msr & 1 << 18) ? "bit18, " : "",
4670 (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : "");
4673 if (do_gfx_perf_limit_reasons) {
4674 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
4675 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4676 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
4677 (msr & 1 << 0) ? "PROCHOT, " : "",
4678 (msr & 1 << 1) ? "ThermStatus, " : "",
4679 (msr & 1 << 4) ? "Graphics, " : "",
4680 (msr & 1 << 6) ? "VR-Therm, " : "",
4681 (msr & 1 << 8) ? "Amps, " : "",
4682 (msr & 1 << 9) ? "GFXPwr, " : "",
4683 (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4684 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
4685 (msr & 1 << 16) ? "PROCHOT, " : "",
4686 (msr & 1 << 17) ? "ThermStatus, " : "",
4687 (msr & 1 << 20) ? "Graphics, " : "",
4688 (msr & 1 << 22) ? "VR-Therm, " : "",
4689 (msr & 1 << 24) ? "Amps, " : "",
4690 (msr & 1 << 25) ? "GFXPwr, " : "",
4691 (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
4693 if (do_ring_perf_limit_reasons) {
4694 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
4695 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4696 fprintf(outf, " (Active: %s%s%s%s%s%s)",
4697 (msr & 1 << 0) ? "PROCHOT, " : "",
4698 (msr & 1 << 1) ? "ThermStatus, " : "",
4699 (msr & 1 << 6) ? "VR-Therm, " : "",
4700 (msr & 1 << 8) ? "Amps, " : "",
4701 (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4702 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
4703 (msr & 1 << 16) ? "PROCHOT, " : "",
4704 (msr & 1 << 17) ? "ThermStatus, " : "",
4705 (msr & 1 << 22) ? "VR-Therm, " : "",
4706 (msr & 1 << 24) ? "Amps, " : "",
4707 (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
4712 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
4713 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
4715 double get_tdp_intel(unsigned int model)
4717 unsigned long long msr;
4719 if (do_rapl & RAPL_PKG_POWER_INFO)
4720 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
4721 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
4724 case INTEL_FAM6_ATOM_SILVERMONT:
4725 case INTEL_FAM6_ATOM_SILVERMONT_D:
4732 double get_tdp_amd(unsigned int family)
4736 /* This is the max stock TDP of HEDT/Server Fam17h+ chips */
4741 * rapl_dram_energy_units_probe()
4742 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
4744 static double rapl_dram_energy_units_probe(int model, double rapl_energy_units)
4746 /* only called for genuine_intel, family 6 */
4749 case INTEL_FAM6_HASWELL_X: /* HSX */
4750 case INTEL_FAM6_BROADWELL_X: /* BDX */
4751 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4752 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4753 case INTEL_FAM6_ICELAKE_X: /* ICX */
4754 return (rapl_dram_energy_units = 15.3 / 1000000);
4756 return (rapl_energy_units);
4760 void rapl_probe_intel(unsigned int family, unsigned int model)
4762 unsigned long long msr;
4763 unsigned int time_unit;
4770 case INTEL_FAM6_SANDYBRIDGE:
4771 case INTEL_FAM6_IVYBRIDGE:
4772 case INTEL_FAM6_HASWELL: /* HSW */
4773 case INTEL_FAM6_HASWELL_L: /* HSW */
4774 case INTEL_FAM6_HASWELL_G: /* HSW */
4775 case INTEL_FAM6_BROADWELL: /* BDW */
4776 case INTEL_FAM6_BROADWELL_G: /* BDW */
4777 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
4779 BIC_PRESENT(BIC_Pkg_J);
4780 BIC_PRESENT(BIC_Cor_J);
4781 BIC_PRESENT(BIC_GFX_J);
4783 BIC_PRESENT(BIC_PkgWatt);
4784 BIC_PRESENT(BIC_CorWatt);
4785 BIC_PRESENT(BIC_GFXWatt);
4788 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4789 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4790 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
4792 BIC_PRESENT(BIC_Pkg_J);
4794 BIC_PRESENT(BIC_PkgWatt);
4796 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
4798 RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
4799 | RAPL_GFX | RAPL_PKG_POWER_INFO;
4801 BIC_PRESENT(BIC_Pkg_J);
4802 BIC_PRESENT(BIC_Cor_J);
4803 BIC_PRESENT(BIC_RAM_J);
4804 BIC_PRESENT(BIC_GFX_J);
4806 BIC_PRESENT(BIC_PkgWatt);
4807 BIC_PRESENT(BIC_CorWatt);
4808 BIC_PRESENT(BIC_RAMWatt);
4809 BIC_PRESENT(BIC_GFXWatt);
4812 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
4813 do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
4814 BIC_PRESENT(BIC_PKG__);
4816 BIC_PRESENT(BIC_Pkg_J);
4818 BIC_PRESENT(BIC_PkgWatt);
4820 case INTEL_FAM6_SKYLAKE_L: /* SKL */
4821 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4823 RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
4824 | RAPL_GFX | RAPL_PKG_POWER_INFO;
4825 BIC_PRESENT(BIC_PKG__);
4826 BIC_PRESENT(BIC_RAM__);
4828 BIC_PRESENT(BIC_Pkg_J);
4829 BIC_PRESENT(BIC_Cor_J);
4830 BIC_PRESENT(BIC_RAM_J);
4831 BIC_PRESENT(BIC_GFX_J);
4833 BIC_PRESENT(BIC_PkgWatt);
4834 BIC_PRESENT(BIC_CorWatt);
4835 BIC_PRESENT(BIC_RAMWatt);
4836 BIC_PRESENT(BIC_GFXWatt);
4839 case INTEL_FAM6_HASWELL_X: /* HSX */
4840 case INTEL_FAM6_BROADWELL_X: /* BDX */
4841 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4842 case INTEL_FAM6_ICELAKE_X: /* ICX */
4843 case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
4844 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
4846 RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
4847 RAPL_PKG_POWER_INFO;
4848 BIC_PRESENT(BIC_PKG__);
4849 BIC_PRESENT(BIC_RAM__);
4851 BIC_PRESENT(BIC_Pkg_J);
4852 BIC_PRESENT(BIC_RAM_J);
4854 BIC_PRESENT(BIC_PkgWatt);
4855 BIC_PRESENT(BIC_RAMWatt);
4858 case INTEL_FAM6_SANDYBRIDGE_X:
4859 case INTEL_FAM6_IVYBRIDGE_X:
4861 RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS |
4862 RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
4863 BIC_PRESENT(BIC_PKG__);
4864 BIC_PRESENT(BIC_RAM__);
4866 BIC_PRESENT(BIC_Pkg_J);
4867 BIC_PRESENT(BIC_Cor_J);
4868 BIC_PRESENT(BIC_RAM_J);
4870 BIC_PRESENT(BIC_PkgWatt);
4871 BIC_PRESENT(BIC_CorWatt);
4872 BIC_PRESENT(BIC_RAMWatt);
4875 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
4876 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
4877 do_rapl = RAPL_PKG | RAPL_CORES;
4879 BIC_PRESENT(BIC_Pkg_J);
4880 BIC_PRESENT(BIC_Cor_J);
4882 BIC_PRESENT(BIC_PkgWatt);
4883 BIC_PRESENT(BIC_CorWatt);
4886 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
4888 RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
4889 RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
4890 BIC_PRESENT(BIC_PKG__);
4891 BIC_PRESENT(BIC_RAM__);
4893 BIC_PRESENT(BIC_Pkg_J);
4894 BIC_PRESENT(BIC_Cor_J);
4895 BIC_PRESENT(BIC_RAM_J);
4897 BIC_PRESENT(BIC_PkgWatt);
4898 BIC_PRESENT(BIC_CorWatt);
4899 BIC_PRESENT(BIC_RAMWatt);
4906 /* units on package 0, verify later other packages match */
4907 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4910 rapl_power_units = 1.0 / (1 << (msr & 0xF));
4911 if (model == INTEL_FAM6_ATOM_SILVERMONT)
4912 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4914 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4916 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
4918 time_unit = msr >> 16 & 0xF;
4922 rapl_time_units = 1.0 / (1 << (time_unit));
4924 tdp = get_tdp_intel(model);
4926 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
4928 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
4931 void rapl_probe_amd(unsigned int family, unsigned int model)
4933 unsigned long long msr;
4934 unsigned int eax, ebx, ecx, edx;
4935 unsigned int has_rapl = 0;
4940 if (max_extended_level >= 0x80000007) {
4941 __cpuid(0x80000007, eax, ebx, ecx, edx);
4942 /* RAPL (Fam 17h+) */
4943 has_rapl = edx & (1 << 14);
4946 if (!has_rapl || family < 0x17)
4949 do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
4951 BIC_PRESENT(BIC_Pkg_J);
4952 BIC_PRESENT(BIC_Cor_J);
4954 BIC_PRESENT(BIC_PkgWatt);
4955 BIC_PRESENT(BIC_CorWatt);
4958 if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
4961 rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
4962 rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
4963 rapl_power_units = ldexp(1.0, -(msr & 0xf));
4965 tdp = get_tdp_amd(family);
4967 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
4969 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
4975 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
4977 void rapl_probe(unsigned int family, unsigned int model)
4980 rapl_probe_intel(family, model);
4981 if (authentic_amd || hygon_genuine)
4982 rapl_probe_amd(family, model);
4985 void perf_limit_reasons_probe(unsigned int family, unsigned int model)
4994 case INTEL_FAM6_HASWELL: /* HSW */
4995 case INTEL_FAM6_HASWELL_L: /* HSW */
4996 case INTEL_FAM6_HASWELL_G: /* HSW */
4997 do_gfx_perf_limit_reasons = 1;
4999 case INTEL_FAM6_HASWELL_X: /* HSX */
5000 do_core_perf_limit_reasons = 1;
5001 do_ring_perf_limit_reasons = 1;
5007 void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
5013 case INTEL_FAM6_BROADWELL_X:
5014 case INTEL_FAM6_SKYLAKE_X:
5015 has_automatic_cstate_conversion = 1;
5019 void prewake_cstate_probe(unsigned int family, unsigned int model)
5021 if (is_icx(family, model) || is_spr(family, model))
5022 dis_cstate_prewake = 1;
5025 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
5027 unsigned long long msr;
5028 unsigned int dts, dts2;
5034 if (!(do_dts || do_ptm))
5039 /* DTS is per-core, no need to print for each thread */
5040 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
5043 if (cpu_migrate(cpu)) {
5044 fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu);
5048 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
5049 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
5052 dts = (msr >> 16) & 0x7F;
5053 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts);
5055 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
5058 dts = (msr >> 16) & 0x7F;
5059 dts2 = (msr >> 8) & 0x7F;
5060 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
5061 cpu, msr, tj_max - dts, tj_max - dts2);
5064 if (do_dts && debug) {
5065 unsigned int resolution;
5067 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
5070 dts = (msr >> 16) & 0x7F;
5071 resolution = (msr >> 27) & 0xF;
5072 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
5073 cpu, msr, tj_max - dts, resolution);
5075 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
5078 dts = (msr >> 16) & 0x7F;
5079 dts2 = (msr >> 8) & 0x7F;
5080 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
5081 cpu, msr, tj_max - dts, tj_max - dts2);
5087 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
5089 fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n",
5091 ((msr >> 15) & 1) ? "EN" : "DIS",
5092 ((msr >> 0) & 0x7FFF) * rapl_power_units,
5093 (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
5094 (((msr >> 16) & 1) ? "EN" : "DIS"));
5099 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
5101 unsigned long long msr;
5102 const char *msr_name;
5111 /* RAPL counters are per package, so print only for 1st thread/package */
5112 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
5116 if (cpu_migrate(cpu)) {
5117 fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu);
5121 if (do_rapl & RAPL_AMD_F17H) {
5122 msr_name = "MSR_RAPL_PWR_UNIT";
5123 if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
5126 msr_name = "MSR_RAPL_POWER_UNIT";
5127 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
5131 fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
5132 rapl_power_units, rapl_energy_units, rapl_time_units);
5134 if (do_rapl & RAPL_PKG_POWER_INFO) {
5136 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
5139 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
5141 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
5142 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
5143 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
5144 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
5147 if (do_rapl & RAPL_PKG) {
5149 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
5152 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
5153 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
5155 print_power_limit_msr(cpu, msr, "PKG Limit #1");
5156 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n",
5158 ((msr >> 47) & 1) ? "EN" : "DIS",
5159 ((msr >> 32) & 0x7FFF) * rapl_power_units,
5160 (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
5161 ((msr >> 48) & 1) ? "EN" : "DIS");
5163 if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr))
5166 fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr);
5167 fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n",
5168 cpu, ((msr >> 0) & 0x1FFF) * rapl_power_units, (msr >> 31) & 1 ? "" : "UN");
5171 if (do_rapl & RAPL_DRAM_POWER_INFO) {
5172 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
5175 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
5177 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
5178 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
5179 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
5180 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
5182 if (do_rapl & RAPL_DRAM) {
5183 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
5185 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
5186 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5188 print_power_limit_msr(cpu, msr, "DRAM Limit");
5190 if (do_rapl & RAPL_CORE_POLICY) {
5191 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
5194 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
5196 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
5197 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
5199 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
5200 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5201 print_power_limit_msr(cpu, msr, "Cores Limit");
5203 if (do_rapl & RAPL_GFX) {
5204 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
5207 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
5209 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
5211 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
5212 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5213 print_power_limit_msr(cpu, msr, "GFX Limit");
5219 * SNB adds support for additional MSRs:
5221 * MSR_PKG_C7_RESIDENCY 0x000003fa
5222 * MSR_CORE_C7_RESIDENCY 0x000003fe
5223 * MSR_PKG_C2_RESIDENCY 0x0000060d
5226 int has_snb_msrs(unsigned int family, unsigned int model)
5235 case INTEL_FAM6_SANDYBRIDGE:
5236 case INTEL_FAM6_SANDYBRIDGE_X:
5237 case INTEL_FAM6_IVYBRIDGE: /* IVB */
5238 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
5239 case INTEL_FAM6_HASWELL: /* HSW */
5240 case INTEL_FAM6_HASWELL_X: /* HSW */
5241 case INTEL_FAM6_HASWELL_L: /* HSW */
5242 case INTEL_FAM6_HASWELL_G: /* HSW */
5243 case INTEL_FAM6_BROADWELL: /* BDW */
5244 case INTEL_FAM6_BROADWELL_G: /* BDW */
5245 case INTEL_FAM6_BROADWELL_X: /* BDX */
5246 case INTEL_FAM6_SKYLAKE_L: /* SKL */
5247 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
5248 case INTEL_FAM6_SKYLAKE_X: /* SKX */
5249 case INTEL_FAM6_ICELAKE_X: /* ICX */
5250 case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */
5251 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5252 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5253 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
5254 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
5255 case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */
5262 * HSW ULT added support for C8/C9/C10 MSRs:
5264 * MSR_PKG_C8_RESIDENCY 0x00000630
5265 * MSR_PKG_C9_RESIDENCY 0x00000631
5266 * MSR_PKG_C10_RESIDENCY 0x00000632
5268 * MSR_PKGC8_IRTL 0x00000633
5269 * MSR_PKGC9_IRTL 0x00000634
5270 * MSR_PKGC10_IRTL 0x00000635
5273 int has_c8910_msrs(unsigned int family, unsigned int model)
5282 case INTEL_FAM6_HASWELL_L: /* HSW */
5283 case INTEL_FAM6_BROADWELL: /* BDW */
5284 case INTEL_FAM6_SKYLAKE_L: /* SKL */
5285 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
5286 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5287 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5288 case INTEL_FAM6_ATOM_TREMONT: /* EHL */
5295 * SKL adds support for additional MSRS:
5297 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
5298 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
5299 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
5300 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
5302 int has_skl_msrs(unsigned int family, unsigned int model)
5311 case INTEL_FAM6_SKYLAKE_L: /* SKL */
5312 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
5318 int is_slm(unsigned int family, unsigned int model)
5327 case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */
5328 case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */
5334 int is_knl(unsigned int family, unsigned int model)
5343 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
5349 int is_cnl(unsigned int family, unsigned int model)
5358 case INTEL_FAM6_CANNONLAKE_L: /* CNL */
5365 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
5367 if (is_knl(family, model))
5372 #define SLM_BCLK_FREQS 5
5373 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 };
5375 double slm_bclk(void)
5377 unsigned long long msr = 3;
5381 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
5382 fprintf(outf, "SLM BCLK: unknown\n");
5385 if (i >= SLM_BCLK_FREQS) {
5386 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
5389 freq = slm_freq_table[i];
5392 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
5397 double discover_bclk(unsigned int family, unsigned int model)
5399 if (has_snb_msrs(family, model) || is_knl(family, model))
5401 else if (is_slm(family, model))
5407 int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p)
5409 unsigned int eax, ebx, ecx, edx;
5417 if (cpu_migrate(t->cpu_id)) {
5418 fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id);
5422 if (max_level < 0x1a)
5425 __cpuid(0x1a, eax, ebx, ecx, edx);
5426 eax = (eax >> 24) & 0xFF;
5433 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
5434 * the Thermal Control Circuit (TCC) activates.
5435 * This is usually equal to tjMax.
5437 * Older processors do not have this MSR, so there we guess,
5438 * but also allow cmdline over-ride with -T.
5440 * Several MSR temperature values are in units of degrees-C
5441 * below this value, including the Digital Thermal Sensor (DTS),
5442 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
5444 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
5446 unsigned long long msr;
5447 unsigned int tcc_default, tcc_offset;
5453 /* tj_max is used only for dts or ptm */
5454 if (!(do_dts || do_ptm))
5457 /* this is a per-package concept */
5458 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
5462 if (cpu_migrate(cpu)) {
5463 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
5467 if (tj_max_override != 0) {
5468 tj_max = tj_max_override;
5469 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max);
5473 /* Temperature Target MSR is Nehalem and newer only */
5474 if (!do_nhm_platform_info)
5477 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
5480 tcc_default = (msr >> 16) & 0xFF;
5483 switch (tcc_offset_bits) {
5485 tcc_offset = (msr >> 24) & 0xF;
5486 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
5487 cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
5490 tcc_offset = (msr >> 24) & 0x3F;
5491 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
5492 cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
5495 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default);
5503 tj_max = tcc_default;
5508 tj_max = TJMAX_DEFAULT;
5509 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max);
5514 void decode_feature_control_msr(void)
5516 unsigned long long msr;
5518 if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
5519 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
5520 base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : "");
5523 void decode_misc_enable_msr(void)
5525 unsigned long long msr;
5530 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
5531 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
5533 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
5534 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
5535 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
5536 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
5537 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
5540 void decode_misc_feature_control(void)
5542 unsigned long long msr;
5544 if (!has_misc_feature_control)
5547 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
5549 "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
5550 base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "",
5551 msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : "");
5555 * Decode MSR_MISC_PWR_MGMT
5557 * Decode the bits according to the Nehalem documentation
5558 * bit[0] seems to continue to have same meaning going forward
5561 void decode_misc_pwr_mgmt_msr(void)
5563 unsigned long long msr;
5565 if (!do_nhm_platform_info)
5568 if (no_MSR_MISC_PWR_MGMT)
5571 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
5572 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
5574 msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS");
5578 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
5580 * This MSRs are present on Silvermont processors,
5581 * Intel Atom processor E3000 series (Baytrail), and friends.
5583 void decode_c6_demotion_policy_msr(void)
5585 unsigned long long msr;
5587 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
5588 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
5589 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5591 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
5592 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
5593 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
5597 * When models are the same, for the purpose of turbostat, reuse
5599 unsigned int intel_model_duplicates(unsigned int model)
5603 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
5604 case INTEL_FAM6_NEHALEM_G: /* Core i7 and i5 Processor - Nehalem */
5605 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
5606 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
5607 return INTEL_FAM6_NEHALEM;
5609 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
5610 return INTEL_FAM6_NEHALEM_EX;
5612 case INTEL_FAM6_XEON_PHI_KNM:
5613 return INTEL_FAM6_XEON_PHI_KNL;
5615 case INTEL_FAM6_BROADWELL_D: /* BDX-DE */
5616 return INTEL_FAM6_BROADWELL_X;
5618 case INTEL_FAM6_SKYLAKE:
5619 case INTEL_FAM6_KABYLAKE_L:
5620 case INTEL_FAM6_KABYLAKE:
5621 case INTEL_FAM6_COMETLAKE_L:
5622 case INTEL_FAM6_COMETLAKE:
5623 return INTEL_FAM6_SKYLAKE_L;
5625 case INTEL_FAM6_ICELAKE_L:
5626 case INTEL_FAM6_ICELAKE_NNPI:
5627 case INTEL_FAM6_TIGERLAKE_L:
5628 case INTEL_FAM6_TIGERLAKE:
5629 case INTEL_FAM6_ROCKETLAKE:
5630 case INTEL_FAM6_LAKEFIELD:
5631 case INTEL_FAM6_ALDERLAKE:
5632 case INTEL_FAM6_ALDERLAKE_L:
5633 case INTEL_FAM6_ATOM_GRACEMONT:
5634 case INTEL_FAM6_RAPTORLAKE:
5635 case INTEL_FAM6_RAPTORLAKE_P:
5636 case INTEL_FAM6_RAPTORLAKE_S:
5637 case INTEL_FAM6_METEORLAKE:
5638 case INTEL_FAM6_METEORLAKE_L:
5639 return INTEL_FAM6_CANNONLAKE_L;
5641 case INTEL_FAM6_ATOM_TREMONT_L:
5642 return INTEL_FAM6_ATOM_TREMONT;
5644 case INTEL_FAM6_ICELAKE_D:
5645 return INTEL_FAM6_ICELAKE_X;
5647 case INTEL_FAM6_EMERALDRAPIDS_X:
5648 return INTEL_FAM6_SAPPHIRERAPIDS_X;
5653 void print_dev_latency(void)
5655 char *path = "/dev/cpu_dma_latency";
5660 fd = open(path, O_RDONLY);
5662 warnx("capget(CAP_SYS_ADMIN) failed, try \"# setcap cap_sys_admin=ep %s\"", progname);
5666 retval = read(fd, (void *)&value, sizeof(int));
5667 if (retval != sizeof(int)) {
5668 warn("read failed %s", path);
5672 fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained");
5678 * Linux-perf manages the HW instructions-retired counter
5679 * by enabling when requested, and hiding rollover
5681 void linux_perf_init(void)
5683 if (!BIC_IS_ENABLED(BIC_IPC))
5686 if (access("/proc/sys/kernel/perf_event_paranoid", F_OK))
5689 fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5690 if (fd_instr_count_percpu == NULL)
5691 err(-1, "calloc fd_instr_count_percpu");
5693 BIC_PRESENT(BIC_IPC);
5696 void process_cpuid()
5698 unsigned int eax, ebx, ecx, edx;
5699 unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
5700 unsigned long long ucode_patch = 0;
5702 eax = ebx = ecx = edx = 0;
5704 __cpuid(0, max_level, ebx, ecx, edx);
5706 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
5708 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
5710 else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
5714 fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n",
5715 (char *)&ebx, (char *)&edx, (char *)&ecx, max_level);
5717 __cpuid(1, fms, ebx, ecx, edx);
5718 family = (fms >> 8) & 0xf;
5719 model = (fms >> 4) & 0xf;
5720 stepping = fms & 0xf;
5722 family += (fms >> 20) & 0xff;
5724 model += ((fms >> 16) & 0xf) << 4;
5728 if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
5729 warnx("get_msr(UCODE)");
5732 * check max extended function levels of CPUID.
5733 * This is needed to check for invariant TSC.
5734 * This check is valid for both Intel and AMD.
5736 ebx = ecx = edx = 0;
5737 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
5740 fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n",
5741 family, model, stepping, family, model, stepping,
5742 (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
5743 fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
5744 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
5745 ecx_flags & (1 << 0) ? "SSE3" : "-",
5746 ecx_flags & (1 << 3) ? "MONITOR" : "-",
5747 ecx_flags & (1 << 6) ? "SMX" : "-",
5748 ecx_flags & (1 << 7) ? "EIST" : "-",
5749 ecx_flags & (1 << 8) ? "TM2" : "-",
5750 edx_flags & (1 << 4) ? "TSC" : "-",
5751 edx_flags & (1 << 5) ? "MSR" : "-",
5752 edx_flags & (1 << 22) ? "ACPI-TM" : "-",
5753 edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-");
5756 probe_platform_features(family, model);
5758 model = intel_model_duplicates(model);
5760 if (!(edx_flags & (1 << 5)))
5761 errx(1, "CPUID: no MSR");
5763 if (max_extended_level >= 0x80000007) {
5766 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
5767 * this check is valid for both Intel and AMD
5769 __cpuid(0x80000007, eax, ebx, ecx, edx);
5770 has_invariant_tsc = edx & (1 << 8);
5774 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
5775 * this check is valid for both Intel and AMD
5778 __cpuid(0x6, eax, ebx, ecx, edx);
5779 has_aperf = ecx & (1 << 0);
5781 BIC_PRESENT(BIC_Avg_MHz);
5782 BIC_PRESENT(BIC_Busy);
5783 BIC_PRESENT(BIC_Bzy_MHz);
5785 do_dts = eax & (1 << 0);
5787 BIC_PRESENT(BIC_CoreTmp);
5788 has_turbo = eax & (1 << 1);
5789 do_ptm = eax & (1 << 6);
5791 BIC_PRESENT(BIC_PkgTmp);
5792 has_hwp = eax & (1 << 7);
5793 has_hwp_notify = eax & (1 << 8);
5794 has_hwp_activity_window = eax & (1 << 9);
5795 has_hwp_epp = eax & (1 << 10);
5796 has_hwp_pkg = eax & (1 << 11);
5797 has_epb = ecx & (1 << 3);
5800 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
5801 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
5802 has_aperf ? "" : "No-",
5803 has_turbo ? "" : "No-",
5804 do_dts ? "" : "No-",
5805 do_ptm ? "" : "No-",
5806 has_hwp ? "" : "No-",
5807 has_hwp_notify ? "" : "No-",
5808 has_hwp_activity_window ? "" : "No-",
5809 has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-");
5812 decode_misc_enable_msr();
5814 if (max_level >= 0x7 && !quiet) {
5819 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
5821 has_sgx = ebx & (1 << 2);
5823 is_hybrid = edx & (1 << 15);
5825 fprintf(outf, "CPUID(7): %sSGX %sHybrid\n", has_sgx ? "" : "No-", is_hybrid ? "" : "No-");
5828 decode_feature_control_msr();
5831 if (max_level >= 0x15) {
5832 unsigned int eax_crystal;
5833 unsigned int ebx_tsc;
5836 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
5838 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
5839 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
5843 if (!quiet && (ebx != 0))
5844 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
5845 eax_crystal, ebx_tsc, crystal_hz);
5847 if (crystal_hz == 0)
5849 case INTEL_FAM6_SKYLAKE_L: /* SKL */
5850 crystal_hz = 24000000; /* 24.0 MHz */
5852 case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */
5853 crystal_hz = 25000000; /* 25.0 MHz */
5855 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
5856 case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5857 crystal_hz = 19200000; /* 19.2 MHz */
5864 tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal;
5866 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
5867 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
5871 if (max_level >= 0x16) {
5872 unsigned int base_mhz, max_mhz, bus_mhz, edx;
5875 * CPUID 16H Base MHz, Max MHz, Bus MHz
5877 base_mhz = max_mhz = bus_mhz = edx = 0;
5879 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
5881 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
5882 base_mhz, max_mhz, bus_mhz);
5886 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
5888 BIC_PRESENT(BIC_IRQ);
5889 BIC_PRESENT(BIC_TSC_MHz);
5891 if (probe_nhm_msrs(family, model)) {
5892 do_nhm_platform_info = 1;
5893 BIC_PRESENT(BIC_CPU_c1);
5894 BIC_PRESENT(BIC_CPU_c3);
5895 BIC_PRESENT(BIC_CPU_c6);
5896 BIC_PRESENT(BIC_SMI);
5898 do_snb_cstates = has_snb_msrs(family, model);
5901 BIC_PRESENT(BIC_CPU_c7);
5903 do_irtl_snb = has_snb_msrs(family, model);
5904 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
5905 BIC_PRESENT(BIC_Pkgpc2);
5906 if (pkg_cstate_limit >= PCL__3)
5907 BIC_PRESENT(BIC_Pkgpc3);
5908 if (pkg_cstate_limit >= PCL__6)
5909 BIC_PRESENT(BIC_Pkgpc6);
5910 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
5911 BIC_PRESENT(BIC_Pkgpc7);
5912 if (has_slv_msrs(family, model)) {
5913 BIC_NOT_PRESENT(BIC_Pkgpc2);
5914 BIC_NOT_PRESENT(BIC_Pkgpc3);
5915 BIC_PRESENT(BIC_Pkgpc6);
5916 BIC_NOT_PRESENT(BIC_Pkgpc7);
5917 BIC_PRESENT(BIC_Mod_c6);
5918 use_c1_residency_msr = 1;
5920 if (is_jvl(family, model)) {
5921 BIC_NOT_PRESENT(BIC_CPU_c3);
5922 BIC_NOT_PRESENT(BIC_CPU_c7);
5923 BIC_NOT_PRESENT(BIC_Pkgpc2);
5924 BIC_NOT_PRESENT(BIC_Pkgpc3);
5925 BIC_NOT_PRESENT(BIC_Pkgpc6);
5926 BIC_NOT_PRESENT(BIC_Pkgpc7);
5928 if (is_dnv(family, model)) {
5929 BIC_PRESENT(BIC_CPU_c1);
5930 BIC_NOT_PRESENT(BIC_CPU_c3);
5931 BIC_NOT_PRESENT(BIC_Pkgpc3);
5932 BIC_NOT_PRESENT(BIC_CPU_c7);
5933 BIC_NOT_PRESENT(BIC_Pkgpc7);
5934 use_c1_residency_msr = 1;
5936 if (is_skx(family, model) || is_icx(family, model) || is_spr(family, model)) {
5937 BIC_NOT_PRESENT(BIC_CPU_c3);
5938 BIC_NOT_PRESENT(BIC_Pkgpc3);
5939 BIC_NOT_PRESENT(BIC_CPU_c7);
5940 BIC_NOT_PRESENT(BIC_Pkgpc7);
5942 if (is_bdx(family, model)) {
5943 BIC_NOT_PRESENT(BIC_CPU_c7);
5944 BIC_NOT_PRESENT(BIC_Pkgpc7);
5946 if (has_c8910_msrs(family, model)) {
5947 if (pkg_cstate_limit >= PCL__8)
5948 BIC_PRESENT(BIC_Pkgpc8);
5949 if (pkg_cstate_limit >= PCL__9)
5950 BIC_PRESENT(BIC_Pkgpc9);
5951 if (pkg_cstate_limit >= PCL_10)
5952 BIC_PRESENT(BIC_Pkgpc10);
5954 do_irtl_hsw = has_c8910_msrs(family, model);
5955 if (has_skl_msrs(family, model)) {
5956 BIC_PRESENT(BIC_Totl_c0);
5957 BIC_PRESENT(BIC_Any_c0);
5958 BIC_PRESENT(BIC_GFX_c0);
5959 BIC_PRESENT(BIC_CPUGFX);
5961 do_slm_cstates = is_slm(family, model);
5962 do_knl_cstates = is_knl(family, model);
5964 if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model))
5965 BIC_NOT_PRESENT(BIC_CPU_c3);
5968 decode_misc_pwr_mgmt_msr();
5970 if (!quiet && has_slv_msrs(family, model))
5971 decode_c6_demotion_policy_msr();
5973 rapl_probe(family, model);
5974 perf_limit_reasons_probe(family, model);
5975 automatic_cstate_conversion_probe(family, model);
5976 prewake_cstate_probe(family, model);
5978 check_tcc_offset(model);
5981 dump_cstate_pstate_config_info(family, model);
5982 intel_uncore_frequency_probe();
5985 print_dev_latency();
5987 dump_sysfs_cstate_config();
5989 dump_sysfs_pstate_config();
5991 if (has_skl_msrs(family, model) || is_ehl(family, model))
5992 calculate_tsc_tweak();
5994 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
5995 BIC_PRESENT(BIC_GFX_rc6);
5997 if (!access("/sys/class/drm/card0/gt_cur_freq_mhz", R_OK) ||
5998 !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5999 BIC_PRESENT(BIC_GFXMHz);
6001 if (!access("/sys/class/drm/card0/gt_act_freq_mhz", R_OK) ||
6002 !access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK))
6003 BIC_PRESENT(BIC_GFXACTMHz);
6005 if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
6006 BIC_PRESENT(BIC_CPU_LPI);
6008 BIC_NOT_PRESENT(BIC_CPU_LPI);
6010 if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK))
6011 BIC_PRESENT(BIC_CORE_THROT_CNT);
6013 BIC_NOT_PRESENT(BIC_CORE_THROT_CNT);
6015 if (!access(sys_lpi_file_sysfs, R_OK)) {
6016 sys_lpi_file = sys_lpi_file_sysfs;
6017 BIC_PRESENT(BIC_SYS_LPI);
6018 } else if (!access(sys_lpi_file_debugfs, R_OK)) {
6019 sys_lpi_file = sys_lpi_file_debugfs;
6020 BIC_PRESENT(BIC_SYS_LPI);
6022 sys_lpi_file_sysfs = NULL;
6023 BIC_NOT_PRESENT(BIC_SYS_LPI);
6027 decode_misc_feature_control();
6033 * in /dev/cpu/ return success for names that are numbers
6034 * ie. filter out ".", "..", "microcode".
6036 int dir_filter(const struct dirent *dirp)
6038 if (isdigit(dirp->d_name[0]))
6044 void topology_probe()
6047 int max_core_id = 0;
6048 int max_package_id = 0;
6050 int max_siblings = 0;
6052 /* Initialize num_cpus, max_cpu_num */
6055 for_all_proc_cpus(count_cpus);
6056 if (!summary_only && topo.num_cpus > 1)
6057 BIC_PRESENT(BIC_CPU);
6060 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
6062 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
6064 err(1, "calloc cpus");
6067 * Allocate and initialize cpu_present_set
6069 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
6070 if (cpu_present_set == NULL)
6071 err(3, "CPU_ALLOC");
6072 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
6073 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
6074 for_all_proc_cpus(mark_cpu_present);
6077 * Validate that all cpus in cpu_subset are also in cpu_present_set
6079 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
6080 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
6081 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
6082 err(1, "cpu%d not present", i);
6086 * Allocate and initialize cpu_affinity_set
6088 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
6089 if (cpu_affinity_set == NULL)
6090 err(3, "CPU_ALLOC");
6091 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
6092 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
6094 for_all_proc_cpus(init_thread_id);
6098 * find max_core_id, max_package_id
6100 for (i = 0; i <= topo.max_cpu_num; ++i) {
6103 if (cpu_is_not_present(i)) {
6105 fprintf(outf, "cpu%d NOT PRESENT\n", i);
6109 cpus[i].logical_cpu_id = i;
6111 /* get package information */
6112 cpus[i].physical_package_id = get_physical_package_id(i);
6113 if (cpus[i].physical_package_id > max_package_id)
6114 max_package_id = cpus[i].physical_package_id;
6116 /* get die information */
6117 cpus[i].die_id = get_die_id(i);
6118 if (cpus[i].die_id > max_die_id)
6119 max_die_id = cpus[i].die_id;
6121 /* get numa node information */
6122 cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
6123 if (cpus[i].physical_node_id > topo.max_node_num)
6124 topo.max_node_num = cpus[i].physical_node_id;
6126 /* get core information */
6127 cpus[i].physical_core_id = get_core_id(i);
6128 if (cpus[i].physical_core_id > max_core_id)
6129 max_core_id = cpus[i].physical_core_id;
6131 /* get thread information */
6132 siblings = get_thread_siblings(&cpus[i]);
6133 if (siblings > max_siblings)
6134 max_siblings = siblings;
6135 if (cpus[i].thread_id == 0)
6139 topo.cores_per_node = max_core_id + 1;
6141 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node);
6142 if (!summary_only && topo.cores_per_node > 1)
6143 BIC_PRESENT(BIC_Core);
6145 topo.num_die = max_die_id + 1;
6147 fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die);
6148 if (!summary_only && topo.num_die > 1)
6149 BIC_PRESENT(BIC_Die);
6151 topo.num_packages = max_package_id + 1;
6153 fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages);
6154 if (!summary_only && topo.num_packages > 1)
6155 BIC_PRESENT(BIC_Package);
6159 fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
6160 if (!summary_only && topo.nodes_per_pkg > 1)
6161 BIC_PRESENT(BIC_Node);
6163 topo.threads_per_core = max_siblings;
6165 fprintf(outf, "max_siblings %d\n", max_siblings);
6170 for (i = 0; i <= topo.max_cpu_num; ++i) {
6171 if (cpu_is_not_present(i))
6174 "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
6175 i, cpus[i].physical_package_id, cpus[i].die_id,
6176 cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id);
6181 void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
6184 int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages;
6185 int num_threads = topo.threads_per_core * num_cores;
6187 *t = calloc(num_threads, sizeof(struct thread_data));
6191 for (i = 0; i < num_threads; i++)
6192 (*t)[i].cpu_id = -1;
6194 *c = calloc(num_cores, sizeof(struct core_data));
6198 for (i = 0; i < num_cores; i++)
6199 (*c)[i].core_id = -1;
6201 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
6205 for (i = 0; i < topo.num_packages; i++)
6206 (*p)[i].package_id = i;
6210 err(1, "calloc counters");
6216 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
6218 void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id)
6220 int pkg_id = cpus[cpu_id].physical_package_id;
6221 int node_id = cpus[cpu_id].logical_node_id;
6222 int core_id = cpus[cpu_id].physical_core_id;
6223 int thread_id = cpus[cpu_id].thread_id;
6224 struct thread_data *t;
6225 struct core_data *c;
6228 /* Workaround for systems where physical_node_id==-1
6229 * and logical_node_id==(-1 - topo.num_cpus)
6234 t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
6235 c = GET_CORE(core_base, core_id, node_id, pkg_id);
6236 p = GET_PKG(pkg_base, pkg_id);
6239 if (thread_id == 0) {
6240 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
6241 if (cpu_is_first_core_in_package(cpu_id))
6242 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
6245 c->core_id = core_id;
6246 p->package_id = pkg_id;
6249 int initialize_counters(int cpu_id)
6251 init_counter(EVEN_COUNTERS, cpu_id);
6252 init_counter(ODD_COUNTERS, cpu_id);
6256 void allocate_output_buffer()
6258 output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
6259 outp = output_buffer;
6261 err(-1, "calloc output buffer");
6264 void allocate_fd_percpu(void)
6266 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
6267 if (fd_percpu == NULL)
6268 err(-1, "calloc fd_percpu");
6271 void allocate_irq_buffers(void)
6273 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
6274 if (irq_column_2_cpu == NULL)
6275 err(-1, "calloc %d", topo.num_cpus);
6277 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
6278 if (irqs_per_cpu == NULL)
6279 err(-1, "calloc %d", topo.max_cpu_num + 1);
6282 void setup_all_buffers(void)
6285 allocate_irq_buffers();
6286 allocate_fd_percpu();
6287 allocate_counters(&thread_even, &core_even, &package_even);
6288 allocate_counters(&thread_odd, &core_odd, &package_odd);
6289 allocate_output_buffer();
6290 for_all_proc_cpus(initialize_counters);
6293 void set_base_cpu(void)
6295 base_cpu = sched_getcpu();
6297 err(-ENODEV, "No valid cpus found");
6300 fprintf(outf, "base_cpu = %d\n", base_cpu);
6303 void turbostat_init()
6305 setup_all_buffers();
6308 check_permissions();
6313 for_all_cpus(print_hwp, ODD_COUNTERS);
6316 for_all_cpus(print_epb, ODD_COUNTERS);
6319 for_all_cpus(print_perf_limit, ODD_COUNTERS);
6322 for_all_cpus(print_rapl, ODD_COUNTERS);
6324 for_all_cpus(set_temperature_target, ODD_COUNTERS);
6326 for_all_cpus(get_cpu_type, ODD_COUNTERS);
6327 for_all_cpus(get_cpu_type, EVEN_COUNTERS);
6330 for_all_cpus(print_thermal, ODD_COUNTERS);
6332 if (!quiet && do_irtl_snb)
6335 if (DO_BIC(BIC_IPC))
6336 (void)get_instr_count_fd(base_cpu);
6339 int fork_it(char **argv)
6344 snapshot_proc_sysfs_files();
6345 status = for_all_cpus(get_counters, EVEN_COUNTERS);
6346 first_counter_read = 0;
6349 /* clear affinity side-effect of get_counters() */
6350 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
6351 gettimeofday(&tv_even, (struct timezone *)NULL);
6356 execvp(argv[0], argv);
6357 err(errno, "exec %s", argv[0]);
6361 if (child_pid == -1)
6364 signal(SIGINT, SIG_IGN);
6365 signal(SIGQUIT, SIG_IGN);
6366 if (waitpid(child_pid, &status, 0) == -1)
6367 err(status, "waitpid");
6369 if (WIFEXITED(status))
6370 status = WEXITSTATUS(status);
6373 * n.b. fork_it() does not check for errors from for_all_cpus()
6374 * because re-starting is problematic when forking
6376 snapshot_proc_sysfs_files();
6377 for_all_cpus(get_counters, ODD_COUNTERS);
6378 gettimeofday(&tv_odd, (struct timezone *)NULL);
6379 timersub(&tv_odd, &tv_even, &tv_delta);
6380 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
6381 fprintf(outf, "%s: Counter reset detected\n", progname);
6383 compute_average(EVEN_COUNTERS);
6384 format_all_counters(EVEN_COUNTERS);
6387 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0);
6389 flush_output_stderr();
6394 int get_and_dump_counters(void)
6398 snapshot_proc_sysfs_files();
6399 status = for_all_cpus(get_counters, ODD_COUNTERS);
6403 status = for_all_cpus(dump_counters, ODD_COUNTERS);
6407 flush_output_stdout();
6412 void print_version()
6414 fprintf(outf, "turbostat version 2023.03.17 - Len Brown <lenb@kernel.org>\n");
6417 #define COMMAND_LINE_SIZE 2048
6419 void print_bootcmd(void)
6421 char bootcmd[COMMAND_LINE_SIZE];
6425 memset(bootcmd, 0, COMMAND_LINE_SIZE);
6426 fp = fopen("/proc/cmdline", "r");
6430 ret = fread(bootcmd, sizeof(char), COMMAND_LINE_SIZE - 1, fp);
6432 bootcmd[ret] = '\0';
6433 /* the last character is already '\n' */
6434 fprintf(outf, "Kernel command line: %s", bootcmd);
6440 int add_counter(unsigned int msr_num, char *path, char *name,
6441 unsigned int width, enum counter_scope scope,
6442 enum counter_type type, enum counter_format format, int flags)
6444 struct msr_counter *msrp;
6446 msrp = calloc(1, sizeof(struct msr_counter));
6452 msrp->msr_num = msr_num;
6453 strncpy(msrp->name, name, NAME_BYTES - 1);
6455 strncpy(msrp->path, path, PATH_BYTES - 1);
6456 msrp->width = width;
6458 msrp->format = format;
6459 msrp->flags = flags;
6464 msrp->next = sys.tp;
6466 sys.added_thread_counters++;
6467 if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
6468 fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS);
6474 msrp->next = sys.cp;
6476 sys.added_core_counters++;
6477 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
6478 fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS);
6484 msrp->next = sys.pp;
6486 sys.added_package_counters++;
6487 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
6488 fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS);
6497 void parse_add_command(char *add_command)
6501 char name_buffer[NAME_BYTES] = "";
6504 enum counter_scope scope = SCOPE_CPU;
6505 enum counter_type type = COUNTER_CYCLES;
6506 enum counter_format format = FORMAT_DELTA;
6508 while (add_command) {
6510 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
6513 if (sscanf(add_command, "msr%d", &msr_num) == 1)
6516 if (*add_command == '/') {
6521 if (sscanf(add_command, "u%d", &width) == 1) {
6522 if ((width == 32) || (width == 64))
6526 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
6530 if (!strncmp(add_command, "core", strlen("core"))) {
6534 if (!strncmp(add_command, "package", strlen("package"))) {
6535 scope = SCOPE_PACKAGE;
6538 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
6539 type = COUNTER_CYCLES;
6542 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
6543 type = COUNTER_SECONDS;
6546 if (!strncmp(add_command, "usec", strlen("usec"))) {
6547 type = COUNTER_USEC;
6550 if (!strncmp(add_command, "raw", strlen("raw"))) {
6551 format = FORMAT_RAW;
6554 if (!strncmp(add_command, "delta", strlen("delta"))) {
6555 format = FORMAT_DELTA;
6558 if (!strncmp(add_command, "percent", strlen("percent"))) {
6559 format = FORMAT_PERCENT;
6563 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
6566 eos = strchr(name_buffer, ',');
6573 add_command = strchr(add_command, ',');
6575 *add_command = '\0';
6580 if ((msr_num == 0) && (path == NULL)) {
6581 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
6585 /* generate default column header */
6586 if (*name_buffer == '\0') {
6588 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6590 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6593 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
6602 int is_deferred_add(char *name)
6606 for (i = 0; i < deferred_add_index; ++i)
6607 if (!strcmp(name, deferred_add_names[i]))
6612 int is_deferred_skip(char *name)
6616 for (i = 0; i < deferred_skip_index; ++i)
6617 if (!strcmp(name, deferred_skip_names[i]))
6622 void probe_sysfs(void)
6630 for (state = 10; state >= 0; --state) {
6632 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
6633 input = fopen(path, "r");
6636 if (!fgets(name_buf, sizeof(name_buf), input))
6637 err(1, "%s: failed to read file", path);
6639 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6640 sp = strchr(name_buf, '-');
6642 sp = strchrnul(name_buf, '\n');
6646 remove_underbar(name_buf);
6650 sprintf(path, "cpuidle/state%d/time", state);
6652 if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
6655 if (is_deferred_skip(name_buf))
6658 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU);
6661 for (state = 10; state >= 0; --state) {
6663 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
6664 input = fopen(path, "r");
6667 if (!fgets(name_buf, sizeof(name_buf), input))
6668 err(1, "%s: failed to read file", path);
6669 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
6670 sp = strchr(name_buf, '-');
6672 sp = strchrnul(name_buf, '\n');
6676 remove_underbar(name_buf);
6678 sprintf(path, "cpuidle/state%d/usage", state);
6680 if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
6683 if (is_deferred_skip(name_buf))
6686 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU);
6692 * parse cpuset with following syntax
6693 * 1,2,4..6,8-10 and set bits in cpu_subset
6695 void parse_cpu_command(char *optarg)
6697 unsigned int start, end;
6700 if (!strcmp(optarg, "core")) {
6706 if (!strcmp(optarg, "package")) {
6712 if (show_core_only || show_pkg_only)
6715 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
6716 if (cpu_subset == NULL)
6717 err(3, "CPU_ALLOC");
6718 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
6720 CPU_ZERO_S(cpu_subset_size, cpu_subset);
6724 while (next && *next) {
6726 if (*next == '-') /* no negative cpu numbers */
6729 start = strtoul(next, &next, 10);
6731 if (start >= CPU_SUBSET_MAXCPUS)
6733 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6744 next += 1; /* start range */
6745 } else if (*next == '.') {
6748 next += 1; /* start range */
6753 end = strtoul(next, &next, 10);
6757 while (++start <= end) {
6758 if (start >= CPU_SUBSET_MAXCPUS)
6760 CPU_SET_S(start, cpu_subset_size, cpu_subset);
6765 else if (*next != '\0')
6772 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
6777 void cmdline(int argc, char **argv)
6780 int option_index = 0;
6781 static struct option long_options[] = {
6782 { "add", required_argument, 0, 'a' },
6783 { "cpu", required_argument, 0, 'c' },
6784 { "Dump", no_argument, 0, 'D' },
6785 { "debug", no_argument, 0, 'd' }, /* internal, not documented */
6786 { "enable", required_argument, 0, 'e' },
6787 { "interval", required_argument, 0, 'i' },
6788 { "IPC", no_argument, 0, 'I' },
6789 { "num_iterations", required_argument, 0, 'n' },
6790 { "header_iterations", required_argument, 0, 'N' },
6791 { "help", no_argument, 0, 'h' },
6792 { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help
6793 { "Joules", no_argument, 0, 'J' },
6794 { "list", no_argument, 0, 'l' },
6795 { "out", required_argument, 0, 'o' },
6796 { "quiet", no_argument, 0, 'q' },
6797 { "show", required_argument, 0, 's' },
6798 { "Summary", no_argument, 0, 'S' },
6799 { "TCC", required_argument, 0, 'T' },
6800 { "version", no_argument, 0, 'v' },
6806 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) {
6809 parse_add_command(optarg);
6812 parse_cpu_command(optarg);
6818 /* --enable specified counter */
6819 bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
6823 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6827 * --hide: do not show those specified
6828 * multiple invocations simply clear more bits in enabled mask
6830 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
6838 double interval = strtod(optarg, NULL);
6840 if (interval < 0.001) {
6841 fprintf(outf, "interval %f seconds is too small\n", interval);
6845 interval_tv.tv_sec = interval_ts.tv_sec = interval;
6846 interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
6847 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
6854 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6859 outf = fopen_or_die(optarg, "w");
6865 num_iterations = strtod(optarg, NULL);
6867 if (num_iterations <= 0) {
6868 fprintf(outf, "iterations %d should be positive number\n", num_iterations);
6873 header_iterations = strtod(optarg, NULL);
6875 if (header_iterations <= 0) {
6876 fprintf(outf, "iterations %d should be positive number\n", header_iterations);
6882 * --show: show only those specified
6883 * The 1st invocation will clear and replace the enabled mask
6884 * subsequent invocations can add to it.
6887 bic_enabled = bic_lookup(optarg, SHOW_LIST);
6889 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
6896 tj_max_override = atoi(optarg);
6906 int main(int argc, char **argv)
6909 cmdline(argc, argv);
6922 /* dump counters and exit */
6924 return get_and_dump_counters();
6926 /* list header and exit */
6927 if (list_header_only) {
6929 flush_output_stdout();
6934 * if any params left, it must be a command to fork
6937 return fork_it(argv + optind);