1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2103a8feaSLen Brown /* 3103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 434041551SLen Brown * on modern Intel and AMD processors. 5103a8feaSLen Brown * 6de7839eeSLen Brown * Copyright (c) 2023 Intel Corporation. 7103a8feaSLen Brown * Len Brown <len.brown@intel.com> 8103a8feaSLen Brown */ 9103a8feaSLen Brown 1088c3281fSLen Brown #define _GNU_SOURCE 11b731f311SJosh Triplett #include MSRHEADER 12869ce69eSLen Brown #include INTEL_FAMILY_HEADER 1395aebc44SJosh Triplett #include <stdarg.h> 14103a8feaSLen Brown #include <stdio.h> 15b2c95d90SJosh Triplett #include <err.h> 16103a8feaSLen Brown #include <unistd.h> 17103a8feaSLen Brown #include <sys/types.h> 18103a8feaSLen Brown #include <sys/wait.h> 19103a8feaSLen Brown #include <sys/stat.h> 20b9ad8ee0SLen Brown #include <sys/select.h> 21103a8feaSLen Brown #include <sys/resource.h> 22103a8feaSLen Brown #include <fcntl.h> 23103a8feaSLen Brown #include <signal.h> 24103a8feaSLen Brown #include <sys/time.h> 25103a8feaSLen Brown #include <stdlib.h> 26d8af6f5fSLen Brown #include <getopt.h> 27103a8feaSLen Brown #include <dirent.h> 28103a8feaSLen Brown #include <string.h> 29103a8feaSLen Brown #include <ctype.h> 3088c3281fSLen Brown #include <sched.h> 312a0609c0SLen Brown #include <time.h> 322b92865eSJosh Triplett #include <cpuid.h> 33fcaa681cSLen Brown #include <sys/capability.h> 3498481e79SLen Brown #include <errno.h> 359392bd98SCalvin Walton #include <math.h> 362af4f9b8SLen Brown #include <linux/perf_event.h> 372af4f9b8SLen Brown #include <asm/unistd.h> 387ab5ff49SZhang Rui #include <stdbool.h> 39103a8feaSLen Brown 409878bf7aSLen Brown #define UNUSED(x) (void)(x) 419878bf7aSLen Brown 42164d7a96SLen Brown /* 43164d7a96SLen Brown * This list matches the column headers, except 44164d7a96SLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 45164d7a96SLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 46164d7a96SLen Brown * matching on them for --show and --hide. 47164d7a96SLen Brown */ 48164d7a96SLen Brown 49164d7a96SLen Brown /* 50164d7a96SLen Brown * buffer size used by sscanf() for added column names 51164d7a96SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 52164d7a96SLen Brown */ 53164d7a96SLen Brown #define NAME_BYTES 20 54164d7a96SLen Brown #define PATH_BYTES 128 55164d7a96SLen Brown 5652b3541bSWyes Karny #define MAX_NOFILE 0x8000 5752b3541bSWyes Karny 58164d7a96SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE }; 59164d7a96SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC }; 60164d7a96SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT }; 61164d7a96SLen Brown 62164d7a96SLen Brown struct msr_counter { 63164d7a96SLen Brown unsigned int msr_num; 64164d7a96SLen Brown char name[NAME_BYTES]; 65164d7a96SLen Brown char path[PATH_BYTES]; 66164d7a96SLen Brown unsigned int width; 67164d7a96SLen Brown enum counter_type type; 68164d7a96SLen Brown enum counter_format format; 69164d7a96SLen Brown struct msr_counter *next; 70164d7a96SLen Brown unsigned int flags; 71164d7a96SLen Brown #define FLAGS_HIDE (1 << 0) 72164d7a96SLen Brown #define FLAGS_SHOW (1 << 1) 73164d7a96SLen Brown #define SYSFS_PERCPU (1 << 1) 74164d7a96SLen Brown }; 75164d7a96SLen Brown 76164d7a96SLen Brown struct msr_counter bic[] = { 779878bf7aSLen Brown { 0x0, "usec", "", 0, 0, 0, NULL, 0 }, 789878bf7aSLen Brown { 0x0, "Time_Of_Day_Seconds", "", 0, 0, 0, NULL, 0 }, 799878bf7aSLen Brown { 0x0, "Package", "", 0, 0, 0, NULL, 0 }, 809878bf7aSLen Brown { 0x0, "Node", "", 0, 0, 0, NULL, 0 }, 819878bf7aSLen Brown { 0x0, "Avg_MHz", "", 0, 0, 0, NULL, 0 }, 829878bf7aSLen Brown { 0x0, "Busy%", "", 0, 0, 0, NULL, 0 }, 839878bf7aSLen Brown { 0x0, "Bzy_MHz", "", 0, 0, 0, NULL, 0 }, 849878bf7aSLen Brown { 0x0, "TSC_MHz", "", 0, 0, 0, NULL, 0 }, 859878bf7aSLen Brown { 0x0, "IRQ", "", 0, 0, 0, NULL, 0 }, 869878bf7aSLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL, 0 }, 879878bf7aSLen Brown { 0x0, "sysfs", "", 0, 0, 0, NULL, 0 }, 889878bf7aSLen Brown { 0x0, "CPU%c1", "", 0, 0, 0, NULL, 0 }, 899878bf7aSLen Brown { 0x0, "CPU%c3", "", 0, 0, 0, NULL, 0 }, 909878bf7aSLen Brown { 0x0, "CPU%c6", "", 0, 0, 0, NULL, 0 }, 919878bf7aSLen Brown { 0x0, "CPU%c7", "", 0, 0, 0, NULL, 0 }, 929878bf7aSLen Brown { 0x0, "ThreadC", "", 0, 0, 0, NULL, 0 }, 939878bf7aSLen Brown { 0x0, "CoreTmp", "", 0, 0, 0, NULL, 0 }, 949878bf7aSLen Brown { 0x0, "CoreCnt", "", 0, 0, 0, NULL, 0 }, 959878bf7aSLen Brown { 0x0, "PkgTmp", "", 0, 0, 0, NULL, 0 }, 969878bf7aSLen Brown { 0x0, "GFX%rc6", "", 0, 0, 0, NULL, 0 }, 979878bf7aSLen Brown { 0x0, "GFXMHz", "", 0, 0, 0, NULL, 0 }, 989878bf7aSLen Brown { 0x0, "Pkg%pc2", "", 0, 0, 0, NULL, 0 }, 999878bf7aSLen Brown { 0x0, "Pkg%pc3", "", 0, 0, 0, NULL, 0 }, 1009878bf7aSLen Brown { 0x0, "Pkg%pc6", "", 0, 0, 0, NULL, 0 }, 1019878bf7aSLen Brown { 0x0, "Pkg%pc7", "", 0, 0, 0, NULL, 0 }, 1029878bf7aSLen Brown { 0x0, "Pkg%pc8", "", 0, 0, 0, NULL, 0 }, 1039878bf7aSLen Brown { 0x0, "Pkg%pc9", "", 0, 0, 0, NULL, 0 }, 1049878bf7aSLen Brown { 0x0, "Pk%pc10", "", 0, 0, 0, NULL, 0 }, 1059878bf7aSLen Brown { 0x0, "CPU%LPI", "", 0, 0, 0, NULL, 0 }, 1069878bf7aSLen Brown { 0x0, "SYS%LPI", "", 0, 0, 0, NULL, 0 }, 1079878bf7aSLen Brown { 0x0, "PkgWatt", "", 0, 0, 0, NULL, 0 }, 1089878bf7aSLen Brown { 0x0, "CorWatt", "", 0, 0, 0, NULL, 0 }, 1099878bf7aSLen Brown { 0x0, "GFXWatt", "", 0, 0, 0, NULL, 0 }, 1109878bf7aSLen Brown { 0x0, "PkgCnt", "", 0, 0, 0, NULL, 0 }, 1119878bf7aSLen Brown { 0x0, "RAMWatt", "", 0, 0, 0, NULL, 0 }, 1129878bf7aSLen Brown { 0x0, "PKG_%", "", 0, 0, 0, NULL, 0 }, 1139878bf7aSLen Brown { 0x0, "RAM_%", "", 0, 0, 0, NULL, 0 }, 1149878bf7aSLen Brown { 0x0, "Pkg_J", "", 0, 0, 0, NULL, 0 }, 1159878bf7aSLen Brown { 0x0, "Cor_J", "", 0, 0, 0, NULL, 0 }, 1169878bf7aSLen Brown { 0x0, "GFX_J", "", 0, 0, 0, NULL, 0 }, 1179878bf7aSLen Brown { 0x0, "RAM_J", "", 0, 0, 0, NULL, 0 }, 1189878bf7aSLen Brown { 0x0, "Mod%c6", "", 0, 0, 0, NULL, 0 }, 1199878bf7aSLen Brown { 0x0, "Totl%C0", "", 0, 0, 0, NULL, 0 }, 1209878bf7aSLen Brown { 0x0, "Any%C0", "", 0, 0, 0, NULL, 0 }, 1219878bf7aSLen Brown { 0x0, "GFX%C0", "", 0, 0, 0, NULL, 0 }, 1229878bf7aSLen Brown { 0x0, "CPUGFX%", "", 0, 0, 0, NULL, 0 }, 1239878bf7aSLen Brown { 0x0, "Core", "", 0, 0, 0, NULL, 0 }, 1249878bf7aSLen Brown { 0x0, "CPU", "", 0, 0, 0, NULL, 0 }, 1259878bf7aSLen Brown { 0x0, "APIC", "", 0, 0, 0, NULL, 0 }, 1269878bf7aSLen Brown { 0x0, "X2APIC", "", 0, 0, 0, NULL, 0 }, 1279878bf7aSLen Brown { 0x0, "Die", "", 0, 0, 0, NULL, 0 }, 1289878bf7aSLen Brown { 0x0, "GFXAMHz", "", 0, 0, 0, NULL, 0 }, 1299878bf7aSLen Brown { 0x0, "IPC", "", 0, 0, 0, NULL, 0 }, 1309878bf7aSLen Brown { 0x0, "CoreThr", "", 0, 0, 0, NULL, 0 }, 131a5c6d65dSLen Brown { 0x0, "UncMHz", "", 0, 0, 0, NULL, 0 }, 132164d7a96SLen Brown }; 133164d7a96SLen Brown 134164d7a96SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 135164d7a96SLen Brown #define BIC_USEC (1ULL << 0) 136164d7a96SLen Brown #define BIC_TOD (1ULL << 1) 137164d7a96SLen Brown #define BIC_Package (1ULL << 2) 138164d7a96SLen Brown #define BIC_Node (1ULL << 3) 139164d7a96SLen Brown #define BIC_Avg_MHz (1ULL << 4) 140164d7a96SLen Brown #define BIC_Busy (1ULL << 5) 141164d7a96SLen Brown #define BIC_Bzy_MHz (1ULL << 6) 142164d7a96SLen Brown #define BIC_TSC_MHz (1ULL << 7) 143164d7a96SLen Brown #define BIC_IRQ (1ULL << 8) 144164d7a96SLen Brown #define BIC_SMI (1ULL << 9) 145164d7a96SLen Brown #define BIC_sysfs (1ULL << 10) 146164d7a96SLen Brown #define BIC_CPU_c1 (1ULL << 11) 147164d7a96SLen Brown #define BIC_CPU_c3 (1ULL << 12) 148164d7a96SLen Brown #define BIC_CPU_c6 (1ULL << 13) 149164d7a96SLen Brown #define BIC_CPU_c7 (1ULL << 14) 150164d7a96SLen Brown #define BIC_ThreadC (1ULL << 15) 151164d7a96SLen Brown #define BIC_CoreTmp (1ULL << 16) 152164d7a96SLen Brown #define BIC_CoreCnt (1ULL << 17) 153164d7a96SLen Brown #define BIC_PkgTmp (1ULL << 18) 154164d7a96SLen Brown #define BIC_GFX_rc6 (1ULL << 19) 155164d7a96SLen Brown #define BIC_GFXMHz (1ULL << 20) 156164d7a96SLen Brown #define BIC_Pkgpc2 (1ULL << 21) 157164d7a96SLen Brown #define BIC_Pkgpc3 (1ULL << 22) 158164d7a96SLen Brown #define BIC_Pkgpc6 (1ULL << 23) 159164d7a96SLen Brown #define BIC_Pkgpc7 (1ULL << 24) 160164d7a96SLen Brown #define BIC_Pkgpc8 (1ULL << 25) 161164d7a96SLen Brown #define BIC_Pkgpc9 (1ULL << 26) 162164d7a96SLen Brown #define BIC_Pkgpc10 (1ULL << 27) 163164d7a96SLen Brown #define BIC_CPU_LPI (1ULL << 28) 164164d7a96SLen Brown #define BIC_SYS_LPI (1ULL << 29) 165164d7a96SLen Brown #define BIC_PkgWatt (1ULL << 30) 166164d7a96SLen Brown #define BIC_CorWatt (1ULL << 31) 167164d7a96SLen Brown #define BIC_GFXWatt (1ULL << 32) 168164d7a96SLen Brown #define BIC_PkgCnt (1ULL << 33) 169164d7a96SLen Brown #define BIC_RAMWatt (1ULL << 34) 170164d7a96SLen Brown #define BIC_PKG__ (1ULL << 35) 171164d7a96SLen Brown #define BIC_RAM__ (1ULL << 36) 172164d7a96SLen Brown #define BIC_Pkg_J (1ULL << 37) 173164d7a96SLen Brown #define BIC_Cor_J (1ULL << 38) 174164d7a96SLen Brown #define BIC_GFX_J (1ULL << 39) 175164d7a96SLen Brown #define BIC_RAM_J (1ULL << 40) 176164d7a96SLen Brown #define BIC_Mod_c6 (1ULL << 41) 177164d7a96SLen Brown #define BIC_Totl_c0 (1ULL << 42) 178164d7a96SLen Brown #define BIC_Any_c0 (1ULL << 43) 179164d7a96SLen Brown #define BIC_GFX_c0 (1ULL << 44) 180164d7a96SLen Brown #define BIC_CPUGFX (1ULL << 45) 181164d7a96SLen Brown #define BIC_Core (1ULL << 46) 182164d7a96SLen Brown #define BIC_CPU (1ULL << 47) 183164d7a96SLen Brown #define BIC_APIC (1ULL << 48) 184164d7a96SLen Brown #define BIC_X2APIC (1ULL << 49) 185164d7a96SLen Brown #define BIC_Die (1ULL << 50) 186164d7a96SLen Brown #define BIC_GFXACTMHz (1ULL << 51) 187164d7a96SLen Brown #define BIC_IPC (1ULL << 52) 188164d7a96SLen Brown #define BIC_CORE_THROT_CNT (1ULL << 53) 189a5c6d65dSLen Brown #define BIC_UNCORE_MHZ (1ULL << 54) 190164d7a96SLen Brown 191164d7a96SLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die ) 192164d7a96SLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__) 193a5c6d65dSLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz | BIC_UNCORE_MHZ) 194164d7a96SLen Brown #define BIC_IDLE ( BIC_sysfs | BIC_CPU_c1 | BIC_CPU_c3 | BIC_CPU_c6 | BIC_CPU_c7 | BIC_GFX_rc6 | BIC_Pkgpc2 | BIC_Pkgpc3 | BIC_Pkgpc6 | BIC_Pkgpc7 | BIC_Pkgpc8 | BIC_Pkgpc9 | BIC_Pkgpc10 | BIC_CPU_LPI | BIC_SYS_LPI | BIC_Mod_c6 | BIC_Totl_c0 | BIC_Any_c0 | BIC_GFX_c0 | BIC_CPUGFX) 195164d7a96SLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC) 196164d7a96SLen Brown 197164d7a96SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 198164d7a96SLen Brown 199164d7a96SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 200164d7a96SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 201164d7a96SLen Brown 202164d7a96SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 203164d7a96SLen Brown #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 204164d7a96SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 205164d7a96SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 206164d7a96SLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 207164d7a96SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 208164d7a96SLen Brown 209103a8feaSLen Brown char *proc_stat = "/proc/stat"; 210b7d8c148SLen Brown FILE *outf; 21136229897SLen Brown int *fd_percpu; 2122af4f9b8SLen Brown int *fd_instr_count_percpu; 213b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 }; 21447936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 }; 215e9d3092fSZhang Rui 216e9d3092fSZhang Rui /* Save original CPU model */ 217e9d3092fSZhang Rui unsigned int model_orig; 218e9d3092fSZhang Rui 219023fe0acSChen Yu unsigned int num_iterations; 220c7e399f8SZephaniah E. Loss-Cutler-Hull unsigned int header_iterations; 221d8af6f5fSLen Brown unsigned int debug; 22296e47158SLen Brown unsigned int quiet; 2233f44a5c6SLen Brown unsigned int shown; 2240de6c0dfSLen Brown unsigned int sums_need_wide_columns; 225d8af6f5fSLen Brown unsigned int rapl_joules; 226d8af6f5fSLen Brown unsigned int summary_only; 227c8ade361SLen Brown unsigned int list_header_only; 228d8af6f5fSLen Brown unsigned int dump_only; 229103a8feaSLen Brown unsigned int do_snb_cstates; 230fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 231144b44b1SLen Brown unsigned int do_slm_cstates; 232144b44b1SLen Brown unsigned int use_c1_residency_msr; 233103a8feaSLen Brown unsigned int has_aperf; 234889facbeSLen Brown unsigned int has_epb; 2353ea8e52eSArtem Bityutskiy unsigned int has_turbo; 236774627c5SLen Brown unsigned int is_hybrid; 2375a63426eSLen Brown unsigned int do_irtl_snb; 2385a63426eSLen Brown unsigned int do_irtl_hsw; 239fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 240103a8feaSLen Brown unsigned int genuine_intel; 24134041551SLen Brown unsigned int authentic_amd; 242c1c10cc7SPu Wen unsigned int hygon_genuine; 24334041551SLen Brown unsigned int max_level, max_extended_level; 244103a8feaSLen Brown unsigned int has_invariant_tsc; 245d7899447SLen Brown unsigned int do_nhm_platform_info; 246cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 247b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 248103a8feaSLen Brown double bclk; 249a2b7b749SLen Brown double base_hz; 25021ed5574SLen Brown unsigned int has_base_hz; 251a2b7b749SLen Brown double tsc_tweak = 1.0; 252c98d5d94SLen Brown unsigned int show_pkg_only; 253c98d5d94SLen Brown unsigned int show_core_only; 254c98d5d94SLen Brown char *output_buffer, *outp; 255889facbeSLen Brown unsigned int do_rapl; 256889facbeSLen Brown unsigned int do_dts; 257889facbeSLen Brown unsigned int do_ptm; 2582af4f9b8SLen Brown unsigned int do_ipc; 259fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 260be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 261be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 26227d47356SLen Brown unsigned int gfx_cur_mhz; 263b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 26455279aefSZhang Rui unsigned int tj_max; 26555279aefSZhang Rui unsigned int tj_max_override; 2660b9a0b9bSZhang Rui int tcc_offset_bits; 26740ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 26840ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 269889facbeSLen Brown double rapl_joule_counter_range; 2703a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 271ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 272aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 2733a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 2743a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 2758a5bdf41SLen Brown unsigned int crystal_hz; 2768a5bdf41SLen Brown unsigned long long tsc_hz; 2777ce7d5deSPrarit Bhargava int base_cpu; 27821ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 2797f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 2807f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 2817f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 2827f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 2837f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 2847f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 28533148d67SLen Brown unsigned int has_misc_feature_control; 2864c2122d4SLen Brown unsigned int first_counter_read = 1; 287c026c236SArtem Bityutskiy int ignore_stdin; 288889facbeSLen Brown 289889facbeSLen Brown #define RAPL_PKG (1 << 0) 290e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 291e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 292e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 293e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 294e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 295e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 296e6f9bb3cSLen Brown 297889facbeSLen Brown #define RAPL_DRAM (1 << 3) 298e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 299e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 300e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 301e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 3020b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 3030b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 304e6f9bb3cSLen Brown 3059148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 306e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 3070b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 308e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 309e6f9bb3cSLen Brown 3100b2bb692SLen Brown #define RAPL_GFX (1 << 8) 311e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 312e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 313e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 3149148494cSJacob Pan 3159148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 3169148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 3179392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 3189392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 3199392bd98SCalvin Walton * not per-package. */ 3209392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 3219392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 3229392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 3239392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 3249148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 325889facbeSLen Brown #define TJMAX_DEFAULT 100 326889facbeSLen Brown 3279392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 3289392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 3299392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 3309392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 3319392bd98SCalvin Walton 332889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 333103a8feaSLen Brown 334103a8feaSLen Brown int backwards_count; 335103a8feaSLen Brown char *progname; 336103a8feaSLen Brown 3371ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 3381ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 3391ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 3400748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 3410748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 3420e2d8f05SLen Brown #define BITMASK_SIZE 32 343c98d5d94SLen Brown 344c98d5d94SLen Brown struct thread_data { 345f4fdf2b4SLen Brown struct timeval tv_begin; 346f4fdf2b4SLen Brown struct timeval tv_end; 347d4794f25SYazen Ghannam struct timeval tv_delta; 348c98d5d94SLen Brown unsigned long long tsc; 349c98d5d94SLen Brown unsigned long long aperf; 350c98d5d94SLen Brown unsigned long long mperf; 351144b44b1SLen Brown unsigned long long c1; 3522af4f9b8SLen Brown unsigned long long instr_count; 3530de6c0dfSLen Brown unsigned long long irq_count; 3541ed51011SLen Brown unsigned int smi_count; 355c98d5d94SLen Brown unsigned int cpu_id; 3564c2122d4SLen Brown unsigned int apic_id; 3574c2122d4SLen Brown unsigned int x2apic_id; 358c98d5d94SLen Brown unsigned int flags; 3597ab5ff49SZhang Rui bool is_atom; 360c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 361c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 3620748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 363c98d5d94SLen Brown } *thread_even, *thread_odd; 364c98d5d94SLen Brown 365c98d5d94SLen Brown struct core_data { 366c98d5d94SLen Brown unsigned long long c3; 367c98d5d94SLen Brown unsigned long long c6; 368c98d5d94SLen Brown unsigned long long c7; 3690539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 370889facbeSLen Brown unsigned int core_temp_c; 3719392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 372c98d5d94SLen Brown unsigned int core_id; 373eae97e05SChen Yu unsigned long long core_throt_cnt; 374678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 375c98d5d94SLen Brown } *core_even, *core_odd; 376c98d5d94SLen Brown 377c98d5d94SLen Brown struct pkg_data { 378c98d5d94SLen Brown unsigned long long pc2; 379c98d5d94SLen Brown unsigned long long pc3; 380c98d5d94SLen Brown unsigned long long pc6; 381c98d5d94SLen Brown unsigned long long pc7; 382ca58710fSKristen Carlson Accardi unsigned long long pc8; 383ca58710fSKristen Carlson Accardi unsigned long long pc9; 384ca58710fSKristen Carlson Accardi unsigned long long pc10; 385be0e54c4SLen Brown unsigned long long cpu_lpi; 386be0e54c4SLen Brown unsigned long long sys_lpi; 3870b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 3880b2bb692SLen Brown unsigned long long pkg_any_core_c0; 3890b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 3900b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 3919185e988SLen Brown long long gfx_rc6_ms; 39227d47356SLen Brown unsigned int gfx_mhz; 393b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 394c98d5d94SLen Brown unsigned int package_id; 3957c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 3967c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 3977c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 3987c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 3997c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 4007c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 401889facbeSLen Brown unsigned int pkg_temp_c; 402a5c6d65dSLen Brown unsigned int uncore_mhz; 403678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 404c98d5d94SLen Brown } *package_even, *package_odd; 405c98d5d94SLen Brown 406c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 407c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 408c98d5d94SLen Brown 40940f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 41040f5cfe7SPrarit Bhargava ((thread_base) + \ 41140f5cfe7SPrarit Bhargava ((pkg_no) * \ 41240f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 41340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 41440f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 41540f5cfe7SPrarit Bhargava (thread_no)) 41640f5cfe7SPrarit Bhargava 41740f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 41840f5cfe7SPrarit Bhargava ((core_base) + \ 41940f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 42040f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 42140f5cfe7SPrarit Bhargava (core_no)) 42240f5cfe7SPrarit Bhargava 423c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 424c98d5d94SLen Brown 42587e15da9SChen Yu /* 42687e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 42787e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 42887e15da9SChen Yu * despite its register's bit width. 42987e15da9SChen Yu */ 43087e15da9SChen Yu enum { 43187e15da9SChen Yu IDX_PKG_ENERGY, 43287e15da9SChen Yu IDX_DRAM_ENERGY, 43387e15da9SChen Yu IDX_PP0_ENERGY, 43487e15da9SChen Yu IDX_PP1_ENERGY, 43587e15da9SChen Yu IDX_PKG_PERF, 43687e15da9SChen Yu IDX_DRAM_PERF, 43787e15da9SChen Yu IDX_COUNT, 43887e15da9SChen Yu }; 43987e15da9SChen Yu 44087e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 44187e15da9SChen Yu 44287e15da9SChen Yu struct msr_sum_array { 44387e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 44487e15da9SChen Yu struct { 44587e15da9SChen Yu /*The accumulated MSR value is updated by the timer */ 44687e15da9SChen Yu unsigned long long sum; 44787e15da9SChen Yu /*The MSR footprint recorded in last timer */ 44887e15da9SChen Yu unsigned long long last; 44987e15da9SChen Yu } entries[IDX_COUNT]; 45087e15da9SChen Yu }; 45187e15da9SChen Yu 45287e15da9SChen Yu /* The percpu MSR sum array.*/ 45387e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 45487e15da9SChen Yu 45513a779deSCalvin Walton off_t idx_to_offset(int idx) 45687e15da9SChen Yu { 45713a779deSCalvin Walton off_t offset; 45887e15da9SChen Yu 45987e15da9SChen Yu switch (idx) { 46087e15da9SChen Yu case IDX_PKG_ENERGY: 461301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H) 462301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT; 463301b1d3aSBas Nieuwenhuizen else 46487e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 46587e15da9SChen Yu break; 46687e15da9SChen Yu case IDX_DRAM_ENERGY: 46787e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 46887e15da9SChen Yu break; 46987e15da9SChen Yu case IDX_PP0_ENERGY: 47087e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 47187e15da9SChen Yu break; 47287e15da9SChen Yu case IDX_PP1_ENERGY: 47387e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 47487e15da9SChen Yu break; 47587e15da9SChen Yu case IDX_PKG_PERF: 47687e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 47787e15da9SChen Yu break; 47887e15da9SChen Yu case IDX_DRAM_PERF: 47987e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 48087e15da9SChen Yu break; 48187e15da9SChen Yu default: 48287e15da9SChen Yu offset = -1; 48387e15da9SChen Yu } 48487e15da9SChen Yu return offset; 48587e15da9SChen Yu } 48687e15da9SChen Yu 48713a779deSCalvin Walton int offset_to_idx(off_t offset) 48887e15da9SChen Yu { 48987e15da9SChen Yu int idx; 49087e15da9SChen Yu 49187e15da9SChen Yu switch (offset) { 49287e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 493301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT: 49487e15da9SChen Yu idx = IDX_PKG_ENERGY; 49587e15da9SChen Yu break; 49687e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 49787e15da9SChen Yu idx = IDX_DRAM_ENERGY; 49887e15da9SChen Yu break; 49987e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 50087e15da9SChen Yu idx = IDX_PP0_ENERGY; 50187e15da9SChen Yu break; 50287e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 50387e15da9SChen Yu idx = IDX_PP1_ENERGY; 50487e15da9SChen Yu break; 50587e15da9SChen Yu case MSR_PKG_PERF_STATUS: 50687e15da9SChen Yu idx = IDX_PKG_PERF; 50787e15da9SChen Yu break; 50887e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 50987e15da9SChen Yu idx = IDX_DRAM_PERF; 51087e15da9SChen Yu break; 51187e15da9SChen Yu default: 51287e15da9SChen Yu idx = -1; 51387e15da9SChen Yu } 51487e15da9SChen Yu return idx; 51587e15da9SChen Yu } 51687e15da9SChen Yu 51787e15da9SChen Yu int idx_valid(int idx) 51887e15da9SChen Yu { 51987e15da9SChen Yu switch (idx) { 52087e15da9SChen Yu case IDX_PKG_ENERGY: 521301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H); 52287e15da9SChen Yu case IDX_DRAM_ENERGY: 52387e15da9SChen Yu return do_rapl & RAPL_DRAM; 52487e15da9SChen Yu case IDX_PP0_ENERGY: 52587e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 52687e15da9SChen Yu case IDX_PP1_ENERGY: 52787e15da9SChen Yu return do_rapl & RAPL_GFX; 52887e15da9SChen Yu case IDX_PKG_PERF: 52987e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 53087e15da9SChen Yu case IDX_DRAM_PERF: 53187e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 53287e15da9SChen Yu default: 53387e15da9SChen Yu return 0; 53487e15da9SChen Yu } 53587e15da9SChen Yu } 5361b439f01SLen Brown 537388e9c81SLen Brown struct sys_counters { 538678a3bd1SLen Brown unsigned int added_thread_counters; 539678a3bd1SLen Brown unsigned int added_core_counters; 540678a3bd1SLen Brown unsigned int added_package_counters; 541388e9c81SLen Brown struct msr_counter *tp; 542388e9c81SLen Brown struct msr_counter *cp; 543388e9c81SLen Brown struct msr_counter *pp; 544388e9c81SLen Brown } sys; 545388e9c81SLen Brown 546c98d5d94SLen Brown struct system_summary { 547c98d5d94SLen Brown struct thread_data threads; 548c98d5d94SLen Brown struct core_data cores; 549c98d5d94SLen Brown struct pkg_data packages; 550388e9c81SLen Brown } average; 551c98d5d94SLen Brown 5520e2d8f05SLen Brown struct cpu_topology { 5530e2d8f05SLen Brown int physical_package_id; 5546de68fe1SLen Brown int die_id; 5550e2d8f05SLen Brown int logical_cpu_id; 556ef605741SPrarit Bhargava int physical_node_id; 557ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 5580e2d8f05SLen Brown int physical_core_id; 5598cb48b32SPrarit Bhargava int thread_id; 5600e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 5610e2d8f05SLen Brown } *cpus; 562c98d5d94SLen Brown 563c98d5d94SLen Brown struct topo_params { 564c98d5d94SLen Brown int num_packages; 5656de68fe1SLen Brown int num_die; 566103a8feaSLen Brown int num_cpus; 567c98d5d94SLen Brown int num_cores; 568c98d5d94SLen Brown int max_cpu_num; 569ef605741SPrarit Bhargava int max_node_num; 57070a9c6e8SPrarit Bhargava int nodes_per_pkg; 57170a9c6e8SPrarit Bhargava int cores_per_node; 57270a9c6e8SPrarit Bhargava int threads_per_core; 573c98d5d94SLen Brown } topo; 574103a8feaSLen Brown 575c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 576103a8feaSLen Brown 577562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 578562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 579562a2d37SLen Brown 580c98d5d94SLen Brown void setup_all_buffers(void); 581103a8feaSLen Brown 5821f81c5efSLen Brown char *sys_lpi_file; 5831f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 5841f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 5851f81c5efSLen Brown 586c98d5d94SLen Brown int cpu_is_not_present(int cpu) 587d15cf7c1SLen Brown { 588c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 589c98d5d94SLen Brown } 5901b439f01SLen Brown 591c98d5d94SLen Brown /* 592c98d5d94SLen Brown * run func(thread, core, package) in topology order 593c98d5d94SLen Brown * skip non-present cpus 594c98d5d94SLen Brown */ 595c98d5d94SLen Brown 596c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), 597c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 598c98d5d94SLen Brown { 59940f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 600c98d5d94SLen Brown 601c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 602df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 60370a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 6041b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 605c98d5d94SLen Brown struct thread_data *t; 606c98d5d94SLen Brown struct core_data *c; 607c98d5d94SLen Brown struct pkg_data *p; 608c98d5d94SLen Brown 6091b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 610c98d5d94SLen Brown 611c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 612c98d5d94SLen Brown continue; 613c98d5d94SLen Brown 6141b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 615c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 616c98d5d94SLen Brown 617c98d5d94SLen Brown retval = func(t, c, p); 618c98d5d94SLen Brown if (retval) 619c98d5d94SLen Brown return retval; 620c98d5d94SLen Brown } 621c98d5d94SLen Brown } 622c98d5d94SLen Brown } 62340f5cfe7SPrarit Bhargava } 624d15cf7c1SLen Brown return 0; 625d15cf7c1SLen Brown } 626d15cf7c1SLen Brown 62788c3281fSLen Brown int cpu_migrate(int cpu) 62888c3281fSLen Brown { 629c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 630c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 631c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 63288c3281fSLen Brown return -1; 63388c3281fSLen Brown else 63488c3281fSLen Brown return 0; 63588c3281fSLen Brown } 6361b439f01SLen Brown 63736229897SLen Brown int get_msr_fd(int cpu) 638103a8feaSLen Brown { 639103a8feaSLen Brown char pathname[32]; 640103a8feaSLen Brown int fd; 641103a8feaSLen Brown 64236229897SLen Brown fd = fd_percpu[cpu]; 64336229897SLen Brown 64436229897SLen Brown if (fd) 64536229897SLen Brown return fd; 64636229897SLen Brown 647103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 648103a8feaSLen Brown fd = open(pathname, O_RDONLY); 64915aaa346SLen Brown if (fd < 0) 65098481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 651103a8feaSLen Brown 65236229897SLen Brown fd_percpu[cpu] = fd; 65336229897SLen Brown 65436229897SLen Brown return fd; 65536229897SLen Brown } 65636229897SLen Brown 6572af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 6582af4f9b8SLen Brown { 6592af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 6602af4f9b8SLen Brown } 6612af4f9b8SLen Brown 6622af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 6632af4f9b8SLen Brown { 6642af4f9b8SLen Brown struct perf_event_attr pea; 6652af4f9b8SLen Brown int fd; 6662af4f9b8SLen Brown 6672af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 6682af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 6692af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 6702af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 6712af4f9b8SLen Brown 6722af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 6732af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 674164d7a96SLen Brown if (fd == -1) { 675de7839eeSLen Brown warnx("capget(CAP_PERFMON) failed, try \"# setcap cap_sys_admin=ep %s\"", progname); 676164d7a96SLen Brown BIC_NOT_PRESENT(BIC_IPC); 677164d7a96SLen Brown } 6782af4f9b8SLen Brown 6792af4f9b8SLen Brown return fd; 6802af4f9b8SLen Brown } 6812af4f9b8SLen Brown 6822af4f9b8SLen Brown int get_instr_count_fd(int cpu) 6832af4f9b8SLen Brown { 6842af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 6852af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 6862af4f9b8SLen Brown 6872af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 6882af4f9b8SLen Brown 6892af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 6902af4f9b8SLen Brown } 6912af4f9b8SLen Brown 69236229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 69336229897SLen Brown { 69436229897SLen Brown ssize_t retval; 69536229897SLen Brown 69636229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 69715aaa346SLen Brown 69898481e79SLen Brown if (retval != sizeof *msr) 699cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 70015aaa346SLen Brown 70115aaa346SLen Brown return 0; 702103a8feaSLen Brown } 703103a8feaSLen Brown 704dd778a5eSLen Brown #define MAX_DEFERRED 16 7050fc521bcSZephaniah E. Loss-Cutler-Hull char *deferred_add_names[MAX_DEFERRED]; 706dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 7070fc521bcSZephaniah E. Loss-Cutler-Hull int deferred_add_index; 708dd778a5eSLen Brown int deferred_skip_index; 709dd778a5eSLen Brown 710dd778a5eSLen Brown /* 711dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 712dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 713dd778a5eSLen Brown */ 714dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 715dd778a5eSLen Brown 716dd778a5eSLen Brown void help(void) 717dd778a5eSLen Brown { 718dd778a5eSLen Brown fprintf(outf, 719dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 720dd778a5eSLen Brown "\n" 721dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 722dd778a5eSLen Brown "when COMMAND completes.\n" 723dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 724dd778a5eSLen Brown "to print statistics, until interrupted.\n" 725cc481650SNathan Ciobanu " -a, --add add a counter\n" 726dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 727cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 728dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7299ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7309ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7319ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7329ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7339ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7349ce80578SNathan Ciobanu " hide the specified column(s)\n" 735cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 736cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7379ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 738cc481650SNathan Ciobanu " -l, --list list column headers only\n" 739cc481650SNathan Ciobanu " -n, --num_iterations num\n" 740cc481650SNathan Ciobanu " number of the measurement iterations\n" 741c7e399f8SZephaniah E. Loss-Cutler-Hull " -N, --header_iterations num\n" 742c7e399f8SZephaniah E. Loss-Cutler-Hull " print header every num iterations\n" 743cc481650SNathan Ciobanu " -o, --out file\n" 744cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 745cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7469ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7479ce80578SNathan Ciobanu " show only the specified column(s)\n" 7489ce80578SNathan Ciobanu " -S, --Summary\n" 7499ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7509ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7519ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7529ce80578SNathan Ciobanu " degrees Celsius\n" 753cc481650SNathan Ciobanu " -h, --help print this help message\n" 7541b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n"); 755dd778a5eSLen Brown } 756dd778a5eSLen Brown 757812db3f7SLen Brown /* 758812db3f7SLen Brown * bic_lookup 759812db3f7SLen Brown * for all the strings in comma separate name_list, 760812db3f7SLen Brown * set the approprate bit in return value. 761812db3f7SLen Brown */ 762dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 763812db3f7SLen Brown { 7649878bf7aSLen Brown unsigned int i; 765812db3f7SLen Brown unsigned long long retval = 0; 766812db3f7SLen Brown 767812db3f7SLen Brown while (name_list) { 768812db3f7SLen Brown char *comma; 769812db3f7SLen Brown 770812db3f7SLen Brown comma = strchr(name_list, ','); 771812db3f7SLen Brown 772812db3f7SLen Brown if (comma) 773812db3f7SLen Brown *comma = '\0'; 774812db3f7SLen Brown 775812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 776812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 777812db3f7SLen Brown retval |= (1ULL << i); 778812db3f7SLen Brown break; 779812db3f7SLen Brown } 7805dc241f2SLen Brown if (!strcmp(name_list, "all")) { 7815dc241f2SLen Brown retval |= ~0; 7825dc241f2SLen Brown break; 7835dc241f2SLen Brown } else if (!strcmp(name_list, "topology")) { 7845dc241f2SLen Brown retval |= BIC_TOPOLOGY; 7855dc241f2SLen Brown break; 7865dc241f2SLen Brown } else if (!strcmp(name_list, "power")) { 7875dc241f2SLen Brown retval |= BIC_THERMAL_PWR; 7885dc241f2SLen Brown break; 7895dc241f2SLen Brown } else if (!strcmp(name_list, "idle")) { 7905dc241f2SLen Brown retval |= BIC_IDLE; 7915dc241f2SLen Brown break; 7925dc241f2SLen Brown } else if (!strcmp(name_list, "frequency")) { 7935dc241f2SLen Brown retval |= BIC_FREQUENCY; 7945dc241f2SLen Brown break; 7955dc241f2SLen Brown } else if (!strcmp(name_list, "other")) { 7965dc241f2SLen Brown retval |= BIC_OTHER; 7975dc241f2SLen Brown break; 7985dc241f2SLen Brown } 7995dc241f2SLen Brown 800812db3f7SLen Brown } 801812db3f7SLen Brown if (i == MAX_BIC) { 802dd778a5eSLen Brown if (mode == SHOW_LIST) { 8030fc521bcSZephaniah E. Loss-Cutler-Hull deferred_add_names[deferred_add_index++] = name_list; 8040fc521bcSZephaniah E. Loss-Cutler-Hull if (deferred_add_index >= MAX_DEFERRED) { 8050fc521bcSZephaniah E. Loss-Cutler-Hull fprintf(stderr, "More than max %d un-recognized --add options '%s'\n", 8060fc521bcSZephaniah E. Loss-Cutler-Hull MAX_DEFERRED, name_list); 8070fc521bcSZephaniah E. Loss-Cutler-Hull help(); 8080fc521bcSZephaniah E. Loss-Cutler-Hull exit(1); 809812db3f7SLen Brown } 8100fc521bcSZephaniah E. Loss-Cutler-Hull } else { 811dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 812dd778a5eSLen Brown if (debug) 813dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 814dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 815dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 816dd778a5eSLen Brown MAX_DEFERRED, name_list); 817dd778a5eSLen Brown help(); 818dd778a5eSLen Brown exit(1); 819dd778a5eSLen Brown } 820dd778a5eSLen Brown } 8210fc521bcSZephaniah E. Loss-Cutler-Hull } 822812db3f7SLen Brown 823812db3f7SLen Brown name_list = comma; 824812db3f7SLen Brown if (name_list) 825812db3f7SLen Brown name_list++; 826812db3f7SLen Brown 827812db3f7SLen Brown } 828812db3f7SLen Brown return retval; 829812db3f7SLen Brown } 830fc04cc67SLen Brown 831c8ade361SLen Brown void print_header(char *delim) 832103a8feaSLen Brown { 833388e9c81SLen Brown struct msr_counter *mp; 8346168c2e0SLen Brown int printed = 0; 835388e9c81SLen Brown 8363f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 8373f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 8383f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 8393f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 840812db3f7SLen Brown if (DO_BIC(BIC_Package)) 8416168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 8426de68fe1SLen Brown if (DO_BIC(BIC_Die)) 8436de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 84401235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 84501235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 846812db3f7SLen Brown if (DO_BIC(BIC_Core)) 8476168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 848812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8496168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8504c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8514c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8524c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8534c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 854812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8556168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 856812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8576168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 858812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8596168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 860812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8616168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8621cc21f7bSLen Brown 8632af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8642af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8652af4f9b8SLen Brown 8660de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8670de6c0dfSLen Brown if (sums_need_wide_columns) 8686168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8690de6c0dfSLen Brown else 8706168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8710de6c0dfSLen Brown } 8720de6c0dfSLen Brown 873812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8746168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8751cc21f7bSLen Brown 876388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 877dd778a5eSLen Brown 878388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 879388e9c81SLen Brown if (mp->width == 64) 880dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 881388e9c81SLen Brown else 882dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 883388e9c81SLen Brown } else { 8840de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 885dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8860de6c0dfSLen Brown else 887dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 888388e9c81SLen Brown } 889388e9c81SLen Brown } 890388e9c81SLen Brown 89141618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8926168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 893562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8946168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 895812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8966168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 897812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8986168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 899678a3bd1SLen Brown 9000539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 9016168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 902678a3bd1SLen Brown 903812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 9046168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 905388e9c81SLen Brown 906eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 907eae97e05SChen Yu outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : "")); 908eae97e05SChen Yu 9099392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 9109392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 9119392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 9129392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 9139392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 9149392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 9159392bd98SCalvin Walton } 9169392bd98SCalvin Walton 917388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 918388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 919388e9c81SLen Brown if (mp->width == 64) 920c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 921388e9c81SLen Brown else 922c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 923388e9c81SLen Brown } else { 9240de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9250de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9260de6c0dfSLen Brown else 927c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 928388e9c81SLen Brown } 929388e9c81SLen Brown } 930388e9c81SLen Brown 931812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 9326168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 933889facbeSLen Brown 934812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 9356168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 936fdf676e5SLen Brown 937812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 9386168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 93927d47356SLen Brown 940b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 941b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 942b4b91569SRafael Antognolli 943a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 9446168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 945a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 9466168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 947a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 9486168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 949a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 9506168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 9510b2bb692SLen Brown 9520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9536168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9540f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9556168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9560f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9576168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9596168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9600f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9616168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9620f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9636168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9640f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9656168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 966be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 967be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 968be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 969be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 970103a8feaSLen Brown 9715c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 972812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9736168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9749392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9756168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 976812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9776168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 978812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9796168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 980812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9816168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 982812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9836168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 984d7899447SLen Brown } else if (do_rapl && rapl_joules) { 985812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9866168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9879392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9886168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 989812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9906168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 991812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9926168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 993812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9946168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 995812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9966168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9975c56be9aSDirk Brandewie } 998a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ)) 999a5c6d65dSLen Brown outp += sprintf(outp, "%sUncMHz", (printed++ ? delim : "")); 1000a5c6d65dSLen Brown 1001388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 1002388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1003388e9c81SLen Brown if (mp->width == 64) 1004c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 1005388e9c81SLen Brown else 1006c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 1007388e9c81SLen Brown } else { 10080de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10090de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 10100de6c0dfSLen Brown else 1011c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 1012388e9c81SLen Brown } 1013388e9c81SLen Brown } 1014388e9c81SLen Brown 1015c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1016103a8feaSLen Brown } 1017103a8feaSLen Brown 10181b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1019103a8feaSLen Brown { 1020388e9c81SLen Brown int i; 1021388e9c81SLen Brown struct msr_counter *mp; 1022388e9c81SLen Brown 10233b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 1024c98d5d94SLen Brown 1025c98d5d94SLen Brown if (t) { 10261b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 10273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 10283b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 10293b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 10303b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 10316886fee4SLen Brown 10322af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 10332af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 10342af4f9b8SLen Brown 1035812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 10360de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 1037812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 1038218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 1039388e9c81SLen Brown 1040388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 10411b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]); 1042388e9c81SLen Brown } 1043103a8feaSLen Brown } 1044103a8feaSLen Brown 1045c98d5d94SLen Brown if (c) { 10463b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 10473b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 10483b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 10493b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 10503b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 1051eae97e05SChen Yu outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt); 10529392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 1053388e9c81SLen Brown 1054388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 10551b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]); 1056388e9c81SLen Brown } 10570539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1058c98d5d94SLen Brown } 1059103a8feaSLen Brown 1060c98d5d94SLen Brown if (p) { 10613b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10620b2bb692SLen Brown 10630b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10640b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10650b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10660b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10670b2bb692SLen Brown 10683b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10703b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10730f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10743b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10753b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10763b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10773b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1078be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1079be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10807c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10817c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10827c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10837c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 10841b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status); 10851b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status); 10863b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1087388e9c81SLen Brown 1088388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 10891b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]); 1090388e9c81SLen Brown } 1091c98d5d94SLen Brown } 10923b4d5c7fSAndy Shevchenko 10933b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10943b4d5c7fSAndy Shevchenko 1095c98d5d94SLen Brown return 0; 1096103a8feaSLen Brown } 1097103a8feaSLen Brown 1098e23da037SLen Brown /* 1099e23da037SLen Brown * column formatting convention & formats 1100e23da037SLen Brown */ 11011b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1102103a8feaSLen Brown { 1103008d396eSLen Brown double interval_float, tsc; 1104fc04cc67SLen Brown char *fmt8; 1105388e9c81SLen Brown int i; 1106388e9c81SLen Brown struct msr_counter *mp; 11076168c2e0SLen Brown char *delim = "\t"; 11086168c2e0SLen Brown int printed = 0; 1109103a8feaSLen Brown 1110c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1111c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1112c98d5d94SLen Brown return 0; 1113c98d5d94SLen Brown 1114c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1115c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1116c98d5d94SLen Brown return 0; 1117c98d5d94SLen Brown 11181ef7d21aSLen Brown /*if not summary line and --cpu is used */ 11191b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 11201ef7d21aSLen Brown return 0; 11211ef7d21aSLen Brown 11223f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1123f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1124f4fdf2b4SLen Brown struct timeval tv; 1125f4fdf2b4SLen Brown 1126f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1127f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1128f4fdf2b4SLen Brown } 1129f4fdf2b4SLen Brown 11303f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 11313f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 11323f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 11333f44a5c6SLen Brown 1134d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 1135103a8feaSLen Brown 1136008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1137008d396eSLen Brown 1138c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1139c98d5d94SLen Brown if (t == &average.threads) { 1140812db3f7SLen Brown if (DO_BIC(BIC_Package)) 11416168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11426de68fe1SLen Brown if (DO_BIC(BIC_Die)) 11436de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 114401235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 114501235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1146812db3f7SLen Brown if (DO_BIC(BIC_Core)) 11476168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1148812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11496168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11504c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11514c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11524c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11534c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1154103a8feaSLen Brown } else { 1155812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1156c98d5d94SLen Brown if (p) 11576168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1158c98d5d94SLen Brown else 11596168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1160c98d5d94SLen Brown } 11616de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11626de68fe1SLen Brown if (c) 11636de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11646de68fe1SLen Brown else 11656de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11666de68fe1SLen Brown } 116701235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 116801235041SPrarit Bhargava if (t) 116901235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 11701b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 117101235041SPrarit Bhargava else 11721b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 117301235041SPrarit Bhargava } 1174812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1175c98d5d94SLen Brown if (c) 11766168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1177c98d5d94SLen Brown else 11786168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1179c98d5d94SLen Brown } 1180812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11816168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11824c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11834c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11844c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11854c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1186103a8feaSLen Brown } 1187fc04cc67SLen Brown 1188812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 11891b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 1190fc04cc67SLen Brown 1191812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11926168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc); 1193103a8feaSLen Brown 1194812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 119521ed5574SLen Brown if (has_base_hz) 11961b439f01SLen Brown outp += 11971b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 119821ed5574SLen Brown else 11996168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1200008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 120121ed5574SLen Brown } 1202103a8feaSLen Brown 1203812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 12046168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 1205103a8feaSLen Brown 12062af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 12072af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 12082af4f9b8SLen Brown 1209562a2d37SLen Brown /* IRQ */ 12100de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 12110de6c0dfSLen Brown if (sums_need_wide_columns) 12126168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 12130de6c0dfSLen Brown else 12146168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 12150de6c0dfSLen Brown } 1216562a2d37SLen Brown 12171cc21f7bSLen Brown /* SMI */ 1218812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 12196168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 12201cc21f7bSLen Brown 1221678a3bd1SLen Brown /* Added counters */ 1222678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1223678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1224678a3bd1SLen Brown if (mp->width == 32) 12251b439f01SLen Brown outp += 12261b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]); 1227678a3bd1SLen Brown else 12286168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1229678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 12300de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12316168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 12320de6c0dfSLen Brown else 12336168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1234678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 123541618e63SLen Brown if (mp->type == COUNTER_USEC) 12361b439f01SLen Brown outp += 12371b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12381b439f01SLen Brown t->counter[i] / interval_float / 10000); 123941618e63SLen Brown else 12406168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc); 1241678a3bd1SLen Brown } 1242678a3bd1SLen Brown } 1243678a3bd1SLen Brown 124441618e63SLen Brown /* C1 */ 124541618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 12466168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc); 124741618e63SLen Brown 1248c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1249c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1250c98d5d94SLen Brown goto done; 1251c98d5d94SLen Brown 1252562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 12536168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc); 1254812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 12556168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc); 1256812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12576168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc); 1258c98d5d94SLen Brown 12590539ba11SLen Brown /* Mod%c6 */ 12600539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12616168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12620539ba11SLen Brown 1263812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12646168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1265889facbeSLen Brown 1266eae97e05SChen Yu /* Core throttle count */ 1267eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 1268eae97e05SChen Yu outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt); 1269eae97e05SChen Yu 1270388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1271388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1272388e9c81SLen Brown if (mp->width == 32) 12731b439f01SLen Brown outp += 12741b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]); 1275388e9c81SLen Brown else 12766168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1277388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12780de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12796168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12800de6c0dfSLen Brown else 12816168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1282388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12836168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc); 1284388e9c81SLen Brown } 1285388e9c81SLen Brown } 1286388e9c81SLen Brown 12879392bd98SCalvin Walton fmt8 = "%s%.2f"; 12889392bd98SCalvin Walton 12899392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12901b439f01SLen Brown outp += 12911b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12929392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12939392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12949392bd98SCalvin Walton 1295c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1296c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1297c98d5d94SLen Brown goto done; 1298c98d5d94SLen Brown 12990b2bb692SLen Brown /* PkgTmp */ 1300812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 13016168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1302889facbeSLen Brown 1303fdf676e5SLen Brown /* GFXrc6 */ 1304812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1305ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 13066168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 13079185e988SLen Brown } else { 13086168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 13099185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 13109185e988SLen Brown } 13119185e988SLen Brown } 1312fdf676e5SLen Brown 131327d47356SLen Brown /* GFXMHz */ 1314812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 13156168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 131627d47356SLen Brown 1317b4b91569SRafael Antognolli /* GFXACTMHz */ 1318b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1319b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1320b4b91569SRafael Antognolli 13210b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1322a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc); 1324a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13256168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc); 1326a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13276168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc); 1328a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 13296168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc); 13300b2bb692SLen Brown 13310f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 13326168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc); 13330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 13346168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc); 13350f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 13366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc); 13370f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 13386168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc); 13390f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 13406168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc); 13410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 13426168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc); 13430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 13446168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc); 1345889facbeSLen Brown 1346be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 13471b439f01SLen Brown outp += 13481b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1349be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 13501b439f01SLen Brown outp += 13511b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1352be0e54c4SLen Brown 1353812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 13541b439f01SLen Brown outp += 13551b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 1356eae97e05SChen Yu 13579392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13581b439f01SLen Brown outp += 13591b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1360812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 13611b439f01SLen Brown outp += 13621b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1363812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 13641b439f01SLen Brown outp += 13651b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13661b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float); 1367812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13686168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13699392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13706168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1371812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13726168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1373812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13746168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1375812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 13761b439f01SLen Brown outp += 13771b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13781b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1379812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 13801b439f01SLen Brown outp += 13811b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13821b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1383a5c6d65dSLen Brown /* UncMHz */ 1384a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ)) 1385a5c6d65dSLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->uncore_mhz); 1386812db3f7SLen Brown 1387388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1388388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1389388e9c81SLen Brown if (mp->width == 32) 13901b439f01SLen Brown outp += 13911b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]); 1392388e9c81SLen Brown else 13936168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1394388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 13950de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 13966168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 13970de6c0dfSLen Brown else 13986168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1399388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 14006168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc); 1401388e9c81SLen Brown } 1402388e9c81SLen Brown } 1403388e9c81SLen Brown 1404c98d5d94SLen Brown done: 140594d6ab4bSLen Brown if (*(outp - 1) != '\n') 1406c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1407c98d5d94SLen Brown 1408c98d5d94SLen Brown return 0; 1409103a8feaSLen Brown } 1410103a8feaSLen Brown 1411b7d8c148SLen Brown void flush_output_stdout(void) 1412103a8feaSLen Brown { 1413b7d8c148SLen Brown FILE *filep; 1414b7d8c148SLen Brown 1415b7d8c148SLen Brown if (outf == stderr) 1416b7d8c148SLen Brown filep = stdout; 1417b7d8c148SLen Brown else 1418b7d8c148SLen Brown filep = outf; 1419b7d8c148SLen Brown 1420b7d8c148SLen Brown fputs(output_buffer, filep); 1421b7d8c148SLen Brown fflush(filep); 1422b7d8c148SLen Brown 1423c98d5d94SLen Brown outp = output_buffer; 1424c98d5d94SLen Brown } 14251b439f01SLen Brown 1426b7d8c148SLen Brown void flush_output_stderr(void) 1427c98d5d94SLen Brown { 1428b7d8c148SLen Brown fputs(output_buffer, outf); 1429b7d8c148SLen Brown fflush(outf); 1430c98d5d94SLen Brown outp = output_buffer; 1431c98d5d94SLen Brown } 14321b439f01SLen Brown 1433c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1434c98d5d94SLen Brown { 1435c7e399f8SZephaniah E. Loss-Cutler-Hull static int count; 1436103a8feaSLen Brown 1437c7e399f8SZephaniah E. Loss-Cutler-Hull if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only) 1438c8ade361SLen Brown print_header("\t"); 1439103a8feaSLen Brown 14409d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1441103a8feaSLen Brown 1442c7e399f8SZephaniah E. Loss-Cutler-Hull count++; 1443e23da037SLen Brown 1444e23da037SLen Brown if (summary_only) 1445e23da037SLen Brown return; 1446e23da037SLen Brown 1447c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1448103a8feaSLen Brown } 1449103a8feaSLen Brown 1450889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 14517c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1452889facbeSLen Brown 14531b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old) 1454103a8feaSLen Brown { 1455388e9c81SLen Brown int i; 1456388e9c81SLen Brown struct msr_counter *mp; 14570b2bb692SLen Brown 1458a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14590b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1460a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14610b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1462a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14630b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1464a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14650b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1466a99d8730SLen Brown 1467c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 14680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1469c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 14700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1471c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 14720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1473c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1474ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1475ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1476ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1477be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1478be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1479889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1480889facbeSLen Brown 14819185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 14829185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 14839185e988SLen Brown old->gfx_rc6_ms = -1; 14849185e988SLen Brown else 1485fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 14869185e988SLen Brown 1487a5c6d65dSLen Brown old->uncore_mhz = new->uncore_mhz; 148827d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1489b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 149027d47356SLen Brown 149187e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 149287e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 149387e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 149487e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 149587e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 149687e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1497ba3dec99SLen Brown 1498388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1499388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1500388e9c81SLen Brown old->counter[i] = new->counter[i]; 1501388e9c81SLen Brown else 1502388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1503388e9c81SLen Brown } 1504388e9c81SLen Brown 1505ba3dec99SLen Brown return 0; 1506103a8feaSLen Brown } 1507103a8feaSLen Brown 15081b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old) 1509c98d5d94SLen Brown { 1510388e9c81SLen Brown int i; 1511388e9c81SLen Brown struct msr_counter *mp; 1512388e9c81SLen Brown 1513c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1514c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1515c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1516889facbeSLen Brown old->core_temp_c = new->core_temp_c; 1517eae97e05SChen Yu old->core_throt_cnt = new->core_throt_cnt; 15180539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1519388e9c81SLen Brown 15209392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 15219392bd98SCalvin Walton 1522388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1523388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1524388e9c81SLen Brown old->counter[i] = new->counter[i]; 1525388e9c81SLen Brown else 1526388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1527388e9c81SLen Brown } 1528103a8feaSLen Brown } 1529c98d5d94SLen Brown 15301e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 15311e9042b9SSrinivas Pandruvada { 15321e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 15331e9042b9SSrinivas Pandruvada return 0; 15341e9042b9SSrinivas Pandruvada 15351e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 15361e9042b9SSrinivas Pandruvada } 15371e9042b9SSrinivas Pandruvada 1538c3ae331dSLen Brown /* 1539c3ae331dSLen Brown * old = new - old 1540c3ae331dSLen Brown */ 15411b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 1542c98d5d94SLen Brown { 1543388e9c81SLen Brown int i; 1544388e9c81SLen Brown struct msr_counter *mp; 1545388e9c81SLen Brown 15464c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 15474c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 15484c2122d4SLen Brown new->apic_id = old->apic_id; 15494c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 15504c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 15514c2122d4SLen Brown 15523f44a5c6SLen Brown /* 15533f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 15543f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 15553f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 15563f44a5c6SLen Brown */ 15573f44a5c6SLen Brown 1558d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 15593f44a5c6SLen Brown old->tv_begin = new->tv_begin; 15603f44a5c6SLen Brown old->tv_end = new->tv_end; 15613f44a5c6SLen Brown 1562c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1563c98d5d94SLen Brown 1564103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1565b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1566b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1567b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1568b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1569103a8feaSLen Brown 1570c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1571c98d5d94SLen Brown 15721b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 1573c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1574c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1575c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1576c98d5d94SLen Brown } else { 1577ba3dec99SLen Brown return -1; 1578103a8feaSLen Brown } 1579a729617cSLen Brown } 1580103a8feaSLen Brown 1581144b44b1SLen Brown if (use_c1_residency_msr) { 1582144b44b1SLen Brown /* 1583144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1584144b44b1SLen Brown * which should be more accurate than the derivation below. 1585144b44b1SLen Brown */ 1586144b44b1SLen Brown } else { 1587103a8feaSLen Brown /* 1588c3ae331dSLen Brown * As counter collection is not atomic, 1589c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1590103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1591103a8feaSLen Brown */ 159295149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1593c98d5d94SLen Brown old->c1 = 0; 1594c98d5d94SLen Brown else { 1595c98d5d94SLen Brown /* normal case, derive c1 */ 1596008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1597c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1598c98d5d94SLen Brown } 1599144b44b1SLen Brown } 1600c3ae331dSLen Brown 1601c98d5d94SLen Brown if (old->mperf == 0) { 1602b7d8c148SLen Brown if (debug > 1) 1603b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1604c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1605c98d5d94SLen Brown } 1606103a8feaSLen Brown 16072af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 16082af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 16092af4f9b8SLen Brown 1610812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1611562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1612562a2d37SLen Brown 1613812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 16141ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1615ba3dec99SLen Brown 1616388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1617388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1618388e9c81SLen Brown old->counter[i] = new->counter[i]; 1619388e9c81SLen Brown else 1620388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1621388e9c81SLen Brown } 1622ba3dec99SLen Brown return 0; 1623103a8feaSLen Brown } 1624c98d5d94SLen Brown 1625c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 16261b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 1627c98d5d94SLen Brown { 1628ba3dec99SLen Brown int retval = 0; 1629ba3dec99SLen Brown 1630c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1631c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1632c98d5d94SLen Brown delta_core(c, c2); 1633c98d5d94SLen Brown 1634c98d5d94SLen Brown /* always calculate thread delta */ 1635ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1636ba3dec99SLen Brown if (retval) 1637ba3dec99SLen Brown return retval; 1638c98d5d94SLen Brown 1639c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1640c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1641ba3dec99SLen Brown retval = delta_package(p, p2); 1642c98d5d94SLen Brown 1643ba3dec99SLen Brown return retval; 1644103a8feaSLen Brown } 1645103a8feaSLen Brown 1646c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1647103a8feaSLen Brown { 1648388e9c81SLen Brown int i; 1649388e9c81SLen Brown struct msr_counter *mp; 1650388e9c81SLen Brown 16513f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 16523f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 16533f44a5c6SLen Brown t->tv_end.tv_sec = 0; 16543f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1655d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1656d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 16573f44a5c6SLen Brown 1658c98d5d94SLen Brown t->tsc = 0; 1659c98d5d94SLen Brown t->aperf = 0; 1660c98d5d94SLen Brown t->mperf = 0; 1661c98d5d94SLen Brown t->c1 = 0; 1662103a8feaSLen Brown 16632af4f9b8SLen Brown t->instr_count = 0; 16642af4f9b8SLen Brown 1665562a2d37SLen Brown t->irq_count = 0; 1666562a2d37SLen Brown t->smi_count = 0; 1667562a2d37SLen Brown 1668c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1669c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1670c98d5d94SLen Brown 1671c98d5d94SLen Brown c->c3 = 0; 1672c98d5d94SLen Brown c->c6 = 0; 1673c98d5d94SLen Brown c->c7 = 0; 16740539ba11SLen Brown c->mc6_us = 0; 1675889facbeSLen Brown c->core_temp_c = 0; 16769392bd98SCalvin Walton c->core_energy = 0; 1677eae97e05SChen Yu c->core_throt_cnt = 0; 1678c98d5d94SLen Brown 16790b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16800b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16810b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16820b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16830b2bb692SLen Brown 1684c98d5d94SLen Brown p->pc2 = 0; 16850f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1686c98d5d94SLen Brown p->pc3 = 0; 16870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1688c98d5d94SLen Brown p->pc6 = 0; 16890f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1690c98d5d94SLen Brown p->pc7 = 0; 1691ca58710fSKristen Carlson Accardi p->pc8 = 0; 1692ca58710fSKristen Carlson Accardi p->pc9 = 0; 1693ca58710fSKristen Carlson Accardi p->pc10 = 0; 1694be0e54c4SLen Brown p->cpu_lpi = 0; 1695be0e54c4SLen Brown p->sys_lpi = 0; 1696889facbeSLen Brown 1697889facbeSLen Brown p->energy_pkg = 0; 1698889facbeSLen Brown p->energy_dram = 0; 1699889facbeSLen Brown p->energy_cores = 0; 1700889facbeSLen Brown p->energy_gfx = 0; 1701889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1702889facbeSLen Brown p->rapl_dram_perf_status = 0; 1703889facbeSLen Brown p->pkg_temp_c = 0; 170427d47356SLen Brown 1705fdf676e5SLen Brown p->gfx_rc6_ms = 0; 1706a5c6d65dSLen Brown p->uncore_mhz = 0; 170727d47356SLen Brown p->gfx_mhz = 0; 1708b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1709388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1710388e9c81SLen Brown t->counter[i] = 0; 1711388e9c81SLen Brown 1712388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1713388e9c81SLen Brown c->counter[i] = 0; 1714388e9c81SLen Brown 1715388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1716388e9c81SLen Brown p->counter[i] = 0; 1717103a8feaSLen Brown } 17181b439f01SLen Brown 17191b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1720103a8feaSLen Brown { 1721388e9c81SLen Brown int i; 1722388e9c81SLen Brown struct msr_counter *mp; 1723388e9c81SLen Brown 17244c2122d4SLen Brown /* copy un-changing apic_id's */ 17254c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 17264c2122d4SLen Brown average.threads.apic_id = t->apic_id; 17274c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 17284c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 17294c2122d4SLen Brown 17303f44a5c6SLen Brown /* remember first tv_begin */ 17313f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 17323f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 17333f44a5c6SLen Brown 17343f44a5c6SLen Brown /* remember last tv_end */ 17353f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 17363f44a5c6SLen Brown 1737c98d5d94SLen Brown average.threads.tsc += t->tsc; 1738c98d5d94SLen Brown average.threads.aperf += t->aperf; 1739c98d5d94SLen Brown average.threads.mperf += t->mperf; 1740c98d5d94SLen Brown average.threads.c1 += t->c1; 174115aaa346SLen Brown 17422af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 17432af4f9b8SLen Brown 1744562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1745562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1746562a2d37SLen Brown 1747388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1748388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1749388e9c81SLen Brown continue; 1750388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1751388e9c81SLen Brown } 1752388e9c81SLen Brown 1753c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1754c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1755c98d5d94SLen Brown return 0; 1756c98d5d94SLen Brown 1757c98d5d94SLen Brown average.cores.c3 += c->c3; 1758c98d5d94SLen Brown average.cores.c6 += c->c6; 1759c98d5d94SLen Brown average.cores.c7 += c->c7; 17600539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1761c98d5d94SLen Brown 1762889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1763eae97e05SChen Yu average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt); 1764889facbeSLen Brown 17659392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 17669392bd98SCalvin Walton 1767388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1768388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1769388e9c81SLen Brown continue; 1770388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1771388e9c81SLen Brown } 1772388e9c81SLen Brown 1773c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1774c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1775c98d5d94SLen Brown return 0; 1776c98d5d94SLen Brown 1777a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17780b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1779a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17800b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1781a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17820b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1783a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17840b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17850b2bb692SLen Brown 1786c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1788c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17890f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1790c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17910f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1792c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1793ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1794ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1795ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1796c98d5d94SLen Brown 1797be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1798be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1799be0e54c4SLen Brown 1800889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1801889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1802889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1803889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1804889facbeSLen Brown 1805fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 1806a5c6d65dSLen Brown average.packages.uncore_mhz = p->uncore_mhz; 180727d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1808b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 180927d47356SLen Brown 1810889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1811889facbeSLen Brown 1812889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1813889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1814388e9c81SLen Brown 1815388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 181695973a9bSDoug Smythies if ((mp->format == FORMAT_RAW) && (topo.num_packages == 0)) 181795973a9bSDoug Smythies average.packages.counter[i] = p->counter[i]; 181895973a9bSDoug Smythies else 1819388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1820388e9c81SLen Brown } 1821c98d5d94SLen Brown return 0; 1822c98d5d94SLen Brown } 18231b439f01SLen Brown 1824c98d5d94SLen Brown /* 1825c98d5d94SLen Brown * sum the counters for all cpus in the system 1826c98d5d94SLen Brown * compute the weighted average 1827c98d5d94SLen Brown */ 18281b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1829c98d5d94SLen Brown { 1830388e9c81SLen Brown int i; 1831388e9c81SLen Brown struct msr_counter *mp; 1832388e9c81SLen Brown 1833c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1834c98d5d94SLen Brown 1835c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1836c98d5d94SLen Brown 1837d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1838d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1839d4794f25SYazen Ghannam 1840c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1841c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1842c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 18432af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1844c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1845c98d5d94SLen Brown 18460de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 18470de6c0dfSLen Brown sums_need_wide_columns = 1; 18480de6c0dfSLen Brown 1849c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1850c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1851c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 18520539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1853c98d5d94SLen Brown 1854a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 18550b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1856a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 18570b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1858a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 18590b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1860a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 18610b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 18620b2bb692SLen Brown 1863c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 18640f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1865c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 18660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1867c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 18680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1869c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1870ca58710fSKristen Carlson Accardi 1871ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1872ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1873ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1874388e9c81SLen Brown 1875388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1876388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1877388e9c81SLen Brown continue; 18780de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18790de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18800de6c0dfSLen Brown sums_need_wide_columns = 1; 188141618e63SLen Brown continue; 18820de6c0dfSLen Brown } 1883388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1884388e9c81SLen Brown } 1885388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1886388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1887388e9c81SLen Brown continue; 18880de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18890de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18900de6c0dfSLen Brown sums_need_wide_columns = 1; 18910de6c0dfSLen Brown } 1892388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1893388e9c81SLen Brown } 1894388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1895388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1896388e9c81SLen Brown continue; 18970de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18980de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18990de6c0dfSLen Brown sums_need_wide_columns = 1; 19000de6c0dfSLen Brown } 1901388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1902388e9c81SLen Brown } 1903c98d5d94SLen Brown } 1904c98d5d94SLen Brown 1905c98d5d94SLen Brown static unsigned long long rdtsc(void) 1906c98d5d94SLen Brown { 1907c98d5d94SLen Brown unsigned int low, high; 1908c98d5d94SLen Brown 1909c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high)); 1910c98d5d94SLen Brown 1911c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1912c98d5d94SLen Brown } 1913c98d5d94SLen Brown 1914c98d5d94SLen Brown /* 1915495c7654SLen Brown * Open a file, and exit on failure 1916495c7654SLen Brown */ 1917495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1918495c7654SLen Brown { 1919495c7654SLen Brown FILE *filep = fopen(path, mode); 1920495c7654SLen Brown 1921495c7654SLen Brown if (!filep) 1922495c7654SLen Brown err(1, "%s: open failed", path); 1923495c7654SLen Brown return filep; 1924495c7654SLen Brown } 19251b439f01SLen Brown 1926495c7654SLen Brown /* 1927495c7654SLen Brown * snapshot_sysfs_counter() 1928495c7654SLen Brown * 1929495c7654SLen Brown * return snapshot of given counter 1930495c7654SLen Brown */ 1931495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1932495c7654SLen Brown { 1933495c7654SLen Brown FILE *fp; 1934495c7654SLen Brown int retval; 1935495c7654SLen Brown unsigned long long counter; 1936495c7654SLen Brown 1937495c7654SLen Brown fp = fopen_or_die(path, "r"); 1938495c7654SLen Brown 1939495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1940495c7654SLen Brown if (retval != 1) 1941495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1942495c7654SLen Brown 1943495c7654SLen Brown fclose(fp); 1944495c7654SLen Brown 1945495c7654SLen Brown return counter; 1946495c7654SLen Brown } 1947495c7654SLen Brown 1948495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1949495c7654SLen Brown { 1950495c7654SLen Brown if (mp->msr_num != 0) { 1951495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1952495c7654SLen Brown return -1; 1953495c7654SLen Brown } else { 195446c27978SLen Brown char path[128 + PATH_BYTES]; 195541618e63SLen Brown 195641618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 19571b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path); 195841618e63SLen Brown 195941618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 196041618e63SLen Brown } else { 1961495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1962495c7654SLen Brown } 196341618e63SLen Brown } 1964495c7654SLen Brown 1965495c7654SLen Brown return 0; 1966495c7654SLen Brown } 1967495c7654SLen Brown 1968a5c6d65dSLen Brown unsigned long long get_uncore_mhz(int package, int die) 1969a5c6d65dSLen Brown { 1970a5c6d65dSLen Brown char path[128]; 1971a5c6d65dSLen Brown 1972*037e72e7SJustin Ernst sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_%02d_die_%02d/current_freq_khz", package, 1973a5c6d65dSLen Brown die); 1974a5c6d65dSLen Brown 1975a5c6d65dSLen Brown return (snapshot_sysfs_counter(path) / 1000); 1976a5c6d65dSLen Brown } 1977a5c6d65dSLen Brown 19786d6501d9SBorislav Petkov int get_epb(int cpu) 19796d6501d9SBorislav Petkov { 19806d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19817f1b11baSBorislav Petkov unsigned long long msr; 19826d6501d9SBorislav Petkov int ret, epb = -1; 19836d6501d9SBorislav Petkov FILE *fp; 19846d6501d9SBorislav Petkov 19856d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19866d6501d9SBorislav Petkov 19877f1b11baSBorislav Petkov fp = fopen(path, "r"); 19887f1b11baSBorislav Petkov if (!fp) 19897f1b11baSBorislav Petkov goto msr_fallback; 19906d6501d9SBorislav Petkov 19916d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19926d6501d9SBorislav Petkov if (ret != 1) 19936d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19946d6501d9SBorislav Petkov 19956d6501d9SBorislav Petkov fclose(fp); 19966d6501d9SBorislav Petkov 19976d6501d9SBorislav Petkov return epb; 19987f1b11baSBorislav Petkov 19997f1b11baSBorislav Petkov msr_fallback: 20007f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 20017f1b11baSBorislav Petkov 20027f1b11baSBorislav Petkov return msr & 0xf; 20036d6501d9SBorislav Petkov } 20046d6501d9SBorislav Petkov 20054c2122d4SLen Brown void get_apic_id(struct thread_data *t) 20064c2122d4SLen Brown { 200734041551SLen Brown unsigned int eax, ebx, ecx, edx; 200834041551SLen Brown 200934041551SLen Brown if (DO_BIC(BIC_APIC)) { 201034041551SLen Brown eax = ebx = ecx = edx = 0; 201134041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 201234041551SLen Brown 201334041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 201434041551SLen Brown } 201534041551SLen Brown 201634041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 201734041551SLen Brown return; 201834041551SLen Brown 2019c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 202034041551SLen Brown unsigned int topology_extensions; 202134041551SLen Brown 202234041551SLen Brown if (max_extended_level < 0x8000001e) 202334041551SLen Brown return; 20244c2122d4SLen Brown 20254c2122d4SLen Brown eax = ebx = ecx = edx = 0; 202634041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 202734041551SLen Brown topology_extensions = ecx & (1 << 22); 202834041551SLen Brown 202934041551SLen Brown if (topology_extensions == 0) 203034041551SLen Brown return; 203134041551SLen Brown 203234041551SLen Brown eax = ebx = ecx = edx = 0; 203334041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 203434041551SLen Brown 203534041551SLen Brown t->x2apic_id = eax; 203634041551SLen Brown return; 203734041551SLen Brown } 20384c2122d4SLen Brown 20394c2122d4SLen Brown if (!genuine_intel) 20404c2122d4SLen Brown return; 20414c2122d4SLen Brown 20424c2122d4SLen Brown if (max_level < 0xb) 20434c2122d4SLen Brown return; 20444c2122d4SLen Brown 20454c2122d4SLen Brown ecx = 0; 20464c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 20474c2122d4SLen Brown t->x2apic_id = edx; 20484c2122d4SLen Brown 204934041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 20501b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 20514c2122d4SLen Brown } 20524c2122d4SLen Brown 2053eae97e05SChen Yu int get_core_throt_cnt(int cpu, unsigned long long *cnt) 2054eae97e05SChen Yu { 2055eae97e05SChen Yu char path[128 + PATH_BYTES]; 2056eae97e05SChen Yu unsigned long long tmp; 2057eae97e05SChen Yu FILE *fp; 2058eae97e05SChen Yu int ret; 2059eae97e05SChen Yu 2060eae97e05SChen Yu sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu); 2061eae97e05SChen Yu fp = fopen(path, "r"); 2062eae97e05SChen Yu if (!fp) 2063eae97e05SChen Yu return -1; 2064eae97e05SChen Yu ret = fscanf(fp, "%lld", &tmp); 20655e5fd36cSColin Ian King fclose(fp); 2066eae97e05SChen Yu if (ret != 1) 2067eae97e05SChen Yu return -1; 2068eae97e05SChen Yu *cnt = tmp; 2069eae97e05SChen Yu 2070eae97e05SChen Yu return 0; 2071eae97e05SChen Yu } 2072eae97e05SChen Yu 2073495c7654SLen Brown /* 2074c98d5d94SLen Brown * get_counters(...) 2075c98d5d94SLen Brown * migrate to cpu 2076c98d5d94SLen Brown * acquire and record local counters for that cpu 2077c98d5d94SLen Brown */ 2078c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2079c98d5d94SLen Brown { 2080c98d5d94SLen Brown int cpu = t->cpu_id; 2081889facbeSLen Brown unsigned long long msr; 20820102b067SLen Brown int aperf_mperf_retry_count = 0; 2083388e9c81SLen Brown struct msr_counter *mp; 2084388e9c81SLen Brown int i; 2085c98d5d94SLen Brown 2086e52966c0SLen Brown if (cpu_migrate(cpu)) { 20873d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 208815aaa346SLen Brown return -1; 2089e52966c0SLen Brown } 209015aaa346SLen Brown 2091d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 2092d4794f25SYazen Ghannam 20934c2122d4SLen Brown if (first_counter_read) 20944c2122d4SLen Brown get_apic_id(t); 20950102b067SLen Brown retry: 2096c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 209715aaa346SLen Brown 20981b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 20990102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 21000102b067SLen Brown 21010102b067SLen Brown /* 21020102b067SLen Brown * The TSC, APERF and MPERF must be read together for 21030102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 21040102b067SLen Brown * 21050102b067SLen Brown * Unfortunately, APERF and MPERF are read by 21060102b067SLen Brown * individual system call, so delays may occur 21070102b067SLen Brown * between them. If the time to read them 21080102b067SLen Brown * varies by a large amount, we re-read them. 21090102b067SLen Brown */ 21100102b067SLen Brown 21110102b067SLen Brown /* 21120102b067SLen Brown * This initial dummy APERF read has been seen to 21130102b067SLen Brown * reduce jitter in the subsequent reads. 21140102b067SLen Brown */ 21150102b067SLen Brown 21169c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2117c98d5d94SLen Brown return -3; 21180102b067SLen Brown 21190102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 21200102b067SLen Brown 21210102b067SLen Brown tsc_before = t->tsc; 21220102b067SLen Brown 21230102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 21240102b067SLen Brown return -3; 21250102b067SLen Brown 21260102b067SLen Brown tsc_between = rdtsc(); 21270102b067SLen Brown 21289c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2129c98d5d94SLen Brown return -4; 21300102b067SLen Brown 21310102b067SLen Brown tsc_after = rdtsc(); 21320102b067SLen Brown 21330102b067SLen Brown aperf_time = tsc_between - tsc_before; 21340102b067SLen Brown mperf_time = tsc_after - tsc_between; 21350102b067SLen Brown 21360102b067SLen Brown /* 21370102b067SLen Brown * If the system call latency to read APERF and MPERF 21380102b067SLen Brown * differ by more than 2x, then try again. 21390102b067SLen Brown */ 21400102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 21410102b067SLen Brown aperf_mperf_retry_count++; 21420102b067SLen Brown if (aperf_mperf_retry_count < 5) 21430102b067SLen Brown goto retry; 21440102b067SLen Brown else 21451b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time); 21460102b067SLen Brown } 21470102b067SLen Brown aperf_mperf_retry_count = 0; 21480102b067SLen Brown 2149b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2150b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 215188c3281fSLen Brown } 215288c3281fSLen Brown 21532af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 21542af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 21552af4f9b8SLen Brown return -4; 21562af4f9b8SLen Brown 2157812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2158562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2159812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 21601ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 21611ed51011SLen Brown return -5; 21621ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 21631ed51011SLen Brown } 21640539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2165144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2166144b44b1SLen Brown return -6; 2167144b44b1SLen Brown } 2168144b44b1SLen Brown 2169388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2170495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2171388e9c81SLen Brown return -10; 2172388e9c81SLen Brown } 2173388e9c81SLen Brown 2174c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2175c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2176f4fdf2b4SLen Brown goto done; 2177c98d5d94SLen Brown 21781e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2179c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2180c98d5d94SLen Brown return -6; 2181144b44b1SLen Brown } 2182144b44b1SLen Brown 21831e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2184c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2185c98d5d94SLen Brown return -7; 2186b866371fSZhang Rui } else if (do_knl_cstates && soft_c1_residency_display(BIC_CPU_c6)) { 2187fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2188fb5d4327SDasaratharaman Chandramouli return -7; 2189103a8feaSLen Brown } 219015aaa346SLen Brown 21917ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2192c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2193c98d5d94SLen Brown return -8; 21947ab5ff49SZhang Rui else if (t->is_atom) { 21957ab5ff49SZhang Rui /* 21967ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 21977ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 21987ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 21997ab5ff49SZhang Rui * accturate cc6 residency. 22007ab5ff49SZhang Rui */ 22017ab5ff49SZhang Rui c->c6 -= c->c7; 22027ab5ff49SZhang Rui } 22037ab5ff49SZhang Rui } 2204c98d5d94SLen Brown 22050539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 22060539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 22070539ba11SLen Brown return -8; 22080539ba11SLen Brown 2209812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2210889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2211889facbeSLen Brown return -9; 221255279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 2213889facbeSLen Brown } 2214889facbeSLen Brown 2215eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 2216eae97e05SChen Yu get_core_throt_cnt(cpu, &c->core_throt_cnt); 2217eae97e05SChen Yu 22189392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22199392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 22209392bd98SCalvin Walton return -14; 22219392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 22229392bd98SCalvin Walton } 22239392bd98SCalvin Walton 2224388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2225495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2226388e9c81SLen Brown return -10; 2227388e9c81SLen Brown } 2228889facbeSLen Brown 2229c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2230c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2231f4fdf2b4SLen Brown goto done; 223215aaa346SLen Brown 2233a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 22340b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 22350b2bb692SLen Brown return -10; 2236a99d8730SLen Brown } 2237a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 22380b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 22390b2bb692SLen Brown return -11; 2240a99d8730SLen Brown } 2241a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 22420b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 22430b2bb692SLen Brown return -12; 2244a99d8730SLen Brown } 2245a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 22460b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 22470b2bb692SLen Brown return -13; 22480b2bb692SLen Brown } 22490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2250c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2251c98d5d94SLen Brown return -9; 22520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 22530539ba11SLen Brown if (do_slm_cstates) { 22540539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 22550539ba11SLen Brown return -10; 22560539ba11SLen Brown } else { 2257c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2258c98d5d94SLen Brown return -10; 22590539ba11SLen Brown } 22600539ba11SLen Brown } 22610539ba11SLen Brown 22620f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2263c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2264c98d5d94SLen Brown return -11; 22650f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2266c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2267c98d5d94SLen Brown return -12; 22680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2269ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2270ca58710fSKristen Carlson Accardi return -13; 22710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2272ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2273ca58710fSKristen Carlson Accardi return -13; 22740f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2275ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2276ca58710fSKristen Carlson Accardi return -13; 22770f47c08dSLen Brown 2278be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2279be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2280be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2281be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2282be0e54c4SLen Brown 2283889facbeSLen Brown if (do_rapl & RAPL_PKG) { 22849972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2285889facbeSLen Brown return -13; 22869972d5d8SChen Yu p->energy_pkg = msr; 2287889facbeSLen Brown } 22889148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 22899972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2290889facbeSLen Brown return -14; 22919972d5d8SChen Yu p->energy_cores = msr; 2292889facbeSLen Brown } 2293889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 22949972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2295889facbeSLen Brown return -15; 22969972d5d8SChen Yu p->energy_dram = msr; 2297889facbeSLen Brown } 2298889facbeSLen Brown if (do_rapl & RAPL_GFX) { 22999972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2300889facbeSLen Brown return -16; 23019972d5d8SChen Yu p->energy_gfx = msr; 2302889facbeSLen Brown } 2303889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 23049972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2305889facbeSLen Brown return -16; 23069972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2307889facbeSLen Brown } 2308889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 23099972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2310889facbeSLen Brown return -16; 23119972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2312889facbeSLen Brown } 23133316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 23149972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 23153316f99aSCalvin Walton return -13; 23169972d5d8SChen Yu p->energy_pkg = msr; 23173316f99aSCalvin Walton } 2318812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2319889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2320889facbeSLen Brown return -17; 232155279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 2322889facbeSLen Brown } 2323fdf676e5SLen Brown 2324812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2325fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2326fdf676e5SLen Brown 2327a5c6d65dSLen Brown /* n.b. assume die0 uncore frequency applies to whole package */ 2328a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ)) 2329a5c6d65dSLen Brown p->uncore_mhz = get_uncore_mhz(p->package_id, 0); 2330a5c6d65dSLen Brown 2331812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 233227d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 233327d47356SLen Brown 2334b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2335b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2336b4b91569SRafael Antognolli 2337388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2338495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2339388e9c81SLen Brown return -10; 2340388e9c81SLen Brown } 2341f4fdf2b4SLen Brown done: 2342f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2343388e9c81SLen Brown 234415aaa346SLen Brown return 0; 2345103a8feaSLen Brown } 2346103a8feaSLen Brown 2347ee7e38e3SLen Brown /* 2348ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2349ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2350ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2351ee7e38e3SLen Brown */ 2352ee7e38e3SLen Brown 2353ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2354ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2355ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2356ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2357ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2358ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2359ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2360ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2361ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2362ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2363ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2364ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 23650b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 23660b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2367445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2368445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2369ee7e38e3SLen Brown 2370ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2371ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 23721b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 23731b439f01SLen Brown }; 2374ee7e38e3SLen Brown 23751b439f01SLen Brown int nhm_pkg_cstate_limits[16] = 23761b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 237738c6663aSLen Brown PCLRSV, PCLRSV 237838c6663aSLen Brown }; 237938c6663aSLen Brown 23801b439f01SLen Brown int snb_pkg_cstate_limits[16] = 23811b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 238238c6663aSLen Brown PCLRSV, PCLRSV 238338c6663aSLen Brown }; 238438c6663aSLen Brown 23851b439f01SLen Brown int hsw_pkg_cstate_limits[16] = 23861b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 238738c6663aSLen Brown PCLRSV, PCLRSV 238838c6663aSLen Brown }; 238938c6663aSLen Brown 23901b439f01SLen Brown int slv_pkg_cstate_limits[16] = 23911b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 239238c6663aSLen Brown PCL__6, PCL__7 239338c6663aSLen Brown }; 239438c6663aSLen Brown 23951b439f01SLen Brown int amt_pkg_cstate_limits[16] = 23961b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 239738c6663aSLen Brown PCLRSV, PCLRSV 239838c6663aSLen Brown }; 239938c6663aSLen Brown 24001b439f01SLen Brown int phi_pkg_cstate_limits[16] = 24011b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 240238c6663aSLen Brown PCLRSV, PCLRSV 240338c6663aSLen Brown }; 240438c6663aSLen Brown 24051b439f01SLen Brown int glm_pkg_cstate_limits[16] = 24061b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 240738c6663aSLen Brown PCLRSV, PCLRSV 240838c6663aSLen Brown }; 240938c6663aSLen Brown 24101b439f01SLen Brown int skx_pkg_cstate_limits[16] = 24111b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 241238c6663aSLen Brown PCLRSV, PCLRSV 241338c6663aSLen Brown }; 241438c6663aSLen Brown 24151b439f01SLen Brown int icx_pkg_cstate_limits[16] = 2416a1b6f487SArtem Bityutskiy { PCL__0, PCL__2, PCL__6, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 241738c6663aSLen Brown PCLRSV, PCLRSV 241838c6663aSLen Brown }; 2419a2b7b749SLen Brown 24201b439f01SLen Brown static void calculate_tsc_tweak() 2421a2b7b749SLen Brown { 2422a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2423a2b7b749SLen Brown } 2424a2b7b749SLen Brown 2425aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2426aeb01e6dSChen Yu 24271b439f01SLen Brown static void dump_nhm_platform_info(void) 2428103a8feaSLen Brown { 2429103a8feaSLen Brown unsigned long long msr; 2430103a8feaSLen Brown unsigned int ratio; 2431103a8feaSLen Brown 2432ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2433103a8feaSLen Brown 2434b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 24356574a5d5SLen Brown 2436103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 24371b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 2438103a8feaSLen Brown 2439103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 24401b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 2441103a8feaSLen Brown 24427ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2443b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2444bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 244567920418SLen Brown 2446aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2447aeb01e6dSChen Yu if (dis_cstate_prewake) 24481b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 2449aeb01e6dSChen Yu 2450fcd17211SLen Brown return; 2451fcd17211SLen Brown } 2452fcd17211SLen Brown 24531b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void) 2454fcd17211SLen Brown { 2455fcd17211SLen Brown unsigned long long msr; 2456fcd17211SLen Brown unsigned int ratio; 2457fcd17211SLen Brown 24587ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2459fcd17211SLen Brown 2460b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2461fcd17211SLen Brown 2462fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2463fcd17211SLen Brown if (ratio) 24641b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 2465fcd17211SLen Brown 2466fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2467fcd17211SLen Brown if (ratio) 24681b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 2469fcd17211SLen Brown return; 2470fcd17211SLen Brown } 2471fcd17211SLen Brown 24721b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void) 2473fcd17211SLen Brown { 2474fcd17211SLen Brown unsigned long long msr; 2475fcd17211SLen Brown unsigned int ratio; 24766574a5d5SLen Brown 24777ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 24786574a5d5SLen Brown 2479b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 24806574a5d5SLen Brown 24816574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 24826574a5d5SLen Brown if (ratio) 24831b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 24846574a5d5SLen Brown 24856574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 24866574a5d5SLen Brown if (ratio) 24871b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 24886574a5d5SLen Brown 24896574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 24906574a5d5SLen Brown if (ratio) 24911b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 24926574a5d5SLen Brown 24936574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 24946574a5d5SLen Brown if (ratio) 24951b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 24966574a5d5SLen Brown 24976574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 24986574a5d5SLen Brown if (ratio) 24991b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 25006574a5d5SLen Brown 25016574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 25026574a5d5SLen Brown if (ratio) 25031b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 25046574a5d5SLen Brown 25056574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 25066574a5d5SLen Brown if (ratio) 25071b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 25086574a5d5SLen Brown 25096574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 25106574a5d5SLen Brown if (ratio) 25111b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 2512103a8feaSLen Brown return; 2513fcd17211SLen Brown } 25141b439f01SLen Brown 251531e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 251631e07522SLen Brown { 251731e07522SLen Brown 251831e07522SLen Brown if (!genuine_intel) 251931e07522SLen Brown return 0; 252031e07522SLen Brown 25219878bf7aSLen Brown if (family != 6) 25229878bf7aSLen Brown return 0; 25239878bf7aSLen Brown 252431e07522SLen Brown switch (model) { 252531e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 252631e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 252725368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 2528684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: 25295ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 253020de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 253131e07522SLen Brown return 1; 25329878bf7aSLen Brown default: 253331e07522SLen Brown return 0; 253431e07522SLen Brown } 25359878bf7aSLen Brown } 2536fcd17211SLen Brown 25374af184eeSLen Brown static void dump_turbo_ratio_limits(int trl_msr_offset, int family, int model) 2538fcd17211SLen Brown { 253931e07522SLen Brown unsigned long long msr, core_counts; 25405d622845SLen Brown int shift; 2541103a8feaSLen Brown 25424af184eeSLen Brown get_msr(base_cpu, trl_msr_offset, &msr); 25434af184eeSLen Brown fprintf(outf, "cpu%d: MSR_%sTURBO_RATIO_LIMIT: 0x%08llx\n", 2544884a1f95SLen Brown base_cpu, trl_msr_offset == MSR_SECONDARY_TURBO_RATIO_LIMIT ? "SECONDARY_" : "", msr); 25456574a5d5SLen Brown 254631e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 254731e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 254831e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 254931e07522SLen Brown } else { 255031e07522SLen Brown core_counts = 0x0807060504030201; 255131e07522SLen Brown } 255231e07522SLen Brown 25535d622845SLen Brown for (shift = 56; shift >= 0; shift -= 8) { 25545d622845SLen Brown unsigned int ratio, group_size; 25556574a5d5SLen Brown 25565d622845SLen Brown ratio = (msr >> shift) & 0xFF; 25575d622845SLen Brown group_size = (core_counts >> shift) & 0xFF; 25586574a5d5SLen Brown if (ratio) 255931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 256031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25615d622845SLen Brown } 25626574a5d5SLen Brown 2563fcd17211SLen Brown return; 2564fcd17211SLen Brown } 25653a9a941dSLen Brown 25661b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void) 25670f7887c4SLen Brown { 25680f7887c4SLen Brown unsigned long long msr; 25690f7887c4SLen Brown unsigned int ratio; 25700f7887c4SLen Brown 25710f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 25720f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25730f7887c4SLen Brown 25740f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25750f7887c4SLen Brown if (ratio) 25761b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 25770f7887c4SLen Brown 25780f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25790f7887c4SLen Brown if (ratio) 25801b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 25810f7887c4SLen Brown 25820f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25830f7887c4SLen Brown if (ratio) 25841b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 25850f7887c4SLen Brown 25860f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 25870f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25880f7887c4SLen Brown 25890f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 25900f7887c4SLen Brown if (ratio) 25911b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 25920f7887c4SLen Brown 25930f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25940f7887c4SLen Brown if (ratio) 25951b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 25960f7887c4SLen Brown 25970f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25980f7887c4SLen Brown if (ratio) 25991b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 26000f7887c4SLen Brown 26010f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 26020f7887c4SLen Brown if (ratio) 26031b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 26040f7887c4SLen Brown } 26050f7887c4SLen Brown 26061b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void) 2607fb5d4327SDasaratharaman Chandramouli { 2608cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2609cbf97abaSHubert Chrzaniuk 2610fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2611cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2612cbf97abaSHubert Chrzaniuk int i, b_nr; 2613cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2614cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2615fb5d4327SDasaratharaman Chandramouli 2616ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2617fb5d4327SDasaratharaman Chandramouli 26181b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 2619fb5d4327SDasaratharaman Chandramouli 26201e3ec5cdSRandy Dunlap /* 2621fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2622cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2623cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2624fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2625fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2626fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2627fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2628fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2629fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2630fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2631fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2632fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2633fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2634fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2635fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2636fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2637fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2638fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2639fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2640fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2641fb5d4327SDasaratharaman Chandramouli */ 2642fb5d4327SDasaratharaman Chandramouli 2643cbf97abaSHubert Chrzaniuk b_nr = 0; 2644cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2645cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2646cbf97abaSHubert Chrzaniuk 2647cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2648fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2649cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2650fb5d4327SDasaratharaman Chandramouli 2651cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2652cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2653cbf97abaSHubert Chrzaniuk b_nr++; 2654fb5d4327SDasaratharaman Chandramouli } 2655cbf97abaSHubert Chrzaniuk 2656cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2657cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2658b7d8c148SLen Brown fprintf(outf, 2659710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2660cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2661fb5d4327SDasaratharaman Chandramouli } 2662fb5d4327SDasaratharaman Chandramouli 26631b439f01SLen Brown static void dump_nhm_cst_cfg(void) 2664fcd17211SLen Brown { 2665fcd17211SLen Brown unsigned long long msr; 2666fcd17211SLen Brown 26671df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2668fcd17211SLen Brown 26691df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2670fcd17211SLen Brown 26713e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2672fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2673fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2674fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2675fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 26761b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 2677ac980e13SArtem Bityutskiy 2678ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2679ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 26801b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2681ac980e13SArtem Bityutskiy } 2682ac980e13SArtem Bityutskiy 2683ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2684ac980e13SArtem Bityutskiy 2685fcd17211SLen Brown return; 2686103a8feaSLen Brown } 2687103a8feaSLen Brown 26881b439f01SLen Brown static void dump_config_tdp(void) 26896fb3143bSLen Brown { 26906fb3143bSLen Brown unsigned long long msr; 26916fb3143bSLen Brown 26926fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2693b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2694685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26956fb3143bSLen Brown 26966fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2697b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 26986fb3143bSLen Brown if (msr) { 2699685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2700685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2701685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2702685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 27036fb3143bSLen Brown } 2704b7d8c148SLen Brown fprintf(outf, ")\n"); 27056fb3143bSLen Brown 27066fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2707b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 27086fb3143bSLen Brown if (msr) { 2709685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2710685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2711685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2712685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 27136fb3143bSLen Brown } 2714b7d8c148SLen Brown fprintf(outf, ")\n"); 27156fb3143bSLen Brown 27166fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2717b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 27186fb3143bSLen Brown if ((msr) & 0x3) 2719b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2720b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2721b7d8c148SLen Brown fprintf(outf, ")\n"); 27226fb3143bSLen Brown 27236fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2724b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2725685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2726b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2727b7d8c148SLen Brown fprintf(outf, ")\n"); 27286fb3143bSLen Brown } 27295a63426eSLen Brown 27305a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 27315a63426eSLen Brown 27325a63426eSLen Brown void print_irtl(void) 27335a63426eSLen Brown { 27345a63426eSLen Brown unsigned long long msr; 27355a63426eSLen Brown 27365a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 27375a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 27385a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27395a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27405a63426eSLen Brown 27415a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 27425a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 27435a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27445a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27455a63426eSLen Brown 27465a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 27475a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 27485a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27495a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27505a63426eSLen Brown 27515a63426eSLen Brown if (!do_irtl_hsw) 27525a63426eSLen Brown return; 27535a63426eSLen Brown 27545a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 27555a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 27565a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27575a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27585a63426eSLen Brown 27595a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 27605a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 27615a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27625a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27635a63426eSLen Brown 27645a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 27655a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 27665a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27675a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27685a63426eSLen Brown 27695a63426eSLen Brown } 27701b439f01SLen Brown 277136229897SLen Brown void free_fd_percpu(void) 277236229897SLen Brown { 277336229897SLen Brown int i; 277436229897SLen Brown 277501a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 277636229897SLen Brown if (fd_percpu[i] != 0) 277736229897SLen Brown close(fd_percpu[i]); 277836229897SLen Brown } 277936229897SLen Brown 278036229897SLen Brown free(fd_percpu); 27816fb3143bSLen Brown } 27826fb3143bSLen Brown 2783c98d5d94SLen Brown void free_all_buffers(void) 2784103a8feaSLen Brown { 27850e2d8f05SLen Brown int i; 27860e2d8f05SLen Brown 2787c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2788c98d5d94SLen Brown cpu_present_set = NULL; 278936229897SLen Brown cpu_present_setsize = 0; 2790103a8feaSLen Brown 2791c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2792c98d5d94SLen Brown cpu_affinity_set = NULL; 2793c98d5d94SLen Brown cpu_affinity_setsize = 0; 2794103a8feaSLen Brown 2795c98d5d94SLen Brown free(thread_even); 2796c98d5d94SLen Brown free(core_even); 2797c98d5d94SLen Brown free(package_even); 2798103a8feaSLen Brown 2799c98d5d94SLen Brown thread_even = NULL; 2800c98d5d94SLen Brown core_even = NULL; 2801c98d5d94SLen Brown package_even = NULL; 2802103a8feaSLen Brown 2803c98d5d94SLen Brown free(thread_odd); 2804c98d5d94SLen Brown free(core_odd); 2805c98d5d94SLen Brown free(package_odd); 2806103a8feaSLen Brown 2807c98d5d94SLen Brown thread_odd = NULL; 2808c98d5d94SLen Brown core_odd = NULL; 2809c98d5d94SLen Brown package_odd = NULL; 2810103a8feaSLen Brown 2811c98d5d94SLen Brown free(output_buffer); 2812c98d5d94SLen Brown output_buffer = NULL; 2813c98d5d94SLen Brown outp = NULL; 281436229897SLen Brown 281536229897SLen Brown free_fd_percpu(); 2816562a2d37SLen Brown 2817562a2d37SLen Brown free(irq_column_2_cpu); 2818562a2d37SLen Brown free(irqs_per_cpu); 28190e2d8f05SLen Brown 28200e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 28210e2d8f05SLen Brown if (cpus[i].put_ids) 28220e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 28230e2d8f05SLen Brown } 28240e2d8f05SLen Brown free(cpus); 2825103a8feaSLen Brown } 2826103a8feaSLen Brown 282757a42a34SJosh Triplett /* 282895aebc44SJosh Triplett * Parse a file containing a single int. 28296de68fe1SLen Brown * Return 0 if file can not be opened 28306de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 283195aebc44SJosh Triplett */ 283295aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 283395aebc44SJosh Triplett { 283495aebc44SJosh Triplett va_list args; 283595aebc44SJosh Triplett char path[PATH_MAX]; 283695aebc44SJosh Triplett FILE *filep; 283795aebc44SJosh Triplett int value; 283895aebc44SJosh Triplett 283995aebc44SJosh Triplett va_start(args, fmt); 284095aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 284195aebc44SJosh Triplett va_end(args); 28426de68fe1SLen Brown filep = fopen(path, "r"); 28436de68fe1SLen Brown if (!filep) 28446de68fe1SLen Brown return 0; 2845b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2846b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 284795aebc44SJosh Triplett fclose(filep); 284895aebc44SJosh Triplett return value; 284995aebc44SJosh Triplett } 285095aebc44SJosh Triplett 285195aebc44SJosh Triplett /* 2852c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2853c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2854c98d5d94SLen Brown */ 2855c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2856c98d5d94SLen Brown { 285795aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2858103a8feaSLen Brown } 2859103a8feaSLen Brown 2860103a8feaSLen Brown int get_physical_package_id(int cpu) 2861103a8feaSLen Brown { 286295aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2863103a8feaSLen Brown } 2864103a8feaSLen Brown 28656de68fe1SLen Brown int get_die_id(int cpu) 28666de68fe1SLen Brown { 28676de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 28686de68fe1SLen Brown } 28696de68fe1SLen Brown 2870103a8feaSLen Brown int get_core_id(int cpu) 2871103a8feaSLen Brown { 287295aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2873103a8feaSLen Brown } 2874103a8feaSLen Brown 2875ef605741SPrarit Bhargava void set_node_data(void) 2876ef605741SPrarit Bhargava { 28772ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 28782ffbb224SPrarit Bhargava int cpu_count; 2879ef605741SPrarit Bhargava 28802ffbb224SPrarit Bhargava /* initialize logical_node_id */ 28812ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 28822ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2883ef605741SPrarit Bhargava 28842ffbb224SPrarit Bhargava cpu_count = 0; 28852ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 28862ffbb224SPrarit Bhargava lnode = 0; 28872ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 28882ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2889ef605741SPrarit Bhargava continue; 28902ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 28912ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 28922ffbb224SPrarit Bhargava continue; 28932ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2894ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28952ffbb224SPrarit Bhargava cpu_count++; 28962ffbb224SPrarit Bhargava /* 28972ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 28982ffbb224SPrarit Bhargava * the logical_node_id 28992ffbb224SPrarit Bhargava */ 29002ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 29011b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 29022ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 29032ffbb224SPrarit Bhargava cpu_count++; 2904ef605741SPrarit Bhargava } 29052ffbb224SPrarit Bhargava } 29062ffbb224SPrarit Bhargava lnode++; 29072ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 29082ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 29092ffbb224SPrarit Bhargava } 29102ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 29112ffbb224SPrarit Bhargava break; 29122ffbb224SPrarit Bhargava } 2913ef605741SPrarit Bhargava } 2914ef605741SPrarit Bhargava 2915ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2916c98d5d94SLen Brown { 2917c98d5d94SLen Brown char path[80]; 2918c98d5d94SLen Brown FILE *filep; 29190e2d8f05SLen Brown int i; 29200e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2921c98d5d94SLen Brown 29220e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 29231b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 29240e2d8f05SLen Brown filep = fopen(path, "r"); 29250e2d8f05SLen Brown if (!filep) 29260e2d8f05SLen Brown continue; 29270e2d8f05SLen Brown fclose(filep); 29280e2d8f05SLen Brown return i; 29290e2d8f05SLen Brown } 29300e2d8f05SLen Brown return -1; 2931e275b388SDasaratharaman Chandramouli } 2932c98d5d94SLen Brown 29330e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 29340e2d8f05SLen Brown { 29350e2d8f05SLen Brown char path[80], character; 29360e2d8f05SLen Brown FILE *filep; 29370e2d8f05SLen Brown unsigned long map; 29388cb48b32SPrarit Bhargava int so, shift, sib_core; 29390e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 29400e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 29410e2d8f05SLen Brown size_t size; 29428cb48b32SPrarit Bhargava int thread_id = 0; 29430e2d8f05SLen Brown 29440e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 29458cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 29468cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 29470e2d8f05SLen Brown if (!thiscpu->put_ids) 29480e2d8f05SLen Brown return -1; 29490e2d8f05SLen Brown 29500e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 29510e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 29520e2d8f05SLen Brown 29531b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 29543d7772eaSLen Brown filep = fopen(path, "r"); 29553d7772eaSLen Brown 29563d7772eaSLen Brown if (!filep) { 29573d7772eaSLen Brown warnx("%s: open failed", path); 29583d7772eaSLen Brown return -1; 29593d7772eaSLen Brown } 29600e2d8f05SLen Brown do { 29610e2d8f05SLen Brown offset -= BITMASK_SIZE; 29628173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 29638173c336SBen Hutchings err(1, "%s: failed to parse file", path); 29640e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 29650e2d8f05SLen Brown if ((map >> shift) & 0x1) { 29668cb48b32SPrarit Bhargava so = shift + offset; 29678cb48b32SPrarit Bhargava sib_core = get_core_id(so); 29688cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 29698cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 29701b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0)) 29711b439f01SLen Brown cpus[so].thread_id = thread_id++; 29728cb48b32SPrarit Bhargava } 29730e2d8f05SLen Brown } 29740e2d8f05SLen Brown } 2975e13da9a1SColin Ian King } while (character == ','); 2976c98d5d94SLen Brown fclose(filep); 29770e2d8f05SLen Brown 29780e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2979c98d5d94SLen Brown } 2980c98d5d94SLen Brown 2981c98d5d94SLen Brown /* 2982c98d5d94SLen Brown * run func(thread, core, package) in topology order 2983c98d5d94SLen Brown * skip non-present cpus 2984103a8feaSLen Brown */ 2985103a8feaSLen Brown 2986c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 2987c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2988c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2989c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 29901b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 2991c98d5d94SLen Brown { 299240f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2993c98d5d94SLen Brown 2994c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 299540f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 29961b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 29971b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 2998c98d5d94SLen Brown struct thread_data *t, *t2; 2999c98d5d94SLen Brown struct core_data *c, *c2; 3000c98d5d94SLen Brown struct pkg_data *p, *p2; 3001c98d5d94SLen Brown 30021b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 3003c98d5d94SLen Brown 3004c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 3005c98d5d94SLen Brown continue; 3006c98d5d94SLen Brown 30071b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no); 3008c98d5d94SLen Brown 30091b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 30101b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no); 3011c98d5d94SLen Brown 3012c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 3013c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 3014c98d5d94SLen Brown 3015c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 3016c98d5d94SLen Brown if (retval) 3017c98d5d94SLen Brown return retval; 3018c98d5d94SLen Brown } 3019c98d5d94SLen Brown } 3020c98d5d94SLen Brown } 302140f5cfe7SPrarit Bhargava } 3022c98d5d94SLen Brown return 0; 3023c98d5d94SLen Brown } 3024c98d5d94SLen Brown 3025c98d5d94SLen Brown /* 3026c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 3027c98d5d94SLen Brown * return max_cpu number 3028c98d5d94SLen Brown */ 3029c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int)) 3030103a8feaSLen Brown { 3031103a8feaSLen Brown FILE *fp; 3032c98d5d94SLen Brown int cpu_num; 3033103a8feaSLen Brown int retval; 3034103a8feaSLen Brown 303557a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 3036103a8feaSLen Brown 3037103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 3038b2c95d90SJosh Triplett if (retval != 0) 3039b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 3040103a8feaSLen Brown 3041c98d5d94SLen Brown while (1) { 3042c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 3043103a8feaSLen Brown if (retval != 1) 3044103a8feaSLen Brown break; 3045103a8feaSLen Brown 3046c98d5d94SLen Brown retval = func(cpu_num); 3047c98d5d94SLen Brown if (retval) { 3048c98d5d94SLen Brown fclose(fp); 3049c98d5d94SLen Brown return (retval); 3050c98d5d94SLen Brown } 3051103a8feaSLen Brown } 3052103a8feaSLen Brown fclose(fp); 3053c98d5d94SLen Brown return 0; 3054103a8feaSLen Brown } 3055103a8feaSLen Brown 3056103a8feaSLen Brown void re_initialize(void) 3057103a8feaSLen Brown { 3058c98d5d94SLen Brown free_all_buffers(); 3059c98d5d94SLen Brown setup_all_buffers(); 30603d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 3061103a8feaSLen Brown } 3062103a8feaSLen Brown 3063843c5791SPrarit Bhargava void set_max_cpu_num(void) 3064843c5791SPrarit Bhargava { 3065843c5791SPrarit Bhargava FILE *filep; 30668201a028SPrarit Bhargava int base_cpu; 3067843c5791SPrarit Bhargava unsigned long dummy; 30688201a028SPrarit Bhargava char pathname[64]; 3069843c5791SPrarit Bhargava 30708201a028SPrarit Bhargava base_cpu = sched_getcpu(); 30718201a028SPrarit Bhargava if (base_cpu < 0) 30728201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 30731b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu); 30748201a028SPrarit Bhargava 30758201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 3076843c5791SPrarit Bhargava topo.max_cpu_num = 0; 3077843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 30780e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 3079843c5791SPrarit Bhargava fclose(filep); 3080843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 3081843c5791SPrarit Bhargava } 3082c98d5d94SLen Brown 3083103a8feaSLen Brown /* 3084c98d5d94SLen Brown * count_cpus() 3085c98d5d94SLen Brown * remember the last one seen, it will be the max 3086103a8feaSLen Brown */ 3087c98d5d94SLen Brown int count_cpus(int cpu) 3088103a8feaSLen Brown { 30899878bf7aSLen Brown UNUSED(cpu); 30909878bf7aSLen Brown 3091843c5791SPrarit Bhargava topo.num_cpus++; 3092c98d5d94SLen Brown return 0; 3093103a8feaSLen Brown } 30941b439f01SLen Brown 3095c98d5d94SLen Brown int mark_cpu_present(int cpu) 3096c98d5d94SLen Brown { 3097c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 309815aaa346SLen Brown return 0; 3099103a8feaSLen Brown } 3100103a8feaSLen Brown 31018cb48b32SPrarit Bhargava int init_thread_id(int cpu) 31028cb48b32SPrarit Bhargava { 31038cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 31048cb48b32SPrarit Bhargava return 0; 31058cb48b32SPrarit Bhargava } 31068cb48b32SPrarit Bhargava 3107562a2d37SLen Brown /* 3108562a2d37SLen Brown * snapshot_proc_interrupts() 3109562a2d37SLen Brown * 3110562a2d37SLen Brown * read and record summary of /proc/interrupts 3111562a2d37SLen Brown * 3112562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3113562a2d37SLen Brown */ 3114562a2d37SLen Brown int snapshot_proc_interrupts(void) 3115562a2d37SLen Brown { 3116562a2d37SLen Brown static FILE *fp; 3117562a2d37SLen Brown int column, retval; 3118562a2d37SLen Brown 3119562a2d37SLen Brown if (fp == NULL) 3120562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3121562a2d37SLen Brown else 3122562a2d37SLen Brown rewind(fp); 3123562a2d37SLen Brown 3124562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3125562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3126562a2d37SLen Brown int cpu_number; 3127562a2d37SLen Brown 3128562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3129562a2d37SLen Brown if (retval != 1) 3130562a2d37SLen Brown break; 3131562a2d37SLen Brown 3132562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3133562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3134562a2d37SLen Brown return 1; 3135562a2d37SLen Brown } 3136562a2d37SLen Brown 3137562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3138562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3139562a2d37SLen Brown } 3140562a2d37SLen Brown 3141562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3142562a2d37SLen Brown while (1) { 3143562a2d37SLen Brown int column; 3144562a2d37SLen Brown char buf[64]; 3145562a2d37SLen Brown 3146562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3147562a2d37SLen Brown if (retval != 1) 3148562a2d37SLen Brown break; 3149562a2d37SLen Brown 3150562a2d37SLen Brown /* read the count per cpu */ 3151562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3152562a2d37SLen Brown 3153562a2d37SLen Brown int cpu_number, irq_count; 3154562a2d37SLen Brown 3155562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3156562a2d37SLen Brown if (retval != 1) 3157562a2d37SLen Brown break; 3158562a2d37SLen Brown 3159562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3160562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3161562a2d37SLen Brown 3162562a2d37SLen Brown } 3163562a2d37SLen Brown 31641b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */ 3165562a2d37SLen Brown 3166562a2d37SLen Brown } 3167562a2d37SLen Brown return 0; 3168562a2d37SLen Brown } 31691b439f01SLen Brown 317027d47356SLen Brown /* 3171fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3172fdf676e5SLen Brown * 3173fdf676e5SLen Brown * record snapshot of 3174fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3175fdf676e5SLen Brown * 3176fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3177fdf676e5SLen Brown */ 3178fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3179fdf676e5SLen Brown { 3180fdf676e5SLen Brown FILE *fp; 3181fdf676e5SLen Brown int retval; 3182fdf676e5SLen Brown 3183fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3184fdf676e5SLen Brown 3185fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3186fdf676e5SLen Brown if (retval != 1) 3187fdf676e5SLen Brown err(1, "GFX rc6"); 3188fdf676e5SLen Brown 3189fdf676e5SLen Brown fclose(fp); 3190fdf676e5SLen Brown 3191fdf676e5SLen Brown return 0; 3192fdf676e5SLen Brown } 31931b439f01SLen Brown 3194fdf676e5SLen Brown /* 319527d47356SLen Brown * snapshot_gfx_mhz() 319627d47356SLen Brown * 319727d47356SLen Brown * record snapshot of 319827d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 319927d47356SLen Brown * 320027d47356SLen Brown * return 1 if config change requires a restart, else return 0 320127d47356SLen Brown */ 320227d47356SLen Brown int snapshot_gfx_mhz(void) 320327d47356SLen Brown { 320427d47356SLen Brown static FILE *fp; 320527d47356SLen Brown int retval; 320627d47356SLen Brown 320727d47356SLen Brown if (fp == NULL) 320827d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 320922048c54SLen Brown else { 321027d47356SLen Brown rewind(fp); 321122048c54SLen Brown fflush(fp); 321222048c54SLen Brown } 321327d47356SLen Brown 321427d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 321527d47356SLen Brown if (retval != 1) 321627d47356SLen Brown err(1, "GFX MHz"); 321727d47356SLen Brown 321827d47356SLen Brown return 0; 321927d47356SLen Brown } 3220562a2d37SLen Brown 3221562a2d37SLen Brown /* 3222b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3223b4b91569SRafael Antognolli * 3224b4b91569SRafael Antognolli * record snapshot of 3225b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3226b4b91569SRafael Antognolli * 3227b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3228b4b91569SRafael Antognolli */ 3229b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3230b4b91569SRafael Antognolli { 3231b4b91569SRafael Antognolli static FILE *fp; 3232b4b91569SRafael Antognolli int retval; 3233b4b91569SRafael Antognolli 3234b4b91569SRafael Antognolli if (fp == NULL) 3235b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3236b4b91569SRafael Antognolli else { 3237b4b91569SRafael Antognolli rewind(fp); 3238b4b91569SRafael Antognolli fflush(fp); 3239b4b91569SRafael Antognolli } 3240b4b91569SRafael Antognolli 3241b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3242b4b91569SRafael Antognolli if (retval != 1) 3243b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3244b4b91569SRafael Antognolli 3245b4b91569SRafael Antognolli return 0; 3246b4b91569SRafael Antognolli } 3247b4b91569SRafael Antognolli 3248b4b91569SRafael Antognolli /* 3249be0e54c4SLen Brown * snapshot_cpu_lpi() 3250be0e54c4SLen Brown * 3251be0e54c4SLen Brown * record snapshot of 3252be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3253be0e54c4SLen Brown */ 3254be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3255be0e54c4SLen Brown { 3256be0e54c4SLen Brown FILE *fp; 3257be0e54c4SLen Brown int retval; 3258be0e54c4SLen Brown 3259be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3260be0e54c4SLen Brown 3261be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 32625ea7647bSPrarit Bhargava if (retval != 1) { 32635ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 32645ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3265605736c6SGustavo A. R. Silva fclose(fp); 32665ea7647bSPrarit Bhargava return -1; 32675ea7647bSPrarit Bhargava } 3268be0e54c4SLen Brown 3269be0e54c4SLen Brown fclose(fp); 3270be0e54c4SLen Brown 3271be0e54c4SLen Brown return 0; 3272be0e54c4SLen Brown } 32731b439f01SLen Brown 3274be0e54c4SLen Brown /* 3275be0e54c4SLen Brown * snapshot_sys_lpi() 3276be0e54c4SLen Brown * 32771f81c5efSLen Brown * record snapshot of sys_lpi_file 3278be0e54c4SLen Brown */ 3279be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3280be0e54c4SLen Brown { 3281be0e54c4SLen Brown FILE *fp; 3282be0e54c4SLen Brown int retval; 3283be0e54c4SLen Brown 32841f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3285be0e54c4SLen Brown 3286be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 32875ea7647bSPrarit Bhargava if (retval != 1) { 32885ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 32895ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 329015423b95SColin Ian King fclose(fp); 32915ea7647bSPrarit Bhargava return -1; 32925ea7647bSPrarit Bhargava } 3293be0e54c4SLen Brown fclose(fp); 3294be0e54c4SLen Brown 3295be0e54c4SLen Brown return 0; 3296be0e54c4SLen Brown } 32971b439f01SLen Brown 3298be0e54c4SLen Brown /* 3299562a2d37SLen Brown * snapshot /proc and /sys files 3300562a2d37SLen Brown * 3301562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3302562a2d37SLen Brown */ 3303562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3304562a2d37SLen Brown { 3305218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3306562a2d37SLen Brown if (snapshot_proc_interrupts()) 3307562a2d37SLen Brown return 1; 3308562a2d37SLen Brown 3309812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3310fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3311fdf676e5SLen Brown 3312812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 331327d47356SLen Brown snapshot_gfx_mhz(); 331427d47356SLen Brown 3315b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3316b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3317b4b91569SRafael Antognolli 3318be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3319be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3320be0e54c4SLen Brown 3321be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3322be0e54c4SLen Brown snapshot_sys_lpi_us(); 3323be0e54c4SLen Brown 3324562a2d37SLen Brown return 0; 3325562a2d37SLen Brown } 3326562a2d37SLen Brown 33278aa2ed0bSLen Brown int exit_requested; 33288aa2ed0bSLen Brown 33298aa2ed0bSLen Brown static void signal_handler(int signal) 33308aa2ed0bSLen Brown { 33318aa2ed0bSLen Brown switch (signal) { 33328aa2ed0bSLen Brown case SIGINT: 33338aa2ed0bSLen Brown exit_requested = 1; 33348aa2ed0bSLen Brown if (debug) 33358aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 33368aa2ed0bSLen Brown break; 333707211960SLen Brown case SIGUSR1: 333807211960SLen Brown if (debug > 1) 333907211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 334007211960SLen Brown break; 33418aa2ed0bSLen Brown } 33428aa2ed0bSLen Brown } 33438aa2ed0bSLen Brown 33448aa2ed0bSLen Brown void setup_signal_handler(void) 33458aa2ed0bSLen Brown { 33468aa2ed0bSLen Brown struct sigaction sa; 33478aa2ed0bSLen Brown 33488aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 33498aa2ed0bSLen Brown 33508aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 33518aa2ed0bSLen Brown 33528aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 33538aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 335407211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 335507211960SLen Brown err(1, "sigaction SIGUSR1"); 33568aa2ed0bSLen Brown } 3357b9ad8ee0SLen Brown 335847936f94SArtem Bityutskiy void do_sleep(void) 3359b9ad8ee0SLen Brown { 3360c026c236SArtem Bityutskiy struct timeval tout; 3361c026c236SArtem Bityutskiy struct timespec rest; 3362b9ad8ee0SLen Brown fd_set readfds; 3363b9ad8ee0SLen Brown int retval; 3364b9ad8ee0SLen Brown 3365b9ad8ee0SLen Brown FD_ZERO(&readfds); 3366b9ad8ee0SLen Brown FD_SET(0, &readfds); 3367b9ad8ee0SLen Brown 3368c026c236SArtem Bityutskiy if (ignore_stdin) { 336947936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 337047936f94SArtem Bityutskiy return; 337147936f94SArtem Bityutskiy } 3372b9ad8ee0SLen Brown 3373c026c236SArtem Bityutskiy tout = interval_tv; 3374c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3375b9ad8ee0SLen Brown 3376b9ad8ee0SLen Brown if (retval == 1) { 3377b9ad8ee0SLen Brown switch (getc(stdin)) { 3378b9ad8ee0SLen Brown case 'q': 3379b9ad8ee0SLen Brown exit_requested = 1; 3380b9ad8ee0SLen Brown break; 3381c026c236SArtem Bityutskiy case EOF: 3382c026c236SArtem Bityutskiy /* 3383c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3384c026c236SArtem Bityutskiy * won't be any further input. 3385c026c236SArtem Bityutskiy */ 3386c026c236SArtem Bityutskiy ignore_stdin = 1; 3387c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3388c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3389c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3390c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3391b9ad8ee0SLen Brown } 3392b9ad8ee0SLen Brown } 3393b9ad8ee0SLen Brown } 339447936f94SArtem Bityutskiy 339587e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 339687e15da9SChen Yu { 339787e15da9SChen Yu int ret, idx; 339887e15da9SChen Yu unsigned long long msr_cur, msr_last; 339987e15da9SChen Yu 340087e15da9SChen Yu if (!per_cpu_msr_sum) 340187e15da9SChen Yu return 1; 340287e15da9SChen Yu 340387e15da9SChen Yu idx = offset_to_idx(offset); 340487e15da9SChen Yu if (idx < 0) 340587e15da9SChen Yu return idx; 340687e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 340787e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 340887e15da9SChen Yu if (ret) 340987e15da9SChen Yu return ret; 341087e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 341187e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 341287e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 341387e15da9SChen Yu 341487e15da9SChen Yu return 0; 341587e15da9SChen Yu } 341687e15da9SChen Yu 341787e15da9SChen Yu timer_t timerid; 341887e15da9SChen Yu 341987e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 342087e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 342187e15da9SChen Yu { 342287e15da9SChen Yu int i, ret; 342387e15da9SChen Yu int cpu = t->cpu_id; 342487e15da9SChen Yu 34259878bf7aSLen Brown UNUSED(c); 34269878bf7aSLen Brown UNUSED(p); 34279878bf7aSLen Brown 342887e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 342987e15da9SChen Yu unsigned long long msr_cur, msr_last; 343013a779deSCalvin Walton off_t offset; 343187e15da9SChen Yu 343287e15da9SChen Yu if (!idx_valid(i)) 343387e15da9SChen Yu continue; 343487e15da9SChen Yu offset = idx_to_offset(i); 343587e15da9SChen Yu if (offset < 0) 343687e15da9SChen Yu continue; 343787e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 343887e15da9SChen Yu if (ret) { 34391b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset); 344087e15da9SChen Yu continue; 344187e15da9SChen Yu } 344287e15da9SChen Yu 344387e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 344487e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 344587e15da9SChen Yu 344687e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 344787e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 344887e15da9SChen Yu } 344987e15da9SChen Yu return 0; 345087e15da9SChen Yu } 345187e15da9SChen Yu 34521b439f01SLen Brown static void msr_record_handler(union sigval v) 345387e15da9SChen Yu { 34549878bf7aSLen Brown UNUSED(v); 34559878bf7aSLen Brown 345687e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 345787e15da9SChen Yu } 345887e15da9SChen Yu 345987e15da9SChen Yu void msr_sum_record(void) 346087e15da9SChen Yu { 346187e15da9SChen Yu struct itimerspec its; 346287e15da9SChen Yu struct sigevent sev; 346387e15da9SChen Yu 346487e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 346587e15da9SChen Yu if (!per_cpu_msr_sum) { 346687e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 346787e15da9SChen Yu return; 346887e15da9SChen Yu } 346987e15da9SChen Yu /* 347087e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 347187e15da9SChen Yu */ 347287e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 347387e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 347487e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 347587e15da9SChen Yu 347687e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 347787e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 347887e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 347987e15da9SChen Yu goto release_msr; 348087e15da9SChen Yu } 348187e15da9SChen Yu 348287e15da9SChen Yu its.it_value.tv_sec = 0; 348387e15da9SChen Yu its.it_value.tv_nsec = 1; 348487e15da9SChen Yu /* 348587e15da9SChen Yu * A wraparound time has been calculated early. 348687e15da9SChen Yu * Some sources state that the peak power for a 348787e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 348887e15da9SChen Yu * use 2 * TDP for safety. 348987e15da9SChen Yu */ 349087e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 349187e15da9SChen Yu its.it_interval.tv_nsec = 0; 349287e15da9SChen Yu 349387e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 349487e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 349587e15da9SChen Yu goto release_timer; 349687e15da9SChen Yu } 349787e15da9SChen Yu return; 349887e15da9SChen Yu 349987e15da9SChen Yu release_timer: 350087e15da9SChen Yu timer_delete(timerid); 350187e15da9SChen Yu release_msr: 350287e15da9SChen Yu free(per_cpu_msr_sum); 350387e15da9SChen Yu } 35044c2122d4SLen Brown 350538c6663aSLen Brown /* 350638c6663aSLen Brown * set_my_sched_priority(pri) 350738c6663aSLen Brown * return previous 350838c6663aSLen Brown */ 350938c6663aSLen Brown int set_my_sched_priority(int priority) 351038c6663aSLen Brown { 351138c6663aSLen Brown int retval; 351238c6663aSLen Brown int original_priority; 351338c6663aSLen Brown 351438c6663aSLen Brown errno = 0; 351538c6663aSLen Brown original_priority = getpriority(PRIO_PROCESS, 0); 351638c6663aSLen Brown if (errno && (original_priority == -1)) 351738c6663aSLen Brown err(errno, "getpriority"); 351838c6663aSLen Brown 351938c6663aSLen Brown retval = setpriority(PRIO_PROCESS, 0, priority); 352038c6663aSLen Brown if (retval) 3521de7839eeSLen Brown errx(retval, "capget(CAP_SYS_NICE) failed,try \"# setcap cap_sys_nice=ep %s\"", progname); 352238c6663aSLen Brown 352338c6663aSLen Brown errno = 0; 352438c6663aSLen Brown retval = getpriority(PRIO_PROCESS, 0); 352538c6663aSLen Brown if (retval != priority) 3526164d7a96SLen Brown err(retval, "getpriority(%d) != setpriority(%d)", retval, priority); 352738c6663aSLen Brown 352838c6663aSLen Brown return original_priority; 352938c6663aSLen Brown } 353038c6663aSLen Brown 3531103a8feaSLen Brown void turbostat_loop() 3532103a8feaSLen Brown { 3533c98d5d94SLen Brown int retval; 3534e52966c0SLen Brown int restarted = 0; 35359878bf7aSLen Brown unsigned int done_iters = 0; 3536c98d5d94SLen Brown 35378aa2ed0bSLen Brown setup_signal_handler(); 35388aa2ed0bSLen Brown 353938c6663aSLen Brown /* 354038c6663aSLen Brown * elevate own priority for interval mode 354138c6663aSLen Brown */ 354238c6663aSLen Brown set_my_sched_priority(-20); 354338c6663aSLen Brown 3544103a8feaSLen Brown restart: 3545e52966c0SLen Brown restarted++; 3546e52966c0SLen Brown 3547562a2d37SLen Brown snapshot_proc_sysfs_files(); 3548c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 35494c2122d4SLen Brown first_counter_read = 0; 3550d91bb17cSLen Brown if (retval < -1) { 3551d91bb17cSLen Brown exit(retval); 3552d91bb17cSLen Brown } else if (retval == -1) { 35533d7772eaSLen Brown if (restarted > 10) { 3554e52966c0SLen Brown exit(retval); 3555e52966c0SLen Brown } 3556c98d5d94SLen Brown re_initialize(); 3557c98d5d94SLen Brown goto restart; 3558c98d5d94SLen Brown } 3559e52966c0SLen Brown restarted = 0; 3560023fe0acSChen Yu done_iters = 0; 3561103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3562103a8feaSLen Brown 3563103a8feaSLen Brown while (1) { 3564c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3565103a8feaSLen Brown re_initialize(); 3566103a8feaSLen Brown goto restart; 3567103a8feaSLen Brown } 3568b9ad8ee0SLen Brown do_sleep(); 3569562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3570562a2d37SLen Brown goto restart; 3571c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3572d91bb17cSLen Brown if (retval < -1) { 3573d91bb17cSLen Brown exit(retval); 3574d91bb17cSLen Brown } else if (retval == -1) { 357515aaa346SLen Brown re_initialize(); 357615aaa346SLen Brown goto restart; 357715aaa346SLen Brown } 3578103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3579103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3580ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3581ba3dec99SLen Brown re_initialize(); 3582ba3dec99SLen Brown goto restart; 3583ba3dec99SLen Brown } 3584c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3585c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3586b7d8c148SLen Brown flush_output_stdout(); 35878aa2ed0bSLen Brown if (exit_requested) 35888aa2ed0bSLen Brown break; 3589023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3590023fe0acSChen Yu break; 3591b9ad8ee0SLen Brown do_sleep(); 3592562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3593562a2d37SLen Brown goto restart; 3594c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3595d91bb17cSLen Brown if (retval < -1) { 3596d91bb17cSLen Brown exit(retval); 3597d91bb17cSLen Brown } else if (retval == -1) { 3598103a8feaSLen Brown re_initialize(); 3599103a8feaSLen Brown goto restart; 3600103a8feaSLen Brown } 3601103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3602103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3603ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3604ba3dec99SLen Brown re_initialize(); 3605ba3dec99SLen Brown goto restart; 3606ba3dec99SLen Brown } 3607c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3608c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3609b7d8c148SLen Brown flush_output_stdout(); 36108aa2ed0bSLen Brown if (exit_requested) 36118aa2ed0bSLen Brown break; 3612023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3613023fe0acSChen Yu break; 3614103a8feaSLen Brown } 3615103a8feaSLen Brown } 3616103a8feaSLen Brown 3617103a8feaSLen Brown void check_dev_msr() 3618103a8feaSLen Brown { 3619103a8feaSLen Brown struct stat sb; 36207ce7d5deSPrarit Bhargava char pathname[32]; 3621103a8feaSLen Brown 36227ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 36237ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3624a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3625d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3626103a8feaSLen Brown } 3627103a8feaSLen Brown 3628fcaa681cSLen Brown /* 3629fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3630fcaa681cSLen Brown * return 0 on success 3631fcaa681cSLen Brown * return 1 on fail 3632fcaa681cSLen Brown */ 3633fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3634103a8feaSLen Brown { 3635fcaa681cSLen Brown cap_t caps; 3636fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3637fcaa681cSLen Brown 3638fcaa681cSLen Brown caps = cap_get_proc(); 3639fcaa681cSLen Brown if (caps == NULL) 3640fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3641fcaa681cSLen Brown 3642fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3643fcaa681cSLen Brown err(-6, "cap_get\n"); 3644fcaa681cSLen Brown 3645fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 36461b439f01SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3647fcaa681cSLen Brown return 1; 3648fcaa681cSLen Brown } 3649fcaa681cSLen Brown 3650fcaa681cSLen Brown if (cap_free(caps) == -1) 3651fcaa681cSLen Brown err(-6, "cap_free\n"); 3652fcaa681cSLen Brown 3653fcaa681cSLen Brown return 0; 3654fcaa681cSLen Brown } 36551b439f01SLen Brown 3656fcaa681cSLen Brown void check_permissions(void) 3657fcaa681cSLen Brown { 365898481e79SLen Brown int do_exit = 0; 36597ce7d5deSPrarit Bhargava char pathname[32]; 366098481e79SLen Brown 366198481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3662fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 366398481e79SLen Brown 366498481e79SLen Brown /* test file permissions */ 36657ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 36667ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 366798481e79SLen Brown do_exit++; 366898481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 366998481e79SLen Brown } 367098481e79SLen Brown 367198481e79SLen Brown /* if all else fails, thell them to be root */ 367298481e79SLen Brown if (do_exit) 3673b2c95d90SJosh Triplett if (getuid() != 0) 3674d7899447SLen Brown warnx("... or simply run as root"); 367598481e79SLen Brown 367698481e79SLen Brown if (do_exit) 367798481e79SLen Brown exit(-6); 3678103a8feaSLen Brown } 3679103a8feaSLen Brown 3680d7899447SLen Brown /* 3681d7899447SLen Brown * NHM adds support for additional MSRs: 3682d7899447SLen Brown * 3683d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3684d7899447SLen Brown * 3685ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 36861df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3687d7899447SLen Brown * 3688cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3689cf4cbe53SLen Brown * 3690d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3691d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3692d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3693d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3694d7899447SLen Brown * 3695ee7e38e3SLen Brown * Side effect: 36961df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 369733148d67SLen Brown * sets has_misc_feature_control 3698d7899447SLen Brown */ 3699ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3700103a8feaSLen Brown { 3701ee7e38e3SLen Brown unsigned long long msr; 370221ed5574SLen Brown unsigned int base_ratio; 3703ee7e38e3SLen Brown int *pkg_cstate_limits; 3704ee7e38e3SLen Brown 3705103a8feaSLen Brown if (!genuine_intel) 3706103a8feaSLen Brown return 0; 3707103a8feaSLen Brown 3708103a8feaSLen Brown if (family != 6) 3709103a8feaSLen Brown return 0; 3710103a8feaSLen Brown 371121ed5574SLen Brown bclk = discover_bclk(family, model); 371221ed5574SLen Brown 3713103a8feaSLen Brown switch (model) { 3714869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3715869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3716ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3717ee7e38e3SLen Brown break; 3718869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3719869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3720869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3721869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3722ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 372333148d67SLen Brown has_misc_feature_control = 1; 3724ee7e38e3SLen Brown break; 3725c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 37265e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3727869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 372877e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3729c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 37305e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3731869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3732af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3733af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3734ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 373533148d67SLen Brown has_misc_feature_control = 1; 3736ee7e38e3SLen Brown break; 3737d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 37380e4d42afSArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */ 3739d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 374033148d67SLen Brown has_misc_feature_control = 1; 3741d8ebb442SLen Brown break; 374225368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 374325368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits; 374425368d7cSChen Yu has_misc_feature_control = 1; 374525368d7cSChen Yu break; 3746f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3747cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 37489878bf7aSLen Brown /* FALLTHRU */ 37495ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3750ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3751ee7e38e3SLen Brown break; 3752869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3753ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3754cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3755ee7e38e3SLen Brown break; 3756869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3757ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3758ee7e38e3SLen Brown break; 3759869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3760f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 37615ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3762f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 376320de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3764445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3765e4085d54SLen Brown break; 3766103a8feaSLen Brown default: 3767103a8feaSLen Brown return 0; 3768103a8feaSLen Brown } 37691df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3770e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3771ee7e38e3SLen Brown 3772ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 377321ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 377421ed5574SLen Brown 377521ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 377621ed5574SLen Brown has_base_hz = 1; 3777ee7e38e3SLen Brown return 1; 3778103a8feaSLen Brown } 37791b439f01SLen Brown 37800f7887c4SLen Brown /* 3781495c7654SLen Brown * SLV client has support for unique MSRs: 37820f7887c4SLen Brown * 37830f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 37840f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 37850f7887c4SLen Brown */ 37860f7887c4SLen Brown 37870f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 37880f7887c4SLen Brown { 37890f7887c4SLen Brown if (!genuine_intel) 37900f7887c4SLen Brown return 0; 37910f7887c4SLen Brown 37929878bf7aSLen Brown if (family != 6) 37939878bf7aSLen Brown return 0; 37949878bf7aSLen Brown 37950f7887c4SLen Brown switch (model) { 3796f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3797f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3798f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 37990f7887c4SLen Brown return 1; 38000f7887c4SLen Brown } 38010f7887c4SLen Brown return 0; 38020f7887c4SLen Brown } 38031b439f01SLen Brown 38047170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 38057170a374SLen Brown { 38067170a374SLen Brown 38077170a374SLen Brown if (!genuine_intel) 38087170a374SLen Brown return 0; 38097170a374SLen Brown 38109878bf7aSLen Brown if (family != 6) 38119878bf7aSLen Brown return 0; 38129878bf7aSLen Brown 38137170a374SLen Brown switch (model) { 38145ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 38157170a374SLen Brown return 1; 38167170a374SLen Brown } 38177170a374SLen Brown return 0; 38187170a374SLen Brown } 38191b439f01SLen Brown 3820ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3821ade0ebacSLen Brown { 3822ade0ebacSLen Brown 3823ade0ebacSLen Brown if (!genuine_intel) 3824ade0ebacSLen Brown return 0; 3825ade0ebacSLen Brown 38269878bf7aSLen Brown if (family != 6) 38279878bf7aSLen Brown return 0; 38289878bf7aSLen Brown 3829ade0ebacSLen Brown switch (model) { 3830ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3831ade0ebacSLen Brown return 1; 3832ade0ebacSLen Brown } 3833ade0ebacSLen Brown return 0; 3834ade0ebacSLen Brown } 38351b439f01SLen Brown 383634c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 383734c76197SLen Brown { 383834c76197SLen Brown 383934c76197SLen Brown if (!genuine_intel) 384034c76197SLen Brown return 0; 384134c76197SLen Brown 38429878bf7aSLen Brown if (family != 6) 38439878bf7aSLen Brown return 0; 38449878bf7aSLen Brown 384534c76197SLen Brown switch (model) { 384634c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 384734c76197SLen Brown return 1; 384834c76197SLen Brown } 384934c76197SLen Brown return 0; 385034c76197SLen Brown } 385125368d7cSChen Yu 385225368d7cSChen Yu int is_icx(unsigned int family, unsigned int model) 385325368d7cSChen Yu { 385425368d7cSChen Yu 385525368d7cSChen Yu if (!genuine_intel) 385625368d7cSChen Yu return 0; 385725368d7cSChen Yu 38589878bf7aSLen Brown if (family != 6) 38599878bf7aSLen Brown return 0; 38609878bf7aSLen Brown 386125368d7cSChen Yu switch (model) { 386225368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 386325368d7cSChen Yu return 1; 386425368d7cSChen Yu } 386525368d7cSChen Yu return 0; 386625368d7cSChen Yu } 386725368d7cSChen Yu 3868684e40e9SArtem Bityutskiy int is_spr(unsigned int family, unsigned int model) 3869684e40e9SArtem Bityutskiy { 3870684e40e9SArtem Bityutskiy 3871684e40e9SArtem Bityutskiy if (!genuine_intel) 3872684e40e9SArtem Bityutskiy return 0; 3873684e40e9SArtem Bityutskiy 3874684e40e9SArtem Bityutskiy if (family != 6) 3875684e40e9SArtem Bityutskiy return 0; 3876684e40e9SArtem Bityutskiy 3877684e40e9SArtem Bityutskiy switch (model) { 3878684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: 3879684e40e9SArtem Bityutskiy return 1; 3880684e40e9SArtem Bityutskiy } 3881684e40e9SArtem Bityutskiy return 0; 3882684e40e9SArtem Bityutskiy } 3883684e40e9SArtem Bityutskiy 3884f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3885f6708400SChen Yu { 3886f6708400SChen Yu if (!genuine_intel) 3887f6708400SChen Yu return 0; 3888f6708400SChen Yu 38899878bf7aSLen Brown if (family != 6) 38909878bf7aSLen Brown return 0; 38919878bf7aSLen Brown 3892f6708400SChen Yu switch (model) { 3893f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3894f6708400SChen Yu return 1; 3895f6708400SChen Yu } 3896f6708400SChen Yu return 0; 3897f6708400SChen Yu } 38981b439f01SLen Brown 389920de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 390020de0dabSAntti Laakso { 390120de0dabSAntti Laakso if (!genuine_intel) 390220de0dabSAntti Laakso return 0; 390320de0dabSAntti Laakso 39049878bf7aSLen Brown if (family != 6) 39059878bf7aSLen Brown return 0; 39069878bf7aSLen Brown 390720de0dabSAntti Laakso switch (model) { 390820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 390920de0dabSAntti Laakso return 1; 391020de0dabSAntti Laakso } 391120de0dabSAntti Laakso return 0; 391220de0dabSAntti Laakso } 39130f7887c4SLen Brown 391431e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3915d7899447SLen Brown { 39160f7887c4SLen Brown if (has_slv_msrs(family, model)) 39170f7887c4SLen Brown return 0; 39180f7887c4SLen Brown 39199878bf7aSLen Brown if (family != 6) 39209878bf7aSLen Brown return 0; 39219878bf7aSLen Brown 3922d7899447SLen Brown switch (model) { 3923d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3924869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3925869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3926d7899447SLen Brown return 0; 3927d7899447SLen Brown default: 3928d7899447SLen Brown return 1; 3929d7899447SLen Brown } 3930d7899447SLen Brown } 39311b439f01SLen Brown 39320f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 39330f7887c4SLen Brown { 39340f7887c4SLen Brown if (has_slv_msrs(family, model)) 39350f7887c4SLen Brown return 1; 39360f7887c4SLen Brown 39370f7887c4SLen Brown return 0; 39380f7887c4SLen Brown } 39391b439f01SLen Brown 39406574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 39416574a5d5SLen Brown { 39426574a5d5SLen Brown if (!genuine_intel) 39436574a5d5SLen Brown return 0; 39446574a5d5SLen Brown 39456574a5d5SLen Brown if (family != 6) 39466574a5d5SLen Brown return 0; 39476574a5d5SLen Brown 39486574a5d5SLen Brown switch (model) { 3949869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3950869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 39516574a5d5SLen Brown return 1; 39526574a5d5SLen Brown default: 39536574a5d5SLen Brown return 0; 39546574a5d5SLen Brown } 39556574a5d5SLen Brown } 39561b439f01SLen Brown 3957fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3958fcd17211SLen Brown { 3959fcd17211SLen Brown if (!genuine_intel) 3960fcd17211SLen Brown return 0; 3961fcd17211SLen Brown 3962fcd17211SLen Brown if (family != 6) 3963fcd17211SLen Brown return 0; 3964fcd17211SLen Brown 3965fcd17211SLen Brown switch (model) { 3966869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3967fcd17211SLen Brown return 1; 3968fcd17211SLen Brown default: 3969fcd17211SLen Brown return 0; 3970fcd17211SLen Brown } 3971fcd17211SLen Brown } 3972fcd17211SLen Brown 3973fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3974fb5d4327SDasaratharaman Chandramouli { 3975fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3976fb5d4327SDasaratharaman Chandramouli return 0; 3977fb5d4327SDasaratharaman Chandramouli 3978fb5d4327SDasaratharaman Chandramouli if (family != 6) 3979fb5d4327SDasaratharaman Chandramouli return 0; 3980fb5d4327SDasaratharaman Chandramouli 3981fb5d4327SDasaratharaman Chandramouli switch (model) { 3982869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3983fb5d4327SDasaratharaman Chandramouli return 1; 3984fb5d4327SDasaratharaman Chandramouli default: 3985fb5d4327SDasaratharaman Chandramouli return 0; 3986fb5d4327SDasaratharaman Chandramouli } 3987fb5d4327SDasaratharaman Chandramouli } 39881b439f01SLen Brown 398931e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 399031e07522SLen Brown { 399131e07522SLen Brown if (!genuine_intel) 399231e07522SLen Brown return 0; 399331e07522SLen Brown 399431e07522SLen Brown if (family != 6) 399531e07522SLen Brown return 0; 399631e07522SLen Brown 399731e07522SLen Brown switch (model) { 399831e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 399931e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 400025368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 4001684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: 400231e07522SLen Brown return 1; 400331e07522SLen Brown default: 400431e07522SLen Brown return 0; 400531e07522SLen Brown } 400631e07522SLen Brown } 40071b439f01SLen Brown 40086fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 40096fb3143bSLen Brown { 40106fb3143bSLen Brown if (!genuine_intel) 40116fb3143bSLen Brown return 0; 40126fb3143bSLen Brown 40136fb3143bSLen Brown if (family != 6) 40146fb3143bSLen Brown return 0; 40156fb3143bSLen Brown 40166fb3143bSLen Brown switch (model) { 4017869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4018c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4019869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 402077e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 40215e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4022c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 40235e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4024869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4025af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4026af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4027869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 402825368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4029684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */ 4030869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 40316fb3143bSLen Brown return 1; 40326fb3143bSLen Brown default: 40336fb3143bSLen Brown return 0; 40346fb3143bSLen Brown } 40356fb3143bSLen Brown } 40366fb3143bSLen Brown 40370b9a0b9bSZhang Rui /* 40380b9a0b9bSZhang Rui * tcc_offset_bits: 40390b9a0b9bSZhang Rui * 0: Tcc Offset not supported (Default) 40400b9a0b9bSZhang Rui * 6: Bit 29:24 of MSR_PLATFORM_INFO 40410b9a0b9bSZhang Rui * 4: Bit 27:24 of MSR_PLATFORM_INFO 40420b9a0b9bSZhang Rui */ 40430b9a0b9bSZhang Rui void check_tcc_offset(int model) 40440b9a0b9bSZhang Rui { 40450b9a0b9bSZhang Rui unsigned long long msr; 40460b9a0b9bSZhang Rui 40470b9a0b9bSZhang Rui if (!genuine_intel) 40480b9a0b9bSZhang Rui return; 40490b9a0b9bSZhang Rui 40500b9a0b9bSZhang Rui switch (model) { 40510b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE_L: 40520b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE: 40530b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE_L: 40540b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE: 40550b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE_L: 40560b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE: 40570b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE_L: 40580b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE: 40590b9a0b9bSZhang Rui case INTEL_FAM6_COMETLAKE: 40600b9a0b9bSZhang Rui if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) { 40610b9a0b9bSZhang Rui msr = (msr >> 30) & 1; 40620b9a0b9bSZhang Rui if (msr) 40630b9a0b9bSZhang Rui tcc_offset_bits = 6; 40640b9a0b9bSZhang Rui } 40650b9a0b9bSZhang Rui return; 40660b9a0b9bSZhang Rui default: 40670b9a0b9bSZhang Rui return; 40680b9a0b9bSZhang Rui } 40690b9a0b9bSZhang Rui } 40700b9a0b9bSZhang Rui 40711b439f01SLen Brown static void remove_underbar(char *s) 4072fecb3bc8SDavid Arcari { 4073fecb3bc8SDavid Arcari char *to = s; 4074fecb3bc8SDavid Arcari 4075fecb3bc8SDavid Arcari while (*s) { 4076fecb3bc8SDavid Arcari if (*s != '_') 4077fecb3bc8SDavid Arcari *to++ = *s; 4078fecb3bc8SDavid Arcari s++; 4079fecb3bc8SDavid Arcari } 4080fecb3bc8SDavid Arcari 4081fecb3bc8SDavid Arcari *to = 0; 4082fecb3bc8SDavid Arcari } 4083fecb3bc8SDavid Arcari 40843ea8e52eSArtem Bityutskiy static void dump_turbo_ratio_info(unsigned int family, unsigned int model) 4085fcd17211SLen Brown { 40863ea8e52eSArtem Bityutskiy if (!has_turbo) 4087fcd17211SLen Brown return; 4088fcd17211SLen Brown 4089fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 4090fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 4091fcd17211SLen Brown 4092fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 4093fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 4094fcd17211SLen Brown 40954af184eeSLen Brown if (has_turbo_ratio_limit(family, model)) { 40964af184eeSLen Brown dump_turbo_ratio_limits(MSR_TURBO_RATIO_LIMIT, family, model); 40974af184eeSLen Brown 40984af184eeSLen Brown if (is_hybrid) 40994af184eeSLen Brown dump_turbo_ratio_limits(MSR_SECONDARY_TURBO_RATIO_LIMIT, family, model); 41004af184eeSLen Brown } 4101fcd17211SLen Brown 41020f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 41030f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 41040f7887c4SLen Brown 4105fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 4106fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 4107fb5d4327SDasaratharaman Chandramouli 41086fb3143bSLen Brown if (has_config_tdp(family, model)) 41096fb3143bSLen Brown dump_config_tdp(); 41103ea8e52eSArtem Bityutskiy } 41116fb3143bSLen Brown 41123ea8e52eSArtem Bityutskiy static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 41133ea8e52eSArtem Bityutskiy { 41143ea8e52eSArtem Bityutskiy if (!do_nhm_platform_info) 41153ea8e52eSArtem Bityutskiy return; 41163ea8e52eSArtem Bityutskiy 41173ea8e52eSArtem Bityutskiy dump_nhm_platform_info(); 41183ea8e52eSArtem Bityutskiy dump_turbo_ratio_info(family, model); 4119fcd17211SLen Brown dump_nhm_cst_cfg(); 4120fcd17211SLen Brown } 4121fcd17211SLen Brown 4122a5c6d65dSLen Brown static int read_sysfs_int(char *path) 4123a5c6d65dSLen Brown { 4124a5c6d65dSLen Brown FILE *input; 4125a5c6d65dSLen Brown int retval = -1; 4126a5c6d65dSLen Brown 4127a5c6d65dSLen Brown input = fopen(path, "r"); 4128a5c6d65dSLen Brown if (input == NULL) { 4129a5c6d65dSLen Brown if (debug) 4130a5c6d65dSLen Brown fprintf(outf, "NSFOD %s\n", path); 4131a5c6d65dSLen Brown return (-1); 4132a5c6d65dSLen Brown } 4133a5c6d65dSLen Brown if (fscanf(input, "%d", &retval) != 1) 4134a5c6d65dSLen Brown err(1, "%s: failed to read int from file", path); 4135a5c6d65dSLen Brown fclose(input); 4136a5c6d65dSLen Brown 4137a5c6d65dSLen Brown return (retval); 4138a5c6d65dSLen Brown } 4139a5c6d65dSLen Brown 4140abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 4141abdcbdb2SLen Brown { 4142abdcbdb2SLen Brown FILE *input; 4143abdcbdb2SLen Brown char cpuidle_buf[64]; 4144abdcbdb2SLen Brown 4145abdcbdb2SLen Brown input = fopen(path, "r"); 4146abdcbdb2SLen Brown if (input == NULL) { 4147abdcbdb2SLen Brown if (debug) 4148abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 4149abdcbdb2SLen Brown return; 4150abdcbdb2SLen Brown } 4151abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 4152abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 4153abdcbdb2SLen Brown fclose(input); 4154abdcbdb2SLen Brown 4155abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 4156abdcbdb2SLen Brown } 41571b439f01SLen Brown 4158a5c6d65dSLen Brown static void intel_uncore_frequency_probe(void) 4159a5c6d65dSLen Brown { 4160a5c6d65dSLen Brown int i, j; 4161a5c6d65dSLen Brown char path[128]; 4162a5c6d65dSLen Brown 4163a5c6d65dSLen Brown if (!genuine_intel) 4164a5c6d65dSLen Brown return; 4165a5c6d65dSLen Brown 4166a5c6d65dSLen Brown if (access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00", R_OK)) 4167a5c6d65dSLen Brown return; 4168a5c6d65dSLen Brown 4169a5c6d65dSLen Brown if (!access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00/current_freq_khz", R_OK)) 4170a5c6d65dSLen Brown BIC_PRESENT(BIC_UNCORE_MHZ); 4171a5c6d65dSLen Brown 4172a5c6d65dSLen Brown if (quiet) 4173a5c6d65dSLen Brown return; 4174a5c6d65dSLen Brown 4175a5c6d65dSLen Brown for (i = 0; i < topo.num_packages; ++i) { 4176a5c6d65dSLen Brown for (j = 0; j < topo.num_die; ++j) { 4177a5c6d65dSLen Brown int k, l; 4178a5c6d65dSLen Brown 4179a5c6d65dSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/min_freq_khz", 4180a5c6d65dSLen Brown i, j); 4181a5c6d65dSLen Brown k = read_sysfs_int(path); 4182a5c6d65dSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/max_freq_khz", 4183a5c6d65dSLen Brown i, j); 4184a5c6d65dSLen Brown l = read_sysfs_int(path); 4185a5c6d65dSLen Brown fprintf(outf, "Uncore Frequency pkg%d die%d: %d - %d MHz ", i, j, k / 1000, l / 1000); 4186a5c6d65dSLen Brown 4187a5c6d65dSLen Brown sprintf(path, 4188a5c6d65dSLen Brown "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_min_freq_khz", 4189a5c6d65dSLen Brown i, j); 4190a5c6d65dSLen Brown k = read_sysfs_int(path); 4191a5c6d65dSLen Brown sprintf(path, 4192a5c6d65dSLen Brown "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_max_freq_khz", 4193a5c6d65dSLen Brown i, j); 4194a5c6d65dSLen Brown l = read_sysfs_int(path); 4195a5c6d65dSLen Brown fprintf(outf, "(%d - %d MHz)\n", k / 1000, l / 1000); 4196a5c6d65dSLen Brown } 4197a5c6d65dSLen Brown } 4198a5c6d65dSLen Brown } 4199a5c6d65dSLen Brown 42001b439f01SLen Brown static void dump_sysfs_cstate_config(void) 420141618e63SLen Brown { 420241618e63SLen Brown char path[64]; 420341618e63SLen Brown char name_buf[16]; 420441618e63SLen Brown char desc[64]; 420541618e63SLen Brown FILE *input; 420641618e63SLen Brown int state; 420741618e63SLen Brown char *sp; 420841618e63SLen Brown 4209abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 4210abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 4211abdcbdb2SLen Brown return; 4212abdcbdb2SLen Brown } 4213abdcbdb2SLen Brown 4214abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 4215abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 4216abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 4217abdcbdb2SLen Brown 421841618e63SLen Brown for (state = 0; state < 10; ++state) { 421941618e63SLen Brown 42201b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 422141618e63SLen Brown input = fopen(path, "r"); 422241618e63SLen Brown if (input == NULL) 422341618e63SLen Brown continue; 42248173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 42258173c336SBen Hutchings err(1, "%s: failed to read file", path); 422641618e63SLen Brown 422741618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 422841618e63SLen Brown sp = strchr(name_buf, '-'); 422941618e63SLen Brown if (!sp) 423041618e63SLen Brown sp = strchrnul(name_buf, '\n'); 423141618e63SLen Brown *sp = '\0'; 423241618e63SLen Brown fclose(input); 423341618e63SLen Brown 4234fecb3bc8SDavid Arcari remove_underbar(name_buf); 4235fecb3bc8SDavid Arcari 42361b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state); 423741618e63SLen Brown input = fopen(path, "r"); 423841618e63SLen Brown if (input == NULL) 423941618e63SLen Brown continue; 42408173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 42418173c336SBen Hutchings err(1, "%s: failed to read file", path); 424241618e63SLen Brown 424341618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 424441618e63SLen Brown fclose(input); 424541618e63SLen Brown } 424641618e63SLen Brown } 42471b439f01SLen Brown 42481b439f01SLen Brown static void dump_sysfs_pstate_config(void) 42497293fccdSLen Brown { 42507293fccdSLen Brown char path[64]; 42517293fccdSLen Brown char driver_buf[64]; 42527293fccdSLen Brown char governor_buf[64]; 42537293fccdSLen Brown FILE *input; 42547293fccdSLen Brown int turbo; 42557293fccdSLen Brown 42561b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu); 42577293fccdSLen Brown input = fopen(path, "r"); 42587293fccdSLen Brown if (input == NULL) { 42590a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 42607293fccdSLen Brown return; 42617293fccdSLen Brown } 42628173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 42638173c336SBen Hutchings err(1, "%s: failed to read file", path); 42647293fccdSLen Brown fclose(input); 42657293fccdSLen Brown 42661b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu); 42677293fccdSLen Brown input = fopen(path, "r"); 42687293fccdSLen Brown if (input == NULL) { 42690a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 42707293fccdSLen Brown return; 42717293fccdSLen Brown } 42728173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 42738173c336SBen Hutchings err(1, "%s: failed to read file", path); 42747293fccdSLen Brown fclose(input); 42757293fccdSLen Brown 42767293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 42777293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 42787293fccdSLen Brown 42797293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 42807293fccdSLen Brown input = fopen(path, "r"); 42817293fccdSLen Brown if (input != NULL) { 42828173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 42838173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 42847293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 42857293fccdSLen Brown fclose(input); 42867293fccdSLen Brown } 42877293fccdSLen Brown 42887293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 42897293fccdSLen Brown input = fopen(path, "r"); 42907293fccdSLen Brown if (input != NULL) { 42918173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 42928173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 42937293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 42947293fccdSLen Brown fclose(input); 42957293fccdSLen Brown } 42967293fccdSLen Brown } 429741618e63SLen Brown 4298889facbeSLen Brown /* 4299889facbeSLen Brown * print_epb() 4300889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 4301889facbeSLen Brown */ 4302889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4303889facbeSLen Brown { 4304889facbeSLen Brown char *epb_string; 43056d6501d9SBorislav Petkov int cpu, epb; 4306889facbeSLen Brown 43079878bf7aSLen Brown UNUSED(c); 43089878bf7aSLen Brown UNUSED(p); 43099878bf7aSLen Brown 4310889facbeSLen Brown if (!has_epb) 4311889facbeSLen Brown return 0; 4312889facbeSLen Brown 4313889facbeSLen Brown cpu = t->cpu_id; 4314889facbeSLen Brown 4315889facbeSLen Brown /* EPB is per-package */ 4316889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4317889facbeSLen Brown return 0; 4318889facbeSLen Brown 4319889facbeSLen Brown if (cpu_migrate(cpu)) { 43203d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 4321889facbeSLen Brown return -1; 4322889facbeSLen Brown } 4323889facbeSLen Brown 43246d6501d9SBorislav Petkov epb = get_epb(cpu); 43256d6501d9SBorislav Petkov if (epb < 0) 4326889facbeSLen Brown return 0; 4327889facbeSLen Brown 43286d6501d9SBorislav Petkov switch (epb) { 4329889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 4330889facbeSLen Brown epb_string = "performance"; 4331889facbeSLen Brown break; 4332889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 4333889facbeSLen Brown epb_string = "balanced"; 4334889facbeSLen Brown break; 4335889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 4336889facbeSLen Brown epb_string = "powersave"; 4337889facbeSLen Brown break; 4338889facbeSLen Brown default: 4339889facbeSLen Brown epb_string = "custom"; 4340889facbeSLen Brown break; 4341889facbeSLen Brown } 43426d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 4343889facbeSLen Brown 4344889facbeSLen Brown return 0; 4345889facbeSLen Brown } 43461b439f01SLen Brown 43477f5c258eSLen Brown /* 43487f5c258eSLen Brown * print_hwp() 43497f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 43507f5c258eSLen Brown */ 43517f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 43527f5c258eSLen Brown { 43537f5c258eSLen Brown unsigned long long msr; 43547f5c258eSLen Brown int cpu; 43557f5c258eSLen Brown 43569878bf7aSLen Brown UNUSED(c); 43579878bf7aSLen Brown UNUSED(p); 43589878bf7aSLen Brown 43597f5c258eSLen Brown if (!has_hwp) 43607f5c258eSLen Brown return 0; 43617f5c258eSLen Brown 43627f5c258eSLen Brown cpu = t->cpu_id; 43637f5c258eSLen Brown 43647f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 43657f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 43667f5c258eSLen Brown return 0; 43677f5c258eSLen Brown 43687f5c258eSLen Brown if (cpu_migrate(cpu)) { 43693d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 43707f5c258eSLen Brown return -1; 43717f5c258eSLen Brown } 43727f5c258eSLen Brown 43737f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 43747f5c258eSLen Brown return 0; 43757f5c258eSLen Brown 43761b439f01SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 43777f5c258eSLen Brown 43787f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 43797f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 43807f5c258eSLen Brown return 0; 43817f5c258eSLen Brown 43827f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 43837f5c258eSLen Brown return 0; 43847f5c258eSLen Brown 4385b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 43866dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 43877f5c258eSLen Brown cpu, msr, 43887f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 43897f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 43901b439f01SLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr)); 43917f5c258eSLen Brown 43927f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 43937f5c258eSLen Brown return 0; 43947f5c258eSLen Brown 4395b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 43966dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 43977f5c258eSLen Brown cpu, msr, 43987f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 43997f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 44007f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 44017f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 44021b439f01SLen Brown (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1)); 44037f5c258eSLen Brown 44047f5c258eSLen Brown if (has_hwp_pkg) { 44057f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 44067f5c258eSLen Brown return 0; 44077f5c258eSLen Brown 4408b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 44096dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 44107f5c258eSLen Brown cpu, msr, 44117f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 44127f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 44137f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 44141b439f01SLen Brown (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3)); 44157f5c258eSLen Brown } 44167f5c258eSLen Brown if (has_hwp_notify) { 44177f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 44187f5c258eSLen Brown return 0; 44197f5c258eSLen Brown 4420b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 44217f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 44221b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis"); 44237f5c258eSLen Brown } 44247f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 44257f5c258eSLen Brown return 0; 44267f5c258eSLen Brown 4427b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 44287f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 442992c25393SAntti Laakso cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x4) ? "" : "No-"); 4430889facbeSLen Brown 4431889facbeSLen Brown return 0; 4432889facbeSLen Brown } 4433889facbeSLen Brown 44343a9a941dSLen Brown /* 44353a9a941dSLen Brown * print_perf_limit() 44363a9a941dSLen Brown */ 44373a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 44383a9a941dSLen Brown { 44393a9a941dSLen Brown unsigned long long msr; 44403a9a941dSLen Brown int cpu; 44413a9a941dSLen Brown 44429878bf7aSLen Brown UNUSED(c); 44439878bf7aSLen Brown UNUSED(p); 44449878bf7aSLen Brown 44453a9a941dSLen Brown cpu = t->cpu_id; 44463a9a941dSLen Brown 44473a9a941dSLen Brown /* per-package */ 44483a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 44493a9a941dSLen Brown return 0; 44503a9a941dSLen Brown 44513a9a941dSLen Brown if (cpu_migrate(cpu)) { 44523d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 44533a9a941dSLen Brown return -1; 44543a9a941dSLen Brown } 44553a9a941dSLen Brown 44563a9a941dSLen Brown if (do_core_perf_limit_reasons) { 44573a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4458b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4459b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4460e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 44613a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4462e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4463e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4464e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4465e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4466e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4467e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4468e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4469e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4470e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4471e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 44721b439f01SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : ""); 4473b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4474e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 44753a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4476e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4477e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4478e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4479e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4480e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4481e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4482e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4483e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4484e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4485e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 44861b439f01SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : ""); 44873a9a941dSLen Brown 44883a9a941dSLen Brown } 44893a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 44903a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4491b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4492b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 44933a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 44943a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 44953a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 44963a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 44973a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 44983a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 44991b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4500b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 45013a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 45023a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 45033a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 45043a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 45053a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 45063a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 45071b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 45083a9a941dSLen Brown } 45093a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 45103a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4511b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4512b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 45133a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 45143a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 45153a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 45163a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 45171b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4518b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 45193a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 45203a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 45213a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 45223a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 45231b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 45243a9a941dSLen Brown } 45253a9a941dSLen Brown return 0; 45263a9a941dSLen Brown } 45273a9a941dSLen Brown 4528889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4529889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4530889facbeSLen Brown 45319392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4532144b44b1SLen Brown { 4533144b44b1SLen Brown unsigned long long msr; 4534144b44b1SLen Brown 4535144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 45367ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4537144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4538144b44b1SLen Brown 4539144b44b1SLen Brown switch (model) { 4540f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 45415ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4542144b44b1SLen Brown return 30.0; 4543144b44b1SLen Brown default: 4544144b44b1SLen Brown return 135.0; 4545144b44b1SLen Brown } 4546144b44b1SLen Brown } 4547144b44b1SLen Brown 45489392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 45499392bd98SCalvin Walton { 45509878bf7aSLen Brown UNUSED(family); 45519878bf7aSLen Brown 455233eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 455333eb8225SKim Phillips return 280.0; 45549392bd98SCalvin Walton } 45559392bd98SCalvin Walton 455640ee8e3bSAndrey Semin /* 455740ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 455840ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 455940ee8e3bSAndrey Semin */ 45601b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units) 456140ee8e3bSAndrey Semin { 456240ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 456340ee8e3bSAndrey Semin 456440ee8e3bSAndrey Semin switch (model) { 4565869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4566869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4567abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4568869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 45696397b641SLen Brown case INTEL_FAM6_ICELAKE_X: /* ICX */ 457040ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 457140ee8e3bSAndrey Semin default: 457240ee8e3bSAndrey Semin return (rapl_energy_units); 457340ee8e3bSAndrey Semin } 457440ee8e3bSAndrey Semin } 457540ee8e3bSAndrey Semin 45769392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4577889facbeSLen Brown { 4578889facbeSLen Brown unsigned long long msr; 4579144b44b1SLen Brown unsigned int time_unit; 4580889facbeSLen Brown double tdp; 4581889facbeSLen Brown 4582889facbeSLen Brown if (family != 6) 4583889facbeSLen Brown return; 4584889facbeSLen Brown 4585889facbeSLen Brown switch (model) { 4586869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4587869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4588c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 458977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 45905e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4591c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 45925e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4593144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4594812db3f7SLen Brown if (rapl_joules) { 4595812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4596812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4597812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4598812db3f7SLen Brown } else { 4599812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4600812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4601812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4602812db3f7SLen Brown } 4603889facbeSLen Brown break; 4604869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4605f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4606e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4607812db3f7SLen Brown if (rapl_joules) 4608812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4609812db3f7SLen Brown else 4610812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4611e4085d54SLen Brown break; 4612f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 46131b439f01SLen Brown do_rapl = 46141b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 46151b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4616f6708400SChen Yu if (rapl_joules) { 4617f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4618f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4619f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4620f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4621f6708400SChen Yu } else { 4622f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4623f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4624f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4625f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4626f6708400SChen Yu } 4627f6708400SChen Yu break; 462820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 462920de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 463020de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 463120de0dabSAntti Laakso if (rapl_joules) 463220de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 463320de0dabSAntti Laakso else 463420de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 463520de0dabSAntti Laakso break; 4636af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4637af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 46381b439f01SLen Brown do_rapl = 46391b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 46401b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4641812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4642812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4643812db3f7SLen Brown if (rapl_joules) { 4644812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4645812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4646812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 464781824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4648812db3f7SLen Brown } else { 4649812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4650812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4651812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 465281824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4653812db3f7SLen Brown } 46540b2bb692SLen Brown break; 4655869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4656869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4657869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 465825368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4659684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */ 4660869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 46611b439f01SLen Brown do_rapl = 46621b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 46631b439f01SLen Brown RAPL_PKG_POWER_INFO; 4664812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4665812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4666812db3f7SLen Brown if (rapl_joules) { 4667812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4668812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4669812db3f7SLen Brown } else { 4670812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4671812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4672812db3f7SLen Brown } 4673e6f9bb3cSLen Brown break; 4674869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4675869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 46761b439f01SLen Brown do_rapl = 46771b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | 46781b439f01SLen Brown RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4679812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4680812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4681812db3f7SLen Brown if (rapl_joules) { 4682812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4683812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4684812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4685812db3f7SLen Brown } else { 4686812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4687812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4688812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4689812db3f7SLen Brown } 4690144b44b1SLen Brown break; 4691f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 46925ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4693144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4694812db3f7SLen Brown if (rapl_joules) { 4695812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4696812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4697812db3f7SLen Brown } else { 4698812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4699812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4700812db3f7SLen Brown } 4701889facbeSLen Brown break; 47025ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 47031b439f01SLen Brown do_rapl = 47041b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 47051b439f01SLen Brown RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4706812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4707812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4708812db3f7SLen Brown if (rapl_joules) { 4709812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4710812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4711812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4712812db3f7SLen Brown } else { 4713812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4714812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4715812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4716812db3f7SLen Brown } 47170f644909SJacob Pan break; 4718889facbeSLen Brown default: 4719889facbeSLen Brown return; 4720889facbeSLen Brown } 4721889facbeSLen Brown 4722889facbeSLen Brown /* units on package 0, verify later other packages match */ 47237ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4724889facbeSLen Brown return; 4725889facbeSLen Brown 4726889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4727f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4728144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4729144b44b1SLen Brown else 4730889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4731889facbeSLen Brown 473240ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 473340ee8e3bSAndrey Semin 4734144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4735144b44b1SLen Brown if (time_unit == 0) 4736144b44b1SLen Brown time_unit = 0xA; 4737889facbeSLen Brown 4738144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4739144b44b1SLen Brown 47409392bd98SCalvin Walton tdp = get_tdp_intel(model); 4741889facbeSLen Brown 4742889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 474396e47158SLen Brown if (!quiet) 4744b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 47459392bd98SCalvin Walton } 4746889facbeSLen Brown 47479392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 47489392bd98SCalvin Walton { 47499392bd98SCalvin Walton unsigned long long msr; 47509392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 47519392bd98SCalvin Walton unsigned int has_rapl = 0; 47529392bd98SCalvin Walton double tdp; 47539392bd98SCalvin Walton 47549878bf7aSLen Brown UNUSED(model); 47559878bf7aSLen Brown 47569392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 47579392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 475833eb8225SKim Phillips /* RAPL (Fam 17h+) */ 47599392bd98SCalvin Walton has_rapl = edx & (1 << 14); 47609392bd98SCalvin Walton } 47619392bd98SCalvin Walton 476233eb8225SKim Phillips if (!has_rapl || family < 0x17) 4763889facbeSLen Brown return; 47649392bd98SCalvin Walton 47659392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 47663316f99aSCalvin Walton if (rapl_joules) { 47673316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 47689392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 47693316f99aSCalvin Walton } else { 47703316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 47719392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 47723316f99aSCalvin Walton } 47739392bd98SCalvin Walton 47749392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 47759392bd98SCalvin Walton return; 47769392bd98SCalvin Walton 47779392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 47789392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 47799392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 47809392bd98SCalvin Walton 47819cfa8e04SPu Wen tdp = get_tdp_amd(family); 47829392bd98SCalvin Walton 47839392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 47849392bd98SCalvin Walton if (!quiet) 47859392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 47869392bd98SCalvin Walton } 47879392bd98SCalvin Walton 47889392bd98SCalvin Walton /* 47899392bd98SCalvin Walton * rapl_probe() 47909392bd98SCalvin Walton * 47919392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 47929392bd98SCalvin Walton */ 47939392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 47949392bd98SCalvin Walton { 47959392bd98SCalvin Walton if (genuine_intel) 47969392bd98SCalvin Walton rapl_probe_intel(family, model); 4797c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 47989392bd98SCalvin Walton rapl_probe_amd(family, model); 4799889facbeSLen Brown } 4800889facbeSLen Brown 48011b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 48023a9a941dSLen Brown { 48033a9a941dSLen Brown if (!genuine_intel) 48043a9a941dSLen Brown return; 48053a9a941dSLen Brown 48063a9a941dSLen Brown if (family != 6) 48073a9a941dSLen Brown return; 48083a9a941dSLen Brown 48093a9a941dSLen Brown switch (model) { 4810c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 481177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 48125e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 48133a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 48149878bf7aSLen Brown /* FALLTHRU */ 4815869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 48163a9a941dSLen Brown do_core_perf_limit_reasons = 1; 48173a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 48183a9a941dSLen Brown default: 48193a9a941dSLen Brown return; 48203a9a941dSLen Brown } 48213a9a941dSLen Brown } 48223a9a941dSLen Brown 4823ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4824ac980e13SArtem Bityutskiy { 4825eade39b2SArtem Bityutskiy if (family != 6) 4826eade39b2SArtem Bityutskiy return; 4827eade39b2SArtem Bityutskiy 4828eade39b2SArtem Bityutskiy switch (model) { 4829eade39b2SArtem Bityutskiy case INTEL_FAM6_BROADWELL_X: 4830eade39b2SArtem Bityutskiy case INTEL_FAM6_SKYLAKE_X: 4831ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4832ac980e13SArtem Bityutskiy } 4833eade39b2SArtem Bityutskiy } 4834ac980e13SArtem Bityutskiy 4835aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model) 4836aeb01e6dSChen Yu { 4837684e40e9SArtem Bityutskiy if (is_icx(family, model) || is_spr(family, model)) 4838aeb01e6dSChen Yu dis_cstate_prewake = 1; 4839aeb01e6dSChen Yu } 4840aeb01e6dSChen Yu 4841889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4842889facbeSLen Brown { 4843889facbeSLen Brown unsigned long long msr; 4844f4896fa5SLen Brown unsigned int dts, dts2; 4845889facbeSLen Brown int cpu; 4846889facbeSLen Brown 48479878bf7aSLen Brown UNUSED(c); 48489878bf7aSLen Brown UNUSED(p); 48499878bf7aSLen Brown 4850889facbeSLen Brown if (!(do_dts || do_ptm)) 4851889facbeSLen Brown return 0; 4852889facbeSLen Brown 4853889facbeSLen Brown cpu = t->cpu_id; 4854889facbeSLen Brown 4855889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4856889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4857889facbeSLen Brown return 0; 4858889facbeSLen Brown 4859889facbeSLen Brown if (cpu_migrate(cpu)) { 48603d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4861889facbeSLen Brown return -1; 4862889facbeSLen Brown } 4863889facbeSLen Brown 4864889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4865889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4866889facbeSLen Brown return 0; 4867889facbeSLen Brown 4868889facbeSLen Brown dts = (msr >> 16) & 0x7F; 48691b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts); 4870889facbeSLen Brown 4871889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4872889facbeSLen Brown return 0; 4873889facbeSLen Brown 4874889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4875889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4876b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 487755279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4878889facbeSLen Brown } 4879889facbeSLen Brown 4880f4896fa5SLen Brown if (do_dts && debug) { 4881889facbeSLen Brown unsigned int resolution; 4882889facbeSLen Brown 4883889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4884889facbeSLen Brown return 0; 4885889facbeSLen Brown 4886889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4887889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4888b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 488955279aefSZhang Rui cpu, msr, tj_max - dts, resolution); 4890889facbeSLen Brown 4891889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4892889facbeSLen Brown return 0; 4893889facbeSLen Brown 4894889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4895889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4896b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 489755279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4898889facbeSLen Brown } 4899889facbeSLen Brown 4900889facbeSLen Brown return 0; 4901889facbeSLen Brown } 4902889facbeSLen Brown 4903889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4904889facbeSLen Brown { 49056b398625SSumeet Pawnikar fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n", 4906889facbeSLen Brown cpu, label, 4907889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4908889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4909889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4910889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4911889facbeSLen Brown 4912889facbeSLen Brown return; 4913889facbeSLen Brown } 4914889facbeSLen Brown 4915889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4916889facbeSLen Brown { 4917889facbeSLen Brown unsigned long long msr; 49189392bd98SCalvin Walton const char *msr_name; 4919889facbeSLen Brown int cpu; 4920889facbeSLen Brown 49219878bf7aSLen Brown UNUSED(c); 49229878bf7aSLen Brown UNUSED(p); 49239878bf7aSLen Brown 4924889facbeSLen Brown if (!do_rapl) 4925889facbeSLen Brown return 0; 4926889facbeSLen Brown 4927889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4928889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4929889facbeSLen Brown return 0; 4930889facbeSLen Brown 4931889facbeSLen Brown cpu = t->cpu_id; 4932889facbeSLen Brown if (cpu_migrate(cpu)) { 49333d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4934889facbeSLen Brown return -1; 4935889facbeSLen Brown } 4936889facbeSLen Brown 49379392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 49389392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 49399392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 49409392bd98SCalvin Walton return -1; 49419392bd98SCalvin Walton } else { 49429392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4943889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4944889facbeSLen Brown return -1; 49459392bd98SCalvin Walton } 4946889facbeSLen Brown 49479392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4948144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 494996e47158SLen Brown 4950144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4951144b44b1SLen Brown 4952889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4953889facbeSLen Brown return -5; 4954889facbeSLen Brown 4955b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4956889facbeSLen Brown cpu, msr, 4957889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4958889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4959889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4960889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4961889facbeSLen Brown 4962144b44b1SLen Brown } 4963144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4964144b44b1SLen Brown 4965889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4966889facbeSLen Brown return -9; 4967889facbeSLen Brown 4968b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4969889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4970889facbeSLen Brown 4971889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 49726b398625SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n", 4973889facbeSLen Brown cpu, 4974889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4975889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4976889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4977889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4978f52ba931SSumeet Pawnikar 4979f52ba931SSumeet Pawnikar if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr)) 4980f52ba931SSumeet Pawnikar return -9; 4981f52ba931SSumeet Pawnikar 4982f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr); 4983f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n", 4984164d7a96SLen Brown cpu, ((msr >> 0) & 0x1FFF) * rapl_power_units, (msr >> 31) & 1 ? "" : "UN"); 4985889facbeSLen Brown } 4986889facbeSLen Brown 49870b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4988889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4989889facbeSLen Brown return -6; 4990889facbeSLen Brown 4991b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4992889facbeSLen Brown cpu, msr, 4993889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4994889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4995889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4996889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 49970b2bb692SLen Brown } 49980b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4999889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 5000889facbeSLen Brown return -9; 5001b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 5002889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 5003889facbeSLen Brown 5004889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 5005889facbeSLen Brown } 5006144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 5007889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 5008889facbeSLen Brown return -7; 5009889facbeSLen Brown 5010b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 5011144b44b1SLen Brown } 50129148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 5013889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 5014889facbeSLen Brown return -9; 5015b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 5016889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 5017889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 5018889facbeSLen Brown } 5019889facbeSLen Brown if (do_rapl & RAPL_GFX) { 5020889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 5021889facbeSLen Brown return -8; 5022889facbeSLen Brown 5023b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 5024889facbeSLen Brown 5025889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 5026889facbeSLen Brown return -9; 5027b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 5028889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 5029889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 5030889facbeSLen Brown } 5031889facbeSLen Brown return 0; 5032889facbeSLen Brown } 5033889facbeSLen Brown 5034d7899447SLen Brown /* 5035d7899447SLen Brown * SNB adds support for additional MSRs: 5036d7899447SLen Brown * 5037d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 5038d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 5039d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 5040d7899447SLen Brown */ 5041103a8feaSLen Brown 5042d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 5043103a8feaSLen Brown { 5044103a8feaSLen Brown if (!genuine_intel) 5045103a8feaSLen Brown return 0; 5046103a8feaSLen Brown 50479878bf7aSLen Brown if (family != 6) 50489878bf7aSLen Brown return 0; 50499878bf7aSLen Brown 5050103a8feaSLen Brown switch (model) { 5051869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 5052869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 5053869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 5054869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 5055c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 5056869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 505777e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 50585e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 5059c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 50605e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 5061869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 5062af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5063af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 5064869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 506525368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 5066684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: /* SPR */ 5067869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5068f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 50695ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5070f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 507120de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 5072103a8feaSLen Brown return 1; 5073103a8feaSLen Brown } 5074103a8feaSLen Brown return 0; 5075103a8feaSLen Brown } 5076103a8feaSLen Brown 5077d7899447SLen Brown /* 5078570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 5079d7899447SLen Brown * 5080d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 5081d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 5082d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 50835a63426eSLen Brown * 50845a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 50855a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 50865a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 50875a63426eSLen Brown * 5088d7899447SLen Brown */ 5089570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 5090ca58710fSKristen Carlson Accardi { 5091ca58710fSKristen Carlson Accardi if (!genuine_intel) 5092ca58710fSKristen Carlson Accardi return 0; 5093ca58710fSKristen Carlson Accardi 50949878bf7aSLen Brown if (family != 6) 50959878bf7aSLen Brown return 0; 50969878bf7aSLen Brown 5097ca58710fSKristen Carlson Accardi switch (model) { 509877e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 5099c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 5100af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5101af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 5102869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5103f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5104f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 5105ca58710fSKristen Carlson Accardi return 1; 5106ca58710fSKristen Carlson Accardi } 5107ca58710fSKristen Carlson Accardi return 0; 5108ca58710fSKristen Carlson Accardi } 5109ca58710fSKristen Carlson Accardi 51100b2bb692SLen Brown /* 51110b2bb692SLen Brown * SKL adds support for additional MSRS: 51120b2bb692SLen Brown * 51130b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 51140b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 51150b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 51160b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 51170b2bb692SLen Brown */ 51180b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 51190b2bb692SLen Brown { 51200b2bb692SLen Brown if (!genuine_intel) 51210b2bb692SLen Brown return 0; 51220b2bb692SLen Brown 51239878bf7aSLen Brown if (family != 6) 51249878bf7aSLen Brown return 0; 51259878bf7aSLen Brown 51260b2bb692SLen Brown switch (model) { 5127af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5128af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 51290b2bb692SLen Brown return 1; 51300b2bb692SLen Brown } 51310b2bb692SLen Brown return 0; 51320b2bb692SLen Brown } 51330b2bb692SLen Brown 5134144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 5135144b44b1SLen Brown { 5136144b44b1SLen Brown if (!genuine_intel) 5137144b44b1SLen Brown return 0; 51389878bf7aSLen Brown 51399878bf7aSLen Brown if (family != 6) 51409878bf7aSLen Brown return 0; 51419878bf7aSLen Brown 5142144b44b1SLen Brown switch (model) { 5143f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 51445ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 5145144b44b1SLen Brown return 1; 5146144b44b1SLen Brown } 5147144b44b1SLen Brown return 0; 5148144b44b1SLen Brown } 5149144b44b1SLen Brown 5150fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 5151fb5d4327SDasaratharaman Chandramouli { 5152fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 5153fb5d4327SDasaratharaman Chandramouli return 0; 51549878bf7aSLen Brown 51559878bf7aSLen Brown if (family != 6) 51569878bf7aSLen Brown return 0; 51579878bf7aSLen Brown 5158fb5d4327SDasaratharaman Chandramouli switch (model) { 5159869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 5160fb5d4327SDasaratharaman Chandramouli return 1; 5161fb5d4327SDasaratharaman Chandramouli } 5162fb5d4327SDasaratharaman Chandramouli return 0; 5163fb5d4327SDasaratharaman Chandramouli } 5164fb5d4327SDasaratharaman Chandramouli 5165997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 5166997e5395SSrinivas Pandruvada { 5167997e5395SSrinivas Pandruvada if (!genuine_intel) 5168997e5395SSrinivas Pandruvada return 0; 5169997e5395SSrinivas Pandruvada 51709878bf7aSLen Brown if (family != 6) 51719878bf7aSLen Brown return 0; 51729878bf7aSLen Brown 5173997e5395SSrinivas Pandruvada switch (model) { 5174af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 5175997e5395SSrinivas Pandruvada return 1; 5176997e5395SSrinivas Pandruvada } 5177997e5395SSrinivas Pandruvada 5178997e5395SSrinivas Pandruvada return 0; 5179997e5395SSrinivas Pandruvada } 5180997e5395SSrinivas Pandruvada 5181b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 5182b2b34dfeSHubert Chrzaniuk { 5183b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 5184b2b34dfeSHubert Chrzaniuk return 1024; 5185b2b34dfeSHubert Chrzaniuk return 1; 5186b2b34dfeSHubert Chrzaniuk } 5187b2b34dfeSHubert Chrzaniuk 5188144b44b1SLen Brown #define SLM_BCLK_FREQS 5 5189144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 }; 5190144b44b1SLen Brown 5191144b44b1SLen Brown double slm_bclk(void) 5192144b44b1SLen Brown { 5193144b44b1SLen Brown unsigned long long msr = 3; 5194144b44b1SLen Brown unsigned int i; 5195144b44b1SLen Brown double freq; 5196144b44b1SLen Brown 51977ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 5198b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 5199144b44b1SLen Brown 5200144b44b1SLen Brown i = msr & 0xf; 5201144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 5202b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 52030a91e551SColin Ian King i = 3; 5204144b44b1SLen Brown } 5205144b44b1SLen Brown freq = slm_freq_table[i]; 5206144b44b1SLen Brown 520796e47158SLen Brown if (!quiet) 5208b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 5209144b44b1SLen Brown 5210144b44b1SLen Brown return freq; 5211144b44b1SLen Brown } 5212144b44b1SLen Brown 5213103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 5214103a8feaSLen Brown { 5215121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 5216103a8feaSLen Brown return 100.00; 5217144b44b1SLen Brown else if (is_slm(family, model)) 5218144b44b1SLen Brown return slm_bclk(); 5219103a8feaSLen Brown else 5220103a8feaSLen Brown return 133.33; 5221103a8feaSLen Brown } 5222103a8feaSLen Brown 52237ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p) 52247ab5ff49SZhang Rui { 52257ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx; 52267ab5ff49SZhang Rui 52279878bf7aSLen Brown UNUSED(c); 52289878bf7aSLen Brown UNUSED(p); 52299878bf7aSLen Brown 52307ab5ff49SZhang Rui if (!genuine_intel) 52317ab5ff49SZhang Rui return 0; 52327ab5ff49SZhang Rui 52337ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) { 52347ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 52357ab5ff49SZhang Rui return -1; 52367ab5ff49SZhang Rui } 52377ab5ff49SZhang Rui 52387ab5ff49SZhang Rui if (max_level < 0x1a) 52397ab5ff49SZhang Rui return 0; 52407ab5ff49SZhang Rui 52417ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx); 52427ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF; 52437ab5ff49SZhang Rui if (eax == 0x20) 52447ab5ff49SZhang Rui t->is_atom = true; 52457ab5ff49SZhang Rui return 0; 52467ab5ff49SZhang Rui } 52477ab5ff49SZhang Rui 5248889facbeSLen Brown /* 5249889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 5250889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 5251889facbeSLen Brown * This is usually equal to tjMax. 5252889facbeSLen Brown * 5253889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 5254889facbeSLen Brown * but also allow cmdline over-ride with -T. 5255889facbeSLen Brown * 5256889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 5257889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 5258889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 5259889facbeSLen Brown */ 52606ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 52616ff7cb37SLen Brown { 5262b2b94be7SLen Brown unsigned long long msr; 526355279aefSZhang Rui unsigned int tcc_default, tcc_offset; 5264b2b94be7SLen Brown int cpu; 5265b2b94be7SLen Brown 52669878bf7aSLen Brown UNUSED(c); 52679878bf7aSLen Brown UNUSED(p); 52689878bf7aSLen Brown 526955279aefSZhang Rui /* tj_max is used only for dts or ptm */ 5270889facbeSLen Brown if (!(do_dts || do_ptm)) 5271889facbeSLen Brown return 0; 5272889facbeSLen Brown 5273889facbeSLen Brown /* this is a per-package concept */ 5274889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 5275889facbeSLen Brown return 0; 5276889facbeSLen Brown 5277b2b94be7SLen Brown cpu = t->cpu_id; 5278b2b94be7SLen Brown if (cpu_migrate(cpu)) { 5279b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 5280b2b94be7SLen Brown return -1; 5281b2b94be7SLen Brown } 5282b2b94be7SLen Brown 528355279aefSZhang Rui if (tj_max_override != 0) { 528455279aefSZhang Rui tj_max = tj_max_override; 52851b439f01SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max); 5286889facbeSLen Brown return 0; 5287889facbeSLen Brown } 5288889facbeSLen Brown 5289b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 5290b2b94be7SLen Brown if (!do_nhm_platform_info) 5291b2b94be7SLen Brown goto guess; 5292b2b94be7SLen Brown 5293b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 5294b2b94be7SLen Brown goto guess; 5295b2b94be7SLen Brown 529655279aefSZhang Rui tcc_default = (msr >> 16) & 0xFF; 5297b2b94be7SLen Brown 52980b9a0b9bSZhang Rui if (!quiet) { 52990b9a0b9bSZhang Rui switch (tcc_offset_bits) { 53000b9a0b9bSZhang Rui case 4: 53010b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0xF; 53020b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 530355279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 53040b9a0b9bSZhang Rui break; 53050b9a0b9bSZhang Rui case 6: 53060b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0x3F; 53070b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 530855279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 53090b9a0b9bSZhang Rui break; 53100b9a0b9bSZhang Rui default: 53111b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default); 53120b9a0b9bSZhang Rui break; 53130b9a0b9bSZhang Rui } 53140b9a0b9bSZhang Rui } 5315b2b94be7SLen Brown 531655279aefSZhang Rui if (!tcc_default) 5317b2b94be7SLen Brown goto guess; 5318b2b94be7SLen Brown 531955279aefSZhang Rui tj_max = tcc_default; 5320b2b94be7SLen Brown 5321889facbeSLen Brown return 0; 5322889facbeSLen Brown 5323b2b94be7SLen Brown guess: 532455279aefSZhang Rui tj_max = TJMAX_DEFAULT; 53251b439f01SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max); 5326889facbeSLen Brown 5327889facbeSLen Brown return 0; 5328889facbeSLen Brown } 532969807a63SLen Brown 5330aa8d8cc7SLen Brown void decode_feature_control_msr(void) 5331aa8d8cc7SLen Brown { 5332aa8d8cc7SLen Brown unsigned long long msr; 5333aa8d8cc7SLen Brown 5334f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 5335aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 53361b439f01SLen Brown base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : ""); 5337aa8d8cc7SLen Brown } 5338aa8d8cc7SLen Brown 533969807a63SLen Brown void decode_misc_enable_msr(void) 534069807a63SLen Brown { 534169807a63SLen Brown unsigned long long msr; 534269807a63SLen Brown 5343f26b1519SLen Brown if (!genuine_intel) 5344f26b1519SLen Brown return; 5345f26b1519SLen Brown 534669807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 5347e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 534869807a63SLen Brown base_cpu, msr, 5349e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 5350e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 5351fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 5352e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 5353e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 535469807a63SLen Brown } 535569807a63SLen Brown 535633148d67SLen Brown void decode_misc_feature_control(void) 535733148d67SLen Brown { 535833148d67SLen Brown unsigned long long msr; 535933148d67SLen Brown 536033148d67SLen Brown if (!has_misc_feature_control) 536133148d67SLen Brown return; 536233148d67SLen Brown 536333148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 53641b439f01SLen Brown fprintf(outf, 53651b439f01SLen Brown "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 53661b439f01SLen Brown base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "", 53671b439f01SLen Brown msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : ""); 536833148d67SLen Brown } 53691b439f01SLen Brown 5370f0057310SLen Brown /* 5371f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 5372f0057310SLen Brown * 5373f0057310SLen Brown * Decode the bits according to the Nehalem documentation 5374f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 5375f0057310SLen Brown * bit[1] less so... 5376f0057310SLen Brown */ 5377f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 5378f0057310SLen Brown { 5379f0057310SLen Brown unsigned long long msr; 5380f0057310SLen Brown 5381f0057310SLen Brown if (!do_nhm_platform_info) 5382f0057310SLen Brown return; 5383f0057310SLen Brown 5384cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 5385cf4cbe53SLen Brown return; 5386cf4cbe53SLen Brown 5387f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 5388ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 5389f0057310SLen Brown base_cpu, msr, 53901b439f01SLen Brown msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS"); 5391f0057310SLen Brown } 53921b439f01SLen Brown 539371616c8eSLen Brown /* 539471616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 539571616c8eSLen Brown * 539671616c8eSLen Brown * This MSRs are present on Silvermont processors, 539771616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 539871616c8eSLen Brown */ 539971616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 540071616c8eSLen Brown { 540171616c8eSLen Brown unsigned long long msr; 540271616c8eSLen Brown 540371616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 540471616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 540571616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 540671616c8eSLen Brown 540771616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 540871616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 540971616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 541071616c8eSLen Brown } 54117f5c258eSLen Brown 5412f5a4c76aSLen Brown /* 5413f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 5414f5a4c76aSLen Brown */ 5415f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 5416f5a4c76aSLen Brown { 5417f5a4c76aSLen Brown 5418f5a4c76aSLen Brown switch (model) { 5419f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 5420f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 5421f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 5422f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 5423f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 5424f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 5425f5a4c76aSLen Brown 5426f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 5427f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 5428f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 5429f5a4c76aSLen Brown 5430f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 5431f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 5432f5a4c76aSLen Brown 5433f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 54345ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 5435f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 5436f5a4c76aSLen Brown 5437af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 5438c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 5439af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 5440c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 5441081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 5442081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 5443af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 5444937807d3SLen Brown 5445af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 5446d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 54474bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 54484bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 5449e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 5450e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 5451e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 54525683460bSChen Yu case INTEL_FAM6_ALDERLAKE_L: 5453882cdb06SPeter Zijlstra case INTEL_FAM6_ATOM_GRACEMONT: 5454fb5e29dfSZhang Rui case INTEL_FAM6_RAPTORLAKE: 54556f9cf553SGeorge D Sworo case INTEL_FAM6_RAPTORLAKE_P: 54569b1c2ecfSZhang Rui case INTEL_FAM6_RAPTORLAKE_S: 54578e45a9bfSZhang Rui case INTEL_FAM6_METEORLAKE: 54588e45a9bfSZhang Rui case INTEL_FAM6_METEORLAKE_L: 5459af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 5460b62b3184SZhang Rui 5461d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 5462d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 5463d7814c30SChen Yu 54646c5c6560SChen Yu case INTEL_FAM6_ICELAKE_D: 546525368d7cSChen Yu return INTEL_FAM6_ICELAKE_X; 546693cac415SZhang Rui 546793cac415SZhang Rui case INTEL_FAM6_EMERALDRAPIDS_X: 546893cac415SZhang Rui return INTEL_FAM6_SAPPHIRERAPIDS_X; 5469f5a4c76aSLen Brown } 5470f5a4c76aSLen Brown return model; 5471f5a4c76aSLen Brown } 5472d76bb7a0SLen Brown 5473d76bb7a0SLen Brown void print_dev_latency(void) 5474d76bb7a0SLen Brown { 5475d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5476d76bb7a0SLen Brown int fd; 5477d76bb7a0SLen Brown int value; 5478d76bb7a0SLen Brown int retval; 5479d76bb7a0SLen Brown 5480d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5481d76bb7a0SLen Brown if (fd < 0) { 54829678a79dSLen Brown if (debug) 54839678a79dSLen Brown warnx("Read %s failed", path); 5484d76bb7a0SLen Brown return; 5485d76bb7a0SLen Brown } 5486d76bb7a0SLen Brown 5487d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5488d76bb7a0SLen Brown if (retval != sizeof(int)) { 54896cbfedc7SLen Brown warn("read failed %s", path); 5490d76bb7a0SLen Brown close(fd); 5491d76bb7a0SLen Brown return; 5492d76bb7a0SLen Brown } 54931b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 5494d76bb7a0SLen Brown 5495d76bb7a0SLen Brown close(fd); 5496d76bb7a0SLen Brown } 5497d76bb7a0SLen Brown 54982af4f9b8SLen Brown /* 54992db0e5ebSJiang Jian * Linux-perf manages the HW instructions-retired counter 55002af4f9b8SLen Brown * by enabling when requested, and hiding rollover 55012af4f9b8SLen Brown */ 55022af4f9b8SLen Brown void linux_perf_init(void) 55032af4f9b8SLen Brown { 55042af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 55052af4f9b8SLen Brown return; 55062af4f9b8SLen Brown 55072af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 55082af4f9b8SLen Brown return; 55092af4f9b8SLen Brown 55102af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 55112af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 55122af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 55132af4f9b8SLen Brown 55142af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 55152af4f9b8SLen Brown } 55162af4f9b8SLen Brown 5517fcd17211SLen Brown void process_cpuid() 5518103a8feaSLen Brown { 551934041551SLen Brown unsigned int eax, ebx, ecx, edx; 552034041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5521ed0757b8SLen Brown unsigned long long ucode_patch = 0; 55228a91872aSPatryk Wlazlyn bool ucode_patch_valid = false; 5523103a8feaSLen Brown 5524103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5525103a8feaSLen Brown 55265aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5527103a8feaSLen Brown 552834041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5529103a8feaSLen Brown genuine_intel = 1; 553034041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 553134041551SLen Brown authentic_amd = 1; 5532c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5533c1c10cc7SPu Wen hygon_genuine = 1; 5534103a8feaSLen Brown 553596e47158SLen Brown if (!quiet) 5536ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5537ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5538103a8feaSLen Brown 55395aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5540103a8feaSLen Brown family = (fms >> 8) & 0xf; 5541103a8feaSLen Brown model = (fms >> 4) & 0xf; 5542103a8feaSLen Brown stepping = fms & 0xf; 55435aa3d1a2SCalvin Walton if (family == 0xf) 55445aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 55455aa3d1a2SCalvin Walton if (family >= 6) 5546103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 554734041551SLen Brown ecx_flags = ecx; 554834041551SLen Brown edx_flags = edx; 5549103a8feaSLen Brown 5550ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 55516cbfedc7SLen Brown warnx("get_msr(UCODE)"); 55528a91872aSPatryk Wlazlyn else 55538a91872aSPatryk Wlazlyn ucode_patch_valid = true; 5554ed0757b8SLen Brown 5555103a8feaSLen Brown /* 5556103a8feaSLen Brown * check max extended function levels of CPUID. 5557103a8feaSLen Brown * This is needed to check for invariant TSC. 5558103a8feaSLen Brown * This check is valid for both Intel and AMD. 5559103a8feaSLen Brown */ 5560103a8feaSLen Brown ebx = ecx = edx = 0; 55615aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5562103a8feaSLen Brown 556334041551SLen Brown if (!quiet) { 55648a91872aSPatryk Wlazlyn fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d)", 55658a91872aSPatryk Wlazlyn family, model, stepping, family, model, stepping); 55668a91872aSPatryk Wlazlyn if (ucode_patch_valid) 55678a91872aSPatryk Wlazlyn fprintf(outf, " microcode 0x%x", (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 55688a91872aSPatryk Wlazlyn fputc('\n', outf); 55698a91872aSPatryk Wlazlyn 5570ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 557134041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 557234041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 557334041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 557434041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 557534041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 557634041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 557734041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 557834041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 557934041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 55801b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-"); 558134041551SLen Brown } 558225368d7cSChen Yu if (genuine_intel) { 5583e9d3092fSZhang Rui model_orig = model; 5584f5a4c76aSLen Brown model = intel_model_duplicates(model); 558525368d7cSChen Yu } 558634041551SLen Brown 558734041551SLen Brown if (!(edx_flags & (1 << 5))) 558834041551SLen Brown errx(1, "CPUID: no MSR"); 558934041551SLen Brown 559061a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5591103a8feaSLen Brown 5592103a8feaSLen Brown /* 5593103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5594103a8feaSLen Brown * this check is valid for both Intel and AMD 5595103a8feaSLen Brown */ 55965aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 55978209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5598d7899447SLen Brown } 5599103a8feaSLen Brown 5600103a8feaSLen Brown /* 5601103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5602103a8feaSLen Brown * this check is valid for both Intel and AMD 5603103a8feaSLen Brown */ 5604103a8feaSLen Brown 56055aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 56068209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5607812db3f7SLen Brown if (has_aperf) { 5608812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5609812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5610812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5611812db3f7SLen Brown } 5612889facbeSLen Brown do_dts = eax & (1 << 0); 5613812db3f7SLen Brown if (do_dts) 5614812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5615b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5616889facbeSLen Brown do_ptm = eax & (1 << 6); 5617812db3f7SLen Brown if (do_ptm) 5618812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 56197f5c258eSLen Brown has_hwp = eax & (1 << 7); 56207f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 56217f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 56227f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 56237f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5624889facbeSLen Brown has_epb = ecx & (1 << 3); 5625889facbeSLen Brown 562696e47158SLen Brown if (!quiet) 5627b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 56287f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 56297f5c258eSLen Brown has_aperf ? "" : "No-", 5630b3a34e93SLen Brown has_turbo ? "" : "No-", 56317f5c258eSLen Brown do_dts ? "" : "No-", 56327f5c258eSLen Brown do_ptm ? "" : "No-", 56337f5c258eSLen Brown has_hwp ? "" : "No-", 56347f5c258eSLen Brown has_hwp_notify ? "" : "No-", 56357f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 56361b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 5637103a8feaSLen Brown 563896e47158SLen Brown if (!quiet) 563969807a63SLen Brown decode_misc_enable_msr(); 564069807a63SLen Brown 564196e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5642aa8d8cc7SLen Brown int has_sgx; 5643aa8d8cc7SLen Brown 5644aa8d8cc7SLen Brown ecx = 0; 5645aa8d8cc7SLen Brown 5646aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5647aa8d8cc7SLen Brown 5648aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5649774627c5SLen Brown 5650774627c5SLen Brown is_hybrid = edx & (1 << 15); 5651774627c5SLen Brown 5652774627c5SLen Brown fprintf(outf, "CPUID(7): %sSGX %sHybrid\n", has_sgx ? "" : "No-", is_hybrid ? "" : "No-"); 5653aa8d8cc7SLen Brown 5654aa8d8cc7SLen Brown if (has_sgx) 5655aa8d8cc7SLen Brown decode_feature_control_msr(); 5656aa8d8cc7SLen Brown } 5657aa8d8cc7SLen Brown 565861a87ba7SLen Brown if (max_level >= 0x15) { 56598a5bdf41SLen Brown unsigned int eax_crystal; 56608a5bdf41SLen Brown unsigned int ebx_tsc; 56618a5bdf41SLen Brown 56628a5bdf41SLen Brown /* 56638a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 56648a5bdf41SLen Brown */ 56658a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 56665aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 56678a5bdf41SLen Brown 56688a5bdf41SLen Brown if (ebx_tsc != 0) { 56698a5bdf41SLen Brown 567096e47158SLen Brown if (!quiet && (ebx != 0)) 5671b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 56728a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 56738a5bdf41SLen Brown 56748a5bdf41SLen Brown if (crystal_hz == 0) 56758a5bdf41SLen Brown switch (model) { 5676af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5677e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5678e8efbc80SLen Brown break; 56795ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5680ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5681ec53e594SLen Brown break; 5682869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5683f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5684e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 56858a5bdf41SLen Brown break; 56868a5bdf41SLen Brown default: 56878a5bdf41SLen Brown crystal_hz = 0; 56888a5bdf41SLen Brown } 56898a5bdf41SLen Brown 56908a5bdf41SLen Brown if (crystal_hz) { 56918a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 569296e47158SLen Brown if (!quiet) 5693b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 56948a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 56958a5bdf41SLen Brown } 56968a5bdf41SLen Brown } 56978a5bdf41SLen Brown } 569861a87ba7SLen Brown if (max_level >= 0x16) { 569961a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 570061a87ba7SLen Brown 570161a87ba7SLen Brown /* 570261a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 570361a87ba7SLen Brown */ 570461a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 570561a87ba7SLen Brown 57065aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 570796e47158SLen Brown if (!quiet) 5708b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 570961a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 571061a87ba7SLen Brown } 57118a5bdf41SLen Brown 5712b2b34dfeSHubert Chrzaniuk if (has_aperf) 5713b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5714b2b34dfeSHubert Chrzaniuk 5715812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5716812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5717812db3f7SLen Brown 5718812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5719812db3f7SLen Brown do_nhm_platform_info = 1; 5720812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5721812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5722812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5723812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5724812db3f7SLen Brown } 5725d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5726812db3f7SLen Brown 5727812db3f7SLen Brown if (do_snb_cstates) 5728812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5729812db3f7SLen Brown 57305a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 57310f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 57320f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 57330f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 57340f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 57350f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 57360f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 57370f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 57380f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 57390539ba11SLen Brown if (has_slv_msrs(family, model)) { 57400f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 57410f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 57420f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 57430f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 57440539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 57450539ba11SLen Brown use_c1_residency_msr = 1; 57460539ba11SLen Brown } 574720de0dabSAntti Laakso if (is_jvl(family, model)) { 574820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 574920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 575020de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 575120de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 575220de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 575320de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 575420de0dabSAntti Laakso } 57557170a374SLen Brown if (is_dnv(family, model)) { 57567170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 57577170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 57587170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 57597170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 57607170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 57617170a374SLen Brown use_c1_residency_msr = 1; 57627170a374SLen Brown } 5763684e40e9SArtem Bityutskiy if (is_skx(family, model) || is_icx(family, model) || is_spr(family, model)) { 576434c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 576534c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 576634c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 576734c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 576834c76197SLen Brown } 5769ade0ebacSLen Brown if (is_bdx(family, model)) { 5770ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5771ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5772ade0ebacSLen Brown } 5773570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5774c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 57750f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5776c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 57770f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5778c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 57790f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 57800f47c08dSLen Brown } 5781570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5782a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5783a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5784a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5785a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5786a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5787a99d8730SLen Brown } 5788144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5789fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5790103a8feaSLen Brown 57911b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model)) 5792562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5793103a8feaSLen Brown 579496e47158SLen Brown if (!quiet) 5795f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5796f0057310SLen Brown 579796e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 579871616c8eSLen Brown decode_c6_demotion_policy_msr(); 579971616c8eSLen Brown 5800889facbeSLen Brown rapl_probe(family, model); 58013a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5802ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 58039b523bf8SChen Yu prewake_cstate_probe(family, model); 5804889facbeSLen Brown 58050b9a0b9bSZhang Rui check_tcc_offset(model_orig); 58060b9a0b9bSZhang Rui 580796e47158SLen Brown if (!quiet) 58081b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5809a5c6d65dSLen Brown intel_uncore_frequency_probe(); 5810fcd17211SLen Brown 581141618e63SLen Brown if (!quiet) 5812d76bb7a0SLen Brown print_dev_latency(); 5813d76bb7a0SLen Brown if (!quiet) 581441618e63SLen Brown dump_sysfs_cstate_config(); 58157293fccdSLen Brown if (!quiet) 58167293fccdSLen Brown dump_sysfs_pstate_config(); 581741618e63SLen Brown 58188c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5819a2b7b749SLen Brown calculate_tsc_tweak(); 5820a2b7b749SLen Brown 5821812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5822812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5823fdf676e5SLen Brown 5824812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5825812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 582627d47356SLen Brown 5827b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5828b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5829b4b91569SRafael Antognolli 5830be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5831be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5832be0e54c4SLen Brown else 5833be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5834be0e54c4SLen Brown 5835eae97e05SChen Yu if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK)) 5836eae97e05SChen Yu BIC_PRESENT(BIC_CORE_THROT_CNT); 5837eae97e05SChen Yu else 5838eae97e05SChen Yu BIC_NOT_PRESENT(BIC_CORE_THROT_CNT); 5839eae97e05SChen Yu 58401f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 58411f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5842be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 58431f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 58441f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 58451f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 58461f81c5efSLen Brown } else { 58471f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5848be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 58491f81c5efSLen Brown } 5850be0e54c4SLen Brown 585196e47158SLen Brown if (!quiet) 585233148d67SLen Brown decode_misc_feature_control(); 585333148d67SLen Brown 5854889facbeSLen Brown return; 5855103a8feaSLen Brown } 5856103a8feaSLen Brown 5857103a8feaSLen Brown /* 5858103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5859103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5860103a8feaSLen Brown */ 5861103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5862103a8feaSLen Brown { 5863103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5864103a8feaSLen Brown return 1; 5865103a8feaSLen Brown else 5866103a8feaSLen Brown return 0; 5867103a8feaSLen Brown } 5868103a8feaSLen Brown 5869c98d5d94SLen Brown void topology_probe() 5870c98d5d94SLen Brown { 5871c98d5d94SLen Brown int i; 5872c98d5d94SLen Brown int max_core_id = 0; 5873c98d5d94SLen Brown int max_package_id = 0; 58746de68fe1SLen Brown int max_die_id = 0; 5875c98d5d94SLen Brown int max_siblings = 0; 5876c98d5d94SLen Brown 5877c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5878843c5791SPrarit Bhargava set_max_cpu_num(); 5879c98d5d94SLen Brown topo.num_cpus = 0; 5880c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5881c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5882812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5883c98d5d94SLen Brown 5884d8af6f5fSLen Brown if (debug > 1) 5885b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5886c98d5d94SLen Brown 5887c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5888b2c95d90SJosh Triplett if (cpus == NULL) 5889b2c95d90SJosh Triplett err(1, "calloc cpus"); 5890c98d5d94SLen Brown 5891c98d5d94SLen Brown /* 5892c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5893c98d5d94SLen Brown */ 5894c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5895b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5896b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5897c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5898c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5899c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5900c98d5d94SLen Brown 5901c98d5d94SLen Brown /* 59021ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 59031ef7d21aSLen Brown */ 59041ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 59051ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 59061ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 59071ef7d21aSLen Brown err(1, "cpu%d not present", i); 59081ef7d21aSLen Brown } 59091ef7d21aSLen Brown 59101ef7d21aSLen Brown /* 5911c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5912c98d5d94SLen Brown */ 5913c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5914b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5915b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5916c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5917c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5918c98d5d94SLen Brown 59198cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5920c98d5d94SLen Brown 5921c98d5d94SLen Brown /* 5922c98d5d94SLen Brown * For online cpus 5923c98d5d94SLen Brown * find max_core_id, max_package_id 5924c98d5d94SLen Brown */ 5925c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5926c98d5d94SLen Brown int siblings; 5927c98d5d94SLen Brown 5928c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5929d8af6f5fSLen Brown if (debug > 1) 5930b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5931c98d5d94SLen Brown continue; 5932c98d5d94SLen Brown } 5933c98d5d94SLen Brown 59340e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 59350e2d8f05SLen Brown 59360e2d8f05SLen Brown /* get package information */ 5937c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5938c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5939c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5940c98d5d94SLen Brown 59416de68fe1SLen Brown /* get die information */ 59426de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 59436de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 59446de68fe1SLen Brown max_die_id = cpus[i].die_id; 59456de68fe1SLen Brown 59460e2d8f05SLen Brown /* get numa node information */ 5947ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5948ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5949ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 59500e2d8f05SLen Brown 59510e2d8f05SLen Brown /* get core information */ 59520e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 59530e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 59540e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 59550e2d8f05SLen Brown 59560e2d8f05SLen Brown /* get thread information */ 59570e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5958c98d5d94SLen Brown if (siblings > max_siblings) 5959c98d5d94SLen Brown max_siblings = siblings; 59604f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 59618cb48b32SPrarit Bhargava topo.num_cores++; 5962c98d5d94SLen Brown } 5963ef605741SPrarit Bhargava 596470a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5965d8af6f5fSLen Brown if (debug > 1) 59661b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node); 596770a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5968812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5969c98d5d94SLen Brown 59706de68fe1SLen Brown topo.num_die = max_die_id + 1; 59716de68fe1SLen Brown if (debug > 1) 59721b439f01SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die); 59736de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 59746de68fe1SLen Brown BIC_PRESENT(BIC_Die); 59756de68fe1SLen Brown 5976c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5977d8af6f5fSLen Brown if (debug > 1) 59781b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 59797da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5980812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5981c98d5d94SLen Brown 5982ef605741SPrarit Bhargava set_node_data(); 5983ef605741SPrarit Bhargava if (debug > 1) 598470a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 598501235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 598601235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5987ef605741SPrarit Bhargava 598870a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5989d8af6f5fSLen Brown if (debug > 1) 5990b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 59912ffbb224SPrarit Bhargava 59922ffbb224SPrarit Bhargava if (debug < 1) 59932ffbb224SPrarit Bhargava return; 59942ffbb224SPrarit Bhargava 59952ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 59960ec712e3SLen Brown if (cpu_is_not_present(i)) 59970ec712e3SLen Brown continue; 59982ffbb224SPrarit Bhargava fprintf(outf, 59996de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 60006de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 60011b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id); 60022ffbb224SPrarit Bhargava } 60032ffbb224SPrarit Bhargava 6004c98d5d94SLen Brown } 6005c98d5d94SLen Brown 60061b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 6007c98d5d94SLen Brown { 6008c98d5d94SLen Brown int i; 60091b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; 601040f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 6011c98d5d94SLen Brown 601240f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 6013c98d5d94SLen Brown if (*t == NULL) 6014c98d5d94SLen Brown goto error; 6015c98d5d94SLen Brown 601640f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 6017c98d5d94SLen Brown (*t)[i].cpu_id = -1; 6018c98d5d94SLen Brown 601940f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 6020c98d5d94SLen Brown if (*c == NULL) 6021c98d5d94SLen Brown goto error; 6022c98d5d94SLen Brown 602340f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 6024c98d5d94SLen Brown (*c)[i].core_id = -1; 6025c98d5d94SLen Brown 6026678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 6027c98d5d94SLen Brown if (*p == NULL) 6028c98d5d94SLen Brown goto error; 6029c98d5d94SLen Brown 6030c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 6031c98d5d94SLen Brown (*p)[i].package_id = i; 6032c98d5d94SLen Brown 6033c98d5d94SLen Brown return; 6034c98d5d94SLen Brown error: 6035b2c95d90SJosh Triplett err(1, "calloc counters"); 6036c98d5d94SLen Brown } 60371b439f01SLen Brown 6038c98d5d94SLen Brown /* 6039c98d5d94SLen Brown * init_counter() 6040c98d5d94SLen Brown * 6041c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 6042c98d5d94SLen Brown */ 60431b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 6044c98d5d94SLen Brown { 60458cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 604640f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 60478cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 60488cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 6049c98d5d94SLen Brown struct thread_data *t; 6050c98d5d94SLen Brown struct core_data *c; 6051c98d5d94SLen Brown struct pkg_data *p; 6052c98d5d94SLen Brown 605342dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 605442dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 605542dd4520SNathan Ciobanu */ 605642dd4520SNathan Ciobanu if (node_id < 0) 605742dd4520SNathan Ciobanu node_id = 0; 605842dd4520SNathan Ciobanu 605940f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 606040f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 60618cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 6062c98d5d94SLen Brown 6063c98d5d94SLen Brown t->cpu_id = cpu_id; 60648cb48b32SPrarit Bhargava if (thread_id == 0) { 6065c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 6066c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 6067c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 6068c98d5d94SLen Brown } 6069c98d5d94SLen Brown 60708cb48b32SPrarit Bhargava c->core_id = core_id; 60718cb48b32SPrarit Bhargava p->package_id = pkg_id; 6072c98d5d94SLen Brown } 6073c98d5d94SLen Brown 6074c98d5d94SLen Brown int initialize_counters(int cpu_id) 6075c98d5d94SLen Brown { 60768cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 60778cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 6078c98d5d94SLen Brown return 0; 6079c98d5d94SLen Brown } 6080c98d5d94SLen Brown 6081c98d5d94SLen Brown void allocate_output_buffer() 6082c98d5d94SLen Brown { 6083eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 6084c98d5d94SLen Brown outp = output_buffer; 6085b2c95d90SJosh Triplett if (outp == NULL) 6086b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 6087c98d5d94SLen Brown } 60881b439f01SLen Brown 608936229897SLen Brown void allocate_fd_percpu(void) 609036229897SLen Brown { 609101a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 609236229897SLen Brown if (fd_percpu == NULL) 609336229897SLen Brown err(-1, "calloc fd_percpu"); 609436229897SLen Brown } 60951b439f01SLen Brown 6096562a2d37SLen Brown void allocate_irq_buffers(void) 6097562a2d37SLen Brown { 6098562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 6099562a2d37SLen Brown if (irq_column_2_cpu == NULL) 6100562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 6101c98d5d94SLen Brown 610201a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 6103562a2d37SLen Brown if (irqs_per_cpu == NULL) 610401a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 6105562a2d37SLen Brown } 61061b439f01SLen Brown 6107c98d5d94SLen Brown void setup_all_buffers(void) 6108c98d5d94SLen Brown { 6109c98d5d94SLen Brown topology_probe(); 6110562a2d37SLen Brown allocate_irq_buffers(); 611136229897SLen Brown allocate_fd_percpu(); 6112c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 6113c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 6114c98d5d94SLen Brown allocate_output_buffer(); 6115c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 6116c98d5d94SLen Brown } 61173b4d5c7fSAndy Shevchenko 61187ce7d5deSPrarit Bhargava void set_base_cpu(void) 61197ce7d5deSPrarit Bhargava { 61207ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 61217ce7d5deSPrarit Bhargava if (base_cpu < 0) 61227ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 61237ce7d5deSPrarit Bhargava 61247ce7d5deSPrarit Bhargava if (debug > 1) 6125b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 61267ce7d5deSPrarit Bhargava } 61277ce7d5deSPrarit Bhargava 6128103a8feaSLen Brown void turbostat_init() 6129103a8feaSLen Brown { 61307ce7d5deSPrarit Bhargava setup_all_buffers(); 61317ce7d5deSPrarit Bhargava set_base_cpu(); 6132103a8feaSLen Brown check_dev_msr(); 613398481e79SLen Brown check_permissions(); 6134fcd17211SLen Brown process_cpuid(); 61352af4f9b8SLen Brown linux_perf_init(); 6136103a8feaSLen Brown 613796e47158SLen Brown if (!quiet) 61387f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 61397f5c258eSLen Brown 614096e47158SLen Brown if (!quiet) 6141889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 6142889facbeSLen Brown 614396e47158SLen Brown if (!quiet) 61443a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 61453a9a941dSLen Brown 614696e47158SLen Brown if (!quiet) 6147889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 6148889facbeSLen Brown 6149889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 6150889facbeSLen Brown 61517ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 61527ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 61537ab5ff49SZhang Rui 615496e47158SLen Brown if (!quiet) 6155889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 61565a63426eSLen Brown 615796e47158SLen Brown if (!quiet && do_irtl_snb) 61585a63426eSLen Brown print_irtl(); 6159164d7a96SLen Brown 6160164d7a96SLen Brown if (DO_BIC(BIC_IPC)) 6161164d7a96SLen Brown (void)get_instr_count_fd(base_cpu); 6162103a8feaSLen Brown } 6163103a8feaSLen Brown 6164103a8feaSLen Brown int fork_it(char **argv) 6165103a8feaSLen Brown { 6166103a8feaSLen Brown pid_t child_pid; 6167d91bb17cSLen Brown int status; 6168d15cf7c1SLen Brown 6169218f0e8dSLen Brown snapshot_proc_sysfs_files(); 6170d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 61714c2122d4SLen Brown first_counter_read = 0; 6172d91bb17cSLen Brown if (status) 6173d91bb17cSLen Brown exit(status); 6174d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 6175d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 6176103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 6177103a8feaSLen Brown 6178103a8feaSLen Brown child_pid = fork(); 6179103a8feaSLen Brown if (!child_pid) { 6180103a8feaSLen Brown /* child */ 6181103a8feaSLen Brown execvp(argv[0], argv); 61820815a3d0SLen Brown err(errno, "exec %s", argv[0]); 6183103a8feaSLen Brown } else { 6184103a8feaSLen Brown 6185103a8feaSLen Brown /* parent */ 6186b2c95d90SJosh Triplett if (child_pid == -1) 6187b2c95d90SJosh Triplett err(1, "fork"); 6188103a8feaSLen Brown 6189103a8feaSLen Brown signal(SIGINT, SIG_IGN); 6190103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 6191b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 6192b2c95d90SJosh Triplett err(status, "waitpid"); 61932a954966SDavid Arcari 61942a954966SDavid Arcari if (WIFEXITED(status)) 61952a954966SDavid Arcari status = WEXITSTATUS(status); 6196103a8feaSLen Brown } 6197c98d5d94SLen Brown /* 6198c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 6199c98d5d94SLen Brown * because re-starting is problematic when forking 6200c98d5d94SLen Brown */ 6201218f0e8dSLen Brown snapshot_proc_sysfs_files(); 6202c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 6203103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 6204103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 6205ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 6206ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 6207ba3dec99SLen Brown else { 6208c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 6209c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 6210ba3dec99SLen Brown } 6211103a8feaSLen Brown 6212b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 6213b7d8c148SLen Brown 6214b7d8c148SLen Brown flush_output_stderr(); 6215103a8feaSLen Brown 6216d91bb17cSLen Brown return status; 6217103a8feaSLen Brown } 6218103a8feaSLen Brown 62193b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 62203b4d5c7fSAndy Shevchenko { 62213b4d5c7fSAndy Shevchenko int status; 62223b4d5c7fSAndy Shevchenko 6223218f0e8dSLen Brown snapshot_proc_sysfs_files(); 62243b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 62253b4d5c7fSAndy Shevchenko if (status) 62263b4d5c7fSAndy Shevchenko return status; 62273b4d5c7fSAndy Shevchenko 62283b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 62293b4d5c7fSAndy Shevchenko if (status) 62303b4d5c7fSAndy Shevchenko return status; 62313b4d5c7fSAndy Shevchenko 6232b7d8c148SLen Brown flush_output_stdout(); 62333b4d5c7fSAndy Shevchenko 62343b4d5c7fSAndy Shevchenko return status; 62353b4d5c7fSAndy Shevchenko } 62363b4d5c7fSAndy Shevchenko 62371b439f01SLen Brown void print_version() 62381b439f01SLen Brown { 6239de7839eeSLen Brown fprintf(outf, "turbostat version 2023.03.17 - Len Brown <lenb@kernel.org>\n"); 6240d8af6f5fSLen Brown } 6241d8af6f5fSLen Brown 624203331233SChen Yu #define COMMAND_LINE_SIZE 2048 624303331233SChen Yu 624403331233SChen Yu void print_bootcmd(void) 624503331233SChen Yu { 624603331233SChen Yu char bootcmd[COMMAND_LINE_SIZE]; 624703331233SChen Yu FILE *fp; 624803331233SChen Yu int ret; 624903331233SChen Yu 625003331233SChen Yu memset(bootcmd, 0, COMMAND_LINE_SIZE); 625103331233SChen Yu fp = fopen("/proc/cmdline", "r"); 625203331233SChen Yu if (!fp) 625303331233SChen Yu return; 625403331233SChen Yu 625503331233SChen Yu ret = fread(bootcmd, sizeof(char), COMMAND_LINE_SIZE - 1, fp); 625603331233SChen Yu if (ret) { 625703331233SChen Yu bootcmd[ret] = '\0'; 625803331233SChen Yu /* the last character is already '\n' */ 625903331233SChen Yu fprintf(outf, "Kernel command line: %s", bootcmd); 626003331233SChen Yu } 626103331233SChen Yu 626203331233SChen Yu fclose(fp); 626303331233SChen Yu } 626403331233SChen Yu 6265495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 6266495c7654SLen Brown unsigned int width, enum counter_scope scope, 626741618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 6268388e9c81SLen Brown { 6269388e9c81SLen Brown struct msr_counter *msrp; 6270388e9c81SLen Brown 6271388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 6272388e9c81SLen Brown if (msrp == NULL) { 6273388e9c81SLen Brown perror("calloc"); 6274388e9c81SLen Brown exit(1); 6275388e9c81SLen Brown } 6276388e9c81SLen Brown 6277388e9c81SLen Brown msrp->msr_num = msr_num; 6278d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 6279495c7654SLen Brown if (path) 6280d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 6281388e9c81SLen Brown msrp->width = width; 6282388e9c81SLen Brown msrp->type = type; 6283388e9c81SLen Brown msrp->format = format; 628441618e63SLen Brown msrp->flags = flags; 6285388e9c81SLen Brown 6286388e9c81SLen Brown switch (scope) { 6287388e9c81SLen Brown 6288388e9c81SLen Brown case SCOPE_CPU: 6289388e9c81SLen Brown msrp->next = sys.tp; 6290388e9c81SLen Brown sys.tp = msrp; 6291678a3bd1SLen Brown sys.added_thread_counters++; 62920748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 62931b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS); 6294678a3bd1SLen Brown exit(-1); 6295678a3bd1SLen Brown } 6296388e9c81SLen Brown break; 6297388e9c81SLen Brown 6298388e9c81SLen Brown case SCOPE_CORE: 6299388e9c81SLen Brown msrp->next = sys.cp; 6300388e9c81SLen Brown sys.cp = msrp; 6301678a3bd1SLen Brown sys.added_core_counters++; 6302678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 63031b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS); 6304678a3bd1SLen Brown exit(-1); 6305678a3bd1SLen Brown } 6306388e9c81SLen Brown break; 6307388e9c81SLen Brown 6308388e9c81SLen Brown case SCOPE_PACKAGE: 6309388e9c81SLen Brown msrp->next = sys.pp; 6310388e9c81SLen Brown sys.pp = msrp; 6311678a3bd1SLen Brown sys.added_package_counters++; 6312678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 63131b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS); 6314678a3bd1SLen Brown exit(-1); 6315678a3bd1SLen Brown } 6316388e9c81SLen Brown break; 6317388e9c81SLen Brown } 6318388e9c81SLen Brown 6319388e9c81SLen Brown return 0; 6320388e9c81SLen Brown } 6321388e9c81SLen Brown 6322388e9c81SLen Brown void parse_add_command(char *add_command) 6323388e9c81SLen Brown { 6324388e9c81SLen Brown int msr_num = 0; 6325495c7654SLen Brown char *path = NULL; 63260f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 6327388e9c81SLen Brown int width = 64; 6328388e9c81SLen Brown int fail = 0; 6329388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 6330388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 6331388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 6332388e9c81SLen Brown 6333388e9c81SLen Brown while (add_command) { 6334388e9c81SLen Brown 6335388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 6336388e9c81SLen Brown goto next; 6337388e9c81SLen Brown 6338388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 6339388e9c81SLen Brown goto next; 6340388e9c81SLen Brown 6341495c7654SLen Brown if (*add_command == '/') { 6342495c7654SLen Brown path = add_command; 6343495c7654SLen Brown goto next; 6344495c7654SLen Brown } 6345495c7654SLen Brown 6346388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 6347388e9c81SLen Brown if ((width == 32) || (width == 64)) 6348388e9c81SLen Brown goto next; 6349388e9c81SLen Brown width = 64; 6350388e9c81SLen Brown } 6351388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6352388e9c81SLen Brown scope = SCOPE_CPU; 6353388e9c81SLen Brown goto next; 6354388e9c81SLen Brown } 6355388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6356388e9c81SLen Brown scope = SCOPE_CORE; 6357388e9c81SLen Brown goto next; 6358388e9c81SLen Brown } 6359388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6360388e9c81SLen Brown scope = SCOPE_PACKAGE; 6361388e9c81SLen Brown goto next; 6362388e9c81SLen Brown } 6363388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6364388e9c81SLen Brown type = COUNTER_CYCLES; 6365388e9c81SLen Brown goto next; 6366388e9c81SLen Brown } 6367388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6368388e9c81SLen Brown type = COUNTER_SECONDS; 6369388e9c81SLen Brown goto next; 6370388e9c81SLen Brown } 637141618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 637241618e63SLen Brown type = COUNTER_USEC; 637341618e63SLen Brown goto next; 637441618e63SLen Brown } 6375388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6376388e9c81SLen Brown format = FORMAT_RAW; 6377388e9c81SLen Brown goto next; 6378388e9c81SLen Brown } 6379388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6380388e9c81SLen Brown format = FORMAT_DELTA; 6381388e9c81SLen Brown goto next; 6382388e9c81SLen Brown } 6383388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6384388e9c81SLen Brown format = FORMAT_PERCENT; 6385388e9c81SLen Brown goto next; 6386388e9c81SLen Brown } 6387388e9c81SLen Brown 6388388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6389388e9c81SLen Brown char *eos; 6390388e9c81SLen Brown 6391388e9c81SLen Brown eos = strchr(name_buffer, ','); 6392388e9c81SLen Brown if (eos) 6393388e9c81SLen Brown *eos = '\0'; 6394388e9c81SLen Brown goto next; 6395388e9c81SLen Brown } 6396388e9c81SLen Brown 6397388e9c81SLen Brown next: 6398388e9c81SLen Brown add_command = strchr(add_command, ','); 6399495c7654SLen Brown if (add_command) { 6400495c7654SLen Brown *add_command = '\0'; 6401388e9c81SLen Brown add_command++; 6402495c7654SLen Brown } 6403388e9c81SLen Brown 6404388e9c81SLen Brown } 6405495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6406495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6407388e9c81SLen Brown fail++; 6408388e9c81SLen Brown } 6409388e9c81SLen Brown 6410388e9c81SLen Brown /* generate default column header */ 6411388e9c81SLen Brown if (*name_buffer == '\0') { 6412388e9c81SLen Brown if (width == 32) 64135f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6414388e9c81SLen Brown else 64155f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6416388e9c81SLen Brown } 6417388e9c81SLen Brown 641841618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6419388e9c81SLen Brown fail++; 6420388e9c81SLen Brown 6421388e9c81SLen Brown if (fail) { 6422388e9c81SLen Brown help(); 6423388e9c81SLen Brown exit(1); 6424388e9c81SLen Brown } 6425388e9c81SLen Brown } 642641618e63SLen Brown 64270fc521bcSZephaniah E. Loss-Cutler-Hull int is_deferred_add(char *name) 64280fc521bcSZephaniah E. Loss-Cutler-Hull { 64290fc521bcSZephaniah E. Loss-Cutler-Hull int i; 64300fc521bcSZephaniah E. Loss-Cutler-Hull 64310fc521bcSZephaniah E. Loss-Cutler-Hull for (i = 0; i < deferred_add_index; ++i) 64320fc521bcSZephaniah E. Loss-Cutler-Hull if (!strcmp(name, deferred_add_names[i])) 64330fc521bcSZephaniah E. Loss-Cutler-Hull return 1; 64340fc521bcSZephaniah E. Loss-Cutler-Hull return 0; 64350fc521bcSZephaniah E. Loss-Cutler-Hull } 64360fc521bcSZephaniah E. Loss-Cutler-Hull 6437dd778a5eSLen Brown int is_deferred_skip(char *name) 6438dd778a5eSLen Brown { 6439dd778a5eSLen Brown int i; 6440dd778a5eSLen Brown 6441dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6442dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6443dd778a5eSLen Brown return 1; 6444dd778a5eSLen Brown return 0; 6445dd778a5eSLen Brown } 6446dd778a5eSLen Brown 644741618e63SLen Brown void probe_sysfs(void) 644841618e63SLen Brown { 644941618e63SLen Brown char path[64]; 645041618e63SLen Brown char name_buf[16]; 645141618e63SLen Brown FILE *input; 645241618e63SLen Brown int state; 645341618e63SLen Brown char *sp; 645441618e63SLen Brown 64550748eaf0SLen Brown for (state = 10; state >= 0; --state) { 645641618e63SLen Brown 64571b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 645841618e63SLen Brown input = fopen(path, "r"); 645941618e63SLen Brown if (input == NULL) 646041618e63SLen Brown continue; 64618173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 64628173c336SBen Hutchings err(1, "%s: failed to read file", path); 646341618e63SLen Brown 646441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 646541618e63SLen Brown sp = strchr(name_buf, '-'); 646641618e63SLen Brown if (!sp) 646741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 646841618e63SLen Brown *sp = '%'; 646941618e63SLen Brown *(sp + 1) = '\0'; 647041618e63SLen Brown 6471fecb3bc8SDavid Arcari remove_underbar(name_buf); 6472fecb3bc8SDavid Arcari 647341618e63SLen Brown fclose(input); 647441618e63SLen Brown 647541618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 647641618e63SLen Brown 64770fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 64780fc521bcSZephaniah E. Loss-Cutler-Hull continue; 64790fc521bcSZephaniah E. Loss-Cutler-Hull 6480dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6481dd778a5eSLen Brown continue; 6482dd778a5eSLen Brown 64831b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU); 648441618e63SLen Brown } 648541618e63SLen Brown 64860748eaf0SLen Brown for (state = 10; state >= 0; --state) { 648741618e63SLen Brown 64881b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 648941618e63SLen Brown input = fopen(path, "r"); 649041618e63SLen Brown if (input == NULL) 649141618e63SLen Brown continue; 64928173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 64938173c336SBen Hutchings err(1, "%s: failed to read file", path); 649441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 649541618e63SLen Brown sp = strchr(name_buf, '-'); 649641618e63SLen Brown if (!sp) 649741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 649841618e63SLen Brown *sp = '\0'; 649941618e63SLen Brown fclose(input); 650041618e63SLen Brown 6501fecb3bc8SDavid Arcari remove_underbar(name_buf); 6502fecb3bc8SDavid Arcari 650341618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 650441618e63SLen Brown 65050fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 65060fc521bcSZephaniah E. Loss-Cutler-Hull continue; 65070fc521bcSZephaniah E. Loss-Cutler-Hull 6508dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6509dd778a5eSLen Brown continue; 6510dd778a5eSLen Brown 65111b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU); 651241618e63SLen Brown } 651341618e63SLen Brown 651441618e63SLen Brown } 651541618e63SLen Brown 65161ef7d21aSLen Brown /* 65171ef7d21aSLen Brown * parse cpuset with following syntax 65181ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 65191ef7d21aSLen Brown */ 65201ef7d21aSLen Brown void parse_cpu_command(char *optarg) 65211ef7d21aSLen Brown { 65221ef7d21aSLen Brown unsigned int start, end; 65231ef7d21aSLen Brown char *next; 65241ef7d21aSLen Brown 65254e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 65264e4e1e7cSLen Brown if (cpu_subset) 65274e4e1e7cSLen Brown goto error; 65284e4e1e7cSLen Brown show_core_only++; 65294e4e1e7cSLen Brown return; 65304e4e1e7cSLen Brown } 65314e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 65324e4e1e7cSLen Brown if (cpu_subset) 65334e4e1e7cSLen Brown goto error; 65344e4e1e7cSLen Brown show_pkg_only++; 65354e4e1e7cSLen Brown return; 65364e4e1e7cSLen Brown } 65374e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 65384e4e1e7cSLen Brown goto error; 65394e4e1e7cSLen Brown 65401ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 65411ef7d21aSLen Brown if (cpu_subset == NULL) 65421ef7d21aSLen Brown err(3, "CPU_ALLOC"); 65431ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 65441ef7d21aSLen Brown 65451ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 65461ef7d21aSLen Brown 65471ef7d21aSLen Brown next = optarg; 65481ef7d21aSLen Brown 65491ef7d21aSLen Brown while (next && *next) { 65501ef7d21aSLen Brown 65511ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 65521ef7d21aSLen Brown goto error; 65531ef7d21aSLen Brown 65541ef7d21aSLen Brown start = strtoul(next, &next, 10); 65551ef7d21aSLen Brown 65561ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 65571ef7d21aSLen Brown goto error; 65581ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 65591ef7d21aSLen Brown 65601ef7d21aSLen Brown if (*next == '\0') 65611ef7d21aSLen Brown break; 65621ef7d21aSLen Brown 65631ef7d21aSLen Brown if (*next == ',') { 65641ef7d21aSLen Brown next += 1; 65651ef7d21aSLen Brown continue; 65661ef7d21aSLen Brown } 65671ef7d21aSLen Brown 65681ef7d21aSLen Brown if (*next == '-') { 65691ef7d21aSLen Brown next += 1; /* start range */ 65701ef7d21aSLen Brown } else if (*next == '.') { 65711ef7d21aSLen Brown next += 1; 65721ef7d21aSLen Brown if (*next == '.') 65731ef7d21aSLen Brown next += 1; /* start range */ 65741ef7d21aSLen Brown else 65751ef7d21aSLen Brown goto error; 65761ef7d21aSLen Brown } 65771ef7d21aSLen Brown 65781ef7d21aSLen Brown end = strtoul(next, &next, 10); 65791ef7d21aSLen Brown if (end <= start) 65801ef7d21aSLen Brown goto error; 65811ef7d21aSLen Brown 65821ef7d21aSLen Brown while (++start <= end) { 65831ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 65841ef7d21aSLen Brown goto error; 65851ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 65861ef7d21aSLen Brown } 65871ef7d21aSLen Brown 65881ef7d21aSLen Brown if (*next == ',') 65891ef7d21aSLen Brown next += 1; 65901ef7d21aSLen Brown else if (*next != '\0') 65911ef7d21aSLen Brown goto error; 65921ef7d21aSLen Brown } 65931ef7d21aSLen Brown 65941ef7d21aSLen Brown return; 65951ef7d21aSLen Brown 65961ef7d21aSLen Brown error: 65974e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 65984e4e1e7cSLen Brown help(); 65991ef7d21aSLen Brown exit(-1); 66001ef7d21aSLen Brown } 66011ef7d21aSLen Brown 6602103a8feaSLen Brown void cmdline(int argc, char **argv) 6603103a8feaSLen Brown { 6604103a8feaSLen Brown int opt; 6605d8af6f5fSLen Brown int option_index = 0; 6606d8af6f5fSLen Brown static struct option long_options[] = { 6607388e9c81SLen Brown { "add", required_argument, 0, 'a' }, 66081ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' }, 6609d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' }, 661096e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 66113f44a5c6SLen Brown { "enable", required_argument, 0, 'e' }, 6612d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' }, 66132af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' }, 6614023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' }, 6615c7e399f8SZephaniah E. Loss-Cutler-Hull { "header_iterations", required_argument, 0, 'N' }, 6616d8af6f5fSLen Brown { "help", no_argument, 0, 'h' }, 6617812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 6618d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' }, 6619c8ade361SLen Brown { "list", no_argument, 0, 'l' }, 6620b7d8c148SLen Brown { "out", required_argument, 0, 'o' }, 662196e47158SLen Brown { "quiet", no_argument, 0, 'q' }, 6622812db3f7SLen Brown { "show", required_argument, 0, 's' }, 6623d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' }, 6624d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' }, 6625d8af6f5fSLen Brown { "version", no_argument, 0, 'v' }, 6626d8af6f5fSLen Brown { 0, 0, 0, 0 } 6627d8af6f5fSLen Brown }; 6628103a8feaSLen Brown 6629103a8feaSLen Brown progname = argv[0]; 6630103a8feaSLen Brown 66311b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) { 6632103a8feaSLen Brown switch (opt) { 6633388e9c81SLen Brown case 'a': 6634388e9c81SLen Brown parse_add_command(optarg); 6635388e9c81SLen Brown break; 66361ef7d21aSLen Brown case 'c': 66371ef7d21aSLen Brown parse_cpu_command(optarg); 66381ef7d21aSLen Brown break; 6639d8af6f5fSLen Brown case 'D': 6640d8af6f5fSLen Brown dump_only++; 66418e180f3cSLen Brown break; 66423f44a5c6SLen Brown case 'e': 66433f44a5c6SLen Brown /* --enable specified counter */ 66444c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 66453f44a5c6SLen Brown break; 6646d8af6f5fSLen Brown case 'd': 6647d8af6f5fSLen Brown debug++; 66483f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 66492f32edf1SLen Brown break; 6650812db3f7SLen Brown case 'H': 66513f44a5c6SLen Brown /* 66523f44a5c6SLen Brown * --hide: do not show those specified 66533f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 66543f44a5c6SLen Brown */ 66553f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6656812db3f7SLen Brown break; 6657d8af6f5fSLen Brown case 'h': 6658d8af6f5fSLen Brown default: 6659d8af6f5fSLen Brown help(); 6660d8af6f5fSLen Brown exit(1); 6661d8af6f5fSLen Brown case 'i': 66622a0609c0SLen Brown { 66632a0609c0SLen Brown double interval = strtod(optarg, NULL); 66642a0609c0SLen Brown 66652a0609c0SLen Brown if (interval < 0.001) { 66661b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval); 66672a0609c0SLen Brown exit(2); 66682a0609c0SLen Brown } 66692a0609c0SLen Brown 667047936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6671b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 667247936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 66732a0609c0SLen Brown } 6674889facbeSLen Brown break; 66755c56be9aSDirk Brandewie case 'J': 66765c56be9aSDirk Brandewie rapl_joules++; 66775c56be9aSDirk Brandewie break; 6678c8ade361SLen Brown case 'l': 66793f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6680c8ade361SLen Brown list_header_only++; 6681c8ade361SLen Brown quiet++; 6682c8ade361SLen Brown break; 6683b7d8c148SLen Brown case 'o': 6684b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6685b7d8c148SLen Brown break; 668696e47158SLen Brown case 'q': 668796e47158SLen Brown quiet = 1; 668896e47158SLen Brown break; 6689023fe0acSChen Yu case 'n': 6690023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6691023fe0acSChen Yu 6692023fe0acSChen Yu if (num_iterations <= 0) { 66931b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations); 6694023fe0acSChen Yu exit(2); 6695023fe0acSChen Yu } 6696023fe0acSChen Yu break; 6697c7e399f8SZephaniah E. Loss-Cutler-Hull case 'N': 6698c7e399f8SZephaniah E. Loss-Cutler-Hull header_iterations = strtod(optarg, NULL); 6699c7e399f8SZephaniah E. Loss-Cutler-Hull 6700c7e399f8SZephaniah E. Loss-Cutler-Hull if (header_iterations <= 0) { 6701164d7a96SLen Brown fprintf(outf, "iterations %d should be positive number\n", header_iterations); 6702c7e399f8SZephaniah E. Loss-Cutler-Hull exit(2); 6703c7e399f8SZephaniah E. Loss-Cutler-Hull } 6704c7e399f8SZephaniah E. Loss-Cutler-Hull break; 6705812db3f7SLen Brown case 's': 67063f44a5c6SLen Brown /* 67073f44a5c6SLen Brown * --show: show only those specified 67083f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 67093f44a5c6SLen Brown * subsequent invocations can add to it. 67103f44a5c6SLen Brown */ 67113f44a5c6SLen Brown if (shown == 0) 67123f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 67133f44a5c6SLen Brown else 67143f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 67153f44a5c6SLen Brown shown = 1; 6716812db3f7SLen Brown break; 6717d8af6f5fSLen Brown case 'S': 6718d8af6f5fSLen Brown summary_only++; 6719d8af6f5fSLen Brown break; 6720d8af6f5fSLen Brown case 'T': 672155279aefSZhang Rui tj_max_override = atoi(optarg); 6722d8af6f5fSLen Brown break; 6723d8af6f5fSLen Brown case 'v': 6724d8af6f5fSLen Brown print_version(); 6725d8af6f5fSLen Brown exit(0); 6726d8af6f5fSLen Brown break; 6727103a8feaSLen Brown } 6728103a8feaSLen Brown } 6729103a8feaSLen Brown } 6730103a8feaSLen Brown 673152b3541bSWyes Karny void set_rlimit(void) 673252b3541bSWyes Karny { 673352b3541bSWyes Karny struct rlimit limit; 673452b3541bSWyes Karny 673552b3541bSWyes Karny if (getrlimit(RLIMIT_NOFILE, &limit) < 0) 673652b3541bSWyes Karny err(1, "Failed to get rlimit"); 673752b3541bSWyes Karny 673852b3541bSWyes Karny if (limit.rlim_max < MAX_NOFILE) 673952b3541bSWyes Karny limit.rlim_max = MAX_NOFILE; 674052b3541bSWyes Karny if (limit.rlim_cur < MAX_NOFILE) 674152b3541bSWyes Karny limit.rlim_cur = MAX_NOFILE; 674252b3541bSWyes Karny 674352b3541bSWyes Karny if (setrlimit(RLIMIT_NOFILE, &limit) < 0) 674452b3541bSWyes Karny err(1, "Failed to set rlimit"); 674552b3541bSWyes Karny } 674652b3541bSWyes Karny 6747103a8feaSLen Brown int main(int argc, char **argv) 6748103a8feaSLen Brown { 6749b7d8c148SLen Brown outf = stderr; 6750103a8feaSLen Brown cmdline(argc, argv); 6751103a8feaSLen Brown 675203331233SChen Yu if (!quiet) { 6753d8af6f5fSLen Brown print_version(); 675403331233SChen Yu print_bootcmd(); 675503331233SChen Yu } 6756103a8feaSLen Brown 675741618e63SLen Brown probe_sysfs(); 675841618e63SLen Brown 675952b3541bSWyes Karny if (!getuid()) 676052b3541bSWyes Karny set_rlimit(); 676152b3541bSWyes Karny 6762103a8feaSLen Brown turbostat_init(); 6763103a8feaSLen Brown 67646799ba84SDan Merillat msr_sum_record(); 67656799ba84SDan Merillat 67663b4d5c7fSAndy Shevchenko /* dump counters and exit */ 67673b4d5c7fSAndy Shevchenko if (dump_only) 67683b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 67693b4d5c7fSAndy Shevchenko 6770c8ade361SLen Brown /* list header and exit */ 6771c8ade361SLen Brown if (list_header_only) { 6772c8ade361SLen Brown print_header(","); 6773c8ade361SLen Brown flush_output_stdout(); 6774c8ade361SLen Brown return 0; 6775c8ade361SLen Brown } 6776c8ade361SLen Brown 6777103a8feaSLen Brown /* 6778103a8feaSLen Brown * if any params left, it must be a command to fork 6779103a8feaSLen Brown */ 6780103a8feaSLen Brown if (argc - optind) 6781103a8feaSLen Brown return fork_it(argv + optind); 6782103a8feaSLen Brown else 6783103a8feaSLen Brown turbostat_loop(); 6784103a8feaSLen Brown 6785103a8feaSLen Brown return 0; 6786103a8feaSLen Brown } 6787