1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2103a8feaSLen Brown /* 3103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 434041551SLen Brown * on modern Intel and AMD processors. 5103a8feaSLen Brown * 6144b44b1SLen Brown * Copyright (c) 2013 Intel Corporation. 7103a8feaSLen Brown * Len Brown <len.brown@intel.com> 8103a8feaSLen Brown */ 9103a8feaSLen Brown 1088c3281fSLen Brown #define _GNU_SOURCE 11b731f311SJosh Triplett #include MSRHEADER 12869ce69eSLen Brown #include INTEL_FAMILY_HEADER 1395aebc44SJosh Triplett #include <stdarg.h> 14103a8feaSLen Brown #include <stdio.h> 15b2c95d90SJosh Triplett #include <err.h> 16103a8feaSLen Brown #include <unistd.h> 17103a8feaSLen Brown #include <sys/types.h> 18103a8feaSLen Brown #include <sys/wait.h> 19103a8feaSLen Brown #include <sys/stat.h> 20b9ad8ee0SLen Brown #include <sys/select.h> 21103a8feaSLen Brown #include <sys/resource.h> 22103a8feaSLen Brown #include <fcntl.h> 23103a8feaSLen Brown #include <signal.h> 24103a8feaSLen Brown #include <sys/time.h> 25103a8feaSLen Brown #include <stdlib.h> 26d8af6f5fSLen Brown #include <getopt.h> 27103a8feaSLen Brown #include <dirent.h> 28103a8feaSLen Brown #include <string.h> 29103a8feaSLen Brown #include <ctype.h> 3088c3281fSLen Brown #include <sched.h> 312a0609c0SLen Brown #include <time.h> 322b92865eSJosh Triplett #include <cpuid.h> 33fcaa681cSLen Brown #include <sys/capability.h> 3498481e79SLen Brown #include <errno.h> 359392bd98SCalvin Walton #include <math.h> 362af4f9b8SLen Brown #include <linux/perf_event.h> 372af4f9b8SLen Brown #include <asm/unistd.h> 387ab5ff49SZhang Rui #include <stdbool.h> 39103a8feaSLen Brown 40103a8feaSLen Brown char *proc_stat = "/proc/stat"; 41b7d8c148SLen Brown FILE *outf; 4236229897SLen Brown int *fd_percpu; 432af4f9b8SLen Brown int *fd_instr_count_percpu; 44b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 }; 4547936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 }; 46e9d3092fSZhang Rui 47e9d3092fSZhang Rui /* Save original CPU model */ 48e9d3092fSZhang Rui unsigned int model_orig; 49e9d3092fSZhang Rui 50023fe0acSChen Yu unsigned int num_iterations; 51d8af6f5fSLen Brown unsigned int debug; 5296e47158SLen Brown unsigned int quiet; 533f44a5c6SLen Brown unsigned int shown; 540de6c0dfSLen Brown unsigned int sums_need_wide_columns; 55d8af6f5fSLen Brown unsigned int rapl_joules; 56d8af6f5fSLen Brown unsigned int summary_only; 57c8ade361SLen Brown unsigned int list_header_only; 58d8af6f5fSLen Brown unsigned int dump_only; 59103a8feaSLen Brown unsigned int do_snb_cstates; 60fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 61144b44b1SLen Brown unsigned int do_slm_cstates; 62144b44b1SLen Brown unsigned int use_c1_residency_msr; 63103a8feaSLen Brown unsigned int has_aperf; 64889facbeSLen Brown unsigned int has_epb; 655a63426eSLen Brown unsigned int do_irtl_snb; 665a63426eSLen Brown unsigned int do_irtl_hsw; 67fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 68103a8feaSLen Brown unsigned int genuine_intel; 6934041551SLen Brown unsigned int authentic_amd; 70c1c10cc7SPu Wen unsigned int hygon_genuine; 7134041551SLen Brown unsigned int max_level, max_extended_level; 72103a8feaSLen Brown unsigned int has_invariant_tsc; 73d7899447SLen Brown unsigned int do_nhm_platform_info; 74cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 75b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 76103a8feaSLen Brown double bclk; 77a2b7b749SLen Brown double base_hz; 7821ed5574SLen Brown unsigned int has_base_hz; 79a2b7b749SLen Brown double tsc_tweak = 1.0; 80c98d5d94SLen Brown unsigned int show_pkg_only; 81c98d5d94SLen Brown unsigned int show_core_only; 82c98d5d94SLen Brown char *output_buffer, *outp; 83889facbeSLen Brown unsigned int do_rapl; 84889facbeSLen Brown unsigned int do_dts; 85889facbeSLen Brown unsigned int do_ptm; 862af4f9b8SLen Brown unsigned int do_ipc; 87fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 88be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 89be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 9027d47356SLen Brown unsigned int gfx_cur_mhz; 91b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 9255279aefSZhang Rui unsigned int tj_max; 9355279aefSZhang Rui unsigned int tj_max_override; 940b9a0b9bSZhang Rui int tcc_offset_bits; 9540ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 9640ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 97889facbeSLen Brown double rapl_joule_counter_range; 983a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 99ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 100aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 1013a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1023a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1038a5bdf41SLen Brown unsigned int crystal_hz; 1048a5bdf41SLen Brown unsigned long long tsc_hz; 1057ce7d5deSPrarit Bhargava int base_cpu; 10621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1077f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1087f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1097f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1107f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1117f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1127f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 11333148d67SLen Brown unsigned int has_misc_feature_control; 1144c2122d4SLen Brown unsigned int first_counter_read = 1; 115c026c236SArtem Bityutskiy int ignore_stdin; 116889facbeSLen Brown 117889facbeSLen Brown #define RAPL_PKG (1 << 0) 118e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 119e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 120e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 121e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 122e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 123e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 124e6f9bb3cSLen Brown 125889facbeSLen Brown #define RAPL_DRAM (1 << 3) 126e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 127e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 128e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 129e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1300b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1310b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 132e6f9bb3cSLen Brown 1339148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 134e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1350b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 136e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 137e6f9bb3cSLen Brown 1380b2bb692SLen Brown #define RAPL_GFX (1 << 8) 139e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 140e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 141e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1429148494cSJacob Pan 1439148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1449148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1459392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1469392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1479392bd98SCalvin Walton * not per-package. */ 1489392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1499392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1509392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1519392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1529148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 153889facbeSLen Brown #define TJMAX_DEFAULT 100 154889facbeSLen Brown 1559392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1569392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1579392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1589392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1599392bd98SCalvin Walton 160889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 161103a8feaSLen Brown 162388e9c81SLen Brown /* 163388e9c81SLen Brown * buffer size used by sscanf() for added column names 164388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 165388e9c81SLen Brown */ 166388e9c81SLen Brown #define NAME_BYTES 20 167495c7654SLen Brown #define PATH_BYTES 128 168388e9c81SLen Brown 169103a8feaSLen Brown int backwards_count; 170103a8feaSLen Brown char *progname; 171103a8feaSLen Brown 1721ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1731ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1741ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1750748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1760748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1770e2d8f05SLen Brown #define BITMASK_SIZE 32 178c98d5d94SLen Brown 179c98d5d94SLen Brown struct thread_data { 180f4fdf2b4SLen Brown struct timeval tv_begin; 181f4fdf2b4SLen Brown struct timeval tv_end; 182d4794f25SYazen Ghannam struct timeval tv_delta; 183c98d5d94SLen Brown unsigned long long tsc; 184c98d5d94SLen Brown unsigned long long aperf; 185c98d5d94SLen Brown unsigned long long mperf; 186144b44b1SLen Brown unsigned long long c1; 1872af4f9b8SLen Brown unsigned long long instr_count; 1880de6c0dfSLen Brown unsigned long long irq_count; 1891ed51011SLen Brown unsigned int smi_count; 190c98d5d94SLen Brown unsigned int cpu_id; 1914c2122d4SLen Brown unsigned int apic_id; 1924c2122d4SLen Brown unsigned int x2apic_id; 193c98d5d94SLen Brown unsigned int flags; 1947ab5ff49SZhang Rui bool is_atom; 195c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 196c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1970748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 198c98d5d94SLen Brown } *thread_even, *thread_odd; 199c98d5d94SLen Brown 200c98d5d94SLen Brown struct core_data { 201c98d5d94SLen Brown unsigned long long c3; 202c98d5d94SLen Brown unsigned long long c6; 203c98d5d94SLen Brown unsigned long long c7; 2040539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 205889facbeSLen Brown unsigned int core_temp_c; 2069392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 207c98d5d94SLen Brown unsigned int core_id; 208678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 209c98d5d94SLen Brown } *core_even, *core_odd; 210c98d5d94SLen Brown 211c98d5d94SLen Brown struct pkg_data { 212c98d5d94SLen Brown unsigned long long pc2; 213c98d5d94SLen Brown unsigned long long pc3; 214c98d5d94SLen Brown unsigned long long pc6; 215c98d5d94SLen Brown unsigned long long pc7; 216ca58710fSKristen Carlson Accardi unsigned long long pc8; 217ca58710fSKristen Carlson Accardi unsigned long long pc9; 218ca58710fSKristen Carlson Accardi unsigned long long pc10; 219be0e54c4SLen Brown unsigned long long cpu_lpi; 220be0e54c4SLen Brown unsigned long long sys_lpi; 2210b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2220b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2230b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2240b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2259185e988SLen Brown long long gfx_rc6_ms; 22627d47356SLen Brown unsigned int gfx_mhz; 227b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 228c98d5d94SLen Brown unsigned int package_id; 2297c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2307c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2317c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2327c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2337c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2347c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 235889facbeSLen Brown unsigned int pkg_temp_c; 236678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 237c98d5d94SLen Brown } *package_even, *package_odd; 238c98d5d94SLen Brown 239c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 240c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 241c98d5d94SLen Brown 24240f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 24340f5cfe7SPrarit Bhargava ((thread_base) + \ 24440f5cfe7SPrarit Bhargava ((pkg_no) * \ 24540f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 24640f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 24740f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 24840f5cfe7SPrarit Bhargava (thread_no)) 24940f5cfe7SPrarit Bhargava 25040f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 25140f5cfe7SPrarit Bhargava ((core_base) + \ 25240f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 25340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 25440f5cfe7SPrarit Bhargava (core_no)) 25540f5cfe7SPrarit Bhargava 256c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 257c98d5d94SLen Brown 258388e9c81SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE }; 25941618e63SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC }; 260388e9c81SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT }; 261388e9c81SLen Brown 262388e9c81SLen Brown struct msr_counter { 263388e9c81SLen Brown unsigned int msr_num; 264388e9c81SLen Brown char name[NAME_BYTES]; 265495c7654SLen Brown char path[PATH_BYTES]; 266388e9c81SLen Brown unsigned int width; 267388e9c81SLen Brown enum counter_type type; 268388e9c81SLen Brown enum counter_format format; 269388e9c81SLen Brown struct msr_counter *next; 270812db3f7SLen Brown unsigned int flags; 271812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 272812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 27341618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 274388e9c81SLen Brown }; 275388e9c81SLen Brown 27687e15da9SChen Yu /* 27787e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 27887e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 27987e15da9SChen Yu * despite its register's bit width. 28087e15da9SChen Yu */ 28187e15da9SChen Yu enum { 28287e15da9SChen Yu IDX_PKG_ENERGY, 28387e15da9SChen Yu IDX_DRAM_ENERGY, 28487e15da9SChen Yu IDX_PP0_ENERGY, 28587e15da9SChen Yu IDX_PP1_ENERGY, 28687e15da9SChen Yu IDX_PKG_PERF, 28787e15da9SChen Yu IDX_DRAM_PERF, 28887e15da9SChen Yu IDX_COUNT, 28987e15da9SChen Yu }; 29087e15da9SChen Yu 29187e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 29287e15da9SChen Yu 29387e15da9SChen Yu struct msr_sum_array { 29487e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 29587e15da9SChen Yu struct { 29687e15da9SChen Yu /*The accumulated MSR value is updated by the timer */ 29787e15da9SChen Yu unsigned long long sum; 29887e15da9SChen Yu /*The MSR footprint recorded in last timer */ 29987e15da9SChen Yu unsigned long long last; 30087e15da9SChen Yu } entries[IDX_COUNT]; 30187e15da9SChen Yu }; 30287e15da9SChen Yu 30387e15da9SChen Yu /* The percpu MSR sum array.*/ 30487e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 30587e15da9SChen Yu 30613a779deSCalvin Walton off_t idx_to_offset(int idx) 30787e15da9SChen Yu { 30813a779deSCalvin Walton off_t offset; 30987e15da9SChen Yu 31087e15da9SChen Yu switch (idx) { 31187e15da9SChen Yu case IDX_PKG_ENERGY: 312301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H) 313301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT; 314301b1d3aSBas Nieuwenhuizen else 31587e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 31687e15da9SChen Yu break; 31787e15da9SChen Yu case IDX_DRAM_ENERGY: 31887e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 31987e15da9SChen Yu break; 32087e15da9SChen Yu case IDX_PP0_ENERGY: 32187e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 32287e15da9SChen Yu break; 32387e15da9SChen Yu case IDX_PP1_ENERGY: 32487e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 32587e15da9SChen Yu break; 32687e15da9SChen Yu case IDX_PKG_PERF: 32787e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 32887e15da9SChen Yu break; 32987e15da9SChen Yu case IDX_DRAM_PERF: 33087e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 33187e15da9SChen Yu break; 33287e15da9SChen Yu default: 33387e15da9SChen Yu offset = -1; 33487e15da9SChen Yu } 33587e15da9SChen Yu return offset; 33687e15da9SChen Yu } 33787e15da9SChen Yu 33813a779deSCalvin Walton int offset_to_idx(off_t offset) 33987e15da9SChen Yu { 34087e15da9SChen Yu int idx; 34187e15da9SChen Yu 34287e15da9SChen Yu switch (offset) { 34387e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 344301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT: 34587e15da9SChen Yu idx = IDX_PKG_ENERGY; 34687e15da9SChen Yu break; 34787e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 34887e15da9SChen Yu idx = IDX_DRAM_ENERGY; 34987e15da9SChen Yu break; 35087e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 35187e15da9SChen Yu idx = IDX_PP0_ENERGY; 35287e15da9SChen Yu break; 35387e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 35487e15da9SChen Yu idx = IDX_PP1_ENERGY; 35587e15da9SChen Yu break; 35687e15da9SChen Yu case MSR_PKG_PERF_STATUS: 35787e15da9SChen Yu idx = IDX_PKG_PERF; 35887e15da9SChen Yu break; 35987e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 36087e15da9SChen Yu idx = IDX_DRAM_PERF; 36187e15da9SChen Yu break; 36287e15da9SChen Yu default: 36387e15da9SChen Yu idx = -1; 36487e15da9SChen Yu } 36587e15da9SChen Yu return idx; 36687e15da9SChen Yu } 36787e15da9SChen Yu 36887e15da9SChen Yu int idx_valid(int idx) 36987e15da9SChen Yu { 37087e15da9SChen Yu switch (idx) { 37187e15da9SChen Yu case IDX_PKG_ENERGY: 372301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H); 37387e15da9SChen Yu case IDX_DRAM_ENERGY: 37487e15da9SChen Yu return do_rapl & RAPL_DRAM; 37587e15da9SChen Yu case IDX_PP0_ENERGY: 37687e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 37787e15da9SChen Yu case IDX_PP1_ENERGY: 37887e15da9SChen Yu return do_rapl & RAPL_GFX; 37987e15da9SChen Yu case IDX_PKG_PERF: 38087e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 38187e15da9SChen Yu case IDX_DRAM_PERF: 38287e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 38387e15da9SChen Yu default: 38487e15da9SChen Yu return 0; 38587e15da9SChen Yu } 38687e15da9SChen Yu } 387*1b439f01SLen Brown 388388e9c81SLen Brown struct sys_counters { 389678a3bd1SLen Brown unsigned int added_thread_counters; 390678a3bd1SLen Brown unsigned int added_core_counters; 391678a3bd1SLen Brown unsigned int added_package_counters; 392388e9c81SLen Brown struct msr_counter *tp; 393388e9c81SLen Brown struct msr_counter *cp; 394388e9c81SLen Brown struct msr_counter *pp; 395388e9c81SLen Brown } sys; 396388e9c81SLen Brown 397c98d5d94SLen Brown struct system_summary { 398c98d5d94SLen Brown struct thread_data threads; 399c98d5d94SLen Brown struct core_data cores; 400c98d5d94SLen Brown struct pkg_data packages; 401388e9c81SLen Brown } average; 402c98d5d94SLen Brown 4030e2d8f05SLen Brown struct cpu_topology { 4040e2d8f05SLen Brown int physical_package_id; 4056de68fe1SLen Brown int die_id; 4060e2d8f05SLen Brown int logical_cpu_id; 407ef605741SPrarit Bhargava int physical_node_id; 408ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 4090e2d8f05SLen Brown int physical_core_id; 4108cb48b32SPrarit Bhargava int thread_id; 4110e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 4120e2d8f05SLen Brown } *cpus; 413c98d5d94SLen Brown 414c98d5d94SLen Brown struct topo_params { 415c98d5d94SLen Brown int num_packages; 4166de68fe1SLen Brown int num_die; 417103a8feaSLen Brown int num_cpus; 418c98d5d94SLen Brown int num_cores; 419c98d5d94SLen Brown int max_cpu_num; 420ef605741SPrarit Bhargava int max_node_num; 42170a9c6e8SPrarit Bhargava int nodes_per_pkg; 42270a9c6e8SPrarit Bhargava int cores_per_node; 42370a9c6e8SPrarit Bhargava int threads_per_core; 424c98d5d94SLen Brown } topo; 425103a8feaSLen Brown 426c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 427103a8feaSLen Brown 428562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 429562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 430562a2d37SLen Brown 431c98d5d94SLen Brown void setup_all_buffers(void); 432103a8feaSLen Brown 4331f81c5efSLen Brown char *sys_lpi_file; 4341f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 4351f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 4361f81c5efSLen Brown 437c98d5d94SLen Brown int cpu_is_not_present(int cpu) 438d15cf7c1SLen Brown { 439c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 440c98d5d94SLen Brown } 441*1b439f01SLen Brown 442c98d5d94SLen Brown /* 443c98d5d94SLen Brown * run func(thread, core, package) in topology order 444c98d5d94SLen Brown * skip non-present cpus 445c98d5d94SLen Brown */ 446c98d5d94SLen Brown 447c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), 448c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 449c98d5d94SLen Brown { 45040f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 451c98d5d94SLen Brown 452c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 453df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 45470a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 455*1b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 456c98d5d94SLen Brown struct thread_data *t; 457c98d5d94SLen Brown struct core_data *c; 458c98d5d94SLen Brown struct pkg_data *p; 459c98d5d94SLen Brown 460*1b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 461c98d5d94SLen Brown 462c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 463c98d5d94SLen Brown continue; 464c98d5d94SLen Brown 465*1b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 466c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 467c98d5d94SLen Brown 468c98d5d94SLen Brown retval = func(t, c, p); 469c98d5d94SLen Brown if (retval) 470c98d5d94SLen Brown return retval; 471c98d5d94SLen Brown } 472c98d5d94SLen Brown } 473c98d5d94SLen Brown } 47440f5cfe7SPrarit Bhargava } 475d15cf7c1SLen Brown return 0; 476d15cf7c1SLen Brown } 477d15cf7c1SLen Brown 47888c3281fSLen Brown int cpu_migrate(int cpu) 47988c3281fSLen Brown { 480c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 481c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 482c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 48388c3281fSLen Brown return -1; 48488c3281fSLen Brown else 48588c3281fSLen Brown return 0; 48688c3281fSLen Brown } 487*1b439f01SLen Brown 48836229897SLen Brown int get_msr_fd(int cpu) 489103a8feaSLen Brown { 490103a8feaSLen Brown char pathname[32]; 491103a8feaSLen Brown int fd; 492103a8feaSLen Brown 49336229897SLen Brown fd = fd_percpu[cpu]; 49436229897SLen Brown 49536229897SLen Brown if (fd) 49636229897SLen Brown return fd; 49736229897SLen Brown 498103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 499103a8feaSLen Brown fd = open(pathname, O_RDONLY); 50015aaa346SLen Brown if (fd < 0) 50198481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 502103a8feaSLen Brown 50336229897SLen Brown fd_percpu[cpu] = fd; 50436229897SLen Brown 50536229897SLen Brown return fd; 50636229897SLen Brown } 50736229897SLen Brown 5082af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 5092af4f9b8SLen Brown { 5102af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 5112af4f9b8SLen Brown } 5122af4f9b8SLen Brown 5132af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 5142af4f9b8SLen Brown { 5152af4f9b8SLen Brown struct perf_event_attr pea; 5162af4f9b8SLen Brown int fd; 5172af4f9b8SLen Brown 5182af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 5192af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 5202af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 5212af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 5222af4f9b8SLen Brown 5232af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 5242af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 5252af4f9b8SLen Brown if (fd == -1) 5262af4f9b8SLen Brown err(-1, "cpu%d: perf instruction counter\n", cpu_num); 5272af4f9b8SLen Brown 5282af4f9b8SLen Brown return fd; 5292af4f9b8SLen Brown } 5302af4f9b8SLen Brown 5312af4f9b8SLen Brown int get_instr_count_fd(int cpu) 5322af4f9b8SLen Brown { 5332af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 5342af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5352af4f9b8SLen Brown 5362af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 5372af4f9b8SLen Brown 5382af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5392af4f9b8SLen Brown } 5402af4f9b8SLen Brown 54136229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 54236229897SLen Brown { 54336229897SLen Brown ssize_t retval; 54436229897SLen Brown 54536229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 54615aaa346SLen Brown 54798481e79SLen Brown if (retval != sizeof *msr) 548cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 54915aaa346SLen Brown 55015aaa346SLen Brown return 0; 551103a8feaSLen Brown } 552103a8feaSLen Brown 553fc04cc67SLen Brown /* 554bdd5ae3aSLen Brown * This list matches the column headers, except 555bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 556bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 557bdd5ae3aSLen Brown * matching on them for --show and --hide. 558fc04cc67SLen Brown */ 559812db3f7SLen Brown struct msr_counter bic[] = { 5603f44a5c6SLen Brown { 0x0, "usec" }, 5613f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 562812db3f7SLen Brown { 0x0, "Package" }, 563bdd5ae3aSLen Brown { 0x0, "Node" }, 564812db3f7SLen Brown { 0x0, "Avg_MHz" }, 565bdd5ae3aSLen Brown { 0x0, "Busy%" }, 566812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 567812db3f7SLen Brown { 0x0, "TSC_MHz" }, 568812db3f7SLen Brown { 0x0, "IRQ" }, 569495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL }, 570bdd5ae3aSLen Brown { 0x0, "sysfs" }, 571812db3f7SLen Brown { 0x0, "CPU%c1" }, 572812db3f7SLen Brown { 0x0, "CPU%c3" }, 573812db3f7SLen Brown { 0x0, "CPU%c6" }, 574812db3f7SLen Brown { 0x0, "CPU%c7" }, 575812db3f7SLen Brown { 0x0, "ThreadC" }, 576812db3f7SLen Brown { 0x0, "CoreTmp" }, 577812db3f7SLen Brown { 0x0, "CoreCnt" }, 578812db3f7SLen Brown { 0x0, "PkgTmp" }, 579812db3f7SLen Brown { 0x0, "GFX%rc6" }, 580812db3f7SLen Brown { 0x0, "GFXMHz" }, 581812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 582812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 583812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 584812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 5850f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 5860f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 5874bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 588be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 589be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 590812db3f7SLen Brown { 0x0, "PkgWatt" }, 591812db3f7SLen Brown { 0x0, "CorWatt" }, 592812db3f7SLen Brown { 0x0, "GFXWatt" }, 593812db3f7SLen Brown { 0x0, "PkgCnt" }, 594812db3f7SLen Brown { 0x0, "RAMWatt" }, 595812db3f7SLen Brown { 0x0, "PKG_%" }, 596812db3f7SLen Brown { 0x0, "RAM_%" }, 597812db3f7SLen Brown { 0x0, "Pkg_J" }, 598812db3f7SLen Brown { 0x0, "Cor_J" }, 599812db3f7SLen Brown { 0x0, "GFX_J" }, 600812db3f7SLen Brown { 0x0, "RAM_J" }, 6010539ba11SLen Brown { 0x0, "Mod%c6" }, 602a99d8730SLen Brown { 0x0, "Totl%C0" }, 603a99d8730SLen Brown { 0x0, "Any%C0" }, 604a99d8730SLen Brown { 0x0, "GFX%C0" }, 605a99d8730SLen Brown { 0x0, "CPUGFX%" }, 606bdd5ae3aSLen Brown { 0x0, "Core" }, 607bdd5ae3aSLen Brown { 0x0, "CPU" }, 6084c2122d4SLen Brown { 0x0, "APIC" }, 6094c2122d4SLen Brown { 0x0, "X2APIC" }, 6106de68fe1SLen Brown { 0x0, "Die" }, 611b4b91569SRafael Antognolli { 0x0, "GFXAMHz" }, 6122af4f9b8SLen Brown { 0x0, "IPC" }, 613812db3f7SLen Brown }; 614812db3f7SLen Brown 615812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 6163f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 6173f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 6183f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 619bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 620bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 621bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 622bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 623bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 624bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 625bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 626bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 627bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 628bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 629bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 630bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 631bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 632bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 633bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 634bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 635bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 636bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 637bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 638bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 639bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 640bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 641bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 642bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 643bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 644bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 645bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 646bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 647bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 648bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 649bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 650bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 651bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 652bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 653bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 654bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 655bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 656bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 657bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 658bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 659bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 660bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 661bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 662bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 663bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 6644c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 6654c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 6666de68fe1SLen Brown #define BIC_Die (1ULL << 50) 667b4b91569SRafael Antognolli #define BIC_GFXACTMHz (1ULL << 51) 6682af4f9b8SLen Brown #define BIC_IPC (1ULL << 52) 669812db3f7SLen Brown 6704c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 6713f44a5c6SLen Brown 6723f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 6734c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 674812db3f7SLen Brown 675812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 6761e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 6773f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 678812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 6790f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 6802af4f9b8SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 681812db3f7SLen Brown 682dd778a5eSLen Brown #define MAX_DEFERRED 16 683dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 684dd778a5eSLen Brown int deferred_skip_index; 685dd778a5eSLen Brown 686dd778a5eSLen Brown /* 687dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 688dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 689dd778a5eSLen Brown */ 690dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 691dd778a5eSLen Brown 692dd778a5eSLen Brown void help(void) 693dd778a5eSLen Brown { 694dd778a5eSLen Brown fprintf(outf, 695dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 696dd778a5eSLen Brown "\n" 697dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 698dd778a5eSLen Brown "when COMMAND completes.\n" 699dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 700dd778a5eSLen Brown "to print statistics, until interrupted.\n" 701cc481650SNathan Ciobanu " -a, --add add a counter\n" 702dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 703cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 704dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7059ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7069ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7079ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7089ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7099ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7109ce80578SNathan Ciobanu " hide the specified column(s)\n" 711cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 712cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7139ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 714cc481650SNathan Ciobanu " -l, --list list column headers only\n" 715cc481650SNathan Ciobanu " -n, --num_iterations num\n" 716cc481650SNathan Ciobanu " number of the measurement iterations\n" 717cc481650SNathan Ciobanu " -o, --out file\n" 718cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 719cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7209ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7219ce80578SNathan Ciobanu " show only the specified column(s)\n" 7229ce80578SNathan Ciobanu " -S, --Summary\n" 7239ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7249ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7259ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7269ce80578SNathan Ciobanu " degrees Celsius\n" 727cc481650SNathan Ciobanu " -h, --help print this help message\n" 728*1b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n"); 729dd778a5eSLen Brown } 730dd778a5eSLen Brown 731812db3f7SLen Brown /* 732812db3f7SLen Brown * bic_lookup 733812db3f7SLen Brown * for all the strings in comma separate name_list, 734812db3f7SLen Brown * set the approprate bit in return value. 735812db3f7SLen Brown */ 736dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 737812db3f7SLen Brown { 738812db3f7SLen Brown int i; 739812db3f7SLen Brown unsigned long long retval = 0; 740812db3f7SLen Brown 741812db3f7SLen Brown while (name_list) { 742812db3f7SLen Brown char *comma; 743812db3f7SLen Brown 744812db3f7SLen Brown comma = strchr(name_list, ','); 745812db3f7SLen Brown 746812db3f7SLen Brown if (comma) 747812db3f7SLen Brown *comma = '\0'; 748812db3f7SLen Brown 7493f44a5c6SLen Brown if (!strcmp(name_list, "all")) 7503f44a5c6SLen Brown return ~0; 7513f44a5c6SLen Brown 752812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 753812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 754812db3f7SLen Brown retval |= (1ULL << i); 755812db3f7SLen Brown break; 756812db3f7SLen Brown } 757812db3f7SLen Brown } 758812db3f7SLen Brown if (i == MAX_BIC) { 759dd778a5eSLen Brown if (mode == SHOW_LIST) { 760812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 761812db3f7SLen Brown exit(-1); 762812db3f7SLen Brown } 763dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 764dd778a5eSLen Brown if (debug) 765dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 766dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 767dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 768dd778a5eSLen Brown MAX_DEFERRED, name_list); 769dd778a5eSLen Brown help(); 770dd778a5eSLen Brown exit(1); 771dd778a5eSLen Brown } 772dd778a5eSLen Brown } 773812db3f7SLen Brown 774812db3f7SLen Brown name_list = comma; 775812db3f7SLen Brown if (name_list) 776812db3f7SLen Brown name_list++; 777812db3f7SLen Brown 778812db3f7SLen Brown } 779812db3f7SLen Brown return retval; 780812db3f7SLen Brown } 781fc04cc67SLen Brown 782c8ade361SLen Brown void print_header(char *delim) 783103a8feaSLen Brown { 784388e9c81SLen Brown struct msr_counter *mp; 7856168c2e0SLen Brown int printed = 0; 786388e9c81SLen Brown 7873f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 7883f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 7893f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 7903f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 791812db3f7SLen Brown if (DO_BIC(BIC_Package)) 7926168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 7936de68fe1SLen Brown if (DO_BIC(BIC_Die)) 7946de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 79501235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 79601235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 797812db3f7SLen Brown if (DO_BIC(BIC_Core)) 7986168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 799812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8006168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8014c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8024c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8034c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8044c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 805812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8066168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 807812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8086168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 809812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8106168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 811812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8126168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8131cc21f7bSLen Brown 8142af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8152af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8162af4f9b8SLen Brown 8170de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8180de6c0dfSLen Brown if (sums_need_wide_columns) 8196168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8200de6c0dfSLen Brown else 8216168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8220de6c0dfSLen Brown } 8230de6c0dfSLen Brown 824812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8256168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8261cc21f7bSLen Brown 827388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 828dd778a5eSLen Brown 829388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 830388e9c81SLen Brown if (mp->width == 64) 831dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 832388e9c81SLen Brown else 833dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 834388e9c81SLen Brown } else { 8350de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 836dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8370de6c0dfSLen Brown else 838dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 839388e9c81SLen Brown } 840388e9c81SLen Brown } 841388e9c81SLen Brown 84241618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8436168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 844562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8456168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 846812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8476168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 848812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8496168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 850678a3bd1SLen Brown 8510539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8526168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 853678a3bd1SLen Brown 854812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8556168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 856388e9c81SLen Brown 8579392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 8589392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8599392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 8609392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 8619392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8629392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 8639392bd98SCalvin Walton } 8649392bd98SCalvin Walton 865388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 866388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 867388e9c81SLen Brown if (mp->width == 64) 868c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 869388e9c81SLen Brown else 870c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 871388e9c81SLen Brown } else { 8720de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8730de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 8740de6c0dfSLen Brown else 875c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 876388e9c81SLen Brown } 877388e9c81SLen Brown } 878388e9c81SLen Brown 879812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 8806168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 881889facbeSLen Brown 882812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 8836168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 884fdf676e5SLen Brown 885812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 8866168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 88727d47356SLen Brown 888b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 889b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 890b4b91569SRafael Antognolli 891a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 8926168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 893a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 8946168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 895a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 8966168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 897a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 8986168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 8990b2bb692SLen Brown 9000f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9016168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9020f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9036168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9040f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9056168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9060f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9076168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9080f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9096168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9100f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9116168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9120f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9136168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 914be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 915be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 916be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 917be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 918103a8feaSLen Brown 9195c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 920812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9216168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9229392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9236168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 924812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9256168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 926812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9276168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 928812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9296168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 930812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9316168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 932d7899447SLen Brown } else if (do_rapl && rapl_joules) { 933812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9346168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9359392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9366168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 937812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9386168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 939812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9406168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 941812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9426168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 943812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9446168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9455c56be9aSDirk Brandewie } 946388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 947388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 948388e9c81SLen Brown if (mp->width == 64) 949c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 950388e9c81SLen Brown else 951c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 952388e9c81SLen Brown } else { 9530de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9540de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9550de6c0dfSLen Brown else 956c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 957388e9c81SLen Brown } 958388e9c81SLen Brown } 959388e9c81SLen Brown 960c98d5d94SLen Brown outp += sprintf(outp, "\n"); 961103a8feaSLen Brown } 962103a8feaSLen Brown 963*1b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 964103a8feaSLen Brown { 965388e9c81SLen Brown int i; 966388e9c81SLen Brown struct msr_counter *mp; 967388e9c81SLen Brown 9683b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 969c98d5d94SLen Brown 970c98d5d94SLen Brown if (t) { 971*1b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 9723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 9733b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 9743b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 9753b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 9766886fee4SLen Brown 9772af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 9782af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 9792af4f9b8SLen Brown 980812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 9810de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 982812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 983218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 984388e9c81SLen Brown 985388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 986*1b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]); 987388e9c81SLen Brown } 988103a8feaSLen Brown } 989103a8feaSLen Brown 990c98d5d94SLen Brown if (c) { 9913b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 9923b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 9933b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 9943b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 9953b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 9969392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 997388e9c81SLen Brown 998388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 999*1b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]); 1000388e9c81SLen Brown } 10010539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1002c98d5d94SLen Brown } 1003103a8feaSLen Brown 1004c98d5d94SLen Brown if (p) { 10053b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10060b2bb692SLen Brown 10070b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10080b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10090b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10100b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10110b2bb692SLen Brown 10123b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10163b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10203b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10213b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1022be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1023be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10247c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10257c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10267c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10277c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 1028*1b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status); 1029*1b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status); 10303b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1031388e9c81SLen Brown 1032388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1033*1b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]); 1034388e9c81SLen Brown } 1035c98d5d94SLen Brown } 10363b4d5c7fSAndy Shevchenko 10373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10383b4d5c7fSAndy Shevchenko 1039c98d5d94SLen Brown return 0; 1040103a8feaSLen Brown } 1041103a8feaSLen Brown 1042e23da037SLen Brown /* 1043e23da037SLen Brown * column formatting convention & formats 1044e23da037SLen Brown */ 1045*1b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1046103a8feaSLen Brown { 1047008d396eSLen Brown double interval_float, tsc; 1048fc04cc67SLen Brown char *fmt8; 1049388e9c81SLen Brown int i; 1050388e9c81SLen Brown struct msr_counter *mp; 10516168c2e0SLen Brown char *delim = "\t"; 10526168c2e0SLen Brown int printed = 0; 1053103a8feaSLen Brown 1054c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1055c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1056c98d5d94SLen Brown return 0; 1057c98d5d94SLen Brown 1058c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1059c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1060c98d5d94SLen Brown return 0; 1061c98d5d94SLen Brown 10621ef7d21aSLen Brown /*if not summary line and --cpu is used */ 1063*1b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 10641ef7d21aSLen Brown return 0; 10651ef7d21aSLen Brown 10663f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1067f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1068f4fdf2b4SLen Brown struct timeval tv; 1069f4fdf2b4SLen Brown 1070f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1071f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1072f4fdf2b4SLen Brown } 1073f4fdf2b4SLen Brown 10743f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 10753f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 10763f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 10773f44a5c6SLen Brown 1078d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 1079103a8feaSLen Brown 1080008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1081008d396eSLen Brown 1082c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1083c98d5d94SLen Brown if (t == &average.threads) { 1084812db3f7SLen Brown if (DO_BIC(BIC_Package)) 10856168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10866de68fe1SLen Brown if (DO_BIC(BIC_Die)) 10876de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 108801235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 108901235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1090812db3f7SLen Brown if (DO_BIC(BIC_Core)) 10916168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1092812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 10936168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10944c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 10954c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10964c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 10974c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1098103a8feaSLen Brown } else { 1099812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1100c98d5d94SLen Brown if (p) 11016168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1102c98d5d94SLen Brown else 11036168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1104c98d5d94SLen Brown } 11056de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11066de68fe1SLen Brown if (c) 11076de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11086de68fe1SLen Brown else 11096de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11106de68fe1SLen Brown } 111101235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 111201235041SPrarit Bhargava if (t) 111301235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 1114*1b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 111501235041SPrarit Bhargava else 1116*1b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 111701235041SPrarit Bhargava } 1118812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1119c98d5d94SLen Brown if (c) 11206168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1121c98d5d94SLen Brown else 11226168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1123c98d5d94SLen Brown } 1124812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11256168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11264c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11274c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11284c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11294c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1130103a8feaSLen Brown } 1131fc04cc67SLen Brown 1132812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 1133*1b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 1134fc04cc67SLen Brown 1135812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc); 1137103a8feaSLen Brown 1138812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 113921ed5574SLen Brown if (has_base_hz) 1140*1b439f01SLen Brown outp += 1141*1b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 114221ed5574SLen Brown else 11436168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1144008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 114521ed5574SLen Brown } 1146103a8feaSLen Brown 1147812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 11486168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 1149103a8feaSLen Brown 11502af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 11512af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 11522af4f9b8SLen Brown 1153562a2d37SLen Brown /* IRQ */ 11540de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 11550de6c0dfSLen Brown if (sums_need_wide_columns) 11566168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 11570de6c0dfSLen Brown else 11586168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 11590de6c0dfSLen Brown } 1160562a2d37SLen Brown 11611cc21f7bSLen Brown /* SMI */ 1162812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 11636168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 11641cc21f7bSLen Brown 1165678a3bd1SLen Brown /* Added counters */ 1166678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1167678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1168678a3bd1SLen Brown if (mp->width == 32) 1169*1b439f01SLen Brown outp += 1170*1b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]); 1171678a3bd1SLen Brown else 11726168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1173678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 11740de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11756168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 11760de6c0dfSLen Brown else 11776168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1178678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 117941618e63SLen Brown if (mp->type == COUNTER_USEC) 1180*1b439f01SLen Brown outp += 1181*1b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1182*1b439f01SLen Brown t->counter[i] / interval_float / 10000); 118341618e63SLen Brown else 11846168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc); 1185678a3bd1SLen Brown } 1186678a3bd1SLen Brown } 1187678a3bd1SLen Brown 118841618e63SLen Brown /* C1 */ 118941618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 11906168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc); 119141618e63SLen Brown 1192c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1193c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1194c98d5d94SLen Brown goto done; 1195c98d5d94SLen Brown 1196562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 11976168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc); 1198812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 11996168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc); 1200812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12016168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc); 1202c98d5d94SLen Brown 12030539ba11SLen Brown /* Mod%c6 */ 12040539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12056168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12060539ba11SLen Brown 1207812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12086168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1209889facbeSLen Brown 1210388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1211388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1212388e9c81SLen Brown if (mp->width == 32) 1213*1b439f01SLen Brown outp += 1214*1b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]); 1215388e9c81SLen Brown else 12166168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1217388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12180de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12196168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12200de6c0dfSLen Brown else 12216168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1222388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc); 1224388e9c81SLen Brown } 1225388e9c81SLen Brown } 1226388e9c81SLen Brown 12279392bd98SCalvin Walton fmt8 = "%s%.2f"; 12289392bd98SCalvin Walton 12299392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 1230*1b439f01SLen Brown outp += 1231*1b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12329392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12339392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12349392bd98SCalvin Walton 1235c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1236c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1237c98d5d94SLen Brown goto done; 1238c98d5d94SLen Brown 12390b2bb692SLen Brown /* PkgTmp */ 1240812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 12416168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1242889facbeSLen Brown 1243fdf676e5SLen Brown /* GFXrc6 */ 1244812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1245ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 12466168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 12479185e988SLen Brown } else { 12486168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12499185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 12509185e988SLen Brown } 12519185e988SLen Brown } 1252fdf676e5SLen Brown 125327d47356SLen Brown /* GFXMHz */ 1254812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 12556168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 125627d47356SLen Brown 1257b4b91569SRafael Antognolli /* GFXACTMHz */ 1258b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1259b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1260b4b91569SRafael Antognolli 12610b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1262a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12636168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc); 1264a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12656168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc); 1266a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12676168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc); 1268a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12696168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc); 12700b2bb692SLen Brown 12710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 12726168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc); 12730f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 12746168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc); 12750f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 12766168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc); 12770f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 12786168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc); 12790f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 12806168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc); 12810f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 12826168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc); 12830f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 12846168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc); 1285889facbeSLen Brown 1286be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1287*1b439f01SLen Brown outp += 1288*1b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1289be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1290*1b439f01SLen Brown outp += 1291*1b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1292be0e54c4SLen Brown 1293812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 1294*1b439f01SLen Brown outp += 1295*1b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 12969392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 1297*1b439f01SLen Brown outp += 1298*1b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1299812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 1300*1b439f01SLen Brown outp += 1301*1b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1302812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 1303*1b439f01SLen Brown outp += 1304*1b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 1305*1b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float); 1306812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13076168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13089392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13096168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1310812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13116168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1312812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13136168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1314812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 1315*1b439f01SLen Brown outp += 1316*1b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 1317*1b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1318812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 1319*1b439f01SLen Brown outp += 1320*1b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 1321*1b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1322812db3f7SLen Brown 1323388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1324388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1325388e9c81SLen Brown if (mp->width == 32) 1326*1b439f01SLen Brown outp += 1327*1b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]); 1328388e9c81SLen Brown else 13296168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1330388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 13310de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 13326168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 13330de6c0dfSLen Brown else 13346168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1335388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 13366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc); 1337388e9c81SLen Brown } 1338388e9c81SLen Brown } 1339388e9c81SLen Brown 1340c98d5d94SLen Brown done: 134194d6ab4bSLen Brown if (*(outp - 1) != '\n') 1342c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1343c98d5d94SLen Brown 1344c98d5d94SLen Brown return 0; 1345103a8feaSLen Brown } 1346103a8feaSLen Brown 1347b7d8c148SLen Brown void flush_output_stdout(void) 1348103a8feaSLen Brown { 1349b7d8c148SLen Brown FILE *filep; 1350b7d8c148SLen Brown 1351b7d8c148SLen Brown if (outf == stderr) 1352b7d8c148SLen Brown filep = stdout; 1353b7d8c148SLen Brown else 1354b7d8c148SLen Brown filep = outf; 1355b7d8c148SLen Brown 1356b7d8c148SLen Brown fputs(output_buffer, filep); 1357b7d8c148SLen Brown fflush(filep); 1358b7d8c148SLen Brown 1359c98d5d94SLen Brown outp = output_buffer; 1360c98d5d94SLen Brown } 1361*1b439f01SLen Brown 1362b7d8c148SLen Brown void flush_output_stderr(void) 1363c98d5d94SLen Brown { 1364b7d8c148SLen Brown fputs(output_buffer, outf); 1365b7d8c148SLen Brown fflush(outf); 1366c98d5d94SLen Brown outp = output_buffer; 1367c98d5d94SLen Brown } 1368*1b439f01SLen Brown 1369c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1370c98d5d94SLen Brown { 1371e23da037SLen Brown static int printed; 1372103a8feaSLen Brown 1373e23da037SLen Brown if (!printed || !summary_only) 1374c8ade361SLen Brown print_header("\t"); 1375103a8feaSLen Brown 13769d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1377103a8feaSLen Brown 1378e23da037SLen Brown printed = 1; 1379e23da037SLen Brown 1380e23da037SLen Brown if (summary_only) 1381e23da037SLen Brown return; 1382e23da037SLen Brown 1383c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1384103a8feaSLen Brown } 1385103a8feaSLen Brown 1386889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 13877c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1388889facbeSLen Brown 1389*1b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old) 1390103a8feaSLen Brown { 1391388e9c81SLen Brown int i; 1392388e9c81SLen Brown struct msr_counter *mp; 13930b2bb692SLen Brown 1394a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13950b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1396a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13970b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1398a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13990b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1400a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14010b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1402a99d8730SLen Brown 1403c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 14040f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1405c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 14060f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1407c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 14080f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1409c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1410ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1411ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1412ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1413be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1414be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1415889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1416889facbeSLen Brown 14179185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 14189185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 14199185e988SLen Brown old->gfx_rc6_ms = -1; 14209185e988SLen Brown else 1421fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 14229185e988SLen Brown 142327d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1424b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 142527d47356SLen Brown 142687e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 142787e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 142887e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 142987e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 143087e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 143187e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1432ba3dec99SLen Brown 1433388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1434388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1435388e9c81SLen Brown old->counter[i] = new->counter[i]; 1436388e9c81SLen Brown else 1437388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1438388e9c81SLen Brown } 1439388e9c81SLen Brown 1440ba3dec99SLen Brown return 0; 1441103a8feaSLen Brown } 1442103a8feaSLen Brown 1443*1b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old) 1444c98d5d94SLen Brown { 1445388e9c81SLen Brown int i; 1446388e9c81SLen Brown struct msr_counter *mp; 1447388e9c81SLen Brown 1448c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1449c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1450c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1451889facbeSLen Brown old->core_temp_c = new->core_temp_c; 14520539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1453388e9c81SLen Brown 14549392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 14559392bd98SCalvin Walton 1456388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1457388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1458388e9c81SLen Brown old->counter[i] = new->counter[i]; 1459388e9c81SLen Brown else 1460388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1461388e9c81SLen Brown } 1462103a8feaSLen Brown } 1463c98d5d94SLen Brown 14641e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 14651e9042b9SSrinivas Pandruvada { 14661e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 14671e9042b9SSrinivas Pandruvada return 0; 14681e9042b9SSrinivas Pandruvada 14691e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 14701e9042b9SSrinivas Pandruvada } 14711e9042b9SSrinivas Pandruvada 1472c3ae331dSLen Brown /* 1473c3ae331dSLen Brown * old = new - old 1474c3ae331dSLen Brown */ 1475*1b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 1476c98d5d94SLen Brown { 1477388e9c81SLen Brown int i; 1478388e9c81SLen Brown struct msr_counter *mp; 1479388e9c81SLen Brown 14804c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 14814c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14824c2122d4SLen Brown new->apic_id = old->apic_id; 14834c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14844c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 14854c2122d4SLen Brown 14863f44a5c6SLen Brown /* 14873f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 14883f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 14893f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 14903f44a5c6SLen Brown */ 14913f44a5c6SLen Brown 1492d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 14933f44a5c6SLen Brown old->tv_begin = new->tv_begin; 14943f44a5c6SLen Brown old->tv_end = new->tv_end; 14953f44a5c6SLen Brown 1496c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1497c98d5d94SLen Brown 1498103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1499b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1500b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1501b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1502b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1503103a8feaSLen Brown 1504c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1505c98d5d94SLen Brown 1506*1b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 1507c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1508c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1509c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1510c98d5d94SLen Brown } else { 1511ba3dec99SLen Brown return -1; 1512103a8feaSLen Brown } 1513a729617cSLen Brown } 1514103a8feaSLen Brown 1515144b44b1SLen Brown if (use_c1_residency_msr) { 1516144b44b1SLen Brown /* 1517144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1518144b44b1SLen Brown * which should be more accurate than the derivation below. 1519144b44b1SLen Brown */ 1520144b44b1SLen Brown } else { 1521103a8feaSLen Brown /* 1522c3ae331dSLen Brown * As counter collection is not atomic, 1523c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1524103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1525103a8feaSLen Brown */ 152695149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1527c98d5d94SLen Brown old->c1 = 0; 1528c98d5d94SLen Brown else { 1529c98d5d94SLen Brown /* normal case, derive c1 */ 1530008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1531c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1532c98d5d94SLen Brown } 1533144b44b1SLen Brown } 1534c3ae331dSLen Brown 1535c98d5d94SLen Brown if (old->mperf == 0) { 1536b7d8c148SLen Brown if (debug > 1) 1537b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1538c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1539c98d5d94SLen Brown } 1540103a8feaSLen Brown 15412af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 15422af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 15432af4f9b8SLen Brown 1544812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1545562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1546562a2d37SLen Brown 1547812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 15481ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1549ba3dec99SLen Brown 1550388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1551388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1552388e9c81SLen Brown old->counter[i] = new->counter[i]; 1553388e9c81SLen Brown else 1554388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1555388e9c81SLen Brown } 1556ba3dec99SLen Brown return 0; 1557103a8feaSLen Brown } 1558c98d5d94SLen Brown 1559c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1560*1b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 1561c98d5d94SLen Brown { 1562ba3dec99SLen Brown int retval = 0; 1563ba3dec99SLen Brown 1564c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1565c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1566c98d5d94SLen Brown delta_core(c, c2); 1567c98d5d94SLen Brown 1568c98d5d94SLen Brown /* always calculate thread delta */ 1569ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1570ba3dec99SLen Brown if (retval) 1571ba3dec99SLen Brown return retval; 1572c98d5d94SLen Brown 1573c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1574c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1575ba3dec99SLen Brown retval = delta_package(p, p2); 1576c98d5d94SLen Brown 1577ba3dec99SLen Brown return retval; 1578103a8feaSLen Brown } 1579103a8feaSLen Brown 1580c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1581103a8feaSLen Brown { 1582388e9c81SLen Brown int i; 1583388e9c81SLen Brown struct msr_counter *mp; 1584388e9c81SLen Brown 15853f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 15863f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 15873f44a5c6SLen Brown t->tv_end.tv_sec = 0; 15883f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1589d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1590d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 15913f44a5c6SLen Brown 1592c98d5d94SLen Brown t->tsc = 0; 1593c98d5d94SLen Brown t->aperf = 0; 1594c98d5d94SLen Brown t->mperf = 0; 1595c98d5d94SLen Brown t->c1 = 0; 1596103a8feaSLen Brown 15972af4f9b8SLen Brown t->instr_count = 0; 15982af4f9b8SLen Brown 1599562a2d37SLen Brown t->irq_count = 0; 1600562a2d37SLen Brown t->smi_count = 0; 1601562a2d37SLen Brown 1602c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1603c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1604c98d5d94SLen Brown 1605c98d5d94SLen Brown c->c3 = 0; 1606c98d5d94SLen Brown c->c6 = 0; 1607c98d5d94SLen Brown c->c7 = 0; 16080539ba11SLen Brown c->mc6_us = 0; 1609889facbeSLen Brown c->core_temp_c = 0; 16109392bd98SCalvin Walton c->core_energy = 0; 1611c98d5d94SLen Brown 16120b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16130b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16140b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16150b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16160b2bb692SLen Brown 1617c98d5d94SLen Brown p->pc2 = 0; 16180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1619c98d5d94SLen Brown p->pc3 = 0; 16200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1621c98d5d94SLen Brown p->pc6 = 0; 16220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1623c98d5d94SLen Brown p->pc7 = 0; 1624ca58710fSKristen Carlson Accardi p->pc8 = 0; 1625ca58710fSKristen Carlson Accardi p->pc9 = 0; 1626ca58710fSKristen Carlson Accardi p->pc10 = 0; 1627be0e54c4SLen Brown p->cpu_lpi = 0; 1628be0e54c4SLen Brown p->sys_lpi = 0; 1629889facbeSLen Brown 1630889facbeSLen Brown p->energy_pkg = 0; 1631889facbeSLen Brown p->energy_dram = 0; 1632889facbeSLen Brown p->energy_cores = 0; 1633889facbeSLen Brown p->energy_gfx = 0; 1634889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1635889facbeSLen Brown p->rapl_dram_perf_status = 0; 1636889facbeSLen Brown p->pkg_temp_c = 0; 163727d47356SLen Brown 1638fdf676e5SLen Brown p->gfx_rc6_ms = 0; 163927d47356SLen Brown p->gfx_mhz = 0; 1640b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1641388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1642388e9c81SLen Brown t->counter[i] = 0; 1643388e9c81SLen Brown 1644388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1645388e9c81SLen Brown c->counter[i] = 0; 1646388e9c81SLen Brown 1647388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1648388e9c81SLen Brown p->counter[i] = 0; 1649103a8feaSLen Brown } 1650*1b439f01SLen Brown 1651*1b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1652103a8feaSLen Brown { 1653388e9c81SLen Brown int i; 1654388e9c81SLen Brown struct msr_counter *mp; 1655388e9c81SLen Brown 16564c2122d4SLen Brown /* copy un-changing apic_id's */ 16574c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 16584c2122d4SLen Brown average.threads.apic_id = t->apic_id; 16594c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 16604c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 16614c2122d4SLen Brown 16623f44a5c6SLen Brown /* remember first tv_begin */ 16633f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 16643f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 16653f44a5c6SLen Brown 16663f44a5c6SLen Brown /* remember last tv_end */ 16673f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 16683f44a5c6SLen Brown 1669c98d5d94SLen Brown average.threads.tsc += t->tsc; 1670c98d5d94SLen Brown average.threads.aperf += t->aperf; 1671c98d5d94SLen Brown average.threads.mperf += t->mperf; 1672c98d5d94SLen Brown average.threads.c1 += t->c1; 167315aaa346SLen Brown 16742af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 16752af4f9b8SLen Brown 1676562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1677562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1678562a2d37SLen Brown 1679388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1680388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1681388e9c81SLen Brown continue; 1682388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1683388e9c81SLen Brown } 1684388e9c81SLen Brown 1685c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1686c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1687c98d5d94SLen Brown return 0; 1688c98d5d94SLen Brown 1689c98d5d94SLen Brown average.cores.c3 += c->c3; 1690c98d5d94SLen Brown average.cores.c6 += c->c6; 1691c98d5d94SLen Brown average.cores.c7 += c->c7; 16920539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1693c98d5d94SLen Brown 1694889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1695889facbeSLen Brown 16969392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 16979392bd98SCalvin Walton 1698388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1699388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1700388e9c81SLen Brown continue; 1701388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1702388e9c81SLen Brown } 1703388e9c81SLen Brown 1704c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1705c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1706c98d5d94SLen Brown return 0; 1707c98d5d94SLen Brown 1708a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17090b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1710a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17110b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1712a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17130b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1714a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17150b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17160b2bb692SLen Brown 1717c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1719c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1721c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1723c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1724ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1725ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1726ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1727c98d5d94SLen Brown 1728be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1729be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1730be0e54c4SLen Brown 1731889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1732889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1733889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1734889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1735889facbeSLen Brown 1736fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 173727d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1738b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 173927d47356SLen Brown 1740889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1741889facbeSLen Brown 1742889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1743889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1744388e9c81SLen Brown 1745388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1746388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1747388e9c81SLen Brown continue; 1748388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1749388e9c81SLen Brown } 1750c98d5d94SLen Brown return 0; 1751c98d5d94SLen Brown } 1752*1b439f01SLen Brown 1753c98d5d94SLen Brown /* 1754c98d5d94SLen Brown * sum the counters for all cpus in the system 1755c98d5d94SLen Brown * compute the weighted average 1756c98d5d94SLen Brown */ 1757*1b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1758c98d5d94SLen Brown { 1759388e9c81SLen Brown int i; 1760388e9c81SLen Brown struct msr_counter *mp; 1761388e9c81SLen Brown 1762c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1763c98d5d94SLen Brown 1764c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1765c98d5d94SLen Brown 1766d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1767d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1768d4794f25SYazen Ghannam 1769c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1770c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1771c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 17722af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1773c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1774c98d5d94SLen Brown 17750de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 17760de6c0dfSLen Brown sums_need_wide_columns = 1; 17770de6c0dfSLen Brown 1778c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1779c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1780c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 17810539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1782c98d5d94SLen Brown 1783a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17840b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1785a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17860b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1787a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17880b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1789a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17900b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 17910b2bb692SLen Brown 1792c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 17930f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1794c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 17950f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1796c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 17970f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1798c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1799ca58710fSKristen Carlson Accardi 1800ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1801ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1802ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1803388e9c81SLen Brown 1804388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1805388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1806388e9c81SLen Brown continue; 18070de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18080de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18090de6c0dfSLen Brown sums_need_wide_columns = 1; 181041618e63SLen Brown continue; 18110de6c0dfSLen Brown } 1812388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1813388e9c81SLen Brown } 1814388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1815388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1816388e9c81SLen Brown continue; 18170de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18180de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18190de6c0dfSLen Brown sums_need_wide_columns = 1; 18200de6c0dfSLen Brown } 1821388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1822388e9c81SLen Brown } 1823388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1824388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1825388e9c81SLen Brown continue; 18260de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18270de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18280de6c0dfSLen Brown sums_need_wide_columns = 1; 18290de6c0dfSLen Brown } 1830388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1831388e9c81SLen Brown } 1832c98d5d94SLen Brown } 1833c98d5d94SLen Brown 1834c98d5d94SLen Brown static unsigned long long rdtsc(void) 1835c98d5d94SLen Brown { 1836c98d5d94SLen Brown unsigned int low, high; 1837c98d5d94SLen Brown 1838c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high)); 1839c98d5d94SLen Brown 1840c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1841c98d5d94SLen Brown } 1842c98d5d94SLen Brown 1843c98d5d94SLen Brown /* 1844495c7654SLen Brown * Open a file, and exit on failure 1845495c7654SLen Brown */ 1846495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1847495c7654SLen Brown { 1848495c7654SLen Brown FILE *filep = fopen(path, mode); 1849495c7654SLen Brown 1850495c7654SLen Brown if (!filep) 1851495c7654SLen Brown err(1, "%s: open failed", path); 1852495c7654SLen Brown return filep; 1853495c7654SLen Brown } 1854*1b439f01SLen Brown 1855495c7654SLen Brown /* 1856495c7654SLen Brown * snapshot_sysfs_counter() 1857495c7654SLen Brown * 1858495c7654SLen Brown * return snapshot of given counter 1859495c7654SLen Brown */ 1860495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1861495c7654SLen Brown { 1862495c7654SLen Brown FILE *fp; 1863495c7654SLen Brown int retval; 1864495c7654SLen Brown unsigned long long counter; 1865495c7654SLen Brown 1866495c7654SLen Brown fp = fopen_or_die(path, "r"); 1867495c7654SLen Brown 1868495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1869495c7654SLen Brown if (retval != 1) 1870495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1871495c7654SLen Brown 1872495c7654SLen Brown fclose(fp); 1873495c7654SLen Brown 1874495c7654SLen Brown return counter; 1875495c7654SLen Brown } 1876495c7654SLen Brown 1877495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1878495c7654SLen Brown { 1879495c7654SLen Brown if (mp->msr_num != 0) { 1880495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1881495c7654SLen Brown return -1; 1882495c7654SLen Brown } else { 188346c27978SLen Brown char path[128 + PATH_BYTES]; 188441618e63SLen Brown 188541618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 1886*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path); 188741618e63SLen Brown 188841618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 188941618e63SLen Brown } else { 1890495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1891495c7654SLen Brown } 189241618e63SLen Brown } 1893495c7654SLen Brown 1894495c7654SLen Brown return 0; 1895495c7654SLen Brown } 1896495c7654SLen Brown 18976d6501d9SBorislav Petkov int get_epb(int cpu) 18986d6501d9SBorislav Petkov { 18996d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19007f1b11baSBorislav Petkov unsigned long long msr; 19016d6501d9SBorislav Petkov int ret, epb = -1; 19026d6501d9SBorislav Petkov FILE *fp; 19036d6501d9SBorislav Petkov 19046d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19056d6501d9SBorislav Petkov 19067f1b11baSBorislav Petkov fp = fopen(path, "r"); 19077f1b11baSBorislav Petkov if (!fp) 19087f1b11baSBorislav Petkov goto msr_fallback; 19096d6501d9SBorislav Petkov 19106d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19116d6501d9SBorislav Petkov if (ret != 1) 19126d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19136d6501d9SBorislav Petkov 19146d6501d9SBorislav Petkov fclose(fp); 19156d6501d9SBorislav Petkov 19166d6501d9SBorislav Petkov return epb; 19177f1b11baSBorislav Petkov 19187f1b11baSBorislav Petkov msr_fallback: 19197f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 19207f1b11baSBorislav Petkov 19217f1b11baSBorislav Petkov return msr & 0xf; 19226d6501d9SBorislav Petkov } 19236d6501d9SBorislav Petkov 19244c2122d4SLen Brown void get_apic_id(struct thread_data *t) 19254c2122d4SLen Brown { 192634041551SLen Brown unsigned int eax, ebx, ecx, edx; 192734041551SLen Brown 192834041551SLen Brown if (DO_BIC(BIC_APIC)) { 192934041551SLen Brown eax = ebx = ecx = edx = 0; 193034041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 193134041551SLen Brown 193234041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 193334041551SLen Brown } 193434041551SLen Brown 193534041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 193634041551SLen Brown return; 193734041551SLen Brown 1938c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 193934041551SLen Brown unsigned int topology_extensions; 194034041551SLen Brown 194134041551SLen Brown if (max_extended_level < 0x8000001e) 194234041551SLen Brown return; 19434c2122d4SLen Brown 19444c2122d4SLen Brown eax = ebx = ecx = edx = 0; 194534041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 194634041551SLen Brown topology_extensions = ecx & (1 << 22); 194734041551SLen Brown 194834041551SLen Brown if (topology_extensions == 0) 194934041551SLen Brown return; 195034041551SLen Brown 195134041551SLen Brown eax = ebx = ecx = edx = 0; 195234041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 195334041551SLen Brown 195434041551SLen Brown t->x2apic_id = eax; 195534041551SLen Brown return; 195634041551SLen Brown } 19574c2122d4SLen Brown 19584c2122d4SLen Brown if (!genuine_intel) 19594c2122d4SLen Brown return; 19604c2122d4SLen Brown 19614c2122d4SLen Brown if (max_level < 0xb) 19624c2122d4SLen Brown return; 19634c2122d4SLen Brown 19644c2122d4SLen Brown ecx = 0; 19654c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 19664c2122d4SLen Brown t->x2apic_id = edx; 19674c2122d4SLen Brown 196834041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 1969*1b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 19704c2122d4SLen Brown } 19714c2122d4SLen Brown 1972495c7654SLen Brown /* 1973c98d5d94SLen Brown * get_counters(...) 1974c98d5d94SLen Brown * migrate to cpu 1975c98d5d94SLen Brown * acquire and record local counters for that cpu 1976c98d5d94SLen Brown */ 1977c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1978c98d5d94SLen Brown { 1979c98d5d94SLen Brown int cpu = t->cpu_id; 1980889facbeSLen Brown unsigned long long msr; 19810102b067SLen Brown int aperf_mperf_retry_count = 0; 1982388e9c81SLen Brown struct msr_counter *mp; 1983388e9c81SLen Brown int i; 1984c98d5d94SLen Brown 1985e52966c0SLen Brown if (cpu_migrate(cpu)) { 19863d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 198715aaa346SLen Brown return -1; 1988e52966c0SLen Brown } 198915aaa346SLen Brown 1990d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1991d4794f25SYazen Ghannam 19924c2122d4SLen Brown if (first_counter_read) 19934c2122d4SLen Brown get_apic_id(t); 19940102b067SLen Brown retry: 1995c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 199615aaa346SLen Brown 1997*1b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 19980102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 19990102b067SLen Brown 20000102b067SLen Brown /* 20010102b067SLen Brown * The TSC, APERF and MPERF must be read together for 20020102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 20030102b067SLen Brown * 20040102b067SLen Brown * Unfortunately, APERF and MPERF are read by 20050102b067SLen Brown * individual system call, so delays may occur 20060102b067SLen Brown * between them. If the time to read them 20070102b067SLen Brown * varies by a large amount, we re-read them. 20080102b067SLen Brown */ 20090102b067SLen Brown 20100102b067SLen Brown /* 20110102b067SLen Brown * This initial dummy APERF read has been seen to 20120102b067SLen Brown * reduce jitter in the subsequent reads. 20130102b067SLen Brown */ 20140102b067SLen Brown 20159c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2016c98d5d94SLen Brown return -3; 20170102b067SLen Brown 20180102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 20190102b067SLen Brown 20200102b067SLen Brown tsc_before = t->tsc; 20210102b067SLen Brown 20220102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 20230102b067SLen Brown return -3; 20240102b067SLen Brown 20250102b067SLen Brown tsc_between = rdtsc(); 20260102b067SLen Brown 20279c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2028c98d5d94SLen Brown return -4; 20290102b067SLen Brown 20300102b067SLen Brown tsc_after = rdtsc(); 20310102b067SLen Brown 20320102b067SLen Brown aperf_time = tsc_between - tsc_before; 20330102b067SLen Brown mperf_time = tsc_after - tsc_between; 20340102b067SLen Brown 20350102b067SLen Brown /* 20360102b067SLen Brown * If the system call latency to read APERF and MPERF 20370102b067SLen Brown * differ by more than 2x, then try again. 20380102b067SLen Brown */ 20390102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 20400102b067SLen Brown aperf_mperf_retry_count++; 20410102b067SLen Brown if (aperf_mperf_retry_count < 5) 20420102b067SLen Brown goto retry; 20430102b067SLen Brown else 2044*1b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time); 20450102b067SLen Brown } 20460102b067SLen Brown aperf_mperf_retry_count = 0; 20470102b067SLen Brown 2048b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2049b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 205088c3281fSLen Brown } 205188c3281fSLen Brown 20522af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 20532af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 20542af4f9b8SLen Brown return -4; 20552af4f9b8SLen Brown 2056812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2057562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2058812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 20591ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 20601ed51011SLen Brown return -5; 20611ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 20621ed51011SLen Brown } 20630539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2064144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2065144b44b1SLen Brown return -6; 2066144b44b1SLen Brown } 2067144b44b1SLen Brown 2068388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2069495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2070388e9c81SLen Brown return -10; 2071388e9c81SLen Brown } 2072388e9c81SLen Brown 2073c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2074c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2075f4fdf2b4SLen Brown goto done; 2076c98d5d94SLen Brown 20771e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2078c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2079c98d5d94SLen Brown return -6; 2080144b44b1SLen Brown } 2081144b44b1SLen Brown 20821e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2083c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2084c98d5d94SLen Brown return -7; 20851e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2086fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2087fb5d4327SDasaratharaman Chandramouli return -7; 2088103a8feaSLen Brown } 208915aaa346SLen Brown 20907ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2091c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2092c98d5d94SLen Brown return -8; 20937ab5ff49SZhang Rui else if (t->is_atom) { 20947ab5ff49SZhang Rui /* 20957ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 20967ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 20977ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 20987ab5ff49SZhang Rui * accturate cc6 residency. 20997ab5ff49SZhang Rui */ 21007ab5ff49SZhang Rui c->c6 -= c->c7; 21017ab5ff49SZhang Rui } 21027ab5ff49SZhang Rui } 2103c98d5d94SLen Brown 21040539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 21050539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 21060539ba11SLen Brown return -8; 21070539ba11SLen Brown 2108812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2109889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2110889facbeSLen Brown return -9; 211155279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 2112889facbeSLen Brown } 2113889facbeSLen Brown 21149392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21159392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 21169392bd98SCalvin Walton return -14; 21179392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 21189392bd98SCalvin Walton } 21199392bd98SCalvin Walton 2120388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2121495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2122388e9c81SLen Brown return -10; 2123388e9c81SLen Brown } 2124889facbeSLen Brown 2125c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2126c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2127f4fdf2b4SLen Brown goto done; 212815aaa346SLen Brown 2129a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 21300b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 21310b2bb692SLen Brown return -10; 2132a99d8730SLen Brown } 2133a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 21340b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 21350b2bb692SLen Brown return -11; 2136a99d8730SLen Brown } 2137a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 21380b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 21390b2bb692SLen Brown return -12; 2140a99d8730SLen Brown } 2141a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 21420b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 21430b2bb692SLen Brown return -13; 21440b2bb692SLen Brown } 21450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2146c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2147c98d5d94SLen Brown return -9; 21480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 21490539ba11SLen Brown if (do_slm_cstates) { 21500539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 21510539ba11SLen Brown return -10; 21520539ba11SLen Brown } else { 2153c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2154c98d5d94SLen Brown return -10; 21550539ba11SLen Brown } 21560539ba11SLen Brown } 21570539ba11SLen Brown 21580f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2159c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2160c98d5d94SLen Brown return -11; 21610f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2162c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2163c98d5d94SLen Brown return -12; 21640f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2165ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2166ca58710fSKristen Carlson Accardi return -13; 21670f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2168ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2169ca58710fSKristen Carlson Accardi return -13; 21700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2171ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2172ca58710fSKristen Carlson Accardi return -13; 21730f47c08dSLen Brown 2174be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2175be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2176be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2177be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2178be0e54c4SLen Brown 2179889facbeSLen Brown if (do_rapl & RAPL_PKG) { 21809972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2181889facbeSLen Brown return -13; 21829972d5d8SChen Yu p->energy_pkg = msr; 2183889facbeSLen Brown } 21849148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 21859972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2186889facbeSLen Brown return -14; 21879972d5d8SChen Yu p->energy_cores = msr; 2188889facbeSLen Brown } 2189889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 21909972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2191889facbeSLen Brown return -15; 21929972d5d8SChen Yu p->energy_dram = msr; 2193889facbeSLen Brown } 2194889facbeSLen Brown if (do_rapl & RAPL_GFX) { 21959972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2196889facbeSLen Brown return -16; 21979972d5d8SChen Yu p->energy_gfx = msr; 2198889facbeSLen Brown } 2199889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 22009972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2201889facbeSLen Brown return -16; 22029972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2203889facbeSLen Brown } 2204889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 22059972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2206889facbeSLen Brown return -16; 22079972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2208889facbeSLen Brown } 22093316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22109972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 22113316f99aSCalvin Walton return -13; 22129972d5d8SChen Yu p->energy_pkg = msr; 22133316f99aSCalvin Walton } 2214812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2215889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2216889facbeSLen Brown return -17; 221755279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 2218889facbeSLen Brown } 2219fdf676e5SLen Brown 2220812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2221fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2222fdf676e5SLen Brown 2223812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 222427d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 222527d47356SLen Brown 2226b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2227b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2228b4b91569SRafael Antognolli 2229388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2230495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2231388e9c81SLen Brown return -10; 2232388e9c81SLen Brown } 2233f4fdf2b4SLen Brown done: 2234f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2235388e9c81SLen Brown 223615aaa346SLen Brown return 0; 2237103a8feaSLen Brown } 2238103a8feaSLen Brown 2239ee7e38e3SLen Brown /* 2240ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2241ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2242ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2243ee7e38e3SLen Brown */ 2244ee7e38e3SLen Brown 2245ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2246ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2247ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2248ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2249ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2250ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2251ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2252ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2253ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2254ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2255ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2256ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 22570b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 22580b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2259445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2260445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2261ee7e38e3SLen Brown 2262ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2263ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 2264*1b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 2265*1b439f01SLen Brown }; 2266ee7e38e3SLen Brown 2267*1b439f01SLen Brown int nhm_pkg_cstate_limits[16] = 2268*1b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2269*1b439f01SLen Brown PCLRSV, PCLRSV }; 2270*1b439f01SLen Brown int snb_pkg_cstate_limits[16] = 2271*1b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2272*1b439f01SLen Brown PCLRSV, PCLRSV }; 2273*1b439f01SLen Brown int hsw_pkg_cstate_limits[16] = 2274*1b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2275*1b439f01SLen Brown PCLRSV, PCLRSV }; 2276*1b439f01SLen Brown int slv_pkg_cstate_limits[16] = 2277*1b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2278*1b439f01SLen Brown PCL__6, PCL__7 }; 2279*1b439f01SLen Brown int amt_pkg_cstate_limits[16] = 2280*1b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2281*1b439f01SLen Brown PCLRSV, PCLRSV }; 2282*1b439f01SLen Brown int phi_pkg_cstate_limits[16] = 2283*1b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2284*1b439f01SLen Brown PCLRSV, PCLRSV }; 2285*1b439f01SLen Brown int glm_pkg_cstate_limits[16] = 2286*1b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2287*1b439f01SLen Brown PCLRSV, PCLRSV }; 2288*1b439f01SLen Brown int skx_pkg_cstate_limits[16] = 2289*1b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2290*1b439f01SLen Brown PCLRSV, PCLRSV }; 2291*1b439f01SLen Brown int icx_pkg_cstate_limits[16] = 2292*1b439f01SLen Brown { PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 2293*1b439f01SLen Brown PCLRSV, PCLRSV }; 2294a2b7b749SLen Brown 2295*1b439f01SLen Brown static void calculate_tsc_tweak() 2296a2b7b749SLen Brown { 2297a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2298a2b7b749SLen Brown } 2299a2b7b749SLen Brown 2300aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2301aeb01e6dSChen Yu 2302*1b439f01SLen Brown static void dump_nhm_platform_info(void) 2303103a8feaSLen Brown { 2304103a8feaSLen Brown unsigned long long msr; 2305103a8feaSLen Brown unsigned int ratio; 2306103a8feaSLen Brown 2307ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2308103a8feaSLen Brown 2309b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 23106574a5d5SLen Brown 2311103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 2312*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 2313103a8feaSLen Brown 2314103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 2315*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 2316103a8feaSLen Brown 23177ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2318b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2319bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 232067920418SLen Brown 2321aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2322aeb01e6dSChen Yu if (dis_cstate_prewake) 2323*1b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 2324aeb01e6dSChen Yu 2325fcd17211SLen Brown return; 2326fcd17211SLen Brown } 2327fcd17211SLen Brown 2328*1b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void) 2329fcd17211SLen Brown { 2330fcd17211SLen Brown unsigned long long msr; 2331fcd17211SLen Brown unsigned int ratio; 2332fcd17211SLen Brown 23337ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2334fcd17211SLen Brown 2335b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2336fcd17211SLen Brown 2337fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2338fcd17211SLen Brown if (ratio) 2339*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 2340fcd17211SLen Brown 2341fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2342fcd17211SLen Brown if (ratio) 2343*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 2344fcd17211SLen Brown return; 2345fcd17211SLen Brown } 2346fcd17211SLen Brown 2347*1b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void) 2348fcd17211SLen Brown { 2349fcd17211SLen Brown unsigned long long msr; 2350fcd17211SLen Brown unsigned int ratio; 23516574a5d5SLen Brown 23527ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 23536574a5d5SLen Brown 2354b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 23556574a5d5SLen Brown 23566574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 23576574a5d5SLen Brown if (ratio) 2358*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 23596574a5d5SLen Brown 23606574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 23616574a5d5SLen Brown if (ratio) 2362*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 23636574a5d5SLen Brown 23646574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 23656574a5d5SLen Brown if (ratio) 2366*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 23676574a5d5SLen Brown 23686574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 23696574a5d5SLen Brown if (ratio) 2370*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 23716574a5d5SLen Brown 23726574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 23736574a5d5SLen Brown if (ratio) 2374*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 23756574a5d5SLen Brown 23766574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 23776574a5d5SLen Brown if (ratio) 2378*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 23796574a5d5SLen Brown 23806574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 23816574a5d5SLen Brown if (ratio) 2382*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 23836574a5d5SLen Brown 23846574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 23856574a5d5SLen Brown if (ratio) 2386*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 2387103a8feaSLen Brown return; 2388fcd17211SLen Brown } 2389*1b439f01SLen Brown 239031e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 239131e07522SLen Brown { 239231e07522SLen Brown 239331e07522SLen Brown if (!genuine_intel) 239431e07522SLen Brown return 0; 239531e07522SLen Brown 239631e07522SLen Brown switch (model) { 239731e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 239831e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 239925368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 24005ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 240120de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 240231e07522SLen Brown return 1; 240331e07522SLen Brown } 240431e07522SLen Brown return 0; 240531e07522SLen Brown } 2406fcd17211SLen Brown 2407*1b439f01SLen Brown static void dump_turbo_ratio_limits(int family, int model) 2408fcd17211SLen Brown { 240931e07522SLen Brown unsigned long long msr, core_counts; 241031e07522SLen Brown unsigned int ratio, group_size; 2411103a8feaSLen Brown 24127ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2413b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 24146574a5d5SLen Brown 241531e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 241631e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 241731e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 241831e07522SLen Brown } else { 241931e07522SLen Brown core_counts = 0x0807060504030201; 242031e07522SLen Brown } 242131e07522SLen Brown 24226574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 242331e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 24246574a5d5SLen Brown if (ratio) 242531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 242631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24276574a5d5SLen Brown 24286574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 242931e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 24306574a5d5SLen Brown if (ratio) 243131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 243231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24336574a5d5SLen Brown 24346574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 243531e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 24366574a5d5SLen Brown if (ratio) 243731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 243831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24396574a5d5SLen Brown 24406574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 244131e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 24426574a5d5SLen Brown if (ratio) 244331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 244431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24456574a5d5SLen Brown 2446103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 244731e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2448103a8feaSLen Brown if (ratio) 244931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 245031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2451103a8feaSLen Brown 2452103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 245331e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2454103a8feaSLen Brown if (ratio) 245531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 245631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2457103a8feaSLen Brown 2458103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 245931e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2460103a8feaSLen Brown if (ratio) 246131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 246231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2463103a8feaSLen Brown 2464103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 246531e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2466103a8feaSLen Brown if (ratio) 246731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 246831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2469fcd17211SLen Brown return; 2470fcd17211SLen Brown } 24713a9a941dSLen Brown 2472*1b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void) 24730f7887c4SLen Brown { 24740f7887c4SLen Brown unsigned long long msr; 24750f7887c4SLen Brown unsigned int ratio; 24760f7887c4SLen Brown 24770f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 24780f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 24790f7887c4SLen Brown 24800f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 24810f7887c4SLen Brown if (ratio) 2482*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 24830f7887c4SLen Brown 24840f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 24850f7887c4SLen Brown if (ratio) 2486*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 24870f7887c4SLen Brown 24880f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 24890f7887c4SLen Brown if (ratio) 2490*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 24910f7887c4SLen Brown 24920f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 24930f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 24940f7887c4SLen Brown 24950f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 24960f7887c4SLen Brown if (ratio) 2497*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 24980f7887c4SLen Brown 24990f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25000f7887c4SLen Brown if (ratio) 2501*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 25020f7887c4SLen Brown 25030f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25040f7887c4SLen Brown if (ratio) 2505*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 25060f7887c4SLen Brown 25070f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25080f7887c4SLen Brown if (ratio) 2509*1b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 25100f7887c4SLen Brown } 25110f7887c4SLen Brown 2512*1b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void) 2513fb5d4327SDasaratharaman Chandramouli { 2514cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2515cbf97abaSHubert Chrzaniuk 2516fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2517cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2518cbf97abaSHubert Chrzaniuk int i, b_nr; 2519cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2520cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2521fb5d4327SDasaratharaman Chandramouli 2522ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2523fb5d4327SDasaratharaman Chandramouli 2524*1b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 2525fb5d4327SDasaratharaman Chandramouli 25261e3ec5cdSRandy Dunlap /* 2527fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2528cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2529cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2530fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2531fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2532fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2533fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2534fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2535fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2536fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2537fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2538fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2539fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2540fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2541fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2542fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2543fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2544fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2545fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2546fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2547fb5d4327SDasaratharaman Chandramouli */ 2548fb5d4327SDasaratharaman Chandramouli 2549cbf97abaSHubert Chrzaniuk b_nr = 0; 2550cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2551cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2552cbf97abaSHubert Chrzaniuk 2553cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2554fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2555cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2556fb5d4327SDasaratharaman Chandramouli 2557cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2558cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2559cbf97abaSHubert Chrzaniuk b_nr++; 2560fb5d4327SDasaratharaman Chandramouli } 2561cbf97abaSHubert Chrzaniuk 2562cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2563cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2564b7d8c148SLen Brown fprintf(outf, 2565710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2566cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2567fb5d4327SDasaratharaman Chandramouli } 2568fb5d4327SDasaratharaman Chandramouli 2569*1b439f01SLen Brown static void dump_nhm_cst_cfg(void) 2570fcd17211SLen Brown { 2571fcd17211SLen Brown unsigned long long msr; 2572fcd17211SLen Brown 25731df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2574fcd17211SLen Brown 25751df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2576fcd17211SLen Brown 25773e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2578fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2579fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2580fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2581fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2582*1b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 2583ac980e13SArtem Bityutskiy 2584ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2585ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2586*1b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2587ac980e13SArtem Bityutskiy } 2588ac980e13SArtem Bityutskiy 2589ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2590ac980e13SArtem Bityutskiy 2591fcd17211SLen Brown return; 2592103a8feaSLen Brown } 2593103a8feaSLen Brown 2594*1b439f01SLen Brown static void dump_config_tdp(void) 25956fb3143bSLen Brown { 25966fb3143bSLen Brown unsigned long long msr; 25976fb3143bSLen Brown 25986fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2599b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2600685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26016fb3143bSLen Brown 26026fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2603b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 26046fb3143bSLen Brown if (msr) { 2605685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2606685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2607685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2608685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 26096fb3143bSLen Brown } 2610b7d8c148SLen Brown fprintf(outf, ")\n"); 26116fb3143bSLen Brown 26126fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2613b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 26146fb3143bSLen Brown if (msr) { 2615685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2616685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2617685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2618685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 26196fb3143bSLen Brown } 2620b7d8c148SLen Brown fprintf(outf, ")\n"); 26216fb3143bSLen Brown 26226fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2623b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 26246fb3143bSLen Brown if ((msr) & 0x3) 2625b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2626b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2627b7d8c148SLen Brown fprintf(outf, ")\n"); 26286fb3143bSLen Brown 26296fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2630b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2631685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2632b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2633b7d8c148SLen Brown fprintf(outf, ")\n"); 26346fb3143bSLen Brown } 26355a63426eSLen Brown 26365a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 26375a63426eSLen Brown 26385a63426eSLen Brown void print_irtl(void) 26395a63426eSLen Brown { 26405a63426eSLen Brown unsigned long long msr; 26415a63426eSLen Brown 26425a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 26435a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 26445a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26455a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26465a63426eSLen Brown 26475a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 26485a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 26495a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26505a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26515a63426eSLen Brown 26525a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 26535a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 26545a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26555a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26565a63426eSLen Brown 26575a63426eSLen Brown if (!do_irtl_hsw) 26585a63426eSLen Brown return; 26595a63426eSLen Brown 26605a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 26615a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 26625a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26635a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26645a63426eSLen Brown 26655a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 26665a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 26675a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26685a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26695a63426eSLen Brown 26705a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 26715a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 26725a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26735a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26745a63426eSLen Brown 26755a63426eSLen Brown } 2676*1b439f01SLen Brown 267736229897SLen Brown void free_fd_percpu(void) 267836229897SLen Brown { 267936229897SLen Brown int i; 268036229897SLen Brown 268101a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 268236229897SLen Brown if (fd_percpu[i] != 0) 268336229897SLen Brown close(fd_percpu[i]); 268436229897SLen Brown } 268536229897SLen Brown 268636229897SLen Brown free(fd_percpu); 26876fb3143bSLen Brown } 26886fb3143bSLen Brown 2689c98d5d94SLen Brown void free_all_buffers(void) 2690103a8feaSLen Brown { 26910e2d8f05SLen Brown int i; 26920e2d8f05SLen Brown 2693c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2694c98d5d94SLen Brown cpu_present_set = NULL; 269536229897SLen Brown cpu_present_setsize = 0; 2696103a8feaSLen Brown 2697c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2698c98d5d94SLen Brown cpu_affinity_set = NULL; 2699c98d5d94SLen Brown cpu_affinity_setsize = 0; 2700103a8feaSLen Brown 2701c98d5d94SLen Brown free(thread_even); 2702c98d5d94SLen Brown free(core_even); 2703c98d5d94SLen Brown free(package_even); 2704103a8feaSLen Brown 2705c98d5d94SLen Brown thread_even = NULL; 2706c98d5d94SLen Brown core_even = NULL; 2707c98d5d94SLen Brown package_even = NULL; 2708103a8feaSLen Brown 2709c98d5d94SLen Brown free(thread_odd); 2710c98d5d94SLen Brown free(core_odd); 2711c98d5d94SLen Brown free(package_odd); 2712103a8feaSLen Brown 2713c98d5d94SLen Brown thread_odd = NULL; 2714c98d5d94SLen Brown core_odd = NULL; 2715c98d5d94SLen Brown package_odd = NULL; 2716103a8feaSLen Brown 2717c98d5d94SLen Brown free(output_buffer); 2718c98d5d94SLen Brown output_buffer = NULL; 2719c98d5d94SLen Brown outp = NULL; 272036229897SLen Brown 272136229897SLen Brown free_fd_percpu(); 2722562a2d37SLen Brown 2723562a2d37SLen Brown free(irq_column_2_cpu); 2724562a2d37SLen Brown free(irqs_per_cpu); 27250e2d8f05SLen Brown 27260e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 27270e2d8f05SLen Brown if (cpus[i].put_ids) 27280e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 27290e2d8f05SLen Brown } 27300e2d8f05SLen Brown free(cpus); 2731103a8feaSLen Brown } 2732103a8feaSLen Brown 273357a42a34SJosh Triplett /* 273495aebc44SJosh Triplett * Parse a file containing a single int. 27356de68fe1SLen Brown * Return 0 if file can not be opened 27366de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 273795aebc44SJosh Triplett */ 273895aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 273995aebc44SJosh Triplett { 274095aebc44SJosh Triplett va_list args; 274195aebc44SJosh Triplett char path[PATH_MAX]; 274295aebc44SJosh Triplett FILE *filep; 274395aebc44SJosh Triplett int value; 274495aebc44SJosh Triplett 274595aebc44SJosh Triplett va_start(args, fmt); 274695aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 274795aebc44SJosh Triplett va_end(args); 27486de68fe1SLen Brown filep = fopen(path, "r"); 27496de68fe1SLen Brown if (!filep) 27506de68fe1SLen Brown return 0; 2751b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2752b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 275395aebc44SJosh Triplett fclose(filep); 275495aebc44SJosh Triplett return value; 275595aebc44SJosh Triplett } 275695aebc44SJosh Triplett 275795aebc44SJosh Triplett /* 2758c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2759c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2760c98d5d94SLen Brown */ 2761c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2762c98d5d94SLen Brown { 276395aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2764103a8feaSLen Brown } 2765103a8feaSLen Brown 2766103a8feaSLen Brown int get_physical_package_id(int cpu) 2767103a8feaSLen Brown { 276895aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2769103a8feaSLen Brown } 2770103a8feaSLen Brown 27716de68fe1SLen Brown int get_die_id(int cpu) 27726de68fe1SLen Brown { 27736de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 27746de68fe1SLen Brown } 27756de68fe1SLen Brown 2776103a8feaSLen Brown int get_core_id(int cpu) 2777103a8feaSLen Brown { 277895aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2779103a8feaSLen Brown } 2780103a8feaSLen Brown 2781ef605741SPrarit Bhargava void set_node_data(void) 2782ef605741SPrarit Bhargava { 27832ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 27842ffbb224SPrarit Bhargava int cpu_count; 2785ef605741SPrarit Bhargava 27862ffbb224SPrarit Bhargava /* initialize logical_node_id */ 27872ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 27882ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2789ef605741SPrarit Bhargava 27902ffbb224SPrarit Bhargava cpu_count = 0; 27912ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 27922ffbb224SPrarit Bhargava lnode = 0; 27932ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 27942ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2795ef605741SPrarit Bhargava continue; 27962ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 27972ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 27982ffbb224SPrarit Bhargava continue; 27992ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2800ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28012ffbb224SPrarit Bhargava cpu_count++; 28022ffbb224SPrarit Bhargava /* 28032ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 28042ffbb224SPrarit Bhargava * the logical_node_id 28052ffbb224SPrarit Bhargava */ 28062ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 2807*1b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 28082ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 28092ffbb224SPrarit Bhargava cpu_count++; 2810ef605741SPrarit Bhargava } 28112ffbb224SPrarit Bhargava } 28122ffbb224SPrarit Bhargava lnode++; 28132ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 28142ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 28152ffbb224SPrarit Bhargava } 28162ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 28172ffbb224SPrarit Bhargava break; 28182ffbb224SPrarit Bhargava } 2819ef605741SPrarit Bhargava } 2820ef605741SPrarit Bhargava 2821ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2822c98d5d94SLen Brown { 2823c98d5d94SLen Brown char path[80]; 2824c98d5d94SLen Brown FILE *filep; 28250e2d8f05SLen Brown int i; 28260e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2827c98d5d94SLen Brown 28280e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 2829*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 28300e2d8f05SLen Brown filep = fopen(path, "r"); 28310e2d8f05SLen Brown if (!filep) 28320e2d8f05SLen Brown continue; 28330e2d8f05SLen Brown fclose(filep); 28340e2d8f05SLen Brown return i; 28350e2d8f05SLen Brown } 28360e2d8f05SLen Brown return -1; 2837e275b388SDasaratharaman Chandramouli } 2838c98d5d94SLen Brown 28390e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 28400e2d8f05SLen Brown { 28410e2d8f05SLen Brown char path[80], character; 28420e2d8f05SLen Brown FILE *filep; 28430e2d8f05SLen Brown unsigned long map; 28448cb48b32SPrarit Bhargava int so, shift, sib_core; 28450e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 28460e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 28470e2d8f05SLen Brown size_t size; 28488cb48b32SPrarit Bhargava int thread_id = 0; 28490e2d8f05SLen Brown 28500e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 28518cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 28528cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 28530e2d8f05SLen Brown if (!thiscpu->put_ids) 28540e2d8f05SLen Brown return -1; 28550e2d8f05SLen Brown 28560e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 28570e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 28580e2d8f05SLen Brown 2859*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 28603d7772eaSLen Brown filep = fopen(path, "r"); 28613d7772eaSLen Brown 28623d7772eaSLen Brown if (!filep) { 28633d7772eaSLen Brown warnx("%s: open failed", path); 28643d7772eaSLen Brown return -1; 28653d7772eaSLen Brown } 28660e2d8f05SLen Brown do { 28670e2d8f05SLen Brown offset -= BITMASK_SIZE; 28688173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 28698173c336SBen Hutchings err(1, "%s: failed to parse file", path); 28700e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 28710e2d8f05SLen Brown if ((map >> shift) & 0x1) { 28728cb48b32SPrarit Bhargava so = shift + offset; 28738cb48b32SPrarit Bhargava sib_core = get_core_id(so); 28748cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 28758cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 2876*1b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0)) 2877*1b439f01SLen Brown cpus[so].thread_id = thread_id++; 28788cb48b32SPrarit Bhargava } 28790e2d8f05SLen Brown } 28800e2d8f05SLen Brown } 28810e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2882c98d5d94SLen Brown fclose(filep); 28830e2d8f05SLen Brown 28840e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2885c98d5d94SLen Brown } 2886c98d5d94SLen Brown 2887c98d5d94SLen Brown /* 2888c98d5d94SLen Brown * run func(thread, core, package) in topology order 2889c98d5d94SLen Brown * skip non-present cpus 2890103a8feaSLen Brown */ 2891103a8feaSLen Brown 2892c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 2893c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2894c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2895c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2896*1b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 2897c98d5d94SLen Brown { 289840f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2899c98d5d94SLen Brown 2900c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 290140f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 2902*1b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 2903*1b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 2904c98d5d94SLen Brown struct thread_data *t, *t2; 2905c98d5d94SLen Brown struct core_data *c, *c2; 2906c98d5d94SLen Brown struct pkg_data *p, *p2; 2907c98d5d94SLen Brown 2908*1b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 2909c98d5d94SLen Brown 2910c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2911c98d5d94SLen Brown continue; 2912c98d5d94SLen Brown 2913*1b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no); 2914c98d5d94SLen Brown 2915*1b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 2916*1b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no); 2917c98d5d94SLen Brown 2918c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2919c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2920c98d5d94SLen Brown 2921c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2922c98d5d94SLen Brown if (retval) 2923c98d5d94SLen Brown return retval; 2924c98d5d94SLen Brown } 2925c98d5d94SLen Brown } 2926c98d5d94SLen Brown } 292740f5cfe7SPrarit Bhargava } 2928c98d5d94SLen Brown return 0; 2929c98d5d94SLen Brown } 2930c98d5d94SLen Brown 2931c98d5d94SLen Brown /* 2932c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2933c98d5d94SLen Brown * return max_cpu number 2934c98d5d94SLen Brown */ 2935c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int)) 2936103a8feaSLen Brown { 2937103a8feaSLen Brown FILE *fp; 2938c98d5d94SLen Brown int cpu_num; 2939103a8feaSLen Brown int retval; 2940103a8feaSLen Brown 294157a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2942103a8feaSLen Brown 2943103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2944b2c95d90SJosh Triplett if (retval != 0) 2945b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2946103a8feaSLen Brown 2947c98d5d94SLen Brown while (1) { 2948c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2949103a8feaSLen Brown if (retval != 1) 2950103a8feaSLen Brown break; 2951103a8feaSLen Brown 2952c98d5d94SLen Brown retval = func(cpu_num); 2953c98d5d94SLen Brown if (retval) { 2954c98d5d94SLen Brown fclose(fp); 2955c98d5d94SLen Brown return (retval); 2956c98d5d94SLen Brown } 2957103a8feaSLen Brown } 2958103a8feaSLen Brown fclose(fp); 2959c98d5d94SLen Brown return 0; 2960103a8feaSLen Brown } 2961103a8feaSLen Brown 2962103a8feaSLen Brown void re_initialize(void) 2963103a8feaSLen Brown { 2964c98d5d94SLen Brown free_all_buffers(); 2965c98d5d94SLen Brown setup_all_buffers(); 29663d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2967103a8feaSLen Brown } 2968103a8feaSLen Brown 2969843c5791SPrarit Bhargava void set_max_cpu_num(void) 2970843c5791SPrarit Bhargava { 2971843c5791SPrarit Bhargava FILE *filep; 29728201a028SPrarit Bhargava int base_cpu; 2973843c5791SPrarit Bhargava unsigned long dummy; 29748201a028SPrarit Bhargava char pathname[64]; 2975843c5791SPrarit Bhargava 29768201a028SPrarit Bhargava base_cpu = sched_getcpu(); 29778201a028SPrarit Bhargava if (base_cpu < 0) 29788201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 2979*1b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu); 29808201a028SPrarit Bhargava 29818201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 2982843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2983843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 29840e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2985843c5791SPrarit Bhargava fclose(filep); 2986843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2987843c5791SPrarit Bhargava } 2988c98d5d94SLen Brown 2989103a8feaSLen Brown /* 2990c98d5d94SLen Brown * count_cpus() 2991c98d5d94SLen Brown * remember the last one seen, it will be the max 2992103a8feaSLen Brown */ 2993c98d5d94SLen Brown int count_cpus(int cpu) 2994103a8feaSLen Brown { 2995843c5791SPrarit Bhargava topo.num_cpus++; 2996c98d5d94SLen Brown return 0; 2997103a8feaSLen Brown } 2998*1b439f01SLen Brown 2999c98d5d94SLen Brown int mark_cpu_present(int cpu) 3000c98d5d94SLen Brown { 3001c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 300215aaa346SLen Brown return 0; 3003103a8feaSLen Brown } 3004103a8feaSLen Brown 30058cb48b32SPrarit Bhargava int init_thread_id(int cpu) 30068cb48b32SPrarit Bhargava { 30078cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 30088cb48b32SPrarit Bhargava return 0; 30098cb48b32SPrarit Bhargava } 30108cb48b32SPrarit Bhargava 3011562a2d37SLen Brown /* 3012562a2d37SLen Brown * snapshot_proc_interrupts() 3013562a2d37SLen Brown * 3014562a2d37SLen Brown * read and record summary of /proc/interrupts 3015562a2d37SLen Brown * 3016562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3017562a2d37SLen Brown */ 3018562a2d37SLen Brown int snapshot_proc_interrupts(void) 3019562a2d37SLen Brown { 3020562a2d37SLen Brown static FILE *fp; 3021562a2d37SLen Brown int column, retval; 3022562a2d37SLen Brown 3023562a2d37SLen Brown if (fp == NULL) 3024562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3025562a2d37SLen Brown else 3026562a2d37SLen Brown rewind(fp); 3027562a2d37SLen Brown 3028562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3029562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3030562a2d37SLen Brown int cpu_number; 3031562a2d37SLen Brown 3032562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3033562a2d37SLen Brown if (retval != 1) 3034562a2d37SLen Brown break; 3035562a2d37SLen Brown 3036562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3037562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3038562a2d37SLen Brown return 1; 3039562a2d37SLen Brown } 3040562a2d37SLen Brown 3041562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3042562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3043562a2d37SLen Brown } 3044562a2d37SLen Brown 3045562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3046562a2d37SLen Brown while (1) { 3047562a2d37SLen Brown int column; 3048562a2d37SLen Brown char buf[64]; 3049562a2d37SLen Brown 3050562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3051562a2d37SLen Brown if (retval != 1) 3052562a2d37SLen Brown break; 3053562a2d37SLen Brown 3054562a2d37SLen Brown /* read the count per cpu */ 3055562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3056562a2d37SLen Brown 3057562a2d37SLen Brown int cpu_number, irq_count; 3058562a2d37SLen Brown 3059562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3060562a2d37SLen Brown if (retval != 1) 3061562a2d37SLen Brown break; 3062562a2d37SLen Brown 3063562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3064562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3065562a2d37SLen Brown 3066562a2d37SLen Brown } 3067562a2d37SLen Brown 3068*1b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */ 3069562a2d37SLen Brown 3070562a2d37SLen Brown } 3071562a2d37SLen Brown return 0; 3072562a2d37SLen Brown } 3073*1b439f01SLen Brown 307427d47356SLen Brown /* 3075fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3076fdf676e5SLen Brown * 3077fdf676e5SLen Brown * record snapshot of 3078fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3079fdf676e5SLen Brown * 3080fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3081fdf676e5SLen Brown */ 3082fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3083fdf676e5SLen Brown { 3084fdf676e5SLen Brown FILE *fp; 3085fdf676e5SLen Brown int retval; 3086fdf676e5SLen Brown 3087fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3088fdf676e5SLen Brown 3089fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3090fdf676e5SLen Brown if (retval != 1) 3091fdf676e5SLen Brown err(1, "GFX rc6"); 3092fdf676e5SLen Brown 3093fdf676e5SLen Brown fclose(fp); 3094fdf676e5SLen Brown 3095fdf676e5SLen Brown return 0; 3096fdf676e5SLen Brown } 3097*1b439f01SLen Brown 3098fdf676e5SLen Brown /* 309927d47356SLen Brown * snapshot_gfx_mhz() 310027d47356SLen Brown * 310127d47356SLen Brown * record snapshot of 310227d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 310327d47356SLen Brown * 310427d47356SLen Brown * return 1 if config change requires a restart, else return 0 310527d47356SLen Brown */ 310627d47356SLen Brown int snapshot_gfx_mhz(void) 310727d47356SLen Brown { 310827d47356SLen Brown static FILE *fp; 310927d47356SLen Brown int retval; 311027d47356SLen Brown 311127d47356SLen Brown if (fp == NULL) 311227d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 311322048c54SLen Brown else { 311427d47356SLen Brown rewind(fp); 311522048c54SLen Brown fflush(fp); 311622048c54SLen Brown } 311727d47356SLen Brown 311827d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 311927d47356SLen Brown if (retval != 1) 312027d47356SLen Brown err(1, "GFX MHz"); 312127d47356SLen Brown 312227d47356SLen Brown return 0; 312327d47356SLen Brown } 3124562a2d37SLen Brown 3125562a2d37SLen Brown /* 3126b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3127b4b91569SRafael Antognolli * 3128b4b91569SRafael Antognolli * record snapshot of 3129b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3130b4b91569SRafael Antognolli * 3131b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3132b4b91569SRafael Antognolli */ 3133b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3134b4b91569SRafael Antognolli { 3135b4b91569SRafael Antognolli static FILE *fp; 3136b4b91569SRafael Antognolli int retval; 3137b4b91569SRafael Antognolli 3138b4b91569SRafael Antognolli if (fp == NULL) 3139b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3140b4b91569SRafael Antognolli else { 3141b4b91569SRafael Antognolli rewind(fp); 3142b4b91569SRafael Antognolli fflush(fp); 3143b4b91569SRafael Antognolli } 3144b4b91569SRafael Antognolli 3145b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3146b4b91569SRafael Antognolli if (retval != 1) 3147b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3148b4b91569SRafael Antognolli 3149b4b91569SRafael Antognolli return 0; 3150b4b91569SRafael Antognolli } 3151b4b91569SRafael Antognolli 3152b4b91569SRafael Antognolli /* 3153be0e54c4SLen Brown * snapshot_cpu_lpi() 3154be0e54c4SLen Brown * 3155be0e54c4SLen Brown * record snapshot of 3156be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3157be0e54c4SLen Brown */ 3158be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3159be0e54c4SLen Brown { 3160be0e54c4SLen Brown FILE *fp; 3161be0e54c4SLen Brown int retval; 3162be0e54c4SLen Brown 3163be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3164be0e54c4SLen Brown 3165be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 31665ea7647bSPrarit Bhargava if (retval != 1) { 31675ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 31685ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3169605736c6SGustavo A. R. Silva fclose(fp); 31705ea7647bSPrarit Bhargava return -1; 31715ea7647bSPrarit Bhargava } 3172be0e54c4SLen Brown 3173be0e54c4SLen Brown fclose(fp); 3174be0e54c4SLen Brown 3175be0e54c4SLen Brown return 0; 3176be0e54c4SLen Brown } 3177*1b439f01SLen Brown 3178be0e54c4SLen Brown /* 3179be0e54c4SLen Brown * snapshot_sys_lpi() 3180be0e54c4SLen Brown * 31811f81c5efSLen Brown * record snapshot of sys_lpi_file 3182be0e54c4SLen Brown */ 3183be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3184be0e54c4SLen Brown { 3185be0e54c4SLen Brown FILE *fp; 3186be0e54c4SLen Brown int retval; 3187be0e54c4SLen Brown 31881f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3189be0e54c4SLen Brown 3190be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 31915ea7647bSPrarit Bhargava if (retval != 1) { 31925ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 31935ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 319415423b95SColin Ian King fclose(fp); 31955ea7647bSPrarit Bhargava return -1; 31965ea7647bSPrarit Bhargava } 3197be0e54c4SLen Brown fclose(fp); 3198be0e54c4SLen Brown 3199be0e54c4SLen Brown return 0; 3200be0e54c4SLen Brown } 3201*1b439f01SLen Brown 3202be0e54c4SLen Brown /* 3203562a2d37SLen Brown * snapshot /proc and /sys files 3204562a2d37SLen Brown * 3205562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3206562a2d37SLen Brown */ 3207562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3208562a2d37SLen Brown { 3209218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3210562a2d37SLen Brown if (snapshot_proc_interrupts()) 3211562a2d37SLen Brown return 1; 3212562a2d37SLen Brown 3213812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3214fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3215fdf676e5SLen Brown 3216812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 321727d47356SLen Brown snapshot_gfx_mhz(); 321827d47356SLen Brown 3219b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3220b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3221b4b91569SRafael Antognolli 3222be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3223be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3224be0e54c4SLen Brown 3225be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3226be0e54c4SLen Brown snapshot_sys_lpi_us(); 3227be0e54c4SLen Brown 3228562a2d37SLen Brown return 0; 3229562a2d37SLen Brown } 3230562a2d37SLen Brown 32318aa2ed0bSLen Brown int exit_requested; 32328aa2ed0bSLen Brown 32338aa2ed0bSLen Brown static void signal_handler(int signal) 32348aa2ed0bSLen Brown { 32358aa2ed0bSLen Brown switch (signal) { 32368aa2ed0bSLen Brown case SIGINT: 32378aa2ed0bSLen Brown exit_requested = 1; 32388aa2ed0bSLen Brown if (debug) 32398aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 32408aa2ed0bSLen Brown break; 324107211960SLen Brown case SIGUSR1: 324207211960SLen Brown if (debug > 1) 324307211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 324407211960SLen Brown break; 32458aa2ed0bSLen Brown } 32468aa2ed0bSLen Brown } 32478aa2ed0bSLen Brown 32488aa2ed0bSLen Brown void setup_signal_handler(void) 32498aa2ed0bSLen Brown { 32508aa2ed0bSLen Brown struct sigaction sa; 32518aa2ed0bSLen Brown 32528aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 32538aa2ed0bSLen Brown 32548aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 32558aa2ed0bSLen Brown 32568aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 32578aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 325807211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 325907211960SLen Brown err(1, "sigaction SIGUSR1"); 32608aa2ed0bSLen Brown } 3261b9ad8ee0SLen Brown 326247936f94SArtem Bityutskiy void do_sleep(void) 3263b9ad8ee0SLen Brown { 3264c026c236SArtem Bityutskiy struct timeval tout; 3265c026c236SArtem Bityutskiy struct timespec rest; 3266b9ad8ee0SLen Brown fd_set readfds; 3267b9ad8ee0SLen Brown int retval; 3268b9ad8ee0SLen Brown 3269b9ad8ee0SLen Brown FD_ZERO(&readfds); 3270b9ad8ee0SLen Brown FD_SET(0, &readfds); 3271b9ad8ee0SLen Brown 3272c026c236SArtem Bityutskiy if (ignore_stdin) { 327347936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 327447936f94SArtem Bityutskiy return; 327547936f94SArtem Bityutskiy } 3276b9ad8ee0SLen Brown 3277c026c236SArtem Bityutskiy tout = interval_tv; 3278c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3279b9ad8ee0SLen Brown 3280b9ad8ee0SLen Brown if (retval == 1) { 3281b9ad8ee0SLen Brown switch (getc(stdin)) { 3282b9ad8ee0SLen Brown case 'q': 3283b9ad8ee0SLen Brown exit_requested = 1; 3284b9ad8ee0SLen Brown break; 3285c026c236SArtem Bityutskiy case EOF: 3286c026c236SArtem Bityutskiy /* 3287c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3288c026c236SArtem Bityutskiy * won't be any further input. 3289c026c236SArtem Bityutskiy */ 3290c026c236SArtem Bityutskiy ignore_stdin = 1; 3291c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3292c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3293c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3294c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3295b9ad8ee0SLen Brown } 3296b9ad8ee0SLen Brown } 3297b9ad8ee0SLen Brown } 329847936f94SArtem Bityutskiy 329987e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 330087e15da9SChen Yu { 330187e15da9SChen Yu int ret, idx; 330287e15da9SChen Yu unsigned long long msr_cur, msr_last; 330387e15da9SChen Yu 330487e15da9SChen Yu if (!per_cpu_msr_sum) 330587e15da9SChen Yu return 1; 330687e15da9SChen Yu 330787e15da9SChen Yu idx = offset_to_idx(offset); 330887e15da9SChen Yu if (idx < 0) 330987e15da9SChen Yu return idx; 331087e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 331187e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 331287e15da9SChen Yu if (ret) 331387e15da9SChen Yu return ret; 331487e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 331587e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 331687e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 331787e15da9SChen Yu 331887e15da9SChen Yu return 0; 331987e15da9SChen Yu } 332087e15da9SChen Yu 332187e15da9SChen Yu timer_t timerid; 332287e15da9SChen Yu 332387e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 332487e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 332587e15da9SChen Yu { 332687e15da9SChen Yu int i, ret; 332787e15da9SChen Yu int cpu = t->cpu_id; 332887e15da9SChen Yu 332987e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 333087e15da9SChen Yu unsigned long long msr_cur, msr_last; 333113a779deSCalvin Walton off_t offset; 333287e15da9SChen Yu 333387e15da9SChen Yu if (!idx_valid(i)) 333487e15da9SChen Yu continue; 333587e15da9SChen Yu offset = idx_to_offset(i); 333687e15da9SChen Yu if (offset < 0) 333787e15da9SChen Yu continue; 333887e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 333987e15da9SChen Yu if (ret) { 3340*1b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset); 334187e15da9SChen Yu continue; 334287e15da9SChen Yu } 334387e15da9SChen Yu 334487e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 334587e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 334687e15da9SChen Yu 334787e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 334887e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 334987e15da9SChen Yu } 335087e15da9SChen Yu return 0; 335187e15da9SChen Yu } 335287e15da9SChen Yu 3353*1b439f01SLen Brown static void msr_record_handler(union sigval v) 335487e15da9SChen Yu { 335587e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 335687e15da9SChen Yu } 335787e15da9SChen Yu 335887e15da9SChen Yu void msr_sum_record(void) 335987e15da9SChen Yu { 336087e15da9SChen Yu struct itimerspec its; 336187e15da9SChen Yu struct sigevent sev; 336287e15da9SChen Yu 336387e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 336487e15da9SChen Yu if (!per_cpu_msr_sum) { 336587e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 336687e15da9SChen Yu return; 336787e15da9SChen Yu } 336887e15da9SChen Yu /* 336987e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 337087e15da9SChen Yu */ 337187e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 337287e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 337387e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 337487e15da9SChen Yu 337587e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 337687e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 337787e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 337887e15da9SChen Yu goto release_msr; 337987e15da9SChen Yu } 338087e15da9SChen Yu 338187e15da9SChen Yu its.it_value.tv_sec = 0; 338287e15da9SChen Yu its.it_value.tv_nsec = 1; 338387e15da9SChen Yu /* 338487e15da9SChen Yu * A wraparound time has been calculated early. 338587e15da9SChen Yu * Some sources state that the peak power for a 338687e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 338787e15da9SChen Yu * use 2 * TDP for safety. 338887e15da9SChen Yu */ 338987e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 339087e15da9SChen Yu its.it_interval.tv_nsec = 0; 339187e15da9SChen Yu 339287e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 339387e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 339487e15da9SChen Yu goto release_timer; 339587e15da9SChen Yu } 339687e15da9SChen Yu return; 339787e15da9SChen Yu 339887e15da9SChen Yu release_timer: 339987e15da9SChen Yu timer_delete(timerid); 340087e15da9SChen Yu release_msr: 340187e15da9SChen Yu free(per_cpu_msr_sum); 340287e15da9SChen Yu } 34034c2122d4SLen Brown 3404103a8feaSLen Brown void turbostat_loop() 3405103a8feaSLen Brown { 3406c98d5d94SLen Brown int retval; 3407e52966c0SLen Brown int restarted = 0; 3408023fe0acSChen Yu int done_iters = 0; 3409c98d5d94SLen Brown 34108aa2ed0bSLen Brown setup_signal_handler(); 34118aa2ed0bSLen Brown 3412103a8feaSLen Brown restart: 3413e52966c0SLen Brown restarted++; 3414e52966c0SLen Brown 3415562a2d37SLen Brown snapshot_proc_sysfs_files(); 3416c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 34174c2122d4SLen Brown first_counter_read = 0; 3418d91bb17cSLen Brown if (retval < -1) { 3419d91bb17cSLen Brown exit(retval); 3420d91bb17cSLen Brown } else if (retval == -1) { 34213d7772eaSLen Brown if (restarted > 10) { 3422e52966c0SLen Brown exit(retval); 3423e52966c0SLen Brown } 3424c98d5d94SLen Brown re_initialize(); 3425c98d5d94SLen Brown goto restart; 3426c98d5d94SLen Brown } 3427e52966c0SLen Brown restarted = 0; 3428023fe0acSChen Yu done_iters = 0; 3429103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3430103a8feaSLen Brown 3431103a8feaSLen Brown while (1) { 3432c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3433103a8feaSLen Brown re_initialize(); 3434103a8feaSLen Brown goto restart; 3435103a8feaSLen Brown } 3436b9ad8ee0SLen Brown do_sleep(); 3437562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3438562a2d37SLen Brown goto restart; 3439c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3440d91bb17cSLen Brown if (retval < -1) { 3441d91bb17cSLen Brown exit(retval); 3442d91bb17cSLen Brown } else if (retval == -1) { 344315aaa346SLen Brown re_initialize(); 344415aaa346SLen Brown goto restart; 344515aaa346SLen Brown } 3446103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3447103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3448ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3449ba3dec99SLen Brown re_initialize(); 3450ba3dec99SLen Brown goto restart; 3451ba3dec99SLen Brown } 3452c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3453c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3454b7d8c148SLen Brown flush_output_stdout(); 34558aa2ed0bSLen Brown if (exit_requested) 34568aa2ed0bSLen Brown break; 3457023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3458023fe0acSChen Yu break; 3459b9ad8ee0SLen Brown do_sleep(); 3460562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3461562a2d37SLen Brown goto restart; 3462c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3463d91bb17cSLen Brown if (retval < -1) { 3464d91bb17cSLen Brown exit(retval); 3465d91bb17cSLen Brown } else if (retval == -1) { 3466103a8feaSLen Brown re_initialize(); 3467103a8feaSLen Brown goto restart; 3468103a8feaSLen Brown } 3469103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3470103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3471ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3472ba3dec99SLen Brown re_initialize(); 3473ba3dec99SLen Brown goto restart; 3474ba3dec99SLen Brown } 3475c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3476c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3477b7d8c148SLen Brown flush_output_stdout(); 34788aa2ed0bSLen Brown if (exit_requested) 34798aa2ed0bSLen Brown break; 3480023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3481023fe0acSChen Yu break; 3482103a8feaSLen Brown } 3483103a8feaSLen Brown } 3484103a8feaSLen Brown 3485103a8feaSLen Brown void check_dev_msr() 3486103a8feaSLen Brown { 3487103a8feaSLen Brown struct stat sb; 34887ce7d5deSPrarit Bhargava char pathname[32]; 3489103a8feaSLen Brown 34907ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 34917ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3492a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3493d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3494103a8feaSLen Brown } 3495103a8feaSLen Brown 3496fcaa681cSLen Brown /* 3497fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3498fcaa681cSLen Brown * return 0 on success 3499fcaa681cSLen Brown * return 1 on fail 3500fcaa681cSLen Brown */ 3501fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3502103a8feaSLen Brown { 3503fcaa681cSLen Brown cap_t caps; 3504fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3505fcaa681cSLen Brown 3506fcaa681cSLen Brown caps = cap_get_proc(); 3507fcaa681cSLen Brown if (caps == NULL) 3508fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3509fcaa681cSLen Brown 3510fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3511fcaa681cSLen Brown err(-6, "cap_get\n"); 3512fcaa681cSLen Brown 3513fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 3514*1b439f01SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3515fcaa681cSLen Brown return 1; 3516fcaa681cSLen Brown } 3517fcaa681cSLen Brown 3518fcaa681cSLen Brown if (cap_free(caps) == -1) 3519fcaa681cSLen Brown err(-6, "cap_free\n"); 3520fcaa681cSLen Brown 3521fcaa681cSLen Brown return 0; 3522fcaa681cSLen Brown } 3523*1b439f01SLen Brown 3524fcaa681cSLen Brown void check_permissions(void) 3525fcaa681cSLen Brown { 352698481e79SLen Brown int do_exit = 0; 35277ce7d5deSPrarit Bhargava char pathname[32]; 352898481e79SLen Brown 352998481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3530fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 353198481e79SLen Brown 353298481e79SLen Brown /* test file permissions */ 35337ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 35347ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 353598481e79SLen Brown do_exit++; 353698481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 353798481e79SLen Brown } 353898481e79SLen Brown 353998481e79SLen Brown /* if all else fails, thell them to be root */ 354098481e79SLen Brown if (do_exit) 3541b2c95d90SJosh Triplett if (getuid() != 0) 3542d7899447SLen Brown warnx("... or simply run as root"); 354398481e79SLen Brown 354498481e79SLen Brown if (do_exit) 354598481e79SLen Brown exit(-6); 3546103a8feaSLen Brown } 3547103a8feaSLen Brown 3548d7899447SLen Brown /* 3549d7899447SLen Brown * NHM adds support for additional MSRs: 3550d7899447SLen Brown * 3551d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3552d7899447SLen Brown * 3553ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 35541df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3555d7899447SLen Brown * 3556cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3557cf4cbe53SLen Brown * 3558d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3559d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3560d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3561d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3562d7899447SLen Brown * 3563ee7e38e3SLen Brown * Side effect: 35641df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 356533148d67SLen Brown * sets has_misc_feature_control 3566d7899447SLen Brown */ 3567ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3568103a8feaSLen Brown { 3569ee7e38e3SLen Brown unsigned long long msr; 357021ed5574SLen Brown unsigned int base_ratio; 3571ee7e38e3SLen Brown int *pkg_cstate_limits; 3572ee7e38e3SLen Brown 3573103a8feaSLen Brown if (!genuine_intel) 3574103a8feaSLen Brown return 0; 3575103a8feaSLen Brown 3576103a8feaSLen Brown if (family != 6) 3577103a8feaSLen Brown return 0; 3578103a8feaSLen Brown 357921ed5574SLen Brown bclk = discover_bclk(family, model); 358021ed5574SLen Brown 3581103a8feaSLen Brown switch (model) { 3582869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3583869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3584ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3585ee7e38e3SLen Brown break; 3586869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3587869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3588869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3589869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3590ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 359133148d67SLen Brown has_misc_feature_control = 1; 3592ee7e38e3SLen Brown break; 3593c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 35945e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3595869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 359677e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3597c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 35985e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3599869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3600af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3601af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3602ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 360333148d67SLen Brown has_misc_feature_control = 1; 3604ee7e38e3SLen Brown break; 3605d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3606d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 360733148d67SLen Brown has_misc_feature_control = 1; 3608d8ebb442SLen Brown break; 360925368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 361025368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits; 361125368d7cSChen Yu has_misc_feature_control = 1; 361225368d7cSChen Yu break; 3613f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3614cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 36155ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3616ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3617ee7e38e3SLen Brown break; 3618869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3619ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3620cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3621ee7e38e3SLen Brown break; 3622869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3623ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3624ee7e38e3SLen Brown break; 3625869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3626f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 36275ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3628f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 362920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3630445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3631e4085d54SLen Brown break; 3632103a8feaSLen Brown default: 3633103a8feaSLen Brown return 0; 3634103a8feaSLen Brown } 36351df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3636e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3637ee7e38e3SLen Brown 3638ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 363921ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 364021ed5574SLen Brown 364121ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 364221ed5574SLen Brown has_base_hz = 1; 3643ee7e38e3SLen Brown return 1; 3644103a8feaSLen Brown } 3645*1b439f01SLen Brown 36460f7887c4SLen Brown /* 3647495c7654SLen Brown * SLV client has support for unique MSRs: 36480f7887c4SLen Brown * 36490f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 36500f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 36510f7887c4SLen Brown */ 36520f7887c4SLen Brown 36530f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 36540f7887c4SLen Brown { 36550f7887c4SLen Brown if (!genuine_intel) 36560f7887c4SLen Brown return 0; 36570f7887c4SLen Brown 36580f7887c4SLen Brown switch (model) { 3659f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3660f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3661f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 36620f7887c4SLen Brown return 1; 36630f7887c4SLen Brown } 36640f7887c4SLen Brown return 0; 36650f7887c4SLen Brown } 3666*1b439f01SLen Brown 36677170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 36687170a374SLen Brown { 36697170a374SLen Brown 36707170a374SLen Brown if (!genuine_intel) 36717170a374SLen Brown return 0; 36727170a374SLen Brown 36737170a374SLen Brown switch (model) { 36745ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 36757170a374SLen Brown return 1; 36767170a374SLen Brown } 36777170a374SLen Brown return 0; 36787170a374SLen Brown } 3679*1b439f01SLen Brown 3680ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3681ade0ebacSLen Brown { 3682ade0ebacSLen Brown 3683ade0ebacSLen Brown if (!genuine_intel) 3684ade0ebacSLen Brown return 0; 3685ade0ebacSLen Brown 3686ade0ebacSLen Brown switch (model) { 3687ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3688ade0ebacSLen Brown return 1; 3689ade0ebacSLen Brown } 3690ade0ebacSLen Brown return 0; 3691ade0ebacSLen Brown } 3692*1b439f01SLen Brown 369334c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 369434c76197SLen Brown { 369534c76197SLen Brown 369634c76197SLen Brown if (!genuine_intel) 369734c76197SLen Brown return 0; 369834c76197SLen Brown 369934c76197SLen Brown switch (model) { 370034c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 370134c76197SLen Brown return 1; 370234c76197SLen Brown } 370334c76197SLen Brown return 0; 370434c76197SLen Brown } 370525368d7cSChen Yu 370625368d7cSChen Yu int is_icx(unsigned int family, unsigned int model) 370725368d7cSChen Yu { 370825368d7cSChen Yu 370925368d7cSChen Yu if (!genuine_intel) 371025368d7cSChen Yu return 0; 371125368d7cSChen Yu 371225368d7cSChen Yu switch (model) { 371325368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 371425368d7cSChen Yu return 1; 371525368d7cSChen Yu } 371625368d7cSChen Yu return 0; 371725368d7cSChen Yu } 371825368d7cSChen Yu 3719f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3720f6708400SChen Yu { 3721f6708400SChen Yu if (!genuine_intel) 3722f6708400SChen Yu return 0; 3723f6708400SChen Yu 3724f6708400SChen Yu switch (model) { 3725f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3726f6708400SChen Yu return 1; 3727f6708400SChen Yu } 3728f6708400SChen Yu return 0; 3729f6708400SChen Yu } 3730*1b439f01SLen Brown 373120de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 373220de0dabSAntti Laakso { 373320de0dabSAntti Laakso if (!genuine_intel) 373420de0dabSAntti Laakso return 0; 373520de0dabSAntti Laakso 373620de0dabSAntti Laakso switch (model) { 373720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 373820de0dabSAntti Laakso return 1; 373920de0dabSAntti Laakso } 374020de0dabSAntti Laakso return 0; 374120de0dabSAntti Laakso } 37420f7887c4SLen Brown 374331e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3744d7899447SLen Brown { 37450f7887c4SLen Brown if (has_slv_msrs(family, model)) 37460f7887c4SLen Brown return 0; 37470f7887c4SLen Brown 3748d7899447SLen Brown switch (model) { 3749d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3750869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3751869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3752d7899447SLen Brown return 0; 3753d7899447SLen Brown default: 3754d7899447SLen Brown return 1; 3755d7899447SLen Brown } 3756d7899447SLen Brown } 3757*1b439f01SLen Brown 37580f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 37590f7887c4SLen Brown { 37600f7887c4SLen Brown if (has_slv_msrs(family, model)) 37610f7887c4SLen Brown return 1; 37620f7887c4SLen Brown 37630f7887c4SLen Brown return 0; 37640f7887c4SLen Brown } 3765*1b439f01SLen Brown 37666574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 37676574a5d5SLen Brown { 37686574a5d5SLen Brown if (!genuine_intel) 37696574a5d5SLen Brown return 0; 37706574a5d5SLen Brown 37716574a5d5SLen Brown if (family != 6) 37726574a5d5SLen Brown return 0; 37736574a5d5SLen Brown 37746574a5d5SLen Brown switch (model) { 3775869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3776869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 37776574a5d5SLen Brown return 1; 37786574a5d5SLen Brown default: 37796574a5d5SLen Brown return 0; 37806574a5d5SLen Brown } 37816574a5d5SLen Brown } 3782*1b439f01SLen Brown 3783fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3784fcd17211SLen Brown { 3785fcd17211SLen Brown if (!genuine_intel) 3786fcd17211SLen Brown return 0; 3787fcd17211SLen Brown 3788fcd17211SLen Brown if (family != 6) 3789fcd17211SLen Brown return 0; 3790fcd17211SLen Brown 3791fcd17211SLen Brown switch (model) { 3792869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3793fcd17211SLen Brown return 1; 3794fcd17211SLen Brown default: 3795fcd17211SLen Brown return 0; 3796fcd17211SLen Brown } 3797fcd17211SLen Brown } 3798fcd17211SLen Brown 3799fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3800fb5d4327SDasaratharaman Chandramouli { 3801fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3802fb5d4327SDasaratharaman Chandramouli return 0; 3803fb5d4327SDasaratharaman Chandramouli 3804fb5d4327SDasaratharaman Chandramouli if (family != 6) 3805fb5d4327SDasaratharaman Chandramouli return 0; 3806fb5d4327SDasaratharaman Chandramouli 3807fb5d4327SDasaratharaman Chandramouli switch (model) { 3808869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3809fb5d4327SDasaratharaman Chandramouli return 1; 3810fb5d4327SDasaratharaman Chandramouli default: 3811fb5d4327SDasaratharaman Chandramouli return 0; 3812fb5d4327SDasaratharaman Chandramouli } 3813fb5d4327SDasaratharaman Chandramouli } 3814*1b439f01SLen Brown 381531e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 381631e07522SLen Brown { 381731e07522SLen Brown if (!genuine_intel) 381831e07522SLen Brown return 0; 381931e07522SLen Brown 382031e07522SLen Brown if (family != 6) 382131e07522SLen Brown return 0; 382231e07522SLen Brown 382331e07522SLen Brown switch (model) { 382431e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 382531e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 382625368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 382731e07522SLen Brown return 1; 382831e07522SLen Brown default: 382931e07522SLen Brown return 0; 383031e07522SLen Brown } 383131e07522SLen Brown } 3832*1b439f01SLen Brown 38336fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 38346fb3143bSLen Brown { 38356fb3143bSLen Brown if (!genuine_intel) 38366fb3143bSLen Brown return 0; 38376fb3143bSLen Brown 38386fb3143bSLen Brown if (family != 6) 38396fb3143bSLen Brown return 0; 38406fb3143bSLen Brown 38416fb3143bSLen Brown switch (model) { 3842869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3843c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3844869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 384577e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 38465e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3847c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 38485e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3849869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3850af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3851af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3852869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 385325368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 38546fb3143bSLen Brown 3855869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 38566fb3143bSLen Brown return 1; 38576fb3143bSLen Brown default: 38586fb3143bSLen Brown return 0; 38596fb3143bSLen Brown } 38606fb3143bSLen Brown } 38616fb3143bSLen Brown 38620b9a0b9bSZhang Rui /* 38630b9a0b9bSZhang Rui * tcc_offset_bits: 38640b9a0b9bSZhang Rui * 0: Tcc Offset not supported (Default) 38650b9a0b9bSZhang Rui * 6: Bit 29:24 of MSR_PLATFORM_INFO 38660b9a0b9bSZhang Rui * 4: Bit 27:24 of MSR_PLATFORM_INFO 38670b9a0b9bSZhang Rui */ 38680b9a0b9bSZhang Rui void check_tcc_offset(int model) 38690b9a0b9bSZhang Rui { 38700b9a0b9bSZhang Rui unsigned long long msr; 38710b9a0b9bSZhang Rui 38720b9a0b9bSZhang Rui if (!genuine_intel) 38730b9a0b9bSZhang Rui return; 38740b9a0b9bSZhang Rui 38750b9a0b9bSZhang Rui switch (model) { 38760b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE_L: 38770b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE: 38780b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE_L: 38790b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE: 38800b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE_L: 38810b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE: 38820b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE_L: 38830b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE: 38840b9a0b9bSZhang Rui case INTEL_FAM6_COMETLAKE: 38850b9a0b9bSZhang Rui if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) { 38860b9a0b9bSZhang Rui msr = (msr >> 30) & 1; 38870b9a0b9bSZhang Rui if (msr) 38880b9a0b9bSZhang Rui tcc_offset_bits = 6; 38890b9a0b9bSZhang Rui } 38900b9a0b9bSZhang Rui return; 38910b9a0b9bSZhang Rui default: 38920b9a0b9bSZhang Rui return; 38930b9a0b9bSZhang Rui } 38940b9a0b9bSZhang Rui } 38950b9a0b9bSZhang Rui 3896*1b439f01SLen Brown static void remove_underbar(char *s) 3897fecb3bc8SDavid Arcari { 3898fecb3bc8SDavid Arcari char *to = s; 3899fecb3bc8SDavid Arcari 3900fecb3bc8SDavid Arcari while (*s) { 3901fecb3bc8SDavid Arcari if (*s != '_') 3902fecb3bc8SDavid Arcari *to++ = *s; 3903fecb3bc8SDavid Arcari s++; 3904fecb3bc8SDavid Arcari } 3905fecb3bc8SDavid Arcari 3906fecb3bc8SDavid Arcari *to = 0; 3907fecb3bc8SDavid Arcari } 3908fecb3bc8SDavid Arcari 3909*1b439f01SLen Brown static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3910fcd17211SLen Brown { 3911fcd17211SLen Brown if (!do_nhm_platform_info) 3912fcd17211SLen Brown return; 3913fcd17211SLen Brown 3914fcd17211SLen Brown dump_nhm_platform_info(); 3915fcd17211SLen Brown 3916fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3917fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3918fcd17211SLen Brown 3919fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3920fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3921fcd17211SLen Brown 392231e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 392331e07522SLen Brown dump_turbo_ratio_limits(family, model); 3924fcd17211SLen Brown 39250f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 39260f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 39270f7887c4SLen Brown 3928fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3929fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3930fb5d4327SDasaratharaman Chandramouli 39316fb3143bSLen Brown if (has_config_tdp(family, model)) 39326fb3143bSLen Brown dump_config_tdp(); 39336fb3143bSLen Brown 3934fcd17211SLen Brown dump_nhm_cst_cfg(); 3935fcd17211SLen Brown } 3936fcd17211SLen Brown 3937abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 3938abdcbdb2SLen Brown { 3939abdcbdb2SLen Brown FILE *input; 3940abdcbdb2SLen Brown char cpuidle_buf[64]; 3941abdcbdb2SLen Brown 3942abdcbdb2SLen Brown input = fopen(path, "r"); 3943abdcbdb2SLen Brown if (input == NULL) { 3944abdcbdb2SLen Brown if (debug) 3945abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 3946abdcbdb2SLen Brown return; 3947abdcbdb2SLen Brown } 3948abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 3949abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 3950abdcbdb2SLen Brown fclose(input); 3951abdcbdb2SLen Brown 3952abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 3953abdcbdb2SLen Brown } 3954*1b439f01SLen Brown 3955*1b439f01SLen Brown static void dump_sysfs_cstate_config(void) 395641618e63SLen Brown { 395741618e63SLen Brown char path[64]; 395841618e63SLen Brown char name_buf[16]; 395941618e63SLen Brown char desc[64]; 396041618e63SLen Brown FILE *input; 396141618e63SLen Brown int state; 396241618e63SLen Brown char *sp; 396341618e63SLen Brown 3964abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 3965abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 3966abdcbdb2SLen Brown return; 3967abdcbdb2SLen Brown } 3968abdcbdb2SLen Brown 3969abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 3970abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 3971abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 3972abdcbdb2SLen Brown 397341618e63SLen Brown for (state = 0; state < 10; ++state) { 397441618e63SLen Brown 3975*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 397641618e63SLen Brown input = fopen(path, "r"); 397741618e63SLen Brown if (input == NULL) 397841618e63SLen Brown continue; 39798173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 39808173c336SBen Hutchings err(1, "%s: failed to read file", path); 398141618e63SLen Brown 398241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 398341618e63SLen Brown sp = strchr(name_buf, '-'); 398441618e63SLen Brown if (!sp) 398541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 398641618e63SLen Brown *sp = '\0'; 398741618e63SLen Brown fclose(input); 398841618e63SLen Brown 3989fecb3bc8SDavid Arcari remove_underbar(name_buf); 3990fecb3bc8SDavid Arcari 3991*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state); 399241618e63SLen Brown input = fopen(path, "r"); 399341618e63SLen Brown if (input == NULL) 399441618e63SLen Brown continue; 39958173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 39968173c336SBen Hutchings err(1, "%s: failed to read file", path); 399741618e63SLen Brown 399841618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 399941618e63SLen Brown fclose(input); 400041618e63SLen Brown } 400141618e63SLen Brown } 4002*1b439f01SLen Brown 4003*1b439f01SLen Brown static void dump_sysfs_pstate_config(void) 40047293fccdSLen Brown { 40057293fccdSLen Brown char path[64]; 40067293fccdSLen Brown char driver_buf[64]; 40077293fccdSLen Brown char governor_buf[64]; 40087293fccdSLen Brown FILE *input; 40097293fccdSLen Brown int turbo; 40107293fccdSLen Brown 4011*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu); 40127293fccdSLen Brown input = fopen(path, "r"); 40137293fccdSLen Brown if (input == NULL) { 40140a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40157293fccdSLen Brown return; 40167293fccdSLen Brown } 40178173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 40188173c336SBen Hutchings err(1, "%s: failed to read file", path); 40197293fccdSLen Brown fclose(input); 40207293fccdSLen Brown 4021*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu); 40227293fccdSLen Brown input = fopen(path, "r"); 40237293fccdSLen Brown if (input == NULL) { 40240a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40257293fccdSLen Brown return; 40267293fccdSLen Brown } 40278173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 40288173c336SBen Hutchings err(1, "%s: failed to read file", path); 40297293fccdSLen Brown fclose(input); 40307293fccdSLen Brown 40317293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 40327293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 40337293fccdSLen Brown 40347293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 40357293fccdSLen Brown input = fopen(path, "r"); 40367293fccdSLen Brown if (input != NULL) { 40378173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 40388173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 40397293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 40407293fccdSLen Brown fclose(input); 40417293fccdSLen Brown } 40427293fccdSLen Brown 40437293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 40447293fccdSLen Brown input = fopen(path, "r"); 40457293fccdSLen Brown if (input != NULL) { 40468173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 40478173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 40487293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 40497293fccdSLen Brown fclose(input); 40507293fccdSLen Brown } 40517293fccdSLen Brown } 405241618e63SLen Brown 4053889facbeSLen Brown /* 4054889facbeSLen Brown * print_epb() 4055889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 4056889facbeSLen Brown */ 4057889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4058889facbeSLen Brown { 4059889facbeSLen Brown char *epb_string; 40606d6501d9SBorislav Petkov int cpu, epb; 4061889facbeSLen Brown 4062889facbeSLen Brown if (!has_epb) 4063889facbeSLen Brown return 0; 4064889facbeSLen Brown 4065889facbeSLen Brown cpu = t->cpu_id; 4066889facbeSLen Brown 4067889facbeSLen Brown /* EPB is per-package */ 4068889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4069889facbeSLen Brown return 0; 4070889facbeSLen Brown 4071889facbeSLen Brown if (cpu_migrate(cpu)) { 40723d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 4073889facbeSLen Brown return -1; 4074889facbeSLen Brown } 4075889facbeSLen Brown 40766d6501d9SBorislav Petkov epb = get_epb(cpu); 40776d6501d9SBorislav Petkov if (epb < 0) 4078889facbeSLen Brown return 0; 4079889facbeSLen Brown 40806d6501d9SBorislav Petkov switch (epb) { 4081889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 4082889facbeSLen Brown epb_string = "performance"; 4083889facbeSLen Brown break; 4084889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 4085889facbeSLen Brown epb_string = "balanced"; 4086889facbeSLen Brown break; 4087889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 4088889facbeSLen Brown epb_string = "powersave"; 4089889facbeSLen Brown break; 4090889facbeSLen Brown default: 4091889facbeSLen Brown epb_string = "custom"; 4092889facbeSLen Brown break; 4093889facbeSLen Brown } 40946d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 4095889facbeSLen Brown 4096889facbeSLen Brown return 0; 4097889facbeSLen Brown } 4098*1b439f01SLen Brown 40997f5c258eSLen Brown /* 41007f5c258eSLen Brown * print_hwp() 41017f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 41027f5c258eSLen Brown */ 41037f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 41047f5c258eSLen Brown { 41057f5c258eSLen Brown unsigned long long msr; 41067f5c258eSLen Brown int cpu; 41077f5c258eSLen Brown 41087f5c258eSLen Brown if (!has_hwp) 41097f5c258eSLen Brown return 0; 41107f5c258eSLen Brown 41117f5c258eSLen Brown cpu = t->cpu_id; 41127f5c258eSLen Brown 41137f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 41147f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 41157f5c258eSLen Brown return 0; 41167f5c258eSLen Brown 41177f5c258eSLen Brown if (cpu_migrate(cpu)) { 41183d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 41197f5c258eSLen Brown return -1; 41207f5c258eSLen Brown } 41217f5c258eSLen Brown 41227f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 41237f5c258eSLen Brown return 0; 41247f5c258eSLen Brown 4125*1b439f01SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 41267f5c258eSLen Brown 41277f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 41287f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 41297f5c258eSLen Brown return 0; 41307f5c258eSLen Brown 41317f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 41327f5c258eSLen Brown return 0; 41337f5c258eSLen Brown 4134b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 41356dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 41367f5c258eSLen Brown cpu, msr, 41377f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 41387f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 4139*1b439f01SLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr)); 41407f5c258eSLen Brown 41417f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 41427f5c258eSLen Brown return 0; 41437f5c258eSLen Brown 4144b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 41456dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 41467f5c258eSLen Brown cpu, msr, 41477f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 41487f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 41497f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 41507f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 4151*1b439f01SLen Brown (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1)); 41527f5c258eSLen Brown 41537f5c258eSLen Brown if (has_hwp_pkg) { 41547f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 41557f5c258eSLen Brown return 0; 41567f5c258eSLen Brown 4157b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 41586dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 41597f5c258eSLen Brown cpu, msr, 41607f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 41617f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 41627f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 4163*1b439f01SLen Brown (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3)); 41647f5c258eSLen Brown } 41657f5c258eSLen Brown if (has_hwp_notify) { 41667f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 41677f5c258eSLen Brown return 0; 41687f5c258eSLen Brown 4169b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 41707f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 4171*1b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis"); 41727f5c258eSLen Brown } 41737f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 41747f5c258eSLen Brown return 0; 41757f5c258eSLen Brown 4176b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 41777f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 4178*1b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-"); 4179889facbeSLen Brown 4180889facbeSLen Brown return 0; 4181889facbeSLen Brown } 4182889facbeSLen Brown 41833a9a941dSLen Brown /* 41843a9a941dSLen Brown * print_perf_limit() 41853a9a941dSLen Brown */ 41863a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 41873a9a941dSLen Brown { 41883a9a941dSLen Brown unsigned long long msr; 41893a9a941dSLen Brown int cpu; 41903a9a941dSLen Brown 41913a9a941dSLen Brown cpu = t->cpu_id; 41923a9a941dSLen Brown 41933a9a941dSLen Brown /* per-package */ 41943a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 41953a9a941dSLen Brown return 0; 41963a9a941dSLen Brown 41973a9a941dSLen Brown if (cpu_migrate(cpu)) { 41983d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 41993a9a941dSLen Brown return -1; 42003a9a941dSLen Brown } 42013a9a941dSLen Brown 42023a9a941dSLen Brown if (do_core_perf_limit_reasons) { 42033a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4204b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4205b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4206e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 42073a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4208e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4209e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4210e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4211e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4212e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4213e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4214e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4215e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4216e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4217e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 4218*1b439f01SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : ""); 4219b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4220e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 42213a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4222e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4223e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4224e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4225e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4226e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4227e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4228e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4229e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4230e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4231e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 4232*1b439f01SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : ""); 42333a9a941dSLen Brown 42343a9a941dSLen Brown } 42353a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 42363a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4237b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4238b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 42393a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 42403a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 42413a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 42423a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 42433a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 42443a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 4245*1b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4246b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 42473a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 42483a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 42493a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 42503a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 42513a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 42523a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 4253*1b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 42543a9a941dSLen Brown } 42553a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 42563a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4257b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4258b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 42593a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 42603a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 42613a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 42623a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 4263*1b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4264b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 42653a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 42663a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 42673a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 42683a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 4269*1b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 42703a9a941dSLen Brown } 42713a9a941dSLen Brown return 0; 42723a9a941dSLen Brown } 42733a9a941dSLen Brown 4274889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4275889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4276889facbeSLen Brown 42779392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4278144b44b1SLen Brown { 4279144b44b1SLen Brown unsigned long long msr; 4280144b44b1SLen Brown 4281144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 42827ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4283144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4284144b44b1SLen Brown 4285144b44b1SLen Brown switch (model) { 4286f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 42875ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4288144b44b1SLen Brown return 30.0; 4289144b44b1SLen Brown default: 4290144b44b1SLen Brown return 135.0; 4291144b44b1SLen Brown } 4292144b44b1SLen Brown } 4293144b44b1SLen Brown 42949392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 42959392bd98SCalvin Walton { 429633eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 429733eb8225SKim Phillips return 280.0; 42989392bd98SCalvin Walton } 42999392bd98SCalvin Walton 430040ee8e3bSAndrey Semin /* 430140ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 430240ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 430340ee8e3bSAndrey Semin */ 4304*1b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units) 430540ee8e3bSAndrey Semin { 430640ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 430740ee8e3bSAndrey Semin 430840ee8e3bSAndrey Semin switch (model) { 4309869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4310869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4311abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4312869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 431340ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 431440ee8e3bSAndrey Semin default: 431540ee8e3bSAndrey Semin return (rapl_energy_units); 431640ee8e3bSAndrey Semin } 431740ee8e3bSAndrey Semin } 431840ee8e3bSAndrey Semin 43199392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4320889facbeSLen Brown { 4321889facbeSLen Brown unsigned long long msr; 4322144b44b1SLen Brown unsigned int time_unit; 4323889facbeSLen Brown double tdp; 4324889facbeSLen Brown 4325889facbeSLen Brown if (family != 6) 4326889facbeSLen Brown return; 4327889facbeSLen Brown 4328889facbeSLen Brown switch (model) { 4329869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4330869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4331c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 433277e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 43335e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4334c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 43355e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4336144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4337812db3f7SLen Brown if (rapl_joules) { 4338812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4339812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4340812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4341812db3f7SLen Brown } else { 4342812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4343812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4344812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4345812db3f7SLen Brown } 4346889facbeSLen Brown break; 4347869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4348f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4349e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4350812db3f7SLen Brown if (rapl_joules) 4351812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4352812db3f7SLen Brown else 4353812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4354e4085d54SLen Brown break; 4355f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4356*1b439f01SLen Brown do_rapl = 4357*1b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 4358*1b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4359f6708400SChen Yu if (rapl_joules) { 4360f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4361f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4362f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4363f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4364f6708400SChen Yu } else { 4365f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4366f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4367f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4368f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4369f6708400SChen Yu } 4370f6708400SChen Yu break; 437120de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 437220de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 437320de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 437420de0dabSAntti Laakso if (rapl_joules) 437520de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 437620de0dabSAntti Laakso else 437720de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 437820de0dabSAntti Laakso break; 4379af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4380af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4381*1b439f01SLen Brown do_rapl = 4382*1b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 4383*1b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4384812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4385812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4386812db3f7SLen Brown if (rapl_joules) { 4387812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4388812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4389812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 439081824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4391812db3f7SLen Brown } else { 4392812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4393812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4394812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 439581824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4396812db3f7SLen Brown } 43970b2bb692SLen Brown break; 4398869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4399869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4400869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 440125368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4402869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4403*1b439f01SLen Brown do_rapl = 4404*1b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 4405*1b439f01SLen Brown RAPL_PKG_POWER_INFO; 4406812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4407812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4408812db3f7SLen Brown if (rapl_joules) { 4409812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4410812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4411812db3f7SLen Brown } else { 4412812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4413812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4414812db3f7SLen Brown } 4415e6f9bb3cSLen Brown break; 4416869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4417869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 4418*1b439f01SLen Brown do_rapl = 4419*1b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | 4420*1b439f01SLen Brown RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4421812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4422812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4423812db3f7SLen Brown if (rapl_joules) { 4424812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4425812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4426812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4427812db3f7SLen Brown } else { 4428812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4429812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4430812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4431812db3f7SLen Brown } 4432144b44b1SLen Brown break; 4433f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 44345ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4435144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4436812db3f7SLen Brown if (rapl_joules) { 4437812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4438812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4439812db3f7SLen Brown } else { 4440812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4441812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4442812db3f7SLen Brown } 4443889facbeSLen Brown break; 44445ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4445*1b439f01SLen Brown do_rapl = 4446*1b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 4447*1b439f01SLen Brown RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4448812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4449812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4450812db3f7SLen Brown if (rapl_joules) { 4451812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4452812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4453812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4454812db3f7SLen Brown } else { 4455812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4456812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4457812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4458812db3f7SLen Brown } 44590f644909SJacob Pan break; 4460889facbeSLen Brown default: 4461889facbeSLen Brown return; 4462889facbeSLen Brown } 4463889facbeSLen Brown 4464889facbeSLen Brown /* units on package 0, verify later other packages match */ 44657ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4466889facbeSLen Brown return; 4467889facbeSLen Brown 4468889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4469f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4470144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4471144b44b1SLen Brown else 4472889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4473889facbeSLen Brown 447440ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 447540ee8e3bSAndrey Semin 4476144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4477144b44b1SLen Brown if (time_unit == 0) 4478144b44b1SLen Brown time_unit = 0xA; 4479889facbeSLen Brown 4480144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4481144b44b1SLen Brown 44829392bd98SCalvin Walton tdp = get_tdp_intel(model); 4483889facbeSLen Brown 4484889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 448596e47158SLen Brown if (!quiet) 4486b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 44879392bd98SCalvin Walton } 4488889facbeSLen Brown 44899392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 44909392bd98SCalvin Walton { 44919392bd98SCalvin Walton unsigned long long msr; 44929392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 44939392bd98SCalvin Walton unsigned int has_rapl = 0; 44949392bd98SCalvin Walton double tdp; 44959392bd98SCalvin Walton 44969392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 44979392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 449833eb8225SKim Phillips /* RAPL (Fam 17h+) */ 44999392bd98SCalvin Walton has_rapl = edx & (1 << 14); 45009392bd98SCalvin Walton } 45019392bd98SCalvin Walton 450233eb8225SKim Phillips if (!has_rapl || family < 0x17) 4503889facbeSLen Brown return; 45049392bd98SCalvin Walton 45059392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 45063316f99aSCalvin Walton if (rapl_joules) { 45073316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 45089392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 45093316f99aSCalvin Walton } else { 45103316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 45119392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 45123316f99aSCalvin Walton } 45139392bd98SCalvin Walton 45149392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 45159392bd98SCalvin Walton return; 45169392bd98SCalvin Walton 45179392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 45189392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 45199392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 45209392bd98SCalvin Walton 45219cfa8e04SPu Wen tdp = get_tdp_amd(family); 45229392bd98SCalvin Walton 45239392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 45249392bd98SCalvin Walton if (!quiet) 45259392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 45269392bd98SCalvin Walton } 45279392bd98SCalvin Walton 45289392bd98SCalvin Walton /* 45299392bd98SCalvin Walton * rapl_probe() 45309392bd98SCalvin Walton * 45319392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 45329392bd98SCalvin Walton */ 45339392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 45349392bd98SCalvin Walton { 45359392bd98SCalvin Walton if (genuine_intel) 45369392bd98SCalvin Walton rapl_probe_intel(family, model); 4537c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 45389392bd98SCalvin Walton rapl_probe_amd(family, model); 4539889facbeSLen Brown } 4540889facbeSLen Brown 45411b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 45423a9a941dSLen Brown { 45433a9a941dSLen Brown if (!genuine_intel) 45443a9a941dSLen Brown return; 45453a9a941dSLen Brown 45463a9a941dSLen Brown if (family != 6) 45473a9a941dSLen Brown return; 45483a9a941dSLen Brown 45493a9a941dSLen Brown switch (model) { 4550c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 455177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 45525e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 45533a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4554869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 45553a9a941dSLen Brown do_core_perf_limit_reasons = 1; 45563a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 45573a9a941dSLen Brown default: 45583a9a941dSLen Brown return; 45593a9a941dSLen Brown } 45603a9a941dSLen Brown } 45613a9a941dSLen Brown 4562ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4563ac980e13SArtem Bityutskiy { 4564*1b439f01SLen Brown if (is_skx(family, model) || is_bdx(family, model) || is_icx(family, model)) 4565ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4566ac980e13SArtem Bityutskiy } 4567ac980e13SArtem Bityutskiy 4568aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model) 4569aeb01e6dSChen Yu { 4570aeb01e6dSChen Yu if (is_icx(family, model)) 4571aeb01e6dSChen Yu dis_cstate_prewake = 1; 4572aeb01e6dSChen Yu } 4573aeb01e6dSChen Yu 4574889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4575889facbeSLen Brown { 4576889facbeSLen Brown unsigned long long msr; 4577f4896fa5SLen Brown unsigned int dts, dts2; 4578889facbeSLen Brown int cpu; 4579889facbeSLen Brown 4580889facbeSLen Brown if (!(do_dts || do_ptm)) 4581889facbeSLen Brown return 0; 4582889facbeSLen Brown 4583889facbeSLen Brown cpu = t->cpu_id; 4584889facbeSLen Brown 4585889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4586889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4587889facbeSLen Brown return 0; 4588889facbeSLen Brown 4589889facbeSLen Brown if (cpu_migrate(cpu)) { 45903d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4591889facbeSLen Brown return -1; 4592889facbeSLen Brown } 4593889facbeSLen Brown 4594889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4595889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4596889facbeSLen Brown return 0; 4597889facbeSLen Brown 4598889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4599*1b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts); 4600889facbeSLen Brown 4601889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4602889facbeSLen Brown return 0; 4603889facbeSLen Brown 4604889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4605889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4606b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 460755279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4608889facbeSLen Brown } 4609889facbeSLen Brown 4610f4896fa5SLen Brown if (do_dts && debug) { 4611889facbeSLen Brown unsigned int resolution; 4612889facbeSLen Brown 4613889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4614889facbeSLen Brown return 0; 4615889facbeSLen Brown 4616889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4617889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4618b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 461955279aefSZhang Rui cpu, msr, tj_max - dts, resolution); 4620889facbeSLen Brown 4621889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4622889facbeSLen Brown return 0; 4623889facbeSLen Brown 4624889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4625889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4626b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 462755279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4628889facbeSLen Brown } 4629889facbeSLen Brown 4630889facbeSLen Brown return 0; 4631889facbeSLen Brown } 4632889facbeSLen Brown 4633889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4634889facbeSLen Brown { 4635b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4636889facbeSLen Brown cpu, label, 4637889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4638889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4639889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4640889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4641889facbeSLen Brown 4642889facbeSLen Brown return; 4643889facbeSLen Brown } 4644889facbeSLen Brown 4645889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4646889facbeSLen Brown { 4647889facbeSLen Brown unsigned long long msr; 46489392bd98SCalvin Walton const char *msr_name; 4649889facbeSLen Brown int cpu; 4650889facbeSLen Brown 4651889facbeSLen Brown if (!do_rapl) 4652889facbeSLen Brown return 0; 4653889facbeSLen Brown 4654889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4655889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4656889facbeSLen Brown return 0; 4657889facbeSLen Brown 4658889facbeSLen Brown cpu = t->cpu_id; 4659889facbeSLen Brown if (cpu_migrate(cpu)) { 46603d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4661889facbeSLen Brown return -1; 4662889facbeSLen Brown } 4663889facbeSLen Brown 46649392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 46659392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 46669392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 46679392bd98SCalvin Walton return -1; 46689392bd98SCalvin Walton } else { 46699392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4670889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4671889facbeSLen Brown return -1; 46729392bd98SCalvin Walton } 4673889facbeSLen Brown 46749392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4675144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 467696e47158SLen Brown 4677144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4678144b44b1SLen Brown 4679889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4680889facbeSLen Brown return -5; 4681889facbeSLen Brown 4682b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4683889facbeSLen Brown cpu, msr, 4684889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4685889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4686889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4687889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4688889facbeSLen Brown 4689144b44b1SLen Brown } 4690144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4691144b44b1SLen Brown 4692889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4693889facbeSLen Brown return -9; 4694889facbeSLen Brown 4695b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4696889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4697889facbeSLen Brown 4698889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4699b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4700889facbeSLen Brown cpu, 4701889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4702889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4703889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4704889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4705889facbeSLen Brown } 4706889facbeSLen Brown 47070b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4708889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4709889facbeSLen Brown return -6; 4710889facbeSLen Brown 4711b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4712889facbeSLen Brown cpu, msr, 4713889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4714889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4715889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4716889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 47170b2bb692SLen Brown } 47180b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4719889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4720889facbeSLen Brown return -9; 4721b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4722889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4723889facbeSLen Brown 4724889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4725889facbeSLen Brown } 4726144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4727889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4728889facbeSLen Brown return -7; 4729889facbeSLen Brown 4730b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4731144b44b1SLen Brown } 47329148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4733889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4734889facbeSLen Brown return -9; 4735b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4736889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4737889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4738889facbeSLen Brown } 4739889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4740889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4741889facbeSLen Brown return -8; 4742889facbeSLen Brown 4743b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4744889facbeSLen Brown 4745889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4746889facbeSLen Brown return -9; 4747b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4748889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4749889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4750889facbeSLen Brown } 4751889facbeSLen Brown return 0; 4752889facbeSLen Brown } 4753889facbeSLen Brown 4754d7899447SLen Brown /* 4755d7899447SLen Brown * SNB adds support for additional MSRs: 4756d7899447SLen Brown * 4757d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4758d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4759d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4760d7899447SLen Brown */ 4761103a8feaSLen Brown 4762d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4763103a8feaSLen Brown { 4764103a8feaSLen Brown if (!genuine_intel) 4765103a8feaSLen Brown return 0; 4766103a8feaSLen Brown 4767103a8feaSLen Brown switch (model) { 4768869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4769869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4770869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4771869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4772c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4773869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 477477e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 47755e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4776c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 47775e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4778869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4779af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4780af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4781869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 478225368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4783869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4784f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 47855ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4786f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 478720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4788103a8feaSLen Brown return 1; 4789103a8feaSLen Brown } 4790103a8feaSLen Brown return 0; 4791103a8feaSLen Brown } 4792103a8feaSLen Brown 4793d7899447SLen Brown /* 4794570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4795d7899447SLen Brown * 4796d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4797d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4798d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 47995a63426eSLen Brown * 48005a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 48015a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 48025a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 48035a63426eSLen Brown * 4804d7899447SLen Brown */ 4805570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4806ca58710fSKristen Carlson Accardi { 4807ca58710fSKristen Carlson Accardi if (!genuine_intel) 4808ca58710fSKristen Carlson Accardi return 0; 4809ca58710fSKristen Carlson Accardi 4810ca58710fSKristen Carlson Accardi switch (model) { 481177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4812c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4813af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4814af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4815869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4816f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4817f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4818ca58710fSKristen Carlson Accardi return 1; 4819ca58710fSKristen Carlson Accardi } 4820ca58710fSKristen Carlson Accardi return 0; 4821ca58710fSKristen Carlson Accardi } 4822ca58710fSKristen Carlson Accardi 48230b2bb692SLen Brown /* 48240b2bb692SLen Brown * SKL adds support for additional MSRS: 48250b2bb692SLen Brown * 48260b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 48270b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 48280b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 48290b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 48300b2bb692SLen Brown */ 48310b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 48320b2bb692SLen Brown { 48330b2bb692SLen Brown if (!genuine_intel) 48340b2bb692SLen Brown return 0; 48350b2bb692SLen Brown 48360b2bb692SLen Brown switch (model) { 4837af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4838af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 48390b2bb692SLen Brown return 1; 48400b2bb692SLen Brown } 48410b2bb692SLen Brown return 0; 48420b2bb692SLen Brown } 48430b2bb692SLen Brown 4844144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4845144b44b1SLen Brown { 4846144b44b1SLen Brown if (!genuine_intel) 4847144b44b1SLen Brown return 0; 4848144b44b1SLen Brown switch (model) { 4849f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 48505ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4851144b44b1SLen Brown return 1; 4852144b44b1SLen Brown } 4853144b44b1SLen Brown return 0; 4854144b44b1SLen Brown } 4855144b44b1SLen Brown 4856fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4857fb5d4327SDasaratharaman Chandramouli { 4858fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4859fb5d4327SDasaratharaman Chandramouli return 0; 4860fb5d4327SDasaratharaman Chandramouli switch (model) { 4861869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4862fb5d4327SDasaratharaman Chandramouli return 1; 4863fb5d4327SDasaratharaman Chandramouli } 4864fb5d4327SDasaratharaman Chandramouli return 0; 4865fb5d4327SDasaratharaman Chandramouli } 4866fb5d4327SDasaratharaman Chandramouli 4867997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4868997e5395SSrinivas Pandruvada { 4869997e5395SSrinivas Pandruvada if (!genuine_intel) 4870997e5395SSrinivas Pandruvada return 0; 4871997e5395SSrinivas Pandruvada 4872997e5395SSrinivas Pandruvada switch (model) { 4873af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4874997e5395SSrinivas Pandruvada return 1; 4875997e5395SSrinivas Pandruvada } 4876997e5395SSrinivas Pandruvada 4877997e5395SSrinivas Pandruvada return 0; 4878997e5395SSrinivas Pandruvada } 4879997e5395SSrinivas Pandruvada 4880b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4881b2b34dfeSHubert Chrzaniuk { 4882b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4883b2b34dfeSHubert Chrzaniuk return 1024; 4884b2b34dfeSHubert Chrzaniuk return 1; 4885b2b34dfeSHubert Chrzaniuk } 4886b2b34dfeSHubert Chrzaniuk 4887144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4888144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 }; 4889144b44b1SLen Brown 4890144b44b1SLen Brown double slm_bclk(void) 4891144b44b1SLen Brown { 4892144b44b1SLen Brown unsigned long long msr = 3; 4893144b44b1SLen Brown unsigned int i; 4894144b44b1SLen Brown double freq; 4895144b44b1SLen Brown 48967ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4897b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4898144b44b1SLen Brown 4899144b44b1SLen Brown i = msr & 0xf; 4900144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4901b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 49020a91e551SColin Ian King i = 3; 4903144b44b1SLen Brown } 4904144b44b1SLen Brown freq = slm_freq_table[i]; 4905144b44b1SLen Brown 490696e47158SLen Brown if (!quiet) 4907b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4908144b44b1SLen Brown 4909144b44b1SLen Brown return freq; 4910144b44b1SLen Brown } 4911144b44b1SLen Brown 4912103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4913103a8feaSLen Brown { 4914121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4915103a8feaSLen Brown return 100.00; 4916144b44b1SLen Brown else if (is_slm(family, model)) 4917144b44b1SLen Brown return slm_bclk(); 4918103a8feaSLen Brown else 4919103a8feaSLen Brown return 133.33; 4920103a8feaSLen Brown } 4921103a8feaSLen Brown 49227ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p) 49237ab5ff49SZhang Rui { 49247ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx; 49257ab5ff49SZhang Rui 49267ab5ff49SZhang Rui if (!genuine_intel) 49277ab5ff49SZhang Rui return 0; 49287ab5ff49SZhang Rui 49297ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) { 49307ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 49317ab5ff49SZhang Rui return -1; 49327ab5ff49SZhang Rui } 49337ab5ff49SZhang Rui 49347ab5ff49SZhang Rui if (max_level < 0x1a) 49357ab5ff49SZhang Rui return 0; 49367ab5ff49SZhang Rui 49377ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx); 49387ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF; 49397ab5ff49SZhang Rui if (eax == 0x20) 49407ab5ff49SZhang Rui t->is_atom = true; 49417ab5ff49SZhang Rui return 0; 49427ab5ff49SZhang Rui } 49437ab5ff49SZhang Rui 4944889facbeSLen Brown /* 4945889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4946889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4947889facbeSLen Brown * This is usually equal to tjMax. 4948889facbeSLen Brown * 4949889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4950889facbeSLen Brown * but also allow cmdline over-ride with -T. 4951889facbeSLen Brown * 4952889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4953889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4954889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4955889facbeSLen Brown */ 49566ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 49576ff7cb37SLen Brown { 4958b2b94be7SLen Brown unsigned long long msr; 495955279aefSZhang Rui unsigned int tcc_default, tcc_offset; 4960b2b94be7SLen Brown int cpu; 4961b2b94be7SLen Brown 496255279aefSZhang Rui /* tj_max is used only for dts or ptm */ 4963889facbeSLen Brown if (!(do_dts || do_ptm)) 4964889facbeSLen Brown return 0; 4965889facbeSLen Brown 4966889facbeSLen Brown /* this is a per-package concept */ 4967889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4968889facbeSLen Brown return 0; 4969889facbeSLen Brown 4970b2b94be7SLen Brown cpu = t->cpu_id; 4971b2b94be7SLen Brown if (cpu_migrate(cpu)) { 4972b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4973b2b94be7SLen Brown return -1; 4974b2b94be7SLen Brown } 4975b2b94be7SLen Brown 497655279aefSZhang Rui if (tj_max_override != 0) { 497755279aefSZhang Rui tj_max = tj_max_override; 4978*1b439f01SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max); 4979889facbeSLen Brown return 0; 4980889facbeSLen Brown } 4981889facbeSLen Brown 4982b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4983b2b94be7SLen Brown if (!do_nhm_platform_info) 4984b2b94be7SLen Brown goto guess; 4985b2b94be7SLen Brown 4986b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4987b2b94be7SLen Brown goto guess; 4988b2b94be7SLen Brown 498955279aefSZhang Rui tcc_default = (msr >> 16) & 0xFF; 4990b2b94be7SLen Brown 49910b9a0b9bSZhang Rui if (!quiet) { 49920b9a0b9bSZhang Rui switch (tcc_offset_bits) { 49930b9a0b9bSZhang Rui case 4: 49940b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0xF; 49950b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 499655279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 49970b9a0b9bSZhang Rui break; 49980b9a0b9bSZhang Rui case 6: 49990b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0x3F; 50000b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 500155279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 50020b9a0b9bSZhang Rui break; 50030b9a0b9bSZhang Rui default: 5004*1b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default); 50050b9a0b9bSZhang Rui break; 50060b9a0b9bSZhang Rui } 50070b9a0b9bSZhang Rui } 5008b2b94be7SLen Brown 500955279aefSZhang Rui if (!tcc_default) 5010b2b94be7SLen Brown goto guess; 5011b2b94be7SLen Brown 501255279aefSZhang Rui tj_max = tcc_default; 5013b2b94be7SLen Brown 5014889facbeSLen Brown return 0; 5015889facbeSLen Brown 5016b2b94be7SLen Brown guess: 501755279aefSZhang Rui tj_max = TJMAX_DEFAULT; 5018*1b439f01SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max); 5019889facbeSLen Brown 5020889facbeSLen Brown return 0; 5021889facbeSLen Brown } 502269807a63SLen Brown 5023aa8d8cc7SLen Brown void decode_feature_control_msr(void) 5024aa8d8cc7SLen Brown { 5025aa8d8cc7SLen Brown unsigned long long msr; 5026aa8d8cc7SLen Brown 5027f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 5028aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 5029*1b439f01SLen Brown base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : ""); 5030aa8d8cc7SLen Brown } 5031aa8d8cc7SLen Brown 503269807a63SLen Brown void decode_misc_enable_msr(void) 503369807a63SLen Brown { 503469807a63SLen Brown unsigned long long msr; 503569807a63SLen Brown 5036f26b1519SLen Brown if (!genuine_intel) 5037f26b1519SLen Brown return; 5038f26b1519SLen Brown 503969807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 5040e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 504169807a63SLen Brown base_cpu, msr, 5042e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 5043e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 5044fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 5045e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 5046e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 504769807a63SLen Brown } 504869807a63SLen Brown 504933148d67SLen Brown void decode_misc_feature_control(void) 505033148d67SLen Brown { 505133148d67SLen Brown unsigned long long msr; 505233148d67SLen Brown 505333148d67SLen Brown if (!has_misc_feature_control) 505433148d67SLen Brown return; 505533148d67SLen Brown 505633148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 5057*1b439f01SLen Brown fprintf(outf, 5058*1b439f01SLen Brown "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 5059*1b439f01SLen Brown base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "", 5060*1b439f01SLen Brown msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : ""); 506133148d67SLen Brown } 5062*1b439f01SLen Brown 5063f0057310SLen Brown /* 5064f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 5065f0057310SLen Brown * 5066f0057310SLen Brown * Decode the bits according to the Nehalem documentation 5067f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 5068f0057310SLen Brown * bit[1] less so... 5069f0057310SLen Brown */ 5070f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 5071f0057310SLen Brown { 5072f0057310SLen Brown unsigned long long msr; 5073f0057310SLen Brown 5074f0057310SLen Brown if (!do_nhm_platform_info) 5075f0057310SLen Brown return; 5076f0057310SLen Brown 5077cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 5078cf4cbe53SLen Brown return; 5079cf4cbe53SLen Brown 5080f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 5081ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 5082f0057310SLen Brown base_cpu, msr, 5083*1b439f01SLen Brown msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS"); 5084f0057310SLen Brown } 5085*1b439f01SLen Brown 508671616c8eSLen Brown /* 508771616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 508871616c8eSLen Brown * 508971616c8eSLen Brown * This MSRs are present on Silvermont processors, 509071616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 509171616c8eSLen Brown */ 509271616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 509371616c8eSLen Brown { 509471616c8eSLen Brown unsigned long long msr; 509571616c8eSLen Brown 509671616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 509771616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 509871616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 509971616c8eSLen Brown 510071616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 510171616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 510271616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 510371616c8eSLen Brown } 51047f5c258eSLen Brown 5105f5a4c76aSLen Brown /* 5106f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 5107f5a4c76aSLen Brown */ 5108f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 5109f5a4c76aSLen Brown { 5110f5a4c76aSLen Brown 5111f5a4c76aSLen Brown switch (model) { 5112f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 5113f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 5114f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 5115f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 5116f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 5117f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 5118f5a4c76aSLen Brown 5119f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 5120f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 5121f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 5122f5a4c76aSLen Brown 5123f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 5124f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 5125f5a4c76aSLen Brown 5126f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 51275ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 5128f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 5129f5a4c76aSLen Brown 5130af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 5131c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 5132af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 5133c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 5134081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 5135081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 5136af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 5137937807d3SLen Brown 5138af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 5139d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 51404bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 51414bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 5142e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 5143e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 5144e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 51455683460bSChen Yu case INTEL_FAM6_ALDERLAKE_L: 5146af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 5147b62b3184SZhang Rui 5148d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 5149d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 5150d7814c30SChen Yu 51516c5c6560SChen Yu case INTEL_FAM6_ICELAKE_D: 5152e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 515325368d7cSChen Yu return INTEL_FAM6_ICELAKE_X; 5154f5a4c76aSLen Brown } 5155f5a4c76aSLen Brown return model; 5156f5a4c76aSLen Brown } 5157d76bb7a0SLen Brown 5158d76bb7a0SLen Brown void print_dev_latency(void) 5159d76bb7a0SLen Brown { 5160d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5161d76bb7a0SLen Brown int fd; 5162d76bb7a0SLen Brown int value; 5163d76bb7a0SLen Brown int retval; 5164d76bb7a0SLen Brown 5165d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5166d76bb7a0SLen Brown if (fd < 0) { 5167d76bb7a0SLen Brown warn("fopen %s\n", path); 5168d76bb7a0SLen Brown return; 5169d76bb7a0SLen Brown } 5170d76bb7a0SLen Brown 5171d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5172d76bb7a0SLen Brown if (retval != sizeof(int)) { 5173d76bb7a0SLen Brown warn("read %s\n", path); 5174d76bb7a0SLen Brown close(fd); 5175d76bb7a0SLen Brown return; 5176d76bb7a0SLen Brown } 5177*1b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 5178d76bb7a0SLen Brown 5179d76bb7a0SLen Brown close(fd); 5180d76bb7a0SLen Brown } 5181d76bb7a0SLen Brown 51822af4f9b8SLen Brown /* 51832af4f9b8SLen Brown * Linux-perf manages the the HW instructions-retired counter 51842af4f9b8SLen Brown * by enabling when requested, and hiding rollover 51852af4f9b8SLen Brown */ 51862af4f9b8SLen Brown void linux_perf_init(void) 51872af4f9b8SLen Brown { 51882af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 51892af4f9b8SLen Brown return; 51902af4f9b8SLen Brown 51912af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 51922af4f9b8SLen Brown return; 51932af4f9b8SLen Brown 51942af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 51952af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 51962af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 51972af4f9b8SLen Brown 51982af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 51992af4f9b8SLen Brown } 52002af4f9b8SLen Brown 5201fcd17211SLen Brown void process_cpuid() 5202103a8feaSLen Brown { 520334041551SLen Brown unsigned int eax, ebx, ecx, edx; 520434041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5205b3a34e93SLen Brown unsigned int has_turbo; 5206ed0757b8SLen Brown unsigned long long ucode_patch = 0; 5207103a8feaSLen Brown 5208103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5209103a8feaSLen Brown 52105aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5211103a8feaSLen Brown 521234041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5213103a8feaSLen Brown genuine_intel = 1; 521434041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 521534041551SLen Brown authentic_amd = 1; 5216c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5217c1c10cc7SPu Wen hygon_genuine = 1; 5218103a8feaSLen Brown 521996e47158SLen Brown if (!quiet) 5220ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5221ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5222103a8feaSLen Brown 52235aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5224103a8feaSLen Brown family = (fms >> 8) & 0xf; 5225103a8feaSLen Brown model = (fms >> 4) & 0xf; 5226103a8feaSLen Brown stepping = fms & 0xf; 52275aa3d1a2SCalvin Walton if (family == 0xf) 52285aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 52295aa3d1a2SCalvin Walton if (family >= 6) 5230103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 523134041551SLen Brown ecx_flags = ecx; 523234041551SLen Brown edx_flags = edx; 5233103a8feaSLen Brown 5234ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 5235ed0757b8SLen Brown warnx("get_msr(UCODE)\n"); 5236ed0757b8SLen Brown 5237103a8feaSLen Brown /* 5238103a8feaSLen Brown * check max extended function levels of CPUID. 5239103a8feaSLen Brown * This is needed to check for invariant TSC. 5240103a8feaSLen Brown * This check is valid for both Intel and AMD. 5241103a8feaSLen Brown */ 5242103a8feaSLen Brown ebx = ecx = edx = 0; 52435aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5244103a8feaSLen Brown 524534041551SLen Brown if (!quiet) { 5246ed0757b8SLen Brown fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n", 5247*1b439f01SLen Brown family, model, stepping, family, model, stepping, 5248*1b439f01SLen Brown (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 5249ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 525034041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 525134041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 525234041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 525334041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 525434041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 525534041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 525634041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 525734041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 525834041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 5259*1b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-"); 526034041551SLen Brown } 526125368d7cSChen Yu if (genuine_intel) { 5262e9d3092fSZhang Rui model_orig = model; 5263f5a4c76aSLen Brown model = intel_model_duplicates(model); 526425368d7cSChen Yu } 526534041551SLen Brown 526634041551SLen Brown if (!(edx_flags & (1 << 5))) 526734041551SLen Brown errx(1, "CPUID: no MSR"); 526834041551SLen Brown 526961a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5270103a8feaSLen Brown 5271103a8feaSLen Brown /* 5272103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5273103a8feaSLen Brown * this check is valid for both Intel and AMD 5274103a8feaSLen Brown */ 52755aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 52768209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5277d7899447SLen Brown } 5278103a8feaSLen Brown 5279103a8feaSLen Brown /* 5280103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5281103a8feaSLen Brown * this check is valid for both Intel and AMD 5282103a8feaSLen Brown */ 5283103a8feaSLen Brown 52845aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 52858209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5286812db3f7SLen Brown if (has_aperf) { 5287812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5288812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5289812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5290812db3f7SLen Brown } 5291889facbeSLen Brown do_dts = eax & (1 << 0); 5292812db3f7SLen Brown if (do_dts) 5293812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5294b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5295889facbeSLen Brown do_ptm = eax & (1 << 6); 5296812db3f7SLen Brown if (do_ptm) 5297812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 52987f5c258eSLen Brown has_hwp = eax & (1 << 7); 52997f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 53007f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 53017f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 53027f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5303889facbeSLen Brown has_epb = ecx & (1 << 3); 5304889facbeSLen Brown 530596e47158SLen Brown if (!quiet) 5306b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 53077f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 53087f5c258eSLen Brown has_aperf ? "" : "No-", 5309b3a34e93SLen Brown has_turbo ? "" : "No-", 53107f5c258eSLen Brown do_dts ? "" : "No-", 53117f5c258eSLen Brown do_ptm ? "" : "No-", 53127f5c258eSLen Brown has_hwp ? "" : "No-", 53137f5c258eSLen Brown has_hwp_notify ? "" : "No-", 53147f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 5315*1b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 5316103a8feaSLen Brown 531796e47158SLen Brown if (!quiet) 531869807a63SLen Brown decode_misc_enable_msr(); 531969807a63SLen Brown 532096e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5321aa8d8cc7SLen Brown int has_sgx; 5322aa8d8cc7SLen Brown 5323aa8d8cc7SLen Brown ecx = 0; 5324aa8d8cc7SLen Brown 5325aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5326aa8d8cc7SLen Brown 5327aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5328aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5329aa8d8cc7SLen Brown 5330aa8d8cc7SLen Brown if (has_sgx) 5331aa8d8cc7SLen Brown decode_feature_control_msr(); 5332aa8d8cc7SLen Brown } 5333aa8d8cc7SLen Brown 533461a87ba7SLen Brown if (max_level >= 0x15) { 53358a5bdf41SLen Brown unsigned int eax_crystal; 53368a5bdf41SLen Brown unsigned int ebx_tsc; 53378a5bdf41SLen Brown 53388a5bdf41SLen Brown /* 53398a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 53408a5bdf41SLen Brown */ 53418a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 53425aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 53438a5bdf41SLen Brown 53448a5bdf41SLen Brown if (ebx_tsc != 0) { 53458a5bdf41SLen Brown 534696e47158SLen Brown if (!quiet && (ebx != 0)) 5347b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 53488a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 53498a5bdf41SLen Brown 53508a5bdf41SLen Brown if (crystal_hz == 0) 53518a5bdf41SLen Brown switch (model) { 5352af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5353e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5354e8efbc80SLen Brown break; 53555ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5356ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5357ec53e594SLen Brown break; 5358869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5359f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5360e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 53618a5bdf41SLen Brown break; 53628a5bdf41SLen Brown default: 53638a5bdf41SLen Brown crystal_hz = 0; 53648a5bdf41SLen Brown } 53658a5bdf41SLen Brown 53668a5bdf41SLen Brown if (crystal_hz) { 53678a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 536896e47158SLen Brown if (!quiet) 5369b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 53708a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 53718a5bdf41SLen Brown } 53728a5bdf41SLen Brown } 53738a5bdf41SLen Brown } 537461a87ba7SLen Brown if (max_level >= 0x16) { 537561a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 537661a87ba7SLen Brown 537761a87ba7SLen Brown /* 537861a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 537961a87ba7SLen Brown */ 538061a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 538161a87ba7SLen Brown 53825aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 538396e47158SLen Brown if (!quiet) 5384b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 538561a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 538661a87ba7SLen Brown } 53878a5bdf41SLen Brown 5388b2b34dfeSHubert Chrzaniuk if (has_aperf) 5389b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5390b2b34dfeSHubert Chrzaniuk 5391812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5392812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5393812db3f7SLen Brown 5394812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5395812db3f7SLen Brown do_nhm_platform_info = 1; 5396812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5397812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5398812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5399812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5400812db3f7SLen Brown } 5401d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5402812db3f7SLen Brown 5403812db3f7SLen Brown if (do_snb_cstates) 5404812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5405812db3f7SLen Brown 54065a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 54070f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 54080f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 54090f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 54100f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 54110f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 54120f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54130f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 54140f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 54150539ba11SLen Brown if (has_slv_msrs(family, model)) { 54160f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 54170f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 54180f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54190f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 54200539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 54210539ba11SLen Brown use_c1_residency_msr = 1; 54220539ba11SLen Brown } 542320de0dabSAntti Laakso if (is_jvl(family, model)) { 542420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 542520de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 542620de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 542720de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 542820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 542920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 543020de0dabSAntti Laakso } 54317170a374SLen Brown if (is_dnv(family, model)) { 54327170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 54337170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 54347170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 54357170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 54367170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 54377170a374SLen Brown use_c1_residency_msr = 1; 54387170a374SLen Brown } 543925368d7cSChen Yu if (is_skx(family, model) || is_icx(family, model)) { 544034c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 544134c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 544234c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 544334c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 544434c76197SLen Brown } 5445ade0ebacSLen Brown if (is_bdx(family, model)) { 5446ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5447ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5448ade0ebacSLen Brown } 5449570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5450c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 54510f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5452c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 54530f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5454c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 54550f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 54560f47c08dSLen Brown } 5457570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5458a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5459a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5460a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5461a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5462a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5463a99d8730SLen Brown } 5464144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5465fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5466103a8feaSLen Brown 5467*1b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model)) 5468562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5469103a8feaSLen Brown 547096e47158SLen Brown if (!quiet) 5471f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5472f0057310SLen Brown 547396e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 547471616c8eSLen Brown decode_c6_demotion_policy_msr(); 547571616c8eSLen Brown 5476889facbeSLen Brown rapl_probe(family, model); 54773a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5478ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5479889facbeSLen Brown 54800b9a0b9bSZhang Rui check_tcc_offset(model_orig); 54810b9a0b9bSZhang Rui 548296e47158SLen Brown if (!quiet) 54831b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5484fcd17211SLen Brown 548541618e63SLen Brown if (!quiet) 5486d76bb7a0SLen Brown print_dev_latency(); 5487d76bb7a0SLen Brown if (!quiet) 548841618e63SLen Brown dump_sysfs_cstate_config(); 54897293fccdSLen Brown if (!quiet) 54907293fccdSLen Brown dump_sysfs_pstate_config(); 549141618e63SLen Brown 54928c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5493a2b7b749SLen Brown calculate_tsc_tweak(); 5494a2b7b749SLen Brown 5495812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5496812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5497fdf676e5SLen Brown 5498812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5499812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 550027d47356SLen Brown 5501b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5502b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5503b4b91569SRafael Antognolli 5504be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5505be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5506be0e54c4SLen Brown else 5507be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5508be0e54c4SLen Brown 55091f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 55101f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5511be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 55121f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 55131f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 55141f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 55151f81c5efSLen Brown } else { 55161f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5517be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 55181f81c5efSLen Brown } 5519be0e54c4SLen Brown 552096e47158SLen Brown if (!quiet) 552133148d67SLen Brown decode_misc_feature_control(); 552233148d67SLen Brown 5523889facbeSLen Brown return; 5524103a8feaSLen Brown } 5525103a8feaSLen Brown 5526103a8feaSLen Brown /* 5527103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5528103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5529103a8feaSLen Brown */ 5530103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5531103a8feaSLen Brown { 5532103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5533103a8feaSLen Brown return 1; 5534103a8feaSLen Brown else 5535103a8feaSLen Brown return 0; 5536103a8feaSLen Brown } 5537103a8feaSLen Brown 5538103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5539103a8feaSLen Brown { 5540103a8feaSLen Brown return 0; 5541103a8feaSLen Brown } 5542103a8feaSLen Brown 5543c98d5d94SLen Brown void topology_probe() 5544c98d5d94SLen Brown { 5545c98d5d94SLen Brown int i; 5546c98d5d94SLen Brown int max_core_id = 0; 5547c98d5d94SLen Brown int max_package_id = 0; 55486de68fe1SLen Brown int max_die_id = 0; 5549c98d5d94SLen Brown int max_siblings = 0; 5550c98d5d94SLen Brown 5551c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5552843c5791SPrarit Bhargava set_max_cpu_num(); 5553c98d5d94SLen Brown topo.num_cpus = 0; 5554c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5555c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5556812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5557c98d5d94SLen Brown 5558d8af6f5fSLen Brown if (debug > 1) 5559b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5560c98d5d94SLen Brown 5561c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5562b2c95d90SJosh Triplett if (cpus == NULL) 5563b2c95d90SJosh Triplett err(1, "calloc cpus"); 5564c98d5d94SLen Brown 5565c98d5d94SLen Brown /* 5566c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5567c98d5d94SLen Brown */ 5568c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5569b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5570b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5571c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5572c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5573c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5574c98d5d94SLen Brown 5575c98d5d94SLen Brown /* 55761ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 55771ef7d21aSLen Brown */ 55781ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 55791ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 55801ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 55811ef7d21aSLen Brown err(1, "cpu%d not present", i); 55821ef7d21aSLen Brown } 55831ef7d21aSLen Brown 55841ef7d21aSLen Brown /* 5585c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5586c98d5d94SLen Brown */ 5587c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5588b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5589b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5590c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5591c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5592c98d5d94SLen Brown 55938cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5594c98d5d94SLen Brown 5595c98d5d94SLen Brown /* 5596c98d5d94SLen Brown * For online cpus 5597c98d5d94SLen Brown * find max_core_id, max_package_id 5598c98d5d94SLen Brown */ 5599c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5600c98d5d94SLen Brown int siblings; 5601c98d5d94SLen Brown 5602c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5603d8af6f5fSLen Brown if (debug > 1) 5604b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5605c98d5d94SLen Brown continue; 5606c98d5d94SLen Brown } 5607c98d5d94SLen Brown 56080e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 56090e2d8f05SLen Brown 56100e2d8f05SLen Brown /* get package information */ 5611c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5612c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5613c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5614c98d5d94SLen Brown 56156de68fe1SLen Brown /* get die information */ 56166de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 56176de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 56186de68fe1SLen Brown max_die_id = cpus[i].die_id; 56196de68fe1SLen Brown 56200e2d8f05SLen Brown /* get numa node information */ 5621ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5622ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5623ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 56240e2d8f05SLen Brown 56250e2d8f05SLen Brown /* get core information */ 56260e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 56270e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 56280e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 56290e2d8f05SLen Brown 56300e2d8f05SLen Brown /* get thread information */ 56310e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5632c98d5d94SLen Brown if (siblings > max_siblings) 5633c98d5d94SLen Brown max_siblings = siblings; 56344f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 56358cb48b32SPrarit Bhargava topo.num_cores++; 5636c98d5d94SLen Brown } 5637ef605741SPrarit Bhargava 563870a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5639d8af6f5fSLen Brown if (debug > 1) 5640*1b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node); 564170a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5642812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5643c98d5d94SLen Brown 56446de68fe1SLen Brown topo.num_die = max_die_id + 1; 56456de68fe1SLen Brown if (debug > 1) 5646*1b439f01SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die); 56476de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 56486de68fe1SLen Brown BIC_PRESENT(BIC_Die); 56496de68fe1SLen Brown 5650c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5651d8af6f5fSLen Brown if (debug > 1) 5652*1b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 56537da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5654812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5655c98d5d94SLen Brown 5656ef605741SPrarit Bhargava set_node_data(); 5657ef605741SPrarit Bhargava if (debug > 1) 565870a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 565901235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 566001235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5661ef605741SPrarit Bhargava 566270a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5663d8af6f5fSLen Brown if (debug > 1) 5664b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 56652ffbb224SPrarit Bhargava 56662ffbb224SPrarit Bhargava if (debug < 1) 56672ffbb224SPrarit Bhargava return; 56682ffbb224SPrarit Bhargava 56692ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 56700ec712e3SLen Brown if (cpu_is_not_present(i)) 56710ec712e3SLen Brown continue; 56722ffbb224SPrarit Bhargava fprintf(outf, 56736de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 56746de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 5675*1b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id); 56762ffbb224SPrarit Bhargava } 56772ffbb224SPrarit Bhargava 5678c98d5d94SLen Brown } 5679c98d5d94SLen Brown 5680*1b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 5681c98d5d94SLen Brown { 5682c98d5d94SLen Brown int i; 5683*1b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; 568440f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5685c98d5d94SLen Brown 568640f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5687c98d5d94SLen Brown if (*t == NULL) 5688c98d5d94SLen Brown goto error; 5689c98d5d94SLen Brown 569040f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5691c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5692c98d5d94SLen Brown 569340f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5694c98d5d94SLen Brown if (*c == NULL) 5695c98d5d94SLen Brown goto error; 5696c98d5d94SLen Brown 569740f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5698c98d5d94SLen Brown (*c)[i].core_id = -1; 5699c98d5d94SLen Brown 5700678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5701c98d5d94SLen Brown if (*p == NULL) 5702c98d5d94SLen Brown goto error; 5703c98d5d94SLen Brown 5704c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5705c98d5d94SLen Brown (*p)[i].package_id = i; 5706c98d5d94SLen Brown 5707c98d5d94SLen Brown return; 5708c98d5d94SLen Brown error: 5709b2c95d90SJosh Triplett err(1, "calloc counters"); 5710c98d5d94SLen Brown } 5711*1b439f01SLen Brown 5712c98d5d94SLen Brown /* 5713c98d5d94SLen Brown * init_counter() 5714c98d5d94SLen Brown * 5715c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5716c98d5d94SLen Brown */ 5717*1b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 5718c98d5d94SLen Brown { 57198cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 572040f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 57218cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 57228cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5723c98d5d94SLen Brown struct thread_data *t; 5724c98d5d94SLen Brown struct core_data *c; 5725c98d5d94SLen Brown struct pkg_data *p; 5726c98d5d94SLen Brown 572742dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 572842dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 572942dd4520SNathan Ciobanu */ 573042dd4520SNathan Ciobanu if (node_id < 0) 573142dd4520SNathan Ciobanu node_id = 0; 573242dd4520SNathan Ciobanu 573340f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 573440f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 57358cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5736c98d5d94SLen Brown 5737c98d5d94SLen Brown t->cpu_id = cpu_id; 57388cb48b32SPrarit Bhargava if (thread_id == 0) { 5739c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5740c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5741c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5742c98d5d94SLen Brown } 5743c98d5d94SLen Brown 57448cb48b32SPrarit Bhargava c->core_id = core_id; 57458cb48b32SPrarit Bhargava p->package_id = pkg_id; 5746c98d5d94SLen Brown } 5747c98d5d94SLen Brown 5748c98d5d94SLen Brown int initialize_counters(int cpu_id) 5749c98d5d94SLen Brown { 57508cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 57518cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5752c98d5d94SLen Brown return 0; 5753c98d5d94SLen Brown } 5754c98d5d94SLen Brown 5755c98d5d94SLen Brown void allocate_output_buffer() 5756c98d5d94SLen Brown { 5757eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5758c98d5d94SLen Brown outp = output_buffer; 5759b2c95d90SJosh Triplett if (outp == NULL) 5760b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5761c98d5d94SLen Brown } 5762*1b439f01SLen Brown 576336229897SLen Brown void allocate_fd_percpu(void) 576436229897SLen Brown { 576501a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 576636229897SLen Brown if (fd_percpu == NULL) 576736229897SLen Brown err(-1, "calloc fd_percpu"); 576836229897SLen Brown } 5769*1b439f01SLen Brown 5770562a2d37SLen Brown void allocate_irq_buffers(void) 5771562a2d37SLen Brown { 5772562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5773562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5774562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5775c98d5d94SLen Brown 577601a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5777562a2d37SLen Brown if (irqs_per_cpu == NULL) 577801a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5779562a2d37SLen Brown } 5780*1b439f01SLen Brown 5781c98d5d94SLen Brown void setup_all_buffers(void) 5782c98d5d94SLen Brown { 5783c98d5d94SLen Brown topology_probe(); 5784562a2d37SLen Brown allocate_irq_buffers(); 578536229897SLen Brown allocate_fd_percpu(); 5786c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5787c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5788c98d5d94SLen Brown allocate_output_buffer(); 5789c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5790c98d5d94SLen Brown } 57913b4d5c7fSAndy Shevchenko 57927ce7d5deSPrarit Bhargava void set_base_cpu(void) 57937ce7d5deSPrarit Bhargava { 57947ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 57957ce7d5deSPrarit Bhargava if (base_cpu < 0) 57967ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 57977ce7d5deSPrarit Bhargava 57987ce7d5deSPrarit Bhargava if (debug > 1) 5799b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 58007ce7d5deSPrarit Bhargava } 58017ce7d5deSPrarit Bhargava 5802103a8feaSLen Brown void turbostat_init() 5803103a8feaSLen Brown { 58047ce7d5deSPrarit Bhargava setup_all_buffers(); 58057ce7d5deSPrarit Bhargava set_base_cpu(); 5806103a8feaSLen Brown check_dev_msr(); 580798481e79SLen Brown check_permissions(); 5808fcd17211SLen Brown process_cpuid(); 58092af4f9b8SLen Brown linux_perf_init(); 5810103a8feaSLen Brown 581196e47158SLen Brown if (!quiet) 58127f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 58137f5c258eSLen Brown 581496e47158SLen Brown if (!quiet) 5815889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5816889facbeSLen Brown 581796e47158SLen Brown if (!quiet) 58183a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 58193a9a941dSLen Brown 582096e47158SLen Brown if (!quiet) 5821889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5822889facbeSLen Brown 5823889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5824889facbeSLen Brown 58257ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 58267ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 58277ab5ff49SZhang Rui 582896e47158SLen Brown if (!quiet) 5829889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 58305a63426eSLen Brown 583196e47158SLen Brown if (!quiet && do_irtl_snb) 58325a63426eSLen Brown print_irtl(); 5833103a8feaSLen Brown } 5834103a8feaSLen Brown 5835103a8feaSLen Brown int fork_it(char **argv) 5836103a8feaSLen Brown { 5837103a8feaSLen Brown pid_t child_pid; 5838d91bb17cSLen Brown int status; 5839d15cf7c1SLen Brown 5840218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5841d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 58424c2122d4SLen Brown first_counter_read = 0; 5843d91bb17cSLen Brown if (status) 5844d91bb17cSLen Brown exit(status); 5845d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5846d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5847103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5848103a8feaSLen Brown 5849103a8feaSLen Brown child_pid = fork(); 5850103a8feaSLen Brown if (!child_pid) { 5851103a8feaSLen Brown /* child */ 5852103a8feaSLen Brown execvp(argv[0], argv); 58530815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5854103a8feaSLen Brown } else { 5855103a8feaSLen Brown 5856103a8feaSLen Brown /* parent */ 5857b2c95d90SJosh Triplett if (child_pid == -1) 5858b2c95d90SJosh Triplett err(1, "fork"); 5859103a8feaSLen Brown 5860103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5861103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5862b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5863b2c95d90SJosh Triplett err(status, "waitpid"); 58642a954966SDavid Arcari 58652a954966SDavid Arcari if (WIFEXITED(status)) 58662a954966SDavid Arcari status = WEXITSTATUS(status); 5867103a8feaSLen Brown } 5868c98d5d94SLen Brown /* 5869c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5870c98d5d94SLen Brown * because re-starting is problematic when forking 5871c98d5d94SLen Brown */ 5872218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5873c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5874103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5875103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5876ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5877ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5878ba3dec99SLen Brown else { 5879c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5880c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5881ba3dec99SLen Brown } 5882103a8feaSLen Brown 5883b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 5884b7d8c148SLen Brown 5885b7d8c148SLen Brown flush_output_stderr(); 5886103a8feaSLen Brown 5887d91bb17cSLen Brown return status; 5888103a8feaSLen Brown } 5889103a8feaSLen Brown 58903b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 58913b4d5c7fSAndy Shevchenko { 58923b4d5c7fSAndy Shevchenko int status; 58933b4d5c7fSAndy Shevchenko 5894218f0e8dSLen Brown snapshot_proc_sysfs_files(); 58953b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 58963b4d5c7fSAndy Shevchenko if (status) 58973b4d5c7fSAndy Shevchenko return status; 58983b4d5c7fSAndy Shevchenko 58993b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 59003b4d5c7fSAndy Shevchenko if (status) 59013b4d5c7fSAndy Shevchenko return status; 59023b4d5c7fSAndy Shevchenko 5903b7d8c148SLen Brown flush_output_stdout(); 59043b4d5c7fSAndy Shevchenko 59053b4d5c7fSAndy Shevchenko return status; 59063b4d5c7fSAndy Shevchenko } 59073b4d5c7fSAndy Shevchenko 5908*1b439f01SLen Brown void print_version() 5909*1b439f01SLen Brown { 5910*1b439f01SLen Brown fprintf(outf, "turbostat version 21.03.12" " - Len Brown <lenb@kernel.org>\n"); 5911d8af6f5fSLen Brown } 5912d8af6f5fSLen Brown 5913495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5914495c7654SLen Brown unsigned int width, enum counter_scope scope, 591541618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5916388e9c81SLen Brown { 5917388e9c81SLen Brown struct msr_counter *msrp; 5918388e9c81SLen Brown 5919388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5920388e9c81SLen Brown if (msrp == NULL) { 5921388e9c81SLen Brown perror("calloc"); 5922388e9c81SLen Brown exit(1); 5923388e9c81SLen Brown } 5924388e9c81SLen Brown 5925388e9c81SLen Brown msrp->msr_num = msr_num; 5926d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 5927495c7654SLen Brown if (path) 5928d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 5929388e9c81SLen Brown msrp->width = width; 5930388e9c81SLen Brown msrp->type = type; 5931388e9c81SLen Brown msrp->format = format; 593241618e63SLen Brown msrp->flags = flags; 5933388e9c81SLen Brown 5934388e9c81SLen Brown switch (scope) { 5935388e9c81SLen Brown 5936388e9c81SLen Brown case SCOPE_CPU: 5937388e9c81SLen Brown msrp->next = sys.tp; 5938388e9c81SLen Brown sys.tp = msrp; 5939678a3bd1SLen Brown sys.added_thread_counters++; 59400748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5941*1b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS); 5942678a3bd1SLen Brown exit(-1); 5943678a3bd1SLen Brown } 5944388e9c81SLen Brown break; 5945388e9c81SLen Brown 5946388e9c81SLen Brown case SCOPE_CORE: 5947388e9c81SLen Brown msrp->next = sys.cp; 5948388e9c81SLen Brown sys.cp = msrp; 5949678a3bd1SLen Brown sys.added_core_counters++; 5950678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5951*1b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS); 5952678a3bd1SLen Brown exit(-1); 5953678a3bd1SLen Brown } 5954388e9c81SLen Brown break; 5955388e9c81SLen Brown 5956388e9c81SLen Brown case SCOPE_PACKAGE: 5957388e9c81SLen Brown msrp->next = sys.pp; 5958388e9c81SLen Brown sys.pp = msrp; 5959678a3bd1SLen Brown sys.added_package_counters++; 5960678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5961*1b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS); 5962678a3bd1SLen Brown exit(-1); 5963678a3bd1SLen Brown } 5964388e9c81SLen Brown break; 5965388e9c81SLen Brown } 5966388e9c81SLen Brown 5967388e9c81SLen Brown return 0; 5968388e9c81SLen Brown } 5969388e9c81SLen Brown 5970388e9c81SLen Brown void parse_add_command(char *add_command) 5971388e9c81SLen Brown { 5972388e9c81SLen Brown int msr_num = 0; 5973495c7654SLen Brown char *path = NULL; 59740f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5975388e9c81SLen Brown int width = 64; 5976388e9c81SLen Brown int fail = 0; 5977388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5978388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5979388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5980388e9c81SLen Brown 5981388e9c81SLen Brown while (add_command) { 5982388e9c81SLen Brown 5983388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5984388e9c81SLen Brown goto next; 5985388e9c81SLen Brown 5986388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5987388e9c81SLen Brown goto next; 5988388e9c81SLen Brown 5989495c7654SLen Brown if (*add_command == '/') { 5990495c7654SLen Brown path = add_command; 5991495c7654SLen Brown goto next; 5992495c7654SLen Brown } 5993495c7654SLen Brown 5994388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5995388e9c81SLen Brown if ((width == 32) || (width == 64)) 5996388e9c81SLen Brown goto next; 5997388e9c81SLen Brown width = 64; 5998388e9c81SLen Brown } 5999388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6000388e9c81SLen Brown scope = SCOPE_CPU; 6001388e9c81SLen Brown goto next; 6002388e9c81SLen Brown } 6003388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6004388e9c81SLen Brown scope = SCOPE_CORE; 6005388e9c81SLen Brown goto next; 6006388e9c81SLen Brown } 6007388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6008388e9c81SLen Brown scope = SCOPE_PACKAGE; 6009388e9c81SLen Brown goto next; 6010388e9c81SLen Brown } 6011388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6012388e9c81SLen Brown type = COUNTER_CYCLES; 6013388e9c81SLen Brown goto next; 6014388e9c81SLen Brown } 6015388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6016388e9c81SLen Brown type = COUNTER_SECONDS; 6017388e9c81SLen Brown goto next; 6018388e9c81SLen Brown } 601941618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 602041618e63SLen Brown type = COUNTER_USEC; 602141618e63SLen Brown goto next; 602241618e63SLen Brown } 6023388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6024388e9c81SLen Brown format = FORMAT_RAW; 6025388e9c81SLen Brown goto next; 6026388e9c81SLen Brown } 6027388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6028388e9c81SLen Brown format = FORMAT_DELTA; 6029388e9c81SLen Brown goto next; 6030388e9c81SLen Brown } 6031388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6032388e9c81SLen Brown format = FORMAT_PERCENT; 6033388e9c81SLen Brown goto next; 6034388e9c81SLen Brown } 6035388e9c81SLen Brown 6036388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6037388e9c81SLen Brown char *eos; 6038388e9c81SLen Brown 6039388e9c81SLen Brown eos = strchr(name_buffer, ','); 6040388e9c81SLen Brown if (eos) 6041388e9c81SLen Brown *eos = '\0'; 6042388e9c81SLen Brown goto next; 6043388e9c81SLen Brown } 6044388e9c81SLen Brown 6045388e9c81SLen Brown next: 6046388e9c81SLen Brown add_command = strchr(add_command, ','); 6047495c7654SLen Brown if (add_command) { 6048495c7654SLen Brown *add_command = '\0'; 6049388e9c81SLen Brown add_command++; 6050495c7654SLen Brown } 6051388e9c81SLen Brown 6052388e9c81SLen Brown } 6053495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6054495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6055388e9c81SLen Brown fail++; 6056388e9c81SLen Brown } 6057388e9c81SLen Brown 6058388e9c81SLen Brown /* generate default column header */ 6059388e9c81SLen Brown if (*name_buffer == '\0') { 6060388e9c81SLen Brown if (width == 32) 60615f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6062388e9c81SLen Brown else 60635f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6064388e9c81SLen Brown } 6065388e9c81SLen Brown 606641618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6067388e9c81SLen Brown fail++; 6068388e9c81SLen Brown 6069388e9c81SLen Brown if (fail) { 6070388e9c81SLen Brown help(); 6071388e9c81SLen Brown exit(1); 6072388e9c81SLen Brown } 6073388e9c81SLen Brown } 607441618e63SLen Brown 6075dd778a5eSLen Brown int is_deferred_skip(char *name) 6076dd778a5eSLen Brown { 6077dd778a5eSLen Brown int i; 6078dd778a5eSLen Brown 6079dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6080dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6081dd778a5eSLen Brown return 1; 6082dd778a5eSLen Brown return 0; 6083dd778a5eSLen Brown } 6084dd778a5eSLen Brown 608541618e63SLen Brown void probe_sysfs(void) 608641618e63SLen Brown { 608741618e63SLen Brown char path[64]; 608841618e63SLen Brown char name_buf[16]; 608941618e63SLen Brown FILE *input; 609041618e63SLen Brown int state; 609141618e63SLen Brown char *sp; 609241618e63SLen Brown 609341618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 609441618e63SLen Brown return; 609541618e63SLen Brown 60960748eaf0SLen Brown for (state = 10; state >= 0; --state) { 609741618e63SLen Brown 6098*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 609941618e63SLen Brown input = fopen(path, "r"); 610041618e63SLen Brown if (input == NULL) 610141618e63SLen Brown continue; 61028173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 61038173c336SBen Hutchings err(1, "%s: failed to read file", path); 610441618e63SLen Brown 610541618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 610641618e63SLen Brown sp = strchr(name_buf, '-'); 610741618e63SLen Brown if (!sp) 610841618e63SLen Brown sp = strchrnul(name_buf, '\n'); 610941618e63SLen Brown *sp = '%'; 611041618e63SLen Brown *(sp + 1) = '\0'; 611141618e63SLen Brown 6112fecb3bc8SDavid Arcari remove_underbar(name_buf); 6113fecb3bc8SDavid Arcari 611441618e63SLen Brown fclose(input); 611541618e63SLen Brown 611641618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 611741618e63SLen Brown 6118dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6119dd778a5eSLen Brown continue; 6120dd778a5eSLen Brown 6121*1b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU); 612241618e63SLen Brown } 612341618e63SLen Brown 61240748eaf0SLen Brown for (state = 10; state >= 0; --state) { 612541618e63SLen Brown 6126*1b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 612741618e63SLen Brown input = fopen(path, "r"); 612841618e63SLen Brown if (input == NULL) 612941618e63SLen Brown continue; 61308173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 61318173c336SBen Hutchings err(1, "%s: failed to read file", path); 613241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 613341618e63SLen Brown sp = strchr(name_buf, '-'); 613441618e63SLen Brown if (!sp) 613541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 613641618e63SLen Brown *sp = '\0'; 613741618e63SLen Brown fclose(input); 613841618e63SLen Brown 6139fecb3bc8SDavid Arcari remove_underbar(name_buf); 6140fecb3bc8SDavid Arcari 614141618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 614241618e63SLen Brown 6143dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6144dd778a5eSLen Brown continue; 6145dd778a5eSLen Brown 6146*1b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU); 614741618e63SLen Brown } 614841618e63SLen Brown 614941618e63SLen Brown } 615041618e63SLen Brown 61511ef7d21aSLen Brown /* 61521ef7d21aSLen Brown * parse cpuset with following syntax 61531ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 61541ef7d21aSLen Brown */ 61551ef7d21aSLen Brown void parse_cpu_command(char *optarg) 61561ef7d21aSLen Brown { 61571ef7d21aSLen Brown unsigned int start, end; 61581ef7d21aSLen Brown char *next; 61591ef7d21aSLen Brown 61604e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 61614e4e1e7cSLen Brown if (cpu_subset) 61624e4e1e7cSLen Brown goto error; 61634e4e1e7cSLen Brown show_core_only++; 61644e4e1e7cSLen Brown return; 61654e4e1e7cSLen Brown } 61664e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 61674e4e1e7cSLen Brown if (cpu_subset) 61684e4e1e7cSLen Brown goto error; 61694e4e1e7cSLen Brown show_pkg_only++; 61704e4e1e7cSLen Brown return; 61714e4e1e7cSLen Brown } 61724e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 61734e4e1e7cSLen Brown goto error; 61744e4e1e7cSLen Brown 61751ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 61761ef7d21aSLen Brown if (cpu_subset == NULL) 61771ef7d21aSLen Brown err(3, "CPU_ALLOC"); 61781ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 61791ef7d21aSLen Brown 61801ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 61811ef7d21aSLen Brown 61821ef7d21aSLen Brown next = optarg; 61831ef7d21aSLen Brown 61841ef7d21aSLen Brown while (next && *next) { 61851ef7d21aSLen Brown 61861ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 61871ef7d21aSLen Brown goto error; 61881ef7d21aSLen Brown 61891ef7d21aSLen Brown start = strtoul(next, &next, 10); 61901ef7d21aSLen Brown 61911ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 61921ef7d21aSLen Brown goto error; 61931ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 61941ef7d21aSLen Brown 61951ef7d21aSLen Brown if (*next == '\0') 61961ef7d21aSLen Brown break; 61971ef7d21aSLen Brown 61981ef7d21aSLen Brown if (*next == ',') { 61991ef7d21aSLen Brown next += 1; 62001ef7d21aSLen Brown continue; 62011ef7d21aSLen Brown } 62021ef7d21aSLen Brown 62031ef7d21aSLen Brown if (*next == '-') { 62041ef7d21aSLen Brown next += 1; /* start range */ 62051ef7d21aSLen Brown } else if (*next == '.') { 62061ef7d21aSLen Brown next += 1; 62071ef7d21aSLen Brown if (*next == '.') 62081ef7d21aSLen Brown next += 1; /* start range */ 62091ef7d21aSLen Brown else 62101ef7d21aSLen Brown goto error; 62111ef7d21aSLen Brown } 62121ef7d21aSLen Brown 62131ef7d21aSLen Brown end = strtoul(next, &next, 10); 62141ef7d21aSLen Brown if (end <= start) 62151ef7d21aSLen Brown goto error; 62161ef7d21aSLen Brown 62171ef7d21aSLen Brown while (++start <= end) { 62181ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 62191ef7d21aSLen Brown goto error; 62201ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 62211ef7d21aSLen Brown } 62221ef7d21aSLen Brown 62231ef7d21aSLen Brown if (*next == ',') 62241ef7d21aSLen Brown next += 1; 62251ef7d21aSLen Brown else if (*next != '\0') 62261ef7d21aSLen Brown goto error; 62271ef7d21aSLen Brown } 62281ef7d21aSLen Brown 62291ef7d21aSLen Brown return; 62301ef7d21aSLen Brown 62311ef7d21aSLen Brown error: 62324e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 62334e4e1e7cSLen Brown help(); 62341ef7d21aSLen Brown exit(-1); 62351ef7d21aSLen Brown } 62361ef7d21aSLen Brown 6237103a8feaSLen Brown void cmdline(int argc, char **argv) 6238103a8feaSLen Brown { 6239103a8feaSLen Brown int opt; 6240d8af6f5fSLen Brown int option_index = 0; 6241d8af6f5fSLen Brown static struct option long_options[] = { 6242388e9c81SLen Brown { "add", required_argument, 0, 'a' }, 62431ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' }, 6244d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' }, 624596e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 62463f44a5c6SLen Brown { "enable", required_argument, 0, 'e' }, 6247d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' }, 62482af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' }, 6249023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' }, 6250d8af6f5fSLen Brown { "help", no_argument, 0, 'h' }, 6251812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 6252d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' }, 6253c8ade361SLen Brown { "list", no_argument, 0, 'l' }, 6254b7d8c148SLen Brown { "out", required_argument, 0, 'o' }, 625596e47158SLen Brown { "quiet", no_argument, 0, 'q' }, 6256812db3f7SLen Brown { "show", required_argument, 0, 's' }, 6257d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' }, 6258d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' }, 6259d8af6f5fSLen Brown { "version", no_argument, 0, 'v' }, 6260d8af6f5fSLen Brown { 0, 0, 0, 0 } 6261d8af6f5fSLen Brown }; 6262103a8feaSLen Brown 6263103a8feaSLen Brown progname = argv[0]; 6264103a8feaSLen Brown 6265*1b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) { 6266103a8feaSLen Brown switch (opt) { 6267388e9c81SLen Brown case 'a': 6268388e9c81SLen Brown parse_add_command(optarg); 6269388e9c81SLen Brown break; 62701ef7d21aSLen Brown case 'c': 62711ef7d21aSLen Brown parse_cpu_command(optarg); 62721ef7d21aSLen Brown break; 6273d8af6f5fSLen Brown case 'D': 6274d8af6f5fSLen Brown dump_only++; 62758e180f3cSLen Brown break; 62763f44a5c6SLen Brown case 'e': 62773f44a5c6SLen Brown /* --enable specified counter */ 62784c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 62793f44a5c6SLen Brown break; 6280d8af6f5fSLen Brown case 'd': 6281d8af6f5fSLen Brown debug++; 62823f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 62832f32edf1SLen Brown break; 6284812db3f7SLen Brown case 'H': 62853f44a5c6SLen Brown /* 62863f44a5c6SLen Brown * --hide: do not show those specified 62873f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 62883f44a5c6SLen Brown */ 62893f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6290812db3f7SLen Brown break; 6291d8af6f5fSLen Brown case 'h': 6292d8af6f5fSLen Brown default: 6293d8af6f5fSLen Brown help(); 6294d8af6f5fSLen Brown exit(1); 6295d8af6f5fSLen Brown case 'i': 62962a0609c0SLen Brown { 62972a0609c0SLen Brown double interval = strtod(optarg, NULL); 62982a0609c0SLen Brown 62992a0609c0SLen Brown if (interval < 0.001) { 6300*1b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval); 63012a0609c0SLen Brown exit(2); 63022a0609c0SLen Brown } 63032a0609c0SLen Brown 630447936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6305b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 630647936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 63072a0609c0SLen Brown } 6308889facbeSLen Brown break; 63095c56be9aSDirk Brandewie case 'J': 63105c56be9aSDirk Brandewie rapl_joules++; 63115c56be9aSDirk Brandewie break; 6312c8ade361SLen Brown case 'l': 63133f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6314c8ade361SLen Brown list_header_only++; 6315c8ade361SLen Brown quiet++; 6316c8ade361SLen Brown break; 6317b7d8c148SLen Brown case 'o': 6318b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6319b7d8c148SLen Brown break; 632096e47158SLen Brown case 'q': 632196e47158SLen Brown quiet = 1; 632296e47158SLen Brown break; 6323023fe0acSChen Yu case 'n': 6324023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6325023fe0acSChen Yu 6326023fe0acSChen Yu if (num_iterations <= 0) { 6327*1b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations); 6328023fe0acSChen Yu exit(2); 6329023fe0acSChen Yu } 6330023fe0acSChen Yu break; 6331812db3f7SLen Brown case 's': 63323f44a5c6SLen Brown /* 63333f44a5c6SLen Brown * --show: show only those specified 63343f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 63353f44a5c6SLen Brown * subsequent invocations can add to it. 63363f44a5c6SLen Brown */ 63373f44a5c6SLen Brown if (shown == 0) 63383f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 63393f44a5c6SLen Brown else 63403f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 63413f44a5c6SLen Brown shown = 1; 6342812db3f7SLen Brown break; 6343d8af6f5fSLen Brown case 'S': 6344d8af6f5fSLen Brown summary_only++; 6345d8af6f5fSLen Brown break; 6346d8af6f5fSLen Brown case 'T': 634755279aefSZhang Rui tj_max_override = atoi(optarg); 6348d8af6f5fSLen Brown break; 6349d8af6f5fSLen Brown case 'v': 6350d8af6f5fSLen Brown print_version(); 6351d8af6f5fSLen Brown exit(0); 6352d8af6f5fSLen Brown break; 6353103a8feaSLen Brown } 6354103a8feaSLen Brown } 6355103a8feaSLen Brown } 6356103a8feaSLen Brown 6357103a8feaSLen Brown int main(int argc, char **argv) 6358103a8feaSLen Brown { 6359b7d8c148SLen Brown outf = stderr; 6360103a8feaSLen Brown cmdline(argc, argv); 6361103a8feaSLen Brown 636296e47158SLen Brown if (!quiet) 6363d8af6f5fSLen Brown print_version(); 6364103a8feaSLen Brown 636541618e63SLen Brown probe_sysfs(); 636641618e63SLen Brown 6367103a8feaSLen Brown turbostat_init(); 6368103a8feaSLen Brown 63693b4d5c7fSAndy Shevchenko /* dump counters and exit */ 63703b4d5c7fSAndy Shevchenko if (dump_only) 63713b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 63723b4d5c7fSAndy Shevchenko 6373c8ade361SLen Brown /* list header and exit */ 6374c8ade361SLen Brown if (list_header_only) { 6375c8ade361SLen Brown print_header(","); 6376c8ade361SLen Brown flush_output_stdout(); 6377c8ade361SLen Brown return 0; 6378c8ade361SLen Brown } 6379c8ade361SLen Brown 63809972d5d8SChen Yu msr_sum_record(); 6381103a8feaSLen Brown /* 6382103a8feaSLen Brown * if any params left, it must be a command to fork 6383103a8feaSLen Brown */ 6384103a8feaSLen Brown if (argc - optind) 6385103a8feaSLen Brown return fork_it(argv + optind); 6386103a8feaSLen Brown else 6387103a8feaSLen Brown turbostat_loop(); 6388103a8feaSLen Brown 6389103a8feaSLen Brown return 0; 6390103a8feaSLen Brown } 6391