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 * 6144b44b1SLen Brown * Copyright (c) 2013 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 40103a8feaSLen Brown char *proc_stat = "/proc/stat"; 41b7d8c148SLen Brown FILE *outf; 4236229897SLen Brown int *fd_percpu; 432af4f9b8SLen Brown int *fd_instr_count_percpu; 44b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 }; 4547936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 }; 46e9d3092fSZhang Rui 47e9d3092fSZhang Rui /* Save original CPU model */ 48e9d3092fSZhang Rui unsigned int model_orig; 49e9d3092fSZhang Rui 50023fe0acSChen Yu unsigned int num_iterations; 51d8af6f5fSLen Brown unsigned int debug; 5296e47158SLen Brown unsigned int quiet; 533f44a5c6SLen Brown unsigned int shown; 540de6c0dfSLen Brown unsigned int sums_need_wide_columns; 55d8af6f5fSLen Brown unsigned int rapl_joules; 56d8af6f5fSLen Brown unsigned int summary_only; 57c8ade361SLen Brown unsigned int list_header_only; 58d8af6f5fSLen Brown unsigned int dump_only; 59103a8feaSLen Brown unsigned int do_snb_cstates; 60fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 61144b44b1SLen Brown unsigned int do_slm_cstates; 62144b44b1SLen Brown unsigned int use_c1_residency_msr; 63103a8feaSLen Brown unsigned int has_aperf; 64889facbeSLen Brown unsigned int has_epb; 655a63426eSLen Brown unsigned int do_irtl_snb; 665a63426eSLen Brown unsigned int do_irtl_hsw; 67fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 68103a8feaSLen Brown unsigned int genuine_intel; 6934041551SLen Brown unsigned int authentic_amd; 70c1c10cc7SPu Wen unsigned int hygon_genuine; 7134041551SLen Brown unsigned int max_level, max_extended_level; 72103a8feaSLen Brown unsigned int has_invariant_tsc; 73d7899447SLen Brown unsigned int do_nhm_platform_info; 74cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 75b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 76103a8feaSLen Brown double bclk; 77a2b7b749SLen Brown double base_hz; 7821ed5574SLen Brown unsigned int has_base_hz; 79a2b7b749SLen Brown double tsc_tweak = 1.0; 80c98d5d94SLen Brown unsigned int show_pkg_only; 81c98d5d94SLen Brown unsigned int show_core_only; 82c98d5d94SLen Brown char *output_buffer, *outp; 83889facbeSLen Brown unsigned int do_rapl; 84889facbeSLen Brown unsigned int do_dts; 85889facbeSLen Brown unsigned int do_ptm; 862af4f9b8SLen Brown unsigned int do_ipc; 87fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 88be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 89be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 9027d47356SLen Brown unsigned int gfx_cur_mhz; 91b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 9255279aefSZhang Rui unsigned int tj_max; 9355279aefSZhang Rui unsigned int tj_max_override; 940b9a0b9bSZhang Rui int tcc_offset_bits; 9540ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 9640ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 97889facbeSLen Brown double rapl_joule_counter_range; 983a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 99ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 100aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 1013a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1023a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1038a5bdf41SLen Brown unsigned int crystal_hz; 1048a5bdf41SLen Brown unsigned long long tsc_hz; 1057ce7d5deSPrarit Bhargava int base_cpu; 10621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1077f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1087f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1097f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1107f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1117f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1127f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 11333148d67SLen Brown unsigned int has_misc_feature_control; 1144c2122d4SLen Brown unsigned int first_counter_read = 1; 115c026c236SArtem Bityutskiy int ignore_stdin; 116889facbeSLen Brown 117889facbeSLen Brown #define RAPL_PKG (1 << 0) 118e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 119e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 120e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 121e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 122e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 123e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 124e6f9bb3cSLen Brown 125889facbeSLen Brown #define RAPL_DRAM (1 << 3) 126e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 127e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 128e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 129e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1300b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1310b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 132e6f9bb3cSLen Brown 1339148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 134e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1350b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 136e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 137e6f9bb3cSLen Brown 1380b2bb692SLen Brown #define RAPL_GFX (1 << 8) 139e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 140e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 141e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1429148494cSJacob Pan 1439148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1449148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1459392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1469392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1479392bd98SCalvin Walton * not per-package. */ 1489392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1499392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1509392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1519392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1529148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 153889facbeSLen Brown #define TJMAX_DEFAULT 100 154889facbeSLen Brown 1559392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1569392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1579392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1589392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1599392bd98SCalvin Walton 160889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 161103a8feaSLen Brown 162388e9c81SLen Brown /* 163388e9c81SLen Brown * buffer size used by sscanf() for added column names 164388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 165388e9c81SLen Brown */ 166388e9c81SLen Brown #define NAME_BYTES 20 167495c7654SLen Brown #define PATH_BYTES 128 168388e9c81SLen Brown 169103a8feaSLen Brown int backwards_count; 170103a8feaSLen Brown char *progname; 171103a8feaSLen Brown 1721ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1731ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1741ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1750748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1760748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1770e2d8f05SLen Brown #define BITMASK_SIZE 32 178c98d5d94SLen Brown 179c98d5d94SLen Brown struct thread_data { 180f4fdf2b4SLen Brown struct timeval tv_begin; 181f4fdf2b4SLen Brown struct timeval tv_end; 182d4794f25SYazen Ghannam struct timeval tv_delta; 183c98d5d94SLen Brown unsigned long long tsc; 184c98d5d94SLen Brown unsigned long long aperf; 185c98d5d94SLen Brown unsigned long long mperf; 186144b44b1SLen Brown unsigned long long c1; 1872af4f9b8SLen Brown unsigned long long instr_count; 1880de6c0dfSLen Brown unsigned long long irq_count; 1891ed51011SLen Brown unsigned int smi_count; 190c98d5d94SLen Brown unsigned int cpu_id; 1914c2122d4SLen Brown unsigned int apic_id; 1924c2122d4SLen Brown unsigned int x2apic_id; 193c98d5d94SLen Brown unsigned int flags; 1947ab5ff49SZhang Rui bool is_atom; 195c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 196c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1970748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 198c98d5d94SLen Brown } *thread_even, *thread_odd; 199c98d5d94SLen Brown 200c98d5d94SLen Brown struct core_data { 201c98d5d94SLen Brown unsigned long long c3; 202c98d5d94SLen Brown unsigned long long c6; 203c98d5d94SLen Brown unsigned long long c7; 2040539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 205889facbeSLen Brown unsigned int core_temp_c; 2069392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 207c98d5d94SLen Brown unsigned int core_id; 208678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 209c98d5d94SLen Brown } *core_even, *core_odd; 210c98d5d94SLen Brown 211c98d5d94SLen Brown struct pkg_data { 212c98d5d94SLen Brown unsigned long long pc2; 213c98d5d94SLen Brown unsigned long long pc3; 214c98d5d94SLen Brown unsigned long long pc6; 215c98d5d94SLen Brown unsigned long long pc7; 216ca58710fSKristen Carlson Accardi unsigned long long pc8; 217ca58710fSKristen Carlson Accardi unsigned long long pc9; 218ca58710fSKristen Carlson Accardi unsigned long long pc10; 219be0e54c4SLen Brown unsigned long long cpu_lpi; 220be0e54c4SLen Brown unsigned long long sys_lpi; 2210b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2220b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2230b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2240b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2259185e988SLen Brown long long gfx_rc6_ms; 22627d47356SLen Brown unsigned int gfx_mhz; 227b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 228c98d5d94SLen Brown unsigned int package_id; 2297c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2307c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2317c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2327c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2337c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2347c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 235889facbeSLen Brown unsigned int pkg_temp_c; 236678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 237c98d5d94SLen Brown } *package_even, *package_odd; 238c98d5d94SLen Brown 239c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 240c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 241c98d5d94SLen Brown 24240f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 24340f5cfe7SPrarit Bhargava ((thread_base) + \ 24440f5cfe7SPrarit Bhargava ((pkg_no) * \ 24540f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 24640f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 24740f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 24840f5cfe7SPrarit Bhargava (thread_no)) 24940f5cfe7SPrarit Bhargava 25040f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 25140f5cfe7SPrarit Bhargava ((core_base) + \ 25240f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 25340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 25440f5cfe7SPrarit Bhargava (core_no)) 25540f5cfe7SPrarit Bhargava 256c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 257c98d5d94SLen Brown 258388e9c81SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE }; 25941618e63SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC }; 260388e9c81SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT }; 261388e9c81SLen Brown 262388e9c81SLen Brown struct msr_counter { 263388e9c81SLen Brown unsigned int msr_num; 264388e9c81SLen Brown char name[NAME_BYTES]; 265495c7654SLen Brown char path[PATH_BYTES]; 266388e9c81SLen Brown unsigned int width; 267388e9c81SLen Brown enum counter_type type; 268388e9c81SLen Brown enum counter_format format; 269388e9c81SLen Brown struct msr_counter *next; 270812db3f7SLen Brown unsigned int flags; 271812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 272812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 27341618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 274388e9c81SLen Brown }; 275388e9c81SLen Brown 27687e15da9SChen Yu /* 27787e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 27887e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 27987e15da9SChen Yu * despite its register's bit width. 28087e15da9SChen Yu */ 28187e15da9SChen Yu enum { 28287e15da9SChen Yu IDX_PKG_ENERGY, 28387e15da9SChen Yu IDX_DRAM_ENERGY, 28487e15da9SChen Yu IDX_PP0_ENERGY, 28587e15da9SChen Yu IDX_PP1_ENERGY, 28687e15da9SChen Yu IDX_PKG_PERF, 28787e15da9SChen Yu IDX_DRAM_PERF, 28887e15da9SChen Yu IDX_COUNT, 28987e15da9SChen Yu }; 29087e15da9SChen Yu 29187e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 29287e15da9SChen Yu 29387e15da9SChen Yu struct msr_sum_array { 29487e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 29587e15da9SChen Yu struct { 29687e15da9SChen Yu /*The accumulated MSR value is updated by the timer */ 29787e15da9SChen Yu unsigned long long sum; 29887e15da9SChen Yu /*The MSR footprint recorded in last timer */ 29987e15da9SChen Yu unsigned long long last; 30087e15da9SChen Yu } entries[IDX_COUNT]; 30187e15da9SChen Yu }; 30287e15da9SChen Yu 30387e15da9SChen Yu /* The percpu MSR sum array.*/ 30487e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 30587e15da9SChen Yu 30613a779deSCalvin Walton off_t idx_to_offset(int idx) 30787e15da9SChen Yu { 30813a779deSCalvin Walton off_t offset; 30987e15da9SChen Yu 31087e15da9SChen Yu switch (idx) { 31187e15da9SChen Yu case IDX_PKG_ENERGY: 312301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H) 313301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT; 314301b1d3aSBas Nieuwenhuizen else 31587e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 31687e15da9SChen Yu break; 31787e15da9SChen Yu case IDX_DRAM_ENERGY: 31887e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 31987e15da9SChen Yu break; 32087e15da9SChen Yu case IDX_PP0_ENERGY: 32187e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 32287e15da9SChen Yu break; 32387e15da9SChen Yu case IDX_PP1_ENERGY: 32487e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 32587e15da9SChen Yu break; 32687e15da9SChen Yu case IDX_PKG_PERF: 32787e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 32887e15da9SChen Yu break; 32987e15da9SChen Yu case IDX_DRAM_PERF: 33087e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 33187e15da9SChen Yu break; 33287e15da9SChen Yu default: 33387e15da9SChen Yu offset = -1; 33487e15da9SChen Yu } 33587e15da9SChen Yu return offset; 33687e15da9SChen Yu } 33787e15da9SChen Yu 33813a779deSCalvin Walton int offset_to_idx(off_t offset) 33987e15da9SChen Yu { 34087e15da9SChen Yu int idx; 34187e15da9SChen Yu 34287e15da9SChen Yu switch (offset) { 34387e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 344301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT: 34587e15da9SChen Yu idx = IDX_PKG_ENERGY; 34687e15da9SChen Yu break; 34787e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 34887e15da9SChen Yu idx = IDX_DRAM_ENERGY; 34987e15da9SChen Yu break; 35087e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 35187e15da9SChen Yu idx = IDX_PP0_ENERGY; 35287e15da9SChen Yu break; 35387e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 35487e15da9SChen Yu idx = IDX_PP1_ENERGY; 35587e15da9SChen Yu break; 35687e15da9SChen Yu case MSR_PKG_PERF_STATUS: 35787e15da9SChen Yu idx = IDX_PKG_PERF; 35887e15da9SChen Yu break; 35987e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 36087e15da9SChen Yu idx = IDX_DRAM_PERF; 36187e15da9SChen Yu break; 36287e15da9SChen Yu default: 36387e15da9SChen Yu idx = -1; 36487e15da9SChen Yu } 36587e15da9SChen Yu return idx; 36687e15da9SChen Yu } 36787e15da9SChen Yu 36887e15da9SChen Yu int idx_valid(int idx) 36987e15da9SChen Yu { 37087e15da9SChen Yu switch (idx) { 37187e15da9SChen Yu case IDX_PKG_ENERGY: 372301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H); 37387e15da9SChen Yu case IDX_DRAM_ENERGY: 37487e15da9SChen Yu return do_rapl & RAPL_DRAM; 37587e15da9SChen Yu case IDX_PP0_ENERGY: 37687e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 37787e15da9SChen Yu case IDX_PP1_ENERGY: 37887e15da9SChen Yu return do_rapl & RAPL_GFX; 37987e15da9SChen Yu case IDX_PKG_PERF: 38087e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 38187e15da9SChen Yu case IDX_DRAM_PERF: 38287e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 38387e15da9SChen Yu default: 38487e15da9SChen Yu return 0; 38587e15da9SChen Yu } 38687e15da9SChen Yu } 3871b439f01SLen Brown 388388e9c81SLen Brown struct sys_counters { 389678a3bd1SLen Brown unsigned int added_thread_counters; 390678a3bd1SLen Brown unsigned int added_core_counters; 391678a3bd1SLen Brown unsigned int added_package_counters; 392388e9c81SLen Brown struct msr_counter *tp; 393388e9c81SLen Brown struct msr_counter *cp; 394388e9c81SLen Brown struct msr_counter *pp; 395388e9c81SLen Brown } sys; 396388e9c81SLen Brown 397c98d5d94SLen Brown struct system_summary { 398c98d5d94SLen Brown struct thread_data threads; 399c98d5d94SLen Brown struct core_data cores; 400c98d5d94SLen Brown struct pkg_data packages; 401388e9c81SLen Brown } average; 402c98d5d94SLen Brown 4030e2d8f05SLen Brown struct cpu_topology { 4040e2d8f05SLen Brown int physical_package_id; 4056de68fe1SLen Brown int die_id; 4060e2d8f05SLen Brown int logical_cpu_id; 407ef605741SPrarit Bhargava int physical_node_id; 408ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 4090e2d8f05SLen Brown int physical_core_id; 4108cb48b32SPrarit Bhargava int thread_id; 4110e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 4120e2d8f05SLen Brown } *cpus; 413c98d5d94SLen Brown 414c98d5d94SLen Brown struct topo_params { 415c98d5d94SLen Brown int num_packages; 4166de68fe1SLen Brown int num_die; 417103a8feaSLen Brown int num_cpus; 418c98d5d94SLen Brown int num_cores; 419c98d5d94SLen Brown int max_cpu_num; 420ef605741SPrarit Bhargava int max_node_num; 42170a9c6e8SPrarit Bhargava int nodes_per_pkg; 42270a9c6e8SPrarit Bhargava int cores_per_node; 42370a9c6e8SPrarit Bhargava int threads_per_core; 424c98d5d94SLen Brown } topo; 425103a8feaSLen Brown 426c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 427103a8feaSLen Brown 428562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 429562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 430562a2d37SLen Brown 431c98d5d94SLen Brown void setup_all_buffers(void); 432103a8feaSLen Brown 4331f81c5efSLen Brown char *sys_lpi_file; 4341f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 4351f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 4361f81c5efSLen Brown 437c98d5d94SLen Brown int cpu_is_not_present(int cpu) 438d15cf7c1SLen Brown { 439c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 440c98d5d94SLen Brown } 4411b439f01SLen Brown 442c98d5d94SLen Brown /* 443c98d5d94SLen Brown * run func(thread, core, package) in topology order 444c98d5d94SLen Brown * skip non-present cpus 445c98d5d94SLen Brown */ 446c98d5d94SLen Brown 447c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), 448c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 449c98d5d94SLen Brown { 45040f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 451c98d5d94SLen Brown 452c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 453df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 45470a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 4551b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 456c98d5d94SLen Brown struct thread_data *t; 457c98d5d94SLen Brown struct core_data *c; 458c98d5d94SLen Brown struct pkg_data *p; 459c98d5d94SLen Brown 4601b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 461c98d5d94SLen Brown 462c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 463c98d5d94SLen Brown continue; 464c98d5d94SLen Brown 4651b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 466c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 467c98d5d94SLen Brown 468c98d5d94SLen Brown retval = func(t, c, p); 469c98d5d94SLen Brown if (retval) 470c98d5d94SLen Brown return retval; 471c98d5d94SLen Brown } 472c98d5d94SLen Brown } 473c98d5d94SLen Brown } 47440f5cfe7SPrarit Bhargava } 475d15cf7c1SLen Brown return 0; 476d15cf7c1SLen Brown } 477d15cf7c1SLen Brown 47888c3281fSLen Brown int cpu_migrate(int cpu) 47988c3281fSLen Brown { 480c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 481c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 482c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 48388c3281fSLen Brown return -1; 48488c3281fSLen Brown else 48588c3281fSLen Brown return 0; 48688c3281fSLen Brown } 4871b439f01SLen Brown 48836229897SLen Brown int get_msr_fd(int cpu) 489103a8feaSLen Brown { 490103a8feaSLen Brown char pathname[32]; 491103a8feaSLen Brown int fd; 492103a8feaSLen Brown 49336229897SLen Brown fd = fd_percpu[cpu]; 49436229897SLen Brown 49536229897SLen Brown if (fd) 49636229897SLen Brown return fd; 49736229897SLen Brown 498103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 499103a8feaSLen Brown fd = open(pathname, O_RDONLY); 50015aaa346SLen Brown if (fd < 0) 50198481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 502103a8feaSLen Brown 50336229897SLen Brown fd_percpu[cpu] = fd; 50436229897SLen Brown 50536229897SLen Brown return fd; 50636229897SLen Brown } 50736229897SLen Brown 5082af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 5092af4f9b8SLen Brown { 5102af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 5112af4f9b8SLen Brown } 5122af4f9b8SLen Brown 5132af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 5142af4f9b8SLen Brown { 5152af4f9b8SLen Brown struct perf_event_attr pea; 5162af4f9b8SLen Brown int fd; 5172af4f9b8SLen Brown 5182af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 5192af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 5202af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 5212af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 5222af4f9b8SLen Brown 5232af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 5242af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 5252af4f9b8SLen Brown if (fd == -1) 5262af4f9b8SLen Brown err(-1, "cpu%d: perf instruction counter\n", cpu_num); 5272af4f9b8SLen Brown 5282af4f9b8SLen Brown return fd; 5292af4f9b8SLen Brown } 5302af4f9b8SLen Brown 5312af4f9b8SLen Brown int get_instr_count_fd(int cpu) 5322af4f9b8SLen Brown { 5332af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 5342af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5352af4f9b8SLen Brown 5362af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 5372af4f9b8SLen Brown 5382af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5392af4f9b8SLen Brown } 5402af4f9b8SLen Brown 54136229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 54236229897SLen Brown { 54336229897SLen Brown ssize_t retval; 54436229897SLen Brown 54536229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 54615aaa346SLen Brown 54798481e79SLen Brown if (retval != sizeof *msr) 548cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 54915aaa346SLen Brown 55015aaa346SLen Brown return 0; 551103a8feaSLen Brown } 552103a8feaSLen Brown 553fc04cc67SLen Brown /* 554bdd5ae3aSLen Brown * This list matches the column headers, except 555bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 556bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 557bdd5ae3aSLen Brown * matching on them for --show and --hide. 558fc04cc67SLen Brown */ 559812db3f7SLen Brown struct msr_counter bic[] = { 5603f44a5c6SLen Brown { 0x0, "usec" }, 5613f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 562812db3f7SLen Brown { 0x0, "Package" }, 563bdd5ae3aSLen Brown { 0x0, "Node" }, 564812db3f7SLen Brown { 0x0, "Avg_MHz" }, 565bdd5ae3aSLen Brown { 0x0, "Busy%" }, 566812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 567812db3f7SLen Brown { 0x0, "TSC_MHz" }, 568812db3f7SLen Brown { 0x0, "IRQ" }, 569495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL }, 570bdd5ae3aSLen Brown { 0x0, "sysfs" }, 571812db3f7SLen Brown { 0x0, "CPU%c1" }, 572812db3f7SLen Brown { 0x0, "CPU%c3" }, 573812db3f7SLen Brown { 0x0, "CPU%c6" }, 574812db3f7SLen Brown { 0x0, "CPU%c7" }, 575812db3f7SLen Brown { 0x0, "ThreadC" }, 576812db3f7SLen Brown { 0x0, "CoreTmp" }, 577812db3f7SLen Brown { 0x0, "CoreCnt" }, 578812db3f7SLen Brown { 0x0, "PkgTmp" }, 579812db3f7SLen Brown { 0x0, "GFX%rc6" }, 580812db3f7SLen Brown { 0x0, "GFXMHz" }, 581812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 582812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 583812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 584812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 5850f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 5860f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 5874bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 588be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 589be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 590812db3f7SLen Brown { 0x0, "PkgWatt" }, 591812db3f7SLen Brown { 0x0, "CorWatt" }, 592812db3f7SLen Brown { 0x0, "GFXWatt" }, 593812db3f7SLen Brown { 0x0, "PkgCnt" }, 594812db3f7SLen Brown { 0x0, "RAMWatt" }, 595812db3f7SLen Brown { 0x0, "PKG_%" }, 596812db3f7SLen Brown { 0x0, "RAM_%" }, 597812db3f7SLen Brown { 0x0, "Pkg_J" }, 598812db3f7SLen Brown { 0x0, "Cor_J" }, 599812db3f7SLen Brown { 0x0, "GFX_J" }, 600812db3f7SLen Brown { 0x0, "RAM_J" }, 6010539ba11SLen Brown { 0x0, "Mod%c6" }, 602a99d8730SLen Brown { 0x0, "Totl%C0" }, 603a99d8730SLen Brown { 0x0, "Any%C0" }, 604a99d8730SLen Brown { 0x0, "GFX%C0" }, 605a99d8730SLen Brown { 0x0, "CPUGFX%" }, 606bdd5ae3aSLen Brown { 0x0, "Core" }, 607bdd5ae3aSLen Brown { 0x0, "CPU" }, 6084c2122d4SLen Brown { 0x0, "APIC" }, 6094c2122d4SLen Brown { 0x0, "X2APIC" }, 6106de68fe1SLen Brown { 0x0, "Die" }, 611b4b91569SRafael Antognolli { 0x0, "GFXAMHz" }, 6122af4f9b8SLen Brown { 0x0, "IPC" }, 613812db3f7SLen Brown }; 614812db3f7SLen Brown 615812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 6163f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 6173f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 6183f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 619bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 620bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 621bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 622bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 623bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 624bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 625bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 626bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 627bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 628bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 629bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 630bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 631bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 632bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 633bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 634bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 635bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 636bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 637bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 638bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 639bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 640bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 641bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 642bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 643bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 644bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 645bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 646bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 647bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 648bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 649bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 650bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 651bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 652bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 653bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 654bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 655bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 656bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 657bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 658bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 659bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 660bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 661bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 662bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 663bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 6644c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 6654c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 6666de68fe1SLen Brown #define BIC_Die (1ULL << 50) 667b4b91569SRafael Antognolli #define BIC_GFXACTMHz (1ULL << 51) 6682af4f9b8SLen Brown #define BIC_IPC (1ULL << 52) 669812db3f7SLen Brown 6704c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 6713f44a5c6SLen Brown 6723f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 6734c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 674812db3f7SLen Brown 675812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 6761e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 6773f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 678812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 6790f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 6802af4f9b8SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 681812db3f7SLen Brown 682dd778a5eSLen Brown #define MAX_DEFERRED 16 683dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 684dd778a5eSLen Brown int deferred_skip_index; 685dd778a5eSLen Brown 686dd778a5eSLen Brown /* 687dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 688dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 689dd778a5eSLen Brown */ 690dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 691dd778a5eSLen Brown 692dd778a5eSLen Brown void help(void) 693dd778a5eSLen Brown { 694dd778a5eSLen Brown fprintf(outf, 695dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 696dd778a5eSLen Brown "\n" 697dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 698dd778a5eSLen Brown "when COMMAND completes.\n" 699dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 700dd778a5eSLen Brown "to print statistics, until interrupted.\n" 701cc481650SNathan Ciobanu " -a, --add add a counter\n" 702dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 703cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 704dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7059ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7069ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7079ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7089ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7099ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7109ce80578SNathan Ciobanu " hide the specified column(s)\n" 711cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 712cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7139ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 714cc481650SNathan Ciobanu " -l, --list list column headers only\n" 715cc481650SNathan Ciobanu " -n, --num_iterations num\n" 716cc481650SNathan Ciobanu " number of the measurement iterations\n" 717cc481650SNathan Ciobanu " -o, --out file\n" 718cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 719cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7209ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7219ce80578SNathan Ciobanu " show only the specified column(s)\n" 7229ce80578SNathan Ciobanu " -S, --Summary\n" 7239ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7249ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7259ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7269ce80578SNathan Ciobanu " degrees Celsius\n" 727cc481650SNathan Ciobanu " -h, --help print this help message\n" 7281b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n"); 729dd778a5eSLen Brown } 730dd778a5eSLen Brown 731812db3f7SLen Brown /* 732812db3f7SLen Brown * bic_lookup 733812db3f7SLen Brown * for all the strings in comma separate name_list, 734812db3f7SLen Brown * set the approprate bit in return value. 735812db3f7SLen Brown */ 736dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 737812db3f7SLen Brown { 738812db3f7SLen Brown int i; 739812db3f7SLen Brown unsigned long long retval = 0; 740812db3f7SLen Brown 741812db3f7SLen Brown while (name_list) { 742812db3f7SLen Brown char *comma; 743812db3f7SLen Brown 744812db3f7SLen Brown comma = strchr(name_list, ','); 745812db3f7SLen Brown 746812db3f7SLen Brown if (comma) 747812db3f7SLen Brown *comma = '\0'; 748812db3f7SLen Brown 7493f44a5c6SLen Brown if (!strcmp(name_list, "all")) 7503f44a5c6SLen Brown return ~0; 7513f44a5c6SLen Brown 752812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 753812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 754812db3f7SLen Brown retval |= (1ULL << i); 755812db3f7SLen Brown break; 756812db3f7SLen Brown } 757812db3f7SLen Brown } 758812db3f7SLen Brown if (i == MAX_BIC) { 759dd778a5eSLen Brown if (mode == SHOW_LIST) { 760812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 761812db3f7SLen Brown exit(-1); 762812db3f7SLen Brown } 763dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 764dd778a5eSLen Brown if (debug) 765dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 766dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 767dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 768dd778a5eSLen Brown MAX_DEFERRED, name_list); 769dd778a5eSLen Brown help(); 770dd778a5eSLen Brown exit(1); 771dd778a5eSLen Brown } 772dd778a5eSLen Brown } 773812db3f7SLen Brown 774812db3f7SLen Brown name_list = comma; 775812db3f7SLen Brown if (name_list) 776812db3f7SLen Brown name_list++; 777812db3f7SLen Brown 778812db3f7SLen Brown } 779812db3f7SLen Brown return retval; 780812db3f7SLen Brown } 781fc04cc67SLen Brown 782c8ade361SLen Brown void print_header(char *delim) 783103a8feaSLen Brown { 784388e9c81SLen Brown struct msr_counter *mp; 7856168c2e0SLen Brown int printed = 0; 786388e9c81SLen Brown 7873f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 7883f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 7893f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 7903f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 791812db3f7SLen Brown if (DO_BIC(BIC_Package)) 7926168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 7936de68fe1SLen Brown if (DO_BIC(BIC_Die)) 7946de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 79501235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 79601235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 797812db3f7SLen Brown if (DO_BIC(BIC_Core)) 7986168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 799812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8006168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8014c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8024c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8034c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8044c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 805812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8066168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 807812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8086168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 809812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8106168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 811812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8126168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8131cc21f7bSLen Brown 8142af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8152af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8162af4f9b8SLen Brown 8170de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8180de6c0dfSLen Brown if (sums_need_wide_columns) 8196168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8200de6c0dfSLen Brown else 8216168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8220de6c0dfSLen Brown } 8230de6c0dfSLen Brown 824812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8256168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8261cc21f7bSLen Brown 827388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 828dd778a5eSLen Brown 829388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 830388e9c81SLen Brown if (mp->width == 64) 831dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 832388e9c81SLen Brown else 833dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 834388e9c81SLen Brown } else { 8350de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 836dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8370de6c0dfSLen Brown else 838dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 839388e9c81SLen Brown } 840388e9c81SLen Brown } 841388e9c81SLen Brown 84241618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8436168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 844562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8456168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 846812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8476168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 848812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8496168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 850678a3bd1SLen Brown 8510539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8526168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 853678a3bd1SLen Brown 854812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8556168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 856388e9c81SLen Brown 8579392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 8589392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8599392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 8609392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 8619392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8629392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 8639392bd98SCalvin Walton } 8649392bd98SCalvin Walton 865388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 866388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 867388e9c81SLen Brown if (mp->width == 64) 868c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 869388e9c81SLen Brown else 870c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 871388e9c81SLen Brown } else { 8720de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8730de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 8740de6c0dfSLen Brown else 875c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 876388e9c81SLen Brown } 877388e9c81SLen Brown } 878388e9c81SLen Brown 879812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 8806168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 881889facbeSLen Brown 882812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 8836168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 884fdf676e5SLen Brown 885812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 8866168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 88727d47356SLen Brown 888b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 889b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 890b4b91569SRafael Antognolli 891a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 8926168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 893a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 8946168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 895a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 8966168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 897a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 8986168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 8990b2bb692SLen Brown 9000f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9016168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9020f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9036168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9040f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9056168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9060f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9076168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9080f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9096168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9100f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9116168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9120f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9136168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 914be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 915be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 916be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 917be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 918103a8feaSLen Brown 9195c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 920812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9216168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9229392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9236168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 924812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9256168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 926812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9276168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 928812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9296168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 930812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9316168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 932d7899447SLen Brown } else if (do_rapl && rapl_joules) { 933812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9346168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9359392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9366168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 937812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9386168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 939812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9406168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 941812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9426168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 943812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9446168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9455c56be9aSDirk Brandewie } 946388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 947388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 948388e9c81SLen Brown if (mp->width == 64) 949c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 950388e9c81SLen Brown else 951c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 952388e9c81SLen Brown } else { 9530de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9540de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9550de6c0dfSLen Brown else 956c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 957388e9c81SLen Brown } 958388e9c81SLen Brown } 959388e9c81SLen Brown 960c98d5d94SLen Brown outp += sprintf(outp, "\n"); 961103a8feaSLen Brown } 962103a8feaSLen Brown 9631b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 964103a8feaSLen Brown { 965388e9c81SLen Brown int i; 966388e9c81SLen Brown struct msr_counter *mp; 967388e9c81SLen Brown 9683b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 969c98d5d94SLen Brown 970c98d5d94SLen Brown if (t) { 9711b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 9723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 9733b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 9743b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 9753b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 9766886fee4SLen Brown 9772af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 9782af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 9792af4f9b8SLen Brown 980812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 9810de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 982812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 983218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 984388e9c81SLen Brown 985388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 9861b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]); 987388e9c81SLen Brown } 988103a8feaSLen Brown } 989103a8feaSLen Brown 990c98d5d94SLen Brown if (c) { 9913b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 9923b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 9933b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 9943b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 9953b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 9969392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 997388e9c81SLen Brown 998388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 9991b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]); 1000388e9c81SLen Brown } 10010539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1002c98d5d94SLen Brown } 1003103a8feaSLen Brown 1004c98d5d94SLen Brown if (p) { 10053b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10060b2bb692SLen Brown 10070b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10080b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10090b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10100b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10110b2bb692SLen Brown 10123b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10163b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10203b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10213b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1022be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1023be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10247c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10257c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10267c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10277c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 10281b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status); 10291b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status); 10303b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1031388e9c81SLen Brown 1032388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 10331b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]); 1034388e9c81SLen Brown } 1035c98d5d94SLen Brown } 10363b4d5c7fSAndy Shevchenko 10373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10383b4d5c7fSAndy Shevchenko 1039c98d5d94SLen Brown return 0; 1040103a8feaSLen Brown } 1041103a8feaSLen Brown 1042e23da037SLen Brown /* 1043e23da037SLen Brown * column formatting convention & formats 1044e23da037SLen Brown */ 10451b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1046103a8feaSLen Brown { 1047008d396eSLen Brown double interval_float, tsc; 1048fc04cc67SLen Brown char *fmt8; 1049388e9c81SLen Brown int i; 1050388e9c81SLen Brown struct msr_counter *mp; 10516168c2e0SLen Brown char *delim = "\t"; 10526168c2e0SLen Brown int printed = 0; 1053103a8feaSLen Brown 1054c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1055c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1056c98d5d94SLen Brown return 0; 1057c98d5d94SLen Brown 1058c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1059c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1060c98d5d94SLen Brown return 0; 1061c98d5d94SLen Brown 10621ef7d21aSLen Brown /*if not summary line and --cpu is used */ 10631b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 10641ef7d21aSLen Brown return 0; 10651ef7d21aSLen Brown 10663f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1067f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1068f4fdf2b4SLen Brown struct timeval tv; 1069f4fdf2b4SLen Brown 1070f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1071f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1072f4fdf2b4SLen Brown } 1073f4fdf2b4SLen Brown 10743f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 10753f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 10763f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 10773f44a5c6SLen Brown 1078d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 1079103a8feaSLen Brown 1080008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1081008d396eSLen Brown 1082c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1083c98d5d94SLen Brown if (t == &average.threads) { 1084812db3f7SLen Brown if (DO_BIC(BIC_Package)) 10856168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10866de68fe1SLen Brown if (DO_BIC(BIC_Die)) 10876de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 108801235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 108901235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1090812db3f7SLen Brown if (DO_BIC(BIC_Core)) 10916168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1092812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 10936168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10944c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 10954c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10964c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 10974c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1098103a8feaSLen Brown } else { 1099812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1100c98d5d94SLen Brown if (p) 11016168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1102c98d5d94SLen Brown else 11036168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1104c98d5d94SLen Brown } 11056de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11066de68fe1SLen Brown if (c) 11076de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11086de68fe1SLen Brown else 11096de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11106de68fe1SLen Brown } 111101235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 111201235041SPrarit Bhargava if (t) 111301235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 11141b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 111501235041SPrarit Bhargava else 11161b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 111701235041SPrarit Bhargava } 1118812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1119c98d5d94SLen Brown if (c) 11206168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1121c98d5d94SLen Brown else 11226168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1123c98d5d94SLen Brown } 1124812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11256168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11264c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11274c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11284c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11294c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1130103a8feaSLen Brown } 1131fc04cc67SLen Brown 1132812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 11331b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 1134fc04cc67SLen Brown 1135812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc); 1137103a8feaSLen Brown 1138812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 113921ed5574SLen Brown if (has_base_hz) 11401b439f01SLen Brown outp += 11411b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 114221ed5574SLen Brown else 11436168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1144008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 114521ed5574SLen Brown } 1146103a8feaSLen Brown 1147812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 11486168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 1149103a8feaSLen Brown 11502af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 11512af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 11522af4f9b8SLen Brown 1153562a2d37SLen Brown /* IRQ */ 11540de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 11550de6c0dfSLen Brown if (sums_need_wide_columns) 11566168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 11570de6c0dfSLen Brown else 11586168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 11590de6c0dfSLen Brown } 1160562a2d37SLen Brown 11611cc21f7bSLen Brown /* SMI */ 1162812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 11636168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 11641cc21f7bSLen Brown 1165678a3bd1SLen Brown /* Added counters */ 1166678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1167678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1168678a3bd1SLen Brown if (mp->width == 32) 11691b439f01SLen Brown outp += 11701b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]); 1171678a3bd1SLen Brown else 11726168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1173678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 11740de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11756168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 11760de6c0dfSLen Brown else 11776168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1178678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 117941618e63SLen Brown if (mp->type == COUNTER_USEC) 11801b439f01SLen Brown outp += 11811b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 11821b439f01SLen Brown t->counter[i] / interval_float / 10000); 118341618e63SLen Brown else 11846168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc); 1185678a3bd1SLen Brown } 1186678a3bd1SLen Brown } 1187678a3bd1SLen Brown 118841618e63SLen Brown /* C1 */ 118941618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 11906168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc); 119141618e63SLen Brown 1192c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1193c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1194c98d5d94SLen Brown goto done; 1195c98d5d94SLen Brown 1196562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 11976168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc); 1198812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 11996168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc); 1200812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12016168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc); 1202c98d5d94SLen Brown 12030539ba11SLen Brown /* Mod%c6 */ 12040539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12056168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12060539ba11SLen Brown 1207812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12086168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1209889facbeSLen Brown 1210388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1211388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1212388e9c81SLen Brown if (mp->width == 32) 12131b439f01SLen Brown outp += 12141b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]); 1215388e9c81SLen Brown else 12166168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1217388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12180de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12196168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12200de6c0dfSLen Brown else 12216168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1222388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc); 1224388e9c81SLen Brown } 1225388e9c81SLen Brown } 1226388e9c81SLen Brown 12279392bd98SCalvin Walton fmt8 = "%s%.2f"; 12289392bd98SCalvin Walton 12299392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12301b439f01SLen Brown outp += 12311b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12329392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12339392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12349392bd98SCalvin Walton 1235c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1236c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1237c98d5d94SLen Brown goto done; 1238c98d5d94SLen Brown 12390b2bb692SLen Brown /* PkgTmp */ 1240812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 12416168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1242889facbeSLen Brown 1243fdf676e5SLen Brown /* GFXrc6 */ 1244812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1245ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 12466168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 12479185e988SLen Brown } else { 12486168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12499185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 12509185e988SLen Brown } 12519185e988SLen Brown } 1252fdf676e5SLen Brown 125327d47356SLen Brown /* GFXMHz */ 1254812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 12556168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 125627d47356SLen Brown 1257b4b91569SRafael Antognolli /* GFXACTMHz */ 1258b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1259b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1260b4b91569SRafael Antognolli 12610b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1262a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12636168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc); 1264a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12656168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc); 1266a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12676168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc); 1268a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12696168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc); 12700b2bb692SLen Brown 12710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 12726168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc); 12730f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 12746168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc); 12750f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 12766168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc); 12770f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 12786168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc); 12790f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 12806168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc); 12810f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 12826168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc); 12830f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 12846168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc); 1285889facbeSLen Brown 1286be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 12871b439f01SLen Brown outp += 12881b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1289be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 12901b439f01SLen Brown outp += 12911b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1292be0e54c4SLen Brown 1293812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 12941b439f01SLen Brown outp += 12951b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 12969392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 12971b439f01SLen Brown outp += 12981b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1299812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 13001b439f01SLen Brown outp += 13011b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1302812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 13031b439f01SLen Brown outp += 13041b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13051b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float); 1306812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13076168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13089392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13096168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1310812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13116168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1312812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13136168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1314812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 13151b439f01SLen Brown outp += 13161b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13171b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1318812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 13191b439f01SLen Brown outp += 13201b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13211b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1322812db3f7SLen Brown 1323388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1324388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1325388e9c81SLen Brown if (mp->width == 32) 13261b439f01SLen Brown outp += 13271b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]); 1328388e9c81SLen Brown else 13296168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1330388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 13310de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 13326168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 13330de6c0dfSLen Brown else 13346168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1335388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 13366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc); 1337388e9c81SLen Brown } 1338388e9c81SLen Brown } 1339388e9c81SLen Brown 1340c98d5d94SLen Brown done: 134194d6ab4bSLen Brown if (*(outp - 1) != '\n') 1342c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1343c98d5d94SLen Brown 1344c98d5d94SLen Brown return 0; 1345103a8feaSLen Brown } 1346103a8feaSLen Brown 1347b7d8c148SLen Brown void flush_output_stdout(void) 1348103a8feaSLen Brown { 1349b7d8c148SLen Brown FILE *filep; 1350b7d8c148SLen Brown 1351b7d8c148SLen Brown if (outf == stderr) 1352b7d8c148SLen Brown filep = stdout; 1353b7d8c148SLen Brown else 1354b7d8c148SLen Brown filep = outf; 1355b7d8c148SLen Brown 1356b7d8c148SLen Brown fputs(output_buffer, filep); 1357b7d8c148SLen Brown fflush(filep); 1358b7d8c148SLen Brown 1359c98d5d94SLen Brown outp = output_buffer; 1360c98d5d94SLen Brown } 13611b439f01SLen Brown 1362b7d8c148SLen Brown void flush_output_stderr(void) 1363c98d5d94SLen Brown { 1364b7d8c148SLen Brown fputs(output_buffer, outf); 1365b7d8c148SLen Brown fflush(outf); 1366c98d5d94SLen Brown outp = output_buffer; 1367c98d5d94SLen Brown } 13681b439f01SLen Brown 1369c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1370c98d5d94SLen Brown { 1371e23da037SLen Brown static int printed; 1372103a8feaSLen Brown 1373e23da037SLen Brown if (!printed || !summary_only) 1374c8ade361SLen Brown print_header("\t"); 1375103a8feaSLen Brown 13769d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1377103a8feaSLen Brown 1378e23da037SLen Brown printed = 1; 1379e23da037SLen Brown 1380e23da037SLen Brown if (summary_only) 1381e23da037SLen Brown return; 1382e23da037SLen Brown 1383c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1384103a8feaSLen Brown } 1385103a8feaSLen Brown 1386889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 13877c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1388889facbeSLen Brown 13891b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old) 1390103a8feaSLen Brown { 1391388e9c81SLen Brown int i; 1392388e9c81SLen Brown struct msr_counter *mp; 13930b2bb692SLen Brown 1394a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13950b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1396a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13970b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1398a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13990b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1400a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14010b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1402a99d8730SLen Brown 1403c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 14040f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1405c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 14060f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1407c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 14080f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1409c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1410ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1411ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1412ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1413be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1414be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1415889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1416889facbeSLen Brown 14179185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 14189185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 14199185e988SLen Brown old->gfx_rc6_ms = -1; 14209185e988SLen Brown else 1421fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 14229185e988SLen Brown 142327d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1424b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 142527d47356SLen Brown 142687e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 142787e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 142887e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 142987e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 143087e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 143187e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1432ba3dec99SLen Brown 1433388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1434388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1435388e9c81SLen Brown old->counter[i] = new->counter[i]; 1436388e9c81SLen Brown else 1437388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1438388e9c81SLen Brown } 1439388e9c81SLen Brown 1440ba3dec99SLen Brown return 0; 1441103a8feaSLen Brown } 1442103a8feaSLen Brown 14431b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old) 1444c98d5d94SLen Brown { 1445388e9c81SLen Brown int i; 1446388e9c81SLen Brown struct msr_counter *mp; 1447388e9c81SLen Brown 1448c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1449c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1450c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1451889facbeSLen Brown old->core_temp_c = new->core_temp_c; 14520539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1453388e9c81SLen Brown 14549392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 14559392bd98SCalvin Walton 1456388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1457388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1458388e9c81SLen Brown old->counter[i] = new->counter[i]; 1459388e9c81SLen Brown else 1460388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1461388e9c81SLen Brown } 1462103a8feaSLen Brown } 1463c98d5d94SLen Brown 14641e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 14651e9042b9SSrinivas Pandruvada { 14661e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 14671e9042b9SSrinivas Pandruvada return 0; 14681e9042b9SSrinivas Pandruvada 14691e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 14701e9042b9SSrinivas Pandruvada } 14711e9042b9SSrinivas Pandruvada 1472c3ae331dSLen Brown /* 1473c3ae331dSLen Brown * old = new - old 1474c3ae331dSLen Brown */ 14751b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 1476c98d5d94SLen Brown { 1477388e9c81SLen Brown int i; 1478388e9c81SLen Brown struct msr_counter *mp; 1479388e9c81SLen Brown 14804c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 14814c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14824c2122d4SLen Brown new->apic_id = old->apic_id; 14834c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14844c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 14854c2122d4SLen Brown 14863f44a5c6SLen Brown /* 14873f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 14883f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 14893f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 14903f44a5c6SLen Brown */ 14913f44a5c6SLen Brown 1492d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 14933f44a5c6SLen Brown old->tv_begin = new->tv_begin; 14943f44a5c6SLen Brown old->tv_end = new->tv_end; 14953f44a5c6SLen Brown 1496c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1497c98d5d94SLen Brown 1498103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1499b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1500b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1501b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1502b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1503103a8feaSLen Brown 1504c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1505c98d5d94SLen Brown 15061b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 1507c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1508c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1509c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1510c98d5d94SLen Brown } else { 1511ba3dec99SLen Brown return -1; 1512103a8feaSLen Brown } 1513a729617cSLen Brown } 1514103a8feaSLen Brown 1515144b44b1SLen Brown if (use_c1_residency_msr) { 1516144b44b1SLen Brown /* 1517144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1518144b44b1SLen Brown * which should be more accurate than the derivation below. 1519144b44b1SLen Brown */ 1520144b44b1SLen Brown } else { 1521103a8feaSLen Brown /* 1522c3ae331dSLen Brown * As counter collection is not atomic, 1523c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1524103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1525103a8feaSLen Brown */ 152695149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1527c98d5d94SLen Brown old->c1 = 0; 1528c98d5d94SLen Brown else { 1529c98d5d94SLen Brown /* normal case, derive c1 */ 1530008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1531c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1532c98d5d94SLen Brown } 1533144b44b1SLen Brown } 1534c3ae331dSLen Brown 1535c98d5d94SLen Brown if (old->mperf == 0) { 1536b7d8c148SLen Brown if (debug > 1) 1537b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1538c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1539c98d5d94SLen Brown } 1540103a8feaSLen Brown 15412af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 15422af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 15432af4f9b8SLen Brown 1544812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1545562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1546562a2d37SLen Brown 1547812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 15481ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1549ba3dec99SLen Brown 1550388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1551388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1552388e9c81SLen Brown old->counter[i] = new->counter[i]; 1553388e9c81SLen Brown else 1554388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1555388e9c81SLen Brown } 1556ba3dec99SLen Brown return 0; 1557103a8feaSLen Brown } 1558c98d5d94SLen Brown 1559c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 15601b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 1561c98d5d94SLen Brown { 1562ba3dec99SLen Brown int retval = 0; 1563ba3dec99SLen Brown 1564c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1565c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1566c98d5d94SLen Brown delta_core(c, c2); 1567c98d5d94SLen Brown 1568c98d5d94SLen Brown /* always calculate thread delta */ 1569ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1570ba3dec99SLen Brown if (retval) 1571ba3dec99SLen Brown return retval; 1572c98d5d94SLen Brown 1573c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1574c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1575ba3dec99SLen Brown retval = delta_package(p, p2); 1576c98d5d94SLen Brown 1577ba3dec99SLen Brown return retval; 1578103a8feaSLen Brown } 1579103a8feaSLen Brown 1580c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1581103a8feaSLen Brown { 1582388e9c81SLen Brown int i; 1583388e9c81SLen Brown struct msr_counter *mp; 1584388e9c81SLen Brown 15853f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 15863f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 15873f44a5c6SLen Brown t->tv_end.tv_sec = 0; 15883f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1589d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1590d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 15913f44a5c6SLen Brown 1592c98d5d94SLen Brown t->tsc = 0; 1593c98d5d94SLen Brown t->aperf = 0; 1594c98d5d94SLen Brown t->mperf = 0; 1595c98d5d94SLen Brown t->c1 = 0; 1596103a8feaSLen Brown 15972af4f9b8SLen Brown t->instr_count = 0; 15982af4f9b8SLen Brown 1599562a2d37SLen Brown t->irq_count = 0; 1600562a2d37SLen Brown t->smi_count = 0; 1601562a2d37SLen Brown 1602c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1603c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1604c98d5d94SLen Brown 1605c98d5d94SLen Brown c->c3 = 0; 1606c98d5d94SLen Brown c->c6 = 0; 1607c98d5d94SLen Brown c->c7 = 0; 16080539ba11SLen Brown c->mc6_us = 0; 1609889facbeSLen Brown c->core_temp_c = 0; 16109392bd98SCalvin Walton c->core_energy = 0; 1611c98d5d94SLen Brown 16120b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16130b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16140b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16150b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16160b2bb692SLen Brown 1617c98d5d94SLen Brown p->pc2 = 0; 16180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1619c98d5d94SLen Brown p->pc3 = 0; 16200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1621c98d5d94SLen Brown p->pc6 = 0; 16220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1623c98d5d94SLen Brown p->pc7 = 0; 1624ca58710fSKristen Carlson Accardi p->pc8 = 0; 1625ca58710fSKristen Carlson Accardi p->pc9 = 0; 1626ca58710fSKristen Carlson Accardi p->pc10 = 0; 1627be0e54c4SLen Brown p->cpu_lpi = 0; 1628be0e54c4SLen Brown p->sys_lpi = 0; 1629889facbeSLen Brown 1630889facbeSLen Brown p->energy_pkg = 0; 1631889facbeSLen Brown p->energy_dram = 0; 1632889facbeSLen Brown p->energy_cores = 0; 1633889facbeSLen Brown p->energy_gfx = 0; 1634889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1635889facbeSLen Brown p->rapl_dram_perf_status = 0; 1636889facbeSLen Brown p->pkg_temp_c = 0; 163727d47356SLen Brown 1638fdf676e5SLen Brown p->gfx_rc6_ms = 0; 163927d47356SLen Brown p->gfx_mhz = 0; 1640b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1641388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1642388e9c81SLen Brown t->counter[i] = 0; 1643388e9c81SLen Brown 1644388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1645388e9c81SLen Brown c->counter[i] = 0; 1646388e9c81SLen Brown 1647388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1648388e9c81SLen Brown p->counter[i] = 0; 1649103a8feaSLen Brown } 16501b439f01SLen Brown 16511b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1652103a8feaSLen Brown { 1653388e9c81SLen Brown int i; 1654388e9c81SLen Brown struct msr_counter *mp; 1655388e9c81SLen Brown 16564c2122d4SLen Brown /* copy un-changing apic_id's */ 16574c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 16584c2122d4SLen Brown average.threads.apic_id = t->apic_id; 16594c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 16604c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 16614c2122d4SLen Brown 16623f44a5c6SLen Brown /* remember first tv_begin */ 16633f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 16643f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 16653f44a5c6SLen Brown 16663f44a5c6SLen Brown /* remember last tv_end */ 16673f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 16683f44a5c6SLen Brown 1669c98d5d94SLen Brown average.threads.tsc += t->tsc; 1670c98d5d94SLen Brown average.threads.aperf += t->aperf; 1671c98d5d94SLen Brown average.threads.mperf += t->mperf; 1672c98d5d94SLen Brown average.threads.c1 += t->c1; 167315aaa346SLen Brown 16742af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 16752af4f9b8SLen Brown 1676562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1677562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1678562a2d37SLen Brown 1679388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1680388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1681388e9c81SLen Brown continue; 1682388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1683388e9c81SLen Brown } 1684388e9c81SLen Brown 1685c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1686c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1687c98d5d94SLen Brown return 0; 1688c98d5d94SLen Brown 1689c98d5d94SLen Brown average.cores.c3 += c->c3; 1690c98d5d94SLen Brown average.cores.c6 += c->c6; 1691c98d5d94SLen Brown average.cores.c7 += c->c7; 16920539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1693c98d5d94SLen Brown 1694889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1695889facbeSLen Brown 16969392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 16979392bd98SCalvin Walton 1698388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1699388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1700388e9c81SLen Brown continue; 1701388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1702388e9c81SLen Brown } 1703388e9c81SLen Brown 1704c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1705c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1706c98d5d94SLen Brown return 0; 1707c98d5d94SLen Brown 1708a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17090b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1710a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17110b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1712a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17130b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1714a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17150b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17160b2bb692SLen Brown 1717c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1719c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1721c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1723c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1724ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1725ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1726ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1727c98d5d94SLen Brown 1728be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1729be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1730be0e54c4SLen Brown 1731889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1732889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1733889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1734889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1735889facbeSLen Brown 1736fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 173727d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1738b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 173927d47356SLen Brown 1740889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1741889facbeSLen Brown 1742889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1743889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1744388e9c81SLen Brown 1745388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1746388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1747388e9c81SLen Brown continue; 1748388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1749388e9c81SLen Brown } 1750c98d5d94SLen Brown return 0; 1751c98d5d94SLen Brown } 17521b439f01SLen Brown 1753c98d5d94SLen Brown /* 1754c98d5d94SLen Brown * sum the counters for all cpus in the system 1755c98d5d94SLen Brown * compute the weighted average 1756c98d5d94SLen Brown */ 17571b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1758c98d5d94SLen Brown { 1759388e9c81SLen Brown int i; 1760388e9c81SLen Brown struct msr_counter *mp; 1761388e9c81SLen Brown 1762c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1763c98d5d94SLen Brown 1764c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1765c98d5d94SLen Brown 1766d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1767d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1768d4794f25SYazen Ghannam 1769c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1770c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1771c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 17722af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1773c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1774c98d5d94SLen Brown 17750de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 17760de6c0dfSLen Brown sums_need_wide_columns = 1; 17770de6c0dfSLen Brown 1778c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1779c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1780c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 17810539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1782c98d5d94SLen Brown 1783a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17840b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1785a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17860b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1787a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17880b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1789a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17900b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 17910b2bb692SLen Brown 1792c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 17930f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1794c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 17950f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1796c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 17970f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1798c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1799ca58710fSKristen Carlson Accardi 1800ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1801ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1802ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1803388e9c81SLen Brown 1804388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1805388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1806388e9c81SLen Brown continue; 18070de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18080de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18090de6c0dfSLen Brown sums_need_wide_columns = 1; 181041618e63SLen Brown continue; 18110de6c0dfSLen Brown } 1812388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1813388e9c81SLen Brown } 1814388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1815388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1816388e9c81SLen Brown continue; 18170de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18180de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18190de6c0dfSLen Brown sums_need_wide_columns = 1; 18200de6c0dfSLen Brown } 1821388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1822388e9c81SLen Brown } 1823388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1824388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1825388e9c81SLen Brown continue; 18260de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18270de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18280de6c0dfSLen Brown sums_need_wide_columns = 1; 18290de6c0dfSLen Brown } 1830388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1831388e9c81SLen Brown } 1832c98d5d94SLen Brown } 1833c98d5d94SLen Brown 1834c98d5d94SLen Brown static unsigned long long rdtsc(void) 1835c98d5d94SLen Brown { 1836c98d5d94SLen Brown unsigned int low, high; 1837c98d5d94SLen Brown 1838c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high)); 1839c98d5d94SLen Brown 1840c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1841c98d5d94SLen Brown } 1842c98d5d94SLen Brown 1843c98d5d94SLen Brown /* 1844495c7654SLen Brown * Open a file, and exit on failure 1845495c7654SLen Brown */ 1846495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1847495c7654SLen Brown { 1848495c7654SLen Brown FILE *filep = fopen(path, mode); 1849495c7654SLen Brown 1850495c7654SLen Brown if (!filep) 1851495c7654SLen Brown err(1, "%s: open failed", path); 1852495c7654SLen Brown return filep; 1853495c7654SLen Brown } 18541b439f01SLen Brown 1855495c7654SLen Brown /* 1856495c7654SLen Brown * snapshot_sysfs_counter() 1857495c7654SLen Brown * 1858495c7654SLen Brown * return snapshot of given counter 1859495c7654SLen Brown */ 1860495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1861495c7654SLen Brown { 1862495c7654SLen Brown FILE *fp; 1863495c7654SLen Brown int retval; 1864495c7654SLen Brown unsigned long long counter; 1865495c7654SLen Brown 1866495c7654SLen Brown fp = fopen_or_die(path, "r"); 1867495c7654SLen Brown 1868495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1869495c7654SLen Brown if (retval != 1) 1870495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1871495c7654SLen Brown 1872495c7654SLen Brown fclose(fp); 1873495c7654SLen Brown 1874495c7654SLen Brown return counter; 1875495c7654SLen Brown } 1876495c7654SLen Brown 1877495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1878495c7654SLen Brown { 1879495c7654SLen Brown if (mp->msr_num != 0) { 1880495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1881495c7654SLen Brown return -1; 1882495c7654SLen Brown } else { 188346c27978SLen Brown char path[128 + PATH_BYTES]; 188441618e63SLen Brown 188541618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 18861b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path); 188741618e63SLen Brown 188841618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 188941618e63SLen Brown } else { 1890495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1891495c7654SLen Brown } 189241618e63SLen Brown } 1893495c7654SLen Brown 1894495c7654SLen Brown return 0; 1895495c7654SLen Brown } 1896495c7654SLen Brown 18976d6501d9SBorislav Petkov int get_epb(int cpu) 18986d6501d9SBorislav Petkov { 18996d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19007f1b11baSBorislav Petkov unsigned long long msr; 19016d6501d9SBorislav Petkov int ret, epb = -1; 19026d6501d9SBorislav Petkov FILE *fp; 19036d6501d9SBorislav Petkov 19046d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19056d6501d9SBorislav Petkov 19067f1b11baSBorislav Petkov fp = fopen(path, "r"); 19077f1b11baSBorislav Petkov if (!fp) 19087f1b11baSBorislav Petkov goto msr_fallback; 19096d6501d9SBorislav Petkov 19106d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19116d6501d9SBorislav Petkov if (ret != 1) 19126d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19136d6501d9SBorislav Petkov 19146d6501d9SBorislav Petkov fclose(fp); 19156d6501d9SBorislav Petkov 19166d6501d9SBorislav Petkov return epb; 19177f1b11baSBorislav Petkov 19187f1b11baSBorislav Petkov msr_fallback: 19197f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 19207f1b11baSBorislav Petkov 19217f1b11baSBorislav Petkov return msr & 0xf; 19226d6501d9SBorislav Petkov } 19236d6501d9SBorislav Petkov 19244c2122d4SLen Brown void get_apic_id(struct thread_data *t) 19254c2122d4SLen Brown { 192634041551SLen Brown unsigned int eax, ebx, ecx, edx; 192734041551SLen Brown 192834041551SLen Brown if (DO_BIC(BIC_APIC)) { 192934041551SLen Brown eax = ebx = ecx = edx = 0; 193034041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 193134041551SLen Brown 193234041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 193334041551SLen Brown } 193434041551SLen Brown 193534041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 193634041551SLen Brown return; 193734041551SLen Brown 1938c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 193934041551SLen Brown unsigned int topology_extensions; 194034041551SLen Brown 194134041551SLen Brown if (max_extended_level < 0x8000001e) 194234041551SLen Brown return; 19434c2122d4SLen Brown 19444c2122d4SLen Brown eax = ebx = ecx = edx = 0; 194534041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 194634041551SLen Brown topology_extensions = ecx & (1 << 22); 194734041551SLen Brown 194834041551SLen Brown if (topology_extensions == 0) 194934041551SLen Brown return; 195034041551SLen Brown 195134041551SLen Brown eax = ebx = ecx = edx = 0; 195234041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 195334041551SLen Brown 195434041551SLen Brown t->x2apic_id = eax; 195534041551SLen Brown return; 195634041551SLen Brown } 19574c2122d4SLen Brown 19584c2122d4SLen Brown if (!genuine_intel) 19594c2122d4SLen Brown return; 19604c2122d4SLen Brown 19614c2122d4SLen Brown if (max_level < 0xb) 19624c2122d4SLen Brown return; 19634c2122d4SLen Brown 19644c2122d4SLen Brown ecx = 0; 19654c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 19664c2122d4SLen Brown t->x2apic_id = edx; 19674c2122d4SLen Brown 196834041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 19691b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 19704c2122d4SLen Brown } 19714c2122d4SLen Brown 1972495c7654SLen Brown /* 1973c98d5d94SLen Brown * get_counters(...) 1974c98d5d94SLen Brown * migrate to cpu 1975c98d5d94SLen Brown * acquire and record local counters for that cpu 1976c98d5d94SLen Brown */ 1977c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1978c98d5d94SLen Brown { 1979c98d5d94SLen Brown int cpu = t->cpu_id; 1980889facbeSLen Brown unsigned long long msr; 19810102b067SLen Brown int aperf_mperf_retry_count = 0; 1982388e9c81SLen Brown struct msr_counter *mp; 1983388e9c81SLen Brown int i; 1984c98d5d94SLen Brown 1985e52966c0SLen Brown if (cpu_migrate(cpu)) { 19863d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 198715aaa346SLen Brown return -1; 1988e52966c0SLen Brown } 198915aaa346SLen Brown 1990d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1991d4794f25SYazen Ghannam 19924c2122d4SLen Brown if (first_counter_read) 19934c2122d4SLen Brown get_apic_id(t); 19940102b067SLen Brown retry: 1995c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 199615aaa346SLen Brown 19971b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 19980102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 19990102b067SLen Brown 20000102b067SLen Brown /* 20010102b067SLen Brown * The TSC, APERF and MPERF must be read together for 20020102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 20030102b067SLen Brown * 20040102b067SLen Brown * Unfortunately, APERF and MPERF are read by 20050102b067SLen Brown * individual system call, so delays may occur 20060102b067SLen Brown * between them. If the time to read them 20070102b067SLen Brown * varies by a large amount, we re-read them. 20080102b067SLen Brown */ 20090102b067SLen Brown 20100102b067SLen Brown /* 20110102b067SLen Brown * This initial dummy APERF read has been seen to 20120102b067SLen Brown * reduce jitter in the subsequent reads. 20130102b067SLen Brown */ 20140102b067SLen Brown 20159c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2016c98d5d94SLen Brown return -3; 20170102b067SLen Brown 20180102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 20190102b067SLen Brown 20200102b067SLen Brown tsc_before = t->tsc; 20210102b067SLen Brown 20220102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 20230102b067SLen Brown return -3; 20240102b067SLen Brown 20250102b067SLen Brown tsc_between = rdtsc(); 20260102b067SLen Brown 20279c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2028c98d5d94SLen Brown return -4; 20290102b067SLen Brown 20300102b067SLen Brown tsc_after = rdtsc(); 20310102b067SLen Brown 20320102b067SLen Brown aperf_time = tsc_between - tsc_before; 20330102b067SLen Brown mperf_time = tsc_after - tsc_between; 20340102b067SLen Brown 20350102b067SLen Brown /* 20360102b067SLen Brown * If the system call latency to read APERF and MPERF 20370102b067SLen Brown * differ by more than 2x, then try again. 20380102b067SLen Brown */ 20390102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 20400102b067SLen Brown aperf_mperf_retry_count++; 20410102b067SLen Brown if (aperf_mperf_retry_count < 5) 20420102b067SLen Brown goto retry; 20430102b067SLen Brown else 20441b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time); 20450102b067SLen Brown } 20460102b067SLen Brown aperf_mperf_retry_count = 0; 20470102b067SLen Brown 2048b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2049b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 205088c3281fSLen Brown } 205188c3281fSLen Brown 20522af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 20532af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 20542af4f9b8SLen Brown return -4; 20552af4f9b8SLen Brown 2056812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2057562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2058812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 20591ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 20601ed51011SLen Brown return -5; 20611ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 20621ed51011SLen Brown } 20630539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2064144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2065144b44b1SLen Brown return -6; 2066144b44b1SLen Brown } 2067144b44b1SLen Brown 2068388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2069495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2070388e9c81SLen Brown return -10; 2071388e9c81SLen Brown } 2072388e9c81SLen Brown 2073c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2074c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2075f4fdf2b4SLen Brown goto done; 2076c98d5d94SLen Brown 20771e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2078c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2079c98d5d94SLen Brown return -6; 2080144b44b1SLen Brown } 2081144b44b1SLen Brown 20821e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2083c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2084c98d5d94SLen Brown return -7; 20851e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2086fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2087fb5d4327SDasaratharaman Chandramouli return -7; 2088103a8feaSLen Brown } 208915aaa346SLen Brown 20907ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2091c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2092c98d5d94SLen Brown return -8; 20937ab5ff49SZhang Rui else if (t->is_atom) { 20947ab5ff49SZhang Rui /* 20957ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 20967ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 20977ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 20987ab5ff49SZhang Rui * accturate cc6 residency. 20997ab5ff49SZhang Rui */ 21007ab5ff49SZhang Rui c->c6 -= c->c7; 21017ab5ff49SZhang Rui } 21027ab5ff49SZhang Rui } 2103c98d5d94SLen Brown 21040539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 21050539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 21060539ba11SLen Brown return -8; 21070539ba11SLen Brown 2108812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2109889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2110889facbeSLen Brown return -9; 211155279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 2112889facbeSLen Brown } 2113889facbeSLen Brown 21149392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21159392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 21169392bd98SCalvin Walton return -14; 21179392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 21189392bd98SCalvin Walton } 21199392bd98SCalvin Walton 2120388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2121495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2122388e9c81SLen Brown return -10; 2123388e9c81SLen Brown } 2124889facbeSLen Brown 2125c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2126c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2127f4fdf2b4SLen Brown goto done; 212815aaa346SLen Brown 2129a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 21300b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 21310b2bb692SLen Brown return -10; 2132a99d8730SLen Brown } 2133a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 21340b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 21350b2bb692SLen Brown return -11; 2136a99d8730SLen Brown } 2137a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 21380b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 21390b2bb692SLen Brown return -12; 2140a99d8730SLen Brown } 2141a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 21420b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 21430b2bb692SLen Brown return -13; 21440b2bb692SLen Brown } 21450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2146c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2147c98d5d94SLen Brown return -9; 21480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 21490539ba11SLen Brown if (do_slm_cstates) { 21500539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 21510539ba11SLen Brown return -10; 21520539ba11SLen Brown } else { 2153c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2154c98d5d94SLen Brown return -10; 21550539ba11SLen Brown } 21560539ba11SLen Brown } 21570539ba11SLen Brown 21580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2159c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2160c98d5d94SLen Brown return -11; 21610f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2162c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2163c98d5d94SLen Brown return -12; 21640f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2165ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2166ca58710fSKristen Carlson Accardi return -13; 21670f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2168ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2169ca58710fSKristen Carlson Accardi return -13; 21700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2171ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2172ca58710fSKristen Carlson Accardi return -13; 21730f47c08dSLen Brown 2174be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2175be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2176be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2177be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2178be0e54c4SLen Brown 2179889facbeSLen Brown if (do_rapl & RAPL_PKG) { 21809972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2181889facbeSLen Brown return -13; 21829972d5d8SChen Yu p->energy_pkg = msr; 2183889facbeSLen Brown } 21849148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 21859972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2186889facbeSLen Brown return -14; 21879972d5d8SChen Yu p->energy_cores = msr; 2188889facbeSLen Brown } 2189889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 21909972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2191889facbeSLen Brown return -15; 21929972d5d8SChen Yu p->energy_dram = msr; 2193889facbeSLen Brown } 2194889facbeSLen Brown if (do_rapl & RAPL_GFX) { 21959972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2196889facbeSLen Brown return -16; 21979972d5d8SChen Yu p->energy_gfx = msr; 2198889facbeSLen Brown } 2199889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 22009972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2201889facbeSLen Brown return -16; 22029972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2203889facbeSLen Brown } 2204889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 22059972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2206889facbeSLen Brown return -16; 22079972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2208889facbeSLen Brown } 22093316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22109972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 22113316f99aSCalvin Walton return -13; 22129972d5d8SChen Yu p->energy_pkg = msr; 22133316f99aSCalvin Walton } 2214812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2215889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2216889facbeSLen Brown return -17; 221755279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 2218889facbeSLen Brown } 2219fdf676e5SLen Brown 2220812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2221fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2222fdf676e5SLen Brown 2223812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 222427d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 222527d47356SLen Brown 2226b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2227b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2228b4b91569SRafael Antognolli 2229388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2230495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2231388e9c81SLen Brown return -10; 2232388e9c81SLen Brown } 2233f4fdf2b4SLen Brown done: 2234f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2235388e9c81SLen Brown 223615aaa346SLen Brown return 0; 2237103a8feaSLen Brown } 2238103a8feaSLen Brown 2239ee7e38e3SLen Brown /* 2240ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2241ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2242ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2243ee7e38e3SLen Brown */ 2244ee7e38e3SLen Brown 2245ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2246ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2247ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2248ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2249ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2250ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2251ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2252ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2253ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2254ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2255ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2256ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 22570b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 22580b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2259445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2260445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2261ee7e38e3SLen Brown 2262ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2263ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 22641b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 22651b439f01SLen Brown }; 2266ee7e38e3SLen Brown 22671b439f01SLen Brown int nhm_pkg_cstate_limits[16] = 22681b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2269*38c6663aSLen Brown PCLRSV, PCLRSV 2270*38c6663aSLen Brown }; 2271*38c6663aSLen Brown 22721b439f01SLen Brown int snb_pkg_cstate_limits[16] = 22731b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2274*38c6663aSLen Brown PCLRSV, PCLRSV 2275*38c6663aSLen Brown }; 2276*38c6663aSLen Brown 22771b439f01SLen Brown int hsw_pkg_cstate_limits[16] = 22781b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2279*38c6663aSLen Brown PCLRSV, PCLRSV 2280*38c6663aSLen Brown }; 2281*38c6663aSLen Brown 22821b439f01SLen Brown int slv_pkg_cstate_limits[16] = 22831b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2284*38c6663aSLen Brown PCL__6, PCL__7 2285*38c6663aSLen Brown }; 2286*38c6663aSLen Brown 22871b439f01SLen Brown int amt_pkg_cstate_limits[16] = 22881b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2289*38c6663aSLen Brown PCLRSV, PCLRSV 2290*38c6663aSLen Brown }; 2291*38c6663aSLen Brown 22921b439f01SLen Brown int phi_pkg_cstate_limits[16] = 22931b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2294*38c6663aSLen Brown PCLRSV, PCLRSV 2295*38c6663aSLen Brown }; 2296*38c6663aSLen Brown 22971b439f01SLen Brown int glm_pkg_cstate_limits[16] = 22981b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2299*38c6663aSLen Brown PCLRSV, PCLRSV 2300*38c6663aSLen Brown }; 2301*38c6663aSLen Brown 23021b439f01SLen Brown int skx_pkg_cstate_limits[16] = 23031b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2304*38c6663aSLen Brown PCLRSV, PCLRSV 2305*38c6663aSLen Brown }; 2306*38c6663aSLen Brown 23071b439f01SLen Brown int icx_pkg_cstate_limits[16] = 23081b439f01SLen Brown { PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2309*38c6663aSLen Brown PCLRSV, PCLRSV 2310*38c6663aSLen Brown }; 2311a2b7b749SLen Brown 23121b439f01SLen Brown static void calculate_tsc_tweak() 2313a2b7b749SLen Brown { 2314a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2315a2b7b749SLen Brown } 2316a2b7b749SLen Brown 2317aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2318aeb01e6dSChen Yu 23191b439f01SLen Brown static void dump_nhm_platform_info(void) 2320103a8feaSLen Brown { 2321103a8feaSLen Brown unsigned long long msr; 2322103a8feaSLen Brown unsigned int ratio; 2323103a8feaSLen Brown 2324ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2325103a8feaSLen Brown 2326b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 23276574a5d5SLen Brown 2328103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 23291b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 2330103a8feaSLen Brown 2331103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 23321b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 2333103a8feaSLen Brown 23347ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2335b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2336bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 233767920418SLen Brown 2338aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2339aeb01e6dSChen Yu if (dis_cstate_prewake) 23401b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 2341aeb01e6dSChen Yu 2342fcd17211SLen Brown return; 2343fcd17211SLen Brown } 2344fcd17211SLen Brown 23451b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void) 2346fcd17211SLen Brown { 2347fcd17211SLen Brown unsigned long long msr; 2348fcd17211SLen Brown unsigned int ratio; 2349fcd17211SLen Brown 23507ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2351fcd17211SLen Brown 2352b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2353fcd17211SLen Brown 2354fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2355fcd17211SLen Brown if (ratio) 23561b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 2357fcd17211SLen Brown 2358fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2359fcd17211SLen Brown if (ratio) 23601b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 2361fcd17211SLen Brown return; 2362fcd17211SLen Brown } 2363fcd17211SLen Brown 23641b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void) 2365fcd17211SLen Brown { 2366fcd17211SLen Brown unsigned long long msr; 2367fcd17211SLen Brown unsigned int ratio; 23686574a5d5SLen Brown 23697ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 23706574a5d5SLen Brown 2371b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 23726574a5d5SLen Brown 23736574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 23746574a5d5SLen Brown if (ratio) 23751b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 23766574a5d5SLen Brown 23776574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 23786574a5d5SLen Brown if (ratio) 23791b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 23806574a5d5SLen Brown 23816574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 23826574a5d5SLen Brown if (ratio) 23831b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 23846574a5d5SLen Brown 23856574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 23866574a5d5SLen Brown if (ratio) 23871b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 23886574a5d5SLen Brown 23896574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 23906574a5d5SLen Brown if (ratio) 23911b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 23926574a5d5SLen Brown 23936574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 23946574a5d5SLen Brown if (ratio) 23951b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 23966574a5d5SLen Brown 23976574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 23986574a5d5SLen Brown if (ratio) 23991b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 24006574a5d5SLen Brown 24016574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 24026574a5d5SLen Brown if (ratio) 24031b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 2404103a8feaSLen Brown return; 2405fcd17211SLen Brown } 24061b439f01SLen Brown 240731e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 240831e07522SLen Brown { 240931e07522SLen Brown 241031e07522SLen Brown if (!genuine_intel) 241131e07522SLen Brown return 0; 241231e07522SLen Brown 241331e07522SLen Brown switch (model) { 241431e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 241531e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 241625368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 24175ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 241820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 241931e07522SLen Brown return 1; 242031e07522SLen Brown } 242131e07522SLen Brown return 0; 242231e07522SLen Brown } 2423fcd17211SLen Brown 24241b439f01SLen Brown static void dump_turbo_ratio_limits(int family, int model) 2425fcd17211SLen Brown { 242631e07522SLen Brown unsigned long long msr, core_counts; 242731e07522SLen Brown unsigned int ratio, group_size; 2428103a8feaSLen Brown 24297ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2430b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 24316574a5d5SLen Brown 243231e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 243331e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 243431e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 243531e07522SLen Brown } else { 243631e07522SLen Brown core_counts = 0x0807060504030201; 243731e07522SLen Brown } 243831e07522SLen Brown 24396574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 244031e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 24416574a5d5SLen Brown if (ratio) 244231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 244331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24446574a5d5SLen Brown 24456574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 244631e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 24476574a5d5SLen Brown if (ratio) 244831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 244931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24506574a5d5SLen Brown 24516574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 245231e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 24536574a5d5SLen Brown if (ratio) 245431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 245531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24566574a5d5SLen Brown 24576574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 245831e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 24596574a5d5SLen Brown if (ratio) 246031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 246131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24626574a5d5SLen Brown 2463103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 246431e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2465103a8feaSLen Brown if (ratio) 246631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 246731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2468103a8feaSLen Brown 2469103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 247031e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2471103a8feaSLen Brown if (ratio) 247231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 247331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2474103a8feaSLen Brown 2475103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 247631e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2477103a8feaSLen Brown if (ratio) 247831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 247931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2480103a8feaSLen Brown 2481103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 248231e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2483103a8feaSLen Brown if (ratio) 248431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 248531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2486fcd17211SLen Brown return; 2487fcd17211SLen Brown } 24883a9a941dSLen Brown 24891b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void) 24900f7887c4SLen Brown { 24910f7887c4SLen Brown unsigned long long msr; 24920f7887c4SLen Brown unsigned int ratio; 24930f7887c4SLen Brown 24940f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 24950f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 24960f7887c4SLen Brown 24970f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 24980f7887c4SLen Brown if (ratio) 24991b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 25000f7887c4SLen Brown 25010f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25020f7887c4SLen Brown if (ratio) 25031b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 25040f7887c4SLen Brown 25050f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25060f7887c4SLen Brown if (ratio) 25071b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 25080f7887c4SLen Brown 25090f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 25100f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25110f7887c4SLen Brown 25120f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 25130f7887c4SLen Brown if (ratio) 25141b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 25150f7887c4SLen Brown 25160f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25170f7887c4SLen Brown if (ratio) 25181b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 25190f7887c4SLen Brown 25200f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25210f7887c4SLen Brown if (ratio) 25221b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 25230f7887c4SLen Brown 25240f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25250f7887c4SLen Brown if (ratio) 25261b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 25270f7887c4SLen Brown } 25280f7887c4SLen Brown 25291b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void) 2530fb5d4327SDasaratharaman Chandramouli { 2531cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2532cbf97abaSHubert Chrzaniuk 2533fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2534cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2535cbf97abaSHubert Chrzaniuk int i, b_nr; 2536cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2537cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2538fb5d4327SDasaratharaman Chandramouli 2539ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2540fb5d4327SDasaratharaman Chandramouli 25411b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 2542fb5d4327SDasaratharaman Chandramouli 25431e3ec5cdSRandy Dunlap /* 2544fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2545cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2546cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2547fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2548fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2549fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2550fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2551fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2552fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2553fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2554fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2555fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2556fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2557fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2558fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2559fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2560fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2561fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2562fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2563fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2564fb5d4327SDasaratharaman Chandramouli */ 2565fb5d4327SDasaratharaman Chandramouli 2566cbf97abaSHubert Chrzaniuk b_nr = 0; 2567cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2568cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2569cbf97abaSHubert Chrzaniuk 2570cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2571fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2572cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2573fb5d4327SDasaratharaman Chandramouli 2574cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2575cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2576cbf97abaSHubert Chrzaniuk b_nr++; 2577fb5d4327SDasaratharaman Chandramouli } 2578cbf97abaSHubert Chrzaniuk 2579cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2580cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2581b7d8c148SLen Brown fprintf(outf, 2582710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2583cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2584fb5d4327SDasaratharaman Chandramouli } 2585fb5d4327SDasaratharaman Chandramouli 25861b439f01SLen Brown static void dump_nhm_cst_cfg(void) 2587fcd17211SLen Brown { 2588fcd17211SLen Brown unsigned long long msr; 2589fcd17211SLen Brown 25901df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2591fcd17211SLen Brown 25921df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2593fcd17211SLen Brown 25943e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2595fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2596fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2597fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2598fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 25991b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 2600ac980e13SArtem Bityutskiy 2601ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2602ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 26031b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2604ac980e13SArtem Bityutskiy } 2605ac980e13SArtem Bityutskiy 2606ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2607ac980e13SArtem Bityutskiy 2608fcd17211SLen Brown return; 2609103a8feaSLen Brown } 2610103a8feaSLen Brown 26111b439f01SLen Brown static void dump_config_tdp(void) 26126fb3143bSLen Brown { 26136fb3143bSLen Brown unsigned long long msr; 26146fb3143bSLen Brown 26156fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2616b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2617685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26186fb3143bSLen Brown 26196fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2620b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 26216fb3143bSLen Brown if (msr) { 2622685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2623685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2624685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2625685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 26266fb3143bSLen Brown } 2627b7d8c148SLen Brown fprintf(outf, ")\n"); 26286fb3143bSLen Brown 26296fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2630b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 26316fb3143bSLen Brown if (msr) { 2632685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2633685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2634685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2635685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 26366fb3143bSLen Brown } 2637b7d8c148SLen Brown fprintf(outf, ")\n"); 26386fb3143bSLen Brown 26396fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2640b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 26416fb3143bSLen Brown if ((msr) & 0x3) 2642b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2643b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2644b7d8c148SLen Brown fprintf(outf, ")\n"); 26456fb3143bSLen Brown 26466fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2647b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2648685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2649b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2650b7d8c148SLen Brown fprintf(outf, ")\n"); 26516fb3143bSLen Brown } 26525a63426eSLen Brown 26535a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 26545a63426eSLen Brown 26555a63426eSLen Brown void print_irtl(void) 26565a63426eSLen Brown { 26575a63426eSLen Brown unsigned long long msr; 26585a63426eSLen Brown 26595a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 26605a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 26615a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26625a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26635a63426eSLen Brown 26645a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 26655a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 26665a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26675a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26685a63426eSLen Brown 26695a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 26705a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 26715a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26725a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26735a63426eSLen Brown 26745a63426eSLen Brown if (!do_irtl_hsw) 26755a63426eSLen Brown return; 26765a63426eSLen Brown 26775a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 26785a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 26795a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26805a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26815a63426eSLen Brown 26825a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 26835a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 26845a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26855a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26865a63426eSLen Brown 26875a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 26885a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 26895a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26905a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26915a63426eSLen Brown 26925a63426eSLen Brown } 26931b439f01SLen Brown 269436229897SLen Brown void free_fd_percpu(void) 269536229897SLen Brown { 269636229897SLen Brown int i; 269736229897SLen Brown 269801a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 269936229897SLen Brown if (fd_percpu[i] != 0) 270036229897SLen Brown close(fd_percpu[i]); 270136229897SLen Brown } 270236229897SLen Brown 270336229897SLen Brown free(fd_percpu); 27046fb3143bSLen Brown } 27056fb3143bSLen Brown 2706c98d5d94SLen Brown void free_all_buffers(void) 2707103a8feaSLen Brown { 27080e2d8f05SLen Brown int i; 27090e2d8f05SLen Brown 2710c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2711c98d5d94SLen Brown cpu_present_set = NULL; 271236229897SLen Brown cpu_present_setsize = 0; 2713103a8feaSLen Brown 2714c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2715c98d5d94SLen Brown cpu_affinity_set = NULL; 2716c98d5d94SLen Brown cpu_affinity_setsize = 0; 2717103a8feaSLen Brown 2718c98d5d94SLen Brown free(thread_even); 2719c98d5d94SLen Brown free(core_even); 2720c98d5d94SLen Brown free(package_even); 2721103a8feaSLen Brown 2722c98d5d94SLen Brown thread_even = NULL; 2723c98d5d94SLen Brown core_even = NULL; 2724c98d5d94SLen Brown package_even = NULL; 2725103a8feaSLen Brown 2726c98d5d94SLen Brown free(thread_odd); 2727c98d5d94SLen Brown free(core_odd); 2728c98d5d94SLen Brown free(package_odd); 2729103a8feaSLen Brown 2730c98d5d94SLen Brown thread_odd = NULL; 2731c98d5d94SLen Brown core_odd = NULL; 2732c98d5d94SLen Brown package_odd = NULL; 2733103a8feaSLen Brown 2734c98d5d94SLen Brown free(output_buffer); 2735c98d5d94SLen Brown output_buffer = NULL; 2736c98d5d94SLen Brown outp = NULL; 273736229897SLen Brown 273836229897SLen Brown free_fd_percpu(); 2739562a2d37SLen Brown 2740562a2d37SLen Brown free(irq_column_2_cpu); 2741562a2d37SLen Brown free(irqs_per_cpu); 27420e2d8f05SLen Brown 27430e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 27440e2d8f05SLen Brown if (cpus[i].put_ids) 27450e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 27460e2d8f05SLen Brown } 27470e2d8f05SLen Brown free(cpus); 2748103a8feaSLen Brown } 2749103a8feaSLen Brown 275057a42a34SJosh Triplett /* 275195aebc44SJosh Triplett * Parse a file containing a single int. 27526de68fe1SLen Brown * Return 0 if file can not be opened 27536de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 275495aebc44SJosh Triplett */ 275595aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 275695aebc44SJosh Triplett { 275795aebc44SJosh Triplett va_list args; 275895aebc44SJosh Triplett char path[PATH_MAX]; 275995aebc44SJosh Triplett FILE *filep; 276095aebc44SJosh Triplett int value; 276195aebc44SJosh Triplett 276295aebc44SJosh Triplett va_start(args, fmt); 276395aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 276495aebc44SJosh Triplett va_end(args); 27656de68fe1SLen Brown filep = fopen(path, "r"); 27666de68fe1SLen Brown if (!filep) 27676de68fe1SLen Brown return 0; 2768b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2769b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 277095aebc44SJosh Triplett fclose(filep); 277195aebc44SJosh Triplett return value; 277295aebc44SJosh Triplett } 277395aebc44SJosh Triplett 277495aebc44SJosh Triplett /* 2775c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2776c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2777c98d5d94SLen Brown */ 2778c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2779c98d5d94SLen Brown { 278095aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2781103a8feaSLen Brown } 2782103a8feaSLen Brown 2783103a8feaSLen Brown int get_physical_package_id(int cpu) 2784103a8feaSLen Brown { 278595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2786103a8feaSLen Brown } 2787103a8feaSLen Brown 27886de68fe1SLen Brown int get_die_id(int cpu) 27896de68fe1SLen Brown { 27906de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 27916de68fe1SLen Brown } 27926de68fe1SLen Brown 2793103a8feaSLen Brown int get_core_id(int cpu) 2794103a8feaSLen Brown { 279595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2796103a8feaSLen Brown } 2797103a8feaSLen Brown 2798ef605741SPrarit Bhargava void set_node_data(void) 2799ef605741SPrarit Bhargava { 28002ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 28012ffbb224SPrarit Bhargava int cpu_count; 2802ef605741SPrarit Bhargava 28032ffbb224SPrarit Bhargava /* initialize logical_node_id */ 28042ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 28052ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2806ef605741SPrarit Bhargava 28072ffbb224SPrarit Bhargava cpu_count = 0; 28082ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 28092ffbb224SPrarit Bhargava lnode = 0; 28102ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 28112ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2812ef605741SPrarit Bhargava continue; 28132ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 28142ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 28152ffbb224SPrarit Bhargava continue; 28162ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2817ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28182ffbb224SPrarit Bhargava cpu_count++; 28192ffbb224SPrarit Bhargava /* 28202ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 28212ffbb224SPrarit Bhargava * the logical_node_id 28222ffbb224SPrarit Bhargava */ 28232ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 28241b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 28252ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 28262ffbb224SPrarit Bhargava cpu_count++; 2827ef605741SPrarit Bhargava } 28282ffbb224SPrarit Bhargava } 28292ffbb224SPrarit Bhargava lnode++; 28302ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 28312ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 28322ffbb224SPrarit Bhargava } 28332ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 28342ffbb224SPrarit Bhargava break; 28352ffbb224SPrarit Bhargava } 2836ef605741SPrarit Bhargava } 2837ef605741SPrarit Bhargava 2838ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2839c98d5d94SLen Brown { 2840c98d5d94SLen Brown char path[80]; 2841c98d5d94SLen Brown FILE *filep; 28420e2d8f05SLen Brown int i; 28430e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2844c98d5d94SLen Brown 28450e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 28461b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 28470e2d8f05SLen Brown filep = fopen(path, "r"); 28480e2d8f05SLen Brown if (!filep) 28490e2d8f05SLen Brown continue; 28500e2d8f05SLen Brown fclose(filep); 28510e2d8f05SLen Brown return i; 28520e2d8f05SLen Brown } 28530e2d8f05SLen Brown return -1; 2854e275b388SDasaratharaman Chandramouli } 2855c98d5d94SLen Brown 28560e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 28570e2d8f05SLen Brown { 28580e2d8f05SLen Brown char path[80], character; 28590e2d8f05SLen Brown FILE *filep; 28600e2d8f05SLen Brown unsigned long map; 28618cb48b32SPrarit Bhargava int so, shift, sib_core; 28620e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 28630e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 28640e2d8f05SLen Brown size_t size; 28658cb48b32SPrarit Bhargava int thread_id = 0; 28660e2d8f05SLen Brown 28670e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 28688cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 28698cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 28700e2d8f05SLen Brown if (!thiscpu->put_ids) 28710e2d8f05SLen Brown return -1; 28720e2d8f05SLen Brown 28730e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 28740e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 28750e2d8f05SLen Brown 28761b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 28773d7772eaSLen Brown filep = fopen(path, "r"); 28783d7772eaSLen Brown 28793d7772eaSLen Brown if (!filep) { 28803d7772eaSLen Brown warnx("%s: open failed", path); 28813d7772eaSLen Brown return -1; 28823d7772eaSLen Brown } 28830e2d8f05SLen Brown do { 28840e2d8f05SLen Brown offset -= BITMASK_SIZE; 28858173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 28868173c336SBen Hutchings err(1, "%s: failed to parse file", path); 28870e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 28880e2d8f05SLen Brown if ((map >> shift) & 0x1) { 28898cb48b32SPrarit Bhargava so = shift + offset; 28908cb48b32SPrarit Bhargava sib_core = get_core_id(so); 28918cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 28928cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 28931b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0)) 28941b439f01SLen Brown cpus[so].thread_id = thread_id++; 28958cb48b32SPrarit Bhargava } 28960e2d8f05SLen Brown } 28970e2d8f05SLen Brown } 28980e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2899c98d5d94SLen Brown fclose(filep); 29000e2d8f05SLen Brown 29010e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2902c98d5d94SLen Brown } 2903c98d5d94SLen Brown 2904c98d5d94SLen Brown /* 2905c98d5d94SLen Brown * run func(thread, core, package) in topology order 2906c98d5d94SLen Brown * skip non-present cpus 2907103a8feaSLen Brown */ 2908103a8feaSLen Brown 2909c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 2910c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2911c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2912c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 29131b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 2914c98d5d94SLen Brown { 291540f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2916c98d5d94SLen Brown 2917c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 291840f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 29191b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 29201b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 2921c98d5d94SLen Brown struct thread_data *t, *t2; 2922c98d5d94SLen Brown struct core_data *c, *c2; 2923c98d5d94SLen Brown struct pkg_data *p, *p2; 2924c98d5d94SLen Brown 29251b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 2926c98d5d94SLen Brown 2927c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2928c98d5d94SLen Brown continue; 2929c98d5d94SLen Brown 29301b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no); 2931c98d5d94SLen Brown 29321b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 29331b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no); 2934c98d5d94SLen Brown 2935c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2936c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2937c98d5d94SLen Brown 2938c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2939c98d5d94SLen Brown if (retval) 2940c98d5d94SLen Brown return retval; 2941c98d5d94SLen Brown } 2942c98d5d94SLen Brown } 2943c98d5d94SLen Brown } 294440f5cfe7SPrarit Bhargava } 2945c98d5d94SLen Brown return 0; 2946c98d5d94SLen Brown } 2947c98d5d94SLen Brown 2948c98d5d94SLen Brown /* 2949c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2950c98d5d94SLen Brown * return max_cpu number 2951c98d5d94SLen Brown */ 2952c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int)) 2953103a8feaSLen Brown { 2954103a8feaSLen Brown FILE *fp; 2955c98d5d94SLen Brown int cpu_num; 2956103a8feaSLen Brown int retval; 2957103a8feaSLen Brown 295857a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2959103a8feaSLen Brown 2960103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2961b2c95d90SJosh Triplett if (retval != 0) 2962b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2963103a8feaSLen Brown 2964c98d5d94SLen Brown while (1) { 2965c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2966103a8feaSLen Brown if (retval != 1) 2967103a8feaSLen Brown break; 2968103a8feaSLen Brown 2969c98d5d94SLen Brown retval = func(cpu_num); 2970c98d5d94SLen Brown if (retval) { 2971c98d5d94SLen Brown fclose(fp); 2972c98d5d94SLen Brown return (retval); 2973c98d5d94SLen Brown } 2974103a8feaSLen Brown } 2975103a8feaSLen Brown fclose(fp); 2976c98d5d94SLen Brown return 0; 2977103a8feaSLen Brown } 2978103a8feaSLen Brown 2979103a8feaSLen Brown void re_initialize(void) 2980103a8feaSLen Brown { 2981c98d5d94SLen Brown free_all_buffers(); 2982c98d5d94SLen Brown setup_all_buffers(); 29833d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2984103a8feaSLen Brown } 2985103a8feaSLen Brown 2986843c5791SPrarit Bhargava void set_max_cpu_num(void) 2987843c5791SPrarit Bhargava { 2988843c5791SPrarit Bhargava FILE *filep; 29898201a028SPrarit Bhargava int base_cpu; 2990843c5791SPrarit Bhargava unsigned long dummy; 29918201a028SPrarit Bhargava char pathname[64]; 2992843c5791SPrarit Bhargava 29938201a028SPrarit Bhargava base_cpu = sched_getcpu(); 29948201a028SPrarit Bhargava if (base_cpu < 0) 29958201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 29961b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu); 29978201a028SPrarit Bhargava 29988201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 2999843c5791SPrarit Bhargava topo.max_cpu_num = 0; 3000843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 30010e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 3002843c5791SPrarit Bhargava fclose(filep); 3003843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 3004843c5791SPrarit Bhargava } 3005c98d5d94SLen Brown 3006103a8feaSLen Brown /* 3007c98d5d94SLen Brown * count_cpus() 3008c98d5d94SLen Brown * remember the last one seen, it will be the max 3009103a8feaSLen Brown */ 3010c98d5d94SLen Brown int count_cpus(int cpu) 3011103a8feaSLen Brown { 3012843c5791SPrarit Bhargava topo.num_cpus++; 3013c98d5d94SLen Brown return 0; 3014103a8feaSLen Brown } 30151b439f01SLen Brown 3016c98d5d94SLen Brown int mark_cpu_present(int cpu) 3017c98d5d94SLen Brown { 3018c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 301915aaa346SLen Brown return 0; 3020103a8feaSLen Brown } 3021103a8feaSLen Brown 30228cb48b32SPrarit Bhargava int init_thread_id(int cpu) 30238cb48b32SPrarit Bhargava { 30248cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 30258cb48b32SPrarit Bhargava return 0; 30268cb48b32SPrarit Bhargava } 30278cb48b32SPrarit Bhargava 3028562a2d37SLen Brown /* 3029562a2d37SLen Brown * snapshot_proc_interrupts() 3030562a2d37SLen Brown * 3031562a2d37SLen Brown * read and record summary of /proc/interrupts 3032562a2d37SLen Brown * 3033562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3034562a2d37SLen Brown */ 3035562a2d37SLen Brown int snapshot_proc_interrupts(void) 3036562a2d37SLen Brown { 3037562a2d37SLen Brown static FILE *fp; 3038562a2d37SLen Brown int column, retval; 3039562a2d37SLen Brown 3040562a2d37SLen Brown if (fp == NULL) 3041562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3042562a2d37SLen Brown else 3043562a2d37SLen Brown rewind(fp); 3044562a2d37SLen Brown 3045562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3046562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3047562a2d37SLen Brown int cpu_number; 3048562a2d37SLen Brown 3049562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3050562a2d37SLen Brown if (retval != 1) 3051562a2d37SLen Brown break; 3052562a2d37SLen Brown 3053562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3054562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3055562a2d37SLen Brown return 1; 3056562a2d37SLen Brown } 3057562a2d37SLen Brown 3058562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3059562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3060562a2d37SLen Brown } 3061562a2d37SLen Brown 3062562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3063562a2d37SLen Brown while (1) { 3064562a2d37SLen Brown int column; 3065562a2d37SLen Brown char buf[64]; 3066562a2d37SLen Brown 3067562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3068562a2d37SLen Brown if (retval != 1) 3069562a2d37SLen Brown break; 3070562a2d37SLen Brown 3071562a2d37SLen Brown /* read the count per cpu */ 3072562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3073562a2d37SLen Brown 3074562a2d37SLen Brown int cpu_number, irq_count; 3075562a2d37SLen Brown 3076562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3077562a2d37SLen Brown if (retval != 1) 3078562a2d37SLen Brown break; 3079562a2d37SLen Brown 3080562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3081562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3082562a2d37SLen Brown 3083562a2d37SLen Brown } 3084562a2d37SLen Brown 30851b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */ 3086562a2d37SLen Brown 3087562a2d37SLen Brown } 3088562a2d37SLen Brown return 0; 3089562a2d37SLen Brown } 30901b439f01SLen Brown 309127d47356SLen Brown /* 3092fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3093fdf676e5SLen Brown * 3094fdf676e5SLen Brown * record snapshot of 3095fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3096fdf676e5SLen Brown * 3097fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3098fdf676e5SLen Brown */ 3099fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3100fdf676e5SLen Brown { 3101fdf676e5SLen Brown FILE *fp; 3102fdf676e5SLen Brown int retval; 3103fdf676e5SLen Brown 3104fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3105fdf676e5SLen Brown 3106fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3107fdf676e5SLen Brown if (retval != 1) 3108fdf676e5SLen Brown err(1, "GFX rc6"); 3109fdf676e5SLen Brown 3110fdf676e5SLen Brown fclose(fp); 3111fdf676e5SLen Brown 3112fdf676e5SLen Brown return 0; 3113fdf676e5SLen Brown } 31141b439f01SLen Brown 3115fdf676e5SLen Brown /* 311627d47356SLen Brown * snapshot_gfx_mhz() 311727d47356SLen Brown * 311827d47356SLen Brown * record snapshot of 311927d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 312027d47356SLen Brown * 312127d47356SLen Brown * return 1 if config change requires a restart, else return 0 312227d47356SLen Brown */ 312327d47356SLen Brown int snapshot_gfx_mhz(void) 312427d47356SLen Brown { 312527d47356SLen Brown static FILE *fp; 312627d47356SLen Brown int retval; 312727d47356SLen Brown 312827d47356SLen Brown if (fp == NULL) 312927d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 313022048c54SLen Brown else { 313127d47356SLen Brown rewind(fp); 313222048c54SLen Brown fflush(fp); 313322048c54SLen Brown } 313427d47356SLen Brown 313527d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 313627d47356SLen Brown if (retval != 1) 313727d47356SLen Brown err(1, "GFX MHz"); 313827d47356SLen Brown 313927d47356SLen Brown return 0; 314027d47356SLen Brown } 3141562a2d37SLen Brown 3142562a2d37SLen Brown /* 3143b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3144b4b91569SRafael Antognolli * 3145b4b91569SRafael Antognolli * record snapshot of 3146b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3147b4b91569SRafael Antognolli * 3148b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3149b4b91569SRafael Antognolli */ 3150b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3151b4b91569SRafael Antognolli { 3152b4b91569SRafael Antognolli static FILE *fp; 3153b4b91569SRafael Antognolli int retval; 3154b4b91569SRafael Antognolli 3155b4b91569SRafael Antognolli if (fp == NULL) 3156b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3157b4b91569SRafael Antognolli else { 3158b4b91569SRafael Antognolli rewind(fp); 3159b4b91569SRafael Antognolli fflush(fp); 3160b4b91569SRafael Antognolli } 3161b4b91569SRafael Antognolli 3162b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3163b4b91569SRafael Antognolli if (retval != 1) 3164b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3165b4b91569SRafael Antognolli 3166b4b91569SRafael Antognolli return 0; 3167b4b91569SRafael Antognolli } 3168b4b91569SRafael Antognolli 3169b4b91569SRafael Antognolli /* 3170be0e54c4SLen Brown * snapshot_cpu_lpi() 3171be0e54c4SLen Brown * 3172be0e54c4SLen Brown * record snapshot of 3173be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3174be0e54c4SLen Brown */ 3175be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3176be0e54c4SLen Brown { 3177be0e54c4SLen Brown FILE *fp; 3178be0e54c4SLen Brown int retval; 3179be0e54c4SLen Brown 3180be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3181be0e54c4SLen Brown 3182be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 31835ea7647bSPrarit Bhargava if (retval != 1) { 31845ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 31855ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3186605736c6SGustavo A. R. Silva fclose(fp); 31875ea7647bSPrarit Bhargava return -1; 31885ea7647bSPrarit Bhargava } 3189be0e54c4SLen Brown 3190be0e54c4SLen Brown fclose(fp); 3191be0e54c4SLen Brown 3192be0e54c4SLen Brown return 0; 3193be0e54c4SLen Brown } 31941b439f01SLen Brown 3195be0e54c4SLen Brown /* 3196be0e54c4SLen Brown * snapshot_sys_lpi() 3197be0e54c4SLen Brown * 31981f81c5efSLen Brown * record snapshot of sys_lpi_file 3199be0e54c4SLen Brown */ 3200be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3201be0e54c4SLen Brown { 3202be0e54c4SLen Brown FILE *fp; 3203be0e54c4SLen Brown int retval; 3204be0e54c4SLen Brown 32051f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3206be0e54c4SLen Brown 3207be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 32085ea7647bSPrarit Bhargava if (retval != 1) { 32095ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 32105ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 321115423b95SColin Ian King fclose(fp); 32125ea7647bSPrarit Bhargava return -1; 32135ea7647bSPrarit Bhargava } 3214be0e54c4SLen Brown fclose(fp); 3215be0e54c4SLen Brown 3216be0e54c4SLen Brown return 0; 3217be0e54c4SLen Brown } 32181b439f01SLen Brown 3219be0e54c4SLen Brown /* 3220562a2d37SLen Brown * snapshot /proc and /sys files 3221562a2d37SLen Brown * 3222562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3223562a2d37SLen Brown */ 3224562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3225562a2d37SLen Brown { 3226218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3227562a2d37SLen Brown if (snapshot_proc_interrupts()) 3228562a2d37SLen Brown return 1; 3229562a2d37SLen Brown 3230812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3231fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3232fdf676e5SLen Brown 3233812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 323427d47356SLen Brown snapshot_gfx_mhz(); 323527d47356SLen Brown 3236b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3237b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3238b4b91569SRafael Antognolli 3239be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3240be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3241be0e54c4SLen Brown 3242be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3243be0e54c4SLen Brown snapshot_sys_lpi_us(); 3244be0e54c4SLen Brown 3245562a2d37SLen Brown return 0; 3246562a2d37SLen Brown } 3247562a2d37SLen Brown 32488aa2ed0bSLen Brown int exit_requested; 32498aa2ed0bSLen Brown 32508aa2ed0bSLen Brown static void signal_handler(int signal) 32518aa2ed0bSLen Brown { 32528aa2ed0bSLen Brown switch (signal) { 32538aa2ed0bSLen Brown case SIGINT: 32548aa2ed0bSLen Brown exit_requested = 1; 32558aa2ed0bSLen Brown if (debug) 32568aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 32578aa2ed0bSLen Brown break; 325807211960SLen Brown case SIGUSR1: 325907211960SLen Brown if (debug > 1) 326007211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 326107211960SLen Brown break; 32628aa2ed0bSLen Brown } 32638aa2ed0bSLen Brown } 32648aa2ed0bSLen Brown 32658aa2ed0bSLen Brown void setup_signal_handler(void) 32668aa2ed0bSLen Brown { 32678aa2ed0bSLen Brown struct sigaction sa; 32688aa2ed0bSLen Brown 32698aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 32708aa2ed0bSLen Brown 32718aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 32728aa2ed0bSLen Brown 32738aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 32748aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 327507211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 327607211960SLen Brown err(1, "sigaction SIGUSR1"); 32778aa2ed0bSLen Brown } 3278b9ad8ee0SLen Brown 327947936f94SArtem Bityutskiy void do_sleep(void) 3280b9ad8ee0SLen Brown { 3281c026c236SArtem Bityutskiy struct timeval tout; 3282c026c236SArtem Bityutskiy struct timespec rest; 3283b9ad8ee0SLen Brown fd_set readfds; 3284b9ad8ee0SLen Brown int retval; 3285b9ad8ee0SLen Brown 3286b9ad8ee0SLen Brown FD_ZERO(&readfds); 3287b9ad8ee0SLen Brown FD_SET(0, &readfds); 3288b9ad8ee0SLen Brown 3289c026c236SArtem Bityutskiy if (ignore_stdin) { 329047936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 329147936f94SArtem Bityutskiy return; 329247936f94SArtem Bityutskiy } 3293b9ad8ee0SLen Brown 3294c026c236SArtem Bityutskiy tout = interval_tv; 3295c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3296b9ad8ee0SLen Brown 3297b9ad8ee0SLen Brown if (retval == 1) { 3298b9ad8ee0SLen Brown switch (getc(stdin)) { 3299b9ad8ee0SLen Brown case 'q': 3300b9ad8ee0SLen Brown exit_requested = 1; 3301b9ad8ee0SLen Brown break; 3302c026c236SArtem Bityutskiy case EOF: 3303c026c236SArtem Bityutskiy /* 3304c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3305c026c236SArtem Bityutskiy * won't be any further input. 3306c026c236SArtem Bityutskiy */ 3307c026c236SArtem Bityutskiy ignore_stdin = 1; 3308c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3309c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3310c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3311c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3312b9ad8ee0SLen Brown } 3313b9ad8ee0SLen Brown } 3314b9ad8ee0SLen Brown } 331547936f94SArtem Bityutskiy 331687e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 331787e15da9SChen Yu { 331887e15da9SChen Yu int ret, idx; 331987e15da9SChen Yu unsigned long long msr_cur, msr_last; 332087e15da9SChen Yu 332187e15da9SChen Yu if (!per_cpu_msr_sum) 332287e15da9SChen Yu return 1; 332387e15da9SChen Yu 332487e15da9SChen Yu idx = offset_to_idx(offset); 332587e15da9SChen Yu if (idx < 0) 332687e15da9SChen Yu return idx; 332787e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 332887e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 332987e15da9SChen Yu if (ret) 333087e15da9SChen Yu return ret; 333187e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 333287e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 333387e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 333487e15da9SChen Yu 333587e15da9SChen Yu return 0; 333687e15da9SChen Yu } 333787e15da9SChen Yu 333887e15da9SChen Yu timer_t timerid; 333987e15da9SChen Yu 334087e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 334187e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 334287e15da9SChen Yu { 334387e15da9SChen Yu int i, ret; 334487e15da9SChen Yu int cpu = t->cpu_id; 334587e15da9SChen Yu 334687e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 334787e15da9SChen Yu unsigned long long msr_cur, msr_last; 334813a779deSCalvin Walton off_t offset; 334987e15da9SChen Yu 335087e15da9SChen Yu if (!idx_valid(i)) 335187e15da9SChen Yu continue; 335287e15da9SChen Yu offset = idx_to_offset(i); 335387e15da9SChen Yu if (offset < 0) 335487e15da9SChen Yu continue; 335587e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 335687e15da9SChen Yu if (ret) { 33571b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset); 335887e15da9SChen Yu continue; 335987e15da9SChen Yu } 336087e15da9SChen Yu 336187e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 336287e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 336387e15da9SChen Yu 336487e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 336587e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 336687e15da9SChen Yu } 336787e15da9SChen Yu return 0; 336887e15da9SChen Yu } 336987e15da9SChen Yu 33701b439f01SLen Brown static void msr_record_handler(union sigval v) 337187e15da9SChen Yu { 337287e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 337387e15da9SChen Yu } 337487e15da9SChen Yu 337587e15da9SChen Yu void msr_sum_record(void) 337687e15da9SChen Yu { 337787e15da9SChen Yu struct itimerspec its; 337887e15da9SChen Yu struct sigevent sev; 337987e15da9SChen Yu 338087e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 338187e15da9SChen Yu if (!per_cpu_msr_sum) { 338287e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 338387e15da9SChen Yu return; 338487e15da9SChen Yu } 338587e15da9SChen Yu /* 338687e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 338787e15da9SChen Yu */ 338887e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 338987e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 339087e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 339187e15da9SChen Yu 339287e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 339387e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 339487e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 339587e15da9SChen Yu goto release_msr; 339687e15da9SChen Yu } 339787e15da9SChen Yu 339887e15da9SChen Yu its.it_value.tv_sec = 0; 339987e15da9SChen Yu its.it_value.tv_nsec = 1; 340087e15da9SChen Yu /* 340187e15da9SChen Yu * A wraparound time has been calculated early. 340287e15da9SChen Yu * Some sources state that the peak power for a 340387e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 340487e15da9SChen Yu * use 2 * TDP for safety. 340587e15da9SChen Yu */ 340687e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 340787e15da9SChen Yu its.it_interval.tv_nsec = 0; 340887e15da9SChen Yu 340987e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 341087e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 341187e15da9SChen Yu goto release_timer; 341287e15da9SChen Yu } 341387e15da9SChen Yu return; 341487e15da9SChen Yu 341587e15da9SChen Yu release_timer: 341687e15da9SChen Yu timer_delete(timerid); 341787e15da9SChen Yu release_msr: 341887e15da9SChen Yu free(per_cpu_msr_sum); 341987e15da9SChen Yu } 34204c2122d4SLen Brown 3421*38c6663aSLen Brown /* 3422*38c6663aSLen Brown * set_my_sched_priority(pri) 3423*38c6663aSLen Brown * return previous 3424*38c6663aSLen Brown */ 3425*38c6663aSLen Brown int set_my_sched_priority(int priority) 3426*38c6663aSLen Brown { 3427*38c6663aSLen Brown int retval; 3428*38c6663aSLen Brown int original_priority; 3429*38c6663aSLen Brown 3430*38c6663aSLen Brown errno = 0; 3431*38c6663aSLen Brown original_priority = getpriority(PRIO_PROCESS, 0); 3432*38c6663aSLen Brown if (errno && (original_priority == -1)) 3433*38c6663aSLen Brown err(errno, "getpriority"); 3434*38c6663aSLen Brown 3435*38c6663aSLen Brown retval = setpriority(PRIO_PROCESS, 0, priority); 3436*38c6663aSLen Brown if (retval) 3437*38c6663aSLen Brown err(retval, "setpriority(%d)", priority); 3438*38c6663aSLen Brown 3439*38c6663aSLen Brown errno = 0; 3440*38c6663aSLen Brown retval = getpriority(PRIO_PROCESS, 0); 3441*38c6663aSLen Brown if (retval != priority) 3442*38c6663aSLen Brown err(-1, "getpriority(%d) != setpriority(%d)", retval, priority); 3443*38c6663aSLen Brown 3444*38c6663aSLen Brown return original_priority; 3445*38c6663aSLen Brown } 3446*38c6663aSLen Brown 3447103a8feaSLen Brown void turbostat_loop() 3448103a8feaSLen Brown { 3449c98d5d94SLen Brown int retval; 3450e52966c0SLen Brown int restarted = 0; 3451023fe0acSChen Yu int done_iters = 0; 3452c98d5d94SLen Brown 34538aa2ed0bSLen Brown setup_signal_handler(); 34548aa2ed0bSLen Brown 3455*38c6663aSLen Brown /* 3456*38c6663aSLen Brown * elevate own priority for interval mode 3457*38c6663aSLen Brown */ 3458*38c6663aSLen Brown set_my_sched_priority(-20); 3459*38c6663aSLen Brown 3460103a8feaSLen Brown restart: 3461e52966c0SLen Brown restarted++; 3462e52966c0SLen Brown 3463562a2d37SLen Brown snapshot_proc_sysfs_files(); 3464c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 34654c2122d4SLen Brown first_counter_read = 0; 3466d91bb17cSLen Brown if (retval < -1) { 3467d91bb17cSLen Brown exit(retval); 3468d91bb17cSLen Brown } else if (retval == -1) { 34693d7772eaSLen Brown if (restarted > 10) { 3470e52966c0SLen Brown exit(retval); 3471e52966c0SLen Brown } 3472c98d5d94SLen Brown re_initialize(); 3473c98d5d94SLen Brown goto restart; 3474c98d5d94SLen Brown } 3475e52966c0SLen Brown restarted = 0; 3476023fe0acSChen Yu done_iters = 0; 3477103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3478103a8feaSLen Brown 3479103a8feaSLen Brown while (1) { 3480c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3481103a8feaSLen Brown re_initialize(); 3482103a8feaSLen Brown goto restart; 3483103a8feaSLen Brown } 3484b9ad8ee0SLen Brown do_sleep(); 3485562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3486562a2d37SLen Brown goto restart; 3487c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3488d91bb17cSLen Brown if (retval < -1) { 3489d91bb17cSLen Brown exit(retval); 3490d91bb17cSLen Brown } else if (retval == -1) { 349115aaa346SLen Brown re_initialize(); 349215aaa346SLen Brown goto restart; 349315aaa346SLen Brown } 3494103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3495103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3496ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3497ba3dec99SLen Brown re_initialize(); 3498ba3dec99SLen Brown goto restart; 3499ba3dec99SLen Brown } 3500c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3501c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3502b7d8c148SLen Brown flush_output_stdout(); 35038aa2ed0bSLen Brown if (exit_requested) 35048aa2ed0bSLen Brown break; 3505023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3506023fe0acSChen Yu break; 3507b9ad8ee0SLen Brown do_sleep(); 3508562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3509562a2d37SLen Brown goto restart; 3510c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3511d91bb17cSLen Brown if (retval < -1) { 3512d91bb17cSLen Brown exit(retval); 3513d91bb17cSLen Brown } else if (retval == -1) { 3514103a8feaSLen Brown re_initialize(); 3515103a8feaSLen Brown goto restart; 3516103a8feaSLen Brown } 3517103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3518103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3519ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3520ba3dec99SLen Brown re_initialize(); 3521ba3dec99SLen Brown goto restart; 3522ba3dec99SLen Brown } 3523c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3524c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3525b7d8c148SLen Brown flush_output_stdout(); 35268aa2ed0bSLen Brown if (exit_requested) 35278aa2ed0bSLen Brown break; 3528023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3529023fe0acSChen Yu break; 3530103a8feaSLen Brown } 3531103a8feaSLen Brown } 3532103a8feaSLen Brown 3533103a8feaSLen Brown void check_dev_msr() 3534103a8feaSLen Brown { 3535103a8feaSLen Brown struct stat sb; 35367ce7d5deSPrarit Bhargava char pathname[32]; 3537103a8feaSLen Brown 35387ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 35397ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3540a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3541d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3542103a8feaSLen Brown } 3543103a8feaSLen Brown 3544fcaa681cSLen Brown /* 3545fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3546fcaa681cSLen Brown * return 0 on success 3547fcaa681cSLen Brown * return 1 on fail 3548fcaa681cSLen Brown */ 3549fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3550103a8feaSLen Brown { 3551fcaa681cSLen Brown cap_t caps; 3552fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3553fcaa681cSLen Brown 3554fcaa681cSLen Brown caps = cap_get_proc(); 3555fcaa681cSLen Brown if (caps == NULL) 3556fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3557fcaa681cSLen Brown 3558fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3559fcaa681cSLen Brown err(-6, "cap_get\n"); 3560fcaa681cSLen Brown 3561fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 35621b439f01SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3563fcaa681cSLen Brown return 1; 3564fcaa681cSLen Brown } 3565fcaa681cSLen Brown 3566fcaa681cSLen Brown if (cap_free(caps) == -1) 3567fcaa681cSLen Brown err(-6, "cap_free\n"); 3568fcaa681cSLen Brown 3569fcaa681cSLen Brown return 0; 3570fcaa681cSLen Brown } 35711b439f01SLen Brown 3572fcaa681cSLen Brown void check_permissions(void) 3573fcaa681cSLen Brown { 357498481e79SLen Brown int do_exit = 0; 35757ce7d5deSPrarit Bhargava char pathname[32]; 357698481e79SLen Brown 357798481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3578fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 357998481e79SLen Brown 358098481e79SLen Brown /* test file permissions */ 35817ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 35827ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 358398481e79SLen Brown do_exit++; 358498481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 358598481e79SLen Brown } 358698481e79SLen Brown 358798481e79SLen Brown /* if all else fails, thell them to be root */ 358898481e79SLen Brown if (do_exit) 3589b2c95d90SJosh Triplett if (getuid() != 0) 3590d7899447SLen Brown warnx("... or simply run as root"); 359198481e79SLen Brown 359298481e79SLen Brown if (do_exit) 359398481e79SLen Brown exit(-6); 3594103a8feaSLen Brown } 3595103a8feaSLen Brown 3596d7899447SLen Brown /* 3597d7899447SLen Brown * NHM adds support for additional MSRs: 3598d7899447SLen Brown * 3599d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3600d7899447SLen Brown * 3601ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 36021df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3603d7899447SLen Brown * 3604cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3605cf4cbe53SLen Brown * 3606d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3607d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3608d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3609d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3610d7899447SLen Brown * 3611ee7e38e3SLen Brown * Side effect: 36121df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 361333148d67SLen Brown * sets has_misc_feature_control 3614d7899447SLen Brown */ 3615ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3616103a8feaSLen Brown { 3617ee7e38e3SLen Brown unsigned long long msr; 361821ed5574SLen Brown unsigned int base_ratio; 3619ee7e38e3SLen Brown int *pkg_cstate_limits; 3620ee7e38e3SLen Brown 3621103a8feaSLen Brown if (!genuine_intel) 3622103a8feaSLen Brown return 0; 3623103a8feaSLen Brown 3624103a8feaSLen Brown if (family != 6) 3625103a8feaSLen Brown return 0; 3626103a8feaSLen Brown 362721ed5574SLen Brown bclk = discover_bclk(family, model); 362821ed5574SLen Brown 3629103a8feaSLen Brown switch (model) { 3630869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3631869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3632ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3633ee7e38e3SLen Brown break; 3634869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3635869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3636869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3637869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3638ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 363933148d67SLen Brown has_misc_feature_control = 1; 3640ee7e38e3SLen Brown break; 3641c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 36425e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3643869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 364477e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3645c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 36465e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3647869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3648af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3649af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3650ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 365133148d67SLen Brown has_misc_feature_control = 1; 3652ee7e38e3SLen Brown break; 3653d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3654d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 365533148d67SLen Brown has_misc_feature_control = 1; 3656d8ebb442SLen Brown break; 365725368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 365825368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits; 365925368d7cSChen Yu has_misc_feature_control = 1; 366025368d7cSChen Yu break; 3661f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3662cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 36635ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3664ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3665ee7e38e3SLen Brown break; 3666869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3667ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3668cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3669ee7e38e3SLen Brown break; 3670869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3671ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3672ee7e38e3SLen Brown break; 3673869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3674f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 36755ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3676f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 367720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3678445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3679e4085d54SLen Brown break; 3680103a8feaSLen Brown default: 3681103a8feaSLen Brown return 0; 3682103a8feaSLen Brown } 36831df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3684e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3685ee7e38e3SLen Brown 3686ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 368721ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 368821ed5574SLen Brown 368921ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 369021ed5574SLen Brown has_base_hz = 1; 3691ee7e38e3SLen Brown return 1; 3692103a8feaSLen Brown } 36931b439f01SLen Brown 36940f7887c4SLen Brown /* 3695495c7654SLen Brown * SLV client has support for unique MSRs: 36960f7887c4SLen Brown * 36970f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 36980f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 36990f7887c4SLen Brown */ 37000f7887c4SLen Brown 37010f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 37020f7887c4SLen Brown { 37030f7887c4SLen Brown if (!genuine_intel) 37040f7887c4SLen Brown return 0; 37050f7887c4SLen Brown 37060f7887c4SLen Brown switch (model) { 3707f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3708f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3709f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 37100f7887c4SLen Brown return 1; 37110f7887c4SLen Brown } 37120f7887c4SLen Brown return 0; 37130f7887c4SLen Brown } 37141b439f01SLen Brown 37157170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 37167170a374SLen Brown { 37177170a374SLen Brown 37187170a374SLen Brown if (!genuine_intel) 37197170a374SLen Brown return 0; 37207170a374SLen Brown 37217170a374SLen Brown switch (model) { 37225ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 37237170a374SLen Brown return 1; 37247170a374SLen Brown } 37257170a374SLen Brown return 0; 37267170a374SLen Brown } 37271b439f01SLen Brown 3728ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3729ade0ebacSLen Brown { 3730ade0ebacSLen Brown 3731ade0ebacSLen Brown if (!genuine_intel) 3732ade0ebacSLen Brown return 0; 3733ade0ebacSLen Brown 3734ade0ebacSLen Brown switch (model) { 3735ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3736ade0ebacSLen Brown return 1; 3737ade0ebacSLen Brown } 3738ade0ebacSLen Brown return 0; 3739ade0ebacSLen Brown } 37401b439f01SLen Brown 374134c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 374234c76197SLen Brown { 374334c76197SLen Brown 374434c76197SLen Brown if (!genuine_intel) 374534c76197SLen Brown return 0; 374634c76197SLen Brown 374734c76197SLen Brown switch (model) { 374834c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 374934c76197SLen Brown return 1; 375034c76197SLen Brown } 375134c76197SLen Brown return 0; 375234c76197SLen Brown } 375325368d7cSChen Yu 375425368d7cSChen Yu int is_icx(unsigned int family, unsigned int model) 375525368d7cSChen Yu { 375625368d7cSChen Yu 375725368d7cSChen Yu if (!genuine_intel) 375825368d7cSChen Yu return 0; 375925368d7cSChen Yu 376025368d7cSChen Yu switch (model) { 376125368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 376225368d7cSChen Yu return 1; 376325368d7cSChen Yu } 376425368d7cSChen Yu return 0; 376525368d7cSChen Yu } 376625368d7cSChen Yu 3767f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3768f6708400SChen Yu { 3769f6708400SChen Yu if (!genuine_intel) 3770f6708400SChen Yu return 0; 3771f6708400SChen Yu 3772f6708400SChen Yu switch (model) { 3773f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3774f6708400SChen Yu return 1; 3775f6708400SChen Yu } 3776f6708400SChen Yu return 0; 3777f6708400SChen Yu } 37781b439f01SLen Brown 377920de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 378020de0dabSAntti Laakso { 378120de0dabSAntti Laakso if (!genuine_intel) 378220de0dabSAntti Laakso return 0; 378320de0dabSAntti Laakso 378420de0dabSAntti Laakso switch (model) { 378520de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 378620de0dabSAntti Laakso return 1; 378720de0dabSAntti Laakso } 378820de0dabSAntti Laakso return 0; 378920de0dabSAntti Laakso } 37900f7887c4SLen Brown 379131e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3792d7899447SLen Brown { 37930f7887c4SLen Brown if (has_slv_msrs(family, model)) 37940f7887c4SLen Brown return 0; 37950f7887c4SLen Brown 3796d7899447SLen Brown switch (model) { 3797d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3798869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3799869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3800d7899447SLen Brown return 0; 3801d7899447SLen Brown default: 3802d7899447SLen Brown return 1; 3803d7899447SLen Brown } 3804d7899447SLen Brown } 38051b439f01SLen Brown 38060f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 38070f7887c4SLen Brown { 38080f7887c4SLen Brown if (has_slv_msrs(family, model)) 38090f7887c4SLen Brown return 1; 38100f7887c4SLen Brown 38110f7887c4SLen Brown return 0; 38120f7887c4SLen Brown } 38131b439f01SLen Brown 38146574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 38156574a5d5SLen Brown { 38166574a5d5SLen Brown if (!genuine_intel) 38176574a5d5SLen Brown return 0; 38186574a5d5SLen Brown 38196574a5d5SLen Brown if (family != 6) 38206574a5d5SLen Brown return 0; 38216574a5d5SLen Brown 38226574a5d5SLen Brown switch (model) { 3823869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3824869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 38256574a5d5SLen Brown return 1; 38266574a5d5SLen Brown default: 38276574a5d5SLen Brown return 0; 38286574a5d5SLen Brown } 38296574a5d5SLen Brown } 38301b439f01SLen Brown 3831fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3832fcd17211SLen Brown { 3833fcd17211SLen Brown if (!genuine_intel) 3834fcd17211SLen Brown return 0; 3835fcd17211SLen Brown 3836fcd17211SLen Brown if (family != 6) 3837fcd17211SLen Brown return 0; 3838fcd17211SLen Brown 3839fcd17211SLen Brown switch (model) { 3840869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3841fcd17211SLen Brown return 1; 3842fcd17211SLen Brown default: 3843fcd17211SLen Brown return 0; 3844fcd17211SLen Brown } 3845fcd17211SLen Brown } 3846fcd17211SLen Brown 3847fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3848fb5d4327SDasaratharaman Chandramouli { 3849fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3850fb5d4327SDasaratharaman Chandramouli return 0; 3851fb5d4327SDasaratharaman Chandramouli 3852fb5d4327SDasaratharaman Chandramouli if (family != 6) 3853fb5d4327SDasaratharaman Chandramouli return 0; 3854fb5d4327SDasaratharaman Chandramouli 3855fb5d4327SDasaratharaman Chandramouli switch (model) { 3856869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3857fb5d4327SDasaratharaman Chandramouli return 1; 3858fb5d4327SDasaratharaman Chandramouli default: 3859fb5d4327SDasaratharaman Chandramouli return 0; 3860fb5d4327SDasaratharaman Chandramouli } 3861fb5d4327SDasaratharaman Chandramouli } 38621b439f01SLen Brown 386331e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 386431e07522SLen Brown { 386531e07522SLen Brown if (!genuine_intel) 386631e07522SLen Brown return 0; 386731e07522SLen Brown 386831e07522SLen Brown if (family != 6) 386931e07522SLen Brown return 0; 387031e07522SLen Brown 387131e07522SLen Brown switch (model) { 387231e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 387331e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 387425368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 387531e07522SLen Brown return 1; 387631e07522SLen Brown default: 387731e07522SLen Brown return 0; 387831e07522SLen Brown } 387931e07522SLen Brown } 38801b439f01SLen Brown 38816fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 38826fb3143bSLen Brown { 38836fb3143bSLen Brown if (!genuine_intel) 38846fb3143bSLen Brown return 0; 38856fb3143bSLen Brown 38866fb3143bSLen Brown if (family != 6) 38876fb3143bSLen Brown return 0; 38886fb3143bSLen Brown 38896fb3143bSLen Brown switch (model) { 3890869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3891c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3892869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 389377e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 38945e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3895c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 38965e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3897869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3898af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3899af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3900869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 390125368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 39026fb3143bSLen Brown 3903869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 39046fb3143bSLen Brown return 1; 39056fb3143bSLen Brown default: 39066fb3143bSLen Brown return 0; 39076fb3143bSLen Brown } 39086fb3143bSLen Brown } 39096fb3143bSLen Brown 39100b9a0b9bSZhang Rui /* 39110b9a0b9bSZhang Rui * tcc_offset_bits: 39120b9a0b9bSZhang Rui * 0: Tcc Offset not supported (Default) 39130b9a0b9bSZhang Rui * 6: Bit 29:24 of MSR_PLATFORM_INFO 39140b9a0b9bSZhang Rui * 4: Bit 27:24 of MSR_PLATFORM_INFO 39150b9a0b9bSZhang Rui */ 39160b9a0b9bSZhang Rui void check_tcc_offset(int model) 39170b9a0b9bSZhang Rui { 39180b9a0b9bSZhang Rui unsigned long long msr; 39190b9a0b9bSZhang Rui 39200b9a0b9bSZhang Rui if (!genuine_intel) 39210b9a0b9bSZhang Rui return; 39220b9a0b9bSZhang Rui 39230b9a0b9bSZhang Rui switch (model) { 39240b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE_L: 39250b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE: 39260b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE_L: 39270b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE: 39280b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE_L: 39290b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE: 39300b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE_L: 39310b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE: 39320b9a0b9bSZhang Rui case INTEL_FAM6_COMETLAKE: 39330b9a0b9bSZhang Rui if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) { 39340b9a0b9bSZhang Rui msr = (msr >> 30) & 1; 39350b9a0b9bSZhang Rui if (msr) 39360b9a0b9bSZhang Rui tcc_offset_bits = 6; 39370b9a0b9bSZhang Rui } 39380b9a0b9bSZhang Rui return; 39390b9a0b9bSZhang Rui default: 39400b9a0b9bSZhang Rui return; 39410b9a0b9bSZhang Rui } 39420b9a0b9bSZhang Rui } 39430b9a0b9bSZhang Rui 39441b439f01SLen Brown static void remove_underbar(char *s) 3945fecb3bc8SDavid Arcari { 3946fecb3bc8SDavid Arcari char *to = s; 3947fecb3bc8SDavid Arcari 3948fecb3bc8SDavid Arcari while (*s) { 3949fecb3bc8SDavid Arcari if (*s != '_') 3950fecb3bc8SDavid Arcari *to++ = *s; 3951fecb3bc8SDavid Arcari s++; 3952fecb3bc8SDavid Arcari } 3953fecb3bc8SDavid Arcari 3954fecb3bc8SDavid Arcari *to = 0; 3955fecb3bc8SDavid Arcari } 3956fecb3bc8SDavid Arcari 39571b439f01SLen Brown static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3958fcd17211SLen Brown { 3959fcd17211SLen Brown if (!do_nhm_platform_info) 3960fcd17211SLen Brown return; 3961fcd17211SLen Brown 3962fcd17211SLen Brown dump_nhm_platform_info(); 3963fcd17211SLen Brown 3964fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3965fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3966fcd17211SLen Brown 3967fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3968fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3969fcd17211SLen Brown 397031e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 397131e07522SLen Brown dump_turbo_ratio_limits(family, model); 3972fcd17211SLen Brown 39730f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 39740f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 39750f7887c4SLen Brown 3976fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3977fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3978fb5d4327SDasaratharaman Chandramouli 39796fb3143bSLen Brown if (has_config_tdp(family, model)) 39806fb3143bSLen Brown dump_config_tdp(); 39816fb3143bSLen Brown 3982fcd17211SLen Brown dump_nhm_cst_cfg(); 3983fcd17211SLen Brown } 3984fcd17211SLen Brown 3985abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 3986abdcbdb2SLen Brown { 3987abdcbdb2SLen Brown FILE *input; 3988abdcbdb2SLen Brown char cpuidle_buf[64]; 3989abdcbdb2SLen Brown 3990abdcbdb2SLen Brown input = fopen(path, "r"); 3991abdcbdb2SLen Brown if (input == NULL) { 3992abdcbdb2SLen Brown if (debug) 3993abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 3994abdcbdb2SLen Brown return; 3995abdcbdb2SLen Brown } 3996abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 3997abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 3998abdcbdb2SLen Brown fclose(input); 3999abdcbdb2SLen Brown 4000abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 4001abdcbdb2SLen Brown } 40021b439f01SLen Brown 40031b439f01SLen Brown static void dump_sysfs_cstate_config(void) 400441618e63SLen Brown { 400541618e63SLen Brown char path[64]; 400641618e63SLen Brown char name_buf[16]; 400741618e63SLen Brown char desc[64]; 400841618e63SLen Brown FILE *input; 400941618e63SLen Brown int state; 401041618e63SLen Brown char *sp; 401141618e63SLen Brown 4012abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 4013abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 4014abdcbdb2SLen Brown return; 4015abdcbdb2SLen Brown } 4016abdcbdb2SLen Brown 4017abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 4018abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 4019abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 4020abdcbdb2SLen Brown 402141618e63SLen Brown for (state = 0; state < 10; ++state) { 402241618e63SLen Brown 40231b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 402441618e63SLen Brown input = fopen(path, "r"); 402541618e63SLen Brown if (input == NULL) 402641618e63SLen Brown continue; 40278173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 40288173c336SBen Hutchings err(1, "%s: failed to read file", path); 402941618e63SLen Brown 403041618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 403141618e63SLen Brown sp = strchr(name_buf, '-'); 403241618e63SLen Brown if (!sp) 403341618e63SLen Brown sp = strchrnul(name_buf, '\n'); 403441618e63SLen Brown *sp = '\0'; 403541618e63SLen Brown fclose(input); 403641618e63SLen Brown 4037fecb3bc8SDavid Arcari remove_underbar(name_buf); 4038fecb3bc8SDavid Arcari 40391b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state); 404041618e63SLen Brown input = fopen(path, "r"); 404141618e63SLen Brown if (input == NULL) 404241618e63SLen Brown continue; 40438173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 40448173c336SBen Hutchings err(1, "%s: failed to read file", path); 404541618e63SLen Brown 404641618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 404741618e63SLen Brown fclose(input); 404841618e63SLen Brown } 404941618e63SLen Brown } 40501b439f01SLen Brown 40511b439f01SLen Brown static void dump_sysfs_pstate_config(void) 40527293fccdSLen Brown { 40537293fccdSLen Brown char path[64]; 40547293fccdSLen Brown char driver_buf[64]; 40557293fccdSLen Brown char governor_buf[64]; 40567293fccdSLen Brown FILE *input; 40577293fccdSLen Brown int turbo; 40587293fccdSLen Brown 40591b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu); 40607293fccdSLen Brown input = fopen(path, "r"); 40617293fccdSLen Brown if (input == NULL) { 40620a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40637293fccdSLen Brown return; 40647293fccdSLen Brown } 40658173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 40668173c336SBen Hutchings err(1, "%s: failed to read file", path); 40677293fccdSLen Brown fclose(input); 40687293fccdSLen Brown 40691b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu); 40707293fccdSLen Brown input = fopen(path, "r"); 40717293fccdSLen Brown if (input == NULL) { 40720a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40737293fccdSLen Brown return; 40747293fccdSLen Brown } 40758173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 40768173c336SBen Hutchings err(1, "%s: failed to read file", path); 40777293fccdSLen Brown fclose(input); 40787293fccdSLen Brown 40797293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 40807293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 40817293fccdSLen Brown 40827293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 40837293fccdSLen Brown input = fopen(path, "r"); 40847293fccdSLen Brown if (input != NULL) { 40858173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 40868173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 40877293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 40887293fccdSLen Brown fclose(input); 40897293fccdSLen Brown } 40907293fccdSLen Brown 40917293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 40927293fccdSLen Brown input = fopen(path, "r"); 40937293fccdSLen Brown if (input != NULL) { 40948173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 40958173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 40967293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 40977293fccdSLen Brown fclose(input); 40987293fccdSLen Brown } 40997293fccdSLen Brown } 410041618e63SLen Brown 4101889facbeSLen Brown /* 4102889facbeSLen Brown * print_epb() 4103889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 4104889facbeSLen Brown */ 4105889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4106889facbeSLen Brown { 4107889facbeSLen Brown char *epb_string; 41086d6501d9SBorislav Petkov int cpu, epb; 4109889facbeSLen Brown 4110889facbeSLen Brown if (!has_epb) 4111889facbeSLen Brown return 0; 4112889facbeSLen Brown 4113889facbeSLen Brown cpu = t->cpu_id; 4114889facbeSLen Brown 4115889facbeSLen Brown /* EPB is per-package */ 4116889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4117889facbeSLen Brown return 0; 4118889facbeSLen Brown 4119889facbeSLen Brown if (cpu_migrate(cpu)) { 41203d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 4121889facbeSLen Brown return -1; 4122889facbeSLen Brown } 4123889facbeSLen Brown 41246d6501d9SBorislav Petkov epb = get_epb(cpu); 41256d6501d9SBorislav Petkov if (epb < 0) 4126889facbeSLen Brown return 0; 4127889facbeSLen Brown 41286d6501d9SBorislav Petkov switch (epb) { 4129889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 4130889facbeSLen Brown epb_string = "performance"; 4131889facbeSLen Brown break; 4132889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 4133889facbeSLen Brown epb_string = "balanced"; 4134889facbeSLen Brown break; 4135889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 4136889facbeSLen Brown epb_string = "powersave"; 4137889facbeSLen Brown break; 4138889facbeSLen Brown default: 4139889facbeSLen Brown epb_string = "custom"; 4140889facbeSLen Brown break; 4141889facbeSLen Brown } 41426d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 4143889facbeSLen Brown 4144889facbeSLen Brown return 0; 4145889facbeSLen Brown } 41461b439f01SLen Brown 41477f5c258eSLen Brown /* 41487f5c258eSLen Brown * print_hwp() 41497f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 41507f5c258eSLen Brown */ 41517f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 41527f5c258eSLen Brown { 41537f5c258eSLen Brown unsigned long long msr; 41547f5c258eSLen Brown int cpu; 41557f5c258eSLen Brown 41567f5c258eSLen Brown if (!has_hwp) 41577f5c258eSLen Brown return 0; 41587f5c258eSLen Brown 41597f5c258eSLen Brown cpu = t->cpu_id; 41607f5c258eSLen Brown 41617f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 41627f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 41637f5c258eSLen Brown return 0; 41647f5c258eSLen Brown 41657f5c258eSLen Brown if (cpu_migrate(cpu)) { 41663d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 41677f5c258eSLen Brown return -1; 41687f5c258eSLen Brown } 41697f5c258eSLen Brown 41707f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 41717f5c258eSLen Brown return 0; 41727f5c258eSLen Brown 41731b439f01SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 41747f5c258eSLen Brown 41757f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 41767f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 41777f5c258eSLen Brown return 0; 41787f5c258eSLen Brown 41797f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 41807f5c258eSLen Brown return 0; 41817f5c258eSLen Brown 4182b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 41836dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 41847f5c258eSLen Brown cpu, msr, 41857f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 41867f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 41871b439f01SLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr)); 41887f5c258eSLen Brown 41897f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 41907f5c258eSLen Brown return 0; 41917f5c258eSLen Brown 4192b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 41936dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 41947f5c258eSLen Brown cpu, msr, 41957f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 41967f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 41977f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 41987f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 41991b439f01SLen Brown (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1)); 42007f5c258eSLen Brown 42017f5c258eSLen Brown if (has_hwp_pkg) { 42027f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 42037f5c258eSLen Brown return 0; 42047f5c258eSLen Brown 4205b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 42066dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 42077f5c258eSLen Brown cpu, msr, 42087f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 42097f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 42107f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 42111b439f01SLen Brown (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3)); 42127f5c258eSLen Brown } 42137f5c258eSLen Brown if (has_hwp_notify) { 42147f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 42157f5c258eSLen Brown return 0; 42167f5c258eSLen Brown 4217b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 42187f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 42191b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis"); 42207f5c258eSLen Brown } 42217f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 42227f5c258eSLen Brown return 0; 42237f5c258eSLen Brown 4224b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 42257f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 42261b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-"); 4227889facbeSLen Brown 4228889facbeSLen Brown return 0; 4229889facbeSLen Brown } 4230889facbeSLen Brown 42313a9a941dSLen Brown /* 42323a9a941dSLen Brown * print_perf_limit() 42333a9a941dSLen Brown */ 42343a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 42353a9a941dSLen Brown { 42363a9a941dSLen Brown unsigned long long msr; 42373a9a941dSLen Brown int cpu; 42383a9a941dSLen Brown 42393a9a941dSLen Brown cpu = t->cpu_id; 42403a9a941dSLen Brown 42413a9a941dSLen Brown /* per-package */ 42423a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 42433a9a941dSLen Brown return 0; 42443a9a941dSLen Brown 42453a9a941dSLen Brown if (cpu_migrate(cpu)) { 42463d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 42473a9a941dSLen Brown return -1; 42483a9a941dSLen Brown } 42493a9a941dSLen Brown 42503a9a941dSLen Brown if (do_core_perf_limit_reasons) { 42513a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4252b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4253b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4254e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 42553a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4256e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4257e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4258e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4259e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4260e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4261e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4262e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4263e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4264e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4265e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 42661b439f01SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : ""); 4267b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4268e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 42693a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4270e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4271e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4272e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4273e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4274e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4275e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4276e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4277e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4278e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4279e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 42801b439f01SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : ""); 42813a9a941dSLen Brown 42823a9a941dSLen Brown } 42833a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 42843a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4285b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4286b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 42873a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 42883a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 42893a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 42903a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 42913a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 42923a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 42931b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4294b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 42953a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 42963a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 42973a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 42983a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 42993a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 43003a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 43011b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 43023a9a941dSLen Brown } 43033a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 43043a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4305b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4306b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 43073a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 43083a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 43093a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 43103a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 43111b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4312b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 43133a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 43143a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 43153a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 43163a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 43171b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 43183a9a941dSLen Brown } 43193a9a941dSLen Brown return 0; 43203a9a941dSLen Brown } 43213a9a941dSLen Brown 4322889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4323889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4324889facbeSLen Brown 43259392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4326144b44b1SLen Brown { 4327144b44b1SLen Brown unsigned long long msr; 4328144b44b1SLen Brown 4329144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 43307ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4331144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4332144b44b1SLen Brown 4333144b44b1SLen Brown switch (model) { 4334f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 43355ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4336144b44b1SLen Brown return 30.0; 4337144b44b1SLen Brown default: 4338144b44b1SLen Brown return 135.0; 4339144b44b1SLen Brown } 4340144b44b1SLen Brown } 4341144b44b1SLen Brown 43429392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 43439392bd98SCalvin Walton { 434433eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 434533eb8225SKim Phillips return 280.0; 43469392bd98SCalvin Walton } 43479392bd98SCalvin Walton 434840ee8e3bSAndrey Semin /* 434940ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 435040ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 435140ee8e3bSAndrey Semin */ 43521b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units) 435340ee8e3bSAndrey Semin { 435440ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 435540ee8e3bSAndrey Semin 435640ee8e3bSAndrey Semin switch (model) { 4357869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4358869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4359abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4360869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 436140ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 436240ee8e3bSAndrey Semin default: 436340ee8e3bSAndrey Semin return (rapl_energy_units); 436440ee8e3bSAndrey Semin } 436540ee8e3bSAndrey Semin } 436640ee8e3bSAndrey Semin 43679392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4368889facbeSLen Brown { 4369889facbeSLen Brown unsigned long long msr; 4370144b44b1SLen Brown unsigned int time_unit; 4371889facbeSLen Brown double tdp; 4372889facbeSLen Brown 4373889facbeSLen Brown if (family != 6) 4374889facbeSLen Brown return; 4375889facbeSLen Brown 4376889facbeSLen Brown switch (model) { 4377869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4378869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4379c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 438077e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 43815e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4382c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 43835e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4384144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4385812db3f7SLen Brown if (rapl_joules) { 4386812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4387812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4388812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4389812db3f7SLen Brown } else { 4390812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4391812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4392812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4393812db3f7SLen Brown } 4394889facbeSLen Brown break; 4395869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4396f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4397e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4398812db3f7SLen Brown if (rapl_joules) 4399812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4400812db3f7SLen Brown else 4401812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4402e4085d54SLen Brown break; 4403f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 44041b439f01SLen Brown do_rapl = 44051b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 44061b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4407f6708400SChen Yu if (rapl_joules) { 4408f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4409f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4410f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4411f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4412f6708400SChen Yu } else { 4413f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4414f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4415f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4416f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4417f6708400SChen Yu } 4418f6708400SChen Yu break; 441920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 442020de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 442120de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 442220de0dabSAntti Laakso if (rapl_joules) 442320de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 442420de0dabSAntti Laakso else 442520de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 442620de0dabSAntti Laakso break; 4427af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4428af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 44291b439f01SLen Brown do_rapl = 44301b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 44311b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4432812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4433812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4434812db3f7SLen Brown if (rapl_joules) { 4435812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4436812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4437812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 443881824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4439812db3f7SLen Brown } else { 4440812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4441812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4442812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 444381824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4444812db3f7SLen Brown } 44450b2bb692SLen Brown break; 4446869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4447869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4448869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 444925368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4450869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 44511b439f01SLen Brown do_rapl = 44521b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 44531b439f01SLen Brown RAPL_PKG_POWER_INFO; 4454812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4455812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4456812db3f7SLen Brown if (rapl_joules) { 4457812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4458812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4459812db3f7SLen Brown } else { 4460812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4461812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4462812db3f7SLen Brown } 4463e6f9bb3cSLen Brown break; 4464869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4465869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 44661b439f01SLen Brown do_rapl = 44671b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | 44681b439f01SLen Brown RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4469812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4470812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4471812db3f7SLen Brown if (rapl_joules) { 4472812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4473812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4474812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4475812db3f7SLen Brown } else { 4476812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4477812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4478812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4479812db3f7SLen Brown } 4480144b44b1SLen Brown break; 4481f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 44825ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4483144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4484812db3f7SLen Brown if (rapl_joules) { 4485812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4486812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4487812db3f7SLen Brown } else { 4488812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4489812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4490812db3f7SLen Brown } 4491889facbeSLen Brown break; 44925ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 44931b439f01SLen Brown do_rapl = 44941b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 44951b439f01SLen Brown RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4496812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4497812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4498812db3f7SLen Brown if (rapl_joules) { 4499812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4500812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4501812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4502812db3f7SLen Brown } else { 4503812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4504812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4505812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4506812db3f7SLen Brown } 45070f644909SJacob Pan break; 4508889facbeSLen Brown default: 4509889facbeSLen Brown return; 4510889facbeSLen Brown } 4511889facbeSLen Brown 4512889facbeSLen Brown /* units on package 0, verify later other packages match */ 45137ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4514889facbeSLen Brown return; 4515889facbeSLen Brown 4516889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4517f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4518144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4519144b44b1SLen Brown else 4520889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4521889facbeSLen Brown 452240ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 452340ee8e3bSAndrey Semin 4524144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4525144b44b1SLen Brown if (time_unit == 0) 4526144b44b1SLen Brown time_unit = 0xA; 4527889facbeSLen Brown 4528144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4529144b44b1SLen Brown 45309392bd98SCalvin Walton tdp = get_tdp_intel(model); 4531889facbeSLen Brown 4532889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 453396e47158SLen Brown if (!quiet) 4534b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 45359392bd98SCalvin Walton } 4536889facbeSLen Brown 45379392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 45389392bd98SCalvin Walton { 45399392bd98SCalvin Walton unsigned long long msr; 45409392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 45419392bd98SCalvin Walton unsigned int has_rapl = 0; 45429392bd98SCalvin Walton double tdp; 45439392bd98SCalvin Walton 45449392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 45459392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 454633eb8225SKim Phillips /* RAPL (Fam 17h+) */ 45479392bd98SCalvin Walton has_rapl = edx & (1 << 14); 45489392bd98SCalvin Walton } 45499392bd98SCalvin Walton 455033eb8225SKim Phillips if (!has_rapl || family < 0x17) 4551889facbeSLen Brown return; 45529392bd98SCalvin Walton 45539392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 45543316f99aSCalvin Walton if (rapl_joules) { 45553316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 45569392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 45573316f99aSCalvin Walton } else { 45583316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 45599392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 45603316f99aSCalvin Walton } 45619392bd98SCalvin Walton 45629392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 45639392bd98SCalvin Walton return; 45649392bd98SCalvin Walton 45659392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 45669392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 45679392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 45689392bd98SCalvin Walton 45699cfa8e04SPu Wen tdp = get_tdp_amd(family); 45709392bd98SCalvin Walton 45719392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 45729392bd98SCalvin Walton if (!quiet) 45739392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 45749392bd98SCalvin Walton } 45759392bd98SCalvin Walton 45769392bd98SCalvin Walton /* 45779392bd98SCalvin Walton * rapl_probe() 45789392bd98SCalvin Walton * 45799392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 45809392bd98SCalvin Walton */ 45819392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 45829392bd98SCalvin Walton { 45839392bd98SCalvin Walton if (genuine_intel) 45849392bd98SCalvin Walton rapl_probe_intel(family, model); 4585c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 45869392bd98SCalvin Walton rapl_probe_amd(family, model); 4587889facbeSLen Brown } 4588889facbeSLen Brown 45891b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 45903a9a941dSLen Brown { 45913a9a941dSLen Brown if (!genuine_intel) 45923a9a941dSLen Brown return; 45933a9a941dSLen Brown 45943a9a941dSLen Brown if (family != 6) 45953a9a941dSLen Brown return; 45963a9a941dSLen Brown 45973a9a941dSLen Brown switch (model) { 4598c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 459977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 46005e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 46013a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4602869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 46033a9a941dSLen Brown do_core_perf_limit_reasons = 1; 46043a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 46053a9a941dSLen Brown default: 46063a9a941dSLen Brown return; 46073a9a941dSLen Brown } 46083a9a941dSLen Brown } 46093a9a941dSLen Brown 4610ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4611ac980e13SArtem Bityutskiy { 46121b439f01SLen Brown if (is_skx(family, model) || is_bdx(family, model) || is_icx(family, model)) 4613ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4614ac980e13SArtem Bityutskiy } 4615ac980e13SArtem Bityutskiy 4616aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model) 4617aeb01e6dSChen Yu { 4618aeb01e6dSChen Yu if (is_icx(family, model)) 4619aeb01e6dSChen Yu dis_cstate_prewake = 1; 4620aeb01e6dSChen Yu } 4621aeb01e6dSChen Yu 4622889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4623889facbeSLen Brown { 4624889facbeSLen Brown unsigned long long msr; 4625f4896fa5SLen Brown unsigned int dts, dts2; 4626889facbeSLen Brown int cpu; 4627889facbeSLen Brown 4628889facbeSLen Brown if (!(do_dts || do_ptm)) 4629889facbeSLen Brown return 0; 4630889facbeSLen Brown 4631889facbeSLen Brown cpu = t->cpu_id; 4632889facbeSLen Brown 4633889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4634889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4635889facbeSLen Brown return 0; 4636889facbeSLen Brown 4637889facbeSLen Brown if (cpu_migrate(cpu)) { 46383d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4639889facbeSLen Brown return -1; 4640889facbeSLen Brown } 4641889facbeSLen Brown 4642889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4643889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4644889facbeSLen Brown return 0; 4645889facbeSLen Brown 4646889facbeSLen Brown dts = (msr >> 16) & 0x7F; 46471b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts); 4648889facbeSLen Brown 4649889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4650889facbeSLen Brown return 0; 4651889facbeSLen Brown 4652889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4653889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4654b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 465555279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4656889facbeSLen Brown } 4657889facbeSLen Brown 4658f4896fa5SLen Brown if (do_dts && debug) { 4659889facbeSLen Brown unsigned int resolution; 4660889facbeSLen Brown 4661889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4662889facbeSLen Brown return 0; 4663889facbeSLen Brown 4664889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4665889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4666b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 466755279aefSZhang Rui cpu, msr, tj_max - dts, resolution); 4668889facbeSLen Brown 4669889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4670889facbeSLen Brown return 0; 4671889facbeSLen Brown 4672889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4673889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4674b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 467555279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4676889facbeSLen Brown } 4677889facbeSLen Brown 4678889facbeSLen Brown return 0; 4679889facbeSLen Brown } 4680889facbeSLen Brown 4681889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4682889facbeSLen Brown { 4683b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4684889facbeSLen Brown cpu, label, 4685889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4686889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4687889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4688889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4689889facbeSLen Brown 4690889facbeSLen Brown return; 4691889facbeSLen Brown } 4692889facbeSLen Brown 4693889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4694889facbeSLen Brown { 4695889facbeSLen Brown unsigned long long msr; 46969392bd98SCalvin Walton const char *msr_name; 4697889facbeSLen Brown int cpu; 4698889facbeSLen Brown 4699889facbeSLen Brown if (!do_rapl) 4700889facbeSLen Brown return 0; 4701889facbeSLen Brown 4702889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4703889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4704889facbeSLen Brown return 0; 4705889facbeSLen Brown 4706889facbeSLen Brown cpu = t->cpu_id; 4707889facbeSLen Brown if (cpu_migrate(cpu)) { 47083d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4709889facbeSLen Brown return -1; 4710889facbeSLen Brown } 4711889facbeSLen Brown 47129392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 47139392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 47149392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 47159392bd98SCalvin Walton return -1; 47169392bd98SCalvin Walton } else { 47179392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4718889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4719889facbeSLen Brown return -1; 47209392bd98SCalvin Walton } 4721889facbeSLen Brown 47229392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4723144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 472496e47158SLen Brown 4725144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4726144b44b1SLen Brown 4727889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4728889facbeSLen Brown return -5; 4729889facbeSLen Brown 4730b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4731889facbeSLen Brown cpu, msr, 4732889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4733889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4734889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4735889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4736889facbeSLen Brown 4737144b44b1SLen Brown } 4738144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4739144b44b1SLen Brown 4740889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4741889facbeSLen Brown return -9; 4742889facbeSLen Brown 4743b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4744889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4745889facbeSLen Brown 4746889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4747b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4748889facbeSLen Brown cpu, 4749889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4750889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4751889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4752889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4753889facbeSLen Brown } 4754889facbeSLen Brown 47550b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4756889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4757889facbeSLen Brown return -6; 4758889facbeSLen Brown 4759b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4760889facbeSLen Brown cpu, msr, 4761889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4762889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4763889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4764889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 47650b2bb692SLen Brown } 47660b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4767889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4768889facbeSLen Brown return -9; 4769b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4770889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4771889facbeSLen Brown 4772889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4773889facbeSLen Brown } 4774144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4775889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4776889facbeSLen Brown return -7; 4777889facbeSLen Brown 4778b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4779144b44b1SLen Brown } 47809148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4781889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4782889facbeSLen Brown return -9; 4783b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4784889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4785889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4786889facbeSLen Brown } 4787889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4788889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4789889facbeSLen Brown return -8; 4790889facbeSLen Brown 4791b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4792889facbeSLen Brown 4793889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4794889facbeSLen Brown return -9; 4795b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4796889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4797889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4798889facbeSLen Brown } 4799889facbeSLen Brown return 0; 4800889facbeSLen Brown } 4801889facbeSLen Brown 4802d7899447SLen Brown /* 4803d7899447SLen Brown * SNB adds support for additional MSRs: 4804d7899447SLen Brown * 4805d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4806d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4807d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4808d7899447SLen Brown */ 4809103a8feaSLen Brown 4810d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4811103a8feaSLen Brown { 4812103a8feaSLen Brown if (!genuine_intel) 4813103a8feaSLen Brown return 0; 4814103a8feaSLen Brown 4815103a8feaSLen Brown switch (model) { 4816869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4817869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4818869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4819869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4820c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4821869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 482277e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 48235e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4824c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 48255e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4826869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4827af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4828af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4829869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 483025368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4831869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4832f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 48335ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4834f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 483520de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4836103a8feaSLen Brown return 1; 4837103a8feaSLen Brown } 4838103a8feaSLen Brown return 0; 4839103a8feaSLen Brown } 4840103a8feaSLen Brown 4841d7899447SLen Brown /* 4842570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4843d7899447SLen Brown * 4844d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4845d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4846d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 48475a63426eSLen Brown * 48485a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 48495a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 48505a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 48515a63426eSLen Brown * 4852d7899447SLen Brown */ 4853570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4854ca58710fSKristen Carlson Accardi { 4855ca58710fSKristen Carlson Accardi if (!genuine_intel) 4856ca58710fSKristen Carlson Accardi return 0; 4857ca58710fSKristen Carlson Accardi 4858ca58710fSKristen Carlson Accardi switch (model) { 485977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4860c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4861af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4862af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4863869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4864f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4865f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4866ca58710fSKristen Carlson Accardi return 1; 4867ca58710fSKristen Carlson Accardi } 4868ca58710fSKristen Carlson Accardi return 0; 4869ca58710fSKristen Carlson Accardi } 4870ca58710fSKristen Carlson Accardi 48710b2bb692SLen Brown /* 48720b2bb692SLen Brown * SKL adds support for additional MSRS: 48730b2bb692SLen Brown * 48740b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 48750b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 48760b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 48770b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 48780b2bb692SLen Brown */ 48790b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 48800b2bb692SLen Brown { 48810b2bb692SLen Brown if (!genuine_intel) 48820b2bb692SLen Brown return 0; 48830b2bb692SLen Brown 48840b2bb692SLen Brown switch (model) { 4885af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4886af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 48870b2bb692SLen Brown return 1; 48880b2bb692SLen Brown } 48890b2bb692SLen Brown return 0; 48900b2bb692SLen Brown } 48910b2bb692SLen Brown 4892144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4893144b44b1SLen Brown { 4894144b44b1SLen Brown if (!genuine_intel) 4895144b44b1SLen Brown return 0; 4896144b44b1SLen Brown switch (model) { 4897f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 48985ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4899144b44b1SLen Brown return 1; 4900144b44b1SLen Brown } 4901144b44b1SLen Brown return 0; 4902144b44b1SLen Brown } 4903144b44b1SLen Brown 4904fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4905fb5d4327SDasaratharaman Chandramouli { 4906fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4907fb5d4327SDasaratharaman Chandramouli return 0; 4908fb5d4327SDasaratharaman Chandramouli switch (model) { 4909869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4910fb5d4327SDasaratharaman Chandramouli return 1; 4911fb5d4327SDasaratharaman Chandramouli } 4912fb5d4327SDasaratharaman Chandramouli return 0; 4913fb5d4327SDasaratharaman Chandramouli } 4914fb5d4327SDasaratharaman Chandramouli 4915997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4916997e5395SSrinivas Pandruvada { 4917997e5395SSrinivas Pandruvada if (!genuine_intel) 4918997e5395SSrinivas Pandruvada return 0; 4919997e5395SSrinivas Pandruvada 4920997e5395SSrinivas Pandruvada switch (model) { 4921af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4922997e5395SSrinivas Pandruvada return 1; 4923997e5395SSrinivas Pandruvada } 4924997e5395SSrinivas Pandruvada 4925997e5395SSrinivas Pandruvada return 0; 4926997e5395SSrinivas Pandruvada } 4927997e5395SSrinivas Pandruvada 4928b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4929b2b34dfeSHubert Chrzaniuk { 4930b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4931b2b34dfeSHubert Chrzaniuk return 1024; 4932b2b34dfeSHubert Chrzaniuk return 1; 4933b2b34dfeSHubert Chrzaniuk } 4934b2b34dfeSHubert Chrzaniuk 4935144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4936144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 }; 4937144b44b1SLen Brown 4938144b44b1SLen Brown double slm_bclk(void) 4939144b44b1SLen Brown { 4940144b44b1SLen Brown unsigned long long msr = 3; 4941144b44b1SLen Brown unsigned int i; 4942144b44b1SLen Brown double freq; 4943144b44b1SLen Brown 49447ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4945b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4946144b44b1SLen Brown 4947144b44b1SLen Brown i = msr & 0xf; 4948144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4949b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 49500a91e551SColin Ian King i = 3; 4951144b44b1SLen Brown } 4952144b44b1SLen Brown freq = slm_freq_table[i]; 4953144b44b1SLen Brown 495496e47158SLen Brown if (!quiet) 4955b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4956144b44b1SLen Brown 4957144b44b1SLen Brown return freq; 4958144b44b1SLen Brown } 4959144b44b1SLen Brown 4960103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4961103a8feaSLen Brown { 4962121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4963103a8feaSLen Brown return 100.00; 4964144b44b1SLen Brown else if (is_slm(family, model)) 4965144b44b1SLen Brown return slm_bclk(); 4966103a8feaSLen Brown else 4967103a8feaSLen Brown return 133.33; 4968103a8feaSLen Brown } 4969103a8feaSLen Brown 49707ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p) 49717ab5ff49SZhang Rui { 49727ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx; 49737ab5ff49SZhang Rui 49747ab5ff49SZhang Rui if (!genuine_intel) 49757ab5ff49SZhang Rui return 0; 49767ab5ff49SZhang Rui 49777ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) { 49787ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 49797ab5ff49SZhang Rui return -1; 49807ab5ff49SZhang Rui } 49817ab5ff49SZhang Rui 49827ab5ff49SZhang Rui if (max_level < 0x1a) 49837ab5ff49SZhang Rui return 0; 49847ab5ff49SZhang Rui 49857ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx); 49867ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF; 49877ab5ff49SZhang Rui if (eax == 0x20) 49887ab5ff49SZhang Rui t->is_atom = true; 49897ab5ff49SZhang Rui return 0; 49907ab5ff49SZhang Rui } 49917ab5ff49SZhang Rui 4992889facbeSLen Brown /* 4993889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4994889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4995889facbeSLen Brown * This is usually equal to tjMax. 4996889facbeSLen Brown * 4997889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4998889facbeSLen Brown * but also allow cmdline over-ride with -T. 4999889facbeSLen Brown * 5000889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 5001889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 5002889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 5003889facbeSLen Brown */ 50046ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 50056ff7cb37SLen Brown { 5006b2b94be7SLen Brown unsigned long long msr; 500755279aefSZhang Rui unsigned int tcc_default, tcc_offset; 5008b2b94be7SLen Brown int cpu; 5009b2b94be7SLen Brown 501055279aefSZhang Rui /* tj_max is used only for dts or ptm */ 5011889facbeSLen Brown if (!(do_dts || do_ptm)) 5012889facbeSLen Brown return 0; 5013889facbeSLen Brown 5014889facbeSLen Brown /* this is a per-package concept */ 5015889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 5016889facbeSLen Brown return 0; 5017889facbeSLen Brown 5018b2b94be7SLen Brown cpu = t->cpu_id; 5019b2b94be7SLen Brown if (cpu_migrate(cpu)) { 5020b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 5021b2b94be7SLen Brown return -1; 5022b2b94be7SLen Brown } 5023b2b94be7SLen Brown 502455279aefSZhang Rui if (tj_max_override != 0) { 502555279aefSZhang Rui tj_max = tj_max_override; 50261b439f01SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max); 5027889facbeSLen Brown return 0; 5028889facbeSLen Brown } 5029889facbeSLen Brown 5030b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 5031b2b94be7SLen Brown if (!do_nhm_platform_info) 5032b2b94be7SLen Brown goto guess; 5033b2b94be7SLen Brown 5034b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 5035b2b94be7SLen Brown goto guess; 5036b2b94be7SLen Brown 503755279aefSZhang Rui tcc_default = (msr >> 16) & 0xFF; 5038b2b94be7SLen Brown 50390b9a0b9bSZhang Rui if (!quiet) { 50400b9a0b9bSZhang Rui switch (tcc_offset_bits) { 50410b9a0b9bSZhang Rui case 4: 50420b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0xF; 50430b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 504455279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 50450b9a0b9bSZhang Rui break; 50460b9a0b9bSZhang Rui case 6: 50470b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0x3F; 50480b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 504955279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 50500b9a0b9bSZhang Rui break; 50510b9a0b9bSZhang Rui default: 50521b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default); 50530b9a0b9bSZhang Rui break; 50540b9a0b9bSZhang Rui } 50550b9a0b9bSZhang Rui } 5056b2b94be7SLen Brown 505755279aefSZhang Rui if (!tcc_default) 5058b2b94be7SLen Brown goto guess; 5059b2b94be7SLen Brown 506055279aefSZhang Rui tj_max = tcc_default; 5061b2b94be7SLen Brown 5062889facbeSLen Brown return 0; 5063889facbeSLen Brown 5064b2b94be7SLen Brown guess: 506555279aefSZhang Rui tj_max = TJMAX_DEFAULT; 50661b439f01SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max); 5067889facbeSLen Brown 5068889facbeSLen Brown return 0; 5069889facbeSLen Brown } 507069807a63SLen Brown 5071aa8d8cc7SLen Brown void decode_feature_control_msr(void) 5072aa8d8cc7SLen Brown { 5073aa8d8cc7SLen Brown unsigned long long msr; 5074aa8d8cc7SLen Brown 5075f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 5076aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 50771b439f01SLen Brown base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : ""); 5078aa8d8cc7SLen Brown } 5079aa8d8cc7SLen Brown 508069807a63SLen Brown void decode_misc_enable_msr(void) 508169807a63SLen Brown { 508269807a63SLen Brown unsigned long long msr; 508369807a63SLen Brown 5084f26b1519SLen Brown if (!genuine_intel) 5085f26b1519SLen Brown return; 5086f26b1519SLen Brown 508769807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 5088e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 508969807a63SLen Brown base_cpu, msr, 5090e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 5091e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 5092fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 5093e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 5094e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 509569807a63SLen Brown } 509669807a63SLen Brown 509733148d67SLen Brown void decode_misc_feature_control(void) 509833148d67SLen Brown { 509933148d67SLen Brown unsigned long long msr; 510033148d67SLen Brown 510133148d67SLen Brown if (!has_misc_feature_control) 510233148d67SLen Brown return; 510333148d67SLen Brown 510433148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 51051b439f01SLen Brown fprintf(outf, 51061b439f01SLen Brown "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 51071b439f01SLen Brown base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "", 51081b439f01SLen Brown msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : ""); 510933148d67SLen Brown } 51101b439f01SLen Brown 5111f0057310SLen Brown /* 5112f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 5113f0057310SLen Brown * 5114f0057310SLen Brown * Decode the bits according to the Nehalem documentation 5115f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 5116f0057310SLen Brown * bit[1] less so... 5117f0057310SLen Brown */ 5118f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 5119f0057310SLen Brown { 5120f0057310SLen Brown unsigned long long msr; 5121f0057310SLen Brown 5122f0057310SLen Brown if (!do_nhm_platform_info) 5123f0057310SLen Brown return; 5124f0057310SLen Brown 5125cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 5126cf4cbe53SLen Brown return; 5127cf4cbe53SLen Brown 5128f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 5129ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 5130f0057310SLen Brown base_cpu, msr, 51311b439f01SLen Brown msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS"); 5132f0057310SLen Brown } 51331b439f01SLen Brown 513471616c8eSLen Brown /* 513571616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 513671616c8eSLen Brown * 513771616c8eSLen Brown * This MSRs are present on Silvermont processors, 513871616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 513971616c8eSLen Brown */ 514071616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 514171616c8eSLen Brown { 514271616c8eSLen Brown unsigned long long msr; 514371616c8eSLen Brown 514471616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 514571616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 514671616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 514771616c8eSLen Brown 514871616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 514971616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 515071616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 515171616c8eSLen Brown } 51527f5c258eSLen Brown 5153f5a4c76aSLen Brown /* 5154f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 5155f5a4c76aSLen Brown */ 5156f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 5157f5a4c76aSLen Brown { 5158f5a4c76aSLen Brown 5159f5a4c76aSLen Brown switch (model) { 5160f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 5161f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 5162f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 5163f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 5164f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 5165f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 5166f5a4c76aSLen Brown 5167f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 5168f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 5169f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 5170f5a4c76aSLen Brown 5171f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 5172f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 5173f5a4c76aSLen Brown 5174f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 51755ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 5176f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 5177f5a4c76aSLen Brown 5178af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 5179c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 5180af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 5181c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 5182081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 5183081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 5184af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 5185937807d3SLen Brown 5186af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 5187d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 51884bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 51894bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 5190e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 5191e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 5192e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 51935683460bSChen Yu case INTEL_FAM6_ALDERLAKE_L: 5194af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 5195b62b3184SZhang Rui 5196d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 5197d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 5198d7814c30SChen Yu 51996c5c6560SChen Yu case INTEL_FAM6_ICELAKE_D: 5200e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 520125368d7cSChen Yu return INTEL_FAM6_ICELAKE_X; 5202f5a4c76aSLen Brown } 5203f5a4c76aSLen Brown return model; 5204f5a4c76aSLen Brown } 5205d76bb7a0SLen Brown 5206d76bb7a0SLen Brown void print_dev_latency(void) 5207d76bb7a0SLen Brown { 5208d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5209d76bb7a0SLen Brown int fd; 5210d76bb7a0SLen Brown int value; 5211d76bb7a0SLen Brown int retval; 5212d76bb7a0SLen Brown 5213d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5214d76bb7a0SLen Brown if (fd < 0) { 5215d76bb7a0SLen Brown warn("fopen %s\n", path); 5216d76bb7a0SLen Brown return; 5217d76bb7a0SLen Brown } 5218d76bb7a0SLen Brown 5219d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5220d76bb7a0SLen Brown if (retval != sizeof(int)) { 5221d76bb7a0SLen Brown warn("read %s\n", path); 5222d76bb7a0SLen Brown close(fd); 5223d76bb7a0SLen Brown return; 5224d76bb7a0SLen Brown } 52251b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 5226d76bb7a0SLen Brown 5227d76bb7a0SLen Brown close(fd); 5228d76bb7a0SLen Brown } 5229d76bb7a0SLen Brown 52302af4f9b8SLen Brown /* 52312af4f9b8SLen Brown * Linux-perf manages the the HW instructions-retired counter 52322af4f9b8SLen Brown * by enabling when requested, and hiding rollover 52332af4f9b8SLen Brown */ 52342af4f9b8SLen Brown void linux_perf_init(void) 52352af4f9b8SLen Brown { 52362af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 52372af4f9b8SLen Brown return; 52382af4f9b8SLen Brown 52392af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 52402af4f9b8SLen Brown return; 52412af4f9b8SLen Brown 52422af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 52432af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 52442af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 52452af4f9b8SLen Brown 52462af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 52472af4f9b8SLen Brown } 52482af4f9b8SLen Brown 5249fcd17211SLen Brown void process_cpuid() 5250103a8feaSLen Brown { 525134041551SLen Brown unsigned int eax, ebx, ecx, edx; 525234041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5253b3a34e93SLen Brown unsigned int has_turbo; 5254ed0757b8SLen Brown unsigned long long ucode_patch = 0; 5255103a8feaSLen Brown 5256103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5257103a8feaSLen Brown 52585aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5259103a8feaSLen Brown 526034041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5261103a8feaSLen Brown genuine_intel = 1; 526234041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 526334041551SLen Brown authentic_amd = 1; 5264c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5265c1c10cc7SPu Wen hygon_genuine = 1; 5266103a8feaSLen Brown 526796e47158SLen Brown if (!quiet) 5268ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5269ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5270103a8feaSLen Brown 52715aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5272103a8feaSLen Brown family = (fms >> 8) & 0xf; 5273103a8feaSLen Brown model = (fms >> 4) & 0xf; 5274103a8feaSLen Brown stepping = fms & 0xf; 52755aa3d1a2SCalvin Walton if (family == 0xf) 52765aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 52775aa3d1a2SCalvin Walton if (family >= 6) 5278103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 527934041551SLen Brown ecx_flags = ecx; 528034041551SLen Brown edx_flags = edx; 5281103a8feaSLen Brown 5282ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 5283ed0757b8SLen Brown warnx("get_msr(UCODE)\n"); 5284ed0757b8SLen Brown 5285103a8feaSLen Brown /* 5286103a8feaSLen Brown * check max extended function levels of CPUID. 5287103a8feaSLen Brown * This is needed to check for invariant TSC. 5288103a8feaSLen Brown * This check is valid for both Intel and AMD. 5289103a8feaSLen Brown */ 5290103a8feaSLen Brown ebx = ecx = edx = 0; 52915aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5292103a8feaSLen Brown 529334041551SLen Brown if (!quiet) { 5294ed0757b8SLen Brown fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n", 52951b439f01SLen Brown family, model, stepping, family, model, stepping, 52961b439f01SLen Brown (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 5297ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 529834041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 529934041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 530034041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 530134041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 530234041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 530334041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 530434041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 530534041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 530634041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 53071b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-"); 530834041551SLen Brown } 530925368d7cSChen Yu if (genuine_intel) { 5310e9d3092fSZhang Rui model_orig = model; 5311f5a4c76aSLen Brown model = intel_model_duplicates(model); 531225368d7cSChen Yu } 531334041551SLen Brown 531434041551SLen Brown if (!(edx_flags & (1 << 5))) 531534041551SLen Brown errx(1, "CPUID: no MSR"); 531634041551SLen Brown 531761a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5318103a8feaSLen Brown 5319103a8feaSLen Brown /* 5320103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5321103a8feaSLen Brown * this check is valid for both Intel and AMD 5322103a8feaSLen Brown */ 53235aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 53248209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5325d7899447SLen Brown } 5326103a8feaSLen Brown 5327103a8feaSLen Brown /* 5328103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5329103a8feaSLen Brown * this check is valid for both Intel and AMD 5330103a8feaSLen Brown */ 5331103a8feaSLen Brown 53325aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 53338209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5334812db3f7SLen Brown if (has_aperf) { 5335812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5336812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5337812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5338812db3f7SLen Brown } 5339889facbeSLen Brown do_dts = eax & (1 << 0); 5340812db3f7SLen Brown if (do_dts) 5341812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5342b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5343889facbeSLen Brown do_ptm = eax & (1 << 6); 5344812db3f7SLen Brown if (do_ptm) 5345812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 53467f5c258eSLen Brown has_hwp = eax & (1 << 7); 53477f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 53487f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 53497f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 53507f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5351889facbeSLen Brown has_epb = ecx & (1 << 3); 5352889facbeSLen Brown 535396e47158SLen Brown if (!quiet) 5354b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 53557f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 53567f5c258eSLen Brown has_aperf ? "" : "No-", 5357b3a34e93SLen Brown has_turbo ? "" : "No-", 53587f5c258eSLen Brown do_dts ? "" : "No-", 53597f5c258eSLen Brown do_ptm ? "" : "No-", 53607f5c258eSLen Brown has_hwp ? "" : "No-", 53617f5c258eSLen Brown has_hwp_notify ? "" : "No-", 53627f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 53631b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 5364103a8feaSLen Brown 536596e47158SLen Brown if (!quiet) 536669807a63SLen Brown decode_misc_enable_msr(); 536769807a63SLen Brown 536896e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5369aa8d8cc7SLen Brown int has_sgx; 5370aa8d8cc7SLen Brown 5371aa8d8cc7SLen Brown ecx = 0; 5372aa8d8cc7SLen Brown 5373aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5374aa8d8cc7SLen Brown 5375aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5376aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5377aa8d8cc7SLen Brown 5378aa8d8cc7SLen Brown if (has_sgx) 5379aa8d8cc7SLen Brown decode_feature_control_msr(); 5380aa8d8cc7SLen Brown } 5381aa8d8cc7SLen Brown 538261a87ba7SLen Brown if (max_level >= 0x15) { 53838a5bdf41SLen Brown unsigned int eax_crystal; 53848a5bdf41SLen Brown unsigned int ebx_tsc; 53858a5bdf41SLen Brown 53868a5bdf41SLen Brown /* 53878a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 53888a5bdf41SLen Brown */ 53898a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 53905aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 53918a5bdf41SLen Brown 53928a5bdf41SLen Brown if (ebx_tsc != 0) { 53938a5bdf41SLen Brown 539496e47158SLen Brown if (!quiet && (ebx != 0)) 5395b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 53968a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 53978a5bdf41SLen Brown 53988a5bdf41SLen Brown if (crystal_hz == 0) 53998a5bdf41SLen Brown switch (model) { 5400af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5401e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5402e8efbc80SLen Brown break; 54035ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5404ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5405ec53e594SLen Brown break; 5406869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5407f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5408e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 54098a5bdf41SLen Brown break; 54108a5bdf41SLen Brown default: 54118a5bdf41SLen Brown crystal_hz = 0; 54128a5bdf41SLen Brown } 54138a5bdf41SLen Brown 54148a5bdf41SLen Brown if (crystal_hz) { 54158a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 541696e47158SLen Brown if (!quiet) 5417b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 54188a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 54198a5bdf41SLen Brown } 54208a5bdf41SLen Brown } 54218a5bdf41SLen Brown } 542261a87ba7SLen Brown if (max_level >= 0x16) { 542361a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 542461a87ba7SLen Brown 542561a87ba7SLen Brown /* 542661a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 542761a87ba7SLen Brown */ 542861a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 542961a87ba7SLen Brown 54305aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 543196e47158SLen Brown if (!quiet) 5432b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 543361a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 543461a87ba7SLen Brown } 54358a5bdf41SLen Brown 5436b2b34dfeSHubert Chrzaniuk if (has_aperf) 5437b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5438b2b34dfeSHubert Chrzaniuk 5439812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5440812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5441812db3f7SLen Brown 5442812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5443812db3f7SLen Brown do_nhm_platform_info = 1; 5444812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5445812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5446812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5447812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5448812db3f7SLen Brown } 5449d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5450812db3f7SLen Brown 5451812db3f7SLen Brown if (do_snb_cstates) 5452812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5453812db3f7SLen Brown 54545a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 54550f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 54560f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 54570f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 54580f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 54590f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 54600f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54610f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 54620f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 54630539ba11SLen Brown if (has_slv_msrs(family, model)) { 54640f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 54650f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 54660f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54670f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 54680539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 54690539ba11SLen Brown use_c1_residency_msr = 1; 54700539ba11SLen Brown } 547120de0dabSAntti Laakso if (is_jvl(family, model)) { 547220de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 547320de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 547420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 547520de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 547620de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 547720de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 547820de0dabSAntti Laakso } 54797170a374SLen Brown if (is_dnv(family, model)) { 54807170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 54817170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 54827170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 54837170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 54847170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 54857170a374SLen Brown use_c1_residency_msr = 1; 54867170a374SLen Brown } 548725368d7cSChen Yu if (is_skx(family, model) || is_icx(family, model)) { 548834c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 548934c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 549034c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 549134c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 549234c76197SLen Brown } 5493ade0ebacSLen Brown if (is_bdx(family, model)) { 5494ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5495ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5496ade0ebacSLen Brown } 5497570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5498c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 54990f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5500c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 55010f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5502c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 55030f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 55040f47c08dSLen Brown } 5505570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5506a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5507a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5508a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5509a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5510a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5511a99d8730SLen Brown } 5512144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5513fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5514103a8feaSLen Brown 55151b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model)) 5516562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5517103a8feaSLen Brown 551896e47158SLen Brown if (!quiet) 5519f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5520f0057310SLen Brown 552196e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 552271616c8eSLen Brown decode_c6_demotion_policy_msr(); 552371616c8eSLen Brown 5524889facbeSLen Brown rapl_probe(family, model); 55253a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5526ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5527889facbeSLen Brown 55280b9a0b9bSZhang Rui check_tcc_offset(model_orig); 55290b9a0b9bSZhang Rui 553096e47158SLen Brown if (!quiet) 55311b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5532fcd17211SLen Brown 553341618e63SLen Brown if (!quiet) 5534d76bb7a0SLen Brown print_dev_latency(); 5535d76bb7a0SLen Brown if (!quiet) 553641618e63SLen Brown dump_sysfs_cstate_config(); 55377293fccdSLen Brown if (!quiet) 55387293fccdSLen Brown dump_sysfs_pstate_config(); 553941618e63SLen Brown 55408c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5541a2b7b749SLen Brown calculate_tsc_tweak(); 5542a2b7b749SLen Brown 5543812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5544812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5545fdf676e5SLen Brown 5546812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5547812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 554827d47356SLen Brown 5549b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5550b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5551b4b91569SRafael Antognolli 5552be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5553be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5554be0e54c4SLen Brown else 5555be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5556be0e54c4SLen Brown 55571f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 55581f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5559be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 55601f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 55611f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 55621f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 55631f81c5efSLen Brown } else { 55641f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5565be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 55661f81c5efSLen Brown } 5567be0e54c4SLen Brown 556896e47158SLen Brown if (!quiet) 556933148d67SLen Brown decode_misc_feature_control(); 557033148d67SLen Brown 5571889facbeSLen Brown return; 5572103a8feaSLen Brown } 5573103a8feaSLen Brown 5574103a8feaSLen Brown /* 5575103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5576103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5577103a8feaSLen Brown */ 5578103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5579103a8feaSLen Brown { 5580103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5581103a8feaSLen Brown return 1; 5582103a8feaSLen Brown else 5583103a8feaSLen Brown return 0; 5584103a8feaSLen Brown } 5585103a8feaSLen Brown 5586103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5587103a8feaSLen Brown { 5588103a8feaSLen Brown return 0; 5589103a8feaSLen Brown } 5590103a8feaSLen Brown 5591c98d5d94SLen Brown void topology_probe() 5592c98d5d94SLen Brown { 5593c98d5d94SLen Brown int i; 5594c98d5d94SLen Brown int max_core_id = 0; 5595c98d5d94SLen Brown int max_package_id = 0; 55966de68fe1SLen Brown int max_die_id = 0; 5597c98d5d94SLen Brown int max_siblings = 0; 5598c98d5d94SLen Brown 5599c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5600843c5791SPrarit Bhargava set_max_cpu_num(); 5601c98d5d94SLen Brown topo.num_cpus = 0; 5602c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5603c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5604812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5605c98d5d94SLen Brown 5606d8af6f5fSLen Brown if (debug > 1) 5607b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5608c98d5d94SLen Brown 5609c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5610b2c95d90SJosh Triplett if (cpus == NULL) 5611b2c95d90SJosh Triplett err(1, "calloc cpus"); 5612c98d5d94SLen Brown 5613c98d5d94SLen Brown /* 5614c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5615c98d5d94SLen Brown */ 5616c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5617b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5618b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5619c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5620c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5621c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5622c98d5d94SLen Brown 5623c98d5d94SLen Brown /* 56241ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 56251ef7d21aSLen Brown */ 56261ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 56271ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 56281ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 56291ef7d21aSLen Brown err(1, "cpu%d not present", i); 56301ef7d21aSLen Brown } 56311ef7d21aSLen Brown 56321ef7d21aSLen Brown /* 5633c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5634c98d5d94SLen Brown */ 5635c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5636b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5637b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5638c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5639c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5640c98d5d94SLen Brown 56418cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5642c98d5d94SLen Brown 5643c98d5d94SLen Brown /* 5644c98d5d94SLen Brown * For online cpus 5645c98d5d94SLen Brown * find max_core_id, max_package_id 5646c98d5d94SLen Brown */ 5647c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5648c98d5d94SLen Brown int siblings; 5649c98d5d94SLen Brown 5650c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5651d8af6f5fSLen Brown if (debug > 1) 5652b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5653c98d5d94SLen Brown continue; 5654c98d5d94SLen Brown } 5655c98d5d94SLen Brown 56560e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 56570e2d8f05SLen Brown 56580e2d8f05SLen Brown /* get package information */ 5659c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5660c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5661c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5662c98d5d94SLen Brown 56636de68fe1SLen Brown /* get die information */ 56646de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 56656de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 56666de68fe1SLen Brown max_die_id = cpus[i].die_id; 56676de68fe1SLen Brown 56680e2d8f05SLen Brown /* get numa node information */ 5669ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5670ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5671ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 56720e2d8f05SLen Brown 56730e2d8f05SLen Brown /* get core information */ 56740e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 56750e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 56760e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 56770e2d8f05SLen Brown 56780e2d8f05SLen Brown /* get thread information */ 56790e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5680c98d5d94SLen Brown if (siblings > max_siblings) 5681c98d5d94SLen Brown max_siblings = siblings; 56824f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 56838cb48b32SPrarit Bhargava topo.num_cores++; 5684c98d5d94SLen Brown } 5685ef605741SPrarit Bhargava 568670a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5687d8af6f5fSLen Brown if (debug > 1) 56881b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node); 568970a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5690812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5691c98d5d94SLen Brown 56926de68fe1SLen Brown topo.num_die = max_die_id + 1; 56936de68fe1SLen Brown if (debug > 1) 56941b439f01SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die); 56956de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 56966de68fe1SLen Brown BIC_PRESENT(BIC_Die); 56976de68fe1SLen Brown 5698c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5699d8af6f5fSLen Brown if (debug > 1) 57001b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 57017da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5702812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5703c98d5d94SLen Brown 5704ef605741SPrarit Bhargava set_node_data(); 5705ef605741SPrarit Bhargava if (debug > 1) 570670a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 570701235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 570801235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5709ef605741SPrarit Bhargava 571070a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5711d8af6f5fSLen Brown if (debug > 1) 5712b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 57132ffbb224SPrarit Bhargava 57142ffbb224SPrarit Bhargava if (debug < 1) 57152ffbb224SPrarit Bhargava return; 57162ffbb224SPrarit Bhargava 57172ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 57180ec712e3SLen Brown if (cpu_is_not_present(i)) 57190ec712e3SLen Brown continue; 57202ffbb224SPrarit Bhargava fprintf(outf, 57216de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 57226de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 57231b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id); 57242ffbb224SPrarit Bhargava } 57252ffbb224SPrarit Bhargava 5726c98d5d94SLen Brown } 5727c98d5d94SLen Brown 57281b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 5729c98d5d94SLen Brown { 5730c98d5d94SLen Brown int i; 57311b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; 573240f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5733c98d5d94SLen Brown 573440f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5735c98d5d94SLen Brown if (*t == NULL) 5736c98d5d94SLen Brown goto error; 5737c98d5d94SLen Brown 573840f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5739c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5740c98d5d94SLen Brown 574140f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5742c98d5d94SLen Brown if (*c == NULL) 5743c98d5d94SLen Brown goto error; 5744c98d5d94SLen Brown 574540f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5746c98d5d94SLen Brown (*c)[i].core_id = -1; 5747c98d5d94SLen Brown 5748678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5749c98d5d94SLen Brown if (*p == NULL) 5750c98d5d94SLen Brown goto error; 5751c98d5d94SLen Brown 5752c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5753c98d5d94SLen Brown (*p)[i].package_id = i; 5754c98d5d94SLen Brown 5755c98d5d94SLen Brown return; 5756c98d5d94SLen Brown error: 5757b2c95d90SJosh Triplett err(1, "calloc counters"); 5758c98d5d94SLen Brown } 57591b439f01SLen Brown 5760c98d5d94SLen Brown /* 5761c98d5d94SLen Brown * init_counter() 5762c98d5d94SLen Brown * 5763c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5764c98d5d94SLen Brown */ 57651b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 5766c98d5d94SLen Brown { 57678cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 576840f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 57698cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 57708cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5771c98d5d94SLen Brown struct thread_data *t; 5772c98d5d94SLen Brown struct core_data *c; 5773c98d5d94SLen Brown struct pkg_data *p; 5774c98d5d94SLen Brown 577542dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 577642dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 577742dd4520SNathan Ciobanu */ 577842dd4520SNathan Ciobanu if (node_id < 0) 577942dd4520SNathan Ciobanu node_id = 0; 578042dd4520SNathan Ciobanu 578140f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 578240f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 57838cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5784c98d5d94SLen Brown 5785c98d5d94SLen Brown t->cpu_id = cpu_id; 57868cb48b32SPrarit Bhargava if (thread_id == 0) { 5787c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5788c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5789c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5790c98d5d94SLen Brown } 5791c98d5d94SLen Brown 57928cb48b32SPrarit Bhargava c->core_id = core_id; 57938cb48b32SPrarit Bhargava p->package_id = pkg_id; 5794c98d5d94SLen Brown } 5795c98d5d94SLen Brown 5796c98d5d94SLen Brown int initialize_counters(int cpu_id) 5797c98d5d94SLen Brown { 57988cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 57998cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5800c98d5d94SLen Brown return 0; 5801c98d5d94SLen Brown } 5802c98d5d94SLen Brown 5803c98d5d94SLen Brown void allocate_output_buffer() 5804c98d5d94SLen Brown { 5805eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5806c98d5d94SLen Brown outp = output_buffer; 5807b2c95d90SJosh Triplett if (outp == NULL) 5808b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5809c98d5d94SLen Brown } 58101b439f01SLen Brown 581136229897SLen Brown void allocate_fd_percpu(void) 581236229897SLen Brown { 581301a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 581436229897SLen Brown if (fd_percpu == NULL) 581536229897SLen Brown err(-1, "calloc fd_percpu"); 581636229897SLen Brown } 58171b439f01SLen Brown 5818562a2d37SLen Brown void allocate_irq_buffers(void) 5819562a2d37SLen Brown { 5820562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5821562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5822562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5823c98d5d94SLen Brown 582401a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5825562a2d37SLen Brown if (irqs_per_cpu == NULL) 582601a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5827562a2d37SLen Brown } 58281b439f01SLen Brown 5829c98d5d94SLen Brown void setup_all_buffers(void) 5830c98d5d94SLen Brown { 5831c98d5d94SLen Brown topology_probe(); 5832562a2d37SLen Brown allocate_irq_buffers(); 583336229897SLen Brown allocate_fd_percpu(); 5834c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5835c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5836c98d5d94SLen Brown allocate_output_buffer(); 5837c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5838c98d5d94SLen Brown } 58393b4d5c7fSAndy Shevchenko 58407ce7d5deSPrarit Bhargava void set_base_cpu(void) 58417ce7d5deSPrarit Bhargava { 58427ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 58437ce7d5deSPrarit Bhargava if (base_cpu < 0) 58447ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 58457ce7d5deSPrarit Bhargava 58467ce7d5deSPrarit Bhargava if (debug > 1) 5847b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 58487ce7d5deSPrarit Bhargava } 58497ce7d5deSPrarit Bhargava 5850103a8feaSLen Brown void turbostat_init() 5851103a8feaSLen Brown { 58527ce7d5deSPrarit Bhargava setup_all_buffers(); 58537ce7d5deSPrarit Bhargava set_base_cpu(); 5854103a8feaSLen Brown check_dev_msr(); 585598481e79SLen Brown check_permissions(); 5856fcd17211SLen Brown process_cpuid(); 58572af4f9b8SLen Brown linux_perf_init(); 5858103a8feaSLen Brown 585996e47158SLen Brown if (!quiet) 58607f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 58617f5c258eSLen Brown 586296e47158SLen Brown if (!quiet) 5863889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5864889facbeSLen Brown 586596e47158SLen Brown if (!quiet) 58663a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 58673a9a941dSLen Brown 586896e47158SLen Brown if (!quiet) 5869889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5870889facbeSLen Brown 5871889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5872889facbeSLen Brown 58737ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 58747ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 58757ab5ff49SZhang Rui 587696e47158SLen Brown if (!quiet) 5877889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 58785a63426eSLen Brown 587996e47158SLen Brown if (!quiet && do_irtl_snb) 58805a63426eSLen Brown print_irtl(); 5881103a8feaSLen Brown } 5882103a8feaSLen Brown 5883103a8feaSLen Brown int fork_it(char **argv) 5884103a8feaSLen Brown { 5885103a8feaSLen Brown pid_t child_pid; 5886d91bb17cSLen Brown int status; 5887d15cf7c1SLen Brown 5888218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5889d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 58904c2122d4SLen Brown first_counter_read = 0; 5891d91bb17cSLen Brown if (status) 5892d91bb17cSLen Brown exit(status); 5893d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5894d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5895103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5896103a8feaSLen Brown 5897103a8feaSLen Brown child_pid = fork(); 5898103a8feaSLen Brown if (!child_pid) { 5899103a8feaSLen Brown /* child */ 5900103a8feaSLen Brown execvp(argv[0], argv); 59010815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5902103a8feaSLen Brown } else { 5903103a8feaSLen Brown 5904103a8feaSLen Brown /* parent */ 5905b2c95d90SJosh Triplett if (child_pid == -1) 5906b2c95d90SJosh Triplett err(1, "fork"); 5907103a8feaSLen Brown 5908103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5909103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5910b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5911b2c95d90SJosh Triplett err(status, "waitpid"); 59122a954966SDavid Arcari 59132a954966SDavid Arcari if (WIFEXITED(status)) 59142a954966SDavid Arcari status = WEXITSTATUS(status); 5915103a8feaSLen Brown } 5916c98d5d94SLen Brown /* 5917c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5918c98d5d94SLen Brown * because re-starting is problematic when forking 5919c98d5d94SLen Brown */ 5920218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5921c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5922103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5923103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5924ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5925ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5926ba3dec99SLen Brown else { 5927c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5928c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5929ba3dec99SLen Brown } 5930103a8feaSLen Brown 5931b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 5932b7d8c148SLen Brown 5933b7d8c148SLen Brown flush_output_stderr(); 5934103a8feaSLen Brown 5935d91bb17cSLen Brown return status; 5936103a8feaSLen Brown } 5937103a8feaSLen Brown 59383b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 59393b4d5c7fSAndy Shevchenko { 59403b4d5c7fSAndy Shevchenko int status; 59413b4d5c7fSAndy Shevchenko 5942218f0e8dSLen Brown snapshot_proc_sysfs_files(); 59433b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 59443b4d5c7fSAndy Shevchenko if (status) 59453b4d5c7fSAndy Shevchenko return status; 59463b4d5c7fSAndy Shevchenko 59473b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 59483b4d5c7fSAndy Shevchenko if (status) 59493b4d5c7fSAndy Shevchenko return status; 59503b4d5c7fSAndy Shevchenko 5951b7d8c148SLen Brown flush_output_stdout(); 59523b4d5c7fSAndy Shevchenko 59533b4d5c7fSAndy Shevchenko return status; 59543b4d5c7fSAndy Shevchenko } 59553b4d5c7fSAndy Shevchenko 59561b439f01SLen Brown void print_version() 59571b439f01SLen Brown { 59581b439f01SLen Brown fprintf(outf, "turbostat version 21.03.12" " - Len Brown <lenb@kernel.org>\n"); 5959d8af6f5fSLen Brown } 5960d8af6f5fSLen Brown 5961495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5962495c7654SLen Brown unsigned int width, enum counter_scope scope, 596341618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5964388e9c81SLen Brown { 5965388e9c81SLen Brown struct msr_counter *msrp; 5966388e9c81SLen Brown 5967388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5968388e9c81SLen Brown if (msrp == NULL) { 5969388e9c81SLen Brown perror("calloc"); 5970388e9c81SLen Brown exit(1); 5971388e9c81SLen Brown } 5972388e9c81SLen Brown 5973388e9c81SLen Brown msrp->msr_num = msr_num; 5974d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 5975495c7654SLen Brown if (path) 5976d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 5977388e9c81SLen Brown msrp->width = width; 5978388e9c81SLen Brown msrp->type = type; 5979388e9c81SLen Brown msrp->format = format; 598041618e63SLen Brown msrp->flags = flags; 5981388e9c81SLen Brown 5982388e9c81SLen Brown switch (scope) { 5983388e9c81SLen Brown 5984388e9c81SLen Brown case SCOPE_CPU: 5985388e9c81SLen Brown msrp->next = sys.tp; 5986388e9c81SLen Brown sys.tp = msrp; 5987678a3bd1SLen Brown sys.added_thread_counters++; 59880748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 59891b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS); 5990678a3bd1SLen Brown exit(-1); 5991678a3bd1SLen Brown } 5992388e9c81SLen Brown break; 5993388e9c81SLen Brown 5994388e9c81SLen Brown case SCOPE_CORE: 5995388e9c81SLen Brown msrp->next = sys.cp; 5996388e9c81SLen Brown sys.cp = msrp; 5997678a3bd1SLen Brown sys.added_core_counters++; 5998678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 59991b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS); 6000678a3bd1SLen Brown exit(-1); 6001678a3bd1SLen Brown } 6002388e9c81SLen Brown break; 6003388e9c81SLen Brown 6004388e9c81SLen Brown case SCOPE_PACKAGE: 6005388e9c81SLen Brown msrp->next = sys.pp; 6006388e9c81SLen Brown sys.pp = msrp; 6007678a3bd1SLen Brown sys.added_package_counters++; 6008678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 60091b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS); 6010678a3bd1SLen Brown exit(-1); 6011678a3bd1SLen Brown } 6012388e9c81SLen Brown break; 6013388e9c81SLen Brown } 6014388e9c81SLen Brown 6015388e9c81SLen Brown return 0; 6016388e9c81SLen Brown } 6017388e9c81SLen Brown 6018388e9c81SLen Brown void parse_add_command(char *add_command) 6019388e9c81SLen Brown { 6020388e9c81SLen Brown int msr_num = 0; 6021495c7654SLen Brown char *path = NULL; 60220f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 6023388e9c81SLen Brown int width = 64; 6024388e9c81SLen Brown int fail = 0; 6025388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 6026388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 6027388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 6028388e9c81SLen Brown 6029388e9c81SLen Brown while (add_command) { 6030388e9c81SLen Brown 6031388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 6032388e9c81SLen Brown goto next; 6033388e9c81SLen Brown 6034388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 6035388e9c81SLen Brown goto next; 6036388e9c81SLen Brown 6037495c7654SLen Brown if (*add_command == '/') { 6038495c7654SLen Brown path = add_command; 6039495c7654SLen Brown goto next; 6040495c7654SLen Brown } 6041495c7654SLen Brown 6042388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 6043388e9c81SLen Brown if ((width == 32) || (width == 64)) 6044388e9c81SLen Brown goto next; 6045388e9c81SLen Brown width = 64; 6046388e9c81SLen Brown } 6047388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6048388e9c81SLen Brown scope = SCOPE_CPU; 6049388e9c81SLen Brown goto next; 6050388e9c81SLen Brown } 6051388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6052388e9c81SLen Brown scope = SCOPE_CORE; 6053388e9c81SLen Brown goto next; 6054388e9c81SLen Brown } 6055388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6056388e9c81SLen Brown scope = SCOPE_PACKAGE; 6057388e9c81SLen Brown goto next; 6058388e9c81SLen Brown } 6059388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6060388e9c81SLen Brown type = COUNTER_CYCLES; 6061388e9c81SLen Brown goto next; 6062388e9c81SLen Brown } 6063388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6064388e9c81SLen Brown type = COUNTER_SECONDS; 6065388e9c81SLen Brown goto next; 6066388e9c81SLen Brown } 606741618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 606841618e63SLen Brown type = COUNTER_USEC; 606941618e63SLen Brown goto next; 607041618e63SLen Brown } 6071388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6072388e9c81SLen Brown format = FORMAT_RAW; 6073388e9c81SLen Brown goto next; 6074388e9c81SLen Brown } 6075388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6076388e9c81SLen Brown format = FORMAT_DELTA; 6077388e9c81SLen Brown goto next; 6078388e9c81SLen Brown } 6079388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6080388e9c81SLen Brown format = FORMAT_PERCENT; 6081388e9c81SLen Brown goto next; 6082388e9c81SLen Brown } 6083388e9c81SLen Brown 6084388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6085388e9c81SLen Brown char *eos; 6086388e9c81SLen Brown 6087388e9c81SLen Brown eos = strchr(name_buffer, ','); 6088388e9c81SLen Brown if (eos) 6089388e9c81SLen Brown *eos = '\0'; 6090388e9c81SLen Brown goto next; 6091388e9c81SLen Brown } 6092388e9c81SLen Brown 6093388e9c81SLen Brown next: 6094388e9c81SLen Brown add_command = strchr(add_command, ','); 6095495c7654SLen Brown if (add_command) { 6096495c7654SLen Brown *add_command = '\0'; 6097388e9c81SLen Brown add_command++; 6098495c7654SLen Brown } 6099388e9c81SLen Brown 6100388e9c81SLen Brown } 6101495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6102495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6103388e9c81SLen Brown fail++; 6104388e9c81SLen Brown } 6105388e9c81SLen Brown 6106388e9c81SLen Brown /* generate default column header */ 6107388e9c81SLen Brown if (*name_buffer == '\0') { 6108388e9c81SLen Brown if (width == 32) 61095f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6110388e9c81SLen Brown else 61115f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6112388e9c81SLen Brown } 6113388e9c81SLen Brown 611441618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6115388e9c81SLen Brown fail++; 6116388e9c81SLen Brown 6117388e9c81SLen Brown if (fail) { 6118388e9c81SLen Brown help(); 6119388e9c81SLen Brown exit(1); 6120388e9c81SLen Brown } 6121388e9c81SLen Brown } 612241618e63SLen Brown 6123dd778a5eSLen Brown int is_deferred_skip(char *name) 6124dd778a5eSLen Brown { 6125dd778a5eSLen Brown int i; 6126dd778a5eSLen Brown 6127dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6128dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6129dd778a5eSLen Brown return 1; 6130dd778a5eSLen Brown return 0; 6131dd778a5eSLen Brown } 6132dd778a5eSLen Brown 613341618e63SLen Brown void probe_sysfs(void) 613441618e63SLen Brown { 613541618e63SLen Brown char path[64]; 613641618e63SLen Brown char name_buf[16]; 613741618e63SLen Brown FILE *input; 613841618e63SLen Brown int state; 613941618e63SLen Brown char *sp; 614041618e63SLen Brown 614141618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 614241618e63SLen Brown return; 614341618e63SLen Brown 61440748eaf0SLen Brown for (state = 10; state >= 0; --state) { 614541618e63SLen Brown 61461b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 614741618e63SLen Brown input = fopen(path, "r"); 614841618e63SLen Brown if (input == NULL) 614941618e63SLen Brown continue; 61508173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 61518173c336SBen Hutchings err(1, "%s: failed to read file", path); 615241618e63SLen Brown 615341618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 615441618e63SLen Brown sp = strchr(name_buf, '-'); 615541618e63SLen Brown if (!sp) 615641618e63SLen Brown sp = strchrnul(name_buf, '\n'); 615741618e63SLen Brown *sp = '%'; 615841618e63SLen Brown *(sp + 1) = '\0'; 615941618e63SLen Brown 6160fecb3bc8SDavid Arcari remove_underbar(name_buf); 6161fecb3bc8SDavid Arcari 616241618e63SLen Brown fclose(input); 616341618e63SLen Brown 616441618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 616541618e63SLen Brown 6166dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6167dd778a5eSLen Brown continue; 6168dd778a5eSLen Brown 61691b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU); 617041618e63SLen Brown } 617141618e63SLen Brown 61720748eaf0SLen Brown for (state = 10; state >= 0; --state) { 617341618e63SLen Brown 61741b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 617541618e63SLen Brown input = fopen(path, "r"); 617641618e63SLen Brown if (input == NULL) 617741618e63SLen Brown continue; 61788173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 61798173c336SBen Hutchings err(1, "%s: failed to read file", path); 618041618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 618141618e63SLen Brown sp = strchr(name_buf, '-'); 618241618e63SLen Brown if (!sp) 618341618e63SLen Brown sp = strchrnul(name_buf, '\n'); 618441618e63SLen Brown *sp = '\0'; 618541618e63SLen Brown fclose(input); 618641618e63SLen Brown 6187fecb3bc8SDavid Arcari remove_underbar(name_buf); 6188fecb3bc8SDavid Arcari 618941618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 619041618e63SLen Brown 6191dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6192dd778a5eSLen Brown continue; 6193dd778a5eSLen Brown 61941b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU); 619541618e63SLen Brown } 619641618e63SLen Brown 619741618e63SLen Brown } 619841618e63SLen Brown 61991ef7d21aSLen Brown /* 62001ef7d21aSLen Brown * parse cpuset with following syntax 62011ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 62021ef7d21aSLen Brown */ 62031ef7d21aSLen Brown void parse_cpu_command(char *optarg) 62041ef7d21aSLen Brown { 62051ef7d21aSLen Brown unsigned int start, end; 62061ef7d21aSLen Brown char *next; 62071ef7d21aSLen Brown 62084e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 62094e4e1e7cSLen Brown if (cpu_subset) 62104e4e1e7cSLen Brown goto error; 62114e4e1e7cSLen Brown show_core_only++; 62124e4e1e7cSLen Brown return; 62134e4e1e7cSLen Brown } 62144e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 62154e4e1e7cSLen Brown if (cpu_subset) 62164e4e1e7cSLen Brown goto error; 62174e4e1e7cSLen Brown show_pkg_only++; 62184e4e1e7cSLen Brown return; 62194e4e1e7cSLen Brown } 62204e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 62214e4e1e7cSLen Brown goto error; 62224e4e1e7cSLen Brown 62231ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 62241ef7d21aSLen Brown if (cpu_subset == NULL) 62251ef7d21aSLen Brown err(3, "CPU_ALLOC"); 62261ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 62271ef7d21aSLen Brown 62281ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 62291ef7d21aSLen Brown 62301ef7d21aSLen Brown next = optarg; 62311ef7d21aSLen Brown 62321ef7d21aSLen Brown while (next && *next) { 62331ef7d21aSLen Brown 62341ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 62351ef7d21aSLen Brown goto error; 62361ef7d21aSLen Brown 62371ef7d21aSLen Brown start = strtoul(next, &next, 10); 62381ef7d21aSLen Brown 62391ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 62401ef7d21aSLen Brown goto error; 62411ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 62421ef7d21aSLen Brown 62431ef7d21aSLen Brown if (*next == '\0') 62441ef7d21aSLen Brown break; 62451ef7d21aSLen Brown 62461ef7d21aSLen Brown if (*next == ',') { 62471ef7d21aSLen Brown next += 1; 62481ef7d21aSLen Brown continue; 62491ef7d21aSLen Brown } 62501ef7d21aSLen Brown 62511ef7d21aSLen Brown if (*next == '-') { 62521ef7d21aSLen Brown next += 1; /* start range */ 62531ef7d21aSLen Brown } else if (*next == '.') { 62541ef7d21aSLen Brown next += 1; 62551ef7d21aSLen Brown if (*next == '.') 62561ef7d21aSLen Brown next += 1; /* start range */ 62571ef7d21aSLen Brown else 62581ef7d21aSLen Brown goto error; 62591ef7d21aSLen Brown } 62601ef7d21aSLen Brown 62611ef7d21aSLen Brown end = strtoul(next, &next, 10); 62621ef7d21aSLen Brown if (end <= start) 62631ef7d21aSLen Brown goto error; 62641ef7d21aSLen Brown 62651ef7d21aSLen Brown while (++start <= end) { 62661ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 62671ef7d21aSLen Brown goto error; 62681ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 62691ef7d21aSLen Brown } 62701ef7d21aSLen Brown 62711ef7d21aSLen Brown if (*next == ',') 62721ef7d21aSLen Brown next += 1; 62731ef7d21aSLen Brown else if (*next != '\0') 62741ef7d21aSLen Brown goto error; 62751ef7d21aSLen Brown } 62761ef7d21aSLen Brown 62771ef7d21aSLen Brown return; 62781ef7d21aSLen Brown 62791ef7d21aSLen Brown error: 62804e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 62814e4e1e7cSLen Brown help(); 62821ef7d21aSLen Brown exit(-1); 62831ef7d21aSLen Brown } 62841ef7d21aSLen Brown 6285103a8feaSLen Brown void cmdline(int argc, char **argv) 6286103a8feaSLen Brown { 6287103a8feaSLen Brown int opt; 6288d8af6f5fSLen Brown int option_index = 0; 6289d8af6f5fSLen Brown static struct option long_options[] = { 6290388e9c81SLen Brown { "add", required_argument, 0, 'a' }, 62911ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' }, 6292d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' }, 629396e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 62943f44a5c6SLen Brown { "enable", required_argument, 0, 'e' }, 6295d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' }, 62962af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' }, 6297023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' }, 6298d8af6f5fSLen Brown { "help", no_argument, 0, 'h' }, 6299812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 6300d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' }, 6301c8ade361SLen Brown { "list", no_argument, 0, 'l' }, 6302b7d8c148SLen Brown { "out", required_argument, 0, 'o' }, 630396e47158SLen Brown { "quiet", no_argument, 0, 'q' }, 6304812db3f7SLen Brown { "show", required_argument, 0, 's' }, 6305d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' }, 6306d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' }, 6307d8af6f5fSLen Brown { "version", no_argument, 0, 'v' }, 6308d8af6f5fSLen Brown { 0, 0, 0, 0 } 6309d8af6f5fSLen Brown }; 6310103a8feaSLen Brown 6311103a8feaSLen Brown progname = argv[0]; 6312103a8feaSLen Brown 63131b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) { 6314103a8feaSLen Brown switch (opt) { 6315388e9c81SLen Brown case 'a': 6316388e9c81SLen Brown parse_add_command(optarg); 6317388e9c81SLen Brown break; 63181ef7d21aSLen Brown case 'c': 63191ef7d21aSLen Brown parse_cpu_command(optarg); 63201ef7d21aSLen Brown break; 6321d8af6f5fSLen Brown case 'D': 6322d8af6f5fSLen Brown dump_only++; 63238e180f3cSLen Brown break; 63243f44a5c6SLen Brown case 'e': 63253f44a5c6SLen Brown /* --enable specified counter */ 63264c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 63273f44a5c6SLen Brown break; 6328d8af6f5fSLen Brown case 'd': 6329d8af6f5fSLen Brown debug++; 63303f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 63312f32edf1SLen Brown break; 6332812db3f7SLen Brown case 'H': 63333f44a5c6SLen Brown /* 63343f44a5c6SLen Brown * --hide: do not show those specified 63353f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 63363f44a5c6SLen Brown */ 63373f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6338812db3f7SLen Brown break; 6339d8af6f5fSLen Brown case 'h': 6340d8af6f5fSLen Brown default: 6341d8af6f5fSLen Brown help(); 6342d8af6f5fSLen Brown exit(1); 6343d8af6f5fSLen Brown case 'i': 63442a0609c0SLen Brown { 63452a0609c0SLen Brown double interval = strtod(optarg, NULL); 63462a0609c0SLen Brown 63472a0609c0SLen Brown if (interval < 0.001) { 63481b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval); 63492a0609c0SLen Brown exit(2); 63502a0609c0SLen Brown } 63512a0609c0SLen Brown 635247936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6353b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 635447936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 63552a0609c0SLen Brown } 6356889facbeSLen Brown break; 63575c56be9aSDirk Brandewie case 'J': 63585c56be9aSDirk Brandewie rapl_joules++; 63595c56be9aSDirk Brandewie break; 6360c8ade361SLen Brown case 'l': 63613f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6362c8ade361SLen Brown list_header_only++; 6363c8ade361SLen Brown quiet++; 6364c8ade361SLen Brown break; 6365b7d8c148SLen Brown case 'o': 6366b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6367b7d8c148SLen Brown break; 636896e47158SLen Brown case 'q': 636996e47158SLen Brown quiet = 1; 637096e47158SLen Brown break; 6371023fe0acSChen Yu case 'n': 6372023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6373023fe0acSChen Yu 6374023fe0acSChen Yu if (num_iterations <= 0) { 63751b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations); 6376023fe0acSChen Yu exit(2); 6377023fe0acSChen Yu } 6378023fe0acSChen Yu break; 6379812db3f7SLen Brown case 's': 63803f44a5c6SLen Brown /* 63813f44a5c6SLen Brown * --show: show only those specified 63823f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 63833f44a5c6SLen Brown * subsequent invocations can add to it. 63843f44a5c6SLen Brown */ 63853f44a5c6SLen Brown if (shown == 0) 63863f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 63873f44a5c6SLen Brown else 63883f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 63893f44a5c6SLen Brown shown = 1; 6390812db3f7SLen Brown break; 6391d8af6f5fSLen Brown case 'S': 6392d8af6f5fSLen Brown summary_only++; 6393d8af6f5fSLen Brown break; 6394d8af6f5fSLen Brown case 'T': 639555279aefSZhang Rui tj_max_override = atoi(optarg); 6396d8af6f5fSLen Brown break; 6397d8af6f5fSLen Brown case 'v': 6398d8af6f5fSLen Brown print_version(); 6399d8af6f5fSLen Brown exit(0); 6400d8af6f5fSLen Brown break; 6401103a8feaSLen Brown } 6402103a8feaSLen Brown } 6403103a8feaSLen Brown } 6404103a8feaSLen Brown 6405103a8feaSLen Brown int main(int argc, char **argv) 6406103a8feaSLen Brown { 6407b7d8c148SLen Brown outf = stderr; 6408103a8feaSLen Brown cmdline(argc, argv); 6409103a8feaSLen Brown 641096e47158SLen Brown if (!quiet) 6411d8af6f5fSLen Brown print_version(); 6412103a8feaSLen Brown 641341618e63SLen Brown probe_sysfs(); 641441618e63SLen Brown 6415103a8feaSLen Brown turbostat_init(); 6416103a8feaSLen Brown 64173b4d5c7fSAndy Shevchenko /* dump counters and exit */ 64183b4d5c7fSAndy Shevchenko if (dump_only) 64193b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 64203b4d5c7fSAndy Shevchenko 6421c8ade361SLen Brown /* list header and exit */ 6422c8ade361SLen Brown if (list_header_only) { 6423c8ade361SLen Brown print_header(","); 6424c8ade361SLen Brown flush_output_stdout(); 6425c8ade361SLen Brown return 0; 6426c8ade361SLen Brown } 6427c8ade361SLen Brown 64289972d5d8SChen Yu msr_sum_record(); 6429103a8feaSLen Brown /* 6430103a8feaSLen Brown * if any params left, it must be a command to fork 6431103a8feaSLen Brown */ 6432103a8feaSLen Brown if (argc - optind) 6433103a8feaSLen Brown return fork_it(argv + optind); 6434103a8feaSLen Brown else 6435103a8feaSLen Brown turbostat_loop(); 6436103a8feaSLen Brown 6437103a8feaSLen Brown return 0; 6438103a8feaSLen Brown } 6439