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}; 46*e9d3092fSZhang Rui 47*e9d3092fSZhang Rui /* Save original CPU model */ 48*e9d3092fSZhang Rui unsigned int model_orig; 49*e9d3092fSZhang 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; 92889facbeSLen Brown unsigned int tcc_activation_temp; 93889facbeSLen Brown unsigned int tcc_activation_temp_override; 9440ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 9540ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 96889facbeSLen Brown double rapl_joule_counter_range; 973a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 98ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 99aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 1003a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1013a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1028a5bdf41SLen Brown unsigned int crystal_hz; 1038a5bdf41SLen Brown unsigned long long tsc_hz; 1047ce7d5deSPrarit Bhargava int base_cpu; 10521ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1067f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1077f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1087f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1097f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1107f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1117f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 11233148d67SLen Brown unsigned int has_misc_feature_control; 1134c2122d4SLen Brown unsigned int first_counter_read = 1; 114c026c236SArtem Bityutskiy int ignore_stdin; 115889facbeSLen Brown 116889facbeSLen Brown #define RAPL_PKG (1 << 0) 117e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 118e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 119e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 120e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 121e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 122e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 123e6f9bb3cSLen Brown 124889facbeSLen Brown #define RAPL_DRAM (1 << 3) 125e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 126e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 127e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 128e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1290b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1300b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 131e6f9bb3cSLen Brown 1329148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 133e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1340b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 135e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 136e6f9bb3cSLen Brown 1370b2bb692SLen Brown #define RAPL_GFX (1 << 8) 138e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 139e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 140e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1419148494cSJacob Pan 1429148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1439148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1449392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1459392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1469392bd98SCalvin Walton * not per-package. */ 1479392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1489392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1499392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1509392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1519148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 152889facbeSLen Brown #define TJMAX_DEFAULT 100 153889facbeSLen Brown 1549392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1559392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1569392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1579392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1589392bd98SCalvin Walton 159889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 160103a8feaSLen Brown 161388e9c81SLen Brown /* 162388e9c81SLen Brown * buffer size used by sscanf() for added column names 163388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 164388e9c81SLen Brown */ 165388e9c81SLen Brown #define NAME_BYTES 20 166495c7654SLen Brown #define PATH_BYTES 128 167388e9c81SLen Brown 168103a8feaSLen Brown int backwards_count; 169103a8feaSLen Brown char *progname; 170103a8feaSLen Brown 1711ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1721ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1731ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1740748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1750748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1760e2d8f05SLen Brown #define BITMASK_SIZE 32 177c98d5d94SLen Brown 178c98d5d94SLen Brown struct thread_data { 179f4fdf2b4SLen Brown struct timeval tv_begin; 180f4fdf2b4SLen Brown struct timeval tv_end; 181d4794f25SYazen Ghannam struct timeval tv_delta; 182c98d5d94SLen Brown unsigned long long tsc; 183c98d5d94SLen Brown unsigned long long aperf; 184c98d5d94SLen Brown unsigned long long mperf; 185144b44b1SLen Brown unsigned long long c1; 1862af4f9b8SLen Brown unsigned long long instr_count; 1870de6c0dfSLen Brown unsigned long long irq_count; 1881ed51011SLen Brown unsigned int smi_count; 189c98d5d94SLen Brown unsigned int cpu_id; 1904c2122d4SLen Brown unsigned int apic_id; 1914c2122d4SLen Brown unsigned int x2apic_id; 192c98d5d94SLen Brown unsigned int flags; 1937ab5ff49SZhang Rui bool is_atom; 194c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 195c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1960748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 197c98d5d94SLen Brown } *thread_even, *thread_odd; 198c98d5d94SLen Brown 199c98d5d94SLen Brown struct core_data { 200c98d5d94SLen Brown unsigned long long c3; 201c98d5d94SLen Brown unsigned long long c6; 202c98d5d94SLen Brown unsigned long long c7; 2030539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 204889facbeSLen Brown unsigned int core_temp_c; 2059392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 206c98d5d94SLen Brown unsigned int core_id; 207678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 208c98d5d94SLen Brown } *core_even, *core_odd; 209c98d5d94SLen Brown 210c98d5d94SLen Brown struct pkg_data { 211c98d5d94SLen Brown unsigned long long pc2; 212c98d5d94SLen Brown unsigned long long pc3; 213c98d5d94SLen Brown unsigned long long pc6; 214c98d5d94SLen Brown unsigned long long pc7; 215ca58710fSKristen Carlson Accardi unsigned long long pc8; 216ca58710fSKristen Carlson Accardi unsigned long long pc9; 217ca58710fSKristen Carlson Accardi unsigned long long pc10; 218be0e54c4SLen Brown unsigned long long cpu_lpi; 219be0e54c4SLen Brown unsigned long long sys_lpi; 2200b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2210b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2220b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2230b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2249185e988SLen Brown long long gfx_rc6_ms; 22527d47356SLen Brown unsigned int gfx_mhz; 226b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 227c98d5d94SLen Brown unsigned int package_id; 2287c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2297c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2307c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2317c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2327c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2337c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 234889facbeSLen Brown unsigned int pkg_temp_c; 235678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 236c98d5d94SLen Brown } *package_even, *package_odd; 237c98d5d94SLen Brown 238c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 239c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 240c98d5d94SLen Brown 24140f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 24240f5cfe7SPrarit Bhargava ((thread_base) + \ 24340f5cfe7SPrarit Bhargava ((pkg_no) * \ 24440f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 24540f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 24640f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 24740f5cfe7SPrarit Bhargava (thread_no)) 24840f5cfe7SPrarit Bhargava 24940f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 25040f5cfe7SPrarit Bhargava ((core_base) + \ 25140f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 25240f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 25340f5cfe7SPrarit Bhargava (core_no)) 25440f5cfe7SPrarit Bhargava 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 } 387388e9c81SLen Brown struct sys_counters { 388678a3bd1SLen Brown unsigned int added_thread_counters; 389678a3bd1SLen Brown unsigned int added_core_counters; 390678a3bd1SLen Brown unsigned int added_package_counters; 391388e9c81SLen Brown struct msr_counter *tp; 392388e9c81SLen Brown struct msr_counter *cp; 393388e9c81SLen Brown struct msr_counter *pp; 394388e9c81SLen Brown } sys; 395388e9c81SLen Brown 396c98d5d94SLen Brown struct system_summary { 397c98d5d94SLen Brown struct thread_data threads; 398c98d5d94SLen Brown struct core_data cores; 399c98d5d94SLen Brown struct pkg_data packages; 400388e9c81SLen Brown } average; 401c98d5d94SLen Brown 4020e2d8f05SLen Brown struct cpu_topology { 4030e2d8f05SLen Brown int physical_package_id; 4046de68fe1SLen Brown int die_id; 4050e2d8f05SLen Brown int logical_cpu_id; 406ef605741SPrarit Bhargava int physical_node_id; 407ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 4080e2d8f05SLen Brown int physical_core_id; 4098cb48b32SPrarit Bhargava int thread_id; 4100e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 4110e2d8f05SLen Brown } *cpus; 412c98d5d94SLen Brown 413c98d5d94SLen Brown struct topo_params { 414c98d5d94SLen Brown int num_packages; 4156de68fe1SLen Brown int num_die; 416103a8feaSLen Brown int num_cpus; 417c98d5d94SLen Brown int num_cores; 418c98d5d94SLen Brown int max_cpu_num; 419ef605741SPrarit Bhargava int max_node_num; 42070a9c6e8SPrarit Bhargava int nodes_per_pkg; 42170a9c6e8SPrarit Bhargava int cores_per_node; 42270a9c6e8SPrarit Bhargava int threads_per_core; 423c98d5d94SLen Brown } topo; 424103a8feaSLen Brown 425c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 426103a8feaSLen Brown 427562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 428562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 429562a2d37SLen Brown 430c98d5d94SLen Brown void setup_all_buffers(void); 431103a8feaSLen Brown 4321f81c5efSLen Brown char *sys_lpi_file; 4331f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 4341f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 4351f81c5efSLen Brown 436c98d5d94SLen Brown int cpu_is_not_present(int cpu) 437d15cf7c1SLen Brown { 438c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 439c98d5d94SLen Brown } 440c98d5d94SLen Brown /* 441c98d5d94SLen Brown * run func(thread, core, package) in topology order 442c98d5d94SLen Brown * skip non-present cpus 443c98d5d94SLen Brown */ 444c98d5d94SLen Brown 445c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 446c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 447c98d5d94SLen Brown { 44840f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 449c98d5d94SLen Brown 450c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 451df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 45270a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 453c98d5d94SLen Brown for (thread_no = 0; thread_no < 45470a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 455c98d5d94SLen Brown struct thread_data *t; 456c98d5d94SLen Brown struct core_data *c; 457c98d5d94SLen Brown struct pkg_data *p; 458c98d5d94SLen Brown 45940f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 46040f5cfe7SPrarit Bhargava core_no, node_no, 46140f5cfe7SPrarit Bhargava pkg_no); 462c98d5d94SLen Brown 463c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 464c98d5d94SLen Brown continue; 465c98d5d94SLen Brown 46640f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 46740f5cfe7SPrarit Bhargava node_no, pkg_no); 468c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 469c98d5d94SLen Brown 470c98d5d94SLen Brown retval = func(t, c, p); 471c98d5d94SLen Brown if (retval) 472c98d5d94SLen Brown return retval; 473c98d5d94SLen Brown } 474c98d5d94SLen Brown } 475c98d5d94SLen Brown } 47640f5cfe7SPrarit Bhargava } 477d15cf7c1SLen Brown return 0; 478d15cf7c1SLen Brown } 479d15cf7c1SLen Brown 48088c3281fSLen Brown int cpu_migrate(int cpu) 48188c3281fSLen Brown { 482c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 483c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 484c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 48588c3281fSLen Brown return -1; 48688c3281fSLen Brown else 48788c3281fSLen Brown return 0; 48888c3281fSLen Brown } 48936229897SLen Brown int get_msr_fd(int cpu) 490103a8feaSLen Brown { 491103a8feaSLen Brown char pathname[32]; 492103a8feaSLen Brown int fd; 493103a8feaSLen Brown 49436229897SLen Brown fd = fd_percpu[cpu]; 49536229897SLen Brown 49636229897SLen Brown if (fd) 49736229897SLen Brown return fd; 49836229897SLen Brown 499103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 500103a8feaSLen Brown fd = open(pathname, O_RDONLY); 50115aaa346SLen Brown if (fd < 0) 50298481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 503103a8feaSLen Brown 50436229897SLen Brown fd_percpu[cpu] = fd; 50536229897SLen Brown 50636229897SLen Brown return fd; 50736229897SLen Brown } 50836229897SLen Brown 5092af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 5102af4f9b8SLen Brown { 5112af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 5122af4f9b8SLen Brown } 5132af4f9b8SLen Brown 5142af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 5152af4f9b8SLen Brown { 5162af4f9b8SLen Brown struct perf_event_attr pea; 5172af4f9b8SLen Brown int fd; 5182af4f9b8SLen Brown 5192af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 5202af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 5212af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 5222af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 5232af4f9b8SLen Brown 5242af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 5252af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 5262af4f9b8SLen Brown if (fd == -1) 5272af4f9b8SLen Brown err(-1, "cpu%d: perf instruction counter\n", cpu_num); 5282af4f9b8SLen Brown 5292af4f9b8SLen Brown return fd; 5302af4f9b8SLen Brown } 5312af4f9b8SLen Brown 5322af4f9b8SLen Brown int get_instr_count_fd(int cpu) 5332af4f9b8SLen Brown { 5342af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 5352af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5362af4f9b8SLen Brown 5372af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 5382af4f9b8SLen Brown 5392af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5402af4f9b8SLen Brown } 5412af4f9b8SLen Brown 54236229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 54336229897SLen Brown { 54436229897SLen Brown ssize_t retval; 54536229897SLen Brown 54636229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 54715aaa346SLen Brown 54898481e79SLen Brown if (retval != sizeof *msr) 549cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 55015aaa346SLen Brown 55115aaa346SLen Brown return 0; 552103a8feaSLen Brown } 553103a8feaSLen Brown 554fc04cc67SLen Brown /* 555bdd5ae3aSLen Brown * This list matches the column headers, except 556bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 557bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 558bdd5ae3aSLen Brown * matching on them for --show and --hide. 559fc04cc67SLen Brown */ 560812db3f7SLen Brown struct msr_counter bic[] = { 5613f44a5c6SLen Brown { 0x0, "usec" }, 5623f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 563812db3f7SLen Brown { 0x0, "Package" }, 564bdd5ae3aSLen Brown { 0x0, "Node" }, 565812db3f7SLen Brown { 0x0, "Avg_MHz" }, 566bdd5ae3aSLen Brown { 0x0, "Busy%" }, 567812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 568812db3f7SLen Brown { 0x0, "TSC_MHz" }, 569812db3f7SLen Brown { 0x0, "IRQ" }, 570495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 571bdd5ae3aSLen Brown { 0x0, "sysfs" }, 572812db3f7SLen Brown { 0x0, "CPU%c1" }, 573812db3f7SLen Brown { 0x0, "CPU%c3" }, 574812db3f7SLen Brown { 0x0, "CPU%c6" }, 575812db3f7SLen Brown { 0x0, "CPU%c7" }, 576812db3f7SLen Brown { 0x0, "ThreadC" }, 577812db3f7SLen Brown { 0x0, "CoreTmp" }, 578812db3f7SLen Brown { 0x0, "CoreCnt" }, 579812db3f7SLen Brown { 0x0, "PkgTmp" }, 580812db3f7SLen Brown { 0x0, "GFX%rc6" }, 581812db3f7SLen Brown { 0x0, "GFXMHz" }, 582812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 583812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 584812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 585812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 5860f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 5870f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 5884bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 589be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 590be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 591812db3f7SLen Brown { 0x0, "PkgWatt" }, 592812db3f7SLen Brown { 0x0, "CorWatt" }, 593812db3f7SLen Brown { 0x0, "GFXWatt" }, 594812db3f7SLen Brown { 0x0, "PkgCnt" }, 595812db3f7SLen Brown { 0x0, "RAMWatt" }, 596812db3f7SLen Brown { 0x0, "PKG_%" }, 597812db3f7SLen Brown { 0x0, "RAM_%" }, 598812db3f7SLen Brown { 0x0, "Pkg_J" }, 599812db3f7SLen Brown { 0x0, "Cor_J" }, 600812db3f7SLen Brown { 0x0, "GFX_J" }, 601812db3f7SLen Brown { 0x0, "RAM_J" }, 6020539ba11SLen Brown { 0x0, "Mod%c6" }, 603a99d8730SLen Brown { 0x0, "Totl%C0" }, 604a99d8730SLen Brown { 0x0, "Any%C0" }, 605a99d8730SLen Brown { 0x0, "GFX%C0" }, 606a99d8730SLen Brown { 0x0, "CPUGFX%" }, 607bdd5ae3aSLen Brown { 0x0, "Core" }, 608bdd5ae3aSLen Brown { 0x0, "CPU" }, 6094c2122d4SLen Brown { 0x0, "APIC" }, 6104c2122d4SLen Brown { 0x0, "X2APIC" }, 6116de68fe1SLen Brown { 0x0, "Die" }, 612b4b91569SRafael Antognolli { 0x0, "GFXAMHz" }, 6132af4f9b8SLen Brown { 0x0, "IPC" }, 614812db3f7SLen Brown }; 615812db3f7SLen Brown 616812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 6173f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 6183f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 6193f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 620bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 621bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 622bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 623bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 624bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 625bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 626bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 627bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 628bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 629bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 630bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 631bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 632bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 633bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 634bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 635bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 636bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 637bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 638bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 639bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 640bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 641bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 642bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 643bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 644bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 645bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 646bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 647bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 648bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 649bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 650bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 651bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 652bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 653bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 654bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 655bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 656bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 657bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 658bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 659bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 660bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 661bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 662bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 663bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 664bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 6654c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 6664c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 6676de68fe1SLen Brown #define BIC_Die (1ULL << 50) 668b4b91569SRafael Antognolli #define BIC_GFXACTMHz (1ULL << 51) 6692af4f9b8SLen Brown #define BIC_IPC (1ULL << 52) 670812db3f7SLen Brown 6714c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 6723f44a5c6SLen Brown 6733f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 6744c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 675812db3f7SLen Brown 676812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 6771e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 6783f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 679812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 6800f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 6812af4f9b8SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 682812db3f7SLen Brown 6833f44a5c6SLen Brown 684dd778a5eSLen Brown #define MAX_DEFERRED 16 685dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 686dd778a5eSLen Brown int deferred_skip_index; 687dd778a5eSLen Brown 688dd778a5eSLen Brown /* 689dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 690dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 691dd778a5eSLen Brown */ 692dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 693dd778a5eSLen Brown 694dd778a5eSLen Brown void help(void) 695dd778a5eSLen Brown { 696dd778a5eSLen Brown fprintf(outf, 697dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 698dd778a5eSLen Brown "\n" 699dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 700dd778a5eSLen Brown "when COMMAND completes.\n" 701dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 702dd778a5eSLen Brown "to print statistics, until interrupted.\n" 703cc481650SNathan Ciobanu " -a, --add add a counter\n" 704dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 705cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 706dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7079ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7089ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7099ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7109ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7119ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7129ce80578SNathan Ciobanu " hide the specified column(s)\n" 713cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 714cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7159ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 716cc481650SNathan Ciobanu " -l, --list list column headers only\n" 717cc481650SNathan Ciobanu " -n, --num_iterations num\n" 718cc481650SNathan Ciobanu " number of the measurement iterations\n" 719cc481650SNathan Ciobanu " -o, --out file\n" 720cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 721cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7229ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7239ce80578SNathan Ciobanu " show only the specified column(s)\n" 7249ce80578SNathan Ciobanu " -S, --Summary\n" 7259ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7269ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7279ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7289ce80578SNathan Ciobanu " degrees Celsius\n" 729cc481650SNathan Ciobanu " -h, --help print this help message\n" 730cc481650SNathan Ciobanu " -v, --version print version information\n" 731dd778a5eSLen Brown "\n" 732dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 733dd778a5eSLen Brown } 734dd778a5eSLen Brown 735812db3f7SLen Brown /* 736812db3f7SLen Brown * bic_lookup 737812db3f7SLen Brown * for all the strings in comma separate name_list, 738812db3f7SLen Brown * set the approprate bit in return value. 739812db3f7SLen Brown */ 740dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 741812db3f7SLen Brown { 742812db3f7SLen Brown int i; 743812db3f7SLen Brown unsigned long long retval = 0; 744812db3f7SLen Brown 745812db3f7SLen Brown while (name_list) { 746812db3f7SLen Brown char *comma; 747812db3f7SLen Brown 748812db3f7SLen Brown comma = strchr(name_list, ','); 749812db3f7SLen Brown 750812db3f7SLen Brown if (comma) 751812db3f7SLen Brown *comma = '\0'; 752812db3f7SLen Brown 7533f44a5c6SLen Brown if (!strcmp(name_list, "all")) 7543f44a5c6SLen Brown return ~0; 7553f44a5c6SLen Brown 756812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 757812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 758812db3f7SLen Brown retval |= (1ULL << i); 759812db3f7SLen Brown break; 760812db3f7SLen Brown } 761812db3f7SLen Brown } 762812db3f7SLen Brown if (i == MAX_BIC) { 763dd778a5eSLen Brown if (mode == SHOW_LIST) { 764812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 765812db3f7SLen Brown exit(-1); 766812db3f7SLen Brown } 767dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 768dd778a5eSLen Brown if (debug) 769dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 770dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 771dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 772dd778a5eSLen Brown MAX_DEFERRED, name_list); 773dd778a5eSLen Brown help(); 774dd778a5eSLen Brown exit(1); 775dd778a5eSLen Brown } 776dd778a5eSLen Brown } 777812db3f7SLen Brown 778812db3f7SLen Brown name_list = comma; 779812db3f7SLen Brown if (name_list) 780812db3f7SLen Brown name_list++; 781812db3f7SLen Brown 782812db3f7SLen Brown } 783812db3f7SLen Brown return retval; 784812db3f7SLen Brown } 785fc04cc67SLen Brown 786dd778a5eSLen Brown 787c8ade361SLen Brown void print_header(char *delim) 788103a8feaSLen Brown { 789388e9c81SLen Brown struct msr_counter *mp; 7906168c2e0SLen Brown int printed = 0; 791388e9c81SLen Brown 7923f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 7933f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 7943f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 7953f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 796812db3f7SLen Brown if (DO_BIC(BIC_Package)) 7976168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 7986de68fe1SLen Brown if (DO_BIC(BIC_Die)) 7996de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 80001235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 80101235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 802812db3f7SLen Brown if (DO_BIC(BIC_Core)) 8036168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 804812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8056168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8064c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8074c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8084c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8094c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 810812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8116168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 812812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8136168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 814812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8156168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 816812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8176168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8181cc21f7bSLen Brown 8192af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8202af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8212af4f9b8SLen Brown 8220de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8230de6c0dfSLen Brown if (sums_need_wide_columns) 8246168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8250de6c0dfSLen Brown else 8266168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8270de6c0dfSLen Brown } 8280de6c0dfSLen Brown 829812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8306168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8311cc21f7bSLen Brown 832388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 833dd778a5eSLen Brown 834388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 835388e9c81SLen Brown if (mp->width == 64) 836dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 837388e9c81SLen Brown else 838dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 839388e9c81SLen Brown } else { 8400de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 841dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8420de6c0dfSLen Brown else 843dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 844388e9c81SLen Brown } 845388e9c81SLen Brown } 846388e9c81SLen Brown 84741618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8486168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 849562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8506168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 851812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8526168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 853812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8546168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 855678a3bd1SLen Brown 8560539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8576168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 858678a3bd1SLen Brown 859812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8606168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 861388e9c81SLen Brown 8629392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 8639392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8649392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 8659392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 8669392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8679392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 8689392bd98SCalvin Walton } 8699392bd98SCalvin Walton 870388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 871388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 872388e9c81SLen Brown if (mp->width == 64) 873c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 874388e9c81SLen Brown else 875c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 876388e9c81SLen Brown } else { 8770de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8780de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 8790de6c0dfSLen Brown else 880c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 881388e9c81SLen Brown } 882388e9c81SLen Brown } 883388e9c81SLen Brown 884812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 8856168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 886889facbeSLen Brown 887812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 8886168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 889fdf676e5SLen Brown 890812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 8916168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 89227d47356SLen Brown 893b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 894b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 895b4b91569SRafael Antognolli 896a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 8976168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 898a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 8996168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 900a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 9016168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 902a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 9036168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 9040b2bb692SLen Brown 9050f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9066168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9070f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9086168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9090f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9106168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9110f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9126168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9146168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9166168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9186168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 919be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 920be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 921be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 922be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 923103a8feaSLen Brown 9245c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 925812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9266168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9279392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9286168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 929812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9306168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 931812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9326168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 933812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9346168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 935812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9366168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 937d7899447SLen Brown } else if (do_rapl && rapl_joules) { 938812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9396168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9409392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9416168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 942812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9436168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 944812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9456168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 946812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9476168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 948812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9496168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9505c56be9aSDirk Brandewie } 951388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 952388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 953388e9c81SLen Brown if (mp->width == 64) 954c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 955388e9c81SLen Brown else 956c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 957388e9c81SLen Brown } else { 9580de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9590de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9600de6c0dfSLen Brown else 961c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 962388e9c81SLen Brown } 963388e9c81SLen Brown } 964388e9c81SLen Brown 965c98d5d94SLen Brown outp += sprintf(outp, "\n"); 966103a8feaSLen Brown } 967103a8feaSLen Brown 968c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 969c98d5d94SLen Brown struct pkg_data *p) 970103a8feaSLen Brown { 971388e9c81SLen Brown int i; 972388e9c81SLen Brown struct msr_counter *mp; 973388e9c81SLen Brown 9743b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 975c98d5d94SLen Brown 976c98d5d94SLen Brown if (t) { 9773b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 9783b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 9793b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 9803b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 9813b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 9823b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 9836886fee4SLen Brown 9842af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 9852af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 9862af4f9b8SLen Brown 987812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 9880de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 989812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 990218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 991388e9c81SLen Brown 992388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 993388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 994388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 995388e9c81SLen Brown } 996103a8feaSLen Brown } 997103a8feaSLen Brown 998c98d5d94SLen Brown if (c) { 9993b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 10003b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 10013b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 10023b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 10033b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 10049392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 1005388e9c81SLen Brown 1006388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1007388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 1008388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 1009388e9c81SLen Brown } 10100539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1011c98d5d94SLen Brown } 1012103a8feaSLen Brown 1013c98d5d94SLen Brown if (p) { 10143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10150b2bb692SLen Brown 10160b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10170b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10180b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10190b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10200b2bb692SLen Brown 10213b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10233b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10240f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10260f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10283b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10293b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10303b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1031be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1032be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10337c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10347c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10357c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10367c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 10377c2ccc50SChen Yu outp += sprintf(outp, "Throttle PKG: %0llX\n", 10383b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 10397c2ccc50SChen Yu outp += sprintf(outp, "Throttle RAM: %0llX\n", 10403b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 10413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1042388e9c81SLen Brown 1043388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1044388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 1045388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 1046388e9c81SLen Brown } 1047c98d5d94SLen Brown } 10483b4d5c7fSAndy Shevchenko 10493b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10503b4d5c7fSAndy Shevchenko 1051c98d5d94SLen Brown return 0; 1052103a8feaSLen Brown } 1053103a8feaSLen Brown 1054e23da037SLen Brown /* 1055e23da037SLen Brown * column formatting convention & formats 1056e23da037SLen Brown */ 1057c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 1058c98d5d94SLen Brown struct pkg_data *p) 1059103a8feaSLen Brown { 1060008d396eSLen Brown double interval_float, tsc; 1061fc04cc67SLen Brown char *fmt8; 1062388e9c81SLen Brown int i; 1063388e9c81SLen Brown struct msr_counter *mp; 10646168c2e0SLen Brown char *delim = "\t"; 10656168c2e0SLen Brown int printed = 0; 1066103a8feaSLen Brown 1067c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1068c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1069c98d5d94SLen Brown return 0; 1070c98d5d94SLen Brown 1071c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1072c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1073c98d5d94SLen Brown return 0; 1074c98d5d94SLen Brown 10751ef7d21aSLen Brown /*if not summary line and --cpu is used */ 10761ef7d21aSLen Brown if ((t != &average.threads) && 10771ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 10781ef7d21aSLen Brown return 0; 10791ef7d21aSLen Brown 10803f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1081f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1082f4fdf2b4SLen Brown struct timeval tv; 1083f4fdf2b4SLen Brown 1084f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1085f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1086f4fdf2b4SLen Brown } 1087f4fdf2b4SLen Brown 10883f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 10893f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 10903f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 10913f44a5c6SLen Brown 1092d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0; 1093103a8feaSLen Brown 1094008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1095008d396eSLen Brown 1096c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1097c98d5d94SLen Brown if (t == &average.threads) { 1098812db3f7SLen Brown if (DO_BIC(BIC_Package)) 10996168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11006de68fe1SLen Brown if (DO_BIC(BIC_Die)) 11016de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 110201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 110301235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1104812db3f7SLen Brown if (DO_BIC(BIC_Core)) 11056168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1106812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11076168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11084c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11094c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11104c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11114c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1112103a8feaSLen Brown } else { 1113812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1114c98d5d94SLen Brown if (p) 11156168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1116c98d5d94SLen Brown else 11176168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1118c98d5d94SLen Brown } 11196de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11206de68fe1SLen Brown if (c) 11216de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11226de68fe1SLen Brown else 11236de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11246de68fe1SLen Brown } 112501235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 112601235041SPrarit Bhargava if (t) 112701235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 112801235041SPrarit Bhargava (printed++ ? delim : ""), 112901235041SPrarit Bhargava cpus[t->cpu_id].physical_node_id); 113001235041SPrarit Bhargava else 113101235041SPrarit Bhargava outp += sprintf(outp, "%s-", 113201235041SPrarit Bhargava (printed++ ? delim : "")); 113301235041SPrarit Bhargava } 1134812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1135c98d5d94SLen Brown if (c) 11366168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1137c98d5d94SLen Brown else 11386168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1139c98d5d94SLen Brown } 1140812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11416168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11424c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11434c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11444c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11454c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1146103a8feaSLen Brown } 1147fc04cc67SLen Brown 1148812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 11496168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1150fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 1151fc04cc67SLen Brown 1152812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11536168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 1154103a8feaSLen Brown 1155812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 115621ed5574SLen Brown if (has_base_hz) 11576168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 115821ed5574SLen Brown else 11596168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1160008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 116121ed5574SLen Brown } 1162103a8feaSLen Brown 1163812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 11646168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 1165103a8feaSLen Brown 11662af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 11672af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 11682af4f9b8SLen Brown 1169562a2d37SLen Brown /* IRQ */ 11700de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 11710de6c0dfSLen Brown if (sums_need_wide_columns) 11726168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 11730de6c0dfSLen Brown else 11746168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 11750de6c0dfSLen Brown } 1176562a2d37SLen Brown 11771cc21f7bSLen Brown /* SMI */ 1178812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 11796168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 11801cc21f7bSLen Brown 1181678a3bd1SLen Brown /* Added counters */ 1182678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1183678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1184678a3bd1SLen Brown if (mp->width == 32) 11855f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 1186678a3bd1SLen Brown else 11876168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1188678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 11890de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11906168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 11910de6c0dfSLen Brown else 11926168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1193678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 119441618e63SLen Brown if (mp->type == COUNTER_USEC) 11956168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 119641618e63SLen Brown else 11976168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 1198678a3bd1SLen Brown } 1199678a3bd1SLen Brown } 1200678a3bd1SLen Brown 120141618e63SLen Brown /* C1 */ 120241618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 12036168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 120441618e63SLen Brown 120541618e63SLen Brown 1206c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1207c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1208c98d5d94SLen Brown goto done; 1209c98d5d94SLen Brown 1210562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 12116168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 1212812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 12136168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 1214812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12156168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 1216c98d5d94SLen Brown 12170539ba11SLen Brown /* Mod%c6 */ 12180539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12196168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12200539ba11SLen Brown 1221812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12226168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1223889facbeSLen Brown 1224388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1225388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1226388e9c81SLen Brown if (mp->width == 32) 12275f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 1228388e9c81SLen Brown else 12296168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1230388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12310de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12326168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12330de6c0dfSLen Brown else 12346168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1235388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 1237388e9c81SLen Brown } 1238388e9c81SLen Brown } 1239388e9c81SLen Brown 12409392bd98SCalvin Walton fmt8 = "%s%.2f"; 12419392bd98SCalvin Walton 12429392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12439392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12449392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12459392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12469392bd98SCalvin Walton 1247c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1248c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1249c98d5d94SLen Brown goto done; 1250c98d5d94SLen Brown 12510b2bb692SLen Brown /* PkgTmp */ 1252812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 12536168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1254889facbeSLen Brown 1255fdf676e5SLen Brown /* GFXrc6 */ 1256812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1257ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 12586168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 12599185e988SLen Brown } else { 12606168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12619185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 12629185e988SLen Brown } 12639185e988SLen Brown } 1264fdf676e5SLen Brown 126527d47356SLen Brown /* GFXMHz */ 1266812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 12676168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 126827d47356SLen Brown 1269b4b91569SRafael Antognolli /* GFXACTMHz */ 1270b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1271b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1272b4b91569SRafael Antognolli 12730b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1274a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12756168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 1276a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12776168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 1278a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12796168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 1280a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12816168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 12820b2bb692SLen Brown 12830f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 12846168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 12850f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 12866168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 12870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 12886168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 12890f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 12906168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 12910f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 12926168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 12930f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 12946168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 12950f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 12966168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 1297889facbeSLen Brown 1298be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1299be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1300be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1301be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1302be0e54c4SLen Brown 1303812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 13046168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 13059392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13066168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1307812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 13086168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1309812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 13106168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 1311812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13126168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13139392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13146168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1315812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13166168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1317812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13186168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1319812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 13206168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1321812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 13226168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1323812db3f7SLen Brown 1324388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1325388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1326388e9c81SLen Brown if (mp->width == 32) 13275f3aea57SLen Brown outp += 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 } 1361b7d8c148SLen Brown void flush_output_stderr(void) 1362c98d5d94SLen Brown { 1363b7d8c148SLen Brown fputs(output_buffer, outf); 1364b7d8c148SLen Brown fflush(outf); 1365c98d5d94SLen Brown outp = output_buffer; 1366c98d5d94SLen Brown } 1367c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1368c98d5d94SLen Brown { 1369e23da037SLen Brown static int printed; 1370103a8feaSLen Brown 1371e23da037SLen Brown if (!printed || !summary_only) 1372c8ade361SLen Brown print_header("\t"); 1373103a8feaSLen Brown 13749d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1375103a8feaSLen Brown 1376e23da037SLen Brown printed = 1; 1377e23da037SLen Brown 1378e23da037SLen Brown if (summary_only) 1379e23da037SLen Brown return; 1380e23da037SLen Brown 1381c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1382103a8feaSLen Brown } 1383103a8feaSLen Brown 1384889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 13857c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1386889facbeSLen Brown 1387ba3dec99SLen Brown int 1388c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1389103a8feaSLen Brown { 1390388e9c81SLen Brown int i; 1391388e9c81SLen Brown struct msr_counter *mp; 13920b2bb692SLen Brown 1393a99d8730SLen 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 1443c98d5d94SLen Brown void 1444c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1445c98d5d94SLen Brown { 1446388e9c81SLen Brown int i; 1447388e9c81SLen Brown struct msr_counter *mp; 1448388e9c81SLen Brown 1449c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1450c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1451c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1452889facbeSLen Brown old->core_temp_c = new->core_temp_c; 14530539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1454388e9c81SLen Brown 14559392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 14569392bd98SCalvin Walton 1457388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1458388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1459388e9c81SLen Brown old->counter[i] = new->counter[i]; 1460388e9c81SLen Brown else 1461388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1462388e9c81SLen Brown } 1463103a8feaSLen Brown } 1464c98d5d94SLen Brown 14651e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 14661e9042b9SSrinivas Pandruvada { 14671e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 14681e9042b9SSrinivas Pandruvada return 0; 14691e9042b9SSrinivas Pandruvada 14701e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 14711e9042b9SSrinivas Pandruvada } 14721e9042b9SSrinivas Pandruvada 1473c3ae331dSLen Brown /* 1474c3ae331dSLen Brown * old = new - old 1475c3ae331dSLen Brown */ 1476ba3dec99SLen Brown int 1477c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1478c98d5d94SLen Brown struct core_data *core_delta) 1479c98d5d94SLen Brown { 1480388e9c81SLen Brown int i; 1481388e9c81SLen Brown struct msr_counter *mp; 1482388e9c81SLen Brown 14834c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 14844c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14854c2122d4SLen Brown new->apic_id = old->apic_id; 14864c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14874c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 14884c2122d4SLen Brown 14893f44a5c6SLen Brown /* 14903f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 14913f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 14923f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 14933f44a5c6SLen Brown */ 14943f44a5c6SLen Brown 1495d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 14963f44a5c6SLen Brown old->tv_begin = new->tv_begin; 14973f44a5c6SLen Brown old->tv_end = new->tv_end; 14983f44a5c6SLen Brown 1499c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1500c98d5d94SLen Brown 1501103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1502b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1503b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1504b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1505b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1506103a8feaSLen Brown 1507c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1508c98d5d94SLen Brown 15091e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 15101e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 1511c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1512c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1513c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1514c98d5d94SLen Brown } else { 1515ba3dec99SLen Brown return -1; 1516103a8feaSLen Brown } 1517a729617cSLen Brown } 1518103a8feaSLen Brown 1519c98d5d94SLen Brown 1520144b44b1SLen Brown if (use_c1_residency_msr) { 1521144b44b1SLen Brown /* 1522144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1523144b44b1SLen Brown * which should be more accurate than the derivation below. 1524144b44b1SLen Brown */ 1525144b44b1SLen Brown } else { 1526103a8feaSLen Brown /* 1527c3ae331dSLen Brown * As counter collection is not atomic, 1528c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1529103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1530103a8feaSLen Brown */ 153195149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1532c98d5d94SLen Brown old->c1 = 0; 1533c98d5d94SLen Brown else { 1534c98d5d94SLen Brown /* normal case, derive c1 */ 1535008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1536c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1537c98d5d94SLen Brown } 1538144b44b1SLen Brown } 1539c3ae331dSLen Brown 1540c98d5d94SLen Brown if (old->mperf == 0) { 1541b7d8c148SLen Brown if (debug > 1) 1542b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1543c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1544c98d5d94SLen Brown } 1545103a8feaSLen Brown 15462af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 15472af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 15482af4f9b8SLen Brown 1549812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1550562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1551562a2d37SLen Brown 1552812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 15531ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1554ba3dec99SLen Brown 1555388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1556388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1557388e9c81SLen Brown old->counter[i] = new->counter[i]; 1558388e9c81SLen Brown else 1559388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1560388e9c81SLen Brown } 1561ba3dec99SLen Brown return 0; 1562103a8feaSLen Brown } 1563c98d5d94SLen Brown 1564c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1565c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1566c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1567c98d5d94SLen Brown { 1568ba3dec99SLen Brown int retval = 0; 1569ba3dec99SLen Brown 1570c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1571c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1572c98d5d94SLen Brown delta_core(c, c2); 1573c98d5d94SLen Brown 1574c98d5d94SLen Brown /* always calculate thread delta */ 1575ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1576ba3dec99SLen Brown if (retval) 1577ba3dec99SLen Brown return retval; 1578c98d5d94SLen Brown 1579c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1580c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1581ba3dec99SLen Brown retval = delta_package(p, p2); 1582c98d5d94SLen Brown 1583ba3dec99SLen Brown return retval; 1584103a8feaSLen Brown } 1585103a8feaSLen Brown 1586c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1587103a8feaSLen Brown { 1588388e9c81SLen Brown int i; 1589388e9c81SLen Brown struct msr_counter *mp; 1590388e9c81SLen Brown 15913f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 15923f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 15933f44a5c6SLen Brown t->tv_end.tv_sec = 0; 15943f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1595d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1596d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 15973f44a5c6SLen Brown 1598c98d5d94SLen Brown t->tsc = 0; 1599c98d5d94SLen Brown t->aperf = 0; 1600c98d5d94SLen Brown t->mperf = 0; 1601c98d5d94SLen Brown t->c1 = 0; 1602103a8feaSLen Brown 16032af4f9b8SLen Brown t->instr_count = 0; 16042af4f9b8SLen Brown 1605562a2d37SLen Brown t->irq_count = 0; 1606562a2d37SLen Brown t->smi_count = 0; 1607562a2d37SLen Brown 1608c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1609c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1610c98d5d94SLen Brown 1611c98d5d94SLen Brown c->c3 = 0; 1612c98d5d94SLen Brown c->c6 = 0; 1613c98d5d94SLen Brown c->c7 = 0; 16140539ba11SLen Brown c->mc6_us = 0; 1615889facbeSLen Brown c->core_temp_c = 0; 16169392bd98SCalvin Walton c->core_energy = 0; 1617c98d5d94SLen Brown 16180b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16190b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16200b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16210b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16220b2bb692SLen Brown 1623c98d5d94SLen Brown p->pc2 = 0; 16240f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1625c98d5d94SLen Brown p->pc3 = 0; 16260f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1627c98d5d94SLen Brown p->pc6 = 0; 16280f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1629c98d5d94SLen Brown p->pc7 = 0; 1630ca58710fSKristen Carlson Accardi p->pc8 = 0; 1631ca58710fSKristen Carlson Accardi p->pc9 = 0; 1632ca58710fSKristen Carlson Accardi p->pc10 = 0; 1633be0e54c4SLen Brown p->cpu_lpi = 0; 1634be0e54c4SLen Brown p->sys_lpi = 0; 1635889facbeSLen Brown 1636889facbeSLen Brown p->energy_pkg = 0; 1637889facbeSLen Brown p->energy_dram = 0; 1638889facbeSLen Brown p->energy_cores = 0; 1639889facbeSLen Brown p->energy_gfx = 0; 1640889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1641889facbeSLen Brown p->rapl_dram_perf_status = 0; 1642889facbeSLen Brown p->pkg_temp_c = 0; 164327d47356SLen Brown 1644fdf676e5SLen Brown p->gfx_rc6_ms = 0; 164527d47356SLen Brown p->gfx_mhz = 0; 1646b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1647388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1648388e9c81SLen Brown t->counter[i] = 0; 1649388e9c81SLen Brown 1650388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1651388e9c81SLen Brown c->counter[i] = 0; 1652388e9c81SLen Brown 1653388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1654388e9c81SLen Brown p->counter[i] = 0; 1655103a8feaSLen Brown } 1656c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1657c98d5d94SLen Brown struct pkg_data *p) 1658103a8feaSLen Brown { 1659388e9c81SLen Brown int i; 1660388e9c81SLen Brown struct msr_counter *mp; 1661388e9c81SLen Brown 16624c2122d4SLen Brown /* copy un-changing apic_id's */ 16634c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 16644c2122d4SLen Brown average.threads.apic_id = t->apic_id; 16654c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 16664c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 16674c2122d4SLen Brown 16683f44a5c6SLen Brown /* remember first tv_begin */ 16693f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 16703f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 16713f44a5c6SLen Brown 16723f44a5c6SLen Brown /* remember last tv_end */ 16733f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 16743f44a5c6SLen Brown 1675c98d5d94SLen Brown average.threads.tsc += t->tsc; 1676c98d5d94SLen Brown average.threads.aperf += t->aperf; 1677c98d5d94SLen Brown average.threads.mperf += t->mperf; 1678c98d5d94SLen Brown average.threads.c1 += t->c1; 167915aaa346SLen Brown 16802af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 16812af4f9b8SLen Brown 1682562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1683562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1684562a2d37SLen Brown 1685388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1686388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1687388e9c81SLen Brown continue; 1688388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1689388e9c81SLen Brown } 1690388e9c81SLen Brown 1691c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1692c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1693c98d5d94SLen Brown return 0; 1694c98d5d94SLen Brown 1695c98d5d94SLen Brown average.cores.c3 += c->c3; 1696c98d5d94SLen Brown average.cores.c6 += c->c6; 1697c98d5d94SLen Brown average.cores.c7 += c->c7; 16980539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1699c98d5d94SLen Brown 1700889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1701889facbeSLen Brown 17029392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 17039392bd98SCalvin Walton 1704388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1705388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1706388e9c81SLen Brown continue; 1707388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1708388e9c81SLen Brown } 1709388e9c81SLen Brown 1710c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1711c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1712c98d5d94SLen Brown return 0; 1713c98d5d94SLen Brown 1714a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17150b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1716a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17170b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1718a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17190b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1720a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17210b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17220b2bb692SLen Brown 1723c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17240f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1725c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17260f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1727c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17280f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1729c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1730ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1731ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1732ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1733c98d5d94SLen Brown 1734be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1735be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1736be0e54c4SLen Brown 1737889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1738889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1739889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1740889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1741889facbeSLen Brown 1742fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 174327d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1744b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 174527d47356SLen Brown 1746889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1747889facbeSLen Brown 1748889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1749889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1750388e9c81SLen Brown 1751388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1752388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1753388e9c81SLen Brown continue; 1754388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1755388e9c81SLen Brown } 1756c98d5d94SLen Brown return 0; 1757c98d5d94SLen Brown } 1758c98d5d94SLen Brown /* 1759c98d5d94SLen Brown * sum the counters for all cpus in the system 1760c98d5d94SLen Brown * compute the weighted average 1761c98d5d94SLen Brown */ 1762c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1763c98d5d94SLen Brown struct pkg_data *p) 1764c98d5d94SLen Brown { 1765388e9c81SLen Brown int i; 1766388e9c81SLen Brown struct msr_counter *mp; 1767388e9c81SLen Brown 1768c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1769c98d5d94SLen Brown 1770c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1771c98d5d94SLen Brown 1772d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1773d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1774d4794f25SYazen Ghannam 1775c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1776c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1777c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 17782af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1779c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1780c98d5d94SLen Brown 17810de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 17820de6c0dfSLen Brown sums_need_wide_columns = 1; 17830de6c0dfSLen Brown 1784c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1785c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1786c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 17870539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1788c98d5d94SLen Brown 1789a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17900b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1791a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17920b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1793a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17940b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1795a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17960b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 17970b2bb692SLen Brown 1798c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 17990f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1800c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 18010f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1802c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 18030f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1804c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1805ca58710fSKristen Carlson Accardi 1806ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1807ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1808ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1809388e9c81SLen Brown 1810388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1811388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1812388e9c81SLen Brown continue; 18130de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18140de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18150de6c0dfSLen Brown sums_need_wide_columns = 1; 181641618e63SLen Brown continue; 18170de6c0dfSLen Brown } 1818388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1819388e9c81SLen Brown } 1820388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1821388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1822388e9c81SLen Brown continue; 18230de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18240de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18250de6c0dfSLen Brown sums_need_wide_columns = 1; 18260de6c0dfSLen Brown } 1827388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1828388e9c81SLen Brown } 1829388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1830388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1831388e9c81SLen Brown continue; 18320de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18330de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18340de6c0dfSLen Brown sums_need_wide_columns = 1; 18350de6c0dfSLen Brown } 1836388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1837388e9c81SLen Brown } 1838c98d5d94SLen Brown } 1839c98d5d94SLen Brown 1840c98d5d94SLen Brown static unsigned long long rdtsc(void) 1841c98d5d94SLen Brown { 1842c98d5d94SLen Brown unsigned int low, high; 1843c98d5d94SLen Brown 1844c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1845c98d5d94SLen Brown 1846c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1847c98d5d94SLen Brown } 1848c98d5d94SLen Brown 1849c98d5d94SLen Brown /* 1850495c7654SLen Brown * Open a file, and exit on failure 1851495c7654SLen Brown */ 1852495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1853495c7654SLen Brown { 1854495c7654SLen Brown FILE *filep = fopen(path, mode); 1855495c7654SLen Brown 1856495c7654SLen Brown if (!filep) 1857495c7654SLen Brown err(1, "%s: open failed", path); 1858495c7654SLen Brown return filep; 1859495c7654SLen Brown } 1860495c7654SLen Brown /* 1861495c7654SLen Brown * snapshot_sysfs_counter() 1862495c7654SLen Brown * 1863495c7654SLen Brown * return snapshot of given counter 1864495c7654SLen Brown */ 1865495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1866495c7654SLen Brown { 1867495c7654SLen Brown FILE *fp; 1868495c7654SLen Brown int retval; 1869495c7654SLen Brown unsigned long long counter; 1870495c7654SLen Brown 1871495c7654SLen Brown fp = fopen_or_die(path, "r"); 1872495c7654SLen Brown 1873495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1874495c7654SLen Brown if (retval != 1) 1875495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1876495c7654SLen Brown 1877495c7654SLen Brown fclose(fp); 1878495c7654SLen Brown 1879495c7654SLen Brown return counter; 1880495c7654SLen Brown } 1881495c7654SLen Brown 1882495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1883495c7654SLen Brown { 1884495c7654SLen Brown if (mp->msr_num != 0) { 1885495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1886495c7654SLen Brown return -1; 1887495c7654SLen Brown } else { 188846c27978SLen Brown char path[128 + PATH_BYTES]; 188941618e63SLen Brown 189041618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 189141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 189241618e63SLen Brown cpu, mp->path); 189341618e63SLen Brown 189441618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 189541618e63SLen Brown } else { 1896495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1897495c7654SLen Brown } 189841618e63SLen Brown } 1899495c7654SLen Brown 1900495c7654SLen Brown return 0; 1901495c7654SLen Brown } 1902495c7654SLen Brown 19036d6501d9SBorislav Petkov int get_epb(int cpu) 19046d6501d9SBorislav Petkov { 19056d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19067f1b11baSBorislav Petkov unsigned long long msr; 19076d6501d9SBorislav Petkov int ret, epb = -1; 19086d6501d9SBorislav Petkov FILE *fp; 19096d6501d9SBorislav Petkov 19106d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19116d6501d9SBorislav Petkov 19127f1b11baSBorislav Petkov fp = fopen(path, "r"); 19137f1b11baSBorislav Petkov if (!fp) 19147f1b11baSBorislav Petkov goto msr_fallback; 19156d6501d9SBorislav Petkov 19166d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19176d6501d9SBorislav Petkov if (ret != 1) 19186d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19196d6501d9SBorislav Petkov 19206d6501d9SBorislav Petkov fclose(fp); 19216d6501d9SBorislav Petkov 19226d6501d9SBorislav Petkov return epb; 19237f1b11baSBorislav Petkov 19247f1b11baSBorislav Petkov msr_fallback: 19257f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 19267f1b11baSBorislav Petkov 19277f1b11baSBorislav Petkov return msr & 0xf; 19286d6501d9SBorislav Petkov } 19296d6501d9SBorislav Petkov 19304c2122d4SLen Brown void get_apic_id(struct thread_data *t) 19314c2122d4SLen Brown { 193234041551SLen Brown unsigned int eax, ebx, ecx, edx; 193334041551SLen Brown 193434041551SLen Brown if (DO_BIC(BIC_APIC)) { 193534041551SLen Brown eax = ebx = ecx = edx = 0; 193634041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 193734041551SLen Brown 193834041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 193934041551SLen Brown } 194034041551SLen Brown 194134041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 194234041551SLen Brown return; 194334041551SLen Brown 1944c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 194534041551SLen Brown unsigned int topology_extensions; 194634041551SLen Brown 194734041551SLen Brown if (max_extended_level < 0x8000001e) 194834041551SLen Brown return; 19494c2122d4SLen Brown 19504c2122d4SLen Brown eax = ebx = ecx = edx = 0; 195134041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 195234041551SLen Brown topology_extensions = ecx & (1 << 22); 195334041551SLen Brown 195434041551SLen Brown if (topology_extensions == 0) 195534041551SLen Brown return; 195634041551SLen Brown 195734041551SLen Brown eax = ebx = ecx = edx = 0; 195834041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 195934041551SLen Brown 196034041551SLen Brown t->x2apic_id = eax; 196134041551SLen Brown return; 196234041551SLen Brown } 19634c2122d4SLen Brown 19644c2122d4SLen Brown if (!genuine_intel) 19654c2122d4SLen Brown return; 19664c2122d4SLen Brown 19674c2122d4SLen Brown if (max_level < 0xb) 19684c2122d4SLen Brown return; 19694c2122d4SLen Brown 19704c2122d4SLen Brown ecx = 0; 19714c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 19724c2122d4SLen Brown t->x2apic_id = edx; 19734c2122d4SLen Brown 197434041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 197534041551SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", 197634041551SLen Brown t->cpu_id, t->apic_id, t->x2apic_id); 19774c2122d4SLen Brown } 19784c2122d4SLen Brown 1979495c7654SLen Brown /* 1980c98d5d94SLen Brown * get_counters(...) 1981c98d5d94SLen Brown * migrate to cpu 1982c98d5d94SLen Brown * acquire and record local counters for that cpu 1983c98d5d94SLen Brown */ 1984c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1985c98d5d94SLen Brown { 1986c98d5d94SLen Brown int cpu = t->cpu_id; 1987889facbeSLen Brown unsigned long long msr; 19880102b067SLen Brown int aperf_mperf_retry_count = 0; 1989388e9c81SLen Brown struct msr_counter *mp; 1990388e9c81SLen Brown int i; 1991c98d5d94SLen Brown 1992e52966c0SLen Brown if (cpu_migrate(cpu)) { 19933d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 199415aaa346SLen Brown return -1; 1995e52966c0SLen Brown } 199615aaa346SLen Brown 1997d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1998d4794f25SYazen Ghannam 19994c2122d4SLen Brown if (first_counter_read) 20004c2122d4SLen Brown get_apic_id(t); 20010102b067SLen Brown retry: 2002c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 200315aaa346SLen Brown 20041e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 20051e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 20060102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 20070102b067SLen Brown 20080102b067SLen Brown /* 20090102b067SLen Brown * The TSC, APERF and MPERF must be read together for 20100102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 20110102b067SLen Brown * 20120102b067SLen Brown * Unfortunately, APERF and MPERF are read by 20130102b067SLen Brown * individual system call, so delays may occur 20140102b067SLen Brown * between them. If the time to read them 20150102b067SLen Brown * varies by a large amount, we re-read them. 20160102b067SLen Brown */ 20170102b067SLen Brown 20180102b067SLen Brown /* 20190102b067SLen Brown * This initial dummy APERF read has been seen to 20200102b067SLen Brown * reduce jitter in the subsequent reads. 20210102b067SLen Brown */ 20220102b067SLen Brown 20239c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2024c98d5d94SLen Brown return -3; 20250102b067SLen Brown 20260102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 20270102b067SLen Brown 20280102b067SLen Brown tsc_before = t->tsc; 20290102b067SLen Brown 20300102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 20310102b067SLen Brown return -3; 20320102b067SLen Brown 20330102b067SLen Brown tsc_between = rdtsc(); 20340102b067SLen Brown 20359c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2036c98d5d94SLen Brown return -4; 20370102b067SLen Brown 20380102b067SLen Brown tsc_after = rdtsc(); 20390102b067SLen Brown 20400102b067SLen Brown aperf_time = tsc_between - tsc_before; 20410102b067SLen Brown mperf_time = tsc_after - tsc_between; 20420102b067SLen Brown 20430102b067SLen Brown /* 20440102b067SLen Brown * If the system call latency to read APERF and MPERF 20450102b067SLen Brown * differ by more than 2x, then try again. 20460102b067SLen Brown */ 20470102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 20480102b067SLen Brown aperf_mperf_retry_count++; 20490102b067SLen Brown if (aperf_mperf_retry_count < 5) 20500102b067SLen Brown goto retry; 20510102b067SLen Brown else 20520102b067SLen Brown warnx("cpu%d jitter %lld %lld", 20530102b067SLen Brown cpu, aperf_time, mperf_time); 20540102b067SLen Brown } 20550102b067SLen Brown aperf_mperf_retry_count = 0; 20560102b067SLen Brown 2057b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2058b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 205988c3281fSLen Brown } 206088c3281fSLen Brown 20612af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 20622af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 20632af4f9b8SLen Brown return -4; 20642af4f9b8SLen Brown 2065812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2066562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2067812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 20681ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 20691ed51011SLen Brown return -5; 20701ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 20711ed51011SLen Brown } 20720539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2073144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2074144b44b1SLen Brown return -6; 2075144b44b1SLen Brown } 2076144b44b1SLen Brown 2077388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2078495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2079388e9c81SLen Brown return -10; 2080388e9c81SLen Brown } 2081388e9c81SLen Brown 2082c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2083c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2084f4fdf2b4SLen Brown goto done; 2085c98d5d94SLen Brown 20861e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2087c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2088c98d5d94SLen Brown return -6; 2089144b44b1SLen Brown } 2090144b44b1SLen Brown 20911e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2092c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2093c98d5d94SLen Brown return -7; 20941e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2095fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2096fb5d4327SDasaratharaman Chandramouli return -7; 2097103a8feaSLen Brown } 209815aaa346SLen Brown 20997ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2100c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2101c98d5d94SLen Brown return -8; 21027ab5ff49SZhang Rui else if (t->is_atom) { 21037ab5ff49SZhang Rui /* 21047ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 21057ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 21067ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 21077ab5ff49SZhang Rui * accturate cc6 residency. 21087ab5ff49SZhang Rui */ 21097ab5ff49SZhang Rui c->c6 -= c->c7; 21107ab5ff49SZhang Rui } 21117ab5ff49SZhang Rui } 2112c98d5d94SLen Brown 21130539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 21140539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 21150539ba11SLen Brown return -8; 21160539ba11SLen Brown 2117812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2118889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2119889facbeSLen Brown return -9; 2120889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2121889facbeSLen Brown } 2122889facbeSLen Brown 21239392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21249392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 21259392bd98SCalvin Walton return -14; 21269392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 21279392bd98SCalvin Walton } 21289392bd98SCalvin Walton 2129388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2130495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2131388e9c81SLen Brown return -10; 2132388e9c81SLen Brown } 2133889facbeSLen Brown 2134c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2135c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2136f4fdf2b4SLen Brown goto done; 213715aaa346SLen Brown 2138a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 21390b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 21400b2bb692SLen Brown return -10; 2141a99d8730SLen Brown } 2142a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 21430b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 21440b2bb692SLen Brown return -11; 2145a99d8730SLen Brown } 2146a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 21470b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 21480b2bb692SLen Brown return -12; 2149a99d8730SLen Brown } 2150a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 21510b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 21520b2bb692SLen Brown return -13; 21530b2bb692SLen Brown } 21540f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2155c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2156c98d5d94SLen Brown return -9; 21570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 21580539ba11SLen Brown if (do_slm_cstates) { 21590539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 21600539ba11SLen Brown return -10; 21610539ba11SLen Brown } else { 2162c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2163c98d5d94SLen Brown return -10; 21640539ba11SLen Brown } 21650539ba11SLen Brown } 21660539ba11SLen Brown 21670f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2168c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2169c98d5d94SLen Brown return -11; 21700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2171c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2172c98d5d94SLen Brown return -12; 21730f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2174ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2175ca58710fSKristen Carlson Accardi return -13; 21760f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2177ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2178ca58710fSKristen Carlson Accardi return -13; 21790f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2180ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2181ca58710fSKristen Carlson Accardi return -13; 21820f47c08dSLen Brown 2183be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2184be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2185be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2186be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2187be0e54c4SLen Brown 2188889facbeSLen Brown if (do_rapl & RAPL_PKG) { 21899972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2190889facbeSLen Brown return -13; 21919972d5d8SChen Yu p->energy_pkg = msr; 2192889facbeSLen Brown } 21939148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 21949972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2195889facbeSLen Brown return -14; 21969972d5d8SChen Yu p->energy_cores = msr; 2197889facbeSLen Brown } 2198889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 21999972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2200889facbeSLen Brown return -15; 22019972d5d8SChen Yu p->energy_dram = msr; 2202889facbeSLen Brown } 2203889facbeSLen Brown if (do_rapl & RAPL_GFX) { 22049972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2205889facbeSLen Brown return -16; 22069972d5d8SChen Yu p->energy_gfx = msr; 2207889facbeSLen Brown } 2208889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 22099972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2210889facbeSLen Brown return -16; 22119972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2212889facbeSLen Brown } 2213889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 22149972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2215889facbeSLen Brown return -16; 22169972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2217889facbeSLen Brown } 22183316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22199972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 22203316f99aSCalvin Walton return -13; 22219972d5d8SChen Yu p->energy_pkg = msr; 22223316f99aSCalvin Walton } 2223812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2224889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2225889facbeSLen Brown return -17; 2226889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2227889facbeSLen Brown } 2228fdf676e5SLen Brown 2229812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2230fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2231fdf676e5SLen Brown 2232812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 223327d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 223427d47356SLen Brown 2235b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2236b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2237b4b91569SRafael Antognolli 2238388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2239495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2240388e9c81SLen Brown return -10; 2241388e9c81SLen Brown } 2242f4fdf2b4SLen Brown done: 2243f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2244388e9c81SLen Brown 224515aaa346SLen Brown return 0; 2246103a8feaSLen Brown } 2247103a8feaSLen Brown 2248ee7e38e3SLen Brown /* 2249ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2250ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2251ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2252ee7e38e3SLen Brown */ 2253ee7e38e3SLen Brown 2254ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2255ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2256ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2257ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2258ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2259ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2260ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2261ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2262ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2263ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2264ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2265ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 22660b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 22670b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2268445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2269445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2270ee7e38e3SLen Brown 2271ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2272ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 2273445640a5SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"}; 2274ee7e38e3SLen Brown 2275e9257f5fSLen Brown int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2276e9257f5fSLen Brown int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2277e9257f5fSLen Brown int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 22780539ba11SLen Brown int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7}; 2279f2642888SLen Brown int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2280e9257f5fSLen Brown int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2281445640a5SLen Brown int glm_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 22822085e124SArtem Bityutskiy int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 228325368d7cSChen Yu int icx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2284a2b7b749SLen Brown 2285a2b7b749SLen Brown static void 2286a2b7b749SLen Brown calculate_tsc_tweak() 2287a2b7b749SLen Brown { 2288a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2289a2b7b749SLen Brown } 2290a2b7b749SLen Brown 2291aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2292aeb01e6dSChen Yu 2293fcd17211SLen Brown static void 2294fcd17211SLen Brown dump_nhm_platform_info(void) 2295103a8feaSLen Brown { 2296103a8feaSLen Brown unsigned long long msr; 2297103a8feaSLen Brown unsigned int ratio; 2298103a8feaSLen Brown 2299ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2300103a8feaSLen Brown 2301b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 23026574a5d5SLen Brown 2303103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 2304710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 2305103a8feaSLen Brown ratio, bclk, ratio * bclk); 2306103a8feaSLen Brown 2307103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 2308710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 2309103a8feaSLen Brown ratio, bclk, ratio * bclk); 2310103a8feaSLen Brown 23117ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2312b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2313bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 231467920418SLen Brown 2315aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2316aeb01e6dSChen Yu if (dis_cstate_prewake) 2317aeb01e6dSChen Yu fprintf(outf, "C-state Pre-wake: %sabled\n", 2318aeb01e6dSChen Yu msr & 0x40000000 ? "DIS" : "EN"); 2319aeb01e6dSChen Yu 2320fcd17211SLen Brown return; 2321fcd17211SLen Brown } 2322fcd17211SLen Brown 2323fcd17211SLen Brown static void 2324fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 2325fcd17211SLen Brown { 2326fcd17211SLen Brown unsigned long long msr; 2327fcd17211SLen Brown unsigned int ratio; 2328fcd17211SLen Brown 23297ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2330fcd17211SLen Brown 2331b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2332fcd17211SLen Brown 2333fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2334fcd17211SLen Brown if (ratio) 2335710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 2336fcd17211SLen Brown ratio, bclk, ratio * bclk); 2337fcd17211SLen Brown 2338fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2339fcd17211SLen Brown if (ratio) 2340710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 2341fcd17211SLen Brown ratio, bclk, ratio * bclk); 2342fcd17211SLen Brown return; 2343fcd17211SLen Brown } 2344fcd17211SLen Brown 2345fcd17211SLen Brown static void 2346fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 2347fcd17211SLen Brown { 2348fcd17211SLen Brown unsigned long long msr; 2349fcd17211SLen Brown unsigned int ratio; 23506574a5d5SLen Brown 23517ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 23526574a5d5SLen Brown 2353b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 23546574a5d5SLen Brown 23556574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 23566574a5d5SLen Brown if (ratio) 2357710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 23586574a5d5SLen Brown ratio, bclk, ratio * bclk); 23596574a5d5SLen Brown 23606574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 23616574a5d5SLen Brown if (ratio) 2362710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 23636574a5d5SLen Brown ratio, bclk, ratio * bclk); 23646574a5d5SLen Brown 23656574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 23666574a5d5SLen Brown if (ratio) 2367710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 23686574a5d5SLen Brown ratio, bclk, ratio * bclk); 23696574a5d5SLen Brown 23706574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 23716574a5d5SLen Brown if (ratio) 2372710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 23736574a5d5SLen Brown ratio, bclk, ratio * bclk); 23746574a5d5SLen Brown 23756574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 23766574a5d5SLen Brown if (ratio) 2377710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 23786574a5d5SLen Brown ratio, bclk, ratio * bclk); 23796574a5d5SLen Brown 23806574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 23816574a5d5SLen Brown if (ratio) 2382710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 23836574a5d5SLen Brown ratio, bclk, ratio * bclk); 23846574a5d5SLen Brown 23856574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 23866574a5d5SLen Brown if (ratio) 2387710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 23886574a5d5SLen Brown ratio, bclk, ratio * bclk); 23896574a5d5SLen Brown 23906574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 23916574a5d5SLen Brown if (ratio) 2392710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 23936574a5d5SLen Brown ratio, bclk, ratio * bclk); 2394103a8feaSLen Brown return; 2395fcd17211SLen Brown } 239631e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 239731e07522SLen Brown { 239831e07522SLen Brown 239931e07522SLen Brown if (!genuine_intel) 240031e07522SLen Brown return 0; 240131e07522SLen Brown 240231e07522SLen Brown switch (model) { 240331e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 240431e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 240525368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 24065ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 240720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 240831e07522SLen Brown return 1; 240931e07522SLen Brown } 241031e07522SLen Brown return 0; 241131e07522SLen Brown } 2412fcd17211SLen Brown 2413fcd17211SLen Brown static void 241431e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 2415fcd17211SLen Brown { 241631e07522SLen Brown unsigned long long msr, core_counts; 241731e07522SLen Brown unsigned int ratio, group_size; 2418103a8feaSLen Brown 24197ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2420b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 24216574a5d5SLen Brown 242231e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 242331e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 242431e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 242531e07522SLen Brown } else { 242631e07522SLen Brown core_counts = 0x0807060504030201; 242731e07522SLen Brown } 242831e07522SLen Brown 24296574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 243031e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 24316574a5d5SLen Brown if (ratio) 243231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 243331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24346574a5d5SLen Brown 24356574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 243631e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 24376574a5d5SLen Brown if (ratio) 243831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 243931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24406574a5d5SLen Brown 24416574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 244231e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 24436574a5d5SLen Brown if (ratio) 244431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 244531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24466574a5d5SLen Brown 24476574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 244831e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 24496574a5d5SLen Brown if (ratio) 245031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 245131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24526574a5d5SLen Brown 2453103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 245431e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2455103a8feaSLen Brown if (ratio) 245631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 245731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2458103a8feaSLen Brown 2459103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 246031e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2461103a8feaSLen Brown if (ratio) 246231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 246331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2464103a8feaSLen Brown 2465103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 246631e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2467103a8feaSLen Brown if (ratio) 246831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 246931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2470103a8feaSLen Brown 2471103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 247231e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2473103a8feaSLen Brown if (ratio) 247431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 247531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2476fcd17211SLen Brown return; 2477fcd17211SLen Brown } 24783a9a941dSLen Brown 2479fcd17211SLen Brown static void 24800f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 24810f7887c4SLen Brown { 24820f7887c4SLen Brown unsigned long long msr; 24830f7887c4SLen Brown unsigned int ratio; 24840f7887c4SLen Brown 24850f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 24860f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 24870f7887c4SLen Brown 24880f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 24890f7887c4SLen Brown if (ratio) 24900f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 24910f7887c4SLen Brown ratio, bclk, ratio * bclk); 24920f7887c4SLen Brown 24930f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 24940f7887c4SLen Brown if (ratio) 24950f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 24960f7887c4SLen Brown ratio, bclk, ratio * bclk); 24970f7887c4SLen Brown 24980f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 24990f7887c4SLen Brown if (ratio) 25000f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 25010f7887c4SLen Brown ratio, bclk, ratio * bclk); 25020f7887c4SLen Brown 25030f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 25040f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25050f7887c4SLen Brown 25060f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 25070f7887c4SLen Brown if (ratio) 25080f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 25090f7887c4SLen Brown ratio, bclk, ratio * bclk); 25100f7887c4SLen Brown 25110f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25120f7887c4SLen Brown if (ratio) 25130f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 25140f7887c4SLen Brown ratio, bclk, ratio * bclk); 25150f7887c4SLen Brown 25160f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25170f7887c4SLen Brown if (ratio) 25180f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 25190f7887c4SLen Brown ratio, bclk, ratio * bclk); 25200f7887c4SLen Brown 25210f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25220f7887c4SLen Brown if (ratio) 25230f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 25240f7887c4SLen Brown ratio, bclk, ratio * bclk); 25250f7887c4SLen Brown } 25260f7887c4SLen Brown 25270f7887c4SLen Brown static void 2528fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2529fb5d4327SDasaratharaman Chandramouli { 2530cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2531cbf97abaSHubert Chrzaniuk 2532fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2533cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2534cbf97abaSHubert Chrzaniuk int i, b_nr; 2535cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2536cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2537fb5d4327SDasaratharaman Chandramouli 2538ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2539fb5d4327SDasaratharaman Chandramouli 2540b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2541bfae2052SLen Brown base_cpu, msr); 2542fb5d4327SDasaratharaman Chandramouli 25431e3ec5cdSRandy Dunlap /* 2544fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2545cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2546cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2547fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2548fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2549fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2550fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2551fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2552fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2553fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2554fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2555fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2556fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2557fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2558fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2559fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2560fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2561fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2562fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2563fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2564fb5d4327SDasaratharaman Chandramouli */ 2565fb5d4327SDasaratharaman Chandramouli 2566cbf97abaSHubert Chrzaniuk b_nr = 0; 2567cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2568cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2569cbf97abaSHubert Chrzaniuk 2570cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2571fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2572cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2573fb5d4327SDasaratharaman Chandramouli 2574cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2575cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2576cbf97abaSHubert Chrzaniuk b_nr++; 2577fb5d4327SDasaratharaman Chandramouli } 2578cbf97abaSHubert Chrzaniuk 2579cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2580cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2581b7d8c148SLen Brown fprintf(outf, 2582710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2583cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2584fb5d4327SDasaratharaman Chandramouli } 2585fb5d4327SDasaratharaman Chandramouli 2586fb5d4327SDasaratharaman Chandramouli static void 2587fcd17211SLen Brown dump_nhm_cst_cfg(void) 2588fcd17211SLen Brown { 2589fcd17211SLen Brown unsigned long long msr; 2590fcd17211SLen Brown 25911df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2592fcd17211SLen Brown 25931df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2594fcd17211SLen Brown 25953e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2596fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2597fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2598fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2599fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2600fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 26016c34f160SLen Brown (unsigned int)msr & 0xF, 2602fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2603ac980e13SArtem Bityutskiy 2604ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2605ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2606ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2607ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2608ac980e13SArtem Bityutskiy } 2609ac980e13SArtem Bityutskiy 2610ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2611ac980e13SArtem Bityutskiy 2612fcd17211SLen Brown return; 2613103a8feaSLen Brown } 2614103a8feaSLen Brown 26156fb3143bSLen Brown static void 26166fb3143bSLen Brown dump_config_tdp(void) 26176fb3143bSLen Brown { 26186fb3143bSLen Brown unsigned long long msr; 26196fb3143bSLen Brown 26206fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2621b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2622685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26236fb3143bSLen Brown 26246fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2625b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 26266fb3143bSLen Brown if (msr) { 2627685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2628685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2629685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2630685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 26316fb3143bSLen Brown } 2632b7d8c148SLen Brown fprintf(outf, ")\n"); 26336fb3143bSLen Brown 26346fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2635b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 26366fb3143bSLen Brown if (msr) { 2637685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2638685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2639685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2640685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 26416fb3143bSLen Brown } 2642b7d8c148SLen Brown fprintf(outf, ")\n"); 26436fb3143bSLen Brown 26446fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2645b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 26466fb3143bSLen Brown if ((msr) & 0x3) 2647b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2648b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2649b7d8c148SLen Brown fprintf(outf, ")\n"); 26506fb3143bSLen Brown 26516fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2652b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2653685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2654b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2655b7d8c148SLen Brown fprintf(outf, ")\n"); 26566fb3143bSLen Brown } 26575a63426eSLen Brown 26585a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 26595a63426eSLen Brown 26605a63426eSLen Brown void print_irtl(void) 26615a63426eSLen Brown { 26625a63426eSLen Brown unsigned long long msr; 26635a63426eSLen Brown 26645a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 26655a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 26665a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26675a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26685a63426eSLen Brown 26695a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 26705a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 26715a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26725a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26735a63426eSLen Brown 26745a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 26755a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 26765a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26775a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26785a63426eSLen Brown 26795a63426eSLen Brown if (!do_irtl_hsw) 26805a63426eSLen Brown return; 26815a63426eSLen Brown 26825a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 26835a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 26845a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26855a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26865a63426eSLen Brown 26875a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 26885a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 26895a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26905a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26915a63426eSLen Brown 26925a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 26935a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 26945a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 26955a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 26965a63426eSLen Brown 26975a63426eSLen Brown } 269836229897SLen Brown void free_fd_percpu(void) 269936229897SLen Brown { 270036229897SLen Brown int i; 270136229897SLen Brown 270201a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 270336229897SLen Brown if (fd_percpu[i] != 0) 270436229897SLen Brown close(fd_percpu[i]); 270536229897SLen Brown } 270636229897SLen Brown 270736229897SLen Brown free(fd_percpu); 27086fb3143bSLen Brown } 27096fb3143bSLen Brown 2710c98d5d94SLen Brown void free_all_buffers(void) 2711103a8feaSLen Brown { 27120e2d8f05SLen Brown int i; 27130e2d8f05SLen Brown 2714c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2715c98d5d94SLen Brown cpu_present_set = NULL; 271636229897SLen Brown cpu_present_setsize = 0; 2717103a8feaSLen Brown 2718c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2719c98d5d94SLen Brown cpu_affinity_set = NULL; 2720c98d5d94SLen Brown cpu_affinity_setsize = 0; 2721103a8feaSLen Brown 2722c98d5d94SLen Brown free(thread_even); 2723c98d5d94SLen Brown free(core_even); 2724c98d5d94SLen Brown free(package_even); 2725103a8feaSLen Brown 2726c98d5d94SLen Brown thread_even = NULL; 2727c98d5d94SLen Brown core_even = NULL; 2728c98d5d94SLen Brown package_even = NULL; 2729103a8feaSLen Brown 2730c98d5d94SLen Brown free(thread_odd); 2731c98d5d94SLen Brown free(core_odd); 2732c98d5d94SLen Brown free(package_odd); 2733103a8feaSLen Brown 2734c98d5d94SLen Brown thread_odd = NULL; 2735c98d5d94SLen Brown core_odd = NULL; 2736c98d5d94SLen Brown package_odd = NULL; 2737103a8feaSLen Brown 2738c98d5d94SLen Brown free(output_buffer); 2739c98d5d94SLen Brown output_buffer = NULL; 2740c98d5d94SLen Brown outp = NULL; 274136229897SLen Brown 274236229897SLen Brown free_fd_percpu(); 2743562a2d37SLen Brown 2744562a2d37SLen Brown free(irq_column_2_cpu); 2745562a2d37SLen Brown free(irqs_per_cpu); 27460e2d8f05SLen Brown 27470e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 27480e2d8f05SLen Brown if (cpus[i].put_ids) 27490e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 27500e2d8f05SLen Brown } 27510e2d8f05SLen Brown free(cpus); 2752103a8feaSLen Brown } 2753103a8feaSLen Brown 275457a42a34SJosh Triplett 275557a42a34SJosh Triplett /* 275695aebc44SJosh Triplett * Parse a file containing a single int. 27576de68fe1SLen Brown * Return 0 if file can not be opened 27586de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 275995aebc44SJosh Triplett */ 276095aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 276195aebc44SJosh Triplett { 276295aebc44SJosh Triplett va_list args; 276395aebc44SJosh Triplett char path[PATH_MAX]; 276495aebc44SJosh Triplett FILE *filep; 276595aebc44SJosh Triplett int value; 276695aebc44SJosh Triplett 276795aebc44SJosh Triplett va_start(args, fmt); 276895aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 276995aebc44SJosh Triplett va_end(args); 27706de68fe1SLen Brown filep = fopen(path, "r"); 27716de68fe1SLen Brown if (!filep) 27726de68fe1SLen Brown return 0; 2773b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2774b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 277595aebc44SJosh Triplett fclose(filep); 277695aebc44SJosh Triplett return value; 277795aebc44SJosh Triplett } 277895aebc44SJosh Triplett 277995aebc44SJosh Triplett /* 2780c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2781c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2782c98d5d94SLen Brown */ 2783c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2784c98d5d94SLen Brown { 278595aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2786103a8feaSLen Brown } 2787103a8feaSLen Brown 2788103a8feaSLen Brown int get_physical_package_id(int cpu) 2789103a8feaSLen Brown { 279095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2791103a8feaSLen Brown } 2792103a8feaSLen Brown 27936de68fe1SLen Brown int get_die_id(int cpu) 27946de68fe1SLen Brown { 27956de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 27966de68fe1SLen Brown } 27976de68fe1SLen Brown 2798103a8feaSLen Brown int get_core_id(int cpu) 2799103a8feaSLen Brown { 280095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2801103a8feaSLen Brown } 2802103a8feaSLen Brown 2803ef605741SPrarit Bhargava void set_node_data(void) 2804ef605741SPrarit Bhargava { 28052ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 28062ffbb224SPrarit Bhargava int cpu_count; 2807ef605741SPrarit Bhargava 28082ffbb224SPrarit Bhargava /* initialize logical_node_id */ 28092ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 28102ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2811ef605741SPrarit Bhargava 28122ffbb224SPrarit Bhargava cpu_count = 0; 28132ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 28142ffbb224SPrarit Bhargava lnode = 0; 28152ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 28162ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2817ef605741SPrarit Bhargava continue; 28182ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 28192ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 28202ffbb224SPrarit Bhargava continue; 28212ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2822ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28232ffbb224SPrarit Bhargava cpu_count++; 28242ffbb224SPrarit Bhargava /* 28252ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 28262ffbb224SPrarit Bhargava * the logical_node_id 28272ffbb224SPrarit Bhargava */ 28282ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 28292ffbb224SPrarit Bhargava if ((cpus[cpux].physical_package_id == pkg) && 28302ffbb224SPrarit Bhargava (cpus[cpux].physical_node_id == node)) { 28312ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 28322ffbb224SPrarit Bhargava cpu_count++; 2833ef605741SPrarit Bhargava } 28342ffbb224SPrarit Bhargava } 28352ffbb224SPrarit Bhargava lnode++; 28362ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 28372ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 28382ffbb224SPrarit Bhargava } 28392ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 28402ffbb224SPrarit Bhargava break; 28412ffbb224SPrarit Bhargava } 2842ef605741SPrarit Bhargava } 2843ef605741SPrarit Bhargava 2844ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2845c98d5d94SLen Brown { 2846c98d5d94SLen Brown char path[80]; 2847c98d5d94SLen Brown FILE *filep; 28480e2d8f05SLen Brown int i; 28490e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2850c98d5d94SLen Brown 28510e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 28520e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 28530e2d8f05SLen Brown cpu, i); 28540e2d8f05SLen Brown filep = fopen(path, "r"); 28550e2d8f05SLen Brown if (!filep) 28560e2d8f05SLen Brown continue; 28570e2d8f05SLen Brown fclose(filep); 28580e2d8f05SLen Brown return i; 28590e2d8f05SLen Brown } 28600e2d8f05SLen Brown return -1; 2861e275b388SDasaratharaman Chandramouli } 2862c98d5d94SLen Brown 28630e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 28640e2d8f05SLen Brown { 28650e2d8f05SLen Brown char path[80], character; 28660e2d8f05SLen Brown FILE *filep; 28670e2d8f05SLen Brown unsigned long map; 28688cb48b32SPrarit Bhargava int so, shift, sib_core; 28690e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 28700e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 28710e2d8f05SLen Brown size_t size; 28728cb48b32SPrarit Bhargava int thread_id = 0; 28730e2d8f05SLen Brown 28740e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 28758cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 28768cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 28770e2d8f05SLen Brown if (!thiscpu->put_ids) 28780e2d8f05SLen Brown return -1; 28790e2d8f05SLen Brown 28800e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 28810e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 28820e2d8f05SLen Brown 28830e2d8f05SLen Brown sprintf(path, 28840e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 28853d7772eaSLen Brown filep = fopen(path, "r"); 28863d7772eaSLen Brown 28873d7772eaSLen Brown if (!filep) { 28883d7772eaSLen Brown warnx("%s: open failed", path); 28893d7772eaSLen Brown return -1; 28903d7772eaSLen Brown } 28910e2d8f05SLen Brown do { 28920e2d8f05SLen Brown offset -= BITMASK_SIZE; 28938173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 28948173c336SBen Hutchings err(1, "%s: failed to parse file", path); 28950e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 28960e2d8f05SLen Brown if ((map >> shift) & 0x1) { 28978cb48b32SPrarit Bhargava so = shift + offset; 28988cb48b32SPrarit Bhargava sib_core = get_core_id(so); 28998cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 29008cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 29018cb48b32SPrarit Bhargava if ((so != cpu) && 29028cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 29038cb48b32SPrarit Bhargava cpus[so].thread_id = 29048cb48b32SPrarit Bhargava thread_id++; 29058cb48b32SPrarit Bhargava } 29060e2d8f05SLen Brown } 29070e2d8f05SLen Brown } 29080e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2909c98d5d94SLen Brown fclose(filep); 29100e2d8f05SLen Brown 29110e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2912c98d5d94SLen Brown } 2913c98d5d94SLen Brown 2914c98d5d94SLen Brown /* 2915c98d5d94SLen Brown * run func(thread, core, package) in topology order 2916c98d5d94SLen Brown * skip non-present cpus 2917103a8feaSLen Brown */ 2918103a8feaSLen Brown 2919c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2920c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2921c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2922c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2923c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2924c98d5d94SLen Brown struct pkg_data *pkg_base2) 2925c98d5d94SLen Brown { 292640f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2927c98d5d94SLen Brown 2928c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 292940f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 293040f5cfe7SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; 293140f5cfe7SPrarit Bhargava ++core_no) { 2932c98d5d94SLen Brown for (thread_no = 0; thread_no < 293370a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2934c98d5d94SLen Brown struct thread_data *t, *t2; 2935c98d5d94SLen Brown struct core_data *c, *c2; 2936c98d5d94SLen Brown struct pkg_data *p, *p2; 2937c98d5d94SLen Brown 293840f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 293940f5cfe7SPrarit Bhargava core_no, node_no, 294040f5cfe7SPrarit Bhargava pkg_no); 2941c98d5d94SLen Brown 2942c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2943c98d5d94SLen Brown continue; 2944c98d5d94SLen Brown 294540f5cfe7SPrarit Bhargava t2 = GET_THREAD(thread_base2, thread_no, 294640f5cfe7SPrarit Bhargava core_no, node_no, 294740f5cfe7SPrarit Bhargava pkg_no); 2948c98d5d94SLen Brown 294940f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 295040f5cfe7SPrarit Bhargava node_no, pkg_no); 295140f5cfe7SPrarit Bhargava c2 = GET_CORE(core_base2, core_no, 295240f5cfe7SPrarit Bhargava node_no, 295340f5cfe7SPrarit Bhargava pkg_no); 2954c98d5d94SLen Brown 2955c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2956c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2957c98d5d94SLen Brown 2958c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2959c98d5d94SLen Brown if (retval) 2960c98d5d94SLen Brown return retval; 2961c98d5d94SLen Brown } 2962c98d5d94SLen Brown } 2963c98d5d94SLen Brown } 296440f5cfe7SPrarit Bhargava } 2965c98d5d94SLen Brown return 0; 2966c98d5d94SLen Brown } 2967c98d5d94SLen Brown 2968c98d5d94SLen Brown /* 2969c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2970c98d5d94SLen Brown * return max_cpu number 2971c98d5d94SLen Brown */ 2972c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2973103a8feaSLen Brown { 2974103a8feaSLen Brown FILE *fp; 2975c98d5d94SLen Brown int cpu_num; 2976103a8feaSLen Brown int retval; 2977103a8feaSLen Brown 297857a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2979103a8feaSLen Brown 2980103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2981b2c95d90SJosh Triplett if (retval != 0) 2982b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2983103a8feaSLen Brown 2984c98d5d94SLen Brown while (1) { 2985c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2986103a8feaSLen Brown if (retval != 1) 2987103a8feaSLen Brown break; 2988103a8feaSLen Brown 2989c98d5d94SLen Brown retval = func(cpu_num); 2990c98d5d94SLen Brown if (retval) { 2991c98d5d94SLen Brown fclose(fp); 2992c98d5d94SLen Brown return(retval); 2993c98d5d94SLen Brown } 2994103a8feaSLen Brown } 2995103a8feaSLen Brown fclose(fp); 2996c98d5d94SLen Brown return 0; 2997103a8feaSLen Brown } 2998103a8feaSLen Brown 2999103a8feaSLen Brown void re_initialize(void) 3000103a8feaSLen Brown { 3001c98d5d94SLen Brown free_all_buffers(); 3002c98d5d94SLen Brown setup_all_buffers(); 30033d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 3004103a8feaSLen Brown } 3005103a8feaSLen Brown 3006843c5791SPrarit Bhargava void set_max_cpu_num(void) 3007843c5791SPrarit Bhargava { 3008843c5791SPrarit Bhargava FILE *filep; 30098201a028SPrarit Bhargava int base_cpu; 3010843c5791SPrarit Bhargava unsigned long dummy; 30118201a028SPrarit Bhargava char pathname[64]; 3012843c5791SPrarit Bhargava 30138201a028SPrarit Bhargava base_cpu = sched_getcpu(); 30148201a028SPrarit Bhargava if (base_cpu < 0) 30158201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 30168201a028SPrarit Bhargava sprintf(pathname, 30178201a028SPrarit Bhargava "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", 30188201a028SPrarit Bhargava base_cpu); 30198201a028SPrarit Bhargava 30208201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 3021843c5791SPrarit Bhargava topo.max_cpu_num = 0; 3022843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 30230e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 3024843c5791SPrarit Bhargava fclose(filep); 3025843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 3026843c5791SPrarit Bhargava } 3027c98d5d94SLen Brown 3028103a8feaSLen Brown /* 3029c98d5d94SLen Brown * count_cpus() 3030c98d5d94SLen Brown * remember the last one seen, it will be the max 3031103a8feaSLen Brown */ 3032c98d5d94SLen Brown int count_cpus(int cpu) 3033103a8feaSLen Brown { 3034843c5791SPrarit Bhargava topo.num_cpus++; 3035c98d5d94SLen Brown return 0; 3036103a8feaSLen Brown } 3037c98d5d94SLen Brown int mark_cpu_present(int cpu) 3038c98d5d94SLen Brown { 3039c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 304015aaa346SLen Brown return 0; 3041103a8feaSLen Brown } 3042103a8feaSLen Brown 30438cb48b32SPrarit Bhargava int init_thread_id(int cpu) 30448cb48b32SPrarit Bhargava { 30458cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 30468cb48b32SPrarit Bhargava return 0; 30478cb48b32SPrarit Bhargava } 30488cb48b32SPrarit Bhargava 3049562a2d37SLen Brown /* 3050562a2d37SLen Brown * snapshot_proc_interrupts() 3051562a2d37SLen Brown * 3052562a2d37SLen Brown * read and record summary of /proc/interrupts 3053562a2d37SLen Brown * 3054562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3055562a2d37SLen Brown */ 3056562a2d37SLen Brown int snapshot_proc_interrupts(void) 3057562a2d37SLen Brown { 3058562a2d37SLen Brown static FILE *fp; 3059562a2d37SLen Brown int column, retval; 3060562a2d37SLen Brown 3061562a2d37SLen Brown if (fp == NULL) 3062562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3063562a2d37SLen Brown else 3064562a2d37SLen Brown rewind(fp); 3065562a2d37SLen Brown 3066562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3067562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3068562a2d37SLen Brown int cpu_number; 3069562a2d37SLen Brown 3070562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3071562a2d37SLen Brown if (retval != 1) 3072562a2d37SLen Brown break; 3073562a2d37SLen Brown 3074562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3075562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3076562a2d37SLen Brown return 1; 3077562a2d37SLen Brown } 3078562a2d37SLen Brown 3079562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3080562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3081562a2d37SLen Brown } 3082562a2d37SLen Brown 3083562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3084562a2d37SLen Brown while (1) { 3085562a2d37SLen Brown int column; 3086562a2d37SLen Brown char buf[64]; 3087562a2d37SLen Brown 3088562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3089562a2d37SLen Brown if (retval != 1) 3090562a2d37SLen Brown break; 3091562a2d37SLen Brown 3092562a2d37SLen Brown /* read the count per cpu */ 3093562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3094562a2d37SLen Brown 3095562a2d37SLen Brown int cpu_number, irq_count; 3096562a2d37SLen Brown 3097562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3098562a2d37SLen Brown if (retval != 1) 3099562a2d37SLen Brown break; 3100562a2d37SLen Brown 3101562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3102562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3103562a2d37SLen Brown 3104562a2d37SLen Brown } 3105562a2d37SLen Brown 3106562a2d37SLen Brown while (getc(fp) != '\n') 3107562a2d37SLen Brown ; /* flush interrupt description */ 3108562a2d37SLen Brown 3109562a2d37SLen Brown } 3110562a2d37SLen Brown return 0; 3111562a2d37SLen Brown } 311227d47356SLen Brown /* 3113fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3114fdf676e5SLen Brown * 3115fdf676e5SLen Brown * record snapshot of 3116fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3117fdf676e5SLen Brown * 3118fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3119fdf676e5SLen Brown */ 3120fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3121fdf676e5SLen Brown { 3122fdf676e5SLen Brown FILE *fp; 3123fdf676e5SLen Brown int retval; 3124fdf676e5SLen Brown 3125fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3126fdf676e5SLen Brown 3127fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3128fdf676e5SLen Brown if (retval != 1) 3129fdf676e5SLen Brown err(1, "GFX rc6"); 3130fdf676e5SLen Brown 3131fdf676e5SLen Brown fclose(fp); 3132fdf676e5SLen Brown 3133fdf676e5SLen Brown return 0; 3134fdf676e5SLen Brown } 3135fdf676e5SLen Brown /* 313627d47356SLen Brown * snapshot_gfx_mhz() 313727d47356SLen Brown * 313827d47356SLen Brown * record snapshot of 313927d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 314027d47356SLen Brown * 314127d47356SLen Brown * return 1 if config change requires a restart, else return 0 314227d47356SLen Brown */ 314327d47356SLen Brown int snapshot_gfx_mhz(void) 314427d47356SLen Brown { 314527d47356SLen Brown static FILE *fp; 314627d47356SLen Brown int retval; 314727d47356SLen Brown 314827d47356SLen Brown if (fp == NULL) 314927d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 315022048c54SLen Brown else { 315127d47356SLen Brown rewind(fp); 315222048c54SLen Brown fflush(fp); 315322048c54SLen Brown } 315427d47356SLen Brown 315527d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 315627d47356SLen Brown if (retval != 1) 315727d47356SLen Brown err(1, "GFX MHz"); 315827d47356SLen Brown 315927d47356SLen Brown return 0; 316027d47356SLen Brown } 3161562a2d37SLen Brown 3162562a2d37SLen Brown /* 3163b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3164b4b91569SRafael Antognolli * 3165b4b91569SRafael Antognolli * record snapshot of 3166b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3167b4b91569SRafael Antognolli * 3168b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3169b4b91569SRafael Antognolli */ 3170b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3171b4b91569SRafael Antognolli { 3172b4b91569SRafael Antognolli static FILE *fp; 3173b4b91569SRafael Antognolli int retval; 3174b4b91569SRafael Antognolli 3175b4b91569SRafael Antognolli if (fp == NULL) 3176b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3177b4b91569SRafael Antognolli else { 3178b4b91569SRafael Antognolli rewind(fp); 3179b4b91569SRafael Antognolli fflush(fp); 3180b4b91569SRafael Antognolli } 3181b4b91569SRafael Antognolli 3182b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3183b4b91569SRafael Antognolli if (retval != 1) 3184b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3185b4b91569SRafael Antognolli 3186b4b91569SRafael Antognolli return 0; 3187b4b91569SRafael Antognolli } 3188b4b91569SRafael Antognolli 3189b4b91569SRafael Antognolli /* 3190be0e54c4SLen Brown * snapshot_cpu_lpi() 3191be0e54c4SLen Brown * 3192be0e54c4SLen Brown * record snapshot of 3193be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3194be0e54c4SLen Brown */ 3195be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3196be0e54c4SLen Brown { 3197be0e54c4SLen Brown FILE *fp; 3198be0e54c4SLen Brown int retval; 3199be0e54c4SLen Brown 3200be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3201be0e54c4SLen Brown 3202be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 32035ea7647bSPrarit Bhargava if (retval != 1) { 32045ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 32055ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3206605736c6SGustavo A. R. Silva fclose(fp); 32075ea7647bSPrarit Bhargava return -1; 32085ea7647bSPrarit Bhargava } 3209be0e54c4SLen Brown 3210be0e54c4SLen Brown fclose(fp); 3211be0e54c4SLen Brown 3212be0e54c4SLen Brown return 0; 3213be0e54c4SLen Brown } 3214be0e54c4SLen Brown /* 3215be0e54c4SLen Brown * snapshot_sys_lpi() 3216be0e54c4SLen Brown * 32171f81c5efSLen Brown * record snapshot of sys_lpi_file 3218be0e54c4SLen Brown */ 3219be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3220be0e54c4SLen Brown { 3221be0e54c4SLen Brown FILE *fp; 3222be0e54c4SLen Brown int retval; 3223be0e54c4SLen Brown 32241f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3225be0e54c4SLen Brown 3226be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 32275ea7647bSPrarit Bhargava if (retval != 1) { 32285ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 32295ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 323015423b95SColin Ian King fclose(fp); 32315ea7647bSPrarit Bhargava return -1; 32325ea7647bSPrarit Bhargava } 3233be0e54c4SLen Brown fclose(fp); 3234be0e54c4SLen Brown 3235be0e54c4SLen Brown return 0; 3236be0e54c4SLen Brown } 3237be0e54c4SLen Brown /* 3238562a2d37SLen Brown * snapshot /proc and /sys files 3239562a2d37SLen Brown * 3240562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3241562a2d37SLen Brown */ 3242562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3243562a2d37SLen Brown { 3244218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3245562a2d37SLen Brown if (snapshot_proc_interrupts()) 3246562a2d37SLen Brown return 1; 3247562a2d37SLen Brown 3248812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3249fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3250fdf676e5SLen Brown 3251812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 325227d47356SLen Brown snapshot_gfx_mhz(); 325327d47356SLen Brown 3254b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3255b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3256b4b91569SRafael Antognolli 3257be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3258be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3259be0e54c4SLen Brown 3260be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3261be0e54c4SLen Brown snapshot_sys_lpi_us(); 3262be0e54c4SLen Brown 3263562a2d37SLen Brown return 0; 3264562a2d37SLen Brown } 3265562a2d37SLen Brown 32668aa2ed0bSLen Brown int exit_requested; 32678aa2ed0bSLen Brown 32688aa2ed0bSLen Brown static void signal_handler (int signal) 32698aa2ed0bSLen Brown { 32708aa2ed0bSLen Brown switch (signal) { 32718aa2ed0bSLen Brown case SIGINT: 32728aa2ed0bSLen Brown exit_requested = 1; 32738aa2ed0bSLen Brown if (debug) 32748aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 32758aa2ed0bSLen Brown break; 327607211960SLen Brown case SIGUSR1: 327707211960SLen Brown if (debug > 1) 327807211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 327907211960SLen Brown break; 32808aa2ed0bSLen Brown } 32818aa2ed0bSLen Brown } 32828aa2ed0bSLen Brown 32838aa2ed0bSLen Brown void setup_signal_handler(void) 32848aa2ed0bSLen Brown { 32858aa2ed0bSLen Brown struct sigaction sa; 32868aa2ed0bSLen Brown 32878aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 32888aa2ed0bSLen Brown 32898aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 32908aa2ed0bSLen Brown 32918aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 32928aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 329307211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 329407211960SLen Brown err(1, "sigaction SIGUSR1"); 32958aa2ed0bSLen Brown } 3296b9ad8ee0SLen Brown 329747936f94SArtem Bityutskiy void do_sleep(void) 3298b9ad8ee0SLen Brown { 3299c026c236SArtem Bityutskiy struct timeval tout; 3300c026c236SArtem Bityutskiy struct timespec rest; 3301b9ad8ee0SLen Brown fd_set readfds; 3302b9ad8ee0SLen Brown int retval; 3303b9ad8ee0SLen Brown 3304b9ad8ee0SLen Brown FD_ZERO(&readfds); 3305b9ad8ee0SLen Brown FD_SET(0, &readfds); 3306b9ad8ee0SLen Brown 3307c026c236SArtem Bityutskiy if (ignore_stdin) { 330847936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 330947936f94SArtem Bityutskiy return; 331047936f94SArtem Bityutskiy } 3311b9ad8ee0SLen Brown 3312c026c236SArtem Bityutskiy tout = interval_tv; 3313c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3314b9ad8ee0SLen Brown 3315b9ad8ee0SLen Brown if (retval == 1) { 3316b9ad8ee0SLen Brown switch (getc(stdin)) { 3317b9ad8ee0SLen Brown case 'q': 3318b9ad8ee0SLen Brown exit_requested = 1; 3319b9ad8ee0SLen Brown break; 3320c026c236SArtem Bityutskiy case EOF: 3321c026c236SArtem Bityutskiy /* 3322c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3323c026c236SArtem Bityutskiy * won't be any further input. 3324c026c236SArtem Bityutskiy */ 3325c026c236SArtem Bityutskiy ignore_stdin = 1; 3326c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3327c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3328c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3329c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3330b9ad8ee0SLen Brown } 3331b9ad8ee0SLen Brown } 3332b9ad8ee0SLen Brown } 333347936f94SArtem Bityutskiy 333487e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 333587e15da9SChen Yu { 333687e15da9SChen Yu int ret, idx; 333787e15da9SChen Yu unsigned long long msr_cur, msr_last; 333887e15da9SChen Yu 333987e15da9SChen Yu if (!per_cpu_msr_sum) 334087e15da9SChen Yu return 1; 334187e15da9SChen Yu 334287e15da9SChen Yu idx = offset_to_idx(offset); 334387e15da9SChen Yu if (idx < 0) 334487e15da9SChen Yu return idx; 334587e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 334687e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 334787e15da9SChen Yu if (ret) 334887e15da9SChen Yu return ret; 334987e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 335087e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 335187e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 335287e15da9SChen Yu 335387e15da9SChen Yu return 0; 335487e15da9SChen Yu } 335587e15da9SChen Yu 335687e15da9SChen Yu timer_t timerid; 335787e15da9SChen Yu 335887e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 335987e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 336087e15da9SChen Yu { 336187e15da9SChen Yu int i, ret; 336287e15da9SChen Yu int cpu = t->cpu_id; 336387e15da9SChen Yu 336487e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 336587e15da9SChen Yu unsigned long long msr_cur, msr_last; 336613a779deSCalvin Walton off_t offset; 336787e15da9SChen Yu 336887e15da9SChen Yu if (!idx_valid(i)) 336987e15da9SChen Yu continue; 337087e15da9SChen Yu offset = idx_to_offset(i); 337187e15da9SChen Yu if (offset < 0) 337287e15da9SChen Yu continue; 337387e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 337487e15da9SChen Yu if (ret) { 337513a779deSCalvin Walton fprintf(outf, "Can not update msr(0x%llx)\n", 337613a779deSCalvin Walton (unsigned long long)offset); 337787e15da9SChen Yu continue; 337887e15da9SChen Yu } 337987e15da9SChen Yu 338087e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 338187e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 338287e15da9SChen Yu 338387e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 338487e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 338587e15da9SChen Yu } 338687e15da9SChen Yu return 0; 338787e15da9SChen Yu } 338887e15da9SChen Yu 338987e15da9SChen Yu static void 339087e15da9SChen Yu msr_record_handler(union sigval v) 339187e15da9SChen Yu { 339287e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 339387e15da9SChen Yu } 339487e15da9SChen Yu 339587e15da9SChen Yu void msr_sum_record(void) 339687e15da9SChen Yu { 339787e15da9SChen Yu struct itimerspec its; 339887e15da9SChen Yu struct sigevent sev; 339987e15da9SChen Yu 340087e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 340187e15da9SChen Yu if (!per_cpu_msr_sum) { 340287e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 340387e15da9SChen Yu return; 340487e15da9SChen Yu } 340587e15da9SChen Yu /* 340687e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 340787e15da9SChen Yu */ 340887e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 340987e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 341087e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 341187e15da9SChen Yu 341287e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 341387e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 341487e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 341587e15da9SChen Yu goto release_msr; 341687e15da9SChen Yu } 341787e15da9SChen Yu 341887e15da9SChen Yu its.it_value.tv_sec = 0; 341987e15da9SChen Yu its.it_value.tv_nsec = 1; 342087e15da9SChen Yu /* 342187e15da9SChen Yu * A wraparound time has been calculated early. 342287e15da9SChen Yu * Some sources state that the peak power for a 342387e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 342487e15da9SChen Yu * use 2 * TDP for safety. 342587e15da9SChen Yu */ 342687e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 342787e15da9SChen Yu its.it_interval.tv_nsec = 0; 342887e15da9SChen Yu 342987e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 343087e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 343187e15da9SChen Yu goto release_timer; 343287e15da9SChen Yu } 343387e15da9SChen Yu return; 343487e15da9SChen Yu 343587e15da9SChen Yu release_timer: 343687e15da9SChen Yu timer_delete(timerid); 343787e15da9SChen Yu release_msr: 343887e15da9SChen Yu free(per_cpu_msr_sum); 343987e15da9SChen Yu } 34404c2122d4SLen Brown 3441103a8feaSLen Brown void turbostat_loop() 3442103a8feaSLen Brown { 3443c98d5d94SLen Brown int retval; 3444e52966c0SLen Brown int restarted = 0; 3445023fe0acSChen Yu int done_iters = 0; 3446c98d5d94SLen Brown 34478aa2ed0bSLen Brown setup_signal_handler(); 34488aa2ed0bSLen Brown 3449103a8feaSLen Brown restart: 3450e52966c0SLen Brown restarted++; 3451e52966c0SLen Brown 3452562a2d37SLen Brown snapshot_proc_sysfs_files(); 3453c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 34544c2122d4SLen Brown first_counter_read = 0; 3455d91bb17cSLen Brown if (retval < -1) { 3456d91bb17cSLen Brown exit(retval); 3457d91bb17cSLen Brown } else if (retval == -1) { 34583d7772eaSLen Brown if (restarted > 10) { 3459e52966c0SLen Brown exit(retval); 3460e52966c0SLen Brown } 3461c98d5d94SLen Brown re_initialize(); 3462c98d5d94SLen Brown goto restart; 3463c98d5d94SLen Brown } 3464e52966c0SLen Brown restarted = 0; 3465023fe0acSChen Yu done_iters = 0; 3466103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3467103a8feaSLen Brown 3468103a8feaSLen Brown while (1) { 3469c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3470103a8feaSLen Brown re_initialize(); 3471103a8feaSLen Brown goto restart; 3472103a8feaSLen Brown } 3473b9ad8ee0SLen Brown do_sleep(); 3474562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3475562a2d37SLen Brown goto restart; 3476c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3477d91bb17cSLen Brown if (retval < -1) { 3478d91bb17cSLen Brown exit(retval); 3479d91bb17cSLen Brown } else if (retval == -1) { 348015aaa346SLen Brown re_initialize(); 348115aaa346SLen Brown goto restart; 348215aaa346SLen Brown } 3483103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3484103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3485ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3486ba3dec99SLen Brown re_initialize(); 3487ba3dec99SLen Brown goto restart; 3488ba3dec99SLen Brown } 3489c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3490c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3491b7d8c148SLen Brown flush_output_stdout(); 34928aa2ed0bSLen Brown if (exit_requested) 34938aa2ed0bSLen Brown break; 3494023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3495023fe0acSChen Yu break; 3496b9ad8ee0SLen Brown do_sleep(); 3497562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3498562a2d37SLen Brown goto restart; 3499c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3500d91bb17cSLen Brown if (retval < -1) { 3501d91bb17cSLen Brown exit(retval); 3502d91bb17cSLen Brown } else if (retval == -1) { 3503103a8feaSLen Brown re_initialize(); 3504103a8feaSLen Brown goto restart; 3505103a8feaSLen Brown } 3506103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3507103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3508ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3509ba3dec99SLen Brown re_initialize(); 3510ba3dec99SLen Brown goto restart; 3511ba3dec99SLen Brown } 3512c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3513c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3514b7d8c148SLen Brown flush_output_stdout(); 35158aa2ed0bSLen Brown if (exit_requested) 35168aa2ed0bSLen Brown break; 3517023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3518023fe0acSChen Yu break; 3519103a8feaSLen Brown } 3520103a8feaSLen Brown } 3521103a8feaSLen Brown 3522103a8feaSLen Brown void check_dev_msr() 3523103a8feaSLen Brown { 3524103a8feaSLen Brown struct stat sb; 35257ce7d5deSPrarit Bhargava char pathname[32]; 3526103a8feaSLen Brown 35277ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 35287ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3529a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3530d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3531103a8feaSLen Brown } 3532103a8feaSLen Brown 3533fcaa681cSLen Brown /* 3534fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3535fcaa681cSLen Brown * return 0 on success 3536fcaa681cSLen Brown * return 1 on fail 3537fcaa681cSLen Brown */ 3538fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3539103a8feaSLen Brown { 3540fcaa681cSLen Brown cap_t caps; 3541fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3542fcaa681cSLen Brown 3543fcaa681cSLen Brown caps = cap_get_proc(); 3544fcaa681cSLen Brown if (caps == NULL) 3545fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3546fcaa681cSLen Brown 3547fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3548fcaa681cSLen Brown err(-6, "cap_get\n"); 3549fcaa681cSLen Brown 3550fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 3551fcaa681cSLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 3552fcaa681cSLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3553fcaa681cSLen Brown return 1; 3554fcaa681cSLen Brown } 3555fcaa681cSLen Brown 3556fcaa681cSLen Brown if (cap_free(caps) == -1) 3557fcaa681cSLen Brown err(-6, "cap_free\n"); 3558fcaa681cSLen Brown 3559fcaa681cSLen Brown return 0; 3560fcaa681cSLen Brown } 3561fcaa681cSLen Brown void check_permissions(void) 3562fcaa681cSLen Brown { 356398481e79SLen Brown int do_exit = 0; 35647ce7d5deSPrarit Bhargava char pathname[32]; 356598481e79SLen Brown 356698481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3567fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 356898481e79SLen Brown 356998481e79SLen Brown /* test file permissions */ 35707ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 35717ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 357298481e79SLen Brown do_exit++; 357398481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 357498481e79SLen Brown } 357598481e79SLen Brown 357698481e79SLen Brown /* if all else fails, thell them to be root */ 357798481e79SLen Brown if (do_exit) 3578b2c95d90SJosh Triplett if (getuid() != 0) 3579d7899447SLen Brown warnx("... or simply run as root"); 358098481e79SLen Brown 358198481e79SLen Brown if (do_exit) 358298481e79SLen Brown exit(-6); 3583103a8feaSLen Brown } 3584103a8feaSLen Brown 3585d7899447SLen Brown /* 3586d7899447SLen Brown * NHM adds support for additional MSRs: 3587d7899447SLen Brown * 3588d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3589d7899447SLen Brown * 3590ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 35911df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3592d7899447SLen Brown * 3593cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3594cf4cbe53SLen Brown * 3595d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3596d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3597d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3598d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3599d7899447SLen Brown * 3600ee7e38e3SLen Brown * Side effect: 36011df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 360233148d67SLen Brown * sets has_misc_feature_control 3603d7899447SLen Brown */ 3604ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3605103a8feaSLen Brown { 3606ee7e38e3SLen Brown unsigned long long msr; 360721ed5574SLen Brown unsigned int base_ratio; 3608ee7e38e3SLen Brown int *pkg_cstate_limits; 3609ee7e38e3SLen Brown 3610103a8feaSLen Brown if (!genuine_intel) 3611103a8feaSLen Brown return 0; 3612103a8feaSLen Brown 3613103a8feaSLen Brown if (family != 6) 3614103a8feaSLen Brown return 0; 3615103a8feaSLen Brown 361621ed5574SLen Brown bclk = discover_bclk(family, model); 361721ed5574SLen Brown 3618103a8feaSLen Brown switch (model) { 3619869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3620869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3621ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3622ee7e38e3SLen Brown break; 3623869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3624869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3625869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3626869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3627ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 362833148d67SLen Brown has_misc_feature_control = 1; 3629ee7e38e3SLen Brown break; 3630c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 36315e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3632869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 363377e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3634c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 36355e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3636869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3637af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3638af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3639ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 364033148d67SLen Brown has_misc_feature_control = 1; 3641ee7e38e3SLen Brown break; 3642d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3643d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 364433148d67SLen Brown has_misc_feature_control = 1; 3645d8ebb442SLen Brown break; 364625368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 364725368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits; 364825368d7cSChen Yu has_misc_feature_control = 1; 364925368d7cSChen Yu break; 3650f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3651cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 36525ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3653ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3654ee7e38e3SLen Brown break; 3655869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3656ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3657cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3658ee7e38e3SLen Brown break; 3659869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3660ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3661ee7e38e3SLen Brown break; 3662869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3663f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 36645ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3665f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 366620de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3667445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3668e4085d54SLen Brown break; 3669103a8feaSLen Brown default: 3670103a8feaSLen Brown return 0; 3671103a8feaSLen Brown } 36721df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3673e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3674ee7e38e3SLen Brown 3675ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 367621ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 367721ed5574SLen Brown 367821ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 367921ed5574SLen Brown has_base_hz = 1; 3680ee7e38e3SLen Brown return 1; 3681103a8feaSLen Brown } 36820f7887c4SLen Brown /* 3683495c7654SLen Brown * SLV client has support for unique MSRs: 36840f7887c4SLen Brown * 36850f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 36860f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 36870f7887c4SLen Brown */ 36880f7887c4SLen Brown 36890f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 36900f7887c4SLen Brown { 36910f7887c4SLen Brown if (!genuine_intel) 36920f7887c4SLen Brown return 0; 36930f7887c4SLen Brown 36940f7887c4SLen Brown switch (model) { 3695f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3696f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3697f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 36980f7887c4SLen Brown return 1; 36990f7887c4SLen Brown } 37000f7887c4SLen Brown return 0; 37010f7887c4SLen Brown } 37027170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 37037170a374SLen Brown { 37047170a374SLen Brown 37057170a374SLen Brown if (!genuine_intel) 37067170a374SLen Brown return 0; 37077170a374SLen Brown 37087170a374SLen Brown switch (model) { 37095ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 37107170a374SLen Brown return 1; 37117170a374SLen Brown } 37127170a374SLen Brown return 0; 37137170a374SLen Brown } 3714ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3715ade0ebacSLen Brown { 3716ade0ebacSLen Brown 3717ade0ebacSLen Brown if (!genuine_intel) 3718ade0ebacSLen Brown return 0; 3719ade0ebacSLen Brown 3720ade0ebacSLen Brown switch (model) { 3721ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3722ade0ebacSLen Brown return 1; 3723ade0ebacSLen Brown } 3724ade0ebacSLen Brown return 0; 3725ade0ebacSLen Brown } 372634c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 372734c76197SLen Brown { 372834c76197SLen Brown 372934c76197SLen Brown if (!genuine_intel) 373034c76197SLen Brown return 0; 373134c76197SLen Brown 373234c76197SLen Brown switch (model) { 373334c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 373434c76197SLen Brown return 1; 373534c76197SLen Brown } 373634c76197SLen Brown return 0; 373734c76197SLen Brown } 373825368d7cSChen Yu 373925368d7cSChen Yu int is_icx(unsigned int family, unsigned int model) 374025368d7cSChen Yu { 374125368d7cSChen Yu 374225368d7cSChen Yu if (!genuine_intel) 374325368d7cSChen Yu return 0; 374425368d7cSChen Yu 374525368d7cSChen Yu switch (model) { 374625368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 374725368d7cSChen Yu return 1; 374825368d7cSChen Yu } 374925368d7cSChen Yu return 0; 375025368d7cSChen Yu } 375125368d7cSChen Yu 3752f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3753f6708400SChen Yu { 3754f6708400SChen Yu if (!genuine_intel) 3755f6708400SChen Yu return 0; 3756f6708400SChen Yu 3757f6708400SChen Yu switch (model) { 3758f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3759f6708400SChen Yu return 1; 3760f6708400SChen Yu } 3761f6708400SChen Yu return 0; 3762f6708400SChen Yu } 376320de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 376420de0dabSAntti Laakso { 376520de0dabSAntti Laakso if (!genuine_intel) 376620de0dabSAntti Laakso return 0; 376720de0dabSAntti Laakso 376820de0dabSAntti Laakso switch (model) { 376920de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 377020de0dabSAntti Laakso return 1; 377120de0dabSAntti Laakso } 377220de0dabSAntti Laakso return 0; 377320de0dabSAntti Laakso } 37740f7887c4SLen Brown 377531e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3776d7899447SLen Brown { 37770f7887c4SLen Brown if (has_slv_msrs(family, model)) 37780f7887c4SLen Brown return 0; 37790f7887c4SLen Brown 3780d7899447SLen Brown switch (model) { 3781d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3782869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3783869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3784d7899447SLen Brown return 0; 3785d7899447SLen Brown default: 3786d7899447SLen Brown return 1; 3787d7899447SLen Brown } 3788d7899447SLen Brown } 37890f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 37900f7887c4SLen Brown { 37910f7887c4SLen Brown if (has_slv_msrs(family, model)) 37920f7887c4SLen Brown return 1; 37930f7887c4SLen Brown 37940f7887c4SLen Brown return 0; 37950f7887c4SLen Brown } 37966574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 37976574a5d5SLen Brown { 37986574a5d5SLen Brown if (!genuine_intel) 37996574a5d5SLen Brown return 0; 38006574a5d5SLen Brown 38016574a5d5SLen Brown if (family != 6) 38026574a5d5SLen Brown return 0; 38036574a5d5SLen Brown 38046574a5d5SLen Brown switch (model) { 3805869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3806869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 38076574a5d5SLen Brown return 1; 38086574a5d5SLen Brown default: 38096574a5d5SLen Brown return 0; 38106574a5d5SLen Brown } 38116574a5d5SLen Brown } 3812fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3813fcd17211SLen Brown { 3814fcd17211SLen Brown if (!genuine_intel) 3815fcd17211SLen Brown return 0; 3816fcd17211SLen Brown 3817fcd17211SLen Brown if (family != 6) 3818fcd17211SLen Brown return 0; 3819fcd17211SLen Brown 3820fcd17211SLen Brown switch (model) { 3821869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3822fcd17211SLen Brown return 1; 3823fcd17211SLen Brown default: 3824fcd17211SLen Brown return 0; 3825fcd17211SLen Brown } 3826fcd17211SLen Brown } 3827fcd17211SLen Brown 3828fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3829fb5d4327SDasaratharaman Chandramouli { 3830fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3831fb5d4327SDasaratharaman Chandramouli return 0; 3832fb5d4327SDasaratharaman Chandramouli 3833fb5d4327SDasaratharaman Chandramouli if (family != 6) 3834fb5d4327SDasaratharaman Chandramouli return 0; 3835fb5d4327SDasaratharaman Chandramouli 3836fb5d4327SDasaratharaman Chandramouli switch (model) { 3837869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3838fb5d4327SDasaratharaman Chandramouli return 1; 3839fb5d4327SDasaratharaman Chandramouli default: 3840fb5d4327SDasaratharaman Chandramouli return 0; 3841fb5d4327SDasaratharaman Chandramouli } 3842fb5d4327SDasaratharaman Chandramouli } 384331e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 384431e07522SLen Brown { 384531e07522SLen Brown if (!genuine_intel) 384631e07522SLen Brown return 0; 384731e07522SLen Brown 384831e07522SLen Brown if (family != 6) 384931e07522SLen Brown return 0; 385031e07522SLen Brown 385131e07522SLen Brown switch (model) { 385231e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 385331e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 385425368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 385531e07522SLen Brown return 1; 385631e07522SLen Brown default: 385731e07522SLen Brown return 0; 385831e07522SLen Brown } 385931e07522SLen Brown } 38606fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 38616fb3143bSLen Brown { 38626fb3143bSLen Brown if (!genuine_intel) 38636fb3143bSLen Brown return 0; 38646fb3143bSLen Brown 38656fb3143bSLen Brown if (family != 6) 38666fb3143bSLen Brown return 0; 38676fb3143bSLen Brown 38686fb3143bSLen Brown switch (model) { 3869869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3870c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3871869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 387277e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 38735e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3874c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 38755e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3876869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3877af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3878af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3879869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 388025368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 38816fb3143bSLen Brown 3882869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 38836fb3143bSLen Brown return 1; 38846fb3143bSLen Brown default: 38856fb3143bSLen Brown return 0; 38866fb3143bSLen Brown } 38876fb3143bSLen Brown } 38886fb3143bSLen Brown 3889fcd17211SLen Brown static void 3890fecb3bc8SDavid Arcari remove_underbar(char *s) 3891fecb3bc8SDavid Arcari { 3892fecb3bc8SDavid Arcari char *to = s; 3893fecb3bc8SDavid Arcari 3894fecb3bc8SDavid Arcari while (*s) { 3895fecb3bc8SDavid Arcari if (*s != '_') 3896fecb3bc8SDavid Arcari *to++ = *s; 3897fecb3bc8SDavid Arcari s++; 3898fecb3bc8SDavid Arcari } 3899fecb3bc8SDavid Arcari 3900fecb3bc8SDavid Arcari *to = 0; 3901fecb3bc8SDavid Arcari } 3902fecb3bc8SDavid Arcari 3903fecb3bc8SDavid Arcari static void 39041b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3905fcd17211SLen Brown { 3906fcd17211SLen Brown if (!do_nhm_platform_info) 3907fcd17211SLen Brown return; 3908fcd17211SLen Brown 3909fcd17211SLen Brown dump_nhm_platform_info(); 3910fcd17211SLen Brown 3911fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3912fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3913fcd17211SLen Brown 3914fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3915fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3916fcd17211SLen Brown 391731e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 391831e07522SLen Brown dump_turbo_ratio_limits(family, model); 3919fcd17211SLen Brown 39200f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 39210f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 39220f7887c4SLen Brown 3923fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3924fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3925fb5d4327SDasaratharaman Chandramouli 39266fb3143bSLen Brown if (has_config_tdp(family, model)) 39276fb3143bSLen Brown dump_config_tdp(); 39286fb3143bSLen Brown 3929fcd17211SLen Brown dump_nhm_cst_cfg(); 3930fcd17211SLen Brown } 3931fcd17211SLen Brown 3932abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 3933abdcbdb2SLen Brown { 3934abdcbdb2SLen Brown FILE *input; 3935abdcbdb2SLen Brown char cpuidle_buf[64]; 3936abdcbdb2SLen Brown 3937abdcbdb2SLen Brown input = fopen(path, "r"); 3938abdcbdb2SLen Brown if (input == NULL) { 3939abdcbdb2SLen Brown if (debug) 3940abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 3941abdcbdb2SLen Brown return; 3942abdcbdb2SLen Brown } 3943abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 3944abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 3945abdcbdb2SLen Brown fclose(input); 3946abdcbdb2SLen Brown 3947abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 3948abdcbdb2SLen Brown } 394941618e63SLen Brown static void 395041618e63SLen Brown dump_sysfs_cstate_config(void) 395141618e63SLen Brown { 395241618e63SLen Brown char path[64]; 395341618e63SLen Brown char name_buf[16]; 395441618e63SLen Brown char desc[64]; 395541618e63SLen Brown FILE *input; 395641618e63SLen Brown int state; 395741618e63SLen Brown char *sp; 395841618e63SLen Brown 3959abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 3960abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 3961abdcbdb2SLen Brown return; 3962abdcbdb2SLen Brown } 3963abdcbdb2SLen Brown 3964abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 3965abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 3966abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 3967abdcbdb2SLen Brown 396841618e63SLen Brown for (state = 0; state < 10; ++state) { 396941618e63SLen Brown 397041618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 397141618e63SLen Brown base_cpu, state); 397241618e63SLen Brown input = fopen(path, "r"); 397341618e63SLen Brown if (input == NULL) 397441618e63SLen Brown continue; 39758173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 39768173c336SBen Hutchings err(1, "%s: failed to read file", path); 397741618e63SLen Brown 397841618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 397941618e63SLen Brown sp = strchr(name_buf, '-'); 398041618e63SLen Brown if (!sp) 398141618e63SLen Brown sp = strchrnul(name_buf, '\n'); 398241618e63SLen Brown *sp = '\0'; 398341618e63SLen Brown fclose(input); 398441618e63SLen Brown 3985fecb3bc8SDavid Arcari remove_underbar(name_buf); 3986fecb3bc8SDavid Arcari 398741618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 398841618e63SLen Brown base_cpu, state); 398941618e63SLen Brown input = fopen(path, "r"); 399041618e63SLen Brown if (input == NULL) 399141618e63SLen Brown continue; 39928173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 39938173c336SBen Hutchings err(1, "%s: failed to read file", path); 399441618e63SLen Brown 399541618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 399641618e63SLen Brown fclose(input); 399741618e63SLen Brown } 399841618e63SLen Brown } 39997293fccdSLen Brown static void 40007293fccdSLen Brown dump_sysfs_pstate_config(void) 40017293fccdSLen Brown { 40027293fccdSLen Brown char path[64]; 40037293fccdSLen Brown char driver_buf[64]; 40047293fccdSLen Brown char governor_buf[64]; 40057293fccdSLen Brown FILE *input; 40067293fccdSLen Brown int turbo; 40077293fccdSLen Brown 40087293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 40097293fccdSLen Brown base_cpu); 40107293fccdSLen Brown input = fopen(path, "r"); 40117293fccdSLen Brown if (input == NULL) { 40120a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40137293fccdSLen Brown return; 40147293fccdSLen Brown } 40158173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 40168173c336SBen Hutchings err(1, "%s: failed to read file", path); 40177293fccdSLen Brown fclose(input); 40187293fccdSLen Brown 40197293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 40207293fccdSLen Brown base_cpu); 40217293fccdSLen Brown input = fopen(path, "r"); 40227293fccdSLen Brown if (input == NULL) { 40230a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40247293fccdSLen Brown return; 40257293fccdSLen Brown } 40268173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 40278173c336SBen Hutchings err(1, "%s: failed to read file", path); 40287293fccdSLen Brown fclose(input); 40297293fccdSLen Brown 40307293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 40317293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 40327293fccdSLen Brown 40337293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 40347293fccdSLen Brown input = fopen(path, "r"); 40357293fccdSLen Brown if (input != NULL) { 40368173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 40378173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 40387293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 40397293fccdSLen Brown fclose(input); 40407293fccdSLen Brown } 40417293fccdSLen Brown 40427293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 40437293fccdSLen Brown input = fopen(path, "r"); 40447293fccdSLen Brown if (input != NULL) { 40458173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 40468173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 40477293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 40487293fccdSLen Brown fclose(input); 40497293fccdSLen Brown } 40507293fccdSLen Brown } 405141618e63SLen Brown 40526574a5d5SLen 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 } 40987f5c258eSLen Brown /* 40997f5c258eSLen Brown * print_hwp() 41007f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 41017f5c258eSLen Brown */ 41027f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 41037f5c258eSLen Brown { 41047f5c258eSLen Brown unsigned long long msr; 41057f5c258eSLen Brown int cpu; 41067f5c258eSLen Brown 41077f5c258eSLen Brown if (!has_hwp) 41087f5c258eSLen Brown return 0; 41097f5c258eSLen Brown 41107f5c258eSLen Brown cpu = t->cpu_id; 41117f5c258eSLen Brown 41127f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 41137f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 41147f5c258eSLen Brown return 0; 41157f5c258eSLen Brown 41167f5c258eSLen Brown if (cpu_migrate(cpu)) { 41173d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 41187f5c258eSLen Brown return -1; 41197f5c258eSLen Brown } 41207f5c258eSLen Brown 41217f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 41227f5c258eSLen Brown return 0; 41237f5c258eSLen Brown 4124b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 41257f5c258eSLen Brown 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), 41397f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 41407f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 41417f5c258eSLen Brown 41427f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 41437f5c258eSLen Brown return 0; 41447f5c258eSLen Brown 4145b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 41466dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 41477f5c258eSLen Brown cpu, msr, 41487f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 41497f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 41507f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 41517f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 41527f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 41537f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 41547f5c258eSLen Brown 41557f5c258eSLen Brown if (has_hwp_pkg) { 41567f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 41577f5c258eSLen Brown return 0; 41587f5c258eSLen Brown 4159b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 41606dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 41617f5c258eSLen Brown cpu, msr, 41627f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 41637f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 41647f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 41657f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 41667f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 41677f5c258eSLen Brown } 41687f5c258eSLen Brown if (has_hwp_notify) { 41697f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 41707f5c258eSLen Brown return 0; 41717f5c258eSLen Brown 4172b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 41737f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 41747f5c258eSLen Brown cpu, msr, 41757f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 41767f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 41777f5c258eSLen Brown } 41787f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 41797f5c258eSLen Brown return 0; 41807f5c258eSLen Brown 4181b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 41827f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 41837f5c258eSLen Brown cpu, msr, 41847f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 41857f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 4186889facbeSLen Brown 4187889facbeSLen Brown return 0; 4188889facbeSLen Brown } 4189889facbeSLen Brown 41903a9a941dSLen Brown /* 41913a9a941dSLen Brown * print_perf_limit() 41923a9a941dSLen Brown */ 41933a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 41943a9a941dSLen Brown { 41953a9a941dSLen Brown unsigned long long msr; 41963a9a941dSLen Brown int cpu; 41973a9a941dSLen Brown 41983a9a941dSLen Brown cpu = t->cpu_id; 41993a9a941dSLen Brown 42003a9a941dSLen Brown /* per-package */ 42013a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 42023a9a941dSLen Brown return 0; 42033a9a941dSLen Brown 42043a9a941dSLen Brown if (cpu_migrate(cpu)) { 42053d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 42063a9a941dSLen Brown return -1; 42073a9a941dSLen Brown } 42083a9a941dSLen Brown 42093a9a941dSLen Brown if (do_core_perf_limit_reasons) { 42103a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4211b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4212b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4213e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 42143a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4215e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4216e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4217e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4218e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4219e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4220e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4221e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4222e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4223e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4224e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 4225e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 4226e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 4227b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4228e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 42293a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4230e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4231e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4232e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4233e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4234e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4235e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4236e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4237e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4238e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4239e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 4240e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 4241e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 42423a9a941dSLen Brown 42433a9a941dSLen Brown } 42443a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 42453a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4246b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4247b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 42483a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 42493a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 42503a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 42513a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 42523a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 42533a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 42543a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 42553a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4256b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 42573a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 42583a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 42593a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 42603a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 42613a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 42623a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 42633a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 42643a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 42653a9a941dSLen Brown } 42663a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 42673a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4268b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4269b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 42703a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 42713a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 42723a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 42733a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 42743a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 42753a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4276b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 42773a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 42783a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 42793a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 42803a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 42813a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 42823a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 42833a9a941dSLen Brown } 42843a9a941dSLen Brown return 0; 42853a9a941dSLen Brown } 42863a9a941dSLen Brown 4287889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4288889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4289889facbeSLen Brown 42909392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4291144b44b1SLen Brown { 4292144b44b1SLen Brown unsigned long long msr; 4293144b44b1SLen Brown 4294144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 42957ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4296144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4297144b44b1SLen Brown 4298144b44b1SLen Brown switch (model) { 4299f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 43005ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4301144b44b1SLen Brown return 30.0; 4302144b44b1SLen Brown default: 4303144b44b1SLen Brown return 135.0; 4304144b44b1SLen Brown } 4305144b44b1SLen Brown } 4306144b44b1SLen Brown 43079392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 43089392bd98SCalvin Walton { 430933eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 431033eb8225SKim Phillips return 280.0; 43119392bd98SCalvin Walton } 43129392bd98SCalvin Walton 431340ee8e3bSAndrey Semin /* 431440ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 431540ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 431640ee8e3bSAndrey Semin */ 431740ee8e3bSAndrey Semin static double 431840ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 431940ee8e3bSAndrey Semin { 432040ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 432140ee8e3bSAndrey Semin 432240ee8e3bSAndrey Semin switch (model) { 4323869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4324869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4325abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4326869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 432740ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 432840ee8e3bSAndrey Semin default: 432940ee8e3bSAndrey Semin return (rapl_energy_units); 433040ee8e3bSAndrey Semin } 433140ee8e3bSAndrey Semin } 433240ee8e3bSAndrey Semin 43339392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4334889facbeSLen Brown { 4335889facbeSLen Brown unsigned long long msr; 4336144b44b1SLen Brown unsigned int time_unit; 4337889facbeSLen Brown double tdp; 4338889facbeSLen Brown 4339889facbeSLen Brown if (family != 6) 4340889facbeSLen Brown return; 4341889facbeSLen Brown 4342889facbeSLen Brown switch (model) { 4343869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4344869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4345c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 434677e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 43475e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4348c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 43495e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4350144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4351812db3f7SLen Brown if (rapl_joules) { 4352812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4353812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4354812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4355812db3f7SLen Brown } else { 4356812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4357812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4358812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4359812db3f7SLen Brown } 4360889facbeSLen Brown break; 4361869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4362f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4363e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4364812db3f7SLen Brown if (rapl_joules) 4365812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4366812db3f7SLen Brown else 4367812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4368e4085d54SLen Brown break; 4369f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4370f6708400SChen Yu do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO; 4371f6708400SChen Yu if (rapl_joules) { 4372f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4373f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4374f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4375f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4376f6708400SChen Yu } else { 4377f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4378f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4379f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4380f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4381f6708400SChen Yu } 4382f6708400SChen Yu break; 438320de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 438420de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 438520de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 438620de0dabSAntti Laakso if (rapl_joules) 438720de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 438820de0dabSAntti Laakso else 438920de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 439020de0dabSAntti Laakso break; 4391af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4392af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 439381824921SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO; 4394812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4395812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4396812db3f7SLen Brown if (rapl_joules) { 4397812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4398812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4399812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 440081824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4401812db3f7SLen Brown } else { 4402812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4403812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4404812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 440581824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4406812db3f7SLen Brown } 44070b2bb692SLen Brown break; 4408869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4409869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4410869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 441125368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4412869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 44130b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 4414812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4415812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4416812db3f7SLen Brown if (rapl_joules) { 4417812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4418812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4419812db3f7SLen Brown } else { 4420812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4421812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4422812db3f7SLen Brown } 4423e6f9bb3cSLen Brown break; 4424869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4425869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 44260b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4427812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4428812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4429812db3f7SLen Brown if (rapl_joules) { 4430812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4431812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4432812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4433812db3f7SLen Brown } else { 4434812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4435812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4436812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4437812db3f7SLen Brown } 4438144b44b1SLen Brown break; 4439f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 44405ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4441144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4442812db3f7SLen Brown if (rapl_joules) { 4443812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4444812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4445812db3f7SLen Brown } else { 4446812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4447812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4448812db3f7SLen Brown } 4449889facbeSLen Brown break; 44505ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 44510f644909SJacob Pan do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4452812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4453812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4454812db3f7SLen Brown if (rapl_joules) { 4455812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4456812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4457812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4458812db3f7SLen Brown } else { 4459812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4460812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4461812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4462812db3f7SLen Brown } 44630f644909SJacob Pan break; 4464889facbeSLen Brown default: 4465889facbeSLen Brown return; 4466889facbeSLen Brown } 4467889facbeSLen Brown 4468889facbeSLen Brown /* units on package 0, verify later other packages match */ 44697ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4470889facbeSLen Brown return; 4471889facbeSLen Brown 4472889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4473f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4474144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4475144b44b1SLen Brown else 4476889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4477889facbeSLen Brown 447840ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 447940ee8e3bSAndrey Semin 4480144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4481144b44b1SLen Brown if (time_unit == 0) 4482144b44b1SLen Brown time_unit = 0xA; 4483889facbeSLen Brown 4484144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4485144b44b1SLen Brown 44869392bd98SCalvin Walton tdp = get_tdp_intel(model); 4487889facbeSLen Brown 4488889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 448996e47158SLen Brown if (!quiet) 4490b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 44919392bd98SCalvin Walton } 4492889facbeSLen Brown 44939392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 44949392bd98SCalvin Walton { 44959392bd98SCalvin Walton unsigned long long msr; 44969392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 44979392bd98SCalvin Walton unsigned int has_rapl = 0; 44989392bd98SCalvin Walton double tdp; 44999392bd98SCalvin Walton 45009392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 45019392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 450233eb8225SKim Phillips /* RAPL (Fam 17h+) */ 45039392bd98SCalvin Walton has_rapl = edx & (1 << 14); 45049392bd98SCalvin Walton } 45059392bd98SCalvin Walton 450633eb8225SKim Phillips if (!has_rapl || family < 0x17) 4507889facbeSLen Brown return; 45089392bd98SCalvin Walton 45099392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 45103316f99aSCalvin Walton if (rapl_joules) { 45113316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 45129392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 45133316f99aSCalvin Walton } else { 45143316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 45159392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 45163316f99aSCalvin Walton } 45179392bd98SCalvin Walton 45189392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 45199392bd98SCalvin Walton return; 45209392bd98SCalvin Walton 45219392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 45229392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 45239392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 45249392bd98SCalvin Walton 45259cfa8e04SPu Wen tdp = get_tdp_amd(family); 45269392bd98SCalvin Walton 45279392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 45289392bd98SCalvin Walton if (!quiet) 45299392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 45309392bd98SCalvin Walton } 45319392bd98SCalvin Walton 45329392bd98SCalvin Walton /* 45339392bd98SCalvin Walton * rapl_probe() 45349392bd98SCalvin Walton * 45359392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 45369392bd98SCalvin Walton */ 45379392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 45389392bd98SCalvin Walton { 45399392bd98SCalvin Walton if (genuine_intel) 45409392bd98SCalvin Walton rapl_probe_intel(family, model); 4541c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 45429392bd98SCalvin Walton rapl_probe_amd(family, model); 4543889facbeSLen Brown } 4544889facbeSLen Brown 45451b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 45463a9a941dSLen Brown { 45473a9a941dSLen Brown if (!genuine_intel) 45483a9a941dSLen Brown return; 45493a9a941dSLen Brown 45503a9a941dSLen Brown if (family != 6) 45513a9a941dSLen Brown return; 45523a9a941dSLen Brown 45533a9a941dSLen Brown switch (model) { 4554c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 455577e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 45565e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 45573a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4558869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 45593a9a941dSLen Brown do_core_perf_limit_reasons = 1; 45603a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 45613a9a941dSLen Brown default: 45623a9a941dSLen Brown return; 45633a9a941dSLen Brown } 45643a9a941dSLen Brown } 45653a9a941dSLen Brown 4566ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4567ac980e13SArtem Bityutskiy { 456825368d7cSChen Yu if (is_skx(family, model) || is_bdx(family, model) || 456925368d7cSChen Yu is_icx(family, model)) 4570ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4571ac980e13SArtem Bityutskiy } 4572ac980e13SArtem Bityutskiy 4573aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model) 4574aeb01e6dSChen Yu { 4575aeb01e6dSChen Yu if (is_icx(family, model)) 4576aeb01e6dSChen Yu dis_cstate_prewake = 1; 4577aeb01e6dSChen Yu } 4578aeb01e6dSChen Yu 4579889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4580889facbeSLen Brown { 4581889facbeSLen Brown unsigned long long msr; 4582f4896fa5SLen Brown unsigned int dts, dts2; 4583889facbeSLen Brown int cpu; 4584889facbeSLen Brown 4585889facbeSLen Brown if (!(do_dts || do_ptm)) 4586889facbeSLen Brown return 0; 4587889facbeSLen Brown 4588889facbeSLen Brown cpu = t->cpu_id; 4589889facbeSLen Brown 4590889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4591889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4592889facbeSLen Brown return 0; 4593889facbeSLen Brown 4594889facbeSLen Brown if (cpu_migrate(cpu)) { 45953d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4596889facbeSLen Brown return -1; 4597889facbeSLen Brown } 4598889facbeSLen Brown 4599889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4600889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4601889facbeSLen Brown return 0; 4602889facbeSLen Brown 4603889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4604b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 4605889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 4606889facbeSLen Brown 4607889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4608889facbeSLen Brown return 0; 4609889facbeSLen Brown 4610889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4611889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4612b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4613889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4614889facbeSLen Brown } 4615889facbeSLen Brown 4616889facbeSLen Brown 4617f4896fa5SLen Brown if (do_dts && debug) { 4618889facbeSLen Brown unsigned int resolution; 4619889facbeSLen Brown 4620889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4621889facbeSLen Brown return 0; 4622889facbeSLen Brown 4623889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4624889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4625b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 4626889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 4627889facbeSLen Brown 4628889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4629889facbeSLen Brown return 0; 4630889facbeSLen Brown 4631889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4632889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4633b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4634889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4635889facbeSLen Brown } 4636889facbeSLen Brown 4637889facbeSLen Brown return 0; 4638889facbeSLen Brown } 4639889facbeSLen Brown 4640889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4641889facbeSLen Brown { 4642b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4643889facbeSLen Brown cpu, label, 4644889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4645889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4646889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4647889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4648889facbeSLen Brown 4649889facbeSLen Brown return; 4650889facbeSLen Brown } 4651889facbeSLen Brown 4652889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4653889facbeSLen Brown { 4654889facbeSLen Brown unsigned long long msr; 46559392bd98SCalvin Walton const char *msr_name; 4656889facbeSLen Brown int cpu; 4657889facbeSLen Brown 4658889facbeSLen Brown if (!do_rapl) 4659889facbeSLen Brown return 0; 4660889facbeSLen Brown 4661889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4662889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4663889facbeSLen Brown return 0; 4664889facbeSLen Brown 4665889facbeSLen Brown cpu = t->cpu_id; 4666889facbeSLen Brown if (cpu_migrate(cpu)) { 46673d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4668889facbeSLen Brown return -1; 4669889facbeSLen Brown } 4670889facbeSLen Brown 46719392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 46729392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 46739392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 46749392bd98SCalvin Walton return -1; 46759392bd98SCalvin Walton } else { 46769392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4677889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4678889facbeSLen Brown return -1; 46799392bd98SCalvin Walton } 4680889facbeSLen Brown 46819392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4682144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 468396e47158SLen Brown 4684144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4685144b44b1SLen Brown 4686889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4687889facbeSLen Brown return -5; 4688889facbeSLen Brown 4689889facbeSLen Brown 4690b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4691889facbeSLen Brown cpu, msr, 4692889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4693889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4694889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4695889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4696889facbeSLen Brown 4697144b44b1SLen Brown } 4698144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4699144b44b1SLen Brown 4700889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4701889facbeSLen Brown return -9; 4702889facbeSLen Brown 4703b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4704889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4705889facbeSLen Brown 4706889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4707b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4708889facbeSLen Brown cpu, 4709889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4710889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4711889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4712889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4713889facbeSLen Brown } 4714889facbeSLen Brown 47150b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4716889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4717889facbeSLen Brown return -6; 4718889facbeSLen Brown 4719b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4720889facbeSLen Brown cpu, msr, 4721889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4722889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4723889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4724889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 47250b2bb692SLen Brown } 47260b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4727889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4728889facbeSLen Brown return -9; 4729b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4730889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4731889facbeSLen Brown 4732889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4733889facbeSLen Brown } 4734144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4735889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4736889facbeSLen Brown return -7; 4737889facbeSLen Brown 4738b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4739144b44b1SLen Brown } 47409148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4741889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4742889facbeSLen Brown return -9; 4743b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4744889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4745889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4746889facbeSLen Brown } 4747889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4748889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4749889facbeSLen Brown return -8; 4750889facbeSLen Brown 4751b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4752889facbeSLen Brown 4753889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4754889facbeSLen Brown return -9; 4755b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4756889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4757889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4758889facbeSLen Brown } 4759889facbeSLen Brown return 0; 4760889facbeSLen Brown } 4761889facbeSLen Brown 4762d7899447SLen Brown /* 4763d7899447SLen Brown * SNB adds support for additional MSRs: 4764d7899447SLen Brown * 4765d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4766d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4767d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4768d7899447SLen Brown */ 4769103a8feaSLen Brown 4770d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4771103a8feaSLen Brown { 4772103a8feaSLen Brown if (!genuine_intel) 4773103a8feaSLen Brown return 0; 4774103a8feaSLen Brown 4775103a8feaSLen Brown switch (model) { 4776869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4777869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4778869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4779869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4780c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4781869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 478277e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 47835e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4784c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 47855e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4786869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4787af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4788af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4789869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 479025368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4791869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4792f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 47935ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4794f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 479520de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4796103a8feaSLen Brown return 1; 4797103a8feaSLen Brown } 4798103a8feaSLen Brown return 0; 4799103a8feaSLen Brown } 4800103a8feaSLen Brown 4801d7899447SLen Brown /* 4802570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4803d7899447SLen Brown * 4804d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4805d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4806d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 48075a63426eSLen Brown * 48085a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 48095a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 48105a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 48115a63426eSLen Brown * 4812d7899447SLen Brown */ 4813570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4814ca58710fSKristen Carlson Accardi { 4815ca58710fSKristen Carlson Accardi if (!genuine_intel) 4816ca58710fSKristen Carlson Accardi return 0; 4817ca58710fSKristen Carlson Accardi 4818ca58710fSKristen Carlson Accardi switch (model) { 481977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4820c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4821af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4822af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4823869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4824f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4825f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4826ca58710fSKristen Carlson Accardi return 1; 4827ca58710fSKristen Carlson Accardi } 4828ca58710fSKristen Carlson Accardi return 0; 4829ca58710fSKristen Carlson Accardi } 4830ca58710fSKristen Carlson Accardi 48310b2bb692SLen Brown /* 48320b2bb692SLen Brown * SKL adds support for additional MSRS: 48330b2bb692SLen Brown * 48340b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 48350b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 48360b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 48370b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 48380b2bb692SLen Brown */ 48390b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 48400b2bb692SLen Brown { 48410b2bb692SLen Brown if (!genuine_intel) 48420b2bb692SLen Brown return 0; 48430b2bb692SLen Brown 48440b2bb692SLen Brown switch (model) { 4845af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4846af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 48470b2bb692SLen Brown return 1; 48480b2bb692SLen Brown } 48490b2bb692SLen Brown return 0; 48500b2bb692SLen Brown } 48510b2bb692SLen Brown 4852144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4853144b44b1SLen Brown { 4854144b44b1SLen Brown if (!genuine_intel) 4855144b44b1SLen Brown return 0; 4856144b44b1SLen Brown switch (model) { 4857f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 48585ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4859144b44b1SLen Brown return 1; 4860144b44b1SLen Brown } 4861144b44b1SLen Brown return 0; 4862144b44b1SLen Brown } 4863144b44b1SLen Brown 4864fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4865fb5d4327SDasaratharaman Chandramouli { 4866fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4867fb5d4327SDasaratharaman Chandramouli return 0; 4868fb5d4327SDasaratharaman Chandramouli switch (model) { 4869869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4870fb5d4327SDasaratharaman Chandramouli return 1; 4871fb5d4327SDasaratharaman Chandramouli } 4872fb5d4327SDasaratharaman Chandramouli return 0; 4873fb5d4327SDasaratharaman Chandramouli } 4874fb5d4327SDasaratharaman Chandramouli 4875997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4876997e5395SSrinivas Pandruvada { 4877997e5395SSrinivas Pandruvada if (!genuine_intel) 4878997e5395SSrinivas Pandruvada return 0; 4879997e5395SSrinivas Pandruvada 4880997e5395SSrinivas Pandruvada switch (model) { 4881af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4882997e5395SSrinivas Pandruvada return 1; 4883997e5395SSrinivas Pandruvada } 4884997e5395SSrinivas Pandruvada 4885997e5395SSrinivas Pandruvada return 0; 4886997e5395SSrinivas Pandruvada } 4887997e5395SSrinivas Pandruvada 4888b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4889b2b34dfeSHubert Chrzaniuk { 4890b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4891b2b34dfeSHubert Chrzaniuk return 1024; 4892b2b34dfeSHubert Chrzaniuk return 1; 4893b2b34dfeSHubert Chrzaniuk } 4894b2b34dfeSHubert Chrzaniuk 4895144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4896144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4897144b44b1SLen Brown 4898144b44b1SLen Brown double slm_bclk(void) 4899144b44b1SLen Brown { 4900144b44b1SLen Brown unsigned long long msr = 3; 4901144b44b1SLen Brown unsigned int i; 4902144b44b1SLen Brown double freq; 4903144b44b1SLen Brown 49047ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4905b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4906144b44b1SLen Brown 4907144b44b1SLen Brown i = msr & 0xf; 4908144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4909b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 49100a91e551SColin Ian King i = 3; 4911144b44b1SLen Brown } 4912144b44b1SLen Brown freq = slm_freq_table[i]; 4913144b44b1SLen Brown 491496e47158SLen Brown if (!quiet) 4915b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4916144b44b1SLen Brown 4917144b44b1SLen Brown return freq; 4918144b44b1SLen Brown } 4919144b44b1SLen Brown 4920103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4921103a8feaSLen Brown { 4922121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4923103a8feaSLen Brown return 100.00; 4924144b44b1SLen Brown else if (is_slm(family, model)) 4925144b44b1SLen Brown return slm_bclk(); 4926103a8feaSLen Brown else 4927103a8feaSLen Brown return 133.33; 4928103a8feaSLen Brown } 4929103a8feaSLen Brown 49307ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p) 49317ab5ff49SZhang Rui { 49327ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx; 49337ab5ff49SZhang Rui 49347ab5ff49SZhang Rui if (!genuine_intel) 49357ab5ff49SZhang Rui return 0; 49367ab5ff49SZhang Rui 49377ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) { 49387ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 49397ab5ff49SZhang Rui return -1; 49407ab5ff49SZhang Rui } 49417ab5ff49SZhang Rui 49427ab5ff49SZhang Rui if (max_level < 0x1a) 49437ab5ff49SZhang Rui return 0; 49447ab5ff49SZhang Rui 49457ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx); 49467ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF; 49477ab5ff49SZhang Rui if (eax == 0x20 ) 49487ab5ff49SZhang Rui t->is_atom = true; 49497ab5ff49SZhang Rui return 0; 49507ab5ff49SZhang Rui } 49517ab5ff49SZhang Rui 4952889facbeSLen Brown /* 4953889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4954889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4955889facbeSLen Brown * This is usually equal to tjMax. 4956889facbeSLen Brown * 4957889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4958889facbeSLen Brown * but also allow cmdline over-ride with -T. 4959889facbeSLen Brown * 4960889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4961889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4962889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4963889facbeSLen Brown */ 49646ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 49656ff7cb37SLen Brown { 4966b2b94be7SLen Brown unsigned long long msr; 4967b2b94be7SLen Brown unsigned int target_c_local; 4968b2b94be7SLen Brown int cpu; 4969b2b94be7SLen Brown 4970889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4971889facbeSLen Brown if (!(do_dts || do_ptm)) 4972889facbeSLen Brown return 0; 4973889facbeSLen Brown 4974889facbeSLen Brown /* this is a per-package concept */ 4975889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4976889facbeSLen Brown return 0; 4977889facbeSLen Brown 4978b2b94be7SLen Brown cpu = t->cpu_id; 4979b2b94be7SLen Brown if (cpu_migrate(cpu)) { 4980b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4981b2b94be7SLen Brown return -1; 4982b2b94be7SLen Brown } 4983b2b94be7SLen Brown 4984889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4985889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 4986b2b94be7SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 4987b2b94be7SLen Brown cpu, tcc_activation_temp); 4988889facbeSLen Brown return 0; 4989889facbeSLen Brown } 4990889facbeSLen Brown 4991b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4992b2b94be7SLen Brown if (!do_nhm_platform_info) 4993b2b94be7SLen Brown goto guess; 4994b2b94be7SLen Brown 4995b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4996b2b94be7SLen Brown goto guess; 4997b2b94be7SLen Brown 4998b2b94be7SLen Brown target_c_local = (msr >> 16) & 0xFF; 4999b2b94be7SLen Brown 5000b2b94be7SLen Brown if (!quiet) 5001b2b94be7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 5002b2b94be7SLen Brown cpu, msr, target_c_local); 5003b2b94be7SLen Brown 5004b2b94be7SLen Brown if (!target_c_local) 5005b2b94be7SLen Brown goto guess; 5006b2b94be7SLen Brown 5007b2b94be7SLen Brown tcc_activation_temp = target_c_local; 5008b2b94be7SLen Brown 5009889facbeSLen Brown return 0; 5010889facbeSLen Brown 5011b2b94be7SLen Brown guess: 5012889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 5013b2b94be7SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 5014b2b94be7SLen Brown cpu, tcc_activation_temp); 5015889facbeSLen Brown 5016889facbeSLen Brown return 0; 5017889facbeSLen Brown } 501869807a63SLen Brown 5019aa8d8cc7SLen Brown void decode_feature_control_msr(void) 5020aa8d8cc7SLen Brown { 5021aa8d8cc7SLen Brown unsigned long long msr; 5022aa8d8cc7SLen Brown 5023f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 5024aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 5025aa8d8cc7SLen Brown base_cpu, msr, 5026f6505c88SSean Christopherson msr & FEAT_CTL_LOCKED ? "" : "UN-", 5027aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 5028aa8d8cc7SLen Brown } 5029aa8d8cc7SLen Brown 503069807a63SLen Brown void decode_misc_enable_msr(void) 503169807a63SLen Brown { 503269807a63SLen Brown unsigned long long msr; 503369807a63SLen Brown 5034f26b1519SLen Brown if (!genuine_intel) 5035f26b1519SLen Brown return; 5036f26b1519SLen Brown 503769807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 5038e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 503969807a63SLen Brown base_cpu, msr, 5040e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 5041e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 5042fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 5043e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 5044e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 504569807a63SLen Brown } 504669807a63SLen Brown 504733148d67SLen Brown void decode_misc_feature_control(void) 504833148d67SLen Brown { 504933148d67SLen Brown unsigned long long msr; 505033148d67SLen Brown 505133148d67SLen Brown if (!has_misc_feature_control) 505233148d67SLen Brown return; 505333148d67SLen Brown 505433148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 505533148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 505633148d67SLen Brown base_cpu, msr, 505733148d67SLen Brown msr & (0 << 0) ? "No-" : "", 505833148d67SLen Brown msr & (1 << 0) ? "No-" : "", 505933148d67SLen Brown msr & (2 << 0) ? "No-" : "", 506033148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 506133148d67SLen Brown } 5062f0057310SLen Brown /* 5063f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 5064f0057310SLen Brown * 5065f0057310SLen Brown * Decode the bits according to the Nehalem documentation 5066f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 5067f0057310SLen Brown * bit[1] less so... 5068f0057310SLen Brown */ 5069f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 5070f0057310SLen Brown { 5071f0057310SLen Brown unsigned long long msr; 5072f0057310SLen Brown 5073f0057310SLen Brown if (!do_nhm_platform_info) 5074f0057310SLen Brown return; 5075f0057310SLen Brown 5076cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 5077cf4cbe53SLen Brown return; 5078cf4cbe53SLen Brown 5079f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 5080ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 5081f0057310SLen Brown base_cpu, msr, 5082f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 5083ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 5084ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 5085f0057310SLen 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 } 5177d76bb7a0SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", 5178d76bb7a0SLen Brown value, value == 2000000000 ? "default" : "constrained"); 5179d76bb7a0SLen Brown 5180d76bb7a0SLen Brown close(fd); 5181d76bb7a0SLen Brown } 5182d76bb7a0SLen Brown 51832af4f9b8SLen Brown 51842af4f9b8SLen Brown /* 51852af4f9b8SLen Brown * Linux-perf manages the the HW instructions-retired counter 51862af4f9b8SLen Brown * by enabling when requested, and hiding rollover 51872af4f9b8SLen Brown */ 51882af4f9b8SLen Brown void linux_perf_init(void) 51892af4f9b8SLen Brown { 51902af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 51912af4f9b8SLen Brown return; 51922af4f9b8SLen Brown 51932af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 51942af4f9b8SLen Brown return; 51952af4f9b8SLen Brown 51962af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 51972af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 51982af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 51992af4f9b8SLen Brown 52002af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 52012af4f9b8SLen Brown } 52022af4f9b8SLen Brown 5203fcd17211SLen Brown void process_cpuid() 5204103a8feaSLen Brown { 520534041551SLen Brown unsigned int eax, ebx, ecx, edx; 520634041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5207b3a34e93SLen Brown unsigned int has_turbo; 5208ed0757b8SLen Brown unsigned long long ucode_patch = 0; 5209103a8feaSLen Brown 5210103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5211103a8feaSLen Brown 52125aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5213103a8feaSLen Brown 521434041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5215103a8feaSLen Brown genuine_intel = 1; 521634041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 521734041551SLen Brown authentic_amd = 1; 5218c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5219c1c10cc7SPu Wen hygon_genuine = 1; 5220103a8feaSLen Brown 522196e47158SLen Brown if (!quiet) 5222ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5223ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5224103a8feaSLen Brown 52255aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5226103a8feaSLen Brown family = (fms >> 8) & 0xf; 5227103a8feaSLen Brown model = (fms >> 4) & 0xf; 5228103a8feaSLen Brown stepping = fms & 0xf; 52295aa3d1a2SCalvin Walton if (family == 0xf) 52305aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 52315aa3d1a2SCalvin Walton if (family >= 6) 5232103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 523334041551SLen Brown ecx_flags = ecx; 523434041551SLen Brown edx_flags = edx; 5235103a8feaSLen Brown 5236ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 5237ed0757b8SLen Brown warnx("get_msr(UCODE)\n"); 5238ed0757b8SLen Brown 5239103a8feaSLen Brown /* 5240103a8feaSLen Brown * check max extended function levels of CPUID. 5241103a8feaSLen Brown * This is needed to check for invariant TSC. 5242103a8feaSLen Brown * This check is valid for both Intel and AMD. 5243103a8feaSLen Brown */ 5244103a8feaSLen Brown ebx = ecx = edx = 0; 52455aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5246103a8feaSLen Brown 524734041551SLen Brown if (!quiet) { 5248ed0757b8SLen Brown fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n", 5249ed0757b8SLen Brown family, model, stepping, family, model, stepping, (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 5250ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 525134041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 525234041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 525334041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 525434041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 525534041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 525634041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 525734041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 525834041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 525934041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 526034041551SLen Brown edx_flags & (1 << 28) ? "HT" : "-", 526134041551SLen Brown edx_flags & (1 << 29) ? "TM" : "-"); 526234041551SLen Brown } 526325368d7cSChen Yu if (genuine_intel) { 5264*e9d3092fSZhang Rui model_orig = model; 5265f5a4c76aSLen Brown model = intel_model_duplicates(model); 526625368d7cSChen Yu } 526734041551SLen Brown 526834041551SLen Brown if (!(edx_flags & (1 << 5))) 526934041551SLen Brown errx(1, "CPUID: no MSR"); 527034041551SLen Brown 527161a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5272103a8feaSLen Brown 5273103a8feaSLen Brown /* 5274103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5275103a8feaSLen Brown * this check is valid for both Intel and AMD 5276103a8feaSLen Brown */ 52775aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 52788209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5279d7899447SLen Brown } 5280103a8feaSLen Brown 5281103a8feaSLen Brown /* 5282103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5283103a8feaSLen Brown * this check is valid for both Intel and AMD 5284103a8feaSLen Brown */ 5285103a8feaSLen Brown 52865aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 52878209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5288812db3f7SLen Brown if (has_aperf) { 5289812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5290812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5291812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5292812db3f7SLen Brown } 5293889facbeSLen Brown do_dts = eax & (1 << 0); 5294812db3f7SLen Brown if (do_dts) 5295812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5296b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5297889facbeSLen Brown do_ptm = eax & (1 << 6); 5298812db3f7SLen Brown if (do_ptm) 5299812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 53007f5c258eSLen Brown has_hwp = eax & (1 << 7); 53017f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 53027f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 53037f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 53047f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5305889facbeSLen Brown has_epb = ecx & (1 << 3); 5306889facbeSLen Brown 530796e47158SLen Brown if (!quiet) 5308b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 53097f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 53107f5c258eSLen Brown has_aperf ? "" : "No-", 5311b3a34e93SLen Brown has_turbo ? "" : "No-", 53127f5c258eSLen Brown do_dts ? "" : "No-", 53137f5c258eSLen Brown do_ptm ? "" : "No-", 53147f5c258eSLen Brown has_hwp ? "" : "No-", 53157f5c258eSLen Brown has_hwp_notify ? "" : "No-", 53167f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 53177f5c258eSLen Brown has_hwp_epp ? "" : "No-", 53187f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 53197f5c258eSLen Brown has_epb ? "" : "No-"); 5320103a8feaSLen Brown 532196e47158SLen Brown if (!quiet) 532269807a63SLen Brown decode_misc_enable_msr(); 532369807a63SLen Brown 532433148d67SLen Brown 532596e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5326aa8d8cc7SLen Brown int has_sgx; 5327aa8d8cc7SLen Brown 5328aa8d8cc7SLen Brown ecx = 0; 5329aa8d8cc7SLen Brown 5330aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5331aa8d8cc7SLen Brown 5332aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5333aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5334aa8d8cc7SLen Brown 5335aa8d8cc7SLen Brown if (has_sgx) 5336aa8d8cc7SLen Brown decode_feature_control_msr(); 5337aa8d8cc7SLen Brown } 5338aa8d8cc7SLen Brown 533961a87ba7SLen Brown if (max_level >= 0x15) { 53408a5bdf41SLen Brown unsigned int eax_crystal; 53418a5bdf41SLen Brown unsigned int ebx_tsc; 53428a5bdf41SLen Brown 53438a5bdf41SLen Brown /* 53448a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 53458a5bdf41SLen Brown */ 53468a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 53475aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 53488a5bdf41SLen Brown 53498a5bdf41SLen Brown if (ebx_tsc != 0) { 53508a5bdf41SLen Brown 535196e47158SLen Brown if (!quiet && (ebx != 0)) 5352b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 53538a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 53548a5bdf41SLen Brown 53558a5bdf41SLen Brown if (crystal_hz == 0) 53568a5bdf41SLen Brown switch(model) { 5357af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5358e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5359e8efbc80SLen Brown break; 53605ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5361ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5362ec53e594SLen Brown break; 5363869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5364f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5365e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 53668a5bdf41SLen Brown break; 53678a5bdf41SLen Brown default: 53688a5bdf41SLen Brown crystal_hz = 0; 53698a5bdf41SLen Brown } 53708a5bdf41SLen Brown 53718a5bdf41SLen Brown if (crystal_hz) { 53728a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 537396e47158SLen Brown if (!quiet) 5374b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 53758a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 53768a5bdf41SLen Brown } 53778a5bdf41SLen Brown } 53788a5bdf41SLen Brown } 537961a87ba7SLen Brown if (max_level >= 0x16) { 538061a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 538161a87ba7SLen Brown 538261a87ba7SLen Brown /* 538361a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 538461a87ba7SLen Brown */ 538561a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 538661a87ba7SLen Brown 53875aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 538896e47158SLen Brown if (!quiet) 5389b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 539061a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 539161a87ba7SLen Brown } 53928a5bdf41SLen Brown 5393b2b34dfeSHubert Chrzaniuk if (has_aperf) 5394b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5395b2b34dfeSHubert Chrzaniuk 5396812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5397812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5398812db3f7SLen Brown 5399812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5400812db3f7SLen Brown do_nhm_platform_info = 1; 5401812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5402812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5403812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5404812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5405812db3f7SLen Brown } 5406d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5407812db3f7SLen Brown 5408812db3f7SLen Brown if (do_snb_cstates) 5409812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5410812db3f7SLen Brown 54115a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 54120f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 54130f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 54140f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 54150f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 54160f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 54170f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54180f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 54190f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 54200539ba11SLen Brown if (has_slv_msrs(family, model)) { 54210f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 54220f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 54230f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54240f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 54250539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 54260539ba11SLen Brown use_c1_residency_msr = 1; 54270539ba11SLen Brown } 542820de0dabSAntti Laakso if (is_jvl(family, model)) { 542920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 543020de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 543120de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 543220de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 543320de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 543420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 543520de0dabSAntti Laakso } 54367170a374SLen Brown if (is_dnv(family, model)) { 54377170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 54387170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 54397170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 54407170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 54417170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 54427170a374SLen Brown use_c1_residency_msr = 1; 54437170a374SLen Brown } 544425368d7cSChen Yu if (is_skx(family, model) || is_icx(family, model)) { 544534c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 544634c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 544734c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 544834c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 544934c76197SLen Brown } 5450ade0ebacSLen Brown if (is_bdx(family, model)) { 5451ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5452ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5453ade0ebacSLen Brown } 5454570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5455c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 54560f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5457c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 54580f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5459c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 54600f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 54610f47c08dSLen Brown } 5462570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5463a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5464a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5465a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5466a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5467a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5468a99d8730SLen Brown } 5469144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5470fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5471103a8feaSLen Brown 5472f6708400SChen Yu if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || 5473f6708400SChen Yu is_ehl(family, model)) 5474562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5475103a8feaSLen Brown 547696e47158SLen Brown if (!quiet) 5477f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5478f0057310SLen Brown 547996e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 548071616c8eSLen Brown decode_c6_demotion_policy_msr(); 548171616c8eSLen Brown 5482889facbeSLen Brown rapl_probe(family, model); 54833a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5484ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5485889facbeSLen Brown 548696e47158SLen Brown if (!quiet) 54871b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5488fcd17211SLen Brown 548941618e63SLen Brown if (!quiet) 5490d76bb7a0SLen Brown print_dev_latency(); 5491d76bb7a0SLen Brown if (!quiet) 549241618e63SLen Brown dump_sysfs_cstate_config(); 54937293fccdSLen Brown if (!quiet) 54947293fccdSLen Brown dump_sysfs_pstate_config(); 549541618e63SLen Brown 54968c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5497a2b7b749SLen Brown calculate_tsc_tweak(); 5498a2b7b749SLen Brown 5499812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5500812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5501fdf676e5SLen Brown 5502812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5503812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 550427d47356SLen Brown 5505b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5506b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5507b4b91569SRafael Antognolli 5508be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5509be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5510be0e54c4SLen Brown else 5511be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5512be0e54c4SLen Brown 55131f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 55141f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5515be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 55161f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 55171f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 55181f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 55191f81c5efSLen Brown } else { 55201f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5521be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 55221f81c5efSLen Brown } 5523be0e54c4SLen Brown 552496e47158SLen Brown if (!quiet) 552533148d67SLen Brown decode_misc_feature_control(); 552633148d67SLen Brown 5527889facbeSLen Brown return; 5528103a8feaSLen Brown } 5529103a8feaSLen Brown 5530103a8feaSLen Brown /* 5531103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5532103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5533103a8feaSLen Brown */ 5534103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5535103a8feaSLen Brown { 5536103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5537103a8feaSLen Brown return 1; 5538103a8feaSLen Brown else 5539103a8feaSLen Brown return 0; 5540103a8feaSLen Brown } 5541103a8feaSLen Brown 5542103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5543103a8feaSLen Brown { 5544103a8feaSLen Brown return 0; 5545103a8feaSLen Brown } 5546103a8feaSLen Brown 5547c98d5d94SLen Brown void topology_probe() 5548c98d5d94SLen Brown { 5549c98d5d94SLen Brown int i; 5550c98d5d94SLen Brown int max_core_id = 0; 5551c98d5d94SLen Brown int max_package_id = 0; 55526de68fe1SLen Brown int max_die_id = 0; 5553c98d5d94SLen Brown int max_siblings = 0; 5554c98d5d94SLen Brown 5555c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5556843c5791SPrarit Bhargava set_max_cpu_num(); 5557c98d5d94SLen Brown topo.num_cpus = 0; 5558c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5559c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5560812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5561c98d5d94SLen Brown 5562d8af6f5fSLen Brown if (debug > 1) 5563b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5564c98d5d94SLen Brown 5565c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5566b2c95d90SJosh Triplett if (cpus == NULL) 5567b2c95d90SJosh Triplett err(1, "calloc cpus"); 5568c98d5d94SLen Brown 5569c98d5d94SLen Brown /* 5570c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5571c98d5d94SLen Brown */ 5572c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5573b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5574b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5575c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5576c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5577c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5578c98d5d94SLen Brown 5579c98d5d94SLen Brown /* 55801ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 55811ef7d21aSLen Brown */ 55821ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 55831ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 55841ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 55851ef7d21aSLen Brown err(1, "cpu%d not present", i); 55861ef7d21aSLen Brown } 55871ef7d21aSLen Brown 55881ef7d21aSLen Brown /* 5589c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5590c98d5d94SLen Brown */ 5591c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5592b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5593b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5594c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5595c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5596c98d5d94SLen Brown 55978cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5598c98d5d94SLen Brown 5599c98d5d94SLen Brown /* 5600c98d5d94SLen Brown * For online cpus 5601c98d5d94SLen Brown * find max_core_id, max_package_id 5602c98d5d94SLen Brown */ 5603c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5604c98d5d94SLen Brown int siblings; 5605c98d5d94SLen Brown 5606c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5607d8af6f5fSLen Brown if (debug > 1) 5608b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5609c98d5d94SLen Brown continue; 5610c98d5d94SLen Brown } 5611c98d5d94SLen Brown 56120e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 56130e2d8f05SLen Brown 56140e2d8f05SLen Brown /* get package information */ 5615c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5616c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5617c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5618c98d5d94SLen Brown 56196de68fe1SLen Brown /* get die information */ 56206de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 56216de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 56226de68fe1SLen Brown max_die_id = cpus[i].die_id; 56236de68fe1SLen Brown 56240e2d8f05SLen Brown /* get numa node information */ 5625ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5626ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5627ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 56280e2d8f05SLen Brown 56290e2d8f05SLen Brown /* get core information */ 56300e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 56310e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 56320e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 56330e2d8f05SLen Brown 56340e2d8f05SLen Brown /* get thread information */ 56350e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5636c98d5d94SLen Brown if (siblings > max_siblings) 5637c98d5d94SLen Brown max_siblings = siblings; 56384f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 56398cb48b32SPrarit Bhargava topo.num_cores++; 5640c98d5d94SLen Brown } 5641ef605741SPrarit Bhargava 564270a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5643d8af6f5fSLen Brown if (debug > 1) 5644b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 564570a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 564670a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5647812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5648c98d5d94SLen Brown 56496de68fe1SLen Brown topo.num_die = max_die_id + 1; 56506de68fe1SLen Brown if (debug > 1) 56516de68fe1SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", 56526de68fe1SLen Brown max_die_id, topo.num_die); 56536de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 56546de68fe1SLen Brown BIC_PRESENT(BIC_Die); 56556de68fe1SLen Brown 5656c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5657d8af6f5fSLen Brown if (debug > 1) 5658b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 5659c98d5d94SLen Brown max_package_id, topo.num_packages); 56607da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5661812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5662c98d5d94SLen Brown 5663ef605741SPrarit Bhargava set_node_data(); 5664ef605741SPrarit Bhargava if (debug > 1) 566570a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 566601235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 566701235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5668ef605741SPrarit Bhargava 566970a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5670d8af6f5fSLen Brown if (debug > 1) 5671b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 56722ffbb224SPrarit Bhargava 56732ffbb224SPrarit Bhargava if (debug < 1) 56742ffbb224SPrarit Bhargava return; 56752ffbb224SPrarit Bhargava 56762ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 56770ec712e3SLen Brown if (cpu_is_not_present(i)) 56780ec712e3SLen Brown continue; 56792ffbb224SPrarit Bhargava fprintf(outf, 56806de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 56816de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 56822ffbb224SPrarit Bhargava cpus[i].physical_node_id, 56832ffbb224SPrarit Bhargava cpus[i].logical_node_id, 56842ffbb224SPrarit Bhargava cpus[i].physical_core_id, 56852ffbb224SPrarit Bhargava cpus[i].thread_id); 56862ffbb224SPrarit Bhargava } 56872ffbb224SPrarit Bhargava 5688c98d5d94SLen Brown } 5689c98d5d94SLen Brown 5690c98d5d94SLen Brown void 569140f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c, 569240f5cfe7SPrarit Bhargava struct pkg_data **p) 5693c98d5d94SLen Brown { 5694c98d5d94SLen Brown int i; 569540f5cfe7SPrarit Bhargava int num_cores = topo.cores_per_node * topo.nodes_per_pkg * 569640f5cfe7SPrarit Bhargava topo.num_packages; 569740f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5698c98d5d94SLen Brown 569940f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5700c98d5d94SLen Brown if (*t == NULL) 5701c98d5d94SLen Brown goto error; 5702c98d5d94SLen Brown 570340f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5704c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5705c98d5d94SLen Brown 570640f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5707c98d5d94SLen Brown if (*c == NULL) 5708c98d5d94SLen Brown goto error; 5709c98d5d94SLen Brown 571040f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5711c98d5d94SLen Brown (*c)[i].core_id = -1; 5712c98d5d94SLen Brown 5713678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5714c98d5d94SLen Brown if (*p == NULL) 5715c98d5d94SLen Brown goto error; 5716c98d5d94SLen Brown 5717c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5718c98d5d94SLen Brown (*p)[i].package_id = i; 5719c98d5d94SLen Brown 5720c98d5d94SLen Brown return; 5721c98d5d94SLen Brown error: 5722b2c95d90SJosh Triplett err(1, "calloc counters"); 5723c98d5d94SLen Brown } 5724c98d5d94SLen Brown /* 5725c98d5d94SLen Brown * init_counter() 5726c98d5d94SLen Brown * 5727c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5728c98d5d94SLen Brown */ 5729c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 57308cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 5731c98d5d94SLen Brown { 57328cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 573340f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 57348cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 57358cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5736c98d5d94SLen Brown struct thread_data *t; 5737c98d5d94SLen Brown struct core_data *c; 5738c98d5d94SLen Brown struct pkg_data *p; 5739c98d5d94SLen Brown 574042dd4520SNathan Ciobanu 574142dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 574242dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 574342dd4520SNathan Ciobanu */ 574442dd4520SNathan Ciobanu if (node_id < 0) 574542dd4520SNathan Ciobanu node_id = 0; 574642dd4520SNathan Ciobanu 574740f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 574840f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 57498cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5750c98d5d94SLen Brown 5751c98d5d94SLen Brown t->cpu_id = cpu_id; 57528cb48b32SPrarit Bhargava if (thread_id == 0) { 5753c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5754c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5755c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5756c98d5d94SLen Brown } 5757c98d5d94SLen Brown 57588cb48b32SPrarit Bhargava c->core_id = core_id; 57598cb48b32SPrarit Bhargava p->package_id = pkg_id; 5760c98d5d94SLen Brown } 5761c98d5d94SLen Brown 5762c98d5d94SLen Brown 5763c98d5d94SLen Brown int initialize_counters(int cpu_id) 5764c98d5d94SLen Brown { 57658cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 57668cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5767c98d5d94SLen Brown return 0; 5768c98d5d94SLen Brown } 5769c98d5d94SLen Brown 5770c98d5d94SLen Brown void allocate_output_buffer() 5771c98d5d94SLen Brown { 5772eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5773c98d5d94SLen Brown outp = output_buffer; 5774b2c95d90SJosh Triplett if (outp == NULL) 5775b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5776c98d5d94SLen Brown } 577736229897SLen Brown void allocate_fd_percpu(void) 577836229897SLen Brown { 577901a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 578036229897SLen Brown if (fd_percpu == NULL) 578136229897SLen Brown err(-1, "calloc fd_percpu"); 578236229897SLen Brown } 5783562a2d37SLen Brown void allocate_irq_buffers(void) 5784562a2d37SLen Brown { 5785562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5786562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5787562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5788c98d5d94SLen Brown 578901a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5790562a2d37SLen Brown if (irqs_per_cpu == NULL) 579101a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5792562a2d37SLen Brown } 5793c98d5d94SLen Brown void setup_all_buffers(void) 5794c98d5d94SLen Brown { 5795c98d5d94SLen Brown topology_probe(); 5796562a2d37SLen Brown allocate_irq_buffers(); 579736229897SLen Brown allocate_fd_percpu(); 5798c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5799c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5800c98d5d94SLen Brown allocate_output_buffer(); 5801c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5802c98d5d94SLen Brown } 58033b4d5c7fSAndy Shevchenko 58047ce7d5deSPrarit Bhargava void set_base_cpu(void) 58057ce7d5deSPrarit Bhargava { 58067ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 58077ce7d5deSPrarit Bhargava if (base_cpu < 0) 58087ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 58097ce7d5deSPrarit Bhargava 58107ce7d5deSPrarit Bhargava if (debug > 1) 5811b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 58127ce7d5deSPrarit Bhargava } 58137ce7d5deSPrarit Bhargava 5814103a8feaSLen Brown void turbostat_init() 5815103a8feaSLen Brown { 58167ce7d5deSPrarit Bhargava setup_all_buffers(); 58177ce7d5deSPrarit Bhargava set_base_cpu(); 5818103a8feaSLen Brown check_dev_msr(); 581998481e79SLen Brown check_permissions(); 5820fcd17211SLen Brown process_cpuid(); 58212af4f9b8SLen Brown linux_perf_init(); 5822103a8feaSLen Brown 5823103a8feaSLen Brown 582496e47158SLen Brown if (!quiet) 58257f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 58267f5c258eSLen Brown 582796e47158SLen Brown if (!quiet) 5828889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5829889facbeSLen Brown 583096e47158SLen Brown if (!quiet) 58313a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 58323a9a941dSLen Brown 583396e47158SLen Brown if (!quiet) 5834889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5835889facbeSLen Brown 5836889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5837889facbeSLen Brown 58387ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 58397ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 58407ab5ff49SZhang Rui 584196e47158SLen Brown if (!quiet) 5842889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 58435a63426eSLen Brown 584496e47158SLen Brown if (!quiet && do_irtl_snb) 58455a63426eSLen Brown print_irtl(); 5846103a8feaSLen Brown } 5847103a8feaSLen Brown 5848103a8feaSLen Brown int fork_it(char **argv) 5849103a8feaSLen Brown { 5850103a8feaSLen Brown pid_t child_pid; 5851d91bb17cSLen Brown int status; 5852d15cf7c1SLen Brown 5853218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5854d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 58554c2122d4SLen Brown first_counter_read = 0; 5856d91bb17cSLen Brown if (status) 5857d91bb17cSLen Brown exit(status); 5858d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5859d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5860103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5861103a8feaSLen Brown 5862103a8feaSLen Brown child_pid = fork(); 5863103a8feaSLen Brown if (!child_pid) { 5864103a8feaSLen Brown /* child */ 5865103a8feaSLen Brown execvp(argv[0], argv); 58660815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5867103a8feaSLen Brown } else { 5868103a8feaSLen Brown 5869103a8feaSLen Brown /* parent */ 5870b2c95d90SJosh Triplett if (child_pid == -1) 5871b2c95d90SJosh Triplett err(1, "fork"); 5872103a8feaSLen Brown 5873103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5874103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5875b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5876b2c95d90SJosh Triplett err(status, "waitpid"); 58772a954966SDavid Arcari 58782a954966SDavid Arcari if (WIFEXITED(status)) 58792a954966SDavid Arcari status = WEXITSTATUS(status); 5880103a8feaSLen Brown } 5881c98d5d94SLen Brown /* 5882c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5883c98d5d94SLen Brown * because re-starting is problematic when forking 5884c98d5d94SLen Brown */ 5885218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5886c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5887103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5888103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5889ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5890ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5891ba3dec99SLen Brown else { 5892c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5893c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5894ba3dec99SLen Brown } 5895103a8feaSLen Brown 5896b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 5897b7d8c148SLen Brown 5898b7d8c148SLen Brown flush_output_stderr(); 5899103a8feaSLen Brown 5900d91bb17cSLen Brown return status; 5901103a8feaSLen Brown } 5902103a8feaSLen Brown 59033b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 59043b4d5c7fSAndy Shevchenko { 59053b4d5c7fSAndy Shevchenko int status; 59063b4d5c7fSAndy Shevchenko 5907218f0e8dSLen Brown snapshot_proc_sysfs_files(); 59083b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 59093b4d5c7fSAndy Shevchenko if (status) 59103b4d5c7fSAndy Shevchenko return status; 59113b4d5c7fSAndy Shevchenko 59123b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 59133b4d5c7fSAndy Shevchenko if (status) 59143b4d5c7fSAndy Shevchenko return status; 59153b4d5c7fSAndy Shevchenko 5916b7d8c148SLen Brown flush_output_stdout(); 59173b4d5c7fSAndy Shevchenko 59183b4d5c7fSAndy Shevchenko return status; 59193b4d5c7fSAndy Shevchenko } 59203b4d5c7fSAndy Shevchenko 5921d8af6f5fSLen Brown void print_version() { 5922ba58ecdeSLen Brown fprintf(outf, "turbostat version 21.03.12" 5923d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 5924d8af6f5fSLen Brown } 5925d8af6f5fSLen Brown 5926495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5927495c7654SLen Brown unsigned int width, enum counter_scope scope, 592841618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5929388e9c81SLen Brown { 5930388e9c81SLen Brown struct msr_counter *msrp; 5931388e9c81SLen Brown 5932388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5933388e9c81SLen Brown if (msrp == NULL) { 5934388e9c81SLen Brown perror("calloc"); 5935388e9c81SLen Brown exit(1); 5936388e9c81SLen Brown } 5937388e9c81SLen Brown 5938388e9c81SLen Brown msrp->msr_num = msr_num; 5939d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 5940495c7654SLen Brown if (path) 5941d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 5942388e9c81SLen Brown msrp->width = width; 5943388e9c81SLen Brown msrp->type = type; 5944388e9c81SLen Brown msrp->format = format; 594541618e63SLen Brown msrp->flags = flags; 5946388e9c81SLen Brown 5947388e9c81SLen Brown switch (scope) { 5948388e9c81SLen Brown 5949388e9c81SLen Brown case SCOPE_CPU: 5950388e9c81SLen Brown msrp->next = sys.tp; 5951388e9c81SLen Brown sys.tp = msrp; 5952678a3bd1SLen Brown sys.added_thread_counters++; 59530748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5954678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5955678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5956678a3bd1SLen Brown exit(-1); 5957678a3bd1SLen Brown } 5958388e9c81SLen Brown break; 5959388e9c81SLen Brown 5960388e9c81SLen Brown case SCOPE_CORE: 5961388e9c81SLen Brown msrp->next = sys.cp; 5962388e9c81SLen Brown sys.cp = msrp; 5963678a3bd1SLen Brown sys.added_core_counters++; 5964678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5965678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5966678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5967678a3bd1SLen Brown exit(-1); 5968678a3bd1SLen Brown } 5969388e9c81SLen Brown break; 5970388e9c81SLen Brown 5971388e9c81SLen Brown case SCOPE_PACKAGE: 5972388e9c81SLen Brown msrp->next = sys.pp; 5973388e9c81SLen Brown sys.pp = msrp; 5974678a3bd1SLen Brown sys.added_package_counters++; 5975678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5976678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5977678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5978678a3bd1SLen Brown exit(-1); 5979678a3bd1SLen Brown } 5980388e9c81SLen Brown break; 5981388e9c81SLen Brown } 5982388e9c81SLen Brown 5983388e9c81SLen Brown return 0; 5984388e9c81SLen Brown } 5985388e9c81SLen Brown 5986388e9c81SLen Brown void parse_add_command(char *add_command) 5987388e9c81SLen Brown { 5988388e9c81SLen Brown int msr_num = 0; 5989495c7654SLen Brown char *path = NULL; 59900f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5991388e9c81SLen Brown int width = 64; 5992388e9c81SLen Brown int fail = 0; 5993388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5994388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5995388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5996388e9c81SLen Brown 5997388e9c81SLen Brown while (add_command) { 5998388e9c81SLen Brown 5999388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 6000388e9c81SLen Brown goto next; 6001388e9c81SLen Brown 6002388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 6003388e9c81SLen Brown goto next; 6004388e9c81SLen Brown 6005495c7654SLen Brown if (*add_command == '/') { 6006495c7654SLen Brown path = add_command; 6007495c7654SLen Brown goto next; 6008495c7654SLen Brown } 6009495c7654SLen Brown 6010388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 6011388e9c81SLen Brown if ((width == 32) || (width == 64)) 6012388e9c81SLen Brown goto next; 6013388e9c81SLen Brown width = 64; 6014388e9c81SLen Brown } 6015388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6016388e9c81SLen Brown scope = SCOPE_CPU; 6017388e9c81SLen Brown goto next; 6018388e9c81SLen Brown } 6019388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6020388e9c81SLen Brown scope = SCOPE_CORE; 6021388e9c81SLen Brown goto next; 6022388e9c81SLen Brown } 6023388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6024388e9c81SLen Brown scope = SCOPE_PACKAGE; 6025388e9c81SLen Brown goto next; 6026388e9c81SLen Brown } 6027388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6028388e9c81SLen Brown type = COUNTER_CYCLES; 6029388e9c81SLen Brown goto next; 6030388e9c81SLen Brown } 6031388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6032388e9c81SLen Brown type = COUNTER_SECONDS; 6033388e9c81SLen Brown goto next; 6034388e9c81SLen Brown } 603541618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 603641618e63SLen Brown type = COUNTER_USEC; 603741618e63SLen Brown goto next; 603841618e63SLen Brown } 6039388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6040388e9c81SLen Brown format = FORMAT_RAW; 6041388e9c81SLen Brown goto next; 6042388e9c81SLen Brown } 6043388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6044388e9c81SLen Brown format = FORMAT_DELTA; 6045388e9c81SLen Brown goto next; 6046388e9c81SLen Brown } 6047388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6048388e9c81SLen Brown format = FORMAT_PERCENT; 6049388e9c81SLen Brown goto next; 6050388e9c81SLen Brown } 6051388e9c81SLen Brown 6052388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6053388e9c81SLen Brown char *eos; 6054388e9c81SLen Brown 6055388e9c81SLen Brown eos = strchr(name_buffer, ','); 6056388e9c81SLen Brown if (eos) 6057388e9c81SLen Brown *eos = '\0'; 6058388e9c81SLen Brown goto next; 6059388e9c81SLen Brown } 6060388e9c81SLen Brown 6061388e9c81SLen Brown next: 6062388e9c81SLen Brown add_command = strchr(add_command, ','); 6063495c7654SLen Brown if (add_command) { 6064495c7654SLen Brown *add_command = '\0'; 6065388e9c81SLen Brown add_command++; 6066495c7654SLen Brown } 6067388e9c81SLen Brown 6068388e9c81SLen Brown } 6069495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6070495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6071388e9c81SLen Brown fail++; 6072388e9c81SLen Brown } 6073388e9c81SLen Brown 6074388e9c81SLen Brown /* generate default column header */ 6075388e9c81SLen Brown if (*name_buffer == '\0') { 6076388e9c81SLen Brown if (width == 32) 60775f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6078388e9c81SLen Brown else 60795f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6080388e9c81SLen Brown } 6081388e9c81SLen Brown 608241618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6083388e9c81SLen Brown fail++; 6084388e9c81SLen Brown 6085388e9c81SLen Brown if (fail) { 6086388e9c81SLen Brown help(); 6087388e9c81SLen Brown exit(1); 6088388e9c81SLen Brown } 6089388e9c81SLen Brown } 609041618e63SLen Brown 6091dd778a5eSLen Brown int is_deferred_skip(char *name) 6092dd778a5eSLen Brown { 6093dd778a5eSLen Brown int i; 6094dd778a5eSLen Brown 6095dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6096dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6097dd778a5eSLen Brown return 1; 6098dd778a5eSLen Brown return 0; 6099dd778a5eSLen Brown } 6100dd778a5eSLen Brown 610141618e63SLen Brown void probe_sysfs(void) 610241618e63SLen Brown { 610341618e63SLen Brown char path[64]; 610441618e63SLen Brown char name_buf[16]; 610541618e63SLen Brown FILE *input; 610641618e63SLen Brown int state; 610741618e63SLen Brown char *sp; 610841618e63SLen Brown 610941618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 611041618e63SLen Brown return; 611141618e63SLen Brown 61120748eaf0SLen Brown for (state = 10; state >= 0; --state) { 611341618e63SLen Brown 611441618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 611541618e63SLen Brown base_cpu, state); 611641618e63SLen Brown input = fopen(path, "r"); 611741618e63SLen Brown if (input == NULL) 611841618e63SLen Brown continue; 61198173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 61208173c336SBen Hutchings err(1, "%s: failed to read file", path); 612141618e63SLen Brown 612241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 612341618e63SLen Brown sp = strchr(name_buf, '-'); 612441618e63SLen Brown if (!sp) 612541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 612641618e63SLen Brown *sp = '%'; 612741618e63SLen Brown *(sp + 1) = '\0'; 612841618e63SLen Brown 6129fecb3bc8SDavid Arcari remove_underbar(name_buf); 6130fecb3bc8SDavid Arcari 613141618e63SLen Brown fclose(input); 613241618e63SLen Brown 613341618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 613441618e63SLen Brown 6135dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6136dd778a5eSLen Brown continue; 6137dd778a5eSLen Brown 613841618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 613941618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 614041618e63SLen Brown } 614141618e63SLen Brown 61420748eaf0SLen Brown for (state = 10; state >= 0; --state) { 614341618e63SLen Brown 614441618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 614541618e63SLen Brown base_cpu, state); 614641618e63SLen Brown input = fopen(path, "r"); 614741618e63SLen Brown if (input == NULL) 614841618e63SLen Brown continue; 61498173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 61508173c336SBen Hutchings err(1, "%s: failed to read file", path); 615141618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 615241618e63SLen Brown sp = strchr(name_buf, '-'); 615341618e63SLen Brown if (!sp) 615441618e63SLen Brown sp = strchrnul(name_buf, '\n'); 615541618e63SLen Brown *sp = '\0'; 615641618e63SLen Brown fclose(input); 615741618e63SLen Brown 6158fecb3bc8SDavid Arcari remove_underbar(name_buf); 6159fecb3bc8SDavid Arcari 616041618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 616141618e63SLen Brown 6162dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6163dd778a5eSLen Brown continue; 6164dd778a5eSLen Brown 616541618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 616641618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 616741618e63SLen Brown } 616841618e63SLen Brown 616941618e63SLen Brown } 617041618e63SLen Brown 61711ef7d21aSLen Brown 61721ef7d21aSLen Brown /* 61731ef7d21aSLen Brown * parse cpuset with following syntax 61741ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 61751ef7d21aSLen Brown */ 61761ef7d21aSLen Brown void parse_cpu_command(char *optarg) 61771ef7d21aSLen Brown { 61781ef7d21aSLen Brown unsigned int start, end; 61791ef7d21aSLen Brown char *next; 61801ef7d21aSLen Brown 61814e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 61824e4e1e7cSLen Brown if (cpu_subset) 61834e4e1e7cSLen Brown goto error; 61844e4e1e7cSLen Brown show_core_only++; 61854e4e1e7cSLen Brown return; 61864e4e1e7cSLen Brown } 61874e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 61884e4e1e7cSLen Brown if (cpu_subset) 61894e4e1e7cSLen Brown goto error; 61904e4e1e7cSLen Brown show_pkg_only++; 61914e4e1e7cSLen Brown return; 61924e4e1e7cSLen Brown } 61934e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 61944e4e1e7cSLen Brown goto error; 61954e4e1e7cSLen Brown 61961ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 61971ef7d21aSLen Brown if (cpu_subset == NULL) 61981ef7d21aSLen Brown err(3, "CPU_ALLOC"); 61991ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 62001ef7d21aSLen Brown 62011ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 62021ef7d21aSLen Brown 62031ef7d21aSLen Brown next = optarg; 62041ef7d21aSLen Brown 62051ef7d21aSLen Brown while (next && *next) { 62061ef7d21aSLen Brown 62071ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 62081ef7d21aSLen Brown goto error; 62091ef7d21aSLen Brown 62101ef7d21aSLen Brown start = strtoul(next, &next, 10); 62111ef7d21aSLen Brown 62121ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 62131ef7d21aSLen Brown goto error; 62141ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 62151ef7d21aSLen Brown 62161ef7d21aSLen Brown if (*next == '\0') 62171ef7d21aSLen Brown break; 62181ef7d21aSLen Brown 62191ef7d21aSLen Brown if (*next == ',') { 62201ef7d21aSLen Brown next += 1; 62211ef7d21aSLen Brown continue; 62221ef7d21aSLen Brown } 62231ef7d21aSLen Brown 62241ef7d21aSLen Brown if (*next == '-') { 62251ef7d21aSLen Brown next += 1; /* start range */ 62261ef7d21aSLen Brown } else if (*next == '.') { 62271ef7d21aSLen Brown next += 1; 62281ef7d21aSLen Brown if (*next == '.') 62291ef7d21aSLen Brown next += 1; /* start range */ 62301ef7d21aSLen Brown else 62311ef7d21aSLen Brown goto error; 62321ef7d21aSLen Brown } 62331ef7d21aSLen Brown 62341ef7d21aSLen Brown end = strtoul(next, &next, 10); 62351ef7d21aSLen Brown if (end <= start) 62361ef7d21aSLen Brown goto error; 62371ef7d21aSLen Brown 62381ef7d21aSLen Brown while (++start <= end) { 62391ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 62401ef7d21aSLen Brown goto error; 62411ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 62421ef7d21aSLen Brown } 62431ef7d21aSLen Brown 62441ef7d21aSLen Brown if (*next == ',') 62451ef7d21aSLen Brown next += 1; 62461ef7d21aSLen Brown else if (*next != '\0') 62471ef7d21aSLen Brown goto error; 62481ef7d21aSLen Brown } 62491ef7d21aSLen Brown 62501ef7d21aSLen Brown return; 62511ef7d21aSLen Brown 62521ef7d21aSLen Brown error: 62534e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 62544e4e1e7cSLen Brown help(); 62551ef7d21aSLen Brown exit(-1); 62561ef7d21aSLen Brown } 62571ef7d21aSLen Brown 6258812db3f7SLen Brown 6259103a8feaSLen Brown void cmdline(int argc, char **argv) 6260103a8feaSLen Brown { 6261103a8feaSLen Brown int opt; 6262d8af6f5fSLen Brown int option_index = 0; 6263d8af6f5fSLen Brown static struct option long_options[] = { 6264388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 62651ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 6266d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 626796e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 62683f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 6269d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 62702af4f9b8SLen Brown {"IPC", no_argument, 0, 'I'}, 6271023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 6272d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 6273812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 6274d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 6275c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 6276b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 627796e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 6278812db3f7SLen Brown {"show", required_argument, 0, 's'}, 6279d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 6280d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 6281d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 6282d8af6f5fSLen Brown {0, 0, 0, 0 } 6283d8af6f5fSLen Brown }; 6284103a8feaSLen Brown 6285103a8feaSLen Brown progname = argv[0]; 6286103a8feaSLen Brown 6287023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 6288d8af6f5fSLen Brown long_options, &option_index)) != -1) { 6289103a8feaSLen Brown switch (opt) { 6290388e9c81SLen Brown case 'a': 6291388e9c81SLen Brown parse_add_command(optarg); 6292388e9c81SLen Brown break; 62931ef7d21aSLen Brown case 'c': 62941ef7d21aSLen Brown parse_cpu_command(optarg); 62951ef7d21aSLen Brown break; 6296d8af6f5fSLen Brown case 'D': 6297d8af6f5fSLen Brown dump_only++; 62988e180f3cSLen Brown break; 62993f44a5c6SLen Brown case 'e': 63003f44a5c6SLen Brown /* --enable specified counter */ 63014c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 63023f44a5c6SLen Brown break; 6303d8af6f5fSLen Brown case 'd': 6304d8af6f5fSLen Brown debug++; 63053f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 63062f32edf1SLen Brown break; 6307812db3f7SLen Brown case 'H': 63083f44a5c6SLen Brown /* 63093f44a5c6SLen Brown * --hide: do not show those specified 63103f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 63113f44a5c6SLen Brown */ 63123f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6313812db3f7SLen Brown break; 6314d8af6f5fSLen Brown case 'h': 6315d8af6f5fSLen Brown default: 6316d8af6f5fSLen Brown help(); 6317d8af6f5fSLen Brown exit(1); 6318d8af6f5fSLen Brown case 'i': 63192a0609c0SLen Brown { 63202a0609c0SLen Brown double interval = strtod(optarg, NULL); 63212a0609c0SLen Brown 63222a0609c0SLen Brown if (interval < 0.001) { 6323b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 63242a0609c0SLen Brown interval); 63252a0609c0SLen Brown exit(2); 63262a0609c0SLen Brown } 63272a0609c0SLen Brown 632847936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6329b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 633047936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 63312a0609c0SLen Brown } 6332889facbeSLen Brown break; 63335c56be9aSDirk Brandewie case 'J': 63345c56be9aSDirk Brandewie rapl_joules++; 63355c56be9aSDirk Brandewie break; 6336c8ade361SLen Brown case 'l': 63373f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6338c8ade361SLen Brown list_header_only++; 6339c8ade361SLen Brown quiet++; 6340c8ade361SLen Brown break; 6341b7d8c148SLen Brown case 'o': 6342b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6343b7d8c148SLen Brown break; 634496e47158SLen Brown case 'q': 634596e47158SLen Brown quiet = 1; 634696e47158SLen Brown break; 6347023fe0acSChen Yu case 'n': 6348023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6349023fe0acSChen Yu 6350023fe0acSChen Yu if (num_iterations <= 0) { 6351023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 6352023fe0acSChen Yu num_iterations); 6353023fe0acSChen Yu exit(2); 6354023fe0acSChen Yu } 6355023fe0acSChen Yu break; 6356812db3f7SLen Brown case 's': 63573f44a5c6SLen Brown /* 63583f44a5c6SLen Brown * --show: show only those specified 63593f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 63603f44a5c6SLen Brown * subsequent invocations can add to it. 63613f44a5c6SLen Brown */ 63623f44a5c6SLen Brown if (shown == 0) 63633f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 63643f44a5c6SLen Brown else 63653f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 63663f44a5c6SLen Brown shown = 1; 6367812db3f7SLen Brown break; 6368d8af6f5fSLen Brown case 'S': 6369d8af6f5fSLen Brown summary_only++; 6370d8af6f5fSLen Brown break; 6371d8af6f5fSLen Brown case 'T': 6372d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 6373d8af6f5fSLen Brown break; 6374d8af6f5fSLen Brown case 'v': 6375d8af6f5fSLen Brown print_version(); 6376d8af6f5fSLen Brown exit(0); 6377d8af6f5fSLen Brown break; 6378103a8feaSLen Brown } 6379103a8feaSLen Brown } 6380103a8feaSLen Brown } 6381103a8feaSLen Brown 6382103a8feaSLen Brown int main(int argc, char **argv) 6383103a8feaSLen Brown { 6384b7d8c148SLen Brown outf = stderr; 6385103a8feaSLen Brown cmdline(argc, argv); 6386103a8feaSLen Brown 638796e47158SLen Brown if (!quiet) 6388d8af6f5fSLen Brown print_version(); 6389103a8feaSLen Brown 639041618e63SLen Brown probe_sysfs(); 639141618e63SLen Brown 6392103a8feaSLen Brown turbostat_init(); 6393103a8feaSLen Brown 63943b4d5c7fSAndy Shevchenko /* dump counters and exit */ 63953b4d5c7fSAndy Shevchenko if (dump_only) 63963b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 63973b4d5c7fSAndy Shevchenko 6398c8ade361SLen Brown /* list header and exit */ 6399c8ade361SLen Brown if (list_header_only) { 6400c8ade361SLen Brown print_header(","); 6401c8ade361SLen Brown flush_output_stdout(); 6402c8ade361SLen Brown return 0; 6403c8ade361SLen Brown } 6404c8ade361SLen Brown 64059972d5d8SChen Yu msr_sum_record(); 6406103a8feaSLen Brown /* 6407103a8feaSLen Brown * if any params left, it must be a command to fork 6408103a8feaSLen Brown */ 6409103a8feaSLen Brown if (argc - optind) 6410103a8feaSLen Brown return fork_it(argv + optind); 6411103a8feaSLen Brown else 6412103a8feaSLen Brown turbostat_loop(); 6413103a8feaSLen Brown 6414103a8feaSLen Brown return 0; 6415103a8feaSLen Brown } 6416