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 }, 129164d7a96SLen Brown }; 130164d7a96SLen Brown 131164d7a96SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 132164d7a96SLen Brown #define BIC_USEC (1ULL << 0) 133164d7a96SLen Brown #define BIC_TOD (1ULL << 1) 134164d7a96SLen Brown #define BIC_Package (1ULL << 2) 135164d7a96SLen Brown #define BIC_Node (1ULL << 3) 136164d7a96SLen Brown #define BIC_Avg_MHz (1ULL << 4) 137164d7a96SLen Brown #define BIC_Busy (1ULL << 5) 138164d7a96SLen Brown #define BIC_Bzy_MHz (1ULL << 6) 139164d7a96SLen Brown #define BIC_TSC_MHz (1ULL << 7) 140164d7a96SLen Brown #define BIC_IRQ (1ULL << 8) 141164d7a96SLen Brown #define BIC_SMI (1ULL << 9) 142164d7a96SLen Brown #define BIC_sysfs (1ULL << 10) 143164d7a96SLen Brown #define BIC_CPU_c1 (1ULL << 11) 144164d7a96SLen Brown #define BIC_CPU_c3 (1ULL << 12) 145164d7a96SLen Brown #define BIC_CPU_c6 (1ULL << 13) 146164d7a96SLen Brown #define BIC_CPU_c7 (1ULL << 14) 147164d7a96SLen Brown #define BIC_ThreadC (1ULL << 15) 148164d7a96SLen Brown #define BIC_CoreTmp (1ULL << 16) 149164d7a96SLen Brown #define BIC_CoreCnt (1ULL << 17) 150164d7a96SLen Brown #define BIC_PkgTmp (1ULL << 18) 151164d7a96SLen Brown #define BIC_GFX_rc6 (1ULL << 19) 152164d7a96SLen Brown #define BIC_GFXMHz (1ULL << 20) 153164d7a96SLen Brown #define BIC_Pkgpc2 (1ULL << 21) 154164d7a96SLen Brown #define BIC_Pkgpc3 (1ULL << 22) 155164d7a96SLen Brown #define BIC_Pkgpc6 (1ULL << 23) 156164d7a96SLen Brown #define BIC_Pkgpc7 (1ULL << 24) 157164d7a96SLen Brown #define BIC_Pkgpc8 (1ULL << 25) 158164d7a96SLen Brown #define BIC_Pkgpc9 (1ULL << 26) 159164d7a96SLen Brown #define BIC_Pkgpc10 (1ULL << 27) 160164d7a96SLen Brown #define BIC_CPU_LPI (1ULL << 28) 161164d7a96SLen Brown #define BIC_SYS_LPI (1ULL << 29) 162164d7a96SLen Brown #define BIC_PkgWatt (1ULL << 30) 163164d7a96SLen Brown #define BIC_CorWatt (1ULL << 31) 164164d7a96SLen Brown #define BIC_GFXWatt (1ULL << 32) 165164d7a96SLen Brown #define BIC_PkgCnt (1ULL << 33) 166164d7a96SLen Brown #define BIC_RAMWatt (1ULL << 34) 167164d7a96SLen Brown #define BIC_PKG__ (1ULL << 35) 168164d7a96SLen Brown #define BIC_RAM__ (1ULL << 36) 169164d7a96SLen Brown #define BIC_Pkg_J (1ULL << 37) 170164d7a96SLen Brown #define BIC_Cor_J (1ULL << 38) 171164d7a96SLen Brown #define BIC_GFX_J (1ULL << 39) 172164d7a96SLen Brown #define BIC_RAM_J (1ULL << 40) 173164d7a96SLen Brown #define BIC_Mod_c6 (1ULL << 41) 174164d7a96SLen Brown #define BIC_Totl_c0 (1ULL << 42) 175164d7a96SLen Brown #define BIC_Any_c0 (1ULL << 43) 176164d7a96SLen Brown #define BIC_GFX_c0 (1ULL << 44) 177164d7a96SLen Brown #define BIC_CPUGFX (1ULL << 45) 178164d7a96SLen Brown #define BIC_Core (1ULL << 46) 179164d7a96SLen Brown #define BIC_CPU (1ULL << 47) 180164d7a96SLen Brown #define BIC_APIC (1ULL << 48) 181164d7a96SLen Brown #define BIC_X2APIC (1ULL << 49) 182164d7a96SLen Brown #define BIC_Die (1ULL << 50) 183164d7a96SLen Brown #define BIC_GFXACTMHz (1ULL << 51) 184164d7a96SLen Brown #define BIC_IPC (1ULL << 52) 185164d7a96SLen Brown #define BIC_CORE_THROT_CNT (1ULL << 53) 186164d7a96SLen Brown 187164d7a96SLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die ) 188164d7a96SLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__) 189164d7a96SLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz ) 190164d7a96SLen 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) 191164d7a96SLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC) 192164d7a96SLen Brown 193164d7a96SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 194164d7a96SLen Brown 195164d7a96SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 196164d7a96SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 197164d7a96SLen Brown 198164d7a96SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 199164d7a96SLen Brown #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 200164d7a96SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 201164d7a96SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 202164d7a96SLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 203164d7a96SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 204164d7a96SLen Brown 205103a8feaSLen Brown char *proc_stat = "/proc/stat"; 206b7d8c148SLen Brown FILE *outf; 20736229897SLen Brown int *fd_percpu; 2082af4f9b8SLen Brown int *fd_instr_count_percpu; 209b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 }; 21047936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 }; 211e9d3092fSZhang Rui 212e9d3092fSZhang Rui /* Save original CPU model */ 213e9d3092fSZhang Rui unsigned int model_orig; 214e9d3092fSZhang Rui 215023fe0acSChen Yu unsigned int num_iterations; 216c7e399f8SZephaniah E. Loss-Cutler-Hull unsigned int header_iterations; 217d8af6f5fSLen Brown unsigned int debug; 21896e47158SLen Brown unsigned int quiet; 2193f44a5c6SLen Brown unsigned int shown; 2200de6c0dfSLen Brown unsigned int sums_need_wide_columns; 221d8af6f5fSLen Brown unsigned int rapl_joules; 222d8af6f5fSLen Brown unsigned int summary_only; 223c8ade361SLen Brown unsigned int list_header_only; 224d8af6f5fSLen Brown unsigned int dump_only; 225103a8feaSLen Brown unsigned int do_snb_cstates; 226fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 227144b44b1SLen Brown unsigned int do_slm_cstates; 228144b44b1SLen Brown unsigned int use_c1_residency_msr; 229103a8feaSLen Brown unsigned int has_aperf; 230889facbeSLen Brown unsigned int has_epb; 2315a63426eSLen Brown unsigned int do_irtl_snb; 2325a63426eSLen Brown unsigned int do_irtl_hsw; 233fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 234103a8feaSLen Brown unsigned int genuine_intel; 23534041551SLen Brown unsigned int authentic_amd; 236c1c10cc7SPu Wen unsigned int hygon_genuine; 23734041551SLen Brown unsigned int max_level, max_extended_level; 238103a8feaSLen Brown unsigned int has_invariant_tsc; 239d7899447SLen Brown unsigned int do_nhm_platform_info; 240cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 241b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 242103a8feaSLen Brown double bclk; 243a2b7b749SLen Brown double base_hz; 24421ed5574SLen Brown unsigned int has_base_hz; 245a2b7b749SLen Brown double tsc_tweak = 1.0; 246c98d5d94SLen Brown unsigned int show_pkg_only; 247c98d5d94SLen Brown unsigned int show_core_only; 248c98d5d94SLen Brown char *output_buffer, *outp; 249889facbeSLen Brown unsigned int do_rapl; 250889facbeSLen Brown unsigned int do_dts; 251889facbeSLen Brown unsigned int do_ptm; 2522af4f9b8SLen Brown unsigned int do_ipc; 253fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 254be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 255be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 25627d47356SLen Brown unsigned int gfx_cur_mhz; 257b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 25855279aefSZhang Rui unsigned int tj_max; 25955279aefSZhang Rui unsigned int tj_max_override; 2600b9a0b9bSZhang Rui int tcc_offset_bits; 26140ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 26240ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 263889facbeSLen Brown double rapl_joule_counter_range; 2643a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 265ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 266aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 2673a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 2683a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 2698a5bdf41SLen Brown unsigned int crystal_hz; 2708a5bdf41SLen Brown unsigned long long tsc_hz; 2717ce7d5deSPrarit Bhargava int base_cpu; 27221ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 2737f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 2747f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 2757f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 2767f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 2777f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 2787f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 27933148d67SLen Brown unsigned int has_misc_feature_control; 2804c2122d4SLen Brown unsigned int first_counter_read = 1; 281c026c236SArtem Bityutskiy int ignore_stdin; 282889facbeSLen Brown 283889facbeSLen Brown #define RAPL_PKG (1 << 0) 284e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 285e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 286e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 287e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 288e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 289e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 290e6f9bb3cSLen Brown 291889facbeSLen Brown #define RAPL_DRAM (1 << 3) 292e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 293e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 294e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 295e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 2960b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 2970b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 298e6f9bb3cSLen Brown 2999148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 300e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 3010b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 302e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 303e6f9bb3cSLen Brown 3040b2bb692SLen Brown #define RAPL_GFX (1 << 8) 305e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 306e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 307e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 3089148494cSJacob Pan 3099148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 3109148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 3119392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 3129392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 3139392bd98SCalvin Walton * not per-package. */ 3149392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 3159392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 3169392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 3179392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 3189148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 319889facbeSLen Brown #define TJMAX_DEFAULT 100 320889facbeSLen Brown 3219392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 3229392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 3239392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 3249392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 3259392bd98SCalvin Walton 326889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 327103a8feaSLen Brown 328103a8feaSLen Brown int backwards_count; 329103a8feaSLen Brown char *progname; 330103a8feaSLen Brown 3311ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 3321ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 3331ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 3340748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 3350748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 3360e2d8f05SLen Brown #define BITMASK_SIZE 32 337c98d5d94SLen Brown 338c98d5d94SLen Brown struct thread_data { 339f4fdf2b4SLen Brown struct timeval tv_begin; 340f4fdf2b4SLen Brown struct timeval tv_end; 341d4794f25SYazen Ghannam struct timeval tv_delta; 342c98d5d94SLen Brown unsigned long long tsc; 343c98d5d94SLen Brown unsigned long long aperf; 344c98d5d94SLen Brown unsigned long long mperf; 345144b44b1SLen Brown unsigned long long c1; 3462af4f9b8SLen Brown unsigned long long instr_count; 3470de6c0dfSLen Brown unsigned long long irq_count; 3481ed51011SLen Brown unsigned int smi_count; 349c98d5d94SLen Brown unsigned int cpu_id; 3504c2122d4SLen Brown unsigned int apic_id; 3514c2122d4SLen Brown unsigned int x2apic_id; 352c98d5d94SLen Brown unsigned int flags; 3537ab5ff49SZhang Rui bool is_atom; 354c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 355c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 3560748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 357c98d5d94SLen Brown } *thread_even, *thread_odd; 358c98d5d94SLen Brown 359c98d5d94SLen Brown struct core_data { 360c98d5d94SLen Brown unsigned long long c3; 361c98d5d94SLen Brown unsigned long long c6; 362c98d5d94SLen Brown unsigned long long c7; 3630539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 364889facbeSLen Brown unsigned int core_temp_c; 3659392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 366c98d5d94SLen Brown unsigned int core_id; 367eae97e05SChen Yu unsigned long long core_throt_cnt; 368678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 369c98d5d94SLen Brown } *core_even, *core_odd; 370c98d5d94SLen Brown 371c98d5d94SLen Brown struct pkg_data { 372c98d5d94SLen Brown unsigned long long pc2; 373c98d5d94SLen Brown unsigned long long pc3; 374c98d5d94SLen Brown unsigned long long pc6; 375c98d5d94SLen Brown unsigned long long pc7; 376ca58710fSKristen Carlson Accardi unsigned long long pc8; 377ca58710fSKristen Carlson Accardi unsigned long long pc9; 378ca58710fSKristen Carlson Accardi unsigned long long pc10; 379be0e54c4SLen Brown unsigned long long cpu_lpi; 380be0e54c4SLen Brown unsigned long long sys_lpi; 3810b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 3820b2bb692SLen Brown unsigned long long pkg_any_core_c0; 3830b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 3840b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 3859185e988SLen Brown long long gfx_rc6_ms; 38627d47356SLen Brown unsigned int gfx_mhz; 387b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 388c98d5d94SLen Brown unsigned int package_id; 3897c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 3907c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 3917c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 3927c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 3937c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 3947c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 395889facbeSLen Brown unsigned int pkg_temp_c; 396678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 397c98d5d94SLen Brown } *package_even, *package_odd; 398c98d5d94SLen Brown 399c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 400c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 401c98d5d94SLen Brown 40240f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 40340f5cfe7SPrarit Bhargava ((thread_base) + \ 40440f5cfe7SPrarit Bhargava ((pkg_no) * \ 40540f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 40640f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 40740f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 40840f5cfe7SPrarit Bhargava (thread_no)) 40940f5cfe7SPrarit Bhargava 41040f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 41140f5cfe7SPrarit Bhargava ((core_base) + \ 41240f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 41340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 41440f5cfe7SPrarit Bhargava (core_no)) 41540f5cfe7SPrarit Bhargava 416c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 417c98d5d94SLen Brown 41887e15da9SChen Yu /* 41987e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 42087e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 42187e15da9SChen Yu * despite its register's bit width. 42287e15da9SChen Yu */ 42387e15da9SChen Yu enum { 42487e15da9SChen Yu IDX_PKG_ENERGY, 42587e15da9SChen Yu IDX_DRAM_ENERGY, 42687e15da9SChen Yu IDX_PP0_ENERGY, 42787e15da9SChen Yu IDX_PP1_ENERGY, 42887e15da9SChen Yu IDX_PKG_PERF, 42987e15da9SChen Yu IDX_DRAM_PERF, 43087e15da9SChen Yu IDX_COUNT, 43187e15da9SChen Yu }; 43287e15da9SChen Yu 43387e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 43487e15da9SChen Yu 43587e15da9SChen Yu struct msr_sum_array { 43687e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 43787e15da9SChen Yu struct { 43887e15da9SChen Yu /*The accumulated MSR value is updated by the timer */ 43987e15da9SChen Yu unsigned long long sum; 44087e15da9SChen Yu /*The MSR footprint recorded in last timer */ 44187e15da9SChen Yu unsigned long long last; 44287e15da9SChen Yu } entries[IDX_COUNT]; 44387e15da9SChen Yu }; 44487e15da9SChen Yu 44587e15da9SChen Yu /* The percpu MSR sum array.*/ 44687e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 44787e15da9SChen Yu 44813a779deSCalvin Walton off_t idx_to_offset(int idx) 44987e15da9SChen Yu { 45013a779deSCalvin Walton off_t offset; 45187e15da9SChen Yu 45287e15da9SChen Yu switch (idx) { 45387e15da9SChen Yu case IDX_PKG_ENERGY: 454301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H) 455301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT; 456301b1d3aSBas Nieuwenhuizen else 45787e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 45887e15da9SChen Yu break; 45987e15da9SChen Yu case IDX_DRAM_ENERGY: 46087e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 46187e15da9SChen Yu break; 46287e15da9SChen Yu case IDX_PP0_ENERGY: 46387e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 46487e15da9SChen Yu break; 46587e15da9SChen Yu case IDX_PP1_ENERGY: 46687e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 46787e15da9SChen Yu break; 46887e15da9SChen Yu case IDX_PKG_PERF: 46987e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 47087e15da9SChen Yu break; 47187e15da9SChen Yu case IDX_DRAM_PERF: 47287e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 47387e15da9SChen Yu break; 47487e15da9SChen Yu default: 47587e15da9SChen Yu offset = -1; 47687e15da9SChen Yu } 47787e15da9SChen Yu return offset; 47887e15da9SChen Yu } 47987e15da9SChen Yu 48013a779deSCalvin Walton int offset_to_idx(off_t offset) 48187e15da9SChen Yu { 48287e15da9SChen Yu int idx; 48387e15da9SChen Yu 48487e15da9SChen Yu switch (offset) { 48587e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 486301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT: 48787e15da9SChen Yu idx = IDX_PKG_ENERGY; 48887e15da9SChen Yu break; 48987e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 49087e15da9SChen Yu idx = IDX_DRAM_ENERGY; 49187e15da9SChen Yu break; 49287e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 49387e15da9SChen Yu idx = IDX_PP0_ENERGY; 49487e15da9SChen Yu break; 49587e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 49687e15da9SChen Yu idx = IDX_PP1_ENERGY; 49787e15da9SChen Yu break; 49887e15da9SChen Yu case MSR_PKG_PERF_STATUS: 49987e15da9SChen Yu idx = IDX_PKG_PERF; 50087e15da9SChen Yu break; 50187e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 50287e15da9SChen Yu idx = IDX_DRAM_PERF; 50387e15da9SChen Yu break; 50487e15da9SChen Yu default: 50587e15da9SChen Yu idx = -1; 50687e15da9SChen Yu } 50787e15da9SChen Yu return idx; 50887e15da9SChen Yu } 50987e15da9SChen Yu 51087e15da9SChen Yu int idx_valid(int idx) 51187e15da9SChen Yu { 51287e15da9SChen Yu switch (idx) { 51387e15da9SChen Yu case IDX_PKG_ENERGY: 514301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H); 51587e15da9SChen Yu case IDX_DRAM_ENERGY: 51687e15da9SChen Yu return do_rapl & RAPL_DRAM; 51787e15da9SChen Yu case IDX_PP0_ENERGY: 51887e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 51987e15da9SChen Yu case IDX_PP1_ENERGY: 52087e15da9SChen Yu return do_rapl & RAPL_GFX; 52187e15da9SChen Yu case IDX_PKG_PERF: 52287e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 52387e15da9SChen Yu case IDX_DRAM_PERF: 52487e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 52587e15da9SChen Yu default: 52687e15da9SChen Yu return 0; 52787e15da9SChen Yu } 52887e15da9SChen Yu } 5291b439f01SLen Brown 530388e9c81SLen Brown struct sys_counters { 531678a3bd1SLen Brown unsigned int added_thread_counters; 532678a3bd1SLen Brown unsigned int added_core_counters; 533678a3bd1SLen Brown unsigned int added_package_counters; 534388e9c81SLen Brown struct msr_counter *tp; 535388e9c81SLen Brown struct msr_counter *cp; 536388e9c81SLen Brown struct msr_counter *pp; 537388e9c81SLen Brown } sys; 538388e9c81SLen Brown 539c98d5d94SLen Brown struct system_summary { 540c98d5d94SLen Brown struct thread_data threads; 541c98d5d94SLen Brown struct core_data cores; 542c98d5d94SLen Brown struct pkg_data packages; 543388e9c81SLen Brown } average; 544c98d5d94SLen Brown 5450e2d8f05SLen Brown struct cpu_topology { 5460e2d8f05SLen Brown int physical_package_id; 5476de68fe1SLen Brown int die_id; 5480e2d8f05SLen Brown int logical_cpu_id; 549ef605741SPrarit Bhargava int physical_node_id; 550ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 5510e2d8f05SLen Brown int physical_core_id; 5528cb48b32SPrarit Bhargava int thread_id; 5530e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 5540e2d8f05SLen Brown } *cpus; 555c98d5d94SLen Brown 556c98d5d94SLen Brown struct topo_params { 557c98d5d94SLen Brown int num_packages; 5586de68fe1SLen Brown int num_die; 559103a8feaSLen Brown int num_cpus; 560c98d5d94SLen Brown int num_cores; 561c98d5d94SLen Brown int max_cpu_num; 562ef605741SPrarit Bhargava int max_node_num; 56370a9c6e8SPrarit Bhargava int nodes_per_pkg; 56470a9c6e8SPrarit Bhargava int cores_per_node; 56570a9c6e8SPrarit Bhargava int threads_per_core; 566c98d5d94SLen Brown } topo; 567103a8feaSLen Brown 568c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 569103a8feaSLen Brown 570562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 571562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 572562a2d37SLen Brown 573c98d5d94SLen Brown void setup_all_buffers(void); 574103a8feaSLen Brown 5751f81c5efSLen Brown char *sys_lpi_file; 5761f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 5771f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 5781f81c5efSLen Brown 579c98d5d94SLen Brown int cpu_is_not_present(int cpu) 580d15cf7c1SLen Brown { 581c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 582c98d5d94SLen Brown } 5831b439f01SLen Brown 584c98d5d94SLen Brown /* 585c98d5d94SLen Brown * run func(thread, core, package) in topology order 586c98d5d94SLen Brown * skip non-present cpus 587c98d5d94SLen Brown */ 588c98d5d94SLen Brown 589c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), 590c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 591c98d5d94SLen Brown { 59240f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 593c98d5d94SLen Brown 594c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 595df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 59670a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 5971b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 598c98d5d94SLen Brown struct thread_data *t; 599c98d5d94SLen Brown struct core_data *c; 600c98d5d94SLen Brown struct pkg_data *p; 601c98d5d94SLen Brown 6021b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 603c98d5d94SLen Brown 604c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 605c98d5d94SLen Brown continue; 606c98d5d94SLen Brown 6071b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 608c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 609c98d5d94SLen Brown 610c98d5d94SLen Brown retval = func(t, c, p); 611c98d5d94SLen Brown if (retval) 612c98d5d94SLen Brown return retval; 613c98d5d94SLen Brown } 614c98d5d94SLen Brown } 615c98d5d94SLen Brown } 61640f5cfe7SPrarit Bhargava } 617d15cf7c1SLen Brown return 0; 618d15cf7c1SLen Brown } 619d15cf7c1SLen Brown 62088c3281fSLen Brown int cpu_migrate(int cpu) 62188c3281fSLen Brown { 622c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 623c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 624c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 62588c3281fSLen Brown return -1; 62688c3281fSLen Brown else 62788c3281fSLen Brown return 0; 62888c3281fSLen Brown } 6291b439f01SLen Brown 63036229897SLen Brown int get_msr_fd(int cpu) 631103a8feaSLen Brown { 632103a8feaSLen Brown char pathname[32]; 633103a8feaSLen Brown int fd; 634103a8feaSLen Brown 63536229897SLen Brown fd = fd_percpu[cpu]; 63636229897SLen Brown 63736229897SLen Brown if (fd) 63836229897SLen Brown return fd; 63936229897SLen Brown 640103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 641103a8feaSLen Brown fd = open(pathname, O_RDONLY); 64215aaa346SLen Brown if (fd < 0) 64398481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 644103a8feaSLen Brown 64536229897SLen Brown fd_percpu[cpu] = fd; 64636229897SLen Brown 64736229897SLen Brown return fd; 64836229897SLen Brown } 64936229897SLen Brown 6502af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 6512af4f9b8SLen Brown { 6522af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 6532af4f9b8SLen Brown } 6542af4f9b8SLen Brown 6552af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 6562af4f9b8SLen Brown { 6572af4f9b8SLen Brown struct perf_event_attr pea; 6582af4f9b8SLen Brown int fd; 6592af4f9b8SLen Brown 6602af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 6612af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 6622af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 6632af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 6642af4f9b8SLen Brown 6652af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 6662af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 667164d7a96SLen Brown if (fd == -1) { 668164d7a96SLen Brown warn("cpu%d: perf instruction counter", cpu_num); 669164d7a96SLen Brown BIC_NOT_PRESENT(BIC_IPC); 670164d7a96SLen Brown } 6712af4f9b8SLen Brown 6722af4f9b8SLen Brown return fd; 6732af4f9b8SLen Brown } 6742af4f9b8SLen Brown 6752af4f9b8SLen Brown int get_instr_count_fd(int cpu) 6762af4f9b8SLen Brown { 6772af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 6782af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 6792af4f9b8SLen Brown 6802af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 6812af4f9b8SLen Brown 6822af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 6832af4f9b8SLen Brown } 6842af4f9b8SLen Brown 68536229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 68636229897SLen Brown { 68736229897SLen Brown ssize_t retval; 68836229897SLen Brown 68936229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 69015aaa346SLen Brown 69198481e79SLen Brown if (retval != sizeof *msr) 692cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 69315aaa346SLen Brown 69415aaa346SLen Brown return 0; 695103a8feaSLen Brown } 696103a8feaSLen Brown 697dd778a5eSLen Brown #define MAX_DEFERRED 16 6980fc521bcSZephaniah E. Loss-Cutler-Hull char *deferred_add_names[MAX_DEFERRED]; 699dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 7000fc521bcSZephaniah E. Loss-Cutler-Hull int deferred_add_index; 701dd778a5eSLen Brown int deferred_skip_index; 702dd778a5eSLen Brown 703dd778a5eSLen Brown /* 704dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 705dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 706dd778a5eSLen Brown */ 707dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 708dd778a5eSLen Brown 709dd778a5eSLen Brown void help(void) 710dd778a5eSLen Brown { 711dd778a5eSLen Brown fprintf(outf, 712dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 713dd778a5eSLen Brown "\n" 714dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 715dd778a5eSLen Brown "when COMMAND completes.\n" 716dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 717dd778a5eSLen Brown "to print statistics, until interrupted.\n" 718cc481650SNathan Ciobanu " -a, --add add a counter\n" 719dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 720cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 721dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7229ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7239ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7249ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7259ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7269ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7279ce80578SNathan Ciobanu " hide the specified column(s)\n" 728cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 729cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7309ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 731cc481650SNathan Ciobanu " -l, --list list column headers only\n" 732cc481650SNathan Ciobanu " -n, --num_iterations num\n" 733cc481650SNathan Ciobanu " number of the measurement iterations\n" 734c7e399f8SZephaniah E. Loss-Cutler-Hull " -N, --header_iterations num\n" 735c7e399f8SZephaniah E. Loss-Cutler-Hull " print header every num iterations\n" 736cc481650SNathan Ciobanu " -o, --out file\n" 737cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 738cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7399ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7409ce80578SNathan Ciobanu " show only the specified column(s)\n" 7419ce80578SNathan Ciobanu " -S, --Summary\n" 7429ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7439ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7449ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7459ce80578SNathan Ciobanu " degrees Celsius\n" 746cc481650SNathan Ciobanu " -h, --help print this help message\n" 7471b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n"); 748dd778a5eSLen Brown } 749dd778a5eSLen Brown 750812db3f7SLen Brown /* 751812db3f7SLen Brown * bic_lookup 752812db3f7SLen Brown * for all the strings in comma separate name_list, 753812db3f7SLen Brown * set the approprate bit in return value. 754812db3f7SLen Brown */ 755dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 756812db3f7SLen Brown { 7579878bf7aSLen Brown unsigned int i; 758812db3f7SLen Brown unsigned long long retval = 0; 759812db3f7SLen Brown 760812db3f7SLen Brown while (name_list) { 761812db3f7SLen Brown char *comma; 762812db3f7SLen Brown 763812db3f7SLen Brown comma = strchr(name_list, ','); 764812db3f7SLen Brown 765812db3f7SLen Brown if (comma) 766812db3f7SLen Brown *comma = '\0'; 767812db3f7SLen Brown 768812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 769812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 770812db3f7SLen Brown retval |= (1ULL << i); 771812db3f7SLen Brown break; 772812db3f7SLen Brown } 7735dc241f2SLen Brown if (!strcmp(name_list, "all")) { 7745dc241f2SLen Brown retval |= ~0; 7755dc241f2SLen Brown break; 7765dc241f2SLen Brown } else if (!strcmp(name_list, "topology")) { 7775dc241f2SLen Brown retval |= BIC_TOPOLOGY; 7785dc241f2SLen Brown break; 7795dc241f2SLen Brown } else if (!strcmp(name_list, "power")) { 7805dc241f2SLen Brown retval |= BIC_THERMAL_PWR; 7815dc241f2SLen Brown break; 7825dc241f2SLen Brown } else if (!strcmp(name_list, "idle")) { 7835dc241f2SLen Brown retval |= BIC_IDLE; 7845dc241f2SLen Brown break; 7855dc241f2SLen Brown } else if (!strcmp(name_list, "frequency")) { 7865dc241f2SLen Brown retval |= BIC_FREQUENCY; 7875dc241f2SLen Brown break; 7885dc241f2SLen Brown } else if (!strcmp(name_list, "other")) { 7895dc241f2SLen Brown retval |= BIC_OTHER; 7905dc241f2SLen Brown break; 7915dc241f2SLen Brown } 7925dc241f2SLen Brown 793812db3f7SLen Brown } 794812db3f7SLen Brown if (i == MAX_BIC) { 795dd778a5eSLen Brown if (mode == SHOW_LIST) { 7960fc521bcSZephaniah E. Loss-Cutler-Hull deferred_add_names[deferred_add_index++] = name_list; 7970fc521bcSZephaniah E. Loss-Cutler-Hull if (deferred_add_index >= MAX_DEFERRED) { 7980fc521bcSZephaniah E. Loss-Cutler-Hull fprintf(stderr, "More than max %d un-recognized --add options '%s'\n", 7990fc521bcSZephaniah E. Loss-Cutler-Hull MAX_DEFERRED, name_list); 8000fc521bcSZephaniah E. Loss-Cutler-Hull help(); 8010fc521bcSZephaniah E. Loss-Cutler-Hull exit(1); 802812db3f7SLen Brown } 8030fc521bcSZephaniah E. Loss-Cutler-Hull } else { 804dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 805dd778a5eSLen Brown if (debug) 806dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 807dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 808dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 809dd778a5eSLen Brown MAX_DEFERRED, name_list); 810dd778a5eSLen Brown help(); 811dd778a5eSLen Brown exit(1); 812dd778a5eSLen Brown } 813dd778a5eSLen Brown } 8140fc521bcSZephaniah E. Loss-Cutler-Hull } 815812db3f7SLen Brown 816812db3f7SLen Brown name_list = comma; 817812db3f7SLen Brown if (name_list) 818812db3f7SLen Brown name_list++; 819812db3f7SLen Brown 820812db3f7SLen Brown } 821812db3f7SLen Brown return retval; 822812db3f7SLen Brown } 823fc04cc67SLen Brown 824c8ade361SLen Brown void print_header(char *delim) 825103a8feaSLen Brown { 826388e9c81SLen Brown struct msr_counter *mp; 8276168c2e0SLen Brown int printed = 0; 828388e9c81SLen Brown 8293f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 8303f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 8313f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 8323f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 833812db3f7SLen Brown if (DO_BIC(BIC_Package)) 8346168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 8356de68fe1SLen Brown if (DO_BIC(BIC_Die)) 8366de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 83701235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 83801235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 839812db3f7SLen Brown if (DO_BIC(BIC_Core)) 8406168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 841812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8426168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8434c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8444c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8454c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8464c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 847812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8486168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 849812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8506168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 851812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8526168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 853812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8546168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8551cc21f7bSLen Brown 8562af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8572af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8582af4f9b8SLen Brown 8590de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8600de6c0dfSLen Brown if (sums_need_wide_columns) 8616168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8620de6c0dfSLen Brown else 8636168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8640de6c0dfSLen Brown } 8650de6c0dfSLen Brown 866812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8676168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8681cc21f7bSLen Brown 869388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 870dd778a5eSLen Brown 871388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 872388e9c81SLen Brown if (mp->width == 64) 873dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 874388e9c81SLen Brown else 875dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 876388e9c81SLen Brown } else { 8770de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 878dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8790de6c0dfSLen Brown else 880dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 881388e9c81SLen Brown } 882388e9c81SLen Brown } 883388e9c81SLen Brown 88441618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8856168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 886562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8876168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 888812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8896168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 890812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8916168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 892678a3bd1SLen Brown 8930539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8946168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 895678a3bd1SLen Brown 896812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8976168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 898388e9c81SLen Brown 899eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 900eae97e05SChen Yu outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : "")); 901eae97e05SChen Yu 9029392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 9039392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 9049392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 9059392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 9069392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 9079392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 9089392bd98SCalvin Walton } 9099392bd98SCalvin Walton 910388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 911388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 912388e9c81SLen Brown if (mp->width == 64) 913c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 914388e9c81SLen Brown else 915c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 916388e9c81SLen Brown } else { 9170de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9180de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9190de6c0dfSLen Brown else 920c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 921388e9c81SLen Brown } 922388e9c81SLen Brown } 923388e9c81SLen Brown 924812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 9256168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 926889facbeSLen Brown 927812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 9286168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 929fdf676e5SLen Brown 930812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 9316168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 93227d47356SLen Brown 933b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 934b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 935b4b91569SRafael Antognolli 936a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 9376168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 938a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 9396168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 940a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 9416168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 942a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 9436168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 9440b2bb692SLen Brown 9450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9466168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9486168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9506168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9510f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9526168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9530f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9546168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9566168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9586168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 959be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 960be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 961be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 962be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 963103a8feaSLen Brown 9645c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 965812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9666168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9679392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9686168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 969812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9706168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 971812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9726168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 973812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9746168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 975812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9766168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 977d7899447SLen Brown } else if (do_rapl && rapl_joules) { 978812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9796168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9809392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9816168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 982812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9836168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 984812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9856168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 986812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9876168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 988812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9896168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9905c56be9aSDirk Brandewie } 991388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 992388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 993388e9c81SLen Brown if (mp->width == 64) 994c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 995388e9c81SLen Brown else 996c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 997388e9c81SLen Brown } else { 9980de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9990de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 10000de6c0dfSLen Brown else 1001c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 1002388e9c81SLen Brown } 1003388e9c81SLen Brown } 1004388e9c81SLen Brown 1005c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1006103a8feaSLen Brown } 1007103a8feaSLen Brown 10081b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1009103a8feaSLen Brown { 1010388e9c81SLen Brown int i; 1011388e9c81SLen Brown struct msr_counter *mp; 1012388e9c81SLen Brown 10133b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 1014c98d5d94SLen Brown 1015c98d5d94SLen Brown if (t) { 10161b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 10173b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 10183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 10193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 10203b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 10216886fee4SLen Brown 10222af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 10232af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 10242af4f9b8SLen Brown 1025812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 10260de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 1027812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 1028218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 1029388e9c81SLen Brown 1030388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 10311b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]); 1032388e9c81SLen Brown } 1033103a8feaSLen Brown } 1034103a8feaSLen Brown 1035c98d5d94SLen Brown if (c) { 10363b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 10373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 10383b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 10393b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 10403b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 1041eae97e05SChen Yu outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt); 10429392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 1043388e9c81SLen Brown 1044388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 10451b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]); 1046388e9c81SLen Brown } 10470539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1048c98d5d94SLen Brown } 1049103a8feaSLen Brown 1050c98d5d94SLen Brown if (p) { 10513b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10520b2bb692SLen Brown 10530b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10540b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10550b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10560b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10570b2bb692SLen Brown 10583b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10610f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10630f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10643b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10653b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10663b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10673b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1068be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1069be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10707c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10717c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10727c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10737c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 10741b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status); 10751b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status); 10763b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1077388e9c81SLen Brown 1078388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 10791b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]); 1080388e9c81SLen Brown } 1081c98d5d94SLen Brown } 10823b4d5c7fSAndy Shevchenko 10833b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10843b4d5c7fSAndy Shevchenko 1085c98d5d94SLen Brown return 0; 1086103a8feaSLen Brown } 1087103a8feaSLen Brown 1088e23da037SLen Brown /* 1089e23da037SLen Brown * column formatting convention & formats 1090e23da037SLen Brown */ 10911b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1092103a8feaSLen Brown { 1093008d396eSLen Brown double interval_float, tsc; 1094fc04cc67SLen Brown char *fmt8; 1095388e9c81SLen Brown int i; 1096388e9c81SLen Brown struct msr_counter *mp; 10976168c2e0SLen Brown char *delim = "\t"; 10986168c2e0SLen Brown int printed = 0; 1099103a8feaSLen Brown 1100c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1101c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1102c98d5d94SLen Brown return 0; 1103c98d5d94SLen Brown 1104c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1105c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1106c98d5d94SLen Brown return 0; 1107c98d5d94SLen Brown 11081ef7d21aSLen Brown /*if not summary line and --cpu is used */ 11091b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 11101ef7d21aSLen Brown return 0; 11111ef7d21aSLen Brown 11123f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1113f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1114f4fdf2b4SLen Brown struct timeval tv; 1115f4fdf2b4SLen Brown 1116f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1117f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1118f4fdf2b4SLen Brown } 1119f4fdf2b4SLen Brown 11203f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 11213f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 11223f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 11233f44a5c6SLen Brown 1124d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 1125103a8feaSLen Brown 1126008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1127008d396eSLen Brown 1128c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1129c98d5d94SLen Brown if (t == &average.threads) { 1130812db3f7SLen Brown if (DO_BIC(BIC_Package)) 11316168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11326de68fe1SLen Brown if (DO_BIC(BIC_Die)) 11336de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 113401235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 113501235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1136812db3f7SLen Brown if (DO_BIC(BIC_Core)) 11376168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1138812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11396168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11404c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11414c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11424c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11434c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1144103a8feaSLen Brown } else { 1145812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1146c98d5d94SLen Brown if (p) 11476168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1148c98d5d94SLen Brown else 11496168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1150c98d5d94SLen Brown } 11516de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11526de68fe1SLen Brown if (c) 11536de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11546de68fe1SLen Brown else 11556de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11566de68fe1SLen Brown } 115701235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 115801235041SPrarit Bhargava if (t) 115901235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 11601b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 116101235041SPrarit Bhargava else 11621b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 116301235041SPrarit Bhargava } 1164812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1165c98d5d94SLen Brown if (c) 11666168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1167c98d5d94SLen Brown else 11686168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1169c98d5d94SLen Brown } 1170812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11716168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11724c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11734c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11744c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11754c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1176103a8feaSLen Brown } 1177fc04cc67SLen Brown 1178812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 11791b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 1180fc04cc67SLen Brown 1181812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11826168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc); 1183103a8feaSLen Brown 1184812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 118521ed5574SLen Brown if (has_base_hz) 11861b439f01SLen Brown outp += 11871b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 118821ed5574SLen Brown else 11896168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1190008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 119121ed5574SLen Brown } 1192103a8feaSLen Brown 1193812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 11946168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 1195103a8feaSLen Brown 11962af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 11972af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 11982af4f9b8SLen Brown 1199562a2d37SLen Brown /* IRQ */ 12000de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 12010de6c0dfSLen Brown if (sums_need_wide_columns) 12026168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 12030de6c0dfSLen Brown else 12046168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 12050de6c0dfSLen Brown } 1206562a2d37SLen Brown 12071cc21f7bSLen Brown /* SMI */ 1208812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 12096168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 12101cc21f7bSLen Brown 1211678a3bd1SLen Brown /* Added counters */ 1212678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1213678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1214678a3bd1SLen Brown if (mp->width == 32) 12151b439f01SLen Brown outp += 12161b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]); 1217678a3bd1SLen Brown else 12186168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1219678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 12200de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12216168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 12220de6c0dfSLen Brown else 12236168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1224678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 122541618e63SLen Brown if (mp->type == COUNTER_USEC) 12261b439f01SLen Brown outp += 12271b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12281b439f01SLen Brown t->counter[i] / interval_float / 10000); 122941618e63SLen Brown else 12306168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc); 1231678a3bd1SLen Brown } 1232678a3bd1SLen Brown } 1233678a3bd1SLen Brown 123441618e63SLen Brown /* C1 */ 123541618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 12366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc); 123741618e63SLen Brown 1238c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1239c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1240c98d5d94SLen Brown goto done; 1241c98d5d94SLen Brown 1242562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 12436168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc); 1244812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 12456168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc); 1246812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12476168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc); 1248c98d5d94SLen Brown 12490539ba11SLen Brown /* Mod%c6 */ 12500539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12516168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12520539ba11SLen Brown 1253812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12546168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1255889facbeSLen Brown 1256eae97e05SChen Yu /* Core throttle count */ 1257eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 1258eae97e05SChen Yu outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt); 1259eae97e05SChen Yu 1260388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1261388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1262388e9c81SLen Brown if (mp->width == 32) 12631b439f01SLen Brown outp += 12641b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]); 1265388e9c81SLen Brown else 12666168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1267388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12680de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12696168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12700de6c0dfSLen Brown else 12716168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1272388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12736168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc); 1274388e9c81SLen Brown } 1275388e9c81SLen Brown } 1276388e9c81SLen Brown 12779392bd98SCalvin Walton fmt8 = "%s%.2f"; 12789392bd98SCalvin Walton 12799392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12801b439f01SLen Brown outp += 12811b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12829392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12839392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12849392bd98SCalvin Walton 1285c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1286c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1287c98d5d94SLen Brown goto done; 1288c98d5d94SLen Brown 12890b2bb692SLen Brown /* PkgTmp */ 1290812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 12916168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1292889facbeSLen Brown 1293fdf676e5SLen Brown /* GFXrc6 */ 1294812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1295ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 12966168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 12979185e988SLen Brown } else { 12986168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12999185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 13009185e988SLen Brown } 13019185e988SLen Brown } 1302fdf676e5SLen Brown 130327d47356SLen Brown /* GFXMHz */ 1304812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 13056168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 130627d47356SLen Brown 1307b4b91569SRafael Antognolli /* GFXACTMHz */ 1308b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1309b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1310b4b91569SRafael Antognolli 13110b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1312a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13136168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc); 1314a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13156168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc); 1316a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13176168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc); 1318a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 13196168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc); 13200b2bb692SLen Brown 13210f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 13226168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc); 13230f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 13246168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc); 13250f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 13266168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc); 13270f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 13286168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc); 13290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 13306168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc); 13310f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 13326168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc); 13330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 13346168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc); 1335889facbeSLen Brown 1336be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 13371b439f01SLen Brown outp += 13381b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1339be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 13401b439f01SLen Brown outp += 13411b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1342be0e54c4SLen Brown 1343812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 13441b439f01SLen Brown outp += 13451b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 1346eae97e05SChen Yu 13479392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13481b439f01SLen Brown outp += 13491b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1350812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 13511b439f01SLen Brown outp += 13521b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1353812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 13541b439f01SLen Brown outp += 13551b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13561b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float); 1357812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13586168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13599392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13606168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1361812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13626168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1363812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13646168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1365812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 13661b439f01SLen Brown outp += 13671b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13681b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1369812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 13701b439f01SLen Brown outp += 13711b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13721b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1373812db3f7SLen Brown 1374388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1375388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1376388e9c81SLen Brown if (mp->width == 32) 13771b439f01SLen Brown outp += 13781b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]); 1379388e9c81SLen Brown else 13806168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1381388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 13820de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 13836168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 13840de6c0dfSLen Brown else 13856168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1386388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 13876168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc); 1388388e9c81SLen Brown } 1389388e9c81SLen Brown } 1390388e9c81SLen Brown 1391c98d5d94SLen Brown done: 139294d6ab4bSLen Brown if (*(outp - 1) != '\n') 1393c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1394c98d5d94SLen Brown 1395c98d5d94SLen Brown return 0; 1396103a8feaSLen Brown } 1397103a8feaSLen Brown 1398b7d8c148SLen Brown void flush_output_stdout(void) 1399103a8feaSLen Brown { 1400b7d8c148SLen Brown FILE *filep; 1401b7d8c148SLen Brown 1402b7d8c148SLen Brown if (outf == stderr) 1403b7d8c148SLen Brown filep = stdout; 1404b7d8c148SLen Brown else 1405b7d8c148SLen Brown filep = outf; 1406b7d8c148SLen Brown 1407b7d8c148SLen Brown fputs(output_buffer, filep); 1408b7d8c148SLen Brown fflush(filep); 1409b7d8c148SLen Brown 1410c98d5d94SLen Brown outp = output_buffer; 1411c98d5d94SLen Brown } 14121b439f01SLen Brown 1413b7d8c148SLen Brown void flush_output_stderr(void) 1414c98d5d94SLen Brown { 1415b7d8c148SLen Brown fputs(output_buffer, outf); 1416b7d8c148SLen Brown fflush(outf); 1417c98d5d94SLen Brown outp = output_buffer; 1418c98d5d94SLen Brown } 14191b439f01SLen Brown 1420c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1421c98d5d94SLen Brown { 1422c7e399f8SZephaniah E. Loss-Cutler-Hull static int count; 1423103a8feaSLen Brown 1424c7e399f8SZephaniah E. Loss-Cutler-Hull if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only) 1425c8ade361SLen Brown print_header("\t"); 1426103a8feaSLen Brown 14279d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1428103a8feaSLen Brown 1429c7e399f8SZephaniah E. Loss-Cutler-Hull count++; 1430e23da037SLen Brown 1431e23da037SLen Brown if (summary_only) 1432e23da037SLen Brown return; 1433e23da037SLen Brown 1434c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1435103a8feaSLen Brown } 1436103a8feaSLen Brown 1437889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 14387c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1439889facbeSLen Brown 14401b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old) 1441103a8feaSLen Brown { 1442388e9c81SLen Brown int i; 1443388e9c81SLen Brown struct msr_counter *mp; 14440b2bb692SLen Brown 1445a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14460b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1447a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14480b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1449a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14500b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1451a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14520b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1453a99d8730SLen Brown 1454c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 14550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1456c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 14570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1458c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 14590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1460c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1461ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1462ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1463ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1464be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1465be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1466889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1467889facbeSLen Brown 14689185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 14699185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 14709185e988SLen Brown old->gfx_rc6_ms = -1; 14719185e988SLen Brown else 1472fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 14739185e988SLen Brown 147427d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1475b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 147627d47356SLen Brown 147787e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 147887e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 147987e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 148087e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 148187e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 148287e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1483ba3dec99SLen Brown 1484388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1485388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1486388e9c81SLen Brown old->counter[i] = new->counter[i]; 1487388e9c81SLen Brown else 1488388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1489388e9c81SLen Brown } 1490388e9c81SLen Brown 1491ba3dec99SLen Brown return 0; 1492103a8feaSLen Brown } 1493103a8feaSLen Brown 14941b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old) 1495c98d5d94SLen Brown { 1496388e9c81SLen Brown int i; 1497388e9c81SLen Brown struct msr_counter *mp; 1498388e9c81SLen Brown 1499c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1500c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1501c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1502889facbeSLen Brown old->core_temp_c = new->core_temp_c; 1503eae97e05SChen Yu old->core_throt_cnt = new->core_throt_cnt; 15040539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1505388e9c81SLen Brown 15069392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 15079392bd98SCalvin Walton 1508388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1509388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1510388e9c81SLen Brown old->counter[i] = new->counter[i]; 1511388e9c81SLen Brown else 1512388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1513388e9c81SLen Brown } 1514103a8feaSLen Brown } 1515c98d5d94SLen Brown 15161e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 15171e9042b9SSrinivas Pandruvada { 15181e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 15191e9042b9SSrinivas Pandruvada return 0; 15201e9042b9SSrinivas Pandruvada 15211e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 15221e9042b9SSrinivas Pandruvada } 15231e9042b9SSrinivas Pandruvada 1524c3ae331dSLen Brown /* 1525c3ae331dSLen Brown * old = new - old 1526c3ae331dSLen Brown */ 15271b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 1528c98d5d94SLen Brown { 1529388e9c81SLen Brown int i; 1530388e9c81SLen Brown struct msr_counter *mp; 1531388e9c81SLen Brown 15324c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 15334c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 15344c2122d4SLen Brown new->apic_id = old->apic_id; 15354c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 15364c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 15374c2122d4SLen Brown 15383f44a5c6SLen Brown /* 15393f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 15403f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 15413f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 15423f44a5c6SLen Brown */ 15433f44a5c6SLen Brown 1544d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 15453f44a5c6SLen Brown old->tv_begin = new->tv_begin; 15463f44a5c6SLen Brown old->tv_end = new->tv_end; 15473f44a5c6SLen Brown 1548c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1549c98d5d94SLen Brown 1550103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1551b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1552b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1553b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1554b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1555103a8feaSLen Brown 1556c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1557c98d5d94SLen Brown 15581b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 1559c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1560c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1561c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1562c98d5d94SLen Brown } else { 1563ba3dec99SLen Brown return -1; 1564103a8feaSLen Brown } 1565a729617cSLen Brown } 1566103a8feaSLen Brown 1567144b44b1SLen Brown if (use_c1_residency_msr) { 1568144b44b1SLen Brown /* 1569144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1570144b44b1SLen Brown * which should be more accurate than the derivation below. 1571144b44b1SLen Brown */ 1572144b44b1SLen Brown } else { 1573103a8feaSLen Brown /* 1574c3ae331dSLen Brown * As counter collection is not atomic, 1575c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1576103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1577103a8feaSLen Brown */ 157895149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1579c98d5d94SLen Brown old->c1 = 0; 1580c98d5d94SLen Brown else { 1581c98d5d94SLen Brown /* normal case, derive c1 */ 1582008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1583c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1584c98d5d94SLen Brown } 1585144b44b1SLen Brown } 1586c3ae331dSLen Brown 1587c98d5d94SLen Brown if (old->mperf == 0) { 1588b7d8c148SLen Brown if (debug > 1) 1589b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1590c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1591c98d5d94SLen Brown } 1592103a8feaSLen Brown 15932af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 15942af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 15952af4f9b8SLen Brown 1596812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1597562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1598562a2d37SLen Brown 1599812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 16001ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1601ba3dec99SLen Brown 1602388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1603388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1604388e9c81SLen Brown old->counter[i] = new->counter[i]; 1605388e9c81SLen Brown else 1606388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1607388e9c81SLen Brown } 1608ba3dec99SLen Brown return 0; 1609103a8feaSLen Brown } 1610c98d5d94SLen Brown 1611c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 16121b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 1613c98d5d94SLen Brown { 1614ba3dec99SLen Brown int retval = 0; 1615ba3dec99SLen Brown 1616c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1617c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1618c98d5d94SLen Brown delta_core(c, c2); 1619c98d5d94SLen Brown 1620c98d5d94SLen Brown /* always calculate thread delta */ 1621ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1622ba3dec99SLen Brown if (retval) 1623ba3dec99SLen Brown return retval; 1624c98d5d94SLen Brown 1625c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1626c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1627ba3dec99SLen Brown retval = delta_package(p, p2); 1628c98d5d94SLen Brown 1629ba3dec99SLen Brown return retval; 1630103a8feaSLen Brown } 1631103a8feaSLen Brown 1632c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1633103a8feaSLen Brown { 1634388e9c81SLen Brown int i; 1635388e9c81SLen Brown struct msr_counter *mp; 1636388e9c81SLen Brown 16373f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 16383f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 16393f44a5c6SLen Brown t->tv_end.tv_sec = 0; 16403f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1641d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1642d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 16433f44a5c6SLen Brown 1644c98d5d94SLen Brown t->tsc = 0; 1645c98d5d94SLen Brown t->aperf = 0; 1646c98d5d94SLen Brown t->mperf = 0; 1647c98d5d94SLen Brown t->c1 = 0; 1648103a8feaSLen Brown 16492af4f9b8SLen Brown t->instr_count = 0; 16502af4f9b8SLen Brown 1651562a2d37SLen Brown t->irq_count = 0; 1652562a2d37SLen Brown t->smi_count = 0; 1653562a2d37SLen Brown 1654c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1655c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1656c98d5d94SLen Brown 1657c98d5d94SLen Brown c->c3 = 0; 1658c98d5d94SLen Brown c->c6 = 0; 1659c98d5d94SLen Brown c->c7 = 0; 16600539ba11SLen Brown c->mc6_us = 0; 1661889facbeSLen Brown c->core_temp_c = 0; 16629392bd98SCalvin Walton c->core_energy = 0; 1663eae97e05SChen Yu c->core_throt_cnt = 0; 1664c98d5d94SLen Brown 16650b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16660b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16670b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16680b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16690b2bb692SLen Brown 1670c98d5d94SLen Brown p->pc2 = 0; 16710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1672c98d5d94SLen Brown p->pc3 = 0; 16730f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1674c98d5d94SLen Brown p->pc6 = 0; 16750f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1676c98d5d94SLen Brown p->pc7 = 0; 1677ca58710fSKristen Carlson Accardi p->pc8 = 0; 1678ca58710fSKristen Carlson Accardi p->pc9 = 0; 1679ca58710fSKristen Carlson Accardi p->pc10 = 0; 1680be0e54c4SLen Brown p->cpu_lpi = 0; 1681be0e54c4SLen Brown p->sys_lpi = 0; 1682889facbeSLen Brown 1683889facbeSLen Brown p->energy_pkg = 0; 1684889facbeSLen Brown p->energy_dram = 0; 1685889facbeSLen Brown p->energy_cores = 0; 1686889facbeSLen Brown p->energy_gfx = 0; 1687889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1688889facbeSLen Brown p->rapl_dram_perf_status = 0; 1689889facbeSLen Brown p->pkg_temp_c = 0; 169027d47356SLen Brown 1691fdf676e5SLen Brown p->gfx_rc6_ms = 0; 169227d47356SLen Brown p->gfx_mhz = 0; 1693b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1694388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1695388e9c81SLen Brown t->counter[i] = 0; 1696388e9c81SLen Brown 1697388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1698388e9c81SLen Brown c->counter[i] = 0; 1699388e9c81SLen Brown 1700388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1701388e9c81SLen Brown p->counter[i] = 0; 1702103a8feaSLen Brown } 17031b439f01SLen Brown 17041b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1705103a8feaSLen Brown { 1706388e9c81SLen Brown int i; 1707388e9c81SLen Brown struct msr_counter *mp; 1708388e9c81SLen Brown 17094c2122d4SLen Brown /* copy un-changing apic_id's */ 17104c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 17114c2122d4SLen Brown average.threads.apic_id = t->apic_id; 17124c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 17134c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 17144c2122d4SLen Brown 17153f44a5c6SLen Brown /* remember first tv_begin */ 17163f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 17173f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 17183f44a5c6SLen Brown 17193f44a5c6SLen Brown /* remember last tv_end */ 17203f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 17213f44a5c6SLen Brown 1722c98d5d94SLen Brown average.threads.tsc += t->tsc; 1723c98d5d94SLen Brown average.threads.aperf += t->aperf; 1724c98d5d94SLen Brown average.threads.mperf += t->mperf; 1725c98d5d94SLen Brown average.threads.c1 += t->c1; 172615aaa346SLen Brown 17272af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 17282af4f9b8SLen Brown 1729562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1730562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1731562a2d37SLen Brown 1732388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1733388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1734388e9c81SLen Brown continue; 1735388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1736388e9c81SLen Brown } 1737388e9c81SLen Brown 1738c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1739c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1740c98d5d94SLen Brown return 0; 1741c98d5d94SLen Brown 1742c98d5d94SLen Brown average.cores.c3 += c->c3; 1743c98d5d94SLen Brown average.cores.c6 += c->c6; 1744c98d5d94SLen Brown average.cores.c7 += c->c7; 17450539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1746c98d5d94SLen Brown 1747889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1748eae97e05SChen Yu average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt); 1749889facbeSLen Brown 17509392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 17519392bd98SCalvin Walton 1752388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1753388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1754388e9c81SLen Brown continue; 1755388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1756388e9c81SLen Brown } 1757388e9c81SLen Brown 1758c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1759c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1760c98d5d94SLen Brown return 0; 1761c98d5d94SLen Brown 1762a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17630b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1764a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17650b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1766a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17670b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1768a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17690b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17700b2bb692SLen Brown 1771c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1773c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17740f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1775c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17760f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1777c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1778ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1779ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1780ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1781c98d5d94SLen Brown 1782be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1783be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1784be0e54c4SLen Brown 1785889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1786889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1787889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1788889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1789889facbeSLen Brown 1790fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 179127d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1792b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 179327d47356SLen Brown 1794889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1795889facbeSLen Brown 1796889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1797889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1798388e9c81SLen Brown 1799388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1800388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1801388e9c81SLen Brown continue; 1802388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1803388e9c81SLen Brown } 1804c98d5d94SLen Brown return 0; 1805c98d5d94SLen Brown } 18061b439f01SLen Brown 1807c98d5d94SLen Brown /* 1808c98d5d94SLen Brown * sum the counters for all cpus in the system 1809c98d5d94SLen Brown * compute the weighted average 1810c98d5d94SLen Brown */ 18111b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1812c98d5d94SLen Brown { 1813388e9c81SLen Brown int i; 1814388e9c81SLen Brown struct msr_counter *mp; 1815388e9c81SLen Brown 1816c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1817c98d5d94SLen Brown 1818c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1819c98d5d94SLen Brown 1820d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1821d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1822d4794f25SYazen Ghannam 1823c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1824c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1825c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 18262af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1827c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1828c98d5d94SLen Brown 18290de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 18300de6c0dfSLen Brown sums_need_wide_columns = 1; 18310de6c0dfSLen Brown 1832c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1833c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1834c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 18350539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1836c98d5d94SLen Brown 1837a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 18380b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1839a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 18400b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1841a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 18420b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1843a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 18440b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 18450b2bb692SLen Brown 1846c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 18470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1848c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 18490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1850c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 18510f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1852c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1853ca58710fSKristen Carlson Accardi 1854ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1855ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1856ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1857388e9c81SLen Brown 1858388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1859388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1860388e9c81SLen Brown continue; 18610de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18620de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18630de6c0dfSLen Brown sums_need_wide_columns = 1; 186441618e63SLen Brown continue; 18650de6c0dfSLen Brown } 1866388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1867388e9c81SLen Brown } 1868388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1869388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1870388e9c81SLen Brown continue; 18710de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18720de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18730de6c0dfSLen Brown sums_need_wide_columns = 1; 18740de6c0dfSLen Brown } 1875388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1876388e9c81SLen Brown } 1877388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1878388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1879388e9c81SLen Brown continue; 18800de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18810de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18820de6c0dfSLen Brown sums_need_wide_columns = 1; 18830de6c0dfSLen Brown } 1884388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1885388e9c81SLen Brown } 1886c98d5d94SLen Brown } 1887c98d5d94SLen Brown 1888c98d5d94SLen Brown static unsigned long long rdtsc(void) 1889c98d5d94SLen Brown { 1890c98d5d94SLen Brown unsigned int low, high; 1891c98d5d94SLen Brown 1892c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high)); 1893c98d5d94SLen Brown 1894c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1895c98d5d94SLen Brown } 1896c98d5d94SLen Brown 1897c98d5d94SLen Brown /* 1898495c7654SLen Brown * Open a file, and exit on failure 1899495c7654SLen Brown */ 1900495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1901495c7654SLen Brown { 1902495c7654SLen Brown FILE *filep = fopen(path, mode); 1903495c7654SLen Brown 1904495c7654SLen Brown if (!filep) 1905495c7654SLen Brown err(1, "%s: open failed", path); 1906495c7654SLen Brown return filep; 1907495c7654SLen Brown } 19081b439f01SLen Brown 1909495c7654SLen Brown /* 1910495c7654SLen Brown * snapshot_sysfs_counter() 1911495c7654SLen Brown * 1912495c7654SLen Brown * return snapshot of given counter 1913495c7654SLen Brown */ 1914495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1915495c7654SLen Brown { 1916495c7654SLen Brown FILE *fp; 1917495c7654SLen Brown int retval; 1918495c7654SLen Brown unsigned long long counter; 1919495c7654SLen Brown 1920495c7654SLen Brown fp = fopen_or_die(path, "r"); 1921495c7654SLen Brown 1922495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1923495c7654SLen Brown if (retval != 1) 1924495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1925495c7654SLen Brown 1926495c7654SLen Brown fclose(fp); 1927495c7654SLen Brown 1928495c7654SLen Brown return counter; 1929495c7654SLen Brown } 1930495c7654SLen Brown 1931495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1932495c7654SLen Brown { 1933495c7654SLen Brown if (mp->msr_num != 0) { 1934495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1935495c7654SLen Brown return -1; 1936495c7654SLen Brown } else { 193746c27978SLen Brown char path[128 + PATH_BYTES]; 193841618e63SLen Brown 193941618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 19401b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path); 194141618e63SLen Brown 194241618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 194341618e63SLen Brown } else { 1944495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1945495c7654SLen Brown } 194641618e63SLen Brown } 1947495c7654SLen Brown 1948495c7654SLen Brown return 0; 1949495c7654SLen Brown } 1950495c7654SLen Brown 19516d6501d9SBorislav Petkov int get_epb(int cpu) 19526d6501d9SBorislav Petkov { 19536d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19547f1b11baSBorislav Petkov unsigned long long msr; 19556d6501d9SBorislav Petkov int ret, epb = -1; 19566d6501d9SBorislav Petkov FILE *fp; 19576d6501d9SBorislav Petkov 19586d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19596d6501d9SBorislav Petkov 19607f1b11baSBorislav Petkov fp = fopen(path, "r"); 19617f1b11baSBorislav Petkov if (!fp) 19627f1b11baSBorislav Petkov goto msr_fallback; 19636d6501d9SBorislav Petkov 19646d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19656d6501d9SBorislav Petkov if (ret != 1) 19666d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19676d6501d9SBorislav Petkov 19686d6501d9SBorislav Petkov fclose(fp); 19696d6501d9SBorislav Petkov 19706d6501d9SBorislav Petkov return epb; 19717f1b11baSBorislav Petkov 19727f1b11baSBorislav Petkov msr_fallback: 19737f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 19747f1b11baSBorislav Petkov 19757f1b11baSBorislav Petkov return msr & 0xf; 19766d6501d9SBorislav Petkov } 19776d6501d9SBorislav Petkov 19784c2122d4SLen Brown void get_apic_id(struct thread_data *t) 19794c2122d4SLen Brown { 198034041551SLen Brown unsigned int eax, ebx, ecx, edx; 198134041551SLen Brown 198234041551SLen Brown if (DO_BIC(BIC_APIC)) { 198334041551SLen Brown eax = ebx = ecx = edx = 0; 198434041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 198534041551SLen Brown 198634041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 198734041551SLen Brown } 198834041551SLen Brown 198934041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 199034041551SLen Brown return; 199134041551SLen Brown 1992c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 199334041551SLen Brown unsigned int topology_extensions; 199434041551SLen Brown 199534041551SLen Brown if (max_extended_level < 0x8000001e) 199634041551SLen Brown return; 19974c2122d4SLen Brown 19984c2122d4SLen Brown eax = ebx = ecx = edx = 0; 199934041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 200034041551SLen Brown topology_extensions = ecx & (1 << 22); 200134041551SLen Brown 200234041551SLen Brown if (topology_extensions == 0) 200334041551SLen Brown return; 200434041551SLen Brown 200534041551SLen Brown eax = ebx = ecx = edx = 0; 200634041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 200734041551SLen Brown 200834041551SLen Brown t->x2apic_id = eax; 200934041551SLen Brown return; 201034041551SLen Brown } 20114c2122d4SLen Brown 20124c2122d4SLen Brown if (!genuine_intel) 20134c2122d4SLen Brown return; 20144c2122d4SLen Brown 20154c2122d4SLen Brown if (max_level < 0xb) 20164c2122d4SLen Brown return; 20174c2122d4SLen Brown 20184c2122d4SLen Brown ecx = 0; 20194c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 20204c2122d4SLen Brown t->x2apic_id = edx; 20214c2122d4SLen Brown 202234041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 20231b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 20244c2122d4SLen Brown } 20254c2122d4SLen Brown 2026eae97e05SChen Yu int get_core_throt_cnt(int cpu, unsigned long long *cnt) 2027eae97e05SChen Yu { 2028eae97e05SChen Yu char path[128 + PATH_BYTES]; 2029eae97e05SChen Yu unsigned long long tmp; 2030eae97e05SChen Yu FILE *fp; 2031eae97e05SChen Yu int ret; 2032eae97e05SChen Yu 2033eae97e05SChen Yu sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu); 2034eae97e05SChen Yu fp = fopen(path, "r"); 2035eae97e05SChen Yu if (!fp) 2036eae97e05SChen Yu return -1; 2037eae97e05SChen Yu ret = fscanf(fp, "%lld", &tmp); 2038*5e5fd36cSColin Ian King fclose(fp); 2039eae97e05SChen Yu if (ret != 1) 2040eae97e05SChen Yu return -1; 2041eae97e05SChen Yu *cnt = tmp; 2042eae97e05SChen Yu 2043eae97e05SChen Yu return 0; 2044eae97e05SChen Yu } 2045eae97e05SChen Yu 2046495c7654SLen Brown /* 2047c98d5d94SLen Brown * get_counters(...) 2048c98d5d94SLen Brown * migrate to cpu 2049c98d5d94SLen Brown * acquire and record local counters for that cpu 2050c98d5d94SLen Brown */ 2051c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2052c98d5d94SLen Brown { 2053c98d5d94SLen Brown int cpu = t->cpu_id; 2054889facbeSLen Brown unsigned long long msr; 20550102b067SLen Brown int aperf_mperf_retry_count = 0; 2056388e9c81SLen Brown struct msr_counter *mp; 2057388e9c81SLen Brown int i; 2058c98d5d94SLen Brown 2059e52966c0SLen Brown if (cpu_migrate(cpu)) { 20603d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 206115aaa346SLen Brown return -1; 2062e52966c0SLen Brown } 206315aaa346SLen Brown 2064d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 2065d4794f25SYazen Ghannam 20664c2122d4SLen Brown if (first_counter_read) 20674c2122d4SLen Brown get_apic_id(t); 20680102b067SLen Brown retry: 2069c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 207015aaa346SLen Brown 20711b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 20720102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 20730102b067SLen Brown 20740102b067SLen Brown /* 20750102b067SLen Brown * The TSC, APERF and MPERF must be read together for 20760102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 20770102b067SLen Brown * 20780102b067SLen Brown * Unfortunately, APERF and MPERF are read by 20790102b067SLen Brown * individual system call, so delays may occur 20800102b067SLen Brown * between them. If the time to read them 20810102b067SLen Brown * varies by a large amount, we re-read them. 20820102b067SLen Brown */ 20830102b067SLen Brown 20840102b067SLen Brown /* 20850102b067SLen Brown * This initial dummy APERF read has been seen to 20860102b067SLen Brown * reduce jitter in the subsequent reads. 20870102b067SLen Brown */ 20880102b067SLen Brown 20899c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2090c98d5d94SLen Brown return -3; 20910102b067SLen Brown 20920102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 20930102b067SLen Brown 20940102b067SLen Brown tsc_before = t->tsc; 20950102b067SLen Brown 20960102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 20970102b067SLen Brown return -3; 20980102b067SLen Brown 20990102b067SLen Brown tsc_between = rdtsc(); 21000102b067SLen Brown 21019c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2102c98d5d94SLen Brown return -4; 21030102b067SLen Brown 21040102b067SLen Brown tsc_after = rdtsc(); 21050102b067SLen Brown 21060102b067SLen Brown aperf_time = tsc_between - tsc_before; 21070102b067SLen Brown mperf_time = tsc_after - tsc_between; 21080102b067SLen Brown 21090102b067SLen Brown /* 21100102b067SLen Brown * If the system call latency to read APERF and MPERF 21110102b067SLen Brown * differ by more than 2x, then try again. 21120102b067SLen Brown */ 21130102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 21140102b067SLen Brown aperf_mperf_retry_count++; 21150102b067SLen Brown if (aperf_mperf_retry_count < 5) 21160102b067SLen Brown goto retry; 21170102b067SLen Brown else 21181b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time); 21190102b067SLen Brown } 21200102b067SLen Brown aperf_mperf_retry_count = 0; 21210102b067SLen Brown 2122b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2123b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 212488c3281fSLen Brown } 212588c3281fSLen Brown 21262af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 21272af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 21282af4f9b8SLen Brown return -4; 21292af4f9b8SLen Brown 2130812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2131562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2132812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 21331ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 21341ed51011SLen Brown return -5; 21351ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 21361ed51011SLen Brown } 21370539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2138144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2139144b44b1SLen Brown return -6; 2140144b44b1SLen Brown } 2141144b44b1SLen Brown 2142388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2143495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2144388e9c81SLen Brown return -10; 2145388e9c81SLen Brown } 2146388e9c81SLen Brown 2147c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2148c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2149f4fdf2b4SLen Brown goto done; 2150c98d5d94SLen Brown 21511e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2152c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2153c98d5d94SLen Brown return -6; 2154144b44b1SLen Brown } 2155144b44b1SLen Brown 21561e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2157c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2158c98d5d94SLen Brown return -7; 21591e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2160fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2161fb5d4327SDasaratharaman Chandramouli return -7; 2162103a8feaSLen Brown } 216315aaa346SLen Brown 21647ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2165c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2166c98d5d94SLen Brown return -8; 21677ab5ff49SZhang Rui else if (t->is_atom) { 21687ab5ff49SZhang Rui /* 21697ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 21707ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 21717ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 21727ab5ff49SZhang Rui * accturate cc6 residency. 21737ab5ff49SZhang Rui */ 21747ab5ff49SZhang Rui c->c6 -= c->c7; 21757ab5ff49SZhang Rui } 21767ab5ff49SZhang Rui } 2177c98d5d94SLen Brown 21780539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 21790539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 21800539ba11SLen Brown return -8; 21810539ba11SLen Brown 2182812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2183889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2184889facbeSLen Brown return -9; 218555279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 2186889facbeSLen Brown } 2187889facbeSLen Brown 2188eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 2189eae97e05SChen Yu get_core_throt_cnt(cpu, &c->core_throt_cnt); 2190eae97e05SChen Yu 21919392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21929392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 21939392bd98SCalvin Walton return -14; 21949392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 21959392bd98SCalvin Walton } 21969392bd98SCalvin Walton 2197388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2198495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2199388e9c81SLen Brown return -10; 2200388e9c81SLen Brown } 2201889facbeSLen Brown 2202c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2203c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2204f4fdf2b4SLen Brown goto done; 220515aaa346SLen Brown 2206a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 22070b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 22080b2bb692SLen Brown return -10; 2209a99d8730SLen Brown } 2210a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 22110b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 22120b2bb692SLen Brown return -11; 2213a99d8730SLen Brown } 2214a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 22150b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 22160b2bb692SLen Brown return -12; 2217a99d8730SLen Brown } 2218a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 22190b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 22200b2bb692SLen Brown return -13; 22210b2bb692SLen Brown } 22220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2223c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2224c98d5d94SLen Brown return -9; 22250f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 22260539ba11SLen Brown if (do_slm_cstates) { 22270539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 22280539ba11SLen Brown return -10; 22290539ba11SLen Brown } else { 2230c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2231c98d5d94SLen Brown return -10; 22320539ba11SLen Brown } 22330539ba11SLen Brown } 22340539ba11SLen Brown 22350f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2236c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2237c98d5d94SLen Brown return -11; 22380f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2239c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2240c98d5d94SLen Brown return -12; 22410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2242ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2243ca58710fSKristen Carlson Accardi return -13; 22440f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2245ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2246ca58710fSKristen Carlson Accardi return -13; 22470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2248ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2249ca58710fSKristen Carlson Accardi return -13; 22500f47c08dSLen Brown 2251be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2252be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2253be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2254be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2255be0e54c4SLen Brown 2256889facbeSLen Brown if (do_rapl & RAPL_PKG) { 22579972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2258889facbeSLen Brown return -13; 22599972d5d8SChen Yu p->energy_pkg = msr; 2260889facbeSLen Brown } 22619148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 22629972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2263889facbeSLen Brown return -14; 22649972d5d8SChen Yu p->energy_cores = msr; 2265889facbeSLen Brown } 2266889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 22679972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2268889facbeSLen Brown return -15; 22699972d5d8SChen Yu p->energy_dram = msr; 2270889facbeSLen Brown } 2271889facbeSLen Brown if (do_rapl & RAPL_GFX) { 22729972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2273889facbeSLen Brown return -16; 22749972d5d8SChen Yu p->energy_gfx = msr; 2275889facbeSLen Brown } 2276889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 22779972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2278889facbeSLen Brown return -16; 22799972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2280889facbeSLen Brown } 2281889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 22829972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2283889facbeSLen Brown return -16; 22849972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2285889facbeSLen Brown } 22863316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22879972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 22883316f99aSCalvin Walton return -13; 22899972d5d8SChen Yu p->energy_pkg = msr; 22903316f99aSCalvin Walton } 2291812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2292889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2293889facbeSLen Brown return -17; 229455279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 2295889facbeSLen Brown } 2296fdf676e5SLen Brown 2297812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2298fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2299fdf676e5SLen Brown 2300812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 230127d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 230227d47356SLen Brown 2303b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2304b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2305b4b91569SRafael Antognolli 2306388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2307495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2308388e9c81SLen Brown return -10; 2309388e9c81SLen Brown } 2310f4fdf2b4SLen Brown done: 2311f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2312388e9c81SLen Brown 231315aaa346SLen Brown return 0; 2314103a8feaSLen Brown } 2315103a8feaSLen Brown 2316ee7e38e3SLen Brown /* 2317ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2318ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2319ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2320ee7e38e3SLen Brown */ 2321ee7e38e3SLen Brown 2322ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2323ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2324ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2325ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2326ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2327ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2328ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2329ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2330ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2331ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2332ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2333ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 23340b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 23350b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2336445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2337445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2338ee7e38e3SLen Brown 2339ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2340ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 23411b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 23421b439f01SLen Brown }; 2343ee7e38e3SLen Brown 23441b439f01SLen Brown int nhm_pkg_cstate_limits[16] = 23451b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 234638c6663aSLen Brown PCLRSV, PCLRSV 234738c6663aSLen Brown }; 234838c6663aSLen Brown 23491b439f01SLen Brown int snb_pkg_cstate_limits[16] = 23501b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 235138c6663aSLen Brown PCLRSV, PCLRSV 235238c6663aSLen Brown }; 235338c6663aSLen Brown 23541b439f01SLen Brown int hsw_pkg_cstate_limits[16] = 23551b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 235638c6663aSLen Brown PCLRSV, PCLRSV 235738c6663aSLen Brown }; 235838c6663aSLen Brown 23591b439f01SLen Brown int slv_pkg_cstate_limits[16] = 23601b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 236138c6663aSLen Brown PCL__6, PCL__7 236238c6663aSLen Brown }; 236338c6663aSLen Brown 23641b439f01SLen Brown int amt_pkg_cstate_limits[16] = 23651b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 236638c6663aSLen Brown PCLRSV, PCLRSV 236738c6663aSLen Brown }; 236838c6663aSLen Brown 23691b439f01SLen Brown int phi_pkg_cstate_limits[16] = 23701b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 237138c6663aSLen Brown PCLRSV, PCLRSV 237238c6663aSLen Brown }; 237338c6663aSLen Brown 23741b439f01SLen Brown int glm_pkg_cstate_limits[16] = 23751b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 237638c6663aSLen Brown PCLRSV, PCLRSV 237738c6663aSLen Brown }; 237838c6663aSLen Brown 23791b439f01SLen Brown int skx_pkg_cstate_limits[16] = 23801b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 238138c6663aSLen Brown PCLRSV, PCLRSV 238238c6663aSLen Brown }; 238338c6663aSLen Brown 23841b439f01SLen Brown int icx_pkg_cstate_limits[16] = 2385a1b6f487SArtem Bityutskiy { PCL__0, PCL__2, PCL__6, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 238638c6663aSLen Brown PCLRSV, PCLRSV 238738c6663aSLen Brown }; 2388a2b7b749SLen Brown 23891b439f01SLen Brown static void calculate_tsc_tweak() 2390a2b7b749SLen Brown { 2391a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2392a2b7b749SLen Brown } 2393a2b7b749SLen Brown 2394aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2395aeb01e6dSChen Yu 23961b439f01SLen Brown static void dump_nhm_platform_info(void) 2397103a8feaSLen Brown { 2398103a8feaSLen Brown unsigned long long msr; 2399103a8feaSLen Brown unsigned int ratio; 2400103a8feaSLen Brown 2401ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2402103a8feaSLen Brown 2403b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 24046574a5d5SLen Brown 2405103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 24061b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 2407103a8feaSLen Brown 2408103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 24091b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 2410103a8feaSLen Brown 24117ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2412b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2413bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 241467920418SLen Brown 2415aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2416aeb01e6dSChen Yu if (dis_cstate_prewake) 24171b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 2418aeb01e6dSChen Yu 2419fcd17211SLen Brown return; 2420fcd17211SLen Brown } 2421fcd17211SLen Brown 24221b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void) 2423fcd17211SLen Brown { 2424fcd17211SLen Brown unsigned long long msr; 2425fcd17211SLen Brown unsigned int ratio; 2426fcd17211SLen Brown 24277ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2428fcd17211SLen Brown 2429b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2430fcd17211SLen Brown 2431fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2432fcd17211SLen Brown if (ratio) 24331b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 2434fcd17211SLen Brown 2435fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2436fcd17211SLen Brown if (ratio) 24371b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 2438fcd17211SLen Brown return; 2439fcd17211SLen Brown } 2440fcd17211SLen Brown 24411b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void) 2442fcd17211SLen Brown { 2443fcd17211SLen Brown unsigned long long msr; 2444fcd17211SLen Brown unsigned int ratio; 24456574a5d5SLen Brown 24467ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 24476574a5d5SLen Brown 2448b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 24496574a5d5SLen Brown 24506574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 24516574a5d5SLen Brown if (ratio) 24521b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 24536574a5d5SLen Brown 24546574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 24556574a5d5SLen Brown if (ratio) 24561b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 24576574a5d5SLen Brown 24586574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 24596574a5d5SLen Brown if (ratio) 24601b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 24616574a5d5SLen Brown 24626574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 24636574a5d5SLen Brown if (ratio) 24641b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 24656574a5d5SLen Brown 24666574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 24676574a5d5SLen Brown if (ratio) 24681b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 24696574a5d5SLen Brown 24706574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 24716574a5d5SLen Brown if (ratio) 24721b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 24736574a5d5SLen Brown 24746574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 24756574a5d5SLen Brown if (ratio) 24761b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 24776574a5d5SLen Brown 24786574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 24796574a5d5SLen Brown if (ratio) 24801b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 2481103a8feaSLen Brown return; 2482fcd17211SLen Brown } 24831b439f01SLen Brown 248431e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 248531e07522SLen Brown { 248631e07522SLen Brown 248731e07522SLen Brown if (!genuine_intel) 248831e07522SLen Brown return 0; 248931e07522SLen Brown 24909878bf7aSLen Brown if (family != 6) 24919878bf7aSLen Brown return 0; 24929878bf7aSLen Brown 249331e07522SLen Brown switch (model) { 249431e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 249531e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 249625368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 24975ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 249820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 249931e07522SLen Brown return 1; 25009878bf7aSLen Brown default: 250131e07522SLen Brown return 0; 250231e07522SLen Brown } 25039878bf7aSLen Brown } 2504fcd17211SLen Brown 25051b439f01SLen Brown static void dump_turbo_ratio_limits(int family, int model) 2506fcd17211SLen Brown { 250731e07522SLen Brown unsigned long long msr, core_counts; 250831e07522SLen Brown unsigned int ratio, group_size; 2509103a8feaSLen Brown 25107ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2511b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 25126574a5d5SLen Brown 251331e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 251431e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 251531e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 251631e07522SLen Brown } else { 251731e07522SLen Brown core_counts = 0x0807060504030201; 251831e07522SLen Brown } 251931e07522SLen Brown 25206574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 252131e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 25226574a5d5SLen Brown if (ratio) 252331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 252431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25256574a5d5SLen Brown 25266574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 252731e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 25286574a5d5SLen Brown if (ratio) 252931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 253031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25316574a5d5SLen Brown 25326574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 253331e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 25346574a5d5SLen Brown if (ratio) 253531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 253631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25376574a5d5SLen Brown 25386574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 253931e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 25406574a5d5SLen Brown if (ratio) 254131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 254231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25436574a5d5SLen Brown 2544103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 254531e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2546103a8feaSLen Brown if (ratio) 254731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 254831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2549103a8feaSLen Brown 2550103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 255131e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2552103a8feaSLen Brown if (ratio) 255331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 255431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2555103a8feaSLen Brown 2556103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 255731e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2558103a8feaSLen Brown if (ratio) 255931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 256031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2561103a8feaSLen Brown 2562103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 256331e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2564103a8feaSLen Brown if (ratio) 256531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 256631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2567fcd17211SLen Brown return; 2568fcd17211SLen Brown } 25693a9a941dSLen Brown 25701b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void) 25710f7887c4SLen Brown { 25720f7887c4SLen Brown unsigned long long msr; 25730f7887c4SLen Brown unsigned int ratio; 25740f7887c4SLen Brown 25750f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 25760f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25770f7887c4SLen Brown 25780f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25790f7887c4SLen Brown if (ratio) 25801b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 25810f7887c4SLen Brown 25820f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25830f7887c4SLen Brown if (ratio) 25841b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 25850f7887c4SLen Brown 25860f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25870f7887c4SLen Brown if (ratio) 25881b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 25890f7887c4SLen Brown 25900f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 25910f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25920f7887c4SLen Brown 25930f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 25940f7887c4SLen Brown if (ratio) 25951b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 25960f7887c4SLen Brown 25970f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25980f7887c4SLen Brown if (ratio) 25991b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 26000f7887c4SLen Brown 26010f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 26020f7887c4SLen Brown if (ratio) 26031b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 26040f7887c4SLen Brown 26050f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 26060f7887c4SLen Brown if (ratio) 26071b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 26080f7887c4SLen Brown } 26090f7887c4SLen Brown 26101b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void) 2611fb5d4327SDasaratharaman Chandramouli { 2612cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2613cbf97abaSHubert Chrzaniuk 2614fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2615cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2616cbf97abaSHubert Chrzaniuk int i, b_nr; 2617cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2618cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2619fb5d4327SDasaratharaman Chandramouli 2620ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2621fb5d4327SDasaratharaman Chandramouli 26221b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 2623fb5d4327SDasaratharaman Chandramouli 26241e3ec5cdSRandy Dunlap /* 2625fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2626cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2627cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2628fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2629fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2630fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2631fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2632fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2633fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2634fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2635fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2636fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2637fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2638fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2639fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2640fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2641fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2642fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2643fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2644fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2645fb5d4327SDasaratharaman Chandramouli */ 2646fb5d4327SDasaratharaman Chandramouli 2647cbf97abaSHubert Chrzaniuk b_nr = 0; 2648cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2649cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2650cbf97abaSHubert Chrzaniuk 2651cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2652fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2653cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2654fb5d4327SDasaratharaman Chandramouli 2655cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2656cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2657cbf97abaSHubert Chrzaniuk b_nr++; 2658fb5d4327SDasaratharaman Chandramouli } 2659cbf97abaSHubert Chrzaniuk 2660cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2661cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2662b7d8c148SLen Brown fprintf(outf, 2663710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2664cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2665fb5d4327SDasaratharaman Chandramouli } 2666fb5d4327SDasaratharaman Chandramouli 26671b439f01SLen Brown static void dump_nhm_cst_cfg(void) 2668fcd17211SLen Brown { 2669fcd17211SLen Brown unsigned long long msr; 2670fcd17211SLen Brown 26711df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2672fcd17211SLen Brown 26731df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2674fcd17211SLen Brown 26753e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2676fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2677fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2678fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2679fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 26801b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 2681ac980e13SArtem Bityutskiy 2682ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2683ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 26841b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2685ac980e13SArtem Bityutskiy } 2686ac980e13SArtem Bityutskiy 2687ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2688ac980e13SArtem Bityutskiy 2689fcd17211SLen Brown return; 2690103a8feaSLen Brown } 2691103a8feaSLen Brown 26921b439f01SLen Brown static void dump_config_tdp(void) 26936fb3143bSLen Brown { 26946fb3143bSLen Brown unsigned long long msr; 26956fb3143bSLen Brown 26966fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2697b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2698685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26996fb3143bSLen Brown 27006fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2701b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 27026fb3143bSLen Brown if (msr) { 2703685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2704685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2705685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2706685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 27076fb3143bSLen Brown } 2708b7d8c148SLen Brown fprintf(outf, ")\n"); 27096fb3143bSLen Brown 27106fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2711b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 27126fb3143bSLen Brown if (msr) { 2713685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2714685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2715685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2716685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 27176fb3143bSLen Brown } 2718b7d8c148SLen Brown fprintf(outf, ")\n"); 27196fb3143bSLen Brown 27206fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2721b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 27226fb3143bSLen Brown if ((msr) & 0x3) 2723b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2724b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2725b7d8c148SLen Brown fprintf(outf, ")\n"); 27266fb3143bSLen Brown 27276fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2728b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2729685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2730b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2731b7d8c148SLen Brown fprintf(outf, ")\n"); 27326fb3143bSLen Brown } 27335a63426eSLen Brown 27345a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 27355a63426eSLen Brown 27365a63426eSLen Brown void print_irtl(void) 27375a63426eSLen Brown { 27385a63426eSLen Brown unsigned long long msr; 27395a63426eSLen Brown 27405a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 27415a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 27425a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27435a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27445a63426eSLen Brown 27455a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 27465a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 27475a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27485a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27495a63426eSLen Brown 27505a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 27515a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 27525a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27535a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27545a63426eSLen Brown 27555a63426eSLen Brown if (!do_irtl_hsw) 27565a63426eSLen Brown return; 27575a63426eSLen Brown 27585a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 27595a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 27605a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27615a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27625a63426eSLen Brown 27635a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 27645a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 27655a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27665a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27675a63426eSLen Brown 27685a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 27695a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 27705a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27715a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27725a63426eSLen Brown 27735a63426eSLen Brown } 27741b439f01SLen Brown 277536229897SLen Brown void free_fd_percpu(void) 277636229897SLen Brown { 277736229897SLen Brown int i; 277836229897SLen Brown 277901a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 278036229897SLen Brown if (fd_percpu[i] != 0) 278136229897SLen Brown close(fd_percpu[i]); 278236229897SLen Brown } 278336229897SLen Brown 278436229897SLen Brown free(fd_percpu); 27856fb3143bSLen Brown } 27866fb3143bSLen Brown 2787c98d5d94SLen Brown void free_all_buffers(void) 2788103a8feaSLen Brown { 27890e2d8f05SLen Brown int i; 27900e2d8f05SLen Brown 2791c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2792c98d5d94SLen Brown cpu_present_set = NULL; 279336229897SLen Brown cpu_present_setsize = 0; 2794103a8feaSLen Brown 2795c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2796c98d5d94SLen Brown cpu_affinity_set = NULL; 2797c98d5d94SLen Brown cpu_affinity_setsize = 0; 2798103a8feaSLen Brown 2799c98d5d94SLen Brown free(thread_even); 2800c98d5d94SLen Brown free(core_even); 2801c98d5d94SLen Brown free(package_even); 2802103a8feaSLen Brown 2803c98d5d94SLen Brown thread_even = NULL; 2804c98d5d94SLen Brown core_even = NULL; 2805c98d5d94SLen Brown package_even = NULL; 2806103a8feaSLen Brown 2807c98d5d94SLen Brown free(thread_odd); 2808c98d5d94SLen Brown free(core_odd); 2809c98d5d94SLen Brown free(package_odd); 2810103a8feaSLen Brown 2811c98d5d94SLen Brown thread_odd = NULL; 2812c98d5d94SLen Brown core_odd = NULL; 2813c98d5d94SLen Brown package_odd = NULL; 2814103a8feaSLen Brown 2815c98d5d94SLen Brown free(output_buffer); 2816c98d5d94SLen Brown output_buffer = NULL; 2817c98d5d94SLen Brown outp = NULL; 281836229897SLen Brown 281936229897SLen Brown free_fd_percpu(); 2820562a2d37SLen Brown 2821562a2d37SLen Brown free(irq_column_2_cpu); 2822562a2d37SLen Brown free(irqs_per_cpu); 28230e2d8f05SLen Brown 28240e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 28250e2d8f05SLen Brown if (cpus[i].put_ids) 28260e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 28270e2d8f05SLen Brown } 28280e2d8f05SLen Brown free(cpus); 2829103a8feaSLen Brown } 2830103a8feaSLen Brown 283157a42a34SJosh Triplett /* 283295aebc44SJosh Triplett * Parse a file containing a single int. 28336de68fe1SLen Brown * Return 0 if file can not be opened 28346de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 283595aebc44SJosh Triplett */ 283695aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 283795aebc44SJosh Triplett { 283895aebc44SJosh Triplett va_list args; 283995aebc44SJosh Triplett char path[PATH_MAX]; 284095aebc44SJosh Triplett FILE *filep; 284195aebc44SJosh Triplett int value; 284295aebc44SJosh Triplett 284395aebc44SJosh Triplett va_start(args, fmt); 284495aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 284595aebc44SJosh Triplett va_end(args); 28466de68fe1SLen Brown filep = fopen(path, "r"); 28476de68fe1SLen Brown if (!filep) 28486de68fe1SLen Brown return 0; 2849b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2850b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 285195aebc44SJosh Triplett fclose(filep); 285295aebc44SJosh Triplett return value; 285395aebc44SJosh Triplett } 285495aebc44SJosh Triplett 285595aebc44SJosh Triplett /* 2856c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2857c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2858c98d5d94SLen Brown */ 2859c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2860c98d5d94SLen Brown { 286195aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2862103a8feaSLen Brown } 2863103a8feaSLen Brown 2864103a8feaSLen Brown int get_physical_package_id(int cpu) 2865103a8feaSLen Brown { 286695aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2867103a8feaSLen Brown } 2868103a8feaSLen Brown 28696de68fe1SLen Brown int get_die_id(int cpu) 28706de68fe1SLen Brown { 28716de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 28726de68fe1SLen Brown } 28736de68fe1SLen Brown 2874103a8feaSLen Brown int get_core_id(int cpu) 2875103a8feaSLen Brown { 287695aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2877103a8feaSLen Brown } 2878103a8feaSLen Brown 2879ef605741SPrarit Bhargava void set_node_data(void) 2880ef605741SPrarit Bhargava { 28812ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 28822ffbb224SPrarit Bhargava int cpu_count; 2883ef605741SPrarit Bhargava 28842ffbb224SPrarit Bhargava /* initialize logical_node_id */ 28852ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 28862ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2887ef605741SPrarit Bhargava 28882ffbb224SPrarit Bhargava cpu_count = 0; 28892ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 28902ffbb224SPrarit Bhargava lnode = 0; 28912ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 28922ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2893ef605741SPrarit Bhargava continue; 28942ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 28952ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 28962ffbb224SPrarit Bhargava continue; 28972ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2898ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28992ffbb224SPrarit Bhargava cpu_count++; 29002ffbb224SPrarit Bhargava /* 29012ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 29022ffbb224SPrarit Bhargava * the logical_node_id 29032ffbb224SPrarit Bhargava */ 29042ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 29051b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 29062ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 29072ffbb224SPrarit Bhargava cpu_count++; 2908ef605741SPrarit Bhargava } 29092ffbb224SPrarit Bhargava } 29102ffbb224SPrarit Bhargava lnode++; 29112ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 29122ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 29132ffbb224SPrarit Bhargava } 29142ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 29152ffbb224SPrarit Bhargava break; 29162ffbb224SPrarit Bhargava } 2917ef605741SPrarit Bhargava } 2918ef605741SPrarit Bhargava 2919ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2920c98d5d94SLen Brown { 2921c98d5d94SLen Brown char path[80]; 2922c98d5d94SLen Brown FILE *filep; 29230e2d8f05SLen Brown int i; 29240e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2925c98d5d94SLen Brown 29260e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 29271b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 29280e2d8f05SLen Brown filep = fopen(path, "r"); 29290e2d8f05SLen Brown if (!filep) 29300e2d8f05SLen Brown continue; 29310e2d8f05SLen Brown fclose(filep); 29320e2d8f05SLen Brown return i; 29330e2d8f05SLen Brown } 29340e2d8f05SLen Brown return -1; 2935e275b388SDasaratharaman Chandramouli } 2936c98d5d94SLen Brown 29370e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 29380e2d8f05SLen Brown { 29390e2d8f05SLen Brown char path[80], character; 29400e2d8f05SLen Brown FILE *filep; 29410e2d8f05SLen Brown unsigned long map; 29428cb48b32SPrarit Bhargava int so, shift, sib_core; 29430e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 29440e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 29450e2d8f05SLen Brown size_t size; 29468cb48b32SPrarit Bhargava int thread_id = 0; 29470e2d8f05SLen Brown 29480e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 29498cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 29508cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 29510e2d8f05SLen Brown if (!thiscpu->put_ids) 29520e2d8f05SLen Brown return -1; 29530e2d8f05SLen Brown 29540e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 29550e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 29560e2d8f05SLen Brown 29571b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 29583d7772eaSLen Brown filep = fopen(path, "r"); 29593d7772eaSLen Brown 29603d7772eaSLen Brown if (!filep) { 29613d7772eaSLen Brown warnx("%s: open failed", path); 29623d7772eaSLen Brown return -1; 29633d7772eaSLen Brown } 29640e2d8f05SLen Brown do { 29650e2d8f05SLen Brown offset -= BITMASK_SIZE; 29668173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 29678173c336SBen Hutchings err(1, "%s: failed to parse file", path); 29680e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 29690e2d8f05SLen Brown if ((map >> shift) & 0x1) { 29708cb48b32SPrarit Bhargava so = shift + offset; 29718cb48b32SPrarit Bhargava sib_core = get_core_id(so); 29728cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 29738cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 29741b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0)) 29751b439f01SLen Brown cpus[so].thread_id = thread_id++; 29768cb48b32SPrarit Bhargava } 29770e2d8f05SLen Brown } 29780e2d8f05SLen Brown } 2979e13da9a1SColin Ian King } while (character == ','); 2980c98d5d94SLen Brown fclose(filep); 29810e2d8f05SLen Brown 29820e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2983c98d5d94SLen Brown } 2984c98d5d94SLen Brown 2985c98d5d94SLen Brown /* 2986c98d5d94SLen Brown * run func(thread, core, package) in topology order 2987c98d5d94SLen Brown * skip non-present cpus 2988103a8feaSLen Brown */ 2989103a8feaSLen Brown 2990c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 2991c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2992c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2993c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 29941b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 2995c98d5d94SLen Brown { 299640f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2997c98d5d94SLen Brown 2998c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 299940f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 30001b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 30011b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 3002c98d5d94SLen Brown struct thread_data *t, *t2; 3003c98d5d94SLen Brown struct core_data *c, *c2; 3004c98d5d94SLen Brown struct pkg_data *p, *p2; 3005c98d5d94SLen Brown 30061b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 3007c98d5d94SLen Brown 3008c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 3009c98d5d94SLen Brown continue; 3010c98d5d94SLen Brown 30111b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no); 3012c98d5d94SLen Brown 30131b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 30141b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no); 3015c98d5d94SLen Brown 3016c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 3017c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 3018c98d5d94SLen Brown 3019c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 3020c98d5d94SLen Brown if (retval) 3021c98d5d94SLen Brown return retval; 3022c98d5d94SLen Brown } 3023c98d5d94SLen Brown } 3024c98d5d94SLen Brown } 302540f5cfe7SPrarit Bhargava } 3026c98d5d94SLen Brown return 0; 3027c98d5d94SLen Brown } 3028c98d5d94SLen Brown 3029c98d5d94SLen Brown /* 3030c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 3031c98d5d94SLen Brown * return max_cpu number 3032c98d5d94SLen Brown */ 3033c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int)) 3034103a8feaSLen Brown { 3035103a8feaSLen Brown FILE *fp; 3036c98d5d94SLen Brown int cpu_num; 3037103a8feaSLen Brown int retval; 3038103a8feaSLen Brown 303957a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 3040103a8feaSLen Brown 3041103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 3042b2c95d90SJosh Triplett if (retval != 0) 3043b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 3044103a8feaSLen Brown 3045c98d5d94SLen Brown while (1) { 3046c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 3047103a8feaSLen Brown if (retval != 1) 3048103a8feaSLen Brown break; 3049103a8feaSLen Brown 3050c98d5d94SLen Brown retval = func(cpu_num); 3051c98d5d94SLen Brown if (retval) { 3052c98d5d94SLen Brown fclose(fp); 3053c98d5d94SLen Brown return (retval); 3054c98d5d94SLen Brown } 3055103a8feaSLen Brown } 3056103a8feaSLen Brown fclose(fp); 3057c98d5d94SLen Brown return 0; 3058103a8feaSLen Brown } 3059103a8feaSLen Brown 3060103a8feaSLen Brown void re_initialize(void) 3061103a8feaSLen Brown { 3062c98d5d94SLen Brown free_all_buffers(); 3063c98d5d94SLen Brown setup_all_buffers(); 30643d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 3065103a8feaSLen Brown } 3066103a8feaSLen Brown 3067843c5791SPrarit Bhargava void set_max_cpu_num(void) 3068843c5791SPrarit Bhargava { 3069843c5791SPrarit Bhargava FILE *filep; 30708201a028SPrarit Bhargava int base_cpu; 3071843c5791SPrarit Bhargava unsigned long dummy; 30728201a028SPrarit Bhargava char pathname[64]; 3073843c5791SPrarit Bhargava 30748201a028SPrarit Bhargava base_cpu = sched_getcpu(); 30758201a028SPrarit Bhargava if (base_cpu < 0) 30768201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 30771b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu); 30788201a028SPrarit Bhargava 30798201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 3080843c5791SPrarit Bhargava topo.max_cpu_num = 0; 3081843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 30820e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 3083843c5791SPrarit Bhargava fclose(filep); 3084843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 3085843c5791SPrarit Bhargava } 3086c98d5d94SLen Brown 3087103a8feaSLen Brown /* 3088c98d5d94SLen Brown * count_cpus() 3089c98d5d94SLen Brown * remember the last one seen, it will be the max 3090103a8feaSLen Brown */ 3091c98d5d94SLen Brown int count_cpus(int cpu) 3092103a8feaSLen Brown { 30939878bf7aSLen Brown UNUSED(cpu); 30949878bf7aSLen Brown 3095843c5791SPrarit Bhargava topo.num_cpus++; 3096c98d5d94SLen Brown return 0; 3097103a8feaSLen Brown } 30981b439f01SLen Brown 3099c98d5d94SLen Brown int mark_cpu_present(int cpu) 3100c98d5d94SLen Brown { 3101c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 310215aaa346SLen Brown return 0; 3103103a8feaSLen Brown } 3104103a8feaSLen Brown 31058cb48b32SPrarit Bhargava int init_thread_id(int cpu) 31068cb48b32SPrarit Bhargava { 31078cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 31088cb48b32SPrarit Bhargava return 0; 31098cb48b32SPrarit Bhargava } 31108cb48b32SPrarit Bhargava 3111562a2d37SLen Brown /* 3112562a2d37SLen Brown * snapshot_proc_interrupts() 3113562a2d37SLen Brown * 3114562a2d37SLen Brown * read and record summary of /proc/interrupts 3115562a2d37SLen Brown * 3116562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3117562a2d37SLen Brown */ 3118562a2d37SLen Brown int snapshot_proc_interrupts(void) 3119562a2d37SLen Brown { 3120562a2d37SLen Brown static FILE *fp; 3121562a2d37SLen Brown int column, retval; 3122562a2d37SLen Brown 3123562a2d37SLen Brown if (fp == NULL) 3124562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3125562a2d37SLen Brown else 3126562a2d37SLen Brown rewind(fp); 3127562a2d37SLen Brown 3128562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3129562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3130562a2d37SLen Brown int cpu_number; 3131562a2d37SLen Brown 3132562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3133562a2d37SLen Brown if (retval != 1) 3134562a2d37SLen Brown break; 3135562a2d37SLen Brown 3136562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3137562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3138562a2d37SLen Brown return 1; 3139562a2d37SLen Brown } 3140562a2d37SLen Brown 3141562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3142562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3143562a2d37SLen Brown } 3144562a2d37SLen Brown 3145562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3146562a2d37SLen Brown while (1) { 3147562a2d37SLen Brown int column; 3148562a2d37SLen Brown char buf[64]; 3149562a2d37SLen Brown 3150562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3151562a2d37SLen Brown if (retval != 1) 3152562a2d37SLen Brown break; 3153562a2d37SLen Brown 3154562a2d37SLen Brown /* read the count per cpu */ 3155562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3156562a2d37SLen Brown 3157562a2d37SLen Brown int cpu_number, irq_count; 3158562a2d37SLen Brown 3159562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3160562a2d37SLen Brown if (retval != 1) 3161562a2d37SLen Brown break; 3162562a2d37SLen Brown 3163562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3164562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3165562a2d37SLen Brown 3166562a2d37SLen Brown } 3167562a2d37SLen Brown 31681b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */ 3169562a2d37SLen Brown 3170562a2d37SLen Brown } 3171562a2d37SLen Brown return 0; 3172562a2d37SLen Brown } 31731b439f01SLen Brown 317427d47356SLen Brown /* 3175fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3176fdf676e5SLen Brown * 3177fdf676e5SLen Brown * record snapshot of 3178fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3179fdf676e5SLen Brown * 3180fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3181fdf676e5SLen Brown */ 3182fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3183fdf676e5SLen Brown { 3184fdf676e5SLen Brown FILE *fp; 3185fdf676e5SLen Brown int retval; 3186fdf676e5SLen Brown 3187fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3188fdf676e5SLen Brown 3189fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3190fdf676e5SLen Brown if (retval != 1) 3191fdf676e5SLen Brown err(1, "GFX rc6"); 3192fdf676e5SLen Brown 3193fdf676e5SLen Brown fclose(fp); 3194fdf676e5SLen Brown 3195fdf676e5SLen Brown return 0; 3196fdf676e5SLen Brown } 31971b439f01SLen Brown 3198fdf676e5SLen Brown /* 319927d47356SLen Brown * snapshot_gfx_mhz() 320027d47356SLen Brown * 320127d47356SLen Brown * record snapshot of 320227d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 320327d47356SLen Brown * 320427d47356SLen Brown * return 1 if config change requires a restart, else return 0 320527d47356SLen Brown */ 320627d47356SLen Brown int snapshot_gfx_mhz(void) 320727d47356SLen Brown { 320827d47356SLen Brown static FILE *fp; 320927d47356SLen Brown int retval; 321027d47356SLen Brown 321127d47356SLen Brown if (fp == NULL) 321227d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 321322048c54SLen Brown else { 321427d47356SLen Brown rewind(fp); 321522048c54SLen Brown fflush(fp); 321622048c54SLen Brown } 321727d47356SLen Brown 321827d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 321927d47356SLen Brown if (retval != 1) 322027d47356SLen Brown err(1, "GFX MHz"); 322127d47356SLen Brown 322227d47356SLen Brown return 0; 322327d47356SLen Brown } 3224562a2d37SLen Brown 3225562a2d37SLen Brown /* 3226b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3227b4b91569SRafael Antognolli * 3228b4b91569SRafael Antognolli * record snapshot of 3229b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3230b4b91569SRafael Antognolli * 3231b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3232b4b91569SRafael Antognolli */ 3233b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3234b4b91569SRafael Antognolli { 3235b4b91569SRafael Antognolli static FILE *fp; 3236b4b91569SRafael Antognolli int retval; 3237b4b91569SRafael Antognolli 3238b4b91569SRafael Antognolli if (fp == NULL) 3239b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3240b4b91569SRafael Antognolli else { 3241b4b91569SRafael Antognolli rewind(fp); 3242b4b91569SRafael Antognolli fflush(fp); 3243b4b91569SRafael Antognolli } 3244b4b91569SRafael Antognolli 3245b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3246b4b91569SRafael Antognolli if (retval != 1) 3247b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3248b4b91569SRafael Antognolli 3249b4b91569SRafael Antognolli return 0; 3250b4b91569SRafael Antognolli } 3251b4b91569SRafael Antognolli 3252b4b91569SRafael Antognolli /* 3253be0e54c4SLen Brown * snapshot_cpu_lpi() 3254be0e54c4SLen Brown * 3255be0e54c4SLen Brown * record snapshot of 3256be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3257be0e54c4SLen Brown */ 3258be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3259be0e54c4SLen Brown { 3260be0e54c4SLen Brown FILE *fp; 3261be0e54c4SLen Brown int retval; 3262be0e54c4SLen Brown 3263be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3264be0e54c4SLen Brown 3265be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 32665ea7647bSPrarit Bhargava if (retval != 1) { 32675ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 32685ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3269605736c6SGustavo A. R. Silva fclose(fp); 32705ea7647bSPrarit Bhargava return -1; 32715ea7647bSPrarit Bhargava } 3272be0e54c4SLen Brown 3273be0e54c4SLen Brown fclose(fp); 3274be0e54c4SLen Brown 3275be0e54c4SLen Brown return 0; 3276be0e54c4SLen Brown } 32771b439f01SLen Brown 3278be0e54c4SLen Brown /* 3279be0e54c4SLen Brown * snapshot_sys_lpi() 3280be0e54c4SLen Brown * 32811f81c5efSLen Brown * record snapshot of sys_lpi_file 3282be0e54c4SLen Brown */ 3283be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3284be0e54c4SLen Brown { 3285be0e54c4SLen Brown FILE *fp; 3286be0e54c4SLen Brown int retval; 3287be0e54c4SLen Brown 32881f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3289be0e54c4SLen Brown 3290be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 32915ea7647bSPrarit Bhargava if (retval != 1) { 32925ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 32935ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 329415423b95SColin Ian King fclose(fp); 32955ea7647bSPrarit Bhargava return -1; 32965ea7647bSPrarit Bhargava } 3297be0e54c4SLen Brown fclose(fp); 3298be0e54c4SLen Brown 3299be0e54c4SLen Brown return 0; 3300be0e54c4SLen Brown } 33011b439f01SLen Brown 3302be0e54c4SLen Brown /* 3303562a2d37SLen Brown * snapshot /proc and /sys files 3304562a2d37SLen Brown * 3305562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3306562a2d37SLen Brown */ 3307562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3308562a2d37SLen Brown { 3309218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3310562a2d37SLen Brown if (snapshot_proc_interrupts()) 3311562a2d37SLen Brown return 1; 3312562a2d37SLen Brown 3313812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3314fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3315fdf676e5SLen Brown 3316812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 331727d47356SLen Brown snapshot_gfx_mhz(); 331827d47356SLen Brown 3319b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3320b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3321b4b91569SRafael Antognolli 3322be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3323be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3324be0e54c4SLen Brown 3325be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3326be0e54c4SLen Brown snapshot_sys_lpi_us(); 3327be0e54c4SLen Brown 3328562a2d37SLen Brown return 0; 3329562a2d37SLen Brown } 3330562a2d37SLen Brown 33318aa2ed0bSLen Brown int exit_requested; 33328aa2ed0bSLen Brown 33338aa2ed0bSLen Brown static void signal_handler(int signal) 33348aa2ed0bSLen Brown { 33358aa2ed0bSLen Brown switch (signal) { 33368aa2ed0bSLen Brown case SIGINT: 33378aa2ed0bSLen Brown exit_requested = 1; 33388aa2ed0bSLen Brown if (debug) 33398aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 33408aa2ed0bSLen Brown break; 334107211960SLen Brown case SIGUSR1: 334207211960SLen Brown if (debug > 1) 334307211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 334407211960SLen Brown break; 33458aa2ed0bSLen Brown } 33468aa2ed0bSLen Brown } 33478aa2ed0bSLen Brown 33488aa2ed0bSLen Brown void setup_signal_handler(void) 33498aa2ed0bSLen Brown { 33508aa2ed0bSLen Brown struct sigaction sa; 33518aa2ed0bSLen Brown 33528aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 33538aa2ed0bSLen Brown 33548aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 33558aa2ed0bSLen Brown 33568aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 33578aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 335807211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 335907211960SLen Brown err(1, "sigaction SIGUSR1"); 33608aa2ed0bSLen Brown } 3361b9ad8ee0SLen Brown 336247936f94SArtem Bityutskiy void do_sleep(void) 3363b9ad8ee0SLen Brown { 3364c026c236SArtem Bityutskiy struct timeval tout; 3365c026c236SArtem Bityutskiy struct timespec rest; 3366b9ad8ee0SLen Brown fd_set readfds; 3367b9ad8ee0SLen Brown int retval; 3368b9ad8ee0SLen Brown 3369b9ad8ee0SLen Brown FD_ZERO(&readfds); 3370b9ad8ee0SLen Brown FD_SET(0, &readfds); 3371b9ad8ee0SLen Brown 3372c026c236SArtem Bityutskiy if (ignore_stdin) { 337347936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 337447936f94SArtem Bityutskiy return; 337547936f94SArtem Bityutskiy } 3376b9ad8ee0SLen Brown 3377c026c236SArtem Bityutskiy tout = interval_tv; 3378c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3379b9ad8ee0SLen Brown 3380b9ad8ee0SLen Brown if (retval == 1) { 3381b9ad8ee0SLen Brown switch (getc(stdin)) { 3382b9ad8ee0SLen Brown case 'q': 3383b9ad8ee0SLen Brown exit_requested = 1; 3384b9ad8ee0SLen Brown break; 3385c026c236SArtem Bityutskiy case EOF: 3386c026c236SArtem Bityutskiy /* 3387c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3388c026c236SArtem Bityutskiy * won't be any further input. 3389c026c236SArtem Bityutskiy */ 3390c026c236SArtem Bityutskiy ignore_stdin = 1; 3391c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3392c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3393c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3394c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3395b9ad8ee0SLen Brown } 3396b9ad8ee0SLen Brown } 3397b9ad8ee0SLen Brown } 339847936f94SArtem Bityutskiy 339987e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 340087e15da9SChen Yu { 340187e15da9SChen Yu int ret, idx; 340287e15da9SChen Yu unsigned long long msr_cur, msr_last; 340387e15da9SChen Yu 340487e15da9SChen Yu if (!per_cpu_msr_sum) 340587e15da9SChen Yu return 1; 340687e15da9SChen Yu 340787e15da9SChen Yu idx = offset_to_idx(offset); 340887e15da9SChen Yu if (idx < 0) 340987e15da9SChen Yu return idx; 341087e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 341187e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 341287e15da9SChen Yu if (ret) 341387e15da9SChen Yu return ret; 341487e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 341587e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 341687e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 341787e15da9SChen Yu 341887e15da9SChen Yu return 0; 341987e15da9SChen Yu } 342087e15da9SChen Yu 342187e15da9SChen Yu timer_t timerid; 342287e15da9SChen Yu 342387e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 342487e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 342587e15da9SChen Yu { 342687e15da9SChen Yu int i, ret; 342787e15da9SChen Yu int cpu = t->cpu_id; 342887e15da9SChen Yu 34299878bf7aSLen Brown UNUSED(c); 34309878bf7aSLen Brown UNUSED(p); 34319878bf7aSLen Brown 343287e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 343387e15da9SChen Yu unsigned long long msr_cur, msr_last; 343413a779deSCalvin Walton off_t offset; 343587e15da9SChen Yu 343687e15da9SChen Yu if (!idx_valid(i)) 343787e15da9SChen Yu continue; 343887e15da9SChen Yu offset = idx_to_offset(i); 343987e15da9SChen Yu if (offset < 0) 344087e15da9SChen Yu continue; 344187e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 344287e15da9SChen Yu if (ret) { 34431b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset); 344487e15da9SChen Yu continue; 344587e15da9SChen Yu } 344687e15da9SChen Yu 344787e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 344887e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 344987e15da9SChen Yu 345087e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 345187e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 345287e15da9SChen Yu } 345387e15da9SChen Yu return 0; 345487e15da9SChen Yu } 345587e15da9SChen Yu 34561b439f01SLen Brown static void msr_record_handler(union sigval v) 345787e15da9SChen Yu { 34589878bf7aSLen Brown UNUSED(v); 34599878bf7aSLen Brown 346087e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 346187e15da9SChen Yu } 346287e15da9SChen Yu 346387e15da9SChen Yu void msr_sum_record(void) 346487e15da9SChen Yu { 346587e15da9SChen Yu struct itimerspec its; 346687e15da9SChen Yu struct sigevent sev; 346787e15da9SChen Yu 346887e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 346987e15da9SChen Yu if (!per_cpu_msr_sum) { 347087e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 347187e15da9SChen Yu return; 347287e15da9SChen Yu } 347387e15da9SChen Yu /* 347487e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 347587e15da9SChen Yu */ 347687e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 347787e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 347887e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 347987e15da9SChen Yu 348087e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 348187e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 348287e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 348387e15da9SChen Yu goto release_msr; 348487e15da9SChen Yu } 348587e15da9SChen Yu 348687e15da9SChen Yu its.it_value.tv_sec = 0; 348787e15da9SChen Yu its.it_value.tv_nsec = 1; 348887e15da9SChen Yu /* 348987e15da9SChen Yu * A wraparound time has been calculated early. 349087e15da9SChen Yu * Some sources state that the peak power for a 349187e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 349287e15da9SChen Yu * use 2 * TDP for safety. 349387e15da9SChen Yu */ 349487e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 349587e15da9SChen Yu its.it_interval.tv_nsec = 0; 349687e15da9SChen Yu 349787e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 349887e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 349987e15da9SChen Yu goto release_timer; 350087e15da9SChen Yu } 350187e15da9SChen Yu return; 350287e15da9SChen Yu 350387e15da9SChen Yu release_timer: 350487e15da9SChen Yu timer_delete(timerid); 350587e15da9SChen Yu release_msr: 350687e15da9SChen Yu free(per_cpu_msr_sum); 350787e15da9SChen Yu } 35084c2122d4SLen Brown 350938c6663aSLen Brown /* 351038c6663aSLen Brown * set_my_sched_priority(pri) 351138c6663aSLen Brown * return previous 3512164d7a96SLen Brown * 3513164d7a96SLen Brown * if non-root, do this: 3514164d7a96SLen Brown * # /sbin/setcap cap_sys_rawio,cap_sys_nice=+ep /usr/bin/turbostat 351538c6663aSLen Brown */ 351638c6663aSLen Brown int set_my_sched_priority(int priority) 351738c6663aSLen Brown { 351838c6663aSLen Brown int retval; 351938c6663aSLen Brown int original_priority; 352038c6663aSLen Brown 352138c6663aSLen Brown errno = 0; 352238c6663aSLen Brown original_priority = getpriority(PRIO_PROCESS, 0); 352338c6663aSLen Brown if (errno && (original_priority == -1)) 352438c6663aSLen Brown err(errno, "getpriority"); 352538c6663aSLen Brown 352638c6663aSLen Brown retval = setpriority(PRIO_PROCESS, 0, priority); 352738c6663aSLen Brown if (retval) 352838c6663aSLen Brown err(retval, "setpriority(%d)", priority); 352938c6663aSLen Brown 353038c6663aSLen Brown errno = 0; 353138c6663aSLen Brown retval = getpriority(PRIO_PROCESS, 0); 353238c6663aSLen Brown if (retval != priority) 3533164d7a96SLen Brown err(retval, "getpriority(%d) != setpriority(%d)", retval, priority); 353438c6663aSLen Brown 353538c6663aSLen Brown return original_priority; 353638c6663aSLen Brown } 353738c6663aSLen Brown 3538103a8feaSLen Brown void turbostat_loop() 3539103a8feaSLen Brown { 3540c98d5d94SLen Brown int retval; 3541e52966c0SLen Brown int restarted = 0; 35429878bf7aSLen Brown unsigned int done_iters = 0; 3543c98d5d94SLen Brown 35448aa2ed0bSLen Brown setup_signal_handler(); 35458aa2ed0bSLen Brown 354638c6663aSLen Brown /* 354738c6663aSLen Brown * elevate own priority for interval mode 354838c6663aSLen Brown */ 354938c6663aSLen Brown set_my_sched_priority(-20); 355038c6663aSLen Brown 3551103a8feaSLen Brown restart: 3552e52966c0SLen Brown restarted++; 3553e52966c0SLen Brown 3554562a2d37SLen Brown snapshot_proc_sysfs_files(); 3555c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 35564c2122d4SLen Brown first_counter_read = 0; 3557d91bb17cSLen Brown if (retval < -1) { 3558d91bb17cSLen Brown exit(retval); 3559d91bb17cSLen Brown } else if (retval == -1) { 35603d7772eaSLen Brown if (restarted > 10) { 3561e52966c0SLen Brown exit(retval); 3562e52966c0SLen Brown } 3563c98d5d94SLen Brown re_initialize(); 3564c98d5d94SLen Brown goto restart; 3565c98d5d94SLen Brown } 3566e52966c0SLen Brown restarted = 0; 3567023fe0acSChen Yu done_iters = 0; 3568103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3569103a8feaSLen Brown 3570103a8feaSLen Brown while (1) { 3571c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3572103a8feaSLen Brown re_initialize(); 3573103a8feaSLen Brown goto restart; 3574103a8feaSLen Brown } 3575b9ad8ee0SLen Brown do_sleep(); 3576562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3577562a2d37SLen Brown goto restart; 3578c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3579d91bb17cSLen Brown if (retval < -1) { 3580d91bb17cSLen Brown exit(retval); 3581d91bb17cSLen Brown } else if (retval == -1) { 358215aaa346SLen Brown re_initialize(); 358315aaa346SLen Brown goto restart; 358415aaa346SLen Brown } 3585103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3586103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3587ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3588ba3dec99SLen Brown re_initialize(); 3589ba3dec99SLen Brown goto restart; 3590ba3dec99SLen Brown } 3591c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3592c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3593b7d8c148SLen Brown flush_output_stdout(); 35948aa2ed0bSLen Brown if (exit_requested) 35958aa2ed0bSLen Brown break; 3596023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3597023fe0acSChen Yu break; 3598b9ad8ee0SLen Brown do_sleep(); 3599562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3600562a2d37SLen Brown goto restart; 3601c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3602d91bb17cSLen Brown if (retval < -1) { 3603d91bb17cSLen Brown exit(retval); 3604d91bb17cSLen Brown } else if (retval == -1) { 3605103a8feaSLen Brown re_initialize(); 3606103a8feaSLen Brown goto restart; 3607103a8feaSLen Brown } 3608103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3609103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3610ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3611ba3dec99SLen Brown re_initialize(); 3612ba3dec99SLen Brown goto restart; 3613ba3dec99SLen Brown } 3614c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3615c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3616b7d8c148SLen Brown flush_output_stdout(); 36178aa2ed0bSLen Brown if (exit_requested) 36188aa2ed0bSLen Brown break; 3619023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3620023fe0acSChen Yu break; 3621103a8feaSLen Brown } 3622103a8feaSLen Brown } 3623103a8feaSLen Brown 3624103a8feaSLen Brown void check_dev_msr() 3625103a8feaSLen Brown { 3626103a8feaSLen Brown struct stat sb; 36277ce7d5deSPrarit Bhargava char pathname[32]; 3628103a8feaSLen Brown 36297ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 36307ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3631a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3632d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3633103a8feaSLen Brown } 3634103a8feaSLen Brown 3635fcaa681cSLen Brown /* 3636fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3637fcaa681cSLen Brown * return 0 on success 3638fcaa681cSLen Brown * return 1 on fail 3639fcaa681cSLen Brown */ 3640fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3641103a8feaSLen Brown { 3642fcaa681cSLen Brown cap_t caps; 3643fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3644fcaa681cSLen Brown 3645fcaa681cSLen Brown caps = cap_get_proc(); 3646fcaa681cSLen Brown if (caps == NULL) 3647fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3648fcaa681cSLen Brown 3649fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3650fcaa681cSLen Brown err(-6, "cap_get\n"); 3651fcaa681cSLen Brown 3652fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 36531b439f01SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3654fcaa681cSLen Brown return 1; 3655fcaa681cSLen Brown } 3656fcaa681cSLen Brown 3657fcaa681cSLen Brown if (cap_free(caps) == -1) 3658fcaa681cSLen Brown err(-6, "cap_free\n"); 3659fcaa681cSLen Brown 3660fcaa681cSLen Brown return 0; 3661fcaa681cSLen Brown } 36621b439f01SLen Brown 3663fcaa681cSLen Brown void check_permissions(void) 3664fcaa681cSLen Brown { 366598481e79SLen Brown int do_exit = 0; 36667ce7d5deSPrarit Bhargava char pathname[32]; 366798481e79SLen Brown 366898481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3669fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 367098481e79SLen Brown 367198481e79SLen Brown /* test file permissions */ 36727ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 36737ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 367498481e79SLen Brown do_exit++; 367598481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 367698481e79SLen Brown } 367798481e79SLen Brown 367898481e79SLen Brown /* if all else fails, thell them to be root */ 367998481e79SLen Brown if (do_exit) 3680b2c95d90SJosh Triplett if (getuid() != 0) 3681d7899447SLen Brown warnx("... or simply run as root"); 368298481e79SLen Brown 368398481e79SLen Brown if (do_exit) 368498481e79SLen Brown exit(-6); 3685103a8feaSLen Brown } 3686103a8feaSLen Brown 3687d7899447SLen Brown /* 3688d7899447SLen Brown * NHM adds support for additional MSRs: 3689d7899447SLen Brown * 3690d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3691d7899447SLen Brown * 3692ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 36931df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3694d7899447SLen Brown * 3695cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3696cf4cbe53SLen Brown * 3697d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3698d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3699d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3700d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3701d7899447SLen Brown * 3702ee7e38e3SLen Brown * Side effect: 37031df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 370433148d67SLen Brown * sets has_misc_feature_control 3705d7899447SLen Brown */ 3706ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3707103a8feaSLen Brown { 3708ee7e38e3SLen Brown unsigned long long msr; 370921ed5574SLen Brown unsigned int base_ratio; 3710ee7e38e3SLen Brown int *pkg_cstate_limits; 3711ee7e38e3SLen Brown 3712103a8feaSLen Brown if (!genuine_intel) 3713103a8feaSLen Brown return 0; 3714103a8feaSLen Brown 3715103a8feaSLen Brown if (family != 6) 3716103a8feaSLen Brown return 0; 3717103a8feaSLen Brown 371821ed5574SLen Brown bclk = discover_bclk(family, model); 371921ed5574SLen Brown 3720103a8feaSLen Brown switch (model) { 3721869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3722869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3723ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3724ee7e38e3SLen Brown break; 3725869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3726869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3727869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3728869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3729ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 373033148d67SLen Brown has_misc_feature_control = 1; 3731ee7e38e3SLen Brown break; 3732c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 37335e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3734869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 373577e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3736c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 37375e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3738869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3739af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3740af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3741ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 374233148d67SLen Brown has_misc_feature_control = 1; 3743ee7e38e3SLen Brown break; 3744d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3745d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 374633148d67SLen Brown has_misc_feature_control = 1; 3747d8ebb442SLen Brown break; 374825368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 374925368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits; 375025368d7cSChen Yu has_misc_feature_control = 1; 375125368d7cSChen Yu break; 3752f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3753cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 37549878bf7aSLen Brown /* FALLTHRU */ 37555ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3756ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3757ee7e38e3SLen Brown break; 3758869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3759ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3760cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3761ee7e38e3SLen Brown break; 3762869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3763ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3764ee7e38e3SLen Brown break; 3765869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3766f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 37675ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3768f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 376920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3770445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3771e4085d54SLen Brown break; 3772103a8feaSLen Brown default: 3773103a8feaSLen Brown return 0; 3774103a8feaSLen Brown } 37751df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3776e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3777ee7e38e3SLen Brown 3778ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 377921ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 378021ed5574SLen Brown 378121ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 378221ed5574SLen Brown has_base_hz = 1; 3783ee7e38e3SLen Brown return 1; 3784103a8feaSLen Brown } 37851b439f01SLen Brown 37860f7887c4SLen Brown /* 3787495c7654SLen Brown * SLV client has support for unique MSRs: 37880f7887c4SLen Brown * 37890f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 37900f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 37910f7887c4SLen Brown */ 37920f7887c4SLen Brown 37930f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 37940f7887c4SLen Brown { 37950f7887c4SLen Brown if (!genuine_intel) 37960f7887c4SLen Brown return 0; 37970f7887c4SLen Brown 37989878bf7aSLen Brown if (family != 6) 37999878bf7aSLen Brown return 0; 38009878bf7aSLen Brown 38010f7887c4SLen Brown switch (model) { 3802f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3803f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3804f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 38050f7887c4SLen Brown return 1; 38060f7887c4SLen Brown } 38070f7887c4SLen Brown return 0; 38080f7887c4SLen Brown } 38091b439f01SLen Brown 38107170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 38117170a374SLen Brown { 38127170a374SLen Brown 38137170a374SLen Brown if (!genuine_intel) 38147170a374SLen Brown return 0; 38157170a374SLen Brown 38169878bf7aSLen Brown if (family != 6) 38179878bf7aSLen Brown return 0; 38189878bf7aSLen Brown 38197170a374SLen Brown switch (model) { 38205ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 38217170a374SLen Brown return 1; 38227170a374SLen Brown } 38237170a374SLen Brown return 0; 38247170a374SLen Brown } 38251b439f01SLen Brown 3826ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3827ade0ebacSLen Brown { 3828ade0ebacSLen Brown 3829ade0ebacSLen Brown if (!genuine_intel) 3830ade0ebacSLen Brown return 0; 3831ade0ebacSLen Brown 38329878bf7aSLen Brown if (family != 6) 38339878bf7aSLen Brown return 0; 38349878bf7aSLen Brown 3835ade0ebacSLen Brown switch (model) { 3836ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3837ade0ebacSLen Brown return 1; 3838ade0ebacSLen Brown } 3839ade0ebacSLen Brown return 0; 3840ade0ebacSLen Brown } 38411b439f01SLen Brown 384234c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 384334c76197SLen Brown { 384434c76197SLen Brown 384534c76197SLen Brown if (!genuine_intel) 384634c76197SLen Brown return 0; 384734c76197SLen Brown 38489878bf7aSLen Brown if (family != 6) 38499878bf7aSLen Brown return 0; 38509878bf7aSLen Brown 385134c76197SLen Brown switch (model) { 385234c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 385334c76197SLen Brown return 1; 385434c76197SLen Brown } 385534c76197SLen Brown return 0; 385634c76197SLen Brown } 385725368d7cSChen Yu 385825368d7cSChen Yu int is_icx(unsigned int family, unsigned int model) 385925368d7cSChen Yu { 386025368d7cSChen Yu 386125368d7cSChen Yu if (!genuine_intel) 386225368d7cSChen Yu return 0; 386325368d7cSChen Yu 38649878bf7aSLen Brown if (family != 6) 38659878bf7aSLen Brown return 0; 38669878bf7aSLen Brown 386725368d7cSChen Yu switch (model) { 386825368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 386925368d7cSChen Yu return 1; 387025368d7cSChen Yu } 387125368d7cSChen Yu return 0; 387225368d7cSChen Yu } 387325368d7cSChen Yu 3874f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3875f6708400SChen Yu { 3876f6708400SChen Yu if (!genuine_intel) 3877f6708400SChen Yu return 0; 3878f6708400SChen Yu 38799878bf7aSLen Brown if (family != 6) 38809878bf7aSLen Brown return 0; 38819878bf7aSLen Brown 3882f6708400SChen Yu switch (model) { 3883f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3884f6708400SChen Yu return 1; 3885f6708400SChen Yu } 3886f6708400SChen Yu return 0; 3887f6708400SChen Yu } 38881b439f01SLen Brown 388920de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 389020de0dabSAntti Laakso { 389120de0dabSAntti Laakso if (!genuine_intel) 389220de0dabSAntti Laakso return 0; 389320de0dabSAntti Laakso 38949878bf7aSLen Brown if (family != 6) 38959878bf7aSLen Brown return 0; 38969878bf7aSLen Brown 389720de0dabSAntti Laakso switch (model) { 389820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 389920de0dabSAntti Laakso return 1; 390020de0dabSAntti Laakso } 390120de0dabSAntti Laakso return 0; 390220de0dabSAntti Laakso } 39030f7887c4SLen Brown 390431e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3905d7899447SLen Brown { 39060f7887c4SLen Brown if (has_slv_msrs(family, model)) 39070f7887c4SLen Brown return 0; 39080f7887c4SLen Brown 39099878bf7aSLen Brown if (family != 6) 39109878bf7aSLen Brown return 0; 39119878bf7aSLen Brown 3912d7899447SLen Brown switch (model) { 3913d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3914869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3915869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3916d7899447SLen Brown return 0; 3917d7899447SLen Brown default: 3918d7899447SLen Brown return 1; 3919d7899447SLen Brown } 3920d7899447SLen Brown } 39211b439f01SLen Brown 39220f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 39230f7887c4SLen Brown { 39240f7887c4SLen Brown if (has_slv_msrs(family, model)) 39250f7887c4SLen Brown return 1; 39260f7887c4SLen Brown 39270f7887c4SLen Brown return 0; 39280f7887c4SLen Brown } 39291b439f01SLen Brown 39306574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 39316574a5d5SLen Brown { 39326574a5d5SLen Brown if (!genuine_intel) 39336574a5d5SLen Brown return 0; 39346574a5d5SLen Brown 39356574a5d5SLen Brown if (family != 6) 39366574a5d5SLen Brown return 0; 39376574a5d5SLen Brown 39386574a5d5SLen Brown switch (model) { 3939869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3940869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 39416574a5d5SLen Brown return 1; 39426574a5d5SLen Brown default: 39436574a5d5SLen Brown return 0; 39446574a5d5SLen Brown } 39456574a5d5SLen Brown } 39461b439f01SLen Brown 3947fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3948fcd17211SLen Brown { 3949fcd17211SLen Brown if (!genuine_intel) 3950fcd17211SLen Brown return 0; 3951fcd17211SLen Brown 3952fcd17211SLen Brown if (family != 6) 3953fcd17211SLen Brown return 0; 3954fcd17211SLen Brown 3955fcd17211SLen Brown switch (model) { 3956869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3957fcd17211SLen Brown return 1; 3958fcd17211SLen Brown default: 3959fcd17211SLen Brown return 0; 3960fcd17211SLen Brown } 3961fcd17211SLen Brown } 3962fcd17211SLen Brown 3963fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3964fb5d4327SDasaratharaman Chandramouli { 3965fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3966fb5d4327SDasaratharaman Chandramouli return 0; 3967fb5d4327SDasaratharaman Chandramouli 3968fb5d4327SDasaratharaman Chandramouli if (family != 6) 3969fb5d4327SDasaratharaman Chandramouli return 0; 3970fb5d4327SDasaratharaman Chandramouli 3971fb5d4327SDasaratharaman Chandramouli switch (model) { 3972869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3973fb5d4327SDasaratharaman Chandramouli return 1; 3974fb5d4327SDasaratharaman Chandramouli default: 3975fb5d4327SDasaratharaman Chandramouli return 0; 3976fb5d4327SDasaratharaman Chandramouli } 3977fb5d4327SDasaratharaman Chandramouli } 39781b439f01SLen Brown 397931e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 398031e07522SLen Brown { 398131e07522SLen Brown if (!genuine_intel) 398231e07522SLen Brown return 0; 398331e07522SLen Brown 398431e07522SLen Brown if (family != 6) 398531e07522SLen Brown return 0; 398631e07522SLen Brown 398731e07522SLen Brown switch (model) { 398831e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 398931e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 399025368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 399131e07522SLen Brown return 1; 399231e07522SLen Brown default: 399331e07522SLen Brown return 0; 399431e07522SLen Brown } 399531e07522SLen Brown } 39961b439f01SLen Brown 39976fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 39986fb3143bSLen Brown { 39996fb3143bSLen Brown if (!genuine_intel) 40006fb3143bSLen Brown return 0; 40016fb3143bSLen Brown 40026fb3143bSLen Brown if (family != 6) 40036fb3143bSLen Brown return 0; 40046fb3143bSLen Brown 40056fb3143bSLen Brown switch (model) { 4006869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4007c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4008869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 400977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 40105e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4011c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 40125e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4013869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4014af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4015af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4016869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 401725368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 40186fb3143bSLen Brown 4019869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 40206fb3143bSLen Brown return 1; 40216fb3143bSLen Brown default: 40226fb3143bSLen Brown return 0; 40236fb3143bSLen Brown } 40246fb3143bSLen Brown } 40256fb3143bSLen Brown 40260b9a0b9bSZhang Rui /* 40270b9a0b9bSZhang Rui * tcc_offset_bits: 40280b9a0b9bSZhang Rui * 0: Tcc Offset not supported (Default) 40290b9a0b9bSZhang Rui * 6: Bit 29:24 of MSR_PLATFORM_INFO 40300b9a0b9bSZhang Rui * 4: Bit 27:24 of MSR_PLATFORM_INFO 40310b9a0b9bSZhang Rui */ 40320b9a0b9bSZhang Rui void check_tcc_offset(int model) 40330b9a0b9bSZhang Rui { 40340b9a0b9bSZhang Rui unsigned long long msr; 40350b9a0b9bSZhang Rui 40360b9a0b9bSZhang Rui if (!genuine_intel) 40370b9a0b9bSZhang Rui return; 40380b9a0b9bSZhang Rui 40390b9a0b9bSZhang Rui switch (model) { 40400b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE_L: 40410b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE: 40420b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE_L: 40430b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE: 40440b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE_L: 40450b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE: 40460b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE_L: 40470b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE: 40480b9a0b9bSZhang Rui case INTEL_FAM6_COMETLAKE: 40490b9a0b9bSZhang Rui if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) { 40500b9a0b9bSZhang Rui msr = (msr >> 30) & 1; 40510b9a0b9bSZhang Rui if (msr) 40520b9a0b9bSZhang Rui tcc_offset_bits = 6; 40530b9a0b9bSZhang Rui } 40540b9a0b9bSZhang Rui return; 40550b9a0b9bSZhang Rui default: 40560b9a0b9bSZhang Rui return; 40570b9a0b9bSZhang Rui } 40580b9a0b9bSZhang Rui } 40590b9a0b9bSZhang Rui 40601b439f01SLen Brown static void remove_underbar(char *s) 4061fecb3bc8SDavid Arcari { 4062fecb3bc8SDavid Arcari char *to = s; 4063fecb3bc8SDavid Arcari 4064fecb3bc8SDavid Arcari while (*s) { 4065fecb3bc8SDavid Arcari if (*s != '_') 4066fecb3bc8SDavid Arcari *to++ = *s; 4067fecb3bc8SDavid Arcari s++; 4068fecb3bc8SDavid Arcari } 4069fecb3bc8SDavid Arcari 4070fecb3bc8SDavid Arcari *to = 0; 4071fecb3bc8SDavid Arcari } 4072fecb3bc8SDavid Arcari 40731b439f01SLen Brown static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 4074fcd17211SLen Brown { 4075fcd17211SLen Brown if (!do_nhm_platform_info) 4076fcd17211SLen Brown return; 4077fcd17211SLen Brown 4078fcd17211SLen Brown dump_nhm_platform_info(); 4079fcd17211SLen Brown 4080fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 4081fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 4082fcd17211SLen Brown 4083fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 4084fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 4085fcd17211SLen Brown 408631e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 408731e07522SLen Brown dump_turbo_ratio_limits(family, model); 4088fcd17211SLen Brown 40890f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 40900f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 40910f7887c4SLen Brown 4092fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 4093fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 4094fb5d4327SDasaratharaman Chandramouli 40956fb3143bSLen Brown if (has_config_tdp(family, model)) 40966fb3143bSLen Brown dump_config_tdp(); 40976fb3143bSLen Brown 4098fcd17211SLen Brown dump_nhm_cst_cfg(); 4099fcd17211SLen Brown } 4100fcd17211SLen Brown 4101abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 4102abdcbdb2SLen Brown { 4103abdcbdb2SLen Brown FILE *input; 4104abdcbdb2SLen Brown char cpuidle_buf[64]; 4105abdcbdb2SLen Brown 4106abdcbdb2SLen Brown input = fopen(path, "r"); 4107abdcbdb2SLen Brown if (input == NULL) { 4108abdcbdb2SLen Brown if (debug) 4109abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 4110abdcbdb2SLen Brown return; 4111abdcbdb2SLen Brown } 4112abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 4113abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 4114abdcbdb2SLen Brown fclose(input); 4115abdcbdb2SLen Brown 4116abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 4117abdcbdb2SLen Brown } 41181b439f01SLen Brown 41191b439f01SLen Brown static void dump_sysfs_cstate_config(void) 412041618e63SLen Brown { 412141618e63SLen Brown char path[64]; 412241618e63SLen Brown char name_buf[16]; 412341618e63SLen Brown char desc[64]; 412441618e63SLen Brown FILE *input; 412541618e63SLen Brown int state; 412641618e63SLen Brown char *sp; 412741618e63SLen Brown 4128abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 4129abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 4130abdcbdb2SLen Brown return; 4131abdcbdb2SLen Brown } 4132abdcbdb2SLen Brown 4133abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 4134abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 4135abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 4136abdcbdb2SLen Brown 413741618e63SLen Brown for (state = 0; state < 10; ++state) { 413841618e63SLen Brown 41391b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 414041618e63SLen Brown input = fopen(path, "r"); 414141618e63SLen Brown if (input == NULL) 414241618e63SLen Brown continue; 41438173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 41448173c336SBen Hutchings err(1, "%s: failed to read file", path); 414541618e63SLen Brown 414641618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 414741618e63SLen Brown sp = strchr(name_buf, '-'); 414841618e63SLen Brown if (!sp) 414941618e63SLen Brown sp = strchrnul(name_buf, '\n'); 415041618e63SLen Brown *sp = '\0'; 415141618e63SLen Brown fclose(input); 415241618e63SLen Brown 4153fecb3bc8SDavid Arcari remove_underbar(name_buf); 4154fecb3bc8SDavid Arcari 41551b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state); 415641618e63SLen Brown input = fopen(path, "r"); 415741618e63SLen Brown if (input == NULL) 415841618e63SLen Brown continue; 41598173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 41608173c336SBen Hutchings err(1, "%s: failed to read file", path); 416141618e63SLen Brown 416241618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 416341618e63SLen Brown fclose(input); 416441618e63SLen Brown } 416541618e63SLen Brown } 41661b439f01SLen Brown 41671b439f01SLen Brown static void dump_sysfs_pstate_config(void) 41687293fccdSLen Brown { 41697293fccdSLen Brown char path[64]; 41707293fccdSLen Brown char driver_buf[64]; 41717293fccdSLen Brown char governor_buf[64]; 41727293fccdSLen Brown FILE *input; 41737293fccdSLen Brown int turbo; 41747293fccdSLen Brown 41751b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu); 41767293fccdSLen Brown input = fopen(path, "r"); 41777293fccdSLen Brown if (input == NULL) { 41780a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 41797293fccdSLen Brown return; 41807293fccdSLen Brown } 41818173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 41828173c336SBen Hutchings err(1, "%s: failed to read file", path); 41837293fccdSLen Brown fclose(input); 41847293fccdSLen Brown 41851b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu); 41867293fccdSLen Brown input = fopen(path, "r"); 41877293fccdSLen Brown if (input == NULL) { 41880a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 41897293fccdSLen Brown return; 41907293fccdSLen Brown } 41918173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 41928173c336SBen Hutchings err(1, "%s: failed to read file", path); 41937293fccdSLen Brown fclose(input); 41947293fccdSLen Brown 41957293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 41967293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 41977293fccdSLen Brown 41987293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 41997293fccdSLen Brown input = fopen(path, "r"); 42007293fccdSLen Brown if (input != NULL) { 42018173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 42028173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 42037293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 42047293fccdSLen Brown fclose(input); 42057293fccdSLen Brown } 42067293fccdSLen Brown 42077293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 42087293fccdSLen Brown input = fopen(path, "r"); 42097293fccdSLen Brown if (input != NULL) { 42108173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 42118173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 42127293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 42137293fccdSLen Brown fclose(input); 42147293fccdSLen Brown } 42157293fccdSLen Brown } 421641618e63SLen Brown 4217889facbeSLen Brown /* 4218889facbeSLen Brown * print_epb() 4219889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 4220889facbeSLen Brown */ 4221889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4222889facbeSLen Brown { 4223889facbeSLen Brown char *epb_string; 42246d6501d9SBorislav Petkov int cpu, epb; 4225889facbeSLen Brown 42269878bf7aSLen Brown UNUSED(c); 42279878bf7aSLen Brown UNUSED(p); 42289878bf7aSLen Brown 4229889facbeSLen Brown if (!has_epb) 4230889facbeSLen Brown return 0; 4231889facbeSLen Brown 4232889facbeSLen Brown cpu = t->cpu_id; 4233889facbeSLen Brown 4234889facbeSLen Brown /* EPB is per-package */ 4235889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4236889facbeSLen Brown return 0; 4237889facbeSLen Brown 4238889facbeSLen Brown if (cpu_migrate(cpu)) { 42393d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 4240889facbeSLen Brown return -1; 4241889facbeSLen Brown } 4242889facbeSLen Brown 42436d6501d9SBorislav Petkov epb = get_epb(cpu); 42446d6501d9SBorislav Petkov if (epb < 0) 4245889facbeSLen Brown return 0; 4246889facbeSLen Brown 42476d6501d9SBorislav Petkov switch (epb) { 4248889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 4249889facbeSLen Brown epb_string = "performance"; 4250889facbeSLen Brown break; 4251889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 4252889facbeSLen Brown epb_string = "balanced"; 4253889facbeSLen Brown break; 4254889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 4255889facbeSLen Brown epb_string = "powersave"; 4256889facbeSLen Brown break; 4257889facbeSLen Brown default: 4258889facbeSLen Brown epb_string = "custom"; 4259889facbeSLen Brown break; 4260889facbeSLen Brown } 42616d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 4262889facbeSLen Brown 4263889facbeSLen Brown return 0; 4264889facbeSLen Brown } 42651b439f01SLen Brown 42667f5c258eSLen Brown /* 42677f5c258eSLen Brown * print_hwp() 42687f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 42697f5c258eSLen Brown */ 42707f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 42717f5c258eSLen Brown { 42727f5c258eSLen Brown unsigned long long msr; 42737f5c258eSLen Brown int cpu; 42747f5c258eSLen Brown 42759878bf7aSLen Brown UNUSED(c); 42769878bf7aSLen Brown UNUSED(p); 42779878bf7aSLen Brown 42787f5c258eSLen Brown if (!has_hwp) 42797f5c258eSLen Brown return 0; 42807f5c258eSLen Brown 42817f5c258eSLen Brown cpu = t->cpu_id; 42827f5c258eSLen Brown 42837f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 42847f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 42857f5c258eSLen Brown return 0; 42867f5c258eSLen Brown 42877f5c258eSLen Brown if (cpu_migrate(cpu)) { 42883d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 42897f5c258eSLen Brown return -1; 42907f5c258eSLen Brown } 42917f5c258eSLen Brown 42927f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 42937f5c258eSLen Brown return 0; 42947f5c258eSLen Brown 42951b439f01SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 42967f5c258eSLen Brown 42977f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 42987f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 42997f5c258eSLen Brown return 0; 43007f5c258eSLen Brown 43017f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 43027f5c258eSLen Brown return 0; 43037f5c258eSLen Brown 4304b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 43056dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 43067f5c258eSLen Brown cpu, msr, 43077f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 43087f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 43091b439f01SLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr)); 43107f5c258eSLen Brown 43117f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 43127f5c258eSLen Brown return 0; 43137f5c258eSLen Brown 4314b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 43156dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 43167f5c258eSLen Brown cpu, msr, 43177f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 43187f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 43197f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 43207f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 43211b439f01SLen Brown (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1)); 43227f5c258eSLen Brown 43237f5c258eSLen Brown if (has_hwp_pkg) { 43247f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 43257f5c258eSLen Brown return 0; 43267f5c258eSLen Brown 4327b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 43286dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 43297f5c258eSLen Brown cpu, msr, 43307f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 43317f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 43327f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 43331b439f01SLen Brown (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3)); 43347f5c258eSLen Brown } 43357f5c258eSLen Brown if (has_hwp_notify) { 43367f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 43377f5c258eSLen Brown return 0; 43387f5c258eSLen Brown 4339b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 43407f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 43411b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis"); 43427f5c258eSLen Brown } 43437f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 43447f5c258eSLen Brown return 0; 43457f5c258eSLen Brown 4346b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 43477f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 43481b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-"); 4349889facbeSLen Brown 4350889facbeSLen Brown return 0; 4351889facbeSLen Brown } 4352889facbeSLen Brown 43533a9a941dSLen Brown /* 43543a9a941dSLen Brown * print_perf_limit() 43553a9a941dSLen Brown */ 43563a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 43573a9a941dSLen Brown { 43583a9a941dSLen Brown unsigned long long msr; 43593a9a941dSLen Brown int cpu; 43603a9a941dSLen Brown 43619878bf7aSLen Brown UNUSED(c); 43629878bf7aSLen Brown UNUSED(p); 43639878bf7aSLen Brown 43643a9a941dSLen Brown cpu = t->cpu_id; 43653a9a941dSLen Brown 43663a9a941dSLen Brown /* per-package */ 43673a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 43683a9a941dSLen Brown return 0; 43693a9a941dSLen Brown 43703a9a941dSLen Brown if (cpu_migrate(cpu)) { 43713d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 43723a9a941dSLen Brown return -1; 43733a9a941dSLen Brown } 43743a9a941dSLen Brown 43753a9a941dSLen Brown if (do_core_perf_limit_reasons) { 43763a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4377b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4378b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4379e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 43803a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4381e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4382e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4383e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4384e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4385e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4386e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4387e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4388e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4389e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4390e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 43911b439f01SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : ""); 4392b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4393e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 43943a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4395e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4396e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4397e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4398e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4399e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4400e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4401e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4402e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4403e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4404e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 44051b439f01SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : ""); 44063a9a941dSLen Brown 44073a9a941dSLen Brown } 44083a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 44093a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4410b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4411b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 44123a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 44133a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 44143a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 44153a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 44163a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 44173a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 44181b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4419b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 44203a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 44213a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 44223a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 44233a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 44243a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 44253a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 44261b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 44273a9a941dSLen Brown } 44283a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 44293a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4430b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4431b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 44323a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 44333a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 44343a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 44353a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 44361b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4437b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 44383a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 44393a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 44403a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 44413a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 44421b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 44433a9a941dSLen Brown } 44443a9a941dSLen Brown return 0; 44453a9a941dSLen Brown } 44463a9a941dSLen Brown 4447889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4448889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4449889facbeSLen Brown 44509392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4451144b44b1SLen Brown { 4452144b44b1SLen Brown unsigned long long msr; 4453144b44b1SLen Brown 4454144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 44557ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4456144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4457144b44b1SLen Brown 4458144b44b1SLen Brown switch (model) { 4459f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 44605ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4461144b44b1SLen Brown return 30.0; 4462144b44b1SLen Brown default: 4463144b44b1SLen Brown return 135.0; 4464144b44b1SLen Brown } 4465144b44b1SLen Brown } 4466144b44b1SLen Brown 44679392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 44689392bd98SCalvin Walton { 44699878bf7aSLen Brown UNUSED(family); 44709878bf7aSLen Brown 447133eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 447233eb8225SKim Phillips return 280.0; 44739392bd98SCalvin Walton } 44749392bd98SCalvin Walton 447540ee8e3bSAndrey Semin /* 447640ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 447740ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 447840ee8e3bSAndrey Semin */ 44791b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units) 448040ee8e3bSAndrey Semin { 448140ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 448240ee8e3bSAndrey Semin 448340ee8e3bSAndrey Semin switch (model) { 4484869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4485869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4486abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4487869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 44886397b641SLen Brown case INTEL_FAM6_ICELAKE_X: /* ICX */ 448940ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 449040ee8e3bSAndrey Semin default: 449140ee8e3bSAndrey Semin return (rapl_energy_units); 449240ee8e3bSAndrey Semin } 449340ee8e3bSAndrey Semin } 449440ee8e3bSAndrey Semin 44959392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4496889facbeSLen Brown { 4497889facbeSLen Brown unsigned long long msr; 4498144b44b1SLen Brown unsigned int time_unit; 4499889facbeSLen Brown double tdp; 4500889facbeSLen Brown 4501889facbeSLen Brown if (family != 6) 4502889facbeSLen Brown return; 4503889facbeSLen Brown 4504889facbeSLen Brown switch (model) { 4505869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4506869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4507c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 450877e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 45095e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4510c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 45115e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4512144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4513812db3f7SLen Brown if (rapl_joules) { 4514812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4515812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4516812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4517812db3f7SLen Brown } else { 4518812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4519812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4520812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4521812db3f7SLen Brown } 4522889facbeSLen Brown break; 4523869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4524f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4525e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4526812db3f7SLen Brown if (rapl_joules) 4527812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4528812db3f7SLen Brown else 4529812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4530e4085d54SLen Brown break; 4531f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 45321b439f01SLen Brown do_rapl = 45331b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 45341b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4535f6708400SChen Yu if (rapl_joules) { 4536f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4537f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4538f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4539f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4540f6708400SChen Yu } else { 4541f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4542f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4543f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4544f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4545f6708400SChen Yu } 4546f6708400SChen Yu break; 454720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 454820de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 454920de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 455020de0dabSAntti Laakso if (rapl_joules) 455120de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 455220de0dabSAntti Laakso else 455320de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 455420de0dabSAntti Laakso break; 4555af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4556af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 45571b439f01SLen Brown do_rapl = 45581b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 45591b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4560812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4561812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4562812db3f7SLen Brown if (rapl_joules) { 4563812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4564812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4565812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 456681824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4567812db3f7SLen Brown } else { 4568812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4569812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4570812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 457181824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4572812db3f7SLen Brown } 45730b2bb692SLen Brown break; 4574869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4575869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4576869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 457725368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4578869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 45791b439f01SLen Brown do_rapl = 45801b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 45811b439f01SLen Brown RAPL_PKG_POWER_INFO; 4582812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4583812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4584812db3f7SLen Brown if (rapl_joules) { 4585812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4586812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4587812db3f7SLen Brown } else { 4588812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4589812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4590812db3f7SLen Brown } 4591e6f9bb3cSLen Brown break; 4592869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4593869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 45941b439f01SLen Brown do_rapl = 45951b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | 45961b439f01SLen Brown RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4597812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4598812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4599812db3f7SLen Brown if (rapl_joules) { 4600812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4601812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4602812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4603812db3f7SLen Brown } else { 4604812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4605812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4606812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4607812db3f7SLen Brown } 4608144b44b1SLen Brown break; 4609f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 46105ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4611144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4612812db3f7SLen Brown if (rapl_joules) { 4613812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4614812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4615812db3f7SLen Brown } else { 4616812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4617812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4618812db3f7SLen Brown } 4619889facbeSLen Brown break; 46205ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 46211b439f01SLen Brown do_rapl = 46221b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 46231b439f01SLen Brown RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4624812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4625812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4626812db3f7SLen Brown if (rapl_joules) { 4627812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4628812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4629812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4630812db3f7SLen Brown } else { 4631812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4632812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4633812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4634812db3f7SLen Brown } 46350f644909SJacob Pan break; 4636889facbeSLen Brown default: 4637889facbeSLen Brown return; 4638889facbeSLen Brown } 4639889facbeSLen Brown 4640889facbeSLen Brown /* units on package 0, verify later other packages match */ 46417ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4642889facbeSLen Brown return; 4643889facbeSLen Brown 4644889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4645f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4646144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4647144b44b1SLen Brown else 4648889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4649889facbeSLen Brown 465040ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 465140ee8e3bSAndrey Semin 4652144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4653144b44b1SLen Brown if (time_unit == 0) 4654144b44b1SLen Brown time_unit = 0xA; 4655889facbeSLen Brown 4656144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4657144b44b1SLen Brown 46589392bd98SCalvin Walton tdp = get_tdp_intel(model); 4659889facbeSLen Brown 4660889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 466196e47158SLen Brown if (!quiet) 4662b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 46639392bd98SCalvin Walton } 4664889facbeSLen Brown 46659392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 46669392bd98SCalvin Walton { 46679392bd98SCalvin Walton unsigned long long msr; 46689392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 46699392bd98SCalvin Walton unsigned int has_rapl = 0; 46709392bd98SCalvin Walton double tdp; 46719392bd98SCalvin Walton 46729878bf7aSLen Brown UNUSED(model); 46739878bf7aSLen Brown 46749392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 46759392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 467633eb8225SKim Phillips /* RAPL (Fam 17h+) */ 46779392bd98SCalvin Walton has_rapl = edx & (1 << 14); 46789392bd98SCalvin Walton } 46799392bd98SCalvin Walton 468033eb8225SKim Phillips if (!has_rapl || family < 0x17) 4681889facbeSLen Brown return; 46829392bd98SCalvin Walton 46839392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 46843316f99aSCalvin Walton if (rapl_joules) { 46853316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 46869392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 46873316f99aSCalvin Walton } else { 46883316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 46899392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 46903316f99aSCalvin Walton } 46919392bd98SCalvin Walton 46929392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 46939392bd98SCalvin Walton return; 46949392bd98SCalvin Walton 46959392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 46969392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 46979392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 46989392bd98SCalvin Walton 46999cfa8e04SPu Wen tdp = get_tdp_amd(family); 47009392bd98SCalvin Walton 47019392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 47029392bd98SCalvin Walton if (!quiet) 47039392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 47049392bd98SCalvin Walton } 47059392bd98SCalvin Walton 47069392bd98SCalvin Walton /* 47079392bd98SCalvin Walton * rapl_probe() 47089392bd98SCalvin Walton * 47099392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 47109392bd98SCalvin Walton */ 47119392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 47129392bd98SCalvin Walton { 47139392bd98SCalvin Walton if (genuine_intel) 47149392bd98SCalvin Walton rapl_probe_intel(family, model); 4715c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 47169392bd98SCalvin Walton rapl_probe_amd(family, model); 4717889facbeSLen Brown } 4718889facbeSLen Brown 47191b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 47203a9a941dSLen Brown { 47213a9a941dSLen Brown if (!genuine_intel) 47223a9a941dSLen Brown return; 47233a9a941dSLen Brown 47243a9a941dSLen Brown if (family != 6) 47253a9a941dSLen Brown return; 47263a9a941dSLen Brown 47273a9a941dSLen Brown switch (model) { 4728c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 472977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 47305e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 47313a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 47329878bf7aSLen Brown /* FALLTHRU */ 4733869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 47343a9a941dSLen Brown do_core_perf_limit_reasons = 1; 47353a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 47363a9a941dSLen Brown default: 47373a9a941dSLen Brown return; 47383a9a941dSLen Brown } 47393a9a941dSLen Brown } 47403a9a941dSLen Brown 4741ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4742ac980e13SArtem Bityutskiy { 47431b439f01SLen Brown if (is_skx(family, model) || is_bdx(family, model) || is_icx(family, model)) 4744ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4745ac980e13SArtem Bityutskiy } 4746ac980e13SArtem Bityutskiy 4747aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model) 4748aeb01e6dSChen Yu { 4749aeb01e6dSChen Yu if (is_icx(family, model)) 4750aeb01e6dSChen Yu dis_cstate_prewake = 1; 4751aeb01e6dSChen Yu } 4752aeb01e6dSChen Yu 4753889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4754889facbeSLen Brown { 4755889facbeSLen Brown unsigned long long msr; 4756f4896fa5SLen Brown unsigned int dts, dts2; 4757889facbeSLen Brown int cpu; 4758889facbeSLen Brown 47599878bf7aSLen Brown UNUSED(c); 47609878bf7aSLen Brown UNUSED(p); 47619878bf7aSLen Brown 4762889facbeSLen Brown if (!(do_dts || do_ptm)) 4763889facbeSLen Brown return 0; 4764889facbeSLen Brown 4765889facbeSLen Brown cpu = t->cpu_id; 4766889facbeSLen Brown 4767889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4768889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4769889facbeSLen Brown return 0; 4770889facbeSLen Brown 4771889facbeSLen Brown if (cpu_migrate(cpu)) { 47723d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4773889facbeSLen Brown return -1; 4774889facbeSLen Brown } 4775889facbeSLen Brown 4776889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4777889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4778889facbeSLen Brown return 0; 4779889facbeSLen Brown 4780889facbeSLen Brown dts = (msr >> 16) & 0x7F; 47811b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts); 4782889facbeSLen Brown 4783889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4784889facbeSLen Brown return 0; 4785889facbeSLen Brown 4786889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4787889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4788b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 478955279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4790889facbeSLen Brown } 4791889facbeSLen Brown 4792f4896fa5SLen Brown if (do_dts && debug) { 4793889facbeSLen Brown unsigned int resolution; 4794889facbeSLen Brown 4795889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4796889facbeSLen Brown return 0; 4797889facbeSLen Brown 4798889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4799889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4800b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 480155279aefSZhang Rui cpu, msr, tj_max - dts, resolution); 4802889facbeSLen Brown 4803889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4804889facbeSLen Brown return 0; 4805889facbeSLen Brown 4806889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4807889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4808b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 480955279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4810889facbeSLen Brown } 4811889facbeSLen Brown 4812889facbeSLen Brown return 0; 4813889facbeSLen Brown } 4814889facbeSLen Brown 4815889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4816889facbeSLen Brown { 48176b398625SSumeet Pawnikar fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n", 4818889facbeSLen Brown cpu, label, 4819889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4820889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4821889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4822889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4823889facbeSLen Brown 4824889facbeSLen Brown return; 4825889facbeSLen Brown } 4826889facbeSLen Brown 4827889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4828889facbeSLen Brown { 4829889facbeSLen Brown unsigned long long msr; 48309392bd98SCalvin Walton const char *msr_name; 4831889facbeSLen Brown int cpu; 4832889facbeSLen Brown 48339878bf7aSLen Brown UNUSED(c); 48349878bf7aSLen Brown UNUSED(p); 48359878bf7aSLen Brown 4836889facbeSLen Brown if (!do_rapl) 4837889facbeSLen Brown return 0; 4838889facbeSLen Brown 4839889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4840889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4841889facbeSLen Brown return 0; 4842889facbeSLen Brown 4843889facbeSLen Brown cpu = t->cpu_id; 4844889facbeSLen Brown if (cpu_migrate(cpu)) { 48453d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4846889facbeSLen Brown return -1; 4847889facbeSLen Brown } 4848889facbeSLen Brown 48499392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 48509392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 48519392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 48529392bd98SCalvin Walton return -1; 48539392bd98SCalvin Walton } else { 48549392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4855889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4856889facbeSLen Brown return -1; 48579392bd98SCalvin Walton } 4858889facbeSLen Brown 48599392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4860144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 486196e47158SLen Brown 4862144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4863144b44b1SLen Brown 4864889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4865889facbeSLen Brown return -5; 4866889facbeSLen Brown 4867b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4868889facbeSLen Brown cpu, msr, 4869889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4870889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4871889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4872889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4873889facbeSLen Brown 4874144b44b1SLen Brown } 4875144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4876144b44b1SLen Brown 4877889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4878889facbeSLen Brown return -9; 4879889facbeSLen Brown 4880b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4881889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4882889facbeSLen Brown 4883889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 48846b398625SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n", 4885889facbeSLen Brown cpu, 4886889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4887889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4888889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4889889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4890f52ba931SSumeet Pawnikar 4891f52ba931SSumeet Pawnikar if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr)) 4892f52ba931SSumeet Pawnikar return -9; 4893f52ba931SSumeet Pawnikar 4894f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr); 4895f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n", 4896164d7a96SLen Brown cpu, ((msr >> 0) & 0x1FFF) * rapl_power_units, (msr >> 31) & 1 ? "" : "UN"); 4897889facbeSLen Brown } 4898889facbeSLen Brown 48990b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4900889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4901889facbeSLen Brown return -6; 4902889facbeSLen Brown 4903b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4904889facbeSLen Brown cpu, msr, 4905889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4906889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4907889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4908889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 49090b2bb692SLen Brown } 49100b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4911889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4912889facbeSLen Brown return -9; 4913b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4914889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4915889facbeSLen Brown 4916889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4917889facbeSLen Brown } 4918144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4919889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4920889facbeSLen Brown return -7; 4921889facbeSLen Brown 4922b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4923144b44b1SLen Brown } 49249148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4925889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4926889facbeSLen Brown return -9; 4927b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4928889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4929889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4930889facbeSLen Brown } 4931889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4932889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4933889facbeSLen Brown return -8; 4934889facbeSLen Brown 4935b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4936889facbeSLen Brown 4937889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4938889facbeSLen Brown return -9; 4939b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4940889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4941889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4942889facbeSLen Brown } 4943889facbeSLen Brown return 0; 4944889facbeSLen Brown } 4945889facbeSLen Brown 4946d7899447SLen Brown /* 4947d7899447SLen Brown * SNB adds support for additional MSRs: 4948d7899447SLen Brown * 4949d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4950d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4951d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4952d7899447SLen Brown */ 4953103a8feaSLen Brown 4954d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4955103a8feaSLen Brown { 4956103a8feaSLen Brown if (!genuine_intel) 4957103a8feaSLen Brown return 0; 4958103a8feaSLen Brown 49599878bf7aSLen Brown if (family != 6) 49609878bf7aSLen Brown return 0; 49619878bf7aSLen Brown 4962103a8feaSLen Brown switch (model) { 4963869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4964869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4965869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4966869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4967c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4968869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 496977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 49705e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4971c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 49725e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4973869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4974af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4975af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4976869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 497725368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4978869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4979f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 49805ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4981f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 498220de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4983103a8feaSLen Brown return 1; 4984103a8feaSLen Brown } 4985103a8feaSLen Brown return 0; 4986103a8feaSLen Brown } 4987103a8feaSLen Brown 4988d7899447SLen Brown /* 4989570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4990d7899447SLen Brown * 4991d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4992d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4993d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 49945a63426eSLen Brown * 49955a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 49965a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 49975a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 49985a63426eSLen Brown * 4999d7899447SLen Brown */ 5000570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 5001ca58710fSKristen Carlson Accardi { 5002ca58710fSKristen Carlson Accardi if (!genuine_intel) 5003ca58710fSKristen Carlson Accardi return 0; 5004ca58710fSKristen Carlson Accardi 50059878bf7aSLen Brown if (family != 6) 50069878bf7aSLen Brown return 0; 50079878bf7aSLen Brown 5008ca58710fSKristen Carlson Accardi switch (model) { 500977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 5010c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 5011af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5012af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 5013869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5014f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5015f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 5016ca58710fSKristen Carlson Accardi return 1; 5017ca58710fSKristen Carlson Accardi } 5018ca58710fSKristen Carlson Accardi return 0; 5019ca58710fSKristen Carlson Accardi } 5020ca58710fSKristen Carlson Accardi 50210b2bb692SLen Brown /* 50220b2bb692SLen Brown * SKL adds support for additional MSRS: 50230b2bb692SLen Brown * 50240b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 50250b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 50260b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 50270b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 50280b2bb692SLen Brown */ 50290b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 50300b2bb692SLen Brown { 50310b2bb692SLen Brown if (!genuine_intel) 50320b2bb692SLen Brown return 0; 50330b2bb692SLen Brown 50349878bf7aSLen Brown if (family != 6) 50359878bf7aSLen Brown return 0; 50369878bf7aSLen Brown 50370b2bb692SLen Brown switch (model) { 5038af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5039af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 50400b2bb692SLen Brown return 1; 50410b2bb692SLen Brown } 50420b2bb692SLen Brown return 0; 50430b2bb692SLen Brown } 50440b2bb692SLen Brown 5045144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 5046144b44b1SLen Brown { 5047144b44b1SLen Brown if (!genuine_intel) 5048144b44b1SLen Brown return 0; 50499878bf7aSLen Brown 50509878bf7aSLen Brown if (family != 6) 50519878bf7aSLen Brown return 0; 50529878bf7aSLen Brown 5053144b44b1SLen Brown switch (model) { 5054f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 50555ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 5056144b44b1SLen Brown return 1; 5057144b44b1SLen Brown } 5058144b44b1SLen Brown return 0; 5059144b44b1SLen Brown } 5060144b44b1SLen Brown 5061fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 5062fb5d4327SDasaratharaman Chandramouli { 5063fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 5064fb5d4327SDasaratharaman Chandramouli return 0; 50659878bf7aSLen Brown 50669878bf7aSLen Brown if (family != 6) 50679878bf7aSLen Brown return 0; 50689878bf7aSLen Brown 5069fb5d4327SDasaratharaman Chandramouli switch (model) { 5070869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 5071fb5d4327SDasaratharaman Chandramouli return 1; 5072fb5d4327SDasaratharaman Chandramouli } 5073fb5d4327SDasaratharaman Chandramouli return 0; 5074fb5d4327SDasaratharaman Chandramouli } 5075fb5d4327SDasaratharaman Chandramouli 5076997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 5077997e5395SSrinivas Pandruvada { 5078997e5395SSrinivas Pandruvada if (!genuine_intel) 5079997e5395SSrinivas Pandruvada return 0; 5080997e5395SSrinivas Pandruvada 50819878bf7aSLen Brown if (family != 6) 50829878bf7aSLen Brown return 0; 50839878bf7aSLen Brown 5084997e5395SSrinivas Pandruvada switch (model) { 5085af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 5086997e5395SSrinivas Pandruvada return 1; 5087997e5395SSrinivas Pandruvada } 5088997e5395SSrinivas Pandruvada 5089997e5395SSrinivas Pandruvada return 0; 5090997e5395SSrinivas Pandruvada } 5091997e5395SSrinivas Pandruvada 5092b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 5093b2b34dfeSHubert Chrzaniuk { 5094b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 5095b2b34dfeSHubert Chrzaniuk return 1024; 5096b2b34dfeSHubert Chrzaniuk return 1; 5097b2b34dfeSHubert Chrzaniuk } 5098b2b34dfeSHubert Chrzaniuk 5099144b44b1SLen Brown #define SLM_BCLK_FREQS 5 5100144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 }; 5101144b44b1SLen Brown 5102144b44b1SLen Brown double slm_bclk(void) 5103144b44b1SLen Brown { 5104144b44b1SLen Brown unsigned long long msr = 3; 5105144b44b1SLen Brown unsigned int i; 5106144b44b1SLen Brown double freq; 5107144b44b1SLen Brown 51087ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 5109b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 5110144b44b1SLen Brown 5111144b44b1SLen Brown i = msr & 0xf; 5112144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 5113b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 51140a91e551SColin Ian King i = 3; 5115144b44b1SLen Brown } 5116144b44b1SLen Brown freq = slm_freq_table[i]; 5117144b44b1SLen Brown 511896e47158SLen Brown if (!quiet) 5119b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 5120144b44b1SLen Brown 5121144b44b1SLen Brown return freq; 5122144b44b1SLen Brown } 5123144b44b1SLen Brown 5124103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 5125103a8feaSLen Brown { 5126121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 5127103a8feaSLen Brown return 100.00; 5128144b44b1SLen Brown else if (is_slm(family, model)) 5129144b44b1SLen Brown return slm_bclk(); 5130103a8feaSLen Brown else 5131103a8feaSLen Brown return 133.33; 5132103a8feaSLen Brown } 5133103a8feaSLen Brown 51347ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p) 51357ab5ff49SZhang Rui { 51367ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx; 51377ab5ff49SZhang Rui 51389878bf7aSLen Brown UNUSED(c); 51399878bf7aSLen Brown UNUSED(p); 51409878bf7aSLen Brown 51417ab5ff49SZhang Rui if (!genuine_intel) 51427ab5ff49SZhang Rui return 0; 51437ab5ff49SZhang Rui 51447ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) { 51457ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 51467ab5ff49SZhang Rui return -1; 51477ab5ff49SZhang Rui } 51487ab5ff49SZhang Rui 51497ab5ff49SZhang Rui if (max_level < 0x1a) 51507ab5ff49SZhang Rui return 0; 51517ab5ff49SZhang Rui 51527ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx); 51537ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF; 51547ab5ff49SZhang Rui if (eax == 0x20) 51557ab5ff49SZhang Rui t->is_atom = true; 51567ab5ff49SZhang Rui return 0; 51577ab5ff49SZhang Rui } 51587ab5ff49SZhang Rui 5159889facbeSLen Brown /* 5160889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 5161889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 5162889facbeSLen Brown * This is usually equal to tjMax. 5163889facbeSLen Brown * 5164889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 5165889facbeSLen Brown * but also allow cmdline over-ride with -T. 5166889facbeSLen Brown * 5167889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 5168889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 5169889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 5170889facbeSLen Brown */ 51716ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 51726ff7cb37SLen Brown { 5173b2b94be7SLen Brown unsigned long long msr; 517455279aefSZhang Rui unsigned int tcc_default, tcc_offset; 5175b2b94be7SLen Brown int cpu; 5176b2b94be7SLen Brown 51779878bf7aSLen Brown UNUSED(c); 51789878bf7aSLen Brown UNUSED(p); 51799878bf7aSLen Brown 518055279aefSZhang Rui /* tj_max is used only for dts or ptm */ 5181889facbeSLen Brown if (!(do_dts || do_ptm)) 5182889facbeSLen Brown return 0; 5183889facbeSLen Brown 5184889facbeSLen Brown /* this is a per-package concept */ 5185889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 5186889facbeSLen Brown return 0; 5187889facbeSLen Brown 5188b2b94be7SLen Brown cpu = t->cpu_id; 5189b2b94be7SLen Brown if (cpu_migrate(cpu)) { 5190b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 5191b2b94be7SLen Brown return -1; 5192b2b94be7SLen Brown } 5193b2b94be7SLen Brown 519455279aefSZhang Rui if (tj_max_override != 0) { 519555279aefSZhang Rui tj_max = tj_max_override; 51961b439f01SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max); 5197889facbeSLen Brown return 0; 5198889facbeSLen Brown } 5199889facbeSLen Brown 5200b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 5201b2b94be7SLen Brown if (!do_nhm_platform_info) 5202b2b94be7SLen Brown goto guess; 5203b2b94be7SLen Brown 5204b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 5205b2b94be7SLen Brown goto guess; 5206b2b94be7SLen Brown 520755279aefSZhang Rui tcc_default = (msr >> 16) & 0xFF; 5208b2b94be7SLen Brown 52090b9a0b9bSZhang Rui if (!quiet) { 52100b9a0b9bSZhang Rui switch (tcc_offset_bits) { 52110b9a0b9bSZhang Rui case 4: 52120b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0xF; 52130b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 521455279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 52150b9a0b9bSZhang Rui break; 52160b9a0b9bSZhang Rui case 6: 52170b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0x3F; 52180b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 521955279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 52200b9a0b9bSZhang Rui break; 52210b9a0b9bSZhang Rui default: 52221b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default); 52230b9a0b9bSZhang Rui break; 52240b9a0b9bSZhang Rui } 52250b9a0b9bSZhang Rui } 5226b2b94be7SLen Brown 522755279aefSZhang Rui if (!tcc_default) 5228b2b94be7SLen Brown goto guess; 5229b2b94be7SLen Brown 523055279aefSZhang Rui tj_max = tcc_default; 5231b2b94be7SLen Brown 5232889facbeSLen Brown return 0; 5233889facbeSLen Brown 5234b2b94be7SLen Brown guess: 523555279aefSZhang Rui tj_max = TJMAX_DEFAULT; 52361b439f01SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max); 5237889facbeSLen Brown 5238889facbeSLen Brown return 0; 5239889facbeSLen Brown } 524069807a63SLen Brown 5241aa8d8cc7SLen Brown void decode_feature_control_msr(void) 5242aa8d8cc7SLen Brown { 5243aa8d8cc7SLen Brown unsigned long long msr; 5244aa8d8cc7SLen Brown 5245f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 5246aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 52471b439f01SLen Brown base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : ""); 5248aa8d8cc7SLen Brown } 5249aa8d8cc7SLen Brown 525069807a63SLen Brown void decode_misc_enable_msr(void) 525169807a63SLen Brown { 525269807a63SLen Brown unsigned long long msr; 525369807a63SLen Brown 5254f26b1519SLen Brown if (!genuine_intel) 5255f26b1519SLen Brown return; 5256f26b1519SLen Brown 525769807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 5258e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 525969807a63SLen Brown base_cpu, msr, 5260e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 5261e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 5262fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 5263e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 5264e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 526569807a63SLen Brown } 526669807a63SLen Brown 526733148d67SLen Brown void decode_misc_feature_control(void) 526833148d67SLen Brown { 526933148d67SLen Brown unsigned long long msr; 527033148d67SLen Brown 527133148d67SLen Brown if (!has_misc_feature_control) 527233148d67SLen Brown return; 527333148d67SLen Brown 527433148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 52751b439f01SLen Brown fprintf(outf, 52761b439f01SLen Brown "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 52771b439f01SLen Brown base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "", 52781b439f01SLen Brown msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : ""); 527933148d67SLen Brown } 52801b439f01SLen Brown 5281f0057310SLen Brown /* 5282f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 5283f0057310SLen Brown * 5284f0057310SLen Brown * Decode the bits according to the Nehalem documentation 5285f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 5286f0057310SLen Brown * bit[1] less so... 5287f0057310SLen Brown */ 5288f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 5289f0057310SLen Brown { 5290f0057310SLen Brown unsigned long long msr; 5291f0057310SLen Brown 5292f0057310SLen Brown if (!do_nhm_platform_info) 5293f0057310SLen Brown return; 5294f0057310SLen Brown 5295cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 5296cf4cbe53SLen Brown return; 5297cf4cbe53SLen Brown 5298f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 5299ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 5300f0057310SLen Brown base_cpu, msr, 53011b439f01SLen Brown msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS"); 5302f0057310SLen Brown } 53031b439f01SLen Brown 530471616c8eSLen Brown /* 530571616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 530671616c8eSLen Brown * 530771616c8eSLen Brown * This MSRs are present on Silvermont processors, 530871616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 530971616c8eSLen Brown */ 531071616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 531171616c8eSLen Brown { 531271616c8eSLen Brown unsigned long long msr; 531371616c8eSLen Brown 531471616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 531571616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 531671616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 531771616c8eSLen Brown 531871616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 531971616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 532071616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 532171616c8eSLen Brown } 53227f5c258eSLen Brown 5323f5a4c76aSLen Brown /* 5324f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 5325f5a4c76aSLen Brown */ 5326f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 5327f5a4c76aSLen Brown { 5328f5a4c76aSLen Brown 5329f5a4c76aSLen Brown switch (model) { 5330f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 5331f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 5332f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 5333f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 5334f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 5335f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 5336f5a4c76aSLen Brown 5337f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 5338f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 5339f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 5340f5a4c76aSLen Brown 5341f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 5342f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 5343f5a4c76aSLen Brown 5344f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 53455ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 5346f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 5347f5a4c76aSLen Brown 5348af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 5349c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 5350af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 5351c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 5352081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 5353081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 5354af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 5355937807d3SLen Brown 5356af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 5357d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 53584bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 53594bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 5360e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 5361e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 5362e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 53635683460bSChen Yu case INTEL_FAM6_ALDERLAKE_L: 5364fb5e29dfSZhang Rui case INTEL_FAM6_RAPTORLAKE: 5365af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 5366b62b3184SZhang Rui 5367d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 5368d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 5369d7814c30SChen Yu 53706c5c6560SChen Yu case INTEL_FAM6_ICELAKE_D: 5371e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 537225368d7cSChen Yu return INTEL_FAM6_ICELAKE_X; 5373f5a4c76aSLen Brown } 5374f5a4c76aSLen Brown return model; 5375f5a4c76aSLen Brown } 5376d76bb7a0SLen Brown 5377d76bb7a0SLen Brown void print_dev_latency(void) 5378d76bb7a0SLen Brown { 5379d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5380d76bb7a0SLen Brown int fd; 5381d76bb7a0SLen Brown int value; 5382d76bb7a0SLen Brown int retval; 5383d76bb7a0SLen Brown 5384d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5385d76bb7a0SLen Brown if (fd < 0) { 5386d76bb7a0SLen Brown warn("fopen %s\n", path); 5387d76bb7a0SLen Brown return; 5388d76bb7a0SLen Brown } 5389d76bb7a0SLen Brown 5390d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5391d76bb7a0SLen Brown if (retval != sizeof(int)) { 5392d76bb7a0SLen Brown warn("read %s\n", path); 5393d76bb7a0SLen Brown close(fd); 5394d76bb7a0SLen Brown return; 5395d76bb7a0SLen Brown } 53961b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 5397d76bb7a0SLen Brown 5398d76bb7a0SLen Brown close(fd); 5399d76bb7a0SLen Brown } 5400d76bb7a0SLen Brown 54012af4f9b8SLen Brown /* 54022af4f9b8SLen Brown * Linux-perf manages the the HW instructions-retired counter 54032af4f9b8SLen Brown * by enabling when requested, and hiding rollover 54042af4f9b8SLen Brown */ 54052af4f9b8SLen Brown void linux_perf_init(void) 54062af4f9b8SLen Brown { 54072af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 54082af4f9b8SLen Brown return; 54092af4f9b8SLen Brown 54102af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 54112af4f9b8SLen Brown return; 54122af4f9b8SLen Brown 54132af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 54142af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 54152af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 54162af4f9b8SLen Brown 54172af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 54182af4f9b8SLen Brown } 54192af4f9b8SLen Brown 5420fcd17211SLen Brown void process_cpuid() 5421103a8feaSLen Brown { 542234041551SLen Brown unsigned int eax, ebx, ecx, edx; 542334041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5424b3a34e93SLen Brown unsigned int has_turbo; 5425ed0757b8SLen Brown unsigned long long ucode_patch = 0; 5426103a8feaSLen Brown 5427103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5428103a8feaSLen Brown 54295aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5430103a8feaSLen Brown 543134041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5432103a8feaSLen Brown genuine_intel = 1; 543334041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 543434041551SLen Brown authentic_amd = 1; 5435c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5436c1c10cc7SPu Wen hygon_genuine = 1; 5437103a8feaSLen Brown 543896e47158SLen Brown if (!quiet) 5439ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5440ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5441103a8feaSLen Brown 54425aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5443103a8feaSLen Brown family = (fms >> 8) & 0xf; 5444103a8feaSLen Brown model = (fms >> 4) & 0xf; 5445103a8feaSLen Brown stepping = fms & 0xf; 54465aa3d1a2SCalvin Walton if (family == 0xf) 54475aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 54485aa3d1a2SCalvin Walton if (family >= 6) 5449103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 545034041551SLen Brown ecx_flags = ecx; 545134041551SLen Brown edx_flags = edx; 5452103a8feaSLen Brown 5453ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 5454ed0757b8SLen Brown warnx("get_msr(UCODE)\n"); 5455ed0757b8SLen Brown 5456103a8feaSLen Brown /* 5457103a8feaSLen Brown * check max extended function levels of CPUID. 5458103a8feaSLen Brown * This is needed to check for invariant TSC. 5459103a8feaSLen Brown * This check is valid for both Intel and AMD. 5460103a8feaSLen Brown */ 5461103a8feaSLen Brown ebx = ecx = edx = 0; 54625aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5463103a8feaSLen Brown 546434041551SLen Brown if (!quiet) { 5465ed0757b8SLen Brown fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n", 54661b439f01SLen Brown family, model, stepping, family, model, stepping, 54671b439f01SLen Brown (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 5468ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 546934041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 547034041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 547134041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 547234041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 547334041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 547434041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 547534041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 547634041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 547734041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 54781b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-"); 547934041551SLen Brown } 548025368d7cSChen Yu if (genuine_intel) { 5481e9d3092fSZhang Rui model_orig = model; 5482f5a4c76aSLen Brown model = intel_model_duplicates(model); 548325368d7cSChen Yu } 548434041551SLen Brown 548534041551SLen Brown if (!(edx_flags & (1 << 5))) 548634041551SLen Brown errx(1, "CPUID: no MSR"); 548734041551SLen Brown 548861a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5489103a8feaSLen Brown 5490103a8feaSLen Brown /* 5491103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5492103a8feaSLen Brown * this check is valid for both Intel and AMD 5493103a8feaSLen Brown */ 54945aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 54958209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5496d7899447SLen Brown } 5497103a8feaSLen Brown 5498103a8feaSLen Brown /* 5499103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5500103a8feaSLen Brown * this check is valid for both Intel and AMD 5501103a8feaSLen Brown */ 5502103a8feaSLen Brown 55035aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 55048209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5505812db3f7SLen Brown if (has_aperf) { 5506812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5507812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5508812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5509812db3f7SLen Brown } 5510889facbeSLen Brown do_dts = eax & (1 << 0); 5511812db3f7SLen Brown if (do_dts) 5512812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5513b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5514889facbeSLen Brown do_ptm = eax & (1 << 6); 5515812db3f7SLen Brown if (do_ptm) 5516812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 55177f5c258eSLen Brown has_hwp = eax & (1 << 7); 55187f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 55197f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 55207f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 55217f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5522889facbeSLen Brown has_epb = ecx & (1 << 3); 5523889facbeSLen Brown 552496e47158SLen Brown if (!quiet) 5525b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 55267f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 55277f5c258eSLen Brown has_aperf ? "" : "No-", 5528b3a34e93SLen Brown has_turbo ? "" : "No-", 55297f5c258eSLen Brown do_dts ? "" : "No-", 55307f5c258eSLen Brown do_ptm ? "" : "No-", 55317f5c258eSLen Brown has_hwp ? "" : "No-", 55327f5c258eSLen Brown has_hwp_notify ? "" : "No-", 55337f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 55341b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 5535103a8feaSLen Brown 553696e47158SLen Brown if (!quiet) 553769807a63SLen Brown decode_misc_enable_msr(); 553869807a63SLen Brown 553996e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5540aa8d8cc7SLen Brown int has_sgx; 5541aa8d8cc7SLen Brown 5542aa8d8cc7SLen Brown ecx = 0; 5543aa8d8cc7SLen Brown 5544aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5545aa8d8cc7SLen Brown 5546aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5547aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5548aa8d8cc7SLen Brown 5549aa8d8cc7SLen Brown if (has_sgx) 5550aa8d8cc7SLen Brown decode_feature_control_msr(); 5551aa8d8cc7SLen Brown } 5552aa8d8cc7SLen Brown 555361a87ba7SLen Brown if (max_level >= 0x15) { 55548a5bdf41SLen Brown unsigned int eax_crystal; 55558a5bdf41SLen Brown unsigned int ebx_tsc; 55568a5bdf41SLen Brown 55578a5bdf41SLen Brown /* 55588a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 55598a5bdf41SLen Brown */ 55608a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 55615aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 55628a5bdf41SLen Brown 55638a5bdf41SLen Brown if (ebx_tsc != 0) { 55648a5bdf41SLen Brown 556596e47158SLen Brown if (!quiet && (ebx != 0)) 5566b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 55678a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 55688a5bdf41SLen Brown 55698a5bdf41SLen Brown if (crystal_hz == 0) 55708a5bdf41SLen Brown switch (model) { 5571af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5572e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5573e8efbc80SLen Brown break; 55745ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5575ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5576ec53e594SLen Brown break; 5577869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5578f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5579e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 55808a5bdf41SLen Brown break; 55818a5bdf41SLen Brown default: 55828a5bdf41SLen Brown crystal_hz = 0; 55838a5bdf41SLen Brown } 55848a5bdf41SLen Brown 55858a5bdf41SLen Brown if (crystal_hz) { 55868a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 558796e47158SLen Brown if (!quiet) 5588b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 55898a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 55908a5bdf41SLen Brown } 55918a5bdf41SLen Brown } 55928a5bdf41SLen Brown } 559361a87ba7SLen Brown if (max_level >= 0x16) { 559461a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 559561a87ba7SLen Brown 559661a87ba7SLen Brown /* 559761a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 559861a87ba7SLen Brown */ 559961a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 560061a87ba7SLen Brown 56015aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 560296e47158SLen Brown if (!quiet) 5603b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 560461a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 560561a87ba7SLen Brown } 56068a5bdf41SLen Brown 5607b2b34dfeSHubert Chrzaniuk if (has_aperf) 5608b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5609b2b34dfeSHubert Chrzaniuk 5610812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5611812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5612812db3f7SLen Brown 5613812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5614812db3f7SLen Brown do_nhm_platform_info = 1; 5615812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5616812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5617812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5618812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5619812db3f7SLen Brown } 5620d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5621812db3f7SLen Brown 5622812db3f7SLen Brown if (do_snb_cstates) 5623812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5624812db3f7SLen Brown 56255a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 56260f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 56270f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 56280f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 56290f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 56300f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 56310f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 56320f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 56330f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 56340539ba11SLen Brown if (has_slv_msrs(family, model)) { 56350f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 56360f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 56370f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 56380f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 56390539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 56400539ba11SLen Brown use_c1_residency_msr = 1; 56410539ba11SLen Brown } 564220de0dabSAntti Laakso if (is_jvl(family, model)) { 564320de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 564420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 564520de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 564620de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 564720de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 564820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 564920de0dabSAntti Laakso } 56507170a374SLen Brown if (is_dnv(family, model)) { 56517170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 56527170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 56537170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 56547170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 56557170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 56567170a374SLen Brown use_c1_residency_msr = 1; 56577170a374SLen Brown } 565825368d7cSChen Yu if (is_skx(family, model) || is_icx(family, model)) { 565934c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 566034c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 566134c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 566234c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 566334c76197SLen Brown } 5664ade0ebacSLen Brown if (is_bdx(family, model)) { 5665ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5666ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5667ade0ebacSLen Brown } 5668570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5669c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 56700f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5671c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 56720f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5673c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 56740f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 56750f47c08dSLen Brown } 5676570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5677a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5678a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5679a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5680a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5681a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5682a99d8730SLen Brown } 5683144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5684fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5685103a8feaSLen Brown 56861b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model)) 5687562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5688103a8feaSLen Brown 568996e47158SLen Brown if (!quiet) 5690f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5691f0057310SLen Brown 569296e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 569371616c8eSLen Brown decode_c6_demotion_policy_msr(); 569471616c8eSLen Brown 5695889facbeSLen Brown rapl_probe(family, model); 56963a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5697ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5698889facbeSLen Brown 56990b9a0b9bSZhang Rui check_tcc_offset(model_orig); 57000b9a0b9bSZhang Rui 570196e47158SLen Brown if (!quiet) 57021b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5703fcd17211SLen Brown 570441618e63SLen Brown if (!quiet) 5705d76bb7a0SLen Brown print_dev_latency(); 5706d76bb7a0SLen Brown if (!quiet) 570741618e63SLen Brown dump_sysfs_cstate_config(); 57087293fccdSLen Brown if (!quiet) 57097293fccdSLen Brown dump_sysfs_pstate_config(); 571041618e63SLen Brown 57118c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5712a2b7b749SLen Brown calculate_tsc_tweak(); 5713a2b7b749SLen Brown 5714812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5715812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5716fdf676e5SLen Brown 5717812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5718812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 571927d47356SLen Brown 5720b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5721b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5722b4b91569SRafael Antognolli 5723be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5724be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5725be0e54c4SLen Brown else 5726be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5727be0e54c4SLen Brown 5728eae97e05SChen Yu if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK)) 5729eae97e05SChen Yu BIC_PRESENT(BIC_CORE_THROT_CNT); 5730eae97e05SChen Yu else 5731eae97e05SChen Yu BIC_NOT_PRESENT(BIC_CORE_THROT_CNT); 5732eae97e05SChen Yu 57331f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 57341f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5735be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 57361f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 57371f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 57381f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 57391f81c5efSLen Brown } else { 57401f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5741be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 57421f81c5efSLen Brown } 5743be0e54c4SLen Brown 574496e47158SLen Brown if (!quiet) 574533148d67SLen Brown decode_misc_feature_control(); 574633148d67SLen Brown 5747889facbeSLen Brown return; 5748103a8feaSLen Brown } 5749103a8feaSLen Brown 5750103a8feaSLen Brown /* 5751103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5752103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5753103a8feaSLen Brown */ 5754103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5755103a8feaSLen Brown { 5756103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5757103a8feaSLen Brown return 1; 5758103a8feaSLen Brown else 5759103a8feaSLen Brown return 0; 5760103a8feaSLen Brown } 5761103a8feaSLen Brown 5762c98d5d94SLen Brown void topology_probe() 5763c98d5d94SLen Brown { 5764c98d5d94SLen Brown int i; 5765c98d5d94SLen Brown int max_core_id = 0; 5766c98d5d94SLen Brown int max_package_id = 0; 57676de68fe1SLen Brown int max_die_id = 0; 5768c98d5d94SLen Brown int max_siblings = 0; 5769c98d5d94SLen Brown 5770c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5771843c5791SPrarit Bhargava set_max_cpu_num(); 5772c98d5d94SLen Brown topo.num_cpus = 0; 5773c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5774c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5775812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5776c98d5d94SLen Brown 5777d8af6f5fSLen Brown if (debug > 1) 5778b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5779c98d5d94SLen Brown 5780c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5781b2c95d90SJosh Triplett if (cpus == NULL) 5782b2c95d90SJosh Triplett err(1, "calloc cpus"); 5783c98d5d94SLen Brown 5784c98d5d94SLen Brown /* 5785c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5786c98d5d94SLen Brown */ 5787c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5788b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5789b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5790c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5791c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5792c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5793c98d5d94SLen Brown 5794c98d5d94SLen Brown /* 57951ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 57961ef7d21aSLen Brown */ 57971ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 57981ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 57991ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 58001ef7d21aSLen Brown err(1, "cpu%d not present", i); 58011ef7d21aSLen Brown } 58021ef7d21aSLen Brown 58031ef7d21aSLen Brown /* 5804c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5805c98d5d94SLen Brown */ 5806c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5807b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5808b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5809c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5810c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5811c98d5d94SLen Brown 58128cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5813c98d5d94SLen Brown 5814c98d5d94SLen Brown /* 5815c98d5d94SLen Brown * For online cpus 5816c98d5d94SLen Brown * find max_core_id, max_package_id 5817c98d5d94SLen Brown */ 5818c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5819c98d5d94SLen Brown int siblings; 5820c98d5d94SLen Brown 5821c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5822d8af6f5fSLen Brown if (debug > 1) 5823b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5824c98d5d94SLen Brown continue; 5825c98d5d94SLen Brown } 5826c98d5d94SLen Brown 58270e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 58280e2d8f05SLen Brown 58290e2d8f05SLen Brown /* get package information */ 5830c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5831c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5832c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5833c98d5d94SLen Brown 58346de68fe1SLen Brown /* get die information */ 58356de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 58366de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 58376de68fe1SLen Brown max_die_id = cpus[i].die_id; 58386de68fe1SLen Brown 58390e2d8f05SLen Brown /* get numa node information */ 5840ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5841ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5842ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 58430e2d8f05SLen Brown 58440e2d8f05SLen Brown /* get core information */ 58450e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 58460e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 58470e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 58480e2d8f05SLen Brown 58490e2d8f05SLen Brown /* get thread information */ 58500e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5851c98d5d94SLen Brown if (siblings > max_siblings) 5852c98d5d94SLen Brown max_siblings = siblings; 58534f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 58548cb48b32SPrarit Bhargava topo.num_cores++; 5855c98d5d94SLen Brown } 5856ef605741SPrarit Bhargava 585770a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5858d8af6f5fSLen Brown if (debug > 1) 58591b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node); 586070a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5861812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5862c98d5d94SLen Brown 58636de68fe1SLen Brown topo.num_die = max_die_id + 1; 58646de68fe1SLen Brown if (debug > 1) 58651b439f01SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die); 58666de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 58676de68fe1SLen Brown BIC_PRESENT(BIC_Die); 58686de68fe1SLen Brown 5869c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5870d8af6f5fSLen Brown if (debug > 1) 58711b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 58727da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5873812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5874c98d5d94SLen Brown 5875ef605741SPrarit Bhargava set_node_data(); 5876ef605741SPrarit Bhargava if (debug > 1) 587770a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 587801235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 587901235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5880ef605741SPrarit Bhargava 588170a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5882d8af6f5fSLen Brown if (debug > 1) 5883b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 58842ffbb224SPrarit Bhargava 58852ffbb224SPrarit Bhargava if (debug < 1) 58862ffbb224SPrarit Bhargava return; 58872ffbb224SPrarit Bhargava 58882ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 58890ec712e3SLen Brown if (cpu_is_not_present(i)) 58900ec712e3SLen Brown continue; 58912ffbb224SPrarit Bhargava fprintf(outf, 58926de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 58936de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 58941b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id); 58952ffbb224SPrarit Bhargava } 58962ffbb224SPrarit Bhargava 5897c98d5d94SLen Brown } 5898c98d5d94SLen Brown 58991b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 5900c98d5d94SLen Brown { 5901c98d5d94SLen Brown int i; 59021b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; 590340f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5904c98d5d94SLen Brown 590540f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5906c98d5d94SLen Brown if (*t == NULL) 5907c98d5d94SLen Brown goto error; 5908c98d5d94SLen Brown 590940f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5910c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5911c98d5d94SLen Brown 591240f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5913c98d5d94SLen Brown if (*c == NULL) 5914c98d5d94SLen Brown goto error; 5915c98d5d94SLen Brown 591640f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5917c98d5d94SLen Brown (*c)[i].core_id = -1; 5918c98d5d94SLen Brown 5919678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5920c98d5d94SLen Brown if (*p == NULL) 5921c98d5d94SLen Brown goto error; 5922c98d5d94SLen Brown 5923c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5924c98d5d94SLen Brown (*p)[i].package_id = i; 5925c98d5d94SLen Brown 5926c98d5d94SLen Brown return; 5927c98d5d94SLen Brown error: 5928b2c95d90SJosh Triplett err(1, "calloc counters"); 5929c98d5d94SLen Brown } 59301b439f01SLen Brown 5931c98d5d94SLen Brown /* 5932c98d5d94SLen Brown * init_counter() 5933c98d5d94SLen Brown * 5934c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5935c98d5d94SLen Brown */ 59361b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 5937c98d5d94SLen Brown { 59388cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 593940f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 59408cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 59418cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5942c98d5d94SLen Brown struct thread_data *t; 5943c98d5d94SLen Brown struct core_data *c; 5944c98d5d94SLen Brown struct pkg_data *p; 5945c98d5d94SLen Brown 594642dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 594742dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 594842dd4520SNathan Ciobanu */ 594942dd4520SNathan Ciobanu if (node_id < 0) 595042dd4520SNathan Ciobanu node_id = 0; 595142dd4520SNathan Ciobanu 595240f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 595340f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 59548cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5955c98d5d94SLen Brown 5956c98d5d94SLen Brown t->cpu_id = cpu_id; 59578cb48b32SPrarit Bhargava if (thread_id == 0) { 5958c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5959c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5960c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5961c98d5d94SLen Brown } 5962c98d5d94SLen Brown 59638cb48b32SPrarit Bhargava c->core_id = core_id; 59648cb48b32SPrarit Bhargava p->package_id = pkg_id; 5965c98d5d94SLen Brown } 5966c98d5d94SLen Brown 5967c98d5d94SLen Brown int initialize_counters(int cpu_id) 5968c98d5d94SLen Brown { 59698cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 59708cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5971c98d5d94SLen Brown return 0; 5972c98d5d94SLen Brown } 5973c98d5d94SLen Brown 5974c98d5d94SLen Brown void allocate_output_buffer() 5975c98d5d94SLen Brown { 5976eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5977c98d5d94SLen Brown outp = output_buffer; 5978b2c95d90SJosh Triplett if (outp == NULL) 5979b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5980c98d5d94SLen Brown } 59811b439f01SLen Brown 598236229897SLen Brown void allocate_fd_percpu(void) 598336229897SLen Brown { 598401a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 598536229897SLen Brown if (fd_percpu == NULL) 598636229897SLen Brown err(-1, "calloc fd_percpu"); 598736229897SLen Brown } 59881b439f01SLen Brown 5989562a2d37SLen Brown void allocate_irq_buffers(void) 5990562a2d37SLen Brown { 5991562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5992562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5993562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5994c98d5d94SLen Brown 599501a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5996562a2d37SLen Brown if (irqs_per_cpu == NULL) 599701a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5998562a2d37SLen Brown } 59991b439f01SLen Brown 6000c98d5d94SLen Brown void setup_all_buffers(void) 6001c98d5d94SLen Brown { 6002c98d5d94SLen Brown topology_probe(); 6003562a2d37SLen Brown allocate_irq_buffers(); 600436229897SLen Brown allocate_fd_percpu(); 6005c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 6006c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 6007c98d5d94SLen Brown allocate_output_buffer(); 6008c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 6009c98d5d94SLen Brown } 60103b4d5c7fSAndy Shevchenko 60117ce7d5deSPrarit Bhargava void set_base_cpu(void) 60127ce7d5deSPrarit Bhargava { 60137ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 60147ce7d5deSPrarit Bhargava if (base_cpu < 0) 60157ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 60167ce7d5deSPrarit Bhargava 60177ce7d5deSPrarit Bhargava if (debug > 1) 6018b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 60197ce7d5deSPrarit Bhargava } 60207ce7d5deSPrarit Bhargava 6021103a8feaSLen Brown void turbostat_init() 6022103a8feaSLen Brown { 60237ce7d5deSPrarit Bhargava setup_all_buffers(); 60247ce7d5deSPrarit Bhargava set_base_cpu(); 6025103a8feaSLen Brown check_dev_msr(); 602698481e79SLen Brown check_permissions(); 6027fcd17211SLen Brown process_cpuid(); 60282af4f9b8SLen Brown linux_perf_init(); 6029103a8feaSLen Brown 603096e47158SLen Brown if (!quiet) 60317f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 60327f5c258eSLen Brown 603396e47158SLen Brown if (!quiet) 6034889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 6035889facbeSLen Brown 603696e47158SLen Brown if (!quiet) 60373a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 60383a9a941dSLen Brown 603996e47158SLen Brown if (!quiet) 6040889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 6041889facbeSLen Brown 6042889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 6043889facbeSLen Brown 60447ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 60457ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 60467ab5ff49SZhang Rui 604796e47158SLen Brown if (!quiet) 6048889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 60495a63426eSLen Brown 605096e47158SLen Brown if (!quiet && do_irtl_snb) 60515a63426eSLen Brown print_irtl(); 6052164d7a96SLen Brown 6053164d7a96SLen Brown if (DO_BIC(BIC_IPC)) 6054164d7a96SLen Brown (void)get_instr_count_fd(base_cpu); 6055103a8feaSLen Brown } 6056103a8feaSLen Brown 6057103a8feaSLen Brown int fork_it(char **argv) 6058103a8feaSLen Brown { 6059103a8feaSLen Brown pid_t child_pid; 6060d91bb17cSLen Brown int status; 6061d15cf7c1SLen Brown 6062218f0e8dSLen Brown snapshot_proc_sysfs_files(); 6063d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 60644c2122d4SLen Brown first_counter_read = 0; 6065d91bb17cSLen Brown if (status) 6066d91bb17cSLen Brown exit(status); 6067d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 6068d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 6069103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 6070103a8feaSLen Brown 6071103a8feaSLen Brown child_pid = fork(); 6072103a8feaSLen Brown if (!child_pid) { 6073103a8feaSLen Brown /* child */ 6074103a8feaSLen Brown execvp(argv[0], argv); 60750815a3d0SLen Brown err(errno, "exec %s", argv[0]); 6076103a8feaSLen Brown } else { 6077103a8feaSLen Brown 6078103a8feaSLen Brown /* parent */ 6079b2c95d90SJosh Triplett if (child_pid == -1) 6080b2c95d90SJosh Triplett err(1, "fork"); 6081103a8feaSLen Brown 6082103a8feaSLen Brown signal(SIGINT, SIG_IGN); 6083103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 6084b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 6085b2c95d90SJosh Triplett err(status, "waitpid"); 60862a954966SDavid Arcari 60872a954966SDavid Arcari if (WIFEXITED(status)) 60882a954966SDavid Arcari status = WEXITSTATUS(status); 6089103a8feaSLen Brown } 6090c98d5d94SLen Brown /* 6091c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 6092c98d5d94SLen Brown * because re-starting is problematic when forking 6093c98d5d94SLen Brown */ 6094218f0e8dSLen Brown snapshot_proc_sysfs_files(); 6095c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 6096103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 6097103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 6098ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 6099ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 6100ba3dec99SLen Brown else { 6101c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 6102c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 6103ba3dec99SLen Brown } 6104103a8feaSLen Brown 6105b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 6106b7d8c148SLen Brown 6107b7d8c148SLen Brown flush_output_stderr(); 6108103a8feaSLen Brown 6109d91bb17cSLen Brown return status; 6110103a8feaSLen Brown } 6111103a8feaSLen Brown 61123b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 61133b4d5c7fSAndy Shevchenko { 61143b4d5c7fSAndy Shevchenko int status; 61153b4d5c7fSAndy Shevchenko 6116218f0e8dSLen Brown snapshot_proc_sysfs_files(); 61173b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 61183b4d5c7fSAndy Shevchenko if (status) 61193b4d5c7fSAndy Shevchenko return status; 61203b4d5c7fSAndy Shevchenko 61213b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 61223b4d5c7fSAndy Shevchenko if (status) 61233b4d5c7fSAndy Shevchenko return status; 61243b4d5c7fSAndy Shevchenko 6125b7d8c148SLen Brown flush_output_stdout(); 61263b4d5c7fSAndy Shevchenko 61273b4d5c7fSAndy Shevchenko return status; 61283b4d5c7fSAndy Shevchenko } 61293b4d5c7fSAndy Shevchenko 61301b439f01SLen Brown void print_version() 61311b439f01SLen Brown { 613258990892SLen Brown fprintf(outf, "turbostat version 2022.04.16 - Len Brown <lenb@kernel.org>\n"); 6133d8af6f5fSLen Brown } 6134d8af6f5fSLen Brown 613503331233SChen Yu #define COMMAND_LINE_SIZE 2048 613603331233SChen Yu 613703331233SChen Yu void print_bootcmd(void) 613803331233SChen Yu { 613903331233SChen Yu char bootcmd[COMMAND_LINE_SIZE]; 614003331233SChen Yu FILE *fp; 614103331233SChen Yu int ret; 614203331233SChen Yu 614303331233SChen Yu memset(bootcmd, 0, COMMAND_LINE_SIZE); 614403331233SChen Yu fp = fopen("/proc/cmdline", "r"); 614503331233SChen Yu if (!fp) 614603331233SChen Yu return; 614703331233SChen Yu 614803331233SChen Yu ret = fread(bootcmd, sizeof(char), COMMAND_LINE_SIZE - 1, fp); 614903331233SChen Yu if (ret) { 615003331233SChen Yu bootcmd[ret] = '\0'; 615103331233SChen Yu /* the last character is already '\n' */ 615203331233SChen Yu fprintf(outf, "Kernel command line: %s", bootcmd); 615303331233SChen Yu } 615403331233SChen Yu 615503331233SChen Yu fclose(fp); 615603331233SChen Yu } 615703331233SChen Yu 6158495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 6159495c7654SLen Brown unsigned int width, enum counter_scope scope, 616041618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 6161388e9c81SLen Brown { 6162388e9c81SLen Brown struct msr_counter *msrp; 6163388e9c81SLen Brown 6164388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 6165388e9c81SLen Brown if (msrp == NULL) { 6166388e9c81SLen Brown perror("calloc"); 6167388e9c81SLen Brown exit(1); 6168388e9c81SLen Brown } 6169388e9c81SLen Brown 6170388e9c81SLen Brown msrp->msr_num = msr_num; 6171d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 6172495c7654SLen Brown if (path) 6173d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 6174388e9c81SLen Brown msrp->width = width; 6175388e9c81SLen Brown msrp->type = type; 6176388e9c81SLen Brown msrp->format = format; 617741618e63SLen Brown msrp->flags = flags; 6178388e9c81SLen Brown 6179388e9c81SLen Brown switch (scope) { 6180388e9c81SLen Brown 6181388e9c81SLen Brown case SCOPE_CPU: 6182388e9c81SLen Brown msrp->next = sys.tp; 6183388e9c81SLen Brown sys.tp = msrp; 6184678a3bd1SLen Brown sys.added_thread_counters++; 61850748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 61861b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS); 6187678a3bd1SLen Brown exit(-1); 6188678a3bd1SLen Brown } 6189388e9c81SLen Brown break; 6190388e9c81SLen Brown 6191388e9c81SLen Brown case SCOPE_CORE: 6192388e9c81SLen Brown msrp->next = sys.cp; 6193388e9c81SLen Brown sys.cp = msrp; 6194678a3bd1SLen Brown sys.added_core_counters++; 6195678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 61961b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS); 6197678a3bd1SLen Brown exit(-1); 6198678a3bd1SLen Brown } 6199388e9c81SLen Brown break; 6200388e9c81SLen Brown 6201388e9c81SLen Brown case SCOPE_PACKAGE: 6202388e9c81SLen Brown msrp->next = sys.pp; 6203388e9c81SLen Brown sys.pp = msrp; 6204678a3bd1SLen Brown sys.added_package_counters++; 6205678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 62061b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS); 6207678a3bd1SLen Brown exit(-1); 6208678a3bd1SLen Brown } 6209388e9c81SLen Brown break; 6210388e9c81SLen Brown } 6211388e9c81SLen Brown 6212388e9c81SLen Brown return 0; 6213388e9c81SLen Brown } 6214388e9c81SLen Brown 6215388e9c81SLen Brown void parse_add_command(char *add_command) 6216388e9c81SLen Brown { 6217388e9c81SLen Brown int msr_num = 0; 6218495c7654SLen Brown char *path = NULL; 62190f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 6220388e9c81SLen Brown int width = 64; 6221388e9c81SLen Brown int fail = 0; 6222388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 6223388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 6224388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 6225388e9c81SLen Brown 6226388e9c81SLen Brown while (add_command) { 6227388e9c81SLen Brown 6228388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 6229388e9c81SLen Brown goto next; 6230388e9c81SLen Brown 6231388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 6232388e9c81SLen Brown goto next; 6233388e9c81SLen Brown 6234495c7654SLen Brown if (*add_command == '/') { 6235495c7654SLen Brown path = add_command; 6236495c7654SLen Brown goto next; 6237495c7654SLen Brown } 6238495c7654SLen Brown 6239388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 6240388e9c81SLen Brown if ((width == 32) || (width == 64)) 6241388e9c81SLen Brown goto next; 6242388e9c81SLen Brown width = 64; 6243388e9c81SLen Brown } 6244388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6245388e9c81SLen Brown scope = SCOPE_CPU; 6246388e9c81SLen Brown goto next; 6247388e9c81SLen Brown } 6248388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6249388e9c81SLen Brown scope = SCOPE_CORE; 6250388e9c81SLen Brown goto next; 6251388e9c81SLen Brown } 6252388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6253388e9c81SLen Brown scope = SCOPE_PACKAGE; 6254388e9c81SLen Brown goto next; 6255388e9c81SLen Brown } 6256388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6257388e9c81SLen Brown type = COUNTER_CYCLES; 6258388e9c81SLen Brown goto next; 6259388e9c81SLen Brown } 6260388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6261388e9c81SLen Brown type = COUNTER_SECONDS; 6262388e9c81SLen Brown goto next; 6263388e9c81SLen Brown } 626441618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 626541618e63SLen Brown type = COUNTER_USEC; 626641618e63SLen Brown goto next; 626741618e63SLen Brown } 6268388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6269388e9c81SLen Brown format = FORMAT_RAW; 6270388e9c81SLen Brown goto next; 6271388e9c81SLen Brown } 6272388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6273388e9c81SLen Brown format = FORMAT_DELTA; 6274388e9c81SLen Brown goto next; 6275388e9c81SLen Brown } 6276388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6277388e9c81SLen Brown format = FORMAT_PERCENT; 6278388e9c81SLen Brown goto next; 6279388e9c81SLen Brown } 6280388e9c81SLen Brown 6281388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6282388e9c81SLen Brown char *eos; 6283388e9c81SLen Brown 6284388e9c81SLen Brown eos = strchr(name_buffer, ','); 6285388e9c81SLen Brown if (eos) 6286388e9c81SLen Brown *eos = '\0'; 6287388e9c81SLen Brown goto next; 6288388e9c81SLen Brown } 6289388e9c81SLen Brown 6290388e9c81SLen Brown next: 6291388e9c81SLen Brown add_command = strchr(add_command, ','); 6292495c7654SLen Brown if (add_command) { 6293495c7654SLen Brown *add_command = '\0'; 6294388e9c81SLen Brown add_command++; 6295495c7654SLen Brown } 6296388e9c81SLen Brown 6297388e9c81SLen Brown } 6298495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6299495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6300388e9c81SLen Brown fail++; 6301388e9c81SLen Brown } 6302388e9c81SLen Brown 6303388e9c81SLen Brown /* generate default column header */ 6304388e9c81SLen Brown if (*name_buffer == '\0') { 6305388e9c81SLen Brown if (width == 32) 63065f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6307388e9c81SLen Brown else 63085f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6309388e9c81SLen Brown } 6310388e9c81SLen Brown 631141618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6312388e9c81SLen Brown fail++; 6313388e9c81SLen Brown 6314388e9c81SLen Brown if (fail) { 6315388e9c81SLen Brown help(); 6316388e9c81SLen Brown exit(1); 6317388e9c81SLen Brown } 6318388e9c81SLen Brown } 631941618e63SLen Brown 63200fc521bcSZephaniah E. Loss-Cutler-Hull int is_deferred_add(char *name) 63210fc521bcSZephaniah E. Loss-Cutler-Hull { 63220fc521bcSZephaniah E. Loss-Cutler-Hull int i; 63230fc521bcSZephaniah E. Loss-Cutler-Hull 63240fc521bcSZephaniah E. Loss-Cutler-Hull for (i = 0; i < deferred_add_index; ++i) 63250fc521bcSZephaniah E. Loss-Cutler-Hull if (!strcmp(name, deferred_add_names[i])) 63260fc521bcSZephaniah E. Loss-Cutler-Hull return 1; 63270fc521bcSZephaniah E. Loss-Cutler-Hull return 0; 63280fc521bcSZephaniah E. Loss-Cutler-Hull } 63290fc521bcSZephaniah E. Loss-Cutler-Hull 6330dd778a5eSLen Brown int is_deferred_skip(char *name) 6331dd778a5eSLen Brown { 6332dd778a5eSLen Brown int i; 6333dd778a5eSLen Brown 6334dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6335dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6336dd778a5eSLen Brown return 1; 6337dd778a5eSLen Brown return 0; 6338dd778a5eSLen Brown } 6339dd778a5eSLen Brown 634041618e63SLen Brown void probe_sysfs(void) 634141618e63SLen Brown { 634241618e63SLen Brown char path[64]; 634341618e63SLen Brown char name_buf[16]; 634441618e63SLen Brown FILE *input; 634541618e63SLen Brown int state; 634641618e63SLen Brown char *sp; 634741618e63SLen Brown 63480748eaf0SLen Brown for (state = 10; state >= 0; --state) { 634941618e63SLen Brown 63501b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 635141618e63SLen Brown input = fopen(path, "r"); 635241618e63SLen Brown if (input == NULL) 635341618e63SLen Brown continue; 63548173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 63558173c336SBen Hutchings err(1, "%s: failed to read file", path); 635641618e63SLen Brown 635741618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 635841618e63SLen Brown sp = strchr(name_buf, '-'); 635941618e63SLen Brown if (!sp) 636041618e63SLen Brown sp = strchrnul(name_buf, '\n'); 636141618e63SLen Brown *sp = '%'; 636241618e63SLen Brown *(sp + 1) = '\0'; 636341618e63SLen Brown 6364fecb3bc8SDavid Arcari remove_underbar(name_buf); 6365fecb3bc8SDavid Arcari 636641618e63SLen Brown fclose(input); 636741618e63SLen Brown 636841618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 636941618e63SLen Brown 63700fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 63710fc521bcSZephaniah E. Loss-Cutler-Hull continue; 63720fc521bcSZephaniah E. Loss-Cutler-Hull 6373dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6374dd778a5eSLen Brown continue; 6375dd778a5eSLen Brown 63761b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU); 637741618e63SLen Brown } 637841618e63SLen Brown 63790748eaf0SLen Brown for (state = 10; state >= 0; --state) { 638041618e63SLen Brown 63811b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 638241618e63SLen Brown input = fopen(path, "r"); 638341618e63SLen Brown if (input == NULL) 638441618e63SLen Brown continue; 63858173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 63868173c336SBen Hutchings err(1, "%s: failed to read file", path); 638741618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 638841618e63SLen Brown sp = strchr(name_buf, '-'); 638941618e63SLen Brown if (!sp) 639041618e63SLen Brown sp = strchrnul(name_buf, '\n'); 639141618e63SLen Brown *sp = '\0'; 639241618e63SLen Brown fclose(input); 639341618e63SLen Brown 6394fecb3bc8SDavid Arcari remove_underbar(name_buf); 6395fecb3bc8SDavid Arcari 639641618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 639741618e63SLen Brown 63980fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 63990fc521bcSZephaniah E. Loss-Cutler-Hull continue; 64000fc521bcSZephaniah E. Loss-Cutler-Hull 6401dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6402dd778a5eSLen Brown continue; 6403dd778a5eSLen Brown 64041b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU); 640541618e63SLen Brown } 640641618e63SLen Brown 640741618e63SLen Brown } 640841618e63SLen Brown 64091ef7d21aSLen Brown /* 64101ef7d21aSLen Brown * parse cpuset with following syntax 64111ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 64121ef7d21aSLen Brown */ 64131ef7d21aSLen Brown void parse_cpu_command(char *optarg) 64141ef7d21aSLen Brown { 64151ef7d21aSLen Brown unsigned int start, end; 64161ef7d21aSLen Brown char *next; 64171ef7d21aSLen Brown 64184e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 64194e4e1e7cSLen Brown if (cpu_subset) 64204e4e1e7cSLen Brown goto error; 64214e4e1e7cSLen Brown show_core_only++; 64224e4e1e7cSLen Brown return; 64234e4e1e7cSLen Brown } 64244e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 64254e4e1e7cSLen Brown if (cpu_subset) 64264e4e1e7cSLen Brown goto error; 64274e4e1e7cSLen Brown show_pkg_only++; 64284e4e1e7cSLen Brown return; 64294e4e1e7cSLen Brown } 64304e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 64314e4e1e7cSLen Brown goto error; 64324e4e1e7cSLen Brown 64331ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 64341ef7d21aSLen Brown if (cpu_subset == NULL) 64351ef7d21aSLen Brown err(3, "CPU_ALLOC"); 64361ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 64371ef7d21aSLen Brown 64381ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 64391ef7d21aSLen Brown 64401ef7d21aSLen Brown next = optarg; 64411ef7d21aSLen Brown 64421ef7d21aSLen Brown while (next && *next) { 64431ef7d21aSLen Brown 64441ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 64451ef7d21aSLen Brown goto error; 64461ef7d21aSLen Brown 64471ef7d21aSLen Brown start = strtoul(next, &next, 10); 64481ef7d21aSLen Brown 64491ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 64501ef7d21aSLen Brown goto error; 64511ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 64521ef7d21aSLen Brown 64531ef7d21aSLen Brown if (*next == '\0') 64541ef7d21aSLen Brown break; 64551ef7d21aSLen Brown 64561ef7d21aSLen Brown if (*next == ',') { 64571ef7d21aSLen Brown next += 1; 64581ef7d21aSLen Brown continue; 64591ef7d21aSLen Brown } 64601ef7d21aSLen Brown 64611ef7d21aSLen Brown if (*next == '-') { 64621ef7d21aSLen Brown next += 1; /* start range */ 64631ef7d21aSLen Brown } else if (*next == '.') { 64641ef7d21aSLen Brown next += 1; 64651ef7d21aSLen Brown if (*next == '.') 64661ef7d21aSLen Brown next += 1; /* start range */ 64671ef7d21aSLen Brown else 64681ef7d21aSLen Brown goto error; 64691ef7d21aSLen Brown } 64701ef7d21aSLen Brown 64711ef7d21aSLen Brown end = strtoul(next, &next, 10); 64721ef7d21aSLen Brown if (end <= start) 64731ef7d21aSLen Brown goto error; 64741ef7d21aSLen Brown 64751ef7d21aSLen Brown while (++start <= end) { 64761ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 64771ef7d21aSLen Brown goto error; 64781ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 64791ef7d21aSLen Brown } 64801ef7d21aSLen Brown 64811ef7d21aSLen Brown if (*next == ',') 64821ef7d21aSLen Brown next += 1; 64831ef7d21aSLen Brown else if (*next != '\0') 64841ef7d21aSLen Brown goto error; 64851ef7d21aSLen Brown } 64861ef7d21aSLen Brown 64871ef7d21aSLen Brown return; 64881ef7d21aSLen Brown 64891ef7d21aSLen Brown error: 64904e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 64914e4e1e7cSLen Brown help(); 64921ef7d21aSLen Brown exit(-1); 64931ef7d21aSLen Brown } 64941ef7d21aSLen Brown 6495103a8feaSLen Brown void cmdline(int argc, char **argv) 6496103a8feaSLen Brown { 6497103a8feaSLen Brown int opt; 6498d8af6f5fSLen Brown int option_index = 0; 6499d8af6f5fSLen Brown static struct option long_options[] = { 6500388e9c81SLen Brown { "add", required_argument, 0, 'a' }, 65011ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' }, 6502d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' }, 650396e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 65043f44a5c6SLen Brown { "enable", required_argument, 0, 'e' }, 6505d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' }, 65062af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' }, 6507023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' }, 6508c7e399f8SZephaniah E. Loss-Cutler-Hull { "header_iterations", required_argument, 0, 'N' }, 6509d8af6f5fSLen Brown { "help", no_argument, 0, 'h' }, 6510812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 6511d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' }, 6512c8ade361SLen Brown { "list", no_argument, 0, 'l' }, 6513b7d8c148SLen Brown { "out", required_argument, 0, 'o' }, 651496e47158SLen Brown { "quiet", no_argument, 0, 'q' }, 6515812db3f7SLen Brown { "show", required_argument, 0, 's' }, 6516d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' }, 6517d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' }, 6518d8af6f5fSLen Brown { "version", no_argument, 0, 'v' }, 6519d8af6f5fSLen Brown { 0, 0, 0, 0 } 6520d8af6f5fSLen Brown }; 6521103a8feaSLen Brown 6522103a8feaSLen Brown progname = argv[0]; 6523103a8feaSLen Brown 65241b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) { 6525103a8feaSLen Brown switch (opt) { 6526388e9c81SLen Brown case 'a': 6527388e9c81SLen Brown parse_add_command(optarg); 6528388e9c81SLen Brown break; 65291ef7d21aSLen Brown case 'c': 65301ef7d21aSLen Brown parse_cpu_command(optarg); 65311ef7d21aSLen Brown break; 6532d8af6f5fSLen Brown case 'D': 6533d8af6f5fSLen Brown dump_only++; 65348e180f3cSLen Brown break; 65353f44a5c6SLen Brown case 'e': 65363f44a5c6SLen Brown /* --enable specified counter */ 65374c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 65383f44a5c6SLen Brown break; 6539d8af6f5fSLen Brown case 'd': 6540d8af6f5fSLen Brown debug++; 65413f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 65422f32edf1SLen Brown break; 6543812db3f7SLen Brown case 'H': 65443f44a5c6SLen Brown /* 65453f44a5c6SLen Brown * --hide: do not show those specified 65463f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 65473f44a5c6SLen Brown */ 65483f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6549812db3f7SLen Brown break; 6550d8af6f5fSLen Brown case 'h': 6551d8af6f5fSLen Brown default: 6552d8af6f5fSLen Brown help(); 6553d8af6f5fSLen Brown exit(1); 6554d8af6f5fSLen Brown case 'i': 65552a0609c0SLen Brown { 65562a0609c0SLen Brown double interval = strtod(optarg, NULL); 65572a0609c0SLen Brown 65582a0609c0SLen Brown if (interval < 0.001) { 65591b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval); 65602a0609c0SLen Brown exit(2); 65612a0609c0SLen Brown } 65622a0609c0SLen Brown 656347936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6564b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 656547936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 65662a0609c0SLen Brown } 6567889facbeSLen Brown break; 65685c56be9aSDirk Brandewie case 'J': 65695c56be9aSDirk Brandewie rapl_joules++; 65705c56be9aSDirk Brandewie break; 6571c8ade361SLen Brown case 'l': 65723f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6573c8ade361SLen Brown list_header_only++; 6574c8ade361SLen Brown quiet++; 6575c8ade361SLen Brown break; 6576b7d8c148SLen Brown case 'o': 6577b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6578b7d8c148SLen Brown break; 657996e47158SLen Brown case 'q': 658096e47158SLen Brown quiet = 1; 658196e47158SLen Brown break; 6582023fe0acSChen Yu case 'n': 6583023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6584023fe0acSChen Yu 6585023fe0acSChen Yu if (num_iterations <= 0) { 65861b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations); 6587023fe0acSChen Yu exit(2); 6588023fe0acSChen Yu } 6589023fe0acSChen Yu break; 6590c7e399f8SZephaniah E. Loss-Cutler-Hull case 'N': 6591c7e399f8SZephaniah E. Loss-Cutler-Hull header_iterations = strtod(optarg, NULL); 6592c7e399f8SZephaniah E. Loss-Cutler-Hull 6593c7e399f8SZephaniah E. Loss-Cutler-Hull if (header_iterations <= 0) { 6594164d7a96SLen Brown fprintf(outf, "iterations %d should be positive number\n", header_iterations); 6595c7e399f8SZephaniah E. Loss-Cutler-Hull exit(2); 6596c7e399f8SZephaniah E. Loss-Cutler-Hull } 6597c7e399f8SZephaniah E. Loss-Cutler-Hull break; 6598812db3f7SLen Brown case 's': 65993f44a5c6SLen Brown /* 66003f44a5c6SLen Brown * --show: show only those specified 66013f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 66023f44a5c6SLen Brown * subsequent invocations can add to it. 66033f44a5c6SLen Brown */ 66043f44a5c6SLen Brown if (shown == 0) 66053f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 66063f44a5c6SLen Brown else 66073f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 66083f44a5c6SLen Brown shown = 1; 6609812db3f7SLen Brown break; 6610d8af6f5fSLen Brown case 'S': 6611d8af6f5fSLen Brown summary_only++; 6612d8af6f5fSLen Brown break; 6613d8af6f5fSLen Brown case 'T': 661455279aefSZhang Rui tj_max_override = atoi(optarg); 6615d8af6f5fSLen Brown break; 6616d8af6f5fSLen Brown case 'v': 6617d8af6f5fSLen Brown print_version(); 6618d8af6f5fSLen Brown exit(0); 6619d8af6f5fSLen Brown break; 6620103a8feaSLen Brown } 6621103a8feaSLen Brown } 6622103a8feaSLen Brown } 6623103a8feaSLen Brown 6624103a8feaSLen Brown int main(int argc, char **argv) 6625103a8feaSLen Brown { 6626b7d8c148SLen Brown outf = stderr; 6627103a8feaSLen Brown cmdline(argc, argv); 6628103a8feaSLen Brown 662903331233SChen Yu if (!quiet) { 6630d8af6f5fSLen Brown print_version(); 663103331233SChen Yu print_bootcmd(); 663203331233SChen Yu } 6633103a8feaSLen Brown 663441618e63SLen Brown probe_sysfs(); 663541618e63SLen Brown 6636103a8feaSLen Brown turbostat_init(); 6637103a8feaSLen Brown 66386799ba84SDan Merillat msr_sum_record(); 66396799ba84SDan Merillat 66403b4d5c7fSAndy Shevchenko /* dump counters and exit */ 66413b4d5c7fSAndy Shevchenko if (dump_only) 66423b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 66433b4d5c7fSAndy Shevchenko 6644c8ade361SLen Brown /* list header and exit */ 6645c8ade361SLen Brown if (list_header_only) { 6646c8ade361SLen Brown print_header(","); 6647c8ade361SLen Brown flush_output_stdout(); 6648c8ade361SLen Brown return 0; 6649c8ade361SLen Brown } 6650c8ade361SLen Brown 6651103a8feaSLen Brown /* 6652103a8feaSLen Brown * if any params left, it must be a command to fork 6653103a8feaSLen Brown */ 6654103a8feaSLen Brown if (argc - optind) 6655103a8feaSLen Brown return fork_it(argv + optind); 6656103a8feaSLen Brown else 6657103a8feaSLen Brown turbostat_loop(); 6658103a8feaSLen Brown 6659103a8feaSLen Brown return 0; 6660103a8feaSLen Brown } 6661