1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2103a8feaSLen Brown /* 3103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 434041551SLen Brown * on modern Intel and AMD processors. 5103a8feaSLen Brown * 6144b44b1SLen Brown * Copyright (c) 2013 Intel Corporation. 7103a8feaSLen Brown * Len Brown <len.brown@intel.com> 8103a8feaSLen Brown */ 9103a8feaSLen Brown 1088c3281fSLen Brown #define _GNU_SOURCE 11b731f311SJosh Triplett #include MSRHEADER 12869ce69eSLen Brown #include INTEL_FAMILY_HEADER 1395aebc44SJosh Triplett #include <stdarg.h> 14103a8feaSLen Brown #include <stdio.h> 15b2c95d90SJosh Triplett #include <err.h> 16103a8feaSLen Brown #include <unistd.h> 17103a8feaSLen Brown #include <sys/types.h> 18103a8feaSLen Brown #include <sys/wait.h> 19103a8feaSLen Brown #include <sys/stat.h> 20b9ad8ee0SLen Brown #include <sys/select.h> 21103a8feaSLen Brown #include <sys/resource.h> 22103a8feaSLen Brown #include <fcntl.h> 23103a8feaSLen Brown #include <signal.h> 24103a8feaSLen Brown #include <sys/time.h> 25103a8feaSLen Brown #include <stdlib.h> 26d8af6f5fSLen Brown #include <getopt.h> 27103a8feaSLen Brown #include <dirent.h> 28103a8feaSLen Brown #include <string.h> 29103a8feaSLen Brown #include <ctype.h> 3088c3281fSLen Brown #include <sched.h> 312a0609c0SLen Brown #include <time.h> 322b92865eSJosh Triplett #include <cpuid.h> 33fcaa681cSLen Brown #include <sys/capability.h> 3498481e79SLen Brown #include <errno.h> 359392bd98SCalvin Walton #include <math.h> 36103a8feaSLen Brown 37103a8feaSLen Brown char *proc_stat = "/proc/stat"; 38b7d8c148SLen Brown FILE *outf; 3936229897SLen Brown int *fd_percpu; 40b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0}; 4147936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0}; 42023fe0acSChen Yu unsigned int num_iterations; 43d8af6f5fSLen Brown unsigned int debug; 4496e47158SLen Brown unsigned int quiet; 453f44a5c6SLen Brown unsigned int shown; 460de6c0dfSLen Brown unsigned int sums_need_wide_columns; 47d8af6f5fSLen Brown unsigned int rapl_joules; 48d8af6f5fSLen Brown unsigned int summary_only; 49c8ade361SLen Brown unsigned int list_header_only; 50d8af6f5fSLen Brown unsigned int dump_only; 51103a8feaSLen Brown unsigned int do_snb_cstates; 52fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 53144b44b1SLen Brown unsigned int do_slm_cstates; 54144b44b1SLen Brown unsigned int use_c1_residency_msr; 55103a8feaSLen Brown unsigned int has_aperf; 56889facbeSLen Brown unsigned int has_epb; 575a63426eSLen Brown unsigned int do_irtl_snb; 585a63426eSLen Brown unsigned int do_irtl_hsw; 59fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 60103a8feaSLen Brown unsigned int genuine_intel; 6134041551SLen Brown unsigned int authentic_amd; 62c1c10cc7SPu Wen unsigned int hygon_genuine; 6334041551SLen Brown unsigned int max_level, max_extended_level; 64103a8feaSLen Brown unsigned int has_invariant_tsc; 65d7899447SLen Brown unsigned int do_nhm_platform_info; 66cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 67b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 68103a8feaSLen Brown double bclk; 69a2b7b749SLen Brown double base_hz; 7021ed5574SLen Brown unsigned int has_base_hz; 71a2b7b749SLen Brown double tsc_tweak = 1.0; 72c98d5d94SLen Brown unsigned int show_pkg_only; 73c98d5d94SLen Brown unsigned int show_core_only; 74c98d5d94SLen Brown char *output_buffer, *outp; 75889facbeSLen Brown unsigned int do_rapl; 76889facbeSLen Brown unsigned int do_dts; 77889facbeSLen Brown unsigned int do_ptm; 78fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 79be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 80be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 8127d47356SLen Brown unsigned int gfx_cur_mhz; 82889facbeSLen Brown unsigned int tcc_activation_temp; 83889facbeSLen Brown unsigned int tcc_activation_temp_override; 8440ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 8540ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 86889facbeSLen Brown double rapl_joule_counter_range; 873a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 88ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 893a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 903a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 918a5bdf41SLen Brown unsigned int crystal_hz; 928a5bdf41SLen Brown unsigned long long tsc_hz; 937ce7d5deSPrarit Bhargava int base_cpu; 9421ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 957f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 967f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 977f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 987f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 997f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1007f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 10133148d67SLen Brown unsigned int has_misc_feature_control; 1024c2122d4SLen Brown unsigned int first_counter_read = 1; 103c026c236SArtem Bityutskiy int ignore_stdin; 104889facbeSLen Brown 105889facbeSLen Brown #define RAPL_PKG (1 << 0) 106e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 107e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 108e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 109e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 110e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 111e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 112e6f9bb3cSLen Brown 113889facbeSLen Brown #define RAPL_DRAM (1 << 3) 114e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 115e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 116e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 117e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1180b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1190b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 120e6f9bb3cSLen Brown 1219148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 122e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1230b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 124e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 125e6f9bb3cSLen Brown 1260b2bb692SLen Brown #define RAPL_GFX (1 << 8) 127e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 128e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 129e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1309148494cSJacob Pan 1319148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1329148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1339392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1349392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1359392bd98SCalvin Walton * not per-package. */ 1369392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1379392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1389392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1399392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1409148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 141889facbeSLen Brown #define TJMAX_DEFAULT 100 142889facbeSLen Brown 1439392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1449392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1459392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1469392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1479392bd98SCalvin Walton 148889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 149103a8feaSLen Brown 150388e9c81SLen Brown /* 151388e9c81SLen Brown * buffer size used by sscanf() for added column names 152388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 153388e9c81SLen Brown */ 154388e9c81SLen Brown #define NAME_BYTES 20 155495c7654SLen Brown #define PATH_BYTES 128 156388e9c81SLen Brown 157103a8feaSLen Brown int backwards_count; 158103a8feaSLen Brown char *progname; 159103a8feaSLen Brown 1601ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1611ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1621ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1630748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1640748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1650e2d8f05SLen Brown #define BITMASK_SIZE 32 166c98d5d94SLen Brown 167c98d5d94SLen Brown struct thread_data { 168f4fdf2b4SLen Brown struct timeval tv_begin; 169f4fdf2b4SLen Brown struct timeval tv_end; 170d4794f25SYazen Ghannam struct timeval tv_delta; 171c98d5d94SLen Brown unsigned long long tsc; 172c98d5d94SLen Brown unsigned long long aperf; 173c98d5d94SLen Brown unsigned long long mperf; 174144b44b1SLen Brown unsigned long long c1; 1750de6c0dfSLen Brown unsigned long long irq_count; 1761ed51011SLen Brown unsigned int smi_count; 177c98d5d94SLen Brown unsigned int cpu_id; 1784c2122d4SLen Brown unsigned int apic_id; 1794c2122d4SLen Brown unsigned int x2apic_id; 180c98d5d94SLen Brown unsigned int flags; 181c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 182c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1830748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 184c98d5d94SLen Brown } *thread_even, *thread_odd; 185c98d5d94SLen Brown 186c98d5d94SLen Brown struct core_data { 187c98d5d94SLen Brown unsigned long long c3; 188c98d5d94SLen Brown unsigned long long c6; 189c98d5d94SLen Brown unsigned long long c7; 1900539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 191889facbeSLen Brown unsigned int core_temp_c; 1929392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 193c98d5d94SLen Brown unsigned int core_id; 194678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 195c98d5d94SLen Brown } *core_even, *core_odd; 196c98d5d94SLen Brown 197c98d5d94SLen Brown struct pkg_data { 198c98d5d94SLen Brown unsigned long long pc2; 199c98d5d94SLen Brown unsigned long long pc3; 200c98d5d94SLen Brown unsigned long long pc6; 201c98d5d94SLen Brown unsigned long long pc7; 202ca58710fSKristen Carlson Accardi unsigned long long pc8; 203ca58710fSKristen Carlson Accardi unsigned long long pc9; 204ca58710fSKristen Carlson Accardi unsigned long long pc10; 205be0e54c4SLen Brown unsigned long long cpu_lpi; 206be0e54c4SLen Brown unsigned long long sys_lpi; 2070b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2080b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2090b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2100b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2119185e988SLen Brown long long gfx_rc6_ms; 21227d47356SLen Brown unsigned int gfx_mhz; 213c98d5d94SLen Brown unsigned int package_id; 2147c2ccc50SChen Yu unsigned long long energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 2157c2ccc50SChen Yu unsigned long long energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 2167c2ccc50SChen Yu unsigned long long energy_cores; /* MSR_PP0_ENERGY_STATUS */ 2177c2ccc50SChen Yu unsigned long long energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 2187c2ccc50SChen Yu unsigned long long rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 2197c2ccc50SChen Yu unsigned long long rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 220889facbeSLen Brown unsigned int pkg_temp_c; 221678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 222c98d5d94SLen Brown } *package_even, *package_odd; 223c98d5d94SLen Brown 224c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 225c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 226c98d5d94SLen Brown 22740f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 22840f5cfe7SPrarit Bhargava ((thread_base) + \ 22940f5cfe7SPrarit Bhargava ((pkg_no) * \ 23040f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 23140f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 23240f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 23340f5cfe7SPrarit Bhargava (thread_no)) 23440f5cfe7SPrarit Bhargava 23540f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 23640f5cfe7SPrarit Bhargava ((core_base) + \ 23740f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 23840f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 23940f5cfe7SPrarit Bhargava (core_no)) 24040f5cfe7SPrarit Bhargava 24140f5cfe7SPrarit Bhargava 242c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 243c98d5d94SLen Brown 244388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE}; 24541618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC}; 246388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT}; 247388e9c81SLen Brown 248388e9c81SLen Brown struct msr_counter { 249388e9c81SLen Brown unsigned int msr_num; 250388e9c81SLen Brown char name[NAME_BYTES]; 251495c7654SLen Brown char path[PATH_BYTES]; 252388e9c81SLen Brown unsigned int width; 253388e9c81SLen Brown enum counter_type type; 254388e9c81SLen Brown enum counter_format format; 255388e9c81SLen Brown struct msr_counter *next; 256812db3f7SLen Brown unsigned int flags; 257812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 258812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 25941618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 260388e9c81SLen Brown }; 261388e9c81SLen Brown 26287e15da9SChen Yu /* 26387e15da9SChen Yu * The accumulated sum of MSR is defined as a monotonic 26487e15da9SChen Yu * increasing MSR, it will be accumulated periodically, 26587e15da9SChen Yu * despite its register's bit width. 26687e15da9SChen Yu */ 26787e15da9SChen Yu enum { 26887e15da9SChen Yu IDX_PKG_ENERGY, 26987e15da9SChen Yu IDX_DRAM_ENERGY, 27087e15da9SChen Yu IDX_PP0_ENERGY, 27187e15da9SChen Yu IDX_PP1_ENERGY, 27287e15da9SChen Yu IDX_PKG_PERF, 27387e15da9SChen Yu IDX_DRAM_PERF, 27487e15da9SChen Yu IDX_COUNT, 27587e15da9SChen Yu }; 27687e15da9SChen Yu 27787e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr); 27887e15da9SChen Yu 27987e15da9SChen Yu struct msr_sum_array { 28087e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 28187e15da9SChen Yu struct { 28287e15da9SChen Yu /*The accumulated MSR value is updated by the timer*/ 28387e15da9SChen Yu unsigned long long sum; 28487e15da9SChen Yu /*The MSR footprint recorded in last timer*/ 28587e15da9SChen Yu unsigned long long last; 28687e15da9SChen Yu } entries[IDX_COUNT]; 28787e15da9SChen Yu }; 28887e15da9SChen Yu 28987e15da9SChen Yu /* The percpu MSR sum array.*/ 29087e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum; 29187e15da9SChen Yu 29287e15da9SChen Yu int idx_to_offset(int idx) 29387e15da9SChen Yu { 29487e15da9SChen Yu int offset; 29587e15da9SChen Yu 29687e15da9SChen Yu switch (idx) { 29787e15da9SChen Yu case IDX_PKG_ENERGY: 29887e15da9SChen Yu offset = MSR_PKG_ENERGY_STATUS; 29987e15da9SChen Yu break; 30087e15da9SChen Yu case IDX_DRAM_ENERGY: 30187e15da9SChen Yu offset = MSR_DRAM_ENERGY_STATUS; 30287e15da9SChen Yu break; 30387e15da9SChen Yu case IDX_PP0_ENERGY: 30487e15da9SChen Yu offset = MSR_PP0_ENERGY_STATUS; 30587e15da9SChen Yu break; 30687e15da9SChen Yu case IDX_PP1_ENERGY: 30787e15da9SChen Yu offset = MSR_PP1_ENERGY_STATUS; 30887e15da9SChen Yu break; 30987e15da9SChen Yu case IDX_PKG_PERF: 31087e15da9SChen Yu offset = MSR_PKG_PERF_STATUS; 31187e15da9SChen Yu break; 31287e15da9SChen Yu case IDX_DRAM_PERF: 31387e15da9SChen Yu offset = MSR_DRAM_PERF_STATUS; 31487e15da9SChen Yu break; 31587e15da9SChen Yu default: 31687e15da9SChen Yu offset = -1; 31787e15da9SChen Yu } 31887e15da9SChen Yu return offset; 31987e15da9SChen Yu } 32087e15da9SChen Yu 32187e15da9SChen Yu int offset_to_idx(int offset) 32287e15da9SChen Yu { 32387e15da9SChen Yu int idx; 32487e15da9SChen Yu 32587e15da9SChen Yu switch (offset) { 32687e15da9SChen Yu case MSR_PKG_ENERGY_STATUS: 32787e15da9SChen Yu idx = IDX_PKG_ENERGY; 32887e15da9SChen Yu break; 32987e15da9SChen Yu case MSR_DRAM_ENERGY_STATUS: 33087e15da9SChen Yu idx = IDX_DRAM_ENERGY; 33187e15da9SChen Yu break; 33287e15da9SChen Yu case MSR_PP0_ENERGY_STATUS: 33387e15da9SChen Yu idx = IDX_PP0_ENERGY; 33487e15da9SChen Yu break; 33587e15da9SChen Yu case MSR_PP1_ENERGY_STATUS: 33687e15da9SChen Yu idx = IDX_PP1_ENERGY; 33787e15da9SChen Yu break; 33887e15da9SChen Yu case MSR_PKG_PERF_STATUS: 33987e15da9SChen Yu idx = IDX_PKG_PERF; 34087e15da9SChen Yu break; 34187e15da9SChen Yu case MSR_DRAM_PERF_STATUS: 34287e15da9SChen Yu idx = IDX_DRAM_PERF; 34387e15da9SChen Yu break; 34487e15da9SChen Yu default: 34587e15da9SChen Yu idx = -1; 34687e15da9SChen Yu } 34787e15da9SChen Yu return idx; 34887e15da9SChen Yu } 34987e15da9SChen Yu 35087e15da9SChen Yu int idx_valid(int idx) 35187e15da9SChen Yu { 35287e15da9SChen Yu switch (idx) { 35387e15da9SChen Yu case IDX_PKG_ENERGY: 35487e15da9SChen Yu return do_rapl & RAPL_PKG; 35587e15da9SChen Yu case IDX_DRAM_ENERGY: 35687e15da9SChen Yu return do_rapl & RAPL_DRAM; 35787e15da9SChen Yu case IDX_PP0_ENERGY: 35887e15da9SChen Yu return do_rapl & RAPL_CORES_ENERGY_STATUS; 35987e15da9SChen Yu case IDX_PP1_ENERGY: 36087e15da9SChen Yu return do_rapl & RAPL_GFX; 36187e15da9SChen Yu case IDX_PKG_PERF: 36287e15da9SChen Yu return do_rapl & RAPL_PKG_PERF_STATUS; 36387e15da9SChen Yu case IDX_DRAM_PERF: 36487e15da9SChen Yu return do_rapl & RAPL_DRAM_PERF_STATUS; 36587e15da9SChen Yu default: 36687e15da9SChen Yu return 0; 36787e15da9SChen Yu } 36887e15da9SChen Yu } 369388e9c81SLen Brown struct sys_counters { 370678a3bd1SLen Brown unsigned int added_thread_counters; 371678a3bd1SLen Brown unsigned int added_core_counters; 372678a3bd1SLen Brown unsigned int added_package_counters; 373388e9c81SLen Brown struct msr_counter *tp; 374388e9c81SLen Brown struct msr_counter *cp; 375388e9c81SLen Brown struct msr_counter *pp; 376388e9c81SLen Brown } sys; 377388e9c81SLen Brown 378c98d5d94SLen Brown struct system_summary { 379c98d5d94SLen Brown struct thread_data threads; 380c98d5d94SLen Brown struct core_data cores; 381c98d5d94SLen Brown struct pkg_data packages; 382388e9c81SLen Brown } average; 383c98d5d94SLen Brown 3840e2d8f05SLen Brown struct cpu_topology { 3850e2d8f05SLen Brown int physical_package_id; 3866de68fe1SLen Brown int die_id; 3870e2d8f05SLen Brown int logical_cpu_id; 388ef605741SPrarit Bhargava int physical_node_id; 389ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 3900e2d8f05SLen Brown int physical_core_id; 3918cb48b32SPrarit Bhargava int thread_id; 3920e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 3930e2d8f05SLen Brown } *cpus; 394c98d5d94SLen Brown 395c98d5d94SLen Brown struct topo_params { 396c98d5d94SLen Brown int num_packages; 3976de68fe1SLen Brown int num_die; 398103a8feaSLen Brown int num_cpus; 399c98d5d94SLen Brown int num_cores; 400c98d5d94SLen Brown int max_cpu_num; 401ef605741SPrarit Bhargava int max_node_num; 40270a9c6e8SPrarit Bhargava int nodes_per_pkg; 40370a9c6e8SPrarit Bhargava int cores_per_node; 40470a9c6e8SPrarit Bhargava int threads_per_core; 405c98d5d94SLen Brown } topo; 406103a8feaSLen Brown 407c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 408103a8feaSLen Brown 409562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 410562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 411562a2d37SLen Brown 412c98d5d94SLen Brown void setup_all_buffers(void); 413103a8feaSLen Brown 4141f81c5efSLen Brown char *sys_lpi_file; 4151f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us"; 4161f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec"; 4171f81c5efSLen Brown 418c98d5d94SLen Brown int cpu_is_not_present(int cpu) 419d15cf7c1SLen Brown { 420c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 421c98d5d94SLen Brown } 422c98d5d94SLen Brown /* 423c98d5d94SLen Brown * run func(thread, core, package) in topology order 424c98d5d94SLen Brown * skip non-present cpus 425c98d5d94SLen Brown */ 426c98d5d94SLen Brown 427c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 428c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 429c98d5d94SLen Brown { 43040f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 431c98d5d94SLen Brown 432c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 433df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 43470a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 435c98d5d94SLen Brown for (thread_no = 0; thread_no < 43670a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 437c98d5d94SLen Brown struct thread_data *t; 438c98d5d94SLen Brown struct core_data *c; 439c98d5d94SLen Brown struct pkg_data *p; 440c98d5d94SLen Brown 44140f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 44240f5cfe7SPrarit Bhargava core_no, node_no, 44340f5cfe7SPrarit Bhargava pkg_no); 444c98d5d94SLen Brown 445c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 446c98d5d94SLen Brown continue; 447c98d5d94SLen Brown 44840f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 44940f5cfe7SPrarit Bhargava node_no, pkg_no); 450c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 451c98d5d94SLen Brown 452c98d5d94SLen Brown retval = func(t, c, p); 453c98d5d94SLen Brown if (retval) 454c98d5d94SLen Brown return retval; 455c98d5d94SLen Brown } 456c98d5d94SLen Brown } 457c98d5d94SLen Brown } 45840f5cfe7SPrarit Bhargava } 459d15cf7c1SLen Brown return 0; 460d15cf7c1SLen Brown } 461d15cf7c1SLen Brown 46288c3281fSLen Brown int cpu_migrate(int cpu) 46388c3281fSLen Brown { 464c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 465c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 466c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 46788c3281fSLen Brown return -1; 46888c3281fSLen Brown else 46988c3281fSLen Brown return 0; 47088c3281fSLen Brown } 47136229897SLen Brown int get_msr_fd(int cpu) 472103a8feaSLen Brown { 473103a8feaSLen Brown char pathname[32]; 474103a8feaSLen Brown int fd; 475103a8feaSLen Brown 47636229897SLen Brown fd = fd_percpu[cpu]; 47736229897SLen Brown 47836229897SLen Brown if (fd) 47936229897SLen Brown return fd; 48036229897SLen Brown 481103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 482103a8feaSLen Brown fd = open(pathname, O_RDONLY); 48315aaa346SLen Brown if (fd < 0) 48498481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 485103a8feaSLen Brown 48636229897SLen Brown fd_percpu[cpu] = fd; 48736229897SLen Brown 48836229897SLen Brown return fd; 48936229897SLen Brown } 49036229897SLen Brown 49136229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 49236229897SLen Brown { 49336229897SLen Brown ssize_t retval; 49436229897SLen Brown 49536229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 49615aaa346SLen Brown 49798481e79SLen Brown if (retval != sizeof *msr) 498cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 49915aaa346SLen Brown 50015aaa346SLen Brown return 0; 501103a8feaSLen Brown } 502103a8feaSLen Brown 503fc04cc67SLen Brown /* 504bdd5ae3aSLen Brown * This list matches the column headers, except 505bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 506bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 507bdd5ae3aSLen Brown * matching on them for --show and --hide. 508fc04cc67SLen Brown */ 509812db3f7SLen Brown struct msr_counter bic[] = { 5103f44a5c6SLen Brown { 0x0, "usec" }, 5113f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 512812db3f7SLen Brown { 0x0, "Package" }, 513bdd5ae3aSLen Brown { 0x0, "Node" }, 514812db3f7SLen Brown { 0x0, "Avg_MHz" }, 515bdd5ae3aSLen Brown { 0x0, "Busy%" }, 516812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 517812db3f7SLen Brown { 0x0, "TSC_MHz" }, 518812db3f7SLen Brown { 0x0, "IRQ" }, 519495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 520bdd5ae3aSLen Brown { 0x0, "sysfs" }, 521812db3f7SLen Brown { 0x0, "CPU%c1" }, 522812db3f7SLen Brown { 0x0, "CPU%c3" }, 523812db3f7SLen Brown { 0x0, "CPU%c6" }, 524812db3f7SLen Brown { 0x0, "CPU%c7" }, 525812db3f7SLen Brown { 0x0, "ThreadC" }, 526812db3f7SLen Brown { 0x0, "CoreTmp" }, 527812db3f7SLen Brown { 0x0, "CoreCnt" }, 528812db3f7SLen Brown { 0x0, "PkgTmp" }, 529812db3f7SLen Brown { 0x0, "GFX%rc6" }, 530812db3f7SLen Brown { 0x0, "GFXMHz" }, 531812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 532812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 533812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 534812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 5350f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 5360f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 5374bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 538be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 539be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 540812db3f7SLen Brown { 0x0, "PkgWatt" }, 541812db3f7SLen Brown { 0x0, "CorWatt" }, 542812db3f7SLen Brown { 0x0, "GFXWatt" }, 543812db3f7SLen Brown { 0x0, "PkgCnt" }, 544812db3f7SLen Brown { 0x0, "RAMWatt" }, 545812db3f7SLen Brown { 0x0, "PKG_%" }, 546812db3f7SLen Brown { 0x0, "RAM_%" }, 547812db3f7SLen Brown { 0x0, "Pkg_J" }, 548812db3f7SLen Brown { 0x0, "Cor_J" }, 549812db3f7SLen Brown { 0x0, "GFX_J" }, 550812db3f7SLen Brown { 0x0, "RAM_J" }, 5510539ba11SLen Brown { 0x0, "Mod%c6" }, 552a99d8730SLen Brown { 0x0, "Totl%C0" }, 553a99d8730SLen Brown { 0x0, "Any%C0" }, 554a99d8730SLen Brown { 0x0, "GFX%C0" }, 555a99d8730SLen Brown { 0x0, "CPUGFX%" }, 556bdd5ae3aSLen Brown { 0x0, "Core" }, 557bdd5ae3aSLen Brown { 0x0, "CPU" }, 5584c2122d4SLen Brown { 0x0, "APIC" }, 5594c2122d4SLen Brown { 0x0, "X2APIC" }, 5606de68fe1SLen Brown { 0x0, "Die" }, 561812db3f7SLen Brown }; 562812db3f7SLen Brown 563812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 5643f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 5653f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 5663f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 567bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 568bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 569bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 570bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 571bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 572bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 573bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 574bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 575bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 576bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 577bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 578bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 579bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 580bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 581bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 582bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 583bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 584bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 585bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 586bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 587bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 588bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 589bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 590bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 591bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 592bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 593bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 594bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 595bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 596bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 597bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 598bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 599bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 600bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 601bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 602bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 603bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 604bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 605bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 606bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 607bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 608bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 609bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 610bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 611bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 6124c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 6134c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 6146de68fe1SLen Brown #define BIC_Die (1ULL << 50) 615812db3f7SLen Brown 6164c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 6173f44a5c6SLen Brown 6183f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 6194c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 620812db3f7SLen Brown 621812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 6221e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 6233f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 624812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 6250f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 626812db3f7SLen Brown 6273f44a5c6SLen Brown 628dd778a5eSLen Brown #define MAX_DEFERRED 16 629dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 630dd778a5eSLen Brown int deferred_skip_index; 631dd778a5eSLen Brown 632dd778a5eSLen Brown /* 633dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 634dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 635dd778a5eSLen Brown */ 636dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 637dd778a5eSLen Brown 638dd778a5eSLen Brown void help(void) 639dd778a5eSLen Brown { 640dd778a5eSLen Brown fprintf(outf, 641dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 642dd778a5eSLen Brown "\n" 643dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 644dd778a5eSLen Brown "when COMMAND completes.\n" 645dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 646dd778a5eSLen Brown "to print statistics, until interrupted.\n" 647cc481650SNathan Ciobanu " -a, --add add a counter\n" 648dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 649cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 650dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 6519ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 6529ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 6539ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 6549ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 6559ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 6569ce80578SNathan Ciobanu " hide the specified column(s)\n" 657cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 658cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 6599ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 660cc481650SNathan Ciobanu " -l, --list list column headers only\n" 661cc481650SNathan Ciobanu " -n, --num_iterations num\n" 662cc481650SNathan Ciobanu " number of the measurement iterations\n" 663cc481650SNathan Ciobanu " -o, --out file\n" 664cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 665cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 6669ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 6679ce80578SNathan Ciobanu " show only the specified column(s)\n" 6689ce80578SNathan Ciobanu " -S, --Summary\n" 6699ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 6709ce80578SNathan Ciobanu " -T, --TCC temperature\n" 6719ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 6729ce80578SNathan Ciobanu " degrees Celsius\n" 673cc481650SNathan Ciobanu " -h, --help print this help message\n" 674cc481650SNathan Ciobanu " -v, --version print version information\n" 675dd778a5eSLen Brown "\n" 676dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 677dd778a5eSLen Brown } 678dd778a5eSLen Brown 679812db3f7SLen Brown /* 680812db3f7SLen Brown * bic_lookup 681812db3f7SLen Brown * for all the strings in comma separate name_list, 682812db3f7SLen Brown * set the approprate bit in return value. 683812db3f7SLen Brown */ 684dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 685812db3f7SLen Brown { 686812db3f7SLen Brown int i; 687812db3f7SLen Brown unsigned long long retval = 0; 688812db3f7SLen Brown 689812db3f7SLen Brown while (name_list) { 690812db3f7SLen Brown char *comma; 691812db3f7SLen Brown 692812db3f7SLen Brown comma = strchr(name_list, ','); 693812db3f7SLen Brown 694812db3f7SLen Brown if (comma) 695812db3f7SLen Brown *comma = '\0'; 696812db3f7SLen Brown 6973f44a5c6SLen Brown if (!strcmp(name_list, "all")) 6983f44a5c6SLen Brown return ~0; 6993f44a5c6SLen Brown 700812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 701812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 702812db3f7SLen Brown retval |= (1ULL << i); 703812db3f7SLen Brown break; 704812db3f7SLen Brown } 705812db3f7SLen Brown } 706812db3f7SLen Brown if (i == MAX_BIC) { 707dd778a5eSLen Brown if (mode == SHOW_LIST) { 708812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 709812db3f7SLen Brown exit(-1); 710812db3f7SLen Brown } 711dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 712dd778a5eSLen Brown if (debug) 713dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 714dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 715dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 716dd778a5eSLen Brown MAX_DEFERRED, name_list); 717dd778a5eSLen Brown help(); 718dd778a5eSLen Brown exit(1); 719dd778a5eSLen Brown } 720dd778a5eSLen Brown } 721812db3f7SLen Brown 722812db3f7SLen Brown name_list = comma; 723812db3f7SLen Brown if (name_list) 724812db3f7SLen Brown name_list++; 725812db3f7SLen Brown 726812db3f7SLen Brown } 727812db3f7SLen Brown return retval; 728812db3f7SLen Brown } 729fc04cc67SLen Brown 730dd778a5eSLen Brown 731c8ade361SLen Brown void print_header(char *delim) 732103a8feaSLen Brown { 733388e9c81SLen Brown struct msr_counter *mp; 7346168c2e0SLen Brown int printed = 0; 735388e9c81SLen Brown 7363f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 7373f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 7383f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 7393f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 740812db3f7SLen Brown if (DO_BIC(BIC_Package)) 7416168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 7426de68fe1SLen Brown if (DO_BIC(BIC_Die)) 7436de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 74401235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 74501235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 746812db3f7SLen Brown if (DO_BIC(BIC_Core)) 7476168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 748812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 7496168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 7504c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 7514c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 7524c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 7534c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 754812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 7556168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 756812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 7576168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 758812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 7596168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 760812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 7616168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 7621cc21f7bSLen Brown 7630de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 7640de6c0dfSLen Brown if (sums_need_wide_columns) 7656168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 7660de6c0dfSLen Brown else 7676168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 7680de6c0dfSLen Brown } 7690de6c0dfSLen Brown 770812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 7716168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 7721cc21f7bSLen Brown 773388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 774dd778a5eSLen Brown 775388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 776388e9c81SLen Brown if (mp->width == 64) 777dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 778388e9c81SLen Brown else 779dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 780388e9c81SLen Brown } else { 7810de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 782dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 7830de6c0dfSLen Brown else 784dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 785388e9c81SLen Brown } 786388e9c81SLen Brown } 787388e9c81SLen Brown 78841618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 7896168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 790562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 7916168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 792812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 7936168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 794812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 7956168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 796678a3bd1SLen Brown 7970539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 7986168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 799678a3bd1SLen Brown 800812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8016168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 802388e9c81SLen Brown 8039392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 8049392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8059392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 8069392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 8079392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 8089392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 8099392bd98SCalvin Walton } 8109392bd98SCalvin Walton 811388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 812388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 813388e9c81SLen Brown if (mp->width == 64) 814c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 815388e9c81SLen Brown else 816c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 817388e9c81SLen Brown } else { 8180de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8190de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 8200de6c0dfSLen Brown else 821c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 822388e9c81SLen Brown } 823388e9c81SLen Brown } 824388e9c81SLen Brown 825812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 8266168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 827889facbeSLen Brown 828812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 8296168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 830fdf676e5SLen Brown 831812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 8326168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 83327d47356SLen Brown 834a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 8356168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 836a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 8376168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 838a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 8396168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 840a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 8416168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 8420b2bb692SLen Brown 8430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 8446168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 8450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 8466168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 8470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 8486168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 8490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 8506168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 8510f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 8526168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 8530f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 8546168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 8550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 8566168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 857be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 858be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 859be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 860be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 861103a8feaSLen Brown 8625c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 863812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 8646168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 8659392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 8666168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 867812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 8686168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 869812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 8706168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 871812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 8726168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 873812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 8746168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 875d7899447SLen Brown } else if (do_rapl && rapl_joules) { 876812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 8776168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 8789392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 8796168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 880812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 8816168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 882812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 8836168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 884812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 8856168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 886812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 8876168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 8885c56be9aSDirk Brandewie } 889388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 890388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 891388e9c81SLen Brown if (mp->width == 64) 892c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 893388e9c81SLen Brown else 894c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 895388e9c81SLen Brown } else { 8960de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8970de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 8980de6c0dfSLen Brown else 899c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 900388e9c81SLen Brown } 901388e9c81SLen Brown } 902388e9c81SLen Brown 903c98d5d94SLen Brown outp += sprintf(outp, "\n"); 904103a8feaSLen Brown } 905103a8feaSLen Brown 906c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 907c98d5d94SLen Brown struct pkg_data *p) 908103a8feaSLen Brown { 909388e9c81SLen Brown int i; 910388e9c81SLen Brown struct msr_counter *mp; 911388e9c81SLen Brown 9123b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 913c98d5d94SLen Brown 914c98d5d94SLen Brown if (t) { 9153b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 9163b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 9173b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 9183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 9193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 9203b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 9216886fee4SLen Brown 922812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 9230de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 924812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 925218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 926388e9c81SLen Brown 927388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 928388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 929388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 930388e9c81SLen Brown } 931103a8feaSLen Brown } 932103a8feaSLen Brown 933c98d5d94SLen Brown if (c) { 9343b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 9353b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 9363b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 9373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 9383b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 9399392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 940388e9c81SLen Brown 941388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 942388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 943388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 944388e9c81SLen Brown } 9450539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 946c98d5d94SLen Brown } 947103a8feaSLen Brown 948c98d5d94SLen Brown if (p) { 9493b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 9500b2bb692SLen Brown 9510b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 9520b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 9530b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 9540b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 9550b2bb692SLen Brown 9563b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 9570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9583b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 9590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 9610f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 9633b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 9643b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 9653b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 966be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 967be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 9687c2ccc50SChen Yu outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg); 9697c2ccc50SChen Yu outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores); 9707c2ccc50SChen Yu outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx); 9717c2ccc50SChen Yu outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram); 9727c2ccc50SChen Yu outp += sprintf(outp, "Throttle PKG: %0llX\n", 9733b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 9747c2ccc50SChen Yu outp += sprintf(outp, "Throttle RAM: %0llX\n", 9753b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 9763b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 977388e9c81SLen Brown 978388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 979388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 980388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 981388e9c81SLen Brown } 982c98d5d94SLen Brown } 9833b4d5c7fSAndy Shevchenko 9843b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 9853b4d5c7fSAndy Shevchenko 986c98d5d94SLen Brown return 0; 987103a8feaSLen Brown } 988103a8feaSLen Brown 989e23da037SLen Brown /* 990e23da037SLen Brown * column formatting convention & formats 991e23da037SLen Brown */ 992c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 993c98d5d94SLen Brown struct pkg_data *p) 994103a8feaSLen Brown { 995008d396eSLen Brown double interval_float, tsc; 996fc04cc67SLen Brown char *fmt8; 997388e9c81SLen Brown int i; 998388e9c81SLen Brown struct msr_counter *mp; 9996168c2e0SLen Brown char *delim = "\t"; 10006168c2e0SLen Brown int printed = 0; 1001103a8feaSLen Brown 1002c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 1003c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1004c98d5d94SLen Brown return 0; 1005c98d5d94SLen Brown 1006c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 1007c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1008c98d5d94SLen Brown return 0; 1009c98d5d94SLen Brown 10101ef7d21aSLen Brown /*if not summary line and --cpu is used */ 10111ef7d21aSLen Brown if ((t != &average.threads) && 10121ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 10131ef7d21aSLen Brown return 0; 10141ef7d21aSLen Brown 10153f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 1016f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 1017f4fdf2b4SLen Brown struct timeval tv; 1018f4fdf2b4SLen Brown 1019f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 1020f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 1021f4fdf2b4SLen Brown } 1022f4fdf2b4SLen Brown 10233f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 10243f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 10253f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 10263f44a5c6SLen Brown 1027d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0; 1028103a8feaSLen Brown 1029008d396eSLen Brown tsc = t->tsc * tsc_tweak; 1030008d396eSLen Brown 1031c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 1032c98d5d94SLen Brown if (t == &average.threads) { 1033812db3f7SLen Brown if (DO_BIC(BIC_Package)) 10346168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10356de68fe1SLen Brown if (DO_BIC(BIC_Die)) 10366de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 103701235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 103801235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1039812db3f7SLen Brown if (DO_BIC(BIC_Core)) 10406168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1041812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 10426168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10434c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 10444c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10454c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 10464c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1047103a8feaSLen Brown } else { 1048812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 1049c98d5d94SLen Brown if (p) 10506168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 1051c98d5d94SLen Brown else 10526168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1053c98d5d94SLen Brown } 10546de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 10556de68fe1SLen Brown if (c) 10566de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 10576de68fe1SLen Brown else 10586de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 10596de68fe1SLen Brown } 106001235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 106101235041SPrarit Bhargava if (t) 106201235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 106301235041SPrarit Bhargava (printed++ ? delim : ""), 106401235041SPrarit Bhargava cpus[t->cpu_id].physical_node_id); 106501235041SPrarit Bhargava else 106601235041SPrarit Bhargava outp += sprintf(outp, "%s-", 106701235041SPrarit Bhargava (printed++ ? delim : "")); 106801235041SPrarit Bhargava } 1069812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 1070c98d5d94SLen Brown if (c) 10716168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 1072c98d5d94SLen Brown else 10736168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 1074c98d5d94SLen Brown } 1075812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 10766168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 10774c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 10784c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 10794c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 10804c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 1081103a8feaSLen Brown } 1082fc04cc67SLen Brown 1083812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 10846168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1085fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 1086fc04cc67SLen Brown 1087812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 10886168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 1089103a8feaSLen Brown 1090812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 109121ed5574SLen Brown if (has_base_hz) 10926168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 109321ed5574SLen Brown else 10946168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1095008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 109621ed5574SLen Brown } 1097103a8feaSLen Brown 1098812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 10996168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 1100103a8feaSLen Brown 1101562a2d37SLen Brown /* IRQ */ 11020de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 11030de6c0dfSLen Brown if (sums_need_wide_columns) 11046168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 11050de6c0dfSLen Brown else 11066168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 11070de6c0dfSLen Brown } 1108562a2d37SLen Brown 11091cc21f7bSLen Brown /* SMI */ 1110812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 11116168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 11121cc21f7bSLen Brown 1113678a3bd1SLen Brown /* Added counters */ 1114678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1115678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1116678a3bd1SLen Brown if (mp->width == 32) 11175f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 1118678a3bd1SLen Brown else 11196168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1120678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 11210de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11226168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 11230de6c0dfSLen Brown else 11246168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1125678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 112641618e63SLen Brown if (mp->type == COUNTER_USEC) 11276168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 112841618e63SLen Brown else 11296168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 1130678a3bd1SLen Brown } 1131678a3bd1SLen Brown } 1132678a3bd1SLen Brown 113341618e63SLen Brown /* C1 */ 113441618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 11356168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 113641618e63SLen Brown 113741618e63SLen Brown 1138c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1139c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1140c98d5d94SLen Brown goto done; 1141c98d5d94SLen Brown 1142562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 11436168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 1144812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 11456168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 1146812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 11476168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 1148c98d5d94SLen Brown 11490539ba11SLen Brown /* Mod%c6 */ 11500539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 11516168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 11520539ba11SLen Brown 1153812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 11546168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1155889facbeSLen Brown 1156388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1157388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1158388e9c81SLen Brown if (mp->width == 32) 11595f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 1160388e9c81SLen Brown else 11616168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1162388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 11630de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11646168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 11650de6c0dfSLen Brown else 11666168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1167388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 11686168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 1169388e9c81SLen Brown } 1170388e9c81SLen Brown } 1171388e9c81SLen Brown 11729392bd98SCalvin Walton fmt8 = "%s%.2f"; 11739392bd98SCalvin Walton 11749392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 11759392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 11769392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 11779392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 11789392bd98SCalvin Walton 1179c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1180c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1181c98d5d94SLen Brown goto done; 1182c98d5d94SLen Brown 11830b2bb692SLen Brown /* PkgTmp */ 1184812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 11856168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1186889facbeSLen Brown 1187fdf676e5SLen Brown /* GFXrc6 */ 1188812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1189ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 11906168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 11919185e988SLen Brown } else { 11926168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 11939185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 11949185e988SLen Brown } 11959185e988SLen Brown } 1196fdf676e5SLen Brown 119727d47356SLen Brown /* GFXMHz */ 1198812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 11996168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 120027d47356SLen Brown 12010b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1202a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12036168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 1204a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12056168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 1206a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12076168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 1208a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12096168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 12100b2bb692SLen Brown 12110f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 12126168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 12130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 12146168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 12150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 12166168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 12170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 12186168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 12190f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 12206168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 12210f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 12226168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 12230f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 12246168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 1225889facbeSLen Brown 1226be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1227be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1228be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1229be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1230be0e54c4SLen Brown 1231812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 12326168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 12339392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 12346168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1235812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 12366168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1237812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 12386168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 1239812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 12406168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 12419392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 12426168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1243812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 12446168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1245812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 12466168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1247812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 12486168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1249812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 12506168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1251812db3f7SLen Brown 1252388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1253388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1254388e9c81SLen Brown if (mp->width == 32) 12555f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]); 1256388e9c81SLen Brown else 12576168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1258388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 12590de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 12606168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 12610de6c0dfSLen Brown else 12626168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1263388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 12646168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc); 1265388e9c81SLen Brown } 1266388e9c81SLen Brown } 1267388e9c81SLen Brown 1268c98d5d94SLen Brown done: 126994d6ab4bSLen Brown if (*(outp - 1) != '\n') 1270c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1271c98d5d94SLen Brown 1272c98d5d94SLen Brown return 0; 1273103a8feaSLen Brown } 1274103a8feaSLen Brown 1275b7d8c148SLen Brown void flush_output_stdout(void) 1276103a8feaSLen Brown { 1277b7d8c148SLen Brown FILE *filep; 1278b7d8c148SLen Brown 1279b7d8c148SLen Brown if (outf == stderr) 1280b7d8c148SLen Brown filep = stdout; 1281b7d8c148SLen Brown else 1282b7d8c148SLen Brown filep = outf; 1283b7d8c148SLen Brown 1284b7d8c148SLen Brown fputs(output_buffer, filep); 1285b7d8c148SLen Brown fflush(filep); 1286b7d8c148SLen Brown 1287c98d5d94SLen Brown outp = output_buffer; 1288c98d5d94SLen Brown } 1289b7d8c148SLen Brown void flush_output_stderr(void) 1290c98d5d94SLen Brown { 1291b7d8c148SLen Brown fputs(output_buffer, outf); 1292b7d8c148SLen Brown fflush(outf); 1293c98d5d94SLen Brown outp = output_buffer; 1294c98d5d94SLen Brown } 1295c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1296c98d5d94SLen Brown { 1297e23da037SLen Brown static int printed; 1298103a8feaSLen Brown 1299e23da037SLen Brown if (!printed || !summary_only) 1300c8ade361SLen Brown print_header("\t"); 1301103a8feaSLen Brown 13029d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1303103a8feaSLen Brown 1304e23da037SLen Brown printed = 1; 1305e23da037SLen Brown 1306e23da037SLen Brown if (summary_only) 1307e23da037SLen Brown return; 1308e23da037SLen Brown 1309c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1310103a8feaSLen Brown } 1311103a8feaSLen Brown 1312889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 13137c2ccc50SChen Yu old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32); 1314889facbeSLen Brown 1315ba3dec99SLen Brown int 1316c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1317103a8feaSLen Brown { 1318388e9c81SLen Brown int i; 1319388e9c81SLen Brown struct msr_counter *mp; 13200b2bb692SLen Brown 1321a99d8730SLen Brown 1322a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 13230b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1324a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 13250b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1326a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 13270b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1328a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 13290b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1330a99d8730SLen Brown 1331c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 13320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1333c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 13340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1335c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 13360f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1337c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1338ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1339ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1340ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1341be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1342be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1343889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1344889facbeSLen Brown 13459185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 13469185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 13479185e988SLen Brown old->gfx_rc6_ms = -1; 13489185e988SLen Brown else 1349fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 13509185e988SLen Brown 135127d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 135227d47356SLen Brown 135387e15da9SChen Yu old->energy_pkg = new->energy_pkg - old->energy_pkg; 135487e15da9SChen Yu old->energy_cores = new->energy_cores - old->energy_cores; 135587e15da9SChen Yu old->energy_gfx = new->energy_gfx - old->energy_gfx; 135687e15da9SChen Yu old->energy_dram = new->energy_dram - old->energy_dram; 135787e15da9SChen Yu old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status; 135887e15da9SChen Yu old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status; 1359ba3dec99SLen Brown 1360388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1361388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1362388e9c81SLen Brown old->counter[i] = new->counter[i]; 1363388e9c81SLen Brown else 1364388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1365388e9c81SLen Brown } 1366388e9c81SLen Brown 1367ba3dec99SLen Brown return 0; 1368103a8feaSLen Brown } 1369103a8feaSLen Brown 1370c98d5d94SLen Brown void 1371c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1372c98d5d94SLen Brown { 1373388e9c81SLen Brown int i; 1374388e9c81SLen Brown struct msr_counter *mp; 1375388e9c81SLen Brown 1376c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1377c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1378c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1379889facbeSLen Brown old->core_temp_c = new->core_temp_c; 13800539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1381388e9c81SLen Brown 13829392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 13839392bd98SCalvin Walton 1384388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1385388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1386388e9c81SLen Brown old->counter[i] = new->counter[i]; 1387388e9c81SLen Brown else 1388388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1389388e9c81SLen Brown } 1390103a8feaSLen Brown } 1391c98d5d94SLen Brown 13921e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 13931e9042b9SSrinivas Pandruvada { 13941e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 13951e9042b9SSrinivas Pandruvada return 0; 13961e9042b9SSrinivas Pandruvada 13971e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 13981e9042b9SSrinivas Pandruvada } 13991e9042b9SSrinivas Pandruvada 1400c3ae331dSLen Brown /* 1401c3ae331dSLen Brown * old = new - old 1402c3ae331dSLen Brown */ 1403ba3dec99SLen Brown int 1404c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1405c98d5d94SLen Brown struct core_data *core_delta) 1406c98d5d94SLen Brown { 1407388e9c81SLen Brown int i; 1408388e9c81SLen Brown struct msr_counter *mp; 1409388e9c81SLen Brown 14104c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 14114c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14124c2122d4SLen Brown new->apic_id = old->apic_id; 14134c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14144c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 14154c2122d4SLen Brown 14163f44a5c6SLen Brown /* 14173f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 14183f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 14193f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 14203f44a5c6SLen Brown */ 14213f44a5c6SLen Brown 1422d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 14233f44a5c6SLen Brown old->tv_begin = new->tv_begin; 14243f44a5c6SLen Brown old->tv_end = new->tv_end; 14253f44a5c6SLen Brown 1426c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1427c98d5d94SLen Brown 1428103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1429b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1430b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1431b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1432b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1433103a8feaSLen Brown 1434c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1435c98d5d94SLen Brown 14361e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 14371e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 1438c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1439c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1440c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1441c98d5d94SLen Brown } else { 1442ba3dec99SLen Brown return -1; 1443103a8feaSLen Brown } 1444a729617cSLen Brown } 1445103a8feaSLen Brown 1446c98d5d94SLen Brown 1447144b44b1SLen Brown if (use_c1_residency_msr) { 1448144b44b1SLen Brown /* 1449144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1450144b44b1SLen Brown * which should be more accurate than the derivation below. 1451144b44b1SLen Brown */ 1452144b44b1SLen Brown } else { 1453103a8feaSLen Brown /* 1454c3ae331dSLen Brown * As counter collection is not atomic, 1455c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1456103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1457103a8feaSLen Brown */ 145895149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1459c98d5d94SLen Brown old->c1 = 0; 1460c98d5d94SLen Brown else { 1461c98d5d94SLen Brown /* normal case, derive c1 */ 1462008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1463c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1464c98d5d94SLen Brown } 1465144b44b1SLen Brown } 1466c3ae331dSLen Brown 1467c98d5d94SLen Brown if (old->mperf == 0) { 1468b7d8c148SLen Brown if (debug > 1) 1469b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1470c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1471c98d5d94SLen Brown } 1472103a8feaSLen Brown 1473812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1474562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1475562a2d37SLen Brown 1476812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 14771ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1478ba3dec99SLen Brown 1479388e9c81SLen Brown for (i = 0, mp = sys.tp; 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 } 1485ba3dec99SLen Brown return 0; 1486103a8feaSLen Brown } 1487c98d5d94SLen Brown 1488c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1489c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1490c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1491c98d5d94SLen Brown { 1492ba3dec99SLen Brown int retval = 0; 1493ba3dec99SLen Brown 1494c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1495c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1496c98d5d94SLen Brown delta_core(c, c2); 1497c98d5d94SLen Brown 1498c98d5d94SLen Brown /* always calculate thread delta */ 1499ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1500ba3dec99SLen Brown if (retval) 1501ba3dec99SLen Brown return retval; 1502c98d5d94SLen Brown 1503c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1504c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1505ba3dec99SLen Brown retval = delta_package(p, p2); 1506c98d5d94SLen Brown 1507ba3dec99SLen Brown return retval; 1508103a8feaSLen Brown } 1509103a8feaSLen Brown 1510c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1511103a8feaSLen Brown { 1512388e9c81SLen Brown int i; 1513388e9c81SLen Brown struct msr_counter *mp; 1514388e9c81SLen Brown 15153f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 15163f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 15173f44a5c6SLen Brown t->tv_end.tv_sec = 0; 15183f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1519d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1520d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 15213f44a5c6SLen Brown 1522c98d5d94SLen Brown t->tsc = 0; 1523c98d5d94SLen Brown t->aperf = 0; 1524c98d5d94SLen Brown t->mperf = 0; 1525c98d5d94SLen Brown t->c1 = 0; 1526103a8feaSLen Brown 1527562a2d37SLen Brown t->irq_count = 0; 1528562a2d37SLen Brown t->smi_count = 0; 1529562a2d37SLen Brown 1530c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1531c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1532c98d5d94SLen Brown 1533c98d5d94SLen Brown c->c3 = 0; 1534c98d5d94SLen Brown c->c6 = 0; 1535c98d5d94SLen Brown c->c7 = 0; 15360539ba11SLen Brown c->mc6_us = 0; 1537889facbeSLen Brown c->core_temp_c = 0; 15389392bd98SCalvin Walton c->core_energy = 0; 1539c98d5d94SLen Brown 15400b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 15410b2bb692SLen Brown p->pkg_any_core_c0 = 0; 15420b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 15430b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 15440b2bb692SLen Brown 1545c98d5d94SLen Brown p->pc2 = 0; 15460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1547c98d5d94SLen Brown p->pc3 = 0; 15480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1549c98d5d94SLen Brown p->pc6 = 0; 15500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1551c98d5d94SLen Brown p->pc7 = 0; 1552ca58710fSKristen Carlson Accardi p->pc8 = 0; 1553ca58710fSKristen Carlson Accardi p->pc9 = 0; 1554ca58710fSKristen Carlson Accardi p->pc10 = 0; 1555be0e54c4SLen Brown p->cpu_lpi = 0; 1556be0e54c4SLen Brown p->sys_lpi = 0; 1557889facbeSLen Brown 1558889facbeSLen Brown p->energy_pkg = 0; 1559889facbeSLen Brown p->energy_dram = 0; 1560889facbeSLen Brown p->energy_cores = 0; 1561889facbeSLen Brown p->energy_gfx = 0; 1562889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1563889facbeSLen Brown p->rapl_dram_perf_status = 0; 1564889facbeSLen Brown p->pkg_temp_c = 0; 156527d47356SLen Brown 1566fdf676e5SLen Brown p->gfx_rc6_ms = 0; 156727d47356SLen Brown p->gfx_mhz = 0; 1568388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1569388e9c81SLen Brown t->counter[i] = 0; 1570388e9c81SLen Brown 1571388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1572388e9c81SLen Brown c->counter[i] = 0; 1573388e9c81SLen Brown 1574388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1575388e9c81SLen Brown p->counter[i] = 0; 1576103a8feaSLen Brown } 1577c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1578c98d5d94SLen Brown struct pkg_data *p) 1579103a8feaSLen Brown { 1580388e9c81SLen Brown int i; 1581388e9c81SLen Brown struct msr_counter *mp; 1582388e9c81SLen Brown 15834c2122d4SLen Brown /* copy un-changing apic_id's */ 15844c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 15854c2122d4SLen Brown average.threads.apic_id = t->apic_id; 15864c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 15874c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 15884c2122d4SLen Brown 15893f44a5c6SLen Brown /* remember first tv_begin */ 15903f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 15913f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 15923f44a5c6SLen Brown 15933f44a5c6SLen Brown /* remember last tv_end */ 15943f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 15953f44a5c6SLen Brown 1596c98d5d94SLen Brown average.threads.tsc += t->tsc; 1597c98d5d94SLen Brown average.threads.aperf += t->aperf; 1598c98d5d94SLen Brown average.threads.mperf += t->mperf; 1599c98d5d94SLen Brown average.threads.c1 += t->c1; 160015aaa346SLen Brown 1601562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1602562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1603562a2d37SLen Brown 1604388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1605388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1606388e9c81SLen Brown continue; 1607388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1608388e9c81SLen Brown } 1609388e9c81SLen Brown 1610c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1611c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1612c98d5d94SLen Brown return 0; 1613c98d5d94SLen Brown 1614c98d5d94SLen Brown average.cores.c3 += c->c3; 1615c98d5d94SLen Brown average.cores.c6 += c->c6; 1616c98d5d94SLen Brown average.cores.c7 += c->c7; 16170539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1618c98d5d94SLen Brown 1619889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1620889facbeSLen Brown 16219392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 16229392bd98SCalvin Walton 1623388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1624388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1625388e9c81SLen Brown continue; 1626388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1627388e9c81SLen Brown } 1628388e9c81SLen Brown 1629c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1630c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1631c98d5d94SLen Brown return 0; 1632c98d5d94SLen Brown 1633a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 16340b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1635a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 16360b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1637a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 16380b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1639a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 16400b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 16410b2bb692SLen Brown 1642c98d5d94SLen Brown average.packages.pc2 += p->pc2; 16430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1644c98d5d94SLen Brown average.packages.pc3 += p->pc3; 16450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1646c98d5d94SLen Brown average.packages.pc6 += p->pc6; 16470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1648c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1649ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1650ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1651ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1652c98d5d94SLen Brown 1653be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1654be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1655be0e54c4SLen Brown 1656889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1657889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1658889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1659889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1660889facbeSLen Brown 1661fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 166227d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 166327d47356SLen Brown 1664889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1665889facbeSLen Brown 1666889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1667889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1668388e9c81SLen Brown 1669388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1670388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1671388e9c81SLen Brown continue; 1672388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1673388e9c81SLen Brown } 1674c98d5d94SLen Brown return 0; 1675c98d5d94SLen Brown } 1676c98d5d94SLen Brown /* 1677c98d5d94SLen Brown * sum the counters for all cpus in the system 1678c98d5d94SLen Brown * compute the weighted average 1679c98d5d94SLen Brown */ 1680c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1681c98d5d94SLen Brown struct pkg_data *p) 1682c98d5d94SLen Brown { 1683388e9c81SLen Brown int i; 1684388e9c81SLen Brown struct msr_counter *mp; 1685388e9c81SLen Brown 1686c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1687c98d5d94SLen Brown 1688c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1689c98d5d94SLen Brown 1690d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1691d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1692d4794f25SYazen Ghannam 1693c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1694c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1695c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1696c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1697c98d5d94SLen Brown 16980de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 16990de6c0dfSLen Brown sums_need_wide_columns = 1; 17000de6c0dfSLen Brown 1701c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1702c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1703c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 17040539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1705c98d5d94SLen Brown 1706a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 17070b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1708a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 17090b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1710a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 17110b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1712a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 17130b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 17140b2bb692SLen Brown 1715c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 17160f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1717c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 17180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1719c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 17200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1721c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1722ca58710fSKristen Carlson Accardi 1723ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1724ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1725ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1726388e9c81SLen Brown 1727388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1728388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1729388e9c81SLen Brown continue; 17300de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 17310de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 17320de6c0dfSLen Brown sums_need_wide_columns = 1; 173341618e63SLen Brown continue; 17340de6c0dfSLen Brown } 1735388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1736388e9c81SLen Brown } 1737388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1738388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1739388e9c81SLen Brown continue; 17400de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 17410de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 17420de6c0dfSLen Brown sums_need_wide_columns = 1; 17430de6c0dfSLen Brown } 1744388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1745388e9c81SLen Brown } 1746388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1747388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1748388e9c81SLen Brown continue; 17490de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 17500de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 17510de6c0dfSLen Brown sums_need_wide_columns = 1; 17520de6c0dfSLen Brown } 1753388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1754388e9c81SLen Brown } 1755c98d5d94SLen Brown } 1756c98d5d94SLen Brown 1757c98d5d94SLen Brown static unsigned long long rdtsc(void) 1758c98d5d94SLen Brown { 1759c98d5d94SLen Brown unsigned int low, high; 1760c98d5d94SLen Brown 1761c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1762c98d5d94SLen Brown 1763c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1764c98d5d94SLen Brown } 1765c98d5d94SLen Brown 1766c98d5d94SLen Brown /* 1767495c7654SLen Brown * Open a file, and exit on failure 1768495c7654SLen Brown */ 1769495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1770495c7654SLen Brown { 1771495c7654SLen Brown FILE *filep = fopen(path, mode); 1772495c7654SLen Brown 1773495c7654SLen Brown if (!filep) 1774495c7654SLen Brown err(1, "%s: open failed", path); 1775495c7654SLen Brown return filep; 1776495c7654SLen Brown } 1777495c7654SLen Brown /* 1778495c7654SLen Brown * snapshot_sysfs_counter() 1779495c7654SLen Brown * 1780495c7654SLen Brown * return snapshot of given counter 1781495c7654SLen Brown */ 1782495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1783495c7654SLen Brown { 1784495c7654SLen Brown FILE *fp; 1785495c7654SLen Brown int retval; 1786495c7654SLen Brown unsigned long long counter; 1787495c7654SLen Brown 1788495c7654SLen Brown fp = fopen_or_die(path, "r"); 1789495c7654SLen Brown 1790495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1791495c7654SLen Brown if (retval != 1) 1792495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1793495c7654SLen Brown 1794495c7654SLen Brown fclose(fp); 1795495c7654SLen Brown 1796495c7654SLen Brown return counter; 1797495c7654SLen Brown } 1798495c7654SLen Brown 1799495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1800495c7654SLen Brown { 1801495c7654SLen Brown if (mp->msr_num != 0) { 1802495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1803495c7654SLen Brown return -1; 1804495c7654SLen Brown } else { 180546c27978SLen Brown char path[128 + PATH_BYTES]; 180641618e63SLen Brown 180741618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 180841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 180941618e63SLen Brown cpu, mp->path); 181041618e63SLen Brown 181141618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 181241618e63SLen Brown } else { 1813495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1814495c7654SLen Brown } 181541618e63SLen Brown } 1816495c7654SLen Brown 1817495c7654SLen Brown return 0; 1818495c7654SLen Brown } 1819495c7654SLen Brown 18204c2122d4SLen Brown void get_apic_id(struct thread_data *t) 18214c2122d4SLen Brown { 182234041551SLen Brown unsigned int eax, ebx, ecx, edx; 182334041551SLen Brown 182434041551SLen Brown if (DO_BIC(BIC_APIC)) { 182534041551SLen Brown eax = ebx = ecx = edx = 0; 182634041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 182734041551SLen Brown 182834041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 182934041551SLen Brown } 183034041551SLen Brown 183134041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 183234041551SLen Brown return; 183334041551SLen Brown 1834c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 183534041551SLen Brown unsigned int topology_extensions; 183634041551SLen Brown 183734041551SLen Brown if (max_extended_level < 0x8000001e) 183834041551SLen Brown return; 18394c2122d4SLen Brown 18404c2122d4SLen Brown eax = ebx = ecx = edx = 0; 184134041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 184234041551SLen Brown topology_extensions = ecx & (1 << 22); 184334041551SLen Brown 184434041551SLen Brown if (topology_extensions == 0) 184534041551SLen Brown return; 184634041551SLen Brown 184734041551SLen Brown eax = ebx = ecx = edx = 0; 184834041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 184934041551SLen Brown 185034041551SLen Brown t->x2apic_id = eax; 185134041551SLen Brown return; 185234041551SLen Brown } 18534c2122d4SLen Brown 18544c2122d4SLen Brown if (!genuine_intel) 18554c2122d4SLen Brown return; 18564c2122d4SLen Brown 18574c2122d4SLen Brown if (max_level < 0xb) 18584c2122d4SLen Brown return; 18594c2122d4SLen Brown 18604c2122d4SLen Brown ecx = 0; 18614c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 18624c2122d4SLen Brown t->x2apic_id = edx; 18634c2122d4SLen Brown 186434041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 186534041551SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", 186634041551SLen Brown t->cpu_id, t->apic_id, t->x2apic_id); 18674c2122d4SLen Brown } 18684c2122d4SLen Brown 1869495c7654SLen Brown /* 1870c98d5d94SLen Brown * get_counters(...) 1871c98d5d94SLen Brown * migrate to cpu 1872c98d5d94SLen Brown * acquire and record local counters for that cpu 1873c98d5d94SLen Brown */ 1874c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1875c98d5d94SLen Brown { 1876c98d5d94SLen Brown int cpu = t->cpu_id; 1877889facbeSLen Brown unsigned long long msr; 18780102b067SLen Brown int aperf_mperf_retry_count = 0; 1879388e9c81SLen Brown struct msr_counter *mp; 1880388e9c81SLen Brown int i; 1881c98d5d94SLen Brown 1882e52966c0SLen Brown if (cpu_migrate(cpu)) { 1883b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 188415aaa346SLen Brown return -1; 1885e52966c0SLen Brown } 188615aaa346SLen Brown 1887d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1888d4794f25SYazen Ghannam 18894c2122d4SLen Brown if (first_counter_read) 18904c2122d4SLen Brown get_apic_id(t); 18910102b067SLen Brown retry: 1892c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 189315aaa346SLen Brown 18941e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 18951e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 18960102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 18970102b067SLen Brown 18980102b067SLen Brown /* 18990102b067SLen Brown * The TSC, APERF and MPERF must be read together for 19000102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 19010102b067SLen Brown * 19020102b067SLen Brown * Unfortunately, APERF and MPERF are read by 19030102b067SLen Brown * individual system call, so delays may occur 19040102b067SLen Brown * between them. If the time to read them 19050102b067SLen Brown * varies by a large amount, we re-read them. 19060102b067SLen Brown */ 19070102b067SLen Brown 19080102b067SLen Brown /* 19090102b067SLen Brown * This initial dummy APERF read has been seen to 19100102b067SLen Brown * reduce jitter in the subsequent reads. 19110102b067SLen Brown */ 19120102b067SLen Brown 19139c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1914c98d5d94SLen Brown return -3; 19150102b067SLen Brown 19160102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 19170102b067SLen Brown 19180102b067SLen Brown tsc_before = t->tsc; 19190102b067SLen Brown 19200102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 19210102b067SLen Brown return -3; 19220102b067SLen Brown 19230102b067SLen Brown tsc_between = rdtsc(); 19240102b067SLen Brown 19259c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1926c98d5d94SLen Brown return -4; 19270102b067SLen Brown 19280102b067SLen Brown tsc_after = rdtsc(); 19290102b067SLen Brown 19300102b067SLen Brown aperf_time = tsc_between - tsc_before; 19310102b067SLen Brown mperf_time = tsc_after - tsc_between; 19320102b067SLen Brown 19330102b067SLen Brown /* 19340102b067SLen Brown * If the system call latency to read APERF and MPERF 19350102b067SLen Brown * differ by more than 2x, then try again. 19360102b067SLen Brown */ 19370102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 19380102b067SLen Brown aperf_mperf_retry_count++; 19390102b067SLen Brown if (aperf_mperf_retry_count < 5) 19400102b067SLen Brown goto retry; 19410102b067SLen Brown else 19420102b067SLen Brown warnx("cpu%d jitter %lld %lld", 19430102b067SLen Brown cpu, aperf_time, mperf_time); 19440102b067SLen Brown } 19450102b067SLen Brown aperf_mperf_retry_count = 0; 19460102b067SLen Brown 1947b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1948b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 194988c3281fSLen Brown } 195088c3281fSLen Brown 1951812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1952562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1953812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 19541ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 19551ed51011SLen Brown return -5; 19561ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 19571ed51011SLen Brown } 19580539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1959144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1960144b44b1SLen Brown return -6; 1961144b44b1SLen Brown } 1962144b44b1SLen Brown 1963388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1964495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1965388e9c81SLen Brown return -10; 1966388e9c81SLen Brown } 1967388e9c81SLen Brown 1968c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1969c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1970f4fdf2b4SLen Brown goto done; 1971c98d5d94SLen Brown 19721e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 1973c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1974c98d5d94SLen Brown return -6; 1975144b44b1SLen Brown } 1976144b44b1SLen Brown 19771e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 1978c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1979c98d5d94SLen Brown return -7; 19801e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 1981fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1982fb5d4327SDasaratharaman Chandramouli return -7; 1983103a8feaSLen Brown } 198415aaa346SLen Brown 19851e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) 1986c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1987c98d5d94SLen Brown return -8; 1988c98d5d94SLen Brown 19890539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 19900539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 19910539ba11SLen Brown return -8; 19920539ba11SLen Brown 1993812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 1994889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1995889facbeSLen Brown return -9; 1996889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1997889facbeSLen Brown } 1998889facbeSLen Brown 19999392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 20009392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 20019392bd98SCalvin Walton return -14; 20029392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 20039392bd98SCalvin Walton } 20049392bd98SCalvin Walton 2005388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 2006495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 2007388e9c81SLen Brown return -10; 2008388e9c81SLen Brown } 2009889facbeSLen Brown 2010c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 2011c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2012f4fdf2b4SLen Brown goto done; 201315aaa346SLen Brown 2014a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 20150b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 20160b2bb692SLen Brown return -10; 2017a99d8730SLen Brown } 2018a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 20190b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 20200b2bb692SLen Brown return -11; 2021a99d8730SLen Brown } 2022a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 20230b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 20240b2bb692SLen Brown return -12; 2025a99d8730SLen Brown } 2026a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 20270b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 20280b2bb692SLen Brown return -13; 20290b2bb692SLen Brown } 20300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 2031c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 2032c98d5d94SLen Brown return -9; 20330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 20340539ba11SLen Brown if (do_slm_cstates) { 20350539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 20360539ba11SLen Brown return -10; 20370539ba11SLen Brown } else { 2038c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 2039c98d5d94SLen Brown return -10; 20400539ba11SLen Brown } 20410539ba11SLen Brown } 20420539ba11SLen Brown 20430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 2044c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 2045c98d5d94SLen Brown return -11; 20460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 2047c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 2048c98d5d94SLen Brown return -12; 20490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 2050ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 2051ca58710fSKristen Carlson Accardi return -13; 20520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 2053ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 2054ca58710fSKristen Carlson Accardi return -13; 20550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 2056ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 2057ca58710fSKristen Carlson Accardi return -13; 20580f47c08dSLen Brown 2059be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2060be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 2061be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2062be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 2063be0e54c4SLen Brown 2064889facbeSLen Brown if (do_rapl & RAPL_PKG) { 20659972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 2066889facbeSLen Brown return -13; 20679972d5d8SChen Yu p->energy_pkg = msr; 2068889facbeSLen Brown } 20699148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 20709972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 2071889facbeSLen Brown return -14; 20729972d5d8SChen Yu p->energy_cores = msr; 2073889facbeSLen Brown } 2074889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 20759972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 2076889facbeSLen Brown return -15; 20779972d5d8SChen Yu p->energy_dram = msr; 2078889facbeSLen Brown } 2079889facbeSLen Brown if (do_rapl & RAPL_GFX) { 20809972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 2081889facbeSLen Brown return -16; 20829972d5d8SChen Yu p->energy_gfx = msr; 2083889facbeSLen Brown } 2084889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 20859972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr)) 2086889facbeSLen Brown return -16; 20879972d5d8SChen Yu p->rapl_pkg_perf_status = msr; 2088889facbeSLen Brown } 2089889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 20909972d5d8SChen Yu if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr)) 2091889facbeSLen Brown return -16; 20929972d5d8SChen Yu p->rapl_dram_perf_status = msr; 2093889facbeSLen Brown } 20943316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 20959972d5d8SChen Yu if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr)) 20963316f99aSCalvin Walton return -13; 20979972d5d8SChen Yu p->energy_pkg = msr; 20983316f99aSCalvin Walton } 2099812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2100889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2101889facbeSLen Brown return -17; 2102889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2103889facbeSLen Brown } 2104fdf676e5SLen Brown 2105812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2106fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2107fdf676e5SLen Brown 2108812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 210927d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 211027d47356SLen Brown 2111388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2112495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2113388e9c81SLen Brown return -10; 2114388e9c81SLen Brown } 2115f4fdf2b4SLen Brown done: 2116f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2117388e9c81SLen Brown 211815aaa346SLen Brown return 0; 2119103a8feaSLen Brown } 2120103a8feaSLen Brown 2121ee7e38e3SLen Brown /* 2122ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2123ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2124ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2125ee7e38e3SLen Brown */ 2126ee7e38e3SLen Brown 2127ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2128ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2129ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2130ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2131ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2132ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2133ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2134ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2135ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2136ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2137ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2138ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 21390b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 21400b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2141445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2142445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2143ee7e38e3SLen Brown 2144ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2145ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 2146445640a5SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"}; 2147ee7e38e3SLen Brown 2148e9257f5fSLen Brown int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2149e9257f5fSLen Brown int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2150e9257f5fSLen Brown int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 21510539ba11SLen Brown int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7}; 2152f2642888SLen Brown int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2153e9257f5fSLen Brown int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2154445640a5SLen Brown int glm_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 21552085e124SArtem Bityutskiy int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 2156ee7e38e3SLen Brown 2157a2b7b749SLen Brown 2158a2b7b749SLen Brown static void 2159a2b7b749SLen Brown calculate_tsc_tweak() 2160a2b7b749SLen Brown { 2161a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2162a2b7b749SLen Brown } 2163a2b7b749SLen Brown 2164fcd17211SLen Brown static void 2165fcd17211SLen Brown dump_nhm_platform_info(void) 2166103a8feaSLen Brown { 2167103a8feaSLen Brown unsigned long long msr; 2168103a8feaSLen Brown unsigned int ratio; 2169103a8feaSLen Brown 2170ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2171103a8feaSLen Brown 2172b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 21736574a5d5SLen Brown 2174103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 2175710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 2176103a8feaSLen Brown ratio, bclk, ratio * bclk); 2177103a8feaSLen Brown 2178103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 2179710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 2180103a8feaSLen Brown ratio, bclk, ratio * bclk); 2181103a8feaSLen Brown 21827ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2183b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2184bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 218567920418SLen Brown 2186fcd17211SLen Brown return; 2187fcd17211SLen Brown } 2188fcd17211SLen Brown 2189fcd17211SLen Brown static void 2190fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 2191fcd17211SLen Brown { 2192fcd17211SLen Brown unsigned long long msr; 2193fcd17211SLen Brown unsigned int ratio; 2194fcd17211SLen Brown 21957ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2196fcd17211SLen Brown 2197b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2198fcd17211SLen Brown 2199fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2200fcd17211SLen Brown if (ratio) 2201710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 2202fcd17211SLen Brown ratio, bclk, ratio * bclk); 2203fcd17211SLen Brown 2204fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2205fcd17211SLen Brown if (ratio) 2206710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 2207fcd17211SLen Brown ratio, bclk, ratio * bclk); 2208fcd17211SLen Brown return; 2209fcd17211SLen Brown } 2210fcd17211SLen Brown 2211fcd17211SLen Brown static void 2212fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 2213fcd17211SLen Brown { 2214fcd17211SLen Brown unsigned long long msr; 2215fcd17211SLen Brown unsigned int ratio; 22166574a5d5SLen Brown 22177ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 22186574a5d5SLen Brown 2219b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 22206574a5d5SLen Brown 22216574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 22226574a5d5SLen Brown if (ratio) 2223710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 22246574a5d5SLen Brown ratio, bclk, ratio * bclk); 22256574a5d5SLen Brown 22266574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 22276574a5d5SLen Brown if (ratio) 2228710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 22296574a5d5SLen Brown ratio, bclk, ratio * bclk); 22306574a5d5SLen Brown 22316574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 22326574a5d5SLen Brown if (ratio) 2233710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 22346574a5d5SLen Brown ratio, bclk, ratio * bclk); 22356574a5d5SLen Brown 22366574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 22376574a5d5SLen Brown if (ratio) 2238710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 22396574a5d5SLen Brown ratio, bclk, ratio * bclk); 22406574a5d5SLen Brown 22416574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 22426574a5d5SLen Brown if (ratio) 2243710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 22446574a5d5SLen Brown ratio, bclk, ratio * bclk); 22456574a5d5SLen Brown 22466574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 22476574a5d5SLen Brown if (ratio) 2248710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 22496574a5d5SLen Brown ratio, bclk, ratio * bclk); 22506574a5d5SLen Brown 22516574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 22526574a5d5SLen Brown if (ratio) 2253710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 22546574a5d5SLen Brown ratio, bclk, ratio * bclk); 22556574a5d5SLen Brown 22566574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 22576574a5d5SLen Brown if (ratio) 2258710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 22596574a5d5SLen Brown ratio, bclk, ratio * bclk); 2260103a8feaSLen Brown return; 2261fcd17211SLen Brown } 226231e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 226331e07522SLen Brown { 226431e07522SLen Brown 226531e07522SLen Brown if (!genuine_intel) 226631e07522SLen Brown return 0; 226731e07522SLen Brown 226831e07522SLen Brown switch (model) { 226931e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 227031e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 22715ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 227231e07522SLen Brown return 1; 227331e07522SLen Brown } 227431e07522SLen Brown return 0; 227531e07522SLen Brown } 2276fcd17211SLen Brown 2277fcd17211SLen Brown static void 227831e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 2279fcd17211SLen Brown { 228031e07522SLen Brown unsigned long long msr, core_counts; 228131e07522SLen Brown unsigned int ratio, group_size; 2282103a8feaSLen Brown 22837ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2284b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 22856574a5d5SLen Brown 228631e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 228731e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 228831e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 228931e07522SLen Brown } else { 229031e07522SLen Brown core_counts = 0x0807060504030201; 229131e07522SLen Brown } 229231e07522SLen Brown 22936574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 229431e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 22956574a5d5SLen Brown if (ratio) 229631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 229731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 22986574a5d5SLen Brown 22996574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 230031e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 23016574a5d5SLen Brown if (ratio) 230231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 230331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23046574a5d5SLen Brown 23056574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 230631e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 23076574a5d5SLen Brown if (ratio) 230831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 230931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23106574a5d5SLen Brown 23116574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 231231e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 23136574a5d5SLen Brown if (ratio) 231431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 231531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 23166574a5d5SLen Brown 2317103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 231831e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2319103a8feaSLen Brown if (ratio) 232031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 232131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2322103a8feaSLen Brown 2323103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 232431e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2325103a8feaSLen Brown if (ratio) 232631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 232731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2328103a8feaSLen Brown 2329103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 233031e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2331103a8feaSLen Brown if (ratio) 233231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 233331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2334103a8feaSLen Brown 2335103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 233631e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2337103a8feaSLen Brown if (ratio) 233831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 233931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2340fcd17211SLen Brown return; 2341fcd17211SLen Brown } 23423a9a941dSLen Brown 2343fcd17211SLen Brown static void 23440f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 23450f7887c4SLen Brown { 23460f7887c4SLen Brown unsigned long long msr; 23470f7887c4SLen Brown unsigned int ratio; 23480f7887c4SLen Brown 23490f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 23500f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 23510f7887c4SLen Brown 23520f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 23530f7887c4SLen Brown if (ratio) 23540f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 23550f7887c4SLen Brown ratio, bclk, ratio * bclk); 23560f7887c4SLen Brown 23570f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 23580f7887c4SLen Brown if (ratio) 23590f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 23600f7887c4SLen Brown ratio, bclk, ratio * bclk); 23610f7887c4SLen Brown 23620f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 23630f7887c4SLen Brown if (ratio) 23640f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 23650f7887c4SLen Brown ratio, bclk, ratio * bclk); 23660f7887c4SLen Brown 23670f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 23680f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 23690f7887c4SLen Brown 23700f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 23710f7887c4SLen Brown if (ratio) 23720f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 23730f7887c4SLen Brown ratio, bclk, ratio * bclk); 23740f7887c4SLen Brown 23750f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 23760f7887c4SLen Brown if (ratio) 23770f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 23780f7887c4SLen Brown ratio, bclk, ratio * bclk); 23790f7887c4SLen Brown 23800f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 23810f7887c4SLen Brown if (ratio) 23820f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 23830f7887c4SLen Brown ratio, bclk, ratio * bclk); 23840f7887c4SLen Brown 23850f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 23860f7887c4SLen Brown if (ratio) 23870f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 23880f7887c4SLen Brown ratio, bclk, ratio * bclk); 23890f7887c4SLen Brown } 23900f7887c4SLen Brown 23910f7887c4SLen Brown static void 2392fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2393fb5d4327SDasaratharaman Chandramouli { 2394cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2395cbf97abaSHubert Chrzaniuk 2396fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2397cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2398cbf97abaSHubert Chrzaniuk int i, b_nr; 2399cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2400cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2401fb5d4327SDasaratharaman Chandramouli 2402ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2403fb5d4327SDasaratharaman Chandramouli 2404b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2405bfae2052SLen Brown base_cpu, msr); 2406fb5d4327SDasaratharaman Chandramouli 2407fb5d4327SDasaratharaman Chandramouli /** 2408fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2409cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2410cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2411fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2412fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2413fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2414fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2415fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2416fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2417fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2418fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2419fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2420fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2421fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2422fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2423fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2424fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2425fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2426fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2427fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2428fb5d4327SDasaratharaman Chandramouli */ 2429fb5d4327SDasaratharaman Chandramouli 2430cbf97abaSHubert Chrzaniuk b_nr = 0; 2431cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2432cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2433cbf97abaSHubert Chrzaniuk 2434cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2435fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2436cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2437fb5d4327SDasaratharaman Chandramouli 2438cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2439cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2440cbf97abaSHubert Chrzaniuk b_nr++; 2441fb5d4327SDasaratharaman Chandramouli } 2442cbf97abaSHubert Chrzaniuk 2443cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2444cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2445b7d8c148SLen Brown fprintf(outf, 2446710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2447cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2448fb5d4327SDasaratharaman Chandramouli } 2449fb5d4327SDasaratharaman Chandramouli 2450fb5d4327SDasaratharaman Chandramouli static void 2451fcd17211SLen Brown dump_nhm_cst_cfg(void) 2452fcd17211SLen Brown { 2453fcd17211SLen Brown unsigned long long msr; 2454fcd17211SLen Brown 24551df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2456fcd17211SLen Brown 24571df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2458fcd17211SLen Brown 24593e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2460fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2461fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2462fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2463fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2464fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 24656c34f160SLen Brown (unsigned int)msr & 0xF, 2466fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2467ac980e13SArtem Bityutskiy 2468ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2469ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2470ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2471ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2472ac980e13SArtem Bityutskiy } 2473ac980e13SArtem Bityutskiy 2474ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2475ac980e13SArtem Bityutskiy 2476fcd17211SLen Brown return; 2477103a8feaSLen Brown } 2478103a8feaSLen Brown 24796fb3143bSLen Brown static void 24806fb3143bSLen Brown dump_config_tdp(void) 24816fb3143bSLen Brown { 24826fb3143bSLen Brown unsigned long long msr; 24836fb3143bSLen Brown 24846fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2485b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2486685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 24876fb3143bSLen Brown 24886fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2489b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 24906fb3143bSLen Brown if (msr) { 2491685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2492685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2493685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2494685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 24956fb3143bSLen Brown } 2496b7d8c148SLen Brown fprintf(outf, ")\n"); 24976fb3143bSLen Brown 24986fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2499b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 25006fb3143bSLen Brown if (msr) { 2501685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2502685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2503685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2504685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 25056fb3143bSLen Brown } 2506b7d8c148SLen Brown fprintf(outf, ")\n"); 25076fb3143bSLen Brown 25086fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2509b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 25106fb3143bSLen Brown if ((msr) & 0x3) 2511b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2512b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2513b7d8c148SLen Brown fprintf(outf, ")\n"); 25146fb3143bSLen Brown 25156fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2516b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2517685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2518b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2519b7d8c148SLen Brown fprintf(outf, ")\n"); 25206fb3143bSLen Brown } 25215a63426eSLen Brown 25225a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 25235a63426eSLen Brown 25245a63426eSLen Brown void print_irtl(void) 25255a63426eSLen Brown { 25265a63426eSLen Brown unsigned long long msr; 25275a63426eSLen Brown 25285a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 25295a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 25305a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25315a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25325a63426eSLen Brown 25335a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 25345a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 25355a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25365a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25375a63426eSLen Brown 25385a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 25395a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 25405a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25415a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25425a63426eSLen Brown 25435a63426eSLen Brown if (!do_irtl_hsw) 25445a63426eSLen Brown return; 25455a63426eSLen Brown 25465a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 25475a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 25485a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25495a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25505a63426eSLen Brown 25515a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 25525a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 25535a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25545a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25555a63426eSLen Brown 25565a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 25575a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 25585a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 25595a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 25605a63426eSLen Brown 25615a63426eSLen Brown } 256236229897SLen Brown void free_fd_percpu(void) 256336229897SLen Brown { 256436229897SLen Brown int i; 256536229897SLen Brown 256601a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 256736229897SLen Brown if (fd_percpu[i] != 0) 256836229897SLen Brown close(fd_percpu[i]); 256936229897SLen Brown } 257036229897SLen Brown 257136229897SLen Brown free(fd_percpu); 25726fb3143bSLen Brown } 25736fb3143bSLen Brown 2574c98d5d94SLen Brown void free_all_buffers(void) 2575103a8feaSLen Brown { 25760e2d8f05SLen Brown int i; 25770e2d8f05SLen Brown 2578c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2579c98d5d94SLen Brown cpu_present_set = NULL; 258036229897SLen Brown cpu_present_setsize = 0; 2581103a8feaSLen Brown 2582c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2583c98d5d94SLen Brown cpu_affinity_set = NULL; 2584c98d5d94SLen Brown cpu_affinity_setsize = 0; 2585103a8feaSLen Brown 2586c98d5d94SLen Brown free(thread_even); 2587c98d5d94SLen Brown free(core_even); 2588c98d5d94SLen Brown free(package_even); 2589103a8feaSLen Brown 2590c98d5d94SLen Brown thread_even = NULL; 2591c98d5d94SLen Brown core_even = NULL; 2592c98d5d94SLen Brown package_even = NULL; 2593103a8feaSLen Brown 2594c98d5d94SLen Brown free(thread_odd); 2595c98d5d94SLen Brown free(core_odd); 2596c98d5d94SLen Brown free(package_odd); 2597103a8feaSLen Brown 2598c98d5d94SLen Brown thread_odd = NULL; 2599c98d5d94SLen Brown core_odd = NULL; 2600c98d5d94SLen Brown package_odd = NULL; 2601103a8feaSLen Brown 2602c98d5d94SLen Brown free(output_buffer); 2603c98d5d94SLen Brown output_buffer = NULL; 2604c98d5d94SLen Brown outp = NULL; 260536229897SLen Brown 260636229897SLen Brown free_fd_percpu(); 2607562a2d37SLen Brown 2608562a2d37SLen Brown free(irq_column_2_cpu); 2609562a2d37SLen Brown free(irqs_per_cpu); 26100e2d8f05SLen Brown 26110e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 26120e2d8f05SLen Brown if (cpus[i].put_ids) 26130e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 26140e2d8f05SLen Brown } 26150e2d8f05SLen Brown free(cpus); 2616103a8feaSLen Brown } 2617103a8feaSLen Brown 261857a42a34SJosh Triplett 261957a42a34SJosh Triplett /* 262095aebc44SJosh Triplett * Parse a file containing a single int. 26216de68fe1SLen Brown * Return 0 if file can not be opened 26226de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 262395aebc44SJosh Triplett */ 262495aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 262595aebc44SJosh Triplett { 262695aebc44SJosh Triplett va_list args; 262795aebc44SJosh Triplett char path[PATH_MAX]; 262895aebc44SJosh Triplett FILE *filep; 262995aebc44SJosh Triplett int value; 263095aebc44SJosh Triplett 263195aebc44SJosh Triplett va_start(args, fmt); 263295aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 263395aebc44SJosh Triplett va_end(args); 26346de68fe1SLen Brown filep = fopen(path, "r"); 26356de68fe1SLen Brown if (!filep) 26366de68fe1SLen Brown return 0; 2637b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2638b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 263995aebc44SJosh Triplett fclose(filep); 264095aebc44SJosh Triplett return value; 264195aebc44SJosh Triplett } 264295aebc44SJosh Triplett 264395aebc44SJosh Triplett /* 2644c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2645c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2646c98d5d94SLen Brown */ 2647c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2648c98d5d94SLen Brown { 264995aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2650103a8feaSLen Brown } 2651103a8feaSLen Brown 2652103a8feaSLen Brown int get_physical_package_id(int cpu) 2653103a8feaSLen Brown { 265495aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2655103a8feaSLen Brown } 2656103a8feaSLen Brown 26576de68fe1SLen Brown int get_die_id(int cpu) 26586de68fe1SLen Brown { 26596de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 26606de68fe1SLen Brown } 26616de68fe1SLen Brown 2662103a8feaSLen Brown int get_core_id(int cpu) 2663103a8feaSLen Brown { 266495aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2665103a8feaSLen Brown } 2666103a8feaSLen Brown 2667ef605741SPrarit Bhargava void set_node_data(void) 2668ef605741SPrarit Bhargava { 26692ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 26702ffbb224SPrarit Bhargava int cpu_count; 2671ef605741SPrarit Bhargava 26722ffbb224SPrarit Bhargava /* initialize logical_node_id */ 26732ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 26742ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2675ef605741SPrarit Bhargava 26762ffbb224SPrarit Bhargava cpu_count = 0; 26772ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 26782ffbb224SPrarit Bhargava lnode = 0; 26792ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 26802ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2681ef605741SPrarit Bhargava continue; 26822ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 26832ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 26842ffbb224SPrarit Bhargava continue; 26852ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2686ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 26872ffbb224SPrarit Bhargava cpu_count++; 26882ffbb224SPrarit Bhargava /* 26892ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 26902ffbb224SPrarit Bhargava * the logical_node_id 26912ffbb224SPrarit Bhargava */ 26922ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 26932ffbb224SPrarit Bhargava if ((cpus[cpux].physical_package_id == pkg) && 26942ffbb224SPrarit Bhargava (cpus[cpux].physical_node_id == node)) { 26952ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 26962ffbb224SPrarit Bhargava cpu_count++; 2697ef605741SPrarit Bhargava } 26982ffbb224SPrarit Bhargava } 26992ffbb224SPrarit Bhargava lnode++; 27002ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 27012ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 27022ffbb224SPrarit Bhargava } 27032ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 27042ffbb224SPrarit Bhargava break; 27052ffbb224SPrarit Bhargava } 2706ef605741SPrarit Bhargava } 2707ef605741SPrarit Bhargava 2708ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2709c98d5d94SLen Brown { 2710c98d5d94SLen Brown char path[80]; 2711c98d5d94SLen Brown FILE *filep; 27120e2d8f05SLen Brown int i; 27130e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2714c98d5d94SLen Brown 27150e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 27160e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 27170e2d8f05SLen Brown cpu, i); 27180e2d8f05SLen Brown filep = fopen(path, "r"); 27190e2d8f05SLen Brown if (!filep) 27200e2d8f05SLen Brown continue; 27210e2d8f05SLen Brown fclose(filep); 27220e2d8f05SLen Brown return i; 27230e2d8f05SLen Brown } 27240e2d8f05SLen Brown return -1; 2725e275b388SDasaratharaman Chandramouli } 2726c98d5d94SLen Brown 27270e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 27280e2d8f05SLen Brown { 27290e2d8f05SLen Brown char path[80], character; 27300e2d8f05SLen Brown FILE *filep; 27310e2d8f05SLen Brown unsigned long map; 27328cb48b32SPrarit Bhargava int so, shift, sib_core; 27330e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 27340e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 27350e2d8f05SLen Brown size_t size; 27368cb48b32SPrarit Bhargava int thread_id = 0; 27370e2d8f05SLen Brown 27380e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 27398cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 27408cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 27410e2d8f05SLen Brown if (!thiscpu->put_ids) 27420e2d8f05SLen Brown return -1; 27430e2d8f05SLen Brown 27440e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 27450e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 27460e2d8f05SLen Brown 27470e2d8f05SLen Brown sprintf(path, 27480e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 27490e2d8f05SLen Brown filep = fopen_or_die(path, "r"); 27500e2d8f05SLen Brown do { 27510e2d8f05SLen Brown offset -= BITMASK_SIZE; 27528173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 27538173c336SBen Hutchings err(1, "%s: failed to parse file", path); 27540e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 27550e2d8f05SLen Brown if ((map >> shift) & 0x1) { 27568cb48b32SPrarit Bhargava so = shift + offset; 27578cb48b32SPrarit Bhargava sib_core = get_core_id(so); 27588cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 27598cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 27608cb48b32SPrarit Bhargava if ((so != cpu) && 27618cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 27628cb48b32SPrarit Bhargava cpus[so].thread_id = 27638cb48b32SPrarit Bhargava thread_id++; 27648cb48b32SPrarit Bhargava } 27650e2d8f05SLen Brown } 27660e2d8f05SLen Brown } 27670e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2768c98d5d94SLen Brown fclose(filep); 27690e2d8f05SLen Brown 27700e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2771c98d5d94SLen Brown } 2772c98d5d94SLen Brown 2773c98d5d94SLen Brown /* 2774c98d5d94SLen Brown * run func(thread, core, package) in topology order 2775c98d5d94SLen Brown * skip non-present cpus 2776103a8feaSLen Brown */ 2777103a8feaSLen Brown 2778c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2779c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2780c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2781c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2782c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2783c98d5d94SLen Brown struct pkg_data *pkg_base2) 2784c98d5d94SLen Brown { 278540f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2786c98d5d94SLen Brown 2787c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 278840f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 278940f5cfe7SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; 279040f5cfe7SPrarit Bhargava ++core_no) { 2791c98d5d94SLen Brown for (thread_no = 0; thread_no < 279270a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2793c98d5d94SLen Brown struct thread_data *t, *t2; 2794c98d5d94SLen Brown struct core_data *c, *c2; 2795c98d5d94SLen Brown struct pkg_data *p, *p2; 2796c98d5d94SLen Brown 279740f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 279840f5cfe7SPrarit Bhargava core_no, node_no, 279940f5cfe7SPrarit Bhargava pkg_no); 2800c98d5d94SLen Brown 2801c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2802c98d5d94SLen Brown continue; 2803c98d5d94SLen Brown 280440f5cfe7SPrarit Bhargava t2 = GET_THREAD(thread_base2, thread_no, 280540f5cfe7SPrarit Bhargava core_no, node_no, 280640f5cfe7SPrarit Bhargava pkg_no); 2807c98d5d94SLen Brown 280840f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 280940f5cfe7SPrarit Bhargava node_no, pkg_no); 281040f5cfe7SPrarit Bhargava c2 = GET_CORE(core_base2, core_no, 281140f5cfe7SPrarit Bhargava node_no, 281240f5cfe7SPrarit Bhargava pkg_no); 2813c98d5d94SLen Brown 2814c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2815c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2816c98d5d94SLen Brown 2817c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2818c98d5d94SLen Brown if (retval) 2819c98d5d94SLen Brown return retval; 2820c98d5d94SLen Brown } 2821c98d5d94SLen Brown } 2822c98d5d94SLen Brown } 282340f5cfe7SPrarit Bhargava } 2824c98d5d94SLen Brown return 0; 2825c98d5d94SLen Brown } 2826c98d5d94SLen Brown 2827c98d5d94SLen Brown /* 2828c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2829c98d5d94SLen Brown * return max_cpu number 2830c98d5d94SLen Brown */ 2831c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2832103a8feaSLen Brown { 2833103a8feaSLen Brown FILE *fp; 2834c98d5d94SLen Brown int cpu_num; 2835103a8feaSLen Brown int retval; 2836103a8feaSLen Brown 283757a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2838103a8feaSLen Brown 2839103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2840b2c95d90SJosh Triplett if (retval != 0) 2841b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2842103a8feaSLen Brown 2843c98d5d94SLen Brown while (1) { 2844c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2845103a8feaSLen Brown if (retval != 1) 2846103a8feaSLen Brown break; 2847103a8feaSLen Brown 2848c98d5d94SLen Brown retval = func(cpu_num); 2849c98d5d94SLen Brown if (retval) { 2850c98d5d94SLen Brown fclose(fp); 2851c98d5d94SLen Brown return(retval); 2852c98d5d94SLen Brown } 2853103a8feaSLen Brown } 2854103a8feaSLen Brown fclose(fp); 2855c98d5d94SLen Brown return 0; 2856103a8feaSLen Brown } 2857103a8feaSLen Brown 2858103a8feaSLen Brown void re_initialize(void) 2859103a8feaSLen Brown { 2860c98d5d94SLen Brown free_all_buffers(); 2861c98d5d94SLen Brown setup_all_buffers(); 2862c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2863103a8feaSLen Brown } 2864103a8feaSLen Brown 2865843c5791SPrarit Bhargava void set_max_cpu_num(void) 2866843c5791SPrarit Bhargava { 2867843c5791SPrarit Bhargava FILE *filep; 28688201a028SPrarit Bhargava int base_cpu; 2869843c5791SPrarit Bhargava unsigned long dummy; 28708201a028SPrarit Bhargava char pathname[64]; 2871843c5791SPrarit Bhargava 28728201a028SPrarit Bhargava base_cpu = sched_getcpu(); 28738201a028SPrarit Bhargava if (base_cpu < 0) 28748201a028SPrarit Bhargava err(1, "cannot find calling cpu ID"); 28758201a028SPrarit Bhargava sprintf(pathname, 28768201a028SPrarit Bhargava "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", 28778201a028SPrarit Bhargava base_cpu); 28788201a028SPrarit Bhargava 28798201a028SPrarit Bhargava filep = fopen_or_die(pathname, "r"); 2880843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2881843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 28820e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2883843c5791SPrarit Bhargava fclose(filep); 2884843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2885843c5791SPrarit Bhargava } 2886c98d5d94SLen Brown 2887103a8feaSLen Brown /* 2888c98d5d94SLen Brown * count_cpus() 2889c98d5d94SLen Brown * remember the last one seen, it will be the max 2890103a8feaSLen Brown */ 2891c98d5d94SLen Brown int count_cpus(int cpu) 2892103a8feaSLen Brown { 2893843c5791SPrarit Bhargava topo.num_cpus++; 2894c98d5d94SLen Brown return 0; 2895103a8feaSLen Brown } 2896c98d5d94SLen Brown int mark_cpu_present(int cpu) 2897c98d5d94SLen Brown { 2898c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 289915aaa346SLen Brown return 0; 2900103a8feaSLen Brown } 2901103a8feaSLen Brown 29028cb48b32SPrarit Bhargava int init_thread_id(int cpu) 29038cb48b32SPrarit Bhargava { 29048cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 29058cb48b32SPrarit Bhargava return 0; 29068cb48b32SPrarit Bhargava } 29078cb48b32SPrarit Bhargava 2908562a2d37SLen Brown /* 2909562a2d37SLen Brown * snapshot_proc_interrupts() 2910562a2d37SLen Brown * 2911562a2d37SLen Brown * read and record summary of /proc/interrupts 2912562a2d37SLen Brown * 2913562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2914562a2d37SLen Brown */ 2915562a2d37SLen Brown int snapshot_proc_interrupts(void) 2916562a2d37SLen Brown { 2917562a2d37SLen Brown static FILE *fp; 2918562a2d37SLen Brown int column, retval; 2919562a2d37SLen Brown 2920562a2d37SLen Brown if (fp == NULL) 2921562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2922562a2d37SLen Brown else 2923562a2d37SLen Brown rewind(fp); 2924562a2d37SLen Brown 2925562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2926562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2927562a2d37SLen Brown int cpu_number; 2928562a2d37SLen Brown 2929562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2930562a2d37SLen Brown if (retval != 1) 2931562a2d37SLen Brown break; 2932562a2d37SLen Brown 2933562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2934562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2935562a2d37SLen Brown return 1; 2936562a2d37SLen Brown } 2937562a2d37SLen Brown 2938562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2939562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2940562a2d37SLen Brown } 2941562a2d37SLen Brown 2942562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2943562a2d37SLen Brown while (1) { 2944562a2d37SLen Brown int column; 2945562a2d37SLen Brown char buf[64]; 2946562a2d37SLen Brown 2947562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2948562a2d37SLen Brown if (retval != 1) 2949562a2d37SLen Brown break; 2950562a2d37SLen Brown 2951562a2d37SLen Brown /* read the count per cpu */ 2952562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2953562a2d37SLen Brown 2954562a2d37SLen Brown int cpu_number, irq_count; 2955562a2d37SLen Brown 2956562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2957562a2d37SLen Brown if (retval != 1) 2958562a2d37SLen Brown break; 2959562a2d37SLen Brown 2960562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2961562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2962562a2d37SLen Brown 2963562a2d37SLen Brown } 2964562a2d37SLen Brown 2965562a2d37SLen Brown while (getc(fp) != '\n') 2966562a2d37SLen Brown ; /* flush interrupt description */ 2967562a2d37SLen Brown 2968562a2d37SLen Brown } 2969562a2d37SLen Brown return 0; 2970562a2d37SLen Brown } 297127d47356SLen Brown /* 2972fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2973fdf676e5SLen Brown * 2974fdf676e5SLen Brown * record snapshot of 2975fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2976fdf676e5SLen Brown * 2977fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2978fdf676e5SLen Brown */ 2979fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2980fdf676e5SLen Brown { 2981fdf676e5SLen Brown FILE *fp; 2982fdf676e5SLen Brown int retval; 2983fdf676e5SLen Brown 2984fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 2985fdf676e5SLen Brown 2986fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 2987fdf676e5SLen Brown if (retval != 1) 2988fdf676e5SLen Brown err(1, "GFX rc6"); 2989fdf676e5SLen Brown 2990fdf676e5SLen Brown fclose(fp); 2991fdf676e5SLen Brown 2992fdf676e5SLen Brown return 0; 2993fdf676e5SLen Brown } 2994fdf676e5SLen Brown /* 299527d47356SLen Brown * snapshot_gfx_mhz() 299627d47356SLen Brown * 299727d47356SLen Brown * record snapshot of 299827d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 299927d47356SLen Brown * 300027d47356SLen Brown * return 1 if config change requires a restart, else return 0 300127d47356SLen Brown */ 300227d47356SLen Brown int snapshot_gfx_mhz(void) 300327d47356SLen Brown { 300427d47356SLen Brown static FILE *fp; 300527d47356SLen Brown int retval; 300627d47356SLen Brown 300727d47356SLen Brown if (fp == NULL) 300827d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 300922048c54SLen Brown else { 301027d47356SLen Brown rewind(fp); 301122048c54SLen Brown fflush(fp); 301222048c54SLen Brown } 301327d47356SLen Brown 301427d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 301527d47356SLen Brown if (retval != 1) 301627d47356SLen Brown err(1, "GFX MHz"); 301727d47356SLen Brown 301827d47356SLen Brown return 0; 301927d47356SLen Brown } 3020562a2d37SLen Brown 3021562a2d37SLen Brown /* 3022be0e54c4SLen Brown * snapshot_cpu_lpi() 3023be0e54c4SLen Brown * 3024be0e54c4SLen Brown * record snapshot of 3025be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 3026be0e54c4SLen Brown */ 3027be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 3028be0e54c4SLen Brown { 3029be0e54c4SLen Brown FILE *fp; 3030be0e54c4SLen Brown int retval; 3031be0e54c4SLen Brown 3032be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 3033be0e54c4SLen Brown 3034be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 30355ea7647bSPrarit Bhargava if (retval != 1) { 30365ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 30375ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 3038605736c6SGustavo A. R. Silva fclose(fp); 30395ea7647bSPrarit Bhargava return -1; 30405ea7647bSPrarit Bhargava } 3041be0e54c4SLen Brown 3042be0e54c4SLen Brown fclose(fp); 3043be0e54c4SLen Brown 3044be0e54c4SLen Brown return 0; 3045be0e54c4SLen Brown } 3046be0e54c4SLen Brown /* 3047be0e54c4SLen Brown * snapshot_sys_lpi() 3048be0e54c4SLen Brown * 30491f81c5efSLen Brown * record snapshot of sys_lpi_file 3050be0e54c4SLen Brown */ 3051be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 3052be0e54c4SLen Brown { 3053be0e54c4SLen Brown FILE *fp; 3054be0e54c4SLen Brown int retval; 3055be0e54c4SLen Brown 30561f81c5efSLen Brown fp = fopen_or_die(sys_lpi_file, "r"); 3057be0e54c4SLen Brown 3058be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 30595ea7647bSPrarit Bhargava if (retval != 1) { 30605ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 30615ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 306215423b95SColin Ian King fclose(fp); 30635ea7647bSPrarit Bhargava return -1; 30645ea7647bSPrarit Bhargava } 3065be0e54c4SLen Brown fclose(fp); 3066be0e54c4SLen Brown 3067be0e54c4SLen Brown return 0; 3068be0e54c4SLen Brown } 3069be0e54c4SLen Brown /* 3070562a2d37SLen Brown * snapshot /proc and /sys files 3071562a2d37SLen Brown * 3072562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 3073562a2d37SLen Brown */ 3074562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 3075562a2d37SLen Brown { 3076218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 3077562a2d37SLen Brown if (snapshot_proc_interrupts()) 3078562a2d37SLen Brown return 1; 3079562a2d37SLen Brown 3080812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 3081fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 3082fdf676e5SLen Brown 3083812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 308427d47356SLen Brown snapshot_gfx_mhz(); 308527d47356SLen Brown 3086be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 3087be0e54c4SLen Brown snapshot_cpu_lpi_us(); 3088be0e54c4SLen Brown 3089be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 3090be0e54c4SLen Brown snapshot_sys_lpi_us(); 3091be0e54c4SLen Brown 3092562a2d37SLen Brown return 0; 3093562a2d37SLen Brown } 3094562a2d37SLen Brown 30958aa2ed0bSLen Brown int exit_requested; 30968aa2ed0bSLen Brown 30978aa2ed0bSLen Brown static void signal_handler (int signal) 30988aa2ed0bSLen Brown { 30998aa2ed0bSLen Brown switch (signal) { 31008aa2ed0bSLen Brown case SIGINT: 31018aa2ed0bSLen Brown exit_requested = 1; 31028aa2ed0bSLen Brown if (debug) 31038aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 31048aa2ed0bSLen Brown break; 310507211960SLen Brown case SIGUSR1: 310607211960SLen Brown if (debug > 1) 310707211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 310807211960SLen Brown break; 31098aa2ed0bSLen Brown } 31108aa2ed0bSLen Brown } 31118aa2ed0bSLen Brown 31128aa2ed0bSLen Brown void setup_signal_handler(void) 31138aa2ed0bSLen Brown { 31148aa2ed0bSLen Brown struct sigaction sa; 31158aa2ed0bSLen Brown 31168aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 31178aa2ed0bSLen Brown 31188aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 31198aa2ed0bSLen Brown 31208aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 31218aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 312207211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 312307211960SLen Brown err(1, "sigaction SIGUSR1"); 31248aa2ed0bSLen Brown } 3125b9ad8ee0SLen Brown 312647936f94SArtem Bityutskiy void do_sleep(void) 3127b9ad8ee0SLen Brown { 3128c026c236SArtem Bityutskiy struct timeval tout; 3129c026c236SArtem Bityutskiy struct timespec rest; 3130b9ad8ee0SLen Brown fd_set readfds; 3131b9ad8ee0SLen Brown int retval; 3132b9ad8ee0SLen Brown 3133b9ad8ee0SLen Brown FD_ZERO(&readfds); 3134b9ad8ee0SLen Brown FD_SET(0, &readfds); 3135b9ad8ee0SLen Brown 3136c026c236SArtem Bityutskiy if (ignore_stdin) { 313747936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 313847936f94SArtem Bityutskiy return; 313947936f94SArtem Bityutskiy } 3140b9ad8ee0SLen Brown 3141c026c236SArtem Bityutskiy tout = interval_tv; 3142c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3143b9ad8ee0SLen Brown 3144b9ad8ee0SLen Brown if (retval == 1) { 3145b9ad8ee0SLen Brown switch (getc(stdin)) { 3146b9ad8ee0SLen Brown case 'q': 3147b9ad8ee0SLen Brown exit_requested = 1; 3148b9ad8ee0SLen Brown break; 3149c026c236SArtem Bityutskiy case EOF: 3150c026c236SArtem Bityutskiy /* 3151c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3152c026c236SArtem Bityutskiy * won't be any further input. 3153c026c236SArtem Bityutskiy */ 3154c026c236SArtem Bityutskiy ignore_stdin = 1; 3155c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3156c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3157c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3158c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3159b9ad8ee0SLen Brown } 3160b9ad8ee0SLen Brown } 3161b9ad8ee0SLen Brown } 316247936f94SArtem Bityutskiy 316387e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr) 316487e15da9SChen Yu { 316587e15da9SChen Yu int ret, idx; 316687e15da9SChen Yu unsigned long long msr_cur, msr_last; 316787e15da9SChen Yu 316887e15da9SChen Yu if (!per_cpu_msr_sum) 316987e15da9SChen Yu return 1; 317087e15da9SChen Yu 317187e15da9SChen Yu idx = offset_to_idx(offset); 317287e15da9SChen Yu if (idx < 0) 317387e15da9SChen Yu return idx; 317487e15da9SChen Yu /* get_msr_sum() = sum + (get_msr() - last) */ 317587e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 317687e15da9SChen Yu if (ret) 317787e15da9SChen Yu return ret; 317887e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[idx].last; 317987e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 318087e15da9SChen Yu *msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum; 318187e15da9SChen Yu 318287e15da9SChen Yu return 0; 318387e15da9SChen Yu } 318487e15da9SChen Yu 318587e15da9SChen Yu timer_t timerid; 318687e15da9SChen Yu 318787e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */ 318887e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p) 318987e15da9SChen Yu { 319087e15da9SChen Yu int i, ret; 319187e15da9SChen Yu int cpu = t->cpu_id; 319287e15da9SChen Yu 319387e15da9SChen Yu for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) { 319487e15da9SChen Yu unsigned long long msr_cur, msr_last; 319587e15da9SChen Yu int offset; 319687e15da9SChen Yu 319787e15da9SChen Yu if (!idx_valid(i)) 319887e15da9SChen Yu continue; 319987e15da9SChen Yu offset = idx_to_offset(i); 320087e15da9SChen Yu if (offset < 0) 320187e15da9SChen Yu continue; 320287e15da9SChen Yu ret = get_msr(cpu, offset, &msr_cur); 320387e15da9SChen Yu if (ret) { 320487e15da9SChen Yu fprintf(outf, "Can not update msr(0x%x)\n", offset); 320587e15da9SChen Yu continue; 320687e15da9SChen Yu } 320787e15da9SChen Yu 320887e15da9SChen Yu msr_last = per_cpu_msr_sum[cpu].entries[i].last; 320987e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff; 321087e15da9SChen Yu 321187e15da9SChen Yu DELTA_WRAP32(msr_cur, msr_last); 321287e15da9SChen Yu per_cpu_msr_sum[cpu].entries[i].sum += msr_last; 321387e15da9SChen Yu } 321487e15da9SChen Yu return 0; 321587e15da9SChen Yu } 321687e15da9SChen Yu 321787e15da9SChen Yu static void 321887e15da9SChen Yu msr_record_handler(union sigval v) 321987e15da9SChen Yu { 322087e15da9SChen Yu for_all_cpus(update_msr_sum, EVEN_COUNTERS); 322187e15da9SChen Yu } 322287e15da9SChen Yu 322387e15da9SChen Yu void msr_sum_record(void) 322487e15da9SChen Yu { 322587e15da9SChen Yu struct itimerspec its; 322687e15da9SChen Yu struct sigevent sev; 322787e15da9SChen Yu 322887e15da9SChen Yu per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array)); 322987e15da9SChen Yu if (!per_cpu_msr_sum) { 323087e15da9SChen Yu fprintf(outf, "Can not allocate memory for long time MSR.\n"); 323187e15da9SChen Yu return; 323287e15da9SChen Yu } 323387e15da9SChen Yu /* 323487e15da9SChen Yu * Signal handler might be restricted, so use thread notifier instead. 323587e15da9SChen Yu */ 323687e15da9SChen Yu memset(&sev, 0, sizeof(struct sigevent)); 323787e15da9SChen Yu sev.sigev_notify = SIGEV_THREAD; 323887e15da9SChen Yu sev.sigev_notify_function = msr_record_handler; 323987e15da9SChen Yu 324087e15da9SChen Yu sev.sigev_value.sival_ptr = &timerid; 324187e15da9SChen Yu if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { 324287e15da9SChen Yu fprintf(outf, "Can not create timer.\n"); 324387e15da9SChen Yu goto release_msr; 324487e15da9SChen Yu } 324587e15da9SChen Yu 324687e15da9SChen Yu its.it_value.tv_sec = 0; 324787e15da9SChen Yu its.it_value.tv_nsec = 1; 324887e15da9SChen Yu /* 324987e15da9SChen Yu * A wraparound time has been calculated early. 325087e15da9SChen Yu * Some sources state that the peak power for a 325187e15da9SChen Yu * microprocessor is usually 1.5 times the TDP rating, 325287e15da9SChen Yu * use 2 * TDP for safety. 325387e15da9SChen Yu */ 325487e15da9SChen Yu its.it_interval.tv_sec = rapl_joule_counter_range / 2; 325587e15da9SChen Yu its.it_interval.tv_nsec = 0; 325687e15da9SChen Yu 325787e15da9SChen Yu if (timer_settime(timerid, 0, &its, NULL) == -1) { 325887e15da9SChen Yu fprintf(outf, "Can not set timer.\n"); 325987e15da9SChen Yu goto release_timer; 326087e15da9SChen Yu } 326187e15da9SChen Yu return; 326287e15da9SChen Yu 326387e15da9SChen Yu release_timer: 326487e15da9SChen Yu timer_delete(timerid); 326587e15da9SChen Yu release_msr: 326687e15da9SChen Yu free(per_cpu_msr_sum); 326787e15da9SChen Yu } 32684c2122d4SLen Brown 3269103a8feaSLen Brown void turbostat_loop() 3270103a8feaSLen Brown { 3271c98d5d94SLen Brown int retval; 3272e52966c0SLen Brown int restarted = 0; 3273023fe0acSChen Yu int done_iters = 0; 3274c98d5d94SLen Brown 32758aa2ed0bSLen Brown setup_signal_handler(); 32768aa2ed0bSLen Brown 3277103a8feaSLen Brown restart: 3278e52966c0SLen Brown restarted++; 3279e52966c0SLen Brown 3280562a2d37SLen Brown snapshot_proc_sysfs_files(); 3281c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 32824c2122d4SLen Brown first_counter_read = 0; 3283d91bb17cSLen Brown if (retval < -1) { 3284d91bb17cSLen Brown exit(retval); 3285d91bb17cSLen Brown } else if (retval == -1) { 3286e52966c0SLen Brown if (restarted > 1) { 3287e52966c0SLen Brown exit(retval); 3288e52966c0SLen Brown } 3289c98d5d94SLen Brown re_initialize(); 3290c98d5d94SLen Brown goto restart; 3291c98d5d94SLen Brown } 3292e52966c0SLen Brown restarted = 0; 3293023fe0acSChen Yu done_iters = 0; 3294103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3295103a8feaSLen Brown 3296103a8feaSLen Brown while (1) { 3297c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3298103a8feaSLen Brown re_initialize(); 3299103a8feaSLen Brown goto restart; 3300103a8feaSLen Brown } 3301b9ad8ee0SLen Brown do_sleep(); 3302562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3303562a2d37SLen Brown goto restart; 3304c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3305d91bb17cSLen Brown if (retval < -1) { 3306d91bb17cSLen Brown exit(retval); 3307d91bb17cSLen Brown } else if (retval == -1) { 330815aaa346SLen Brown re_initialize(); 330915aaa346SLen Brown goto restart; 331015aaa346SLen Brown } 3311103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3312103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3313ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3314ba3dec99SLen Brown re_initialize(); 3315ba3dec99SLen Brown goto restart; 3316ba3dec99SLen Brown } 3317c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3318c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3319b7d8c148SLen Brown flush_output_stdout(); 33208aa2ed0bSLen Brown if (exit_requested) 33218aa2ed0bSLen Brown break; 3322023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3323023fe0acSChen Yu break; 3324b9ad8ee0SLen Brown do_sleep(); 3325562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3326562a2d37SLen Brown goto restart; 3327c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3328d91bb17cSLen Brown if (retval < -1) { 3329d91bb17cSLen Brown exit(retval); 3330d91bb17cSLen Brown } else if (retval == -1) { 3331103a8feaSLen Brown re_initialize(); 3332103a8feaSLen Brown goto restart; 3333103a8feaSLen Brown } 3334103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3335103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3336ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3337ba3dec99SLen Brown re_initialize(); 3338ba3dec99SLen Brown goto restart; 3339ba3dec99SLen Brown } 3340c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3341c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3342b7d8c148SLen Brown flush_output_stdout(); 33438aa2ed0bSLen Brown if (exit_requested) 33448aa2ed0bSLen Brown break; 3345023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3346023fe0acSChen Yu break; 3347103a8feaSLen Brown } 3348103a8feaSLen Brown } 3349103a8feaSLen Brown 3350103a8feaSLen Brown void check_dev_msr() 3351103a8feaSLen Brown { 3352103a8feaSLen Brown struct stat sb; 33537ce7d5deSPrarit Bhargava char pathname[32]; 3354103a8feaSLen Brown 33557ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 33567ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3357a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3358d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3359103a8feaSLen Brown } 3360103a8feaSLen Brown 3361fcaa681cSLen Brown /* 3362fcaa681cSLen Brown * check for CAP_SYS_RAWIO 3363fcaa681cSLen Brown * return 0 on success 3364fcaa681cSLen Brown * return 1 on fail 3365fcaa681cSLen Brown */ 3366fcaa681cSLen Brown int check_for_cap_sys_rawio(void) 3367103a8feaSLen Brown { 3368fcaa681cSLen Brown cap_t caps; 3369fcaa681cSLen Brown cap_flag_value_t cap_flag_value; 3370fcaa681cSLen Brown 3371fcaa681cSLen Brown caps = cap_get_proc(); 3372fcaa681cSLen Brown if (caps == NULL) 3373fcaa681cSLen Brown err(-6, "cap_get_proc\n"); 3374fcaa681cSLen Brown 3375fcaa681cSLen Brown if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value)) 3376fcaa681cSLen Brown err(-6, "cap_get\n"); 3377fcaa681cSLen Brown 3378fcaa681cSLen Brown if (cap_flag_value != CAP_SET) { 3379fcaa681cSLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 3380fcaa681cSLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 3381fcaa681cSLen Brown return 1; 3382fcaa681cSLen Brown } 3383fcaa681cSLen Brown 3384fcaa681cSLen Brown if (cap_free(caps) == -1) 3385fcaa681cSLen Brown err(-6, "cap_free\n"); 3386fcaa681cSLen Brown 3387fcaa681cSLen Brown return 0; 3388fcaa681cSLen Brown } 3389fcaa681cSLen Brown void check_permissions(void) 3390fcaa681cSLen Brown { 339198481e79SLen Brown int do_exit = 0; 33927ce7d5deSPrarit Bhargava char pathname[32]; 339398481e79SLen Brown 339498481e79SLen Brown /* check for CAP_SYS_RAWIO */ 3395fcaa681cSLen Brown do_exit += check_for_cap_sys_rawio(); 339698481e79SLen Brown 339798481e79SLen Brown /* test file permissions */ 33987ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 33997ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 340098481e79SLen Brown do_exit++; 340198481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 340298481e79SLen Brown } 340398481e79SLen Brown 340498481e79SLen Brown /* if all else fails, thell them to be root */ 340598481e79SLen Brown if (do_exit) 3406b2c95d90SJosh Triplett if (getuid() != 0) 3407d7899447SLen Brown warnx("... or simply run as root"); 340898481e79SLen Brown 340998481e79SLen Brown if (do_exit) 341098481e79SLen Brown exit(-6); 3411103a8feaSLen Brown } 3412103a8feaSLen Brown 3413d7899447SLen Brown /* 3414d7899447SLen Brown * NHM adds support for additional MSRs: 3415d7899447SLen Brown * 3416d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3417d7899447SLen Brown * 3418ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 34191df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3420d7899447SLen Brown * 3421cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3422cf4cbe53SLen Brown * 3423d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3424d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3425d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3426d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3427d7899447SLen Brown * 3428ee7e38e3SLen Brown * Side effect: 34291df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 343033148d67SLen Brown * sets has_misc_feature_control 3431d7899447SLen Brown */ 3432ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3433103a8feaSLen Brown { 3434ee7e38e3SLen Brown unsigned long long msr; 343521ed5574SLen Brown unsigned int base_ratio; 3436ee7e38e3SLen Brown int *pkg_cstate_limits; 3437ee7e38e3SLen Brown 3438103a8feaSLen Brown if (!genuine_intel) 3439103a8feaSLen Brown return 0; 3440103a8feaSLen Brown 3441103a8feaSLen Brown if (family != 6) 3442103a8feaSLen Brown return 0; 3443103a8feaSLen Brown 344421ed5574SLen Brown bclk = discover_bclk(family, model); 344521ed5574SLen Brown 3446103a8feaSLen Brown switch (model) { 3447869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3448869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3449ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3450ee7e38e3SLen Brown break; 3451869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3452869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3453869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3454869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3455ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 345633148d67SLen Brown has_misc_feature_control = 1; 3457ee7e38e3SLen Brown break; 3458c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 34595e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3460869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 346177e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 3462c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 34635e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3464869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3465af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3466af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3467ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 346833148d67SLen Brown has_misc_feature_control = 1; 3469ee7e38e3SLen Brown break; 3470d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3471d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 347233148d67SLen Brown has_misc_feature_control = 1; 3473d8ebb442SLen Brown break; 3474f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3475cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 34765ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 3477ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3478ee7e38e3SLen Brown break; 3479869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3480ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3481cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3482ee7e38e3SLen Brown break; 3483869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3484ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3485ee7e38e3SLen Brown break; 3486869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3487f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 34885ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 3489f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 3490445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3491e4085d54SLen Brown break; 3492103a8feaSLen Brown default: 3493103a8feaSLen Brown return 0; 3494103a8feaSLen Brown } 34951df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3496e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3497ee7e38e3SLen Brown 3498ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 349921ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 350021ed5574SLen Brown 350121ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 350221ed5574SLen Brown has_base_hz = 1; 3503ee7e38e3SLen Brown return 1; 3504103a8feaSLen Brown } 35050f7887c4SLen Brown /* 3506495c7654SLen Brown * SLV client has support for unique MSRs: 35070f7887c4SLen Brown * 35080f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 35090f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 35100f7887c4SLen Brown */ 35110f7887c4SLen Brown 35120f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 35130f7887c4SLen Brown { 35140f7887c4SLen Brown if (!genuine_intel) 35150f7887c4SLen Brown return 0; 35160f7887c4SLen Brown 35170f7887c4SLen Brown switch (model) { 3518f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3519f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3520f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 35210f7887c4SLen Brown return 1; 35220f7887c4SLen Brown } 35230f7887c4SLen Brown return 0; 35240f7887c4SLen Brown } 35257170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 35267170a374SLen Brown { 35277170a374SLen Brown 35287170a374SLen Brown if (!genuine_intel) 35297170a374SLen Brown return 0; 35307170a374SLen Brown 35317170a374SLen Brown switch (model) { 35325ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: 35337170a374SLen Brown return 1; 35347170a374SLen Brown } 35357170a374SLen Brown return 0; 35367170a374SLen Brown } 3537ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3538ade0ebacSLen Brown { 3539ade0ebacSLen Brown 3540ade0ebacSLen Brown if (!genuine_intel) 3541ade0ebacSLen Brown return 0; 3542ade0ebacSLen Brown 3543ade0ebacSLen Brown switch (model) { 3544ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3545ade0ebacSLen Brown return 1; 3546ade0ebacSLen Brown } 3547ade0ebacSLen Brown return 0; 3548ade0ebacSLen Brown } 354934c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 355034c76197SLen Brown { 355134c76197SLen Brown 355234c76197SLen Brown if (!genuine_intel) 355334c76197SLen Brown return 0; 355434c76197SLen Brown 355534c76197SLen Brown switch (model) { 355634c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 355734c76197SLen Brown return 1; 355834c76197SLen Brown } 355934c76197SLen Brown return 0; 356034c76197SLen Brown } 3561f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model) 3562f6708400SChen Yu { 3563f6708400SChen Yu if (!genuine_intel) 3564f6708400SChen Yu return 0; 3565f6708400SChen Yu 3566f6708400SChen Yu switch (model) { 3567f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: 3568f6708400SChen Yu return 1; 3569f6708400SChen Yu } 3570f6708400SChen Yu return 0; 3571f6708400SChen Yu } 35720f7887c4SLen Brown 357331e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3574d7899447SLen Brown { 35750f7887c4SLen Brown if (has_slv_msrs(family, model)) 35760f7887c4SLen Brown return 0; 35770f7887c4SLen Brown 3578d7899447SLen Brown switch (model) { 3579d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3580869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3581869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3582d7899447SLen Brown return 0; 3583d7899447SLen Brown default: 3584d7899447SLen Brown return 1; 3585d7899447SLen Brown } 3586d7899447SLen Brown } 35870f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 35880f7887c4SLen Brown { 35890f7887c4SLen Brown if (has_slv_msrs(family, model)) 35900f7887c4SLen Brown return 1; 35910f7887c4SLen Brown 35920f7887c4SLen Brown return 0; 35930f7887c4SLen Brown } 35946574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 35956574a5d5SLen Brown { 35966574a5d5SLen Brown if (!genuine_intel) 35976574a5d5SLen Brown return 0; 35986574a5d5SLen Brown 35996574a5d5SLen Brown if (family != 6) 36006574a5d5SLen Brown return 0; 36016574a5d5SLen Brown 36026574a5d5SLen Brown switch (model) { 3603869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3604869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 36056574a5d5SLen Brown return 1; 36066574a5d5SLen Brown default: 36076574a5d5SLen Brown return 0; 36086574a5d5SLen Brown } 36096574a5d5SLen Brown } 3610fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3611fcd17211SLen Brown { 3612fcd17211SLen Brown if (!genuine_intel) 3613fcd17211SLen Brown return 0; 3614fcd17211SLen Brown 3615fcd17211SLen Brown if (family != 6) 3616fcd17211SLen Brown return 0; 3617fcd17211SLen Brown 3618fcd17211SLen Brown switch (model) { 3619869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3620fcd17211SLen Brown return 1; 3621fcd17211SLen Brown default: 3622fcd17211SLen Brown return 0; 3623fcd17211SLen Brown } 3624fcd17211SLen Brown } 3625fcd17211SLen Brown 3626fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3627fb5d4327SDasaratharaman Chandramouli { 3628fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3629fb5d4327SDasaratharaman Chandramouli return 0; 3630fb5d4327SDasaratharaman Chandramouli 3631fb5d4327SDasaratharaman Chandramouli if (family != 6) 3632fb5d4327SDasaratharaman Chandramouli return 0; 3633fb5d4327SDasaratharaman Chandramouli 3634fb5d4327SDasaratharaman Chandramouli switch (model) { 3635869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3636fb5d4327SDasaratharaman Chandramouli return 1; 3637fb5d4327SDasaratharaman Chandramouli default: 3638fb5d4327SDasaratharaman Chandramouli return 0; 3639fb5d4327SDasaratharaman Chandramouli } 3640fb5d4327SDasaratharaman Chandramouli } 364131e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 364231e07522SLen Brown { 364331e07522SLen Brown if (!genuine_intel) 364431e07522SLen Brown return 0; 364531e07522SLen Brown 364631e07522SLen Brown if (family != 6) 364731e07522SLen Brown return 0; 364831e07522SLen Brown 364931e07522SLen Brown switch (model) { 365031e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 365131e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 365231e07522SLen Brown return 1; 365331e07522SLen Brown default: 365431e07522SLen Brown return 0; 365531e07522SLen Brown } 365631e07522SLen Brown } 36576fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 36586fb3143bSLen Brown { 36596fb3143bSLen Brown if (!genuine_intel) 36606fb3143bSLen Brown return 0; 36616fb3143bSLen Brown 36626fb3143bSLen Brown if (family != 6) 36636fb3143bSLen Brown return 0; 36646fb3143bSLen Brown 36656fb3143bSLen Brown switch (model) { 3666869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3667c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 3668869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 366977e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 36705e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 3671c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 36725e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 3673869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3674af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 3675af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 3676869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 36776fb3143bSLen Brown 3678869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 36796fb3143bSLen Brown return 1; 36806fb3143bSLen Brown default: 36816fb3143bSLen Brown return 0; 36826fb3143bSLen Brown } 36836fb3143bSLen Brown } 36846fb3143bSLen Brown 3685fcd17211SLen Brown static void 3686fecb3bc8SDavid Arcari remove_underbar(char *s) 3687fecb3bc8SDavid Arcari { 3688fecb3bc8SDavid Arcari char *to = s; 3689fecb3bc8SDavid Arcari 3690fecb3bc8SDavid Arcari while (*s) { 3691fecb3bc8SDavid Arcari if (*s != '_') 3692fecb3bc8SDavid Arcari *to++ = *s; 3693fecb3bc8SDavid Arcari s++; 3694fecb3bc8SDavid Arcari } 3695fecb3bc8SDavid Arcari 3696fecb3bc8SDavid Arcari *to = 0; 3697fecb3bc8SDavid Arcari } 3698fecb3bc8SDavid Arcari 3699fecb3bc8SDavid Arcari static void 37001b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3701fcd17211SLen Brown { 3702fcd17211SLen Brown if (!do_nhm_platform_info) 3703fcd17211SLen Brown return; 3704fcd17211SLen Brown 3705fcd17211SLen Brown dump_nhm_platform_info(); 3706fcd17211SLen Brown 3707fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3708fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3709fcd17211SLen Brown 3710fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3711fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3712fcd17211SLen Brown 371331e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 371431e07522SLen Brown dump_turbo_ratio_limits(family, model); 3715fcd17211SLen Brown 37160f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 37170f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 37180f7887c4SLen Brown 3719fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3720fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3721fb5d4327SDasaratharaman Chandramouli 37226fb3143bSLen Brown if (has_config_tdp(family, model)) 37236fb3143bSLen Brown dump_config_tdp(); 37246fb3143bSLen Brown 3725fcd17211SLen Brown dump_nhm_cst_cfg(); 3726fcd17211SLen Brown } 3727fcd17211SLen Brown 3728abdcbdb2SLen Brown static void dump_sysfs_file(char *path) 3729abdcbdb2SLen Brown { 3730abdcbdb2SLen Brown FILE *input; 3731abdcbdb2SLen Brown char cpuidle_buf[64]; 3732abdcbdb2SLen Brown 3733abdcbdb2SLen Brown input = fopen(path, "r"); 3734abdcbdb2SLen Brown if (input == NULL) { 3735abdcbdb2SLen Brown if (debug) 3736abdcbdb2SLen Brown fprintf(outf, "NSFOD %s\n", path); 3737abdcbdb2SLen Brown return; 3738abdcbdb2SLen Brown } 3739abdcbdb2SLen Brown if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input)) 3740abdcbdb2SLen Brown err(1, "%s: failed to read file", path); 3741abdcbdb2SLen Brown fclose(input); 3742abdcbdb2SLen Brown 3743abdcbdb2SLen Brown fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf); 3744abdcbdb2SLen Brown } 374541618e63SLen Brown static void 374641618e63SLen Brown dump_sysfs_cstate_config(void) 374741618e63SLen Brown { 374841618e63SLen Brown char path[64]; 374941618e63SLen Brown char name_buf[16]; 375041618e63SLen Brown char desc[64]; 375141618e63SLen Brown FILE *input; 375241618e63SLen Brown int state; 375341618e63SLen Brown char *sp; 375441618e63SLen Brown 3755abdcbdb2SLen Brown if (access("/sys/devices/system/cpu/cpuidle", R_OK)) { 3756abdcbdb2SLen Brown fprintf(outf, "cpuidle not loaded\n"); 3757abdcbdb2SLen Brown return; 3758abdcbdb2SLen Brown } 3759abdcbdb2SLen Brown 3760abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver"); 3761abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor"); 3762abdcbdb2SLen Brown dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro"); 3763abdcbdb2SLen Brown 376441618e63SLen Brown for (state = 0; state < 10; ++state) { 376541618e63SLen Brown 376641618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 376741618e63SLen Brown base_cpu, state); 376841618e63SLen Brown input = fopen(path, "r"); 376941618e63SLen Brown if (input == NULL) 377041618e63SLen Brown continue; 37718173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 37728173c336SBen Hutchings err(1, "%s: failed to read file", path); 377341618e63SLen Brown 377441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 377541618e63SLen Brown sp = strchr(name_buf, '-'); 377641618e63SLen Brown if (!sp) 377741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 377841618e63SLen Brown *sp = '\0'; 377941618e63SLen Brown fclose(input); 378041618e63SLen Brown 3781fecb3bc8SDavid Arcari remove_underbar(name_buf); 3782fecb3bc8SDavid Arcari 378341618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 378441618e63SLen Brown base_cpu, state); 378541618e63SLen Brown input = fopen(path, "r"); 378641618e63SLen Brown if (input == NULL) 378741618e63SLen Brown continue; 37888173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 37898173c336SBen Hutchings err(1, "%s: failed to read file", path); 379041618e63SLen Brown 379141618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 379241618e63SLen Brown fclose(input); 379341618e63SLen Brown } 379441618e63SLen Brown } 37957293fccdSLen Brown static void 37967293fccdSLen Brown dump_sysfs_pstate_config(void) 37977293fccdSLen Brown { 37987293fccdSLen Brown char path[64]; 37997293fccdSLen Brown char driver_buf[64]; 38007293fccdSLen Brown char governor_buf[64]; 38017293fccdSLen Brown FILE *input; 38027293fccdSLen Brown int turbo; 38037293fccdSLen Brown 38047293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 38057293fccdSLen Brown base_cpu); 38067293fccdSLen Brown input = fopen(path, "r"); 38077293fccdSLen Brown if (input == NULL) { 38080a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 38097293fccdSLen Brown return; 38107293fccdSLen Brown } 38118173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 38128173c336SBen Hutchings err(1, "%s: failed to read file", path); 38137293fccdSLen Brown fclose(input); 38147293fccdSLen Brown 38157293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 38167293fccdSLen Brown base_cpu); 38177293fccdSLen Brown input = fopen(path, "r"); 38187293fccdSLen Brown if (input == NULL) { 38190a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 38207293fccdSLen Brown return; 38217293fccdSLen Brown } 38228173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 38238173c336SBen Hutchings err(1, "%s: failed to read file", path); 38247293fccdSLen Brown fclose(input); 38257293fccdSLen Brown 38267293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 38277293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 38287293fccdSLen Brown 38297293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 38307293fccdSLen Brown input = fopen(path, "r"); 38317293fccdSLen Brown if (input != NULL) { 38328173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 38338173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 38347293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 38357293fccdSLen Brown fclose(input); 38367293fccdSLen Brown } 38377293fccdSLen Brown 38387293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 38397293fccdSLen Brown input = fopen(path, "r"); 38407293fccdSLen Brown if (input != NULL) { 38418173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 38428173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 38437293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 38447293fccdSLen Brown fclose(input); 38457293fccdSLen Brown } 38467293fccdSLen Brown } 384741618e63SLen Brown 38486574a5d5SLen Brown 3849889facbeSLen Brown /* 3850889facbeSLen Brown * print_epb() 3851889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3852889facbeSLen Brown */ 3853889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3854889facbeSLen Brown { 3855889facbeSLen Brown unsigned long long msr; 3856889facbeSLen Brown char *epb_string; 3857889facbeSLen Brown int cpu; 3858889facbeSLen Brown 3859889facbeSLen Brown if (!has_epb) 3860889facbeSLen Brown return 0; 3861889facbeSLen Brown 3862889facbeSLen Brown cpu = t->cpu_id; 3863889facbeSLen Brown 3864889facbeSLen Brown /* EPB is per-package */ 3865889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3866889facbeSLen Brown return 0; 3867889facbeSLen Brown 3868889facbeSLen Brown if (cpu_migrate(cpu)) { 3869b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3870889facbeSLen Brown return -1; 3871889facbeSLen Brown } 3872889facbeSLen Brown 3873889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 3874889facbeSLen Brown return 0; 3875889facbeSLen Brown 3876e9be7dd6SLen Brown switch (msr & 0xF) { 3877889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3878889facbeSLen Brown epb_string = "performance"; 3879889facbeSLen Brown break; 3880889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3881889facbeSLen Brown epb_string = "balanced"; 3882889facbeSLen Brown break; 3883889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3884889facbeSLen Brown epb_string = "powersave"; 3885889facbeSLen Brown break; 3886889facbeSLen Brown default: 3887889facbeSLen Brown epb_string = "custom"; 3888889facbeSLen Brown break; 3889889facbeSLen Brown } 3890b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 3891889facbeSLen Brown 3892889facbeSLen Brown return 0; 3893889facbeSLen Brown } 38947f5c258eSLen Brown /* 38957f5c258eSLen Brown * print_hwp() 38967f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 38977f5c258eSLen Brown */ 38987f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 38997f5c258eSLen Brown { 39007f5c258eSLen Brown unsigned long long msr; 39017f5c258eSLen Brown int cpu; 39027f5c258eSLen Brown 39037f5c258eSLen Brown if (!has_hwp) 39047f5c258eSLen Brown return 0; 39057f5c258eSLen Brown 39067f5c258eSLen Brown cpu = t->cpu_id; 39077f5c258eSLen Brown 39087f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 39097f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 39107f5c258eSLen Brown return 0; 39117f5c258eSLen Brown 39127f5c258eSLen Brown if (cpu_migrate(cpu)) { 3913b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 39147f5c258eSLen Brown return -1; 39157f5c258eSLen Brown } 39167f5c258eSLen Brown 39177f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 39187f5c258eSLen Brown return 0; 39197f5c258eSLen Brown 3920b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 39217f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 39227f5c258eSLen Brown 39237f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 39247f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 39257f5c258eSLen Brown return 0; 39267f5c258eSLen Brown 39277f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 39287f5c258eSLen Brown return 0; 39297f5c258eSLen Brown 3930b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 39316dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 39327f5c258eSLen Brown cpu, msr, 39337f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 39347f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 39357f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 39367f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 39377f5c258eSLen Brown 39387f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 39397f5c258eSLen Brown return 0; 39407f5c258eSLen Brown 3941b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 39426dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 39437f5c258eSLen Brown cpu, msr, 39447f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 39457f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 39467f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 39477f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 39487f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 39497f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 39507f5c258eSLen Brown 39517f5c258eSLen Brown if (has_hwp_pkg) { 39527f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 39537f5c258eSLen Brown return 0; 39547f5c258eSLen Brown 3955b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 39566dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 39577f5c258eSLen Brown cpu, msr, 39587f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 39597f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 39607f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 39617f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 39627f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 39637f5c258eSLen Brown } 39647f5c258eSLen Brown if (has_hwp_notify) { 39657f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 39667f5c258eSLen Brown return 0; 39677f5c258eSLen Brown 3968b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 39697f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 39707f5c258eSLen Brown cpu, msr, 39717f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 39727f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 39737f5c258eSLen Brown } 39747f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 39757f5c258eSLen Brown return 0; 39767f5c258eSLen Brown 3977b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 39787f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 39797f5c258eSLen Brown cpu, msr, 39807f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 39817f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 3982889facbeSLen Brown 3983889facbeSLen Brown return 0; 3984889facbeSLen Brown } 3985889facbeSLen Brown 39863a9a941dSLen Brown /* 39873a9a941dSLen Brown * print_perf_limit() 39883a9a941dSLen Brown */ 39893a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 39903a9a941dSLen Brown { 39913a9a941dSLen Brown unsigned long long msr; 39923a9a941dSLen Brown int cpu; 39933a9a941dSLen Brown 39943a9a941dSLen Brown cpu = t->cpu_id; 39953a9a941dSLen Brown 39963a9a941dSLen Brown /* per-package */ 39973a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 39983a9a941dSLen Brown return 0; 39993a9a941dSLen Brown 40003a9a941dSLen Brown if (cpu_migrate(cpu)) { 4001b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 40023a9a941dSLen Brown return -1; 40033a9a941dSLen Brown } 40043a9a941dSLen Brown 40053a9a941dSLen Brown if (do_core_perf_limit_reasons) { 40063a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 4007b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4008b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 4009e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 40103a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 4011e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 4012e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 4013e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 4014e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 4015e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 4016e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 4017e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 4018e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 4019e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 4020e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 4021e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 4022e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 4023b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 4024e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 40253a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 4026e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 4027e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 4028e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 4029e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 4030e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 4031e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 4032e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 4033e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 4034e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 4035e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 4036e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 4037e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 40383a9a941dSLen Brown 40393a9a941dSLen Brown } 40403a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 40413a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 4042b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4043b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 40443a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 40453a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 40463a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 40473a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 40483a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 40493a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 40503a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 40513a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4052b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 40533a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 40543a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 40553a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 40563a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 40573a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 40583a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 40593a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 40603a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 40613a9a941dSLen Brown } 40623a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 40633a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 4064b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 4065b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 40663a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 40673a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 40683a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 40693a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 40703a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 40713a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 4072b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 40733a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 40743a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 40753a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 40763a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 40773a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 40783a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 40793a9a941dSLen Brown } 40803a9a941dSLen Brown return 0; 40813a9a941dSLen Brown } 40823a9a941dSLen Brown 4083889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 4084889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 4085889facbeSLen Brown 40869392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 4087144b44b1SLen Brown { 4088144b44b1SLen Brown unsigned long long msr; 4089144b44b1SLen Brown 4090144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 40917ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 4092144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 4093144b44b1SLen Brown 4094144b44b1SLen Brown switch (model) { 4095f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 40965ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: 4097144b44b1SLen Brown return 30.0; 4098144b44b1SLen Brown default: 4099144b44b1SLen Brown return 135.0; 4100144b44b1SLen Brown } 4101144b44b1SLen Brown } 4102144b44b1SLen Brown 41039392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 41049392bd98SCalvin Walton { 41059392bd98SCalvin Walton switch (family) { 41069392bd98SCalvin Walton case 0x17: 4107c1c10cc7SPu Wen case 0x18: 41089392bd98SCalvin Walton default: 41099392bd98SCalvin Walton /* This is the max stock TDP of HEDT/Server Fam17h chips */ 41109392bd98SCalvin Walton return 250.0; 41119392bd98SCalvin Walton } 41129392bd98SCalvin Walton } 41139392bd98SCalvin Walton 411440ee8e3bSAndrey Semin /* 411540ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 411640ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 411740ee8e3bSAndrey Semin */ 411840ee8e3bSAndrey Semin static double 411940ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 412040ee8e3bSAndrey Semin { 412140ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 412240ee8e3bSAndrey Semin 412340ee8e3bSAndrey Semin switch (model) { 4124869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4125869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4126869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 412740ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 412840ee8e3bSAndrey Semin default: 412940ee8e3bSAndrey Semin return (rapl_energy_units); 413040ee8e3bSAndrey Semin } 413140ee8e3bSAndrey Semin } 413240ee8e3bSAndrey Semin 41339392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 4134889facbeSLen Brown { 4135889facbeSLen Brown unsigned long long msr; 4136144b44b1SLen Brown unsigned int time_unit; 4137889facbeSLen Brown double tdp; 4138889facbeSLen Brown 4139889facbeSLen Brown if (family != 6) 4140889facbeSLen Brown return; 4141889facbeSLen Brown 4142889facbeSLen Brown switch (model) { 4143869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4144869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 4145c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 414677e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 41475e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4148c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 41495e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4150144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 4151812db3f7SLen Brown if (rapl_joules) { 4152812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4153812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4154812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 4155812db3f7SLen Brown } else { 4156812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4157812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4158812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 4159812db3f7SLen Brown } 4160889facbeSLen Brown break; 4161869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4162f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4163e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 4164812db3f7SLen Brown if (rapl_joules) 4165812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4166812db3f7SLen Brown else 4167812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4168e4085d54SLen Brown break; 4169f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4170f6708400SChen Yu do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO; 4171f6708400SChen Yu if (rapl_joules) { 4172f6708400SChen Yu BIC_PRESENT(BIC_Pkg_J); 4173f6708400SChen Yu BIC_PRESENT(BIC_Cor_J); 4174f6708400SChen Yu BIC_PRESENT(BIC_RAM_J); 4175f6708400SChen Yu BIC_PRESENT(BIC_GFX_J); 4176f6708400SChen Yu } else { 4177f6708400SChen Yu BIC_PRESENT(BIC_PkgWatt); 4178f6708400SChen Yu BIC_PRESENT(BIC_CorWatt); 4179f6708400SChen Yu BIC_PRESENT(BIC_RAMWatt); 4180f6708400SChen Yu BIC_PRESENT(BIC_GFXWatt); 4181f6708400SChen Yu } 4182f6708400SChen Yu break; 4183af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4184af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 418581824921SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO; 4186812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4187812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4188812db3f7SLen Brown if (rapl_joules) { 4189812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4190812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4191812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 419281824921SLen Brown BIC_PRESENT(BIC_GFX_J); 4193812db3f7SLen Brown } else { 4194812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4195812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4196812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 419781824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 4198812db3f7SLen Brown } 41990b2bb692SLen Brown break; 4200869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 4201869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4202869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4203869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 42040b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 4205812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4206812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4207812db3f7SLen Brown if (rapl_joules) { 4208812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4209812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4210812db3f7SLen Brown } else { 4211812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4212812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4213812db3f7SLen Brown } 4214e6f9bb3cSLen Brown break; 4215869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4216869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 42170b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO; 4218812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4219812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4220812db3f7SLen Brown if (rapl_joules) { 4221812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4222812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4223812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4224812db3f7SLen Brown } else { 4225812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4226812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4227812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4228812db3f7SLen Brown } 4229144b44b1SLen Brown break; 4230f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 42315ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4232144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 4233812db3f7SLen Brown if (rapl_joules) { 4234812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4235812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4236812db3f7SLen Brown } else { 4237812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4238812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4239812db3f7SLen Brown } 4240889facbeSLen Brown break; 42415ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 42420f644909SJacob Pan do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS; 4243812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 4244812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 4245812db3f7SLen Brown if (rapl_joules) { 4246812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 4247812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 4248812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 4249812db3f7SLen Brown } else { 4250812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 4251812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 4252812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 4253812db3f7SLen Brown } 42540f644909SJacob Pan break; 4255889facbeSLen Brown default: 4256889facbeSLen Brown return; 4257889facbeSLen Brown } 4258889facbeSLen Brown 4259889facbeSLen Brown /* units on package 0, verify later other packages match */ 42607ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 4261889facbeSLen Brown return; 4262889facbeSLen Brown 4263889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 4264f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 4265144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 4266144b44b1SLen Brown else 4267889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 4268889facbeSLen Brown 426940ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 427040ee8e3bSAndrey Semin 4271144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 4272144b44b1SLen Brown if (time_unit == 0) 4273144b44b1SLen Brown time_unit = 0xA; 4274889facbeSLen Brown 4275144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 4276144b44b1SLen Brown 42779392bd98SCalvin Walton tdp = get_tdp_intel(model); 4278889facbeSLen Brown 4279889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 428096e47158SLen Brown if (!quiet) 4281b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 42829392bd98SCalvin Walton } 4283889facbeSLen Brown 42849392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 42859392bd98SCalvin Walton { 42869392bd98SCalvin Walton unsigned long long msr; 42879392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 42889392bd98SCalvin Walton unsigned int has_rapl = 0; 42899392bd98SCalvin Walton double tdp; 42909392bd98SCalvin Walton 42919392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 42929392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 42939392bd98SCalvin Walton /* RAPL (Fam 17h) */ 42949392bd98SCalvin Walton has_rapl = edx & (1 << 14); 42959392bd98SCalvin Walton } 42969392bd98SCalvin Walton 42979392bd98SCalvin Walton if (!has_rapl) 4298889facbeSLen Brown return; 42999392bd98SCalvin Walton 43009392bd98SCalvin Walton switch (family) { 43019392bd98SCalvin Walton case 0x17: /* Zen, Zen+ */ 4302c1c10cc7SPu Wen case 0x18: /* Hygon Dhyana */ 43039392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 43043316f99aSCalvin Walton if (rapl_joules) { 43053316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 43069392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 43073316f99aSCalvin Walton } else { 43083316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 43099392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 43103316f99aSCalvin Walton } 43119392bd98SCalvin Walton break; 43129392bd98SCalvin Walton default: 43139392bd98SCalvin Walton return; 43149392bd98SCalvin Walton } 43159392bd98SCalvin Walton 43169392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 43179392bd98SCalvin Walton return; 43189392bd98SCalvin Walton 43199392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 43209392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 43219392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 43229392bd98SCalvin Walton 43239cfa8e04SPu Wen tdp = get_tdp_amd(family); 43249392bd98SCalvin Walton 43259392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 43269392bd98SCalvin Walton if (!quiet) 43279392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 43289392bd98SCalvin Walton } 43299392bd98SCalvin Walton 43309392bd98SCalvin Walton /* 43319392bd98SCalvin Walton * rapl_probe() 43329392bd98SCalvin Walton * 43339392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 43349392bd98SCalvin Walton */ 43359392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 43369392bd98SCalvin Walton { 43379392bd98SCalvin Walton if (genuine_intel) 43389392bd98SCalvin Walton rapl_probe_intel(family, model); 4339c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 43409392bd98SCalvin Walton rapl_probe_amd(family, model); 4341889facbeSLen Brown } 4342889facbeSLen Brown 43431b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 43443a9a941dSLen Brown { 43453a9a941dSLen Brown if (!genuine_intel) 43463a9a941dSLen Brown return; 43473a9a941dSLen Brown 43483a9a941dSLen Brown if (family != 6) 43493a9a941dSLen Brown return; 43503a9a941dSLen Brown 43513a9a941dSLen Brown switch (model) { 4352c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 435377e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 43545e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 43553a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4356869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 43573a9a941dSLen Brown do_core_perf_limit_reasons = 1; 43583a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 43593a9a941dSLen Brown default: 43603a9a941dSLen Brown return; 43613a9a941dSLen Brown } 43623a9a941dSLen Brown } 43633a9a941dSLen Brown 4364ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4365ac980e13SArtem Bityutskiy { 4366ac980e13SArtem Bityutskiy if (is_skx(family, model) || is_bdx(family, model)) 4367ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4368ac980e13SArtem Bityutskiy } 4369ac980e13SArtem Bityutskiy 4370889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4371889facbeSLen Brown { 4372889facbeSLen Brown unsigned long long msr; 4373f4896fa5SLen Brown unsigned int dts, dts2; 4374889facbeSLen Brown int cpu; 4375889facbeSLen Brown 4376889facbeSLen Brown if (!(do_dts || do_ptm)) 4377889facbeSLen Brown return 0; 4378889facbeSLen Brown 4379889facbeSLen Brown cpu = t->cpu_id; 4380889facbeSLen Brown 4381889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4382889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4383889facbeSLen Brown return 0; 4384889facbeSLen Brown 4385889facbeSLen Brown if (cpu_migrate(cpu)) { 4386b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4387889facbeSLen Brown return -1; 4388889facbeSLen Brown } 4389889facbeSLen Brown 4390889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4391889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4392889facbeSLen Brown return 0; 4393889facbeSLen Brown 4394889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4395b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 4396889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 4397889facbeSLen Brown 4398889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4399889facbeSLen Brown return 0; 4400889facbeSLen Brown 4401889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4402889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4403b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4404889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4405889facbeSLen Brown } 4406889facbeSLen Brown 4407889facbeSLen Brown 4408f4896fa5SLen Brown if (do_dts && debug) { 4409889facbeSLen Brown unsigned int resolution; 4410889facbeSLen Brown 4411889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4412889facbeSLen Brown return 0; 4413889facbeSLen Brown 4414889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4415889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4416b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 4417889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 4418889facbeSLen Brown 4419889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4420889facbeSLen Brown return 0; 4421889facbeSLen Brown 4422889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4423889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4424b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4425889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4426889facbeSLen Brown } 4427889facbeSLen Brown 4428889facbeSLen Brown return 0; 4429889facbeSLen Brown } 4430889facbeSLen Brown 4431889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4432889facbeSLen Brown { 4433b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4434889facbeSLen Brown cpu, label, 4435889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4436889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4437889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4438889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4439889facbeSLen Brown 4440889facbeSLen Brown return; 4441889facbeSLen Brown } 4442889facbeSLen Brown 4443889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4444889facbeSLen Brown { 4445889facbeSLen Brown unsigned long long msr; 44469392bd98SCalvin Walton const char *msr_name; 4447889facbeSLen Brown int cpu; 4448889facbeSLen Brown 4449889facbeSLen Brown if (!do_rapl) 4450889facbeSLen Brown return 0; 4451889facbeSLen Brown 4452889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4453889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4454889facbeSLen Brown return 0; 4455889facbeSLen Brown 4456889facbeSLen Brown cpu = t->cpu_id; 4457889facbeSLen Brown if (cpu_migrate(cpu)) { 4458b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4459889facbeSLen Brown return -1; 4460889facbeSLen Brown } 4461889facbeSLen Brown 44629392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 44639392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 44649392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 44659392bd98SCalvin Walton return -1; 44669392bd98SCalvin Walton } else { 44679392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4468889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4469889facbeSLen Brown return -1; 44709392bd98SCalvin Walton } 4471889facbeSLen Brown 44729392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4473144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 447496e47158SLen Brown 4475144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4476144b44b1SLen Brown 4477889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4478889facbeSLen Brown return -5; 4479889facbeSLen Brown 4480889facbeSLen Brown 4481b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4482889facbeSLen Brown cpu, msr, 4483889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4484889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4485889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4486889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4487889facbeSLen Brown 4488144b44b1SLen Brown } 4489144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4490144b44b1SLen Brown 4491889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4492889facbeSLen Brown return -9; 4493889facbeSLen Brown 4494b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4495889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4496889facbeSLen Brown 4497889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4498b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4499889facbeSLen Brown cpu, 4500889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4501889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4502889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4503889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4504889facbeSLen Brown } 4505889facbeSLen Brown 45060b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4507889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4508889facbeSLen Brown return -6; 4509889facbeSLen Brown 4510b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4511889facbeSLen Brown cpu, msr, 4512889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4513889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4514889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4515889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 45160b2bb692SLen Brown } 45170b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4518889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4519889facbeSLen Brown return -9; 4520b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4521889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4522889facbeSLen Brown 4523889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4524889facbeSLen Brown } 4525144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4526889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4527889facbeSLen Brown return -7; 4528889facbeSLen Brown 4529b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4530144b44b1SLen Brown } 45319148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4532889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4533889facbeSLen Brown return -9; 4534b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4535889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4536889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4537889facbeSLen Brown } 4538889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4539889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4540889facbeSLen Brown return -8; 4541889facbeSLen Brown 4542b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4543889facbeSLen Brown 4544889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4545889facbeSLen Brown return -9; 4546b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4547889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4548889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4549889facbeSLen Brown } 4550889facbeSLen Brown return 0; 4551889facbeSLen Brown } 4552889facbeSLen Brown 4553d7899447SLen Brown /* 4554d7899447SLen Brown * SNB adds support for additional MSRs: 4555d7899447SLen Brown * 4556d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4557d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4558d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4559d7899447SLen Brown */ 4560103a8feaSLen Brown 4561d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4562103a8feaSLen Brown { 4563103a8feaSLen Brown if (!genuine_intel) 4564103a8feaSLen Brown return 0; 4565103a8feaSLen Brown 4566103a8feaSLen Brown switch (model) { 4567869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4568869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4569869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4570869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4571c66f78a6SPeter Zijlstra case INTEL_FAM6_HASWELL: /* HSW */ 4572869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 457377e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 45745e741407SPeter Zijlstra case INTEL_FAM6_HASWELL_G: /* HSW */ 4575c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 45765e741407SPeter Zijlstra case INTEL_FAM6_BROADWELL_G: /* BDW */ 4577869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4578af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4579af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4580869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4581869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4582f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 45835ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 4584f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4585103a8feaSLen Brown return 1; 4586103a8feaSLen Brown } 4587103a8feaSLen Brown return 0; 4588103a8feaSLen Brown } 4589103a8feaSLen Brown 4590d7899447SLen Brown /* 4591570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4592d7899447SLen Brown * 4593d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4594d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4595d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 45965a63426eSLen Brown * 45975a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 45985a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 45995a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 46005a63426eSLen Brown * 4601d7899447SLen Brown */ 4602570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4603ca58710fSKristen Carlson Accardi { 4604ca58710fSKristen Carlson Accardi if (!genuine_intel) 4605ca58710fSKristen Carlson Accardi return 0; 4606ca58710fSKristen Carlson Accardi 4607ca58710fSKristen Carlson Accardi switch (model) { 460877e5517cSIngo Molnar case INTEL_FAM6_HASWELL_L: /* HSW */ 4609c66f78a6SPeter Zijlstra case INTEL_FAM6_BROADWELL: /* BDW */ 4610af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4611af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4612869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4613f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4614f6708400SChen Yu case INTEL_FAM6_ATOM_TREMONT: /* EHL */ 4615ca58710fSKristen Carlson Accardi return 1; 4616ca58710fSKristen Carlson Accardi } 4617ca58710fSKristen Carlson Accardi return 0; 4618ca58710fSKristen Carlson Accardi } 4619ca58710fSKristen Carlson Accardi 46200b2bb692SLen Brown /* 46210b2bb692SLen Brown * SKL adds support for additional MSRS: 46220b2bb692SLen Brown * 46230b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 46240b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 46250b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 46260b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 46270b2bb692SLen Brown */ 46280b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 46290b2bb692SLen Brown { 46300b2bb692SLen Brown if (!genuine_intel) 46310b2bb692SLen Brown return 0; 46320b2bb692SLen Brown 46330b2bb692SLen Brown switch (model) { 4634af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 4635af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 46360b2bb692SLen Brown return 1; 46370b2bb692SLen Brown } 46380b2bb692SLen Brown return 0; 46390b2bb692SLen Brown } 46400b2bb692SLen Brown 4641144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4642144b44b1SLen Brown { 4643144b44b1SLen Brown if (!genuine_intel) 4644144b44b1SLen Brown return 0; 4645144b44b1SLen Brown switch (model) { 4646f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 46475ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_D: /* AVN */ 4648144b44b1SLen Brown return 1; 4649144b44b1SLen Brown } 4650144b44b1SLen Brown return 0; 4651144b44b1SLen Brown } 4652144b44b1SLen Brown 4653fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4654fb5d4327SDasaratharaman Chandramouli { 4655fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4656fb5d4327SDasaratharaman Chandramouli return 0; 4657fb5d4327SDasaratharaman Chandramouli switch (model) { 4658869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4659fb5d4327SDasaratharaman Chandramouli return 1; 4660fb5d4327SDasaratharaman Chandramouli } 4661fb5d4327SDasaratharaman Chandramouli return 0; 4662fb5d4327SDasaratharaman Chandramouli } 4663fb5d4327SDasaratharaman Chandramouli 4664997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4665997e5395SSrinivas Pandruvada { 4666997e5395SSrinivas Pandruvada if (!genuine_intel) 4667997e5395SSrinivas Pandruvada return 0; 4668997e5395SSrinivas Pandruvada 4669997e5395SSrinivas Pandruvada switch (model) { 4670af239c44SPeter Zijlstra case INTEL_FAM6_CANNONLAKE_L: /* CNL */ 4671997e5395SSrinivas Pandruvada return 1; 4672997e5395SSrinivas Pandruvada } 4673997e5395SSrinivas Pandruvada 4674997e5395SSrinivas Pandruvada return 0; 4675997e5395SSrinivas Pandruvada } 4676997e5395SSrinivas Pandruvada 4677b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4678b2b34dfeSHubert Chrzaniuk { 4679b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4680b2b34dfeSHubert Chrzaniuk return 1024; 4681b2b34dfeSHubert Chrzaniuk return 1; 4682b2b34dfeSHubert Chrzaniuk } 4683b2b34dfeSHubert Chrzaniuk 4684144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4685144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4686144b44b1SLen Brown 4687144b44b1SLen Brown double slm_bclk(void) 4688144b44b1SLen Brown { 4689144b44b1SLen Brown unsigned long long msr = 3; 4690144b44b1SLen Brown unsigned int i; 4691144b44b1SLen Brown double freq; 4692144b44b1SLen Brown 46937ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4694b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4695144b44b1SLen Brown 4696144b44b1SLen Brown i = msr & 0xf; 4697144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4698b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 46990a91e551SColin Ian King i = 3; 4700144b44b1SLen Brown } 4701144b44b1SLen Brown freq = slm_freq_table[i]; 4702144b44b1SLen Brown 470396e47158SLen Brown if (!quiet) 4704b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4705144b44b1SLen Brown 4706144b44b1SLen Brown return freq; 4707144b44b1SLen Brown } 4708144b44b1SLen Brown 4709103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4710103a8feaSLen Brown { 4711121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4712103a8feaSLen Brown return 100.00; 4713144b44b1SLen Brown else if (is_slm(family, model)) 4714144b44b1SLen Brown return slm_bclk(); 4715103a8feaSLen Brown else 4716103a8feaSLen Brown return 133.33; 4717103a8feaSLen Brown } 4718103a8feaSLen Brown 4719889facbeSLen Brown /* 4720889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4721889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4722889facbeSLen Brown * This is usually equal to tjMax. 4723889facbeSLen Brown * 4724889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4725889facbeSLen Brown * but also allow cmdline over-ride with -T. 4726889facbeSLen Brown * 4727889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4728889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4729889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4730889facbeSLen Brown */ 4731889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4732889facbeSLen Brown { 4733889facbeSLen Brown unsigned long long msr; 4734889facbeSLen Brown unsigned int target_c_local; 4735889facbeSLen Brown int cpu; 4736889facbeSLen Brown 4737889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4738889facbeSLen Brown if (!(do_dts || do_ptm)) 4739889facbeSLen Brown return 0; 4740889facbeSLen Brown 4741889facbeSLen Brown /* this is a per-package concept */ 4742889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4743889facbeSLen Brown return 0; 4744889facbeSLen Brown 4745889facbeSLen Brown cpu = t->cpu_id; 4746889facbeSLen Brown if (cpu_migrate(cpu)) { 4747b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4748889facbeSLen Brown return -1; 4749889facbeSLen Brown } 4750889facbeSLen Brown 4751889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4752889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 4753b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 4754889facbeSLen Brown cpu, tcc_activation_temp); 4755889facbeSLen Brown return 0; 4756889facbeSLen Brown } 4757889facbeSLen Brown 4758889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4759d7899447SLen Brown if (!do_nhm_platform_info) 4760889facbeSLen Brown goto guess; 4761889facbeSLen Brown 47627ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4763889facbeSLen Brown goto guess; 4764889facbeSLen Brown 47653482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 4766889facbeSLen Brown 476796e47158SLen Brown if (!quiet) 4768b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 4769889facbeSLen Brown cpu, msr, target_c_local); 4770889facbeSLen Brown 47713482124aSJean Delvare if (!target_c_local) 4772889facbeSLen Brown goto guess; 4773889facbeSLen Brown 4774889facbeSLen Brown tcc_activation_temp = target_c_local; 4775889facbeSLen Brown 4776889facbeSLen Brown return 0; 4777889facbeSLen Brown 4778889facbeSLen Brown guess: 4779889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 4780b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 4781889facbeSLen Brown cpu, tcc_activation_temp); 4782889facbeSLen Brown 4783889facbeSLen Brown return 0; 4784889facbeSLen Brown } 478569807a63SLen Brown 4786aa8d8cc7SLen Brown void decode_feature_control_msr(void) 4787aa8d8cc7SLen Brown { 4788aa8d8cc7SLen Brown unsigned long long msr; 4789aa8d8cc7SLen Brown 4790f6505c88SSean Christopherson if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr)) 4791aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 4792aa8d8cc7SLen Brown base_cpu, msr, 4793f6505c88SSean Christopherson msr & FEAT_CTL_LOCKED ? "" : "UN-", 4794aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 4795aa8d8cc7SLen Brown } 4796aa8d8cc7SLen Brown 479769807a63SLen Brown void decode_misc_enable_msr(void) 479869807a63SLen Brown { 479969807a63SLen Brown unsigned long long msr; 480069807a63SLen Brown 4801f26b1519SLen Brown if (!genuine_intel) 4802f26b1519SLen Brown return; 4803f26b1519SLen Brown 480469807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 4805e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 480669807a63SLen Brown base_cpu, msr, 4807e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 4808e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 4809fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 4810e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 4811e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 481269807a63SLen Brown } 481369807a63SLen Brown 481433148d67SLen Brown void decode_misc_feature_control(void) 481533148d67SLen Brown { 481633148d67SLen Brown unsigned long long msr; 481733148d67SLen Brown 481833148d67SLen Brown if (!has_misc_feature_control) 481933148d67SLen Brown return; 482033148d67SLen Brown 482133148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 482233148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 482333148d67SLen Brown base_cpu, msr, 482433148d67SLen Brown msr & (0 << 0) ? "No-" : "", 482533148d67SLen Brown msr & (1 << 0) ? "No-" : "", 482633148d67SLen Brown msr & (2 << 0) ? "No-" : "", 482733148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 482833148d67SLen Brown } 4829f0057310SLen Brown /* 4830f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 4831f0057310SLen Brown * 4832f0057310SLen Brown * Decode the bits according to the Nehalem documentation 4833f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 4834f0057310SLen Brown * bit[1] less so... 4835f0057310SLen Brown */ 4836f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 4837f0057310SLen Brown { 4838f0057310SLen Brown unsigned long long msr; 4839f0057310SLen Brown 4840f0057310SLen Brown if (!do_nhm_platform_info) 4841f0057310SLen Brown return; 4842f0057310SLen Brown 4843cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 4844cf4cbe53SLen Brown return; 4845cf4cbe53SLen Brown 4846f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 4847ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 4848f0057310SLen Brown base_cpu, msr, 4849f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 4850ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 4851ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 4852f0057310SLen Brown } 485371616c8eSLen Brown /* 485471616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 485571616c8eSLen Brown * 485671616c8eSLen Brown * This MSRs are present on Silvermont processors, 485771616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 485871616c8eSLen Brown */ 485971616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 486071616c8eSLen Brown { 486171616c8eSLen Brown unsigned long long msr; 486271616c8eSLen Brown 486371616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 486471616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 486571616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 486671616c8eSLen Brown 486771616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 486871616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 486971616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 487071616c8eSLen Brown } 48717f5c258eSLen Brown 4872f5a4c76aSLen Brown /* 4873f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 4874f5a4c76aSLen Brown */ 4875f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 4876f5a4c76aSLen Brown { 4877f5a4c76aSLen Brown 4878f5a4c76aSLen Brown switch(model) { 4879f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 4880f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 4881f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 4882f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 4883f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 4884f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 4885f5a4c76aSLen Brown 4886f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 4887f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 4888f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 4889f5a4c76aSLen Brown 4890f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 4891f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 4892f5a4c76aSLen Brown 4893f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 48945ebb34edSPeter Zijlstra case INTEL_FAM6_BROADWELL_D: /* BDX-DE */ 4895f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 4896f5a4c76aSLen Brown 4897af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: 4898c66f78a6SPeter Zijlstra case INTEL_FAM6_SKYLAKE: 4899af239c44SPeter Zijlstra case INTEL_FAM6_KABYLAKE_L: 4900c66f78a6SPeter Zijlstra case INTEL_FAM6_KABYLAKE: 4901081c5432SChen Yu case INTEL_FAM6_COMETLAKE_L: 4902081c5432SChen Yu case INTEL_FAM6_COMETLAKE: 4903af239c44SPeter Zijlstra return INTEL_FAM6_SKYLAKE_L; 4904937807d3SLen Brown 4905af239c44SPeter Zijlstra case INTEL_FAM6_ICELAKE_L: 4906d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 49074bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE_L: 49084bf7132aSChen Yu case INTEL_FAM6_TIGERLAKE: 4909e7af1ed3SLen Brown case INTEL_FAM6_ROCKETLAKE: 4910e7af1ed3SLen Brown case INTEL_FAM6_LAKEFIELD: 4911e7af1ed3SLen Brown case INTEL_FAM6_ALDERLAKE: 4912af239c44SPeter Zijlstra return INTEL_FAM6_CANNONLAKE_L; 4913b62b3184SZhang Rui 491477e5517cSIngo Molnar case INTEL_FAM6_ATOM_TREMONT_D: 491577e5517cSIngo Molnar return INTEL_FAM6_ATOM_GOLDMONT_D; 491623274fafSChen Yu 4917d7814c30SChen Yu case INTEL_FAM6_ATOM_TREMONT_L: 4918d7814c30SChen Yu return INTEL_FAM6_ATOM_TREMONT; 4919d7814c30SChen Yu 492023274fafSChen Yu case INTEL_FAM6_ICELAKE_X: 4921e7af1ed3SLen Brown case INTEL_FAM6_SAPPHIRERAPIDS_X: 492223274fafSChen Yu return INTEL_FAM6_SKYLAKE_X; 4923f5a4c76aSLen Brown } 4924f5a4c76aSLen Brown return model; 4925f5a4c76aSLen Brown } 4926d76bb7a0SLen Brown 4927d76bb7a0SLen Brown void print_dev_latency(void) 4928d76bb7a0SLen Brown { 4929d76bb7a0SLen Brown char *path = "/dev/cpu_dma_latency"; 4930d76bb7a0SLen Brown int fd; 4931d76bb7a0SLen Brown int value; 4932d76bb7a0SLen Brown int retval; 4933d76bb7a0SLen Brown 4934d76bb7a0SLen Brown fd = open(path, O_RDONLY); 4935d76bb7a0SLen Brown if (fd < 0) { 4936d76bb7a0SLen Brown warn("fopen %s\n", path); 4937d76bb7a0SLen Brown return; 4938d76bb7a0SLen Brown } 4939d76bb7a0SLen Brown 4940d76bb7a0SLen Brown retval = read(fd, (void *)&value, sizeof(int)); 4941d76bb7a0SLen Brown if (retval != sizeof(int)) { 4942d76bb7a0SLen Brown warn("read %s\n", path); 4943d76bb7a0SLen Brown close(fd); 4944d76bb7a0SLen Brown return; 4945d76bb7a0SLen Brown } 4946d76bb7a0SLen Brown fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", 4947d76bb7a0SLen Brown value, value == 2000000000 ? "default" : "constrained"); 4948d76bb7a0SLen Brown 4949d76bb7a0SLen Brown close(fd); 4950d76bb7a0SLen Brown } 4951d76bb7a0SLen Brown 4952fcd17211SLen Brown void process_cpuid() 4953103a8feaSLen Brown { 495434041551SLen Brown unsigned int eax, ebx, ecx, edx; 495534041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 4956b3a34e93SLen Brown unsigned int has_turbo; 4957103a8feaSLen Brown 4958103a8feaSLen Brown eax = ebx = ecx = edx = 0; 4959103a8feaSLen Brown 49605aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 4961103a8feaSLen Brown 496234041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 4963103a8feaSLen Brown genuine_intel = 1; 496434041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 496534041551SLen Brown authentic_amd = 1; 4966c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 4967c1c10cc7SPu Wen hygon_genuine = 1; 4968103a8feaSLen Brown 496996e47158SLen Brown if (!quiet) 4970b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 4971103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 4972103a8feaSLen Brown 49735aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 4974103a8feaSLen Brown family = (fms >> 8) & 0xf; 4975103a8feaSLen Brown model = (fms >> 4) & 0xf; 4976103a8feaSLen Brown stepping = fms & 0xf; 49775aa3d1a2SCalvin Walton if (family == 0xf) 49785aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 49795aa3d1a2SCalvin Walton if (family >= 6) 4980103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 498134041551SLen Brown ecx_flags = ecx; 498234041551SLen Brown edx_flags = edx; 4983103a8feaSLen Brown 4984103a8feaSLen Brown /* 4985103a8feaSLen Brown * check max extended function levels of CPUID. 4986103a8feaSLen Brown * This is needed to check for invariant TSC. 4987103a8feaSLen Brown * This check is valid for both Intel and AMD. 4988103a8feaSLen Brown */ 4989103a8feaSLen Brown ebx = ecx = edx = 0; 49905aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 4991103a8feaSLen Brown 499234041551SLen Brown if (!quiet) { 499334041551SLen Brown fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 499434041551SLen Brown max_level, max_extended_level, family, model, stepping, family, model, stepping); 499534041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 499634041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 499734041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 499834041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 499934041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 500034041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 500134041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 500234041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 500334041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 500434041551SLen Brown edx_flags & (1 << 28) ? "HT" : "-", 500534041551SLen Brown edx_flags & (1 << 29) ? "TM" : "-"); 500634041551SLen Brown } 5007f5a4c76aSLen Brown if (genuine_intel) 5008f5a4c76aSLen Brown model = intel_model_duplicates(model); 500934041551SLen Brown 501034041551SLen Brown if (!(edx_flags & (1 << 5))) 501134041551SLen Brown errx(1, "CPUID: no MSR"); 501234041551SLen Brown 501361a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 5014103a8feaSLen Brown 5015103a8feaSLen Brown /* 5016103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 5017103a8feaSLen Brown * this check is valid for both Intel and AMD 5018103a8feaSLen Brown */ 50195aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 50208209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 5021d7899447SLen Brown } 5022103a8feaSLen Brown 5023103a8feaSLen Brown /* 5024103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 5025103a8feaSLen Brown * this check is valid for both Intel and AMD 5026103a8feaSLen Brown */ 5027103a8feaSLen Brown 50285aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 50298209e054SThomas Renninger has_aperf = ecx & (1 << 0); 5030812db3f7SLen Brown if (has_aperf) { 5031812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 5032812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 5033812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 5034812db3f7SLen Brown } 5035889facbeSLen Brown do_dts = eax & (1 << 0); 5036812db3f7SLen Brown if (do_dts) 5037812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 5038b3a34e93SLen Brown has_turbo = eax & (1 << 1); 5039889facbeSLen Brown do_ptm = eax & (1 << 6); 5040812db3f7SLen Brown if (do_ptm) 5041812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 50427f5c258eSLen Brown has_hwp = eax & (1 << 7); 50437f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 50447f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 50457f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 50467f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 5047889facbeSLen Brown has_epb = ecx & (1 << 3); 5048889facbeSLen Brown 504996e47158SLen Brown if (!quiet) 5050b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 50517f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 50527f5c258eSLen Brown has_aperf ? "" : "No-", 5053b3a34e93SLen Brown has_turbo ? "" : "No-", 50547f5c258eSLen Brown do_dts ? "" : "No-", 50557f5c258eSLen Brown do_ptm ? "" : "No-", 50567f5c258eSLen Brown has_hwp ? "" : "No-", 50577f5c258eSLen Brown has_hwp_notify ? "" : "No-", 50587f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 50597f5c258eSLen Brown has_hwp_epp ? "" : "No-", 50607f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 50617f5c258eSLen Brown has_epb ? "" : "No-"); 5062103a8feaSLen Brown 506396e47158SLen Brown if (!quiet) 506469807a63SLen Brown decode_misc_enable_msr(); 506569807a63SLen Brown 506633148d67SLen Brown 506796e47158SLen Brown if (max_level >= 0x7 && !quiet) { 5068aa8d8cc7SLen Brown int has_sgx; 5069aa8d8cc7SLen Brown 5070aa8d8cc7SLen Brown ecx = 0; 5071aa8d8cc7SLen Brown 5072aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 5073aa8d8cc7SLen Brown 5074aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 5075aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 5076aa8d8cc7SLen Brown 5077aa8d8cc7SLen Brown if (has_sgx) 5078aa8d8cc7SLen Brown decode_feature_control_msr(); 5079aa8d8cc7SLen Brown } 5080aa8d8cc7SLen Brown 508161a87ba7SLen Brown if (max_level >= 0x15) { 50828a5bdf41SLen Brown unsigned int eax_crystal; 50838a5bdf41SLen Brown unsigned int ebx_tsc; 50848a5bdf41SLen Brown 50858a5bdf41SLen Brown /* 50868a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 50878a5bdf41SLen Brown */ 50888a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 50895aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 50908a5bdf41SLen Brown 50918a5bdf41SLen Brown if (ebx_tsc != 0) { 50928a5bdf41SLen Brown 509396e47158SLen Brown if (!quiet && (ebx != 0)) 5094b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 50958a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 50968a5bdf41SLen Brown 50978a5bdf41SLen Brown if (crystal_hz == 0) 50988a5bdf41SLen Brown switch(model) { 5099af239c44SPeter Zijlstra case INTEL_FAM6_SKYLAKE_L: /* SKL */ 5100e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 5101e8efbc80SLen Brown break; 51025ebb34edSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_D: /* DNV */ 5103ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 5104ec53e594SLen Brown break; 5105869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 5106f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 5107e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 51088a5bdf41SLen Brown break; 51098a5bdf41SLen Brown default: 51108a5bdf41SLen Brown crystal_hz = 0; 51118a5bdf41SLen Brown } 51128a5bdf41SLen Brown 51138a5bdf41SLen Brown if (crystal_hz) { 51148a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 511596e47158SLen Brown if (!quiet) 5116b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 51178a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 51188a5bdf41SLen Brown } 51198a5bdf41SLen Brown } 51208a5bdf41SLen Brown } 512161a87ba7SLen Brown if (max_level >= 0x16) { 512261a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 512361a87ba7SLen Brown 512461a87ba7SLen Brown /* 512561a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 512661a87ba7SLen Brown */ 512761a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 512861a87ba7SLen Brown 51295aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 513096e47158SLen Brown if (!quiet) 5131b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 513261a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 513361a87ba7SLen Brown } 51348a5bdf41SLen Brown 5135b2b34dfeSHubert Chrzaniuk if (has_aperf) 5136b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 5137b2b34dfeSHubert Chrzaniuk 5138812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 5139812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 5140812db3f7SLen Brown 5141812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 5142812db3f7SLen Brown do_nhm_platform_info = 1; 5143812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 5144812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 5145812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 5146812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 5147812db3f7SLen Brown } 5148d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 5149812db3f7SLen Brown 5150812db3f7SLen Brown if (do_snb_cstates) 5151812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 5152812db3f7SLen Brown 51535a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 51540f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 51550f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 51560f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 51570f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 51580f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 51590f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 51600f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 51610f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 51620539ba11SLen Brown if (has_slv_msrs(family, model)) { 51630f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 51640f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 51650f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 51660f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 51670539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 51680539ba11SLen Brown use_c1_residency_msr = 1; 51690539ba11SLen Brown } 51707170a374SLen Brown if (is_dnv(family, model)) { 51717170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 51727170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 51737170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 51747170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 51757170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 51767170a374SLen Brown use_c1_residency_msr = 1; 51777170a374SLen Brown } 517834c76197SLen Brown if (is_skx(family, model)) { 517934c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 518034c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 518134c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 518234c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 518334c76197SLen Brown } 5184ade0ebacSLen Brown if (is_bdx(family, model)) { 5185ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 5186ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 5187ade0ebacSLen Brown } 5188570992fcSLen Brown if (has_c8910_msrs(family, model)) { 5189*c315a09bSLen Brown if (pkg_cstate_limit >= PCL__8) 51900f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 5191*c315a09bSLen Brown if (pkg_cstate_limit >= PCL__9) 51920f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 5193*c315a09bSLen Brown if (pkg_cstate_limit >= PCL_10) 51940f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 51950f47c08dSLen Brown } 5196570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 5197a99d8730SLen Brown if (has_skl_msrs(family, model)) { 5198a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 5199a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 5200a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 5201a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 5202a99d8730SLen Brown } 5203144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 5204fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 5205103a8feaSLen Brown 5206f6708400SChen Yu if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || 5207f6708400SChen Yu is_ehl(family, model)) 5208562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 5209103a8feaSLen Brown 521096e47158SLen Brown if (!quiet) 5211f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 5212f0057310SLen Brown 521396e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 521471616c8eSLen Brown decode_c6_demotion_policy_msr(); 521571616c8eSLen Brown 5216889facbeSLen Brown rapl_probe(family, model); 52173a9a941dSLen Brown perf_limit_reasons_probe(family, model); 5218ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 5219889facbeSLen Brown 522096e47158SLen Brown if (!quiet) 52211b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 5222fcd17211SLen Brown 522341618e63SLen Brown if (!quiet) 5224d76bb7a0SLen Brown print_dev_latency(); 5225d76bb7a0SLen Brown if (!quiet) 522641618e63SLen Brown dump_sysfs_cstate_config(); 52277293fccdSLen Brown if (!quiet) 52287293fccdSLen Brown dump_sysfs_pstate_config(); 522941618e63SLen Brown 5230a2b7b749SLen Brown if (has_skl_msrs(family, model)) 5231a2b7b749SLen Brown calculate_tsc_tweak(); 5232a2b7b749SLen Brown 5233812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 5234812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 5235fdf676e5SLen Brown 5236812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 5237812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 523827d47356SLen Brown 5239be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 5240be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 5241be0e54c4SLen Brown else 5242be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 5243be0e54c4SLen Brown 52441f81c5efSLen Brown if (!access(sys_lpi_file_sysfs, R_OK)) { 52451f81c5efSLen Brown sys_lpi_file = sys_lpi_file_sysfs; 5246be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 52471f81c5efSLen Brown } else if (!access(sys_lpi_file_debugfs, R_OK)) { 52481f81c5efSLen Brown sys_lpi_file = sys_lpi_file_debugfs; 52491f81c5efSLen Brown BIC_PRESENT(BIC_SYS_LPI); 52501f81c5efSLen Brown } else { 52511f81c5efSLen Brown sys_lpi_file_sysfs = NULL; 5252be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 52531f81c5efSLen Brown } 5254be0e54c4SLen Brown 525596e47158SLen Brown if (!quiet) 525633148d67SLen Brown decode_misc_feature_control(); 525733148d67SLen Brown 5258889facbeSLen Brown return; 5259103a8feaSLen Brown } 5260103a8feaSLen Brown 5261103a8feaSLen Brown /* 5262103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 5263103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 5264103a8feaSLen Brown */ 5265103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 5266103a8feaSLen Brown { 5267103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 5268103a8feaSLen Brown return 1; 5269103a8feaSLen Brown else 5270103a8feaSLen Brown return 0; 5271103a8feaSLen Brown } 5272103a8feaSLen Brown 5273103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 5274103a8feaSLen Brown { 5275103a8feaSLen Brown return 0; 5276103a8feaSLen Brown } 5277103a8feaSLen Brown 5278c98d5d94SLen Brown void topology_probe() 5279c98d5d94SLen Brown { 5280c98d5d94SLen Brown int i; 5281c98d5d94SLen Brown int max_core_id = 0; 5282c98d5d94SLen Brown int max_package_id = 0; 52836de68fe1SLen Brown int max_die_id = 0; 5284c98d5d94SLen Brown int max_siblings = 0; 5285c98d5d94SLen Brown 5286c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 5287843c5791SPrarit Bhargava set_max_cpu_num(); 5288c98d5d94SLen Brown topo.num_cpus = 0; 5289c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 5290c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 5291812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 5292c98d5d94SLen Brown 5293d8af6f5fSLen Brown if (debug > 1) 5294b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 5295c98d5d94SLen Brown 5296c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 5297b2c95d90SJosh Triplett if (cpus == NULL) 5298b2c95d90SJosh Triplett err(1, "calloc cpus"); 5299c98d5d94SLen Brown 5300c98d5d94SLen Brown /* 5301c98d5d94SLen Brown * Allocate and initialize cpu_present_set 5302c98d5d94SLen Brown */ 5303c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5304b2c95d90SJosh Triplett if (cpu_present_set == NULL) 5305b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5306c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5307c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 5308c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 5309c98d5d94SLen Brown 5310c98d5d94SLen Brown /* 53111ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 53121ef7d21aSLen Brown */ 53131ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 53141ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 53151ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 53161ef7d21aSLen Brown err(1, "cpu%d not present", i); 53171ef7d21aSLen Brown } 53181ef7d21aSLen Brown 53191ef7d21aSLen Brown /* 5320c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 5321c98d5d94SLen Brown */ 5322c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 5323b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 5324b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 5325c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 5326c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 5327c98d5d94SLen Brown 53288cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 5329c98d5d94SLen Brown 5330c98d5d94SLen Brown /* 5331c98d5d94SLen Brown * For online cpus 5332c98d5d94SLen Brown * find max_core_id, max_package_id 5333c98d5d94SLen Brown */ 5334c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 5335c98d5d94SLen Brown int siblings; 5336c98d5d94SLen Brown 5337c98d5d94SLen Brown if (cpu_is_not_present(i)) { 5338d8af6f5fSLen Brown if (debug > 1) 5339b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5340c98d5d94SLen Brown continue; 5341c98d5d94SLen Brown } 5342c98d5d94SLen Brown 53430e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 53440e2d8f05SLen Brown 53450e2d8f05SLen Brown /* get package information */ 5346c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5347c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5348c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5349c98d5d94SLen Brown 53506de68fe1SLen Brown /* get die information */ 53516de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 53526de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 53536de68fe1SLen Brown max_die_id = cpus[i].die_id; 53546de68fe1SLen Brown 53550e2d8f05SLen Brown /* get numa node information */ 5356ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5357ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5358ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 53590e2d8f05SLen Brown 53600e2d8f05SLen Brown /* get core information */ 53610e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 53620e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 53630e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 53640e2d8f05SLen Brown 53650e2d8f05SLen Brown /* get thread information */ 53660e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5367c98d5d94SLen Brown if (siblings > max_siblings) 5368c98d5d94SLen Brown max_siblings = siblings; 53694f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 53708cb48b32SPrarit Bhargava topo.num_cores++; 5371c98d5d94SLen Brown } 5372ef605741SPrarit Bhargava 537370a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5374d8af6f5fSLen Brown if (debug > 1) 5375b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 537670a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 537770a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5378812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5379c98d5d94SLen Brown 53806de68fe1SLen Brown topo.num_die = max_die_id + 1; 53816de68fe1SLen Brown if (debug > 1) 53826de68fe1SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", 53836de68fe1SLen Brown max_die_id, topo.num_die); 53846de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 53856de68fe1SLen Brown BIC_PRESENT(BIC_Die); 53866de68fe1SLen Brown 5387c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5388d8af6f5fSLen Brown if (debug > 1) 5389b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 5390c98d5d94SLen Brown max_package_id, topo.num_packages); 53917da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5392812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5393c98d5d94SLen Brown 5394ef605741SPrarit Bhargava set_node_data(); 5395ef605741SPrarit Bhargava if (debug > 1) 539670a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 539701235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 539801235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5399ef605741SPrarit Bhargava 540070a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5401d8af6f5fSLen Brown if (debug > 1) 5402b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 54032ffbb224SPrarit Bhargava 54042ffbb224SPrarit Bhargava if (debug < 1) 54052ffbb224SPrarit Bhargava return; 54062ffbb224SPrarit Bhargava 54072ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 54080ec712e3SLen Brown if (cpu_is_not_present(i)) 54090ec712e3SLen Brown continue; 54102ffbb224SPrarit Bhargava fprintf(outf, 54116de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 54126de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 54132ffbb224SPrarit Bhargava cpus[i].physical_node_id, 54142ffbb224SPrarit Bhargava cpus[i].logical_node_id, 54152ffbb224SPrarit Bhargava cpus[i].physical_core_id, 54162ffbb224SPrarit Bhargava cpus[i].thread_id); 54172ffbb224SPrarit Bhargava } 54182ffbb224SPrarit Bhargava 5419c98d5d94SLen Brown } 5420c98d5d94SLen Brown 5421c98d5d94SLen Brown void 542240f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c, 542340f5cfe7SPrarit Bhargava struct pkg_data **p) 5424c98d5d94SLen Brown { 5425c98d5d94SLen Brown int i; 542640f5cfe7SPrarit Bhargava int num_cores = topo.cores_per_node * topo.nodes_per_pkg * 542740f5cfe7SPrarit Bhargava topo.num_packages; 542840f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5429c98d5d94SLen Brown 543040f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5431c98d5d94SLen Brown if (*t == NULL) 5432c98d5d94SLen Brown goto error; 5433c98d5d94SLen Brown 543440f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5435c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5436c98d5d94SLen Brown 543740f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5438c98d5d94SLen Brown if (*c == NULL) 5439c98d5d94SLen Brown goto error; 5440c98d5d94SLen Brown 544140f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5442c98d5d94SLen Brown (*c)[i].core_id = -1; 5443c98d5d94SLen Brown 5444678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5445c98d5d94SLen Brown if (*p == NULL) 5446c98d5d94SLen Brown goto error; 5447c98d5d94SLen Brown 5448c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5449c98d5d94SLen Brown (*p)[i].package_id = i; 5450c98d5d94SLen Brown 5451c98d5d94SLen Brown return; 5452c98d5d94SLen Brown error: 5453b2c95d90SJosh Triplett err(1, "calloc counters"); 5454c98d5d94SLen Brown } 5455c98d5d94SLen Brown /* 5456c98d5d94SLen Brown * init_counter() 5457c98d5d94SLen Brown * 5458c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5459c98d5d94SLen Brown */ 5460c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 54618cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 5462c98d5d94SLen Brown { 54638cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 546440f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 54658cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 54668cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5467c98d5d94SLen Brown struct thread_data *t; 5468c98d5d94SLen Brown struct core_data *c; 5469c98d5d94SLen Brown struct pkg_data *p; 5470c98d5d94SLen Brown 547142dd4520SNathan Ciobanu 547242dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 547342dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 547442dd4520SNathan Ciobanu */ 547542dd4520SNathan Ciobanu if (node_id < 0) 547642dd4520SNathan Ciobanu node_id = 0; 547742dd4520SNathan Ciobanu 547840f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 547940f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 54808cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5481c98d5d94SLen Brown 5482c98d5d94SLen Brown t->cpu_id = cpu_id; 54838cb48b32SPrarit Bhargava if (thread_id == 0) { 5484c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5485c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5486c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5487c98d5d94SLen Brown } 5488c98d5d94SLen Brown 54898cb48b32SPrarit Bhargava c->core_id = core_id; 54908cb48b32SPrarit Bhargava p->package_id = pkg_id; 5491c98d5d94SLen Brown } 5492c98d5d94SLen Brown 5493c98d5d94SLen Brown 5494c98d5d94SLen Brown int initialize_counters(int cpu_id) 5495c98d5d94SLen Brown { 54968cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 54978cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5498c98d5d94SLen Brown return 0; 5499c98d5d94SLen Brown } 5500c98d5d94SLen Brown 5501c98d5d94SLen Brown void allocate_output_buffer() 5502c98d5d94SLen Brown { 5503eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5504c98d5d94SLen Brown outp = output_buffer; 5505b2c95d90SJosh Triplett if (outp == NULL) 5506b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5507c98d5d94SLen Brown } 550836229897SLen Brown void allocate_fd_percpu(void) 550936229897SLen Brown { 551001a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 551136229897SLen Brown if (fd_percpu == NULL) 551236229897SLen Brown err(-1, "calloc fd_percpu"); 551336229897SLen Brown } 5514562a2d37SLen Brown void allocate_irq_buffers(void) 5515562a2d37SLen Brown { 5516562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5517562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5518562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5519c98d5d94SLen Brown 552001a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5521562a2d37SLen Brown if (irqs_per_cpu == NULL) 552201a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5523562a2d37SLen Brown } 5524c98d5d94SLen Brown void setup_all_buffers(void) 5525c98d5d94SLen Brown { 5526c98d5d94SLen Brown topology_probe(); 5527562a2d37SLen Brown allocate_irq_buffers(); 552836229897SLen Brown allocate_fd_percpu(); 5529c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5530c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5531c98d5d94SLen Brown allocate_output_buffer(); 5532c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5533c98d5d94SLen Brown } 55343b4d5c7fSAndy Shevchenko 55357ce7d5deSPrarit Bhargava void set_base_cpu(void) 55367ce7d5deSPrarit Bhargava { 55377ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 55387ce7d5deSPrarit Bhargava if (base_cpu < 0) 55397ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 55407ce7d5deSPrarit Bhargava 55417ce7d5deSPrarit Bhargava if (debug > 1) 5542b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 55437ce7d5deSPrarit Bhargava } 55447ce7d5deSPrarit Bhargava 5545103a8feaSLen Brown void turbostat_init() 5546103a8feaSLen Brown { 55477ce7d5deSPrarit Bhargava setup_all_buffers(); 55487ce7d5deSPrarit Bhargava set_base_cpu(); 5549103a8feaSLen Brown check_dev_msr(); 555098481e79SLen Brown check_permissions(); 5551fcd17211SLen Brown process_cpuid(); 5552103a8feaSLen Brown 5553103a8feaSLen Brown 555496e47158SLen Brown if (!quiet) 55557f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 55567f5c258eSLen Brown 555796e47158SLen Brown if (!quiet) 5558889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5559889facbeSLen Brown 556096e47158SLen Brown if (!quiet) 55613a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 55623a9a941dSLen Brown 556396e47158SLen Brown if (!quiet) 5564889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5565889facbeSLen Brown 5566889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5567889facbeSLen Brown 556896e47158SLen Brown if (!quiet) 5569889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 55705a63426eSLen Brown 557196e47158SLen Brown if (!quiet && do_irtl_snb) 55725a63426eSLen Brown print_irtl(); 5573103a8feaSLen Brown } 5574103a8feaSLen Brown 5575103a8feaSLen Brown int fork_it(char **argv) 5576103a8feaSLen Brown { 5577103a8feaSLen Brown pid_t child_pid; 5578d91bb17cSLen Brown int status; 5579d15cf7c1SLen Brown 5580218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5581d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 55824c2122d4SLen Brown first_counter_read = 0; 5583d91bb17cSLen Brown if (status) 5584d91bb17cSLen Brown exit(status); 5585d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5586d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5587103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5588103a8feaSLen Brown 5589103a8feaSLen Brown child_pid = fork(); 5590103a8feaSLen Brown if (!child_pid) { 5591103a8feaSLen Brown /* child */ 5592103a8feaSLen Brown execvp(argv[0], argv); 55930815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5594103a8feaSLen Brown } else { 5595103a8feaSLen Brown 5596103a8feaSLen Brown /* parent */ 5597b2c95d90SJosh Triplett if (child_pid == -1) 5598b2c95d90SJosh Triplett err(1, "fork"); 5599103a8feaSLen Brown 5600103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5601103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5602b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5603b2c95d90SJosh Triplett err(status, "waitpid"); 56042a954966SDavid Arcari 56052a954966SDavid Arcari if (WIFEXITED(status)) 56062a954966SDavid Arcari status = WEXITSTATUS(status); 5607103a8feaSLen Brown } 5608c98d5d94SLen Brown /* 5609c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5610c98d5d94SLen Brown * because re-starting is problematic when forking 5611c98d5d94SLen Brown */ 5612218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5613c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5614103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5615103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5616ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5617ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5618ba3dec99SLen Brown else { 5619c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5620c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5621ba3dec99SLen Brown } 5622103a8feaSLen Brown 5623b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 5624b7d8c148SLen Brown 5625b7d8c148SLen Brown flush_output_stderr(); 5626103a8feaSLen Brown 5627d91bb17cSLen Brown return status; 5628103a8feaSLen Brown } 5629103a8feaSLen Brown 56303b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 56313b4d5c7fSAndy Shevchenko { 56323b4d5c7fSAndy Shevchenko int status; 56333b4d5c7fSAndy Shevchenko 5634218f0e8dSLen Brown snapshot_proc_sysfs_files(); 56353b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 56363b4d5c7fSAndy Shevchenko if (status) 56373b4d5c7fSAndy Shevchenko return status; 56383b4d5c7fSAndy Shevchenko 56393b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 56403b4d5c7fSAndy Shevchenko if (status) 56413b4d5c7fSAndy Shevchenko return status; 56423b4d5c7fSAndy Shevchenko 5643b7d8c148SLen Brown flush_output_stdout(); 56443b4d5c7fSAndy Shevchenko 56453b4d5c7fSAndy Shevchenko return status; 56463b4d5c7fSAndy Shevchenko } 56473b4d5c7fSAndy Shevchenko 5648d8af6f5fSLen Brown void print_version() { 5649b95fffb9SLen Brown fprintf(outf, "turbostat version 20.03.20" 5650d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 5651d8af6f5fSLen Brown } 5652d8af6f5fSLen Brown 5653495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5654495c7654SLen Brown unsigned int width, enum counter_scope scope, 565541618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5656388e9c81SLen Brown { 5657388e9c81SLen Brown struct msr_counter *msrp; 5658388e9c81SLen Brown 5659388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5660388e9c81SLen Brown if (msrp == NULL) { 5661388e9c81SLen Brown perror("calloc"); 5662388e9c81SLen Brown exit(1); 5663388e9c81SLen Brown } 5664388e9c81SLen Brown 5665388e9c81SLen Brown msrp->msr_num = msr_num; 5666d8d005baSLen Brown strncpy(msrp->name, name, NAME_BYTES - 1); 5667495c7654SLen Brown if (path) 5668d8d005baSLen Brown strncpy(msrp->path, path, PATH_BYTES - 1); 5669388e9c81SLen Brown msrp->width = width; 5670388e9c81SLen Brown msrp->type = type; 5671388e9c81SLen Brown msrp->format = format; 567241618e63SLen Brown msrp->flags = flags; 5673388e9c81SLen Brown 5674388e9c81SLen Brown switch (scope) { 5675388e9c81SLen Brown 5676388e9c81SLen Brown case SCOPE_CPU: 5677388e9c81SLen Brown msrp->next = sys.tp; 5678388e9c81SLen Brown sys.tp = msrp; 5679678a3bd1SLen Brown sys.added_thread_counters++; 56800748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5681678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5682678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5683678a3bd1SLen Brown exit(-1); 5684678a3bd1SLen Brown } 5685388e9c81SLen Brown break; 5686388e9c81SLen Brown 5687388e9c81SLen Brown case SCOPE_CORE: 5688388e9c81SLen Brown msrp->next = sys.cp; 5689388e9c81SLen Brown sys.cp = msrp; 5690678a3bd1SLen Brown sys.added_core_counters++; 5691678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5692678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5693678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5694678a3bd1SLen Brown exit(-1); 5695678a3bd1SLen Brown } 5696388e9c81SLen Brown break; 5697388e9c81SLen Brown 5698388e9c81SLen Brown case SCOPE_PACKAGE: 5699388e9c81SLen Brown msrp->next = sys.pp; 5700388e9c81SLen Brown sys.pp = msrp; 5701678a3bd1SLen Brown sys.added_package_counters++; 5702678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5703678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5704678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5705678a3bd1SLen Brown exit(-1); 5706678a3bd1SLen Brown } 5707388e9c81SLen Brown break; 5708388e9c81SLen Brown } 5709388e9c81SLen Brown 5710388e9c81SLen Brown return 0; 5711388e9c81SLen Brown } 5712388e9c81SLen Brown 5713388e9c81SLen Brown void parse_add_command(char *add_command) 5714388e9c81SLen Brown { 5715388e9c81SLen Brown int msr_num = 0; 5716495c7654SLen Brown char *path = NULL; 57170f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5718388e9c81SLen Brown int width = 64; 5719388e9c81SLen Brown int fail = 0; 5720388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5721388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5722388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5723388e9c81SLen Brown 5724388e9c81SLen Brown while (add_command) { 5725388e9c81SLen Brown 5726388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5727388e9c81SLen Brown goto next; 5728388e9c81SLen Brown 5729388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5730388e9c81SLen Brown goto next; 5731388e9c81SLen Brown 5732495c7654SLen Brown if (*add_command == '/') { 5733495c7654SLen Brown path = add_command; 5734495c7654SLen Brown goto next; 5735495c7654SLen Brown } 5736495c7654SLen Brown 5737388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5738388e9c81SLen Brown if ((width == 32) || (width == 64)) 5739388e9c81SLen Brown goto next; 5740388e9c81SLen Brown width = 64; 5741388e9c81SLen Brown } 5742388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 5743388e9c81SLen Brown scope = SCOPE_CPU; 5744388e9c81SLen Brown goto next; 5745388e9c81SLen Brown } 5746388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 5747388e9c81SLen Brown scope = SCOPE_CORE; 5748388e9c81SLen Brown goto next; 5749388e9c81SLen Brown } 5750388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 5751388e9c81SLen Brown scope = SCOPE_PACKAGE; 5752388e9c81SLen Brown goto next; 5753388e9c81SLen Brown } 5754388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 5755388e9c81SLen Brown type = COUNTER_CYCLES; 5756388e9c81SLen Brown goto next; 5757388e9c81SLen Brown } 5758388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 5759388e9c81SLen Brown type = COUNTER_SECONDS; 5760388e9c81SLen Brown goto next; 5761388e9c81SLen Brown } 576241618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 576341618e63SLen Brown type = COUNTER_USEC; 576441618e63SLen Brown goto next; 576541618e63SLen Brown } 5766388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 5767388e9c81SLen Brown format = FORMAT_RAW; 5768388e9c81SLen Brown goto next; 5769388e9c81SLen Brown } 5770388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 5771388e9c81SLen Brown format = FORMAT_DELTA; 5772388e9c81SLen Brown goto next; 5773388e9c81SLen Brown } 5774388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 5775388e9c81SLen Brown format = FORMAT_PERCENT; 5776388e9c81SLen Brown goto next; 5777388e9c81SLen Brown } 5778388e9c81SLen Brown 5779388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 5780388e9c81SLen Brown char *eos; 5781388e9c81SLen Brown 5782388e9c81SLen Brown eos = strchr(name_buffer, ','); 5783388e9c81SLen Brown if (eos) 5784388e9c81SLen Brown *eos = '\0'; 5785388e9c81SLen Brown goto next; 5786388e9c81SLen Brown } 5787388e9c81SLen Brown 5788388e9c81SLen Brown next: 5789388e9c81SLen Brown add_command = strchr(add_command, ','); 5790495c7654SLen Brown if (add_command) { 5791495c7654SLen Brown *add_command = '\0'; 5792388e9c81SLen Brown add_command++; 5793495c7654SLen Brown } 5794388e9c81SLen Brown 5795388e9c81SLen Brown } 5796495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 5797495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 5798388e9c81SLen Brown fail++; 5799388e9c81SLen Brown } 5800388e9c81SLen Brown 5801388e9c81SLen Brown /* generate default column header */ 5802388e9c81SLen Brown if (*name_buffer == '\0') { 5803388e9c81SLen Brown if (width == 32) 58045f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5805388e9c81SLen Brown else 58065f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5807388e9c81SLen Brown } 5808388e9c81SLen Brown 580941618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 5810388e9c81SLen Brown fail++; 5811388e9c81SLen Brown 5812388e9c81SLen Brown if (fail) { 5813388e9c81SLen Brown help(); 5814388e9c81SLen Brown exit(1); 5815388e9c81SLen Brown } 5816388e9c81SLen Brown } 581741618e63SLen Brown 5818dd778a5eSLen Brown int is_deferred_skip(char *name) 5819dd778a5eSLen Brown { 5820dd778a5eSLen Brown int i; 5821dd778a5eSLen Brown 5822dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 5823dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 5824dd778a5eSLen Brown return 1; 5825dd778a5eSLen Brown return 0; 5826dd778a5eSLen Brown } 5827dd778a5eSLen Brown 582841618e63SLen Brown void probe_sysfs(void) 582941618e63SLen Brown { 583041618e63SLen Brown char path[64]; 583141618e63SLen Brown char name_buf[16]; 583241618e63SLen Brown FILE *input; 583341618e63SLen Brown int state; 583441618e63SLen Brown char *sp; 583541618e63SLen Brown 583641618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 583741618e63SLen Brown return; 583841618e63SLen Brown 58390748eaf0SLen Brown for (state = 10; state >= 0; --state) { 584041618e63SLen Brown 584141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 584241618e63SLen Brown base_cpu, state); 584341618e63SLen Brown input = fopen(path, "r"); 584441618e63SLen Brown if (input == NULL) 584541618e63SLen Brown continue; 58468173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 58478173c336SBen Hutchings err(1, "%s: failed to read file", path); 584841618e63SLen Brown 584941618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 585041618e63SLen Brown sp = strchr(name_buf, '-'); 585141618e63SLen Brown if (!sp) 585241618e63SLen Brown sp = strchrnul(name_buf, '\n'); 585341618e63SLen Brown *sp = '%'; 585441618e63SLen Brown *(sp + 1) = '\0'; 585541618e63SLen Brown 5856fecb3bc8SDavid Arcari remove_underbar(name_buf); 5857fecb3bc8SDavid Arcari 585841618e63SLen Brown fclose(input); 585941618e63SLen Brown 586041618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 586141618e63SLen Brown 5862dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5863dd778a5eSLen Brown continue; 5864dd778a5eSLen Brown 586541618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 586641618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 586741618e63SLen Brown } 586841618e63SLen Brown 58690748eaf0SLen Brown for (state = 10; state >= 0; --state) { 587041618e63SLen Brown 587141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 587241618e63SLen Brown base_cpu, state); 587341618e63SLen Brown input = fopen(path, "r"); 587441618e63SLen Brown if (input == NULL) 587541618e63SLen Brown continue; 58768173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 58778173c336SBen Hutchings err(1, "%s: failed to read file", path); 587841618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 587941618e63SLen Brown sp = strchr(name_buf, '-'); 588041618e63SLen Brown if (!sp) 588141618e63SLen Brown sp = strchrnul(name_buf, '\n'); 588241618e63SLen Brown *sp = '\0'; 588341618e63SLen Brown fclose(input); 588441618e63SLen Brown 5885fecb3bc8SDavid Arcari remove_underbar(name_buf); 5886fecb3bc8SDavid Arcari 588741618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 588841618e63SLen Brown 5889dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5890dd778a5eSLen Brown continue; 5891dd778a5eSLen Brown 589241618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 589341618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 589441618e63SLen Brown } 589541618e63SLen Brown 589641618e63SLen Brown } 589741618e63SLen Brown 58981ef7d21aSLen Brown 58991ef7d21aSLen Brown /* 59001ef7d21aSLen Brown * parse cpuset with following syntax 59011ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 59021ef7d21aSLen Brown */ 59031ef7d21aSLen Brown void parse_cpu_command(char *optarg) 59041ef7d21aSLen Brown { 59051ef7d21aSLen Brown unsigned int start, end; 59061ef7d21aSLen Brown char *next; 59071ef7d21aSLen Brown 59084e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 59094e4e1e7cSLen Brown if (cpu_subset) 59104e4e1e7cSLen Brown goto error; 59114e4e1e7cSLen Brown show_core_only++; 59124e4e1e7cSLen Brown return; 59134e4e1e7cSLen Brown } 59144e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 59154e4e1e7cSLen Brown if (cpu_subset) 59164e4e1e7cSLen Brown goto error; 59174e4e1e7cSLen Brown show_pkg_only++; 59184e4e1e7cSLen Brown return; 59194e4e1e7cSLen Brown } 59204e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 59214e4e1e7cSLen Brown goto error; 59224e4e1e7cSLen Brown 59231ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 59241ef7d21aSLen Brown if (cpu_subset == NULL) 59251ef7d21aSLen Brown err(3, "CPU_ALLOC"); 59261ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 59271ef7d21aSLen Brown 59281ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 59291ef7d21aSLen Brown 59301ef7d21aSLen Brown next = optarg; 59311ef7d21aSLen Brown 59321ef7d21aSLen Brown while (next && *next) { 59331ef7d21aSLen Brown 59341ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 59351ef7d21aSLen Brown goto error; 59361ef7d21aSLen Brown 59371ef7d21aSLen Brown start = strtoul(next, &next, 10); 59381ef7d21aSLen Brown 59391ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 59401ef7d21aSLen Brown goto error; 59411ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 59421ef7d21aSLen Brown 59431ef7d21aSLen Brown if (*next == '\0') 59441ef7d21aSLen Brown break; 59451ef7d21aSLen Brown 59461ef7d21aSLen Brown if (*next == ',') { 59471ef7d21aSLen Brown next += 1; 59481ef7d21aSLen Brown continue; 59491ef7d21aSLen Brown } 59501ef7d21aSLen Brown 59511ef7d21aSLen Brown if (*next == '-') { 59521ef7d21aSLen Brown next += 1; /* start range */ 59531ef7d21aSLen Brown } else if (*next == '.') { 59541ef7d21aSLen Brown next += 1; 59551ef7d21aSLen Brown if (*next == '.') 59561ef7d21aSLen Brown next += 1; /* start range */ 59571ef7d21aSLen Brown else 59581ef7d21aSLen Brown goto error; 59591ef7d21aSLen Brown } 59601ef7d21aSLen Brown 59611ef7d21aSLen Brown end = strtoul(next, &next, 10); 59621ef7d21aSLen Brown if (end <= start) 59631ef7d21aSLen Brown goto error; 59641ef7d21aSLen Brown 59651ef7d21aSLen Brown while (++start <= end) { 59661ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 59671ef7d21aSLen Brown goto error; 59681ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 59691ef7d21aSLen Brown } 59701ef7d21aSLen Brown 59711ef7d21aSLen Brown if (*next == ',') 59721ef7d21aSLen Brown next += 1; 59731ef7d21aSLen Brown else if (*next != '\0') 59741ef7d21aSLen Brown goto error; 59751ef7d21aSLen Brown } 59761ef7d21aSLen Brown 59771ef7d21aSLen Brown return; 59781ef7d21aSLen Brown 59791ef7d21aSLen Brown error: 59804e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 59814e4e1e7cSLen Brown help(); 59821ef7d21aSLen Brown exit(-1); 59831ef7d21aSLen Brown } 59841ef7d21aSLen Brown 5985812db3f7SLen Brown 5986103a8feaSLen Brown void cmdline(int argc, char **argv) 5987103a8feaSLen Brown { 5988103a8feaSLen Brown int opt; 5989d8af6f5fSLen Brown int option_index = 0; 5990d8af6f5fSLen Brown static struct option long_options[] = { 5991388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 59921ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 5993d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 599496e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 59953f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 5996d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 5997023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 5998d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 5999812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 6000d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 6001c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 6002b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 600396e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 6004812db3f7SLen Brown {"show", required_argument, 0, 's'}, 6005d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 6006d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 6007d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 6008d8af6f5fSLen Brown {0, 0, 0, 0 } 6009d8af6f5fSLen Brown }; 6010103a8feaSLen Brown 6011103a8feaSLen Brown progname = argv[0]; 6012103a8feaSLen Brown 6013023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 6014d8af6f5fSLen Brown long_options, &option_index)) != -1) { 6015103a8feaSLen Brown switch (opt) { 6016388e9c81SLen Brown case 'a': 6017388e9c81SLen Brown parse_add_command(optarg); 6018388e9c81SLen Brown break; 60191ef7d21aSLen Brown case 'c': 60201ef7d21aSLen Brown parse_cpu_command(optarg); 60211ef7d21aSLen Brown break; 6022d8af6f5fSLen Brown case 'D': 6023d8af6f5fSLen Brown dump_only++; 60248e180f3cSLen Brown break; 60253f44a5c6SLen Brown case 'e': 60263f44a5c6SLen Brown /* --enable specified counter */ 60274c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 60283f44a5c6SLen Brown break; 6029d8af6f5fSLen Brown case 'd': 6030d8af6f5fSLen Brown debug++; 60313f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 60322f32edf1SLen Brown break; 6033812db3f7SLen Brown case 'H': 60343f44a5c6SLen Brown /* 60353f44a5c6SLen Brown * --hide: do not show those specified 60363f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 60373f44a5c6SLen Brown */ 60383f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 6039812db3f7SLen Brown break; 6040d8af6f5fSLen Brown case 'h': 6041d8af6f5fSLen Brown default: 6042d8af6f5fSLen Brown help(); 6043d8af6f5fSLen Brown exit(1); 6044d8af6f5fSLen Brown case 'i': 60452a0609c0SLen Brown { 60462a0609c0SLen Brown double interval = strtod(optarg, NULL); 60472a0609c0SLen Brown 60482a0609c0SLen Brown if (interval < 0.001) { 6049b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 60502a0609c0SLen Brown interval); 60512a0609c0SLen Brown exit(2); 60522a0609c0SLen Brown } 60532a0609c0SLen Brown 605447936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 6055b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 605647936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 60572a0609c0SLen Brown } 6058889facbeSLen Brown break; 60595c56be9aSDirk Brandewie case 'J': 60605c56be9aSDirk Brandewie rapl_joules++; 60615c56be9aSDirk Brandewie break; 6062c8ade361SLen Brown case 'l': 60633f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 6064c8ade361SLen Brown list_header_only++; 6065c8ade361SLen Brown quiet++; 6066c8ade361SLen Brown break; 6067b7d8c148SLen Brown case 'o': 6068b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 6069b7d8c148SLen Brown break; 607096e47158SLen Brown case 'q': 607196e47158SLen Brown quiet = 1; 607296e47158SLen Brown break; 6073023fe0acSChen Yu case 'n': 6074023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 6075023fe0acSChen Yu 6076023fe0acSChen Yu if (num_iterations <= 0) { 6077023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 6078023fe0acSChen Yu num_iterations); 6079023fe0acSChen Yu exit(2); 6080023fe0acSChen Yu } 6081023fe0acSChen Yu break; 6082812db3f7SLen Brown case 's': 60833f44a5c6SLen Brown /* 60843f44a5c6SLen Brown * --show: show only those specified 60853f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 60863f44a5c6SLen Brown * subsequent invocations can add to it. 60873f44a5c6SLen Brown */ 60883f44a5c6SLen Brown if (shown == 0) 60893f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 60903f44a5c6SLen Brown else 60913f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 60923f44a5c6SLen Brown shown = 1; 6093812db3f7SLen Brown break; 6094d8af6f5fSLen Brown case 'S': 6095d8af6f5fSLen Brown summary_only++; 6096d8af6f5fSLen Brown break; 6097d8af6f5fSLen Brown case 'T': 6098d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 6099d8af6f5fSLen Brown break; 6100d8af6f5fSLen Brown case 'v': 6101d8af6f5fSLen Brown print_version(); 6102d8af6f5fSLen Brown exit(0); 6103d8af6f5fSLen Brown break; 6104103a8feaSLen Brown } 6105103a8feaSLen Brown } 6106103a8feaSLen Brown } 6107103a8feaSLen Brown 6108103a8feaSLen Brown int main(int argc, char **argv) 6109103a8feaSLen Brown { 6110b7d8c148SLen Brown outf = stderr; 6111103a8feaSLen Brown cmdline(argc, argv); 6112103a8feaSLen Brown 611396e47158SLen Brown if (!quiet) 6114d8af6f5fSLen Brown print_version(); 6115103a8feaSLen Brown 611641618e63SLen Brown probe_sysfs(); 611741618e63SLen Brown 6118103a8feaSLen Brown turbostat_init(); 6119103a8feaSLen Brown 61203b4d5c7fSAndy Shevchenko /* dump counters and exit */ 61213b4d5c7fSAndy Shevchenko if (dump_only) 61223b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 61233b4d5c7fSAndy Shevchenko 6124c8ade361SLen Brown /* list header and exit */ 6125c8ade361SLen Brown if (list_header_only) { 6126c8ade361SLen Brown print_header(","); 6127c8ade361SLen Brown flush_output_stdout(); 6128c8ade361SLen Brown return 0; 6129c8ade361SLen Brown } 6130c8ade361SLen Brown 61319972d5d8SChen Yu msr_sum_record(); 6132103a8feaSLen Brown /* 6133103a8feaSLen Brown * if any params left, it must be a command to fork 6134103a8feaSLen Brown */ 6135103a8feaSLen Brown if (argc - optind) 6136103a8feaSLen Brown return fork_it(argv + optind); 6137103a8feaSLen Brown else 6138103a8feaSLen Brown turbostat_loop(); 6139103a8feaSLen Brown 6140103a8feaSLen Brown return 0; 6141103a8feaSLen Brown } 6142