1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2103a8feaSLen Brown /* 3103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 434041551SLen Brown * on modern Intel and AMD processors. 5103a8feaSLen Brown * 63c070b2aSLen Brown * Copyright (c) 2021 Intel Corporation. 7103a8feaSLen Brown * Len Brown <len.brown@intel.com> 8103a8feaSLen Brown */ 9103a8feaSLen Brown 1088c3281fSLen Brown #define _GNU_SOURCE 11b731f311SJosh Triplett #include MSRHEADER 12869ce69eSLen Brown #include INTEL_FAMILY_HEADER 1395aebc44SJosh Triplett #include <stdarg.h> 14103a8feaSLen Brown #include <stdio.h> 15b2c95d90SJosh Triplett #include <err.h> 16103a8feaSLen Brown #include <unistd.h> 17103a8feaSLen Brown #include <sys/types.h> 18103a8feaSLen Brown #include <sys/wait.h> 19103a8feaSLen Brown #include <sys/stat.h> 20b9ad8ee0SLen Brown #include <sys/select.h> 21103a8feaSLen Brown #include <sys/resource.h> 22103a8feaSLen Brown #include <fcntl.h> 23103a8feaSLen Brown #include <signal.h> 24103a8feaSLen Brown #include <sys/time.h> 25103a8feaSLen Brown #include <stdlib.h> 26d8af6f5fSLen Brown #include <getopt.h> 27103a8feaSLen Brown #include <dirent.h> 28103a8feaSLen Brown #include <string.h> 29103a8feaSLen Brown #include <ctype.h> 3088c3281fSLen Brown #include <sched.h> 312a0609c0SLen Brown #include <time.h> 322b92865eSJosh Triplett #include <cpuid.h> 33fcaa681cSLen Brown #include <sys/capability.h> 3498481e79SLen Brown #include <errno.h> 359392bd98SCalvin Walton #include <math.h> 362af4f9b8SLen Brown #include <linux/perf_event.h> 372af4f9b8SLen Brown #include <asm/unistd.h> 387ab5ff49SZhang Rui #include <stdbool.h> 39103a8feaSLen Brown 40103a8feaSLen Brown char *proc_stat = "/proc/stat"; 41b7d8c148SLen Brown FILE *outf; 4236229897SLen Brown int *fd_percpu; 432af4f9b8SLen Brown int *fd_instr_count_percpu; 44b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 }; 4547936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 }; 46e9d3092fSZhang Rui 47e9d3092fSZhang Rui /* Save original CPU model */ 48e9d3092fSZhang Rui unsigned int model_orig; 49e9d3092fSZhang Rui 50023fe0acSChen Yu unsigned int num_iterations; 51d8af6f5fSLen Brown unsigned int debug; 5296e47158SLen Brown unsigned int quiet; 533f44a5c6SLen Brown unsigned int shown; 540de6c0dfSLen Brown unsigned int sums_need_wide_columns; 55d8af6f5fSLen Brown unsigned int rapl_joules; 56d8af6f5fSLen Brown unsigned int summary_only; 57c8ade361SLen Brown unsigned int list_header_only; 58d8af6f5fSLen Brown unsigned int dump_only; 59103a8feaSLen Brown unsigned int do_snb_cstates; 60fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 61144b44b1SLen Brown unsigned int do_slm_cstates; 62144b44b1SLen Brown unsigned int use_c1_residency_msr; 63103a8feaSLen Brown unsigned int has_aperf; 64889facbeSLen Brown unsigned int has_epb; 655a63426eSLen Brown unsigned int do_irtl_snb; 665a63426eSLen Brown unsigned int do_irtl_hsw; 67fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 68103a8feaSLen Brown unsigned int genuine_intel; 6934041551SLen Brown unsigned int authentic_amd; 70c1c10cc7SPu Wen unsigned int hygon_genuine; 7134041551SLen Brown unsigned int max_level, max_extended_level; 72103a8feaSLen Brown unsigned int has_invariant_tsc; 73d7899447SLen Brown unsigned int do_nhm_platform_info; 74cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 75b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 76103a8feaSLen Brown double bclk; 77a2b7b749SLen Brown double base_hz; 7821ed5574SLen Brown unsigned int has_base_hz; 79a2b7b749SLen Brown double tsc_tweak = 1.0; 80c98d5d94SLen Brown unsigned int show_pkg_only; 81c98d5d94SLen Brown unsigned int show_core_only; 82c98d5d94SLen Brown char *output_buffer, *outp; 83889facbeSLen Brown unsigned int do_rapl; 84889facbeSLen Brown unsigned int do_dts; 85889facbeSLen Brown unsigned int do_ptm; 862af4f9b8SLen Brown unsigned int do_ipc; 87fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 88be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 89be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 9027d47356SLen Brown unsigned int gfx_cur_mhz; 91b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 9255279aefSZhang Rui unsigned int tj_max; 9355279aefSZhang Rui unsigned int tj_max_override; 940b9a0b9bSZhang Rui int tcc_offset_bits; 9540ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 9640ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 97889facbeSLen Brown double rapl_joule_counter_range; 983a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 99ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 100aeb01e6dSChen Yu unsigned int dis_cstate_prewake; 1013a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1023a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1038a5bdf41SLen Brown unsigned int crystal_hz; 1048a5bdf41SLen Brown unsigned long long tsc_hz; 1057ce7d5deSPrarit Bhargava int base_cpu; 10621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1077f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1087f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1097f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1107f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1117f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1127f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 11333148d67SLen Brown unsigned int has_misc_feature_control; 1144c2122d4SLen Brown unsigned int first_counter_read = 1; 115c026c236SArtem Bityutskiy int ignore_stdin; 116889facbeSLen Brown 117889facbeSLen Brown #define RAPL_PKG (1 << 0) 118e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 119e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 120e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 121e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 122e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 123e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 124e6f9bb3cSLen Brown 125889facbeSLen Brown #define RAPL_DRAM (1 << 3) 126e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 127e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 128e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 129e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1300b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1310b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 132e6f9bb3cSLen Brown 1339148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 134e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1350b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 136e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 137e6f9bb3cSLen Brown 1380b2bb692SLen Brown #define RAPL_GFX (1 << 8) 139e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 140e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 141e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1429148494cSJacob Pan 1439148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1449148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1459392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1469392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1479392bd98SCalvin Walton * not per-package. */ 1489392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1499392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1509392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1519392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1529148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 153889facbeSLen Brown #define TJMAX_DEFAULT 100 154889facbeSLen Brown 1559392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1569392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1579392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1589392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1599392bd98SCalvin Walton 160889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 161103a8feaSLen Brown 162388e9c81SLen Brown /* 163388e9c81SLen Brown * buffer size used by sscanf() for added column names 164388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 165388e9c81SLen Brown */ 166388e9c81SLen Brown #define NAME_BYTES 20 167495c7654SLen Brown #define PATH_BYTES 128 168388e9c81SLen Brown 169103a8feaSLen Brown int backwards_count; 170103a8feaSLen Brown char *progname; 171103a8feaSLen Brown 1721ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1731ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1741ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1750748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1760748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1770e2d8f05SLen Brown #define BITMASK_SIZE 32 178c98d5d94SLen Brown 179c98d5d94SLen Brown struct thread_data { 180f4fdf2b4SLen Brown struct timeval tv_begin; 181f4fdf2b4SLen Brown struct timeval tv_end; 182d4794f25SYazen Ghannam struct timeval tv_delta; 183c98d5d94SLen Brown unsigned long long tsc; 184c98d5d94SLen Brown unsigned long long aperf; 185c98d5d94SLen Brown unsigned long long mperf; 186144b44b1SLen Brown unsigned long long c1; 1872af4f9b8SLen Brown unsigned long long instr_count; 1880de6c0dfSLen Brown unsigned long long irq_count; 1891ed51011SLen Brown unsigned int smi_count; 190c98d5d94SLen Brown unsigned int cpu_id; 1914c2122d4SLen Brown unsigned int apic_id; 1924c2122d4SLen Brown unsigned int x2apic_id; 193c98d5d94SLen Brown unsigned int flags; 1947ab5ff49SZhang Rui bool is_atom; 195c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 196c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1970748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 198c98d5d94SLen Brown } *thread_even, *thread_odd; 199c98d5d94SLen Brown 200c98d5d94SLen Brown struct core_data { 201c98d5d94SLen Brown unsigned long long c3; 202c98d5d94SLen Brown unsigned long long c6; 203c98d5d94SLen Brown unsigned long long c7; 2040539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 205889facbeSLen Brown unsigned int core_temp_c; 2069392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 207c98d5d94SLen Brown unsigned int core_id; 208678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 209c98d5d94SLen Brown } *core_even, *core_odd; 210c98d5d94SLen Brown 211c98d5d94SLen Brown struct pkg_data { 212c98d5d94SLen Brown unsigned long long pc2; 213c98d5d94SLen Brown unsigned long long pc3; 214c98d5d94SLen Brown unsigned long long pc6; 215c98d5d94SLen Brown unsigned long long pc7; 216ca58710fSKristen Carlson Accardi unsigned long long pc8; 217ca58710fSKristen Carlson Accardi unsigned long long pc9; 218ca58710fSKristen Carlson Accardi unsigned long long pc10; 219be0e54c4SLen Brown unsigned long long cpu_lpi; 220be0e54c4SLen Brown unsigned long long sys_lpi; 2210b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2220b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2230b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2240b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2259185e988SLen Brown long long gfx_rc6_ms; 22627d47356SLen Brown unsigned int gfx_mhz; 227b4b91569SRafael Antognolli unsigned int gfx_act_mhz; 228c98d5d94SLen Brown unsigned int package_id; 2297c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2307c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2317c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2327c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2337c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2347c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 235889facbeSLen Brown unsigned int pkg_temp_c; 236678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 237c98d5d94SLen Brown } *package_even, *package_odd; 238c98d5d94SLen Brown 239c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 240c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 241c98d5d94SLen Brown 24240f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 24340f5cfe7SPrarit Bhargava ((thread_base) + \ 24440f5cfe7SPrarit Bhargava ((pkg_no) * \ 24540f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 24640f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 24740f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 24840f5cfe7SPrarit Bhargava (thread_no)) 24940f5cfe7SPrarit Bhargava 25040f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 25140f5cfe7SPrarit Bhargava ((core_base) + \ 25240f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 25340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 25440f5cfe7SPrarit Bhargava (core_no)) 25540f5cfe7SPrarit Bhargava 256c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 257c98d5d94SLen Brown 258388e9c81SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE }; 25941618e63SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC }; 260388e9c81SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT }; 261388e9c81SLen Brown 262388e9c81SLen Brown struct msr_counter { 263388e9c81SLen Brown unsigned int msr_num; 264388e9c81SLen Brown char name[NAME_BYTES]; 265495c7654SLen Brown char path[PATH_BYTES]; 266388e9c81SLen Brown unsigned int width; 267388e9c81SLen Brown enum counter_type type; 268388e9c81SLen Brown enum counter_format format; 269388e9c81SLen Brown struct msr_counter *next; 270812db3f7SLen Brown unsigned int flags; 271812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 272812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 27341618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 274388e9c81SLen Brown }; 275388e9c81SLen Brown 27687e15da9SChen Yu /* 27787e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 27887e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 27987e15da9SChen Yu * despite its register's bit width. 28087e15da9SChen Yu */ 28187e15da9SChen Yu enum { 28287e15da9SChen Yu IDX_PKG_ENERGY, 28387e15da9SChen Yu IDX_DRAM_ENERGY, 28487e15da9SChen Yu IDX_PP0_ENERGY, 28587e15da9SChen Yu IDX_PP1_ENERGY, 28687e15da9SChen Yu IDX_PKG_PERF, 28787e15da9SChen Yu IDX_DRAM_PERF, 28887e15da9SChen Yu IDX_COUNT, 28987e15da9SChen Yu }; 29087e15da9SChen Yu 29187e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 29287e15da9SChen Yu 29387e15da9SChen Yu struct msr_sum_array { 29487e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 29587e15da9SChen Yu struct { 29687e15da9SChen Yu /*The accumulated MSR value is updated by the timer */ 29787e15da9SChen Yu unsigned long long sum; 29887e15da9SChen Yu /*The MSR footprint recorded in last timer */ 29987e15da9SChen Yu unsigned long long last; 30087e15da9SChen Yu } entries[IDX_COUNT]; 30187e15da9SChen Yu }; 30287e15da9SChen Yu 30387e15da9SChen Yu /* The percpu MSR sum array.*/ 30487e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 30587e15da9SChen Yu 30613a779deSCalvin Walton off_t idx_to_offset(int idx) 30787e15da9SChen Yu { 30813a779deSCalvin Walton off_t offset; 30987e15da9SChen Yu 31087e15da9SChen Yu switch (idx) { 31187e15da9SChen Yu case IDX_PKG_ENERGY: 312301b1d3aSBas Nieuwenhuizen if (do_rapl & RAPL_AMD_F17H) 313301b1d3aSBas Nieuwenhuizen offset = MSR_PKG_ENERGY_STAT; 314301b1d3aSBas Nieuwenhuizen else 31587e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 31687e15da9SChen Yu break; 31787e15da9SChen Yu case IDX_DRAM_ENERGY: 31887e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 31987e15da9SChen Yu break; 32087e15da9SChen Yu case IDX_PP0_ENERGY: 32187e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 32287e15da9SChen Yu break; 32387e15da9SChen Yu case IDX_PP1_ENERGY: 32487e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 32587e15da9SChen Yu break; 32687e15da9SChen Yu case IDX_PKG_PERF: 32787e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 32887e15da9SChen Yu break; 32987e15da9SChen Yu case IDX_DRAM_PERF: 33087e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 33187e15da9SChen Yu break; 33287e15da9SChen Yu default: 33387e15da9SChen Yu offset = -1; 33487e15da9SChen Yu } 33587e15da9SChen Yu return offset; 33687e15da9SChen Yu } 33787e15da9SChen Yu 33813a779deSCalvin Walton int offset_to_idx(off_t offset) 33987e15da9SChen Yu { 34087e15da9SChen Yu int idx; 34187e15da9SChen Yu 34287e15da9SChen Yu switch (offset) { 34387e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 344301b1d3aSBas Nieuwenhuizen case MSR_PKG_ENERGY_STAT: 34587e15da9SChen Yu idx = IDX_PKG_ENERGY; 34687e15da9SChen Yu break; 34787e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 34887e15da9SChen Yu idx = IDX_DRAM_ENERGY; 34987e15da9SChen Yu break; 35087e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 35187e15da9SChen Yu idx = IDX_PP0_ENERGY; 35287e15da9SChen Yu break; 35387e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 35487e15da9SChen Yu idx = IDX_PP1_ENERGY; 35587e15da9SChen Yu break; 35687e15da9SChen Yu case MSR_PKG_PERF_STATUS: 35787e15da9SChen Yu idx = IDX_PKG_PERF; 35887e15da9SChen Yu break; 35987e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 36087e15da9SChen Yu idx = IDX_DRAM_PERF; 36187e15da9SChen Yu break; 36287e15da9SChen Yu default: 36387e15da9SChen Yu idx = -1; 36487e15da9SChen Yu } 36587e15da9SChen Yu return idx; 36687e15da9SChen Yu } 36787e15da9SChen Yu 36887e15da9SChen Yu int idx_valid(int idx) 36987e15da9SChen Yu { 37087e15da9SChen Yu switch (idx) { 37187e15da9SChen Yu case IDX_PKG_ENERGY: 372301b1d3aSBas Nieuwenhuizen return do_rapl & (RAPL_PKG | RAPL_AMD_F17H); 37387e15da9SChen Yu case IDX_DRAM_ENERGY: 37487e15da9SChen Yu return do_rapl & RAPL_DRAM; 37587e15da9SChen Yu case IDX_PP0_ENERGY: 37687e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 37787e15da9SChen Yu case IDX_PP1_ENERGY: 37887e15da9SChen Yu return do_rapl & RAPL_GFX; 37987e15da9SChen Yu case IDX_PKG_PERF: 38087e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 38187e15da9SChen Yu case IDX_DRAM_PERF: 38287e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 38387e15da9SChen Yu default: 38487e15da9SChen Yu return 0; 38587e15da9SChen Yu } 38687e15da9SChen Yu } 3871b439f01SLen Brown 388388e9c81SLen Brown struct sys_counters { 389678a3bd1SLen Brown unsigned int added_thread_counters; 390678a3bd1SLen Brown unsigned int added_core_counters; 391678a3bd1SLen Brown unsigned int added_package_counters; 392388e9c81SLen Brown struct msr_counter *tp; 393388e9c81SLen Brown struct msr_counter *cp; 394388e9c81SLen Brown struct msr_counter *pp; 395388e9c81SLen Brown } sys; 396388e9c81SLen Brown 397c98d5d94SLen Brown struct system_summary { 398c98d5d94SLen Brown struct thread_data threads; 399c98d5d94SLen Brown struct core_data cores; 400c98d5d94SLen Brown struct pkg_data packages; 401388e9c81SLen Brown } average; 402c98d5d94SLen Brown 4030e2d8f05SLen Brown struct cpu_topology { 4040e2d8f05SLen Brown int physical_package_id; 4056de68fe1SLen Brown int die_id; 4060e2d8f05SLen Brown int logical_cpu_id; 407ef605741SPrarit Bhargava int physical_node_id; 408ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 4090e2d8f05SLen Brown int physical_core_id; 4108cb48b32SPrarit Bhargava int thread_id; 4110e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 4120e2d8f05SLen Brown } *cpus; 413c98d5d94SLen Brown 414c98d5d94SLen Brown struct topo_params { 415c98d5d94SLen Brown int num_packages; 4166de68fe1SLen Brown int num_die; 417103a8feaSLen Brown int num_cpus; 418c98d5d94SLen Brown int num_cores; 419c98d5d94SLen Brown int max_cpu_num; 420ef605741SPrarit Bhargava int max_node_num; 42170a9c6e8SPrarit Bhargava int nodes_per_pkg; 42270a9c6e8SPrarit Bhargava int cores_per_node; 42370a9c6e8SPrarit Bhargava int threads_per_core; 424c98d5d94SLen Brown } topo; 425103a8feaSLen Brown 426c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 427103a8feaSLen Brown 428562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 429562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 430562a2d37SLen Brown 431c98d5d94SLen Brown void setup_all_buffers(void); 432103a8feaSLen Brown 4331f81c5efSLen Brown char *sys_lpi_file; 4341f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 4351f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 4361f81c5efSLen Brown 437c98d5d94SLen Brown int cpu_is_not_present(int cpu) 438d15cf7c1SLen Brown { 439c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 440c98d5d94SLen Brown } 4411b439f01SLen Brown 442c98d5d94SLen Brown /* 443c98d5d94SLen Brown * run func(thread, core, package) in topology order 444c98d5d94SLen Brown * skip non-present cpus 445c98d5d94SLen Brown */ 446c98d5d94SLen Brown 447c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *), 448c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 449c98d5d94SLen Brown { 45040f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 451c98d5d94SLen Brown 452c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 453df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 45470a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 4551b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 456c98d5d94SLen Brown struct thread_data *t; 457c98d5d94SLen Brown struct core_data *c; 458c98d5d94SLen Brown struct pkg_data *p; 459c98d5d94SLen Brown 4601b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 461c98d5d94SLen Brown 462c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 463c98d5d94SLen Brown continue; 464c98d5d94SLen Brown 4651b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 466c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 467c98d5d94SLen Brown 468c98d5d94SLen Brown retval = func(t, c, p); 469c98d5d94SLen Brown if (retval) 470c98d5d94SLen Brown return retval; 471c98d5d94SLen Brown } 472c98d5d94SLen Brown } 473c98d5d94SLen Brown } 47440f5cfe7SPrarit Bhargava } 475d15cf7c1SLen Brown return 0; 476d15cf7c1SLen Brown } 477d15cf7c1SLen Brown 47888c3281fSLen Brown int cpu_migrate(int cpu) 47988c3281fSLen Brown { 480c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 481c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 482c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 48388c3281fSLen Brown return -1; 48488c3281fSLen Brown else 48588c3281fSLen Brown return 0; 48688c3281fSLen Brown } 4871b439f01SLen Brown 48836229897SLen Brown int get_msr_fd(int cpu) 489103a8feaSLen Brown { 490103a8feaSLen Brown char pathname[32]; 491103a8feaSLen Brown int fd; 492103a8feaSLen Brown 49336229897SLen Brown fd = fd_percpu[cpu]; 49436229897SLen Brown 49536229897SLen Brown if (fd) 49636229897SLen Brown return fd; 49736229897SLen Brown 498103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 499103a8feaSLen Brown fd = open(pathname, O_RDONLY); 50015aaa346SLen Brown if (fd < 0) 50198481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 502103a8feaSLen Brown 50336229897SLen Brown fd_percpu[cpu] = fd; 50436229897SLen Brown 50536229897SLen Brown return fd; 50636229897SLen Brown } 50736229897SLen Brown 5082af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) 5092af4f9b8SLen Brown { 5102af4f9b8SLen Brown return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); 5112af4f9b8SLen Brown } 5122af4f9b8SLen Brown 5132af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num) 5142af4f9b8SLen Brown { 5152af4f9b8SLen Brown struct perf_event_attr pea; 5162af4f9b8SLen Brown int fd; 5172af4f9b8SLen Brown 5182af4f9b8SLen Brown memset(&pea, 0, sizeof(struct perf_event_attr)); 5192af4f9b8SLen Brown pea.type = PERF_TYPE_HARDWARE; 5202af4f9b8SLen Brown pea.size = sizeof(struct perf_event_attr); 5212af4f9b8SLen Brown pea.config = PERF_COUNT_HW_INSTRUCTIONS; 5222af4f9b8SLen Brown 5232af4f9b8SLen Brown /* counter for cpu_num, including user + kernel and all processes */ 5242af4f9b8SLen Brown fd = perf_event_open(&pea, -1, cpu_num, -1, 0); 5252af4f9b8SLen Brown if (fd == -1) 5262af4f9b8SLen Brown err(-1, "cpu%d: perf instruction counter\n", cpu_num); 5272af4f9b8SLen Brown 5282af4f9b8SLen Brown return fd; 5292af4f9b8SLen Brown } 5302af4f9b8SLen Brown 5312af4f9b8SLen Brown int get_instr_count_fd(int cpu) 5322af4f9b8SLen Brown { 5332af4f9b8SLen Brown if (fd_instr_count_percpu[cpu]) 5342af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5352af4f9b8SLen Brown 5362af4f9b8SLen Brown fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu); 5372af4f9b8SLen Brown 5382af4f9b8SLen Brown return fd_instr_count_percpu[cpu]; 5392af4f9b8SLen Brown } 5402af4f9b8SLen Brown 54136229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 54236229897SLen Brown { 54336229897SLen Brown ssize_t retval; 54436229897SLen Brown 54536229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 54615aaa346SLen Brown 54798481e79SLen Brown if (retval != sizeof *msr) 548cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 54915aaa346SLen Brown 55015aaa346SLen Brown return 0; 551103a8feaSLen Brown } 552103a8feaSLen Brown 553fc04cc67SLen Brown /* 554bdd5ae3aSLen Brown * This list matches the column headers, except 555bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 556bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 557bdd5ae3aSLen Brown * matching on them for --show and --hide. 558fc04cc67SLen Brown */ 559812db3f7SLen Brown struct msr_counter bic[] = { 5603f44a5c6SLen Brown { 0x0, "usec" }, 5613f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 562812db3f7SLen Brown { 0x0, "Package" }, 563bdd5ae3aSLen Brown { 0x0, "Node" }, 564812db3f7SLen Brown { 0x0, "Avg_MHz" }, 565bdd5ae3aSLen Brown { 0x0, "Busy%" }, 566812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 567812db3f7SLen Brown { 0x0, "TSC_MHz" }, 568812db3f7SLen Brown { 0x0, "IRQ" }, 569495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL }, 570bdd5ae3aSLen Brown { 0x0, "sysfs" }, 571812db3f7SLen Brown { 0x0, "CPU%c1" }, 572812db3f7SLen Brown { 0x0, "CPU%c3" }, 573812db3f7SLen Brown { 0x0, "CPU%c6" }, 574812db3f7SLen Brown { 0x0, "CPU%c7" }, 575812db3f7SLen Brown { 0x0, "ThreadC" }, 576812db3f7SLen Brown { 0x0, "CoreTmp" }, 577812db3f7SLen Brown { 0x0, "CoreCnt" }, 578812db3f7SLen Brown { 0x0, "PkgTmp" }, 579812db3f7SLen Brown { 0x0, "GFX%rc6" }, 580812db3f7SLen Brown { 0x0, "GFXMHz" }, 581812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 582812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 583812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 584812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 5850f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 5860f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 5874bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 588be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 589be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 590812db3f7SLen Brown { 0x0, "PkgWatt" }, 591812db3f7SLen Brown { 0x0, "CorWatt" }, 592812db3f7SLen Brown { 0x0, "GFXWatt" }, 593812db3f7SLen Brown { 0x0, "PkgCnt" }, 594812db3f7SLen Brown { 0x0, "RAMWatt" }, 595812db3f7SLen Brown { 0x0, "PKG_%" }, 596812db3f7SLen Brown { 0x0, "RAM_%" }, 597812db3f7SLen Brown { 0x0, "Pkg_J" }, 598812db3f7SLen Brown { 0x0, "Cor_J" }, 599812db3f7SLen Brown { 0x0, "GFX_J" }, 600812db3f7SLen Brown { 0x0, "RAM_J" }, 6010539ba11SLen Brown { 0x0, "Mod%c6" }, 602a99d8730SLen Brown { 0x0, "Totl%C0" }, 603a99d8730SLen Brown { 0x0, "Any%C0" }, 604a99d8730SLen Brown { 0x0, "GFX%C0" }, 605a99d8730SLen Brown { 0x0, "CPUGFX%" }, 606bdd5ae3aSLen Brown { 0x0, "Core" }, 607bdd5ae3aSLen Brown { 0x0, "CPU" }, 6084c2122d4SLen Brown { 0x0, "APIC" }, 6094c2122d4SLen Brown { 0x0, "X2APIC" }, 6106de68fe1SLen Brown { 0x0, "Die" }, 611b4b91569SRafael Antognolli { 0x0, "GFXAMHz" }, 6122af4f9b8SLen Brown { 0x0, "IPC" }, 613812db3f7SLen Brown }; 614812db3f7SLen Brown 615812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 6163f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 6173f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 6183f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 619bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 620bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 621bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 622bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 623bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 624bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 625bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 626bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 627bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 628bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 629bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 630bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 631bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 632bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 633bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 634bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 635bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 636bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 637bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 638bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 639bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 640bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 641bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 642bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 643bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 644bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 645bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 646bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 647bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 648bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 649bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 650bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 651bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 652bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 653bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 654bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 655bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 656bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 657bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 658bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 659bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 660bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 661bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 662bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 663bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 6644c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 6654c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 6666de68fe1SLen Brown #define BIC_Die (1ULL << 50) 667b4b91569SRafael Antognolli #define BIC_GFXACTMHz (1ULL << 51) 6682af4f9b8SLen Brown #define BIC_IPC (1ULL << 52) 669812db3f7SLen Brown 670b60c573dSLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die ) 671b60c573dSLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__) 672b60c573dSLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz ) 673b60c573dSLen Brown #define BIC_IDLE ( BIC_sysfs | BIC_CPU_c1 | BIC_CPU_c3 | BIC_CPU_c6 | BIC_CPU_c7 | BIC_GFX_rc6 | BIC_Pkgpc2 | BIC_Pkgpc3 | BIC_Pkgpc6 | BIC_Pkgpc7 | BIC_Pkgpc8 | BIC_Pkgpc9 | BIC_Pkgpc10 | BIC_CPU_LPI | BIC_SYS_LPI | BIC_Mod_c6 | BIC_Totl_c0 | BIC_Any_c0 | BIC_GFX_c0 | BIC_CPUGFX) 674b60c573dSLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC) 675b60c573dSLen Brown 6764c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 6773f44a5c6SLen Brown 6783f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 6794c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 680812db3f7SLen Brown 681812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 6821e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 6833f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 684812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 6850f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 6862af4f9b8SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT) 687812db3f7SLen Brown 688dd778a5eSLen Brown #define MAX_DEFERRED 16 689dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 690dd778a5eSLen Brown int deferred_skip_index; 691dd778a5eSLen Brown 692dd778a5eSLen Brown /* 693dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 694dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 695dd778a5eSLen Brown */ 696dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 697dd778a5eSLen Brown 698dd778a5eSLen Brown void help(void) 699dd778a5eSLen Brown { 700dd778a5eSLen Brown fprintf(outf, 701dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 702dd778a5eSLen Brown "\n" 703dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 704dd778a5eSLen Brown "when COMMAND completes.\n" 705dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 706dd778a5eSLen Brown "to print statistics, until interrupted.\n" 707cc481650SNathan Ciobanu " -a, --add add a counter\n" 708dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 709cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 710dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 7119ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 7129ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 7139ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 7149ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 7159ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 7169ce80578SNathan Ciobanu " hide the specified column(s)\n" 717cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 718cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 7199ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 720cc481650SNathan Ciobanu " -l, --list list column headers only\n" 721cc481650SNathan Ciobanu " -n, --num_iterations num\n" 722cc481650SNathan Ciobanu " number of the measurement iterations\n" 723cc481650SNathan Ciobanu " -o, --out file\n" 724cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 725cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 7269ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 7279ce80578SNathan Ciobanu " show only the specified column(s)\n" 7289ce80578SNathan Ciobanu " -S, --Summary\n" 7299ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 7309ce80578SNathan Ciobanu " -T, --TCC temperature\n" 7319ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 7329ce80578SNathan Ciobanu " degrees Celsius\n" 733cc481650SNathan Ciobanu " -h, --help print this help message\n" 7341b439f01SLen Brown " -v, --version print version information\n" "\n" "For more help, run \"man turbostat\"\n"); 735dd778a5eSLen Brown } 736dd778a5eSLen Brown 737812db3f7SLen Brown /* 738812db3f7SLen Brown * bic_lookup 739812db3f7SLen Brown * for all the strings in comma separate name_list, 740812db3f7SLen Brown * set the approprate bit in return value. 741812db3f7SLen Brown */ 742dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 743812db3f7SLen Brown { 744812db3f7SLen Brown int i; 745812db3f7SLen Brown unsigned long long retval = 0; 746812db3f7SLen Brown 747812db3f7SLen Brown while (name_list) { 748812db3f7SLen Brown char *comma; 749812db3f7SLen Brown 750812db3f7SLen Brown comma = strchr(name_list, ','); 751812db3f7SLen Brown 752812db3f7SLen Brown if (comma) 753812db3f7SLen Brown *comma = '\0'; 754812db3f7SLen Brown 755812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 756812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 757812db3f7SLen Brown retval |= (1ULL << i); 758812db3f7SLen Brown break; 759812db3f7SLen Brown } 7605dc241f2SLen Brown if (!strcmp(name_list, "all")) { 7615dc241f2SLen Brown retval |= ~0; 7625dc241f2SLen Brown break; 7635dc241f2SLen Brown } else if (!strcmp(name_list, "topology")) { 7645dc241f2SLen Brown retval |= BIC_TOPOLOGY; 7655dc241f2SLen Brown break; 7665dc241f2SLen Brown } else if (!strcmp(name_list, "power")) { 7675dc241f2SLen Brown retval |= BIC_THERMAL_PWR; 7685dc241f2SLen Brown break; 7695dc241f2SLen Brown } else if (!strcmp(name_list, "idle")) { 7705dc241f2SLen Brown retval |= BIC_IDLE; 7715dc241f2SLen Brown break; 7725dc241f2SLen Brown } else if (!strcmp(name_list, "frequency")) { 7735dc241f2SLen Brown retval |= BIC_FREQUENCY; 7745dc241f2SLen Brown break; 7755dc241f2SLen Brown } else if (!strcmp(name_list, "other")) { 7765dc241f2SLen Brown retval |= BIC_OTHER; 7775dc241f2SLen Brown break; 7785dc241f2SLen Brown } 7795dc241f2SLen Brown 780812db3f7SLen Brown } 781812db3f7SLen Brown if (i == MAX_BIC) { 782dd778a5eSLen Brown if (mode == SHOW_LIST) { 783812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 784812db3f7SLen Brown exit(-1); 785812db3f7SLen Brown } 786dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 787dd778a5eSLen Brown if (debug) 788dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 789dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 790dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 791dd778a5eSLen Brown MAX_DEFERRED, name_list); 792dd778a5eSLen Brown help(); 793dd778a5eSLen Brown exit(1); 794dd778a5eSLen Brown } 795dd778a5eSLen Brown } 796812db3f7SLen Brown 797812db3f7SLen Brown name_list = comma; 798812db3f7SLen Brown if (name_list) 799812db3f7SLen Brown name_list++; 800812db3f7SLen Brown 801812db3f7SLen Brown } 802812db3f7SLen Brown return retval; 803812db3f7SLen Brown } 804fc04cc67SLen Brown 805c8ade361SLen Brown void print_header(char *delim) 806103a8feaSLen Brown { 807388e9c81SLen Brown struct msr_counter *mp; 8086168c2e0SLen Brown int printed = 0; 809388e9c81SLen Brown 8103f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 8113f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 8123f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 8133f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 814812db3f7SLen Brown if (DO_BIC(BIC_Package)) 8156168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 8166de68fe1SLen Brown if (DO_BIC(BIC_Die)) 8176de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 81801235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 81901235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 820812db3f7SLen Brown if (DO_BIC(BIC_Core)) 8216168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 822812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8236168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 8244c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 8254c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 8264c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 8274c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 828812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8296168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 830812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8316168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 832812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 8336168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 834812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8356168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 8361cc21f7bSLen Brown 8372af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 8382af4f9b8SLen Brown outp += sprintf(outp, "%sIPC", (printed++ ? delim : "")); 8392af4f9b8SLen Brown 8400de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8410de6c0dfSLen Brown if (sums_need_wide_columns) 8426168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 8430de6c0dfSLen Brown else 8446168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 8450de6c0dfSLen Brown } 8460de6c0dfSLen Brown 847812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8486168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 8491cc21f7bSLen Brown 850388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 851dd778a5eSLen Brown 852388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 853388e9c81SLen Brown if (mp->width == 64) 854dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 855388e9c81SLen Brown else 856dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 857388e9c81SLen Brown } else { 8580de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 859dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 8600de6c0dfSLen Brown else 861dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 862388e9c81SLen Brown } 863388e9c81SLen Brown } 864388e9c81SLen Brown 86541618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8666168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 867562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 8686168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 869812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8706168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 871812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8726168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 873678a3bd1SLen Brown 8740539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8756168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 876678a3bd1SLen Brown 877812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8786168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 879388e9c81SLen Brown 8809392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 8819392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8829392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 8839392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 8849392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8859392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 8869392bd98SCalvin Walton } 8879392bd98SCalvin Walton 888388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 889388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 890388e9c81SLen Brown if (mp->width == 64) 891c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 892388e9c81SLen Brown else 893c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 894388e9c81SLen Brown } else { 8950de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8960de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 8970de6c0dfSLen Brown else 898c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 899388e9c81SLen Brown } 900388e9c81SLen Brown } 901388e9c81SLen Brown 902812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 9036168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 904889facbeSLen Brown 905812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 9066168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 907fdf676e5SLen Brown 908812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 9096168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 91027d47356SLen Brown 911b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 912b4b91569SRafael Antognolli outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : "")); 913b4b91569SRafael Antognolli 914a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 9156168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 916a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 9176168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 918a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 9196168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 920a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 9216168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 9220b2bb692SLen Brown 9230f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9246168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 9250f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9266168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 9270f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9286168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 9290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9306168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 9310f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9326168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 9330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9346168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 9350f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9366168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 937be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 938be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 939be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 940be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 941103a8feaSLen Brown 9425c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 943812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9446168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 9459392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9466168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 947812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9486168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 949812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9506168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 951812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9526168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 953812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9546168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 955d7899447SLen Brown } else if (do_rapl && rapl_joules) { 956812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9576168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 9589392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 9596168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 960812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9616168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 962812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9636168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 964812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9656168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 966812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9676168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 9685c56be9aSDirk Brandewie } 969388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 970388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 971388e9c81SLen Brown if (mp->width == 64) 972c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 973388e9c81SLen Brown else 974c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 975388e9c81SLen Brown } else { 9760de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9770de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 9780de6c0dfSLen Brown else 979c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 980388e9c81SLen Brown } 981388e9c81SLen Brown } 982388e9c81SLen Brown 983c98d5d94SLen Brown outp += sprintf(outp, "\n"); 984103a8feaSLen Brown } 985103a8feaSLen Brown 9861b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 987103a8feaSLen Brown { 988388e9c81SLen Brown int i; 989388e9c81SLen Brown struct msr_counter *mp; 990388e9c81SLen Brown 9913b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 992c98d5d94SLen Brown 993c98d5d94SLen Brown if (t) { 9941b439f01SLen Brown outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags); 9953b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 9963b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 9973b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 9983b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 9996886fee4SLen Brown 10002af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 10012af4f9b8SLen Brown outp += sprintf(outp, "IPC: %lld\n", t->instr_count); 10022af4f9b8SLen Brown 1003812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 10040de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 1005812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 1006218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 1007388e9c81SLen Brown 1008388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 10091b439f01SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]); 1010388e9c81SLen Brown } 1011103a8feaSLen Brown } 1012103a8feaSLen Brown 1013c98d5d94SLen Brown if (c) { 10143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 10153b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 10163b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 10173b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 10183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 10199392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 1020388e9c81SLen Brown 1021388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 10221b439f01SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]); 1023388e9c81SLen Brown } 10240539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 1025c98d5d94SLen Brown } 1026103a8feaSLen Brown 1027c98d5d94SLen Brown if (p) { 10283b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 10290b2bb692SLen Brown 10300b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 10310b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 10320b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 10330b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 10340b2bb692SLen Brown 10353b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 10360f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 10380f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10393b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 10400f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 10423b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 10433b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 10443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 1045be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 1046be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 10477c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 10487c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 10497c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 10507c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 10511b439f01SLen Brown outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status); 10521b439f01SLen Brown outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status); 10533b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 1054388e9c81SLen Brown 1055388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 10561b439f01SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]); 1057388e9c81SLen Brown } 1058c98d5d94SLen Brown } 10593b4d5c7fSAndy Shevchenko 10603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 10613b4d5c7fSAndy Shevchenko 1062c98d5d94SLen Brown return 0; 1063103a8feaSLen Brown } 1064103a8feaSLen Brown 1065e23da037SLen Brown /* 1066e23da037SLen Brown * column formatting convention & formats 1067e23da037SLen Brown */ 10681b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1069103a8feaSLen Brown { 1070008d396eSLen Brown double interval_float, tsc; 1071fc04cc67SLen Brown char *fmt8; 1072388e9c81SLen Brown int i; 1073388e9c81SLen Brown struct msr_counter *mp; 10746168c2e0SLen Brown char *delim = "\t"; 10756168c2e0SLen Brown int printed = 0; 1076103a8feaSLen Brown 1077c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1078c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1079c98d5d94SLen Brown return 0; 1080c98d5d94SLen Brown 1081c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1082c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1083c98d5d94SLen Brown return 0; 1084c98d5d94SLen Brown 10851ef7d21aSLen Brown /*if not summary line and --cpu is used */ 10861b439f01SLen Brown if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 10871ef7d21aSLen Brown return 0; 10881ef7d21aSLen Brown 10893f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1090f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1091f4fdf2b4SLen Brown struct timeval tv; 1092f4fdf2b4SLen Brown 1093f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1094f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1095f4fdf2b4SLen Brown } 1096f4fdf2b4SLen Brown 10973f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 10983f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 10993f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 11003f44a5c6SLen Brown 1101d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0; 1102103a8feaSLen Brown 1103008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1104008d396eSLen Brown 1105c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1106c98d5d94SLen Brown if (t == &average.threads) { 1107812db3f7SLen Brown if (DO_BIC(BIC_Package)) 11086168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11096de68fe1SLen Brown if (DO_BIC(BIC_Die)) 11106de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 111101235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 111201235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1113812db3f7SLen Brown if (DO_BIC(BIC_Core)) 11146168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1115812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11166168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11174c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11184c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11194c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11204c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1121103a8feaSLen Brown } else { 1122812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1123c98d5d94SLen Brown if (p) 11246168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1125c98d5d94SLen Brown else 11266168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1127c98d5d94SLen Brown } 11286de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 11296de68fe1SLen Brown if (c) 11306de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 11316de68fe1SLen Brown else 11326de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 11336de68fe1SLen Brown } 113401235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 113501235041SPrarit Bhargava if (t) 113601235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 11371b439f01SLen Brown (printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id); 113801235041SPrarit Bhargava else 11391b439f01SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 114001235041SPrarit Bhargava } 1141812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1142c98d5d94SLen Brown if (c) 11436168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1144c98d5d94SLen Brown else 11456168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1146c98d5d94SLen Brown } 1147812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 11486168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 11494c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 11504c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 11514c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 11524c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1153103a8feaSLen Brown } 1154fc04cc67SLen Brown 1155812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 11561b439f01SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float); 1157fc04cc67SLen Brown 1158812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 11596168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc); 1160103a8feaSLen Brown 1161812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 116221ed5574SLen Brown if (has_base_hz) 11631b439f01SLen Brown outp += 11641b439f01SLen Brown sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 116521ed5574SLen Brown else 11666168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1167008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 116821ed5574SLen Brown } 1169103a8feaSLen Brown 1170812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 11716168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float); 1172103a8feaSLen Brown 11732af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 11742af4f9b8SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf); 11752af4f9b8SLen Brown 1176562a2d37SLen Brown /* IRQ */ 11770de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 11780de6c0dfSLen Brown if (sums_need_wide_columns) 11796168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 11800de6c0dfSLen Brown else 11816168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 11820de6c0dfSLen Brown } 1183562a2d37SLen Brown 11841cc21f7bSLen Brown /* SMI */ 1185812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 11866168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 11871cc21f7bSLen Brown 1188678a3bd1SLen Brown /* Added counters */ 1189678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1190678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1191678a3bd1SLen Brown if (mp->width == 32) 11921b439f01SLen Brown outp += 11931b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]); 1194678a3bd1SLen Brown else 11956168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1196678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 11970de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11986168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 11990de6c0dfSLen Brown else 12006168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1201678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 120241618e63SLen Brown if (mp->type == COUNTER_USEC) 12031b439f01SLen Brown outp += 12041b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12051b439f01SLen Brown t->counter[i] / interval_float / 10000); 120641618e63SLen Brown else 12076168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc); 1208678a3bd1SLen Brown } 1209678a3bd1SLen Brown } 1210678a3bd1SLen Brown 121141618e63SLen Brown /* C1 */ 121241618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 12136168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc); 121441618e63SLen Brown 1215c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1216c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1217c98d5d94SLen Brown goto done; 1218c98d5d94SLen Brown 1219562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 12206168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc); 1221812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 12226168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc); 1223812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 12246168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc); 1225c98d5d94SLen Brown 12260539ba11SLen Brown /* Mod%c6 */ 12270539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 12286168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 12290539ba11SLen Brown 1230812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 12316168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1232889facbeSLen Brown 1233388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1234388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1235388e9c81SLen Brown if (mp->width == 32) 12361b439f01SLen Brown outp += 12371b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]); 1238388e9c81SLen Brown else 12396168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1240388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12410de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12426168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 12430de6c0dfSLen Brown else 12446168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1245388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12466168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc); 1247388e9c81SLen Brown } 1248388e9c81SLen Brown } 1249388e9c81SLen Brown 12509392bd98SCalvin Walton fmt8 = "%s%.2f"; 12519392bd98SCalvin Walton 12529392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12531b439f01SLen Brown outp += 12541b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 12559392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 12569392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 12579392bd98SCalvin Walton 1258c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1259c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1260c98d5d94SLen Brown goto done; 1261c98d5d94SLen Brown 12620b2bb692SLen Brown /* PkgTmp */ 1263812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 12646168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1265889facbeSLen Brown 1266fdf676e5SLen Brown /* GFXrc6 */ 1267812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1268ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 12696168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 12709185e988SLen Brown } else { 12716168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 12729185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 12739185e988SLen Brown } 12749185e988SLen Brown } 1275fdf676e5SLen Brown 127627d47356SLen Brown /* GFXMHz */ 1277812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 12786168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 127927d47356SLen Brown 1280b4b91569SRafael Antognolli /* GFXACTMHz */ 1281b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 1282b4b91569SRafael Antognolli outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz); 1283b4b91569SRafael Antognolli 12840b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1285a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12866168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc); 1287a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12886168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc); 1289a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12906168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc); 1291a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12926168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc); 12930b2bb692SLen Brown 12940f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 12956168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc); 12960f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 12976168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc); 12980f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 12996168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc); 13000f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 13016168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc); 13020f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 13036168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc); 13040f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 13056168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc); 13060f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 13076168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc); 1308889facbeSLen Brown 1309be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 13101b439f01SLen Brown outp += 13111b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1312be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 13131b439f01SLen Brown outp += 13141b439f01SLen Brown sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1315be0e54c4SLen Brown 1316812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 13171b439f01SLen Brown outp += 13181b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 13199392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13201b439f01SLen Brown outp += 13211b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1322812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 13231b439f01SLen Brown outp += 13241b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1325812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 13261b439f01SLen Brown outp += 13271b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13281b439f01SLen Brown p->energy_dram * rapl_dram_energy_units / interval_float); 1329812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 13306168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 13319392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 13326168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1333812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 13346168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1335812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 13366168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1337812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 13381b439f01SLen Brown outp += 13391b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13401b439f01SLen Brown 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1341812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 13421b439f01SLen Brown outp += 13431b439f01SLen Brown sprintf(outp, fmt8, (printed++ ? delim : ""), 13441b439f01SLen Brown 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1345812db3f7SLen Brown 1346388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1347388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1348388e9c81SLen Brown if (mp->width == 32) 13491b439f01SLen Brown outp += 13501b439f01SLen Brown sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]); 1351388e9c81SLen Brown else 13526168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1353388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 13540de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 13556168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 13560de6c0dfSLen Brown else 13576168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1358388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 13596168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc); 1360388e9c81SLen Brown } 1361388e9c81SLen Brown } 1362388e9c81SLen Brown 1363c98d5d94SLen Brown done: 136494d6ab4bSLen Brown if (*(outp - 1) != '\n') 1365c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1366c98d5d94SLen Brown 1367c98d5d94SLen Brown return 0; 1368103a8feaSLen Brown } 1369103a8feaSLen Brown 1370b7d8c148SLen Brown void flush_output_stdout(void) 1371103a8feaSLen Brown { 1372b7d8c148SLen Brown FILE *filep; 1373b7d8c148SLen Brown 1374b7d8c148SLen Brown if (outf == stderr) 1375b7d8c148SLen Brown filep = stdout; 1376b7d8c148SLen Brown else 1377b7d8c148SLen Brown filep = outf; 1378b7d8c148SLen Brown 1379b7d8c148SLen Brown fputs(output_buffer, filep); 1380b7d8c148SLen Brown fflush(filep); 1381b7d8c148SLen Brown 1382c98d5d94SLen Brown outp = output_buffer; 1383c98d5d94SLen Brown } 13841b439f01SLen Brown 1385b7d8c148SLen Brown void flush_output_stderr(void) 1386c98d5d94SLen Brown { 1387b7d8c148SLen Brown fputs(output_buffer, outf); 1388b7d8c148SLen Brown fflush(outf); 1389c98d5d94SLen Brown outp = output_buffer; 1390c98d5d94SLen Brown } 13911b439f01SLen Brown 1392c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1393c98d5d94SLen Brown { 1394e23da037SLen Brown static int printed; 1395103a8feaSLen Brown 1396e23da037SLen Brown if (!printed || !summary_only) 1397c8ade361SLen Brown print_header("\t"); 1398103a8feaSLen Brown 13999d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1400103a8feaSLen Brown 1401e23da037SLen Brown printed = 1; 1402e23da037SLen Brown 1403e23da037SLen Brown if (summary_only) 1404e23da037SLen Brown return; 1405e23da037SLen Brown 1406c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1407103a8feaSLen Brown } 1408103a8feaSLen Brown 1409889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 14107c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1411889facbeSLen Brown 14121b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old) 1413103a8feaSLen Brown { 1414388e9c81SLen Brown int i; 1415388e9c81SLen Brown struct msr_counter *mp; 14160b2bb692SLen Brown 1417a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14180b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1419a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14200b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1421a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14220b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1423a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14240b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1425a99d8730SLen Brown 1426c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 14270f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1428c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 14290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1430c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 14310f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1432c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1433ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1434ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1435ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1436be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1437be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1438889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1439889facbeSLen Brown 14409185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 14419185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 14429185e988SLen Brown old->gfx_rc6_ms = -1; 14439185e988SLen Brown else 1444fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 14459185e988SLen Brown 144627d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 1447b4b91569SRafael Antognolli old->gfx_act_mhz = new->gfx_act_mhz; 144827d47356SLen Brown 144987e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 145087e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 145187e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 145287e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 145387e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 145487e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1455ba3dec99SLen Brown 1456388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1457388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1458388e9c81SLen Brown old->counter[i] = new->counter[i]; 1459388e9c81SLen Brown else 1460388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1461388e9c81SLen Brown } 1462388e9c81SLen Brown 1463ba3dec99SLen Brown return 0; 1464103a8feaSLen Brown } 1465103a8feaSLen Brown 14661b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old) 1467c98d5d94SLen Brown { 1468388e9c81SLen Brown int i; 1469388e9c81SLen Brown struct msr_counter *mp; 1470388e9c81SLen Brown 1471c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1472c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1473c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1474889facbeSLen Brown old->core_temp_c = new->core_temp_c; 14750539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1476388e9c81SLen Brown 14779392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 14789392bd98SCalvin Walton 1479388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1480388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1481388e9c81SLen Brown old->counter[i] = new->counter[i]; 1482388e9c81SLen Brown else 1483388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1484388e9c81SLen Brown } 1485103a8feaSLen Brown } 1486c98d5d94SLen Brown 14871e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 14881e9042b9SSrinivas Pandruvada { 14891e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 14901e9042b9SSrinivas Pandruvada return 0; 14911e9042b9SSrinivas Pandruvada 14921e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 14931e9042b9SSrinivas Pandruvada } 14941e9042b9SSrinivas Pandruvada 1495c3ae331dSLen Brown /* 1496c3ae331dSLen Brown * old = new - old 1497c3ae331dSLen Brown */ 14981b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta) 1499c98d5d94SLen Brown { 1500388e9c81SLen Brown int i; 1501388e9c81SLen Brown struct msr_counter *mp; 1502388e9c81SLen Brown 15034c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 15044c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 15054c2122d4SLen Brown new->apic_id = old->apic_id; 15064c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 15074c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 15084c2122d4SLen Brown 15093f44a5c6SLen Brown /* 15103f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 15113f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 15123f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 15133f44a5c6SLen Brown */ 15143f44a5c6SLen Brown 1515d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 15163f44a5c6SLen Brown old->tv_begin = new->tv_begin; 15173f44a5c6SLen Brown old->tv_end = new->tv_end; 15183f44a5c6SLen Brown 1519c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1520c98d5d94SLen Brown 1521103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1522b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1523b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1524b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1525b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1526103a8feaSLen Brown 1527c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1528c98d5d94SLen Brown 15291b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 1530c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1531c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1532c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1533c98d5d94SLen Brown } else { 1534ba3dec99SLen Brown return -1; 1535103a8feaSLen Brown } 1536a729617cSLen Brown } 1537103a8feaSLen Brown 1538144b44b1SLen Brown if (use_c1_residency_msr) { 1539144b44b1SLen Brown /* 1540144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1541144b44b1SLen Brown * which should be more accurate than the derivation below. 1542144b44b1SLen Brown */ 1543144b44b1SLen Brown } else { 1544103a8feaSLen Brown /* 1545c3ae331dSLen Brown * As counter collection is not atomic, 1546c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1547103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1548103a8feaSLen Brown */ 154995149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1550c98d5d94SLen Brown old->c1 = 0; 1551c98d5d94SLen Brown else { 1552c98d5d94SLen Brown /* normal case, derive c1 */ 1553008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1554c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1555c98d5d94SLen Brown } 1556144b44b1SLen Brown } 1557c3ae331dSLen Brown 1558c98d5d94SLen Brown if (old->mperf == 0) { 1559b7d8c148SLen Brown if (debug > 1) 1560b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1561c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1562c98d5d94SLen Brown } 1563103a8feaSLen Brown 15642af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 15652af4f9b8SLen Brown old->instr_count = new->instr_count - old->instr_count; 15662af4f9b8SLen Brown 1567812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1568562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1569562a2d37SLen Brown 1570812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 15711ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1572ba3dec99SLen Brown 1573388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1574388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1575388e9c81SLen Brown old->counter[i] = new->counter[i]; 1576388e9c81SLen Brown else 1577388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1578388e9c81SLen Brown } 1579ba3dec99SLen Brown return 0; 1580103a8feaSLen Brown } 1581c98d5d94SLen Brown 1582c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 15831b439f01SLen Brown struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2) 1584c98d5d94SLen Brown { 1585ba3dec99SLen Brown int retval = 0; 1586ba3dec99SLen Brown 1587c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1588c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1589c98d5d94SLen Brown delta_core(c, c2); 1590c98d5d94SLen Brown 1591c98d5d94SLen Brown /* always calculate thread delta */ 1592ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1593ba3dec99SLen Brown if (retval) 1594ba3dec99SLen Brown return retval; 1595c98d5d94SLen Brown 1596c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1597c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1598ba3dec99SLen Brown retval = delta_package(p, p2); 1599c98d5d94SLen Brown 1600ba3dec99SLen Brown return retval; 1601103a8feaSLen Brown } 1602103a8feaSLen Brown 1603c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1604103a8feaSLen Brown { 1605388e9c81SLen Brown int i; 1606388e9c81SLen Brown struct msr_counter *mp; 1607388e9c81SLen Brown 16083f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 16093f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 16103f44a5c6SLen Brown t->tv_end.tv_sec = 0; 16113f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1612d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1613d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 16143f44a5c6SLen Brown 1615c98d5d94SLen Brown t->tsc = 0; 1616c98d5d94SLen Brown t->aperf = 0; 1617c98d5d94SLen Brown t->mperf = 0; 1618c98d5d94SLen Brown t->c1 = 0; 1619103a8feaSLen Brown 16202af4f9b8SLen Brown t->instr_count = 0; 16212af4f9b8SLen Brown 1622562a2d37SLen Brown t->irq_count = 0; 1623562a2d37SLen Brown t->smi_count = 0; 1624562a2d37SLen Brown 1625c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1626c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1627c98d5d94SLen Brown 1628c98d5d94SLen Brown c->c3 = 0; 1629c98d5d94SLen Brown c->c6 = 0; 1630c98d5d94SLen Brown c->c7 = 0; 16310539ba11SLen Brown c->mc6_us = 0; 1632889facbeSLen Brown c->core_temp_c = 0; 16339392bd98SCalvin Walton c->core_energy = 0; 1634c98d5d94SLen Brown 16350b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 16360b2bb692SLen Brown p->pkg_any_core_c0 = 0; 16370b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 16380b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 16390b2bb692SLen Brown 1640c98d5d94SLen Brown p->pc2 = 0; 16410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1642c98d5d94SLen Brown p->pc3 = 0; 16430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1644c98d5d94SLen Brown p->pc6 = 0; 16450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1646c98d5d94SLen Brown p->pc7 = 0; 1647ca58710fSKristen Carlson Accardi p->pc8 = 0; 1648ca58710fSKristen Carlson Accardi p->pc9 = 0; 1649ca58710fSKristen Carlson Accardi p->pc10 = 0; 1650be0e54c4SLen Brown p->cpu_lpi = 0; 1651be0e54c4SLen Brown p->sys_lpi = 0; 1652889facbeSLen Brown 1653889facbeSLen Brown p->energy_pkg = 0; 1654889facbeSLen Brown p->energy_dram = 0; 1655889facbeSLen Brown p->energy_cores = 0; 1656889facbeSLen Brown p->energy_gfx = 0; 1657889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1658889facbeSLen Brown p->rapl_dram_perf_status = 0; 1659889facbeSLen Brown p->pkg_temp_c = 0; 166027d47356SLen Brown 1661fdf676e5SLen Brown p->gfx_rc6_ms = 0; 166227d47356SLen Brown p->gfx_mhz = 0; 1663b4b91569SRafael Antognolli p->gfx_act_mhz = 0; 1664388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1665388e9c81SLen Brown t->counter[i] = 0; 1666388e9c81SLen Brown 1667388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1668388e9c81SLen Brown c->counter[i] = 0; 1669388e9c81SLen Brown 1670388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1671388e9c81SLen Brown p->counter[i] = 0; 1672103a8feaSLen Brown } 16731b439f01SLen Brown 16741b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1675103a8feaSLen Brown { 1676388e9c81SLen Brown int i; 1677388e9c81SLen Brown struct msr_counter *mp; 1678388e9c81SLen Brown 16794c2122d4SLen Brown /* copy un-changing apic_id's */ 16804c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 16814c2122d4SLen Brown average.threads.apic_id = t->apic_id; 16824c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 16834c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 16844c2122d4SLen Brown 16853f44a5c6SLen Brown /* remember first tv_begin */ 16863f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 16873f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 16883f44a5c6SLen Brown 16893f44a5c6SLen Brown /* remember last tv_end */ 16903f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 16913f44a5c6SLen Brown 1692c98d5d94SLen Brown average.threads.tsc += t->tsc; 1693c98d5d94SLen Brown average.threads.aperf += t->aperf; 1694c98d5d94SLen Brown average.threads.mperf += t->mperf; 1695c98d5d94SLen Brown average.threads.c1 += t->c1; 169615aaa346SLen Brown 16972af4f9b8SLen Brown average.threads.instr_count += t->instr_count; 16982af4f9b8SLen Brown 1699562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1700562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1701562a2d37SLen Brown 1702388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1703388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1704388e9c81SLen Brown continue; 1705388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1706388e9c81SLen Brown } 1707388e9c81SLen Brown 1708c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1709c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1710c98d5d94SLen Brown return 0; 1711c98d5d94SLen Brown 1712c98d5d94SLen Brown average.cores.c3 += c->c3; 1713c98d5d94SLen Brown average.cores.c6 += c->c6; 1714c98d5d94SLen Brown average.cores.c7 += c->c7; 17150539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1716c98d5d94SLen Brown 1717889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1718889facbeSLen Brown 17199392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 17209392bd98SCalvin Walton 1721388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1722388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1723388e9c81SLen Brown continue; 1724388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1725388e9c81SLen Brown } 1726388e9c81SLen Brown 1727c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1728c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1729c98d5d94SLen Brown return 0; 1730c98d5d94SLen Brown 1731a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17320b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1733a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17340b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1735a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17360b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1737a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17380b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 17390b2bb692SLen Brown 1740c98d5d94SLen Brown average.packages.pc2 += p->pc2; 17410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1742c98d5d94SLen Brown average.packages.pc3 += p->pc3; 17430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1744c98d5d94SLen Brown average.packages.pc6 += p->pc6; 17450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1746c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1747ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1748ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1749ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1750c98d5d94SLen Brown 1751be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1752be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1753be0e54c4SLen Brown 1754889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1755889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1756889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1757889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1758889facbeSLen Brown 1759fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 176027d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 1761b4b91569SRafael Antognolli average.packages.gfx_act_mhz = p->gfx_act_mhz; 176227d47356SLen Brown 1763889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1764889facbeSLen Brown 1765889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1766889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1767388e9c81SLen Brown 1768388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1769388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1770388e9c81SLen Brown continue; 1771388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1772388e9c81SLen Brown } 1773c98d5d94SLen Brown return 0; 1774c98d5d94SLen Brown } 17751b439f01SLen Brown 1776c98d5d94SLen Brown /* 1777c98d5d94SLen Brown * sum the counters for all cpus in the system 1778c98d5d94SLen Brown * compute the weighted average 1779c98d5d94SLen Brown */ 17801b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1781c98d5d94SLen Brown { 1782388e9c81SLen Brown int i; 1783388e9c81SLen Brown struct msr_counter *mp; 1784388e9c81SLen Brown 1785c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1786c98d5d94SLen Brown 1787c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1788c98d5d94SLen Brown 1789d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1790d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1791d4794f25SYazen Ghannam 1792c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1793c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1794c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 17952af4f9b8SLen Brown average.threads.instr_count /= topo.num_cpus; 1796c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1797c98d5d94SLen Brown 17980de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 17990de6c0dfSLen Brown sums_need_wide_columns = 1; 18000de6c0dfSLen Brown 1801c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1802c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1803c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 18040539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1805c98d5d94SLen Brown 1806a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 18070b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1808a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 18090b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1810a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 18110b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1812a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 18130b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 18140b2bb692SLen Brown 1815c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 18160f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1817c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 18180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1819c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 18200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1821c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1822ca58710fSKristen Carlson Accardi 1823ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1824ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1825ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1826388e9c81SLen Brown 1827388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1828388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1829388e9c81SLen Brown continue; 18300de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18310de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 18320de6c0dfSLen Brown sums_need_wide_columns = 1; 183341618e63SLen Brown continue; 18340de6c0dfSLen Brown } 1835388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1836388e9c81SLen Brown } 1837388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1838388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1839388e9c81SLen Brown continue; 18400de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18410de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 18420de6c0dfSLen Brown sums_need_wide_columns = 1; 18430de6c0dfSLen Brown } 1844388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1845388e9c81SLen Brown } 1846388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1847388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1848388e9c81SLen Brown continue; 18490de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 18500de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 18510de6c0dfSLen Brown sums_need_wide_columns = 1; 18520de6c0dfSLen Brown } 1853388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1854388e9c81SLen Brown } 1855c98d5d94SLen Brown } 1856c98d5d94SLen Brown 1857c98d5d94SLen Brown static unsigned long long rdtsc(void) 1858c98d5d94SLen Brown { 1859c98d5d94SLen Brown unsigned int low, high; 1860c98d5d94SLen Brown 1861c98d5d94SLen Brown asm volatile ("rdtsc":"=a" (low), "=d"(high)); 1862c98d5d94SLen Brown 1863c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1864c98d5d94SLen Brown } 1865c98d5d94SLen Brown 1866c98d5d94SLen Brown /* 1867495c7654SLen Brown * Open a file, and exit on failure 1868495c7654SLen Brown */ 1869495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1870495c7654SLen Brown { 1871495c7654SLen Brown FILE *filep = fopen(path, mode); 1872495c7654SLen Brown 1873495c7654SLen Brown if (!filep) 1874495c7654SLen Brown err(1, "%s: open failed", path); 1875495c7654SLen Brown return filep; 1876495c7654SLen Brown } 18771b439f01SLen Brown 1878495c7654SLen Brown /* 1879495c7654SLen Brown * snapshot_sysfs_counter() 1880495c7654SLen Brown * 1881495c7654SLen Brown * return snapshot of given counter 1882495c7654SLen Brown */ 1883495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1884495c7654SLen Brown { 1885495c7654SLen Brown FILE *fp; 1886495c7654SLen Brown int retval; 1887495c7654SLen Brown unsigned long long counter; 1888495c7654SLen Brown 1889495c7654SLen Brown fp = fopen_or_die(path, "r"); 1890495c7654SLen Brown 1891495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1892495c7654SLen Brown if (retval != 1) 1893495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1894495c7654SLen Brown 1895495c7654SLen Brown fclose(fp); 1896495c7654SLen Brown 1897495c7654SLen Brown return counter; 1898495c7654SLen Brown } 1899495c7654SLen Brown 1900495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1901495c7654SLen Brown { 1902495c7654SLen Brown if (mp->msr_num != 0) { 1903495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1904495c7654SLen Brown return -1; 1905495c7654SLen Brown } else { 190646c27978SLen Brown char path[128 + PATH_BYTES]; 190741618e63SLen Brown 190841618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 19091b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path); 191041618e63SLen Brown 191141618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 191241618e63SLen Brown } else { 1913495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1914495c7654SLen Brown } 191541618e63SLen Brown } 1916495c7654SLen Brown 1917495c7654SLen Brown return 0; 1918495c7654SLen Brown } 1919495c7654SLen Brown 19206d6501d9SBorislav Petkov int get_epb(int cpu) 19216d6501d9SBorislav Petkov { 19226d6501d9SBorislav Petkov char path[128 + PATH_BYTES]; 19237f1b11baSBorislav Petkov unsigned long long msr; 19246d6501d9SBorislav Petkov int ret, epb = -1; 19256d6501d9SBorislav Petkov FILE *fp; 19266d6501d9SBorislav Petkov 19276d6501d9SBorislav Petkov sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu); 19286d6501d9SBorislav Petkov 19297f1b11baSBorislav Petkov fp = fopen(path, "r"); 19307f1b11baSBorislav Petkov if (!fp) 19317f1b11baSBorislav Petkov goto msr_fallback; 19326d6501d9SBorislav Petkov 19336d6501d9SBorislav Petkov ret = fscanf(fp, "%d", &epb); 19346d6501d9SBorislav Petkov if (ret != 1) 19356d6501d9SBorislav Petkov err(1, "%s(%s)", __func__, path); 19366d6501d9SBorislav Petkov 19376d6501d9SBorislav Petkov fclose(fp); 19386d6501d9SBorislav Petkov 19396d6501d9SBorislav Petkov return epb; 19407f1b11baSBorislav Petkov 19417f1b11baSBorislav Petkov msr_fallback: 19427f1b11baSBorislav Petkov get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr); 19437f1b11baSBorislav Petkov 19447f1b11baSBorislav Petkov return msr & 0xf; 19456d6501d9SBorislav Petkov } 19466d6501d9SBorislav Petkov 19474c2122d4SLen Brown void get_apic_id(struct thread_data *t) 19484c2122d4SLen Brown { 194934041551SLen Brown unsigned int eax, ebx, ecx, edx; 195034041551SLen Brown 195134041551SLen Brown if (DO_BIC(BIC_APIC)) { 195234041551SLen Brown eax = ebx = ecx = edx = 0; 195334041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 195434041551SLen Brown 195534041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 195634041551SLen Brown } 195734041551SLen Brown 195834041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 195934041551SLen Brown return; 196034041551SLen Brown 1961c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 196234041551SLen Brown unsigned int topology_extensions; 196334041551SLen Brown 196434041551SLen Brown if (max_extended_level < 0x8000001e) 196534041551SLen Brown return; 19664c2122d4SLen Brown 19674c2122d4SLen Brown eax = ebx = ecx = edx = 0; 196834041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 196934041551SLen Brown topology_extensions = ecx & (1 << 22); 197034041551SLen Brown 197134041551SLen Brown if (topology_extensions == 0) 197234041551SLen Brown return; 197334041551SLen Brown 197434041551SLen Brown eax = ebx = ecx = edx = 0; 197534041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 197634041551SLen Brown 197734041551SLen Brown t->x2apic_id = eax; 197834041551SLen Brown return; 197934041551SLen Brown } 19804c2122d4SLen Brown 19814c2122d4SLen Brown if (!genuine_intel) 19824c2122d4SLen Brown return; 19834c2122d4SLen Brown 19844c2122d4SLen Brown if (max_level < 0xb) 19854c2122d4SLen Brown return; 19864c2122d4SLen Brown 19874c2122d4SLen Brown ecx = 0; 19884c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 19894c2122d4SLen Brown t->x2apic_id = edx; 19904c2122d4SLen Brown 199134041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 19921b439f01SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 19934c2122d4SLen Brown } 19944c2122d4SLen Brown 1995495c7654SLen Brown /* 1996c98d5d94SLen Brown * get_counters(...) 1997c98d5d94SLen Brown * migrate to cpu 1998c98d5d94SLen Brown * acquire and record local counters for that cpu 1999c98d5d94SLen Brown */ 2000c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2001c98d5d94SLen Brown { 2002c98d5d94SLen Brown int cpu = t->cpu_id; 2003889facbeSLen Brown unsigned long long msr; 20040102b067SLen Brown int aperf_mperf_retry_count = 0; 2005388e9c81SLen Brown struct msr_counter *mp; 2006388e9c81SLen Brown int i; 2007c98d5d94SLen Brown 2008e52966c0SLen Brown if (cpu_migrate(cpu)) { 20093d7772eaSLen Brown fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu); 201015aaa346SLen Brown return -1; 2011e52966c0SLen Brown } 201215aaa346SLen Brown 2013d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 2014d4794f25SYazen Ghannam 20154c2122d4SLen Brown if (first_counter_read) 20164c2122d4SLen Brown get_apic_id(t); 20170102b067SLen Brown retry: 2018c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 201915aaa346SLen Brown 20201b439f01SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) { 20210102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 20220102b067SLen Brown 20230102b067SLen Brown /* 20240102b067SLen Brown * The TSC, APERF and MPERF must be read together for 20250102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 20260102b067SLen Brown * 20270102b067SLen Brown * Unfortunately, APERF and MPERF are read by 20280102b067SLen Brown * individual system call, so delays may occur 20290102b067SLen Brown * between them. If the time to read them 20300102b067SLen Brown * varies by a large amount, we re-read them. 20310102b067SLen Brown */ 20320102b067SLen Brown 20330102b067SLen Brown /* 20340102b067SLen Brown * This initial dummy APERF read has been seen to 20350102b067SLen Brown * reduce jitter in the subsequent reads. 20360102b067SLen Brown */ 20370102b067SLen Brown 20389c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 2039c98d5d94SLen Brown return -3; 20400102b067SLen Brown 20410102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 20420102b067SLen Brown 20430102b067SLen Brown tsc_before = t->tsc; 20440102b067SLen Brown 20450102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 20460102b067SLen Brown return -3; 20470102b067SLen Brown 20480102b067SLen Brown tsc_between = rdtsc(); 20490102b067SLen Brown 20509c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 2051c98d5d94SLen Brown return -4; 20520102b067SLen Brown 20530102b067SLen Brown tsc_after = rdtsc(); 20540102b067SLen Brown 20550102b067SLen Brown aperf_time = tsc_between - tsc_before; 20560102b067SLen Brown mperf_time = tsc_after - tsc_between; 20570102b067SLen Brown 20580102b067SLen Brown /* 20590102b067SLen Brown * If the system call latency to read APERF and MPERF 20600102b067SLen Brown * differ by more than 2x, then try again. 20610102b067SLen Brown */ 20620102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 20630102b067SLen Brown aperf_mperf_retry_count++; 20640102b067SLen Brown if (aperf_mperf_retry_count < 5) 20650102b067SLen Brown goto retry; 20660102b067SLen Brown else 20671b439f01SLen Brown warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time); 20680102b067SLen Brown } 20690102b067SLen Brown aperf_mperf_retry_count = 0; 20700102b067SLen Brown 2071b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 2072b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 207388c3281fSLen Brown } 207488c3281fSLen Brown 20752af4f9b8SLen Brown if (DO_BIC(BIC_IPC)) 20762af4f9b8SLen Brown if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long)) 20772af4f9b8SLen Brown return -4; 20782af4f9b8SLen Brown 2079812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 2080562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 2081812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 20821ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 20831ed51011SLen Brown return -5; 20841ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 20851ed51011SLen Brown } 20860539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 2087144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 2088144b44b1SLen Brown return -6; 2089144b44b1SLen Brown } 2090144b44b1SLen Brown 2091388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 2092495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 2093388e9c81SLen Brown return -10; 2094388e9c81SLen Brown } 2095388e9c81SLen Brown 2096c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 2097c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 2098f4fdf2b4SLen Brown goto done; 2099c98d5d94SLen Brown 21001e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 2101c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 2102c98d5d94SLen Brown return -6; 2103144b44b1SLen Brown } 2104144b44b1SLen Brown 21051e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 2106c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 2107c98d5d94SLen Brown return -7; 21081e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 2109fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 2110fb5d4327SDasaratharaman Chandramouli return -7; 2111103a8feaSLen Brown } 211215aaa346SLen Brown 21137ab5ff49SZhang Rui if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) { 2114c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 2115c98d5d94SLen Brown return -8; 21167ab5ff49SZhang Rui else if (t->is_atom) { 21177ab5ff49SZhang Rui /* 21187ab5ff49SZhang Rui * For Atom CPUs that has core cstate deeper than c6, 21197ab5ff49SZhang Rui * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper. 21207ab5ff49SZhang Rui * Minus CC7 (and deeper cstates) residency to get 21217ab5ff49SZhang Rui * accturate cc6 residency. 21227ab5ff49SZhang Rui */ 21237ab5ff49SZhang Rui c->c6 -= c->c7; 21247ab5ff49SZhang Rui } 21257ab5ff49SZhang Rui } 2126c98d5d94SLen Brown 21270539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 21280539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 21290539ba11SLen Brown return -8; 21300539ba11SLen Brown 2131812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 2132889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 2133889facbeSLen Brown return -9; 213455279aefSZhang Rui c->core_temp_c = tj_max - ((msr >> 16) & 0x7F); 2135889facbeSLen Brown } 2136889facbeSLen Brown 21379392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 21389392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 21399392bd98SCalvin Walton return -14; 21409392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 21419392bd98SCalvin Walton } 21429392bd98SCalvin Walton 2143388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2144495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2145388e9c81SLen Brown return -10; 2146388e9c81SLen Brown } 2147889facbeSLen Brown 2148c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2149c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2150f4fdf2b4SLen Brown goto done; 215115aaa346SLen Brown 2152a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 21530b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 21540b2bb692SLen Brown return -10; 2155a99d8730SLen Brown } 2156a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 21570b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 21580b2bb692SLen Brown return -11; 2159a99d8730SLen Brown } 2160a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 21610b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 21620b2bb692SLen Brown return -12; 2163a99d8730SLen Brown } 2164a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 21650b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 21660b2bb692SLen Brown return -13; 21670b2bb692SLen Brown } 21680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2169c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2170c98d5d94SLen Brown return -9; 21710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 21720539ba11SLen Brown if (do_slm_cstates) { 21730539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 21740539ba11SLen Brown return -10; 21750539ba11SLen Brown } else { 2176c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2177c98d5d94SLen Brown return -10; 21780539ba11SLen Brown } 21790539ba11SLen Brown } 21800539ba11SLen Brown 21810f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2182c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2183c98d5d94SLen Brown return -11; 21840f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2185c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2186c98d5d94SLen Brown return -12; 21870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2188ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2189ca58710fSKristen Carlson Accardi return -13; 21900f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2191ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2192ca58710fSKristen Carlson Accardi return -13; 21930f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2194ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2195ca58710fSKristen Carlson Accardi return -13; 21960f47c08dSLen Brown 2197be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2198be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2199be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2200be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2201be0e54c4SLen Brown 2202889facbeSLen Brown if (do_rapl & RAPL_PKG) { 22039972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2204889facbeSLen Brown return -13; 22059972d5d8SChen Yu p->energy_pkg = msr; 2206889facbeSLen Brown } 22079148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 22089972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2209889facbeSLen Brown return -14; 22109972d5d8SChen Yu p->energy_cores = msr; 2211889facbeSLen Brown } 2212889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 22139972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2214889facbeSLen Brown return -15; 22159972d5d8SChen Yu p->energy_dram = msr; 2216889facbeSLen Brown } 2217889facbeSLen Brown if (do_rapl & RAPL_GFX) { 22189972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2219889facbeSLen Brown return -16; 22209972d5d8SChen Yu p->energy_gfx = msr; 2221889facbeSLen Brown } 2222889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 22239972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2224889facbeSLen Brown return -16; 22259972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2226889facbeSLen Brown } 2227889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 22289972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2229889facbeSLen Brown return -16; 22309972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2231889facbeSLen Brown } 22323316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 22339972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 22343316f99aSCalvin Walton return -13; 22359972d5d8SChen Yu p->energy_pkg = msr; 22363316f99aSCalvin Walton } 2237812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2238889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2239889facbeSLen Brown return -17; 224055279aefSZhang Rui p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F); 2241889facbeSLen Brown } 2242fdf676e5SLen Brown 2243812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2244fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2245fdf676e5SLen Brown 2246812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 224727d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 224827d47356SLen Brown 2249b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 2250b4b91569SRafael Antognolli p->gfx_act_mhz = gfx_act_mhz; 2251b4b91569SRafael Antognolli 2252388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2253495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2254388e9c81SLen Brown return -10; 2255388e9c81SLen Brown } 2256f4fdf2b4SLen Brown done: 2257f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2258388e9c81SLen Brown 225915aaa346SLen Brown return 0; 2260103a8feaSLen Brown } 2261103a8feaSLen Brown 2262ee7e38e3SLen Brown /* 2263ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2264ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2265ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2266ee7e38e3SLen Brown */ 2267ee7e38e3SLen Brown 2268ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2269ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2270ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2271ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2272ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2273ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2274ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2275ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2276ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2277ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2278ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2279ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 22800b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 22810b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2282445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2283445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2284ee7e38e3SLen Brown 2285ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2286ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 22871b439f01SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited" 22881b439f01SLen Brown }; 2289ee7e38e3SLen Brown 22901b439f01SLen Brown int nhm_pkg_cstate_limits[16] = 22911b439f01SLen Brown { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 229238c6663aSLen Brown PCLRSV, PCLRSV 229338c6663aSLen Brown }; 229438c6663aSLen Brown 22951b439f01SLen Brown int snb_pkg_cstate_limits[16] = 22961b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 229738c6663aSLen Brown PCLRSV, PCLRSV 229838c6663aSLen Brown }; 229938c6663aSLen Brown 23001b439f01SLen Brown int hsw_pkg_cstate_limits[16] = 23011b439f01SLen Brown { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 230238c6663aSLen Brown PCLRSV, PCLRSV 230338c6663aSLen Brown }; 230438c6663aSLen Brown 23051b439f01SLen Brown int slv_pkg_cstate_limits[16] = 23061b439f01SLen Brown { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 230738c6663aSLen Brown PCL__6, PCL__7 230838c6663aSLen Brown }; 230938c6663aSLen Brown 23101b439f01SLen Brown int amt_pkg_cstate_limits[16] = 23111b439f01SLen Brown { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 231238c6663aSLen Brown PCLRSV, PCLRSV 231338c6663aSLen Brown }; 231438c6663aSLen Brown 23151b439f01SLen Brown int phi_pkg_cstate_limits[16] = 23161b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 231738c6663aSLen Brown PCLRSV, PCLRSV 231838c6663aSLen Brown }; 231938c6663aSLen Brown 23201b439f01SLen Brown int glm_pkg_cstate_limits[16] = 23211b439f01SLen Brown { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 232238c6663aSLen Brown PCLRSV, PCLRSV 232338c6663aSLen Brown }; 232438c6663aSLen Brown 23251b439f01SLen Brown int skx_pkg_cstate_limits[16] = 23261b439f01SLen Brown { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 232738c6663aSLen Brown PCLRSV, PCLRSV 232838c6663aSLen Brown }; 232938c6663aSLen Brown 23301b439f01SLen Brown int icx_pkg_cstate_limits[16] = 23311b439f01SLen Brown { PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, 233238c6663aSLen Brown PCLRSV, PCLRSV 233338c6663aSLen Brown }; 2334a2b7b749SLen Brown 23351b439f01SLen Brown static void calculate_tsc_tweak() 2336a2b7b749SLen Brown { 2337a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2338a2b7b749SLen Brown } 2339a2b7b749SLen Brown 2340aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model); 2341aeb01e6dSChen Yu 23421b439f01SLen Brown static void dump_nhm_platform_info(void) 2343103a8feaSLen Brown { 2344103a8feaSLen Brown unsigned long long msr; 2345103a8feaSLen Brown unsigned int ratio; 2346103a8feaSLen Brown 2347ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2348103a8feaSLen Brown 2349b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 23506574a5d5SLen Brown 2351103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 23521b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk); 2353103a8feaSLen Brown 2354103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 23551b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 2356103a8feaSLen Brown 23577ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2358b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2359bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 236067920418SLen Brown 2361aeb01e6dSChen Yu /* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */ 2362aeb01e6dSChen Yu if (dis_cstate_prewake) 23631b439f01SLen Brown fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN"); 2364aeb01e6dSChen Yu 2365fcd17211SLen Brown return; 2366fcd17211SLen Brown } 2367fcd17211SLen Brown 23681b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void) 2369fcd17211SLen Brown { 2370fcd17211SLen Brown unsigned long long msr; 2371fcd17211SLen Brown unsigned int ratio; 2372fcd17211SLen Brown 23737ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2374fcd17211SLen Brown 2375b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2376fcd17211SLen Brown 2377fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2378fcd17211SLen Brown if (ratio) 23791b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk); 2380fcd17211SLen Brown 2381fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2382fcd17211SLen Brown if (ratio) 23831b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk); 2384fcd17211SLen Brown return; 2385fcd17211SLen Brown } 2386fcd17211SLen Brown 23871b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void) 2388fcd17211SLen Brown { 2389fcd17211SLen Brown unsigned long long msr; 2390fcd17211SLen Brown unsigned int ratio; 23916574a5d5SLen Brown 23927ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 23936574a5d5SLen Brown 2394b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 23956574a5d5SLen Brown 23966574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 23976574a5d5SLen Brown if (ratio) 23981b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk); 23996574a5d5SLen Brown 24006574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 24016574a5d5SLen Brown if (ratio) 24021b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk); 24036574a5d5SLen Brown 24046574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 24056574a5d5SLen Brown if (ratio) 24061b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk); 24076574a5d5SLen Brown 24086574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 24096574a5d5SLen Brown if (ratio) 24101b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk); 24116574a5d5SLen Brown 24126574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 24136574a5d5SLen Brown if (ratio) 24141b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk); 24156574a5d5SLen Brown 24166574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 24176574a5d5SLen Brown if (ratio) 24181b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk); 24196574a5d5SLen Brown 24206574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 24216574a5d5SLen Brown if (ratio) 24221b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk); 24236574a5d5SLen Brown 24246574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 24256574a5d5SLen Brown if (ratio) 24261b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk); 2427103a8feaSLen Brown return; 2428fcd17211SLen Brown } 24291b439f01SLen Brown 243031e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 243131e07522SLen Brown { 243231e07522SLen Brown 243331e07522SLen Brown if (!genuine_intel) 243431e07522SLen Brown return 0; 243531e07522SLen Brown 243631e07522SLen Brown switch (model) { 243731e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 243831e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 243925368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 24405ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 244120de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 244231e07522SLen Brown return 1; 244331e07522SLen Brown } 244431e07522SLen Brown return 0; 244531e07522SLen Brown } 2446fcd17211SLen Brown 24471b439f01SLen Brown static void dump_turbo_ratio_limits(int family, int model) 2448fcd17211SLen Brown { 244931e07522SLen Brown unsigned long long msr, core_counts; 245031e07522SLen Brown unsigned int ratio, group_size; 2451103a8feaSLen Brown 24527ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2453b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 24546574a5d5SLen Brown 245531e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 245631e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 245731e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 245831e07522SLen Brown } else { 245931e07522SLen Brown core_counts = 0x0807060504030201; 246031e07522SLen Brown } 246131e07522SLen Brown 24626574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 246331e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 24646574a5d5SLen Brown if (ratio) 246531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 246631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24676574a5d5SLen Brown 24686574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 246931e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 24706574a5d5SLen Brown if (ratio) 247131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 247231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24736574a5d5SLen Brown 24746574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 247531e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 24766574a5d5SLen Brown if (ratio) 247731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 247831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24796574a5d5SLen Brown 24806574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 248131e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 24826574a5d5SLen Brown if (ratio) 248331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 248431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 24856574a5d5SLen Brown 2486103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 248731e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2488103a8feaSLen Brown if (ratio) 248931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 249031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2491103a8feaSLen Brown 2492103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 249331e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2494103a8feaSLen Brown if (ratio) 249531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 249631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2497103a8feaSLen Brown 2498103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 249931e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2500103a8feaSLen Brown if (ratio) 250131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 250231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2503103a8feaSLen Brown 2504103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 250531e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2506103a8feaSLen Brown if (ratio) 250731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 250831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2509fcd17211SLen Brown return; 2510fcd17211SLen Brown } 25113a9a941dSLen Brown 25121b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void) 25130f7887c4SLen Brown { 25140f7887c4SLen Brown unsigned long long msr; 25150f7887c4SLen Brown unsigned int ratio; 25160f7887c4SLen Brown 25170f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 25180f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25190f7887c4SLen Brown 25200f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25210f7887c4SLen Brown if (ratio) 25221b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk); 25230f7887c4SLen Brown 25240f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25250f7887c4SLen Brown if (ratio) 25261b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk); 25270f7887c4SLen Brown 25280f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25290f7887c4SLen Brown if (ratio) 25301b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk); 25310f7887c4SLen Brown 25320f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 25330f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 25340f7887c4SLen Brown 25350f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 25360f7887c4SLen Brown if (ratio) 25371b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk); 25380f7887c4SLen Brown 25390f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 25400f7887c4SLen Brown if (ratio) 25411b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk); 25420f7887c4SLen Brown 25430f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 25440f7887c4SLen Brown if (ratio) 25451b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk); 25460f7887c4SLen Brown 25470f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 25480f7887c4SLen Brown if (ratio) 25491b439f01SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk); 25500f7887c4SLen Brown } 25510f7887c4SLen Brown 25521b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void) 2553fb5d4327SDasaratharaman Chandramouli { 2554cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2555cbf97abaSHubert Chrzaniuk 2556fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2557cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2558cbf97abaSHubert Chrzaniuk int i, b_nr; 2559cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2560cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2561fb5d4327SDasaratharaman Chandramouli 2562ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2563fb5d4327SDasaratharaman Chandramouli 25641b439f01SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 2565fb5d4327SDasaratharaman Chandramouli 25661e3ec5cdSRandy Dunlap /* 2567fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2568cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2569cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2570fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2571fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2572fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2573fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2574fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2575fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2576fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2577fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2578fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2579fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2580fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2581fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2582fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2583fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2584fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2585fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2586fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2587fb5d4327SDasaratharaman Chandramouli */ 2588fb5d4327SDasaratharaman Chandramouli 2589cbf97abaSHubert Chrzaniuk b_nr = 0; 2590cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2591cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2592cbf97abaSHubert Chrzaniuk 2593cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2594fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2595cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2596fb5d4327SDasaratharaman Chandramouli 2597cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2598cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2599cbf97abaSHubert Chrzaniuk b_nr++; 2600fb5d4327SDasaratharaman Chandramouli } 2601cbf97abaSHubert Chrzaniuk 2602cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2603cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2604b7d8c148SLen Brown fprintf(outf, 2605710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2606cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2607fb5d4327SDasaratharaman Chandramouli } 2608fb5d4327SDasaratharaman Chandramouli 26091b439f01SLen Brown static void dump_nhm_cst_cfg(void) 2610fcd17211SLen Brown { 2611fcd17211SLen Brown unsigned long long msr; 2612fcd17211SLen Brown 26131df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2614fcd17211SLen Brown 26151df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2616fcd17211SLen Brown 26173e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2618fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2619fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2620fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2621fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 26221b439f01SLen Brown (msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]); 2623ac980e13SArtem Bityutskiy 2624ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2625ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 26261b439f01SLen Brown fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2627ac980e13SArtem Bityutskiy } 2628ac980e13SArtem Bityutskiy 2629ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2630ac980e13SArtem Bityutskiy 2631fcd17211SLen Brown return; 2632103a8feaSLen Brown } 2633103a8feaSLen Brown 26341b439f01SLen Brown static void dump_config_tdp(void) 26356fb3143bSLen Brown { 26366fb3143bSLen Brown unsigned long long msr; 26376fb3143bSLen Brown 26386fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2639b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2640685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 26416fb3143bSLen Brown 26426fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2643b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 26446fb3143bSLen Brown if (msr) { 2645685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2646685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2647685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2648685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 26496fb3143bSLen Brown } 2650b7d8c148SLen Brown fprintf(outf, ")\n"); 26516fb3143bSLen Brown 26526fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2653b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 26546fb3143bSLen Brown if (msr) { 2655685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2656685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2657685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2658685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 26596fb3143bSLen Brown } 2660b7d8c148SLen Brown fprintf(outf, ")\n"); 26616fb3143bSLen Brown 26626fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2663b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 26646fb3143bSLen Brown if ((msr) & 0x3) 2665b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2666b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2667b7d8c148SLen Brown fprintf(outf, ")\n"); 26686fb3143bSLen Brown 26696fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2670b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2671685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2672b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2673b7d8c148SLen Brown fprintf(outf, ")\n"); 26746fb3143bSLen Brown } 26755a63426eSLen Brown 26765a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 26775a63426eSLen Brown 26785a63426eSLen Brown void print_irtl(void) 26795a63426eSLen Brown { 26805a63426eSLen Brown unsigned long long msr; 26815a63426eSLen Brown 26825a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 26835a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_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_PKGC6_IRTL, &msr); 26885a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_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_PKGC7_IRTL, &msr); 26935a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_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 if (!do_irtl_hsw) 26985a63426eSLen Brown return; 26995a63426eSLen Brown 27005a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 27015a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 27025a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27035a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27045a63426eSLen Brown 27055a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 27065a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 27075a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27085a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27095a63426eSLen Brown 27105a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 27115a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 27125a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 27135a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 27145a63426eSLen Brown 27155a63426eSLen Brown } 27161b439f01SLen Brown 271736229897SLen Brown void free_fd_percpu(void) 271836229897SLen Brown { 271936229897SLen Brown int i; 272036229897SLen Brown 272101a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 272236229897SLen Brown if (fd_percpu[i] != 0) 272336229897SLen Brown close(fd_percpu[i]); 272436229897SLen Brown } 272536229897SLen Brown 272636229897SLen Brown free(fd_percpu); 27276fb3143bSLen Brown } 27286fb3143bSLen Brown 2729c98d5d94SLen Brown void free_all_buffers(void) 2730103a8feaSLen Brown { 27310e2d8f05SLen Brown int i; 27320e2d8f05SLen Brown 2733c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2734c98d5d94SLen Brown cpu_present_set = NULL; 273536229897SLen Brown cpu_present_setsize = 0; 2736103a8feaSLen Brown 2737c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2738c98d5d94SLen Brown cpu_affinity_set = NULL; 2739c98d5d94SLen Brown cpu_affinity_setsize = 0; 2740103a8feaSLen Brown 2741c98d5d94SLen Brown free(thread_even); 2742c98d5d94SLen Brown free(core_even); 2743c98d5d94SLen Brown free(package_even); 2744103a8feaSLen Brown 2745c98d5d94SLen Brown thread_even = NULL; 2746c98d5d94SLen Brown core_even = NULL; 2747c98d5d94SLen Brown package_even = NULL; 2748103a8feaSLen Brown 2749c98d5d94SLen Brown free(thread_odd); 2750c98d5d94SLen Brown free(core_odd); 2751c98d5d94SLen Brown free(package_odd); 2752103a8feaSLen Brown 2753c98d5d94SLen Brown thread_odd = NULL; 2754c98d5d94SLen Brown core_odd = NULL; 2755c98d5d94SLen Brown package_odd = NULL; 2756103a8feaSLen Brown 2757c98d5d94SLen Brown free(output_buffer); 2758c98d5d94SLen Brown output_buffer = NULL; 2759c98d5d94SLen Brown outp = NULL; 276036229897SLen Brown 276136229897SLen Brown free_fd_percpu(); 2762562a2d37SLen Brown 2763562a2d37SLen Brown free(irq_column_2_cpu); 2764562a2d37SLen Brown free(irqs_per_cpu); 27650e2d8f05SLen Brown 27660e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 27670e2d8f05SLen Brown if (cpus[i].put_ids) 27680e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 27690e2d8f05SLen Brown } 27700e2d8f05SLen Brown free(cpus); 2771103a8feaSLen Brown } 2772103a8feaSLen Brown 277357a42a34SJosh Triplett /* 277495aebc44SJosh Triplett * Parse a file containing a single int. 27756de68fe1SLen Brown * Return 0 if file can not be opened 27766de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 277795aebc44SJosh Triplett */ 277895aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 277995aebc44SJosh Triplett { 278095aebc44SJosh Triplett va_list args; 278195aebc44SJosh Triplett char path[PATH_MAX]; 278295aebc44SJosh Triplett FILE *filep; 278395aebc44SJosh Triplett int value; 278495aebc44SJosh Triplett 278595aebc44SJosh Triplett va_start(args, fmt); 278695aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 278795aebc44SJosh Triplett va_end(args); 27886de68fe1SLen Brown filep = fopen(path, "r"); 27896de68fe1SLen Brown if (!filep) 27906de68fe1SLen Brown return 0; 2791b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2792b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 279395aebc44SJosh Triplett fclose(filep); 279495aebc44SJosh Triplett return value; 279595aebc44SJosh Triplett } 279695aebc44SJosh Triplett 279795aebc44SJosh Triplett /* 2798c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2799c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2800c98d5d94SLen Brown */ 2801c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2802c98d5d94SLen Brown { 280395aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2804103a8feaSLen Brown } 2805103a8feaSLen Brown 2806103a8feaSLen Brown int get_physical_package_id(int cpu) 2807103a8feaSLen Brown { 280895aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2809103a8feaSLen Brown } 2810103a8feaSLen Brown 28116de68fe1SLen Brown int get_die_id(int cpu) 28126de68fe1SLen Brown { 28136de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 28146de68fe1SLen Brown } 28156de68fe1SLen Brown 2816103a8feaSLen Brown int get_core_id(int cpu) 2817103a8feaSLen Brown { 281895aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2819103a8feaSLen Brown } 2820103a8feaSLen Brown 2821ef605741SPrarit Bhargava void set_node_data(void) 2822ef605741SPrarit Bhargava { 28232ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 28242ffbb224SPrarit Bhargava int cpu_count; 2825ef605741SPrarit Bhargava 28262ffbb224SPrarit Bhargava /* initialize logical_node_id */ 28272ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 28282ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2829ef605741SPrarit Bhargava 28302ffbb224SPrarit Bhargava cpu_count = 0; 28312ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 28322ffbb224SPrarit Bhargava lnode = 0; 28332ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 28342ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2835ef605741SPrarit Bhargava continue; 28362ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 28372ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 28382ffbb224SPrarit Bhargava continue; 28392ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2840ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 28412ffbb224SPrarit Bhargava cpu_count++; 28422ffbb224SPrarit Bhargava /* 28432ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 28442ffbb224SPrarit Bhargava * the logical_node_id 28452ffbb224SPrarit Bhargava */ 28462ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 28471b439f01SLen Brown if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) { 28482ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 28492ffbb224SPrarit Bhargava cpu_count++; 2850ef605741SPrarit Bhargava } 28512ffbb224SPrarit Bhargava } 28522ffbb224SPrarit Bhargava lnode++; 28532ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 28542ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 28552ffbb224SPrarit Bhargava } 28562ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 28572ffbb224SPrarit Bhargava break; 28582ffbb224SPrarit Bhargava } 2859ef605741SPrarit Bhargava } 2860ef605741SPrarit Bhargava 2861ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2862c98d5d94SLen Brown { 2863c98d5d94SLen Brown char path[80]; 2864c98d5d94SLen Brown FILE *filep; 28650e2d8f05SLen Brown int i; 28660e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2867c98d5d94SLen Brown 28680e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 28691b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i); 28700e2d8f05SLen Brown filep = fopen(path, "r"); 28710e2d8f05SLen Brown if (!filep) 28720e2d8f05SLen Brown continue; 28730e2d8f05SLen Brown fclose(filep); 28740e2d8f05SLen Brown return i; 28750e2d8f05SLen Brown } 28760e2d8f05SLen Brown return -1; 2877e275b388SDasaratharaman Chandramouli } 2878c98d5d94SLen Brown 28790e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 28800e2d8f05SLen Brown { 28810e2d8f05SLen Brown char path[80], character; 28820e2d8f05SLen Brown FILE *filep; 28830e2d8f05SLen Brown unsigned long map; 28848cb48b32SPrarit Bhargava int so, shift, sib_core; 28850e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 28860e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 28870e2d8f05SLen Brown size_t size; 28888cb48b32SPrarit Bhargava int thread_id = 0; 28890e2d8f05SLen Brown 28900e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 28918cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 28928cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 28930e2d8f05SLen Brown if (!thiscpu->put_ids) 28940e2d8f05SLen Brown return -1; 28950e2d8f05SLen Brown 28960e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 28970e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 28980e2d8f05SLen Brown 28991b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 29003d7772eaSLen Brown filep = fopen(path, "r"); 29013d7772eaSLen Brown 29023d7772eaSLen Brown if (!filep) { 29033d7772eaSLen Brown warnx("%s: open failed", path); 29043d7772eaSLen Brown return -1; 29053d7772eaSLen Brown } 29060e2d8f05SLen Brown do { 29070e2d8f05SLen Brown offset -= BITMASK_SIZE; 29088173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 29098173c336SBen Hutchings err(1, "%s: failed to parse file", path); 29100e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 29110e2d8f05SLen Brown if ((map >> shift) & 0x1) { 29128cb48b32SPrarit Bhargava so = shift + offset; 29138cb48b32SPrarit Bhargava sib_core = get_core_id(so); 29148cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 29158cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 29161b439f01SLen Brown if ((so != cpu) && (cpus[so].thread_id < 0)) 29171b439f01SLen Brown cpus[so].thread_id = thread_id++; 29188cb48b32SPrarit Bhargava } 29190e2d8f05SLen Brown } 29200e2d8f05SLen Brown } 29210e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2922c98d5d94SLen Brown fclose(filep); 29230e2d8f05SLen Brown 29240e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2925c98d5d94SLen Brown } 2926c98d5d94SLen Brown 2927c98d5d94SLen Brown /* 2928c98d5d94SLen Brown * run func(thread, core, package) in topology order 2929c98d5d94SLen Brown * skip non-present cpus 2930103a8feaSLen Brown */ 2931103a8feaSLen Brown 2932c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *, 2933c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2934c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2935c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 29361b439f01SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2) 2937c98d5d94SLen Brown { 293840f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2939c98d5d94SLen Brown 2940c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 294140f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 29421b439f01SLen Brown for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 29431b439f01SLen Brown for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) { 2944c98d5d94SLen Brown struct thread_data *t, *t2; 2945c98d5d94SLen Brown struct core_data *c, *c2; 2946c98d5d94SLen Brown struct pkg_data *p, *p2; 2947c98d5d94SLen Brown 29481b439f01SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no); 2949c98d5d94SLen Brown 2950c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2951c98d5d94SLen Brown continue; 2952c98d5d94SLen Brown 29531b439f01SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no); 2954c98d5d94SLen Brown 29551b439f01SLen Brown c = GET_CORE(core_base, core_no, node_no, pkg_no); 29561b439f01SLen Brown c2 = GET_CORE(core_base2, core_no, node_no, pkg_no); 2957c98d5d94SLen Brown 2958c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2959c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2960c98d5d94SLen Brown 2961c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2962c98d5d94SLen Brown if (retval) 2963c98d5d94SLen Brown return retval; 2964c98d5d94SLen Brown } 2965c98d5d94SLen Brown } 2966c98d5d94SLen Brown } 296740f5cfe7SPrarit Bhargava } 2968c98d5d94SLen Brown return 0; 2969c98d5d94SLen Brown } 2970c98d5d94SLen Brown 2971c98d5d94SLen Brown /* 2972c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2973c98d5d94SLen Brown * return max_cpu number 2974c98d5d94SLen Brown */ 2975c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int)) 2976103a8feaSLen Brown { 2977103a8feaSLen Brown FILE *fp; 2978c98d5d94SLen Brown int cpu_num; 2979103a8feaSLen Brown int retval; 2980103a8feaSLen Brown 298157a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2982103a8feaSLen Brown 2983103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2984b2c95d90SJosh Triplett if (retval != 0) 2985b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2986103a8feaSLen Brown 2987c98d5d94SLen Brown while (1) { 2988c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2989103a8feaSLen Brown if (retval != 1) 2990103a8feaSLen Brown break; 2991103a8feaSLen Brown 2992c98d5d94SLen Brown retval = func(cpu_num); 2993c98d5d94SLen Brown if (retval) { 2994c98d5d94SLen Brown fclose(fp); 2995c98d5d94SLen Brown return (retval); 2996c98d5d94SLen Brown } 2997103a8feaSLen Brown } 2998103a8feaSLen Brown fclose(fp); 2999c98d5d94SLen Brown return 0; 3000103a8feaSLen Brown } 3001103a8feaSLen Brown 3002103a8feaSLen Brown void re_initialize(void) 3003103a8feaSLen Brown { 3004c98d5d94SLen Brown free_all_buffers(); 3005c98d5d94SLen Brown setup_all_buffers(); 30063d7772eaSLen Brown fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 3007103a8feaSLen Brown } 3008103a8feaSLen Brown 3009843c5791SPrarit Bhargava void set_max_cpu_num(void) 3010843c5791SPrarit Bhargava { 3011843c5791SPrarit Bhargava FILE *filep; 30128201a028SPrarit Bhargava int base_cpu; 3013843c5791SPrarit Bhargava unsigned long dummy; 30148201a028SPrarit Bhargava char pathname[64]; 3015843c5791SPrarit Bhargava 30168201a028SPrarit Bhargava base_cpu = sched_getcpu(); 30178201a028SPrarit Bhargava if (base_cpu < 0) 30188201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 30191b439f01SLen Brown sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu); 30208201a028SPrarit Bhargava 30218201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 3022843c5791SPrarit Bhargava topo.max_cpu_num = 0; 3023843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 30240e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 3025843c5791SPrarit Bhargava fclose(filep); 3026843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 3027843c5791SPrarit Bhargava } 3028c98d5d94SLen Brown 3029103a8feaSLen Brown /* 3030c98d5d94SLen Brown * count_cpus() 3031c98d5d94SLen Brown * remember the last one seen, it will be the max 3032103a8feaSLen Brown */ 3033c98d5d94SLen Brown int count_cpus(int cpu) 3034103a8feaSLen Brown { 3035843c5791SPrarit Bhargava topo.num_cpus++; 3036c98d5d94SLen Brown return 0; 3037103a8feaSLen Brown } 30381b439f01SLen Brown 3039c98d5d94SLen Brown int mark_cpu_present(int cpu) 3040c98d5d94SLen Brown { 3041c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 304215aaa346SLen Brown return 0; 3043103a8feaSLen Brown } 3044103a8feaSLen Brown 30458cb48b32SPrarit Bhargava int init_thread_id(int cpu) 30468cb48b32SPrarit Bhargava { 30478cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 30488cb48b32SPrarit Bhargava return 0; 30498cb48b32SPrarit Bhargava } 30508cb48b32SPrarit Bhargava 3051562a2d37SLen Brown /* 3052562a2d37SLen Brown * snapshot_proc_interrupts() 3053562a2d37SLen Brown * 3054562a2d37SLen Brown * read and record summary of /proc/interrupts 3055562a2d37SLen Brown * 3056562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 3057562a2d37SLen Brown */ 3058562a2d37SLen Brown int snapshot_proc_interrupts(void) 3059562a2d37SLen Brown { 3060562a2d37SLen Brown static FILE *fp; 3061562a2d37SLen Brown int column, retval; 3062562a2d37SLen Brown 3063562a2d37SLen Brown if (fp == NULL) 3064562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 3065562a2d37SLen Brown else 3066562a2d37SLen Brown rewind(fp); 3067562a2d37SLen Brown 3068562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 3069562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3070562a2d37SLen Brown int cpu_number; 3071562a2d37SLen Brown 3072562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 3073562a2d37SLen Brown if (retval != 1) 3074562a2d37SLen Brown break; 3075562a2d37SLen Brown 3076562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 3077562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 3078562a2d37SLen Brown return 1; 3079562a2d37SLen Brown } 3080562a2d37SLen Brown 3081562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 3082562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 3083562a2d37SLen Brown } 3084562a2d37SLen Brown 3085562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 3086562a2d37SLen Brown while (1) { 3087562a2d37SLen Brown int column; 3088562a2d37SLen Brown char buf[64]; 3089562a2d37SLen Brown 3090562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 3091562a2d37SLen Brown if (retval != 1) 3092562a2d37SLen Brown break; 3093562a2d37SLen Brown 3094562a2d37SLen Brown /* read the count per cpu */ 3095562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 3096562a2d37SLen Brown 3097562a2d37SLen Brown int cpu_number, irq_count; 3098562a2d37SLen Brown 3099562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 3100562a2d37SLen Brown if (retval != 1) 3101562a2d37SLen Brown break; 3102562a2d37SLen Brown 3103562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 3104562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 3105562a2d37SLen Brown 3106562a2d37SLen Brown } 3107562a2d37SLen Brown 31081b439f01SLen Brown while (getc(fp) != '\n') ; /* flush interrupt description */ 3109562a2d37SLen Brown 3110562a2d37SLen Brown } 3111562a2d37SLen Brown return 0; 3112562a2d37SLen Brown } 31131b439f01SLen Brown 311427d47356SLen Brown /* 3115fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 3116fdf676e5SLen Brown * 3117fdf676e5SLen Brown * record snapshot of 3118fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 3119fdf676e5SLen Brown * 3120fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 3121fdf676e5SLen Brown */ 3122fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 3123fdf676e5SLen Brown { 3124fdf676e5SLen Brown FILE *fp; 3125fdf676e5SLen Brown int retval; 3126fdf676e5SLen Brown 3127fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 3128fdf676e5SLen Brown 3129fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 3130fdf676e5SLen Brown if (retval != 1) 3131fdf676e5SLen Brown err(1, "GFX rc6"); 3132fdf676e5SLen Brown 3133fdf676e5SLen Brown fclose(fp); 3134fdf676e5SLen Brown 3135fdf676e5SLen Brown return 0; 3136fdf676e5SLen Brown } 31371b439f01SLen Brown 3138fdf676e5SLen Brown /* 313927d47356SLen Brown * snapshot_gfx_mhz() 314027d47356SLen Brown * 314127d47356SLen Brown * record snapshot of 314227d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 314327d47356SLen Brown * 314427d47356SLen Brown * return 1 if config change requires a restart, else return 0 314527d47356SLen Brown */ 314627d47356SLen Brown int snapshot_gfx_mhz(void) 314727d47356SLen Brown { 314827d47356SLen Brown static FILE *fp; 314927d47356SLen Brown int retval; 315027d47356SLen Brown 315127d47356SLen Brown if (fp == NULL) 315227d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 315322048c54SLen Brown else { 315427d47356SLen Brown rewind(fp); 315522048c54SLen Brown fflush(fp); 315622048c54SLen Brown } 315727d47356SLen Brown 315827d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 315927d47356SLen Brown if (retval != 1) 316027d47356SLen Brown err(1, "GFX MHz"); 316127d47356SLen Brown 316227d47356SLen Brown return 0; 316327d47356SLen Brown } 3164562a2d37SLen Brown 3165562a2d37SLen Brown /* 3166b4b91569SRafael Antognolli * snapshot_gfx_cur_mhz() 3167b4b91569SRafael Antognolli * 3168b4b91569SRafael Antognolli * record snapshot of 3169b4b91569SRafael Antognolli * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz 3170b4b91569SRafael Antognolli * 3171b4b91569SRafael Antognolli * return 1 if config change requires a restart, else return 0 3172b4b91569SRafael Antognolli */ 3173b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void) 3174b4b91569SRafael Antognolli { 3175b4b91569SRafael Antognolli static FILE *fp; 3176b4b91569SRafael Antognolli int retval; 3177b4b91569SRafael Antognolli 3178b4b91569SRafael Antognolli if (fp == NULL) 3179b4b91569SRafael Antognolli fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r"); 3180b4b91569SRafael Antognolli else { 3181b4b91569SRafael Antognolli rewind(fp); 3182b4b91569SRafael Antognolli fflush(fp); 3183b4b91569SRafael Antognolli } 3184b4b91569SRafael Antognolli 3185b4b91569SRafael Antognolli retval = fscanf(fp, "%d", &gfx_act_mhz); 3186b4b91569SRafael Antognolli if (retval != 1) 3187b4b91569SRafael Antognolli err(1, "GFX ACT MHz"); 3188b4b91569SRafael Antognolli 3189b4b91569SRafael Antognolli return 0; 3190b4b91569SRafael Antognolli } 3191b4b91569SRafael Antognolli 3192b4b91569SRafael Antognolli /* 3193be0e54c4SLen Brown * snapshot_cpu_lpi() 3194be0e54c4SLen Brown * 3195be0e54c4SLen Brown * record snapshot of 3196be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3197be0e54c4SLen Brown */ 3198be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3199be0e54c4SLen Brown { 3200be0e54c4SLen Brown FILE *fp; 3201be0e54c4SLen Brown int retval; 3202be0e54c4SLen Brown 3203be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3204be0e54c4SLen Brown 3205be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 32065ea7647bSPrarit Bhargava if (retval != 1) { 32075ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 32085ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3209605736c6SGustavo A. R. Silva fclose(fp); 32105ea7647bSPrarit Bhargava return -1; 32115ea7647bSPrarit Bhargava } 3212be0e54c4SLen Brown 3213be0e54c4SLen Brown fclose(fp); 3214be0e54c4SLen Brown 3215be0e54c4SLen Brown return 0; 3216be0e54c4SLen Brown } 32171b439f01SLen Brown 3218be0e54c4SLen Brown /* 3219be0e54c4SLen Brown * snapshot_sys_lpi() 3220be0e54c4SLen Brown * 32211f81c5efSLen Brown * record snapshot of sys_lpi_file 3222be0e54c4SLen Brown */ 3223be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3224be0e54c4SLen Brown { 3225be0e54c4SLen Brown FILE *fp; 3226be0e54c4SLen Brown int retval; 3227be0e54c4SLen Brown 32281f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3229be0e54c4SLen Brown 3230be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 32315ea7647bSPrarit Bhargava if (retval != 1) { 32325ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 32335ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 323415423b95SColin Ian King fclose(fp); 32355ea7647bSPrarit Bhargava return -1; 32365ea7647bSPrarit Bhargava } 3237be0e54c4SLen Brown fclose(fp); 3238be0e54c4SLen Brown 3239be0e54c4SLen Brown return 0; 3240be0e54c4SLen Brown } 32411b439f01SLen Brown 3242be0e54c4SLen Brown /* 3243562a2d37SLen Brown * snapshot /proc and /sys files 3244562a2d37SLen Brown * 3245562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3246562a2d37SLen Brown */ 3247562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3248562a2d37SLen Brown { 3249218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3250562a2d37SLen Brown if (snapshot_proc_interrupts()) 3251562a2d37SLen Brown return 1; 3252562a2d37SLen Brown 3253812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3254fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3255fdf676e5SLen Brown 3256812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 325727d47356SLen Brown snapshot_gfx_mhz(); 325827d47356SLen Brown 3259b4b91569SRafael Antognolli if (DO_BIC(BIC_GFXACTMHz)) 3260b4b91569SRafael Antognolli snapshot_gfx_act_mhz(); 3261b4b91569SRafael Antognolli 3262be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3263be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3264be0e54c4SLen Brown 3265be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3266be0e54c4SLen Brown snapshot_sys_lpi_us(); 3267be0e54c4SLen Brown 3268562a2d37SLen Brown return 0; 3269562a2d37SLen Brown } 3270562a2d37SLen Brown 32718aa2ed0bSLen Brown int exit_requested; 32728aa2ed0bSLen Brown 32738aa2ed0bSLen Brown static void signal_handler(int signal) 32748aa2ed0bSLen Brown { 32758aa2ed0bSLen Brown switch (signal) { 32768aa2ed0bSLen Brown case SIGINT: 32778aa2ed0bSLen Brown exit_requested = 1; 32788aa2ed0bSLen Brown if (debug) 32798aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 32808aa2ed0bSLen Brown break; 328107211960SLen Brown case SIGUSR1: 328207211960SLen Brown if (debug > 1) 328307211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 328407211960SLen Brown break; 32858aa2ed0bSLen Brown } 32868aa2ed0bSLen Brown } 32878aa2ed0bSLen Brown 32888aa2ed0bSLen Brown void setup_signal_handler(void) 32898aa2ed0bSLen Brown { 32908aa2ed0bSLen Brown struct sigaction sa; 32918aa2ed0bSLen Brown 32928aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 32938aa2ed0bSLen Brown 32948aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 32958aa2ed0bSLen Brown 32968aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 32978aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 329807211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 329907211960SLen Brown err(1, "sigaction SIGUSR1"); 33008aa2ed0bSLen Brown } 3301b9ad8ee0SLen Brown 330247936f94SArtem Bityutskiy void do_sleep(void) 3303b9ad8ee0SLen Brown { 3304c026c236SArtem Bityutskiy struct timeval tout; 3305c026c236SArtem Bityutskiy struct timespec rest; 3306b9ad8ee0SLen Brown fd_set readfds; 3307b9ad8ee0SLen Brown int retval; 3308b9ad8ee0SLen Brown 3309b9ad8ee0SLen Brown FD_ZERO(&readfds); 3310b9ad8ee0SLen Brown FD_SET(0, &readfds); 3311b9ad8ee0SLen Brown 3312c026c236SArtem Bityutskiy if (ignore_stdin) { 331347936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 331447936f94SArtem Bityutskiy return; 331547936f94SArtem Bityutskiy } 3316b9ad8ee0SLen Brown 3317c026c236SArtem Bityutskiy tout = interval_tv; 3318c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3319b9ad8ee0SLen Brown 3320b9ad8ee0SLen Brown if (retval == 1) { 3321b9ad8ee0SLen Brown switch (getc(stdin)) { 3322b9ad8ee0SLen Brown case 'q': 3323b9ad8ee0SLen Brown exit_requested = 1; 3324b9ad8ee0SLen Brown break; 3325c026c236SArtem Bityutskiy case EOF: 3326c026c236SArtem Bityutskiy /* 3327c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3328c026c236SArtem Bityutskiy * won't be any further input. 3329c026c236SArtem Bityutskiy */ 3330c026c236SArtem Bityutskiy ignore_stdin = 1; 3331c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3332c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3333c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3334c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3335b9ad8ee0SLen Brown } 3336b9ad8ee0SLen Brown } 3337b9ad8ee0SLen Brown } 333847936f94SArtem Bityutskiy 333987e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 334087e15da9SChen Yu { 334187e15da9SChen Yu int ret, idx; 334287e15da9SChen Yu unsigned long long msr_cur, msr_last; 334387e15da9SChen Yu 334487e15da9SChen Yu if (!per_cpu_msr_sum) 334587e15da9SChen Yu return 1; 334687e15da9SChen Yu 334787e15da9SChen Yu idx = offset_to_idx(offset); 334887e15da9SChen Yu if (idx < 0) 334987e15da9SChen Yu return idx; 335087e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 335187e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 335287e15da9SChen Yu if (ret) 335387e15da9SChen Yu return ret; 335487e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 335587e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 335687e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 335787e15da9SChen Yu 335887e15da9SChen Yu return 0; 335987e15da9SChen Yu } 336087e15da9SChen Yu 336187e15da9SChen Yu timer_t timerid; 336287e15da9SChen Yu 336387e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 336487e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 336587e15da9SChen Yu { 336687e15da9SChen Yu int i, ret; 336787e15da9SChen Yu int cpu = t->cpu_id; 336887e15da9SChen Yu 336987e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 337087e15da9SChen Yu unsigned long long msr_cur, msr_last; 337113a779deSCalvin Walton off_t offset; 337287e15da9SChen Yu 337387e15da9SChen Yu if (!idx_valid(i)) 337487e15da9SChen Yu continue; 337587e15da9SChen Yu offset = idx_to_offset(i); 337687e15da9SChen Yu if (offset < 0) 337787e15da9SChen Yu continue; 337887e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 337987e15da9SChen Yu if (ret) { 33801b439f01SLen Brown fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset); 338187e15da9SChen Yu continue; 338287e15da9SChen Yu } 338387e15da9SChen Yu 338487e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 338587e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 338687e15da9SChen Yu 338787e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 338887e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 338987e15da9SChen Yu } 339087e15da9SChen Yu return 0; 339187e15da9SChen Yu } 339287e15da9SChen Yu 33931b439f01SLen Brown static void msr_record_handler(union sigval v) 339487e15da9SChen Yu { 339587e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 339687e15da9SChen Yu } 339787e15da9SChen Yu 339887e15da9SChen Yu void msr_sum_record(void) 339987e15da9SChen Yu { 340087e15da9SChen Yu struct itimerspec its; 340187e15da9SChen Yu struct sigevent sev; 340287e15da9SChen Yu 340387e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 340487e15da9SChen Yu if (!per_cpu_msr_sum) { 340587e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 340687e15da9SChen Yu return; 340787e15da9SChen Yu } 340887e15da9SChen Yu /* 340987e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 341087e15da9SChen Yu */ 341187e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 341287e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 341387e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 341487e15da9SChen Yu 341587e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 341687e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 341787e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 341887e15da9SChen Yu goto release_msr; 341987e15da9SChen Yu } 342087e15da9SChen Yu 342187e15da9SChen Yu its.it_value.tv_sec = 0; 342287e15da9SChen Yu its.it_value.tv_nsec = 1; 342387e15da9SChen Yu /* 342487e15da9SChen Yu * A wraparound time has been calculated early. 342587e15da9SChen Yu * Some sources state that the peak power for a 342687e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 342787e15da9SChen Yu * use 2 * TDP for safety. 342887e15da9SChen Yu */ 342987e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 343087e15da9SChen Yu its.it_interval.tv_nsec = 0; 343187e15da9SChen Yu 343287e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 343387e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 343487e15da9SChen Yu goto release_timer; 343587e15da9SChen Yu } 343687e15da9SChen Yu return; 343787e15da9SChen Yu 343887e15da9SChen Yu release_timer: 343987e15da9SChen Yu timer_delete(timerid); 344087e15da9SChen Yu release_msr: 344187e15da9SChen Yu free(per_cpu_msr_sum); 344287e15da9SChen Yu } 34434c2122d4SLen Brown 344438c6663aSLen Brown /* 344538c6663aSLen Brown * set_my_sched_priority(pri) 344638c6663aSLen Brown * return previous 344738c6663aSLen Brown */ 344838c6663aSLen Brown int set_my_sched_priority(int priority) 344938c6663aSLen Brown { 345038c6663aSLen Brown int retval; 345138c6663aSLen Brown int original_priority; 345238c6663aSLen Brown 345338c6663aSLen Brown errno = 0; 345438c6663aSLen Brown original_priority = getpriority(PRIO_PROCESS, 0); 345538c6663aSLen Brown if (errno && (original_priority == -1)) 345638c6663aSLen Brown err(errno, "getpriority"); 345738c6663aSLen Brown 345838c6663aSLen Brown retval = setpriority(PRIO_PROCESS, 0, priority); 345938c6663aSLen Brown if (retval) 346038c6663aSLen Brown err(retval, "setpriority(%d)", priority); 346138c6663aSLen Brown 346238c6663aSLen Brown errno = 0; 346338c6663aSLen Brown retval = getpriority(PRIO_PROCESS, 0); 346438c6663aSLen Brown if (retval != priority) 346538c6663aSLen Brown err(-1, "getpriority(%d) != setpriority(%d)", retval, priority); 346638c6663aSLen Brown 346738c6663aSLen Brown return original_priority; 346838c6663aSLen Brown } 346938c6663aSLen Brown 3470103a8feaSLen Brown void turbostat_loop() 3471103a8feaSLen Brown { 3472c98d5d94SLen Brown int retval; 3473e52966c0SLen Brown int restarted = 0; 3474023fe0acSChen Yu int done_iters = 0; 3475c98d5d94SLen Brown 34768aa2ed0bSLen Brown setup_signal_handler(); 34778aa2ed0bSLen Brown 347838c6663aSLen Brown /* 347938c6663aSLen Brown * elevate own priority for interval mode 348038c6663aSLen Brown */ 348138c6663aSLen Brown set_my_sched_priority(-20); 348238c6663aSLen Brown 3483103a8feaSLen Brown restart: 3484e52966c0SLen Brown restarted++; 3485e52966c0SLen Brown 3486562a2d37SLen Brown snapshot_proc_sysfs_files(); 3487c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 34884c2122d4SLen Brown first_counter_read = 0; 3489d91bb17cSLen Brown if (retval < -1) { 3490d91bb17cSLen Brown exit(retval); 3491d91bb17cSLen Brown } else if (retval == -1) { 34923d7772eaSLen Brown if (restarted > 10) { 3493e52966c0SLen Brown exit(retval); 3494e52966c0SLen Brown } 3495c98d5d94SLen Brown re_initialize(); 3496c98d5d94SLen Brown goto restart; 3497c98d5d94SLen Brown } 3498e52966c0SLen Brown restarted = 0; 3499023fe0acSChen Yu done_iters = 0; 3500103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3501103a8feaSLen Brown 3502103a8feaSLen Brown while (1) { 3503c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3504103a8feaSLen Brown re_initialize(); 3505103a8feaSLen Brown goto restart; 3506103a8feaSLen Brown } 3507b9ad8ee0SLen Brown do_sleep(); 3508562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3509562a2d37SLen Brown goto restart; 3510c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3511d91bb17cSLen Brown if (retval < -1) { 3512d91bb17cSLen Brown exit(retval); 3513d91bb17cSLen Brown } else if (retval == -1) { 351415aaa346SLen Brown re_initialize(); 351515aaa346SLen Brown goto restart; 351615aaa346SLen Brown } 3517103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3518103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3519ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3520ba3dec99SLen Brown re_initialize(); 3521ba3dec99SLen Brown goto restart; 3522ba3dec99SLen Brown } 3523c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3524c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3525b7d8c148SLen Brown flush_output_stdout(); 35268aa2ed0bSLen Brown if (exit_requested) 35278aa2ed0bSLen Brown break; 3528023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3529023fe0acSChen Yu break; 3530b9ad8ee0SLen Brown do_sleep(); 3531562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3532562a2d37SLen Brown goto restart; 3533c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3534d91bb17cSLen Brown if (retval < -1) { 3535d91bb17cSLen Brown exit(retval); 3536d91bb17cSLen Brown } else if (retval == -1) { 3537103a8feaSLen Brown re_initialize(); 3538103a8feaSLen Brown goto restart; 3539103a8feaSLen Brown } 3540103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3541103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3542ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3543ba3dec99SLen Brown re_initialize(); 3544ba3dec99SLen Brown goto restart; 3545ba3dec99SLen Brown } 3546c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3547c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3548b7d8c148SLen Brown flush_output_stdout(); 35498aa2ed0bSLen Brown if (exit_requested) 35508aa2ed0bSLen Brown break; 3551023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3552023fe0acSChen Yu break; 3553103a8feaSLen Brown } 3554103a8feaSLen Brown } 3555103a8feaSLen Brown 3556103a8feaSLen Brown void check_dev_msr() 3557103a8feaSLen Brown { 3558103a8feaSLen Brown struct stat sb; 35597ce7d5deSPrarit Bhargava char pathname[32]; 3560103a8feaSLen Brown 35617ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 35627ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3563a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3564d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3565103a8feaSLen Brown } 3566103a8feaSLen Brown 3567fcaa681cSLen Brown /* 3568fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3569fcaa681cSLen Brown * return 0 on success 3570fcaa681cSLen Brown * return 1 on fail 3571fcaa681cSLen Brown */ 3572fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3573103a8feaSLen Brown { 3574fcaa681cSLen Brown cap_t caps; 3575fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3576fcaa681cSLen Brown 3577fcaa681cSLen Brown caps = cap_get_proc(); 3578fcaa681cSLen Brown if (caps == NULL) 3579fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3580fcaa681cSLen Brown 3581fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3582fcaa681cSLen Brown err(-6, "cap_get\n"); 3583fcaa681cSLen Brown 3584fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 35851b439f01SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3586fcaa681cSLen Brown return 1; 3587fcaa681cSLen Brown } 3588fcaa681cSLen Brown 3589fcaa681cSLen Brown if (cap_free(caps) == -1) 3590fcaa681cSLen Brown err(-6, "cap_free\n"); 3591fcaa681cSLen Brown 3592fcaa681cSLen Brown return 0; 3593fcaa681cSLen Brown } 35941b439f01SLen Brown 3595fcaa681cSLen Brown void check_permissions(void) 3596fcaa681cSLen Brown { 359798481e79SLen Brown int do_exit = 0; 35987ce7d5deSPrarit Bhargava char pathname[32]; 359998481e79SLen Brown 360098481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3601fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 360298481e79SLen Brown 360398481e79SLen Brown /* test file permissions */ 36047ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 36057ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 360698481e79SLen Brown do_exit++; 360798481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 360898481e79SLen Brown } 360998481e79SLen Brown 361098481e79SLen Brown /* if all else fails, thell them to be root */ 361198481e79SLen Brown if (do_exit) 3612b2c95d90SJosh Triplett if (getuid() != 0) 3613d7899447SLen Brown warnx("... or simply run as root"); 361498481e79SLen Brown 361598481e79SLen Brown if (do_exit) 361698481e79SLen Brown exit(-6); 3617103a8feaSLen Brown } 3618103a8feaSLen Brown 3619d7899447SLen Brown /* 3620d7899447SLen Brown * NHM adds support for additional MSRs: 3621d7899447SLen Brown * 3622d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3623d7899447SLen Brown * 3624ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 36251df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3626d7899447SLen Brown * 3627cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3628cf4cbe53SLen Brown * 3629d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3630d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3631d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3632d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3633d7899447SLen Brown * 3634ee7e38e3SLen Brown * Side effect: 36351df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 363633148d67SLen Brown * sets has_misc_feature_control 3637d7899447SLen Brown */ 3638ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3639103a8feaSLen Brown { 3640ee7e38e3SLen Brown unsigned long long msr; 364121ed5574SLen Brown unsigned int base_ratio; 3642ee7e38e3SLen Brown int *pkg_cstate_limits; 3643ee7e38e3SLen Brown 3644103a8feaSLen Brown if (!genuine_intel) 3645103a8feaSLen Brown return 0; 3646103a8feaSLen Brown 3647103a8feaSLen Brown if (family != 6) 3648103a8feaSLen Brown return 0; 3649103a8feaSLen Brown 365021ed5574SLen Brown bclk = discover_bclk(family, model); 365121ed5574SLen Brown 3652103a8feaSLen Brown switch (model) { 3653869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3654869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3655ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3656ee7e38e3SLen Brown break; 3657869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3658869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3659869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3660869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3661ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 366233148d67SLen Brown has_misc_feature_control = 1; 3663ee7e38e3SLen Brown break; 3664c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 36655e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3666869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 366777e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3668c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 36695e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3670869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3671af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3672af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3673ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 367433148d67SLen Brown has_misc_feature_control = 1; 3675ee7e38e3SLen Brown break; 3676d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3677d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 367833148d67SLen Brown has_misc_feature_control = 1; 3679d8ebb442SLen Brown break; 368025368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 368125368d7cSChen Yu pkg_cstate_limits = icx_pkg_cstate_limits; 368225368d7cSChen Yu has_misc_feature_control = 1; 368325368d7cSChen Yu break; 3684f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3685cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 36865ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3687ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3688ee7e38e3SLen Brown break; 3689869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3690ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3691cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3692ee7e38e3SLen Brown break; 3693869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3694ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3695ee7e38e3SLen Brown break; 3696869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3697f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 36985ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3699f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 370020de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 3701445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3702e4085d54SLen Brown break; 3703103a8feaSLen Brown default: 3704103a8feaSLen Brown return 0; 3705103a8feaSLen Brown } 37061df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3707e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3708ee7e38e3SLen Brown 3709ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 371021ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 371121ed5574SLen Brown 371221ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 371321ed5574SLen Brown has_base_hz = 1; 3714ee7e38e3SLen Brown return 1; 3715103a8feaSLen Brown } 37161b439f01SLen Brown 37170f7887c4SLen Brown /* 3718495c7654SLen Brown * SLV client has support for unique MSRs: 37190f7887c4SLen Brown * 37200f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 37210f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 37220f7887c4SLen Brown */ 37230f7887c4SLen Brown 37240f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 37250f7887c4SLen Brown { 37260f7887c4SLen Brown if (!genuine_intel) 37270f7887c4SLen Brown return 0; 37280f7887c4SLen Brown 37290f7887c4SLen Brown switch (model) { 3730f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3731f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3732f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 37330f7887c4SLen Brown return 1; 37340f7887c4SLen Brown } 37350f7887c4SLen Brown return 0; 37360f7887c4SLen Brown } 37371b439f01SLen Brown 37387170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 37397170a374SLen Brown { 37407170a374SLen Brown 37417170a374SLen Brown if (!genuine_intel) 37427170a374SLen Brown return 0; 37437170a374SLen Brown 37447170a374SLen Brown switch (model) { 37455ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 37467170a374SLen Brown return 1; 37477170a374SLen Brown } 37487170a374SLen Brown return 0; 37497170a374SLen Brown } 37501b439f01SLen Brown 3751ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3752ade0ebacSLen Brown { 3753ade0ebacSLen Brown 3754ade0ebacSLen Brown if (!genuine_intel) 3755ade0ebacSLen Brown return 0; 3756ade0ebacSLen Brown 3757ade0ebacSLen Brown switch (model) { 3758ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3759ade0ebacSLen Brown return 1; 3760ade0ebacSLen Brown } 3761ade0ebacSLen Brown return 0; 3762ade0ebacSLen Brown } 37631b439f01SLen Brown 376434c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 376534c76197SLen Brown { 376634c76197SLen Brown 376734c76197SLen Brown if (!genuine_intel) 376834c76197SLen Brown return 0; 376934c76197SLen Brown 377034c76197SLen Brown switch (model) { 377134c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 377234c76197SLen Brown return 1; 377334c76197SLen Brown } 377434c76197SLen Brown return 0; 377534c76197SLen Brown } 377625368d7cSChen Yu 377725368d7cSChen Yu int is_icx(unsigned int family, unsigned int model) 377825368d7cSChen Yu { 377925368d7cSChen Yu 378025368d7cSChen Yu if (!genuine_intel) 378125368d7cSChen Yu return 0; 378225368d7cSChen Yu 378325368d7cSChen Yu switch (model) { 378425368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 378525368d7cSChen Yu return 1; 378625368d7cSChen Yu } 378725368d7cSChen Yu return 0; 378825368d7cSChen Yu } 378925368d7cSChen Yu 3790f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3791f6708400SChen Yu { 3792f6708400SChen Yu if (!genuine_intel) 3793f6708400SChen Yu return 0; 3794f6708400SChen Yu 3795f6708400SChen Yu switch (model) { 3796f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3797f6708400SChen Yu return 1; 3798f6708400SChen Yu } 3799f6708400SChen Yu return 0; 3800f6708400SChen Yu } 38011b439f01SLen Brown 380220de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model) 380320de0dabSAntti Laakso { 380420de0dabSAntti Laakso if (!genuine_intel) 380520de0dabSAntti Laakso return 0; 380620de0dabSAntti Laakso 380720de0dabSAntti Laakso switch (model) { 380820de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: 380920de0dabSAntti Laakso return 1; 381020de0dabSAntti Laakso } 381120de0dabSAntti Laakso return 0; 381220de0dabSAntti Laakso } 38130f7887c4SLen Brown 381431e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3815d7899447SLen Brown { 38160f7887c4SLen Brown if (has_slv_msrs(family, model)) 38170f7887c4SLen Brown return 0; 38180f7887c4SLen Brown 3819d7899447SLen Brown switch (model) { 3820d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3821869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3822869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3823d7899447SLen Brown return 0; 3824d7899447SLen Brown default: 3825d7899447SLen Brown return 1; 3826d7899447SLen Brown } 3827d7899447SLen Brown } 38281b439f01SLen Brown 38290f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 38300f7887c4SLen Brown { 38310f7887c4SLen Brown if (has_slv_msrs(family, model)) 38320f7887c4SLen Brown return 1; 38330f7887c4SLen Brown 38340f7887c4SLen Brown return 0; 38350f7887c4SLen Brown } 38361b439f01SLen Brown 38376574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 38386574a5d5SLen Brown { 38396574a5d5SLen Brown if (!genuine_intel) 38406574a5d5SLen Brown return 0; 38416574a5d5SLen Brown 38426574a5d5SLen Brown if (family != 6) 38436574a5d5SLen Brown return 0; 38446574a5d5SLen Brown 38456574a5d5SLen Brown switch (model) { 3846869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3847869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 38486574a5d5SLen Brown return 1; 38496574a5d5SLen Brown default: 38506574a5d5SLen Brown return 0; 38516574a5d5SLen Brown } 38526574a5d5SLen Brown } 38531b439f01SLen Brown 3854fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3855fcd17211SLen Brown { 3856fcd17211SLen Brown if (!genuine_intel) 3857fcd17211SLen Brown return 0; 3858fcd17211SLen Brown 3859fcd17211SLen Brown if (family != 6) 3860fcd17211SLen Brown return 0; 3861fcd17211SLen Brown 3862fcd17211SLen Brown switch (model) { 3863869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3864fcd17211SLen Brown return 1; 3865fcd17211SLen Brown default: 3866fcd17211SLen Brown return 0; 3867fcd17211SLen Brown } 3868fcd17211SLen Brown } 3869fcd17211SLen Brown 3870fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3871fb5d4327SDasaratharaman Chandramouli { 3872fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3873fb5d4327SDasaratharaman Chandramouli return 0; 3874fb5d4327SDasaratharaman Chandramouli 3875fb5d4327SDasaratharaman Chandramouli if (family != 6) 3876fb5d4327SDasaratharaman Chandramouli return 0; 3877fb5d4327SDasaratharaman Chandramouli 3878fb5d4327SDasaratharaman Chandramouli switch (model) { 3879869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3880fb5d4327SDasaratharaman Chandramouli return 1; 3881fb5d4327SDasaratharaman Chandramouli default: 3882fb5d4327SDasaratharaman Chandramouli return 0; 3883fb5d4327SDasaratharaman Chandramouli } 3884fb5d4327SDasaratharaman Chandramouli } 38851b439f01SLen Brown 388631e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 388731e07522SLen Brown { 388831e07522SLen Brown if (!genuine_intel) 388931e07522SLen Brown return 0; 389031e07522SLen Brown 389131e07522SLen Brown if (family != 6) 389231e07522SLen Brown return 0; 389331e07522SLen Brown 389431e07522SLen Brown switch (model) { 389531e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 389631e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 389725368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: 389831e07522SLen Brown return 1; 389931e07522SLen Brown default: 390031e07522SLen Brown return 0; 390131e07522SLen Brown } 390231e07522SLen Brown } 39031b439f01SLen Brown 39046fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 39056fb3143bSLen Brown { 39066fb3143bSLen Brown if (!genuine_intel) 39076fb3143bSLen Brown return 0; 39086fb3143bSLen Brown 39096fb3143bSLen Brown if (family != 6) 39106fb3143bSLen Brown return 0; 39116fb3143bSLen Brown 39126fb3143bSLen Brown switch (model) { 3913869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3914c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3915869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 391677e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 39175e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3918c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 39195e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3920869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3921af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3922af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3923869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 392425368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 39256fb3143bSLen Brown 3926869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 39276fb3143bSLen Brown return 1; 39286fb3143bSLen Brown default: 39296fb3143bSLen Brown return 0; 39306fb3143bSLen Brown } 39316fb3143bSLen Brown } 39326fb3143bSLen Brown 39330b9a0b9bSZhang Rui /* 39340b9a0b9bSZhang Rui * tcc_offset_bits: 39350b9a0b9bSZhang Rui * 0: Tcc Offset not supported (Default) 39360b9a0b9bSZhang Rui * 6: Bit 29:24 of MSR_PLATFORM_INFO 39370b9a0b9bSZhang Rui * 4: Bit 27:24 of MSR_PLATFORM_INFO 39380b9a0b9bSZhang Rui */ 39390b9a0b9bSZhang Rui void check_tcc_offset(int model) 39400b9a0b9bSZhang Rui { 39410b9a0b9bSZhang Rui unsigned long long msr; 39420b9a0b9bSZhang Rui 39430b9a0b9bSZhang Rui if (!genuine_intel) 39440b9a0b9bSZhang Rui return; 39450b9a0b9bSZhang Rui 39460b9a0b9bSZhang Rui switch (model) { 39470b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE_L: 39480b9a0b9bSZhang Rui case INTEL_FAM6_SKYLAKE: 39490b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE_L: 39500b9a0b9bSZhang Rui case INTEL_FAM6_KABYLAKE: 39510b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE_L: 39520b9a0b9bSZhang Rui case INTEL_FAM6_ICELAKE: 39530b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE_L: 39540b9a0b9bSZhang Rui case INTEL_FAM6_TIGERLAKE: 39550b9a0b9bSZhang Rui case INTEL_FAM6_COMETLAKE: 39560b9a0b9bSZhang Rui if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) { 39570b9a0b9bSZhang Rui msr = (msr >> 30) & 1; 39580b9a0b9bSZhang Rui if (msr) 39590b9a0b9bSZhang Rui tcc_offset_bits = 6; 39600b9a0b9bSZhang Rui } 39610b9a0b9bSZhang Rui return; 39620b9a0b9bSZhang Rui default: 39630b9a0b9bSZhang Rui return; 39640b9a0b9bSZhang Rui } 39650b9a0b9bSZhang Rui } 39660b9a0b9bSZhang Rui 39671b439f01SLen Brown static void remove_underbar(char *s) 3968fecb3bc8SDavid Arcari { 3969fecb3bc8SDavid Arcari char *to = s; 3970fecb3bc8SDavid Arcari 3971fecb3bc8SDavid Arcari while (*s) { 3972fecb3bc8SDavid Arcari if (*s != '_') 3973fecb3bc8SDavid Arcari *to++ = *s; 3974fecb3bc8SDavid Arcari s++; 3975fecb3bc8SDavid Arcari } 3976fecb3bc8SDavid Arcari 3977fecb3bc8SDavid Arcari *to = 0; 3978fecb3bc8SDavid Arcari } 3979fecb3bc8SDavid Arcari 39801b439f01SLen Brown static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3981fcd17211SLen Brown { 3982fcd17211SLen Brown if (!do_nhm_platform_info) 3983fcd17211SLen Brown return; 3984fcd17211SLen Brown 3985fcd17211SLen Brown dump_nhm_platform_info(); 3986fcd17211SLen Brown 3987fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3988fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3989fcd17211SLen Brown 3990fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3991fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3992fcd17211SLen Brown 399331e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 399431e07522SLen Brown dump_turbo_ratio_limits(family, model); 3995fcd17211SLen Brown 39960f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 39970f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 39980f7887c4SLen Brown 3999fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 4000fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 4001fb5d4327SDasaratharaman Chandramouli 40026fb3143bSLen Brown if (has_config_tdp(family, model)) 40036fb3143bSLen Brown dump_config_tdp(); 40046fb3143bSLen Brown 4005fcd17211SLen Brown dump_nhm_cst_cfg(); 4006fcd17211SLen Brown } 4007fcd17211SLen Brown 4008abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 4009abdcbdb2SLen Brown { 4010abdcbdb2SLen Brown FILE *input; 4011abdcbdb2SLen Brown char cpuidle_buf[64]; 4012abdcbdb2SLen Brown 4013abdcbdb2SLen Brown input = fopen(path, "r"); 4014abdcbdb2SLen Brown if (input == NULL) { 4015abdcbdb2SLen Brown if (debug) 4016abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 4017abdcbdb2SLen Brown return; 4018abdcbdb2SLen Brown } 4019abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 4020abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 4021abdcbdb2SLen Brown fclose(input); 4022abdcbdb2SLen Brown 4023abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 4024abdcbdb2SLen Brown } 40251b439f01SLen Brown 40261b439f01SLen Brown static void dump_sysfs_cstate_config(void) 402741618e63SLen Brown { 402841618e63SLen Brown char path[64]; 402941618e63SLen Brown char name_buf[16]; 403041618e63SLen Brown char desc[64]; 403141618e63SLen Brown FILE *input; 403241618e63SLen Brown int state; 403341618e63SLen Brown char *sp; 403441618e63SLen Brown 4035abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 4036abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 4037abdcbdb2SLen Brown return; 4038abdcbdb2SLen Brown } 4039abdcbdb2SLen Brown 4040abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 4041abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 4042abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 4043abdcbdb2SLen Brown 404441618e63SLen Brown for (state = 0; state < 10; ++state) { 404541618e63SLen Brown 40461b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 404741618e63SLen Brown input = fopen(path, "r"); 404841618e63SLen Brown if (input == NULL) 404941618e63SLen Brown continue; 40508173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 40518173c336SBen Hutchings err(1, "%s: failed to read file", path); 405241618e63SLen Brown 405341618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 405441618e63SLen Brown sp = strchr(name_buf, '-'); 405541618e63SLen Brown if (!sp) 405641618e63SLen Brown sp = strchrnul(name_buf, '\n'); 405741618e63SLen Brown *sp = '\0'; 405841618e63SLen Brown fclose(input); 405941618e63SLen Brown 4060fecb3bc8SDavid Arcari remove_underbar(name_buf); 4061fecb3bc8SDavid Arcari 40621b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state); 406341618e63SLen Brown input = fopen(path, "r"); 406441618e63SLen Brown if (input == NULL) 406541618e63SLen Brown continue; 40668173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 40678173c336SBen Hutchings err(1, "%s: failed to read file", path); 406841618e63SLen Brown 406941618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 407041618e63SLen Brown fclose(input); 407141618e63SLen Brown } 407241618e63SLen Brown } 40731b439f01SLen Brown 40741b439f01SLen Brown static void dump_sysfs_pstate_config(void) 40757293fccdSLen Brown { 40767293fccdSLen Brown char path[64]; 40777293fccdSLen Brown char driver_buf[64]; 40787293fccdSLen Brown char governor_buf[64]; 40797293fccdSLen Brown FILE *input; 40807293fccdSLen Brown int turbo; 40817293fccdSLen Brown 40821b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu); 40837293fccdSLen Brown input = fopen(path, "r"); 40847293fccdSLen Brown if (input == NULL) { 40850a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40867293fccdSLen Brown return; 40877293fccdSLen Brown } 40888173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 40898173c336SBen Hutchings err(1, "%s: failed to read file", path); 40907293fccdSLen Brown fclose(input); 40917293fccdSLen Brown 40921b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu); 40937293fccdSLen Brown input = fopen(path, "r"); 40947293fccdSLen Brown if (input == NULL) { 40950a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 40967293fccdSLen Brown return; 40977293fccdSLen Brown } 40988173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 40998173c336SBen Hutchings err(1, "%s: failed to read file", path); 41007293fccdSLen Brown fclose(input); 41017293fccdSLen Brown 41027293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 41037293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 41047293fccdSLen Brown 41057293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 41067293fccdSLen Brown input = fopen(path, "r"); 41077293fccdSLen Brown if (input != NULL) { 41088173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 41098173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 41107293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 41117293fccdSLen Brown fclose(input); 41127293fccdSLen Brown } 41137293fccdSLen Brown 41147293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 41157293fccdSLen Brown input = fopen(path, "r"); 41167293fccdSLen Brown if (input != NULL) { 41178173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 41188173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 41197293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 41207293fccdSLen Brown fclose(input); 41217293fccdSLen Brown } 41227293fccdSLen Brown } 412341618e63SLen Brown 4124889facbeSLen Brown /* 4125889facbeSLen Brown * print_epb() 4126889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 4127889facbeSLen Brown */ 4128889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4129889facbeSLen Brown { 4130889facbeSLen Brown char *epb_string; 41316d6501d9SBorislav Petkov int cpu, epb; 4132889facbeSLen Brown 4133889facbeSLen Brown if (!has_epb) 4134889facbeSLen Brown return 0; 4135889facbeSLen Brown 4136889facbeSLen Brown cpu = t->cpu_id; 4137889facbeSLen Brown 4138889facbeSLen Brown /* EPB is per-package */ 4139889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4140889facbeSLen Brown return 0; 4141889facbeSLen Brown 4142889facbeSLen Brown if (cpu_migrate(cpu)) { 41433d7772eaSLen Brown fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu); 4144889facbeSLen Brown return -1; 4145889facbeSLen Brown } 4146889facbeSLen Brown 41476d6501d9SBorislav Petkov epb = get_epb(cpu); 41486d6501d9SBorislav Petkov if (epb < 0) 4149889facbeSLen Brown return 0; 4150889facbeSLen Brown 41516d6501d9SBorislav Petkov switch (epb) { 4152889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 4153889facbeSLen Brown epb_string = "performance"; 4154889facbeSLen Brown break; 4155889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 4156889facbeSLen Brown epb_string = "balanced"; 4157889facbeSLen Brown break; 4158889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 4159889facbeSLen Brown epb_string = "powersave"; 4160889facbeSLen Brown break; 4161889facbeSLen Brown default: 4162889facbeSLen Brown epb_string = "custom"; 4163889facbeSLen Brown break; 4164889facbeSLen Brown } 41656d6501d9SBorislav Petkov fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string); 4166889facbeSLen Brown 4167889facbeSLen Brown return 0; 4168889facbeSLen Brown } 41691b439f01SLen Brown 41707f5c258eSLen Brown /* 41717f5c258eSLen Brown * print_hwp() 41727f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 41737f5c258eSLen Brown */ 41747f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 41757f5c258eSLen Brown { 41767f5c258eSLen Brown unsigned long long msr; 41777f5c258eSLen Brown int cpu; 41787f5c258eSLen Brown 41797f5c258eSLen Brown if (!has_hwp) 41807f5c258eSLen Brown return 0; 41817f5c258eSLen Brown 41827f5c258eSLen Brown cpu = t->cpu_id; 41837f5c258eSLen Brown 41847f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 41857f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 41867f5c258eSLen Brown return 0; 41877f5c258eSLen Brown 41887f5c258eSLen Brown if (cpu_migrate(cpu)) { 41893d7772eaSLen Brown fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu); 41907f5c258eSLen Brown return -1; 41917f5c258eSLen Brown } 41927f5c258eSLen Brown 41937f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 41947f5c258eSLen Brown return 0; 41957f5c258eSLen Brown 41961b439f01SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 41977f5c258eSLen Brown 41987f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 41997f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 42007f5c258eSLen Brown return 0; 42017f5c258eSLen Brown 42027f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 42037f5c258eSLen Brown return 0; 42047f5c258eSLen Brown 4205b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 42066dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 42077f5c258eSLen Brown cpu, msr, 42087f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 42097f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 42101b439f01SLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr)); 42117f5c258eSLen Brown 42127f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 42137f5c258eSLen Brown return 0; 42147f5c258eSLen Brown 4215b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 42166dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 42177f5c258eSLen Brown cpu, msr, 42187f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 42197f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 42207f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 42217f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 42221b439f01SLen Brown (unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1)); 42237f5c258eSLen Brown 42247f5c258eSLen Brown if (has_hwp_pkg) { 42257f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 42267f5c258eSLen Brown return 0; 42277f5c258eSLen Brown 4228b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 42296dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 42307f5c258eSLen Brown cpu, msr, 42317f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 42327f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 42337f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 42341b439f01SLen Brown (unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3)); 42357f5c258eSLen Brown } 42367f5c258eSLen Brown if (has_hwp_notify) { 42377f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 42387f5c258eSLen Brown return 0; 42397f5c258eSLen Brown 4240b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 42417f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 42421b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis"); 42437f5c258eSLen Brown } 42447f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 42457f5c258eSLen Brown return 0; 42467f5c258eSLen Brown 4247b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 42487f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 42491b439f01SLen Brown cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-"); 4250889facbeSLen Brown 4251889facbeSLen Brown return 0; 4252889facbeSLen Brown } 4253889facbeSLen Brown 42543a9a941dSLen Brown /* 42553a9a941dSLen Brown * print_perf_limit() 42563a9a941dSLen Brown */ 42573a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 42583a9a941dSLen Brown { 42593a9a941dSLen Brown unsigned long long msr; 42603a9a941dSLen Brown int cpu; 42613a9a941dSLen Brown 42623a9a941dSLen Brown cpu = t->cpu_id; 42633a9a941dSLen Brown 42643a9a941dSLen Brown /* per-package */ 42653a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 42663a9a941dSLen Brown return 0; 42673a9a941dSLen Brown 42683a9a941dSLen Brown if (cpu_migrate(cpu)) { 42693d7772eaSLen Brown fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu); 42703a9a941dSLen Brown return -1; 42713a9a941dSLen Brown } 42723a9a941dSLen Brown 42733a9a941dSLen Brown if (do_core_perf_limit_reasons) { 42743a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4275b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4276b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4277e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 42783a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4279e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4280e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4281e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4282e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4283e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4284e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4285e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4286e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4287e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4288e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 42891b439f01SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : ""); 4290b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4291e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 42923a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4293e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4294e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4295e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4296e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4297e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4298e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4299e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4300e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4301e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4302e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 43031b439f01SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : ""); 43043a9a941dSLen Brown 43053a9a941dSLen Brown } 43063a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 43073a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4308b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4309b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 43103a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 43113a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 43123a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 43133a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 43143a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 43153a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 43161b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4317b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 43183a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 43193a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 43203a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 43213a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 43223a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 43233a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 43241b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 43253a9a941dSLen Brown } 43263a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 43273a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4328b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4329b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 43303a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 43313a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 43323a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 43333a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 43341b439f01SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4335b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 43363a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 43373a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 43383a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 43393a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 43401b439f01SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : ""); 43413a9a941dSLen Brown } 43423a9a941dSLen Brown return 0; 43433a9a941dSLen Brown } 43443a9a941dSLen Brown 4345889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4346889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4347889facbeSLen Brown 43489392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4349144b44b1SLen Brown { 4350144b44b1SLen Brown unsigned long long msr; 4351144b44b1SLen Brown 4352144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 43537ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4354144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4355144b44b1SLen Brown 4356144b44b1SLen Brown switch (model) { 4357f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 43585ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4359144b44b1SLen Brown return 30.0; 4360144b44b1SLen Brown default: 4361144b44b1SLen Brown return 135.0; 4362144b44b1SLen Brown } 4363144b44b1SLen Brown } 4364144b44b1SLen Brown 43659392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 43669392bd98SCalvin Walton { 436733eb8225SKim Phillips /* This is the max stock TDP of HEDT/Server Fam17h+ chips */ 436833eb8225SKim Phillips return 280.0; 43699392bd98SCalvin Walton } 43709392bd98SCalvin Walton 437140ee8e3bSAndrey Semin /* 437240ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 437340ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 437440ee8e3bSAndrey Semin */ 43751b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units) 437640ee8e3bSAndrey Semin { 437740ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 437840ee8e3bSAndrey Semin 437940ee8e3bSAndrey Semin switch (model) { 4380869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4381869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4382abdc75abSZhang Rui case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4383869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 438440ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 438540ee8e3bSAndrey Semin default: 438640ee8e3bSAndrey Semin return (rapl_energy_units); 438740ee8e3bSAndrey Semin } 438840ee8e3bSAndrey Semin } 438940ee8e3bSAndrey Semin 43909392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4391889facbeSLen Brown { 4392889facbeSLen Brown unsigned long long msr; 4393144b44b1SLen Brown unsigned int time_unit; 4394889facbeSLen Brown double tdp; 4395889facbeSLen Brown 4396889facbeSLen Brown if (family != 6) 4397889facbeSLen Brown return; 4398889facbeSLen Brown 4399889facbeSLen Brown switch (model) { 4400869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4401869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4402c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 440377e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 44045e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4405c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 44065e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4407144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4408812db3f7SLen Brown if (rapl_joules) { 4409812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4410812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4411812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4412812db3f7SLen Brown } else { 4413812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4414812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4415812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4416812db3f7SLen Brown } 4417889facbeSLen Brown break; 4418869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4419f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4420e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4421812db3f7SLen Brown if (rapl_joules) 4422812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4423812db3f7SLen Brown else 4424812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4425e4085d54SLen Brown break; 4426f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 44271b439f01SLen Brown do_rapl = 44281b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 44291b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4430f6708400SChen Yu if (rapl_joules) { 4431f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4432f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4433f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4434f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4435f6708400SChen Yu } else { 4436f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4437f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4438f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4439f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4440f6708400SChen Yu } 4441f6708400SChen Yu break; 444220de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 444320de0dabSAntti Laakso do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 444420de0dabSAntti Laakso BIC_PRESENT(BIC_PKG__); 444520de0dabSAntti Laakso if (rapl_joules) 444620de0dabSAntti Laakso BIC_PRESENT(BIC_Pkg_J); 444720de0dabSAntti Laakso else 444820de0dabSAntti Laakso BIC_PRESENT(BIC_PkgWatt); 444920de0dabSAntti Laakso break; 4450af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4451af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 44521b439f01SLen Brown do_rapl = 44531b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS 44541b439f01SLen Brown | RAPL_GFX | RAPL_PKG_POWER_INFO; 4455812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4456812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4457812db3f7SLen Brown if (rapl_joules) { 4458812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4459812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4460812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 446181824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4462812db3f7SLen Brown } else { 4463812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4464812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4465812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 446681824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4467812db3f7SLen Brown } 44680b2bb692SLen Brown break; 4469869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4470869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4471869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 447225368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4473869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 44741b439f01SLen Brown do_rapl = 44751b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 44761b439f01SLen Brown RAPL_PKG_POWER_INFO; 4477812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4478812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4479812db3f7SLen Brown if (rapl_joules) { 4480812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4481812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4482812db3f7SLen Brown } else { 4483812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4484812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4485812db3f7SLen Brown } 4486e6f9bb3cSLen Brown break; 4487869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4488869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 44891b439f01SLen Brown do_rapl = 44901b439f01SLen Brown RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | 44911b439f01SLen Brown RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4492812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4493812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4494812db3f7SLen Brown if (rapl_joules) { 4495812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4496812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4497812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4498812db3f7SLen Brown } else { 4499812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4500812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4501812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4502812db3f7SLen Brown } 4503144b44b1SLen Brown break; 4504f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 45055ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4506144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4507812db3f7SLen Brown if (rapl_joules) { 4508812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4509812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4510812db3f7SLen Brown } else { 4511812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4512812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4513812db3f7SLen Brown } 4514889facbeSLen Brown break; 45155ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 45161b439f01SLen Brown do_rapl = 45171b439f01SLen Brown RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 45181b439f01SLen Brown RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4519812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4520812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4521812db3f7SLen Brown if (rapl_joules) { 4522812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4523812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4524812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4525812db3f7SLen Brown } else { 4526812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4527812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4528812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4529812db3f7SLen Brown } 45300f644909SJacob Pan break; 4531889facbeSLen Brown default: 4532889facbeSLen Brown return; 4533889facbeSLen Brown } 4534889facbeSLen Brown 4535889facbeSLen Brown /* units on package 0, verify later other packages match */ 45367ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4537889facbeSLen Brown return; 4538889facbeSLen Brown 4539889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4540f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4541144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4542144b44b1SLen Brown else 4543889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4544889facbeSLen Brown 454540ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 454640ee8e3bSAndrey Semin 4547144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4548144b44b1SLen Brown if (time_unit == 0) 4549144b44b1SLen Brown time_unit = 0xA; 4550889facbeSLen Brown 4551144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4552144b44b1SLen Brown 45539392bd98SCalvin Walton tdp = get_tdp_intel(model); 4554889facbeSLen Brown 4555889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 455696e47158SLen Brown if (!quiet) 4557b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 45589392bd98SCalvin Walton } 4559889facbeSLen Brown 45609392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 45619392bd98SCalvin Walton { 45629392bd98SCalvin Walton unsigned long long msr; 45639392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 45649392bd98SCalvin Walton unsigned int has_rapl = 0; 45659392bd98SCalvin Walton double tdp; 45669392bd98SCalvin Walton 45679392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 45689392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 456933eb8225SKim Phillips /* RAPL (Fam 17h+) */ 45709392bd98SCalvin Walton has_rapl = edx & (1 << 14); 45719392bd98SCalvin Walton } 45729392bd98SCalvin Walton 457333eb8225SKim Phillips if (!has_rapl || family < 0x17) 4574889facbeSLen Brown return; 45759392bd98SCalvin Walton 45769392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 45773316f99aSCalvin Walton if (rapl_joules) { 45783316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 45799392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 45803316f99aSCalvin Walton } else { 45813316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 45829392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 45833316f99aSCalvin Walton } 45849392bd98SCalvin Walton 45859392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 45869392bd98SCalvin Walton return; 45879392bd98SCalvin Walton 45889392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 45899392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 45909392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 45919392bd98SCalvin Walton 45929cfa8e04SPu Wen tdp = get_tdp_amd(family); 45939392bd98SCalvin Walton 45949392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 45959392bd98SCalvin Walton if (!quiet) 45969392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 45979392bd98SCalvin Walton } 45989392bd98SCalvin Walton 45999392bd98SCalvin Walton /* 46009392bd98SCalvin Walton * rapl_probe() 46019392bd98SCalvin Walton * 46029392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 46039392bd98SCalvin Walton */ 46049392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 46059392bd98SCalvin Walton { 46069392bd98SCalvin Walton if (genuine_intel) 46079392bd98SCalvin Walton rapl_probe_intel(family, model); 4608c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 46099392bd98SCalvin Walton rapl_probe_amd(family, model); 4610889facbeSLen Brown } 4611889facbeSLen Brown 46121b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 46133a9a941dSLen Brown { 46143a9a941dSLen Brown if (!genuine_intel) 46153a9a941dSLen Brown return; 46163a9a941dSLen Brown 46173a9a941dSLen Brown if (family != 6) 46183a9a941dSLen Brown return; 46193a9a941dSLen Brown 46203a9a941dSLen Brown switch (model) { 4621c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 462277e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 46235e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 46243a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4625869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 46263a9a941dSLen Brown do_core_perf_limit_reasons = 1; 46273a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 46283a9a941dSLen Brown default: 46293a9a941dSLen Brown return; 46303a9a941dSLen Brown } 46313a9a941dSLen Brown } 46323a9a941dSLen Brown 4633ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4634ac980e13SArtem Bityutskiy { 46351b439f01SLen Brown if (is_skx(family, model) || is_bdx(family, model) || is_icx(family, model)) 4636ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4637ac980e13SArtem Bityutskiy } 4638ac980e13SArtem Bityutskiy 4639aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model) 4640aeb01e6dSChen Yu { 4641aeb01e6dSChen Yu if (is_icx(family, model)) 4642aeb01e6dSChen Yu dis_cstate_prewake = 1; 4643aeb01e6dSChen Yu } 4644aeb01e6dSChen Yu 4645889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4646889facbeSLen Brown { 4647889facbeSLen Brown unsigned long long msr; 4648f4896fa5SLen Brown unsigned int dts, dts2; 4649889facbeSLen Brown int cpu; 4650889facbeSLen Brown 4651889facbeSLen Brown if (!(do_dts || do_ptm)) 4652889facbeSLen Brown return 0; 4653889facbeSLen Brown 4654889facbeSLen Brown cpu = t->cpu_id; 4655889facbeSLen Brown 4656889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4657889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4658889facbeSLen Brown return 0; 4659889facbeSLen Brown 4660889facbeSLen Brown if (cpu_migrate(cpu)) { 46613d7772eaSLen Brown fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu); 4662889facbeSLen Brown return -1; 4663889facbeSLen Brown } 4664889facbeSLen Brown 4665889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4666889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4667889facbeSLen Brown return 0; 4668889facbeSLen Brown 4669889facbeSLen Brown dts = (msr >> 16) & 0x7F; 46701b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts); 4671889facbeSLen Brown 4672889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4673889facbeSLen Brown return 0; 4674889facbeSLen Brown 4675889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4676889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4677b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 467855279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4679889facbeSLen Brown } 4680889facbeSLen Brown 4681f4896fa5SLen Brown if (do_dts && debug) { 4682889facbeSLen Brown unsigned int resolution; 4683889facbeSLen Brown 4684889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4685889facbeSLen Brown return 0; 4686889facbeSLen Brown 4687889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4688889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4689b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 469055279aefSZhang Rui cpu, msr, tj_max - dts, resolution); 4691889facbeSLen Brown 4692889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4693889facbeSLen Brown return 0; 4694889facbeSLen Brown 4695889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4696889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4697b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 469855279aefSZhang Rui cpu, msr, tj_max - dts, tj_max - dts2); 4699889facbeSLen Brown } 4700889facbeSLen Brown 4701889facbeSLen Brown return 0; 4702889facbeSLen Brown } 4703889facbeSLen Brown 4704889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4705889facbeSLen Brown { 4706b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4707889facbeSLen Brown cpu, label, 4708889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4709889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4710889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4711889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4712889facbeSLen Brown 4713889facbeSLen Brown return; 4714889facbeSLen Brown } 4715889facbeSLen Brown 4716889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4717889facbeSLen Brown { 4718889facbeSLen Brown unsigned long long msr; 47199392bd98SCalvin Walton const char *msr_name; 4720889facbeSLen Brown int cpu; 4721889facbeSLen Brown 4722889facbeSLen Brown if (!do_rapl) 4723889facbeSLen Brown return 0; 4724889facbeSLen Brown 4725889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4726889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4727889facbeSLen Brown return 0; 4728889facbeSLen Brown 4729889facbeSLen Brown cpu = t->cpu_id; 4730889facbeSLen Brown if (cpu_migrate(cpu)) { 47313d7772eaSLen Brown fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu); 4732889facbeSLen Brown return -1; 4733889facbeSLen Brown } 4734889facbeSLen Brown 47359392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 47369392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 47379392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 47389392bd98SCalvin Walton return -1; 47399392bd98SCalvin Walton } else { 47409392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4741889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4742889facbeSLen Brown return -1; 47439392bd98SCalvin Walton } 4744889facbeSLen Brown 47459392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4746144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 474796e47158SLen Brown 4748144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4749144b44b1SLen Brown 4750889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4751889facbeSLen Brown return -5; 4752889facbeSLen Brown 4753b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4754889facbeSLen Brown cpu, msr, 4755889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4756889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4757889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4758889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4759889facbeSLen Brown 4760144b44b1SLen Brown } 4761144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4762144b44b1SLen Brown 4763889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4764889facbeSLen Brown return -9; 4765889facbeSLen Brown 4766b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4767889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4768889facbeSLen Brown 4769889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4770b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4771889facbeSLen Brown cpu, 4772889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4773889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4774889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4775889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4776*f52ba931SSumeet Pawnikar 4777*f52ba931SSumeet Pawnikar if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr)) 4778*f52ba931SSumeet Pawnikar return -9; 4779*f52ba931SSumeet Pawnikar 4780*f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr); 4781*f52ba931SSumeet Pawnikar fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n", 4782*f52ba931SSumeet Pawnikar cpu, 4783*f52ba931SSumeet Pawnikar ((msr >> 0) & 0x1FFF) * rapl_power_units, 4784*f52ba931SSumeet Pawnikar (msr >> 31) & 1 ? "" : "UN"); 4785889facbeSLen Brown } 4786889facbeSLen Brown 47870b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4788889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4789889facbeSLen Brown return -6; 4790889facbeSLen Brown 4791b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4792889facbeSLen Brown cpu, msr, 4793889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4794889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4795889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4796889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 47970b2bb692SLen Brown } 47980b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4799889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4800889facbeSLen Brown return -9; 4801b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4802889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4803889facbeSLen Brown 4804889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4805889facbeSLen Brown } 4806144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4807889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4808889facbeSLen Brown return -7; 4809889facbeSLen Brown 4810b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4811144b44b1SLen Brown } 48129148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4813889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4814889facbeSLen Brown return -9; 4815b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4816889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4817889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4818889facbeSLen Brown } 4819889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4820889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4821889facbeSLen Brown return -8; 4822889facbeSLen Brown 4823b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4824889facbeSLen Brown 4825889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4826889facbeSLen Brown return -9; 4827b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4828889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4829889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4830889facbeSLen Brown } 4831889facbeSLen Brown return 0; 4832889facbeSLen Brown } 4833889facbeSLen Brown 4834d7899447SLen Brown /* 4835d7899447SLen Brown * SNB adds support for additional MSRs: 4836d7899447SLen Brown * 4837d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4838d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4839d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4840d7899447SLen Brown */ 4841103a8feaSLen Brown 4842d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4843103a8feaSLen Brown { 4844103a8feaSLen Brown if (!genuine_intel) 4845103a8feaSLen Brown return 0; 4846103a8feaSLen Brown 4847103a8feaSLen Brown switch (model) { 4848869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4849869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4850869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4851869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4852c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4853869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 485477e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 48555e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4856c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 48575e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4858869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4859af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4860af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4861869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 486225368d7cSChen Yu case INTEL_FAM6_ICELAKE_X: /* ICX */ 4863869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4864f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 48655ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4866f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 486720de0dabSAntti Laakso case INTEL_FAM6_ATOM_TREMONT_D: /* JVL */ 4868103a8feaSLen Brown return 1; 4869103a8feaSLen Brown } 4870103a8feaSLen Brown return 0; 4871103a8feaSLen Brown } 4872103a8feaSLen Brown 4873d7899447SLen Brown /* 4874570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4875d7899447SLen Brown * 4876d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4877d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4878d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 48795a63426eSLen Brown * 48805a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 48815a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 48825a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 48835a63426eSLen Brown * 4884d7899447SLen Brown */ 4885570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4886ca58710fSKristen Carlson Accardi { 4887ca58710fSKristen Carlson Accardi if (!genuine_intel) 4888ca58710fSKristen Carlson Accardi return 0; 4889ca58710fSKristen Carlson Accardi 4890ca58710fSKristen Carlson Accardi switch (model) { 489177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4892c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4893af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4894af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4895869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4896f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4897f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4898ca58710fSKristen Carlson Accardi return 1; 4899ca58710fSKristen Carlson Accardi } 4900ca58710fSKristen Carlson Accardi return 0; 4901ca58710fSKristen Carlson Accardi } 4902ca58710fSKristen Carlson Accardi 49030b2bb692SLen Brown /* 49040b2bb692SLen Brown * SKL adds support for additional MSRS: 49050b2bb692SLen Brown * 49060b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 49070b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 49080b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 49090b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 49100b2bb692SLen Brown */ 49110b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 49120b2bb692SLen Brown { 49130b2bb692SLen Brown if (!genuine_intel) 49140b2bb692SLen Brown return 0; 49150b2bb692SLen Brown 49160b2bb692SLen Brown switch (model) { 4917af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4918af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 49190b2bb692SLen Brown return 1; 49200b2bb692SLen Brown } 49210b2bb692SLen Brown return 0; 49220b2bb692SLen Brown } 49230b2bb692SLen Brown 4924144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4925144b44b1SLen Brown { 4926144b44b1SLen Brown if (!genuine_intel) 4927144b44b1SLen Brown return 0; 4928144b44b1SLen Brown switch (model) { 4929f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 49305ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4931144b44b1SLen Brown return 1; 4932144b44b1SLen Brown } 4933144b44b1SLen Brown return 0; 4934144b44b1SLen Brown } 4935144b44b1SLen Brown 4936fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4937fb5d4327SDasaratharaman Chandramouli { 4938fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4939fb5d4327SDasaratharaman Chandramouli return 0; 4940fb5d4327SDasaratharaman Chandramouli switch (model) { 4941869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4942fb5d4327SDasaratharaman Chandramouli return 1; 4943fb5d4327SDasaratharaman Chandramouli } 4944fb5d4327SDasaratharaman Chandramouli return 0; 4945fb5d4327SDasaratharaman Chandramouli } 4946fb5d4327SDasaratharaman Chandramouli 4947997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4948997e5395SSrinivas Pandruvada { 4949997e5395SSrinivas Pandruvada if (!genuine_intel) 4950997e5395SSrinivas Pandruvada return 0; 4951997e5395SSrinivas Pandruvada 4952997e5395SSrinivas Pandruvada switch (model) { 4953af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4954997e5395SSrinivas Pandruvada return 1; 4955997e5395SSrinivas Pandruvada } 4956997e5395SSrinivas Pandruvada 4957997e5395SSrinivas Pandruvada return 0; 4958997e5395SSrinivas Pandruvada } 4959997e5395SSrinivas Pandruvada 4960b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4961b2b34dfeSHubert Chrzaniuk { 4962b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4963b2b34dfeSHubert Chrzaniuk return 1024; 4964b2b34dfeSHubert Chrzaniuk return 1; 4965b2b34dfeSHubert Chrzaniuk } 4966b2b34dfeSHubert Chrzaniuk 4967144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4968144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 }; 4969144b44b1SLen Brown 4970144b44b1SLen Brown double slm_bclk(void) 4971144b44b1SLen Brown { 4972144b44b1SLen Brown unsigned long long msr = 3; 4973144b44b1SLen Brown unsigned int i; 4974144b44b1SLen Brown double freq; 4975144b44b1SLen Brown 49767ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4977b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4978144b44b1SLen Brown 4979144b44b1SLen Brown i = msr & 0xf; 4980144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4981b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 49820a91e551SColin Ian King i = 3; 4983144b44b1SLen Brown } 4984144b44b1SLen Brown freq = slm_freq_table[i]; 4985144b44b1SLen Brown 498696e47158SLen Brown if (!quiet) 4987b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4988144b44b1SLen Brown 4989144b44b1SLen Brown return freq; 4990144b44b1SLen Brown } 4991144b44b1SLen Brown 4992103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4993103a8feaSLen Brown { 4994121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4995103a8feaSLen Brown return 100.00; 4996144b44b1SLen Brown else if (is_slm(family, model)) 4997144b44b1SLen Brown return slm_bclk(); 4998103a8feaSLen Brown else 4999103a8feaSLen Brown return 133.33; 5000103a8feaSLen Brown } 5001103a8feaSLen Brown 50027ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p) 50037ab5ff49SZhang Rui { 50047ab5ff49SZhang Rui unsigned int eax, ebx, ecx, edx; 50057ab5ff49SZhang Rui 50067ab5ff49SZhang Rui if (!genuine_intel) 50077ab5ff49SZhang Rui return 0; 50087ab5ff49SZhang Rui 50097ab5ff49SZhang Rui if (cpu_migrate(t->cpu_id)) { 50107ab5ff49SZhang Rui fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id); 50117ab5ff49SZhang Rui return -1; 50127ab5ff49SZhang Rui } 50137ab5ff49SZhang Rui 50147ab5ff49SZhang Rui if (max_level < 0x1a) 50157ab5ff49SZhang Rui return 0; 50167ab5ff49SZhang Rui 50177ab5ff49SZhang Rui __cpuid(0x1a, eax, ebx, ecx, edx); 50187ab5ff49SZhang Rui eax = (eax >> 24) & 0xFF; 50197ab5ff49SZhang Rui if (eax == 0x20) 50207ab5ff49SZhang Rui t->is_atom = true; 50217ab5ff49SZhang Rui return 0; 50227ab5ff49SZhang Rui } 50237ab5ff49SZhang Rui 5024889facbeSLen Brown /* 5025889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 5026889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 5027889facbeSLen Brown * This is usually equal to tjMax. 5028889facbeSLen Brown * 5029889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 5030889facbeSLen Brown * but also allow cmdline over-ride with -T. 5031889facbeSLen Brown * 5032889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 5033889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 5034889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 5035889facbeSLen Brown */ 50366ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 50376ff7cb37SLen Brown { 5038b2b94be7SLen Brown unsigned long long msr; 503955279aefSZhang Rui unsigned int tcc_default, tcc_offset; 5040b2b94be7SLen Brown int cpu; 5041b2b94be7SLen Brown 504255279aefSZhang Rui /* tj_max is used only for dts or ptm */ 5043889facbeSLen Brown if (!(do_dts || do_ptm)) 5044889facbeSLen Brown return 0; 5045889facbeSLen Brown 5046889facbeSLen Brown /* this is a per-package concept */ 5047889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 5048889facbeSLen Brown return 0; 5049889facbeSLen Brown 5050b2b94be7SLen Brown cpu = t->cpu_id; 5051b2b94be7SLen Brown if (cpu_migrate(cpu)) { 5052b2b94be7SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 5053b2b94be7SLen Brown return -1; 5054b2b94be7SLen Brown } 5055b2b94be7SLen Brown 505655279aefSZhang Rui if (tj_max_override != 0) { 505755279aefSZhang Rui tj_max = tj_max_override; 50581b439f01SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max); 5059889facbeSLen Brown return 0; 5060889facbeSLen Brown } 5061889facbeSLen Brown 5062b2b94be7SLen Brown /* Temperature Target MSR is Nehalem and newer only */ 5063b2b94be7SLen Brown if (!do_nhm_platform_info) 5064b2b94be7SLen Brown goto guess; 5065b2b94be7SLen Brown 5066b2b94be7SLen Brown if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 5067b2b94be7SLen Brown goto guess; 5068b2b94be7SLen Brown 506955279aefSZhang Rui tcc_default = (msr >> 16) & 0xFF; 5070b2b94be7SLen Brown 50710b9a0b9bSZhang Rui if (!quiet) { 50720b9a0b9bSZhang Rui switch (tcc_offset_bits) { 50730b9a0b9bSZhang Rui case 4: 50740b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0xF; 50750b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 507655279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 50770b9a0b9bSZhang Rui break; 50780b9a0b9bSZhang Rui case 6: 50790b9a0b9bSZhang Rui tcc_offset = (msr >> 24) & 0x3F; 50800b9a0b9bSZhang Rui fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n", 508155279aefSZhang Rui cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset); 50820b9a0b9bSZhang Rui break; 50830b9a0b9bSZhang Rui default: 50841b439f01SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default); 50850b9a0b9bSZhang Rui break; 50860b9a0b9bSZhang Rui } 50870b9a0b9bSZhang Rui } 5088b2b94be7SLen Brown 508955279aefSZhang Rui if (!tcc_default) 5090b2b94be7SLen Brown goto guess; 5091b2b94be7SLen Brown 509255279aefSZhang Rui tj_max = tcc_default; 5093b2b94be7SLen Brown 5094889facbeSLen Brown return 0; 5095889facbeSLen Brown 5096b2b94be7SLen Brown guess: 509755279aefSZhang Rui tj_max = TJMAX_DEFAULT; 50981b439f01SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max); 5099889facbeSLen Brown 5100889facbeSLen Brown return 0; 5101889facbeSLen Brown } 510269807a63SLen Brown 5103aa8d8cc7SLen Brown void decode_feature_control_msr(void) 5104aa8d8cc7SLen Brown { 5105aa8d8cc7SLen Brown unsigned long long msr; 5106aa8d8cc7SLen Brown 5107f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 5108aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 51091b439f01SLen Brown base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : ""); 5110aa8d8cc7SLen Brown } 5111aa8d8cc7SLen Brown 511269807a63SLen Brown void decode_misc_enable_msr(void) 511369807a63SLen Brown { 511469807a63SLen Brown unsigned long long msr; 511569807a63SLen Brown 5116f26b1519SLen Brown if (!genuine_intel) 5117f26b1519SLen Brown return; 5118f26b1519SLen Brown 511969807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 5120e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 512169807a63SLen Brown base_cpu, msr, 5122e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 5123e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 5124fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 5125e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 5126e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 512769807a63SLen Brown } 512869807a63SLen Brown 512933148d67SLen Brown void decode_misc_feature_control(void) 513033148d67SLen Brown { 513133148d67SLen Brown unsigned long long msr; 513233148d67SLen Brown 513333148d67SLen Brown if (!has_misc_feature_control) 513433148d67SLen Brown return; 513533148d67SLen Brown 513633148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 51371b439f01SLen Brown fprintf(outf, 51381b439f01SLen Brown "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 51391b439f01SLen Brown base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "", 51401b439f01SLen Brown msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : ""); 514133148d67SLen Brown } 51421b439f01SLen Brown 5143f0057310SLen Brown /* 5144f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 5145f0057310SLen Brown * 5146f0057310SLen Brown * Decode the bits according to the Nehalem documentation 5147f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 5148f0057310SLen Brown * bit[1] less so... 5149f0057310SLen Brown */ 5150f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 5151f0057310SLen Brown { 5152f0057310SLen Brown unsigned long long msr; 5153f0057310SLen Brown 5154f0057310SLen Brown if (!do_nhm_platform_info) 5155f0057310SLen Brown return; 5156f0057310SLen Brown 5157cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 5158cf4cbe53SLen Brown return; 5159cf4cbe53SLen Brown 5160f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 5161ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 5162f0057310SLen Brown base_cpu, msr, 51631b439f01SLen Brown msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS"); 5164f0057310SLen Brown } 51651b439f01SLen Brown 516671616c8eSLen Brown /* 516771616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 516871616c8eSLen Brown * 516971616c8eSLen Brown * This MSRs are present on Silvermont processors, 517071616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 517171616c8eSLen Brown */ 517271616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 517371616c8eSLen Brown { 517471616c8eSLen Brown unsigned long long msr; 517571616c8eSLen Brown 517671616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 517771616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 517871616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 517971616c8eSLen Brown 518071616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 518171616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 518271616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 518371616c8eSLen Brown } 51847f5c258eSLen Brown 5185f5a4c76aSLen Brown /* 5186f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 5187f5a4c76aSLen Brown */ 5188f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 5189f5a4c76aSLen Brown { 5190f5a4c76aSLen Brown 5191f5a4c76aSLen Brown switch (model) { 5192f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 5193f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 5194f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 5195f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 5196f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 5197f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 5198f5a4c76aSLen Brown 5199f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 5200f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 5201f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 5202f5a4c76aSLen Brown 5203f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 5204f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 5205f5a4c76aSLen Brown 5206f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 52075ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 5208f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 5209f5a4c76aSLen Brown 5210af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 5211c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 5212af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 5213c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 5214081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 5215081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 5216af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 5217937807d3SLen Brown 5218af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 5219d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 52204bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 52214bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 5222e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 5223e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 5224e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 52255683460bSChen Yu case INTEL_FAM6_ALDERLAKE_L: 5226af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 5227b62b3184SZhang Rui 5228d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 5229d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 5230d7814c30SChen Yu 52316c5c6560SChen Yu case INTEL_FAM6_ICELAKE_D: 5232e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 523325368d7cSChen Yu return INTEL_FAM6_ICELAKE_X; 5234f5a4c76aSLen Brown } 5235f5a4c76aSLen Brown return model; 5236f5a4c76aSLen Brown } 5237d76bb7a0SLen Brown 5238d76bb7a0SLen Brown void print_dev_latency(void) 5239d76bb7a0SLen Brown { 5240d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 5241d76bb7a0SLen Brown int fd; 5242d76bb7a0SLen Brown int value; 5243d76bb7a0SLen Brown int retval; 5244d76bb7a0SLen Brown 5245d76bb7a0SLen Brown fd = open(path, O_RDONLY); 5246d76bb7a0SLen Brown if (fd < 0) { 5247d76bb7a0SLen Brown warn("fopen %s\n", path); 5248d76bb7a0SLen Brown return; 5249d76bb7a0SLen Brown } 5250d76bb7a0SLen Brown 5251d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 5252d76bb7a0SLen Brown if (retval != sizeof(int)) { 5253d76bb7a0SLen Brown warn("read %s\n", path); 5254d76bb7a0SLen Brown close(fd); 5255d76bb7a0SLen Brown return; 5256d76bb7a0SLen Brown } 52571b439f01SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained"); 5258d76bb7a0SLen Brown 5259d76bb7a0SLen Brown close(fd); 5260d76bb7a0SLen Brown } 5261d76bb7a0SLen Brown 52622af4f9b8SLen Brown /* 52632af4f9b8SLen Brown * Linux-perf manages the the HW instructions-retired counter 52642af4f9b8SLen Brown * by enabling when requested, and hiding rollover 52652af4f9b8SLen Brown */ 52662af4f9b8SLen Brown void linux_perf_init(void) 52672af4f9b8SLen Brown { 52682af4f9b8SLen Brown if (!BIC_IS_ENABLED(BIC_IPC)) 52692af4f9b8SLen Brown return; 52702af4f9b8SLen Brown 52712af4f9b8SLen Brown if (access("/proc/sys/kernel/perf_event_paranoid", F_OK)) 52722af4f9b8SLen Brown return; 52732af4f9b8SLen Brown 52742af4f9b8SLen Brown fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 52752af4f9b8SLen Brown if (fd_instr_count_percpu == NULL) 52762af4f9b8SLen Brown err(-1, "calloc fd_instr_count_percpu"); 52772af4f9b8SLen Brown 52782af4f9b8SLen Brown BIC_PRESENT(BIC_IPC); 52792af4f9b8SLen Brown } 52802af4f9b8SLen Brown 5281fcd17211SLen Brown void process_cpuid() 5282103a8feaSLen Brown { 528334041551SLen Brown unsigned int eax, ebx, ecx, edx; 528434041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 5285b3a34e93SLen Brown unsigned int has_turbo; 5286ed0757b8SLen Brown unsigned long long ucode_patch = 0; 5287103a8feaSLen Brown 5288103a8feaSLen Brown eax = ebx = ecx = edx = 0; 5289103a8feaSLen Brown 52905aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 5291103a8feaSLen Brown 529234041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 5293103a8feaSLen Brown genuine_intel = 1; 529434041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 529534041551SLen Brown authentic_amd = 1; 5296c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 5297c1c10cc7SPu Wen hygon_genuine = 1; 5298103a8feaSLen Brown 529996e47158SLen Brown if (!quiet) 5300ed0757b8SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n", 5301ed0757b8SLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx, max_level); 5302103a8feaSLen Brown 53035aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 5304103a8feaSLen Brown family = (fms >> 8) & 0xf; 5305103a8feaSLen Brown model = (fms >> 4) & 0xf; 5306103a8feaSLen Brown stepping = fms & 0xf; 53075aa3d1a2SCalvin Walton if (family == 0xf) 53085aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 53095aa3d1a2SCalvin Walton if (family >= 6) 5310103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 531134041551SLen Brown ecx_flags = ecx; 531234041551SLen Brown edx_flags = edx; 5313103a8feaSLen Brown 5314ed0757b8SLen Brown if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch)) 5315ed0757b8SLen Brown warnx("get_msr(UCODE)\n"); 5316ed0757b8SLen Brown 5317103a8feaSLen Brown /* 5318103a8feaSLen Brown * check max extended function levels of CPUID. 5319103a8feaSLen Brown * This is needed to check for invariant TSC. 5320103a8feaSLen Brown * This check is valid for both Intel and AMD. 5321103a8feaSLen Brown */ 5322103a8feaSLen Brown ebx = ecx = edx = 0; 53235aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 5324103a8feaSLen Brown 532534041551SLen Brown if (!quiet) { 5326ed0757b8SLen Brown fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n", 53271b439f01SLen Brown family, model, stepping, family, model, stepping, 53281b439f01SLen Brown (unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF)); 5329ed0757b8SLen Brown fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level); 533034041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 533134041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 533234041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 533334041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 533434041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 533534041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 533634041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 533734041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 533834041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 53391b439f01SLen Brown edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-"); 534034041551SLen Brown } 534125368d7cSChen Yu if (genuine_intel) { 5342e9d3092fSZhang Rui model_orig = model; 5343f5a4c76aSLen Brown model = intel_model_duplicates(model); 534425368d7cSChen Yu } 534534041551SLen Brown 534634041551SLen Brown if (!(edx_flags & (1 << 5))) 534734041551SLen Brown errx(1, "CPUID: no MSR"); 534834041551SLen Brown 534961a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5350103a8feaSLen Brown 5351103a8feaSLen Brown /* 5352103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5353103a8feaSLen Brown * this check is valid for both Intel and AMD 5354103a8feaSLen Brown */ 53555aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 53568209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5357d7899447SLen Brown } 5358103a8feaSLen Brown 5359103a8feaSLen Brown /* 5360103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5361103a8feaSLen Brown * this check is valid for both Intel and AMD 5362103a8feaSLen Brown */ 5363103a8feaSLen Brown 53645aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 53658209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5366812db3f7SLen Brown if (has_aperf) { 5367812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5368812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5369812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5370812db3f7SLen Brown } 5371889facbeSLen Brown do_dts = eax & (1 << 0); 5372812db3f7SLen Brown if (do_dts) 5373812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5374b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5375889facbeSLen Brown do_ptm = eax & (1 << 6); 5376812db3f7SLen Brown if (do_ptm) 5377812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 53787f5c258eSLen Brown has_hwp = eax & (1 << 7); 53797f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 53807f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 53817f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 53827f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5383889facbeSLen Brown has_epb = ecx & (1 << 3); 5384889facbeSLen Brown 538596e47158SLen Brown if (!quiet) 5386b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 53877f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 53887f5c258eSLen Brown has_aperf ? "" : "No-", 5389b3a34e93SLen Brown has_turbo ? "" : "No-", 53907f5c258eSLen Brown do_dts ? "" : "No-", 53917f5c258eSLen Brown do_ptm ? "" : "No-", 53927f5c258eSLen Brown has_hwp ? "" : "No-", 53937f5c258eSLen Brown has_hwp_notify ? "" : "No-", 53947f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 53951b439f01SLen Brown has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-"); 5396103a8feaSLen Brown 539796e47158SLen Brown if (!quiet) 539869807a63SLen Brown decode_misc_enable_msr(); 539969807a63SLen Brown 540096e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5401aa8d8cc7SLen Brown int has_sgx; 5402aa8d8cc7SLen Brown 5403aa8d8cc7SLen Brown ecx = 0; 5404aa8d8cc7SLen Brown 5405aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5406aa8d8cc7SLen Brown 5407aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5408aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5409aa8d8cc7SLen Brown 5410aa8d8cc7SLen Brown if (has_sgx) 5411aa8d8cc7SLen Brown decode_feature_control_msr(); 5412aa8d8cc7SLen Brown } 5413aa8d8cc7SLen Brown 541461a87ba7SLen Brown if (max_level >= 0x15) { 54158a5bdf41SLen Brown unsigned int eax_crystal; 54168a5bdf41SLen Brown unsigned int ebx_tsc; 54178a5bdf41SLen Brown 54188a5bdf41SLen Brown /* 54198a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 54208a5bdf41SLen Brown */ 54218a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 54225aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 54238a5bdf41SLen Brown 54248a5bdf41SLen Brown if (ebx_tsc != 0) { 54258a5bdf41SLen Brown 542696e47158SLen Brown if (!quiet && (ebx != 0)) 5427b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 54288a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 54298a5bdf41SLen Brown 54308a5bdf41SLen Brown if (crystal_hz == 0) 54318a5bdf41SLen Brown switch (model) { 5432af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5433e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5434e8efbc80SLen Brown break; 54355ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5436ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5437ec53e594SLen Brown break; 5438869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5439f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5440e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 54418a5bdf41SLen Brown break; 54428a5bdf41SLen Brown default: 54438a5bdf41SLen Brown crystal_hz = 0; 54448a5bdf41SLen Brown } 54458a5bdf41SLen Brown 54468a5bdf41SLen Brown if (crystal_hz) { 54478a5bdf41SLen Brown tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal; 544896e47158SLen Brown if (!quiet) 5449b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 54508a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 54518a5bdf41SLen Brown } 54528a5bdf41SLen Brown } 54538a5bdf41SLen Brown } 545461a87ba7SLen Brown if (max_level >= 0x16) { 545561a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 545661a87ba7SLen Brown 545761a87ba7SLen Brown /* 545861a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 545961a87ba7SLen Brown */ 546061a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 546161a87ba7SLen Brown 54625aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 546396e47158SLen Brown if (!quiet) 5464b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 546561a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 546661a87ba7SLen Brown } 54678a5bdf41SLen Brown 5468b2b34dfeSHubert Chrzaniuk if (has_aperf) 5469b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5470b2b34dfeSHubert Chrzaniuk 5471812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5472812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5473812db3f7SLen Brown 5474812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5475812db3f7SLen Brown do_nhm_platform_info = 1; 5476812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5477812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5478812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5479812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5480812db3f7SLen Brown } 5481d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5482812db3f7SLen Brown 5483812db3f7SLen Brown if (do_snb_cstates) 5484812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5485812db3f7SLen Brown 54865a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 54870f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 54880f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 54890f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 54900f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 54910f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 54920f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54930f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 54940f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 54950539ba11SLen Brown if (has_slv_msrs(family, model)) { 54960f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 54970f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 54980f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 54990f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 55000539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 55010539ba11SLen Brown use_c1_residency_msr = 1; 55020539ba11SLen Brown } 550320de0dabSAntti Laakso if (is_jvl(family, model)) { 550420de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c3); 550520de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_CPU_c7); 550620de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc2); 550720de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc3); 550820de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc6); 550920de0dabSAntti Laakso BIC_NOT_PRESENT(BIC_Pkgpc7); 551020de0dabSAntti Laakso } 55117170a374SLen Brown if (is_dnv(family, model)) { 55127170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 55137170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 55147170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 55157170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 55167170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 55177170a374SLen Brown use_c1_residency_msr = 1; 55187170a374SLen Brown } 551925368d7cSChen Yu if (is_skx(family, model) || is_icx(family, model)) { 552034c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 552134c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 552234c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 552334c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 552434c76197SLen Brown } 5525ade0ebacSLen Brown if (is_bdx(family, model)) { 5526ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5527ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5528ade0ebacSLen Brown } 5529570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5530c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 55310f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5532c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 55330f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5534c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 55350f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 55360f47c08dSLen Brown } 5537570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5538a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5539a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5540a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5541a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5542a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5543a99d8730SLen Brown } 5544144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5545fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5546103a8feaSLen Brown 55471b439f01SLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model)) 5548562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5549103a8feaSLen Brown 555096e47158SLen Brown if (!quiet) 5551f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5552f0057310SLen Brown 555396e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 555471616c8eSLen Brown decode_c6_demotion_policy_msr(); 555571616c8eSLen Brown 5556889facbeSLen Brown rapl_probe(family, model); 55573a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5558ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5559889facbeSLen Brown 55600b9a0b9bSZhang Rui check_tcc_offset(model_orig); 55610b9a0b9bSZhang Rui 556296e47158SLen Brown if (!quiet) 55631b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5564fcd17211SLen Brown 556541618e63SLen Brown if (!quiet) 5566d76bb7a0SLen Brown print_dev_latency(); 5567d76bb7a0SLen Brown if (!quiet) 556841618e63SLen Brown dump_sysfs_cstate_config(); 55697293fccdSLen Brown if (!quiet) 55707293fccdSLen Brown dump_sysfs_pstate_config(); 557141618e63SLen Brown 55728c69da29SChen Yu if (has_skl_msrs(family, model) || is_ehl(family, model)) 5573a2b7b749SLen Brown calculate_tsc_tweak(); 5574a2b7b749SLen Brown 5575812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5576812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5577fdf676e5SLen Brown 5578812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5579812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 558027d47356SLen Brown 5581b4b91569SRafael Antognolli if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK)) 5582b4b91569SRafael Antognolli BIC_PRESENT(BIC_GFXACTMHz); 5583b4b91569SRafael Antognolli 5584be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5585be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5586be0e54c4SLen Brown else 5587be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5588be0e54c4SLen Brown 55891f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 55901f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5591be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 55921f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 55931f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 55941f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 55951f81c5efSLen Brown } else { 55961f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5597be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 55981f81c5efSLen Brown } 5599be0e54c4SLen Brown 560096e47158SLen Brown if (!quiet) 560133148d67SLen Brown decode_misc_feature_control(); 560233148d67SLen Brown 5603889facbeSLen Brown return; 5604103a8feaSLen Brown } 5605103a8feaSLen Brown 5606103a8feaSLen Brown /* 5607103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5608103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5609103a8feaSLen Brown */ 5610103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5611103a8feaSLen Brown { 5612103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5613103a8feaSLen Brown return 1; 5614103a8feaSLen Brown else 5615103a8feaSLen Brown return 0; 5616103a8feaSLen Brown } 5617103a8feaSLen Brown 5618103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5619103a8feaSLen Brown { 5620103a8feaSLen Brown return 0; 5621103a8feaSLen Brown } 5622103a8feaSLen Brown 5623c98d5d94SLen Brown void topology_probe() 5624c98d5d94SLen Brown { 5625c98d5d94SLen Brown int i; 5626c98d5d94SLen Brown int max_core_id = 0; 5627c98d5d94SLen Brown int max_package_id = 0; 56286de68fe1SLen Brown int max_die_id = 0; 5629c98d5d94SLen Brown int max_siblings = 0; 5630c98d5d94SLen Brown 5631c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5632843c5791SPrarit Bhargava set_max_cpu_num(); 5633c98d5d94SLen Brown topo.num_cpus = 0; 5634c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5635c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5636812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5637c98d5d94SLen Brown 5638d8af6f5fSLen Brown if (debug > 1) 5639b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5640c98d5d94SLen Brown 5641c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5642b2c95d90SJosh Triplett if (cpus == NULL) 5643b2c95d90SJosh Triplett err(1, "calloc cpus"); 5644c98d5d94SLen Brown 5645c98d5d94SLen Brown /* 5646c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5647c98d5d94SLen Brown */ 5648c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5649b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5650b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5651c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5652c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5653c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5654c98d5d94SLen Brown 5655c98d5d94SLen Brown /* 56561ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 56571ef7d21aSLen Brown */ 56581ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 56591ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 56601ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 56611ef7d21aSLen Brown err(1, "cpu%d not present", i); 56621ef7d21aSLen Brown } 56631ef7d21aSLen Brown 56641ef7d21aSLen Brown /* 5665c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5666c98d5d94SLen Brown */ 5667c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5668b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5669b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5670c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5671c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5672c98d5d94SLen Brown 56738cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5674c98d5d94SLen Brown 5675c98d5d94SLen Brown /* 5676c98d5d94SLen Brown * For online cpus 5677c98d5d94SLen Brown * find max_core_id, max_package_id 5678c98d5d94SLen Brown */ 5679c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5680c98d5d94SLen Brown int siblings; 5681c98d5d94SLen Brown 5682c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5683d8af6f5fSLen Brown if (debug > 1) 5684b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5685c98d5d94SLen Brown continue; 5686c98d5d94SLen Brown } 5687c98d5d94SLen Brown 56880e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 56890e2d8f05SLen Brown 56900e2d8f05SLen Brown /* get package information */ 5691c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5692c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5693c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5694c98d5d94SLen Brown 56956de68fe1SLen Brown /* get die information */ 56966de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 56976de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 56986de68fe1SLen Brown max_die_id = cpus[i].die_id; 56996de68fe1SLen Brown 57000e2d8f05SLen Brown /* get numa node information */ 5701ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5702ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5703ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 57040e2d8f05SLen Brown 57050e2d8f05SLen Brown /* get core information */ 57060e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 57070e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 57080e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 57090e2d8f05SLen Brown 57100e2d8f05SLen Brown /* get thread information */ 57110e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5712c98d5d94SLen Brown if (siblings > max_siblings) 5713c98d5d94SLen Brown max_siblings = siblings; 57144f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 57158cb48b32SPrarit Bhargava topo.num_cores++; 5716c98d5d94SLen Brown } 5717ef605741SPrarit Bhargava 571870a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5719d8af6f5fSLen Brown if (debug > 1) 57201b439f01SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node); 572170a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5722812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5723c98d5d94SLen Brown 57246de68fe1SLen Brown topo.num_die = max_die_id + 1; 57256de68fe1SLen Brown if (debug > 1) 57261b439f01SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die); 57276de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 57286de68fe1SLen Brown BIC_PRESENT(BIC_Die); 57296de68fe1SLen Brown 5730c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5731d8af6f5fSLen Brown if (debug > 1) 57321b439f01SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages); 57337da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5734812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5735c98d5d94SLen Brown 5736ef605741SPrarit Bhargava set_node_data(); 5737ef605741SPrarit Bhargava if (debug > 1) 573870a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 573901235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 574001235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5741ef605741SPrarit Bhargava 574270a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5743d8af6f5fSLen Brown if (debug > 1) 5744b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 57452ffbb224SPrarit Bhargava 57462ffbb224SPrarit Bhargava if (debug < 1) 57472ffbb224SPrarit Bhargava return; 57482ffbb224SPrarit Bhargava 57492ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 57500ec712e3SLen Brown if (cpu_is_not_present(i)) 57510ec712e3SLen Brown continue; 57522ffbb224SPrarit Bhargava fprintf(outf, 57536de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 57546de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 57551b439f01SLen Brown cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id); 57562ffbb224SPrarit Bhargava } 57572ffbb224SPrarit Bhargava 5758c98d5d94SLen Brown } 5759c98d5d94SLen Brown 57601b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 5761c98d5d94SLen Brown { 5762c98d5d94SLen Brown int i; 57631b439f01SLen Brown int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages; 576440f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5765c98d5d94SLen Brown 576640f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5767c98d5d94SLen Brown if (*t == NULL) 5768c98d5d94SLen Brown goto error; 5769c98d5d94SLen Brown 577040f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5771c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5772c98d5d94SLen Brown 577340f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5774c98d5d94SLen Brown if (*c == NULL) 5775c98d5d94SLen Brown goto error; 5776c98d5d94SLen Brown 577740f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5778c98d5d94SLen Brown (*c)[i].core_id = -1; 5779c98d5d94SLen Brown 5780678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5781c98d5d94SLen Brown if (*p == NULL) 5782c98d5d94SLen Brown goto error; 5783c98d5d94SLen Brown 5784c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5785c98d5d94SLen Brown (*p)[i].package_id = i; 5786c98d5d94SLen Brown 5787c98d5d94SLen Brown return; 5788c98d5d94SLen Brown error: 5789b2c95d90SJosh Triplett err(1, "calloc counters"); 5790c98d5d94SLen Brown } 57911b439f01SLen Brown 5792c98d5d94SLen Brown /* 5793c98d5d94SLen Brown * init_counter() 5794c98d5d94SLen Brown * 5795c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5796c98d5d94SLen Brown */ 57971b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id) 5798c98d5d94SLen Brown { 57998cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 580040f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 58018cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 58028cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5803c98d5d94SLen Brown struct thread_data *t; 5804c98d5d94SLen Brown struct core_data *c; 5805c98d5d94SLen Brown struct pkg_data *p; 5806c98d5d94SLen Brown 580742dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 580842dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 580942dd4520SNathan Ciobanu */ 581042dd4520SNathan Ciobanu if (node_id < 0) 581142dd4520SNathan Ciobanu node_id = 0; 581242dd4520SNathan Ciobanu 581340f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 581440f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 58158cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5816c98d5d94SLen Brown 5817c98d5d94SLen Brown t->cpu_id = cpu_id; 58188cb48b32SPrarit Bhargava if (thread_id == 0) { 5819c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5820c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5821c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5822c98d5d94SLen Brown } 5823c98d5d94SLen Brown 58248cb48b32SPrarit Bhargava c->core_id = core_id; 58258cb48b32SPrarit Bhargava p->package_id = pkg_id; 5826c98d5d94SLen Brown } 5827c98d5d94SLen Brown 5828c98d5d94SLen Brown int initialize_counters(int cpu_id) 5829c98d5d94SLen Brown { 58308cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 58318cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5832c98d5d94SLen Brown return 0; 5833c98d5d94SLen Brown } 5834c98d5d94SLen Brown 5835c98d5d94SLen Brown void allocate_output_buffer() 5836c98d5d94SLen Brown { 5837eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5838c98d5d94SLen Brown outp = output_buffer; 5839b2c95d90SJosh Triplett if (outp == NULL) 5840b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5841c98d5d94SLen Brown } 58421b439f01SLen Brown 584336229897SLen Brown void allocate_fd_percpu(void) 584436229897SLen Brown { 584501a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 584636229897SLen Brown if (fd_percpu == NULL) 584736229897SLen Brown err(-1, "calloc fd_percpu"); 584836229897SLen Brown } 58491b439f01SLen Brown 5850562a2d37SLen Brown void allocate_irq_buffers(void) 5851562a2d37SLen Brown { 5852562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5853562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5854562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5855c98d5d94SLen Brown 585601a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5857562a2d37SLen Brown if (irqs_per_cpu == NULL) 585801a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5859562a2d37SLen Brown } 58601b439f01SLen Brown 5861c98d5d94SLen Brown void setup_all_buffers(void) 5862c98d5d94SLen Brown { 5863c98d5d94SLen Brown topology_probe(); 5864562a2d37SLen Brown allocate_irq_buffers(); 586536229897SLen Brown allocate_fd_percpu(); 5866c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5867c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5868c98d5d94SLen Brown allocate_output_buffer(); 5869c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5870c98d5d94SLen Brown } 58713b4d5c7fSAndy Shevchenko 58727ce7d5deSPrarit Bhargava void set_base_cpu(void) 58737ce7d5deSPrarit Bhargava { 58747ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 58757ce7d5deSPrarit Bhargava if (base_cpu < 0) 58767ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 58777ce7d5deSPrarit Bhargava 58787ce7d5deSPrarit Bhargava if (debug > 1) 5879b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 58807ce7d5deSPrarit Bhargava } 58817ce7d5deSPrarit Bhargava 5882103a8feaSLen Brown void turbostat_init() 5883103a8feaSLen Brown { 58847ce7d5deSPrarit Bhargava setup_all_buffers(); 58857ce7d5deSPrarit Bhargava set_base_cpu(); 5886103a8feaSLen Brown check_dev_msr(); 588798481e79SLen Brown check_permissions(); 5888fcd17211SLen Brown process_cpuid(); 58892af4f9b8SLen Brown linux_perf_init(); 5890103a8feaSLen Brown 589196e47158SLen Brown if (!quiet) 58927f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 58937f5c258eSLen Brown 589496e47158SLen Brown if (!quiet) 5895889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5896889facbeSLen Brown 589796e47158SLen Brown if (!quiet) 58983a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 58993a9a941dSLen Brown 590096e47158SLen Brown if (!quiet) 5901889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5902889facbeSLen Brown 5903889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5904889facbeSLen Brown 59057ab5ff49SZhang Rui for_all_cpus(get_cpu_type, ODD_COUNTERS); 59067ab5ff49SZhang Rui for_all_cpus(get_cpu_type, EVEN_COUNTERS); 59077ab5ff49SZhang Rui 590896e47158SLen Brown if (!quiet) 5909889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 59105a63426eSLen Brown 591196e47158SLen Brown if (!quiet && do_irtl_snb) 59125a63426eSLen Brown print_irtl(); 5913103a8feaSLen Brown } 5914103a8feaSLen Brown 5915103a8feaSLen Brown int fork_it(char **argv) 5916103a8feaSLen Brown { 5917103a8feaSLen Brown pid_t child_pid; 5918d91bb17cSLen Brown int status; 5919d15cf7c1SLen Brown 5920218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5921d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 59224c2122d4SLen Brown first_counter_read = 0; 5923d91bb17cSLen Brown if (status) 5924d91bb17cSLen Brown exit(status); 5925d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5926d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5927103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5928103a8feaSLen Brown 5929103a8feaSLen Brown child_pid = fork(); 5930103a8feaSLen Brown if (!child_pid) { 5931103a8feaSLen Brown /* child */ 5932103a8feaSLen Brown execvp(argv[0], argv); 59330815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5934103a8feaSLen Brown } else { 5935103a8feaSLen Brown 5936103a8feaSLen Brown /* parent */ 5937b2c95d90SJosh Triplett if (child_pid == -1) 5938b2c95d90SJosh Triplett err(1, "fork"); 5939103a8feaSLen Brown 5940103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5941103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5942b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5943b2c95d90SJosh Triplett err(status, "waitpid"); 59442a954966SDavid Arcari 59452a954966SDavid Arcari if (WIFEXITED(status)) 59462a954966SDavid Arcari status = WEXITSTATUS(status); 5947103a8feaSLen Brown } 5948c98d5d94SLen Brown /* 5949c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5950c98d5d94SLen Brown * because re-starting is problematic when forking 5951c98d5d94SLen Brown */ 5952218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5953c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5954103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5955103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5956ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5957ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5958ba3dec99SLen Brown else { 5959c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5960c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5961ba3dec99SLen Brown } 5962103a8feaSLen Brown 5963b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0); 5964b7d8c148SLen Brown 5965b7d8c148SLen Brown flush_output_stderr(); 5966103a8feaSLen Brown 5967d91bb17cSLen Brown return status; 5968103a8feaSLen Brown } 5969103a8feaSLen Brown 59703b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 59713b4d5c7fSAndy Shevchenko { 59723b4d5c7fSAndy Shevchenko int status; 59733b4d5c7fSAndy Shevchenko 5974218f0e8dSLen Brown snapshot_proc_sysfs_files(); 59753b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 59763b4d5c7fSAndy Shevchenko if (status) 59773b4d5c7fSAndy Shevchenko return status; 59783b4d5c7fSAndy Shevchenko 59793b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 59803b4d5c7fSAndy Shevchenko if (status) 59813b4d5c7fSAndy Shevchenko return status; 59823b4d5c7fSAndy Shevchenko 5983b7d8c148SLen Brown flush_output_stdout(); 59843b4d5c7fSAndy Shevchenko 59853b4d5c7fSAndy Shevchenko return status; 59863b4d5c7fSAndy Shevchenko } 59873b4d5c7fSAndy Shevchenko 59881b439f01SLen Brown void print_version() 59891b439f01SLen Brown { 59903c070b2aSLen Brown fprintf(outf, "turbostat version 21.05.04" " - Len Brown <lenb@kernel.org>\n"); 5991d8af6f5fSLen Brown } 5992d8af6f5fSLen Brown 5993495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5994495c7654SLen Brown unsigned int width, enum counter_scope scope, 599541618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5996388e9c81SLen Brown { 5997388e9c81SLen Brown struct msr_counter *msrp; 5998388e9c81SLen Brown 5999388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 6000388e9c81SLen Brown if (msrp == NULL) { 6001388e9c81SLen Brown perror("calloc"); 6002388e9c81SLen Brown exit(1); 6003388e9c81SLen Brown } 6004388e9c81SLen Brown 6005388e9c81SLen Brown msrp->msr_num = msr_num; 6006d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 6007495c7654SLen Brown if (path) 6008d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 6009388e9c81SLen Brown msrp->width = width; 6010388e9c81SLen Brown msrp->type = type; 6011388e9c81SLen Brown msrp->format = format; 601241618e63SLen Brown msrp->flags = flags; 6013388e9c81SLen Brown 6014388e9c81SLen Brown switch (scope) { 6015388e9c81SLen Brown 6016388e9c81SLen Brown case SCOPE_CPU: 6017388e9c81SLen Brown msrp->next = sys.tp; 6018388e9c81SLen Brown sys.tp = msrp; 6019678a3bd1SLen Brown sys.added_thread_counters++; 60200748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 60211b439f01SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS); 6022678a3bd1SLen Brown exit(-1); 6023678a3bd1SLen Brown } 6024388e9c81SLen Brown break; 6025388e9c81SLen Brown 6026388e9c81SLen Brown case SCOPE_CORE: 6027388e9c81SLen Brown msrp->next = sys.cp; 6028388e9c81SLen Brown sys.cp = msrp; 6029678a3bd1SLen Brown sys.added_core_counters++; 6030678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 60311b439f01SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS); 6032678a3bd1SLen Brown exit(-1); 6033678a3bd1SLen Brown } 6034388e9c81SLen Brown break; 6035388e9c81SLen Brown 6036388e9c81SLen Brown case SCOPE_PACKAGE: 6037388e9c81SLen Brown msrp->next = sys.pp; 6038388e9c81SLen Brown sys.pp = msrp; 6039678a3bd1SLen Brown sys.added_package_counters++; 6040678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 60411b439f01SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS); 6042678a3bd1SLen Brown exit(-1); 6043678a3bd1SLen Brown } 6044388e9c81SLen Brown break; 6045388e9c81SLen Brown } 6046388e9c81SLen Brown 6047388e9c81SLen Brown return 0; 6048388e9c81SLen Brown } 6049388e9c81SLen Brown 6050388e9c81SLen Brown void parse_add_command(char *add_command) 6051388e9c81SLen Brown { 6052388e9c81SLen Brown int msr_num = 0; 6053495c7654SLen Brown char *path = NULL; 60540f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 6055388e9c81SLen Brown int width = 64; 6056388e9c81SLen Brown int fail = 0; 6057388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 6058388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 6059388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 6060388e9c81SLen Brown 6061388e9c81SLen Brown while (add_command) { 6062388e9c81SLen Brown 6063388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 6064388e9c81SLen Brown goto next; 6065388e9c81SLen Brown 6066388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 6067388e9c81SLen Brown goto next; 6068388e9c81SLen Brown 6069495c7654SLen Brown if (*add_command == '/') { 6070495c7654SLen Brown path = add_command; 6071495c7654SLen Brown goto next; 6072495c7654SLen Brown } 6073495c7654SLen Brown 6074388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 6075388e9c81SLen Brown if ((width == 32) || (width == 64)) 6076388e9c81SLen Brown goto next; 6077388e9c81SLen Brown width = 64; 6078388e9c81SLen Brown } 6079388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 6080388e9c81SLen Brown scope = SCOPE_CPU; 6081388e9c81SLen Brown goto next; 6082388e9c81SLen Brown } 6083388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 6084388e9c81SLen Brown scope = SCOPE_CORE; 6085388e9c81SLen Brown goto next; 6086388e9c81SLen Brown } 6087388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 6088388e9c81SLen Brown scope = SCOPE_PACKAGE; 6089388e9c81SLen Brown goto next; 6090388e9c81SLen Brown } 6091388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 6092388e9c81SLen Brown type = COUNTER_CYCLES; 6093388e9c81SLen Brown goto next; 6094388e9c81SLen Brown } 6095388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 6096388e9c81SLen Brown type = COUNTER_SECONDS; 6097388e9c81SLen Brown goto next; 6098388e9c81SLen Brown } 609941618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 610041618e63SLen Brown type = COUNTER_USEC; 610141618e63SLen Brown goto next; 610241618e63SLen Brown } 6103388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 6104388e9c81SLen Brown format = FORMAT_RAW; 6105388e9c81SLen Brown goto next; 6106388e9c81SLen Brown } 6107388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 6108388e9c81SLen Brown format = FORMAT_DELTA; 6109388e9c81SLen Brown goto next; 6110388e9c81SLen Brown } 6111388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 6112388e9c81SLen Brown format = FORMAT_PERCENT; 6113388e9c81SLen Brown goto next; 6114388e9c81SLen Brown } 6115388e9c81SLen Brown 6116388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 6117388e9c81SLen Brown char *eos; 6118388e9c81SLen Brown 6119388e9c81SLen Brown eos = strchr(name_buffer, ','); 6120388e9c81SLen Brown if (eos) 6121388e9c81SLen Brown *eos = '\0'; 6122388e9c81SLen Brown goto next; 6123388e9c81SLen Brown } 6124388e9c81SLen Brown 6125388e9c81SLen Brown next: 6126388e9c81SLen Brown add_command = strchr(add_command, ','); 6127495c7654SLen Brown if (add_command) { 6128495c7654SLen Brown *add_command = '\0'; 6129388e9c81SLen Brown add_command++; 6130495c7654SLen Brown } 6131388e9c81SLen Brown 6132388e9c81SLen Brown } 6133495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 6134495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 6135388e9c81SLen Brown fail++; 6136388e9c81SLen Brown } 6137388e9c81SLen Brown 6138388e9c81SLen Brown /* generate default column header */ 6139388e9c81SLen Brown if (*name_buffer == '\0') { 6140388e9c81SLen Brown if (width == 32) 61415f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6142388e9c81SLen Brown else 61435f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 6144388e9c81SLen Brown } 6145388e9c81SLen Brown 614641618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 6147388e9c81SLen Brown fail++; 6148388e9c81SLen Brown 6149388e9c81SLen Brown if (fail) { 6150388e9c81SLen Brown help(); 6151388e9c81SLen Brown exit(1); 6152388e9c81SLen Brown } 6153388e9c81SLen Brown } 615441618e63SLen Brown 6155dd778a5eSLen Brown int is_deferred_skip(char *name) 6156dd778a5eSLen Brown { 6157dd778a5eSLen Brown int i; 6158dd778a5eSLen Brown 6159dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 6160dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 6161dd778a5eSLen Brown return 1; 6162dd778a5eSLen Brown return 0; 6163dd778a5eSLen Brown } 6164dd778a5eSLen Brown 616541618e63SLen Brown void probe_sysfs(void) 616641618e63SLen Brown { 616741618e63SLen Brown char path[64]; 616841618e63SLen Brown char name_buf[16]; 616941618e63SLen Brown FILE *input; 617041618e63SLen Brown int state; 617141618e63SLen Brown char *sp; 617241618e63SLen Brown 617341618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 617441618e63SLen Brown return; 617541618e63SLen Brown 61760748eaf0SLen Brown for (state = 10; state >= 0; --state) { 617741618e63SLen Brown 61781b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 617941618e63SLen Brown input = fopen(path, "r"); 618041618e63SLen Brown if (input == NULL) 618141618e63SLen Brown continue; 61828173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 61838173c336SBen Hutchings err(1, "%s: failed to read file", path); 618441618e63SLen Brown 618541618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 618641618e63SLen Brown sp = strchr(name_buf, '-'); 618741618e63SLen Brown if (!sp) 618841618e63SLen Brown sp = strchrnul(name_buf, '\n'); 618941618e63SLen Brown *sp = '%'; 619041618e63SLen Brown *(sp + 1) = '\0'; 619141618e63SLen Brown 6192fecb3bc8SDavid Arcari remove_underbar(name_buf); 6193fecb3bc8SDavid Arcari 619441618e63SLen Brown fclose(input); 619541618e63SLen Brown 619641618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 619741618e63SLen Brown 6198dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6199dd778a5eSLen Brown continue; 6200dd778a5eSLen Brown 62011b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU); 620241618e63SLen Brown } 620341618e63SLen Brown 62040748eaf0SLen Brown for (state = 10; state >= 0; --state) { 620541618e63SLen Brown 62061b439f01SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state); 620741618e63SLen Brown input = fopen(path, "r"); 620841618e63SLen Brown if (input == NULL) 620941618e63SLen Brown continue; 62108173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 62118173c336SBen Hutchings err(1, "%s: failed to read file", path); 621241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 621341618e63SLen Brown sp = strchr(name_buf, '-'); 621441618e63SLen Brown if (!sp) 621541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 621641618e63SLen Brown *sp = '\0'; 621741618e63SLen Brown fclose(input); 621841618e63SLen Brown 6219fecb3bc8SDavid Arcari remove_underbar(name_buf); 6220fecb3bc8SDavid Arcari 622141618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 622241618e63SLen Brown 6223dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 6224dd778a5eSLen Brown continue; 6225dd778a5eSLen Brown 62261b439f01SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU); 622741618e63SLen Brown } 622841618e63SLen Brown 622941618e63SLen Brown } 623041618e63SLen Brown 62311ef7d21aSLen Brown /* 62321ef7d21aSLen Brown * parse cpuset with following syntax 62331ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 62341ef7d21aSLen Brown */ 62351ef7d21aSLen Brown void parse_cpu_command(char *optarg) 62361ef7d21aSLen Brown { 62371ef7d21aSLen Brown unsigned int start, end; 62381ef7d21aSLen Brown char *next; 62391ef7d21aSLen Brown 62404e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 62414e4e1e7cSLen Brown if (cpu_subset) 62424e4e1e7cSLen Brown goto error; 62434e4e1e7cSLen Brown show_core_only++; 62444e4e1e7cSLen Brown return; 62454e4e1e7cSLen Brown } 62464e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 62474e4e1e7cSLen Brown if (cpu_subset) 62484e4e1e7cSLen Brown goto error; 62494e4e1e7cSLen Brown show_pkg_only++; 62504e4e1e7cSLen Brown return; 62514e4e1e7cSLen Brown } 62524e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 62534e4e1e7cSLen Brown goto error; 62544e4e1e7cSLen Brown 62551ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 62561ef7d21aSLen Brown if (cpu_subset == NULL) 62571ef7d21aSLen Brown err(3, "CPU_ALLOC"); 62581ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 62591ef7d21aSLen Brown 62601ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 62611ef7d21aSLen Brown 62621ef7d21aSLen Brown next = optarg; 62631ef7d21aSLen Brown 62641ef7d21aSLen Brown while (next && *next) { 62651ef7d21aSLen Brown 62661ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 62671ef7d21aSLen Brown goto error; 62681ef7d21aSLen Brown 62691ef7d21aSLen Brown start = strtoul(next, &next, 10); 62701ef7d21aSLen Brown 62711ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 62721ef7d21aSLen Brown goto error; 62731ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 62741ef7d21aSLen Brown 62751ef7d21aSLen Brown if (*next == '\0') 62761ef7d21aSLen Brown break; 62771ef7d21aSLen Brown 62781ef7d21aSLen Brown if (*next == ',') { 62791ef7d21aSLen Brown next += 1; 62801ef7d21aSLen Brown continue; 62811ef7d21aSLen Brown } 62821ef7d21aSLen Brown 62831ef7d21aSLen Brown if (*next == '-') { 62841ef7d21aSLen Brown next += 1; /* start range */ 62851ef7d21aSLen Brown } else if (*next == '.') { 62861ef7d21aSLen Brown next += 1; 62871ef7d21aSLen Brown if (*next == '.') 62881ef7d21aSLen Brown next += 1; /* start range */ 62891ef7d21aSLen Brown else 62901ef7d21aSLen Brown goto error; 62911ef7d21aSLen Brown } 62921ef7d21aSLen Brown 62931ef7d21aSLen Brown end = strtoul(next, &next, 10); 62941ef7d21aSLen Brown if (end <= start) 62951ef7d21aSLen Brown goto error; 62961ef7d21aSLen Brown 62971ef7d21aSLen Brown while (++start <= end) { 62981ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 62991ef7d21aSLen Brown goto error; 63001ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 63011ef7d21aSLen Brown } 63021ef7d21aSLen Brown 63031ef7d21aSLen Brown if (*next == ',') 63041ef7d21aSLen Brown next += 1; 63051ef7d21aSLen Brown else if (*next != '\0') 63061ef7d21aSLen Brown goto error; 63071ef7d21aSLen Brown } 63081ef7d21aSLen Brown 63091ef7d21aSLen Brown return; 63101ef7d21aSLen Brown 63111ef7d21aSLen Brown error: 63124e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 63134e4e1e7cSLen Brown help(); 63141ef7d21aSLen Brown exit(-1); 63151ef7d21aSLen Brown } 63161ef7d21aSLen Brown 6317103a8feaSLen Brown void cmdline(int argc, char **argv) 6318103a8feaSLen Brown { 6319103a8feaSLen Brown int opt; 6320d8af6f5fSLen Brown int option_index = 0; 6321d8af6f5fSLen Brown static struct option long_options[] = { 6322388e9c81SLen Brown { "add", required_argument, 0, 'a' }, 63231ef7d21aSLen Brown { "cpu", required_argument, 0, 'c' }, 6324d8af6f5fSLen Brown { "Dump", no_argument, 0, 'D' }, 632596e47158SLen Brown { "debug", no_argument, 0, 'd' }, /* internal, not documented */ 63263f44a5c6SLen Brown { "enable", required_argument, 0, 'e' }, 6327d8af6f5fSLen Brown { "interval", required_argument, 0, 'i' }, 63282af4f9b8SLen Brown { "IPC", no_argument, 0, 'I' }, 6329023fe0acSChen Yu { "num_iterations", required_argument, 0, 'n' }, 6330d8af6f5fSLen Brown { "help", no_argument, 0, 'h' }, 6331812db3f7SLen Brown { "hide", required_argument, 0, 'H' }, // meh, -h taken by --help 6332d8af6f5fSLen Brown { "Joules", no_argument, 0, 'J' }, 6333c8ade361SLen Brown { "list", no_argument, 0, 'l' }, 6334b7d8c148SLen Brown { "out", required_argument, 0, 'o' }, 633596e47158SLen Brown { "quiet", no_argument, 0, 'q' }, 6336812db3f7SLen Brown { "show", required_argument, 0, 's' }, 6337d8af6f5fSLen Brown { "Summary", no_argument, 0, 'S' }, 6338d8af6f5fSLen Brown { "TCC", required_argument, 0, 'T' }, 6339d8af6f5fSLen Brown { "version", no_argument, 0, 'v' }, 6340d8af6f5fSLen Brown { 0, 0, 0, 0 } 6341d8af6f5fSLen Brown }; 6342103a8feaSLen Brown 6343103a8feaSLen Brown progname = argv[0]; 6344103a8feaSLen Brown 63451b439f01SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) { 6346103a8feaSLen Brown switch (opt) { 6347388e9c81SLen Brown case 'a': 6348388e9c81SLen Brown parse_add_command(optarg); 6349388e9c81SLen Brown break; 63501ef7d21aSLen Brown case 'c': 63511ef7d21aSLen Brown parse_cpu_command(optarg); 63521ef7d21aSLen Brown break; 6353d8af6f5fSLen Brown case 'D': 6354d8af6f5fSLen Brown dump_only++; 63558e180f3cSLen Brown break; 63563f44a5c6SLen Brown case 'e': 63573f44a5c6SLen Brown /* --enable specified counter */ 63584c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 63593f44a5c6SLen Brown break; 6360d8af6f5fSLen Brown case 'd': 6361d8af6f5fSLen Brown debug++; 63623f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 63632f32edf1SLen Brown break; 6364812db3f7SLen Brown case 'H': 63653f44a5c6SLen Brown /* 63663f44a5c6SLen Brown * --hide: do not show those specified 63673f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 63683f44a5c6SLen Brown */ 63693f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6370812db3f7SLen Brown break; 6371d8af6f5fSLen Brown case 'h': 6372d8af6f5fSLen Brown default: 6373d8af6f5fSLen Brown help(); 6374d8af6f5fSLen Brown exit(1); 6375d8af6f5fSLen Brown case 'i': 63762a0609c0SLen Brown { 63772a0609c0SLen Brown double interval = strtod(optarg, NULL); 63782a0609c0SLen Brown 63792a0609c0SLen Brown if (interval < 0.001) { 63801b439f01SLen Brown fprintf(outf, "interval %f seconds is too small\n", interval); 63812a0609c0SLen Brown exit(2); 63822a0609c0SLen Brown } 63832a0609c0SLen Brown 638447936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6385b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 638647936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 63872a0609c0SLen Brown } 6388889facbeSLen Brown break; 63895c56be9aSDirk Brandewie case 'J': 63905c56be9aSDirk Brandewie rapl_joules++; 63915c56be9aSDirk Brandewie break; 6392c8ade361SLen Brown case 'l': 63933f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6394c8ade361SLen Brown list_header_only++; 6395c8ade361SLen Brown quiet++; 6396c8ade361SLen Brown break; 6397b7d8c148SLen Brown case 'o': 6398b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6399b7d8c148SLen Brown break; 640096e47158SLen Brown case 'q': 640196e47158SLen Brown quiet = 1; 640296e47158SLen Brown break; 6403023fe0acSChen Yu case 'n': 6404023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6405023fe0acSChen Yu 6406023fe0acSChen Yu if (num_iterations <= 0) { 64071b439f01SLen Brown fprintf(outf, "iterations %d should be positive number\n", num_iterations); 6408023fe0acSChen Yu exit(2); 6409023fe0acSChen Yu } 6410023fe0acSChen Yu break; 6411812db3f7SLen Brown case 's': 64123f44a5c6SLen Brown /* 64133f44a5c6SLen Brown * --show: show only those specified 64143f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 64153f44a5c6SLen Brown * subsequent invocations can add to it. 64163f44a5c6SLen Brown */ 64173f44a5c6SLen Brown if (shown == 0) 64183f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 64193f44a5c6SLen Brown else 64203f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 64213f44a5c6SLen Brown shown = 1; 6422812db3f7SLen Brown break; 6423d8af6f5fSLen Brown case 'S': 6424d8af6f5fSLen Brown summary_only++; 6425d8af6f5fSLen Brown break; 6426d8af6f5fSLen Brown case 'T': 642755279aefSZhang Rui tj_max_override = atoi(optarg); 6428d8af6f5fSLen Brown break; 6429d8af6f5fSLen Brown case 'v': 6430d8af6f5fSLen Brown print_version(); 6431d8af6f5fSLen Brown exit(0); 6432d8af6f5fSLen Brown break; 6433103a8feaSLen Brown } 6434103a8feaSLen Brown } 6435103a8feaSLen Brown } 6436103a8feaSLen Brown 6437103a8feaSLen Brown int main(int argc, char **argv) 6438103a8feaSLen Brown { 6439b7d8c148SLen Brown outf = stderr; 6440103a8feaSLen Brown cmdline(argc, argv); 6441103a8feaSLen Brown 644296e47158SLen Brown if (!quiet) 6443d8af6f5fSLen Brown print_version(); 6444103a8feaSLen Brown 644541618e63SLen Brown probe_sysfs(); 644641618e63SLen Brown 6447103a8feaSLen Brown turbostat_init(); 6448103a8feaSLen Brown 64496799ba84SDan Merillat msr_sum_record(); 64506799ba84SDan Merillat 64513b4d5c7fSAndy Shevchenko /* dump counters and exit */ 64523b4d5c7fSAndy Shevchenko if (dump_only) 64533b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 64543b4d5c7fSAndy Shevchenko 6455c8ade361SLen Brown /* list header and exit */ 6456c8ade361SLen Brown if (list_header_only) { 6457c8ade361SLen Brown print_header(","); 6458c8ade361SLen Brown flush_output_stdout(); 6459c8ade361SLen Brown return 0; 6460c8ade361SLen Brown } 6461c8ade361SLen Brown 6462103a8feaSLen Brown /* 6463103a8feaSLen Brown * if any params left, it must be a command to fork 6464103a8feaSLen Brown */ 6465103a8feaSLen Brown if (argc - optind) 6466103a8feaSLen Brown return fork_it(argv + optind); 6467103a8feaSLen Brown else 6468103a8feaSLen Brown turbostat_loop(); 6469103a8feaSLen Brown 6470103a8feaSLen Brown return 0; 6471103a8feaSLen Brown } 6472