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 * 63c070b2aSLen Brown * Copyright (c) 2021 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; 51c7e399f8SZephaniah E. Loss-Cutler-Hull unsigned int header_iterations; 52d8af6f5fSLen Brown unsigned int debug; 5396e47158SLen Brown unsigned int quiet; 543f44a5c6SLen Brown unsigned int shown; 550de6c0dfSLen Brown unsigned int sums_need_wide_columns; 56d8af6f5fSLen Brown unsigned int rapl_joules; 57d8af6f5fSLen Brown unsigned int summary_only; 58c8ade361SLen Brown unsigned int list_header_only; 59d8af6f5fSLen Brown unsigned int dump_only; 60103a8feaSLen Brown unsigned int do_snb_cstates; 61fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 62144b44b1SLen Brown unsigned int do_slm_cstates; 63144b44b1SLen Brown unsigned int use_c1_residency_msr; 64103a8feaSLen Brown unsigned int has_aperf; 65889facbeSLen Brown unsigned int has_epb; 665a63426eSLen Brown unsigned int do_irtl_snb; 675a63426eSLen Brown unsigned int do_irtl_hsw; 68fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 69103a8feaSLen Brown unsigned int genuine_intel; 7034041551SLen Brown unsigned int authentic_amd; 71c1c10cc7SPu Wen unsigned int hygon_genuine; 7234041551SLen Brown unsigned int max_level, max_extended_level; 73103a8feaSLen Brown unsigned int has_invariant_tsc; 74d7899447SLen Brown unsigned int do_nhm_platform_info; 75cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 76b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 77103a8feaSLen Brown double bclk; 78a2b7b749SLen Brown double base_hz; 7921ed5574SLen Brown unsigned int has_base_hz; 80a2b7b749SLen Brown double tsc_tweak = 1.0; 81c98d5d94SLen Brown unsigned int show_pkg_only; 82c98d5d94SLen Brown unsigned int show_core_only; 83c98d5d94SLen Brown char *output_buffer, *outp; 84889facbeSLen Brown unsigned int do_rapl; 85889facbeSLen Brown unsigned int do_dts; 86889facbeSLen Brown unsigned int do_ptm; 872af4f9b8SLen Brown unsigned int do_ipc; 88fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 89be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 90be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 9127d47356SLen Brown unsigned int gfx_cur_mhz; 92b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 9355279aefSZhang Rui unsigned int tj_max; 9455279aefSZhang Rui unsigned int tj_max_override; 950b9a0b9bSZhang Rui int tcc_offset_bits; 9640ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 9740ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 98889facbeSLen Brown double rapl_joule_counter_range; 993a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 100ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 101aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 1023a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1033a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1048a5bdf41SLen Brown unsigned int crystal_hz; 1058a5bdf41SLen Brown unsigned long long tsc_hz; 1067ce7d5deSPrarit Bhargava int base_cpu; 10721ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1087f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1097f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1107f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1117f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1127f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1137f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 11433148d67SLen Brown unsigned int has_misc_feature_control; 1154c2122d4SLen Brown unsigned int first_counter_read = 1; 116c026c236SArtem Bityutskiy int ignore_stdin; 117889facbeSLen Brown 118889facbeSLen Brown #define RAPL_PKG (1 << 0) 119e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 120e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 121e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 122e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 123e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 124e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 125e6f9bb3cSLen Brown 126889facbeSLen Brown #define RAPL_DRAM (1 << 3) 127e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 128e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 129e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 130e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1310b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1320b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 133e6f9bb3cSLen Brown 1349148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 135e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1360b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 137e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 138e6f9bb3cSLen Brown 1390b2bb692SLen Brown #define RAPL_GFX (1 << 8) 140e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 141e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 142e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1439148494cSJacob Pan 1449148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1459148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1469392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1479392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1489392bd98SCalvin Walton * not per-package. */ 1499392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1509392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1519392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1529392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1539148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 154889facbeSLen Brown #define TJMAX_DEFAULT 100 155889facbeSLen Brown 1569392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1579392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1589392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1599392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1609392bd98SCalvin Walton 161889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 162103a8feaSLen Brown 163388e9c81SLen Brown /* 164388e9c81SLen Brown * buffer size used by sscanf() for added column names 165388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 166388e9c81SLen Brown */ 167388e9c81SLen Brown #define NAME_BYTES 20 168495c7654SLen Brown #define PATH_BYTES 128 169388e9c81SLen Brown 170103a8feaSLen Brown int backwards_count; 171103a8feaSLen Brown char *progname; 172103a8feaSLen Brown 1731ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1741ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1751ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1760748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1770748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1780e2d8f05SLen Brown #define BITMASK_SIZE 32 179c98d5d94SLen Brown 180c98d5d94SLen Brown struct thread_data { 181f4fdf2b4SLen Brown struct timeval tv_begin; 182f4fdf2b4SLen Brown struct timeval tv_end; 183d4794f25SYazen Ghannam struct timeval tv_delta; 184c98d5d94SLen Brown unsigned long long tsc; 185c98d5d94SLen Brown unsigned long long aperf; 186c98d5d94SLen Brown unsigned long long mperf; 187144b44b1SLen Brown unsigned long long c1; 1882af4f9b8SLen Brown unsigned long long instr_count; 1890de6c0dfSLen Brown unsigned long long irq_count; 1901ed51011SLen Brown unsigned int smi_count; 191c98d5d94SLen Brown unsigned int cpu_id; 1924c2122d4SLen Brown unsigned int apic_id; 1934c2122d4SLen Brown unsigned int x2apic_id; 194c98d5d94SLen Brown unsigned int flags; 1957ab5ff49SZhang Rui bool is_atom; 196c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 197c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1980748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 199c98d5d94SLen Brown } *thread_even, *thread_odd; 200c98d5d94SLen Brown 201c98d5d94SLen Brown struct core_data { 202c98d5d94SLen Brown unsigned long long c3; 203c98d5d94SLen Brown unsigned long long c6; 204c98d5d94SLen Brown unsigned long long c7; 2050539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 206889facbeSLen Brown unsigned int core_temp_c; 2079392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 208c98d5d94SLen Brown unsigned int core_id; 209eae97e05SChen Yu unsigned long long core_throt_cnt; 210678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 211c98d5d94SLen Brown } *core_even, *core_odd; 212c98d5d94SLen Brown 213c98d5d94SLen Brown struct pkg_data { 214c98d5d94SLen Brown unsigned long long pc2; 215c98d5d94SLen Brown unsigned long long pc3; 216c98d5d94SLen Brown unsigned long long pc6; 217c98d5d94SLen Brown unsigned long long pc7; 218ca58710fSKristen Carlson Accardi unsigned long long pc8; 219ca58710fSKristen Carlson Accardi unsigned long long pc9; 220ca58710fSKristen Carlson Accardi unsigned long long pc10; 221be0e54c4SLen Brown unsigned long long cpu_lpi; 222be0e54c4SLen Brown unsigned long long sys_lpi; 2230b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2240b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2250b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2260b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2279185e988SLen Brown long long gfx_rc6_ms; 22827d47356SLen Brown unsigned int gfx_mhz; 229b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 230c98d5d94SLen Brown unsigned int package_id; 2317c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2327c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2337c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2347c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2357c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2367c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 237889facbeSLen Brown unsigned int pkg_temp_c; 238678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 239c98d5d94SLen Brown } *package_even, *package_odd; 240c98d5d94SLen Brown 241c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 242c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 243c98d5d94SLen Brown 24440f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 24540f5cfe7SPrarit Bhargava ((thread_base) + \ 24640f5cfe7SPrarit Bhargava ((pkg_no) * \ 24740f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 24840f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 24940f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 25040f5cfe7SPrarit Bhargava (thread_no)) 25140f5cfe7SPrarit Bhargava 25240f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 25340f5cfe7SPrarit Bhargava ((core_base) + \ 25440f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 25540f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 25640f5cfe7SPrarit Bhargava (core_no)) 25740f5cfe7SPrarit Bhargava 258c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 259c98d5d94SLen Brown 260388e9c81SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE }; 26141618e63SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC }; 262388e9c81SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT }; 263388e9c81SLen Brown 264388e9c81SLen Brown struct msr_counter { 265388e9c81SLen Brown unsigned int msr_num; 266388e9c81SLen Brown char name[NAME_BYTES]; 267495c7654SLen Brown char path[PATH_BYTES]; 268388e9c81SLen Brown unsigned int width; 269388e9c81SLen Brown enum counter_type type; 270388e9c81SLen Brown enum counter_format format; 271388e9c81SLen Brown struct msr_counter *next; 272812db3f7SLen Brown unsigned int flags; 273812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 274812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 27541618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 276388e9c81SLen Brown }; 277388e9c81SLen Brown 27887e15da9SChen Yu /* 27987e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 28087e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 28187e15da9SChen Yu * despite its register's bit width. 28287e15da9SChen Yu */ 28387e15da9SChen Yu enum { 28487e15da9SChen Yu IDX_PKG_ENERGY, 28587e15da9SChen Yu IDX_DRAM_ENERGY, 28687e15da9SChen Yu IDX_PP0_ENERGY, 28787e15da9SChen Yu IDX_PP1_ENERGY, 28887e15da9SChen Yu IDX_PKG_PERF, 28987e15da9SChen Yu IDX_DRAM_PERF, 29087e15da9SChen Yu IDX_COUNT, 29187e15da9SChen Yu }; 29287e15da9SChen Yu 29387e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 29487e15da9SChen Yu 29587e15da9SChen Yu struct msr_sum_array { 29687e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 29787e15da9SChen Yu struct { 29887e15da9SChen Yu /*The accumulated MSR value is updated by the timer */ 29987e15da9SChen Yu unsigned long long sum; 30087e15da9SChen Yu /*The MSR footprint recorded in last timer */ 30187e15da9SChen Yu unsigned long long last; 30287e15da9SChen Yu } entries[IDX_COUNT]; 30387e15da9SChen Yu }; 30487e15da9SChen Yu 30587e15da9SChen Yu /* The percpu MSR sum array.*/ 30687e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 30787e15da9SChen Yu 30813a779deSCalvin Walton off_t idx_to_offset(int idx) 30987e15da9SChen Yu { 31013a779deSCalvin Walton off_t offset; 31187e15da9SChen Yu 31287e15da9SChen Yu switch (idx) { 31387e15da9SChen Yu case IDX_PKG_ENERGY: 314301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H) 315301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT; 316301b1d3aSBas Nieuwenhuizen else 31787e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 31887e15da9SChen Yu break; 31987e15da9SChen Yu case IDX_DRAM_ENERGY: 32087e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 32187e15da9SChen Yu break; 32287e15da9SChen Yu case IDX_PP0_ENERGY: 32387e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 32487e15da9SChen Yu break; 32587e15da9SChen Yu case IDX_PP1_ENERGY: 32687e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 32787e15da9SChen Yu break; 32887e15da9SChen Yu case IDX_PKG_PERF: 32987e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 33087e15da9SChen Yu break; 33187e15da9SChen Yu case IDX_DRAM_PERF: 33287e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 33387e15da9SChen Yu break; 33487e15da9SChen Yu default: 33587e15da9SChen Yu offset = -1; 33687e15da9SChen Yu } 33787e15da9SChen Yu return offset; 33887e15da9SChen Yu } 33987e15da9SChen Yu 34013a779deSCalvin Walton int offset_to_idx(off_t offset) 34187e15da9SChen Yu { 34287e15da9SChen Yu int idx; 34387e15da9SChen Yu 34487e15da9SChen Yu switch (offset) { 34587e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 346301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT: 34787e15da9SChen Yu idx = IDX_PKG_ENERGY; 34887e15da9SChen Yu break; 34987e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 35087e15da9SChen Yu idx = IDX_DRAM_ENERGY; 35187e15da9SChen Yu break; 35287e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 35387e15da9SChen Yu idx = IDX_PP0_ENERGY; 35487e15da9SChen Yu break; 35587e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 35687e15da9SChen Yu idx = IDX_PP1_ENERGY; 35787e15da9SChen Yu break; 35887e15da9SChen Yu case MSR_PKG_PERF_STATUS: 35987e15da9SChen Yu idx = IDX_PKG_PERF; 36087e15da9SChen Yu break; 36187e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 36287e15da9SChen Yu idx = IDX_DRAM_PERF; 36387e15da9SChen Yu break; 36487e15da9SChen Yu default: 36587e15da9SChen Yu idx = -1; 36687e15da9SChen Yu } 36787e15da9SChen Yu return idx; 36887e15da9SChen Yu } 36987e15da9SChen Yu 37087e15da9SChen Yu int idx_valid(int idx) 37187e15da9SChen Yu { 37287e15da9SChen Yu switch (idx) { 37387e15da9SChen Yu case IDX_PKG_ENERGY: 374301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H); 37587e15da9SChen Yu case IDX_DRAM_ENERGY: 37687e15da9SChen Yu return do_rapl & RAPL_DRAM; 37787e15da9SChen Yu case IDX_PP0_ENERGY: 37887e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 37987e15da9SChen Yu case IDX_PP1_ENERGY: 38087e15da9SChen Yu return do_rapl & RAPL_GFX; 38187e15da9SChen Yu case IDX_PKG_PERF: 38287e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 38387e15da9SChen Yu case IDX_DRAM_PERF: 38487e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 38587e15da9SChen Yu default: 38687e15da9SChen Yu return 0; 38787e15da9SChen Yu } 38887e15da9SChen Yu } 3891b439f01SLen Brown 390388e9c81SLen Brown struct sys_counters { 391678a3bd1SLen Brown unsigned int added_thread_counters; 392678a3bd1SLen Brown unsigned int added_core_counters; 393678a3bd1SLen Brown unsigned int added_package_counters; 394388e9c81SLen Brown struct msr_counter *tp; 395388e9c81SLen Brown struct msr_counter *cp; 396388e9c81SLen Brown struct msr_counter *pp; 397388e9c81SLen Brown } sys; 398388e9c81SLen Brown 399c98d5d94SLen Brown struct system_summary { 400c98d5d94SLen Brown struct thread_data threads; 401c98d5d94SLen Brown struct core_data cores; 402c98d5d94SLen Brown struct pkg_data packages; 403388e9c81SLen Brown } average; 404c98d5d94SLen Brown 4050e2d8f05SLen Brown struct cpu_topology { 4060e2d8f05SLen Brown int physical_package_id; 4076de68fe1SLen Brown int die_id; 4080e2d8f05SLen Brown int logical_cpu_id; 409ef605741SPrarit Bhargava int physical_node_id; 410ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 4110e2d8f05SLen Brown int physical_core_id; 4128cb48b32SPrarit Bhargava int thread_id; 4130e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 4140e2d8f05SLen Brown } *cpus; 415c98d5d94SLen Brown 416c98d5d94SLen Brown struct topo_params { 417c98d5d94SLen Brown int num_packages; 4186de68fe1SLen Brown int num_die; 419103a8feaSLen Brown int num_cpus; 420c98d5d94SLen Brown int num_cores; 421c98d5d94SLen Brown int max_cpu_num; 422ef605741SPrarit Bhargava int max_node_num; 42370a9c6e8SPrarit Bhargava int nodes_per_pkg; 42470a9c6e8SPrarit Bhargava int cores_per_node; 42570a9c6e8SPrarit Bhargava int threads_per_core; 426c98d5d94SLen Brown } topo; 427103a8feaSLen Brown 428c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 429103a8feaSLen Brown 430562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 431562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 432562a2d37SLen Brown 433c98d5d94SLen Brown void setup_all_buffers(void); 434103a8feaSLen Brown 4351f81c5efSLen Brown char *sys_lpi_file; 4361f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 4371f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 4381f81c5efSLen Brown 439c98d5d94SLen Brown int cpu_is_not_present(int cpu) 440d15cf7c1SLen Brown { 441c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 442c98d5d94SLen Brown } 4431b439f01SLen Brown 444c98d5d94SLen Brown /* 445c98d5d94SLen Brown * run func(thread, core, package) in topology order 446c98d5d94SLen Brown * skip non-present cpus 447c98d5d94SLen Brown */ 448c98d5d94SLen Brown 449c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), 450c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 451c98d5d94SLen Brown { 45240f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 453c98d5d94SLen Brown 454c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 455df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 45670a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 4571b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 458c98d5d94SLen Brown struct thread_data *t; 459c98d5d94SLen Brown struct core_data *c; 460c98d5d94SLen Brown struct pkg_data *p; 461c98d5d94SLen Brown 4621b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 463c98d5d94SLen Brown 464c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 465c98d5d94SLen Brown continue; 466c98d5d94SLen Brown 4671b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 468c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 469c98d5d94SLen Brown 470c98d5d94SLen Brown retval = func(t, c, p); 471c98d5d94SLen Brown if (retval) 472c98d5d94SLen Brown return retval; 473c98d5d94SLen Brown } 474c98d5d94SLen Brown } 475c98d5d94SLen Brown } 47640f5cfe7SPrarit Bhargava } 477d15cf7c1SLen Brown return 0; 478d15cf7c1SLen Brown } 479d15cf7c1SLen Brown 48088c3281fSLen Brown int cpu_migrate(int cpu) 48188c3281fSLen Brown { 482c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 483c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 484c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 48588c3281fSLen Brown return -1; 48688c3281fSLen Brown else 48788c3281fSLen Brown return 0; 48888c3281fSLen Brown } 4891b439f01SLen Brown 49036229897SLen Brown int get_msr_fd(int cpu) 491103a8feaSLen Brown { 492103a8feaSLen Brown char pathname[32]; 493103a8feaSLen Brown int fd; 494103a8feaSLen Brown 49536229897SLen Brown fd = fd_percpu[cpu]; 49636229897SLen Brown 49736229897SLen Brown if (fd) 49836229897SLen Brown return fd; 49936229897SLen Brown 500103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 501103a8feaSLen Brown fd = open(pathname, O_RDONLY); 50215aaa346SLen Brown if (fd < 0) 50398481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 504103a8feaSLen Brown 50536229897SLen Brown fd_percpu[cpu] = fd; 50636229897SLen Brown 50736229897SLen Brown return fd; 50836229897SLen Brown } 50936229897SLen Brown 5102af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 5112af4f9b8SLen Brown { 5122af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 5132af4f9b8SLen Brown } 5142af4f9b8SLen Brown 5152af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 5162af4f9b8SLen Brown { 5172af4f9b8SLen Brown struct perf_event_attr pea; 5182af4f9b8SLen Brown int fd; 5192af4f9b8SLen Brown 5202af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 5212af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 5222af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 5232af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 5242af4f9b8SLen Brown 5252af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 5262af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 5272af4f9b8SLen Brown if (fd == -1) 5282af4f9b8SLen Brown err(-1, "cpu%d: perf instruction counter\n", cpu_num); 5292af4f9b8SLen Brown 5302af4f9b8SLen Brown return fd; 5312af4f9b8SLen Brown } 5322af4f9b8SLen Brown 5332af4f9b8SLen Brown int get_instr_count_fd(int cpu) 5342af4f9b8SLen Brown { 5352af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 5362af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5372af4f9b8SLen Brown 5382af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 5392af4f9b8SLen Brown 5402af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5412af4f9b8SLen Brown } 5422af4f9b8SLen Brown 54336229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 54436229897SLen Brown { 54536229897SLen Brown ssize_t retval; 54636229897SLen Brown 54736229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 54815aaa346SLen Brown 54998481e79SLen Brown if (retval != sizeof *msr) 550cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 55115aaa346SLen Brown 55215aaa346SLen Brown return 0; 553103a8feaSLen Brown } 554103a8feaSLen Brown 555fc04cc67SLen Brown /* 556bdd5ae3aSLen Brown * This list matches the column headers, except 557bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 558bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 559bdd5ae3aSLen Brown * matching on them for --show and --hide. 560fc04cc67SLen Brown */ 561812db3f7SLen Brown struct msr_counter bic[] = { 5623f44a5c6SLen Brown { 0x0, "usec" }, 5633f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 564812db3f7SLen Brown { 0x0, "Package" }, 565bdd5ae3aSLen Brown { 0x0, "Node" }, 566812db3f7SLen Brown { 0x0, "Avg_MHz" }, 567bdd5ae3aSLen Brown { 0x0, "Busy%" }, 568812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 569812db3f7SLen Brown { 0x0, "TSC_MHz" }, 570812db3f7SLen Brown { 0x0, "IRQ" }, 571495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL }, 572bdd5ae3aSLen Brown { 0x0, "sysfs" }, 573812db3f7SLen Brown { 0x0, "CPU%c1" }, 574812db3f7SLen Brown { 0x0, "CPU%c3" }, 575812db3f7SLen Brown { 0x0, "CPU%c6" }, 576812db3f7SLen Brown { 0x0, "CPU%c7" }, 577812db3f7SLen Brown { 0x0, "ThreadC" }, 578812db3f7SLen Brown { 0x0, "CoreTmp" }, 579812db3f7SLen Brown { 0x0, "CoreCnt" }, 580812db3f7SLen Brown { 0x0, "PkgTmp" }, 581812db3f7SLen Brown { 0x0, "GFX%rc6" }, 582812db3f7SLen Brown { 0x0, "GFXMHz" }, 583812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 584812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 585812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 586812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 5870f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 5880f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 5894bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 590be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 591be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 592812db3f7SLen Brown { 0x0, "PkgWatt" }, 593812db3f7SLen Brown { 0x0, "CorWatt" }, 594812db3f7SLen Brown { 0x0, "GFXWatt" }, 595812db3f7SLen Brown { 0x0, "PkgCnt" }, 596812db3f7SLen Brown { 0x0, "RAMWatt" }, 597812db3f7SLen Brown { 0x0, "PKG_%" }, 598812db3f7SLen Brown { 0x0, "RAM_%" }, 599812db3f7SLen Brown { 0x0, "Pkg_J" }, 600812db3f7SLen Brown { 0x0, "Cor_J" }, 601812db3f7SLen Brown { 0x0, "GFX_J" }, 602812db3f7SLen Brown { 0x0, "RAM_J" }, 6030539ba11SLen Brown { 0x0, "Mod%c6" }, 604a99d8730SLen Brown { 0x0, "Totl%C0" }, 605a99d8730SLen Brown { 0x0, "Any%C0" }, 606a99d8730SLen Brown { 0x0, "GFX%C0" }, 607a99d8730SLen Brown { 0x0, "CPUGFX%" }, 608bdd5ae3aSLen Brown { 0x0, "Core" }, 609bdd5ae3aSLen Brown { 0x0, "CPU" }, 6104c2122d4SLen Brown { 0x0, "APIC" }, 6114c2122d4SLen Brown { 0x0, "X2APIC" }, 6126de68fe1SLen Brown { 0x0, "Die" }, 613b4b91569SRafael Antognolli { 0x0, "GFXAMHz" }, 6142af4f9b8SLen Brown { 0x0, "IPC" }, 615eae97e05SChen Yu { 0x0, "CoreThr" }, 616812db3f7SLen Brown }; 617812db3f7SLen Brown 618812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 6193f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 6203f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 6213f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 622bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 623bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 624bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 625bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 626bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 627bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 628bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 629bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 630bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 631bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 632bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 633bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 634bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 635bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 636bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 637bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 638bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 639bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 640bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 641bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 642bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 643bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 644bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 645bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 646bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 647bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 648bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 649bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 650bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 651bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 652bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 653bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 654bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 655bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 656bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 657bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 658bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 659bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 660bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 661bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 662bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 663bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 664bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 665bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 666bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 6674c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 6684c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 6696de68fe1SLen Brown #define BIC_Die (1ULL << 50) 670b4b91569SRafael Antognolli #define BIC_GFXACTMHz (1ULL << 51) 6712af4f9b8SLen Brown #define BIC_IPC (1ULL << 52) 672eae97e05SChen Yu #define BIC_CORE_THROT_CNT (1ULL << 53) 673812db3f7SLen Brown 674b60c573dSLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die ) 675b60c573dSLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__) 676b60c573dSLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz ) 677b60c573dSLen Brown #define BIC_IDLE ( BIC_sysfs | BIC_CPU_c1 | BIC_CPU_c3 | BIC_CPU_c6 | BIC_CPU_c7 | BIC_GFX_rc6 | BIC_Pkgpc2 | BIC_Pkgpc3 | BIC_Pkgpc6 | BIC_Pkgpc7 | BIC_Pkgpc8 | BIC_Pkgpc9 | BIC_Pkgpc10 | BIC_CPU_LPI | BIC_SYS_LPI | BIC_Mod_c6 | BIC_Totl_c0 | BIC_Any_c0 | BIC_GFX_c0 | BIC_CPUGFX) 678b60c573dSLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC) 679b60c573dSLen Brown 6804c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 6813f44a5c6SLen Brown 6823f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 6834c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 684812db3f7SLen Brown 685812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 6861e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 6873f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 688812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 6890f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 6902af4f9b8SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 691812db3f7SLen Brown 692dd778a5eSLen Brown #define MAX_DEFERRED 16 6930fc521bcSZephaniah E. Loss-Cutler-Hull char *deferred_add_names[MAX_DEFERRED]; 694dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 6950fc521bcSZephaniah E. Loss-Cutler-Hull int deferred_add_index; 696dd778a5eSLen Brown int deferred_skip_index; 697dd778a5eSLen Brown 698dd778a5eSLen Brown /* 699dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 700dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 701dd778a5eSLen Brown */ 702dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 703dd778a5eSLen Brown 704dd778a5eSLen Brown void help(void) 705dd778a5eSLen Brown { 706dd778a5eSLen Brown fprintf(outf, 707dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 708dd778a5eSLen Brown "\n" 709dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 710dd778a5eSLen Brown "when COMMAND completes.\n" 711dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 712dd778a5eSLen Brown "to print statistics, until interrupted.\n" 713cc481650SNathan Ciobanu " -a, --add add a counter\n" 714dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 715cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 716dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7179ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7189ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7199ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7209ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7219ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7229ce80578SNathan Ciobanu " hide the specified column(s)\n" 723cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 724cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7259ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 726cc481650SNathan Ciobanu " -l, --list list column headers only\n" 727cc481650SNathan Ciobanu " -n, --num_iterations num\n" 728cc481650SNathan Ciobanu " number of the measurement iterations\n" 729c7e399f8SZephaniah E. Loss-Cutler-Hull " -N, --header_iterations num\n" 730c7e399f8SZephaniah E. Loss-Cutler-Hull " print header every num iterations\n" 731cc481650SNathan Ciobanu " -o, --out file\n" 732cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 733cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7349ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7359ce80578SNathan Ciobanu " show only the specified column(s)\n" 7369ce80578SNathan Ciobanu " -S, --Summary\n" 7379ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7389ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7399ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7409ce80578SNathan Ciobanu " degrees Celsius\n" 741cc481650SNathan Ciobanu " -h, --help print this help message\n" 7421b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n"); 743dd778a5eSLen Brown } 744dd778a5eSLen Brown 745812db3f7SLen Brown /* 746812db3f7SLen Brown * bic_lookup 747812db3f7SLen Brown * for all the strings in comma separate name_list, 748812db3f7SLen Brown * set the approprate bit in return value. 749812db3f7SLen Brown */ 750dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 751812db3f7SLen Brown { 752812db3f7SLen Brown int i; 753812db3f7SLen Brown unsigned long long retval = 0; 754812db3f7SLen Brown 755812db3f7SLen Brown while (name_list) { 756812db3f7SLen Brown char *comma; 757812db3f7SLen Brown 758812db3f7SLen Brown comma = strchr(name_list, ','); 759812db3f7SLen Brown 760812db3f7SLen Brown if (comma) 761812db3f7SLen Brown *comma = '\0'; 762812db3f7SLen Brown 763812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 764812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 765812db3f7SLen Brown retval |= (1ULL << i); 766812db3f7SLen Brown break; 767812db3f7SLen Brown } 7685dc241f2SLen Brown if (!strcmp(name_list, "all")) { 7695dc241f2SLen Brown retval |= ~0; 7705dc241f2SLen Brown break; 7715dc241f2SLen Brown } else if (!strcmp(name_list, "topology")) { 7725dc241f2SLen Brown retval |= BIC_TOPOLOGY; 7735dc241f2SLen Brown break; 7745dc241f2SLen Brown } else if (!strcmp(name_list, "power")) { 7755dc241f2SLen Brown retval |= BIC_THERMAL_PWR; 7765dc241f2SLen Brown break; 7775dc241f2SLen Brown } else if (!strcmp(name_list, "idle")) { 7785dc241f2SLen Brown retval |= BIC_IDLE; 7795dc241f2SLen Brown break; 7805dc241f2SLen Brown } else if (!strcmp(name_list, "frequency")) { 7815dc241f2SLen Brown retval |= BIC_FREQUENCY; 7825dc241f2SLen Brown break; 7835dc241f2SLen Brown } else if (!strcmp(name_list, "other")) { 7845dc241f2SLen Brown retval |= BIC_OTHER; 7855dc241f2SLen Brown break; 7865dc241f2SLen Brown } 7875dc241f2SLen Brown 788812db3f7SLen Brown } 789812db3f7SLen Brown if (i == MAX_BIC) { 790dd778a5eSLen Brown if (mode == SHOW_LIST) { 7910fc521bcSZephaniah E. Loss-Cutler-Hull deferred_add_names[deferred_add_index++] = name_list; 7920fc521bcSZephaniah E. Loss-Cutler-Hull if (deferred_add_index >= MAX_DEFERRED) { 7930fc521bcSZephaniah E. Loss-Cutler-Hull fprintf(stderr, "More than max %d un-recognized --add options '%s'\n", 7940fc521bcSZephaniah E. Loss-Cutler-Hull MAX_DEFERRED, name_list); 7950fc521bcSZephaniah E. Loss-Cutler-Hull help(); 7960fc521bcSZephaniah E. Loss-Cutler-Hull exit(1); 797812db3f7SLen Brown } 7980fc521bcSZephaniah E. Loss-Cutler-Hull } else { 799dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 800dd778a5eSLen Brown if (debug) 801dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 802dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 803dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 804dd778a5eSLen Brown MAX_DEFERRED, name_list); 805dd778a5eSLen Brown help(); 806dd778a5eSLen Brown exit(1); 807dd778a5eSLen Brown } 808dd778a5eSLen Brown } 8090fc521bcSZephaniah E. Loss-Cutler-Hull } 810812db3f7SLen Brown 811812db3f7SLen Brown name_list = comma; 812812db3f7SLen Brown if (name_list) 813812db3f7SLen Brown name_list++; 814812db3f7SLen Brown 815812db3f7SLen Brown } 816812db3f7SLen Brown return retval; 817812db3f7SLen Brown } 818fc04cc67SLen Brown 819c8ade361SLen Brown void print_header(char *delim) 820103a8feaSLen Brown { 821388e9c81SLen Brown struct msr_counter *mp; 8226168c2e0SLen Brown int printed = 0; 823388e9c81SLen Brown 8243f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 8253f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 8263f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 8273f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 828812db3f7SLen Brown if (DO_BIC(BIC_Package)) 8296168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 8306de68fe1SLen Brown if (DO_BIC(BIC_Die)) 8316de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 83201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 83301235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 834812db3f7SLen Brown if (DO_BIC(BIC_Core)) 8356168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 836812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8376168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8384c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8394c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8404c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8414c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 842812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8436168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 844812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8456168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 846812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8476168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 848812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8496168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8501cc21f7bSLen Brown 8512af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8522af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8532af4f9b8SLen Brown 8540de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8550de6c0dfSLen Brown if (sums_need_wide_columns) 8566168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8570de6c0dfSLen Brown else 8586168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8590de6c0dfSLen Brown } 8600de6c0dfSLen Brown 861812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8626168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8631cc21f7bSLen Brown 864388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 865dd778a5eSLen Brown 866388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 867388e9c81SLen Brown if (mp->width == 64) 868dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 869388e9c81SLen Brown else 870dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 871388e9c81SLen Brown } else { 8720de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 873dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8740de6c0dfSLen Brown else 875dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 876388e9c81SLen Brown } 877388e9c81SLen Brown } 878388e9c81SLen Brown 87941618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8806168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 881562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8826168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 883812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8846168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 885812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8866168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 887678a3bd1SLen Brown 8880539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8896168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 890678a3bd1SLen Brown 891812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8926168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 893388e9c81SLen Brown 894eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 895eae97e05SChen Yu outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : "")); 896eae97e05SChen Yu 8979392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 8989392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8999392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 9009392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 9019392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 9029392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 9039392bd98SCalvin Walton } 9049392bd98SCalvin Walton 905388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 906388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 907388e9c81SLen Brown if (mp->width == 64) 908c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 909388e9c81SLen Brown else 910c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 911388e9c81SLen Brown } else { 9120de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9130de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9140de6c0dfSLen Brown else 915c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 916388e9c81SLen Brown } 917388e9c81SLen Brown } 918388e9c81SLen Brown 919812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 9206168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 921889facbeSLen Brown 922812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 9236168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 924fdf676e5SLen Brown 925812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 9266168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 92727d47356SLen Brown 928b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 929b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 930b4b91569SRafael Antognolli 931a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 9326168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 933a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 9346168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 935a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 9366168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 937a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 9386168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 9390b2bb692SLen Brown 9400f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9416168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9420f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9436168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9440f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9456168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9476168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9496168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9516168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9536168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 954be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 955be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 956be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 957be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 958103a8feaSLen Brown 9595c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 960812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9616168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9629392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9636168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 964812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9656168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 966812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9676168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 968812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9696168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 970812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9716168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 972d7899447SLen Brown } else if (do_rapl && rapl_joules) { 973812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9746168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9759392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9766168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 977812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9786168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 979812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9806168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 981812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9826168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 983812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9846168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9855c56be9aSDirk Brandewie } 986388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 987388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 988388e9c81SLen Brown if (mp->width == 64) 989c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 990388e9c81SLen Brown else 991c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 992388e9c81SLen Brown } else { 9930de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9940de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9950de6c0dfSLen Brown else 996c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 997388e9c81SLen Brown } 998388e9c81SLen Brown } 999388e9c81SLen Brown 1000c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1001103a8feaSLen Brown } 1002103a8feaSLen Brown 10031b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1004103a8feaSLen Brown { 1005388e9c81SLen Brown int i; 1006388e9c81SLen Brown struct msr_counter *mp; 1007388e9c81SLen Brown 10083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 1009c98d5d94SLen Brown 1010c98d5d94SLen Brown if (t) { 10111b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 10123b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 10133b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 10143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 10153b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 10166886fee4SLen Brown 10172af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 10182af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 10192af4f9b8SLen Brown 1020812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 10210de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 1022812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 1023218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 1024388e9c81SLen Brown 1025388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 10261b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]); 1027388e9c81SLen Brown } 1028103a8feaSLen Brown } 1029103a8feaSLen Brown 1030c98d5d94SLen Brown if (c) { 10313b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 10323b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 10333b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 10343b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 10353b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 1036eae97e05SChen Yu outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt); 10379392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 1038388e9c81SLen Brown 1039388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 10401b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]); 1041388e9c81SLen Brown } 10420539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1043c98d5d94SLen Brown } 1044103a8feaSLen Brown 1045c98d5d94SLen Brown if (p) { 10463b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10470b2bb692SLen Brown 10480b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10490b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10500b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10510b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10520b2bb692SLen Brown 10533b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10540f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10553b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10560f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10573b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10613b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1063be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1064be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10657c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10667c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10677c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10687c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 10691b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status); 10701b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status); 10713b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1072388e9c81SLen Brown 1073388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 10741b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]); 1075388e9c81SLen Brown } 1076c98d5d94SLen Brown } 10773b4d5c7fSAndy Shevchenko 10783b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10793b4d5c7fSAndy Shevchenko 1080c98d5d94SLen Brown return 0; 1081103a8feaSLen Brown } 1082103a8feaSLen Brown 1083e23da037SLen Brown /* 1084e23da037SLen Brown * column formatting convention & formats 1085e23da037SLen Brown */ 10861b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1087103a8feaSLen Brown { 1088008d396eSLen Brown double interval_float, tsc; 1089fc04cc67SLen Brown char *fmt8; 1090388e9c81SLen Brown int i; 1091388e9c81SLen Brown struct msr_counter *mp; 10926168c2e0SLen Brown char *delim = "\t"; 10936168c2e0SLen Brown int printed = 0; 1094103a8feaSLen Brown 1095c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1096c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1097c98d5d94SLen Brown return 0; 1098c98d5d94SLen Brown 1099c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1100c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1101c98d5d94SLen Brown return 0; 1102c98d5d94SLen Brown 11031ef7d21aSLen Brown /*if not summary line and --cpu is used */ 11041b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 11051ef7d21aSLen Brown return 0; 11061ef7d21aSLen Brown 11073f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1108f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1109f4fdf2b4SLen Brown struct timeval tv; 1110f4fdf2b4SLen Brown 1111f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1112f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1113f4fdf2b4SLen Brown } 1114f4fdf2b4SLen Brown 11153f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 11163f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 11173f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 11183f44a5c6SLen Brown 1119d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 1120103a8feaSLen Brown 1121008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1122008d396eSLen Brown 1123c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1124c98d5d94SLen Brown if (t == &average.threads) { 1125812db3f7SLen Brown if (DO_BIC(BIC_Package)) 11266168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11276de68fe1SLen Brown if (DO_BIC(BIC_Die)) 11286de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 112901235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 113001235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1131812db3f7SLen Brown if (DO_BIC(BIC_Core)) 11326168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1133812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11346168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11354c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11364c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11374c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11384c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1139103a8feaSLen Brown } else { 1140812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1141c98d5d94SLen Brown if (p) 11426168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1143c98d5d94SLen Brown else 11446168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1145c98d5d94SLen Brown } 11466de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11476de68fe1SLen Brown if (c) 11486de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11496de68fe1SLen Brown else 11506de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11516de68fe1SLen Brown } 115201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 115301235041SPrarit Bhargava if (t) 115401235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 11551b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 115601235041SPrarit Bhargava else 11571b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 115801235041SPrarit Bhargava } 1159812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1160c98d5d94SLen Brown if (c) 11616168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1162c98d5d94SLen Brown else 11636168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1164c98d5d94SLen Brown } 1165812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11666168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11674c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11684c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11694c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11704c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1171103a8feaSLen Brown } 1172fc04cc67SLen Brown 1173812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 11741b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 1175fc04cc67SLen Brown 1176812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11776168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc); 1178103a8feaSLen Brown 1179812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 118021ed5574SLen Brown if (has_base_hz) 11811b439f01SLen Brown outp += 11821b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 118321ed5574SLen Brown else 11846168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1185008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 118621ed5574SLen Brown } 1187103a8feaSLen Brown 1188812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 11896168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 1190103a8feaSLen Brown 11912af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 11922af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 11932af4f9b8SLen Brown 1194562a2d37SLen Brown /* IRQ */ 11950de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 11960de6c0dfSLen Brown if (sums_need_wide_columns) 11976168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 11980de6c0dfSLen Brown else 11996168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 12000de6c0dfSLen Brown } 1201562a2d37SLen Brown 12021cc21f7bSLen Brown /* SMI */ 1203812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 12046168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 12051cc21f7bSLen Brown 1206678a3bd1SLen Brown /* Added counters */ 1207678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1208678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1209678a3bd1SLen Brown if (mp->width == 32) 12101b439f01SLen Brown outp += 12111b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]); 1212678a3bd1SLen Brown else 12136168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1214678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 12150de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12166168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 12170de6c0dfSLen Brown else 12186168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1219678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 122041618e63SLen Brown if (mp->type == COUNTER_USEC) 12211b439f01SLen Brown outp += 12221b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12231b439f01SLen Brown t->counter[i] / interval_float / 10000); 122441618e63SLen Brown else 12256168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc); 1226678a3bd1SLen Brown } 1227678a3bd1SLen Brown } 1228678a3bd1SLen Brown 122941618e63SLen Brown /* C1 */ 123041618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 12316168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc); 123241618e63SLen Brown 1233c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1234c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1235c98d5d94SLen Brown goto done; 1236c98d5d94SLen Brown 1237562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 12386168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc); 1239812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 12406168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc); 1241812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12426168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc); 1243c98d5d94SLen Brown 12440539ba11SLen Brown /* Mod%c6 */ 12450539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12466168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12470539ba11SLen Brown 1248812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12496168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1250889facbeSLen Brown 1251eae97e05SChen Yu /* Core throttle count */ 1252eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 1253eae97e05SChen Yu outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt); 1254eae97e05SChen Yu 1255388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1256388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1257388e9c81SLen Brown if (mp->width == 32) 12581b439f01SLen Brown outp += 12591b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]); 1260388e9c81SLen Brown else 12616168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1262388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12630de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12646168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12650de6c0dfSLen Brown else 12666168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1267388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12686168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc); 1269388e9c81SLen Brown } 1270388e9c81SLen Brown } 1271388e9c81SLen Brown 12729392bd98SCalvin Walton fmt8 = "%s%.2f"; 12739392bd98SCalvin Walton 12749392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12751b439f01SLen Brown outp += 12761b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12779392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12789392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12799392bd98SCalvin Walton 1280c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1281c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1282c98d5d94SLen Brown goto done; 1283c98d5d94SLen Brown 12840b2bb692SLen Brown /* PkgTmp */ 1285812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 12866168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1287889facbeSLen Brown 1288fdf676e5SLen Brown /* GFXrc6 */ 1289812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1290ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 12916168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 12929185e988SLen Brown } else { 12936168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12949185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 12959185e988SLen Brown } 12969185e988SLen Brown } 1297fdf676e5SLen Brown 129827d47356SLen Brown /* GFXMHz */ 1299812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 13006168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 130127d47356SLen Brown 1302b4b91569SRafael Antognolli /* GFXACTMHz */ 1303b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1304b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1305b4b91569SRafael Antognolli 13060b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1307a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13086168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc); 1309a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13106168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc); 1311a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13126168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc); 1313a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 13146168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc); 13150b2bb692SLen Brown 13160f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 13176168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc); 13180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 13196168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc); 13200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 13216168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc); 13220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 13236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc); 13240f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 13256168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc); 13260f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 13276168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc); 13280f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 13296168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc); 1330889facbeSLen Brown 1331be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 13321b439f01SLen Brown outp += 13331b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1334be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 13351b439f01SLen Brown outp += 13361b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1337be0e54c4SLen Brown 1338812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 13391b439f01SLen Brown outp += 13401b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 1341eae97e05SChen Yu 13429392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13431b439f01SLen Brown outp += 13441b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1345812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 13461b439f01SLen Brown outp += 13471b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1348812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 13491b439f01SLen Brown outp += 13501b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13511b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float); 1352812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13536168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13549392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13556168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1356812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13576168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1358812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13596168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1360812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 13611b439f01SLen Brown outp += 13621b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13631b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1364812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 13651b439f01SLen Brown outp += 13661b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13671b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1368812db3f7SLen Brown 1369388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1370388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1371388e9c81SLen Brown if (mp->width == 32) 13721b439f01SLen Brown outp += 13731b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]); 1374388e9c81SLen Brown else 13756168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1376388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 13770de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 13786168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 13790de6c0dfSLen Brown else 13806168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1381388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 13826168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc); 1383388e9c81SLen Brown } 1384388e9c81SLen Brown } 1385388e9c81SLen Brown 1386c98d5d94SLen Brown done: 138794d6ab4bSLen Brown if (*(outp - 1) != '\n') 1388c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1389c98d5d94SLen Brown 1390c98d5d94SLen Brown return 0; 1391103a8feaSLen Brown } 1392103a8feaSLen Brown 1393b7d8c148SLen Brown void flush_output_stdout(void) 1394103a8feaSLen Brown { 1395b7d8c148SLen Brown FILE *filep; 1396b7d8c148SLen Brown 1397b7d8c148SLen Brown if (outf == stderr) 1398b7d8c148SLen Brown filep = stdout; 1399b7d8c148SLen Brown else 1400b7d8c148SLen Brown filep = outf; 1401b7d8c148SLen Brown 1402b7d8c148SLen Brown fputs(output_buffer, filep); 1403b7d8c148SLen Brown fflush(filep); 1404b7d8c148SLen Brown 1405c98d5d94SLen Brown outp = output_buffer; 1406c98d5d94SLen Brown } 14071b439f01SLen Brown 1408b7d8c148SLen Brown void flush_output_stderr(void) 1409c98d5d94SLen Brown { 1410b7d8c148SLen Brown fputs(output_buffer, outf); 1411b7d8c148SLen Brown fflush(outf); 1412c98d5d94SLen Brown outp = output_buffer; 1413c98d5d94SLen Brown } 14141b439f01SLen Brown 1415c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1416c98d5d94SLen Brown { 1417c7e399f8SZephaniah E. Loss-Cutler-Hull static int count; 1418103a8feaSLen Brown 1419c7e399f8SZephaniah E. Loss-Cutler-Hull if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only) 1420c8ade361SLen Brown print_header("\t"); 1421103a8feaSLen Brown 14229d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1423103a8feaSLen Brown 1424c7e399f8SZephaniah E. Loss-Cutler-Hull count++; 1425e23da037SLen Brown 1426e23da037SLen Brown if (summary_only) 1427e23da037SLen Brown return; 1428e23da037SLen Brown 1429c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1430103a8feaSLen Brown } 1431103a8feaSLen Brown 1432889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 14337c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1434889facbeSLen Brown 14351b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old) 1436103a8feaSLen Brown { 1437388e9c81SLen Brown int i; 1438388e9c81SLen Brown struct msr_counter *mp; 14390b2bb692SLen Brown 1440a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14410b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1442a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14430b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1444a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14450b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1446a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14470b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1448a99d8730SLen Brown 1449c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 14500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1451c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 14520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1453c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 14540f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1455c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1456ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1457ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1458ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1459be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1460be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1461889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1462889facbeSLen Brown 14639185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 14649185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 14659185e988SLen Brown old->gfx_rc6_ms = -1; 14669185e988SLen Brown else 1467fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 14689185e988SLen Brown 146927d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1470b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 147127d47356SLen Brown 147287e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 147387e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 147487e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 147587e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 147687e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 147787e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1478ba3dec99SLen Brown 1479388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1480388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1481388e9c81SLen Brown old->counter[i] = new->counter[i]; 1482388e9c81SLen Brown else 1483388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1484388e9c81SLen Brown } 1485388e9c81SLen Brown 1486ba3dec99SLen Brown return 0; 1487103a8feaSLen Brown } 1488103a8feaSLen Brown 14891b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old) 1490c98d5d94SLen Brown { 1491388e9c81SLen Brown int i; 1492388e9c81SLen Brown struct msr_counter *mp; 1493388e9c81SLen Brown 1494c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1495c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1496c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1497889facbeSLen Brown old->core_temp_c = new->core_temp_c; 1498eae97e05SChen Yu old->core_throt_cnt = new->core_throt_cnt; 14990539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1500388e9c81SLen Brown 15019392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 15029392bd98SCalvin Walton 1503388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1504388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1505388e9c81SLen Brown old->counter[i] = new->counter[i]; 1506388e9c81SLen Brown else 1507388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1508388e9c81SLen Brown } 1509103a8feaSLen Brown } 1510c98d5d94SLen Brown 15111e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 15121e9042b9SSrinivas Pandruvada { 15131e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 15141e9042b9SSrinivas Pandruvada return 0; 15151e9042b9SSrinivas Pandruvada 15161e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 15171e9042b9SSrinivas Pandruvada } 15181e9042b9SSrinivas Pandruvada 1519c3ae331dSLen Brown /* 1520c3ae331dSLen Brown * old = new - old 1521c3ae331dSLen Brown */ 15221b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 1523c98d5d94SLen Brown { 1524388e9c81SLen Brown int i; 1525388e9c81SLen Brown struct msr_counter *mp; 1526388e9c81SLen Brown 15274c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 15284c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 15294c2122d4SLen Brown new->apic_id = old->apic_id; 15304c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 15314c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 15324c2122d4SLen Brown 15333f44a5c6SLen Brown /* 15343f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 15353f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 15363f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 15373f44a5c6SLen Brown */ 15383f44a5c6SLen Brown 1539d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 15403f44a5c6SLen Brown old->tv_begin = new->tv_begin; 15413f44a5c6SLen Brown old->tv_end = new->tv_end; 15423f44a5c6SLen Brown 1543c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1544c98d5d94SLen Brown 1545103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1546b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1547b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1548b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1549b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1550103a8feaSLen Brown 1551c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1552c98d5d94SLen Brown 15531b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 1554c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1555c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1556c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1557c98d5d94SLen Brown } else { 1558ba3dec99SLen Brown return -1; 1559103a8feaSLen Brown } 1560a729617cSLen Brown } 1561103a8feaSLen Brown 1562144b44b1SLen Brown if (use_c1_residency_msr) { 1563144b44b1SLen Brown /* 1564144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1565144b44b1SLen Brown * which should be more accurate than the derivation below. 1566144b44b1SLen Brown */ 1567144b44b1SLen Brown } else { 1568103a8feaSLen Brown /* 1569c3ae331dSLen Brown * As counter collection is not atomic, 1570c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1571103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1572103a8feaSLen Brown */ 157395149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1574c98d5d94SLen Brown old->c1 = 0; 1575c98d5d94SLen Brown else { 1576c98d5d94SLen Brown /* normal case, derive c1 */ 1577008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1578c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1579c98d5d94SLen Brown } 1580144b44b1SLen Brown } 1581c3ae331dSLen Brown 1582c98d5d94SLen Brown if (old->mperf == 0) { 1583b7d8c148SLen Brown if (debug > 1) 1584b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1585c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1586c98d5d94SLen Brown } 1587103a8feaSLen Brown 15882af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 15892af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 15902af4f9b8SLen Brown 1591812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1592562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1593562a2d37SLen Brown 1594812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 15951ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1596ba3dec99SLen Brown 1597388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1598388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1599388e9c81SLen Brown old->counter[i] = new->counter[i]; 1600388e9c81SLen Brown else 1601388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1602388e9c81SLen Brown } 1603ba3dec99SLen Brown return 0; 1604103a8feaSLen Brown } 1605c98d5d94SLen Brown 1606c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 16071b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 1608c98d5d94SLen Brown { 1609ba3dec99SLen Brown int retval = 0; 1610ba3dec99SLen Brown 1611c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1612c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1613c98d5d94SLen Brown delta_core(c, c2); 1614c98d5d94SLen Brown 1615c98d5d94SLen Brown /* always calculate thread delta */ 1616ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1617ba3dec99SLen Brown if (retval) 1618ba3dec99SLen Brown return retval; 1619c98d5d94SLen Brown 1620c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1621c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1622ba3dec99SLen Brown retval = delta_package(p, p2); 1623c98d5d94SLen Brown 1624ba3dec99SLen Brown return retval; 1625103a8feaSLen Brown } 1626103a8feaSLen Brown 1627c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1628103a8feaSLen Brown { 1629388e9c81SLen Brown int i; 1630388e9c81SLen Brown struct msr_counter *mp; 1631388e9c81SLen Brown 16323f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 16333f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 16343f44a5c6SLen Brown t->tv_end.tv_sec = 0; 16353f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1636d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1637d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 16383f44a5c6SLen Brown 1639c98d5d94SLen Brown t->tsc = 0; 1640c98d5d94SLen Brown t->aperf = 0; 1641c98d5d94SLen Brown t->mperf = 0; 1642c98d5d94SLen Brown t->c1 = 0; 1643103a8feaSLen Brown 16442af4f9b8SLen Brown t->instr_count = 0; 16452af4f9b8SLen Brown 1646562a2d37SLen Brown t->irq_count = 0; 1647562a2d37SLen Brown t->smi_count = 0; 1648562a2d37SLen Brown 1649c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1650c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1651c98d5d94SLen Brown 1652c98d5d94SLen Brown c->c3 = 0; 1653c98d5d94SLen Brown c->c6 = 0; 1654c98d5d94SLen Brown c->c7 = 0; 16550539ba11SLen Brown c->mc6_us = 0; 1656889facbeSLen Brown c->core_temp_c = 0; 16579392bd98SCalvin Walton c->core_energy = 0; 1658eae97e05SChen Yu c->core_throt_cnt = 0; 1659c98d5d94SLen Brown 16600b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16610b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16620b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16630b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16640b2bb692SLen Brown 1665c98d5d94SLen Brown p->pc2 = 0; 16660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1667c98d5d94SLen Brown p->pc3 = 0; 16680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1669c98d5d94SLen Brown p->pc6 = 0; 16700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1671c98d5d94SLen Brown p->pc7 = 0; 1672ca58710fSKristen Carlson Accardi p->pc8 = 0; 1673ca58710fSKristen Carlson Accardi p->pc9 = 0; 1674ca58710fSKristen Carlson Accardi p->pc10 = 0; 1675be0e54c4SLen Brown p->cpu_lpi = 0; 1676be0e54c4SLen Brown p->sys_lpi = 0; 1677889facbeSLen Brown 1678889facbeSLen Brown p->energy_pkg = 0; 1679889facbeSLen Brown p->energy_dram = 0; 1680889facbeSLen Brown p->energy_cores = 0; 1681889facbeSLen Brown p->energy_gfx = 0; 1682889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1683889facbeSLen Brown p->rapl_dram_perf_status = 0; 1684889facbeSLen Brown p->pkg_temp_c = 0; 168527d47356SLen Brown 1686fdf676e5SLen Brown p->gfx_rc6_ms = 0; 168727d47356SLen Brown p->gfx_mhz = 0; 1688b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1689388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1690388e9c81SLen Brown t->counter[i] = 0; 1691388e9c81SLen Brown 1692388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1693388e9c81SLen Brown c->counter[i] = 0; 1694388e9c81SLen Brown 1695388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1696388e9c81SLen Brown p->counter[i] = 0; 1697103a8feaSLen Brown } 16981b439f01SLen Brown 16991b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1700103a8feaSLen Brown { 1701388e9c81SLen Brown int i; 1702388e9c81SLen Brown struct msr_counter *mp; 1703388e9c81SLen Brown 17044c2122d4SLen Brown /* copy un-changing apic_id's */ 17054c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 17064c2122d4SLen Brown average.threads.apic_id = t->apic_id; 17074c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 17084c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 17094c2122d4SLen Brown 17103f44a5c6SLen Brown /* remember first tv_begin */ 17113f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 17123f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 17133f44a5c6SLen Brown 17143f44a5c6SLen Brown /* remember last tv_end */ 17153f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 17163f44a5c6SLen Brown 1717c98d5d94SLen Brown average.threads.tsc += t->tsc; 1718c98d5d94SLen Brown average.threads.aperf += t->aperf; 1719c98d5d94SLen Brown average.threads.mperf += t->mperf; 1720c98d5d94SLen Brown average.threads.c1 += t->c1; 172115aaa346SLen Brown 17222af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 17232af4f9b8SLen Brown 1724562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1725562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1726562a2d37SLen Brown 1727388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1728388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1729388e9c81SLen Brown continue; 1730388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1731388e9c81SLen Brown } 1732388e9c81SLen Brown 1733c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1734c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1735c98d5d94SLen Brown return 0; 1736c98d5d94SLen Brown 1737c98d5d94SLen Brown average.cores.c3 += c->c3; 1738c98d5d94SLen Brown average.cores.c6 += c->c6; 1739c98d5d94SLen Brown average.cores.c7 += c->c7; 17400539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1741c98d5d94SLen Brown 1742889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1743eae97e05SChen Yu average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt); 1744889facbeSLen Brown 17459392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 17469392bd98SCalvin Walton 1747388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1748388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1749388e9c81SLen Brown continue; 1750388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1751388e9c81SLen Brown } 1752388e9c81SLen Brown 1753c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1754c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1755c98d5d94SLen Brown return 0; 1756c98d5d94SLen Brown 1757a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17580b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1759a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17600b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1761a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17620b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1763a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17640b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17650b2bb692SLen Brown 1766c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17670f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1768c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1770c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1772c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1773ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1774ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1775ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1776c98d5d94SLen Brown 1777be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1778be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1779be0e54c4SLen Brown 1780889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1781889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1782889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1783889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1784889facbeSLen Brown 1785fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 178627d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1787b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 178827d47356SLen Brown 1789889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1790889facbeSLen Brown 1791889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1792889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1793388e9c81SLen Brown 1794388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1795388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1796388e9c81SLen Brown continue; 1797388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1798388e9c81SLen Brown } 1799c98d5d94SLen Brown return 0; 1800c98d5d94SLen Brown } 18011b439f01SLen Brown 1802c98d5d94SLen Brown /* 1803c98d5d94SLen Brown * sum the counters for all cpus in the system 1804c98d5d94SLen Brown * compute the weighted average 1805c98d5d94SLen Brown */ 18061b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1807c98d5d94SLen Brown { 1808388e9c81SLen Brown int i; 1809388e9c81SLen Brown struct msr_counter *mp; 1810388e9c81SLen Brown 1811c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1812c98d5d94SLen Brown 1813c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1814c98d5d94SLen Brown 1815d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1816d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1817d4794f25SYazen Ghannam 1818c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1819c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1820c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 18212af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1822c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1823c98d5d94SLen Brown 18240de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 18250de6c0dfSLen Brown sums_need_wide_columns = 1; 18260de6c0dfSLen Brown 1827c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1828c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1829c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 18300539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1831c98d5d94SLen Brown 1832a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 18330b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1834a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 18350b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1836a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 18370b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1838a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 18390b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 18400b2bb692SLen Brown 1841c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 18420f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1843c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 18440f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1845c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 18460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1847c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1848ca58710fSKristen Carlson Accardi 1849ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1850ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1851ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1852388e9c81SLen Brown 1853388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1854388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1855388e9c81SLen Brown continue; 18560de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18570de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18580de6c0dfSLen Brown sums_need_wide_columns = 1; 185941618e63SLen Brown continue; 18600de6c0dfSLen Brown } 1861388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1862388e9c81SLen Brown } 1863388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1864388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1865388e9c81SLen Brown continue; 18660de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18670de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18680de6c0dfSLen Brown sums_need_wide_columns = 1; 18690de6c0dfSLen Brown } 1870388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1871388e9c81SLen Brown } 1872388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1873388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1874388e9c81SLen Brown continue; 18750de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18760de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18770de6c0dfSLen Brown sums_need_wide_columns = 1; 18780de6c0dfSLen Brown } 1879388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1880388e9c81SLen Brown } 1881c98d5d94SLen Brown } 1882c98d5d94SLen Brown 1883c98d5d94SLen Brown static unsigned long long rdtsc(void) 1884c98d5d94SLen Brown { 1885c98d5d94SLen Brown unsigned int low, high; 1886c98d5d94SLen Brown 1887c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high)); 1888c98d5d94SLen Brown 1889c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1890c98d5d94SLen Brown } 1891c98d5d94SLen Brown 1892c98d5d94SLen Brown /* 1893495c7654SLen Brown * Open a file, and exit on failure 1894495c7654SLen Brown */ 1895495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1896495c7654SLen Brown { 1897495c7654SLen Brown FILE *filep = fopen(path, mode); 1898495c7654SLen Brown 1899495c7654SLen Brown if (!filep) 1900495c7654SLen Brown err(1, "%s: open failed", path); 1901495c7654SLen Brown return filep; 1902495c7654SLen Brown } 19031b439f01SLen Brown 1904495c7654SLen Brown /* 1905495c7654SLen Brown * snapshot_sysfs_counter() 1906495c7654SLen Brown * 1907495c7654SLen Brown * return snapshot of given counter 1908495c7654SLen Brown */ 1909495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1910495c7654SLen Brown { 1911495c7654SLen Brown FILE *fp; 1912495c7654SLen Brown int retval; 1913495c7654SLen Brown unsigned long long counter; 1914495c7654SLen Brown 1915495c7654SLen Brown fp = fopen_or_die(path, "r"); 1916495c7654SLen Brown 1917495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1918495c7654SLen Brown if (retval != 1) 1919495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1920495c7654SLen Brown 1921495c7654SLen Brown fclose(fp); 1922495c7654SLen Brown 1923495c7654SLen Brown return counter; 1924495c7654SLen Brown } 1925495c7654SLen Brown 1926495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1927495c7654SLen Brown { 1928495c7654SLen Brown if (mp->msr_num != 0) { 1929495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1930495c7654SLen Brown return -1; 1931495c7654SLen Brown } else { 193246c27978SLen Brown char path[128 + PATH_BYTES]; 193341618e63SLen Brown 193441618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 19351b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path); 193641618e63SLen Brown 193741618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 193841618e63SLen Brown } else { 1939495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1940495c7654SLen Brown } 194141618e63SLen Brown } 1942495c7654SLen Brown 1943495c7654SLen Brown return 0; 1944495c7654SLen Brown } 1945495c7654SLen Brown 19466d6501d9SBorislav Petkov int get_epb(int cpu) 19476d6501d9SBorislav Petkov { 19486d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19497f1b11baSBorislav Petkov unsigned long long msr; 19506d6501d9SBorislav Petkov int ret, epb = -1; 19516d6501d9SBorislav Petkov FILE *fp; 19526d6501d9SBorislav Petkov 19536d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19546d6501d9SBorislav Petkov 19557f1b11baSBorislav Petkov fp = fopen(path, "r"); 19567f1b11baSBorislav Petkov if (!fp) 19577f1b11baSBorislav Petkov goto msr_fallback; 19586d6501d9SBorislav Petkov 19596d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19606d6501d9SBorislav Petkov if (ret != 1) 19616d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19626d6501d9SBorislav Petkov 19636d6501d9SBorislav Petkov fclose(fp); 19646d6501d9SBorislav Petkov 19656d6501d9SBorislav Petkov return epb; 19667f1b11baSBorislav Petkov 19677f1b11baSBorislav Petkov msr_fallback: 19687f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 19697f1b11baSBorislav Petkov 19707f1b11baSBorislav Petkov return msr & 0xf; 19716d6501d9SBorislav Petkov } 19726d6501d9SBorislav Petkov 19734c2122d4SLen Brown void get_apic_id(struct thread_data *t) 19744c2122d4SLen Brown { 197534041551SLen Brown unsigned int eax, ebx, ecx, edx; 197634041551SLen Brown 197734041551SLen Brown if (DO_BIC(BIC_APIC)) { 197834041551SLen Brown eax = ebx = ecx = edx = 0; 197934041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 198034041551SLen Brown 198134041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 198234041551SLen Brown } 198334041551SLen Brown 198434041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 198534041551SLen Brown return; 198634041551SLen Brown 1987c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 198834041551SLen Brown unsigned int topology_extensions; 198934041551SLen Brown 199034041551SLen Brown if (max_extended_level < 0x8000001e) 199134041551SLen Brown return; 19924c2122d4SLen Brown 19934c2122d4SLen Brown eax = ebx = ecx = edx = 0; 199434041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 199534041551SLen Brown topology_extensions = ecx & (1 << 22); 199634041551SLen Brown 199734041551SLen Brown if (topology_extensions == 0) 199834041551SLen Brown return; 199934041551SLen Brown 200034041551SLen Brown eax = ebx = ecx = edx = 0; 200134041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 200234041551SLen Brown 200334041551SLen Brown t->x2apic_id = eax; 200434041551SLen Brown return; 200534041551SLen Brown } 20064c2122d4SLen Brown 20074c2122d4SLen Brown if (!genuine_intel) 20084c2122d4SLen Brown return; 20094c2122d4SLen Brown 20104c2122d4SLen Brown if (max_level < 0xb) 20114c2122d4SLen Brown return; 20124c2122d4SLen Brown 20134c2122d4SLen Brown ecx = 0; 20144c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 20154c2122d4SLen Brown t->x2apic_id = edx; 20164c2122d4SLen Brown 201734041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 20181b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 20194c2122d4SLen Brown } 20204c2122d4SLen Brown 2021eae97e05SChen Yu int get_core_throt_cnt(int cpu, unsigned long long *cnt) 2022eae97e05SChen Yu { 2023eae97e05SChen Yu char path[128 + PATH_BYTES]; 2024eae97e05SChen Yu unsigned long long tmp; 2025eae97e05SChen Yu FILE *fp; 2026eae97e05SChen Yu int ret; 2027eae97e05SChen Yu 2028eae97e05SChen Yu sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu); 2029eae97e05SChen Yu fp = fopen(path, "r"); 2030eae97e05SChen Yu if (!fp) 2031eae97e05SChen Yu return -1; 2032eae97e05SChen Yu ret = fscanf(fp, "%lld", &tmp); 2033eae97e05SChen Yu if (ret != 1) 2034eae97e05SChen Yu return -1; 2035eae97e05SChen Yu fclose(fp); 2036eae97e05SChen Yu *cnt = tmp; 2037eae97e05SChen Yu 2038eae97e05SChen Yu return 0; 2039eae97e05SChen Yu } 2040eae97e05SChen Yu 2041495c7654SLen Brown /* 2042c98d5d94SLen Brown * get_counters(...) 2043c98d5d94SLen Brown * migrate to cpu 2044c98d5d94SLen Brown * acquire and record local counters for that cpu 2045c98d5d94SLen Brown */ 2046c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2047c98d5d94SLen Brown { 2048c98d5d94SLen Brown int cpu = t->cpu_id; 2049889facbeSLen Brown unsigned long long msr; 20500102b067SLen Brown int aperf_mperf_retry_count = 0; 2051388e9c81SLen Brown struct msr_counter *mp; 2052388e9c81SLen Brown int i; 2053c98d5d94SLen Brown 2054e52966c0SLen Brown if (cpu_migrate(cpu)) { 20553d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 205615aaa346SLen Brown return -1; 2057e52966c0SLen Brown } 205815aaa346SLen Brown 2059d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 2060d4794f25SYazen Ghannam 20614c2122d4SLen Brown if (first_counter_read) 20624c2122d4SLen Brown get_apic_id(t); 20630102b067SLen Brown retry: 2064c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 206515aaa346SLen Brown 20661b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 20670102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 20680102b067SLen Brown 20690102b067SLen Brown /* 20700102b067SLen Brown * The TSC, APERF and MPERF must be read together for 20710102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 20720102b067SLen Brown * 20730102b067SLen Brown * Unfortunately, APERF and MPERF are read by 20740102b067SLen Brown * individual system call, so delays may occur 20750102b067SLen Brown * between them. If the time to read them 20760102b067SLen Brown * varies by a large amount, we re-read them. 20770102b067SLen Brown */ 20780102b067SLen Brown 20790102b067SLen Brown /* 20800102b067SLen Brown * This initial dummy APERF read has been seen to 20810102b067SLen Brown * reduce jitter in the subsequent reads. 20820102b067SLen Brown */ 20830102b067SLen Brown 20849c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2085c98d5d94SLen Brown return -3; 20860102b067SLen Brown 20870102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 20880102b067SLen Brown 20890102b067SLen Brown tsc_before = t->tsc; 20900102b067SLen Brown 20910102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 20920102b067SLen Brown return -3; 20930102b067SLen Brown 20940102b067SLen Brown tsc_between = rdtsc(); 20950102b067SLen Brown 20969c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2097c98d5d94SLen Brown return -4; 20980102b067SLen Brown 20990102b067SLen Brown tsc_after = rdtsc(); 21000102b067SLen Brown 21010102b067SLen Brown aperf_time = tsc_between - tsc_before; 21020102b067SLen Brown mperf_time = tsc_after - tsc_between; 21030102b067SLen Brown 21040102b067SLen Brown /* 21050102b067SLen Brown * If the system call latency to read APERF and MPERF 21060102b067SLen Brown * differ by more than 2x, then try again. 21070102b067SLen Brown */ 21080102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 21090102b067SLen Brown aperf_mperf_retry_count++; 21100102b067SLen Brown if (aperf_mperf_retry_count < 5) 21110102b067SLen Brown goto retry; 21120102b067SLen Brown else 21131b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time); 21140102b067SLen Brown } 21150102b067SLen Brown aperf_mperf_retry_count = 0; 21160102b067SLen Brown 2117b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2118b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 211988c3281fSLen Brown } 212088c3281fSLen Brown 21212af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 21222af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 21232af4f9b8SLen Brown return -4; 21242af4f9b8SLen Brown 2125812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2126562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2127812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 21281ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 21291ed51011SLen Brown return -5; 21301ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 21311ed51011SLen Brown } 21320539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2133144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2134144b44b1SLen Brown return -6; 2135144b44b1SLen Brown } 2136144b44b1SLen Brown 2137388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2138495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2139388e9c81SLen Brown return -10; 2140388e9c81SLen Brown } 2141388e9c81SLen Brown 2142c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2143c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2144f4fdf2b4SLen Brown goto done; 2145c98d5d94SLen Brown 21461e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2147c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2148c98d5d94SLen Brown return -6; 2149144b44b1SLen Brown } 2150144b44b1SLen Brown 21511e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2152c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2153c98d5d94SLen Brown return -7; 21541e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2155fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2156fb5d4327SDasaratharaman Chandramouli return -7; 2157103a8feaSLen Brown } 215815aaa346SLen Brown 21597ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2160c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2161c98d5d94SLen Brown return -8; 21627ab5ff49SZhang Rui else if (t->is_atom) { 21637ab5ff49SZhang Rui /* 21647ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 21657ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 21667ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 21677ab5ff49SZhang Rui * accturate cc6 residency. 21687ab5ff49SZhang Rui */ 21697ab5ff49SZhang Rui c->c6 -= c->c7; 21707ab5ff49SZhang Rui } 21717ab5ff49SZhang Rui } 2172c98d5d94SLen Brown 21730539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 21740539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 21750539ba11SLen Brown return -8; 21760539ba11SLen Brown 2177812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2178889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2179889facbeSLen Brown return -9; 218055279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 2181889facbeSLen Brown } 2182889facbeSLen Brown 2183eae97e05SChen Yu if (DO_BIC(BIC_CORE_THROT_CNT)) 2184eae97e05SChen Yu get_core_throt_cnt(cpu, &c->core_throt_cnt); 2185eae97e05SChen Yu 21869392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21879392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 21889392bd98SCalvin Walton return -14; 21899392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 21909392bd98SCalvin Walton } 21919392bd98SCalvin Walton 2192388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2193495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2194388e9c81SLen Brown return -10; 2195388e9c81SLen Brown } 2196889facbeSLen Brown 2197c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2198c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2199f4fdf2b4SLen Brown goto done; 220015aaa346SLen Brown 2201a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 22020b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 22030b2bb692SLen Brown return -10; 2204a99d8730SLen Brown } 2205a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 22060b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 22070b2bb692SLen Brown return -11; 2208a99d8730SLen Brown } 2209a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 22100b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 22110b2bb692SLen Brown return -12; 2212a99d8730SLen Brown } 2213a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 22140b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 22150b2bb692SLen Brown return -13; 22160b2bb692SLen Brown } 22170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2218c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2219c98d5d94SLen Brown return -9; 22200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 22210539ba11SLen Brown if (do_slm_cstates) { 22220539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 22230539ba11SLen Brown return -10; 22240539ba11SLen Brown } else { 2225c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2226c98d5d94SLen Brown return -10; 22270539ba11SLen Brown } 22280539ba11SLen Brown } 22290539ba11SLen Brown 22300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2231c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2232c98d5d94SLen Brown return -11; 22330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2234c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2235c98d5d94SLen Brown return -12; 22360f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2237ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2238ca58710fSKristen Carlson Accardi return -13; 22390f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2240ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2241ca58710fSKristen Carlson Accardi return -13; 22420f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2243ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2244ca58710fSKristen Carlson Accardi return -13; 22450f47c08dSLen Brown 2246be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2247be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2248be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2249be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2250be0e54c4SLen Brown 2251889facbeSLen Brown if (do_rapl & RAPL_PKG) { 22529972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2253889facbeSLen Brown return -13; 22549972d5d8SChen Yu p->energy_pkg = msr; 2255889facbeSLen Brown } 22569148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 22579972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2258889facbeSLen Brown return -14; 22599972d5d8SChen Yu p->energy_cores = msr; 2260889facbeSLen Brown } 2261889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 22629972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2263889facbeSLen Brown return -15; 22649972d5d8SChen Yu p->energy_dram = msr; 2265889facbeSLen Brown } 2266889facbeSLen Brown if (do_rapl & RAPL_GFX) { 22679972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2268889facbeSLen Brown return -16; 22699972d5d8SChen Yu p->energy_gfx = msr; 2270889facbeSLen Brown } 2271889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 22729972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2273889facbeSLen Brown return -16; 22749972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2275889facbeSLen Brown } 2276889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 22779972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2278889facbeSLen Brown return -16; 22799972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2280889facbeSLen Brown } 22813316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22829972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 22833316f99aSCalvin Walton return -13; 22849972d5d8SChen Yu p->energy_pkg = msr; 22853316f99aSCalvin Walton } 2286812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2287889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2288889facbeSLen Brown return -17; 228955279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 2290889facbeSLen Brown } 2291fdf676e5SLen Brown 2292812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2293fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2294fdf676e5SLen Brown 2295812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 229627d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 229727d47356SLen Brown 2298b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2299b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2300b4b91569SRafael Antognolli 2301388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2302495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2303388e9c81SLen Brown return -10; 2304388e9c81SLen Brown } 2305f4fdf2b4SLen Brown done: 2306f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2307388e9c81SLen Brown 230815aaa346SLen Brown return 0; 2309103a8feaSLen Brown } 2310103a8feaSLen Brown 2311ee7e38e3SLen Brown /* 2312ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2313ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2314ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2315ee7e38e3SLen Brown */ 2316ee7e38e3SLen Brown 2317ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2318ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2319ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2320ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2321ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2322ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2323ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2324ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2325ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2326ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2327ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2328ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 23290b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 23300b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2331445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2332445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2333ee7e38e3SLen Brown 2334ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2335ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 23361b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 23371b439f01SLen Brown }; 2338ee7e38e3SLen Brown 23391b439f01SLen Brown int nhm_pkg_cstate_limits[16] = 23401b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 234138c6663aSLen Brown PCLRSV, PCLRSV 234238c6663aSLen Brown }; 234338c6663aSLen Brown 23441b439f01SLen Brown int snb_pkg_cstate_limits[16] = 23451b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 234638c6663aSLen Brown PCLRSV, PCLRSV 234738c6663aSLen Brown }; 234838c6663aSLen Brown 23491b439f01SLen Brown int hsw_pkg_cstate_limits[16] = 23501b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 235138c6663aSLen Brown PCLRSV, PCLRSV 235238c6663aSLen Brown }; 235338c6663aSLen Brown 23541b439f01SLen Brown int slv_pkg_cstate_limits[16] = 23551b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 235638c6663aSLen Brown PCL__6, PCL__7 235738c6663aSLen Brown }; 235838c6663aSLen Brown 23591b439f01SLen Brown int amt_pkg_cstate_limits[16] = 23601b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 236138c6663aSLen Brown PCLRSV, PCLRSV 236238c6663aSLen Brown }; 236338c6663aSLen Brown 23641b439f01SLen Brown int phi_pkg_cstate_limits[16] = 23651b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 236638c6663aSLen Brown PCLRSV, PCLRSV 236738c6663aSLen Brown }; 236838c6663aSLen Brown 23691b439f01SLen Brown int glm_pkg_cstate_limits[16] = 23701b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 237138c6663aSLen Brown PCLRSV, PCLRSV 237238c6663aSLen Brown }; 237338c6663aSLen Brown 23741b439f01SLen Brown int skx_pkg_cstate_limits[16] = 23751b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 237638c6663aSLen Brown PCLRSV, PCLRSV 237738c6663aSLen Brown }; 237838c6663aSLen Brown 23791b439f01SLen Brown int icx_pkg_cstate_limits[16] = 23801b439f01SLen Brown { PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 238138c6663aSLen Brown PCLRSV, PCLRSV 238238c6663aSLen Brown }; 2383a2b7b749SLen Brown 23841b439f01SLen Brown static void calculate_tsc_tweak() 2385a2b7b749SLen Brown { 2386a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2387a2b7b749SLen Brown } 2388a2b7b749SLen Brown 2389aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2390aeb01e6dSChen Yu 23911b439f01SLen Brown static void dump_nhm_platform_info(void) 2392103a8feaSLen Brown { 2393103a8feaSLen Brown unsigned long long msr; 2394103a8feaSLen Brown unsigned int ratio; 2395103a8feaSLen Brown 2396ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2397103a8feaSLen Brown 2398b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 23996574a5d5SLen Brown 2400103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 24011b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 2402103a8feaSLen Brown 2403103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 24041b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 2405103a8feaSLen Brown 24067ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2407b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2408bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 240967920418SLen Brown 2410aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2411aeb01e6dSChen Yu if (dis_cstate_prewake) 24121b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 2413aeb01e6dSChen Yu 2414fcd17211SLen Brown return; 2415fcd17211SLen Brown } 2416fcd17211SLen Brown 24171b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void) 2418fcd17211SLen Brown { 2419fcd17211SLen Brown unsigned long long msr; 2420fcd17211SLen Brown unsigned int ratio; 2421fcd17211SLen Brown 24227ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2423fcd17211SLen Brown 2424b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2425fcd17211SLen Brown 2426fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2427fcd17211SLen Brown if (ratio) 24281b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 2429fcd17211SLen Brown 2430fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2431fcd17211SLen Brown if (ratio) 24321b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 2433fcd17211SLen Brown return; 2434fcd17211SLen Brown } 2435fcd17211SLen Brown 24361b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void) 2437fcd17211SLen Brown { 2438fcd17211SLen Brown unsigned long long msr; 2439fcd17211SLen Brown unsigned int ratio; 24406574a5d5SLen Brown 24417ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 24426574a5d5SLen Brown 2443b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 24446574a5d5SLen Brown 24456574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 24466574a5d5SLen Brown if (ratio) 24471b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 24486574a5d5SLen Brown 24496574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 24506574a5d5SLen Brown if (ratio) 24511b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 24526574a5d5SLen Brown 24536574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 24546574a5d5SLen Brown if (ratio) 24551b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 24566574a5d5SLen Brown 24576574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 24586574a5d5SLen Brown if (ratio) 24591b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 24606574a5d5SLen Brown 24616574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 24626574a5d5SLen Brown if (ratio) 24631b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 24646574a5d5SLen Brown 24656574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 24666574a5d5SLen Brown if (ratio) 24671b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 24686574a5d5SLen Brown 24696574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 24706574a5d5SLen Brown if (ratio) 24711b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 24726574a5d5SLen Brown 24736574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 24746574a5d5SLen Brown if (ratio) 24751b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 2476103a8feaSLen Brown return; 2477fcd17211SLen Brown } 24781b439f01SLen Brown 247931e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 248031e07522SLen Brown { 248131e07522SLen Brown 248231e07522SLen Brown if (!genuine_intel) 248331e07522SLen Brown return 0; 248431e07522SLen Brown 248531e07522SLen Brown switch (model) { 248631e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 248731e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 248825368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 24895ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 249020de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 249131e07522SLen Brown return 1; 249231e07522SLen Brown } 249331e07522SLen Brown return 0; 249431e07522SLen Brown } 2495fcd17211SLen Brown 24961b439f01SLen Brown static void dump_turbo_ratio_limits(int family, int model) 2497fcd17211SLen Brown { 249831e07522SLen Brown unsigned long long msr, core_counts; 249931e07522SLen Brown unsigned int ratio, group_size; 2500103a8feaSLen Brown 25017ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2502b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 25036574a5d5SLen Brown 250431e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 250531e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 250631e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 250731e07522SLen Brown } else { 250831e07522SLen Brown core_counts = 0x0807060504030201; 250931e07522SLen Brown } 251031e07522SLen Brown 25116574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 251231e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 25136574a5d5SLen Brown if (ratio) 251431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 251531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25166574a5d5SLen Brown 25176574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 251831e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 25196574a5d5SLen Brown if (ratio) 252031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 252131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25226574a5d5SLen Brown 25236574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 252431e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 25256574a5d5SLen Brown if (ratio) 252631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 252731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25286574a5d5SLen Brown 25296574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 253031e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 25316574a5d5SLen Brown if (ratio) 253231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 253331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 25346574a5d5SLen Brown 2535103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 253631e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2537103a8feaSLen Brown if (ratio) 253831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 253931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2540103a8feaSLen Brown 2541103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 254231e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2543103a8feaSLen Brown if (ratio) 254431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 254531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2546103a8feaSLen Brown 2547103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 254831e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2549103a8feaSLen Brown if (ratio) 255031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 255131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2552103a8feaSLen Brown 2553103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 255431e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2555103a8feaSLen Brown if (ratio) 255631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 255731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2558fcd17211SLen Brown return; 2559fcd17211SLen Brown } 25603a9a941dSLen Brown 25611b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void) 25620f7887c4SLen Brown { 25630f7887c4SLen Brown unsigned long long msr; 25640f7887c4SLen Brown unsigned int ratio; 25650f7887c4SLen Brown 25660f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 25670f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25680f7887c4SLen Brown 25690f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25700f7887c4SLen Brown if (ratio) 25711b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 25720f7887c4SLen Brown 25730f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25740f7887c4SLen Brown if (ratio) 25751b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 25760f7887c4SLen Brown 25770f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25780f7887c4SLen Brown if (ratio) 25791b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 25800f7887c4SLen Brown 25810f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 25820f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25830f7887c4SLen Brown 25840f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 25850f7887c4SLen Brown if (ratio) 25861b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 25870f7887c4SLen Brown 25880f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25890f7887c4SLen Brown if (ratio) 25901b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 25910f7887c4SLen Brown 25920f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25930f7887c4SLen Brown if (ratio) 25941b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 25950f7887c4SLen Brown 25960f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25970f7887c4SLen Brown if (ratio) 25981b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 25990f7887c4SLen Brown } 26000f7887c4SLen Brown 26011b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void) 2602fb5d4327SDasaratharaman Chandramouli { 2603cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2604cbf97abaSHubert Chrzaniuk 2605fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2606cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2607cbf97abaSHubert Chrzaniuk int i, b_nr; 2608cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2609cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2610fb5d4327SDasaratharaman Chandramouli 2611ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2612fb5d4327SDasaratharaman Chandramouli 26131b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 2614fb5d4327SDasaratharaman Chandramouli 26151e3ec5cdSRandy Dunlap /* 2616fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2617cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2618cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2619fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2620fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2621fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2622fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2623fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2624fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2625fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2626fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2627fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2628fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2629fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2630fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2631fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2632fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2633fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2634fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2635fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2636fb5d4327SDasaratharaman Chandramouli */ 2637fb5d4327SDasaratharaman Chandramouli 2638cbf97abaSHubert Chrzaniuk b_nr = 0; 2639cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2640cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2641cbf97abaSHubert Chrzaniuk 2642cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2643fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2644cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2645fb5d4327SDasaratharaman Chandramouli 2646cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2647cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2648cbf97abaSHubert Chrzaniuk b_nr++; 2649fb5d4327SDasaratharaman Chandramouli } 2650cbf97abaSHubert Chrzaniuk 2651cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2652cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2653b7d8c148SLen Brown fprintf(outf, 2654710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2655cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2656fb5d4327SDasaratharaman Chandramouli } 2657fb5d4327SDasaratharaman Chandramouli 26581b439f01SLen Brown static void dump_nhm_cst_cfg(void) 2659fcd17211SLen Brown { 2660fcd17211SLen Brown unsigned long long msr; 2661fcd17211SLen Brown 26621df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2663fcd17211SLen Brown 26641df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2665fcd17211SLen Brown 26663e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2667fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2668fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2669fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2670fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 26711b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 2672ac980e13SArtem Bityutskiy 2673ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2674ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 26751b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2676ac980e13SArtem Bityutskiy } 2677ac980e13SArtem Bityutskiy 2678ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2679ac980e13SArtem Bityutskiy 2680fcd17211SLen Brown return; 2681103a8feaSLen Brown } 2682103a8feaSLen Brown 26831b439f01SLen Brown static void dump_config_tdp(void) 26846fb3143bSLen Brown { 26856fb3143bSLen Brown unsigned long long msr; 26866fb3143bSLen Brown 26876fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2688b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2689685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26906fb3143bSLen Brown 26916fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2692b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 26936fb3143bSLen Brown if (msr) { 2694685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2695685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2696685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2697685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 26986fb3143bSLen Brown } 2699b7d8c148SLen Brown fprintf(outf, ")\n"); 27006fb3143bSLen Brown 27016fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2702b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 27036fb3143bSLen Brown if (msr) { 2704685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2705685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2706685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2707685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 27086fb3143bSLen Brown } 2709b7d8c148SLen Brown fprintf(outf, ")\n"); 27106fb3143bSLen Brown 27116fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2712b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 27136fb3143bSLen Brown if ((msr) & 0x3) 2714b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2715b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2716b7d8c148SLen Brown fprintf(outf, ")\n"); 27176fb3143bSLen Brown 27186fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2719b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2720685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2721b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2722b7d8c148SLen Brown fprintf(outf, ")\n"); 27236fb3143bSLen Brown } 27245a63426eSLen Brown 27255a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 27265a63426eSLen Brown 27275a63426eSLen Brown void print_irtl(void) 27285a63426eSLen Brown { 27295a63426eSLen Brown unsigned long long msr; 27305a63426eSLen Brown 27315a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 27325a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 27335a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27345a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27355a63426eSLen Brown 27365a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 27375a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 27385a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27395a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27405a63426eSLen Brown 27415a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 27425a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 27435a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27445a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27455a63426eSLen Brown 27465a63426eSLen Brown if (!do_irtl_hsw) 27475a63426eSLen Brown return; 27485a63426eSLen Brown 27495a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 27505a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 27515a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27525a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27535a63426eSLen Brown 27545a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 27555a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 27565a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27575a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27585a63426eSLen Brown 27595a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 27605a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 27615a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27625a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27635a63426eSLen Brown 27645a63426eSLen Brown } 27651b439f01SLen Brown 276636229897SLen Brown void free_fd_percpu(void) 276736229897SLen Brown { 276836229897SLen Brown int i; 276936229897SLen Brown 277001a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 277136229897SLen Brown if (fd_percpu[i] != 0) 277236229897SLen Brown close(fd_percpu[i]); 277336229897SLen Brown } 277436229897SLen Brown 277536229897SLen Brown free(fd_percpu); 27766fb3143bSLen Brown } 27776fb3143bSLen Brown 2778c98d5d94SLen Brown void free_all_buffers(void) 2779103a8feaSLen Brown { 27800e2d8f05SLen Brown int i; 27810e2d8f05SLen Brown 2782c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2783c98d5d94SLen Brown cpu_present_set = NULL; 278436229897SLen Brown cpu_present_setsize = 0; 2785103a8feaSLen Brown 2786c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2787c98d5d94SLen Brown cpu_affinity_set = NULL; 2788c98d5d94SLen Brown cpu_affinity_setsize = 0; 2789103a8feaSLen Brown 2790c98d5d94SLen Brown free(thread_even); 2791c98d5d94SLen Brown free(core_even); 2792c98d5d94SLen Brown free(package_even); 2793103a8feaSLen Brown 2794c98d5d94SLen Brown thread_even = NULL; 2795c98d5d94SLen Brown core_even = NULL; 2796c98d5d94SLen Brown package_even = NULL; 2797103a8feaSLen Brown 2798c98d5d94SLen Brown free(thread_odd); 2799c98d5d94SLen Brown free(core_odd); 2800c98d5d94SLen Brown free(package_odd); 2801103a8feaSLen Brown 2802c98d5d94SLen Brown thread_odd = NULL; 2803c98d5d94SLen Brown core_odd = NULL; 2804c98d5d94SLen Brown package_odd = NULL; 2805103a8feaSLen Brown 2806c98d5d94SLen Brown free(output_buffer); 2807c98d5d94SLen Brown output_buffer = NULL; 2808c98d5d94SLen Brown outp = NULL; 280936229897SLen Brown 281036229897SLen Brown free_fd_percpu(); 2811562a2d37SLen Brown 2812562a2d37SLen Brown free(irq_column_2_cpu); 2813562a2d37SLen Brown free(irqs_per_cpu); 28140e2d8f05SLen Brown 28150e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 28160e2d8f05SLen Brown if (cpus[i].put_ids) 28170e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 28180e2d8f05SLen Brown } 28190e2d8f05SLen Brown free(cpus); 2820103a8feaSLen Brown } 2821103a8feaSLen Brown 282257a42a34SJosh Triplett /* 282395aebc44SJosh Triplett * Parse a file containing a single int. 28246de68fe1SLen Brown * Return 0 if file can not be opened 28256de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 282695aebc44SJosh Triplett */ 282795aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 282895aebc44SJosh Triplett { 282995aebc44SJosh Triplett va_list args; 283095aebc44SJosh Triplett char path[PATH_MAX]; 283195aebc44SJosh Triplett FILE *filep; 283295aebc44SJosh Triplett int value; 283395aebc44SJosh Triplett 283495aebc44SJosh Triplett va_start(args, fmt); 283595aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 283695aebc44SJosh Triplett va_end(args); 28376de68fe1SLen Brown filep = fopen(path, "r"); 28386de68fe1SLen Brown if (!filep) 28396de68fe1SLen Brown return 0; 2840b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2841b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 284295aebc44SJosh Triplett fclose(filep); 284395aebc44SJosh Triplett return value; 284495aebc44SJosh Triplett } 284595aebc44SJosh Triplett 284695aebc44SJosh Triplett /* 2847c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2848c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2849c98d5d94SLen Brown */ 2850c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2851c98d5d94SLen Brown { 285295aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2853103a8feaSLen Brown } 2854103a8feaSLen Brown 2855103a8feaSLen Brown int get_physical_package_id(int cpu) 2856103a8feaSLen Brown { 285795aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2858103a8feaSLen Brown } 2859103a8feaSLen Brown 28606de68fe1SLen Brown int get_die_id(int cpu) 28616de68fe1SLen Brown { 28626de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 28636de68fe1SLen Brown } 28646de68fe1SLen Brown 2865103a8feaSLen Brown int get_core_id(int cpu) 2866103a8feaSLen Brown { 286795aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2868103a8feaSLen Brown } 2869103a8feaSLen Brown 2870ef605741SPrarit Bhargava void set_node_data(void) 2871ef605741SPrarit Bhargava { 28722ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 28732ffbb224SPrarit Bhargava int cpu_count; 2874ef605741SPrarit Bhargava 28752ffbb224SPrarit Bhargava /* initialize logical_node_id */ 28762ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 28772ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2878ef605741SPrarit Bhargava 28792ffbb224SPrarit Bhargava cpu_count = 0; 28802ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 28812ffbb224SPrarit Bhargava lnode = 0; 28822ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 28832ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2884ef605741SPrarit Bhargava continue; 28852ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 28862ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 28872ffbb224SPrarit Bhargava continue; 28882ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2889ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28902ffbb224SPrarit Bhargava cpu_count++; 28912ffbb224SPrarit Bhargava /* 28922ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 28932ffbb224SPrarit Bhargava * the logical_node_id 28942ffbb224SPrarit Bhargava */ 28952ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 28961b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 28972ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 28982ffbb224SPrarit Bhargava cpu_count++; 2899ef605741SPrarit Bhargava } 29002ffbb224SPrarit Bhargava } 29012ffbb224SPrarit Bhargava lnode++; 29022ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 29032ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 29042ffbb224SPrarit Bhargava } 29052ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 29062ffbb224SPrarit Bhargava break; 29072ffbb224SPrarit Bhargava } 2908ef605741SPrarit Bhargava } 2909ef605741SPrarit Bhargava 2910ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2911c98d5d94SLen Brown { 2912c98d5d94SLen Brown char path[80]; 2913c98d5d94SLen Brown FILE *filep; 29140e2d8f05SLen Brown int i; 29150e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2916c98d5d94SLen Brown 29170e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 29181b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 29190e2d8f05SLen Brown filep = fopen(path, "r"); 29200e2d8f05SLen Brown if (!filep) 29210e2d8f05SLen Brown continue; 29220e2d8f05SLen Brown fclose(filep); 29230e2d8f05SLen Brown return i; 29240e2d8f05SLen Brown } 29250e2d8f05SLen Brown return -1; 2926e275b388SDasaratharaman Chandramouli } 2927c98d5d94SLen Brown 29280e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 29290e2d8f05SLen Brown { 29300e2d8f05SLen Brown char path[80], character; 29310e2d8f05SLen Brown FILE *filep; 29320e2d8f05SLen Brown unsigned long map; 29338cb48b32SPrarit Bhargava int so, shift, sib_core; 29340e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 29350e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 29360e2d8f05SLen Brown size_t size; 29378cb48b32SPrarit Bhargava int thread_id = 0; 29380e2d8f05SLen Brown 29390e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 29408cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 29418cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 29420e2d8f05SLen Brown if (!thiscpu->put_ids) 29430e2d8f05SLen Brown return -1; 29440e2d8f05SLen Brown 29450e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 29460e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 29470e2d8f05SLen Brown 29481b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 29493d7772eaSLen Brown filep = fopen(path, "r"); 29503d7772eaSLen Brown 29513d7772eaSLen Brown if (!filep) { 29523d7772eaSLen Brown warnx("%s: open failed", path); 29533d7772eaSLen Brown return -1; 29543d7772eaSLen Brown } 29550e2d8f05SLen Brown do { 29560e2d8f05SLen Brown offset -= BITMASK_SIZE; 29578173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 29588173c336SBen Hutchings err(1, "%s: failed to parse file", path); 29590e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 29600e2d8f05SLen Brown if ((map >> shift) & 0x1) { 29618cb48b32SPrarit Bhargava so = shift + offset; 29628cb48b32SPrarit Bhargava sib_core = get_core_id(so); 29638cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 29648cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 29651b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0)) 29661b439f01SLen Brown cpus[so].thread_id = thread_id++; 29678cb48b32SPrarit Bhargava } 29680e2d8f05SLen Brown } 29690e2d8f05SLen Brown } 29700e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2971c98d5d94SLen Brown fclose(filep); 29720e2d8f05SLen Brown 29730e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2974c98d5d94SLen Brown } 2975c98d5d94SLen Brown 2976c98d5d94SLen Brown /* 2977c98d5d94SLen Brown * run func(thread, core, package) in topology order 2978c98d5d94SLen Brown * skip non-present cpus 2979103a8feaSLen Brown */ 2980103a8feaSLen Brown 2981c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 2982c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2983c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2984c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 29851b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 2986c98d5d94SLen Brown { 298740f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2988c98d5d94SLen Brown 2989c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 299040f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 29911b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 29921b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 2993c98d5d94SLen Brown struct thread_data *t, *t2; 2994c98d5d94SLen Brown struct core_data *c, *c2; 2995c98d5d94SLen Brown struct pkg_data *p, *p2; 2996c98d5d94SLen Brown 29971b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 2998c98d5d94SLen Brown 2999c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 3000c98d5d94SLen Brown continue; 3001c98d5d94SLen Brown 30021b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no); 3003c98d5d94SLen Brown 30041b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 30051b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no); 3006c98d5d94SLen Brown 3007c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 3008c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 3009c98d5d94SLen Brown 3010c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 3011c98d5d94SLen Brown if (retval) 3012c98d5d94SLen Brown return retval; 3013c98d5d94SLen Brown } 3014c98d5d94SLen Brown } 3015c98d5d94SLen Brown } 301640f5cfe7SPrarit Bhargava } 3017c98d5d94SLen Brown return 0; 3018c98d5d94SLen Brown } 3019c98d5d94SLen Brown 3020c98d5d94SLen Brown /* 3021c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 3022c98d5d94SLen Brown * return max_cpu number 3023c98d5d94SLen Brown */ 3024c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int)) 3025103a8feaSLen Brown { 3026103a8feaSLen Brown FILE *fp; 3027c98d5d94SLen Brown int cpu_num; 3028103a8feaSLen Brown int retval; 3029103a8feaSLen Brown 303057a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 3031103a8feaSLen Brown 3032103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 3033b2c95d90SJosh Triplett if (retval != 0) 3034b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 3035103a8feaSLen Brown 3036c98d5d94SLen Brown while (1) { 3037c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 3038103a8feaSLen Brown if (retval != 1) 3039103a8feaSLen Brown break; 3040103a8feaSLen Brown 3041c98d5d94SLen Brown retval = func(cpu_num); 3042c98d5d94SLen Brown if (retval) { 3043c98d5d94SLen Brown fclose(fp); 3044c98d5d94SLen Brown return (retval); 3045c98d5d94SLen Brown } 3046103a8feaSLen Brown } 3047103a8feaSLen Brown fclose(fp); 3048c98d5d94SLen Brown return 0; 3049103a8feaSLen Brown } 3050103a8feaSLen Brown 3051103a8feaSLen Brown void re_initialize(void) 3052103a8feaSLen Brown { 3053c98d5d94SLen Brown free_all_buffers(); 3054c98d5d94SLen Brown setup_all_buffers(); 30553d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 3056103a8feaSLen Brown } 3057103a8feaSLen Brown 3058843c5791SPrarit Bhargava void set_max_cpu_num(void) 3059843c5791SPrarit Bhargava { 3060843c5791SPrarit Bhargava FILE *filep; 30618201a028SPrarit Bhargava int base_cpu; 3062843c5791SPrarit Bhargava unsigned long dummy; 30638201a028SPrarit Bhargava char pathname[64]; 3064843c5791SPrarit Bhargava 30658201a028SPrarit Bhargava base_cpu = sched_getcpu(); 30668201a028SPrarit Bhargava if (base_cpu < 0) 30678201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 30681b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu); 30698201a028SPrarit Bhargava 30708201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 3071843c5791SPrarit Bhargava topo.max_cpu_num = 0; 3072843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 30730e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 3074843c5791SPrarit Bhargava fclose(filep); 3075843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 3076843c5791SPrarit Bhargava } 3077c98d5d94SLen Brown 3078103a8feaSLen Brown /* 3079c98d5d94SLen Brown * count_cpus() 3080c98d5d94SLen Brown * remember the last one seen, it will be the max 3081103a8feaSLen Brown */ 3082c98d5d94SLen Brown int count_cpus(int cpu) 3083103a8feaSLen Brown { 3084843c5791SPrarit Bhargava topo.num_cpus++; 3085c98d5d94SLen Brown return 0; 3086103a8feaSLen Brown } 30871b439f01SLen Brown 3088c98d5d94SLen Brown int mark_cpu_present(int cpu) 3089c98d5d94SLen Brown { 3090c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 309115aaa346SLen Brown return 0; 3092103a8feaSLen Brown } 3093103a8feaSLen Brown 30948cb48b32SPrarit Bhargava int init_thread_id(int cpu) 30958cb48b32SPrarit Bhargava { 30968cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 30978cb48b32SPrarit Bhargava return 0; 30988cb48b32SPrarit Bhargava } 30998cb48b32SPrarit Bhargava 3100562a2d37SLen Brown /* 3101562a2d37SLen Brown * snapshot_proc_interrupts() 3102562a2d37SLen Brown * 3103562a2d37SLen Brown * read and record summary of /proc/interrupts 3104562a2d37SLen Brown * 3105562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3106562a2d37SLen Brown */ 3107562a2d37SLen Brown int snapshot_proc_interrupts(void) 3108562a2d37SLen Brown { 3109562a2d37SLen Brown static FILE *fp; 3110562a2d37SLen Brown int column, retval; 3111562a2d37SLen Brown 3112562a2d37SLen Brown if (fp == NULL) 3113562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3114562a2d37SLen Brown else 3115562a2d37SLen Brown rewind(fp); 3116562a2d37SLen Brown 3117562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3118562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3119562a2d37SLen Brown int cpu_number; 3120562a2d37SLen Brown 3121562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3122562a2d37SLen Brown if (retval != 1) 3123562a2d37SLen Brown break; 3124562a2d37SLen Brown 3125562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3126562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3127562a2d37SLen Brown return 1; 3128562a2d37SLen Brown } 3129562a2d37SLen Brown 3130562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3131562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3132562a2d37SLen Brown } 3133562a2d37SLen Brown 3134562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3135562a2d37SLen Brown while (1) { 3136562a2d37SLen Brown int column; 3137562a2d37SLen Brown char buf[64]; 3138562a2d37SLen Brown 3139562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3140562a2d37SLen Brown if (retval != 1) 3141562a2d37SLen Brown break; 3142562a2d37SLen Brown 3143562a2d37SLen Brown /* read the count per cpu */ 3144562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3145562a2d37SLen Brown 3146562a2d37SLen Brown int cpu_number, irq_count; 3147562a2d37SLen Brown 3148562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3149562a2d37SLen Brown if (retval != 1) 3150562a2d37SLen Brown break; 3151562a2d37SLen Brown 3152562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3153562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3154562a2d37SLen Brown 3155562a2d37SLen Brown } 3156562a2d37SLen Brown 31571b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */ 3158562a2d37SLen Brown 3159562a2d37SLen Brown } 3160562a2d37SLen Brown return 0; 3161562a2d37SLen Brown } 31621b439f01SLen Brown 316327d47356SLen Brown /* 3164fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3165fdf676e5SLen Brown * 3166fdf676e5SLen Brown * record snapshot of 3167fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3168fdf676e5SLen Brown * 3169fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3170fdf676e5SLen Brown */ 3171fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3172fdf676e5SLen Brown { 3173fdf676e5SLen Brown FILE *fp; 3174fdf676e5SLen Brown int retval; 3175fdf676e5SLen Brown 3176fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3177fdf676e5SLen Brown 3178fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3179fdf676e5SLen Brown if (retval != 1) 3180fdf676e5SLen Brown err(1, "GFX rc6"); 3181fdf676e5SLen Brown 3182fdf676e5SLen Brown fclose(fp); 3183fdf676e5SLen Brown 3184fdf676e5SLen Brown return 0; 3185fdf676e5SLen Brown } 31861b439f01SLen Brown 3187fdf676e5SLen Brown /* 318827d47356SLen Brown * snapshot_gfx_mhz() 318927d47356SLen Brown * 319027d47356SLen Brown * record snapshot of 319127d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 319227d47356SLen Brown * 319327d47356SLen Brown * return 1 if config change requires a restart, else return 0 319427d47356SLen Brown */ 319527d47356SLen Brown int snapshot_gfx_mhz(void) 319627d47356SLen Brown { 319727d47356SLen Brown static FILE *fp; 319827d47356SLen Brown int retval; 319927d47356SLen Brown 320027d47356SLen Brown if (fp == NULL) 320127d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 320222048c54SLen Brown else { 320327d47356SLen Brown rewind(fp); 320422048c54SLen Brown fflush(fp); 320522048c54SLen Brown } 320627d47356SLen Brown 320727d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 320827d47356SLen Brown if (retval != 1) 320927d47356SLen Brown err(1, "GFX MHz"); 321027d47356SLen Brown 321127d47356SLen Brown return 0; 321227d47356SLen Brown } 3213562a2d37SLen Brown 3214562a2d37SLen Brown /* 3215b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3216b4b91569SRafael Antognolli * 3217b4b91569SRafael Antognolli * record snapshot of 3218b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3219b4b91569SRafael Antognolli * 3220b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3221b4b91569SRafael Antognolli */ 3222b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3223b4b91569SRafael Antognolli { 3224b4b91569SRafael Antognolli static FILE *fp; 3225b4b91569SRafael Antognolli int retval; 3226b4b91569SRafael Antognolli 3227b4b91569SRafael Antognolli if (fp == NULL) 3228b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3229b4b91569SRafael Antognolli else { 3230b4b91569SRafael Antognolli rewind(fp); 3231b4b91569SRafael Antognolli fflush(fp); 3232b4b91569SRafael Antognolli } 3233b4b91569SRafael Antognolli 3234b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3235b4b91569SRafael Antognolli if (retval != 1) 3236b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3237b4b91569SRafael Antognolli 3238b4b91569SRafael Antognolli return 0; 3239b4b91569SRafael Antognolli } 3240b4b91569SRafael Antognolli 3241b4b91569SRafael Antognolli /* 3242be0e54c4SLen Brown * snapshot_cpu_lpi() 3243be0e54c4SLen Brown * 3244be0e54c4SLen Brown * record snapshot of 3245be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3246be0e54c4SLen Brown */ 3247be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3248be0e54c4SLen Brown { 3249be0e54c4SLen Brown FILE *fp; 3250be0e54c4SLen Brown int retval; 3251be0e54c4SLen Brown 3252be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3253be0e54c4SLen Brown 3254be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 32555ea7647bSPrarit Bhargava if (retval != 1) { 32565ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 32575ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3258605736c6SGustavo A. R. Silva fclose(fp); 32595ea7647bSPrarit Bhargava return -1; 32605ea7647bSPrarit Bhargava } 3261be0e54c4SLen Brown 3262be0e54c4SLen Brown fclose(fp); 3263be0e54c4SLen Brown 3264be0e54c4SLen Brown return 0; 3265be0e54c4SLen Brown } 32661b439f01SLen Brown 3267be0e54c4SLen Brown /* 3268be0e54c4SLen Brown * snapshot_sys_lpi() 3269be0e54c4SLen Brown * 32701f81c5efSLen Brown * record snapshot of sys_lpi_file 3271be0e54c4SLen Brown */ 3272be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3273be0e54c4SLen Brown { 3274be0e54c4SLen Brown FILE *fp; 3275be0e54c4SLen Brown int retval; 3276be0e54c4SLen Brown 32771f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3278be0e54c4SLen Brown 3279be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 32805ea7647bSPrarit Bhargava if (retval != 1) { 32815ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 32825ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 328315423b95SColin Ian King fclose(fp); 32845ea7647bSPrarit Bhargava return -1; 32855ea7647bSPrarit Bhargava } 3286be0e54c4SLen Brown fclose(fp); 3287be0e54c4SLen Brown 3288be0e54c4SLen Brown return 0; 3289be0e54c4SLen Brown } 32901b439f01SLen Brown 3291be0e54c4SLen Brown /* 3292562a2d37SLen Brown * snapshot /proc and /sys files 3293562a2d37SLen Brown * 3294562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3295562a2d37SLen Brown */ 3296562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3297562a2d37SLen Brown { 3298218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3299562a2d37SLen Brown if (snapshot_proc_interrupts()) 3300562a2d37SLen Brown return 1; 3301562a2d37SLen Brown 3302812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3303fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3304fdf676e5SLen Brown 3305812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 330627d47356SLen Brown snapshot_gfx_mhz(); 330727d47356SLen Brown 3308b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3309b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3310b4b91569SRafael Antognolli 3311be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3312be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3313be0e54c4SLen Brown 3314be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3315be0e54c4SLen Brown snapshot_sys_lpi_us(); 3316be0e54c4SLen Brown 3317562a2d37SLen Brown return 0; 3318562a2d37SLen Brown } 3319562a2d37SLen Brown 33208aa2ed0bSLen Brown int exit_requested; 33218aa2ed0bSLen Brown 33228aa2ed0bSLen Brown static void signal_handler(int signal) 33238aa2ed0bSLen Brown { 33248aa2ed0bSLen Brown switch (signal) { 33258aa2ed0bSLen Brown case SIGINT: 33268aa2ed0bSLen Brown exit_requested = 1; 33278aa2ed0bSLen Brown if (debug) 33288aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 33298aa2ed0bSLen Brown break; 333007211960SLen Brown case SIGUSR1: 333107211960SLen Brown if (debug > 1) 333207211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 333307211960SLen Brown break; 33348aa2ed0bSLen Brown } 33358aa2ed0bSLen Brown } 33368aa2ed0bSLen Brown 33378aa2ed0bSLen Brown void setup_signal_handler(void) 33388aa2ed0bSLen Brown { 33398aa2ed0bSLen Brown struct sigaction sa; 33408aa2ed0bSLen Brown 33418aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 33428aa2ed0bSLen Brown 33438aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 33448aa2ed0bSLen Brown 33458aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 33468aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 334707211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 334807211960SLen Brown err(1, "sigaction SIGUSR1"); 33498aa2ed0bSLen Brown } 3350b9ad8ee0SLen Brown 335147936f94SArtem Bityutskiy void do_sleep(void) 3352b9ad8ee0SLen Brown { 3353c026c236SArtem Bityutskiy struct timeval tout; 3354c026c236SArtem Bityutskiy struct timespec rest; 3355b9ad8ee0SLen Brown fd_set readfds; 3356b9ad8ee0SLen Brown int retval; 3357b9ad8ee0SLen Brown 3358b9ad8ee0SLen Brown FD_ZERO(&readfds); 3359b9ad8ee0SLen Brown FD_SET(0, &readfds); 3360b9ad8ee0SLen Brown 3361c026c236SArtem Bityutskiy if (ignore_stdin) { 336247936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 336347936f94SArtem Bityutskiy return; 336447936f94SArtem Bityutskiy } 3365b9ad8ee0SLen Brown 3366c026c236SArtem Bityutskiy tout = interval_tv; 3367c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3368b9ad8ee0SLen Brown 3369b9ad8ee0SLen Brown if (retval == 1) { 3370b9ad8ee0SLen Brown switch (getc(stdin)) { 3371b9ad8ee0SLen Brown case 'q': 3372b9ad8ee0SLen Brown exit_requested = 1; 3373b9ad8ee0SLen Brown break; 3374c026c236SArtem Bityutskiy case EOF: 3375c026c236SArtem Bityutskiy /* 3376c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3377c026c236SArtem Bityutskiy * won't be any further input. 3378c026c236SArtem Bityutskiy */ 3379c026c236SArtem Bityutskiy ignore_stdin = 1; 3380c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3381c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3382c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3383c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3384b9ad8ee0SLen Brown } 3385b9ad8ee0SLen Brown } 3386b9ad8ee0SLen Brown } 338747936f94SArtem Bityutskiy 338887e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 338987e15da9SChen Yu { 339087e15da9SChen Yu int ret, idx; 339187e15da9SChen Yu unsigned long long msr_cur, msr_last; 339287e15da9SChen Yu 339387e15da9SChen Yu if (!per_cpu_msr_sum) 339487e15da9SChen Yu return 1; 339587e15da9SChen Yu 339687e15da9SChen Yu idx = offset_to_idx(offset); 339787e15da9SChen Yu if (idx < 0) 339887e15da9SChen Yu return idx; 339987e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 340087e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 340187e15da9SChen Yu if (ret) 340287e15da9SChen Yu return ret; 340387e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 340487e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 340587e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 340687e15da9SChen Yu 340787e15da9SChen Yu return 0; 340887e15da9SChen Yu } 340987e15da9SChen Yu 341087e15da9SChen Yu timer_t timerid; 341187e15da9SChen Yu 341287e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 341387e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 341487e15da9SChen Yu { 341587e15da9SChen Yu int i, ret; 341687e15da9SChen Yu int cpu = t->cpu_id; 341787e15da9SChen Yu 341887e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 341987e15da9SChen Yu unsigned long long msr_cur, msr_last; 342013a779deSCalvin Walton off_t offset; 342187e15da9SChen Yu 342287e15da9SChen Yu if (!idx_valid(i)) 342387e15da9SChen Yu continue; 342487e15da9SChen Yu offset = idx_to_offset(i); 342587e15da9SChen Yu if (offset < 0) 342687e15da9SChen Yu continue; 342787e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 342887e15da9SChen Yu if (ret) { 34291b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset); 343087e15da9SChen Yu continue; 343187e15da9SChen Yu } 343287e15da9SChen Yu 343387e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 343487e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 343587e15da9SChen Yu 343687e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 343787e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 343887e15da9SChen Yu } 343987e15da9SChen Yu return 0; 344087e15da9SChen Yu } 344187e15da9SChen Yu 34421b439f01SLen Brown static void msr_record_handler(union sigval v) 344387e15da9SChen Yu { 344487e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 344587e15da9SChen Yu } 344687e15da9SChen Yu 344787e15da9SChen Yu void msr_sum_record(void) 344887e15da9SChen Yu { 344987e15da9SChen Yu struct itimerspec its; 345087e15da9SChen Yu struct sigevent sev; 345187e15da9SChen Yu 345287e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 345387e15da9SChen Yu if (!per_cpu_msr_sum) { 345487e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 345587e15da9SChen Yu return; 345687e15da9SChen Yu } 345787e15da9SChen Yu /* 345887e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 345987e15da9SChen Yu */ 346087e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 346187e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 346287e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 346387e15da9SChen Yu 346487e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 346587e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 346687e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 346787e15da9SChen Yu goto release_msr; 346887e15da9SChen Yu } 346987e15da9SChen Yu 347087e15da9SChen Yu its.it_value.tv_sec = 0; 347187e15da9SChen Yu its.it_value.tv_nsec = 1; 347287e15da9SChen Yu /* 347387e15da9SChen Yu * A wraparound time has been calculated early. 347487e15da9SChen Yu * Some sources state that the peak power for a 347587e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 347687e15da9SChen Yu * use 2 * TDP for safety. 347787e15da9SChen Yu */ 347887e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 347987e15da9SChen Yu its.it_interval.tv_nsec = 0; 348087e15da9SChen Yu 348187e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 348287e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 348387e15da9SChen Yu goto release_timer; 348487e15da9SChen Yu } 348587e15da9SChen Yu return; 348687e15da9SChen Yu 348787e15da9SChen Yu release_timer: 348887e15da9SChen Yu timer_delete(timerid); 348987e15da9SChen Yu release_msr: 349087e15da9SChen Yu free(per_cpu_msr_sum); 349187e15da9SChen Yu } 34924c2122d4SLen Brown 349338c6663aSLen Brown /* 349438c6663aSLen Brown * set_my_sched_priority(pri) 349538c6663aSLen Brown * return previous 349638c6663aSLen Brown */ 349738c6663aSLen Brown int set_my_sched_priority(int priority) 349838c6663aSLen Brown { 349938c6663aSLen Brown int retval; 350038c6663aSLen Brown int original_priority; 350138c6663aSLen Brown 350238c6663aSLen Brown errno = 0; 350338c6663aSLen Brown original_priority = getpriority(PRIO_PROCESS, 0); 350438c6663aSLen Brown if (errno && (original_priority == -1)) 350538c6663aSLen Brown err(errno, "getpriority"); 350638c6663aSLen Brown 350738c6663aSLen Brown retval = setpriority(PRIO_PROCESS, 0, priority); 350838c6663aSLen Brown if (retval) 350938c6663aSLen Brown err(retval, "setpriority(%d)", priority); 351038c6663aSLen Brown 351138c6663aSLen Brown errno = 0; 351238c6663aSLen Brown retval = getpriority(PRIO_PROCESS, 0); 351338c6663aSLen Brown if (retval != priority) 351438c6663aSLen Brown err(-1, "getpriority(%d) != setpriority(%d)", retval, priority); 351538c6663aSLen Brown 351638c6663aSLen Brown return original_priority; 351738c6663aSLen Brown } 351838c6663aSLen Brown 3519103a8feaSLen Brown void turbostat_loop() 3520103a8feaSLen Brown { 3521c98d5d94SLen Brown int retval; 3522e52966c0SLen Brown int restarted = 0; 3523023fe0acSChen Yu int done_iters = 0; 3524c98d5d94SLen Brown 35258aa2ed0bSLen Brown setup_signal_handler(); 35268aa2ed0bSLen Brown 352738c6663aSLen Brown /* 352838c6663aSLen Brown * elevate own priority for interval mode 352938c6663aSLen Brown */ 353038c6663aSLen Brown set_my_sched_priority(-20); 353138c6663aSLen Brown 3532103a8feaSLen Brown restart: 3533e52966c0SLen Brown restarted++; 3534e52966c0SLen Brown 3535562a2d37SLen Brown snapshot_proc_sysfs_files(); 3536c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 35374c2122d4SLen Brown first_counter_read = 0; 3538d91bb17cSLen Brown if (retval < -1) { 3539d91bb17cSLen Brown exit(retval); 3540d91bb17cSLen Brown } else if (retval == -1) { 35413d7772eaSLen Brown if (restarted > 10) { 3542e52966c0SLen Brown exit(retval); 3543e52966c0SLen Brown } 3544c98d5d94SLen Brown re_initialize(); 3545c98d5d94SLen Brown goto restart; 3546c98d5d94SLen Brown } 3547e52966c0SLen Brown restarted = 0; 3548023fe0acSChen Yu done_iters = 0; 3549103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3550103a8feaSLen Brown 3551103a8feaSLen Brown while (1) { 3552c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3553103a8feaSLen Brown re_initialize(); 3554103a8feaSLen Brown goto restart; 3555103a8feaSLen Brown } 3556b9ad8ee0SLen Brown do_sleep(); 3557562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3558562a2d37SLen Brown goto restart; 3559c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3560d91bb17cSLen Brown if (retval < -1) { 3561d91bb17cSLen Brown exit(retval); 3562d91bb17cSLen Brown } else if (retval == -1) { 356315aaa346SLen Brown re_initialize(); 356415aaa346SLen Brown goto restart; 356515aaa346SLen Brown } 3566103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3567103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3568ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3569ba3dec99SLen Brown re_initialize(); 3570ba3dec99SLen Brown goto restart; 3571ba3dec99SLen Brown } 3572c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3573c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3574b7d8c148SLen Brown flush_output_stdout(); 35758aa2ed0bSLen Brown if (exit_requested) 35768aa2ed0bSLen Brown break; 3577023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3578023fe0acSChen Yu break; 3579b9ad8ee0SLen Brown do_sleep(); 3580562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3581562a2d37SLen Brown goto restart; 3582c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3583d91bb17cSLen Brown if (retval < -1) { 3584d91bb17cSLen Brown exit(retval); 3585d91bb17cSLen Brown } else if (retval == -1) { 3586103a8feaSLen Brown re_initialize(); 3587103a8feaSLen Brown goto restart; 3588103a8feaSLen Brown } 3589103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3590103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3591ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3592ba3dec99SLen Brown re_initialize(); 3593ba3dec99SLen Brown goto restart; 3594ba3dec99SLen Brown } 3595c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3596c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3597b7d8c148SLen Brown flush_output_stdout(); 35988aa2ed0bSLen Brown if (exit_requested) 35998aa2ed0bSLen Brown break; 3600023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3601023fe0acSChen Yu break; 3602103a8feaSLen Brown } 3603103a8feaSLen Brown } 3604103a8feaSLen Brown 3605103a8feaSLen Brown void check_dev_msr() 3606103a8feaSLen Brown { 3607103a8feaSLen Brown struct stat sb; 36087ce7d5deSPrarit Bhargava char pathname[32]; 3609103a8feaSLen Brown 36107ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 36117ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3612a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3613d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3614103a8feaSLen Brown } 3615103a8feaSLen Brown 3616fcaa681cSLen Brown /* 3617fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3618fcaa681cSLen Brown * return 0 on success 3619fcaa681cSLen Brown * return 1 on fail 3620fcaa681cSLen Brown */ 3621fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3622103a8feaSLen Brown { 3623fcaa681cSLen Brown cap_t caps; 3624fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3625fcaa681cSLen Brown 3626fcaa681cSLen Brown caps = cap_get_proc(); 3627fcaa681cSLen Brown if (caps == NULL) 3628fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3629fcaa681cSLen Brown 3630fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3631fcaa681cSLen Brown err(-6, "cap_get\n"); 3632fcaa681cSLen Brown 3633fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 36341b439f01SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3635fcaa681cSLen Brown return 1; 3636fcaa681cSLen Brown } 3637fcaa681cSLen Brown 3638fcaa681cSLen Brown if (cap_free(caps) == -1) 3639fcaa681cSLen Brown err(-6, "cap_free\n"); 3640fcaa681cSLen Brown 3641fcaa681cSLen Brown return 0; 3642fcaa681cSLen Brown } 36431b439f01SLen Brown 3644fcaa681cSLen Brown void check_permissions(void) 3645fcaa681cSLen Brown { 364698481e79SLen Brown int do_exit = 0; 36477ce7d5deSPrarit Bhargava char pathname[32]; 364898481e79SLen Brown 364998481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3650fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 365198481e79SLen Brown 365298481e79SLen Brown /* test file permissions */ 36537ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 36547ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 365598481e79SLen Brown do_exit++; 365698481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 365798481e79SLen Brown } 365898481e79SLen Brown 365998481e79SLen Brown /* if all else fails, thell them to be root */ 366098481e79SLen Brown if (do_exit) 3661b2c95d90SJosh Triplett if (getuid() != 0) 3662d7899447SLen Brown warnx("... or simply run as root"); 366398481e79SLen Brown 366498481e79SLen Brown if (do_exit) 366598481e79SLen Brown exit(-6); 3666103a8feaSLen Brown } 3667103a8feaSLen Brown 3668d7899447SLen Brown /* 3669d7899447SLen Brown * NHM adds support for additional MSRs: 3670d7899447SLen Brown * 3671d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3672d7899447SLen Brown * 3673ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 36741df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3675d7899447SLen Brown * 3676cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3677cf4cbe53SLen Brown * 3678d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3679d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3680d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3681d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3682d7899447SLen Brown * 3683ee7e38e3SLen Brown * Side effect: 36841df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 368533148d67SLen Brown * sets has_misc_feature_control 3686d7899447SLen Brown */ 3687ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3688103a8feaSLen Brown { 3689ee7e38e3SLen Brown unsigned long long msr; 369021ed5574SLen Brown unsigned int base_ratio; 3691ee7e38e3SLen Brown int *pkg_cstate_limits; 3692ee7e38e3SLen Brown 3693103a8feaSLen Brown if (!genuine_intel) 3694103a8feaSLen Brown return 0; 3695103a8feaSLen Brown 3696103a8feaSLen Brown if (family != 6) 3697103a8feaSLen Brown return 0; 3698103a8feaSLen Brown 369921ed5574SLen Brown bclk = discover_bclk(family, model); 370021ed5574SLen Brown 3701103a8feaSLen Brown switch (model) { 3702869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3703869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3704ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3705ee7e38e3SLen Brown break; 3706869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3707869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3708869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3709869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3710ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 371133148d67SLen Brown has_misc_feature_control = 1; 3712ee7e38e3SLen Brown break; 3713c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 37145e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3715869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 371677e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3717c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 37185e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3719869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3720af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3721af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3722ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 372333148d67SLen Brown has_misc_feature_control = 1; 3724ee7e38e3SLen Brown break; 3725d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3726d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 372733148d67SLen Brown has_misc_feature_control = 1; 3728d8ebb442SLen Brown break; 372925368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 373025368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits; 373125368d7cSChen Yu has_misc_feature_control = 1; 373225368d7cSChen Yu break; 3733f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3734cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 37355ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3736ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3737ee7e38e3SLen Brown break; 3738869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3739ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3740cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3741ee7e38e3SLen Brown break; 3742869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3743ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3744ee7e38e3SLen Brown break; 3745869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3746f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 37475ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3748f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 374920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3750445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3751e4085d54SLen Brown break; 3752103a8feaSLen Brown default: 3753103a8feaSLen Brown return 0; 3754103a8feaSLen Brown } 37551df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3756e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3757ee7e38e3SLen Brown 3758ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 375921ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 376021ed5574SLen Brown 376121ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 376221ed5574SLen Brown has_base_hz = 1; 3763ee7e38e3SLen Brown return 1; 3764103a8feaSLen Brown } 37651b439f01SLen Brown 37660f7887c4SLen Brown /* 3767495c7654SLen Brown * SLV client has support for unique MSRs: 37680f7887c4SLen Brown * 37690f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 37700f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 37710f7887c4SLen Brown */ 37720f7887c4SLen Brown 37730f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 37740f7887c4SLen Brown { 37750f7887c4SLen Brown if (!genuine_intel) 37760f7887c4SLen Brown return 0; 37770f7887c4SLen Brown 37780f7887c4SLen Brown switch (model) { 3779f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3780f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3781f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 37820f7887c4SLen Brown return 1; 37830f7887c4SLen Brown } 37840f7887c4SLen Brown return 0; 37850f7887c4SLen Brown } 37861b439f01SLen Brown 37877170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 37887170a374SLen Brown { 37897170a374SLen Brown 37907170a374SLen Brown if (!genuine_intel) 37917170a374SLen Brown return 0; 37927170a374SLen Brown 37937170a374SLen Brown switch (model) { 37945ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 37957170a374SLen Brown return 1; 37967170a374SLen Brown } 37977170a374SLen Brown return 0; 37987170a374SLen Brown } 37991b439f01SLen Brown 3800ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3801ade0ebacSLen Brown { 3802ade0ebacSLen Brown 3803ade0ebacSLen Brown if (!genuine_intel) 3804ade0ebacSLen Brown return 0; 3805ade0ebacSLen Brown 3806ade0ebacSLen Brown switch (model) { 3807ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3808ade0ebacSLen Brown return 1; 3809ade0ebacSLen Brown } 3810ade0ebacSLen Brown return 0; 3811ade0ebacSLen Brown } 38121b439f01SLen Brown 381334c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 381434c76197SLen Brown { 381534c76197SLen Brown 381634c76197SLen Brown if (!genuine_intel) 381734c76197SLen Brown return 0; 381834c76197SLen Brown 381934c76197SLen Brown switch (model) { 382034c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 382134c76197SLen Brown return 1; 382234c76197SLen Brown } 382334c76197SLen Brown return 0; 382434c76197SLen Brown } 382525368d7cSChen Yu 382625368d7cSChen Yu int is_icx(unsigned int family, unsigned int model) 382725368d7cSChen Yu { 382825368d7cSChen Yu 382925368d7cSChen Yu if (!genuine_intel) 383025368d7cSChen Yu return 0; 383125368d7cSChen Yu 383225368d7cSChen Yu switch (model) { 383325368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 383425368d7cSChen Yu return 1; 383525368d7cSChen Yu } 383625368d7cSChen Yu return 0; 383725368d7cSChen Yu } 383825368d7cSChen Yu 3839f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3840f6708400SChen Yu { 3841f6708400SChen Yu if (!genuine_intel) 3842f6708400SChen Yu return 0; 3843f6708400SChen Yu 3844f6708400SChen Yu switch (model) { 3845f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3846f6708400SChen Yu return 1; 3847f6708400SChen Yu } 3848f6708400SChen Yu return 0; 3849f6708400SChen Yu } 38501b439f01SLen Brown 385120de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 385220de0dabSAntti Laakso { 385320de0dabSAntti Laakso if (!genuine_intel) 385420de0dabSAntti Laakso return 0; 385520de0dabSAntti Laakso 385620de0dabSAntti Laakso switch (model) { 385720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 385820de0dabSAntti Laakso return 1; 385920de0dabSAntti Laakso } 386020de0dabSAntti Laakso return 0; 386120de0dabSAntti Laakso } 38620f7887c4SLen Brown 386331e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3864d7899447SLen Brown { 38650f7887c4SLen Brown if (has_slv_msrs(family, model)) 38660f7887c4SLen Brown return 0; 38670f7887c4SLen Brown 3868d7899447SLen Brown switch (model) { 3869d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3870869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3871869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3872d7899447SLen Brown return 0; 3873d7899447SLen Brown default: 3874d7899447SLen Brown return 1; 3875d7899447SLen Brown } 3876d7899447SLen Brown } 38771b439f01SLen Brown 38780f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 38790f7887c4SLen Brown { 38800f7887c4SLen Brown if (has_slv_msrs(family, model)) 38810f7887c4SLen Brown return 1; 38820f7887c4SLen Brown 38830f7887c4SLen Brown return 0; 38840f7887c4SLen Brown } 38851b439f01SLen Brown 38866574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 38876574a5d5SLen Brown { 38886574a5d5SLen Brown if (!genuine_intel) 38896574a5d5SLen Brown return 0; 38906574a5d5SLen Brown 38916574a5d5SLen Brown if (family != 6) 38926574a5d5SLen Brown return 0; 38936574a5d5SLen Brown 38946574a5d5SLen Brown switch (model) { 3895869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3896869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 38976574a5d5SLen Brown return 1; 38986574a5d5SLen Brown default: 38996574a5d5SLen Brown return 0; 39006574a5d5SLen Brown } 39016574a5d5SLen Brown } 39021b439f01SLen Brown 3903fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3904fcd17211SLen Brown { 3905fcd17211SLen Brown if (!genuine_intel) 3906fcd17211SLen Brown return 0; 3907fcd17211SLen Brown 3908fcd17211SLen Brown if (family != 6) 3909fcd17211SLen Brown return 0; 3910fcd17211SLen Brown 3911fcd17211SLen Brown switch (model) { 3912869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3913fcd17211SLen Brown return 1; 3914fcd17211SLen Brown default: 3915fcd17211SLen Brown return 0; 3916fcd17211SLen Brown } 3917fcd17211SLen Brown } 3918fcd17211SLen Brown 3919fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3920fb5d4327SDasaratharaman Chandramouli { 3921fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3922fb5d4327SDasaratharaman Chandramouli return 0; 3923fb5d4327SDasaratharaman Chandramouli 3924fb5d4327SDasaratharaman Chandramouli if (family != 6) 3925fb5d4327SDasaratharaman Chandramouli return 0; 3926fb5d4327SDasaratharaman Chandramouli 3927fb5d4327SDasaratharaman Chandramouli switch (model) { 3928869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3929fb5d4327SDasaratharaman Chandramouli return 1; 3930fb5d4327SDasaratharaman Chandramouli default: 3931fb5d4327SDasaratharaman Chandramouli return 0; 3932fb5d4327SDasaratharaman Chandramouli } 3933fb5d4327SDasaratharaman Chandramouli } 39341b439f01SLen Brown 393531e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 393631e07522SLen Brown { 393731e07522SLen Brown if (!genuine_intel) 393831e07522SLen Brown return 0; 393931e07522SLen Brown 394031e07522SLen Brown if (family != 6) 394131e07522SLen Brown return 0; 394231e07522SLen Brown 394331e07522SLen Brown switch (model) { 394431e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 394531e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 394625368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 394731e07522SLen Brown return 1; 394831e07522SLen Brown default: 394931e07522SLen Brown return 0; 395031e07522SLen Brown } 395131e07522SLen Brown } 39521b439f01SLen Brown 39536fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 39546fb3143bSLen Brown { 39556fb3143bSLen Brown if (!genuine_intel) 39566fb3143bSLen Brown return 0; 39576fb3143bSLen Brown 39586fb3143bSLen Brown if (family != 6) 39596fb3143bSLen Brown return 0; 39606fb3143bSLen Brown 39616fb3143bSLen Brown switch (model) { 3962869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3963c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3964869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 396577e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 39665e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3967c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 39685e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3969869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3970af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3971af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3972869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 397325368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 39746fb3143bSLen Brown 3975869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 39766fb3143bSLen Brown return 1; 39776fb3143bSLen Brown default: 39786fb3143bSLen Brown return 0; 39796fb3143bSLen Brown } 39806fb3143bSLen Brown } 39816fb3143bSLen Brown 39820b9a0b9bSZhang Rui /* 39830b9a0b9bSZhang Rui * tcc_offset_bits: 39840b9a0b9bSZhang Rui * 0: Tcc Offset not supported (Default) 39850b9a0b9bSZhang Rui * 6: Bit 29:24 of MSR_PLATFORM_INFO 39860b9a0b9bSZhang Rui * 4: Bit 27:24 of MSR_PLATFORM_INFO 39870b9a0b9bSZhang Rui */ 39880b9a0b9bSZhang Rui void check_tcc_offset(int model) 39890b9a0b9bSZhang Rui { 39900b9a0b9bSZhang Rui unsigned long long msr; 39910b9a0b9bSZhang Rui 39920b9a0b9bSZhang Rui if (!genuine_intel) 39930b9a0b9bSZhang Rui return; 39940b9a0b9bSZhang Rui 39950b9a0b9bSZhang Rui switch (model) { 39960b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE_L: 39970b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE: 39980b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE_L: 39990b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE: 40000b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE_L: 40010b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE: 40020b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE_L: 40030b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE: 40040b9a0b9bSZhang Rui case INTEL_FAM6_COMETLAKE: 40050b9a0b9bSZhang Rui if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) { 40060b9a0b9bSZhang Rui msr = (msr >> 30) & 1; 40070b9a0b9bSZhang Rui if (msr) 40080b9a0b9bSZhang Rui tcc_offset_bits = 6; 40090b9a0b9bSZhang Rui } 40100b9a0b9bSZhang Rui return; 40110b9a0b9bSZhang Rui default: 40120b9a0b9bSZhang Rui return; 40130b9a0b9bSZhang Rui } 40140b9a0b9bSZhang Rui } 40150b9a0b9bSZhang Rui 40161b439f01SLen Brown static void remove_underbar(char *s) 4017fecb3bc8SDavid Arcari { 4018fecb3bc8SDavid Arcari char *to = s; 4019fecb3bc8SDavid Arcari 4020fecb3bc8SDavid Arcari while (*s) { 4021fecb3bc8SDavid Arcari if (*s != '_') 4022fecb3bc8SDavid Arcari *to++ = *s; 4023fecb3bc8SDavid Arcari s++; 4024fecb3bc8SDavid Arcari } 4025fecb3bc8SDavid Arcari 4026fecb3bc8SDavid Arcari *to = 0; 4027fecb3bc8SDavid Arcari } 4028fecb3bc8SDavid Arcari 40291b439f01SLen Brown static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 4030fcd17211SLen Brown { 4031fcd17211SLen Brown if (!do_nhm_platform_info) 4032fcd17211SLen Brown return; 4033fcd17211SLen Brown 4034fcd17211SLen Brown dump_nhm_platform_info(); 4035fcd17211SLen Brown 4036fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 4037fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 4038fcd17211SLen Brown 4039fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 4040fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 4041fcd17211SLen Brown 404231e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 404331e07522SLen Brown dump_turbo_ratio_limits(family, model); 4044fcd17211SLen Brown 40450f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 40460f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 40470f7887c4SLen Brown 4048fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 4049fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 4050fb5d4327SDasaratharaman Chandramouli 40516fb3143bSLen Brown if (has_config_tdp(family, model)) 40526fb3143bSLen Brown dump_config_tdp(); 40536fb3143bSLen Brown 4054fcd17211SLen Brown dump_nhm_cst_cfg(); 4055fcd17211SLen Brown } 4056fcd17211SLen Brown 4057abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 4058abdcbdb2SLen Brown { 4059abdcbdb2SLen Brown FILE *input; 4060abdcbdb2SLen Brown char cpuidle_buf[64]; 4061abdcbdb2SLen Brown 4062abdcbdb2SLen Brown input = fopen(path, "r"); 4063abdcbdb2SLen Brown if (input == NULL) { 4064abdcbdb2SLen Brown if (debug) 4065abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 4066abdcbdb2SLen Brown return; 4067abdcbdb2SLen Brown } 4068abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 4069abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 4070abdcbdb2SLen Brown fclose(input); 4071abdcbdb2SLen Brown 4072abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 4073abdcbdb2SLen Brown } 40741b439f01SLen Brown 40751b439f01SLen Brown static void dump_sysfs_cstate_config(void) 407641618e63SLen Brown { 407741618e63SLen Brown char path[64]; 407841618e63SLen Brown char name_buf[16]; 407941618e63SLen Brown char desc[64]; 408041618e63SLen Brown FILE *input; 408141618e63SLen Brown int state; 408241618e63SLen Brown char *sp; 408341618e63SLen Brown 4084abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 4085abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 4086abdcbdb2SLen Brown return; 4087abdcbdb2SLen Brown } 4088abdcbdb2SLen Brown 4089abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 4090abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 4091abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 4092abdcbdb2SLen Brown 409341618e63SLen Brown for (state = 0; state < 10; ++state) { 409441618e63SLen Brown 40951b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 409641618e63SLen Brown input = fopen(path, "r"); 409741618e63SLen Brown if (input == NULL) 409841618e63SLen Brown continue; 40998173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 41008173c336SBen Hutchings err(1, "%s: failed to read file", path); 410141618e63SLen Brown 410241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 410341618e63SLen Brown sp = strchr(name_buf, '-'); 410441618e63SLen Brown if (!sp) 410541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 410641618e63SLen Brown *sp = '\0'; 410741618e63SLen Brown fclose(input); 410841618e63SLen Brown 4109fecb3bc8SDavid Arcari remove_underbar(name_buf); 4110fecb3bc8SDavid Arcari 41111b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state); 411241618e63SLen Brown input = fopen(path, "r"); 411341618e63SLen Brown if (input == NULL) 411441618e63SLen Brown continue; 41158173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 41168173c336SBen Hutchings err(1, "%s: failed to read file", path); 411741618e63SLen Brown 411841618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 411941618e63SLen Brown fclose(input); 412041618e63SLen Brown } 412141618e63SLen Brown } 41221b439f01SLen Brown 41231b439f01SLen Brown static void dump_sysfs_pstate_config(void) 41247293fccdSLen Brown { 41257293fccdSLen Brown char path[64]; 41267293fccdSLen Brown char driver_buf[64]; 41277293fccdSLen Brown char governor_buf[64]; 41287293fccdSLen Brown FILE *input; 41297293fccdSLen Brown int turbo; 41307293fccdSLen Brown 41311b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu); 41327293fccdSLen Brown input = fopen(path, "r"); 41337293fccdSLen Brown if (input == NULL) { 41340a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 41357293fccdSLen Brown return; 41367293fccdSLen Brown } 41378173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 41388173c336SBen Hutchings err(1, "%s: failed to read file", path); 41397293fccdSLen Brown fclose(input); 41407293fccdSLen Brown 41411b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu); 41427293fccdSLen Brown input = fopen(path, "r"); 41437293fccdSLen Brown if (input == NULL) { 41440a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 41457293fccdSLen Brown return; 41467293fccdSLen Brown } 41478173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 41488173c336SBen Hutchings err(1, "%s: failed to read file", path); 41497293fccdSLen Brown fclose(input); 41507293fccdSLen Brown 41517293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 41527293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 41537293fccdSLen Brown 41547293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 41557293fccdSLen Brown input = fopen(path, "r"); 41567293fccdSLen Brown if (input != NULL) { 41578173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 41588173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 41597293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 41607293fccdSLen Brown fclose(input); 41617293fccdSLen Brown } 41627293fccdSLen Brown 41637293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 41647293fccdSLen Brown input = fopen(path, "r"); 41657293fccdSLen Brown if (input != NULL) { 41668173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 41678173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 41687293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 41697293fccdSLen Brown fclose(input); 41707293fccdSLen Brown } 41717293fccdSLen Brown } 417241618e63SLen Brown 4173889facbeSLen Brown /* 4174889facbeSLen Brown * print_epb() 4175889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 4176889facbeSLen Brown */ 4177889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4178889facbeSLen Brown { 4179889facbeSLen Brown char *epb_string; 41806d6501d9SBorislav Petkov int cpu, epb; 4181889facbeSLen Brown 4182889facbeSLen Brown if (!has_epb) 4183889facbeSLen Brown return 0; 4184889facbeSLen Brown 4185889facbeSLen Brown cpu = t->cpu_id; 4186889facbeSLen Brown 4187889facbeSLen Brown /* EPB is per-package */ 4188889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4189889facbeSLen Brown return 0; 4190889facbeSLen Brown 4191889facbeSLen Brown if (cpu_migrate(cpu)) { 41923d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 4193889facbeSLen Brown return -1; 4194889facbeSLen Brown } 4195889facbeSLen Brown 41966d6501d9SBorislav Petkov epb = get_epb(cpu); 41976d6501d9SBorislav Petkov if (epb < 0) 4198889facbeSLen Brown return 0; 4199889facbeSLen Brown 42006d6501d9SBorislav Petkov switch (epb) { 4201889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 4202889facbeSLen Brown epb_string = "performance"; 4203889facbeSLen Brown break; 4204889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 4205889facbeSLen Brown epb_string = "balanced"; 4206889facbeSLen Brown break; 4207889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 4208889facbeSLen Brown epb_string = "powersave"; 4209889facbeSLen Brown break; 4210889facbeSLen Brown default: 4211889facbeSLen Brown epb_string = "custom"; 4212889facbeSLen Brown break; 4213889facbeSLen Brown } 42146d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 4215889facbeSLen Brown 4216889facbeSLen Brown return 0; 4217889facbeSLen Brown } 42181b439f01SLen Brown 42197f5c258eSLen Brown /* 42207f5c258eSLen Brown * print_hwp() 42217f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 42227f5c258eSLen Brown */ 42237f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 42247f5c258eSLen Brown { 42257f5c258eSLen Brown unsigned long long msr; 42267f5c258eSLen Brown int cpu; 42277f5c258eSLen Brown 42287f5c258eSLen Brown if (!has_hwp) 42297f5c258eSLen Brown return 0; 42307f5c258eSLen Brown 42317f5c258eSLen Brown cpu = t->cpu_id; 42327f5c258eSLen Brown 42337f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 42347f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 42357f5c258eSLen Brown return 0; 42367f5c258eSLen Brown 42377f5c258eSLen Brown if (cpu_migrate(cpu)) { 42383d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 42397f5c258eSLen Brown return -1; 42407f5c258eSLen Brown } 42417f5c258eSLen Brown 42427f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 42437f5c258eSLen Brown return 0; 42447f5c258eSLen Brown 42451b439f01SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 42467f5c258eSLen Brown 42477f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 42487f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 42497f5c258eSLen Brown return 0; 42507f5c258eSLen Brown 42517f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 42527f5c258eSLen Brown return 0; 42537f5c258eSLen Brown 4254b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 42556dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 42567f5c258eSLen Brown cpu, msr, 42577f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 42587f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 42591b439f01SLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr)); 42607f5c258eSLen Brown 42617f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 42627f5c258eSLen Brown return 0; 42637f5c258eSLen Brown 4264b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 42656dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 42667f5c258eSLen Brown cpu, msr, 42677f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 42687f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 42697f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 42707f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 42711b439f01SLen Brown (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1)); 42727f5c258eSLen Brown 42737f5c258eSLen Brown if (has_hwp_pkg) { 42747f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 42757f5c258eSLen Brown return 0; 42767f5c258eSLen Brown 4277b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 42786dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 42797f5c258eSLen Brown cpu, msr, 42807f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 42817f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 42827f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 42831b439f01SLen Brown (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3)); 42847f5c258eSLen Brown } 42857f5c258eSLen Brown if (has_hwp_notify) { 42867f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 42877f5c258eSLen Brown return 0; 42887f5c258eSLen Brown 4289b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 42907f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 42911b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis"); 42927f5c258eSLen Brown } 42937f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 42947f5c258eSLen Brown return 0; 42957f5c258eSLen Brown 4296b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 42977f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 42981b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-"); 4299889facbeSLen Brown 4300889facbeSLen Brown return 0; 4301889facbeSLen Brown } 4302889facbeSLen Brown 43033a9a941dSLen Brown /* 43043a9a941dSLen Brown * print_perf_limit() 43053a9a941dSLen Brown */ 43063a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 43073a9a941dSLen Brown { 43083a9a941dSLen Brown unsigned long long msr; 43093a9a941dSLen Brown int cpu; 43103a9a941dSLen Brown 43113a9a941dSLen Brown cpu = t->cpu_id; 43123a9a941dSLen Brown 43133a9a941dSLen Brown /* per-package */ 43143a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 43153a9a941dSLen Brown return 0; 43163a9a941dSLen Brown 43173a9a941dSLen Brown if (cpu_migrate(cpu)) { 43183d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 43193a9a941dSLen Brown return -1; 43203a9a941dSLen Brown } 43213a9a941dSLen Brown 43223a9a941dSLen Brown if (do_core_perf_limit_reasons) { 43233a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4324b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4325b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4326e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 43273a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4328e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4329e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4330e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4331e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4332e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4333e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4334e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4335e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4336e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4337e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 43381b439f01SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : ""); 4339b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4340e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 43413a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4342e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4343e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4344e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4345e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4346e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4347e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4348e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4349e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4350e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4351e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 43521b439f01SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : ""); 43533a9a941dSLen Brown 43543a9a941dSLen Brown } 43553a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 43563a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4357b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4358b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 43593a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 43603a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 43613a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 43623a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 43633a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 43643a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 43651b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4366b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 43673a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 43683a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 43693a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 43703a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 43713a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 43723a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 43731b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 43743a9a941dSLen Brown } 43753a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 43763a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4377b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4378b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 43793a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 43803a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 43813a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 43823a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 43831b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4384b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 43853a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 43863a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 43873a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 43883a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 43891b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 43903a9a941dSLen Brown } 43913a9a941dSLen Brown return 0; 43923a9a941dSLen Brown } 43933a9a941dSLen Brown 4394889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4395889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4396889facbeSLen Brown 43979392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4398144b44b1SLen Brown { 4399144b44b1SLen Brown unsigned long long msr; 4400144b44b1SLen Brown 4401144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 44027ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4403144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4404144b44b1SLen Brown 4405144b44b1SLen Brown switch (model) { 4406f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 44075ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4408144b44b1SLen Brown return 30.0; 4409144b44b1SLen Brown default: 4410144b44b1SLen Brown return 135.0; 4411144b44b1SLen Brown } 4412144b44b1SLen Brown } 4413144b44b1SLen Brown 44149392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 44159392bd98SCalvin Walton { 441633eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 441733eb8225SKim Phillips return 280.0; 44189392bd98SCalvin Walton } 44199392bd98SCalvin Walton 442040ee8e3bSAndrey Semin /* 442140ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 442240ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 442340ee8e3bSAndrey Semin */ 44241b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units) 442540ee8e3bSAndrey Semin { 442640ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 442740ee8e3bSAndrey Semin 442840ee8e3bSAndrey Semin switch (model) { 4429869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4430869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4431abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4432869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4433*6397b641SLen Brown case INTEL_FAM6_ICELAKE_X: /* ICX */ 443440ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 443540ee8e3bSAndrey Semin default: 443640ee8e3bSAndrey Semin return (rapl_energy_units); 443740ee8e3bSAndrey Semin } 443840ee8e3bSAndrey Semin } 443940ee8e3bSAndrey Semin 44409392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4441889facbeSLen Brown { 4442889facbeSLen Brown unsigned long long msr; 4443144b44b1SLen Brown unsigned int time_unit; 4444889facbeSLen Brown double tdp; 4445889facbeSLen Brown 4446889facbeSLen Brown if (family != 6) 4447889facbeSLen Brown return; 4448889facbeSLen Brown 4449889facbeSLen Brown switch (model) { 4450869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4451869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4452c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 445377e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 44545e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4455c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 44565e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4457144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4458812db3f7SLen Brown if (rapl_joules) { 4459812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4460812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4461812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4462812db3f7SLen Brown } else { 4463812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4464812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4465812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4466812db3f7SLen Brown } 4467889facbeSLen Brown break; 4468869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4469f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4470e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4471812db3f7SLen Brown if (rapl_joules) 4472812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4473812db3f7SLen Brown else 4474812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4475e4085d54SLen Brown break; 4476f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 44771b439f01SLen Brown do_rapl = 44781b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 44791b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4480f6708400SChen Yu if (rapl_joules) { 4481f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4482f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4483f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4484f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4485f6708400SChen Yu } else { 4486f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4487f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4488f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4489f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4490f6708400SChen Yu } 4491f6708400SChen Yu break; 449220de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 449320de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 449420de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 449520de0dabSAntti Laakso if (rapl_joules) 449620de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 449720de0dabSAntti Laakso else 449820de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 449920de0dabSAntti Laakso break; 4500af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4501af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 45021b439f01SLen Brown do_rapl = 45031b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 45041b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4505812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4506812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4507812db3f7SLen Brown if (rapl_joules) { 4508812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4509812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4510812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 451181824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4512812db3f7SLen Brown } else { 4513812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4514812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4515812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 451681824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4517812db3f7SLen Brown } 45180b2bb692SLen Brown break; 4519869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4520869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4521869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 452225368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4523869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 45241b439f01SLen Brown do_rapl = 45251b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 45261b439f01SLen Brown RAPL_PKG_POWER_INFO; 4527812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4528812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4529812db3f7SLen Brown if (rapl_joules) { 4530812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4531812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4532812db3f7SLen Brown } else { 4533812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4534812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4535812db3f7SLen Brown } 4536e6f9bb3cSLen Brown break; 4537869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4538869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 45391b439f01SLen Brown do_rapl = 45401b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | 45411b439f01SLen Brown RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4542812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4543812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4544812db3f7SLen Brown if (rapl_joules) { 4545812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4546812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4547812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4548812db3f7SLen Brown } else { 4549812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4550812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4551812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4552812db3f7SLen Brown } 4553144b44b1SLen Brown break; 4554f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 45555ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4556144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4557812db3f7SLen Brown if (rapl_joules) { 4558812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4559812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4560812db3f7SLen Brown } else { 4561812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4562812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4563812db3f7SLen Brown } 4564889facbeSLen Brown break; 45655ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 45661b439f01SLen Brown do_rapl = 45671b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 45681b439f01SLen Brown RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4569812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4570812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4571812db3f7SLen Brown if (rapl_joules) { 4572812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4573812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4574812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4575812db3f7SLen Brown } else { 4576812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4577812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4578812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4579812db3f7SLen Brown } 45800f644909SJacob Pan break; 4581889facbeSLen Brown default: 4582889facbeSLen Brown return; 4583889facbeSLen Brown } 4584889facbeSLen Brown 4585889facbeSLen Brown /* units on package 0, verify later other packages match */ 45867ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4587889facbeSLen Brown return; 4588889facbeSLen Brown 4589889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4590f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4591144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4592144b44b1SLen Brown else 4593889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4594889facbeSLen Brown 459540ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 459640ee8e3bSAndrey Semin 4597144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4598144b44b1SLen Brown if (time_unit == 0) 4599144b44b1SLen Brown time_unit = 0xA; 4600889facbeSLen Brown 4601144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4602144b44b1SLen Brown 46039392bd98SCalvin Walton tdp = get_tdp_intel(model); 4604889facbeSLen Brown 4605889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 460696e47158SLen Brown if (!quiet) 4607b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 46089392bd98SCalvin Walton } 4609889facbeSLen Brown 46109392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 46119392bd98SCalvin Walton { 46129392bd98SCalvin Walton unsigned long long msr; 46139392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 46149392bd98SCalvin Walton unsigned int has_rapl = 0; 46159392bd98SCalvin Walton double tdp; 46169392bd98SCalvin Walton 46179392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 46189392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 461933eb8225SKim Phillips /* RAPL (Fam 17h+) */ 46209392bd98SCalvin Walton has_rapl = edx & (1 << 14); 46219392bd98SCalvin Walton } 46229392bd98SCalvin Walton 462333eb8225SKim Phillips if (!has_rapl || family < 0x17) 4624889facbeSLen Brown return; 46259392bd98SCalvin Walton 46269392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 46273316f99aSCalvin Walton if (rapl_joules) { 46283316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 46299392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 46303316f99aSCalvin Walton } else { 46313316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 46329392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 46333316f99aSCalvin Walton } 46349392bd98SCalvin Walton 46359392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 46369392bd98SCalvin Walton return; 46379392bd98SCalvin Walton 46389392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 46399392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 46409392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 46419392bd98SCalvin Walton 46429cfa8e04SPu Wen tdp = get_tdp_amd(family); 46439392bd98SCalvin Walton 46449392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 46459392bd98SCalvin Walton if (!quiet) 46469392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 46479392bd98SCalvin Walton } 46489392bd98SCalvin Walton 46499392bd98SCalvin Walton /* 46509392bd98SCalvin Walton * rapl_probe() 46519392bd98SCalvin Walton * 46529392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 46539392bd98SCalvin Walton */ 46549392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 46559392bd98SCalvin Walton { 46569392bd98SCalvin Walton if (genuine_intel) 46579392bd98SCalvin Walton rapl_probe_intel(family, model); 4658c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 46599392bd98SCalvin Walton rapl_probe_amd(family, model); 4660889facbeSLen Brown } 4661889facbeSLen Brown 46621b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 46633a9a941dSLen Brown { 46643a9a941dSLen Brown if (!genuine_intel) 46653a9a941dSLen Brown return; 46663a9a941dSLen Brown 46673a9a941dSLen Brown if (family != 6) 46683a9a941dSLen Brown return; 46693a9a941dSLen Brown 46703a9a941dSLen Brown switch (model) { 4671c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 467277e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 46735e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 46743a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4675869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 46763a9a941dSLen Brown do_core_perf_limit_reasons = 1; 46773a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 46783a9a941dSLen Brown default: 46793a9a941dSLen Brown return; 46803a9a941dSLen Brown } 46813a9a941dSLen Brown } 46823a9a941dSLen Brown 4683ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4684ac980e13SArtem Bityutskiy { 46851b439f01SLen Brown if (is_skx(family, model) || is_bdx(family, model) || is_icx(family, model)) 4686ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4687ac980e13SArtem Bityutskiy } 4688ac980e13SArtem Bityutskiy 4689aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model) 4690aeb01e6dSChen Yu { 4691aeb01e6dSChen Yu if (is_icx(family, model)) 4692aeb01e6dSChen Yu dis_cstate_prewake = 1; 4693aeb01e6dSChen Yu } 4694aeb01e6dSChen Yu 4695889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4696889facbeSLen Brown { 4697889facbeSLen Brown unsigned long long msr; 4698f4896fa5SLen Brown unsigned int dts, dts2; 4699889facbeSLen Brown int cpu; 4700889facbeSLen Brown 4701889facbeSLen Brown if (!(do_dts || do_ptm)) 4702889facbeSLen Brown return 0; 4703889facbeSLen Brown 4704889facbeSLen Brown cpu = t->cpu_id; 4705889facbeSLen Brown 4706889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4707889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4708889facbeSLen Brown return 0; 4709889facbeSLen Brown 4710889facbeSLen Brown if (cpu_migrate(cpu)) { 47113d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4712889facbeSLen Brown return -1; 4713889facbeSLen Brown } 4714889facbeSLen Brown 4715889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4716889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4717889facbeSLen Brown return 0; 4718889facbeSLen Brown 4719889facbeSLen Brown dts = (msr >> 16) & 0x7F; 47201b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts); 4721889facbeSLen Brown 4722889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4723889facbeSLen Brown return 0; 4724889facbeSLen Brown 4725889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4726889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4727b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 472855279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4729889facbeSLen Brown } 4730889facbeSLen Brown 4731f4896fa5SLen Brown if (do_dts && debug) { 4732889facbeSLen Brown unsigned int resolution; 4733889facbeSLen Brown 4734889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4735889facbeSLen Brown return 0; 4736889facbeSLen Brown 4737889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4738889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4739b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 474055279aefSZhang Rui cpu, msr, tj_max - dts, resolution); 4741889facbeSLen Brown 4742889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4743889facbeSLen Brown return 0; 4744889facbeSLen Brown 4745889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4746889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4747b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 474855279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4749889facbeSLen Brown } 4750889facbeSLen Brown 4751889facbeSLen Brown return 0; 4752889facbeSLen Brown } 4753889facbeSLen Brown 4754889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4755889facbeSLen Brown { 47566b398625SSumeet Pawnikar fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n", 4757889facbeSLen Brown cpu, label, 4758889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4759889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4760889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4761889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4762889facbeSLen Brown 4763889facbeSLen Brown return; 4764889facbeSLen Brown } 4765889facbeSLen Brown 4766889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4767889facbeSLen Brown { 4768889facbeSLen Brown unsigned long long msr; 47699392bd98SCalvin Walton const char *msr_name; 4770889facbeSLen Brown int cpu; 4771889facbeSLen Brown 4772889facbeSLen Brown if (!do_rapl) 4773889facbeSLen Brown return 0; 4774889facbeSLen Brown 4775889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4776889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4777889facbeSLen Brown return 0; 4778889facbeSLen Brown 4779889facbeSLen Brown cpu = t->cpu_id; 4780889facbeSLen Brown if (cpu_migrate(cpu)) { 47813d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4782889facbeSLen Brown return -1; 4783889facbeSLen Brown } 4784889facbeSLen Brown 47859392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 47869392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 47879392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 47889392bd98SCalvin Walton return -1; 47899392bd98SCalvin Walton } else { 47909392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4791889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4792889facbeSLen Brown return -1; 47939392bd98SCalvin Walton } 4794889facbeSLen Brown 47959392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4796144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 479796e47158SLen Brown 4798144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4799144b44b1SLen Brown 4800889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4801889facbeSLen Brown return -5; 4802889facbeSLen Brown 4803b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4804889facbeSLen Brown cpu, msr, 4805889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4806889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4807889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4808889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4809889facbeSLen Brown 4810144b44b1SLen Brown } 4811144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4812144b44b1SLen Brown 4813889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4814889facbeSLen Brown return -9; 4815889facbeSLen Brown 4816b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4817889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4818889facbeSLen Brown 4819889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 48206b398625SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n", 4821889facbeSLen Brown cpu, 4822889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4823889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4824889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4825889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4826f52ba931SSumeet Pawnikar 4827f52ba931SSumeet Pawnikar if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr)) 4828f52ba931SSumeet Pawnikar return -9; 4829f52ba931SSumeet Pawnikar 4830f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr); 4831f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n", 4832f52ba931SSumeet Pawnikar cpu, 4833f52ba931SSumeet Pawnikar ((msr >> 0) & 0x1FFF) * rapl_power_units, 4834f52ba931SSumeet Pawnikar (msr >> 31) & 1 ? "" : "UN"); 4835889facbeSLen Brown } 4836889facbeSLen Brown 48370b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4838889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4839889facbeSLen Brown return -6; 4840889facbeSLen Brown 4841b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4842889facbeSLen Brown cpu, msr, 4843889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4844889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4845889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4846889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 48470b2bb692SLen Brown } 48480b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4849889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4850889facbeSLen Brown return -9; 4851b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4852889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4853889facbeSLen Brown 4854889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4855889facbeSLen Brown } 4856144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4857889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4858889facbeSLen Brown return -7; 4859889facbeSLen Brown 4860b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4861144b44b1SLen Brown } 48629148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4863889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4864889facbeSLen Brown return -9; 4865b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4866889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4867889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4868889facbeSLen Brown } 4869889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4870889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4871889facbeSLen Brown return -8; 4872889facbeSLen Brown 4873b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4874889facbeSLen Brown 4875889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4876889facbeSLen Brown return -9; 4877b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4878889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4879889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4880889facbeSLen Brown } 4881889facbeSLen Brown return 0; 4882889facbeSLen Brown } 4883889facbeSLen Brown 4884d7899447SLen Brown /* 4885d7899447SLen Brown * SNB adds support for additional MSRs: 4886d7899447SLen Brown * 4887d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4888d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4889d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4890d7899447SLen Brown */ 4891103a8feaSLen Brown 4892d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4893103a8feaSLen Brown { 4894103a8feaSLen Brown if (!genuine_intel) 4895103a8feaSLen Brown return 0; 4896103a8feaSLen Brown 4897103a8feaSLen Brown switch (model) { 4898869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4899869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4900869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4901869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4902c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4903869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 490477e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 49055e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4906c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 49075e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4908869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4909af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4910af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4911869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 491225368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4913869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4914f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 49155ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4916f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 491720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4918103a8feaSLen Brown return 1; 4919103a8feaSLen Brown } 4920103a8feaSLen Brown return 0; 4921103a8feaSLen Brown } 4922103a8feaSLen Brown 4923d7899447SLen Brown /* 4924570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4925d7899447SLen Brown * 4926d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4927d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4928d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 49295a63426eSLen Brown * 49305a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 49315a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 49325a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 49335a63426eSLen Brown * 4934d7899447SLen Brown */ 4935570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4936ca58710fSKristen Carlson Accardi { 4937ca58710fSKristen Carlson Accardi if (!genuine_intel) 4938ca58710fSKristen Carlson Accardi return 0; 4939ca58710fSKristen Carlson Accardi 4940ca58710fSKristen Carlson Accardi switch (model) { 494177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4942c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4943af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4944af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4945869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4946f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4947f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4948ca58710fSKristen Carlson Accardi return 1; 4949ca58710fSKristen Carlson Accardi } 4950ca58710fSKristen Carlson Accardi return 0; 4951ca58710fSKristen Carlson Accardi } 4952ca58710fSKristen Carlson Accardi 49530b2bb692SLen Brown /* 49540b2bb692SLen Brown * SKL adds support for additional MSRS: 49550b2bb692SLen Brown * 49560b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 49570b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 49580b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 49590b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 49600b2bb692SLen Brown */ 49610b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 49620b2bb692SLen Brown { 49630b2bb692SLen Brown if (!genuine_intel) 49640b2bb692SLen Brown return 0; 49650b2bb692SLen Brown 49660b2bb692SLen Brown switch (model) { 4967af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4968af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 49690b2bb692SLen Brown return 1; 49700b2bb692SLen Brown } 49710b2bb692SLen Brown return 0; 49720b2bb692SLen Brown } 49730b2bb692SLen Brown 4974144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4975144b44b1SLen Brown { 4976144b44b1SLen Brown if (!genuine_intel) 4977144b44b1SLen Brown return 0; 4978144b44b1SLen Brown switch (model) { 4979f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 49805ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4981144b44b1SLen Brown return 1; 4982144b44b1SLen Brown } 4983144b44b1SLen Brown return 0; 4984144b44b1SLen Brown } 4985144b44b1SLen Brown 4986fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4987fb5d4327SDasaratharaman Chandramouli { 4988fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4989fb5d4327SDasaratharaman Chandramouli return 0; 4990fb5d4327SDasaratharaman Chandramouli switch (model) { 4991869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4992fb5d4327SDasaratharaman Chandramouli return 1; 4993fb5d4327SDasaratharaman Chandramouli } 4994fb5d4327SDasaratharaman Chandramouli return 0; 4995fb5d4327SDasaratharaman Chandramouli } 4996fb5d4327SDasaratharaman Chandramouli 4997997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4998997e5395SSrinivas Pandruvada { 4999997e5395SSrinivas Pandruvada if (!genuine_intel) 5000997e5395SSrinivas Pandruvada return 0; 5001997e5395SSrinivas Pandruvada 5002997e5395SSrinivas Pandruvada switch (model) { 5003af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 5004997e5395SSrinivas Pandruvada return 1; 5005997e5395SSrinivas Pandruvada } 5006997e5395SSrinivas Pandruvada 5007997e5395SSrinivas Pandruvada return 0; 5008997e5395SSrinivas Pandruvada } 5009997e5395SSrinivas Pandruvada 5010b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 5011b2b34dfeSHubert Chrzaniuk { 5012b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 5013b2b34dfeSHubert Chrzaniuk return 1024; 5014b2b34dfeSHubert Chrzaniuk return 1; 5015b2b34dfeSHubert Chrzaniuk } 5016b2b34dfeSHubert Chrzaniuk 5017144b44b1SLen Brown #define SLM_BCLK_FREQS 5 5018144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 }; 5019144b44b1SLen Brown 5020144b44b1SLen Brown double slm_bclk(void) 5021144b44b1SLen Brown { 5022144b44b1SLen Brown unsigned long long msr = 3; 5023144b44b1SLen Brown unsigned int i; 5024144b44b1SLen Brown double freq; 5025144b44b1SLen Brown 50267ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 5027b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 5028144b44b1SLen Brown 5029144b44b1SLen Brown i = msr & 0xf; 5030144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 5031b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 50320a91e551SColin Ian King i = 3; 5033144b44b1SLen Brown } 5034144b44b1SLen Brown freq = slm_freq_table[i]; 5035144b44b1SLen Brown 503696e47158SLen Brown if (!quiet) 5037b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 5038144b44b1SLen Brown 5039144b44b1SLen Brown return freq; 5040144b44b1SLen Brown } 5041144b44b1SLen Brown 5042103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 5043103a8feaSLen Brown { 5044121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 5045103a8feaSLen Brown return 100.00; 5046144b44b1SLen Brown else if (is_slm(family, model)) 5047144b44b1SLen Brown return slm_bclk(); 5048103a8feaSLen Brown else 5049103a8feaSLen Brown return 133.33; 5050103a8feaSLen Brown } 5051103a8feaSLen Brown 50527ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p) 50537ab5ff49SZhang Rui { 50547ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx; 50557ab5ff49SZhang Rui 50567ab5ff49SZhang Rui if (!genuine_intel) 50577ab5ff49SZhang Rui return 0; 50587ab5ff49SZhang Rui 50597ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) { 50607ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 50617ab5ff49SZhang Rui return -1; 50627ab5ff49SZhang Rui } 50637ab5ff49SZhang Rui 50647ab5ff49SZhang Rui if (max_level < 0x1a) 50657ab5ff49SZhang Rui return 0; 50667ab5ff49SZhang Rui 50677ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx); 50687ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF; 50697ab5ff49SZhang Rui if (eax == 0x20) 50707ab5ff49SZhang Rui t->is_atom = true; 50717ab5ff49SZhang Rui return 0; 50727ab5ff49SZhang Rui } 50737ab5ff49SZhang Rui 5074889facbeSLen Brown /* 5075889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 5076889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 5077889facbeSLen Brown * This is usually equal to tjMax. 5078889facbeSLen Brown * 5079889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 5080889facbeSLen Brown * but also allow cmdline over-ride with -T. 5081889facbeSLen Brown * 5082889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 5083889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 5084889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 5085889facbeSLen Brown */ 50866ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 50876ff7cb37SLen Brown { 5088b2b94be7SLen Brown unsigned long long msr; 508955279aefSZhang Rui unsigned int tcc_default, tcc_offset; 5090b2b94be7SLen Brown int cpu; 5091b2b94be7SLen Brown 509255279aefSZhang Rui /* tj_max is used only for dts or ptm */ 5093889facbeSLen Brown if (!(do_dts || do_ptm)) 5094889facbeSLen Brown return 0; 5095889facbeSLen Brown 5096889facbeSLen Brown /* this is a per-package concept */ 5097889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 5098889facbeSLen Brown return 0; 5099889facbeSLen Brown 5100b2b94be7SLen Brown cpu = t->cpu_id; 5101b2b94be7SLen Brown if (cpu_migrate(cpu)) { 5102b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 5103b2b94be7SLen Brown return -1; 5104b2b94be7SLen Brown } 5105b2b94be7SLen Brown 510655279aefSZhang Rui if (tj_max_override != 0) { 510755279aefSZhang Rui tj_max = tj_max_override; 51081b439f01SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max); 5109889facbeSLen Brown return 0; 5110889facbeSLen Brown } 5111889facbeSLen Brown 5112b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 5113b2b94be7SLen Brown if (!do_nhm_platform_info) 5114b2b94be7SLen Brown goto guess; 5115b2b94be7SLen Brown 5116b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 5117b2b94be7SLen Brown goto guess; 5118b2b94be7SLen Brown 511955279aefSZhang Rui tcc_default = (msr >> 16) & 0xFF; 5120b2b94be7SLen Brown 51210b9a0b9bSZhang Rui if (!quiet) { 51220b9a0b9bSZhang Rui switch (tcc_offset_bits) { 51230b9a0b9bSZhang Rui case 4: 51240b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0xF; 51250b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 512655279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 51270b9a0b9bSZhang Rui break; 51280b9a0b9bSZhang Rui case 6: 51290b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0x3F; 51300b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 513155279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 51320b9a0b9bSZhang Rui break; 51330b9a0b9bSZhang Rui default: 51341b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default); 51350b9a0b9bSZhang Rui break; 51360b9a0b9bSZhang Rui } 51370b9a0b9bSZhang Rui } 5138b2b94be7SLen Brown 513955279aefSZhang Rui if (!tcc_default) 5140b2b94be7SLen Brown goto guess; 5141b2b94be7SLen Brown 514255279aefSZhang Rui tj_max = tcc_default; 5143b2b94be7SLen Brown 5144889facbeSLen Brown return 0; 5145889facbeSLen Brown 5146b2b94be7SLen Brown guess: 514755279aefSZhang Rui tj_max = TJMAX_DEFAULT; 51481b439f01SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max); 5149889facbeSLen Brown 5150889facbeSLen Brown return 0; 5151889facbeSLen Brown } 515269807a63SLen Brown 5153aa8d8cc7SLen Brown void decode_feature_control_msr(void) 5154aa8d8cc7SLen Brown { 5155aa8d8cc7SLen Brown unsigned long long msr; 5156aa8d8cc7SLen Brown 5157f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 5158aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 51591b439f01SLen Brown base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : ""); 5160aa8d8cc7SLen Brown } 5161aa8d8cc7SLen Brown 516269807a63SLen Brown void decode_misc_enable_msr(void) 516369807a63SLen Brown { 516469807a63SLen Brown unsigned long long msr; 516569807a63SLen Brown 5166f26b1519SLen Brown if (!genuine_intel) 5167f26b1519SLen Brown return; 5168f26b1519SLen Brown 516969807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 5170e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 517169807a63SLen Brown base_cpu, msr, 5172e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 5173e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 5174fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 5175e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 5176e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 517769807a63SLen Brown } 517869807a63SLen Brown 517933148d67SLen Brown void decode_misc_feature_control(void) 518033148d67SLen Brown { 518133148d67SLen Brown unsigned long long msr; 518233148d67SLen Brown 518333148d67SLen Brown if (!has_misc_feature_control) 518433148d67SLen Brown return; 518533148d67SLen Brown 518633148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 51871b439f01SLen Brown fprintf(outf, 51881b439f01SLen Brown "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 51891b439f01SLen Brown base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "", 51901b439f01SLen Brown msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : ""); 519133148d67SLen Brown } 51921b439f01SLen Brown 5193f0057310SLen Brown /* 5194f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 5195f0057310SLen Brown * 5196f0057310SLen Brown * Decode the bits according to the Nehalem documentation 5197f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 5198f0057310SLen Brown * bit[1] less so... 5199f0057310SLen Brown */ 5200f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 5201f0057310SLen Brown { 5202f0057310SLen Brown unsigned long long msr; 5203f0057310SLen Brown 5204f0057310SLen Brown if (!do_nhm_platform_info) 5205f0057310SLen Brown return; 5206f0057310SLen Brown 5207cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 5208cf4cbe53SLen Brown return; 5209cf4cbe53SLen Brown 5210f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 5211ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 5212f0057310SLen Brown base_cpu, msr, 52131b439f01SLen Brown msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS"); 5214f0057310SLen Brown } 52151b439f01SLen Brown 521671616c8eSLen Brown /* 521771616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 521871616c8eSLen Brown * 521971616c8eSLen Brown * This MSRs are present on Silvermont processors, 522071616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 522171616c8eSLen Brown */ 522271616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 522371616c8eSLen Brown { 522471616c8eSLen Brown unsigned long long msr; 522571616c8eSLen Brown 522671616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 522771616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 522871616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 522971616c8eSLen Brown 523071616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 523171616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 523271616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 523371616c8eSLen Brown } 52347f5c258eSLen Brown 5235f5a4c76aSLen Brown /* 5236f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 5237f5a4c76aSLen Brown */ 5238f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 5239f5a4c76aSLen Brown { 5240f5a4c76aSLen Brown 5241f5a4c76aSLen Brown switch (model) { 5242f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 5243f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 5244f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 5245f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 5246f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 5247f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 5248f5a4c76aSLen Brown 5249f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 5250f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 5251f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 5252f5a4c76aSLen Brown 5253f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 5254f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 5255f5a4c76aSLen Brown 5256f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 52575ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 5258f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 5259f5a4c76aSLen Brown 5260af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 5261c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 5262af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 5263c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 5264081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 5265081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 5266af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 5267937807d3SLen Brown 5268af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 5269d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 52704bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 52714bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 5272e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 5273e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 5274e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 52755683460bSChen Yu case INTEL_FAM6_ALDERLAKE_L: 5276af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 5277b62b3184SZhang Rui 5278d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 5279d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 5280d7814c30SChen Yu 52816c5c6560SChen Yu case INTEL_FAM6_ICELAKE_D: 5282e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 528325368d7cSChen Yu return INTEL_FAM6_ICELAKE_X; 5284f5a4c76aSLen Brown } 5285f5a4c76aSLen Brown return model; 5286f5a4c76aSLen Brown } 5287d76bb7a0SLen Brown 5288d76bb7a0SLen Brown void print_dev_latency(void) 5289d76bb7a0SLen Brown { 5290d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5291d76bb7a0SLen Brown int fd; 5292d76bb7a0SLen Brown int value; 5293d76bb7a0SLen Brown int retval; 5294d76bb7a0SLen Brown 5295d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5296d76bb7a0SLen Brown if (fd < 0) { 5297d76bb7a0SLen Brown warn("fopen %s\n", path); 5298d76bb7a0SLen Brown return; 5299d76bb7a0SLen Brown } 5300d76bb7a0SLen Brown 5301d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5302d76bb7a0SLen Brown if (retval != sizeof(int)) { 5303d76bb7a0SLen Brown warn("read %s\n", path); 5304d76bb7a0SLen Brown close(fd); 5305d76bb7a0SLen Brown return; 5306d76bb7a0SLen Brown } 53071b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 5308d76bb7a0SLen Brown 5309d76bb7a0SLen Brown close(fd); 5310d76bb7a0SLen Brown } 5311d76bb7a0SLen Brown 53122af4f9b8SLen Brown /* 53132af4f9b8SLen Brown * Linux-perf manages the the HW instructions-retired counter 53142af4f9b8SLen Brown * by enabling when requested, and hiding rollover 53152af4f9b8SLen Brown */ 53162af4f9b8SLen Brown void linux_perf_init(void) 53172af4f9b8SLen Brown { 53182af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 53192af4f9b8SLen Brown return; 53202af4f9b8SLen Brown 53212af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 53222af4f9b8SLen Brown return; 53232af4f9b8SLen Brown 53242af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 53252af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 53262af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 53272af4f9b8SLen Brown 53282af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 53292af4f9b8SLen Brown } 53302af4f9b8SLen Brown 5331fcd17211SLen Brown void process_cpuid() 5332103a8feaSLen Brown { 533334041551SLen Brown unsigned int eax, ebx, ecx, edx; 533434041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5335b3a34e93SLen Brown unsigned int has_turbo; 5336ed0757b8SLen Brown unsigned long long ucode_patch = 0; 5337103a8feaSLen Brown 5338103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5339103a8feaSLen Brown 53405aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5341103a8feaSLen Brown 534234041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5343103a8feaSLen Brown genuine_intel = 1; 534434041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 534534041551SLen Brown authentic_amd = 1; 5346c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5347c1c10cc7SPu Wen hygon_genuine = 1; 5348103a8feaSLen Brown 534996e47158SLen Brown if (!quiet) 5350ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5351ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5352103a8feaSLen Brown 53535aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5354103a8feaSLen Brown family = (fms >> 8) & 0xf; 5355103a8feaSLen Brown model = (fms >> 4) & 0xf; 5356103a8feaSLen Brown stepping = fms & 0xf; 53575aa3d1a2SCalvin Walton if (family == 0xf) 53585aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 53595aa3d1a2SCalvin Walton if (family >= 6) 5360103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 536134041551SLen Brown ecx_flags = ecx; 536234041551SLen Brown edx_flags = edx; 5363103a8feaSLen Brown 5364ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 5365ed0757b8SLen Brown warnx("get_msr(UCODE)\n"); 5366ed0757b8SLen Brown 5367103a8feaSLen Brown /* 5368103a8feaSLen Brown * check max extended function levels of CPUID. 5369103a8feaSLen Brown * This is needed to check for invariant TSC. 5370103a8feaSLen Brown * This check is valid for both Intel and AMD. 5371103a8feaSLen Brown */ 5372103a8feaSLen Brown ebx = ecx = edx = 0; 53735aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5374103a8feaSLen Brown 537534041551SLen Brown if (!quiet) { 5376ed0757b8SLen Brown fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n", 53771b439f01SLen Brown family, model, stepping, family, model, stepping, 53781b439f01SLen Brown (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 5379ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 538034041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 538134041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 538234041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 538334041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 538434041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 538534041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 538634041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 538734041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 538834041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 53891b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-"); 539034041551SLen Brown } 539125368d7cSChen Yu if (genuine_intel) { 5392e9d3092fSZhang Rui model_orig = model; 5393f5a4c76aSLen Brown model = intel_model_duplicates(model); 539425368d7cSChen Yu } 539534041551SLen Brown 539634041551SLen Brown if (!(edx_flags & (1 << 5))) 539734041551SLen Brown errx(1, "CPUID: no MSR"); 539834041551SLen Brown 539961a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5400103a8feaSLen Brown 5401103a8feaSLen Brown /* 5402103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5403103a8feaSLen Brown * this check is valid for both Intel and AMD 5404103a8feaSLen Brown */ 54055aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 54068209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5407d7899447SLen Brown } 5408103a8feaSLen Brown 5409103a8feaSLen Brown /* 5410103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5411103a8feaSLen Brown * this check is valid for both Intel and AMD 5412103a8feaSLen Brown */ 5413103a8feaSLen Brown 54145aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 54158209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5416812db3f7SLen Brown if (has_aperf) { 5417812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5418812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5419812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5420812db3f7SLen Brown } 5421889facbeSLen Brown do_dts = eax & (1 << 0); 5422812db3f7SLen Brown if (do_dts) 5423812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5424b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5425889facbeSLen Brown do_ptm = eax & (1 << 6); 5426812db3f7SLen Brown if (do_ptm) 5427812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 54287f5c258eSLen Brown has_hwp = eax & (1 << 7); 54297f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 54307f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 54317f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 54327f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5433889facbeSLen Brown has_epb = ecx & (1 << 3); 5434889facbeSLen Brown 543596e47158SLen Brown if (!quiet) 5436b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 54377f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 54387f5c258eSLen Brown has_aperf ? "" : "No-", 5439b3a34e93SLen Brown has_turbo ? "" : "No-", 54407f5c258eSLen Brown do_dts ? "" : "No-", 54417f5c258eSLen Brown do_ptm ? "" : "No-", 54427f5c258eSLen Brown has_hwp ? "" : "No-", 54437f5c258eSLen Brown has_hwp_notify ? "" : "No-", 54447f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 54451b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 5446103a8feaSLen Brown 544796e47158SLen Brown if (!quiet) 544869807a63SLen Brown decode_misc_enable_msr(); 544969807a63SLen Brown 545096e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5451aa8d8cc7SLen Brown int has_sgx; 5452aa8d8cc7SLen Brown 5453aa8d8cc7SLen Brown ecx = 0; 5454aa8d8cc7SLen Brown 5455aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5456aa8d8cc7SLen Brown 5457aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5458aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5459aa8d8cc7SLen Brown 5460aa8d8cc7SLen Brown if (has_sgx) 5461aa8d8cc7SLen Brown decode_feature_control_msr(); 5462aa8d8cc7SLen Brown } 5463aa8d8cc7SLen Brown 546461a87ba7SLen Brown if (max_level >= 0x15) { 54658a5bdf41SLen Brown unsigned int eax_crystal; 54668a5bdf41SLen Brown unsigned int ebx_tsc; 54678a5bdf41SLen Brown 54688a5bdf41SLen Brown /* 54698a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 54708a5bdf41SLen Brown */ 54718a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 54725aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 54738a5bdf41SLen Brown 54748a5bdf41SLen Brown if (ebx_tsc != 0) { 54758a5bdf41SLen Brown 547696e47158SLen Brown if (!quiet && (ebx != 0)) 5477b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 54788a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 54798a5bdf41SLen Brown 54808a5bdf41SLen Brown if (crystal_hz == 0) 54818a5bdf41SLen Brown switch (model) { 5482af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5483e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5484e8efbc80SLen Brown break; 54855ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5486ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5487ec53e594SLen Brown break; 5488869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5489f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5490e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 54918a5bdf41SLen Brown break; 54928a5bdf41SLen Brown default: 54938a5bdf41SLen Brown crystal_hz = 0; 54948a5bdf41SLen Brown } 54958a5bdf41SLen Brown 54968a5bdf41SLen Brown if (crystal_hz) { 54978a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 549896e47158SLen Brown if (!quiet) 5499b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 55008a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 55018a5bdf41SLen Brown } 55028a5bdf41SLen Brown } 55038a5bdf41SLen Brown } 550461a87ba7SLen Brown if (max_level >= 0x16) { 550561a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 550661a87ba7SLen Brown 550761a87ba7SLen Brown /* 550861a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 550961a87ba7SLen Brown */ 551061a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 551161a87ba7SLen Brown 55125aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 551396e47158SLen Brown if (!quiet) 5514b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 551561a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 551661a87ba7SLen Brown } 55178a5bdf41SLen Brown 5518b2b34dfeSHubert Chrzaniuk if (has_aperf) 5519b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5520b2b34dfeSHubert Chrzaniuk 5521812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5522812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5523812db3f7SLen Brown 5524812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5525812db3f7SLen Brown do_nhm_platform_info = 1; 5526812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5527812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5528812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5529812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5530812db3f7SLen Brown } 5531d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5532812db3f7SLen Brown 5533812db3f7SLen Brown if (do_snb_cstates) 5534812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5535812db3f7SLen Brown 55365a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 55370f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 55380f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 55390f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 55400f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 55410f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 55420f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 55430f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 55440f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 55450539ba11SLen Brown if (has_slv_msrs(family, model)) { 55460f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 55470f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 55480f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 55490f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 55500539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 55510539ba11SLen Brown use_c1_residency_msr = 1; 55520539ba11SLen Brown } 555320de0dabSAntti Laakso if (is_jvl(family, model)) { 555420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 555520de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 555620de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 555720de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 555820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 555920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 556020de0dabSAntti Laakso } 55617170a374SLen Brown if (is_dnv(family, model)) { 55627170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 55637170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 55647170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 55657170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 55667170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 55677170a374SLen Brown use_c1_residency_msr = 1; 55687170a374SLen Brown } 556925368d7cSChen Yu if (is_skx(family, model) || is_icx(family, model)) { 557034c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 557134c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 557234c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 557334c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 557434c76197SLen Brown } 5575ade0ebacSLen Brown if (is_bdx(family, model)) { 5576ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5577ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5578ade0ebacSLen Brown } 5579570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5580c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 55810f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5582c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 55830f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5584c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 55850f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 55860f47c08dSLen Brown } 5587570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5588a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5589a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5590a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5591a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5592a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5593a99d8730SLen Brown } 5594144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5595fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5596103a8feaSLen Brown 55971b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model)) 5598562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5599103a8feaSLen Brown 560096e47158SLen Brown if (!quiet) 5601f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5602f0057310SLen Brown 560396e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 560471616c8eSLen Brown decode_c6_demotion_policy_msr(); 560571616c8eSLen Brown 5606889facbeSLen Brown rapl_probe(family, model); 56073a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5608ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5609889facbeSLen Brown 56100b9a0b9bSZhang Rui check_tcc_offset(model_orig); 56110b9a0b9bSZhang Rui 561296e47158SLen Brown if (!quiet) 56131b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5614fcd17211SLen Brown 561541618e63SLen Brown if (!quiet) 5616d76bb7a0SLen Brown print_dev_latency(); 5617d76bb7a0SLen Brown if (!quiet) 561841618e63SLen Brown dump_sysfs_cstate_config(); 56197293fccdSLen Brown if (!quiet) 56207293fccdSLen Brown dump_sysfs_pstate_config(); 562141618e63SLen Brown 56228c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5623a2b7b749SLen Brown calculate_tsc_tweak(); 5624a2b7b749SLen Brown 5625812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5626812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5627fdf676e5SLen Brown 5628812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5629812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 563027d47356SLen Brown 5631b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5632b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5633b4b91569SRafael Antognolli 5634be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5635be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5636be0e54c4SLen Brown else 5637be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5638be0e54c4SLen Brown 5639eae97e05SChen Yu if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK)) 5640eae97e05SChen Yu BIC_PRESENT(BIC_CORE_THROT_CNT); 5641eae97e05SChen Yu else 5642eae97e05SChen Yu BIC_NOT_PRESENT(BIC_CORE_THROT_CNT); 5643eae97e05SChen Yu 56441f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 56451f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5646be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 56471f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 56481f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 56491f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 56501f81c5efSLen Brown } else { 56511f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5652be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 56531f81c5efSLen Brown } 5654be0e54c4SLen Brown 565596e47158SLen Brown if (!quiet) 565633148d67SLen Brown decode_misc_feature_control(); 565733148d67SLen Brown 5658889facbeSLen Brown return; 5659103a8feaSLen Brown } 5660103a8feaSLen Brown 5661103a8feaSLen Brown /* 5662103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5663103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5664103a8feaSLen Brown */ 5665103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5666103a8feaSLen Brown { 5667103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5668103a8feaSLen Brown return 1; 5669103a8feaSLen Brown else 5670103a8feaSLen Brown return 0; 5671103a8feaSLen Brown } 5672103a8feaSLen Brown 5673103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5674103a8feaSLen Brown { 5675103a8feaSLen Brown return 0; 5676103a8feaSLen Brown } 5677103a8feaSLen Brown 5678c98d5d94SLen Brown void topology_probe() 5679c98d5d94SLen Brown { 5680c98d5d94SLen Brown int i; 5681c98d5d94SLen Brown int max_core_id = 0; 5682c98d5d94SLen Brown int max_package_id = 0; 56836de68fe1SLen Brown int max_die_id = 0; 5684c98d5d94SLen Brown int max_siblings = 0; 5685c98d5d94SLen Brown 5686c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5687843c5791SPrarit Bhargava set_max_cpu_num(); 5688c98d5d94SLen Brown topo.num_cpus = 0; 5689c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5690c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5691812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5692c98d5d94SLen Brown 5693d8af6f5fSLen Brown if (debug > 1) 5694b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5695c98d5d94SLen Brown 5696c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5697b2c95d90SJosh Triplett if (cpus == NULL) 5698b2c95d90SJosh Triplett err(1, "calloc cpus"); 5699c98d5d94SLen Brown 5700c98d5d94SLen Brown /* 5701c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5702c98d5d94SLen Brown */ 5703c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5704b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5705b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5706c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5707c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5708c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5709c98d5d94SLen Brown 5710c98d5d94SLen Brown /* 57111ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 57121ef7d21aSLen Brown */ 57131ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 57141ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 57151ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 57161ef7d21aSLen Brown err(1, "cpu%d not present", i); 57171ef7d21aSLen Brown } 57181ef7d21aSLen Brown 57191ef7d21aSLen Brown /* 5720c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5721c98d5d94SLen Brown */ 5722c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5723b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5724b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5725c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5726c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5727c98d5d94SLen Brown 57288cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5729c98d5d94SLen Brown 5730c98d5d94SLen Brown /* 5731c98d5d94SLen Brown * For online cpus 5732c98d5d94SLen Brown * find max_core_id, max_package_id 5733c98d5d94SLen Brown */ 5734c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5735c98d5d94SLen Brown int siblings; 5736c98d5d94SLen Brown 5737c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5738d8af6f5fSLen Brown if (debug > 1) 5739b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5740c98d5d94SLen Brown continue; 5741c98d5d94SLen Brown } 5742c98d5d94SLen Brown 57430e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 57440e2d8f05SLen Brown 57450e2d8f05SLen Brown /* get package information */ 5746c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5747c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5748c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5749c98d5d94SLen Brown 57506de68fe1SLen Brown /* get die information */ 57516de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 57526de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 57536de68fe1SLen Brown max_die_id = cpus[i].die_id; 57546de68fe1SLen Brown 57550e2d8f05SLen Brown /* get numa node information */ 5756ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5757ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5758ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 57590e2d8f05SLen Brown 57600e2d8f05SLen Brown /* get core information */ 57610e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 57620e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 57630e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 57640e2d8f05SLen Brown 57650e2d8f05SLen Brown /* get thread information */ 57660e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5767c98d5d94SLen Brown if (siblings > max_siblings) 5768c98d5d94SLen Brown max_siblings = siblings; 57694f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 57708cb48b32SPrarit Bhargava topo.num_cores++; 5771c98d5d94SLen Brown } 5772ef605741SPrarit Bhargava 577370a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5774d8af6f5fSLen Brown if (debug > 1) 57751b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node); 577670a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5777812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5778c98d5d94SLen Brown 57796de68fe1SLen Brown topo.num_die = max_die_id + 1; 57806de68fe1SLen Brown if (debug > 1) 57811b439f01SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die); 57826de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 57836de68fe1SLen Brown BIC_PRESENT(BIC_Die); 57846de68fe1SLen Brown 5785c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5786d8af6f5fSLen Brown if (debug > 1) 57871b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 57887da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5789812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5790c98d5d94SLen Brown 5791ef605741SPrarit Bhargava set_node_data(); 5792ef605741SPrarit Bhargava if (debug > 1) 579370a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 579401235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 579501235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5796ef605741SPrarit Bhargava 579770a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5798d8af6f5fSLen Brown if (debug > 1) 5799b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 58002ffbb224SPrarit Bhargava 58012ffbb224SPrarit Bhargava if (debug < 1) 58022ffbb224SPrarit Bhargava return; 58032ffbb224SPrarit Bhargava 58042ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 58050ec712e3SLen Brown if (cpu_is_not_present(i)) 58060ec712e3SLen Brown continue; 58072ffbb224SPrarit Bhargava fprintf(outf, 58086de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 58096de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 58101b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id); 58112ffbb224SPrarit Bhargava } 58122ffbb224SPrarit Bhargava 5813c98d5d94SLen Brown } 5814c98d5d94SLen Brown 58151b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 5816c98d5d94SLen Brown { 5817c98d5d94SLen Brown int i; 58181b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; 581940f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5820c98d5d94SLen Brown 582140f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5822c98d5d94SLen Brown if (*t == NULL) 5823c98d5d94SLen Brown goto error; 5824c98d5d94SLen Brown 582540f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5826c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5827c98d5d94SLen Brown 582840f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5829c98d5d94SLen Brown if (*c == NULL) 5830c98d5d94SLen Brown goto error; 5831c98d5d94SLen Brown 583240f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5833c98d5d94SLen Brown (*c)[i].core_id = -1; 5834c98d5d94SLen Brown 5835678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5836c98d5d94SLen Brown if (*p == NULL) 5837c98d5d94SLen Brown goto error; 5838c98d5d94SLen Brown 5839c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5840c98d5d94SLen Brown (*p)[i].package_id = i; 5841c98d5d94SLen Brown 5842c98d5d94SLen Brown return; 5843c98d5d94SLen Brown error: 5844b2c95d90SJosh Triplett err(1, "calloc counters"); 5845c98d5d94SLen Brown } 58461b439f01SLen Brown 5847c98d5d94SLen Brown /* 5848c98d5d94SLen Brown * init_counter() 5849c98d5d94SLen Brown * 5850c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5851c98d5d94SLen Brown */ 58521b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 5853c98d5d94SLen Brown { 58548cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 585540f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 58568cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 58578cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5858c98d5d94SLen Brown struct thread_data *t; 5859c98d5d94SLen Brown struct core_data *c; 5860c98d5d94SLen Brown struct pkg_data *p; 5861c98d5d94SLen Brown 586242dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 586342dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 586442dd4520SNathan Ciobanu */ 586542dd4520SNathan Ciobanu if (node_id < 0) 586642dd4520SNathan Ciobanu node_id = 0; 586742dd4520SNathan Ciobanu 586840f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 586940f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 58708cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5871c98d5d94SLen Brown 5872c98d5d94SLen Brown t->cpu_id = cpu_id; 58738cb48b32SPrarit Bhargava if (thread_id == 0) { 5874c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5875c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5876c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5877c98d5d94SLen Brown } 5878c98d5d94SLen Brown 58798cb48b32SPrarit Bhargava c->core_id = core_id; 58808cb48b32SPrarit Bhargava p->package_id = pkg_id; 5881c98d5d94SLen Brown } 5882c98d5d94SLen Brown 5883c98d5d94SLen Brown int initialize_counters(int cpu_id) 5884c98d5d94SLen Brown { 58858cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 58868cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5887c98d5d94SLen Brown return 0; 5888c98d5d94SLen Brown } 5889c98d5d94SLen Brown 5890c98d5d94SLen Brown void allocate_output_buffer() 5891c98d5d94SLen Brown { 5892eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5893c98d5d94SLen Brown outp = output_buffer; 5894b2c95d90SJosh Triplett if (outp == NULL) 5895b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5896c98d5d94SLen Brown } 58971b439f01SLen Brown 589836229897SLen Brown void allocate_fd_percpu(void) 589936229897SLen Brown { 590001a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 590136229897SLen Brown if (fd_percpu == NULL) 590236229897SLen Brown err(-1, "calloc fd_percpu"); 590336229897SLen Brown } 59041b439f01SLen Brown 5905562a2d37SLen Brown void allocate_irq_buffers(void) 5906562a2d37SLen Brown { 5907562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5908562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5909562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5910c98d5d94SLen Brown 591101a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5912562a2d37SLen Brown if (irqs_per_cpu == NULL) 591301a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5914562a2d37SLen Brown } 59151b439f01SLen Brown 5916c98d5d94SLen Brown void setup_all_buffers(void) 5917c98d5d94SLen Brown { 5918c98d5d94SLen Brown topology_probe(); 5919562a2d37SLen Brown allocate_irq_buffers(); 592036229897SLen Brown allocate_fd_percpu(); 5921c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5922c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5923c98d5d94SLen Brown allocate_output_buffer(); 5924c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5925c98d5d94SLen Brown } 59263b4d5c7fSAndy Shevchenko 59277ce7d5deSPrarit Bhargava void set_base_cpu(void) 59287ce7d5deSPrarit Bhargava { 59297ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 59307ce7d5deSPrarit Bhargava if (base_cpu < 0) 59317ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 59327ce7d5deSPrarit Bhargava 59337ce7d5deSPrarit Bhargava if (debug > 1) 5934b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 59357ce7d5deSPrarit Bhargava } 59367ce7d5deSPrarit Bhargava 5937103a8feaSLen Brown void turbostat_init() 5938103a8feaSLen Brown { 59397ce7d5deSPrarit Bhargava setup_all_buffers(); 59407ce7d5deSPrarit Bhargava set_base_cpu(); 5941103a8feaSLen Brown check_dev_msr(); 594298481e79SLen Brown check_permissions(); 5943fcd17211SLen Brown process_cpuid(); 59442af4f9b8SLen Brown linux_perf_init(); 5945103a8feaSLen Brown 594696e47158SLen Brown if (!quiet) 59477f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 59487f5c258eSLen Brown 594996e47158SLen Brown if (!quiet) 5950889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5951889facbeSLen Brown 595296e47158SLen Brown if (!quiet) 59533a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 59543a9a941dSLen Brown 595596e47158SLen Brown if (!quiet) 5956889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5957889facbeSLen Brown 5958889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5959889facbeSLen Brown 59607ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 59617ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 59627ab5ff49SZhang Rui 596396e47158SLen Brown if (!quiet) 5964889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 59655a63426eSLen Brown 596696e47158SLen Brown if (!quiet && do_irtl_snb) 59675a63426eSLen Brown print_irtl(); 5968103a8feaSLen Brown } 5969103a8feaSLen Brown 5970103a8feaSLen Brown int fork_it(char **argv) 5971103a8feaSLen Brown { 5972103a8feaSLen Brown pid_t child_pid; 5973d91bb17cSLen Brown int status; 5974d15cf7c1SLen Brown 5975218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5976d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 59774c2122d4SLen Brown first_counter_read = 0; 5978d91bb17cSLen Brown if (status) 5979d91bb17cSLen Brown exit(status); 5980d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5981d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5982103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5983103a8feaSLen Brown 5984103a8feaSLen Brown child_pid = fork(); 5985103a8feaSLen Brown if (!child_pid) { 5986103a8feaSLen Brown /* child */ 5987103a8feaSLen Brown execvp(argv[0], argv); 59880815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5989103a8feaSLen Brown } else { 5990103a8feaSLen Brown 5991103a8feaSLen Brown /* parent */ 5992b2c95d90SJosh Triplett if (child_pid == -1) 5993b2c95d90SJosh Triplett err(1, "fork"); 5994103a8feaSLen Brown 5995103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5996103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5997b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5998b2c95d90SJosh Triplett err(status, "waitpid"); 59992a954966SDavid Arcari 60002a954966SDavid Arcari if (WIFEXITED(status)) 60012a954966SDavid Arcari status = WEXITSTATUS(status); 6002103a8feaSLen Brown } 6003c98d5d94SLen Brown /* 6004c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 6005c98d5d94SLen Brown * because re-starting is problematic when forking 6006c98d5d94SLen Brown */ 6007218f0e8dSLen Brown snapshot_proc_sysfs_files(); 6008c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 6009103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 6010103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 6011ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 6012ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 6013ba3dec99SLen Brown else { 6014c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 6015c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 6016ba3dec99SLen Brown } 6017103a8feaSLen Brown 6018b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 6019b7d8c148SLen Brown 6020b7d8c148SLen Brown flush_output_stderr(); 6021103a8feaSLen Brown 6022d91bb17cSLen Brown return status; 6023103a8feaSLen Brown } 6024103a8feaSLen Brown 60253b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 60263b4d5c7fSAndy Shevchenko { 60273b4d5c7fSAndy Shevchenko int status; 60283b4d5c7fSAndy Shevchenko 6029218f0e8dSLen Brown snapshot_proc_sysfs_files(); 60303b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 60313b4d5c7fSAndy Shevchenko if (status) 60323b4d5c7fSAndy Shevchenko return status; 60333b4d5c7fSAndy Shevchenko 60343b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 60353b4d5c7fSAndy Shevchenko if (status) 60363b4d5c7fSAndy Shevchenko return status; 60373b4d5c7fSAndy Shevchenko 6038b7d8c148SLen Brown flush_output_stdout(); 60393b4d5c7fSAndy Shevchenko 60403b4d5c7fSAndy Shevchenko return status; 60413b4d5c7fSAndy Shevchenko } 60423b4d5c7fSAndy Shevchenko 60431b439f01SLen Brown void print_version() 60441b439f01SLen Brown { 60453c070b2aSLen Brown fprintf(outf, "turbostat version 21.05.04" " - Len Brown <lenb@kernel.org>\n"); 6046d8af6f5fSLen Brown } 6047d8af6f5fSLen Brown 6048495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 6049495c7654SLen Brown unsigned int width, enum counter_scope scope, 605041618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 6051388e9c81SLen Brown { 6052388e9c81SLen Brown struct msr_counter *msrp; 6053388e9c81SLen Brown 6054388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 6055388e9c81SLen Brown if (msrp == NULL) { 6056388e9c81SLen Brown perror("calloc"); 6057388e9c81SLen Brown exit(1); 6058388e9c81SLen Brown } 6059388e9c81SLen Brown 6060388e9c81SLen Brown msrp->msr_num = msr_num; 6061d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 6062495c7654SLen Brown if (path) 6063d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 6064388e9c81SLen Brown msrp->width = width; 6065388e9c81SLen Brown msrp->type = type; 6066388e9c81SLen Brown msrp->format = format; 606741618e63SLen Brown msrp->flags = flags; 6068388e9c81SLen Brown 6069388e9c81SLen Brown switch (scope) { 6070388e9c81SLen Brown 6071388e9c81SLen Brown case SCOPE_CPU: 6072388e9c81SLen Brown msrp->next = sys.tp; 6073388e9c81SLen Brown sys.tp = msrp; 6074678a3bd1SLen Brown sys.added_thread_counters++; 60750748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 60761b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS); 6077678a3bd1SLen Brown exit(-1); 6078678a3bd1SLen Brown } 6079388e9c81SLen Brown break; 6080388e9c81SLen Brown 6081388e9c81SLen Brown case SCOPE_CORE: 6082388e9c81SLen Brown msrp->next = sys.cp; 6083388e9c81SLen Brown sys.cp = msrp; 6084678a3bd1SLen Brown sys.added_core_counters++; 6085678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 60861b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS); 6087678a3bd1SLen Brown exit(-1); 6088678a3bd1SLen Brown } 6089388e9c81SLen Brown break; 6090388e9c81SLen Brown 6091388e9c81SLen Brown case SCOPE_PACKAGE: 6092388e9c81SLen Brown msrp->next = sys.pp; 6093388e9c81SLen Brown sys.pp = msrp; 6094678a3bd1SLen Brown sys.added_package_counters++; 6095678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 60961b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS); 6097678a3bd1SLen Brown exit(-1); 6098678a3bd1SLen Brown } 6099388e9c81SLen Brown break; 6100388e9c81SLen Brown } 6101388e9c81SLen Brown 6102388e9c81SLen Brown return 0; 6103388e9c81SLen Brown } 6104388e9c81SLen Brown 6105388e9c81SLen Brown void parse_add_command(char *add_command) 6106388e9c81SLen Brown { 6107388e9c81SLen Brown int msr_num = 0; 6108495c7654SLen Brown char *path = NULL; 61090f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 6110388e9c81SLen Brown int width = 64; 6111388e9c81SLen Brown int fail = 0; 6112388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 6113388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 6114388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 6115388e9c81SLen Brown 6116388e9c81SLen Brown while (add_command) { 6117388e9c81SLen Brown 6118388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 6119388e9c81SLen Brown goto next; 6120388e9c81SLen Brown 6121388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 6122388e9c81SLen Brown goto next; 6123388e9c81SLen Brown 6124495c7654SLen Brown if (*add_command == '/') { 6125495c7654SLen Brown path = add_command; 6126495c7654SLen Brown goto next; 6127495c7654SLen Brown } 6128495c7654SLen Brown 6129388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 6130388e9c81SLen Brown if ((width == 32) || (width == 64)) 6131388e9c81SLen Brown goto next; 6132388e9c81SLen Brown width = 64; 6133388e9c81SLen Brown } 6134388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6135388e9c81SLen Brown scope = SCOPE_CPU; 6136388e9c81SLen Brown goto next; 6137388e9c81SLen Brown } 6138388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6139388e9c81SLen Brown scope = SCOPE_CORE; 6140388e9c81SLen Brown goto next; 6141388e9c81SLen Brown } 6142388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6143388e9c81SLen Brown scope = SCOPE_PACKAGE; 6144388e9c81SLen Brown goto next; 6145388e9c81SLen Brown } 6146388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6147388e9c81SLen Brown type = COUNTER_CYCLES; 6148388e9c81SLen Brown goto next; 6149388e9c81SLen Brown } 6150388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6151388e9c81SLen Brown type = COUNTER_SECONDS; 6152388e9c81SLen Brown goto next; 6153388e9c81SLen Brown } 615441618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 615541618e63SLen Brown type = COUNTER_USEC; 615641618e63SLen Brown goto next; 615741618e63SLen Brown } 6158388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6159388e9c81SLen Brown format = FORMAT_RAW; 6160388e9c81SLen Brown goto next; 6161388e9c81SLen Brown } 6162388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6163388e9c81SLen Brown format = FORMAT_DELTA; 6164388e9c81SLen Brown goto next; 6165388e9c81SLen Brown } 6166388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6167388e9c81SLen Brown format = FORMAT_PERCENT; 6168388e9c81SLen Brown goto next; 6169388e9c81SLen Brown } 6170388e9c81SLen Brown 6171388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6172388e9c81SLen Brown char *eos; 6173388e9c81SLen Brown 6174388e9c81SLen Brown eos = strchr(name_buffer, ','); 6175388e9c81SLen Brown if (eos) 6176388e9c81SLen Brown *eos = '\0'; 6177388e9c81SLen Brown goto next; 6178388e9c81SLen Brown } 6179388e9c81SLen Brown 6180388e9c81SLen Brown next: 6181388e9c81SLen Brown add_command = strchr(add_command, ','); 6182495c7654SLen Brown if (add_command) { 6183495c7654SLen Brown *add_command = '\0'; 6184388e9c81SLen Brown add_command++; 6185495c7654SLen Brown } 6186388e9c81SLen Brown 6187388e9c81SLen Brown } 6188495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6189495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6190388e9c81SLen Brown fail++; 6191388e9c81SLen Brown } 6192388e9c81SLen Brown 6193388e9c81SLen Brown /* generate default column header */ 6194388e9c81SLen Brown if (*name_buffer == '\0') { 6195388e9c81SLen Brown if (width == 32) 61965f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6197388e9c81SLen Brown else 61985f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6199388e9c81SLen Brown } 6200388e9c81SLen Brown 620141618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6202388e9c81SLen Brown fail++; 6203388e9c81SLen Brown 6204388e9c81SLen Brown if (fail) { 6205388e9c81SLen Brown help(); 6206388e9c81SLen Brown exit(1); 6207388e9c81SLen Brown } 6208388e9c81SLen Brown } 620941618e63SLen Brown 62100fc521bcSZephaniah E. Loss-Cutler-Hull int is_deferred_add(char *name) 62110fc521bcSZephaniah E. Loss-Cutler-Hull { 62120fc521bcSZephaniah E. Loss-Cutler-Hull int i; 62130fc521bcSZephaniah E. Loss-Cutler-Hull 62140fc521bcSZephaniah E. Loss-Cutler-Hull for (i = 0; i < deferred_add_index; ++i) 62150fc521bcSZephaniah E. Loss-Cutler-Hull if (!strcmp(name, deferred_add_names[i])) 62160fc521bcSZephaniah E. Loss-Cutler-Hull return 1; 62170fc521bcSZephaniah E. Loss-Cutler-Hull return 0; 62180fc521bcSZephaniah E. Loss-Cutler-Hull } 62190fc521bcSZephaniah E. Loss-Cutler-Hull 6220dd778a5eSLen Brown int is_deferred_skip(char *name) 6221dd778a5eSLen Brown { 6222dd778a5eSLen Brown int i; 6223dd778a5eSLen Brown 6224dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6225dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6226dd778a5eSLen Brown return 1; 6227dd778a5eSLen Brown return 0; 6228dd778a5eSLen Brown } 6229dd778a5eSLen Brown 623041618e63SLen Brown void probe_sysfs(void) 623141618e63SLen Brown { 623241618e63SLen Brown char path[64]; 623341618e63SLen Brown char name_buf[16]; 623441618e63SLen Brown FILE *input; 623541618e63SLen Brown int state; 623641618e63SLen Brown char *sp; 623741618e63SLen Brown 62380748eaf0SLen Brown for (state = 10; state >= 0; --state) { 623941618e63SLen Brown 62401b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 624141618e63SLen Brown input = fopen(path, "r"); 624241618e63SLen Brown if (input == NULL) 624341618e63SLen Brown continue; 62448173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 62458173c336SBen Hutchings err(1, "%s: failed to read file", path); 624641618e63SLen Brown 624741618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 624841618e63SLen Brown sp = strchr(name_buf, '-'); 624941618e63SLen Brown if (!sp) 625041618e63SLen Brown sp = strchrnul(name_buf, '\n'); 625141618e63SLen Brown *sp = '%'; 625241618e63SLen Brown *(sp + 1) = '\0'; 625341618e63SLen Brown 6254fecb3bc8SDavid Arcari remove_underbar(name_buf); 6255fecb3bc8SDavid Arcari 625641618e63SLen Brown fclose(input); 625741618e63SLen Brown 625841618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 625941618e63SLen Brown 62600fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 62610fc521bcSZephaniah E. Loss-Cutler-Hull continue; 62620fc521bcSZephaniah E. Loss-Cutler-Hull 6263dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6264dd778a5eSLen Brown continue; 6265dd778a5eSLen Brown 62661b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU); 626741618e63SLen Brown } 626841618e63SLen Brown 62690748eaf0SLen Brown for (state = 10; state >= 0; --state) { 627041618e63SLen Brown 62711b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 627241618e63SLen Brown input = fopen(path, "r"); 627341618e63SLen Brown if (input == NULL) 627441618e63SLen Brown continue; 62758173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 62768173c336SBen Hutchings err(1, "%s: failed to read file", path); 627741618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 627841618e63SLen Brown sp = strchr(name_buf, '-'); 627941618e63SLen Brown if (!sp) 628041618e63SLen Brown sp = strchrnul(name_buf, '\n'); 628141618e63SLen Brown *sp = '\0'; 628241618e63SLen Brown fclose(input); 628341618e63SLen Brown 6284fecb3bc8SDavid Arcari remove_underbar(name_buf); 6285fecb3bc8SDavid Arcari 628641618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 628741618e63SLen Brown 62880fc521bcSZephaniah E. Loss-Cutler-Hull if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf)) 62890fc521bcSZephaniah E. Loss-Cutler-Hull continue; 62900fc521bcSZephaniah E. Loss-Cutler-Hull 6291dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6292dd778a5eSLen Brown continue; 6293dd778a5eSLen Brown 62941b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU); 629541618e63SLen Brown } 629641618e63SLen Brown 629741618e63SLen Brown } 629841618e63SLen Brown 62991ef7d21aSLen Brown /* 63001ef7d21aSLen Brown * parse cpuset with following syntax 63011ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 63021ef7d21aSLen Brown */ 63031ef7d21aSLen Brown void parse_cpu_command(char *optarg) 63041ef7d21aSLen Brown { 63051ef7d21aSLen Brown unsigned int start, end; 63061ef7d21aSLen Brown char *next; 63071ef7d21aSLen Brown 63084e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 63094e4e1e7cSLen Brown if (cpu_subset) 63104e4e1e7cSLen Brown goto error; 63114e4e1e7cSLen Brown show_core_only++; 63124e4e1e7cSLen Brown return; 63134e4e1e7cSLen Brown } 63144e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 63154e4e1e7cSLen Brown if (cpu_subset) 63164e4e1e7cSLen Brown goto error; 63174e4e1e7cSLen Brown show_pkg_only++; 63184e4e1e7cSLen Brown return; 63194e4e1e7cSLen Brown } 63204e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 63214e4e1e7cSLen Brown goto error; 63224e4e1e7cSLen Brown 63231ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 63241ef7d21aSLen Brown if (cpu_subset == NULL) 63251ef7d21aSLen Brown err(3, "CPU_ALLOC"); 63261ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 63271ef7d21aSLen Brown 63281ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 63291ef7d21aSLen Brown 63301ef7d21aSLen Brown next = optarg; 63311ef7d21aSLen Brown 63321ef7d21aSLen Brown while (next && *next) { 63331ef7d21aSLen Brown 63341ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 63351ef7d21aSLen Brown goto error; 63361ef7d21aSLen Brown 63371ef7d21aSLen Brown start = strtoul(next, &next, 10); 63381ef7d21aSLen Brown 63391ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 63401ef7d21aSLen Brown goto error; 63411ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 63421ef7d21aSLen Brown 63431ef7d21aSLen Brown if (*next == '\0') 63441ef7d21aSLen Brown break; 63451ef7d21aSLen Brown 63461ef7d21aSLen Brown if (*next == ',') { 63471ef7d21aSLen Brown next += 1; 63481ef7d21aSLen Brown continue; 63491ef7d21aSLen Brown } 63501ef7d21aSLen Brown 63511ef7d21aSLen Brown if (*next == '-') { 63521ef7d21aSLen Brown next += 1; /* start range */ 63531ef7d21aSLen Brown } else if (*next == '.') { 63541ef7d21aSLen Brown next += 1; 63551ef7d21aSLen Brown if (*next == '.') 63561ef7d21aSLen Brown next += 1; /* start range */ 63571ef7d21aSLen Brown else 63581ef7d21aSLen Brown goto error; 63591ef7d21aSLen Brown } 63601ef7d21aSLen Brown 63611ef7d21aSLen Brown end = strtoul(next, &next, 10); 63621ef7d21aSLen Brown if (end <= start) 63631ef7d21aSLen Brown goto error; 63641ef7d21aSLen Brown 63651ef7d21aSLen Brown while (++start <= end) { 63661ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 63671ef7d21aSLen Brown goto error; 63681ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 63691ef7d21aSLen Brown } 63701ef7d21aSLen Brown 63711ef7d21aSLen Brown if (*next == ',') 63721ef7d21aSLen Brown next += 1; 63731ef7d21aSLen Brown else if (*next != '\0') 63741ef7d21aSLen Brown goto error; 63751ef7d21aSLen Brown } 63761ef7d21aSLen Brown 63771ef7d21aSLen Brown return; 63781ef7d21aSLen Brown 63791ef7d21aSLen Brown error: 63804e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 63814e4e1e7cSLen Brown help(); 63821ef7d21aSLen Brown exit(-1); 63831ef7d21aSLen Brown } 63841ef7d21aSLen Brown 6385103a8feaSLen Brown void cmdline(int argc, char **argv) 6386103a8feaSLen Brown { 6387103a8feaSLen Brown int opt; 6388d8af6f5fSLen Brown int option_index = 0; 6389d8af6f5fSLen Brown static struct option long_options[] = { 6390388e9c81SLen Brown { "add", required_argument, 0, 'a' }, 63911ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' }, 6392d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' }, 639396e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 63943f44a5c6SLen Brown { "enable", required_argument, 0, 'e' }, 6395d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' }, 63962af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' }, 6397023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' }, 6398c7e399f8SZephaniah E. Loss-Cutler-Hull { "header_iterations", required_argument, 0, 'N' }, 6399d8af6f5fSLen Brown { "help", no_argument, 0, 'h' }, 6400812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 6401d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' }, 6402c8ade361SLen Brown { "list", no_argument, 0, 'l' }, 6403b7d8c148SLen Brown { "out", required_argument, 0, 'o' }, 640496e47158SLen Brown { "quiet", no_argument, 0, 'q' }, 6405812db3f7SLen Brown { "show", required_argument, 0, 's' }, 6406d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' }, 6407d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' }, 6408d8af6f5fSLen Brown { "version", no_argument, 0, 'v' }, 6409d8af6f5fSLen Brown { 0, 0, 0, 0 } 6410d8af6f5fSLen Brown }; 6411103a8feaSLen Brown 6412103a8feaSLen Brown progname = argv[0]; 6413103a8feaSLen Brown 64141b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) { 6415103a8feaSLen Brown switch (opt) { 6416388e9c81SLen Brown case 'a': 6417388e9c81SLen Brown parse_add_command(optarg); 6418388e9c81SLen Brown break; 64191ef7d21aSLen Brown case 'c': 64201ef7d21aSLen Brown parse_cpu_command(optarg); 64211ef7d21aSLen Brown break; 6422d8af6f5fSLen Brown case 'D': 6423d8af6f5fSLen Brown dump_only++; 64248e180f3cSLen Brown break; 64253f44a5c6SLen Brown case 'e': 64263f44a5c6SLen Brown /* --enable specified counter */ 64274c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 64283f44a5c6SLen Brown break; 6429d8af6f5fSLen Brown case 'd': 6430d8af6f5fSLen Brown debug++; 64313f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 64322f32edf1SLen Brown break; 6433812db3f7SLen Brown case 'H': 64343f44a5c6SLen Brown /* 64353f44a5c6SLen Brown * --hide: do not show those specified 64363f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 64373f44a5c6SLen Brown */ 64383f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6439812db3f7SLen Brown break; 6440d8af6f5fSLen Brown case 'h': 6441d8af6f5fSLen Brown default: 6442d8af6f5fSLen Brown help(); 6443d8af6f5fSLen Brown exit(1); 6444d8af6f5fSLen Brown case 'i': 64452a0609c0SLen Brown { 64462a0609c0SLen Brown double interval = strtod(optarg, NULL); 64472a0609c0SLen Brown 64482a0609c0SLen Brown if (interval < 0.001) { 64491b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval); 64502a0609c0SLen Brown exit(2); 64512a0609c0SLen Brown } 64522a0609c0SLen Brown 645347936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6454b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 645547936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 64562a0609c0SLen Brown } 6457889facbeSLen Brown break; 64585c56be9aSDirk Brandewie case 'J': 64595c56be9aSDirk Brandewie rapl_joules++; 64605c56be9aSDirk Brandewie break; 6461c8ade361SLen Brown case 'l': 64623f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6463c8ade361SLen Brown list_header_only++; 6464c8ade361SLen Brown quiet++; 6465c8ade361SLen Brown break; 6466b7d8c148SLen Brown case 'o': 6467b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6468b7d8c148SLen Brown break; 646996e47158SLen Brown case 'q': 647096e47158SLen Brown quiet = 1; 647196e47158SLen Brown break; 6472023fe0acSChen Yu case 'n': 6473023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6474023fe0acSChen Yu 6475023fe0acSChen Yu if (num_iterations <= 0) { 64761b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations); 6477023fe0acSChen Yu exit(2); 6478023fe0acSChen Yu } 6479023fe0acSChen Yu break; 6480c7e399f8SZephaniah E. Loss-Cutler-Hull case 'N': 6481c7e399f8SZephaniah E. Loss-Cutler-Hull header_iterations = strtod(optarg, NULL); 6482c7e399f8SZephaniah E. Loss-Cutler-Hull 6483c7e399f8SZephaniah E. Loss-Cutler-Hull if (header_iterations <= 0) { 6484c7e399f8SZephaniah E. Loss-Cutler-Hull fprintf(outf, "iterations %d should be positive number\n", 6485c7e399f8SZephaniah E. Loss-Cutler-Hull header_iterations); 6486c7e399f8SZephaniah E. Loss-Cutler-Hull exit(2); 6487c7e399f8SZephaniah E. Loss-Cutler-Hull } 6488c7e399f8SZephaniah E. Loss-Cutler-Hull break; 6489812db3f7SLen Brown case 's': 64903f44a5c6SLen Brown /* 64913f44a5c6SLen Brown * --show: show only those specified 64923f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 64933f44a5c6SLen Brown * subsequent invocations can add to it. 64943f44a5c6SLen Brown */ 64953f44a5c6SLen Brown if (shown == 0) 64963f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 64973f44a5c6SLen Brown else 64983f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 64993f44a5c6SLen Brown shown = 1; 6500812db3f7SLen Brown break; 6501d8af6f5fSLen Brown case 'S': 6502d8af6f5fSLen Brown summary_only++; 6503d8af6f5fSLen Brown break; 6504d8af6f5fSLen Brown case 'T': 650555279aefSZhang Rui tj_max_override = atoi(optarg); 6506d8af6f5fSLen Brown break; 6507d8af6f5fSLen Brown case 'v': 6508d8af6f5fSLen Brown print_version(); 6509d8af6f5fSLen Brown exit(0); 6510d8af6f5fSLen Brown break; 6511103a8feaSLen Brown } 6512103a8feaSLen Brown } 6513103a8feaSLen Brown } 6514103a8feaSLen Brown 6515103a8feaSLen Brown int main(int argc, char **argv) 6516103a8feaSLen Brown { 6517b7d8c148SLen Brown outf = stderr; 6518103a8feaSLen Brown cmdline(argc, argv); 6519103a8feaSLen Brown 652096e47158SLen Brown if (!quiet) 6521d8af6f5fSLen Brown print_version(); 6522103a8feaSLen Brown 652341618e63SLen Brown probe_sysfs(); 652441618e63SLen Brown 6525103a8feaSLen Brown turbostat_init(); 6526103a8feaSLen Brown 65276799ba84SDan Merillat msr_sum_record(); 65286799ba84SDan Merillat 65293b4d5c7fSAndy Shevchenko /* dump counters and exit */ 65303b4d5c7fSAndy Shevchenko if (dump_only) 65313b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 65323b4d5c7fSAndy Shevchenko 6533c8ade361SLen Brown /* list header and exit */ 6534c8ade361SLen Brown if (list_header_only) { 6535c8ade361SLen Brown print_header(","); 6536c8ade361SLen Brown flush_output_stdout(); 6537c8ade361SLen Brown return 0; 6538c8ade361SLen Brown } 6539c8ade361SLen Brown 6540103a8feaSLen Brown /* 6541103a8feaSLen Brown * if any params left, it must be a command to fork 6542103a8feaSLen Brown */ 6543103a8feaSLen Brown if (argc - optind) 6544103a8feaSLen Brown return fork_it(argv + optind); 6545103a8feaSLen Brown else 6546103a8feaSLen Brown turbostat_loop(); 6547103a8feaSLen Brown 6548103a8feaSLen Brown return 0; 6549103a8feaSLen Brown } 6550