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 * 658990892SLen Brown * Copyright (c) 2022 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 56164d7a96SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE }; 57164d7a96SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC }; 58164d7a96SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT }; 59164d7a96SLen Brown 60164d7a96SLen Brown struct msr_counter { 61164d7a96SLen Brown unsigned int msr_num; 62164d7a96SLen Brown char name[NAME_BYTES]; 63164d7a96SLen Brown char path[PATH_BYTES]; 64164d7a96SLen Brown unsigned int width; 65164d7a96SLen Brown enum counter_type type; 66164d7a96SLen Brown enum counter_format format; 67164d7a96SLen Brown struct msr_counter *next; 68164d7a96SLen Brown unsigned int flags; 69164d7a96SLen Brown #define FLAGS_HIDE (1 << 0) 70164d7a96SLen Brown #define FLAGS_SHOW (1 << 1) 71164d7a96SLen Brown #define SYSFS_PERCPU (1 << 1) 72164d7a96SLen Brown }; 73164d7a96SLen Brown 74164d7a96SLen Brown struct msr_counter bic[] = { 759878bf7aSLen Brown { 0x0, "usec", "", 0, 0, 0, NULL, 0 }, 769878bf7aSLen Brown { 0x0, "Time_Of_Day_Seconds", "", 0, 0, 0, NULL, 0 }, 779878bf7aSLen Brown { 0x0, "Package", "", 0, 0, 0, NULL, 0 }, 789878bf7aSLen Brown { 0x0, "Node", "", 0, 0, 0, NULL, 0 }, 799878bf7aSLen Brown { 0x0, "Avg_MHz", "", 0, 0, 0, NULL, 0 }, 809878bf7aSLen Brown { 0x0, "Busy%", "", 0, 0, 0, NULL, 0 }, 819878bf7aSLen Brown { 0x0, "Bzy_MHz", "", 0, 0, 0, NULL, 0 }, 829878bf7aSLen Brown { 0x0, "TSC_MHz", "", 0, 0, 0, NULL, 0 }, 839878bf7aSLen Brown { 0x0, "IRQ", "", 0, 0, 0, NULL, 0 }, 849878bf7aSLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL, 0 }, 859878bf7aSLen Brown { 0x0, "sysfs", "", 0, 0, 0, NULL, 0 }, 869878bf7aSLen Brown { 0x0, "CPU%c1", "", 0, 0, 0, NULL, 0 }, 879878bf7aSLen Brown { 0x0, "CPU%c3", "", 0, 0, 0, NULL, 0 }, 889878bf7aSLen Brown { 0x0, "CPU%c6", "", 0, 0, 0, NULL, 0 }, 899878bf7aSLen Brown { 0x0, "CPU%c7", "", 0, 0, 0, NULL, 0 }, 909878bf7aSLen Brown { 0x0, "ThreadC", "", 0, 0, 0, NULL, 0 }, 919878bf7aSLen Brown { 0x0, "CoreTmp", "", 0, 0, 0, NULL, 0 }, 929878bf7aSLen Brown { 0x0, "CoreCnt", "", 0, 0, 0, NULL, 0 }, 939878bf7aSLen Brown { 0x0, "PkgTmp", "", 0, 0, 0, NULL, 0 }, 949878bf7aSLen Brown { 0x0, "GFX%rc6", "", 0, 0, 0, NULL, 0 }, 959878bf7aSLen Brown { 0x0, "GFXMHz", "", 0, 0, 0, NULL, 0 }, 969878bf7aSLen Brown { 0x0, "Pkg%pc2", "", 0, 0, 0, NULL, 0 }, 979878bf7aSLen Brown { 0x0, "Pkg%pc3", "", 0, 0, 0, NULL, 0 }, 989878bf7aSLen Brown { 0x0, "Pkg%pc6", "", 0, 0, 0, NULL, 0 }, 999878bf7aSLen Brown { 0x0, "Pkg%pc7", "", 0, 0, 0, NULL, 0 }, 1009878bf7aSLen Brown { 0x0, "Pkg%pc8", "", 0, 0, 0, NULL, 0 }, 1019878bf7aSLen Brown { 0x0, "Pkg%pc9", "", 0, 0, 0, NULL, 0 }, 1029878bf7aSLen Brown { 0x0, "Pk%pc10", "", 0, 0, 0, NULL, 0 }, 1039878bf7aSLen Brown { 0x0, "CPU%LPI", "", 0, 0, 0, NULL, 0 }, 1049878bf7aSLen Brown { 0x0, "SYS%LPI", "", 0, 0, 0, NULL, 0 }, 1059878bf7aSLen Brown { 0x0, "PkgWatt", "", 0, 0, 0, NULL, 0 }, 1069878bf7aSLen Brown { 0x0, "CorWatt", "", 0, 0, 0, NULL, 0 }, 1079878bf7aSLen Brown { 0x0, "GFXWatt", "", 0, 0, 0, NULL, 0 }, 1089878bf7aSLen Brown { 0x0, "PkgCnt", "", 0, 0, 0, NULL, 0 }, 1099878bf7aSLen Brown { 0x0, "RAMWatt", "", 0, 0, 0, NULL, 0 }, 1109878bf7aSLen Brown { 0x0, "PKG_%", "", 0, 0, 0, NULL, 0 }, 1119878bf7aSLen Brown { 0x0, "RAM_%", "", 0, 0, 0, NULL, 0 }, 1129878bf7aSLen Brown { 0x0, "Pkg_J", "", 0, 0, 0, NULL, 0 }, 1139878bf7aSLen Brown { 0x0, "Cor_J", "", 0, 0, 0, NULL, 0 }, 1149878bf7aSLen Brown { 0x0, "GFX_J", "", 0, 0, 0, NULL, 0 }, 1159878bf7aSLen Brown { 0x0, "RAM_J", "", 0, 0, 0, NULL, 0 }, 1169878bf7aSLen Brown { 0x0, "Mod%c6", "", 0, 0, 0, NULL, 0 }, 1179878bf7aSLen Brown { 0x0, "Totl%C0", "", 0, 0, 0, NULL, 0 }, 1189878bf7aSLen Brown { 0x0, "Any%C0", "", 0, 0, 0, NULL, 0 }, 1199878bf7aSLen Brown { 0x0, "GFX%C0", "", 0, 0, 0, NULL, 0 }, 1209878bf7aSLen Brown { 0x0, "CPUGFX%", "", 0, 0, 0, NULL, 0 }, 1219878bf7aSLen Brown { 0x0, "Core", "", 0, 0, 0, NULL, 0 }, 1229878bf7aSLen Brown { 0x0, "CPU", "", 0, 0, 0, NULL, 0 }, 1239878bf7aSLen Brown { 0x0, "APIC", "", 0, 0, 0, NULL, 0 }, 1249878bf7aSLen Brown { 0x0, "X2APIC", "", 0, 0, 0, NULL, 0 }, 1259878bf7aSLen Brown { 0x0, "Die", "", 0, 0, 0, NULL, 0 }, 1269878bf7aSLen Brown { 0x0, "GFXAMHz", "", 0, 0, 0, NULL, 0 }, 1279878bf7aSLen Brown { 0x0, "IPC", "", 0, 0, 0, NULL, 0 }, 1289878bf7aSLen Brown { 0x0, "CoreThr", "", 0, 0, 0, NULL, 0 }, 129a5c6d65dSLen Brown { 0x0, "UncMHz", "", 0, 0, 0, NULL, 0 }, 130164d7a96SLen Brown }; 131164d7a96SLen Brown 132164d7a96SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 133164d7a96SLen Brown #define BIC_USEC (1ULL << 0) 134164d7a96SLen Brown #define BIC_TOD (1ULL << 1) 135164d7a96SLen Brown #define BIC_Package (1ULL << 2) 136164d7a96SLen Brown #define BIC_Node (1ULL << 3) 137164d7a96SLen Brown #define BIC_Avg_MHz (1ULL << 4) 138164d7a96SLen Brown #define BIC_Busy (1ULL << 5) 139164d7a96SLen Brown #define BIC_Bzy_MHz (1ULL << 6) 140164d7a96SLen Brown #define BIC_TSC_MHz (1ULL << 7) 141164d7a96SLen Brown #define BIC_IRQ (1ULL << 8) 142164d7a96SLen Brown #define BIC_SMI (1ULL << 9) 143164d7a96SLen Brown #define BIC_sysfs (1ULL << 10) 144164d7a96SLen Brown #define BIC_CPU_c1 (1ULL << 11) 145164d7a96SLen Brown #define BIC_CPU_c3 (1ULL << 12) 146164d7a96SLen Brown #define BIC_CPU_c6 (1ULL << 13) 147164d7a96SLen Brown #define BIC_CPU_c7 (1ULL << 14) 148164d7a96SLen Brown #define BIC_ThreadC (1ULL << 15) 149164d7a96SLen Brown #define BIC_CoreTmp (1ULL << 16) 150164d7a96SLen Brown #define BIC_CoreCnt (1ULL << 17) 151164d7a96SLen Brown #define BIC_PkgTmp (1ULL << 18) 152164d7a96SLen Brown #define BIC_GFX_rc6 (1ULL << 19) 153164d7a96SLen Brown #define BIC_GFXMHz (1ULL << 20) 154164d7a96SLen Brown #define BIC_Pkgpc2 (1ULL << 21) 155164d7a96SLen Brown #define BIC_Pkgpc3 (1ULL << 22) 156164d7a96SLen Brown #define BIC_Pkgpc6 (1ULL << 23) 157164d7a96SLen Brown #define BIC_Pkgpc7 (1ULL << 24) 158164d7a96SLen Brown #define BIC_Pkgpc8 (1ULL << 25) 159164d7a96SLen Brown #define BIC_Pkgpc9 (1ULL << 26) 160164d7a96SLen Brown #define BIC_Pkgpc10 (1ULL << 27) 161164d7a96SLen Brown #define BIC_CPU_LPI (1ULL << 28) 162164d7a96SLen Brown #define BIC_SYS_LPI (1ULL << 29) 163164d7a96SLen Brown #define BIC_PkgWatt (1ULL << 30) 164164d7a96SLen Brown #define BIC_CorWatt (1ULL << 31) 165164d7a96SLen Brown #define BIC_GFXWatt (1ULL << 32) 166164d7a96SLen Brown #define BIC_PkgCnt (1ULL << 33) 167164d7a96SLen Brown #define BIC_RAMWatt (1ULL << 34) 168164d7a96SLen Brown #define BIC_PKG__ (1ULL << 35) 169164d7a96SLen Brown #define BIC_RAM__ (1ULL << 36) 170164d7a96SLen Brown #define BIC_Pkg_J (1ULL << 37) 171164d7a96SLen Brown #define BIC_Cor_J (1ULL << 38) 172164d7a96SLen Brown #define BIC_GFX_J (1ULL << 39) 173164d7a96SLen Brown #define BIC_RAM_J (1ULL << 40) 174164d7a96SLen Brown #define BIC_Mod_c6 (1ULL << 41) 175164d7a96SLen Brown #define BIC_Totl_c0 (1ULL << 42) 176164d7a96SLen Brown #define BIC_Any_c0 (1ULL << 43) 177164d7a96SLen Brown #define BIC_GFX_c0 (1ULL << 44) 178164d7a96SLen Brown #define BIC_CPUGFX (1ULL << 45) 179164d7a96SLen Brown #define BIC_Core (1ULL << 46) 180164d7a96SLen Brown #define BIC_CPU (1ULL << 47) 181164d7a96SLen Brown #define BIC_APIC (1ULL << 48) 182164d7a96SLen Brown #define BIC_X2APIC (1ULL << 49) 183164d7a96SLen Brown #define BIC_Die (1ULL << 50) 184164d7a96SLen Brown #define BIC_GFXACTMHz (1ULL << 51) 185164d7a96SLen Brown #define BIC_IPC (1ULL << 52) 186164d7a96SLen Brown #define BIC_CORE_THROT_CNT (1ULL << 53) 187a5c6d65dSLen Brown #define BIC_UNCORE_MHZ (1ULL << 54) 188164d7a96SLen Brown 189164d7a96SLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die ) 190164d7a96SLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__) 191a5c6d65dSLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz | BIC_UNCORE_MHZ) 192164d7a96SLen 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) 193164d7a96SLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC) 194164d7a96SLen Brown 195164d7a96SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 196164d7a96SLen Brown 197164d7a96SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 198164d7a96SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 199164d7a96SLen Brown 200164d7a96SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 201164d7a96SLen Brown #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 202164d7a96SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 203164d7a96SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 204164d7a96SLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 205164d7a96SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 206164d7a96SLen Brown 207103a8feaSLen Brown char *proc_stat = "/proc/stat"; 208b7d8c148SLen Brown FILE *outf; 20936229897SLen Brown int *fd_percpu; 2102af4f9b8SLen Brown int *fd_instr_count_percpu; 211b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 }; 21247936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 }; 213e9d3092fSZhang Rui 214e9d3092fSZhang Rui /* Save original CPU model */ 215e9d3092fSZhang Rui unsigned int model_orig; 216e9d3092fSZhang Rui 217023fe0acSChen Yu unsigned int num_iterations; 218c7e399f8SZephaniah E. Loss-Cutler-Hull unsigned int header_iterations; 219d8af6f5fSLen Brown unsigned int debug; 22096e47158SLen Brown unsigned int quiet; 2213f44a5c6SLen Brown unsigned int shown; 2220de6c0dfSLen Brown unsigned int sums_need_wide_columns; 223d8af6f5fSLen Brown unsigned int rapl_joules; 224d8af6f5fSLen Brown unsigned int summary_only; 225c8ade361SLen Brown unsigned int list_header_only; 226d8af6f5fSLen Brown unsigned int dump_only; 227103a8feaSLen Brown unsigned int do_snb_cstates; 228fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 229144b44b1SLen Brown unsigned int do_slm_cstates; 230144b44b1SLen Brown unsigned int use_c1_residency_msr; 231103a8feaSLen Brown unsigned int has_aperf; 232889facbeSLen Brown unsigned int has_epb; 2333ea8e52eSArtem Bityutskiy unsigned int has_turbo; 234774627c5SLen Brown unsigned int is_hybrid; 2355a63426eSLen Brown unsigned int do_irtl_snb; 2365a63426eSLen Brown unsigned int do_irtl_hsw; 237fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 238103a8feaSLen Brown unsigned int genuine_intel; 23934041551SLen Brown unsigned int authentic_amd; 240c1c10cc7SPu Wen unsigned int hygon_genuine; 24134041551SLen Brown unsigned int max_level, max_extended_level; 242103a8feaSLen Brown unsigned int has_invariant_tsc; 243d7899447SLen Brown unsigned int do_nhm_platform_info; 244cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 245b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 246103a8feaSLen Brown double bclk; 247a2b7b749SLen Brown double base_hz; 24821ed5574SLen Brown unsigned int has_base_hz; 249a2b7b749SLen Brown double tsc_tweak = 1.0; 250c98d5d94SLen Brown unsigned int show_pkg_only; 251c98d5d94SLen Brown unsigned int show_core_only; 252c98d5d94SLen Brown char *output_buffer, *outp; 253889facbeSLen Brown unsigned int do_rapl; 254889facbeSLen Brown unsigned int do_dts; 255889facbeSLen Brown unsigned int do_ptm; 2562af4f9b8SLen Brown unsigned int do_ipc; 257fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 258be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 259be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 26027d47356SLen Brown unsigned int gfx_cur_mhz; 261b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 26255279aefSZhang Rui unsigned int tj_max; 26355279aefSZhang Rui unsigned int tj_max_override; 2640b9a0b9bSZhang Rui int tcc_offset_bits; 26540ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 26640ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 267889facbeSLen Brown double rapl_joule_counter_range; 2683a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 269ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 270aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 2713a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 2723a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 2738a5bdf41SLen Brown unsigned int crystal_hz; 2748a5bdf41SLen Brown unsigned long long tsc_hz; 2757ce7d5deSPrarit Bhargava int base_cpu; 27621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 2777f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 2787f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 2797f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 2807f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 2817f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 2827f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 28333148d67SLen Brown unsigned int has_misc_feature_control; 2844c2122d4SLen Brown unsigned int first_counter_read = 1; 285c026c236SArtem Bityutskiy int ignore_stdin; 286889facbeSLen Brown 287889facbeSLen Brown #define RAPL_PKG (1 << 0) 288e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 289e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 290e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 291e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 292e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 293e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 294e6f9bb3cSLen Brown 295889facbeSLen Brown #define RAPL_DRAM (1 << 3) 296e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 297e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 298e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 299e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 3000b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 3010b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 302e6f9bb3cSLen Brown 3039148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 304e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 3050b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 306e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 307e6f9bb3cSLen Brown 3080b2bb692SLen Brown #define RAPL_GFX (1 << 8) 309e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 310e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 311e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 3129148494cSJacob Pan 3139148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 3149148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 3159392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 3169392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 3179392bd98SCalvin Walton * not per-package. */ 3189392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 3199392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 3209392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 3219392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 3229148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 323889facbeSLen Brown #define TJMAX_DEFAULT 100 324889facbeSLen Brown 3259392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 3269392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 3279392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 3289392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 3299392bd98SCalvin Walton 330889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 331103a8feaSLen Brown 332103a8feaSLen Brown int backwards_count; 333103a8feaSLen Brown char *progname; 334103a8feaSLen Brown 3351ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 3361ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 3371ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 3380748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 3390748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 3400e2d8f05SLen Brown #define BITMASK_SIZE 32 341c98d5d94SLen Brown 342c98d5d94SLen Brown struct thread_data { 343f4fdf2b4SLen Brown struct timeval tv_begin; 344f4fdf2b4SLen Brown struct timeval tv_end; 345d4794f25SYazen Ghannam struct timeval tv_delta; 346c98d5d94SLen Brown unsigned long long tsc; 347c98d5d94SLen Brown unsigned long long aperf; 348c98d5d94SLen Brown unsigned long long mperf; 349144b44b1SLen Brown unsigned long long c1; 3502af4f9b8SLen Brown unsigned long long instr_count; 3510de6c0dfSLen Brown unsigned long long irq_count; 3521ed51011SLen Brown unsigned int smi_count; 353c98d5d94SLen Brown unsigned int cpu_id; 3544c2122d4SLen Brown unsigned int apic_id; 3554c2122d4SLen Brown unsigned int x2apic_id; 356c98d5d94SLen Brown unsigned int flags; 3577ab5ff49SZhang Rui bool is_atom; 358c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 359c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 3600748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 361c98d5d94SLen Brown } *thread_even, *thread_odd; 362c98d5d94SLen Brown 363c98d5d94SLen Brown struct core_data { 364c98d5d94SLen Brown unsigned long long c3; 365c98d5d94SLen Brown unsigned long long c6; 366c98d5d94SLen Brown unsigned long long c7; 3670539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 368889facbeSLen Brown unsigned int core_temp_c; 3699392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 370c98d5d94SLen Brown unsigned int core_id; 371eae97e05SChen Yu unsigned long long core_throt_cnt; 372678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 373c98d5d94SLen Brown } *core_even, *core_odd; 374c98d5d94SLen Brown 375c98d5d94SLen Brown struct pkg_data { 376c98d5d94SLen Brown unsigned long long pc2; 377c98d5d94SLen Brown unsigned long long pc3; 378c98d5d94SLen Brown unsigned long long pc6; 379c98d5d94SLen Brown unsigned long long pc7; 380ca58710fSKristen Carlson Accardi unsigned long long pc8; 381ca58710fSKristen Carlson Accardi unsigned long long pc9; 382ca58710fSKristen Carlson Accardi unsigned long long pc10; 383be0e54c4SLen Brown unsigned long long cpu_lpi; 384be0e54c4SLen Brown unsigned long long sys_lpi; 3850b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 3860b2bb692SLen Brown unsigned long long pkg_any_core_c0; 3870b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 3880b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 3899185e988SLen Brown long long gfx_rc6_ms; 39027d47356SLen Brown unsigned int gfx_mhz; 391b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 392c98d5d94SLen Brown unsigned int package_id; 3937c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 3947c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 3957c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 3967c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 3977c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 3987c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 399889facbeSLen Brown unsigned int pkg_temp_c; 400a5c6d65dSLen Brown unsigned int uncore_mhz; 401678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 402c98d5d94SLen Brown } *package_even, *package_odd; 403c98d5d94SLen Brown 404c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 405c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 406c98d5d94SLen Brown 40740f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 40840f5cfe7SPrarit Bhargava ((thread_base) + \ 40940f5cfe7SPrarit Bhargava ((pkg_no) * \ 41040f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 41140f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 41240f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 41340f5cfe7SPrarit Bhargava (thread_no)) 41440f5cfe7SPrarit Bhargava 41540f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 41640f5cfe7SPrarit Bhargava ((core_base) + \ 41740f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 41840f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 41940f5cfe7SPrarit Bhargava (core_no)) 42040f5cfe7SPrarit Bhargava 421c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 422c98d5d94SLen Brown 42387e15da9SChen Yu /* 42487e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 42587e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 42687e15da9SChen Yu * despite its register's bit width. 42787e15da9SChen Yu */ 42887e15da9SChen Yu enum { 42987e15da9SChen Yu IDX_PKG_ENERGY, 43087e15da9SChen Yu IDX_DRAM_ENERGY, 43187e15da9SChen Yu IDX_PP0_ENERGY, 43287e15da9SChen Yu IDX_PP1_ENERGY, 43387e15da9SChen Yu IDX_PKG_PERF, 43487e15da9SChen Yu IDX_DRAM_PERF, 43587e15da9SChen Yu IDX_COUNT, 43687e15da9SChen Yu }; 43787e15da9SChen Yu 43887e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 43987e15da9SChen Yu 44087e15da9SChen Yu struct msr_sum_array { 44187e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 44287e15da9SChen Yu struct { 44387e15da9SChen Yu /*The accumulated MSR value is updated by the timer */ 44487e15da9SChen Yu unsigned long long sum; 44587e15da9SChen Yu /*The MSR footprint recorded in last timer */ 44687e15da9SChen Yu unsigned long long last; 44787e15da9SChen Yu } entries[IDX_COUNT]; 44887e15da9SChen Yu }; 44987e15da9SChen Yu 45087e15da9SChen Yu /* The percpu MSR sum array.*/ 45187e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 45287e15da9SChen Yu 45313a779deSCalvin Walton off_t idx_to_offset(int idx) 45487e15da9SChen Yu { 45513a779deSCalvin Walton off_t offset; 45687e15da9SChen Yu 45787e15da9SChen Yu switch (idx) { 45887e15da9SChen Yu case IDX_PKG_ENERGY: 459301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H) 460301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT; 461301b1d3aSBas Nieuwenhuizen else 46287e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 46387e15da9SChen Yu break; 46487e15da9SChen Yu case IDX_DRAM_ENERGY: 46587e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 46687e15da9SChen Yu break; 46787e15da9SChen Yu case IDX_PP0_ENERGY: 46887e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 46987e15da9SChen Yu break; 47087e15da9SChen Yu case IDX_PP1_ENERGY: 47187e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 47287e15da9SChen Yu break; 47387e15da9SChen Yu case IDX_PKG_PERF: 47487e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 47587e15da9SChen Yu break; 47687e15da9SChen Yu case IDX_DRAM_PERF: 47787e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 47887e15da9SChen Yu break; 47987e15da9SChen Yu default: 48087e15da9SChen Yu offset = -1; 48187e15da9SChen Yu } 48287e15da9SChen Yu return offset; 48387e15da9SChen Yu } 48487e15da9SChen Yu 48513a779deSCalvin Walton int offset_to_idx(off_t offset) 48687e15da9SChen Yu { 48787e15da9SChen Yu int idx; 48887e15da9SChen Yu 48987e15da9SChen Yu switch (offset) { 49087e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 491301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT: 49287e15da9SChen Yu idx = IDX_PKG_ENERGY; 49387e15da9SChen Yu break; 49487e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 49587e15da9SChen Yu idx = IDX_DRAM_ENERGY; 49687e15da9SChen Yu break; 49787e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 49887e15da9SChen Yu idx = IDX_PP0_ENERGY; 49987e15da9SChen Yu break; 50087e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 50187e15da9SChen Yu idx = IDX_PP1_ENERGY; 50287e15da9SChen Yu break; 50387e15da9SChen Yu case MSR_PKG_PERF_STATUS: 50487e15da9SChen Yu idx = IDX_PKG_PERF; 50587e15da9SChen Yu break; 50687e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 50787e15da9SChen Yu idx = IDX_DRAM_PERF; 50887e15da9SChen Yu break; 50987e15da9SChen Yu default: 51087e15da9SChen Yu idx = -1; 51187e15da9SChen Yu } 51287e15da9SChen Yu return idx; 51387e15da9SChen Yu } 51487e15da9SChen Yu 51587e15da9SChen Yu int idx_valid(int idx) 51687e15da9SChen Yu { 51787e15da9SChen Yu switch (idx) { 51887e15da9SChen Yu case IDX_PKG_ENERGY: 519301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H); 52087e15da9SChen Yu case IDX_DRAM_ENERGY: 52187e15da9SChen Yu return do_rapl & RAPL_DRAM; 52287e15da9SChen Yu case IDX_PP0_ENERGY: 52387e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 52487e15da9SChen Yu case IDX_PP1_ENERGY: 52587e15da9SChen Yu return do_rapl & RAPL_GFX; 52687e15da9SChen Yu case IDX_PKG_PERF: 52787e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 52887e15da9SChen Yu case IDX_DRAM_PERF: 52987e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 53087e15da9SChen Yu default: 53187e15da9SChen Yu return 0; 53287e15da9SChen Yu } 53387e15da9SChen Yu } 5341b439f01SLen Brown 535388e9c81SLen Brown struct sys_counters { 536678a3bd1SLen Brown unsigned int added_thread_counters; 537678a3bd1SLen Brown unsigned int added_core_counters; 538678a3bd1SLen Brown unsigned int added_package_counters; 539388e9c81SLen Brown struct msr_counter *tp; 540388e9c81SLen Brown struct msr_counter *cp; 541388e9c81SLen Brown struct msr_counter *pp; 542388e9c81SLen Brown } sys; 543388e9c81SLen Brown 544c98d5d94SLen Brown struct system_summary { 545c98d5d94SLen Brown struct thread_data threads; 546c98d5d94SLen Brown struct core_data cores; 547c98d5d94SLen Brown struct pkg_data packages; 548388e9c81SLen Brown } average; 549c98d5d94SLen Brown 5500e2d8f05SLen Brown struct cpu_topology { 5510e2d8f05SLen Brown int physical_package_id; 5526de68fe1SLen Brown int die_id; 5530e2d8f05SLen Brown int logical_cpu_id; 554ef605741SPrarit Bhargava int physical_node_id; 555ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 5560e2d8f05SLen Brown int physical_core_id; 5578cb48b32SPrarit Bhargava int thread_id; 5580e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 5590e2d8f05SLen Brown } *cpus; 560c98d5d94SLen Brown 561c98d5d94SLen Brown struct topo_params { 562c98d5d94SLen Brown int num_packages; 5636de68fe1SLen Brown int num_die; 564103a8feaSLen Brown int num_cpus; 565c98d5d94SLen Brown int num_cores; 566c98d5d94SLen Brown int max_cpu_num; 567ef605741SPrarit Bhargava int max_node_num; 56870a9c6e8SPrarit Bhargava int nodes_per_pkg; 56970a9c6e8SPrarit Bhargava int cores_per_node; 57070a9c6e8SPrarit Bhargava int threads_per_core; 571c98d5d94SLen Brown } topo; 572103a8feaSLen Brown 573c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 574103a8feaSLen Brown 575562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 576562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 577562a2d37SLen Brown 578c98d5d94SLen Brown void setup_all_buffers(void); 579103a8feaSLen Brown 5801f81c5efSLen Brown char *sys_lpi_file; 5811f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 5821f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 5831f81c5efSLen Brown 584c98d5d94SLen Brown int cpu_is_not_present(int cpu) 585d15cf7c1SLen Brown { 586c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 587c98d5d94SLen Brown } 5881b439f01SLen Brown 589c98d5d94SLen Brown /* 590c98d5d94SLen Brown * run func(thread, core, package) in topology order 591c98d5d94SLen Brown * skip non-present cpus 592c98d5d94SLen Brown */ 593c98d5d94SLen Brown 594c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), 595c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 596c98d5d94SLen Brown { 59740f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 598c98d5d94SLen Brown 599c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 600df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 60170a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 6021b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 603c98d5d94SLen Brown struct thread_data *t; 604c98d5d94SLen Brown struct core_data *c; 605c98d5d94SLen Brown struct pkg_data *p; 606c98d5d94SLen Brown 6071b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 608c98d5d94SLen Brown 609c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 610c98d5d94SLen Brown continue; 611c98d5d94SLen Brown 6121b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 613c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 614c98d5d94SLen Brown 615c98d5d94SLen Brown retval = func(t, c, p); 616c98d5d94SLen Brown if (retval) 617c98d5d94SLen Brown return retval; 618c98d5d94SLen Brown } 619c98d5d94SLen Brown } 620c98d5d94SLen Brown } 62140f5cfe7SPrarit Bhargava } 622d15cf7c1SLen Brown return 0; 623d15cf7c1SLen Brown } 624d15cf7c1SLen Brown 62588c3281fSLen Brown int cpu_migrate(int cpu) 62688c3281fSLen Brown { 627c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 628c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 629c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 63088c3281fSLen Brown return -1; 63188c3281fSLen Brown else 63288c3281fSLen Brown return 0; 63388c3281fSLen Brown } 6341b439f01SLen Brown 63536229897SLen Brown int get_msr_fd(int cpu) 636103a8feaSLen Brown { 637103a8feaSLen Brown char pathname[32]; 638103a8feaSLen Brown int fd; 639103a8feaSLen Brown 64036229897SLen Brown fd = fd_percpu[cpu]; 64136229897SLen Brown 64236229897SLen Brown if (fd) 64336229897SLen Brown return fd; 64436229897SLen Brown 645103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 646103a8feaSLen Brown fd = open(pathname, O_RDONLY); 64715aaa346SLen Brown if (fd < 0) 64898481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 649103a8feaSLen Brown 65036229897SLen Brown fd_percpu[cpu] = fd; 65136229897SLen Brown 65236229897SLen Brown return fd; 65336229897SLen Brown } 65436229897SLen Brown 6552af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 6562af4f9b8SLen Brown { 6572af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 6582af4f9b8SLen Brown } 6592af4f9b8SLen Brown 6602af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 6612af4f9b8SLen Brown { 6622af4f9b8SLen Brown struct perf_event_attr pea; 6632af4f9b8SLen Brown int fd; 6642af4f9b8SLen Brown 6652af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 6662af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 6672af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 6682af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 6692af4f9b8SLen Brown 6702af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 6712af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 672164d7a96SLen Brown if (fd == -1) { 673164d7a96SLen Brown warn("cpu%d: perf instruction counter", cpu_num); 674164d7a96SLen Brown BIC_NOT_PRESENT(BIC_IPC); 675164d7a96SLen Brown } 6762af4f9b8SLen Brown 6772af4f9b8SLen Brown return fd; 6782af4f9b8SLen Brown } 6792af4f9b8SLen Brown 6802af4f9b8SLen Brown int get_instr_count_fd(int cpu) 6812af4f9b8SLen Brown { 6822af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 6832af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 6842af4f9b8SLen Brown 6852af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 6862af4f9b8SLen Brown 6872af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 6882af4f9b8SLen Brown } 6892af4f9b8SLen Brown 69036229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 69136229897SLen Brown { 69236229897SLen Brown ssize_t retval; 69336229897SLen Brown 69436229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 69515aaa346SLen Brown 69698481e79SLen Brown if (retval != sizeof *msr) 697cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 69815aaa346SLen Brown 69915aaa346SLen Brown return 0; 700103a8feaSLen Brown } 701103a8feaSLen Brown 702dd778a5eSLen Brown #define MAX_DEFERRED 16 7030fc521bcSZephaniah E. Loss-Cutler-Hull char *deferred_add_names[MAX_DEFERRED]; 704dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 7050fc521bcSZephaniah E. Loss-Cutler-Hull int deferred_add_index; 706dd778a5eSLen Brown int deferred_skip_index; 707dd778a5eSLen Brown 708dd778a5eSLen Brown /* 709dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 710dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 711dd778a5eSLen Brown */ 712dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 713dd778a5eSLen Brown 714dd778a5eSLen Brown void help(void) 715dd778a5eSLen Brown { 716dd778a5eSLen Brown fprintf(outf, 717dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 718dd778a5eSLen Brown "\n" 719dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 720dd778a5eSLen Brown "when COMMAND completes.\n" 721dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 722dd778a5eSLen Brown "to print statistics, until interrupted.\n" 723cc481650SNathan Ciobanu " -a, --add add a counter\n" 724dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 725cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 726dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7279ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7289ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7299ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7309ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7319ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7329ce80578SNathan Ciobanu " hide the specified column(s)\n" 733cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 734cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7359ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 736cc481650SNathan Ciobanu " -l, --list list column headers only\n" 737cc481650SNathan Ciobanu " -n, --num_iterations num\n" 738cc481650SNathan Ciobanu " number of the measurement iterations\n" 739c7e399f8SZephaniah E. Loss-Cutler-Hull " -N, --header_iterations num\n" 740c7e399f8SZephaniah E. Loss-Cutler-Hull " print header every num iterations\n" 741cc481650SNathan Ciobanu " -o, --out file\n" 742cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 743cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7449ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7459ce80578SNathan Ciobanu " show only the specified column(s)\n" 7469ce80578SNathan Ciobanu " -S, --Summary\n" 7479ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7489ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7499ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7509ce80578SNathan Ciobanu " degrees Celsius\n" 751cc481650SNathan Ciobanu " -h, --help print this help message\n" 7521b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n"); 753dd778a5eSLen Brown } 754dd778a5eSLen Brown 755812db3f7SLen Brown /* 756812db3f7SLen Brown * bic_lookup 757812db3f7SLen Brown * for all the strings in comma separate name_list, 758812db3f7SLen Brown * set the approprate bit in return value. 759812db3f7SLen Brown */ 760dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 761812db3f7SLen Brown { 7629878bf7aSLen Brown unsigned int i; 763812db3f7SLen Brown unsigned long long retval = 0; 764812db3f7SLen Brown 765812db3f7SLen Brown while (name_list) { 766812db3f7SLen Brown char *comma; 767812db3f7SLen Brown 768812db3f7SLen Brown comma = strchr(name_list, ','); 769812db3f7SLen Brown 770812db3f7SLen Brown if (comma) 771812db3f7SLen Brown *comma = '\0'; 772812db3f7SLen Brown 773812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 774812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 775812db3f7SLen Brown retval |= (1ULL << i); 776812db3f7SLen Brown break; 777812db3f7SLen Brown } 7785dc241f2SLen Brown if (!strcmp(name_list, "all")) { 7795dc241f2SLen Brown retval |= ~0; 7805dc241f2SLen Brown break; 7815dc241f2SLen Brown } else if (!strcmp(name_list, "topology")) { 7825dc241f2SLen Brown retval |= BIC_TOPOLOGY; 7835dc241f2SLen Brown break; 7845dc241f2SLen Brown } else if (!strcmp(name_list, "power")) { 7855dc241f2SLen Brown retval |= BIC_THERMAL_PWR; 7865dc241f2SLen Brown break; 7875dc241f2SLen Brown } else if (!strcmp(name_list, "idle")) { 7885dc241f2SLen Brown retval |= BIC_IDLE; 7895dc241f2SLen Brown break; 7905dc241f2SLen Brown } else if (!strcmp(name_list, "frequency")) { 7915dc241f2SLen Brown retval |= BIC_FREQUENCY; 7925dc241f2SLen Brown break; 7935dc241f2SLen Brown } else if (!strcmp(name_list, "other")) { 7945dc241f2SLen Brown retval |= BIC_OTHER; 7955dc241f2SLen Brown break; 7965dc241f2SLen Brown } 7975dc241f2SLen Brown 798812db3f7SLen Brown } 799812db3f7SLen Brown if (i == MAX_BIC) { 800dd778a5eSLen Brown if (mode == SHOW_LIST) { 8010fc521bcSZephaniah E. Loss-Cutler-Hull deferred_add_names[deferred_add_index++] = name_list; 8020fc521bcSZephaniah E. Loss-Cutler-Hull if (deferred_add_index >= MAX_DEFERRED) { 8030fc521bcSZephaniah E. Loss-Cutler-Hull fprintf(stderr, "More than max %d un-recognized --add options '%s'\n", 8040fc521bcSZephaniah E. Loss-Cutler-Hull MAX_DEFERRED, name_list); 8050fc521bcSZephaniah E. Loss-Cutler-Hull help(); 8060fc521bcSZephaniah E. Loss-Cutler-Hull exit(1); 807812db3f7SLen Brown } 8080fc521bcSZephaniah E. Loss-Cutler-Hull } else { 809dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 810dd778a5eSLen Brown if (debug) 811dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 812dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 813dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 814dd778a5eSLen Brown MAX_DEFERRED, name_list); 815dd778a5eSLen Brown help(); 816dd778a5eSLen Brown exit(1); 817dd778a5eSLen Brown } 818dd778a5eSLen Brown } 8190fc521bcSZephaniah E. Loss-Cutler-Hull } 820812db3f7SLen Brown 821812db3f7SLen Brown name_list = comma; 822812db3f7SLen Brown if (name_list) 823812db3f7SLen Brown name_list++; 824812db3f7SLen Brown 825812db3f7SLen Brown } 826812db3f7SLen Brown return retval; 827812db3f7SLen Brown } 828fc04cc67SLen Brown 829c8ade361SLen Brown void print_header(char *delim) 830103a8feaSLen Brown { 831388e9c81SLen Brown struct msr_counter *mp; 8326168c2e0SLen Brown int printed = 0; 833388e9c81SLen Brown 8343f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 8353f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 8363f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 8373f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 838812db3f7SLen Brown if (DO_BIC(BIC_Package)) 8396168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 8406de68fe1SLen Brown if (DO_BIC(BIC_Die)) 8416de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 84201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 84301235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 844812db3f7SLen Brown if (DO_BIC(BIC_Core)) 8456168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 846812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8476168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8484c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8494c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8504c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8514c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 852812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8536168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 854812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8556168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 856812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8576168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 858812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8596168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8601cc21f7bSLen Brown 8612af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8622af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8632af4f9b8SLen Brown 8640de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8650de6c0dfSLen Brown if (sums_need_wide_columns) 8666168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8670de6c0dfSLen Brown else 8686168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8690de6c0dfSLen Brown } 8700de6c0dfSLen Brown 871812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8726168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8731cc21f7bSLen Brown 874388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 875dd778a5eSLen Brown 876388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 877388e9c81SLen Brown if (mp->width == 64) 878dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 879388e9c81SLen Brown else 880dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 881388e9c81SLen Brown } else { 8820de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 883dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8840de6c0dfSLen Brown else 885dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 886388e9c81SLen Brown } 887388e9c81SLen Brown } 888388e9c81SLen Brown 88941618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8906168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 891562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8926168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 893812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8946168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 895812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8966168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 897678a3bd1SLen Brown 8980539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8996168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 900678a3bd1SLen Brown 901812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 9026168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 903388e9c81SLen Brown 904eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 905eae97e05SChen Yu outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : "")); 906eae97e05SChen Yu 9079392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 9089392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 9099392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 9109392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 9119392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 9129392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 9139392bd98SCalvin Walton } 9149392bd98SCalvin Walton 915388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 916388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 917388e9c81SLen Brown if (mp->width == 64) 918c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 919388e9c81SLen Brown else 920c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 921388e9c81SLen Brown } else { 9220de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9230de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9240de6c0dfSLen Brown else 925c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 926388e9c81SLen Brown } 927388e9c81SLen Brown } 928388e9c81SLen Brown 929812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 9306168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 931889facbeSLen Brown 932812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 9336168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 934fdf676e5SLen Brown 935812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 9366168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 93727d47356SLen Brown 938b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 939b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 940b4b91569SRafael Antognolli 941a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 9426168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 943a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 9446168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 945a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 9466168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 947a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 9486168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 9490b2bb692SLen Brown 9500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9516168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9536168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9540f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9556168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9560f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9576168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9596168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9600f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9616168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9620f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9636168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 964be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 965be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 966be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 967be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 968103a8feaSLen Brown 9695c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 970812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9716168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9729392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9736168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 974812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9756168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 976812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9776168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 978812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9796168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 980812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9816168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 982d7899447SLen Brown } else if (do_rapl && rapl_joules) { 983812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9846168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9859392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9866168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 987812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9886168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 989812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9906168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 991812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9926168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 993812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9946168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9955c56be9aSDirk Brandewie } 996a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ)) 997a5c6d65dSLen Brown outp += sprintf(outp, "%sUncMHz", (printed++ ? delim : "")); 998a5c6d65dSLen Brown 999388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 1000388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1001388e9c81SLen Brown if (mp->width == 64) 1002c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 1003388e9c81SLen Brown else 1004c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 1005388e9c81SLen Brown } else { 10060de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10070de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 10080de6c0dfSLen Brown else 1009c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 1010388e9c81SLen Brown } 1011388e9c81SLen Brown } 1012388e9c81SLen Brown 1013c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1014103a8feaSLen Brown } 1015103a8feaSLen Brown 10161b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1017103a8feaSLen Brown { 1018388e9c81SLen Brown int i; 1019388e9c81SLen Brown struct msr_counter *mp; 1020388e9c81SLen Brown 10213b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 1022c98d5d94SLen Brown 1023c98d5d94SLen Brown if (t) { 10241b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 10253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 10263b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 10273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 10283b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 10296886fee4SLen Brown 10302af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 10312af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 10322af4f9b8SLen Brown 1033812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 10340de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 1035812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 1036218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 1037388e9c81SLen Brown 1038388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 10391b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]); 1040388e9c81SLen Brown } 1041103a8feaSLen Brown } 1042103a8feaSLen Brown 1043c98d5d94SLen Brown if (c) { 10443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 10453b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 10463b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 10473b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 10483b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 1049eae97e05SChen Yu outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt); 10509392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 1051388e9c81SLen Brown 1052388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 10531b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]); 1054388e9c81SLen Brown } 10550539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1056c98d5d94SLen Brown } 1057103a8feaSLen Brown 1058c98d5d94SLen Brown if (p) { 10593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10600b2bb692SLen Brown 10610b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10620b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10630b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10640b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10650b2bb692SLen Brown 10663b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10670f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10683b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10703b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10733b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10743b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10753b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1076be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1077be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10787c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10797c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10807c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10817c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 10821b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status); 10831b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status); 10843b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1085388e9c81SLen Brown 1086388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 10871b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]); 1088388e9c81SLen Brown } 1089c98d5d94SLen Brown } 10903b4d5c7fSAndy Shevchenko 10913b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10923b4d5c7fSAndy Shevchenko 1093c98d5d94SLen Brown return 0; 1094103a8feaSLen Brown } 1095103a8feaSLen Brown 1096e23da037SLen Brown /* 1097e23da037SLen Brown * column formatting convention & formats 1098e23da037SLen Brown */ 10991b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1100103a8feaSLen Brown { 1101008d396eSLen Brown double interval_float, tsc; 1102fc04cc67SLen Brown char *fmt8; 1103388e9c81SLen Brown int i; 1104388e9c81SLen Brown struct msr_counter *mp; 11056168c2e0SLen Brown char *delim = "\t"; 11066168c2e0SLen Brown int printed = 0; 1107103a8feaSLen Brown 1108c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1109c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1110c98d5d94SLen Brown return 0; 1111c98d5d94SLen Brown 1112c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1113c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1114c98d5d94SLen Brown return 0; 1115c98d5d94SLen Brown 11161ef7d21aSLen Brown /*if not summary line and --cpu is used */ 11171b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 11181ef7d21aSLen Brown return 0; 11191ef7d21aSLen Brown 11203f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1121f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1122f4fdf2b4SLen Brown struct timeval tv; 1123f4fdf2b4SLen Brown 1124f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1125f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1126f4fdf2b4SLen Brown } 1127f4fdf2b4SLen Brown 11283f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 11293f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 11303f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 11313f44a5c6SLen Brown 1132d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 1133103a8feaSLen Brown 1134008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1135008d396eSLen Brown 1136c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1137c98d5d94SLen Brown if (t == &average.threads) { 1138812db3f7SLen Brown if (DO_BIC(BIC_Package)) 11396168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11406de68fe1SLen Brown if (DO_BIC(BIC_Die)) 11416de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 114201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 114301235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1144812db3f7SLen Brown if (DO_BIC(BIC_Core)) 11456168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1146812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11476168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11484c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11494c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11504c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11514c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1152103a8feaSLen Brown } else { 1153812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1154c98d5d94SLen Brown if (p) 11556168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1156c98d5d94SLen Brown else 11576168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1158c98d5d94SLen Brown } 11596de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11606de68fe1SLen Brown if (c) 11616de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11626de68fe1SLen Brown else 11636de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11646de68fe1SLen Brown } 116501235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 116601235041SPrarit Bhargava if (t) 116701235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 11681b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 116901235041SPrarit Bhargava else 11701b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 117101235041SPrarit Bhargava } 1172812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1173c98d5d94SLen Brown if (c) 11746168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1175c98d5d94SLen Brown else 11766168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1177c98d5d94SLen Brown } 1178812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11796168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11804c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11814c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11824c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11834c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1184103a8feaSLen Brown } 1185fc04cc67SLen Brown 1186812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 11871b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 1188fc04cc67SLen Brown 1189812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11906168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc); 1191103a8feaSLen Brown 1192812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 119321ed5574SLen Brown if (has_base_hz) 11941b439f01SLen Brown outp += 11951b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 119621ed5574SLen Brown else 11976168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1198008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 119921ed5574SLen Brown } 1200103a8feaSLen Brown 1201812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 12026168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 1203103a8feaSLen Brown 12042af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 12052af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 12062af4f9b8SLen Brown 1207562a2d37SLen Brown /* IRQ */ 12080de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 12090de6c0dfSLen Brown if (sums_need_wide_columns) 12106168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 12110de6c0dfSLen Brown else 12126168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 12130de6c0dfSLen Brown } 1214562a2d37SLen Brown 12151cc21f7bSLen Brown /* SMI */ 1216812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 12176168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 12181cc21f7bSLen Brown 1219678a3bd1SLen Brown /* Added counters */ 1220678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1221678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1222678a3bd1SLen Brown if (mp->width == 32) 12231b439f01SLen Brown outp += 12241b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]); 1225678a3bd1SLen Brown else 12266168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1227678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 12280de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12296168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 12300de6c0dfSLen Brown else 12316168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1232678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 123341618e63SLen Brown if (mp->type == COUNTER_USEC) 12341b439f01SLen Brown outp += 12351b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12361b439f01SLen Brown t->counter[i] / interval_float / 10000); 123741618e63SLen Brown else 12386168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc); 1239678a3bd1SLen Brown } 1240678a3bd1SLen Brown } 1241678a3bd1SLen Brown 124241618e63SLen Brown /* C1 */ 124341618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 12446168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc); 124541618e63SLen Brown 1246c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1247c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1248c98d5d94SLen Brown goto done; 1249c98d5d94SLen Brown 1250562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 12516168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc); 1252812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 12536168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc); 1254812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12556168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc); 1256c98d5d94SLen Brown 12570539ba11SLen Brown /* Mod%c6 */ 12580539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12596168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12600539ba11SLen Brown 1261812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12626168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1263889facbeSLen Brown 1264eae97e05SChen Yu /* Core throttle count */ 1265eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 1266eae97e05SChen Yu outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt); 1267eae97e05SChen Yu 1268388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1269388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1270388e9c81SLen Brown if (mp->width == 32) 12711b439f01SLen Brown outp += 12721b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]); 1273388e9c81SLen Brown else 12746168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1275388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12760de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12776168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12780de6c0dfSLen Brown else 12796168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1280388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12816168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc); 1282388e9c81SLen Brown } 1283388e9c81SLen Brown } 1284388e9c81SLen Brown 12859392bd98SCalvin Walton fmt8 = "%s%.2f"; 12869392bd98SCalvin Walton 12879392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12881b439f01SLen Brown outp += 12891b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12909392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12919392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12929392bd98SCalvin Walton 1293c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1294c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1295c98d5d94SLen Brown goto done; 1296c98d5d94SLen Brown 12970b2bb692SLen Brown /* PkgTmp */ 1298812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 12996168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1300889facbeSLen Brown 1301fdf676e5SLen Brown /* GFXrc6 */ 1302812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1303ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 13046168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 13059185e988SLen Brown } else { 13066168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 13079185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 13089185e988SLen Brown } 13099185e988SLen Brown } 1310fdf676e5SLen Brown 131127d47356SLen Brown /* GFXMHz */ 1312812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 13136168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 131427d47356SLen Brown 1315b4b91569SRafael Antognolli /* GFXACTMHz */ 1316b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1317b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1318b4b91569SRafael Antognolli 13190b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1320a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13216168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc); 1322a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc); 1324a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13256168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc); 1326a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 13276168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc); 13280b2bb692SLen Brown 13290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 13306168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc); 13310f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 13326168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc); 13330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 13346168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc); 13350f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 13366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc); 13370f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 13386168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc); 13390f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 13406168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc); 13410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 13426168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc); 1343889facbeSLen Brown 1344be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 13451b439f01SLen Brown outp += 13461b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1347be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 13481b439f01SLen Brown outp += 13491b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1350be0e54c4SLen Brown 1351812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 13521b439f01SLen Brown outp += 13531b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 1354eae97e05SChen Yu 13559392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13561b439f01SLen Brown outp += 13571b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1358812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 13591b439f01SLen Brown outp += 13601b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1361812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 13621b439f01SLen Brown outp += 13631b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13641b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float); 1365812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13666168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13679392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13686168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1369812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13706168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1371812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13726168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1373812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 13741b439f01SLen Brown outp += 13751b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13761b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1377812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 13781b439f01SLen Brown outp += 13791b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13801b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1381a5c6d65dSLen Brown /* UncMHz */ 1382a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ)) 1383a5c6d65dSLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->uncore_mhz); 1384812db3f7SLen Brown 1385388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1386388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1387388e9c81SLen Brown if (mp->width == 32) 13881b439f01SLen Brown outp += 13891b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]); 1390388e9c81SLen Brown else 13916168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1392388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 13930de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 13946168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 13950de6c0dfSLen Brown else 13966168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1397388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 13986168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc); 1399388e9c81SLen Brown } 1400388e9c81SLen Brown } 1401388e9c81SLen Brown 1402c98d5d94SLen Brown done: 140394d6ab4bSLen Brown if (*(outp - 1) != '\n') 1404c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1405c98d5d94SLen Brown 1406c98d5d94SLen Brown return 0; 1407103a8feaSLen Brown } 1408103a8feaSLen Brown 1409b7d8c148SLen Brown void flush_output_stdout(void) 1410103a8feaSLen Brown { 1411b7d8c148SLen Brown FILE *filep; 1412b7d8c148SLen Brown 1413b7d8c148SLen Brown if (outf == stderr) 1414b7d8c148SLen Brown filep = stdout; 1415b7d8c148SLen Brown else 1416b7d8c148SLen Brown filep = outf; 1417b7d8c148SLen Brown 1418b7d8c148SLen Brown fputs(output_buffer, filep); 1419b7d8c148SLen Brown fflush(filep); 1420b7d8c148SLen Brown 1421c98d5d94SLen Brown outp = output_buffer; 1422c98d5d94SLen Brown } 14231b439f01SLen Brown 1424b7d8c148SLen Brown void flush_output_stderr(void) 1425c98d5d94SLen Brown { 1426b7d8c148SLen Brown fputs(output_buffer, outf); 1427b7d8c148SLen Brown fflush(outf); 1428c98d5d94SLen Brown outp = output_buffer; 1429c98d5d94SLen Brown } 14301b439f01SLen Brown 1431c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1432c98d5d94SLen Brown { 1433c7e399f8SZephaniah E. Loss-Cutler-Hull static int count; 1434103a8feaSLen Brown 1435c7e399f8SZephaniah E. Loss-Cutler-Hull if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only) 1436c8ade361SLen Brown print_header("\t"); 1437103a8feaSLen Brown 14389d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1439103a8feaSLen Brown 1440c7e399f8SZephaniah E. Loss-Cutler-Hull count++; 1441e23da037SLen Brown 1442e23da037SLen Brown if (summary_only) 1443e23da037SLen Brown return; 1444e23da037SLen Brown 1445c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1446103a8feaSLen Brown } 1447103a8feaSLen Brown 1448889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 14497c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1450889facbeSLen Brown 14511b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old) 1452103a8feaSLen Brown { 1453388e9c81SLen Brown int i; 1454388e9c81SLen Brown struct msr_counter *mp; 14550b2bb692SLen Brown 1456a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14570b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1458a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14590b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1460a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14610b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1462a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14630b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1464a99d8730SLen Brown 1465c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 14660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1467c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 14680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1469c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 14700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1471c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1472ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1473ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1474ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1475be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1476be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1477889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1478889facbeSLen Brown 14799185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 14809185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 14819185e988SLen Brown old->gfx_rc6_ms = -1; 14829185e988SLen Brown else 1483fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 14849185e988SLen Brown 1485a5c6d65dSLen Brown old->uncore_mhz = new->uncore_mhz; 148627d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1487b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 148827d47356SLen Brown 148987e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 149087e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 149187e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 149287e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 149387e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 149487e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1495ba3dec99SLen Brown 1496388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1497388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1498388e9c81SLen Brown old->counter[i] = new->counter[i]; 1499388e9c81SLen Brown else 1500388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1501388e9c81SLen Brown } 1502388e9c81SLen Brown 1503ba3dec99SLen Brown return 0; 1504103a8feaSLen Brown } 1505103a8feaSLen Brown 15061b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old) 1507c98d5d94SLen Brown { 1508388e9c81SLen Brown int i; 1509388e9c81SLen Brown struct msr_counter *mp; 1510388e9c81SLen Brown 1511c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1512c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1513c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1514889facbeSLen Brown old->core_temp_c = new->core_temp_c; 1515eae97e05SChen Yu old->core_throt_cnt = new->core_throt_cnt; 15160539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1517388e9c81SLen Brown 15189392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 15199392bd98SCalvin Walton 1520388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1521388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1522388e9c81SLen Brown old->counter[i] = new->counter[i]; 1523388e9c81SLen Brown else 1524388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1525388e9c81SLen Brown } 1526103a8feaSLen Brown } 1527c98d5d94SLen Brown 15281e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 15291e9042b9SSrinivas Pandruvada { 15301e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 15311e9042b9SSrinivas Pandruvada return 0; 15321e9042b9SSrinivas Pandruvada 15331e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 15341e9042b9SSrinivas Pandruvada } 15351e9042b9SSrinivas Pandruvada 1536c3ae331dSLen Brown /* 1537c3ae331dSLen Brown * old = new - old 1538c3ae331dSLen Brown */ 15391b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 1540c98d5d94SLen Brown { 1541388e9c81SLen Brown int i; 1542388e9c81SLen Brown struct msr_counter *mp; 1543388e9c81SLen Brown 15444c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 15454c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 15464c2122d4SLen Brown new->apic_id = old->apic_id; 15474c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 15484c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 15494c2122d4SLen Brown 15503f44a5c6SLen Brown /* 15513f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 15523f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 15533f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 15543f44a5c6SLen Brown */ 15553f44a5c6SLen Brown 1556d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 15573f44a5c6SLen Brown old->tv_begin = new->tv_begin; 15583f44a5c6SLen Brown old->tv_end = new->tv_end; 15593f44a5c6SLen Brown 1560c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1561c98d5d94SLen Brown 1562103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1563b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1564b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1565b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1566b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1567103a8feaSLen Brown 1568c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1569c98d5d94SLen Brown 15701b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 1571c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1572c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1573c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1574c98d5d94SLen Brown } else { 1575ba3dec99SLen Brown return -1; 1576103a8feaSLen Brown } 1577a729617cSLen Brown } 1578103a8feaSLen Brown 1579144b44b1SLen Brown if (use_c1_residency_msr) { 1580144b44b1SLen Brown /* 1581144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1582144b44b1SLen Brown * which should be more accurate than the derivation below. 1583144b44b1SLen Brown */ 1584144b44b1SLen Brown } else { 1585103a8feaSLen Brown /* 1586c3ae331dSLen Brown * As counter collection is not atomic, 1587c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1588103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1589103a8feaSLen Brown */ 159095149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1591c98d5d94SLen Brown old->c1 = 0; 1592c98d5d94SLen Brown else { 1593c98d5d94SLen Brown /* normal case, derive c1 */ 1594008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1595c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1596c98d5d94SLen Brown } 1597144b44b1SLen Brown } 1598c3ae331dSLen Brown 1599c98d5d94SLen Brown if (old->mperf == 0) { 1600b7d8c148SLen Brown if (debug > 1) 1601b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1602c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1603c98d5d94SLen Brown } 1604103a8feaSLen Brown 16052af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 16062af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 16072af4f9b8SLen Brown 1608812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1609562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1610562a2d37SLen Brown 1611812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 16121ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1613ba3dec99SLen Brown 1614388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1615388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1616388e9c81SLen Brown old->counter[i] = new->counter[i]; 1617388e9c81SLen Brown else 1618388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1619388e9c81SLen Brown } 1620ba3dec99SLen Brown return 0; 1621103a8feaSLen Brown } 1622c98d5d94SLen Brown 1623c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 16241b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 1625c98d5d94SLen Brown { 1626ba3dec99SLen Brown int retval = 0; 1627ba3dec99SLen Brown 1628c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1629c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1630c98d5d94SLen Brown delta_core(c, c2); 1631c98d5d94SLen Brown 1632c98d5d94SLen Brown /* always calculate thread delta */ 1633ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1634ba3dec99SLen Brown if (retval) 1635ba3dec99SLen Brown return retval; 1636c98d5d94SLen Brown 1637c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1638c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1639ba3dec99SLen Brown retval = delta_package(p, p2); 1640c98d5d94SLen Brown 1641ba3dec99SLen Brown return retval; 1642103a8feaSLen Brown } 1643103a8feaSLen Brown 1644c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1645103a8feaSLen Brown { 1646388e9c81SLen Brown int i; 1647388e9c81SLen Brown struct msr_counter *mp; 1648388e9c81SLen Brown 16493f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 16503f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 16513f44a5c6SLen Brown t->tv_end.tv_sec = 0; 16523f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1653d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1654d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 16553f44a5c6SLen Brown 1656c98d5d94SLen Brown t->tsc = 0; 1657c98d5d94SLen Brown t->aperf = 0; 1658c98d5d94SLen Brown t->mperf = 0; 1659c98d5d94SLen Brown t->c1 = 0; 1660103a8feaSLen Brown 16612af4f9b8SLen Brown t->instr_count = 0; 16622af4f9b8SLen Brown 1663562a2d37SLen Brown t->irq_count = 0; 1664562a2d37SLen Brown t->smi_count = 0; 1665562a2d37SLen Brown 1666c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1667c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1668c98d5d94SLen Brown 1669c98d5d94SLen Brown c->c3 = 0; 1670c98d5d94SLen Brown c->c6 = 0; 1671c98d5d94SLen Brown c->c7 = 0; 16720539ba11SLen Brown c->mc6_us = 0; 1673889facbeSLen Brown c->core_temp_c = 0; 16749392bd98SCalvin Walton c->core_energy = 0; 1675eae97e05SChen Yu c->core_throt_cnt = 0; 1676c98d5d94SLen Brown 16770b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16780b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16790b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16800b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16810b2bb692SLen Brown 1682c98d5d94SLen Brown p->pc2 = 0; 16830f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1684c98d5d94SLen Brown p->pc3 = 0; 16850f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1686c98d5d94SLen Brown p->pc6 = 0; 16870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1688c98d5d94SLen Brown p->pc7 = 0; 1689ca58710fSKristen Carlson Accardi p->pc8 = 0; 1690ca58710fSKristen Carlson Accardi p->pc9 = 0; 1691ca58710fSKristen Carlson Accardi p->pc10 = 0; 1692be0e54c4SLen Brown p->cpu_lpi = 0; 1693be0e54c4SLen Brown p->sys_lpi = 0; 1694889facbeSLen Brown 1695889facbeSLen Brown p->energy_pkg = 0; 1696889facbeSLen Brown p->energy_dram = 0; 1697889facbeSLen Brown p->energy_cores = 0; 1698889facbeSLen Brown p->energy_gfx = 0; 1699889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1700889facbeSLen Brown p->rapl_dram_perf_status = 0; 1701889facbeSLen Brown p->pkg_temp_c = 0; 170227d47356SLen Brown 1703fdf676e5SLen Brown p->gfx_rc6_ms = 0; 1704a5c6d65dSLen Brown p->uncore_mhz = 0; 170527d47356SLen Brown p->gfx_mhz = 0; 1706b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1707388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1708388e9c81SLen Brown t->counter[i] = 0; 1709388e9c81SLen Brown 1710388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1711388e9c81SLen Brown c->counter[i] = 0; 1712388e9c81SLen Brown 1713388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1714388e9c81SLen Brown p->counter[i] = 0; 1715103a8feaSLen Brown } 17161b439f01SLen Brown 17171b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1718103a8feaSLen Brown { 1719388e9c81SLen Brown int i; 1720388e9c81SLen Brown struct msr_counter *mp; 1721388e9c81SLen Brown 17224c2122d4SLen Brown /* copy un-changing apic_id's */ 17234c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 17244c2122d4SLen Brown average.threads.apic_id = t->apic_id; 17254c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 17264c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 17274c2122d4SLen Brown 17283f44a5c6SLen Brown /* remember first tv_begin */ 17293f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 17303f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 17313f44a5c6SLen Brown 17323f44a5c6SLen Brown /* remember last tv_end */ 17333f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 17343f44a5c6SLen Brown 1735c98d5d94SLen Brown average.threads.tsc += t->tsc; 1736c98d5d94SLen Brown average.threads.aperf += t->aperf; 1737c98d5d94SLen Brown average.threads.mperf += t->mperf; 1738c98d5d94SLen Brown average.threads.c1 += t->c1; 173915aaa346SLen Brown 17402af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 17412af4f9b8SLen Brown 1742562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1743562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1744562a2d37SLen Brown 1745388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1746388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1747388e9c81SLen Brown continue; 1748388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1749388e9c81SLen Brown } 1750388e9c81SLen Brown 1751c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1752c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1753c98d5d94SLen Brown return 0; 1754c98d5d94SLen Brown 1755c98d5d94SLen Brown average.cores.c3 += c->c3; 1756c98d5d94SLen Brown average.cores.c6 += c->c6; 1757c98d5d94SLen Brown average.cores.c7 += c->c7; 17580539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1759c98d5d94SLen Brown 1760889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1761eae97e05SChen Yu average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt); 1762889facbeSLen Brown 17639392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 17649392bd98SCalvin Walton 1765388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1766388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1767388e9c81SLen Brown continue; 1768388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1769388e9c81SLen Brown } 1770388e9c81SLen Brown 1771c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1772c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1773c98d5d94SLen Brown return 0; 1774c98d5d94SLen Brown 1775a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17760b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1777a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17780b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1779a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17800b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1781a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17820b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17830b2bb692SLen Brown 1784c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17850f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1786c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1788c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17890f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1790c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1791ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1792ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1793ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1794c98d5d94SLen Brown 1795be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1796be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1797be0e54c4SLen Brown 1798889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1799889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1800889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1801889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1802889facbeSLen Brown 1803fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 1804a5c6d65dSLen Brown average.packages.uncore_mhz = p->uncore_mhz; 180527d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1806b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 180727d47356SLen Brown 1808889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1809889facbeSLen Brown 1810889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1811889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1812388e9c81SLen Brown 1813388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1814388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1815388e9c81SLen Brown continue; 1816388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1817388e9c81SLen Brown } 1818c98d5d94SLen Brown return 0; 1819c98d5d94SLen Brown } 18201b439f01SLen Brown 1821c98d5d94SLen Brown /* 1822c98d5d94SLen Brown * sum the counters for all cpus in the system 1823c98d5d94SLen Brown * compute the weighted average 1824c98d5d94SLen Brown */ 18251b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1826c98d5d94SLen Brown { 1827388e9c81SLen Brown int i; 1828388e9c81SLen Brown struct msr_counter *mp; 1829388e9c81SLen Brown 1830c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1831c98d5d94SLen Brown 1832c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1833c98d5d94SLen Brown 1834d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1835d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1836d4794f25SYazen Ghannam 1837c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1838c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1839c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 18402af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1841c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1842c98d5d94SLen Brown 18430de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 18440de6c0dfSLen Brown sums_need_wide_columns = 1; 18450de6c0dfSLen Brown 1846c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1847c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1848c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 18490539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1850c98d5d94SLen Brown 1851a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 18520b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1853a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 18540b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1855a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 18560b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1857a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 18580b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 18590b2bb692SLen Brown 1860c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 18610f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1862c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 18630f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1864c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 18650f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1866c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1867ca58710fSKristen Carlson Accardi 1868ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1869ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1870ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1871388e9c81SLen Brown 1872388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1873388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1874388e9c81SLen Brown continue; 18750de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18760de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18770de6c0dfSLen Brown sums_need_wide_columns = 1; 187841618e63SLen Brown continue; 18790de6c0dfSLen Brown } 1880388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1881388e9c81SLen Brown } 1882388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1883388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1884388e9c81SLen Brown continue; 18850de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18860de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18870de6c0dfSLen Brown sums_need_wide_columns = 1; 18880de6c0dfSLen Brown } 1889388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1890388e9c81SLen Brown } 1891388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1892388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1893388e9c81SLen Brown continue; 18940de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18950de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18960de6c0dfSLen Brown sums_need_wide_columns = 1; 18970de6c0dfSLen Brown } 1898388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1899388e9c81SLen Brown } 1900c98d5d94SLen Brown } 1901c98d5d94SLen Brown 1902c98d5d94SLen Brown static unsigned long long rdtsc(void) 1903c98d5d94SLen Brown { 1904c98d5d94SLen Brown unsigned int low, high; 1905c98d5d94SLen Brown 1906c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high)); 1907c98d5d94SLen Brown 1908c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1909c98d5d94SLen Brown } 1910c98d5d94SLen Brown 1911c98d5d94SLen Brown /* 1912495c7654SLen Brown * Open a file, and exit on failure 1913495c7654SLen Brown */ 1914495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1915495c7654SLen Brown { 1916495c7654SLen Brown FILE *filep = fopen(path, mode); 1917495c7654SLen Brown 1918495c7654SLen Brown if (!filep) 1919495c7654SLen Brown err(1, "%s: open failed", path); 1920495c7654SLen Brown return filep; 1921495c7654SLen Brown } 19221b439f01SLen Brown 1923495c7654SLen Brown /* 1924495c7654SLen Brown * snapshot_sysfs_counter() 1925495c7654SLen Brown * 1926495c7654SLen Brown * return snapshot of given counter 1927495c7654SLen Brown */ 1928495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1929495c7654SLen Brown { 1930495c7654SLen Brown FILE *fp; 1931495c7654SLen Brown int retval; 1932495c7654SLen Brown unsigned long long counter; 1933495c7654SLen Brown 1934495c7654SLen Brown fp = fopen_or_die(path, "r"); 1935495c7654SLen Brown 1936495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1937495c7654SLen Brown if (retval != 1) 1938495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1939495c7654SLen Brown 1940495c7654SLen Brown fclose(fp); 1941495c7654SLen Brown 1942495c7654SLen Brown return counter; 1943495c7654SLen Brown } 1944495c7654SLen Brown 1945495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1946495c7654SLen Brown { 1947495c7654SLen Brown if (mp->msr_num != 0) { 1948495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1949495c7654SLen Brown return -1; 1950495c7654SLen Brown } else { 195146c27978SLen Brown char path[128 + PATH_BYTES]; 195241618e63SLen Brown 195341618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 19541b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path); 195541618e63SLen Brown 195641618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 195741618e63SLen Brown } else { 1958495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1959495c7654SLen Brown } 196041618e63SLen Brown } 1961495c7654SLen Brown 1962495c7654SLen Brown return 0; 1963495c7654SLen Brown } 1964495c7654SLen Brown 1965a5c6d65dSLen Brown unsigned long long get_uncore_mhz(int package, int die) 1966a5c6d65dSLen Brown { 1967a5c6d65dSLen Brown char path[128]; 1968a5c6d65dSLen Brown 1969a5c6d65dSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/current_freq_khz", package, 1970a5c6d65dSLen Brown die); 1971a5c6d65dSLen Brown 1972a5c6d65dSLen Brown return (snapshot_sysfs_counter(path) / 1000); 1973a5c6d65dSLen Brown } 1974a5c6d65dSLen Brown 19756d6501d9SBorislav Petkov int get_epb(int cpu) 19766d6501d9SBorislav Petkov { 19776d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19787f1b11baSBorislav Petkov unsigned long long msr; 19796d6501d9SBorislav Petkov int ret, epb = -1; 19806d6501d9SBorislav Petkov FILE *fp; 19816d6501d9SBorislav Petkov 19826d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19836d6501d9SBorislav Petkov 19847f1b11baSBorislav Petkov fp = fopen(path, "r"); 19857f1b11baSBorislav Petkov if (!fp) 19867f1b11baSBorislav Petkov goto msr_fallback; 19876d6501d9SBorislav Petkov 19886d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19896d6501d9SBorislav Petkov if (ret != 1) 19906d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19916d6501d9SBorislav Petkov 19926d6501d9SBorislav Petkov fclose(fp); 19936d6501d9SBorislav Petkov 19946d6501d9SBorislav Petkov return epb; 19957f1b11baSBorislav Petkov 19967f1b11baSBorislav Petkov msr_fallback: 19977f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 19987f1b11baSBorislav Petkov 19997f1b11baSBorislav Petkov return msr & 0xf; 20006d6501d9SBorislav Petkov } 20016d6501d9SBorislav Petkov 20024c2122d4SLen Brown void get_apic_id(struct thread_data *t) 20034c2122d4SLen Brown { 200434041551SLen Brown unsigned int eax, ebx, ecx, edx; 200534041551SLen Brown 200634041551SLen Brown if (DO_BIC(BIC_APIC)) { 200734041551SLen Brown eax = ebx = ecx = edx = 0; 200834041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 200934041551SLen Brown 201034041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 201134041551SLen Brown } 201234041551SLen Brown 201334041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 201434041551SLen Brown return; 201534041551SLen Brown 2016c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 201734041551SLen Brown unsigned int topology_extensions; 201834041551SLen Brown 201934041551SLen Brown if (max_extended_level < 0x8000001e) 202034041551SLen Brown return; 20214c2122d4SLen Brown 20224c2122d4SLen Brown eax = ebx = ecx = edx = 0; 202334041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 202434041551SLen Brown topology_extensions = ecx & (1 << 22); 202534041551SLen Brown 202634041551SLen Brown if (topology_extensions == 0) 202734041551SLen Brown return; 202834041551SLen Brown 202934041551SLen Brown eax = ebx = ecx = edx = 0; 203034041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 203134041551SLen Brown 203234041551SLen Brown t->x2apic_id = eax; 203334041551SLen Brown return; 203434041551SLen Brown } 20354c2122d4SLen Brown 20364c2122d4SLen Brown if (!genuine_intel) 20374c2122d4SLen Brown return; 20384c2122d4SLen Brown 20394c2122d4SLen Brown if (max_level < 0xb) 20404c2122d4SLen Brown return; 20414c2122d4SLen Brown 20424c2122d4SLen Brown ecx = 0; 20434c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 20444c2122d4SLen Brown t->x2apic_id = edx; 20454c2122d4SLen Brown 204634041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 20471b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 20484c2122d4SLen Brown } 20494c2122d4SLen Brown 2050eae97e05SChen Yu int get_core_throt_cnt(int cpu, unsigned long long *cnt) 2051eae97e05SChen Yu { 2052eae97e05SChen Yu char path[128 + PATH_BYTES]; 2053eae97e05SChen Yu unsigned long long tmp; 2054eae97e05SChen Yu FILE *fp; 2055eae97e05SChen Yu int ret; 2056eae97e05SChen Yu 2057eae97e05SChen Yu sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu); 2058eae97e05SChen Yu fp = fopen(path, "r"); 2059eae97e05SChen Yu if (!fp) 2060eae97e05SChen Yu return -1; 2061eae97e05SChen Yu ret = fscanf(fp, "%lld", &tmp); 20625e5fd36cSColin Ian King fclose(fp); 2063eae97e05SChen Yu if (ret != 1) 2064eae97e05SChen Yu return -1; 2065eae97e05SChen Yu *cnt = tmp; 2066eae97e05SChen Yu 2067eae97e05SChen Yu return 0; 2068eae97e05SChen Yu } 2069eae97e05SChen Yu 2070495c7654SLen Brown /* 2071c98d5d94SLen Brown * get_counters(...) 2072c98d5d94SLen Brown * migrate to cpu 2073c98d5d94SLen Brown * acquire and record local counters for that cpu 2074c98d5d94SLen Brown */ 2075c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2076c98d5d94SLen Brown { 2077c98d5d94SLen Brown int cpu = t->cpu_id; 2078889facbeSLen Brown unsigned long long msr; 20790102b067SLen Brown int aperf_mperf_retry_count = 0; 2080388e9c81SLen Brown struct msr_counter *mp; 2081388e9c81SLen Brown int i; 2082c98d5d94SLen Brown 2083e52966c0SLen Brown if (cpu_migrate(cpu)) { 20843d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 208515aaa346SLen Brown return -1; 2086e52966c0SLen Brown } 208715aaa346SLen Brown 2088d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 2089d4794f25SYazen Ghannam 20904c2122d4SLen Brown if (first_counter_read) 20914c2122d4SLen Brown get_apic_id(t); 20920102b067SLen Brown retry: 2093c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 209415aaa346SLen Brown 20951b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 20960102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 20970102b067SLen Brown 20980102b067SLen Brown /* 20990102b067SLen Brown * The TSC, APERF and MPERF must be read together for 21000102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 21010102b067SLen Brown * 21020102b067SLen Brown * Unfortunately, APERF and MPERF are read by 21030102b067SLen Brown * individual system call, so delays may occur 21040102b067SLen Brown * between them. If the time to read them 21050102b067SLen Brown * varies by a large amount, we re-read them. 21060102b067SLen Brown */ 21070102b067SLen Brown 21080102b067SLen Brown /* 21090102b067SLen Brown * This initial dummy APERF read has been seen to 21100102b067SLen Brown * reduce jitter in the subsequent reads. 21110102b067SLen Brown */ 21120102b067SLen Brown 21139c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2114c98d5d94SLen Brown return -3; 21150102b067SLen Brown 21160102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 21170102b067SLen Brown 21180102b067SLen Brown tsc_before = t->tsc; 21190102b067SLen Brown 21200102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 21210102b067SLen Brown return -3; 21220102b067SLen Brown 21230102b067SLen Brown tsc_between = rdtsc(); 21240102b067SLen Brown 21259c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2126c98d5d94SLen Brown return -4; 21270102b067SLen Brown 21280102b067SLen Brown tsc_after = rdtsc(); 21290102b067SLen Brown 21300102b067SLen Brown aperf_time = tsc_between - tsc_before; 21310102b067SLen Brown mperf_time = tsc_after - tsc_between; 21320102b067SLen Brown 21330102b067SLen Brown /* 21340102b067SLen Brown * If the system call latency to read APERF and MPERF 21350102b067SLen Brown * differ by more than 2x, then try again. 21360102b067SLen Brown */ 21370102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 21380102b067SLen Brown aperf_mperf_retry_count++; 21390102b067SLen Brown if (aperf_mperf_retry_count < 5) 21400102b067SLen Brown goto retry; 21410102b067SLen Brown else 21421b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time); 21430102b067SLen Brown } 21440102b067SLen Brown aperf_mperf_retry_count = 0; 21450102b067SLen Brown 2146b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2147b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 214888c3281fSLen Brown } 214988c3281fSLen Brown 21502af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 21512af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 21522af4f9b8SLen Brown return -4; 21532af4f9b8SLen Brown 2154812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2155562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2156812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 21571ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 21581ed51011SLen Brown return -5; 21591ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 21601ed51011SLen Brown } 21610539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2162144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2163144b44b1SLen Brown return -6; 2164144b44b1SLen Brown } 2165144b44b1SLen Brown 2166388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2167495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2168388e9c81SLen Brown return -10; 2169388e9c81SLen Brown } 2170388e9c81SLen Brown 2171c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2172c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2173f4fdf2b4SLen Brown goto done; 2174c98d5d94SLen Brown 21751e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2176c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2177c98d5d94SLen Brown return -6; 2178144b44b1SLen Brown } 2179144b44b1SLen Brown 21801e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2181c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2182c98d5d94SLen Brown return -7; 21831e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2184fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2185fb5d4327SDasaratharaman Chandramouli return -7; 2186103a8feaSLen Brown } 218715aaa346SLen Brown 21887ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2189c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2190c98d5d94SLen Brown return -8; 21917ab5ff49SZhang Rui else if (t->is_atom) { 21927ab5ff49SZhang Rui /* 21937ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 21947ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 21957ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 21967ab5ff49SZhang Rui * accturate cc6 residency. 21977ab5ff49SZhang Rui */ 21987ab5ff49SZhang Rui c->c6 -= c->c7; 21997ab5ff49SZhang Rui } 22007ab5ff49SZhang Rui } 2201c98d5d94SLen Brown 22020539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 22030539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 22040539ba11SLen Brown return -8; 22050539ba11SLen Brown 2206812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2207889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2208889facbeSLen Brown return -9; 220955279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 2210889facbeSLen Brown } 2211889facbeSLen Brown 2212eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 2213eae97e05SChen Yu get_core_throt_cnt(cpu, &c->core_throt_cnt); 2214eae97e05SChen Yu 22159392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22169392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 22179392bd98SCalvin Walton return -14; 22189392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 22199392bd98SCalvin Walton } 22209392bd98SCalvin Walton 2221388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2222495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2223388e9c81SLen Brown return -10; 2224388e9c81SLen Brown } 2225889facbeSLen Brown 2226c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2227c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2228f4fdf2b4SLen Brown goto done; 222915aaa346SLen Brown 2230a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 22310b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 22320b2bb692SLen Brown return -10; 2233a99d8730SLen Brown } 2234a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 22350b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 22360b2bb692SLen Brown return -11; 2237a99d8730SLen Brown } 2238a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 22390b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 22400b2bb692SLen Brown return -12; 2241a99d8730SLen Brown } 2242a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 22430b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 22440b2bb692SLen Brown return -13; 22450b2bb692SLen Brown } 22460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2247c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2248c98d5d94SLen Brown return -9; 22490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 22500539ba11SLen Brown if (do_slm_cstates) { 22510539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 22520539ba11SLen Brown return -10; 22530539ba11SLen Brown } else { 2254c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2255c98d5d94SLen Brown return -10; 22560539ba11SLen Brown } 22570539ba11SLen Brown } 22580539ba11SLen Brown 22590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2260c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2261c98d5d94SLen Brown return -11; 22620f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2263c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2264c98d5d94SLen Brown return -12; 22650f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2266ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2267ca58710fSKristen Carlson Accardi return -13; 22680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2269ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2270ca58710fSKristen Carlson Accardi return -13; 22710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2272ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2273ca58710fSKristen Carlson Accardi return -13; 22740f47c08dSLen Brown 2275be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2276be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2277be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2278be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2279be0e54c4SLen Brown 2280889facbeSLen Brown if (do_rapl & RAPL_PKG) { 22819972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2282889facbeSLen Brown return -13; 22839972d5d8SChen Yu p->energy_pkg = msr; 2284889facbeSLen Brown } 22859148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 22869972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2287889facbeSLen Brown return -14; 22889972d5d8SChen Yu p->energy_cores = msr; 2289889facbeSLen Brown } 2290889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 22919972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2292889facbeSLen Brown return -15; 22939972d5d8SChen Yu p->energy_dram = msr; 2294889facbeSLen Brown } 2295889facbeSLen Brown if (do_rapl & RAPL_GFX) { 22969972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2297889facbeSLen Brown return -16; 22989972d5d8SChen Yu p->energy_gfx = msr; 2299889facbeSLen Brown } 2300889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 23019972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2302889facbeSLen Brown return -16; 23039972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2304889facbeSLen Brown } 2305889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 23069972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2307889facbeSLen Brown return -16; 23089972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2309889facbeSLen Brown } 23103316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 23119972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 23123316f99aSCalvin Walton return -13; 23139972d5d8SChen Yu p->energy_pkg = msr; 23143316f99aSCalvin Walton } 2315812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2316889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2317889facbeSLen Brown return -17; 231855279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 2319889facbeSLen Brown } 2320fdf676e5SLen Brown 2321812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2322fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2323fdf676e5SLen Brown 2324a5c6d65dSLen Brown /* n.b. assume die0 uncore frequency applies to whole package */ 2325a5c6d65dSLen Brown if (DO_BIC(BIC_UNCORE_MHZ)) 2326a5c6d65dSLen Brown p->uncore_mhz = get_uncore_mhz(p->package_id, 0); 2327a5c6d65dSLen Brown 2328812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 232927d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 233027d47356SLen Brown 2331b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2332b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2333b4b91569SRafael Antognolli 2334388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2335495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2336388e9c81SLen Brown return -10; 2337388e9c81SLen Brown } 2338f4fdf2b4SLen Brown done: 2339f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2340388e9c81SLen Brown 234115aaa346SLen Brown return 0; 2342103a8feaSLen Brown } 2343103a8feaSLen Brown 2344ee7e38e3SLen Brown /* 2345ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2346ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2347ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2348ee7e38e3SLen Brown */ 2349ee7e38e3SLen Brown 2350ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2351ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2352ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2353ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2354ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2355ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2356ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2357ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2358ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2359ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2360ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2361ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 23620b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 23630b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2364445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2365445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2366ee7e38e3SLen Brown 2367ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2368ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 23691b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 23701b439f01SLen Brown }; 2371ee7e38e3SLen Brown 23721b439f01SLen Brown int nhm_pkg_cstate_limits[16] = 23731b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 237438c6663aSLen Brown PCLRSV, PCLRSV 237538c6663aSLen Brown }; 237638c6663aSLen Brown 23771b439f01SLen Brown int snb_pkg_cstate_limits[16] = 23781b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 237938c6663aSLen Brown PCLRSV, PCLRSV 238038c6663aSLen Brown }; 238138c6663aSLen Brown 23821b439f01SLen Brown int hsw_pkg_cstate_limits[16] = 23831b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 238438c6663aSLen Brown PCLRSV, PCLRSV 238538c6663aSLen Brown }; 238638c6663aSLen Brown 23871b439f01SLen Brown int slv_pkg_cstate_limits[16] = 23881b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 238938c6663aSLen Brown PCL__6, PCL__7 239038c6663aSLen Brown }; 239138c6663aSLen Brown 23921b439f01SLen Brown int amt_pkg_cstate_limits[16] = 23931b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 239438c6663aSLen Brown PCLRSV, PCLRSV 239538c6663aSLen Brown }; 239638c6663aSLen Brown 23971b439f01SLen Brown int phi_pkg_cstate_limits[16] = 23981b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 239938c6663aSLen Brown PCLRSV, PCLRSV 240038c6663aSLen Brown }; 240138c6663aSLen Brown 24021b439f01SLen Brown int glm_pkg_cstate_limits[16] = 24031b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 240438c6663aSLen Brown PCLRSV, PCLRSV 240538c6663aSLen Brown }; 240638c6663aSLen Brown 24071b439f01SLen Brown int skx_pkg_cstate_limits[16] = 24081b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 240938c6663aSLen Brown PCLRSV, PCLRSV 241038c6663aSLen Brown }; 241138c6663aSLen Brown 24121b439f01SLen Brown int icx_pkg_cstate_limits[16] = 2413a1b6f487SArtem Bityutskiy { PCL__0, PCL__2, PCL__6, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 241438c6663aSLen Brown PCLRSV, PCLRSV 241538c6663aSLen Brown }; 2416a2b7b749SLen Brown 24171b439f01SLen Brown static void calculate_tsc_tweak() 2418a2b7b749SLen Brown { 2419a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2420a2b7b749SLen Brown } 2421a2b7b749SLen Brown 2422aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2423aeb01e6dSChen Yu 24241b439f01SLen Brown static void dump_nhm_platform_info(void) 2425103a8feaSLen Brown { 2426103a8feaSLen Brown unsigned long long msr; 2427103a8feaSLen Brown unsigned int ratio; 2428103a8feaSLen Brown 2429ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2430103a8feaSLen Brown 2431b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 24326574a5d5SLen Brown 2433103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 24341b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 2435103a8feaSLen Brown 2436103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 24371b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 2438103a8feaSLen Brown 24397ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2440b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2441bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 244267920418SLen Brown 2443aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2444aeb01e6dSChen Yu if (dis_cstate_prewake) 24451b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 2446aeb01e6dSChen Yu 2447fcd17211SLen Brown return; 2448fcd17211SLen Brown } 2449fcd17211SLen Brown 24501b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void) 2451fcd17211SLen Brown { 2452fcd17211SLen Brown unsigned long long msr; 2453fcd17211SLen Brown unsigned int ratio; 2454fcd17211SLen Brown 24557ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2456fcd17211SLen Brown 2457b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2458fcd17211SLen Brown 2459fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2460fcd17211SLen Brown if (ratio) 24611b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 2462fcd17211SLen Brown 2463fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2464fcd17211SLen Brown if (ratio) 24651b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 2466fcd17211SLen Brown return; 2467fcd17211SLen Brown } 2468fcd17211SLen Brown 24691b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void) 2470fcd17211SLen Brown { 2471fcd17211SLen Brown unsigned long long msr; 2472fcd17211SLen Brown unsigned int ratio; 24736574a5d5SLen Brown 24747ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 24756574a5d5SLen Brown 2476b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 24776574a5d5SLen Brown 24786574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 24796574a5d5SLen Brown if (ratio) 24801b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 24816574a5d5SLen Brown 24826574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 24836574a5d5SLen Brown if (ratio) 24841b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 24856574a5d5SLen Brown 24866574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 24876574a5d5SLen Brown if (ratio) 24881b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 24896574a5d5SLen Brown 24906574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 24916574a5d5SLen Brown if (ratio) 24921b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 24936574a5d5SLen Brown 24946574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 24956574a5d5SLen Brown if (ratio) 24961b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 24976574a5d5SLen Brown 24986574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 24996574a5d5SLen Brown if (ratio) 25001b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 25016574a5d5SLen Brown 25026574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 25036574a5d5SLen Brown if (ratio) 25041b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 25056574a5d5SLen Brown 25066574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 25076574a5d5SLen Brown if (ratio) 25081b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 2509103a8feaSLen Brown return; 2510fcd17211SLen Brown } 25111b439f01SLen Brown 251231e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 251331e07522SLen Brown { 251431e07522SLen Brown 251531e07522SLen Brown if (!genuine_intel) 251631e07522SLen Brown return 0; 251731e07522SLen Brown 25189878bf7aSLen Brown if (family != 6) 25199878bf7aSLen Brown return 0; 25209878bf7aSLen Brown 252131e07522SLen Brown switch (model) { 252231e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 252331e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 252425368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 2525684e40e9SArtem Bityutskiy case INTEL_FAM6_SAPPHIRERAPIDS_X: 25265ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 252720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 252831e07522SLen Brown return 1; 25299878bf7aSLen Brown default: 253031e07522SLen Brown return 0; 253131e07522SLen Brown } 25329878bf7aSLen Brown } 2533fcd17211SLen Brown 25344af184eeSLen Brown static void dump_turbo_ratio_limits(int trl_msr_offset, int family, int model) 2535fcd17211SLen Brown { 253631e07522SLen Brown unsigned long long msr, core_counts; 25375d622845SLen Brown int shift; 2538103a8feaSLen Brown 25394af184eeSLen Brown get_msr(base_cpu, trl_msr_offset, &msr); 25404af184eeSLen Brown fprintf(outf, "cpu%d: MSR_%sTURBO_RATIO_LIMIT: 0x%08llx\n", 2541*884a1f95SLen Brown base_cpu, trl_msr_offset == MSR_SECONDARY_TURBO_RATIO_LIMIT ? "SECONDARY_" : "", msr); 25426574a5d5SLen Brown 254331e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 254431e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 254531e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 254631e07522SLen Brown } else { 254731e07522SLen Brown core_counts = 0x0807060504030201; 254831e07522SLen Brown } 254931e07522SLen Brown 25505d622845SLen Brown for (shift = 56; shift >= 0; shift -= 8) { 25515d622845SLen Brown unsigned int ratio, group_size; 25526574a5d5SLen Brown 25535d622845SLen Brown ratio = (msr >> shift) & 0xFF; 25545d622845SLen Brown group_size = (core_counts >> shift) & 0xFF; 25556574a5d5SLen Brown if (ratio) 255631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 255731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25585d622845SLen Brown } 25596574a5d5SLen Brown 2560fcd17211SLen Brown return; 2561fcd17211SLen Brown } 25623a9a941dSLen Brown 25631b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void) 25640f7887c4SLen Brown { 25650f7887c4SLen Brown unsigned long long msr; 25660f7887c4SLen Brown unsigned int ratio; 25670f7887c4SLen Brown 25680f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 25690f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25700f7887c4SLen Brown 25710f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25720f7887c4SLen Brown if (ratio) 25731b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 25740f7887c4SLen Brown 25750f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25760f7887c4SLen Brown if (ratio) 25771b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 25780f7887c4SLen Brown 25790f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25800f7887c4SLen Brown if (ratio) 25811b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 25820f7887c4SLen Brown 25830f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 25840f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25850f7887c4SLen Brown 25860f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 25870f7887c4SLen Brown if (ratio) 25881b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 25890f7887c4SLen Brown 25900f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25910f7887c4SLen Brown if (ratio) 25921b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 25930f7887c4SLen Brown 25940f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25950f7887c4SLen Brown if (ratio) 25961b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 25970f7887c4SLen Brown 25980f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25990f7887c4SLen Brown if (ratio) 26001b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 26010f7887c4SLen Brown } 26020f7887c4SLen Brown 26031b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void) 2604fb5d4327SDasaratharaman Chandramouli { 2605cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2606cbf97abaSHubert Chrzaniuk 2607fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2608cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2609cbf97abaSHubert Chrzaniuk int i, b_nr; 2610cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2611cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2612fb5d4327SDasaratharaman Chandramouli 2613ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2614fb5d4327SDasaratharaman Chandramouli 26151b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 2616fb5d4327SDasaratharaman Chandramouli 26171e3ec5cdSRandy Dunlap /* 2618fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2619cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2620cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2621fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2622fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2623fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2624fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2625fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2626fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2627fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2628fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2629fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2630fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2631fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2632fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2633fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2634fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2635fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2636fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2637fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2638fb5d4327SDasaratharaman Chandramouli */ 2639fb5d4327SDasaratharaman Chandramouli 2640cbf97abaSHubert Chrzaniuk b_nr = 0; 2641cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2642cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2643cbf97abaSHubert Chrzaniuk 2644cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2645fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2646cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2647fb5d4327SDasaratharaman Chandramouli 2648cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2649cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2650cbf97abaSHubert Chrzaniuk b_nr++; 2651fb5d4327SDasaratharaman Chandramouli } 2652cbf97abaSHubert Chrzaniuk 2653cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2654cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2655b7d8c148SLen Brown fprintf(outf, 2656710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2657cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2658fb5d4327SDasaratharaman Chandramouli } 2659fb5d4327SDasaratharaman Chandramouli 26601b439f01SLen Brown static void dump_nhm_cst_cfg(void) 2661fcd17211SLen Brown { 2662fcd17211SLen Brown unsigned long long msr; 2663fcd17211SLen Brown 26641df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2665fcd17211SLen Brown 26661df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2667fcd17211SLen Brown 26683e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2669fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2670fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2671fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2672fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 26731b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 2674ac980e13SArtem Bityutskiy 2675ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2676ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 26771b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2678ac980e13SArtem Bityutskiy } 2679ac980e13SArtem Bityutskiy 2680ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2681ac980e13SArtem Bityutskiy 2682fcd17211SLen Brown return; 2683103a8feaSLen Brown } 2684103a8feaSLen Brown 26851b439f01SLen Brown static void dump_config_tdp(void) 26866fb3143bSLen Brown { 26876fb3143bSLen Brown unsigned long long msr; 26886fb3143bSLen Brown 26896fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2690b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2691685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26926fb3143bSLen Brown 26936fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2694b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 26956fb3143bSLen Brown if (msr) { 2696685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2697685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2698685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2699685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 27006fb3143bSLen Brown } 2701b7d8c148SLen Brown fprintf(outf, ")\n"); 27026fb3143bSLen Brown 27036fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2704b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 27056fb3143bSLen Brown if (msr) { 2706685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2707685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2708685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2709685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 27106fb3143bSLen Brown } 2711b7d8c148SLen Brown fprintf(outf, ")\n"); 27126fb3143bSLen Brown 27136fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2714b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 27156fb3143bSLen Brown if ((msr) & 0x3) 2716b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2717b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2718b7d8c148SLen Brown fprintf(outf, ")\n"); 27196fb3143bSLen Brown 27206fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2721b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2722685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2723b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2724b7d8c148SLen Brown fprintf(outf, ")\n"); 27256fb3143bSLen Brown } 27265a63426eSLen Brown 27275a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 27285a63426eSLen Brown 27295a63426eSLen Brown void print_irtl(void) 27305a63426eSLen Brown { 27315a63426eSLen Brown unsigned long long msr; 27325a63426eSLen Brown 27335a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 27345a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 27355a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27365a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27375a63426eSLen Brown 27385a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 27395a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 27405a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27415a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27425a63426eSLen Brown 27435a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 27445a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 27455a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27465a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27475a63426eSLen Brown 27485a63426eSLen Brown if (!do_irtl_hsw) 27495a63426eSLen Brown return; 27505a63426eSLen Brown 27515a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 27525a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 27535a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27545a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27555a63426eSLen Brown 27565a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 27575a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 27585a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27595a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27605a63426eSLen Brown 27615a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 27625a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 27635a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27645a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27655a63426eSLen Brown 27665a63426eSLen Brown } 27671b439f01SLen Brown 276836229897SLen Brown void free_fd_percpu(void) 276936229897SLen Brown { 277036229897SLen Brown int i; 277136229897SLen Brown 277201a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 277336229897SLen Brown if (fd_percpu[i] != 0) 277436229897SLen Brown close(fd_percpu[i]); 277536229897SLen Brown } 277636229897SLen Brown 277736229897SLen Brown free(fd_percpu); 27786fb3143bSLen Brown } 27796fb3143bSLen Brown 2780c98d5d94SLen Brown void free_all_buffers(void) 2781103a8feaSLen Brown { 27820e2d8f05SLen Brown int i; 27830e2d8f05SLen Brown 2784c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2785c98d5d94SLen Brown cpu_present_set = NULL; 278636229897SLen Brown cpu_present_setsize = 0; 2787103a8feaSLen Brown 2788c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2789c98d5d94SLen Brown cpu_affinity_set = NULL; 2790c98d5d94SLen Brown cpu_affinity_setsize = 0; 2791103a8feaSLen Brown 2792c98d5d94SLen Brown free(thread_even); 2793c98d5d94SLen Brown free(core_even); 2794c98d5d94SLen Brown free(package_even); 2795103a8feaSLen Brown 2796c98d5d94SLen Brown thread_even = NULL; 2797c98d5d94SLen Brown core_even = NULL; 2798c98d5d94SLen Brown package_even = NULL; 2799103a8feaSLen Brown 2800c98d5d94SLen Brown free(thread_odd); 2801c98d5d94SLen Brown free(core_odd); 2802c98d5d94SLen Brown free(package_odd); 2803103a8feaSLen Brown 2804c98d5d94SLen Brown thread_odd = NULL; 2805c98d5d94SLen Brown core_odd = NULL; 2806c98d5d94SLen Brown package_odd = NULL; 2807103a8feaSLen Brown 2808c98d5d94SLen Brown free(output_buffer); 2809c98d5d94SLen Brown output_buffer = NULL; 2810c98d5d94SLen Brown outp = NULL; 281136229897SLen Brown 281236229897SLen Brown free_fd_percpu(); 2813562a2d37SLen Brown 2814562a2d37SLen Brown free(irq_column_2_cpu); 2815562a2d37SLen Brown free(irqs_per_cpu); 28160e2d8f05SLen Brown 28170e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 28180e2d8f05SLen Brown if (cpus[i].put_ids) 28190e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 28200e2d8f05SLen Brown } 28210e2d8f05SLen Brown free(cpus); 2822103a8feaSLen Brown } 2823103a8feaSLen Brown 282457a42a34SJosh Triplett /* 282595aebc44SJosh Triplett * Parse a file containing a single int. 28266de68fe1SLen Brown * Return 0 if file can not be opened 28276de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 282895aebc44SJosh Triplett */ 282995aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 283095aebc44SJosh Triplett { 283195aebc44SJosh Triplett va_list args; 283295aebc44SJosh Triplett char path[PATH_MAX]; 283395aebc44SJosh Triplett FILE *filep; 283495aebc44SJosh Triplett int value; 283595aebc44SJosh Triplett 283695aebc44SJosh Triplett va_start(args, fmt); 283795aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 283895aebc44SJosh Triplett va_end(args); 28396de68fe1SLen Brown filep = fopen(path, "r"); 28406de68fe1SLen Brown if (!filep) 28416de68fe1SLen Brown return 0; 2842b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2843b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 284495aebc44SJosh Triplett fclose(filep); 284595aebc44SJosh Triplett return value; 284695aebc44SJosh Triplett } 284795aebc44SJosh Triplett 284895aebc44SJosh Triplett /* 2849c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2850c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2851c98d5d94SLen Brown */ 2852c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2853c98d5d94SLen Brown { 285495aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2855103a8feaSLen Brown } 2856103a8feaSLen Brown 2857103a8feaSLen Brown int get_physical_package_id(int cpu) 2858103a8feaSLen Brown { 285995aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2860103a8feaSLen Brown } 2861103a8feaSLen Brown 28626de68fe1SLen Brown int get_die_id(int cpu) 28636de68fe1SLen Brown { 28646de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 28656de68fe1SLen Brown } 28666de68fe1SLen Brown 2867103a8feaSLen Brown int get_core_id(int cpu) 2868103a8feaSLen Brown { 286995aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2870103a8feaSLen Brown } 2871103a8feaSLen Brown 2872ef605741SPrarit Bhargava void set_node_data(void) 2873ef605741SPrarit Bhargava { 28742ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 28752ffbb224SPrarit Bhargava int cpu_count; 2876ef605741SPrarit Bhargava 28772ffbb224SPrarit Bhargava /* initialize logical_node_id */ 28782ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 28792ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2880ef605741SPrarit Bhargava 28812ffbb224SPrarit Bhargava cpu_count = 0; 28822ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 28832ffbb224SPrarit Bhargava lnode = 0; 28842ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 28852ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2886ef605741SPrarit Bhargava continue; 28872ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 28882ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 28892ffbb224SPrarit Bhargava continue; 28902ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2891ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28922ffbb224SPrarit Bhargava cpu_count++; 28932ffbb224SPrarit Bhargava /* 28942ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 28952ffbb224SPrarit Bhargava * the logical_node_id 28962ffbb224SPrarit Bhargava */ 28972ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 28981b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 28992ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 29002ffbb224SPrarit Bhargava cpu_count++; 2901ef605741SPrarit Bhargava } 29022ffbb224SPrarit Bhargava } 29032ffbb224SPrarit Bhargava lnode++; 29042ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 29052ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 29062ffbb224SPrarit Bhargava } 29072ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 29082ffbb224SPrarit Bhargava break; 29092ffbb224SPrarit Bhargava } 2910ef605741SPrarit Bhargava } 2911ef605741SPrarit Bhargava 2912ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2913c98d5d94SLen Brown { 2914c98d5d94SLen Brown char path[80]; 2915c98d5d94SLen Brown FILE *filep; 29160e2d8f05SLen Brown int i; 29170e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2918c98d5d94SLen Brown 29190e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 29201b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 29210e2d8f05SLen Brown filep = fopen(path, "r"); 29220e2d8f05SLen Brown if (!filep) 29230e2d8f05SLen Brown continue; 29240e2d8f05SLen Brown fclose(filep); 29250e2d8f05SLen Brown return i; 29260e2d8f05SLen Brown } 29270e2d8f05SLen Brown return -1; 2928e275b388SDasaratharaman Chandramouli } 2929c98d5d94SLen Brown 29300e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 29310e2d8f05SLen Brown { 29320e2d8f05SLen Brown char path[80], character; 29330e2d8f05SLen Brown FILE *filep; 29340e2d8f05SLen Brown unsigned long map; 29358cb48b32SPrarit Bhargava int so, shift, sib_core; 29360e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 29370e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 29380e2d8f05SLen Brown size_t size; 29398cb48b32SPrarit Bhargava int thread_id = 0; 29400e2d8f05SLen Brown 29410e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 29428cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 29438cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 29440e2d8f05SLen Brown if (!thiscpu->put_ids) 29450e2d8f05SLen Brown return -1; 29460e2d8f05SLen Brown 29470e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 29480e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 29490e2d8f05SLen Brown 29501b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 29513d7772eaSLen Brown filep = fopen(path, "r"); 29523d7772eaSLen Brown 29533d7772eaSLen Brown if (!filep) { 29543d7772eaSLen Brown warnx("%s: open failed", path); 29553d7772eaSLen Brown return -1; 29563d7772eaSLen Brown } 29570e2d8f05SLen Brown do { 29580e2d8f05SLen Brown offset -= BITMASK_SIZE; 29598173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 29608173c336SBen Hutchings err(1, "%s: failed to parse file", path); 29610e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 29620e2d8f05SLen Brown if ((map >> shift) & 0x1) { 29638cb48b32SPrarit Bhargava so = shift + offset; 29648cb48b32SPrarit Bhargava sib_core = get_core_id(so); 29658cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 29668cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 29671b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0)) 29681b439f01SLen Brown cpus[so].thread_id = thread_id++; 29698cb48b32SPrarit Bhargava } 29700e2d8f05SLen Brown } 29710e2d8f05SLen Brown } 2972e13da9a1SColin Ian King } while (character == ','); 2973c98d5d94SLen Brown fclose(filep); 29740e2d8f05SLen Brown 29750e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2976c98d5d94SLen Brown } 2977c98d5d94SLen Brown 2978c98d5d94SLen Brown /* 2979c98d5d94SLen Brown * run func(thread, core, package) in topology order 2980c98d5d94SLen Brown * skip non-present cpus 2981103a8feaSLen Brown */ 2982103a8feaSLen Brown 2983c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 2984c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2985c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2986c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 29871b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 2988c98d5d94SLen Brown { 298940f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2990c98d5d94SLen Brown 2991c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 299240f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 29931b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 29941b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 2995c98d5d94SLen Brown struct thread_data *t, *t2; 2996c98d5d94SLen Brown struct core_data *c, *c2; 2997c98d5d94SLen Brown struct pkg_data *p, *p2; 2998c98d5d94SLen Brown 29991b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 3000c98d5d94SLen Brown 3001c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 3002c98d5d94SLen Brown continue; 3003c98d5d94SLen Brown 30041b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no); 3005c98d5d94SLen Brown 30061b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 30071b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no); 3008c98d5d94SLen Brown 3009c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 3010c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 3011c98d5d94SLen Brown 3012c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 3013c98d5d94SLen Brown if (retval) 3014c98d5d94SLen Brown return retval; 3015c98d5d94SLen Brown } 3016c98d5d94SLen Brown } 3017c98d5d94SLen Brown } 301840f5cfe7SPrarit Bhargava } 3019c98d5d94SLen Brown return 0; 3020c98d5d94SLen Brown } 3021c98d5d94SLen Brown 3022c98d5d94SLen Brown /* 3023c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 3024c98d5d94SLen Brown * return max_cpu number 3025c98d5d94SLen Brown */ 3026c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int)) 3027103a8feaSLen Brown { 3028103a8feaSLen Brown FILE *fp; 3029c98d5d94SLen Brown int cpu_num; 3030103a8feaSLen Brown int retval; 3031103a8feaSLen Brown 303257a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 3033103a8feaSLen Brown 3034103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 3035b2c95d90SJosh Triplett if (retval != 0) 3036b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 3037103a8feaSLen Brown 3038c98d5d94SLen Brown while (1) { 3039c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 3040103a8feaSLen Brown if (retval != 1) 3041103a8feaSLen Brown break; 3042103a8feaSLen Brown 3043c98d5d94SLen Brown retval = func(cpu_num); 3044c98d5d94SLen Brown if (retval) { 3045c98d5d94SLen Brown fclose(fp); 3046c98d5d94SLen Brown return (retval); 3047c98d5d94SLen Brown } 3048103a8feaSLen Brown } 3049103a8feaSLen Brown fclose(fp); 3050c98d5d94SLen Brown return 0; 3051103a8feaSLen Brown } 3052103a8feaSLen Brown 3053103a8feaSLen Brown void re_initialize(void) 3054103a8feaSLen Brown { 3055c98d5d94SLen Brown free_all_buffers(); 3056c98d5d94SLen Brown setup_all_buffers(); 30573d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 3058103a8feaSLen Brown } 3059103a8feaSLen Brown 3060843c5791SPrarit Bhargava void set_max_cpu_num(void) 3061843c5791SPrarit Bhargava { 3062843c5791SPrarit Bhargava FILE *filep; 30638201a028SPrarit Bhargava int base_cpu; 3064843c5791SPrarit Bhargava unsigned long dummy; 30658201a028SPrarit Bhargava char pathname[64]; 3066843c5791SPrarit Bhargava 30678201a028SPrarit Bhargava base_cpu = sched_getcpu(); 30688201a028SPrarit Bhargava if (base_cpu < 0) 30698201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 30701b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu); 30718201a028SPrarit Bhargava 30728201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 3073843c5791SPrarit Bhargava topo.max_cpu_num = 0; 3074843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 30750e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 3076843c5791SPrarit Bhargava fclose(filep); 3077843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 3078843c5791SPrarit Bhargava } 3079c98d5d94SLen Brown 3080103a8feaSLen Brown /* 3081c98d5d94SLen Brown * count_cpus() 3082c98d5d94SLen Brown * remember the last one seen, it will be the max 3083103a8feaSLen Brown */ 3084c98d5d94SLen Brown int count_cpus(int cpu) 3085103a8feaSLen Brown { 30869878bf7aSLen Brown UNUSED(cpu); 30879878bf7aSLen Brown 3088843c5791SPrarit Bhargava topo.num_cpus++; 3089c98d5d94SLen Brown return 0; 3090103a8feaSLen Brown } 30911b439f01SLen Brown 3092c98d5d94SLen Brown int mark_cpu_present(int cpu) 3093c98d5d94SLen Brown { 3094c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 309515aaa346SLen Brown return 0; 3096103a8feaSLen Brown } 3097103a8feaSLen Brown 30988cb48b32SPrarit Bhargava int init_thread_id(int cpu) 30998cb48b32SPrarit Bhargava { 31008cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 31018cb48b32SPrarit Bhargava return 0; 31028cb48b32SPrarit Bhargava } 31038cb48b32SPrarit Bhargava 3104562a2d37SLen Brown /* 3105562a2d37SLen Brown * snapshot_proc_interrupts() 3106562a2d37SLen Brown * 3107562a2d37SLen Brown * read and record summary of /proc/interrupts 3108562a2d37SLen Brown * 3109562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3110562a2d37SLen Brown */ 3111562a2d37SLen Brown int snapshot_proc_interrupts(void) 3112562a2d37SLen Brown { 3113562a2d37SLen Brown static FILE *fp; 3114562a2d37SLen Brown int column, retval; 3115562a2d37SLen Brown 3116562a2d37SLen Brown if (fp == NULL) 3117562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3118562a2d37SLen Brown else 3119562a2d37SLen Brown rewind(fp); 3120562a2d37SLen Brown 3121562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3122562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3123562a2d37SLen Brown int cpu_number; 3124562a2d37SLen Brown 3125562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3126562a2d37SLen Brown if (retval != 1) 3127562a2d37SLen Brown break; 3128562a2d37SLen Brown 3129562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3130562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3131562a2d37SLen Brown return 1; 3132562a2d37SLen Brown } 3133562a2d37SLen Brown 3134562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3135562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3136562a2d37SLen Brown } 3137562a2d37SLen Brown 3138562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3139562a2d37SLen Brown while (1) { 3140562a2d37SLen Brown int column; 3141562a2d37SLen Brown char buf[64]; 3142562a2d37SLen Brown 3143562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3144562a2d37SLen Brown if (retval != 1) 3145562a2d37SLen Brown break; 3146562a2d37SLen Brown 3147562a2d37SLen Brown /* read the count per cpu */ 3148562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3149562a2d37SLen Brown 3150562a2d37SLen Brown int cpu_number, irq_count; 3151562a2d37SLen Brown 3152562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3153562a2d37SLen Brown if (retval != 1) 3154562a2d37SLen Brown break; 3155562a2d37SLen Brown 3156562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3157562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3158562a2d37SLen Brown 3159562a2d37SLen Brown } 3160562a2d37SLen Brown 31611b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */ 3162562a2d37SLen Brown 3163562a2d37SLen Brown } 3164562a2d37SLen Brown return 0; 3165562a2d37SLen Brown } 31661b439f01SLen Brown 316727d47356SLen Brown /* 3168fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3169fdf676e5SLen Brown * 3170fdf676e5SLen Brown * record snapshot of 3171fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3172fdf676e5SLen Brown * 3173fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3174fdf676e5SLen Brown */ 3175fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3176fdf676e5SLen Brown { 3177fdf676e5SLen Brown FILE *fp; 3178fdf676e5SLen Brown int retval; 3179fdf676e5SLen Brown 3180fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3181fdf676e5SLen Brown 3182fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3183fdf676e5SLen Brown if (retval != 1) 3184fdf676e5SLen Brown err(1, "GFX rc6"); 3185fdf676e5SLen Brown 3186fdf676e5SLen Brown fclose(fp); 3187fdf676e5SLen Brown 3188fdf676e5SLen Brown return 0; 3189fdf676e5SLen Brown } 31901b439f01SLen Brown 3191fdf676e5SLen Brown /* 319227d47356SLen Brown * snapshot_gfx_mhz() 319327d47356SLen Brown * 319427d47356SLen Brown * record snapshot of 319527d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 319627d47356SLen Brown * 319727d47356SLen Brown * return 1 if config change requires a restart, else return 0 319827d47356SLen Brown */ 319927d47356SLen Brown int snapshot_gfx_mhz(void) 320027d47356SLen Brown { 320127d47356SLen Brown static FILE *fp; 320227d47356SLen Brown int retval; 320327d47356SLen Brown 320427d47356SLen Brown if (fp == NULL) 320527d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 320622048c54SLen Brown else { 320727d47356SLen Brown rewind(fp); 320822048c54SLen Brown fflush(fp); 320922048c54SLen Brown } 321027d47356SLen Brown 321127d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 321227d47356SLen Brown if (retval != 1) 321327d47356SLen Brown err(1, "GFX MHz"); 321427d47356SLen Brown 321527d47356SLen Brown return 0; 321627d47356SLen Brown } 3217562a2d37SLen Brown 3218562a2d37SLen Brown /* 3219b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3220b4b91569SRafael Antognolli * 3221b4b91569SRafael Antognolli * record snapshot of 3222b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3223b4b91569SRafael Antognolli * 3224b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3225b4b91569SRafael Antognolli */ 3226b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3227b4b91569SRafael Antognolli { 3228b4b91569SRafael Antognolli static FILE *fp; 3229b4b91569SRafael Antognolli int retval; 3230b4b91569SRafael Antognolli 3231b4b91569SRafael Antognolli if (fp == NULL) 3232b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3233b4b91569SRafael Antognolli else { 3234b4b91569SRafael Antognolli rewind(fp); 3235b4b91569SRafael Antognolli fflush(fp); 3236b4b91569SRafael Antognolli } 3237b4b91569SRafael Antognolli 3238b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3239b4b91569SRafael Antognolli if (retval != 1) 3240b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3241b4b91569SRafael Antognolli 3242b4b91569SRafael Antognolli return 0; 3243b4b91569SRafael Antognolli } 3244b4b91569SRafael Antognolli 3245b4b91569SRafael Antognolli /* 3246be0e54c4SLen Brown * snapshot_cpu_lpi() 3247be0e54c4SLen Brown * 3248be0e54c4SLen Brown * record snapshot of 3249be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3250be0e54c4SLen Brown */ 3251be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3252be0e54c4SLen Brown { 3253be0e54c4SLen Brown FILE *fp; 3254be0e54c4SLen Brown int retval; 3255be0e54c4SLen Brown 3256be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3257be0e54c4SLen Brown 3258be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 32595ea7647bSPrarit Bhargava if (retval != 1) { 32605ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 32615ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3262605736c6SGustavo A. R. Silva fclose(fp); 32635ea7647bSPrarit Bhargava return -1; 32645ea7647bSPrarit Bhargava } 3265be0e54c4SLen Brown 3266be0e54c4SLen Brown fclose(fp); 3267be0e54c4SLen Brown 3268be0e54c4SLen Brown return 0; 3269be0e54c4SLen Brown } 32701b439f01SLen Brown 3271be0e54c4SLen Brown /* 3272be0e54c4SLen Brown * snapshot_sys_lpi() 3273be0e54c4SLen Brown * 32741f81c5efSLen Brown * record snapshot of sys_lpi_file 3275be0e54c4SLen Brown */ 3276be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3277be0e54c4SLen Brown { 3278be0e54c4SLen Brown FILE *fp; 3279be0e54c4SLen Brown int retval; 3280be0e54c4SLen Brown 32811f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3282be0e54c4SLen Brown 3283be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 32845ea7647bSPrarit Bhargava if (retval != 1) { 32855ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 32865ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 328715423b95SColin Ian King fclose(fp); 32885ea7647bSPrarit Bhargava return -1; 32895ea7647bSPrarit Bhargava } 3290be0e54c4SLen Brown fclose(fp); 3291be0e54c4SLen Brown 3292be0e54c4SLen Brown return 0; 3293be0e54c4SLen Brown } 32941b439f01SLen Brown 3295be0e54c4SLen Brown /* 3296562a2d37SLen Brown * snapshot /proc and /sys files 3297562a2d37SLen Brown * 3298562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3299562a2d37SLen Brown */ 3300562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3301562a2d37SLen Brown { 3302218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3303562a2d37SLen Brown if (snapshot_proc_interrupts()) 3304562a2d37SLen Brown return 1; 3305562a2d37SLen Brown 3306812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3307fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3308fdf676e5SLen Brown 3309812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 331027d47356SLen Brown snapshot_gfx_mhz(); 331127d47356SLen Brown 3312b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3313b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3314b4b91569SRafael Antognolli 3315be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3316be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3317be0e54c4SLen Brown 3318be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3319be0e54c4SLen Brown snapshot_sys_lpi_us(); 3320be0e54c4SLen Brown 3321562a2d37SLen Brown return 0; 3322562a2d37SLen Brown } 3323562a2d37SLen Brown 33248aa2ed0bSLen Brown int exit_requested; 33258aa2ed0bSLen Brown 33268aa2ed0bSLen Brown static void signal_handler(int signal) 33278aa2ed0bSLen Brown { 33288aa2ed0bSLen Brown switch (signal) { 33298aa2ed0bSLen Brown case SIGINT: 33308aa2ed0bSLen Brown exit_requested = 1; 33318aa2ed0bSLen Brown if (debug) 33328aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 33338aa2ed0bSLen Brown break; 333407211960SLen Brown case SIGUSR1: 333507211960SLen Brown if (debug > 1) 333607211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 333707211960SLen Brown break; 33388aa2ed0bSLen Brown } 33398aa2ed0bSLen Brown } 33408aa2ed0bSLen Brown 33418aa2ed0bSLen Brown void setup_signal_handler(void) 33428aa2ed0bSLen Brown { 33438aa2ed0bSLen Brown struct sigaction sa; 33448aa2ed0bSLen Brown 33458aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 33468aa2ed0bSLen Brown 33478aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 33488aa2ed0bSLen Brown 33498aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 33508aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 335107211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 335207211960SLen Brown err(1, "sigaction SIGUSR1"); 33538aa2ed0bSLen Brown } 3354b9ad8ee0SLen Brown 335547936f94SArtem Bityutskiy void do_sleep(void) 3356b9ad8ee0SLen Brown { 3357c026c236SArtem Bityutskiy struct timeval tout; 3358c026c236SArtem Bityutskiy struct timespec rest; 3359b9ad8ee0SLen Brown fd_set readfds; 3360b9ad8ee0SLen Brown int retval; 3361b9ad8ee0SLen Brown 3362b9ad8ee0SLen Brown FD_ZERO(&readfds); 3363b9ad8ee0SLen Brown FD_SET(0, &readfds); 3364b9ad8ee0SLen Brown 3365c026c236SArtem Bityutskiy if (ignore_stdin) { 336647936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 336747936f94SArtem Bityutskiy return; 336847936f94SArtem Bityutskiy } 3369b9ad8ee0SLen Brown 3370c026c236SArtem Bityutskiy tout = interval_tv; 3371c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3372b9ad8ee0SLen Brown 3373b9ad8ee0SLen Brown if (retval == 1) { 3374b9ad8ee0SLen Brown switch (getc(stdin)) { 3375b9ad8ee0SLen Brown case 'q': 3376b9ad8ee0SLen Brown exit_requested = 1; 3377b9ad8ee0SLen Brown break; 3378c026c236SArtem Bityutskiy case EOF: 3379c026c236SArtem Bityutskiy /* 3380c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3381c026c236SArtem Bityutskiy * won't be any further input. 3382c026c236SArtem Bityutskiy */ 3383c026c236SArtem Bityutskiy ignore_stdin = 1; 3384c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3385c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3386c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3387c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3388b9ad8ee0SLen Brown } 3389b9ad8ee0SLen Brown } 3390b9ad8ee0SLen Brown } 339147936f94SArtem Bityutskiy 339287e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 339387e15da9SChen Yu { 339487e15da9SChen Yu int ret, idx; 339587e15da9SChen Yu unsigned long long msr_cur, msr_last; 339687e15da9SChen Yu 339787e15da9SChen Yu if (!per_cpu_msr_sum) 339887e15da9SChen Yu return 1; 339987e15da9SChen Yu 340087e15da9SChen Yu idx = offset_to_idx(offset); 340187e15da9SChen Yu if (idx < 0) 340287e15da9SChen Yu return idx; 340387e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 340487e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 340587e15da9SChen Yu if (ret) 340687e15da9SChen Yu return ret; 340787e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 340887e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 340987e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 341087e15da9SChen Yu 341187e15da9SChen Yu return 0; 341287e15da9SChen Yu } 341387e15da9SChen Yu 341487e15da9SChen Yu timer_t timerid; 341587e15da9SChen Yu 341687e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 341787e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 341887e15da9SChen Yu { 341987e15da9SChen Yu int i, ret; 342087e15da9SChen Yu int cpu = t->cpu_id; 342187e15da9SChen Yu 34229878bf7aSLen Brown UNUSED(c); 34239878bf7aSLen Brown UNUSED(p); 34249878bf7aSLen Brown 342587e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 342687e15da9SChen Yu unsigned long long msr_cur, msr_last; 342713a779deSCalvin Walton off_t offset; 342887e15da9SChen Yu 342987e15da9SChen Yu if (!idx_valid(i)) 343087e15da9SChen Yu continue; 343187e15da9SChen Yu offset = idx_to_offset(i); 343287e15da9SChen Yu if (offset < 0) 343387e15da9SChen Yu continue; 343487e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 343587e15da9SChen Yu if (ret) { 34361b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset); 343787e15da9SChen Yu continue; 343887e15da9SChen Yu } 343987e15da9SChen Yu 344087e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 344187e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 344287e15da9SChen Yu 344387e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 344487e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 344587e15da9SChen Yu } 344687e15da9SChen Yu return 0; 344787e15da9SChen Yu } 344887e15da9SChen Yu 34491b439f01SLen Brown static void msr_record_handler(union sigval v) 345087e15da9SChen Yu { 34519878bf7aSLen Brown UNUSED(v); 34529878bf7aSLen Brown 345387e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 345487e15da9SChen Yu } 345587e15da9SChen Yu 345687e15da9SChen Yu void msr_sum_record(void) 345787e15da9SChen Yu { 345887e15da9SChen Yu struct itimerspec its; 345987e15da9SChen Yu struct sigevent sev; 346087e15da9SChen Yu 346187e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 346287e15da9SChen Yu if (!per_cpu_msr_sum) { 346387e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 346487e15da9SChen Yu return; 346587e15da9SChen Yu } 346687e15da9SChen Yu /* 346787e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 346887e15da9SChen Yu */ 346987e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 347087e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 347187e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 347287e15da9SChen Yu 347387e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 347487e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 347587e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 347687e15da9SChen Yu goto release_msr; 347787e15da9SChen Yu } 347887e15da9SChen Yu 347987e15da9SChen Yu its.it_value.tv_sec = 0; 348087e15da9SChen Yu its.it_value.tv_nsec = 1; 348187e15da9SChen Yu /* 348287e15da9SChen Yu * A wraparound time has been calculated early. 348387e15da9SChen Yu * Some sources state that the peak power for a 348487e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 348587e15da9SChen Yu * use 2 * TDP for safety. 348687e15da9SChen Yu */ 348787e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 348887e15da9SChen Yu its.it_interval.tv_nsec = 0; 348987e15da9SChen Yu 349087e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 349187e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 349287e15da9SChen Yu goto release_timer; 349387e15da9SChen Yu } 349487e15da9SChen Yu return; 349587e15da9SChen Yu 349687e15da9SChen Yu release_timer: 349787e15da9SChen Yu timer_delete(timerid); 349887e15da9SChen Yu release_msr: 349987e15da9SChen Yu free(per_cpu_msr_sum); 350087e15da9SChen Yu } 35014c2122d4SLen Brown 350238c6663aSLen Brown /* 350338c6663aSLen Brown * set_my_sched_priority(pri) 350438c6663aSLen Brown * return previous 3505164d7a96SLen Brown * 3506164d7a96SLen Brown * if non-root, do this: 3507164d7a96SLen Brown * # /sbin/setcap cap_sys_rawio,cap_sys_nice=+ep /usr/bin/turbostat 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) 352138c6663aSLen Brown err(retval, "setpriority(%d)", priority); 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", 44291b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "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: 54531c1313b5SZhang Rui case INTEL_FAM6_ALDERLAKE_N: 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; 5466f5a4c76aSLen Brown } 5467f5a4c76aSLen Brown return model; 5468f5a4c76aSLen Brown } 5469d76bb7a0SLen Brown 5470d76bb7a0SLen Brown void print_dev_latency(void) 5471d76bb7a0SLen Brown { 5472d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5473d76bb7a0SLen Brown int fd; 5474d76bb7a0SLen Brown int value; 5475d76bb7a0SLen Brown int retval; 5476d76bb7a0SLen Brown 5477d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5478d76bb7a0SLen Brown if (fd < 0) { 5479d76bb7a0SLen Brown warn("fopen %s\n", path); 5480d76bb7a0SLen Brown return; 5481d76bb7a0SLen Brown } 5482d76bb7a0SLen Brown 5483d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5484d76bb7a0SLen Brown if (retval != sizeof(int)) { 5485d76bb7a0SLen Brown warn("read %s\n", path); 5486d76bb7a0SLen Brown close(fd); 5487d76bb7a0SLen Brown return; 5488d76bb7a0SLen Brown } 54891b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 5490d76bb7a0SLen Brown 5491d76bb7a0SLen Brown close(fd); 5492d76bb7a0SLen Brown } 5493d76bb7a0SLen Brown 54942af4f9b8SLen Brown /* 54952db0e5ebSJiang Jian * Linux-perf manages the HW instructions-retired counter 54962af4f9b8SLen Brown * by enabling when requested, and hiding rollover 54972af4f9b8SLen Brown */ 54982af4f9b8SLen Brown void linux_perf_init(void) 54992af4f9b8SLen Brown { 55002af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 55012af4f9b8SLen Brown return; 55022af4f9b8SLen Brown 55032af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 55042af4f9b8SLen Brown return; 55052af4f9b8SLen Brown 55062af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 55072af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 55082af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 55092af4f9b8SLen Brown 55102af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 55112af4f9b8SLen Brown } 55122af4f9b8SLen Brown 5513fcd17211SLen Brown void process_cpuid() 5514103a8feaSLen Brown { 551534041551SLen Brown unsigned int eax, ebx, ecx, edx; 551634041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5517ed0757b8SLen Brown unsigned long long ucode_patch = 0; 5518103a8feaSLen Brown 5519103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5520103a8feaSLen Brown 55215aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5522103a8feaSLen Brown 552334041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5524103a8feaSLen Brown genuine_intel = 1; 552534041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 552634041551SLen Brown authentic_amd = 1; 5527c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5528c1c10cc7SPu Wen hygon_genuine = 1; 5529103a8feaSLen Brown 553096e47158SLen Brown if (!quiet) 5531ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5532ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5533103a8feaSLen Brown 55345aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5535103a8feaSLen Brown family = (fms >> 8) & 0xf; 5536103a8feaSLen Brown model = (fms >> 4) & 0xf; 5537103a8feaSLen Brown stepping = fms & 0xf; 55385aa3d1a2SCalvin Walton if (family == 0xf) 55395aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 55405aa3d1a2SCalvin Walton if (family >= 6) 5541103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 554234041551SLen Brown ecx_flags = ecx; 554334041551SLen Brown edx_flags = edx; 5544103a8feaSLen Brown 5545ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 5546ed0757b8SLen Brown warnx("get_msr(UCODE)\n"); 5547ed0757b8SLen Brown 5548103a8feaSLen Brown /* 5549103a8feaSLen Brown * check max extended function levels of CPUID. 5550103a8feaSLen Brown * This is needed to check for invariant TSC. 5551103a8feaSLen Brown * This check is valid for both Intel and AMD. 5552103a8feaSLen Brown */ 5553103a8feaSLen Brown ebx = ecx = edx = 0; 55545aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5555103a8feaSLen Brown 555634041551SLen Brown if (!quiet) { 5557ed0757b8SLen Brown fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n", 55581b439f01SLen Brown family, model, stepping, family, model, stepping, 55591b439f01SLen Brown (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 5560ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 556134041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 556234041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 556334041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 556434041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 556534041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 556634041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 556734041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 556834041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 556934041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 55701b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-"); 557134041551SLen Brown } 557225368d7cSChen Yu if (genuine_intel) { 5573e9d3092fSZhang Rui model_orig = model; 5574f5a4c76aSLen Brown model = intel_model_duplicates(model); 557525368d7cSChen Yu } 557634041551SLen Brown 557734041551SLen Brown if (!(edx_flags & (1 << 5))) 557834041551SLen Brown errx(1, "CPUID: no MSR"); 557934041551SLen Brown 558061a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5581103a8feaSLen Brown 5582103a8feaSLen Brown /* 5583103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5584103a8feaSLen Brown * this check is valid for both Intel and AMD 5585103a8feaSLen Brown */ 55865aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 55878209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5588d7899447SLen Brown } 5589103a8feaSLen Brown 5590103a8feaSLen Brown /* 5591103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5592103a8feaSLen Brown * this check is valid for both Intel and AMD 5593103a8feaSLen Brown */ 5594103a8feaSLen Brown 55955aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 55968209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5597812db3f7SLen Brown if (has_aperf) { 5598812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5599812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5600812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5601812db3f7SLen Brown } 5602889facbeSLen Brown do_dts = eax & (1 << 0); 5603812db3f7SLen Brown if (do_dts) 5604812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5605b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5606889facbeSLen Brown do_ptm = eax & (1 << 6); 5607812db3f7SLen Brown if (do_ptm) 5608812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 56097f5c258eSLen Brown has_hwp = eax & (1 << 7); 56107f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 56117f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 56127f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 56137f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5614889facbeSLen Brown has_epb = ecx & (1 << 3); 5615889facbeSLen Brown 561696e47158SLen Brown if (!quiet) 5617b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 56187f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 56197f5c258eSLen Brown has_aperf ? "" : "No-", 5620b3a34e93SLen Brown has_turbo ? "" : "No-", 56217f5c258eSLen Brown do_dts ? "" : "No-", 56227f5c258eSLen Brown do_ptm ? "" : "No-", 56237f5c258eSLen Brown has_hwp ? "" : "No-", 56247f5c258eSLen Brown has_hwp_notify ? "" : "No-", 56257f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 56261b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 5627103a8feaSLen Brown 562896e47158SLen Brown if (!quiet) 562969807a63SLen Brown decode_misc_enable_msr(); 563069807a63SLen Brown 563196e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5632aa8d8cc7SLen Brown int has_sgx; 5633aa8d8cc7SLen Brown 5634aa8d8cc7SLen Brown ecx = 0; 5635aa8d8cc7SLen Brown 5636aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5637aa8d8cc7SLen Brown 5638aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5639774627c5SLen Brown 5640774627c5SLen Brown is_hybrid = edx & (1 << 15); 5641774627c5SLen Brown 5642774627c5SLen Brown fprintf(outf, "CPUID(7): %sSGX %sHybrid\n", has_sgx ? "" : "No-", is_hybrid ? "" : "No-"); 5643aa8d8cc7SLen Brown 5644aa8d8cc7SLen Brown if (has_sgx) 5645aa8d8cc7SLen Brown decode_feature_control_msr(); 5646aa8d8cc7SLen Brown } 5647aa8d8cc7SLen Brown 564861a87ba7SLen Brown if (max_level >= 0x15) { 56498a5bdf41SLen Brown unsigned int eax_crystal; 56508a5bdf41SLen Brown unsigned int ebx_tsc; 56518a5bdf41SLen Brown 56528a5bdf41SLen Brown /* 56538a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 56548a5bdf41SLen Brown */ 56558a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 56565aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 56578a5bdf41SLen Brown 56588a5bdf41SLen Brown if (ebx_tsc != 0) { 56598a5bdf41SLen Brown 566096e47158SLen Brown if (!quiet && (ebx != 0)) 5661b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 56628a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 56638a5bdf41SLen Brown 56648a5bdf41SLen Brown if (crystal_hz == 0) 56658a5bdf41SLen Brown switch (model) { 5666af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5667e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5668e8efbc80SLen Brown break; 56695ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5670ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5671ec53e594SLen Brown break; 5672869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5673f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5674e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 56758a5bdf41SLen Brown break; 56768a5bdf41SLen Brown default: 56778a5bdf41SLen Brown crystal_hz = 0; 56788a5bdf41SLen Brown } 56798a5bdf41SLen Brown 56808a5bdf41SLen Brown if (crystal_hz) { 56818a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 568296e47158SLen Brown if (!quiet) 5683b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 56848a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 56858a5bdf41SLen Brown } 56868a5bdf41SLen Brown } 56878a5bdf41SLen Brown } 568861a87ba7SLen Brown if (max_level >= 0x16) { 568961a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 569061a87ba7SLen Brown 569161a87ba7SLen Brown /* 569261a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 569361a87ba7SLen Brown */ 569461a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 569561a87ba7SLen Brown 56965aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 569796e47158SLen Brown if (!quiet) 5698b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 569961a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 570061a87ba7SLen Brown } 57018a5bdf41SLen Brown 5702b2b34dfeSHubert Chrzaniuk if (has_aperf) 5703b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5704b2b34dfeSHubert Chrzaniuk 5705812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5706812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5707812db3f7SLen Brown 5708812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5709812db3f7SLen Brown do_nhm_platform_info = 1; 5710812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5711812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5712812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5713812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5714812db3f7SLen Brown } 5715d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5716812db3f7SLen Brown 5717812db3f7SLen Brown if (do_snb_cstates) 5718812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5719812db3f7SLen Brown 57205a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 57210f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 57220f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 57230f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 57240f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 57250f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 57260f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 57270f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 57280f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 57290539ba11SLen Brown if (has_slv_msrs(family, model)) { 57300f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 57310f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 57320f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 57330f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 57340539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 57350539ba11SLen Brown use_c1_residency_msr = 1; 57360539ba11SLen Brown } 573720de0dabSAntti Laakso if (is_jvl(family, model)) { 573820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 573920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 574020de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 574120de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 574220de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 574320de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 574420de0dabSAntti Laakso } 57457170a374SLen Brown if (is_dnv(family, model)) { 57467170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 57477170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 57487170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 57497170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 57507170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 57517170a374SLen Brown use_c1_residency_msr = 1; 57527170a374SLen Brown } 5753684e40e9SArtem Bityutskiy if (is_skx(family, model) || is_icx(family, model) || is_spr(family, model)) { 575434c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 575534c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 575634c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 575734c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 575834c76197SLen Brown } 5759ade0ebacSLen Brown if (is_bdx(family, model)) { 5760ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5761ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5762ade0ebacSLen Brown } 5763570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5764c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 57650f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5766c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 57670f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5768c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 57690f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 57700f47c08dSLen Brown } 5771570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5772a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5773a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5774a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5775a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5776a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5777a99d8730SLen Brown } 5778144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5779fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5780103a8feaSLen Brown 57811b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model)) 5782562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5783103a8feaSLen Brown 578496e47158SLen Brown if (!quiet) 5785f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5786f0057310SLen Brown 578796e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 578871616c8eSLen Brown decode_c6_demotion_policy_msr(); 578971616c8eSLen Brown 5790889facbeSLen Brown rapl_probe(family, model); 57913a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5792ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5793889facbeSLen Brown 57940b9a0b9bSZhang Rui check_tcc_offset(model_orig); 57950b9a0b9bSZhang Rui 579696e47158SLen Brown if (!quiet) 57971b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5798a5c6d65dSLen Brown intel_uncore_frequency_probe(); 5799fcd17211SLen Brown 580041618e63SLen Brown if (!quiet) 5801d76bb7a0SLen Brown print_dev_latency(); 5802d76bb7a0SLen Brown if (!quiet) 580341618e63SLen Brown dump_sysfs_cstate_config(); 58047293fccdSLen Brown if (!quiet) 58057293fccdSLen Brown dump_sysfs_pstate_config(); 580641618e63SLen Brown 58078c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5808a2b7b749SLen Brown calculate_tsc_tweak(); 5809a2b7b749SLen Brown 5810812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5811812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5812fdf676e5SLen Brown 5813812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5814812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 581527d47356SLen Brown 5816b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5817b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5818b4b91569SRafael Antognolli 5819be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5820be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5821be0e54c4SLen Brown else 5822be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5823be0e54c4SLen Brown 5824eae97e05SChen Yu if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK)) 5825eae97e05SChen Yu BIC_PRESENT(BIC_CORE_THROT_CNT); 5826eae97e05SChen Yu else 5827eae97e05SChen Yu BIC_NOT_PRESENT(BIC_CORE_THROT_CNT); 5828eae97e05SChen Yu 58291f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 58301f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5831be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 58321f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 58331f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 58341f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 58351f81c5efSLen Brown } else { 58361f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5837be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 58381f81c5efSLen Brown } 5839be0e54c4SLen Brown 584096e47158SLen Brown if (!quiet) 584133148d67SLen Brown decode_misc_feature_control(); 584233148d67SLen Brown 5843889facbeSLen Brown return; 5844103a8feaSLen Brown } 5845103a8feaSLen Brown 5846103a8feaSLen Brown /* 5847103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5848103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5849103a8feaSLen Brown */ 5850103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5851103a8feaSLen Brown { 5852103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5853103a8feaSLen Brown return 1; 5854103a8feaSLen Brown else 5855103a8feaSLen Brown return 0; 5856103a8feaSLen Brown } 5857103a8feaSLen Brown 5858c98d5d94SLen Brown void topology_probe() 5859c98d5d94SLen Brown { 5860c98d5d94SLen Brown int i; 5861c98d5d94SLen Brown int max_core_id = 0; 5862c98d5d94SLen Brown int max_package_id = 0; 58636de68fe1SLen Brown int max_die_id = 0; 5864c98d5d94SLen Brown int max_siblings = 0; 5865c98d5d94SLen Brown 5866c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5867843c5791SPrarit Bhargava set_max_cpu_num(); 5868c98d5d94SLen Brown topo.num_cpus = 0; 5869c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5870c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5871812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5872c98d5d94SLen Brown 5873d8af6f5fSLen Brown if (debug > 1) 5874b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5875c98d5d94SLen Brown 5876c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5877b2c95d90SJosh Triplett if (cpus == NULL) 5878b2c95d90SJosh Triplett err(1, "calloc cpus"); 5879c98d5d94SLen Brown 5880c98d5d94SLen Brown /* 5881c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5882c98d5d94SLen Brown */ 5883c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5884b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5885b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5886c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5887c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5888c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5889c98d5d94SLen Brown 5890c98d5d94SLen Brown /* 58911ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 58921ef7d21aSLen Brown */ 58931ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 58941ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 58951ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 58961ef7d21aSLen Brown err(1, "cpu%d not present", i); 58971ef7d21aSLen Brown } 58981ef7d21aSLen Brown 58991ef7d21aSLen Brown /* 5900c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5901c98d5d94SLen Brown */ 5902c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5903b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5904b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5905c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5906c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5907c98d5d94SLen Brown 59088cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5909c98d5d94SLen Brown 5910c98d5d94SLen Brown /* 5911c98d5d94SLen Brown * For online cpus 5912c98d5d94SLen Brown * find max_core_id, max_package_id 5913c98d5d94SLen Brown */ 5914c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5915c98d5d94SLen Brown int siblings; 5916c98d5d94SLen Brown 5917c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5918d8af6f5fSLen Brown if (debug > 1) 5919b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5920c98d5d94SLen Brown continue; 5921c98d5d94SLen Brown } 5922c98d5d94SLen Brown 59230e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 59240e2d8f05SLen Brown 59250e2d8f05SLen Brown /* get package information */ 5926c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5927c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5928c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5929c98d5d94SLen Brown 59306de68fe1SLen Brown /* get die information */ 59316de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 59326de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 59336de68fe1SLen Brown max_die_id = cpus[i].die_id; 59346de68fe1SLen Brown 59350e2d8f05SLen Brown /* get numa node information */ 5936ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5937ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5938ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 59390e2d8f05SLen Brown 59400e2d8f05SLen Brown /* get core information */ 59410e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 59420e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 59430e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 59440e2d8f05SLen Brown 59450e2d8f05SLen Brown /* get thread information */ 59460e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5947c98d5d94SLen Brown if (siblings > max_siblings) 5948c98d5d94SLen Brown max_siblings = siblings; 59494f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 59508cb48b32SPrarit Bhargava topo.num_cores++; 5951c98d5d94SLen Brown } 5952ef605741SPrarit Bhargava 595370a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5954d8af6f5fSLen Brown if (debug > 1) 59551b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node); 595670a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5957812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5958c98d5d94SLen Brown 59596de68fe1SLen Brown topo.num_die = max_die_id + 1; 59606de68fe1SLen Brown if (debug > 1) 59611b439f01SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die); 59626de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 59636de68fe1SLen Brown BIC_PRESENT(BIC_Die); 59646de68fe1SLen Brown 5965c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5966d8af6f5fSLen Brown if (debug > 1) 59671b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 59687da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5969812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5970c98d5d94SLen Brown 5971ef605741SPrarit Bhargava set_node_data(); 5972ef605741SPrarit Bhargava if (debug > 1) 597370a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 597401235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 597501235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5976ef605741SPrarit Bhargava 597770a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5978d8af6f5fSLen Brown if (debug > 1) 5979b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 59802ffbb224SPrarit Bhargava 59812ffbb224SPrarit Bhargava if (debug < 1) 59822ffbb224SPrarit Bhargava return; 59832ffbb224SPrarit Bhargava 59842ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 59850ec712e3SLen Brown if (cpu_is_not_present(i)) 59860ec712e3SLen Brown continue; 59872ffbb224SPrarit Bhargava fprintf(outf, 59886de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 59896de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 59901b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id); 59912ffbb224SPrarit Bhargava } 59922ffbb224SPrarit Bhargava 5993c98d5d94SLen Brown } 5994c98d5d94SLen Brown 59951b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 5996c98d5d94SLen Brown { 5997c98d5d94SLen Brown int i; 59981b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; 599940f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 6000c98d5d94SLen Brown 600140f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 6002c98d5d94SLen Brown if (*t == NULL) 6003c98d5d94SLen Brown goto error; 6004c98d5d94SLen Brown 600540f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 6006c98d5d94SLen Brown (*t)[i].cpu_id = -1; 6007c98d5d94SLen Brown 600840f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 6009c98d5d94SLen Brown if (*c == NULL) 6010c98d5d94SLen Brown goto error; 6011c98d5d94SLen Brown 601240f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 6013c98d5d94SLen Brown (*c)[i].core_id = -1; 6014c98d5d94SLen Brown 6015678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 6016c98d5d94SLen Brown if (*p == NULL) 6017c98d5d94SLen Brown goto error; 6018c98d5d94SLen Brown 6019c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 6020c98d5d94SLen Brown (*p)[i].package_id = i; 6021c98d5d94SLen Brown 6022c98d5d94SLen Brown return; 6023c98d5d94SLen Brown error: 6024b2c95d90SJosh Triplett err(1, "calloc counters"); 6025c98d5d94SLen Brown } 60261b439f01SLen Brown 6027c98d5d94SLen Brown /* 6028c98d5d94SLen Brown * init_counter() 6029c98d5d94SLen Brown * 6030c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 6031c98d5d94SLen Brown */ 60321b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 6033c98d5d94SLen Brown { 60348cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 603540f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 60368cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 60378cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 6038c98d5d94SLen Brown struct thread_data *t; 6039c98d5d94SLen Brown struct core_data *c; 6040c98d5d94SLen Brown struct pkg_data *p; 6041c98d5d94SLen Brown 604242dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 604342dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 604442dd4520SNathan Ciobanu */ 604542dd4520SNathan Ciobanu if (node_id < 0) 604642dd4520SNathan Ciobanu node_id = 0; 604742dd4520SNathan Ciobanu 604840f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 604940f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 60508cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 6051c98d5d94SLen Brown 6052c98d5d94SLen Brown t->cpu_id = cpu_id; 60538cb48b32SPrarit Bhargava if (thread_id == 0) { 6054c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 6055c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 6056c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 6057c98d5d94SLen Brown } 6058c98d5d94SLen Brown 60598cb48b32SPrarit Bhargava c->core_id = core_id; 60608cb48b32SPrarit Bhargava p->package_id = pkg_id; 6061c98d5d94SLen Brown } 6062c98d5d94SLen Brown 6063c98d5d94SLen Brown int initialize_counters(int cpu_id) 6064c98d5d94SLen Brown { 60658cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 60668cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 6067c98d5d94SLen Brown return 0; 6068c98d5d94SLen Brown } 6069c98d5d94SLen Brown 6070c98d5d94SLen Brown void allocate_output_buffer() 6071c98d5d94SLen Brown { 6072eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 6073c98d5d94SLen Brown outp = output_buffer; 6074b2c95d90SJosh Triplett if (outp == NULL) 6075b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 6076c98d5d94SLen Brown } 60771b439f01SLen Brown 607836229897SLen Brown void allocate_fd_percpu(void) 607936229897SLen Brown { 608001a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 608136229897SLen Brown if (fd_percpu == NULL) 608236229897SLen Brown err(-1, "calloc fd_percpu"); 608336229897SLen Brown } 60841b439f01SLen Brown 6085562a2d37SLen Brown void allocate_irq_buffers(void) 6086562a2d37SLen Brown { 6087562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 6088562a2d37SLen Brown if (irq_column_2_cpu == NULL) 6089562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 6090c98d5d94SLen Brown 609101a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 6092562a2d37SLen Brown if (irqs_per_cpu == NULL) 609301a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 6094562a2d37SLen Brown } 60951b439f01SLen Brown 6096c98d5d94SLen Brown void setup_all_buffers(void) 6097c98d5d94SLen Brown { 6098c98d5d94SLen Brown topology_probe(); 6099562a2d37SLen Brown allocate_irq_buffers(); 610036229897SLen Brown allocate_fd_percpu(); 6101c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 6102c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 6103c98d5d94SLen Brown allocate_output_buffer(); 6104c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 6105c98d5d94SLen Brown } 61063b4d5c7fSAndy Shevchenko 61077ce7d5deSPrarit Bhargava void set_base_cpu(void) 61087ce7d5deSPrarit Bhargava { 61097ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 61107ce7d5deSPrarit Bhargava if (base_cpu < 0) 61117ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 61127ce7d5deSPrarit Bhargava 61137ce7d5deSPrarit Bhargava if (debug > 1) 6114b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 61157ce7d5deSPrarit Bhargava } 61167ce7d5deSPrarit Bhargava 6117103a8feaSLen Brown void turbostat_init() 6118103a8feaSLen Brown { 61197ce7d5deSPrarit Bhargava setup_all_buffers(); 61207ce7d5deSPrarit Bhargava set_base_cpu(); 6121103a8feaSLen Brown check_dev_msr(); 612298481e79SLen Brown check_permissions(); 6123fcd17211SLen Brown process_cpuid(); 61242af4f9b8SLen Brown linux_perf_init(); 6125103a8feaSLen Brown 612696e47158SLen Brown if (!quiet) 61277f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 61287f5c258eSLen Brown 612996e47158SLen Brown if (!quiet) 6130889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 6131889facbeSLen Brown 613296e47158SLen Brown if (!quiet) 61333a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 61343a9a941dSLen Brown 613596e47158SLen Brown if (!quiet) 6136889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 6137889facbeSLen Brown 6138889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 6139889facbeSLen Brown 61407ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 61417ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 61427ab5ff49SZhang Rui 614396e47158SLen Brown if (!quiet) 6144889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 61455a63426eSLen Brown 614696e47158SLen Brown if (!quiet && do_irtl_snb) 61475a63426eSLen Brown print_irtl(); 6148164d7a96SLen Brown 6149164d7a96SLen Brown if (DO_BIC(BIC_IPC)) 6150164d7a96SLen Brown (void)get_instr_count_fd(base_cpu); 6151103a8feaSLen Brown } 6152103a8feaSLen Brown 6153103a8feaSLen Brown int fork_it(char **argv) 6154103a8feaSLen Brown { 6155103a8feaSLen Brown pid_t child_pid; 6156d91bb17cSLen Brown int status; 6157d15cf7c1SLen Brown 6158218f0e8dSLen Brown snapshot_proc_sysfs_files(); 6159d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 61604c2122d4SLen Brown first_counter_read = 0; 6161d91bb17cSLen Brown if (status) 6162d91bb17cSLen Brown exit(status); 6163d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 6164d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 6165103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 6166103a8feaSLen Brown 6167103a8feaSLen Brown child_pid = fork(); 6168103a8feaSLen Brown if (!child_pid) { 6169103a8feaSLen Brown /* child */ 6170103a8feaSLen Brown execvp(argv[0], argv); 61710815a3d0SLen Brown err(errno, "exec %s", argv[0]); 6172103a8feaSLen Brown } else { 6173103a8feaSLen Brown 6174103a8feaSLen Brown /* parent */ 6175b2c95d90SJosh Triplett if (child_pid == -1) 6176b2c95d90SJosh Triplett err(1, "fork"); 6177103a8feaSLen Brown 6178103a8feaSLen Brown signal(SIGINT, SIG_IGN); 6179103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 6180b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 6181b2c95d90SJosh Triplett err(status, "waitpid"); 61822a954966SDavid Arcari 61832a954966SDavid Arcari if (WIFEXITED(status)) 61842a954966SDavid Arcari status = WEXITSTATUS(status); 6185103a8feaSLen Brown } 6186c98d5d94SLen Brown /* 6187c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 6188c98d5d94SLen Brown * because re-starting is problematic when forking 6189c98d5d94SLen Brown */ 6190218f0e8dSLen Brown snapshot_proc_sysfs_files(); 6191c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 6192103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 6193103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 6194ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 6195ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 6196ba3dec99SLen Brown else { 6197c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 6198c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 6199ba3dec99SLen Brown } 6200103a8feaSLen Brown 6201b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 6202b7d8c148SLen Brown 6203b7d8c148SLen Brown flush_output_stderr(); 6204103a8feaSLen Brown 6205d91bb17cSLen Brown return status; 6206103a8feaSLen Brown } 6207103a8feaSLen Brown 62083b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 62093b4d5c7fSAndy Shevchenko { 62103b4d5c7fSAndy Shevchenko int status; 62113b4d5c7fSAndy Shevchenko 6212218f0e8dSLen Brown snapshot_proc_sysfs_files(); 62133b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 62143b4d5c7fSAndy Shevchenko if (status) 62153b4d5c7fSAndy Shevchenko return status; 62163b4d5c7fSAndy Shevchenko 62173b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 62183b4d5c7fSAndy Shevchenko if (status) 62193b4d5c7fSAndy Shevchenko return status; 62203b4d5c7fSAndy Shevchenko 6221b7d8c148SLen Brown flush_output_stdout(); 62223b4d5c7fSAndy Shevchenko 62233b4d5c7fSAndy Shevchenko return status; 62243b4d5c7fSAndy Shevchenko } 62253b4d5c7fSAndy Shevchenko 62261b439f01SLen Brown void print_version() 62271b439f01SLen Brown { 62289992dd77SLen Brown fprintf(outf, "turbostat version 2022.10.04 - Len Brown <lenb@kernel.org>\n"); 6229d8af6f5fSLen Brown } 6230d8af6f5fSLen Brown 623103331233SChen Yu #define COMMAND_LINE_SIZE 2048 623203331233SChen Yu 623303331233SChen Yu void print_bootcmd(void) 623403331233SChen Yu { 623503331233SChen Yu char bootcmd[COMMAND_LINE_SIZE]; 623603331233SChen Yu FILE *fp; 623703331233SChen Yu int ret; 623803331233SChen Yu 623903331233SChen Yu memset(bootcmd, 0, COMMAND_LINE_SIZE); 624003331233SChen Yu fp = fopen("/proc/cmdline", "r"); 624103331233SChen Yu if (!fp) 624203331233SChen Yu return; 624303331233SChen Yu 624403331233SChen Yu ret = fread(bootcmd, sizeof(char), COMMAND_LINE_SIZE - 1, fp); 624503331233SChen Yu if (ret) { 624603331233SChen Yu bootcmd[ret] = '\0'; 624703331233SChen Yu /* the last character is already '\n' */ 624803331233SChen Yu fprintf(outf, "Kernel command line: %s", bootcmd); 624903331233SChen Yu } 625003331233SChen Yu 625103331233SChen Yu fclose(fp); 625203331233SChen Yu } 625303331233SChen Yu 6254495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 6255495c7654SLen Brown unsigned int width, enum counter_scope scope, 625641618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 6257388e9c81SLen Brown { 6258388e9c81SLen Brown struct msr_counter *msrp; 6259388e9c81SLen Brown 6260388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 6261388e9c81SLen Brown if (msrp == NULL) { 6262388e9c81SLen Brown perror("calloc"); 6263388e9c81SLen Brown exit(1); 6264388e9c81SLen Brown } 6265388e9c81SLen Brown 6266388e9c81SLen Brown msrp->msr_num = msr_num; 6267d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 6268495c7654SLen Brown if (path) 6269d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 6270388e9c81SLen Brown msrp->width = width; 6271388e9c81SLen Brown msrp->type = type; 6272388e9c81SLen Brown msrp->format = format; 627341618e63SLen Brown msrp->flags = flags; 6274388e9c81SLen Brown 6275388e9c81SLen Brown switch (scope) { 6276388e9c81SLen Brown 6277388e9c81SLen Brown case SCOPE_CPU: 6278388e9c81SLen Brown msrp->next = sys.tp; 6279388e9c81SLen Brown sys.tp = msrp; 6280678a3bd1SLen Brown sys.added_thread_counters++; 62810748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 62821b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS); 6283678a3bd1SLen Brown exit(-1); 6284678a3bd1SLen Brown } 6285388e9c81SLen Brown break; 6286388e9c81SLen Brown 6287388e9c81SLen Brown case SCOPE_CORE: 6288388e9c81SLen Brown msrp->next = sys.cp; 6289388e9c81SLen Brown sys.cp = msrp; 6290678a3bd1SLen Brown sys.added_core_counters++; 6291678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 62921b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS); 6293678a3bd1SLen Brown exit(-1); 6294678a3bd1SLen Brown } 6295388e9c81SLen Brown break; 6296388e9c81SLen Brown 6297388e9c81SLen Brown case SCOPE_PACKAGE: 6298388e9c81SLen Brown msrp->next = sys.pp; 6299388e9c81SLen Brown sys.pp = msrp; 6300678a3bd1SLen Brown sys.added_package_counters++; 6301678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 63021b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS); 6303678a3bd1SLen Brown exit(-1); 6304678a3bd1SLen Brown } 6305388e9c81SLen Brown break; 6306388e9c81SLen Brown } 6307388e9c81SLen Brown 6308388e9c81SLen Brown return 0; 6309388e9c81SLen Brown } 6310388e9c81SLen Brown 6311388e9c81SLen Brown void parse_add_command(char *add_command) 6312388e9c81SLen Brown { 6313388e9c81SLen Brown int msr_num = 0; 6314495c7654SLen Brown char *path = NULL; 63150f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 6316388e9c81SLen Brown int width = 64; 6317388e9c81SLen Brown int fail = 0; 6318388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 6319388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 6320388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 6321388e9c81SLen Brown 6322388e9c81SLen Brown while (add_command) { 6323388e9c81SLen Brown 6324388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 6325388e9c81SLen Brown goto next; 6326388e9c81SLen Brown 6327388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 6328388e9c81SLen Brown goto next; 6329388e9c81SLen Brown 6330495c7654SLen Brown if (*add_command == '/') { 6331495c7654SLen Brown path = add_command; 6332495c7654SLen Brown goto next; 6333495c7654SLen Brown } 6334495c7654SLen Brown 6335388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 6336388e9c81SLen Brown if ((width == 32) || (width == 64)) 6337388e9c81SLen Brown goto next; 6338388e9c81SLen Brown width = 64; 6339388e9c81SLen Brown } 6340388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6341388e9c81SLen Brown scope = SCOPE_CPU; 6342388e9c81SLen Brown goto next; 6343388e9c81SLen Brown } 6344388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6345388e9c81SLen Brown scope = SCOPE_CORE; 6346388e9c81SLen Brown goto next; 6347388e9c81SLen Brown } 6348388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6349388e9c81SLen Brown scope = SCOPE_PACKAGE; 6350388e9c81SLen Brown goto next; 6351388e9c81SLen Brown } 6352388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6353388e9c81SLen Brown type = COUNTER_CYCLES; 6354388e9c81SLen Brown goto next; 6355388e9c81SLen Brown } 6356388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6357388e9c81SLen Brown type = COUNTER_SECONDS; 6358388e9c81SLen Brown goto next; 6359388e9c81SLen Brown } 636041618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 636141618e63SLen Brown type = COUNTER_USEC; 636241618e63SLen Brown goto next; 636341618e63SLen Brown } 6364388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6365388e9c81SLen Brown format = FORMAT_RAW; 6366388e9c81SLen Brown goto next; 6367388e9c81SLen Brown } 6368388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6369388e9c81SLen Brown format = FORMAT_DELTA; 6370388e9c81SLen Brown goto next; 6371388e9c81SLen Brown } 6372388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6373388e9c81SLen Brown format = FORMAT_PERCENT; 6374388e9c81SLen Brown goto next; 6375388e9c81SLen Brown } 6376388e9c81SLen Brown 6377388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6378388e9c81SLen Brown char *eos; 6379388e9c81SLen Brown 6380388e9c81SLen Brown eos = strchr(name_buffer, ','); 6381388e9c81SLen Brown if (eos) 6382388e9c81SLen Brown *eos = '\0'; 6383388e9c81SLen Brown goto next; 6384388e9c81SLen Brown } 6385388e9c81SLen Brown 6386388e9c81SLen Brown next: 6387388e9c81SLen Brown add_command = strchr(add_command, ','); 6388495c7654SLen Brown if (add_command) { 6389495c7654SLen Brown *add_command = '\0'; 6390388e9c81SLen Brown add_command++; 6391495c7654SLen Brown } 6392388e9c81SLen Brown 6393388e9c81SLen Brown } 6394495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6395495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6396388e9c81SLen Brown fail++; 6397388e9c81SLen Brown } 6398388e9c81SLen Brown 6399388e9c81SLen Brown /* generate default column header */ 6400388e9c81SLen Brown if (*name_buffer == '\0') { 6401388e9c81SLen Brown if (width == 32) 64025f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6403388e9c81SLen Brown else 64045f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6405388e9c81SLen Brown } 6406388e9c81SLen Brown 640741618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6408388e9c81SLen Brown fail++; 6409388e9c81SLen Brown 6410388e9c81SLen Brown if (fail) { 6411388e9c81SLen Brown help(); 6412388e9c81SLen Brown exit(1); 6413388e9c81SLen Brown } 6414388e9c81SLen Brown } 641541618e63SLen Brown 64160fc521bcSZephaniah E. Loss-Cutler-Hull int is_deferred_add(char *name) 64170fc521bcSZephaniah E. Loss-Cutler-Hull { 64180fc521bcSZephaniah E. Loss-Cutler-Hull int i; 64190fc521bcSZephaniah E. Loss-Cutler-Hull 64200fc521bcSZephaniah E. Loss-Cutler-Hull for (i = 0; i < deferred_add_index; ++i) 64210fc521bcSZephaniah E. Loss-Cutler-Hull if (!strcmp(name, deferred_add_names[i])) 64220fc521bcSZephaniah E. Loss-Cutler-Hull return 1; 64230fc521bcSZephaniah E. Loss-Cutler-Hull return 0; 64240fc521bcSZephaniah E. Loss-Cutler-Hull } 64250fc521bcSZephaniah E. Loss-Cutler-Hull 6426dd778a5eSLen Brown int is_deferred_skip(char *name) 6427dd778a5eSLen Brown { 6428dd778a5eSLen Brown int i; 6429dd778a5eSLen Brown 6430dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6431dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6432dd778a5eSLen Brown return 1; 6433dd778a5eSLen Brown return 0; 6434dd778a5eSLen Brown } 6435dd778a5eSLen Brown 643641618e63SLen Brown void probe_sysfs(void) 643741618e63SLen Brown { 643841618e63SLen Brown char path[64]; 643941618e63SLen Brown char name_buf[16]; 644041618e63SLen Brown FILE *input; 644141618e63SLen Brown int state; 644241618e63SLen Brown char *sp; 644341618e63SLen Brown 64440748eaf0SLen Brown for (state = 10; state >= 0; --state) { 644541618e63SLen Brown 64461b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 644741618e63SLen Brown input = fopen(path, "r"); 644841618e63SLen Brown if (input == NULL) 644941618e63SLen Brown continue; 64508173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 64518173c336SBen Hutchings err(1, "%s: failed to read file", path); 645241618e63SLen Brown 645341618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 645441618e63SLen Brown sp = strchr(name_buf, '-'); 645541618e63SLen Brown if (!sp) 645641618e63SLen Brown sp = strchrnul(name_buf, '\n'); 645741618e63SLen Brown *sp = '%'; 645841618e63SLen Brown *(sp + 1) = '\0'; 645941618e63SLen Brown 6460fecb3bc8SDavid Arcari remove_underbar(name_buf); 6461fecb3bc8SDavid Arcari 646241618e63SLen Brown fclose(input); 646341618e63SLen Brown 646441618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 646541618e63SLen Brown 64660fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 64670fc521bcSZephaniah E. Loss-Cutler-Hull continue; 64680fc521bcSZephaniah E. Loss-Cutler-Hull 6469dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6470dd778a5eSLen Brown continue; 6471dd778a5eSLen Brown 64721b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU); 647341618e63SLen Brown } 647441618e63SLen Brown 64750748eaf0SLen Brown for (state = 10; state >= 0; --state) { 647641618e63SLen Brown 64771b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 647841618e63SLen Brown input = fopen(path, "r"); 647941618e63SLen Brown if (input == NULL) 648041618e63SLen Brown continue; 64818173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 64828173c336SBen Hutchings err(1, "%s: failed to read file", path); 648341618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 648441618e63SLen Brown sp = strchr(name_buf, '-'); 648541618e63SLen Brown if (!sp) 648641618e63SLen Brown sp = strchrnul(name_buf, '\n'); 648741618e63SLen Brown *sp = '\0'; 648841618e63SLen Brown fclose(input); 648941618e63SLen Brown 6490fecb3bc8SDavid Arcari remove_underbar(name_buf); 6491fecb3bc8SDavid Arcari 649241618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 649341618e63SLen Brown 64940fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 64950fc521bcSZephaniah E. Loss-Cutler-Hull continue; 64960fc521bcSZephaniah E. Loss-Cutler-Hull 6497dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6498dd778a5eSLen Brown continue; 6499dd778a5eSLen Brown 65001b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU); 650141618e63SLen Brown } 650241618e63SLen Brown 650341618e63SLen Brown } 650441618e63SLen Brown 65051ef7d21aSLen Brown /* 65061ef7d21aSLen Brown * parse cpuset with following syntax 65071ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 65081ef7d21aSLen Brown */ 65091ef7d21aSLen Brown void parse_cpu_command(char *optarg) 65101ef7d21aSLen Brown { 65111ef7d21aSLen Brown unsigned int start, end; 65121ef7d21aSLen Brown char *next; 65131ef7d21aSLen Brown 65144e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 65154e4e1e7cSLen Brown if (cpu_subset) 65164e4e1e7cSLen Brown goto error; 65174e4e1e7cSLen Brown show_core_only++; 65184e4e1e7cSLen Brown return; 65194e4e1e7cSLen Brown } 65204e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 65214e4e1e7cSLen Brown if (cpu_subset) 65224e4e1e7cSLen Brown goto error; 65234e4e1e7cSLen Brown show_pkg_only++; 65244e4e1e7cSLen Brown return; 65254e4e1e7cSLen Brown } 65264e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 65274e4e1e7cSLen Brown goto error; 65284e4e1e7cSLen Brown 65291ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 65301ef7d21aSLen Brown if (cpu_subset == NULL) 65311ef7d21aSLen Brown err(3, "CPU_ALLOC"); 65321ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 65331ef7d21aSLen Brown 65341ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 65351ef7d21aSLen Brown 65361ef7d21aSLen Brown next = optarg; 65371ef7d21aSLen Brown 65381ef7d21aSLen Brown while (next && *next) { 65391ef7d21aSLen Brown 65401ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 65411ef7d21aSLen Brown goto error; 65421ef7d21aSLen Brown 65431ef7d21aSLen Brown start = strtoul(next, &next, 10); 65441ef7d21aSLen Brown 65451ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 65461ef7d21aSLen Brown goto error; 65471ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 65481ef7d21aSLen Brown 65491ef7d21aSLen Brown if (*next == '\0') 65501ef7d21aSLen Brown break; 65511ef7d21aSLen Brown 65521ef7d21aSLen Brown if (*next == ',') { 65531ef7d21aSLen Brown next += 1; 65541ef7d21aSLen Brown continue; 65551ef7d21aSLen Brown } 65561ef7d21aSLen Brown 65571ef7d21aSLen Brown if (*next == '-') { 65581ef7d21aSLen Brown next += 1; /* start range */ 65591ef7d21aSLen Brown } else if (*next == '.') { 65601ef7d21aSLen Brown next += 1; 65611ef7d21aSLen Brown if (*next == '.') 65621ef7d21aSLen Brown next += 1; /* start range */ 65631ef7d21aSLen Brown else 65641ef7d21aSLen Brown goto error; 65651ef7d21aSLen Brown } 65661ef7d21aSLen Brown 65671ef7d21aSLen Brown end = strtoul(next, &next, 10); 65681ef7d21aSLen Brown if (end <= start) 65691ef7d21aSLen Brown goto error; 65701ef7d21aSLen Brown 65711ef7d21aSLen Brown while (++start <= end) { 65721ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 65731ef7d21aSLen Brown goto error; 65741ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 65751ef7d21aSLen Brown } 65761ef7d21aSLen Brown 65771ef7d21aSLen Brown if (*next == ',') 65781ef7d21aSLen Brown next += 1; 65791ef7d21aSLen Brown else if (*next != '\0') 65801ef7d21aSLen Brown goto error; 65811ef7d21aSLen Brown } 65821ef7d21aSLen Brown 65831ef7d21aSLen Brown return; 65841ef7d21aSLen Brown 65851ef7d21aSLen Brown error: 65864e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 65874e4e1e7cSLen Brown help(); 65881ef7d21aSLen Brown exit(-1); 65891ef7d21aSLen Brown } 65901ef7d21aSLen Brown 6591103a8feaSLen Brown void cmdline(int argc, char **argv) 6592103a8feaSLen Brown { 6593103a8feaSLen Brown int opt; 6594d8af6f5fSLen Brown int option_index = 0; 6595d8af6f5fSLen Brown static struct option long_options[] = { 6596388e9c81SLen Brown { "add", required_argument, 0, 'a' }, 65971ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' }, 6598d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' }, 659996e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 66003f44a5c6SLen Brown { "enable", required_argument, 0, 'e' }, 6601d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' }, 66022af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' }, 6603023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' }, 6604c7e399f8SZephaniah E. Loss-Cutler-Hull { "header_iterations", required_argument, 0, 'N' }, 6605d8af6f5fSLen Brown { "help", no_argument, 0, 'h' }, 6606812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 6607d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' }, 6608c8ade361SLen Brown { "list", no_argument, 0, 'l' }, 6609b7d8c148SLen Brown { "out", required_argument, 0, 'o' }, 661096e47158SLen Brown { "quiet", no_argument, 0, 'q' }, 6611812db3f7SLen Brown { "show", required_argument, 0, 's' }, 6612d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' }, 6613d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' }, 6614d8af6f5fSLen Brown { "version", no_argument, 0, 'v' }, 6615d8af6f5fSLen Brown { 0, 0, 0, 0 } 6616d8af6f5fSLen Brown }; 6617103a8feaSLen Brown 6618103a8feaSLen Brown progname = argv[0]; 6619103a8feaSLen Brown 66201b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) { 6621103a8feaSLen Brown switch (opt) { 6622388e9c81SLen Brown case 'a': 6623388e9c81SLen Brown parse_add_command(optarg); 6624388e9c81SLen Brown break; 66251ef7d21aSLen Brown case 'c': 66261ef7d21aSLen Brown parse_cpu_command(optarg); 66271ef7d21aSLen Brown break; 6628d8af6f5fSLen Brown case 'D': 6629d8af6f5fSLen Brown dump_only++; 66308e180f3cSLen Brown break; 66313f44a5c6SLen Brown case 'e': 66323f44a5c6SLen Brown /* --enable specified counter */ 66334c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 66343f44a5c6SLen Brown break; 6635d8af6f5fSLen Brown case 'd': 6636d8af6f5fSLen Brown debug++; 66373f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 66382f32edf1SLen Brown break; 6639812db3f7SLen Brown case 'H': 66403f44a5c6SLen Brown /* 66413f44a5c6SLen Brown * --hide: do not show those specified 66423f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 66433f44a5c6SLen Brown */ 66443f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6645812db3f7SLen Brown break; 6646d8af6f5fSLen Brown case 'h': 6647d8af6f5fSLen Brown default: 6648d8af6f5fSLen Brown help(); 6649d8af6f5fSLen Brown exit(1); 6650d8af6f5fSLen Brown case 'i': 66512a0609c0SLen Brown { 66522a0609c0SLen Brown double interval = strtod(optarg, NULL); 66532a0609c0SLen Brown 66542a0609c0SLen Brown if (interval < 0.001) { 66551b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval); 66562a0609c0SLen Brown exit(2); 66572a0609c0SLen Brown } 66582a0609c0SLen Brown 665947936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6660b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 666147936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 66622a0609c0SLen Brown } 6663889facbeSLen Brown break; 66645c56be9aSDirk Brandewie case 'J': 66655c56be9aSDirk Brandewie rapl_joules++; 66665c56be9aSDirk Brandewie break; 6667c8ade361SLen Brown case 'l': 66683f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6669c8ade361SLen Brown list_header_only++; 6670c8ade361SLen Brown quiet++; 6671c8ade361SLen Brown break; 6672b7d8c148SLen Brown case 'o': 6673b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6674b7d8c148SLen Brown break; 667596e47158SLen Brown case 'q': 667696e47158SLen Brown quiet = 1; 667796e47158SLen Brown break; 6678023fe0acSChen Yu case 'n': 6679023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6680023fe0acSChen Yu 6681023fe0acSChen Yu if (num_iterations <= 0) { 66821b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations); 6683023fe0acSChen Yu exit(2); 6684023fe0acSChen Yu } 6685023fe0acSChen Yu break; 6686c7e399f8SZephaniah E. Loss-Cutler-Hull case 'N': 6687c7e399f8SZephaniah E. Loss-Cutler-Hull header_iterations = strtod(optarg, NULL); 6688c7e399f8SZephaniah E. Loss-Cutler-Hull 6689c7e399f8SZephaniah E. Loss-Cutler-Hull if (header_iterations <= 0) { 6690164d7a96SLen Brown fprintf(outf, "iterations %d should be positive number\n", header_iterations); 6691c7e399f8SZephaniah E. Loss-Cutler-Hull exit(2); 6692c7e399f8SZephaniah E. Loss-Cutler-Hull } 6693c7e399f8SZephaniah E. Loss-Cutler-Hull break; 6694812db3f7SLen Brown case 's': 66953f44a5c6SLen Brown /* 66963f44a5c6SLen Brown * --show: show only those specified 66973f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 66983f44a5c6SLen Brown * subsequent invocations can add to it. 66993f44a5c6SLen Brown */ 67003f44a5c6SLen Brown if (shown == 0) 67013f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 67023f44a5c6SLen Brown else 67033f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 67043f44a5c6SLen Brown shown = 1; 6705812db3f7SLen Brown break; 6706d8af6f5fSLen Brown case 'S': 6707d8af6f5fSLen Brown summary_only++; 6708d8af6f5fSLen Brown break; 6709d8af6f5fSLen Brown case 'T': 671055279aefSZhang Rui tj_max_override = atoi(optarg); 6711d8af6f5fSLen Brown break; 6712d8af6f5fSLen Brown case 'v': 6713d8af6f5fSLen Brown print_version(); 6714d8af6f5fSLen Brown exit(0); 6715d8af6f5fSLen Brown break; 6716103a8feaSLen Brown } 6717103a8feaSLen Brown } 6718103a8feaSLen Brown } 6719103a8feaSLen Brown 6720103a8feaSLen Brown int main(int argc, char **argv) 6721103a8feaSLen Brown { 6722b7d8c148SLen Brown outf = stderr; 6723103a8feaSLen Brown cmdline(argc, argv); 6724103a8feaSLen Brown 672503331233SChen Yu if (!quiet) { 6726d8af6f5fSLen Brown print_version(); 672703331233SChen Yu print_bootcmd(); 672803331233SChen Yu } 6729103a8feaSLen Brown 673041618e63SLen Brown probe_sysfs(); 673141618e63SLen Brown 6732103a8feaSLen Brown turbostat_init(); 6733103a8feaSLen Brown 67346799ba84SDan Merillat msr_sum_record(); 67356799ba84SDan Merillat 67363b4d5c7fSAndy Shevchenko /* dump counters and exit */ 67373b4d5c7fSAndy Shevchenko if (dump_only) 67383b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 67393b4d5c7fSAndy Shevchenko 6740c8ade361SLen Brown /* list header and exit */ 6741c8ade361SLen Brown if (list_header_only) { 6742c8ade361SLen Brown print_header(","); 6743c8ade361SLen Brown flush_output_stdout(); 6744c8ade361SLen Brown return 0; 6745c8ade361SLen Brown } 6746c8ade361SLen Brown 6747103a8feaSLen Brown /* 6748103a8feaSLen Brown * if any params left, it must be a command to fork 6749103a8feaSLen Brown */ 6750103a8feaSLen Brown if (argc - optind) 6751103a8feaSLen Brown return fork_it(argv + optind); 6752103a8feaSLen Brown else 6753103a8feaSLen Brown turbostat_loop(); 6754103a8feaSLen Brown 6755103a8feaSLen Brown return 0; 6756103a8feaSLen Brown } 6757