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> 3398481e79SLen Brown #include <linux/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; 62*c1c10cc7SPu 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; 214889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 215889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 216889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 217889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 218889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 219889facbeSLen Brown unsigned int 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 262388e9c81SLen Brown struct sys_counters { 263678a3bd1SLen Brown unsigned int added_thread_counters; 264678a3bd1SLen Brown unsigned int added_core_counters; 265678a3bd1SLen Brown unsigned int added_package_counters; 266388e9c81SLen Brown struct msr_counter *tp; 267388e9c81SLen Brown struct msr_counter *cp; 268388e9c81SLen Brown struct msr_counter *pp; 269388e9c81SLen Brown } sys; 270388e9c81SLen Brown 271c98d5d94SLen Brown struct system_summary { 272c98d5d94SLen Brown struct thread_data threads; 273c98d5d94SLen Brown struct core_data cores; 274c98d5d94SLen Brown struct pkg_data packages; 275388e9c81SLen Brown } average; 276c98d5d94SLen Brown 2770e2d8f05SLen Brown struct cpu_topology { 2780e2d8f05SLen Brown int physical_package_id; 2796de68fe1SLen Brown int die_id; 2800e2d8f05SLen Brown int logical_cpu_id; 281ef605741SPrarit Bhargava int physical_node_id; 282ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 2830e2d8f05SLen Brown int physical_core_id; 2848cb48b32SPrarit Bhargava int thread_id; 2850e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 2860e2d8f05SLen Brown } *cpus; 287c98d5d94SLen Brown 288c98d5d94SLen Brown struct topo_params { 289c98d5d94SLen Brown int num_packages; 2906de68fe1SLen Brown int num_die; 291103a8feaSLen Brown int num_cpus; 292c98d5d94SLen Brown int num_cores; 293c98d5d94SLen Brown int max_cpu_num; 294ef605741SPrarit Bhargava int max_node_num; 29570a9c6e8SPrarit Bhargava int nodes_per_pkg; 29670a9c6e8SPrarit Bhargava int cores_per_node; 29770a9c6e8SPrarit Bhargava int threads_per_core; 298c98d5d94SLen Brown } topo; 299103a8feaSLen Brown 300c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 301103a8feaSLen Brown 302562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 303562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 304562a2d37SLen Brown 305c98d5d94SLen Brown void setup_all_buffers(void); 306103a8feaSLen Brown 307c98d5d94SLen Brown int cpu_is_not_present(int cpu) 308d15cf7c1SLen Brown { 309c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 310c98d5d94SLen Brown } 311c98d5d94SLen Brown /* 312c98d5d94SLen Brown * run func(thread, core, package) in topology order 313c98d5d94SLen Brown * skip non-present cpus 314c98d5d94SLen Brown */ 315c98d5d94SLen Brown 316c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 317c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 318c98d5d94SLen Brown { 31940f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 320c98d5d94SLen Brown 321c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 322df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 32370a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 324c98d5d94SLen Brown for (thread_no = 0; thread_no < 32570a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 326c98d5d94SLen Brown struct thread_data *t; 327c98d5d94SLen Brown struct core_data *c; 328c98d5d94SLen Brown struct pkg_data *p; 329c98d5d94SLen Brown 33040f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 33140f5cfe7SPrarit Bhargava core_no, node_no, 33240f5cfe7SPrarit Bhargava pkg_no); 333c98d5d94SLen Brown 334c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 335c98d5d94SLen Brown continue; 336c98d5d94SLen Brown 33740f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 33840f5cfe7SPrarit Bhargava node_no, pkg_no); 339c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 340c98d5d94SLen Brown 341c98d5d94SLen Brown retval = func(t, c, p); 342c98d5d94SLen Brown if (retval) 343c98d5d94SLen Brown return retval; 344c98d5d94SLen Brown } 345c98d5d94SLen Brown } 346c98d5d94SLen Brown } 34740f5cfe7SPrarit Bhargava } 348d15cf7c1SLen Brown return 0; 349d15cf7c1SLen Brown } 350d15cf7c1SLen Brown 35188c3281fSLen Brown int cpu_migrate(int cpu) 35288c3281fSLen Brown { 353c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 354c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 355c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 35688c3281fSLen Brown return -1; 35788c3281fSLen Brown else 35888c3281fSLen Brown return 0; 35988c3281fSLen Brown } 36036229897SLen Brown int get_msr_fd(int cpu) 361103a8feaSLen Brown { 362103a8feaSLen Brown char pathname[32]; 363103a8feaSLen Brown int fd; 364103a8feaSLen Brown 36536229897SLen Brown fd = fd_percpu[cpu]; 36636229897SLen Brown 36736229897SLen Brown if (fd) 36836229897SLen Brown return fd; 36936229897SLen Brown 370103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 371103a8feaSLen Brown fd = open(pathname, O_RDONLY); 37215aaa346SLen Brown if (fd < 0) 37398481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 374103a8feaSLen Brown 37536229897SLen Brown fd_percpu[cpu] = fd; 37636229897SLen Brown 37736229897SLen Brown return fd; 37836229897SLen Brown } 37936229897SLen Brown 38036229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 38136229897SLen Brown { 38236229897SLen Brown ssize_t retval; 38336229897SLen Brown 38436229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 38515aaa346SLen Brown 38698481e79SLen Brown if (retval != sizeof *msr) 387cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 38815aaa346SLen Brown 38915aaa346SLen Brown return 0; 390103a8feaSLen Brown } 391103a8feaSLen Brown 392fc04cc67SLen Brown /* 393bdd5ae3aSLen Brown * This list matches the column headers, except 394bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 395bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 396bdd5ae3aSLen Brown * matching on them for --show and --hide. 397fc04cc67SLen Brown */ 398812db3f7SLen Brown struct msr_counter bic[] = { 3993f44a5c6SLen Brown { 0x0, "usec" }, 4003f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 401812db3f7SLen Brown { 0x0, "Package" }, 402bdd5ae3aSLen Brown { 0x0, "Node" }, 403812db3f7SLen Brown { 0x0, "Avg_MHz" }, 404bdd5ae3aSLen Brown { 0x0, "Busy%" }, 405812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 406812db3f7SLen Brown { 0x0, "TSC_MHz" }, 407812db3f7SLen Brown { 0x0, "IRQ" }, 408495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 409bdd5ae3aSLen Brown { 0x0, "sysfs" }, 410812db3f7SLen Brown { 0x0, "CPU%c1" }, 411812db3f7SLen Brown { 0x0, "CPU%c3" }, 412812db3f7SLen Brown { 0x0, "CPU%c6" }, 413812db3f7SLen Brown { 0x0, "CPU%c7" }, 414812db3f7SLen Brown { 0x0, "ThreadC" }, 415812db3f7SLen Brown { 0x0, "CoreTmp" }, 416812db3f7SLen Brown { 0x0, "CoreCnt" }, 417812db3f7SLen Brown { 0x0, "PkgTmp" }, 418812db3f7SLen Brown { 0x0, "GFX%rc6" }, 419812db3f7SLen Brown { 0x0, "GFXMHz" }, 420812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 421812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 422812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 423812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 4240f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 4250f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 4264bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 427be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 428be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 429812db3f7SLen Brown { 0x0, "PkgWatt" }, 430812db3f7SLen Brown { 0x0, "CorWatt" }, 431812db3f7SLen Brown { 0x0, "GFXWatt" }, 432812db3f7SLen Brown { 0x0, "PkgCnt" }, 433812db3f7SLen Brown { 0x0, "RAMWatt" }, 434812db3f7SLen Brown { 0x0, "PKG_%" }, 435812db3f7SLen Brown { 0x0, "RAM_%" }, 436812db3f7SLen Brown { 0x0, "Pkg_J" }, 437812db3f7SLen Brown { 0x0, "Cor_J" }, 438812db3f7SLen Brown { 0x0, "GFX_J" }, 439812db3f7SLen Brown { 0x0, "RAM_J" }, 4400539ba11SLen Brown { 0x0, "Mod%c6" }, 441a99d8730SLen Brown { 0x0, "Totl%C0" }, 442a99d8730SLen Brown { 0x0, "Any%C0" }, 443a99d8730SLen Brown { 0x0, "GFX%C0" }, 444a99d8730SLen Brown { 0x0, "CPUGFX%" }, 445bdd5ae3aSLen Brown { 0x0, "Core" }, 446bdd5ae3aSLen Brown { 0x0, "CPU" }, 4474c2122d4SLen Brown { 0x0, "APIC" }, 4484c2122d4SLen Brown { 0x0, "X2APIC" }, 4496de68fe1SLen Brown { 0x0, "Die" }, 450812db3f7SLen Brown }; 451812db3f7SLen Brown 452812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 4533f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 4543f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 4553f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 456bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 457bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 458bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 459bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 460bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 461bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 462bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 463bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 464bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 465bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 466bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 467bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 468bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 469bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 470bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 471bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 472bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 473bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 474bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 475bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 476bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 477bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 478bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 479bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 480bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 481bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 482bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 483bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 484bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 485bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 486bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 487bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 488bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 489bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 490bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 491bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 492bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 493bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 494bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 495bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 496bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 497bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 498bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 499bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 500bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 5014c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 5024c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 5036de68fe1SLen Brown #define BIC_Die (1ULL << 50) 504812db3f7SLen Brown 5054c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 5063f44a5c6SLen Brown 5073f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 5084c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 509812db3f7SLen Brown 510812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 5111e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 5123f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 513812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 5140f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 515812db3f7SLen Brown 5163f44a5c6SLen Brown 517dd778a5eSLen Brown #define MAX_DEFERRED 16 518dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 519dd778a5eSLen Brown int deferred_skip_index; 520dd778a5eSLen Brown 521dd778a5eSLen Brown /* 522dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 523dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 524dd778a5eSLen Brown */ 525dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 526dd778a5eSLen Brown 527dd778a5eSLen Brown void help(void) 528dd778a5eSLen Brown { 529dd778a5eSLen Brown fprintf(outf, 530dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 531dd778a5eSLen Brown "\n" 532dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 533dd778a5eSLen Brown "when COMMAND completes.\n" 534dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 535dd778a5eSLen Brown "to print statistics, until interrupted.\n" 536cc481650SNathan Ciobanu " -a, --add add a counter\n" 537dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 538cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 539dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 5409ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 5419ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 5429ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 5439ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 5449ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 5459ce80578SNathan Ciobanu " hide the specified column(s)\n" 546cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 547cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 5489ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 549cc481650SNathan Ciobanu " -l, --list list column headers only\n" 550cc481650SNathan Ciobanu " -n, --num_iterations num\n" 551cc481650SNathan Ciobanu " number of the measurement iterations\n" 552cc481650SNathan Ciobanu " -o, --out file\n" 553cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 554cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 5559ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 5569ce80578SNathan Ciobanu " show only the specified column(s)\n" 5579ce80578SNathan Ciobanu " -S, --Summary\n" 5589ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 5599ce80578SNathan Ciobanu " -T, --TCC temperature\n" 5609ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 5619ce80578SNathan Ciobanu " degrees Celsius\n" 562cc481650SNathan Ciobanu " -h, --help print this help message\n" 563cc481650SNathan Ciobanu " -v, --version print version information\n" 564dd778a5eSLen Brown "\n" 565dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 566dd778a5eSLen Brown } 567dd778a5eSLen Brown 568812db3f7SLen Brown /* 569812db3f7SLen Brown * bic_lookup 570812db3f7SLen Brown * for all the strings in comma separate name_list, 571812db3f7SLen Brown * set the approprate bit in return value. 572812db3f7SLen Brown */ 573dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 574812db3f7SLen Brown { 575812db3f7SLen Brown int i; 576812db3f7SLen Brown unsigned long long retval = 0; 577812db3f7SLen Brown 578812db3f7SLen Brown while (name_list) { 579812db3f7SLen Brown char *comma; 580812db3f7SLen Brown 581812db3f7SLen Brown comma = strchr(name_list, ','); 582812db3f7SLen Brown 583812db3f7SLen Brown if (comma) 584812db3f7SLen Brown *comma = '\0'; 585812db3f7SLen Brown 5863f44a5c6SLen Brown if (!strcmp(name_list, "all")) 5873f44a5c6SLen Brown return ~0; 5883f44a5c6SLen Brown 589812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 590812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 591812db3f7SLen Brown retval |= (1ULL << i); 592812db3f7SLen Brown break; 593812db3f7SLen Brown } 594812db3f7SLen Brown } 595812db3f7SLen Brown if (i == MAX_BIC) { 596dd778a5eSLen Brown if (mode == SHOW_LIST) { 597812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 598812db3f7SLen Brown exit(-1); 599812db3f7SLen Brown } 600dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 601dd778a5eSLen Brown if (debug) 602dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 603dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 604dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 605dd778a5eSLen Brown MAX_DEFERRED, name_list); 606dd778a5eSLen Brown help(); 607dd778a5eSLen Brown exit(1); 608dd778a5eSLen Brown } 609dd778a5eSLen Brown } 610812db3f7SLen Brown 611812db3f7SLen Brown name_list = comma; 612812db3f7SLen Brown if (name_list) 613812db3f7SLen Brown name_list++; 614812db3f7SLen Brown 615812db3f7SLen Brown } 616812db3f7SLen Brown return retval; 617812db3f7SLen Brown } 618fc04cc67SLen Brown 619dd778a5eSLen Brown 620c8ade361SLen Brown void print_header(char *delim) 621103a8feaSLen Brown { 622388e9c81SLen Brown struct msr_counter *mp; 6236168c2e0SLen Brown int printed = 0; 624388e9c81SLen Brown 6253f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 6263f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 6273f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 6283f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 629812db3f7SLen Brown if (DO_BIC(BIC_Package)) 6306168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 6316de68fe1SLen Brown if (DO_BIC(BIC_Die)) 6326de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 63301235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 63401235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 635812db3f7SLen Brown if (DO_BIC(BIC_Core)) 6366168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 637812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 6386168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 6394c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 6404c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 6414c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 6424c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 643812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 6446168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 645812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 6466168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 647812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 6486168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 649812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 6506168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 6511cc21f7bSLen Brown 6520de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 6530de6c0dfSLen Brown if (sums_need_wide_columns) 6546168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 6550de6c0dfSLen Brown else 6566168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 6570de6c0dfSLen Brown } 6580de6c0dfSLen Brown 659812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 6606168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 6611cc21f7bSLen Brown 662388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 663dd778a5eSLen Brown 664388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 665388e9c81SLen Brown if (mp->width == 64) 666dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 667388e9c81SLen Brown else 668dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 669388e9c81SLen Brown } else { 6700de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 671dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 6720de6c0dfSLen Brown else 673dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 674388e9c81SLen Brown } 675388e9c81SLen Brown } 676388e9c81SLen Brown 67741618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 6786168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 679562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 6806168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 681812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 6826168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 683812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 6846168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 685678a3bd1SLen Brown 6860539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 6876168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 688678a3bd1SLen Brown 689812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 6906168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 691388e9c81SLen Brown 6929392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 6939392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 6949392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 6959392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 6969392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 6979392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 6989392bd98SCalvin Walton } 6999392bd98SCalvin Walton 700388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 701388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 702388e9c81SLen Brown if (mp->width == 64) 703c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 704388e9c81SLen Brown else 705c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 706388e9c81SLen Brown } else { 7070de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 7080de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 7090de6c0dfSLen Brown else 710c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 711388e9c81SLen Brown } 712388e9c81SLen Brown } 713388e9c81SLen Brown 714812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 7156168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 716889facbeSLen Brown 717812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 7186168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 719fdf676e5SLen Brown 720812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 7216168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 72227d47356SLen Brown 723a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 7246168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 725a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 7266168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 727a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 7286168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 729a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 7306168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 7310b2bb692SLen Brown 7320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 7336168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 7340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 7356168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 7360f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 7376168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 7380f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 7396168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 7400f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 7416168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 7420f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 7436168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 7440f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 7456168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 746be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 747be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 748be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 749be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 750103a8feaSLen Brown 7515c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 752812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 7536168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 7549392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 7556168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 756812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 7576168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 758812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 7596168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 760812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 7616168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 762812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 7636168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 764d7899447SLen Brown } else if (do_rapl && rapl_joules) { 765812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 7666168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 7679392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 7686168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 769812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 7706168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 771812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 7726168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 773812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 7746168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 775812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 7766168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 7775c56be9aSDirk Brandewie } 778388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 779388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 780388e9c81SLen Brown if (mp->width == 64) 781c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 782388e9c81SLen Brown else 783c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 784388e9c81SLen Brown } else { 7850de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 7860de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 7870de6c0dfSLen Brown else 788c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 789388e9c81SLen Brown } 790388e9c81SLen Brown } 791388e9c81SLen Brown 792c98d5d94SLen Brown outp += sprintf(outp, "\n"); 793103a8feaSLen Brown } 794103a8feaSLen Brown 795c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 796c98d5d94SLen Brown struct pkg_data *p) 797103a8feaSLen Brown { 798388e9c81SLen Brown int i; 799388e9c81SLen Brown struct msr_counter *mp; 800388e9c81SLen Brown 8013b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 802c98d5d94SLen Brown 803c98d5d94SLen Brown if (t) { 8043b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 8053b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 8063b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 8073b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 8083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 8093b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 8106886fee4SLen Brown 811812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 8120de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 813812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 814218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 815388e9c81SLen Brown 816388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 817388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 818388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 819388e9c81SLen Brown } 820103a8feaSLen Brown } 821103a8feaSLen Brown 822c98d5d94SLen Brown if (c) { 8233b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 8243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 8253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 8263b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 8273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 8289392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 829388e9c81SLen Brown 830388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 831388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 832388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 833388e9c81SLen Brown } 8340539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 835c98d5d94SLen Brown } 836103a8feaSLen Brown 837c98d5d94SLen Brown if (p) { 8383b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 8390b2bb692SLen Brown 8400b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 8410b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 8420b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 8430b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 8440b2bb692SLen Brown 8453b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 8460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 8473b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 8480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 8493b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 8500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 8513b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 8523b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 8533b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 8543b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 855be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 856be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 8573b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 8583b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 8593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 8603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 8613b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 8623b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 8633b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 8643b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 8653b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 866388e9c81SLen Brown 867388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 868388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 869388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 870388e9c81SLen Brown } 871c98d5d94SLen Brown } 8723b4d5c7fSAndy Shevchenko 8733b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 8743b4d5c7fSAndy Shevchenko 875c98d5d94SLen Brown return 0; 876103a8feaSLen Brown } 877103a8feaSLen Brown 878e23da037SLen Brown /* 879e23da037SLen Brown * column formatting convention & formats 880e23da037SLen Brown */ 881c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 882c98d5d94SLen Brown struct pkg_data *p) 883103a8feaSLen Brown { 884008d396eSLen Brown double interval_float, tsc; 885fc04cc67SLen Brown char *fmt8; 886388e9c81SLen Brown int i; 887388e9c81SLen Brown struct msr_counter *mp; 8886168c2e0SLen Brown char *delim = "\t"; 8896168c2e0SLen Brown int printed = 0; 890103a8feaSLen Brown 891c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 892c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 893c98d5d94SLen Brown return 0; 894c98d5d94SLen Brown 895c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 896c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 897c98d5d94SLen Brown return 0; 898c98d5d94SLen Brown 8991ef7d21aSLen Brown /*if not summary line and --cpu is used */ 9001ef7d21aSLen Brown if ((t != &average.threads) && 9011ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 9021ef7d21aSLen Brown return 0; 9031ef7d21aSLen Brown 9043f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 905f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 906f4fdf2b4SLen Brown struct timeval tv; 907f4fdf2b4SLen Brown 908f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 909f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 910f4fdf2b4SLen Brown } 911f4fdf2b4SLen Brown 9123f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 9133f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 9143f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 9153f44a5c6SLen Brown 916d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0; 917103a8feaSLen Brown 918008d396eSLen Brown tsc = t->tsc * tsc_tweak; 919008d396eSLen Brown 920c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 921c98d5d94SLen Brown if (t == &average.threads) { 922812db3f7SLen Brown if (DO_BIC(BIC_Package)) 9236168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9246de68fe1SLen Brown if (DO_BIC(BIC_Die)) 9256de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 92601235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 92701235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 928812db3f7SLen Brown if (DO_BIC(BIC_Core)) 9296168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 930812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 9316168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9324c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 9334c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9344c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 9354c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 936103a8feaSLen Brown } else { 937812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 938c98d5d94SLen Brown if (p) 9396168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 940c98d5d94SLen Brown else 9416168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 942c98d5d94SLen Brown } 9436de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 9446de68fe1SLen Brown if (c) 9456de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 9466de68fe1SLen Brown else 9476de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9486de68fe1SLen Brown } 94901235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 95001235041SPrarit Bhargava if (t) 95101235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 95201235041SPrarit Bhargava (printed++ ? delim : ""), 95301235041SPrarit Bhargava cpus[t->cpu_id].physical_node_id); 95401235041SPrarit Bhargava else 95501235041SPrarit Bhargava outp += sprintf(outp, "%s-", 95601235041SPrarit Bhargava (printed++ ? delim : "")); 95701235041SPrarit Bhargava } 958812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 959c98d5d94SLen Brown if (c) 9606168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 961c98d5d94SLen Brown else 9626168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 963c98d5d94SLen Brown } 964812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 9656168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 9664c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 9674c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 9684c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 9694c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 970103a8feaSLen Brown } 971fc04cc67SLen Brown 972812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 9736168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 974fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 975fc04cc67SLen Brown 976812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 9776168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 978103a8feaSLen Brown 979812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 98021ed5574SLen Brown if (has_base_hz) 9816168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 98221ed5574SLen Brown else 9836168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 984008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 98521ed5574SLen Brown } 986103a8feaSLen Brown 987812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 9886168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 989103a8feaSLen Brown 990562a2d37SLen Brown /* IRQ */ 9910de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 9920de6c0dfSLen Brown if (sums_need_wide_columns) 9936168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 9940de6c0dfSLen Brown else 9956168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 9960de6c0dfSLen Brown } 997562a2d37SLen Brown 9981cc21f7bSLen Brown /* SMI */ 999812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 10006168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 10011cc21f7bSLen Brown 1002678a3bd1SLen Brown /* Added counters */ 1003678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1004678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1005678a3bd1SLen Brown if (mp->width == 32) 10065f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 1007678a3bd1SLen Brown else 10086168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1009678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 10100de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10116168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 10120de6c0dfSLen Brown else 10136168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1014678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 101541618e63SLen Brown if (mp->type == COUNTER_USEC) 10166168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 101741618e63SLen Brown else 10186168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 1019678a3bd1SLen Brown } 1020678a3bd1SLen Brown } 1021678a3bd1SLen Brown 102241618e63SLen Brown /* C1 */ 102341618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 10246168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 102541618e63SLen Brown 102641618e63SLen Brown 1027c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1028c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1029c98d5d94SLen Brown goto done; 1030c98d5d94SLen Brown 1031562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 10326168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 1033812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 10346168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 1035812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 10366168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 1037c98d5d94SLen Brown 10380539ba11SLen Brown /* Mod%c6 */ 10390539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 10406168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 10410539ba11SLen Brown 1042812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 10436168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1044889facbeSLen Brown 1045388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1046388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1047388e9c81SLen Brown if (mp->width == 32) 10485f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 1049388e9c81SLen Brown else 10506168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1051388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 10520de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10536168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 10540de6c0dfSLen Brown else 10556168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1056388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 10576168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 1058388e9c81SLen Brown } 1059388e9c81SLen Brown } 1060388e9c81SLen Brown 10619392bd98SCalvin Walton /* 10629392bd98SCalvin Walton * If measurement interval exceeds minimum RAPL Joule Counter range, 10639392bd98SCalvin Walton * indicate that results are suspect by printing "**" in fraction place. 10649392bd98SCalvin Walton */ 10659392bd98SCalvin Walton if (interval_float < rapl_joule_counter_range) 10669392bd98SCalvin Walton fmt8 = "%s%.2f"; 10679392bd98SCalvin Walton else 10689392bd98SCalvin Walton fmt8 = "%6.0f**"; 10699392bd98SCalvin Walton 10709392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 10719392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 10729392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 10739392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 10749392bd98SCalvin Walton 1075c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1076c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1077c98d5d94SLen Brown goto done; 1078c98d5d94SLen Brown 10790b2bb692SLen Brown /* PkgTmp */ 1080812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 10816168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1082889facbeSLen Brown 1083fdf676e5SLen Brown /* GFXrc6 */ 1084812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1085ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 10866168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 10879185e988SLen Brown } else { 10886168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 10899185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 10909185e988SLen Brown } 10919185e988SLen Brown } 1092fdf676e5SLen Brown 109327d47356SLen Brown /* GFXMHz */ 1094812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 10956168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 109627d47356SLen Brown 10970b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1098a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 10996168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 1100a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 11016168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 1102a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 11036168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 1104a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 11056168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 11060b2bb692SLen Brown 11070f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 11086168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 11090f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 11106168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 11110f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 11126168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 11130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 11146168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 11150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 11166168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 11170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 11186168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 11190f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 11206168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 1121889facbeSLen Brown 1122be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1123be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1124be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1125be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1126be0e54c4SLen Brown 1127812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 11286168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 11299392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 11306168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1131812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 11326168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1133812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 11346168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 1135812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 11366168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 11379392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 11386168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1139812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 11406168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1141812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 11426168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1143812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 11446168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1145812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 11466168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1147812db3f7SLen Brown 1148388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1149388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1150388e9c81SLen Brown if (mp->width == 32) 11515f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]); 1152388e9c81SLen Brown else 11536168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1154388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 11550de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11566168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 11570de6c0dfSLen Brown else 11586168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1159388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 11606168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc); 1161388e9c81SLen Brown } 1162388e9c81SLen Brown } 1163388e9c81SLen Brown 1164c98d5d94SLen Brown done: 116594d6ab4bSLen Brown if (*(outp - 1) != '\n') 1166c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1167c98d5d94SLen Brown 1168c98d5d94SLen Brown return 0; 1169103a8feaSLen Brown } 1170103a8feaSLen Brown 1171b7d8c148SLen Brown void flush_output_stdout(void) 1172103a8feaSLen Brown { 1173b7d8c148SLen Brown FILE *filep; 1174b7d8c148SLen Brown 1175b7d8c148SLen Brown if (outf == stderr) 1176b7d8c148SLen Brown filep = stdout; 1177b7d8c148SLen Brown else 1178b7d8c148SLen Brown filep = outf; 1179b7d8c148SLen Brown 1180b7d8c148SLen Brown fputs(output_buffer, filep); 1181b7d8c148SLen Brown fflush(filep); 1182b7d8c148SLen Brown 1183c98d5d94SLen Brown outp = output_buffer; 1184c98d5d94SLen Brown } 1185b7d8c148SLen Brown void flush_output_stderr(void) 1186c98d5d94SLen Brown { 1187b7d8c148SLen Brown fputs(output_buffer, outf); 1188b7d8c148SLen Brown fflush(outf); 1189c98d5d94SLen Brown outp = output_buffer; 1190c98d5d94SLen Brown } 1191c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1192c98d5d94SLen Brown { 1193e23da037SLen Brown static int printed; 1194103a8feaSLen Brown 1195e23da037SLen Brown if (!printed || !summary_only) 1196c8ade361SLen Brown print_header("\t"); 1197103a8feaSLen Brown 11989d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1199103a8feaSLen Brown 1200e23da037SLen Brown printed = 1; 1201e23da037SLen Brown 1202e23da037SLen Brown if (summary_only) 1203e23da037SLen Brown return; 1204e23da037SLen Brown 1205c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1206103a8feaSLen Brown } 1207103a8feaSLen Brown 1208889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 1209889facbeSLen Brown if (new > old) { \ 1210889facbeSLen Brown old = new - old; \ 1211889facbeSLen Brown } else { \ 1212889facbeSLen Brown old = 0x100000000 + new - old; \ 1213889facbeSLen Brown } 1214889facbeSLen Brown 1215ba3dec99SLen Brown int 1216c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1217103a8feaSLen Brown { 1218388e9c81SLen Brown int i; 1219388e9c81SLen Brown struct msr_counter *mp; 12200b2bb692SLen Brown 1221a99d8730SLen Brown 1222a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12230b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1224a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12250b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1226a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12270b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1228a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12290b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1230a99d8730SLen Brown 1231c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 12320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1233c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 12340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1235c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 12360f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1237c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1238ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1239ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1240ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1241be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1242be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1243889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1244889facbeSLen Brown 12459185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 12469185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 12479185e988SLen Brown old->gfx_rc6_ms = -1; 12489185e988SLen Brown else 1249fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 12509185e988SLen Brown 125127d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 125227d47356SLen Brown 1253889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 1254889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 1255889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 1256889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 1257889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 1258889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 1259ba3dec99SLen Brown 1260388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1261388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1262388e9c81SLen Brown old->counter[i] = new->counter[i]; 1263388e9c81SLen Brown else 1264388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1265388e9c81SLen Brown } 1266388e9c81SLen Brown 1267ba3dec99SLen Brown return 0; 1268103a8feaSLen Brown } 1269103a8feaSLen Brown 1270c98d5d94SLen Brown void 1271c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1272c98d5d94SLen Brown { 1273388e9c81SLen Brown int i; 1274388e9c81SLen Brown struct msr_counter *mp; 1275388e9c81SLen Brown 1276c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1277c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1278c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1279889facbeSLen Brown old->core_temp_c = new->core_temp_c; 12800539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1281388e9c81SLen Brown 12829392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 12839392bd98SCalvin Walton 1284388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1285388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1286388e9c81SLen Brown old->counter[i] = new->counter[i]; 1287388e9c81SLen Brown else 1288388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1289388e9c81SLen Brown } 1290103a8feaSLen Brown } 1291c98d5d94SLen Brown 12921e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 12931e9042b9SSrinivas Pandruvada { 12941e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 12951e9042b9SSrinivas Pandruvada return 0; 12961e9042b9SSrinivas Pandruvada 12971e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 12981e9042b9SSrinivas Pandruvada } 12991e9042b9SSrinivas Pandruvada 1300c3ae331dSLen Brown /* 1301c3ae331dSLen Brown * old = new - old 1302c3ae331dSLen Brown */ 1303ba3dec99SLen Brown int 1304c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1305c98d5d94SLen Brown struct core_data *core_delta) 1306c98d5d94SLen Brown { 1307388e9c81SLen Brown int i; 1308388e9c81SLen Brown struct msr_counter *mp; 1309388e9c81SLen Brown 13104c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 13114c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 13124c2122d4SLen Brown new->apic_id = old->apic_id; 13134c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 13144c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 13154c2122d4SLen Brown 13163f44a5c6SLen Brown /* 13173f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 13183f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 13193f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 13203f44a5c6SLen Brown */ 13213f44a5c6SLen Brown 1322d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 13233f44a5c6SLen Brown old->tv_begin = new->tv_begin; 13243f44a5c6SLen Brown old->tv_end = new->tv_end; 13253f44a5c6SLen Brown 1326c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1327c98d5d94SLen Brown 1328103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1329b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1330b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1331b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1332b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1333103a8feaSLen Brown 1334c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1335c98d5d94SLen Brown 13361e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 13371e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 1338c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1339c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1340c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1341c98d5d94SLen Brown } else { 1342ba3dec99SLen Brown return -1; 1343103a8feaSLen Brown } 1344a729617cSLen Brown } 1345103a8feaSLen Brown 1346c98d5d94SLen Brown 1347144b44b1SLen Brown if (use_c1_residency_msr) { 1348144b44b1SLen Brown /* 1349144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1350144b44b1SLen Brown * which should be more accurate than the derivation below. 1351144b44b1SLen Brown */ 1352144b44b1SLen Brown } else { 1353103a8feaSLen Brown /* 1354c3ae331dSLen Brown * As counter collection is not atomic, 1355c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1356103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1357103a8feaSLen Brown */ 135895149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1359c98d5d94SLen Brown old->c1 = 0; 1360c98d5d94SLen Brown else { 1361c98d5d94SLen Brown /* normal case, derive c1 */ 1362008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1363c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1364c98d5d94SLen Brown } 1365144b44b1SLen Brown } 1366c3ae331dSLen Brown 1367c98d5d94SLen Brown if (old->mperf == 0) { 1368b7d8c148SLen Brown if (debug > 1) 1369b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1370c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1371c98d5d94SLen Brown } 1372103a8feaSLen Brown 1373812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1374562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1375562a2d37SLen Brown 1376812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 13771ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1378ba3dec99SLen Brown 1379388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1380388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1381388e9c81SLen Brown old->counter[i] = new->counter[i]; 1382388e9c81SLen Brown else 1383388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1384388e9c81SLen Brown } 1385ba3dec99SLen Brown return 0; 1386103a8feaSLen Brown } 1387c98d5d94SLen Brown 1388c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1389c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1390c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1391c98d5d94SLen Brown { 1392ba3dec99SLen Brown int retval = 0; 1393ba3dec99SLen Brown 1394c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1395c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1396c98d5d94SLen Brown delta_core(c, c2); 1397c98d5d94SLen Brown 1398c98d5d94SLen Brown /* always calculate thread delta */ 1399ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1400ba3dec99SLen Brown if (retval) 1401ba3dec99SLen Brown return retval; 1402c98d5d94SLen Brown 1403c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1404c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1405ba3dec99SLen Brown retval = delta_package(p, p2); 1406c98d5d94SLen Brown 1407ba3dec99SLen Brown return retval; 1408103a8feaSLen Brown } 1409103a8feaSLen Brown 1410c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1411103a8feaSLen Brown { 1412388e9c81SLen Brown int i; 1413388e9c81SLen Brown struct msr_counter *mp; 1414388e9c81SLen Brown 14153f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 14163f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 14173f44a5c6SLen Brown t->tv_end.tv_sec = 0; 14183f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1419d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1420d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 14213f44a5c6SLen Brown 1422c98d5d94SLen Brown t->tsc = 0; 1423c98d5d94SLen Brown t->aperf = 0; 1424c98d5d94SLen Brown t->mperf = 0; 1425c98d5d94SLen Brown t->c1 = 0; 1426103a8feaSLen Brown 1427562a2d37SLen Brown t->irq_count = 0; 1428562a2d37SLen Brown t->smi_count = 0; 1429562a2d37SLen Brown 1430c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1431c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1432c98d5d94SLen Brown 1433c98d5d94SLen Brown c->c3 = 0; 1434c98d5d94SLen Brown c->c6 = 0; 1435c98d5d94SLen Brown c->c7 = 0; 14360539ba11SLen Brown c->mc6_us = 0; 1437889facbeSLen Brown c->core_temp_c = 0; 14389392bd98SCalvin Walton c->core_energy = 0; 1439c98d5d94SLen Brown 14400b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 14410b2bb692SLen Brown p->pkg_any_core_c0 = 0; 14420b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 14430b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 14440b2bb692SLen Brown 1445c98d5d94SLen Brown p->pc2 = 0; 14460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1447c98d5d94SLen Brown p->pc3 = 0; 14480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1449c98d5d94SLen Brown p->pc6 = 0; 14500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1451c98d5d94SLen Brown p->pc7 = 0; 1452ca58710fSKristen Carlson Accardi p->pc8 = 0; 1453ca58710fSKristen Carlson Accardi p->pc9 = 0; 1454ca58710fSKristen Carlson Accardi p->pc10 = 0; 1455be0e54c4SLen Brown p->cpu_lpi = 0; 1456be0e54c4SLen Brown p->sys_lpi = 0; 1457889facbeSLen Brown 1458889facbeSLen Brown p->energy_pkg = 0; 1459889facbeSLen Brown p->energy_dram = 0; 1460889facbeSLen Brown p->energy_cores = 0; 1461889facbeSLen Brown p->energy_gfx = 0; 1462889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1463889facbeSLen Brown p->rapl_dram_perf_status = 0; 1464889facbeSLen Brown p->pkg_temp_c = 0; 146527d47356SLen Brown 1466fdf676e5SLen Brown p->gfx_rc6_ms = 0; 146727d47356SLen Brown p->gfx_mhz = 0; 1468388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1469388e9c81SLen Brown t->counter[i] = 0; 1470388e9c81SLen Brown 1471388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1472388e9c81SLen Brown c->counter[i] = 0; 1473388e9c81SLen Brown 1474388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1475388e9c81SLen Brown p->counter[i] = 0; 1476103a8feaSLen Brown } 1477c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1478c98d5d94SLen Brown struct pkg_data *p) 1479103a8feaSLen Brown { 1480388e9c81SLen Brown int i; 1481388e9c81SLen Brown struct msr_counter *mp; 1482388e9c81SLen Brown 14834c2122d4SLen Brown /* copy un-changing apic_id's */ 14844c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14854c2122d4SLen Brown average.threads.apic_id = t->apic_id; 14864c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14874c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 14884c2122d4SLen Brown 14893f44a5c6SLen Brown /* remember first tv_begin */ 14903f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 14913f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 14923f44a5c6SLen Brown 14933f44a5c6SLen Brown /* remember last tv_end */ 14943f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 14953f44a5c6SLen Brown 1496c98d5d94SLen Brown average.threads.tsc += t->tsc; 1497c98d5d94SLen Brown average.threads.aperf += t->aperf; 1498c98d5d94SLen Brown average.threads.mperf += t->mperf; 1499c98d5d94SLen Brown average.threads.c1 += t->c1; 150015aaa346SLen Brown 1501562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1502562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1503562a2d37SLen Brown 1504388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1505388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1506388e9c81SLen Brown continue; 1507388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1508388e9c81SLen Brown } 1509388e9c81SLen Brown 1510c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1511c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1512c98d5d94SLen Brown return 0; 1513c98d5d94SLen Brown 1514c98d5d94SLen Brown average.cores.c3 += c->c3; 1515c98d5d94SLen Brown average.cores.c6 += c->c6; 1516c98d5d94SLen Brown average.cores.c7 += c->c7; 15170539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1518c98d5d94SLen Brown 1519889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1520889facbeSLen Brown 15219392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 15229392bd98SCalvin Walton 1523388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1524388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1525388e9c81SLen Brown continue; 1526388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1527388e9c81SLen Brown } 1528388e9c81SLen Brown 1529c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1530c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1531c98d5d94SLen Brown return 0; 1532c98d5d94SLen Brown 1533a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 15340b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1535a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 15360b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1537a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 15380b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1539a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 15400b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 15410b2bb692SLen Brown 1542c98d5d94SLen Brown average.packages.pc2 += p->pc2; 15430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1544c98d5d94SLen Brown average.packages.pc3 += p->pc3; 15450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1546c98d5d94SLen Brown average.packages.pc6 += p->pc6; 15470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1548c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1549ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1550ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1551ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1552c98d5d94SLen Brown 1553be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1554be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1555be0e54c4SLen Brown 1556889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1557889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1558889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1559889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1560889facbeSLen Brown 1561fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 156227d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 156327d47356SLen Brown 1564889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1565889facbeSLen Brown 1566889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1567889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1568388e9c81SLen Brown 1569388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1570388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1571388e9c81SLen Brown continue; 1572388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1573388e9c81SLen Brown } 1574c98d5d94SLen Brown return 0; 1575c98d5d94SLen Brown } 1576c98d5d94SLen Brown /* 1577c98d5d94SLen Brown * sum the counters for all cpus in the system 1578c98d5d94SLen Brown * compute the weighted average 1579c98d5d94SLen Brown */ 1580c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1581c98d5d94SLen Brown struct pkg_data *p) 1582c98d5d94SLen Brown { 1583388e9c81SLen Brown int i; 1584388e9c81SLen Brown struct msr_counter *mp; 1585388e9c81SLen Brown 1586c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1587c98d5d94SLen Brown 1588c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1589c98d5d94SLen Brown 1590d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1591d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1592d4794f25SYazen Ghannam 1593c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1594c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1595c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1596c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1597c98d5d94SLen Brown 15980de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 15990de6c0dfSLen Brown sums_need_wide_columns = 1; 16000de6c0dfSLen Brown 1601c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1602c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1603c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 16040539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1605c98d5d94SLen Brown 1606a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 16070b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1608a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 16090b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1610a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 16110b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1612a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 16130b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 16140b2bb692SLen Brown 1615c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 16160f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1617c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 16180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1619c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 16200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1621c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1622ca58710fSKristen Carlson Accardi 1623ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1624ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1625ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1626388e9c81SLen Brown 1627388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1628388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1629388e9c81SLen Brown continue; 16300de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 16310de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 16320de6c0dfSLen Brown sums_need_wide_columns = 1; 163341618e63SLen Brown continue; 16340de6c0dfSLen Brown } 1635388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1636388e9c81SLen Brown } 1637388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1638388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1639388e9c81SLen Brown continue; 16400de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 16410de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 16420de6c0dfSLen Brown sums_need_wide_columns = 1; 16430de6c0dfSLen Brown } 1644388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1645388e9c81SLen Brown } 1646388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1647388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1648388e9c81SLen Brown continue; 16490de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 16500de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 16510de6c0dfSLen Brown sums_need_wide_columns = 1; 16520de6c0dfSLen Brown } 1653388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1654388e9c81SLen Brown } 1655c98d5d94SLen Brown } 1656c98d5d94SLen Brown 1657c98d5d94SLen Brown static unsigned long long rdtsc(void) 1658c98d5d94SLen Brown { 1659c98d5d94SLen Brown unsigned int low, high; 1660c98d5d94SLen Brown 1661c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1662c98d5d94SLen Brown 1663c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1664c98d5d94SLen Brown } 1665c98d5d94SLen Brown 1666c98d5d94SLen Brown /* 1667495c7654SLen Brown * Open a file, and exit on failure 1668495c7654SLen Brown */ 1669495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1670495c7654SLen Brown { 1671495c7654SLen Brown FILE *filep = fopen(path, mode); 1672495c7654SLen Brown 1673495c7654SLen Brown if (!filep) 1674495c7654SLen Brown err(1, "%s: open failed", path); 1675495c7654SLen Brown return filep; 1676495c7654SLen Brown } 1677495c7654SLen Brown /* 1678495c7654SLen Brown * snapshot_sysfs_counter() 1679495c7654SLen Brown * 1680495c7654SLen Brown * return snapshot of given counter 1681495c7654SLen Brown */ 1682495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1683495c7654SLen Brown { 1684495c7654SLen Brown FILE *fp; 1685495c7654SLen Brown int retval; 1686495c7654SLen Brown unsigned long long counter; 1687495c7654SLen Brown 1688495c7654SLen Brown fp = fopen_or_die(path, "r"); 1689495c7654SLen Brown 1690495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1691495c7654SLen Brown if (retval != 1) 1692495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1693495c7654SLen Brown 1694495c7654SLen Brown fclose(fp); 1695495c7654SLen Brown 1696495c7654SLen Brown return counter; 1697495c7654SLen Brown } 1698495c7654SLen Brown 1699495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1700495c7654SLen Brown { 1701495c7654SLen Brown if (mp->msr_num != 0) { 1702495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1703495c7654SLen Brown return -1; 1704495c7654SLen Brown } else { 170546c27978SLen Brown char path[128 + PATH_BYTES]; 170641618e63SLen Brown 170741618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 170841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 170941618e63SLen Brown cpu, mp->path); 171041618e63SLen Brown 171141618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 171241618e63SLen Brown } else { 1713495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1714495c7654SLen Brown } 171541618e63SLen Brown } 1716495c7654SLen Brown 1717495c7654SLen Brown return 0; 1718495c7654SLen Brown } 1719495c7654SLen Brown 17204c2122d4SLen Brown void get_apic_id(struct thread_data *t) 17214c2122d4SLen Brown { 172234041551SLen Brown unsigned int eax, ebx, ecx, edx; 172334041551SLen Brown 172434041551SLen Brown if (DO_BIC(BIC_APIC)) { 172534041551SLen Brown eax = ebx = ecx = edx = 0; 172634041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 172734041551SLen Brown 172834041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 172934041551SLen Brown } 173034041551SLen Brown 173134041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 173234041551SLen Brown return; 173334041551SLen Brown 1734*c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) { 173534041551SLen Brown unsigned int topology_extensions; 173634041551SLen Brown 173734041551SLen Brown if (max_extended_level < 0x8000001e) 173834041551SLen Brown return; 17394c2122d4SLen Brown 17404c2122d4SLen Brown eax = ebx = ecx = edx = 0; 174134041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 174234041551SLen Brown topology_extensions = ecx & (1 << 22); 174334041551SLen Brown 174434041551SLen Brown if (topology_extensions == 0) 174534041551SLen Brown return; 174634041551SLen Brown 174734041551SLen Brown eax = ebx = ecx = edx = 0; 174834041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 174934041551SLen Brown 175034041551SLen Brown t->x2apic_id = eax; 175134041551SLen Brown return; 175234041551SLen Brown } 17534c2122d4SLen Brown 17544c2122d4SLen Brown if (!genuine_intel) 17554c2122d4SLen Brown return; 17564c2122d4SLen Brown 17574c2122d4SLen Brown if (max_level < 0xb) 17584c2122d4SLen Brown return; 17594c2122d4SLen Brown 17604c2122d4SLen Brown ecx = 0; 17614c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 17624c2122d4SLen Brown t->x2apic_id = edx; 17634c2122d4SLen Brown 176434041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 176534041551SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", 176634041551SLen Brown t->cpu_id, t->apic_id, t->x2apic_id); 17674c2122d4SLen Brown } 17684c2122d4SLen Brown 1769495c7654SLen Brown /* 1770c98d5d94SLen Brown * get_counters(...) 1771c98d5d94SLen Brown * migrate to cpu 1772c98d5d94SLen Brown * acquire and record local counters for that cpu 1773c98d5d94SLen Brown */ 1774c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1775c98d5d94SLen Brown { 1776c98d5d94SLen Brown int cpu = t->cpu_id; 1777889facbeSLen Brown unsigned long long msr; 17780102b067SLen Brown int aperf_mperf_retry_count = 0; 1779388e9c81SLen Brown struct msr_counter *mp; 1780388e9c81SLen Brown int i; 1781c98d5d94SLen Brown 1782e52966c0SLen Brown if (cpu_migrate(cpu)) { 1783b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 178415aaa346SLen Brown return -1; 1785e52966c0SLen Brown } 178615aaa346SLen Brown 1787d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1788d4794f25SYazen Ghannam 17894c2122d4SLen Brown if (first_counter_read) 17904c2122d4SLen Brown get_apic_id(t); 17910102b067SLen Brown retry: 1792c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 179315aaa346SLen Brown 17941e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 17951e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 17960102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 17970102b067SLen Brown 17980102b067SLen Brown /* 17990102b067SLen Brown * The TSC, APERF and MPERF must be read together for 18000102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 18010102b067SLen Brown * 18020102b067SLen Brown * Unfortunately, APERF and MPERF are read by 18030102b067SLen Brown * individual system call, so delays may occur 18040102b067SLen Brown * between them. If the time to read them 18050102b067SLen Brown * varies by a large amount, we re-read them. 18060102b067SLen Brown */ 18070102b067SLen Brown 18080102b067SLen Brown /* 18090102b067SLen Brown * This initial dummy APERF read has been seen to 18100102b067SLen Brown * reduce jitter in the subsequent reads. 18110102b067SLen Brown */ 18120102b067SLen Brown 18139c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1814c98d5d94SLen Brown return -3; 18150102b067SLen Brown 18160102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 18170102b067SLen Brown 18180102b067SLen Brown tsc_before = t->tsc; 18190102b067SLen Brown 18200102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 18210102b067SLen Brown return -3; 18220102b067SLen Brown 18230102b067SLen Brown tsc_between = rdtsc(); 18240102b067SLen Brown 18259c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1826c98d5d94SLen Brown return -4; 18270102b067SLen Brown 18280102b067SLen Brown tsc_after = rdtsc(); 18290102b067SLen Brown 18300102b067SLen Brown aperf_time = tsc_between - tsc_before; 18310102b067SLen Brown mperf_time = tsc_after - tsc_between; 18320102b067SLen Brown 18330102b067SLen Brown /* 18340102b067SLen Brown * If the system call latency to read APERF and MPERF 18350102b067SLen Brown * differ by more than 2x, then try again. 18360102b067SLen Brown */ 18370102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 18380102b067SLen Brown aperf_mperf_retry_count++; 18390102b067SLen Brown if (aperf_mperf_retry_count < 5) 18400102b067SLen Brown goto retry; 18410102b067SLen Brown else 18420102b067SLen Brown warnx("cpu%d jitter %lld %lld", 18430102b067SLen Brown cpu, aperf_time, mperf_time); 18440102b067SLen Brown } 18450102b067SLen Brown aperf_mperf_retry_count = 0; 18460102b067SLen Brown 1847b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1848b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 184988c3281fSLen Brown } 185088c3281fSLen Brown 1851812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1852562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1853812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 18541ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 18551ed51011SLen Brown return -5; 18561ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 18571ed51011SLen Brown } 18580539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1859144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1860144b44b1SLen Brown return -6; 1861144b44b1SLen Brown } 1862144b44b1SLen Brown 1863388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1864495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1865388e9c81SLen Brown return -10; 1866388e9c81SLen Brown } 1867388e9c81SLen Brown 1868c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1869c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1870f4fdf2b4SLen Brown goto done; 1871c98d5d94SLen Brown 18721e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 1873c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1874c98d5d94SLen Brown return -6; 1875144b44b1SLen Brown } 1876144b44b1SLen Brown 18771e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 1878c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1879c98d5d94SLen Brown return -7; 18801e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 1881fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1882fb5d4327SDasaratharaman Chandramouli return -7; 1883103a8feaSLen Brown } 188415aaa346SLen Brown 18851e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) 1886c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1887c98d5d94SLen Brown return -8; 1888c98d5d94SLen Brown 18890539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 18900539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 18910539ba11SLen Brown return -8; 18920539ba11SLen Brown 1893812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 1894889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1895889facbeSLen Brown return -9; 1896889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1897889facbeSLen Brown } 1898889facbeSLen Brown 18999392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 19009392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 19019392bd98SCalvin Walton return -14; 19029392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 19039392bd98SCalvin Walton } 19049392bd98SCalvin Walton 1905388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1906495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 1907388e9c81SLen Brown return -10; 1908388e9c81SLen Brown } 1909889facbeSLen Brown 1910c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1911c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1912f4fdf2b4SLen Brown goto done; 191315aaa346SLen Brown 1914a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 19150b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 19160b2bb692SLen Brown return -10; 1917a99d8730SLen Brown } 1918a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 19190b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 19200b2bb692SLen Brown return -11; 1921a99d8730SLen Brown } 1922a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 19230b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 19240b2bb692SLen Brown return -12; 1925a99d8730SLen Brown } 1926a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 19270b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 19280b2bb692SLen Brown return -13; 19290b2bb692SLen Brown } 19300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1931c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1932c98d5d94SLen Brown return -9; 19330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 19340539ba11SLen Brown if (do_slm_cstates) { 19350539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 19360539ba11SLen Brown return -10; 19370539ba11SLen Brown } else { 1938c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1939c98d5d94SLen Brown return -10; 19400539ba11SLen Brown } 19410539ba11SLen Brown } 19420539ba11SLen Brown 19430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 1944c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1945c98d5d94SLen Brown return -11; 19460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1947c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1948c98d5d94SLen Brown return -12; 19490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 1950ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1951ca58710fSKristen Carlson Accardi return -13; 19520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 1953ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1954ca58710fSKristen Carlson Accardi return -13; 19550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 1956ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1957ca58710fSKristen Carlson Accardi return -13; 19580f47c08dSLen Brown 1959be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1960be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 1961be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1962be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 1963be0e54c4SLen Brown 1964889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1965889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1966889facbeSLen Brown return -13; 1967889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1968889facbeSLen Brown } 19699148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 1970889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1971889facbeSLen Brown return -14; 1972889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1973889facbeSLen Brown } 1974889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1975889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1976889facbeSLen Brown return -15; 1977889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1978889facbeSLen Brown } 1979889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1980889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1981889facbeSLen Brown return -16; 1982889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1983889facbeSLen Brown } 1984889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1985889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1986889facbeSLen Brown return -16; 1987889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1988889facbeSLen Brown } 1989889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1990889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1991889facbeSLen Brown return -16; 1992889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1993889facbeSLen Brown } 19943316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 19953316f99aSCalvin Walton if (get_msr(cpu, MSR_PKG_ENERGY_STAT, &msr)) 19963316f99aSCalvin Walton return -13; 19973316f99aSCalvin Walton p->energy_pkg = msr & 0xFFFFFFFF; 19983316f99aSCalvin Walton } 1999812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 2000889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2001889facbeSLen Brown return -17; 2002889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2003889facbeSLen Brown } 2004fdf676e5SLen Brown 2005812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2006fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2007fdf676e5SLen Brown 2008812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 200927d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 201027d47356SLen Brown 2011388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2012495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2013388e9c81SLen Brown return -10; 2014388e9c81SLen Brown } 2015f4fdf2b4SLen Brown done: 2016f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2017388e9c81SLen Brown 201815aaa346SLen Brown return 0; 2019103a8feaSLen Brown } 2020103a8feaSLen Brown 2021ee7e38e3SLen Brown /* 2022ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2023ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2024ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2025ee7e38e3SLen Brown */ 2026ee7e38e3SLen Brown 2027ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2028ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2029ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2030ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2031ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2032ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2033ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2034ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2035ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2036ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2037ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2038ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 20390b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 20400b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2041445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2042445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2043ee7e38e3SLen Brown 2044ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2045ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 2046445640a5SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"}; 2047ee7e38e3SLen Brown 2048e9257f5fSLen 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}; 2049e9257f5fSLen 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}; 2050e9257f5fSLen 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}; 20510539ba11SLen 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}; 2052f2642888SLen 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}; 2053e9257f5fSLen 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}; 2054445640a5SLen 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}; 20552085e124SArtem 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}; 2056ee7e38e3SLen Brown 2057a2b7b749SLen Brown 2058a2b7b749SLen Brown static void 2059a2b7b749SLen Brown calculate_tsc_tweak() 2060a2b7b749SLen Brown { 2061a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2062a2b7b749SLen Brown } 2063a2b7b749SLen Brown 2064fcd17211SLen Brown static void 2065fcd17211SLen Brown dump_nhm_platform_info(void) 2066103a8feaSLen Brown { 2067103a8feaSLen Brown unsigned long long msr; 2068103a8feaSLen Brown unsigned int ratio; 2069103a8feaSLen Brown 2070ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2071103a8feaSLen Brown 2072b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 20736574a5d5SLen Brown 2074103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 2075710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 2076103a8feaSLen Brown ratio, bclk, ratio * bclk); 2077103a8feaSLen Brown 2078103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 2079710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 2080103a8feaSLen Brown ratio, bclk, ratio * bclk); 2081103a8feaSLen Brown 20827ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2083b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2084bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 208567920418SLen Brown 2086fcd17211SLen Brown return; 2087fcd17211SLen Brown } 2088fcd17211SLen Brown 2089fcd17211SLen Brown static void 2090fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 2091fcd17211SLen Brown { 2092fcd17211SLen Brown unsigned long long msr; 2093fcd17211SLen Brown unsigned int ratio; 2094fcd17211SLen Brown 20957ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2096fcd17211SLen Brown 2097b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2098fcd17211SLen Brown 2099fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2100fcd17211SLen Brown if (ratio) 2101710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 2102fcd17211SLen Brown ratio, bclk, ratio * bclk); 2103fcd17211SLen Brown 2104fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2105fcd17211SLen Brown if (ratio) 2106710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 2107fcd17211SLen Brown ratio, bclk, ratio * bclk); 2108fcd17211SLen Brown return; 2109fcd17211SLen Brown } 2110fcd17211SLen Brown 2111fcd17211SLen Brown static void 2112fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 2113fcd17211SLen Brown { 2114fcd17211SLen Brown unsigned long long msr; 2115fcd17211SLen Brown unsigned int ratio; 21166574a5d5SLen Brown 21177ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 21186574a5d5SLen Brown 2119b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 21206574a5d5SLen Brown 21216574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 21226574a5d5SLen Brown if (ratio) 2123710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 21246574a5d5SLen Brown ratio, bclk, ratio * bclk); 21256574a5d5SLen Brown 21266574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 21276574a5d5SLen Brown if (ratio) 2128710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 21296574a5d5SLen Brown ratio, bclk, ratio * bclk); 21306574a5d5SLen Brown 21316574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 21326574a5d5SLen Brown if (ratio) 2133710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 21346574a5d5SLen Brown ratio, bclk, ratio * bclk); 21356574a5d5SLen Brown 21366574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 21376574a5d5SLen Brown if (ratio) 2138710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 21396574a5d5SLen Brown ratio, bclk, ratio * bclk); 21406574a5d5SLen Brown 21416574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 21426574a5d5SLen Brown if (ratio) 2143710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 21446574a5d5SLen Brown ratio, bclk, ratio * bclk); 21456574a5d5SLen Brown 21466574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 21476574a5d5SLen Brown if (ratio) 2148710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 21496574a5d5SLen Brown ratio, bclk, ratio * bclk); 21506574a5d5SLen Brown 21516574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 21526574a5d5SLen Brown if (ratio) 2153710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 21546574a5d5SLen Brown ratio, bclk, ratio * bclk); 21556574a5d5SLen Brown 21566574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 21576574a5d5SLen Brown if (ratio) 2158710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 21596574a5d5SLen Brown ratio, bclk, ratio * bclk); 2160103a8feaSLen Brown return; 2161fcd17211SLen Brown } 216231e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 216331e07522SLen Brown { 216431e07522SLen Brown 216531e07522SLen Brown if (!genuine_intel) 216631e07522SLen Brown return 0; 216731e07522SLen Brown 216831e07522SLen Brown switch (model) { 216931e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 217031e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 2171f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: 217231e07522SLen Brown return 1; 217331e07522SLen Brown } 217431e07522SLen Brown return 0; 217531e07522SLen Brown } 2176fcd17211SLen Brown 2177fcd17211SLen Brown static void 217831e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 2179fcd17211SLen Brown { 218031e07522SLen Brown unsigned long long msr, core_counts; 218131e07522SLen Brown unsigned int ratio, group_size; 2182103a8feaSLen Brown 21837ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2184b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 21856574a5d5SLen Brown 218631e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 218731e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 218831e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 218931e07522SLen Brown } else { 219031e07522SLen Brown core_counts = 0x0807060504030201; 219131e07522SLen Brown } 219231e07522SLen Brown 21936574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 219431e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 21956574a5d5SLen Brown if (ratio) 219631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 219731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 21986574a5d5SLen Brown 21996574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 220031e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 22016574a5d5SLen Brown if (ratio) 220231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 220331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 22046574a5d5SLen Brown 22056574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 220631e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 22076574a5d5SLen Brown if (ratio) 220831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 220931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 22106574a5d5SLen Brown 22116574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 221231e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 22136574a5d5SLen Brown if (ratio) 221431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 221531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 22166574a5d5SLen Brown 2217103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 221831e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2219103a8feaSLen Brown if (ratio) 222031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 222131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2222103a8feaSLen Brown 2223103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 222431e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2225103a8feaSLen Brown if (ratio) 222631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 222731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2228103a8feaSLen Brown 2229103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 223031e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2231103a8feaSLen Brown if (ratio) 223231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 223331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2234103a8feaSLen Brown 2235103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 223631e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2237103a8feaSLen Brown if (ratio) 223831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 223931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2240fcd17211SLen Brown return; 2241fcd17211SLen Brown } 22423a9a941dSLen Brown 2243fcd17211SLen Brown static void 22440f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 22450f7887c4SLen Brown { 22460f7887c4SLen Brown unsigned long long msr; 22470f7887c4SLen Brown unsigned int ratio; 22480f7887c4SLen Brown 22490f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 22500f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 22510f7887c4SLen Brown 22520f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 22530f7887c4SLen Brown if (ratio) 22540f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 22550f7887c4SLen Brown ratio, bclk, ratio * bclk); 22560f7887c4SLen Brown 22570f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 22580f7887c4SLen Brown if (ratio) 22590f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 22600f7887c4SLen Brown ratio, bclk, ratio * bclk); 22610f7887c4SLen Brown 22620f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 22630f7887c4SLen Brown if (ratio) 22640f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 22650f7887c4SLen Brown ratio, bclk, ratio * bclk); 22660f7887c4SLen Brown 22670f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 22680f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 22690f7887c4SLen Brown 22700f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 22710f7887c4SLen Brown if (ratio) 22720f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 22730f7887c4SLen Brown ratio, bclk, ratio * bclk); 22740f7887c4SLen Brown 22750f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 22760f7887c4SLen Brown if (ratio) 22770f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 22780f7887c4SLen Brown ratio, bclk, ratio * bclk); 22790f7887c4SLen Brown 22800f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 22810f7887c4SLen Brown if (ratio) 22820f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 22830f7887c4SLen Brown ratio, bclk, ratio * bclk); 22840f7887c4SLen Brown 22850f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 22860f7887c4SLen Brown if (ratio) 22870f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 22880f7887c4SLen Brown ratio, bclk, ratio * bclk); 22890f7887c4SLen Brown } 22900f7887c4SLen Brown 22910f7887c4SLen Brown static void 2292fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2293fb5d4327SDasaratharaman Chandramouli { 2294cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2295cbf97abaSHubert Chrzaniuk 2296fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2297cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2298cbf97abaSHubert Chrzaniuk int i, b_nr; 2299cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2300cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2301fb5d4327SDasaratharaman Chandramouli 2302ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2303fb5d4327SDasaratharaman Chandramouli 2304b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2305bfae2052SLen Brown base_cpu, msr); 2306fb5d4327SDasaratharaman Chandramouli 2307fb5d4327SDasaratharaman Chandramouli /** 2308fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2309cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2310cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2311fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2312fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2313fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2314fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2315fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2316fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2317fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2318fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2319fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2320fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2321fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2322fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2323fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2324fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2325fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2326fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2327fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2328fb5d4327SDasaratharaman Chandramouli */ 2329fb5d4327SDasaratharaman Chandramouli 2330cbf97abaSHubert Chrzaniuk b_nr = 0; 2331cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2332cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2333cbf97abaSHubert Chrzaniuk 2334cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2335fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2336cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2337fb5d4327SDasaratharaman Chandramouli 2338cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2339cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2340cbf97abaSHubert Chrzaniuk b_nr++; 2341fb5d4327SDasaratharaman Chandramouli } 2342cbf97abaSHubert Chrzaniuk 2343cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2344cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2345b7d8c148SLen Brown fprintf(outf, 2346710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2347cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2348fb5d4327SDasaratharaman Chandramouli } 2349fb5d4327SDasaratharaman Chandramouli 2350fb5d4327SDasaratharaman Chandramouli static void 2351fcd17211SLen Brown dump_nhm_cst_cfg(void) 2352fcd17211SLen Brown { 2353fcd17211SLen Brown unsigned long long msr; 2354fcd17211SLen Brown 23551df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2356fcd17211SLen Brown 23571df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2358fcd17211SLen Brown 23593e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2360fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2361fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2362fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2363fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2364fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 23656c34f160SLen Brown (unsigned int)msr & 0xF, 2366fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2367ac980e13SArtem Bityutskiy 2368ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2369ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2370ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2371ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2372ac980e13SArtem Bityutskiy } 2373ac980e13SArtem Bityutskiy 2374ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2375ac980e13SArtem Bityutskiy 2376fcd17211SLen Brown return; 2377103a8feaSLen Brown } 2378103a8feaSLen Brown 23796fb3143bSLen Brown static void 23806fb3143bSLen Brown dump_config_tdp(void) 23816fb3143bSLen Brown { 23826fb3143bSLen Brown unsigned long long msr; 23836fb3143bSLen Brown 23846fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2385b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2386685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 23876fb3143bSLen Brown 23886fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2389b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 23906fb3143bSLen Brown if (msr) { 2391685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2392685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2393685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2394685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 23956fb3143bSLen Brown } 2396b7d8c148SLen Brown fprintf(outf, ")\n"); 23976fb3143bSLen Brown 23986fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2399b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 24006fb3143bSLen Brown if (msr) { 2401685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2402685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2403685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2404685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 24056fb3143bSLen Brown } 2406b7d8c148SLen Brown fprintf(outf, ")\n"); 24076fb3143bSLen Brown 24086fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2409b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 24106fb3143bSLen Brown if ((msr) & 0x3) 2411b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2412b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2413b7d8c148SLen Brown fprintf(outf, ")\n"); 24146fb3143bSLen Brown 24156fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2416b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2417685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2418b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2419b7d8c148SLen Brown fprintf(outf, ")\n"); 24206fb3143bSLen Brown } 24215a63426eSLen Brown 24225a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 24235a63426eSLen Brown 24245a63426eSLen Brown void print_irtl(void) 24255a63426eSLen Brown { 24265a63426eSLen Brown unsigned long long msr; 24275a63426eSLen Brown 24285a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 24295a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 24305a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24315a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24325a63426eSLen Brown 24335a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 24345a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 24355a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24365a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24375a63426eSLen Brown 24385a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 24395a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 24405a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24415a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24425a63426eSLen Brown 24435a63426eSLen Brown if (!do_irtl_hsw) 24445a63426eSLen Brown return; 24455a63426eSLen Brown 24465a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 24475a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 24485a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24495a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24505a63426eSLen Brown 24515a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 24525a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 24535a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24545a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24555a63426eSLen Brown 24565a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 24575a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 24585a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24595a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24605a63426eSLen Brown 24615a63426eSLen Brown } 246236229897SLen Brown void free_fd_percpu(void) 246336229897SLen Brown { 246436229897SLen Brown int i; 246536229897SLen Brown 246601a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 246736229897SLen Brown if (fd_percpu[i] != 0) 246836229897SLen Brown close(fd_percpu[i]); 246936229897SLen Brown } 247036229897SLen Brown 247136229897SLen Brown free(fd_percpu); 24726fb3143bSLen Brown } 24736fb3143bSLen Brown 2474c98d5d94SLen Brown void free_all_buffers(void) 2475103a8feaSLen Brown { 24760e2d8f05SLen Brown int i; 24770e2d8f05SLen Brown 2478c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2479c98d5d94SLen Brown cpu_present_set = NULL; 248036229897SLen Brown cpu_present_setsize = 0; 2481103a8feaSLen Brown 2482c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2483c98d5d94SLen Brown cpu_affinity_set = NULL; 2484c98d5d94SLen Brown cpu_affinity_setsize = 0; 2485103a8feaSLen Brown 2486c98d5d94SLen Brown free(thread_even); 2487c98d5d94SLen Brown free(core_even); 2488c98d5d94SLen Brown free(package_even); 2489103a8feaSLen Brown 2490c98d5d94SLen Brown thread_even = NULL; 2491c98d5d94SLen Brown core_even = NULL; 2492c98d5d94SLen Brown package_even = NULL; 2493103a8feaSLen Brown 2494c98d5d94SLen Brown free(thread_odd); 2495c98d5d94SLen Brown free(core_odd); 2496c98d5d94SLen Brown free(package_odd); 2497103a8feaSLen Brown 2498c98d5d94SLen Brown thread_odd = NULL; 2499c98d5d94SLen Brown core_odd = NULL; 2500c98d5d94SLen Brown package_odd = NULL; 2501103a8feaSLen Brown 2502c98d5d94SLen Brown free(output_buffer); 2503c98d5d94SLen Brown output_buffer = NULL; 2504c98d5d94SLen Brown outp = NULL; 250536229897SLen Brown 250636229897SLen Brown free_fd_percpu(); 2507562a2d37SLen Brown 2508562a2d37SLen Brown free(irq_column_2_cpu); 2509562a2d37SLen Brown free(irqs_per_cpu); 25100e2d8f05SLen Brown 25110e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 25120e2d8f05SLen Brown if (cpus[i].put_ids) 25130e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 25140e2d8f05SLen Brown } 25150e2d8f05SLen Brown free(cpus); 2516103a8feaSLen Brown } 2517103a8feaSLen Brown 251857a42a34SJosh Triplett 251957a42a34SJosh Triplett /* 252095aebc44SJosh Triplett * Parse a file containing a single int. 25216de68fe1SLen Brown * Return 0 if file can not be opened 25226de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 252395aebc44SJosh Triplett */ 252495aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 252595aebc44SJosh Triplett { 252695aebc44SJosh Triplett va_list args; 252795aebc44SJosh Triplett char path[PATH_MAX]; 252895aebc44SJosh Triplett FILE *filep; 252995aebc44SJosh Triplett int value; 253095aebc44SJosh Triplett 253195aebc44SJosh Triplett va_start(args, fmt); 253295aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 253395aebc44SJosh Triplett va_end(args); 25346de68fe1SLen Brown filep = fopen(path, "r"); 25356de68fe1SLen Brown if (!filep) 25366de68fe1SLen Brown return 0; 2537b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2538b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 253995aebc44SJosh Triplett fclose(filep); 254095aebc44SJosh Triplett return value; 254195aebc44SJosh Triplett } 254295aebc44SJosh Triplett 254395aebc44SJosh Triplett /* 2544c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2545c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2546c98d5d94SLen Brown */ 2547c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2548c98d5d94SLen Brown { 254995aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2550103a8feaSLen Brown } 2551103a8feaSLen Brown 2552103a8feaSLen Brown int get_physical_package_id(int cpu) 2553103a8feaSLen Brown { 255495aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2555103a8feaSLen Brown } 2556103a8feaSLen Brown 25576de68fe1SLen Brown int get_die_id(int cpu) 25586de68fe1SLen Brown { 25596de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 25606de68fe1SLen Brown } 25616de68fe1SLen Brown 2562103a8feaSLen Brown int get_core_id(int cpu) 2563103a8feaSLen Brown { 256495aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2565103a8feaSLen Brown } 2566103a8feaSLen Brown 2567ef605741SPrarit Bhargava void set_node_data(void) 2568ef605741SPrarit Bhargava { 25692ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 25702ffbb224SPrarit Bhargava int cpu_count; 2571ef605741SPrarit Bhargava 25722ffbb224SPrarit Bhargava /* initialize logical_node_id */ 25732ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 25742ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2575ef605741SPrarit Bhargava 25762ffbb224SPrarit Bhargava cpu_count = 0; 25772ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 25782ffbb224SPrarit Bhargava lnode = 0; 25792ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 25802ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2581ef605741SPrarit Bhargava continue; 25822ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 25832ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 25842ffbb224SPrarit Bhargava continue; 25852ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2586ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 25872ffbb224SPrarit Bhargava cpu_count++; 25882ffbb224SPrarit Bhargava /* 25892ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 25902ffbb224SPrarit Bhargava * the logical_node_id 25912ffbb224SPrarit Bhargava */ 25922ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 25932ffbb224SPrarit Bhargava if ((cpus[cpux].physical_package_id == pkg) && 25942ffbb224SPrarit Bhargava (cpus[cpux].physical_node_id == node)) { 25952ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 25962ffbb224SPrarit Bhargava cpu_count++; 2597ef605741SPrarit Bhargava } 25982ffbb224SPrarit Bhargava } 25992ffbb224SPrarit Bhargava lnode++; 26002ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 26012ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 26022ffbb224SPrarit Bhargava } 26032ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 26042ffbb224SPrarit Bhargava break; 26052ffbb224SPrarit Bhargava } 2606ef605741SPrarit Bhargava } 2607ef605741SPrarit Bhargava 2608ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2609c98d5d94SLen Brown { 2610c98d5d94SLen Brown char path[80]; 2611c98d5d94SLen Brown FILE *filep; 26120e2d8f05SLen Brown int i; 26130e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2614c98d5d94SLen Brown 26150e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 26160e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 26170e2d8f05SLen Brown cpu, i); 26180e2d8f05SLen Brown filep = fopen(path, "r"); 26190e2d8f05SLen Brown if (!filep) 26200e2d8f05SLen Brown continue; 26210e2d8f05SLen Brown fclose(filep); 26220e2d8f05SLen Brown return i; 26230e2d8f05SLen Brown } 26240e2d8f05SLen Brown return -1; 2625e275b388SDasaratharaman Chandramouli } 2626c98d5d94SLen Brown 26270e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 26280e2d8f05SLen Brown { 26290e2d8f05SLen Brown char path[80], character; 26300e2d8f05SLen Brown FILE *filep; 26310e2d8f05SLen Brown unsigned long map; 26328cb48b32SPrarit Bhargava int so, shift, sib_core; 26330e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 26340e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 26350e2d8f05SLen Brown size_t size; 26368cb48b32SPrarit Bhargava int thread_id = 0; 26370e2d8f05SLen Brown 26380e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 26398cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 26408cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 26410e2d8f05SLen Brown if (!thiscpu->put_ids) 26420e2d8f05SLen Brown return -1; 26430e2d8f05SLen Brown 26440e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 26450e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 26460e2d8f05SLen Brown 26470e2d8f05SLen Brown sprintf(path, 26480e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 26490e2d8f05SLen Brown filep = fopen_or_die(path, "r"); 26500e2d8f05SLen Brown do { 26510e2d8f05SLen Brown offset -= BITMASK_SIZE; 26528173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 26538173c336SBen Hutchings err(1, "%s: failed to parse file", path); 26540e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 26550e2d8f05SLen Brown if ((map >> shift) & 0x1) { 26568cb48b32SPrarit Bhargava so = shift + offset; 26578cb48b32SPrarit Bhargava sib_core = get_core_id(so); 26588cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 26598cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 26608cb48b32SPrarit Bhargava if ((so != cpu) && 26618cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 26628cb48b32SPrarit Bhargava cpus[so].thread_id = 26638cb48b32SPrarit Bhargava thread_id++; 26648cb48b32SPrarit Bhargava } 26650e2d8f05SLen Brown } 26660e2d8f05SLen Brown } 26670e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2668c98d5d94SLen Brown fclose(filep); 26690e2d8f05SLen Brown 26700e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2671c98d5d94SLen Brown } 2672c98d5d94SLen Brown 2673c98d5d94SLen Brown /* 2674c98d5d94SLen Brown * run func(thread, core, package) in topology order 2675c98d5d94SLen Brown * skip non-present cpus 2676103a8feaSLen Brown */ 2677103a8feaSLen Brown 2678c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2679c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2680c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2681c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2682c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2683c98d5d94SLen Brown struct pkg_data *pkg_base2) 2684c98d5d94SLen Brown { 268540f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2686c98d5d94SLen Brown 2687c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 268840f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 268940f5cfe7SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; 269040f5cfe7SPrarit Bhargava ++core_no) { 2691c98d5d94SLen Brown for (thread_no = 0; thread_no < 269270a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2693c98d5d94SLen Brown struct thread_data *t, *t2; 2694c98d5d94SLen Brown struct core_data *c, *c2; 2695c98d5d94SLen Brown struct pkg_data *p, *p2; 2696c98d5d94SLen Brown 269740f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 269840f5cfe7SPrarit Bhargava core_no, node_no, 269940f5cfe7SPrarit Bhargava pkg_no); 2700c98d5d94SLen Brown 2701c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2702c98d5d94SLen Brown continue; 2703c98d5d94SLen Brown 270440f5cfe7SPrarit Bhargava t2 = GET_THREAD(thread_base2, thread_no, 270540f5cfe7SPrarit Bhargava core_no, node_no, 270640f5cfe7SPrarit Bhargava pkg_no); 2707c98d5d94SLen Brown 270840f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 270940f5cfe7SPrarit Bhargava node_no, pkg_no); 271040f5cfe7SPrarit Bhargava c2 = GET_CORE(core_base2, core_no, 271140f5cfe7SPrarit Bhargava node_no, 271240f5cfe7SPrarit Bhargava pkg_no); 2713c98d5d94SLen Brown 2714c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2715c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2716c98d5d94SLen Brown 2717c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2718c98d5d94SLen Brown if (retval) 2719c98d5d94SLen Brown return retval; 2720c98d5d94SLen Brown } 2721c98d5d94SLen Brown } 2722c98d5d94SLen Brown } 272340f5cfe7SPrarit Bhargava } 2724c98d5d94SLen Brown return 0; 2725c98d5d94SLen Brown } 2726c98d5d94SLen Brown 2727c98d5d94SLen Brown /* 2728c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2729c98d5d94SLen Brown * return max_cpu number 2730c98d5d94SLen Brown */ 2731c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2732103a8feaSLen Brown { 2733103a8feaSLen Brown FILE *fp; 2734c98d5d94SLen Brown int cpu_num; 2735103a8feaSLen Brown int retval; 2736103a8feaSLen Brown 273757a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2738103a8feaSLen Brown 2739103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2740b2c95d90SJosh Triplett if (retval != 0) 2741b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2742103a8feaSLen Brown 2743c98d5d94SLen Brown while (1) { 2744c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2745103a8feaSLen Brown if (retval != 1) 2746103a8feaSLen Brown break; 2747103a8feaSLen Brown 2748c98d5d94SLen Brown retval = func(cpu_num); 2749c98d5d94SLen Brown if (retval) { 2750c98d5d94SLen Brown fclose(fp); 2751c98d5d94SLen Brown return(retval); 2752c98d5d94SLen Brown } 2753103a8feaSLen Brown } 2754103a8feaSLen Brown fclose(fp); 2755c98d5d94SLen Brown return 0; 2756103a8feaSLen Brown } 2757103a8feaSLen Brown 2758103a8feaSLen Brown void re_initialize(void) 2759103a8feaSLen Brown { 2760c98d5d94SLen Brown free_all_buffers(); 2761c98d5d94SLen Brown setup_all_buffers(); 2762c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2763103a8feaSLen Brown } 2764103a8feaSLen Brown 2765843c5791SPrarit Bhargava void set_max_cpu_num(void) 2766843c5791SPrarit Bhargava { 2767843c5791SPrarit Bhargava FILE *filep; 2768843c5791SPrarit Bhargava unsigned long dummy; 2769843c5791SPrarit Bhargava 2770843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2771843c5791SPrarit Bhargava filep = fopen_or_die( 2772843c5791SPrarit Bhargava "/sys/devices/system/cpu/cpu0/topology/thread_siblings", 2773843c5791SPrarit Bhargava "r"); 2774843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 27750e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2776843c5791SPrarit Bhargava fclose(filep); 2777843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2778843c5791SPrarit Bhargava } 2779c98d5d94SLen Brown 2780103a8feaSLen Brown /* 2781c98d5d94SLen Brown * count_cpus() 2782c98d5d94SLen Brown * remember the last one seen, it will be the max 2783103a8feaSLen Brown */ 2784c98d5d94SLen Brown int count_cpus(int cpu) 2785103a8feaSLen Brown { 2786843c5791SPrarit Bhargava topo.num_cpus++; 2787c98d5d94SLen Brown return 0; 2788103a8feaSLen Brown } 2789c98d5d94SLen Brown int mark_cpu_present(int cpu) 2790c98d5d94SLen Brown { 2791c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 279215aaa346SLen Brown return 0; 2793103a8feaSLen Brown } 2794103a8feaSLen Brown 27958cb48b32SPrarit Bhargava int init_thread_id(int cpu) 27968cb48b32SPrarit Bhargava { 27978cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 27988cb48b32SPrarit Bhargava return 0; 27998cb48b32SPrarit Bhargava } 28008cb48b32SPrarit Bhargava 2801562a2d37SLen Brown /* 2802562a2d37SLen Brown * snapshot_proc_interrupts() 2803562a2d37SLen Brown * 2804562a2d37SLen Brown * read and record summary of /proc/interrupts 2805562a2d37SLen Brown * 2806562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2807562a2d37SLen Brown */ 2808562a2d37SLen Brown int snapshot_proc_interrupts(void) 2809562a2d37SLen Brown { 2810562a2d37SLen Brown static FILE *fp; 2811562a2d37SLen Brown int column, retval; 2812562a2d37SLen Brown 2813562a2d37SLen Brown if (fp == NULL) 2814562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2815562a2d37SLen Brown else 2816562a2d37SLen Brown rewind(fp); 2817562a2d37SLen Brown 2818562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2819562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2820562a2d37SLen Brown int cpu_number; 2821562a2d37SLen Brown 2822562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2823562a2d37SLen Brown if (retval != 1) 2824562a2d37SLen Brown break; 2825562a2d37SLen Brown 2826562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2827562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2828562a2d37SLen Brown return 1; 2829562a2d37SLen Brown } 2830562a2d37SLen Brown 2831562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2832562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2833562a2d37SLen Brown } 2834562a2d37SLen Brown 2835562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2836562a2d37SLen Brown while (1) { 2837562a2d37SLen Brown int column; 2838562a2d37SLen Brown char buf[64]; 2839562a2d37SLen Brown 2840562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2841562a2d37SLen Brown if (retval != 1) 2842562a2d37SLen Brown break; 2843562a2d37SLen Brown 2844562a2d37SLen Brown /* read the count per cpu */ 2845562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2846562a2d37SLen Brown 2847562a2d37SLen Brown int cpu_number, irq_count; 2848562a2d37SLen Brown 2849562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2850562a2d37SLen Brown if (retval != 1) 2851562a2d37SLen Brown break; 2852562a2d37SLen Brown 2853562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2854562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2855562a2d37SLen Brown 2856562a2d37SLen Brown } 2857562a2d37SLen Brown 2858562a2d37SLen Brown while (getc(fp) != '\n') 2859562a2d37SLen Brown ; /* flush interrupt description */ 2860562a2d37SLen Brown 2861562a2d37SLen Brown } 2862562a2d37SLen Brown return 0; 2863562a2d37SLen Brown } 286427d47356SLen Brown /* 2865fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2866fdf676e5SLen Brown * 2867fdf676e5SLen Brown * record snapshot of 2868fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2869fdf676e5SLen Brown * 2870fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2871fdf676e5SLen Brown */ 2872fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2873fdf676e5SLen Brown { 2874fdf676e5SLen Brown FILE *fp; 2875fdf676e5SLen Brown int retval; 2876fdf676e5SLen Brown 2877fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 2878fdf676e5SLen Brown 2879fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 2880fdf676e5SLen Brown if (retval != 1) 2881fdf676e5SLen Brown err(1, "GFX rc6"); 2882fdf676e5SLen Brown 2883fdf676e5SLen Brown fclose(fp); 2884fdf676e5SLen Brown 2885fdf676e5SLen Brown return 0; 2886fdf676e5SLen Brown } 2887fdf676e5SLen Brown /* 288827d47356SLen Brown * snapshot_gfx_mhz() 288927d47356SLen Brown * 289027d47356SLen Brown * record snapshot of 289127d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 289227d47356SLen Brown * 289327d47356SLen Brown * return 1 if config change requires a restart, else return 0 289427d47356SLen Brown */ 289527d47356SLen Brown int snapshot_gfx_mhz(void) 289627d47356SLen Brown { 289727d47356SLen Brown static FILE *fp; 289827d47356SLen Brown int retval; 289927d47356SLen Brown 290027d47356SLen Brown if (fp == NULL) 290127d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 290222048c54SLen Brown else { 290327d47356SLen Brown rewind(fp); 290422048c54SLen Brown fflush(fp); 290522048c54SLen Brown } 290627d47356SLen Brown 290727d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 290827d47356SLen Brown if (retval != 1) 290927d47356SLen Brown err(1, "GFX MHz"); 291027d47356SLen Brown 291127d47356SLen Brown return 0; 291227d47356SLen Brown } 2913562a2d37SLen Brown 2914562a2d37SLen Brown /* 2915be0e54c4SLen Brown * snapshot_cpu_lpi() 2916be0e54c4SLen Brown * 2917be0e54c4SLen Brown * record snapshot of 2918be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 2919be0e54c4SLen Brown * 2920be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2921be0e54c4SLen Brown */ 2922be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 2923be0e54c4SLen Brown { 2924be0e54c4SLen Brown FILE *fp; 2925be0e54c4SLen Brown int retval; 2926be0e54c4SLen Brown 2927be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 2928be0e54c4SLen Brown 2929be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 29305ea7647bSPrarit Bhargava if (retval != 1) { 29315ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 29325ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 2933605736c6SGustavo A. R. Silva fclose(fp); 29345ea7647bSPrarit Bhargava return -1; 29355ea7647bSPrarit Bhargava } 2936be0e54c4SLen Brown 2937be0e54c4SLen Brown fclose(fp); 2938be0e54c4SLen Brown 2939be0e54c4SLen Brown return 0; 2940be0e54c4SLen Brown } 2941be0e54c4SLen Brown /* 2942be0e54c4SLen Brown * snapshot_sys_lpi() 2943be0e54c4SLen Brown * 2944be0e54c4SLen Brown * record snapshot of 2945be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us 2946be0e54c4SLen Brown * 2947be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2948be0e54c4SLen Brown */ 2949be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 2950be0e54c4SLen Brown { 2951be0e54c4SLen Brown FILE *fp; 2952be0e54c4SLen Brown int retval; 2953be0e54c4SLen Brown 2954be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r"); 2955be0e54c4SLen Brown 2956be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 29575ea7647bSPrarit Bhargava if (retval != 1) { 29585ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 29595ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 296015423b95SColin Ian King fclose(fp); 29615ea7647bSPrarit Bhargava return -1; 29625ea7647bSPrarit Bhargava } 2963be0e54c4SLen Brown fclose(fp); 2964be0e54c4SLen Brown 2965be0e54c4SLen Brown return 0; 2966be0e54c4SLen Brown } 2967be0e54c4SLen Brown /* 2968562a2d37SLen Brown * snapshot /proc and /sys files 2969562a2d37SLen Brown * 2970562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 2971562a2d37SLen Brown */ 2972562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 2973562a2d37SLen Brown { 2974218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 2975562a2d37SLen Brown if (snapshot_proc_interrupts()) 2976562a2d37SLen Brown return 1; 2977562a2d37SLen Brown 2978812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2979fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 2980fdf676e5SLen Brown 2981812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 298227d47356SLen Brown snapshot_gfx_mhz(); 298327d47356SLen Brown 2984be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2985be0e54c4SLen Brown snapshot_cpu_lpi_us(); 2986be0e54c4SLen Brown 2987be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2988be0e54c4SLen Brown snapshot_sys_lpi_us(); 2989be0e54c4SLen Brown 2990562a2d37SLen Brown return 0; 2991562a2d37SLen Brown } 2992562a2d37SLen Brown 29938aa2ed0bSLen Brown int exit_requested; 29948aa2ed0bSLen Brown 29958aa2ed0bSLen Brown static void signal_handler (int signal) 29968aa2ed0bSLen Brown { 29978aa2ed0bSLen Brown switch (signal) { 29988aa2ed0bSLen Brown case SIGINT: 29998aa2ed0bSLen Brown exit_requested = 1; 30008aa2ed0bSLen Brown if (debug) 30018aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 30028aa2ed0bSLen Brown break; 300307211960SLen Brown case SIGUSR1: 300407211960SLen Brown if (debug > 1) 300507211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 300607211960SLen Brown break; 30078aa2ed0bSLen Brown } 30088aa2ed0bSLen Brown } 30098aa2ed0bSLen Brown 30108aa2ed0bSLen Brown void setup_signal_handler(void) 30118aa2ed0bSLen Brown { 30128aa2ed0bSLen Brown struct sigaction sa; 30138aa2ed0bSLen Brown 30148aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 30158aa2ed0bSLen Brown 30168aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 30178aa2ed0bSLen Brown 30188aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 30198aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 302007211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 302107211960SLen Brown err(1, "sigaction SIGUSR1"); 30228aa2ed0bSLen Brown } 3023b9ad8ee0SLen Brown 302447936f94SArtem Bityutskiy void do_sleep(void) 3025b9ad8ee0SLen Brown { 3026c026c236SArtem Bityutskiy struct timeval tout; 3027c026c236SArtem Bityutskiy struct timespec rest; 3028b9ad8ee0SLen Brown fd_set readfds; 3029b9ad8ee0SLen Brown int retval; 3030b9ad8ee0SLen Brown 3031b9ad8ee0SLen Brown FD_ZERO(&readfds); 3032b9ad8ee0SLen Brown FD_SET(0, &readfds); 3033b9ad8ee0SLen Brown 3034c026c236SArtem Bityutskiy if (ignore_stdin) { 303547936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 303647936f94SArtem Bityutskiy return; 303747936f94SArtem Bityutskiy } 3038b9ad8ee0SLen Brown 3039c026c236SArtem Bityutskiy tout = interval_tv; 3040c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3041b9ad8ee0SLen Brown 3042b9ad8ee0SLen Brown if (retval == 1) { 3043b9ad8ee0SLen Brown switch (getc(stdin)) { 3044b9ad8ee0SLen Brown case 'q': 3045b9ad8ee0SLen Brown exit_requested = 1; 3046b9ad8ee0SLen Brown break; 3047c026c236SArtem Bityutskiy case EOF: 3048c026c236SArtem Bityutskiy /* 3049c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3050c026c236SArtem Bityutskiy * won't be any further input. 3051c026c236SArtem Bityutskiy */ 3052c026c236SArtem Bityutskiy ignore_stdin = 1; 3053c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3054c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3055c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3056c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3057b9ad8ee0SLen Brown } 3058b9ad8ee0SLen Brown } 3059b9ad8ee0SLen Brown } 306047936f94SArtem Bityutskiy 30614c2122d4SLen Brown 3062103a8feaSLen Brown void turbostat_loop() 3063103a8feaSLen Brown { 3064c98d5d94SLen Brown int retval; 3065e52966c0SLen Brown int restarted = 0; 3066023fe0acSChen Yu int done_iters = 0; 3067c98d5d94SLen Brown 30688aa2ed0bSLen Brown setup_signal_handler(); 30698aa2ed0bSLen Brown 3070103a8feaSLen Brown restart: 3071e52966c0SLen Brown restarted++; 3072e52966c0SLen Brown 3073562a2d37SLen Brown snapshot_proc_sysfs_files(); 3074c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 30754c2122d4SLen Brown first_counter_read = 0; 3076d91bb17cSLen Brown if (retval < -1) { 3077d91bb17cSLen Brown exit(retval); 3078d91bb17cSLen Brown } else if (retval == -1) { 3079e52966c0SLen Brown if (restarted > 1) { 3080e52966c0SLen Brown exit(retval); 3081e52966c0SLen Brown } 3082c98d5d94SLen Brown re_initialize(); 3083c98d5d94SLen Brown goto restart; 3084c98d5d94SLen Brown } 3085e52966c0SLen Brown restarted = 0; 3086023fe0acSChen Yu done_iters = 0; 3087103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3088103a8feaSLen Brown 3089103a8feaSLen Brown while (1) { 3090c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3091103a8feaSLen Brown re_initialize(); 3092103a8feaSLen Brown goto restart; 3093103a8feaSLen Brown } 3094b9ad8ee0SLen Brown do_sleep(); 3095562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3096562a2d37SLen Brown goto restart; 3097c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3098d91bb17cSLen Brown if (retval < -1) { 3099d91bb17cSLen Brown exit(retval); 3100d91bb17cSLen Brown } else if (retval == -1) { 310115aaa346SLen Brown re_initialize(); 310215aaa346SLen Brown goto restart; 310315aaa346SLen Brown } 3104103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3105103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3106ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3107ba3dec99SLen Brown re_initialize(); 3108ba3dec99SLen Brown goto restart; 3109ba3dec99SLen Brown } 3110c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3111c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3112b7d8c148SLen Brown flush_output_stdout(); 31138aa2ed0bSLen Brown if (exit_requested) 31148aa2ed0bSLen Brown break; 3115023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3116023fe0acSChen Yu break; 3117b9ad8ee0SLen Brown do_sleep(); 3118562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3119562a2d37SLen Brown goto restart; 3120c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3121d91bb17cSLen Brown if (retval < -1) { 3122d91bb17cSLen Brown exit(retval); 3123d91bb17cSLen Brown } else if (retval == -1) { 3124103a8feaSLen Brown re_initialize(); 3125103a8feaSLen Brown goto restart; 3126103a8feaSLen Brown } 3127103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3128103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3129ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3130ba3dec99SLen Brown re_initialize(); 3131ba3dec99SLen Brown goto restart; 3132ba3dec99SLen Brown } 3133c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3134c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3135b7d8c148SLen Brown flush_output_stdout(); 31368aa2ed0bSLen Brown if (exit_requested) 31378aa2ed0bSLen Brown break; 3138023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3139023fe0acSChen Yu break; 3140103a8feaSLen Brown } 3141103a8feaSLen Brown } 3142103a8feaSLen Brown 3143103a8feaSLen Brown void check_dev_msr() 3144103a8feaSLen Brown { 3145103a8feaSLen Brown struct stat sb; 31467ce7d5deSPrarit Bhargava char pathname[32]; 3147103a8feaSLen Brown 31487ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 31497ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3150a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3151d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3152103a8feaSLen Brown } 3153103a8feaSLen Brown 315498481e79SLen Brown void check_permissions() 3155103a8feaSLen Brown { 315698481e79SLen Brown struct __user_cap_header_struct cap_header_data; 315798481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 315898481e79SLen Brown struct __user_cap_data_struct cap_data_data; 315998481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 316098481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 316198481e79SLen Brown int do_exit = 0; 31627ce7d5deSPrarit Bhargava char pathname[32]; 316398481e79SLen Brown 316498481e79SLen Brown /* check for CAP_SYS_RAWIO */ 316598481e79SLen Brown cap_header->pid = getpid(); 316698481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 316798481e79SLen Brown if (capget(cap_header, cap_data) < 0) 316898481e79SLen Brown err(-6, "capget(2) failed"); 316998481e79SLen Brown 317098481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 317198481e79SLen Brown do_exit++; 317298481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 317398481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 317498481e79SLen Brown } 317598481e79SLen Brown 317698481e79SLen Brown /* test file permissions */ 31777ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 31787ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 317998481e79SLen Brown do_exit++; 318098481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 318198481e79SLen Brown } 318298481e79SLen Brown 318398481e79SLen Brown /* if all else fails, thell them to be root */ 318498481e79SLen Brown if (do_exit) 3185b2c95d90SJosh Triplett if (getuid() != 0) 3186d7899447SLen Brown warnx("... or simply run as root"); 318798481e79SLen Brown 318898481e79SLen Brown if (do_exit) 318998481e79SLen Brown exit(-6); 3190103a8feaSLen Brown } 3191103a8feaSLen Brown 3192d7899447SLen Brown /* 3193d7899447SLen Brown * NHM adds support for additional MSRs: 3194d7899447SLen Brown * 3195d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3196d7899447SLen Brown * 3197ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 31981df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3199d7899447SLen Brown * 3200cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3201cf4cbe53SLen Brown * 3202d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3203d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3204d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3205d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3206d7899447SLen Brown * 3207ee7e38e3SLen Brown * Side effect: 32081df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 320933148d67SLen Brown * sets has_misc_feature_control 3210d7899447SLen Brown */ 3211ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3212103a8feaSLen Brown { 3213ee7e38e3SLen Brown unsigned long long msr; 321421ed5574SLen Brown unsigned int base_ratio; 3215ee7e38e3SLen Brown int *pkg_cstate_limits; 3216ee7e38e3SLen Brown 3217103a8feaSLen Brown if (!genuine_intel) 3218103a8feaSLen Brown return 0; 3219103a8feaSLen Brown 3220103a8feaSLen Brown if (family != 6) 3221103a8feaSLen Brown return 0; 3222103a8feaSLen Brown 322321ed5574SLen Brown bclk = discover_bclk(family, model); 322421ed5574SLen Brown 3225103a8feaSLen Brown switch (model) { 3226869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3227869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3228ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3229ee7e38e3SLen Brown break; 3230869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3231869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3232869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3233869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3234ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 323533148d67SLen Brown has_misc_feature_control = 1; 3236ee7e38e3SLen Brown break; 3237869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3238869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3239cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3240869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3241869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3242869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3243869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3244869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3245997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3246ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 324733148d67SLen Brown has_misc_feature_control = 1; 3248ee7e38e3SLen Brown break; 3249d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3250d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 325133148d67SLen Brown has_misc_feature_control = 1; 3252d8ebb442SLen Brown break; 3253f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3254cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3255f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 3256ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3257ee7e38e3SLen Brown break; 3258869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3259ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3260cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3261ee7e38e3SLen Brown break; 3262869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3263ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3264ee7e38e3SLen Brown break; 3265869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3266f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 3267f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 3268445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3269e4085d54SLen Brown break; 3270103a8feaSLen Brown default: 3271103a8feaSLen Brown return 0; 3272103a8feaSLen Brown } 32731df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3274e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3275ee7e38e3SLen Brown 3276ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 327721ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 327821ed5574SLen Brown 327921ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 328021ed5574SLen Brown has_base_hz = 1; 3281ee7e38e3SLen Brown return 1; 3282103a8feaSLen Brown } 32830f7887c4SLen Brown /* 3284495c7654SLen Brown * SLV client has support for unique MSRs: 32850f7887c4SLen Brown * 32860f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 32870f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 32880f7887c4SLen Brown */ 32890f7887c4SLen Brown 32900f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 32910f7887c4SLen Brown { 32920f7887c4SLen Brown if (!genuine_intel) 32930f7887c4SLen Brown return 0; 32940f7887c4SLen Brown 32950f7887c4SLen Brown switch (model) { 3296f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3297f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3298f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 32990f7887c4SLen Brown return 1; 33000f7887c4SLen Brown } 33010f7887c4SLen Brown return 0; 33020f7887c4SLen Brown } 33037170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 33047170a374SLen Brown { 33057170a374SLen Brown 33067170a374SLen Brown if (!genuine_intel) 33077170a374SLen Brown return 0; 33087170a374SLen Brown 33097170a374SLen Brown switch (model) { 3310f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: 33117170a374SLen Brown return 1; 33127170a374SLen Brown } 33137170a374SLen Brown return 0; 33147170a374SLen Brown } 3315ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3316ade0ebacSLen Brown { 3317ade0ebacSLen Brown 3318ade0ebacSLen Brown if (!genuine_intel) 3319ade0ebacSLen Brown return 0; 3320ade0ebacSLen Brown 3321ade0ebacSLen Brown switch (model) { 3322ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3323ade0ebacSLen Brown return 1; 3324ade0ebacSLen Brown } 3325ade0ebacSLen Brown return 0; 3326ade0ebacSLen Brown } 332734c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 332834c76197SLen Brown { 332934c76197SLen Brown 333034c76197SLen Brown if (!genuine_intel) 333134c76197SLen Brown return 0; 333234c76197SLen Brown 333334c76197SLen Brown switch (model) { 333434c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 333534c76197SLen Brown return 1; 333634c76197SLen Brown } 333734c76197SLen Brown return 0; 333834c76197SLen Brown } 33390f7887c4SLen Brown 334031e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3341d7899447SLen Brown { 33420f7887c4SLen Brown if (has_slv_msrs(family, model)) 33430f7887c4SLen Brown return 0; 33440f7887c4SLen Brown 3345d7899447SLen Brown switch (model) { 3346d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3347869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3348869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3349d7899447SLen Brown return 0; 3350d7899447SLen Brown default: 3351d7899447SLen Brown return 1; 3352d7899447SLen Brown } 3353d7899447SLen Brown } 33540f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 33550f7887c4SLen Brown { 33560f7887c4SLen Brown if (has_slv_msrs(family, model)) 33570f7887c4SLen Brown return 1; 33580f7887c4SLen Brown 33590f7887c4SLen Brown return 0; 33600f7887c4SLen Brown } 33616574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 33626574a5d5SLen Brown { 33636574a5d5SLen Brown if (!genuine_intel) 33646574a5d5SLen Brown return 0; 33656574a5d5SLen Brown 33666574a5d5SLen Brown if (family != 6) 33676574a5d5SLen Brown return 0; 33686574a5d5SLen Brown 33696574a5d5SLen Brown switch (model) { 3370869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3371869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 33726574a5d5SLen Brown return 1; 33736574a5d5SLen Brown default: 33746574a5d5SLen Brown return 0; 33756574a5d5SLen Brown } 33766574a5d5SLen Brown } 3377fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3378fcd17211SLen Brown { 3379fcd17211SLen Brown if (!genuine_intel) 3380fcd17211SLen Brown return 0; 3381fcd17211SLen Brown 3382fcd17211SLen Brown if (family != 6) 3383fcd17211SLen Brown return 0; 3384fcd17211SLen Brown 3385fcd17211SLen Brown switch (model) { 3386869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3387fcd17211SLen Brown return 1; 3388fcd17211SLen Brown default: 3389fcd17211SLen Brown return 0; 3390fcd17211SLen Brown } 3391fcd17211SLen Brown } 3392fcd17211SLen Brown 3393fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3394fb5d4327SDasaratharaman Chandramouli { 3395fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3396fb5d4327SDasaratharaman Chandramouli return 0; 3397fb5d4327SDasaratharaman Chandramouli 3398fb5d4327SDasaratharaman Chandramouli if (family != 6) 3399fb5d4327SDasaratharaman Chandramouli return 0; 3400fb5d4327SDasaratharaman Chandramouli 3401fb5d4327SDasaratharaman Chandramouli switch (model) { 3402869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3403fb5d4327SDasaratharaman Chandramouli return 1; 3404fb5d4327SDasaratharaman Chandramouli default: 3405fb5d4327SDasaratharaman Chandramouli return 0; 3406fb5d4327SDasaratharaman Chandramouli } 3407fb5d4327SDasaratharaman Chandramouli } 340831e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 340931e07522SLen Brown { 341031e07522SLen Brown if (!genuine_intel) 341131e07522SLen Brown return 0; 341231e07522SLen Brown 341331e07522SLen Brown if (family != 6) 341431e07522SLen Brown return 0; 341531e07522SLen Brown 341631e07522SLen Brown switch (model) { 341731e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 341831e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 341931e07522SLen Brown return 1; 342031e07522SLen Brown default: 342131e07522SLen Brown return 0; 342231e07522SLen Brown } 342331e07522SLen Brown } 34246fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 34256fb3143bSLen Brown { 34266fb3143bSLen Brown if (!genuine_intel) 34276fb3143bSLen Brown return 0; 34286fb3143bSLen Brown 34296fb3143bSLen Brown if (family != 6) 34306fb3143bSLen Brown return 0; 34316fb3143bSLen Brown 34326fb3143bSLen Brown switch (model) { 3433869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3434869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3435869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3436cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3437869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3438869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3439869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3440869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3441869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3442997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3443869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 34446fb3143bSLen Brown 3445869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 34466fb3143bSLen Brown return 1; 34476fb3143bSLen Brown default: 34486fb3143bSLen Brown return 0; 34496fb3143bSLen Brown } 34506fb3143bSLen Brown } 34516fb3143bSLen Brown 3452fcd17211SLen Brown static void 34531b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3454fcd17211SLen Brown { 3455fcd17211SLen Brown if (!do_nhm_platform_info) 3456fcd17211SLen Brown return; 3457fcd17211SLen Brown 3458fcd17211SLen Brown dump_nhm_platform_info(); 3459fcd17211SLen Brown 3460fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3461fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3462fcd17211SLen Brown 3463fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3464fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3465fcd17211SLen Brown 346631e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 346731e07522SLen Brown dump_turbo_ratio_limits(family, model); 3468fcd17211SLen Brown 34690f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 34700f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 34710f7887c4SLen Brown 3472fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3473fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3474fb5d4327SDasaratharaman Chandramouli 34756fb3143bSLen Brown if (has_config_tdp(family, model)) 34766fb3143bSLen Brown dump_config_tdp(); 34776fb3143bSLen Brown 3478fcd17211SLen Brown dump_nhm_cst_cfg(); 3479fcd17211SLen Brown } 3480fcd17211SLen Brown 348141618e63SLen Brown static void 348241618e63SLen Brown dump_sysfs_cstate_config(void) 348341618e63SLen Brown { 348441618e63SLen Brown char path[64]; 348541618e63SLen Brown char name_buf[16]; 348641618e63SLen Brown char desc[64]; 348741618e63SLen Brown FILE *input; 348841618e63SLen Brown int state; 348941618e63SLen Brown char *sp; 349041618e63SLen Brown 349141618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 349241618e63SLen Brown return; 349341618e63SLen Brown 349441618e63SLen Brown for (state = 0; state < 10; ++state) { 349541618e63SLen Brown 349641618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 349741618e63SLen Brown base_cpu, state); 349841618e63SLen Brown input = fopen(path, "r"); 349941618e63SLen Brown if (input == NULL) 350041618e63SLen Brown continue; 35018173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 35028173c336SBen Hutchings err(1, "%s: failed to read file", path); 350341618e63SLen Brown 350441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 350541618e63SLen Brown sp = strchr(name_buf, '-'); 350641618e63SLen Brown if (!sp) 350741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 350841618e63SLen Brown *sp = '\0'; 350941618e63SLen Brown fclose(input); 351041618e63SLen Brown 351141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 351241618e63SLen Brown base_cpu, state); 351341618e63SLen Brown input = fopen(path, "r"); 351441618e63SLen Brown if (input == NULL) 351541618e63SLen Brown continue; 35168173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 35178173c336SBen Hutchings err(1, "%s: failed to read file", path); 351841618e63SLen Brown 351941618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 352041618e63SLen Brown fclose(input); 352141618e63SLen Brown } 352241618e63SLen Brown } 35237293fccdSLen Brown static void 35247293fccdSLen Brown dump_sysfs_pstate_config(void) 35257293fccdSLen Brown { 35267293fccdSLen Brown char path[64]; 35277293fccdSLen Brown char driver_buf[64]; 35287293fccdSLen Brown char governor_buf[64]; 35297293fccdSLen Brown FILE *input; 35307293fccdSLen Brown int turbo; 35317293fccdSLen Brown 35327293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 35337293fccdSLen Brown base_cpu); 35347293fccdSLen Brown input = fopen(path, "r"); 35357293fccdSLen Brown if (input == NULL) { 35360a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 35377293fccdSLen Brown return; 35387293fccdSLen Brown } 35398173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 35408173c336SBen Hutchings err(1, "%s: failed to read file", path); 35417293fccdSLen Brown fclose(input); 35427293fccdSLen Brown 35437293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 35447293fccdSLen Brown base_cpu); 35457293fccdSLen Brown input = fopen(path, "r"); 35467293fccdSLen Brown if (input == NULL) { 35470a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 35487293fccdSLen Brown return; 35497293fccdSLen Brown } 35508173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 35518173c336SBen Hutchings err(1, "%s: failed to read file", path); 35527293fccdSLen Brown fclose(input); 35537293fccdSLen Brown 35547293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 35557293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 35567293fccdSLen Brown 35577293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 35587293fccdSLen Brown input = fopen(path, "r"); 35597293fccdSLen Brown if (input != NULL) { 35608173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 35618173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 35627293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 35637293fccdSLen Brown fclose(input); 35647293fccdSLen Brown } 35657293fccdSLen Brown 35667293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 35677293fccdSLen Brown input = fopen(path, "r"); 35687293fccdSLen Brown if (input != NULL) { 35698173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 35708173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 35717293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 35727293fccdSLen Brown fclose(input); 35737293fccdSLen Brown } 35747293fccdSLen Brown } 357541618e63SLen Brown 35766574a5d5SLen Brown 3577889facbeSLen Brown /* 3578889facbeSLen Brown * print_epb() 3579889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3580889facbeSLen Brown */ 3581889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3582889facbeSLen Brown { 3583889facbeSLen Brown unsigned long long msr; 3584889facbeSLen Brown char *epb_string; 3585889facbeSLen Brown int cpu; 3586889facbeSLen Brown 3587889facbeSLen Brown if (!has_epb) 3588889facbeSLen Brown return 0; 3589889facbeSLen Brown 3590889facbeSLen Brown cpu = t->cpu_id; 3591889facbeSLen Brown 3592889facbeSLen Brown /* EPB is per-package */ 3593889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3594889facbeSLen Brown return 0; 3595889facbeSLen Brown 3596889facbeSLen Brown if (cpu_migrate(cpu)) { 3597b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3598889facbeSLen Brown return -1; 3599889facbeSLen Brown } 3600889facbeSLen Brown 3601889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 3602889facbeSLen Brown return 0; 3603889facbeSLen Brown 3604e9be7dd6SLen Brown switch (msr & 0xF) { 3605889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3606889facbeSLen Brown epb_string = "performance"; 3607889facbeSLen Brown break; 3608889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3609889facbeSLen Brown epb_string = "balanced"; 3610889facbeSLen Brown break; 3611889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3612889facbeSLen Brown epb_string = "powersave"; 3613889facbeSLen Brown break; 3614889facbeSLen Brown default: 3615889facbeSLen Brown epb_string = "custom"; 3616889facbeSLen Brown break; 3617889facbeSLen Brown } 3618b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 3619889facbeSLen Brown 3620889facbeSLen Brown return 0; 3621889facbeSLen Brown } 36227f5c258eSLen Brown /* 36237f5c258eSLen Brown * print_hwp() 36247f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 36257f5c258eSLen Brown */ 36267f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 36277f5c258eSLen Brown { 36287f5c258eSLen Brown unsigned long long msr; 36297f5c258eSLen Brown int cpu; 36307f5c258eSLen Brown 36317f5c258eSLen Brown if (!has_hwp) 36327f5c258eSLen Brown return 0; 36337f5c258eSLen Brown 36347f5c258eSLen Brown cpu = t->cpu_id; 36357f5c258eSLen Brown 36367f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 36377f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 36387f5c258eSLen Brown return 0; 36397f5c258eSLen Brown 36407f5c258eSLen Brown if (cpu_migrate(cpu)) { 3641b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 36427f5c258eSLen Brown return -1; 36437f5c258eSLen Brown } 36447f5c258eSLen Brown 36457f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 36467f5c258eSLen Brown return 0; 36477f5c258eSLen Brown 3648b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 36497f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 36507f5c258eSLen Brown 36517f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 36527f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 36537f5c258eSLen Brown return 0; 36547f5c258eSLen Brown 36557f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 36567f5c258eSLen Brown return 0; 36577f5c258eSLen Brown 3658b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 36596dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 36607f5c258eSLen Brown cpu, msr, 36617f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 36627f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 36637f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 36647f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 36657f5c258eSLen Brown 36667f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 36677f5c258eSLen Brown return 0; 36687f5c258eSLen Brown 3669b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 36706dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 36717f5c258eSLen Brown cpu, msr, 36727f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 36737f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 36747f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 36757f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 36767f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 36777f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 36787f5c258eSLen Brown 36797f5c258eSLen Brown if (has_hwp_pkg) { 36807f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 36817f5c258eSLen Brown return 0; 36827f5c258eSLen Brown 3683b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 36846dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 36857f5c258eSLen Brown cpu, msr, 36867f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 36877f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 36887f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 36897f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 36907f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 36917f5c258eSLen Brown } 36927f5c258eSLen Brown if (has_hwp_notify) { 36937f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 36947f5c258eSLen Brown return 0; 36957f5c258eSLen Brown 3696b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 36977f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 36987f5c258eSLen Brown cpu, msr, 36997f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 37007f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 37017f5c258eSLen Brown } 37027f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 37037f5c258eSLen Brown return 0; 37047f5c258eSLen Brown 3705b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 37067f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 37077f5c258eSLen Brown cpu, msr, 37087f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 37097f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 3710889facbeSLen Brown 3711889facbeSLen Brown return 0; 3712889facbeSLen Brown } 3713889facbeSLen Brown 37143a9a941dSLen Brown /* 37153a9a941dSLen Brown * print_perf_limit() 37163a9a941dSLen Brown */ 37173a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 37183a9a941dSLen Brown { 37193a9a941dSLen Brown unsigned long long msr; 37203a9a941dSLen Brown int cpu; 37213a9a941dSLen Brown 37223a9a941dSLen Brown cpu = t->cpu_id; 37233a9a941dSLen Brown 37243a9a941dSLen Brown /* per-package */ 37253a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 37263a9a941dSLen Brown return 0; 37273a9a941dSLen Brown 37283a9a941dSLen Brown if (cpu_migrate(cpu)) { 3729b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 37303a9a941dSLen Brown return -1; 37313a9a941dSLen Brown } 37323a9a941dSLen Brown 37333a9a941dSLen Brown if (do_core_perf_limit_reasons) { 37343a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 3735b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3736b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 3737e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 37383a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 3739e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 3740e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 3741e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 3742e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 3743e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 3744e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 3745e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 3746e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 3747e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 3748e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 3749e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 3750e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 3751b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 3752e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 37533a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 3754e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 3755e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 3756e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 3757e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 3758e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 3759e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 3760e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 3761e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 3762e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 3763e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 3764e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 3765e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 37663a9a941dSLen Brown 37673a9a941dSLen Brown } 37683a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 37693a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 3770b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3771b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 37723a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 37733a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 37743a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 37753a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 37763a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 37773a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 37783a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 37793a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3780b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 37813a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 37823a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 37833a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 37843a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 37853a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 37863a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 37873a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 37883a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 37893a9a941dSLen Brown } 37903a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 37913a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 3792b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3793b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 37943a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 37953a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 37963a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 37973a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 37983a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 37993a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3800b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 38013a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 38023a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 38033a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 38043a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 38053a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 38063a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 38073a9a941dSLen Brown } 38083a9a941dSLen Brown return 0; 38093a9a941dSLen Brown } 38103a9a941dSLen Brown 3811889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 3812889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 3813889facbeSLen Brown 38149392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 3815144b44b1SLen Brown { 3816144b44b1SLen Brown unsigned long long msr; 3817144b44b1SLen Brown 3818144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 38197ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 3820144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 3821144b44b1SLen Brown 3822144b44b1SLen Brown switch (model) { 3823f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3824f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: 3825144b44b1SLen Brown return 30.0; 3826144b44b1SLen Brown default: 3827144b44b1SLen Brown return 135.0; 3828144b44b1SLen Brown } 3829144b44b1SLen Brown } 3830144b44b1SLen Brown 38319392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 38329392bd98SCalvin Walton { 38339392bd98SCalvin Walton switch (family) { 38349392bd98SCalvin Walton case 0x17: 3835*c1c10cc7SPu Wen case 0x18: 38369392bd98SCalvin Walton default: 38379392bd98SCalvin Walton /* This is the max stock TDP of HEDT/Server Fam17h chips */ 38389392bd98SCalvin Walton return 250.0; 38399392bd98SCalvin Walton } 38409392bd98SCalvin Walton } 38419392bd98SCalvin Walton 384240ee8e3bSAndrey Semin /* 384340ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 384440ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 384540ee8e3bSAndrey Semin */ 384640ee8e3bSAndrey Semin static double 384740ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 384840ee8e3bSAndrey Semin { 384940ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 385040ee8e3bSAndrey Semin 385140ee8e3bSAndrey Semin switch (model) { 3852869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3853869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3854869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 385540ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 385640ee8e3bSAndrey Semin default: 385740ee8e3bSAndrey Semin return (rapl_energy_units); 385840ee8e3bSAndrey Semin } 385940ee8e3bSAndrey Semin } 386040ee8e3bSAndrey Semin 38619392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 3862889facbeSLen Brown { 3863889facbeSLen Brown unsigned long long msr; 3864144b44b1SLen Brown unsigned int time_unit; 3865889facbeSLen Brown double tdp; 3866889facbeSLen Brown 3867889facbeSLen Brown if (family != 6) 3868889facbeSLen Brown return; 3869889facbeSLen Brown 3870889facbeSLen Brown switch (model) { 3871869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3872869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 3873869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3874cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3875869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3876869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3877869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3878144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 3879812db3f7SLen Brown if (rapl_joules) { 3880812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3881812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3882812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 3883812db3f7SLen Brown } else { 3884812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3885812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3886812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 3887812db3f7SLen Brown } 3888889facbeSLen Brown break; 3889869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3890f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 3891e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 3892812db3f7SLen Brown if (rapl_joules) 3893812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3894812db3f7SLen Brown else 3895812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3896e4085d54SLen Brown break; 3897869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3898997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 389981824921SLen 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; 3900812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3901812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3902812db3f7SLen Brown if (rapl_joules) { 3903812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3904812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3905812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 390681824921SLen Brown BIC_PRESENT(BIC_GFX_J); 3907812db3f7SLen Brown } else { 3908812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3909812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3910812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 391181824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 3912812db3f7SLen Brown } 39130b2bb692SLen Brown break; 3914869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3915869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3916869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3917869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 39180b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 3919812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3920812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3921812db3f7SLen Brown if (rapl_joules) { 3922812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3923812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3924812db3f7SLen Brown } else { 3925812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3926812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3927812db3f7SLen Brown } 3928e6f9bb3cSLen Brown break; 3929869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3930869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 39310b2bb692SLen 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; 3932812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3933812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3934812db3f7SLen Brown if (rapl_joules) { 3935812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3936812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3937812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3938812db3f7SLen Brown } else { 3939812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3940812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3941812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3942812db3f7SLen Brown } 3943144b44b1SLen Brown break; 3944f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3945f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 3946144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 3947812db3f7SLen Brown if (rapl_joules) { 3948812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3949812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3950812db3f7SLen Brown } else { 3951812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3952812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3953812db3f7SLen Brown } 3954889facbeSLen Brown break; 3955f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 39560f644909SJacob 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; 3957812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3958812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3959812db3f7SLen Brown if (rapl_joules) { 3960812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3961812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3962812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3963812db3f7SLen Brown } else { 3964812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3965812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3966812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3967812db3f7SLen Brown } 39680f644909SJacob Pan break; 3969889facbeSLen Brown default: 3970889facbeSLen Brown return; 3971889facbeSLen Brown } 3972889facbeSLen Brown 3973889facbeSLen Brown /* units on package 0, verify later other packages match */ 39747ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 3975889facbeSLen Brown return; 3976889facbeSLen Brown 3977889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 3978f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 3979144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 3980144b44b1SLen Brown else 3981889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 3982889facbeSLen Brown 398340ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 398440ee8e3bSAndrey Semin 3985144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 3986144b44b1SLen Brown if (time_unit == 0) 3987144b44b1SLen Brown time_unit = 0xA; 3988889facbeSLen Brown 3989144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 3990144b44b1SLen Brown 39919392bd98SCalvin Walton tdp = get_tdp_intel(model); 3992889facbeSLen Brown 3993889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 399496e47158SLen Brown if (!quiet) 3995b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 39969392bd98SCalvin Walton } 3997889facbeSLen Brown 39989392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 39999392bd98SCalvin Walton { 40009392bd98SCalvin Walton unsigned long long msr; 40019392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 40029392bd98SCalvin Walton unsigned int has_rapl = 0; 40039392bd98SCalvin Walton double tdp; 40049392bd98SCalvin Walton 40059392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 40069392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 40079392bd98SCalvin Walton /* RAPL (Fam 17h) */ 40089392bd98SCalvin Walton has_rapl = edx & (1 << 14); 40099392bd98SCalvin Walton } 40109392bd98SCalvin Walton 40119392bd98SCalvin Walton if (!has_rapl) 4012889facbeSLen Brown return; 40139392bd98SCalvin Walton 40149392bd98SCalvin Walton switch (family) { 40159392bd98SCalvin Walton case 0x17: /* Zen, Zen+ */ 4016*c1c10cc7SPu Wen case 0x18: /* Hygon Dhyana */ 40179392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 40183316f99aSCalvin Walton if (rapl_joules) { 40193316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 40209392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 40213316f99aSCalvin Walton } else { 40223316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 40239392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 40243316f99aSCalvin Walton } 40259392bd98SCalvin Walton break; 40269392bd98SCalvin Walton default: 40279392bd98SCalvin Walton return; 40289392bd98SCalvin Walton } 40299392bd98SCalvin Walton 40309392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 40319392bd98SCalvin Walton return; 40329392bd98SCalvin Walton 40339392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 40349392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 40359392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 40369392bd98SCalvin Walton 40379cfa8e04SPu Wen tdp = get_tdp_amd(family); 40389392bd98SCalvin Walton 40399392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 40409392bd98SCalvin Walton if (!quiet) 40419392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 40429392bd98SCalvin Walton } 40439392bd98SCalvin Walton 40449392bd98SCalvin Walton /* 40459392bd98SCalvin Walton * rapl_probe() 40469392bd98SCalvin Walton * 40479392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 40489392bd98SCalvin Walton */ 40499392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 40509392bd98SCalvin Walton { 40519392bd98SCalvin Walton if (genuine_intel) 40529392bd98SCalvin Walton rapl_probe_intel(family, model); 4053*c1c10cc7SPu Wen if (authentic_amd || hygon_genuine) 40549392bd98SCalvin Walton rapl_probe_amd(family, model); 4055889facbeSLen Brown } 4056889facbeSLen Brown 40571b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 40583a9a941dSLen Brown { 40593a9a941dSLen Brown if (!genuine_intel) 40603a9a941dSLen Brown return; 40613a9a941dSLen Brown 40623a9a941dSLen Brown if (family != 6) 40633a9a941dSLen Brown return; 40643a9a941dSLen Brown 40653a9a941dSLen Brown switch (model) { 4066869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 4067cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4068869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 40693a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4070869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 40713a9a941dSLen Brown do_core_perf_limit_reasons = 1; 40723a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 40733a9a941dSLen Brown default: 40743a9a941dSLen Brown return; 40753a9a941dSLen Brown } 40763a9a941dSLen Brown } 40773a9a941dSLen Brown 4078ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4079ac980e13SArtem Bityutskiy { 4080ac980e13SArtem Bityutskiy if (is_skx(family, model) || is_bdx(family, model)) 4081ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4082ac980e13SArtem Bityutskiy } 4083ac980e13SArtem Bityutskiy 4084889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4085889facbeSLen Brown { 4086889facbeSLen Brown unsigned long long msr; 4087f4896fa5SLen Brown unsigned int dts, dts2; 4088889facbeSLen Brown int cpu; 4089889facbeSLen Brown 4090889facbeSLen Brown if (!(do_dts || do_ptm)) 4091889facbeSLen Brown return 0; 4092889facbeSLen Brown 4093889facbeSLen Brown cpu = t->cpu_id; 4094889facbeSLen Brown 4095889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4096889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4097889facbeSLen Brown return 0; 4098889facbeSLen Brown 4099889facbeSLen Brown if (cpu_migrate(cpu)) { 4100b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4101889facbeSLen Brown return -1; 4102889facbeSLen Brown } 4103889facbeSLen Brown 4104889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4105889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4106889facbeSLen Brown return 0; 4107889facbeSLen Brown 4108889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4109b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 4110889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 4111889facbeSLen Brown 4112889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4113889facbeSLen Brown return 0; 4114889facbeSLen Brown 4115889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4116889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4117b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4118889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4119889facbeSLen Brown } 4120889facbeSLen Brown 4121889facbeSLen Brown 4122f4896fa5SLen Brown if (do_dts && debug) { 4123889facbeSLen Brown unsigned int resolution; 4124889facbeSLen Brown 4125889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4126889facbeSLen Brown return 0; 4127889facbeSLen Brown 4128889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4129889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4130b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 4131889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 4132889facbeSLen Brown 4133889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4134889facbeSLen Brown return 0; 4135889facbeSLen Brown 4136889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4137889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4138b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4139889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4140889facbeSLen Brown } 4141889facbeSLen Brown 4142889facbeSLen Brown return 0; 4143889facbeSLen Brown } 4144889facbeSLen Brown 4145889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4146889facbeSLen Brown { 4147b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4148889facbeSLen Brown cpu, label, 4149889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4150889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4151889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4152889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4153889facbeSLen Brown 4154889facbeSLen Brown return; 4155889facbeSLen Brown } 4156889facbeSLen Brown 4157889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4158889facbeSLen Brown { 4159889facbeSLen Brown unsigned long long msr; 41609392bd98SCalvin Walton const char *msr_name; 4161889facbeSLen Brown int cpu; 4162889facbeSLen Brown 4163889facbeSLen Brown if (!do_rapl) 4164889facbeSLen Brown return 0; 4165889facbeSLen Brown 4166889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4167889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4168889facbeSLen Brown return 0; 4169889facbeSLen Brown 4170889facbeSLen Brown cpu = t->cpu_id; 4171889facbeSLen Brown if (cpu_migrate(cpu)) { 4172b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4173889facbeSLen Brown return -1; 4174889facbeSLen Brown } 4175889facbeSLen Brown 41769392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 41779392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 41789392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 41799392bd98SCalvin Walton return -1; 41809392bd98SCalvin Walton } else { 41819392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4182889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4183889facbeSLen Brown return -1; 41849392bd98SCalvin Walton } 4185889facbeSLen Brown 41869392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4187144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 418896e47158SLen Brown 4189144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4190144b44b1SLen Brown 4191889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4192889facbeSLen Brown return -5; 4193889facbeSLen Brown 4194889facbeSLen Brown 4195b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4196889facbeSLen Brown cpu, msr, 4197889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4198889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4199889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4200889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4201889facbeSLen Brown 4202144b44b1SLen Brown } 4203144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4204144b44b1SLen Brown 4205889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4206889facbeSLen Brown return -9; 4207889facbeSLen Brown 4208b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4209889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4210889facbeSLen Brown 4211889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4212b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4213889facbeSLen Brown cpu, 4214889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4215889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4216889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4217889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4218889facbeSLen Brown } 4219889facbeSLen Brown 42200b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4221889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4222889facbeSLen Brown return -6; 4223889facbeSLen Brown 4224b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4225889facbeSLen Brown cpu, msr, 4226889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4227889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4228889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4229889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 42300b2bb692SLen Brown } 42310b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4232889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4233889facbeSLen Brown return -9; 4234b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4235889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4236889facbeSLen Brown 4237889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4238889facbeSLen Brown } 4239144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4240889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4241889facbeSLen Brown return -7; 4242889facbeSLen Brown 4243b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4244144b44b1SLen Brown } 42459148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4246889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4247889facbeSLen Brown return -9; 4248b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4249889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4250889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4251889facbeSLen Brown } 4252889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4253889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4254889facbeSLen Brown return -8; 4255889facbeSLen Brown 4256b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4257889facbeSLen Brown 4258889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4259889facbeSLen Brown return -9; 4260b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4261889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4262889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4263889facbeSLen Brown } 4264889facbeSLen Brown return 0; 4265889facbeSLen Brown } 4266889facbeSLen Brown 4267d7899447SLen Brown /* 4268d7899447SLen Brown * SNB adds support for additional MSRs: 4269d7899447SLen Brown * 4270d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4271d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4272d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4273d7899447SLen Brown */ 4274103a8feaSLen Brown 4275d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4276103a8feaSLen Brown { 4277103a8feaSLen Brown if (!genuine_intel) 4278103a8feaSLen Brown return 0; 4279103a8feaSLen Brown 4280103a8feaSLen Brown switch (model) { 4281869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4282869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4283869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4284869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4285869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 4286869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 4287cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4288869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 4289869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4290869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 4291869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4292869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4293997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4294869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4295869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4296f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4297f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 4298103a8feaSLen Brown return 1; 4299103a8feaSLen Brown } 4300103a8feaSLen Brown return 0; 4301103a8feaSLen Brown } 4302103a8feaSLen Brown 4303d7899447SLen Brown /* 4304570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4305d7899447SLen Brown * 4306d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4307d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4308d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 43095a63426eSLen Brown * 43105a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 43115a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 43125a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 43135a63426eSLen Brown * 4314d7899447SLen Brown */ 4315570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4316ca58710fSKristen Carlson Accardi { 4317ca58710fSKristen Carlson Accardi if (!genuine_intel) 4318ca58710fSKristen Carlson Accardi return 0; 4319ca58710fSKristen Carlson Accardi 4320ca58710fSKristen Carlson Accardi switch (model) { 4321cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4322869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4323869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4324997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4325869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4326f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4327ca58710fSKristen Carlson Accardi return 1; 4328ca58710fSKristen Carlson Accardi } 4329ca58710fSKristen Carlson Accardi return 0; 4330ca58710fSKristen Carlson Accardi } 4331ca58710fSKristen Carlson Accardi 43320b2bb692SLen Brown /* 43330b2bb692SLen Brown * SKL adds support for additional MSRS: 43340b2bb692SLen Brown * 43350b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 43360b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 43370b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 43380b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 43390b2bb692SLen Brown */ 43400b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 43410b2bb692SLen Brown { 43420b2bb692SLen Brown if (!genuine_intel) 43430b2bb692SLen Brown return 0; 43440b2bb692SLen Brown 43450b2bb692SLen Brown switch (model) { 4346869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4347997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 43480b2bb692SLen Brown return 1; 43490b2bb692SLen Brown } 43500b2bb692SLen Brown return 0; 43510b2bb692SLen Brown } 43520b2bb692SLen Brown 4353144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4354144b44b1SLen Brown { 4355144b44b1SLen Brown if (!genuine_intel) 4356144b44b1SLen Brown return 0; 4357144b44b1SLen Brown switch (model) { 4358f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 4359f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 4360144b44b1SLen Brown return 1; 4361144b44b1SLen Brown } 4362144b44b1SLen Brown return 0; 4363144b44b1SLen Brown } 4364144b44b1SLen Brown 4365fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4366fb5d4327SDasaratharaman Chandramouli { 4367fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4368fb5d4327SDasaratharaman Chandramouli return 0; 4369fb5d4327SDasaratharaman Chandramouli switch (model) { 4370869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4371fb5d4327SDasaratharaman Chandramouli return 1; 4372fb5d4327SDasaratharaman Chandramouli } 4373fb5d4327SDasaratharaman Chandramouli return 0; 4374fb5d4327SDasaratharaman Chandramouli } 4375fb5d4327SDasaratharaman Chandramouli 4376997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4377997e5395SSrinivas Pandruvada { 4378997e5395SSrinivas Pandruvada if (!genuine_intel) 4379997e5395SSrinivas Pandruvada return 0; 4380997e5395SSrinivas Pandruvada 4381997e5395SSrinivas Pandruvada switch (model) { 4382997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4383997e5395SSrinivas Pandruvada return 1; 4384997e5395SSrinivas Pandruvada } 4385997e5395SSrinivas Pandruvada 4386997e5395SSrinivas Pandruvada return 0; 4387997e5395SSrinivas Pandruvada } 4388997e5395SSrinivas Pandruvada 4389b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4390b2b34dfeSHubert Chrzaniuk { 4391b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4392b2b34dfeSHubert Chrzaniuk return 1024; 4393b2b34dfeSHubert Chrzaniuk return 1; 4394b2b34dfeSHubert Chrzaniuk } 4395b2b34dfeSHubert Chrzaniuk 4396144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4397144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4398144b44b1SLen Brown 4399144b44b1SLen Brown double slm_bclk(void) 4400144b44b1SLen Brown { 4401144b44b1SLen Brown unsigned long long msr = 3; 4402144b44b1SLen Brown unsigned int i; 4403144b44b1SLen Brown double freq; 4404144b44b1SLen Brown 44057ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4406b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4407144b44b1SLen Brown 4408144b44b1SLen Brown i = msr & 0xf; 4409144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4410b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 44110a91e551SColin Ian King i = 3; 4412144b44b1SLen Brown } 4413144b44b1SLen Brown freq = slm_freq_table[i]; 4414144b44b1SLen Brown 441596e47158SLen Brown if (!quiet) 4416b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4417144b44b1SLen Brown 4418144b44b1SLen Brown return freq; 4419144b44b1SLen Brown } 4420144b44b1SLen Brown 4421103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4422103a8feaSLen Brown { 4423121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4424103a8feaSLen Brown return 100.00; 4425144b44b1SLen Brown else if (is_slm(family, model)) 4426144b44b1SLen Brown return slm_bclk(); 4427103a8feaSLen Brown else 4428103a8feaSLen Brown return 133.33; 4429103a8feaSLen Brown } 4430103a8feaSLen Brown 4431889facbeSLen Brown /* 4432889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4433889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4434889facbeSLen Brown * This is usually equal to tjMax. 4435889facbeSLen Brown * 4436889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4437889facbeSLen Brown * but also allow cmdline over-ride with -T. 4438889facbeSLen Brown * 4439889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4440889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4441889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4442889facbeSLen Brown */ 4443889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4444889facbeSLen Brown { 4445889facbeSLen Brown unsigned long long msr; 4446889facbeSLen Brown unsigned int target_c_local; 4447889facbeSLen Brown int cpu; 4448889facbeSLen Brown 4449889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4450889facbeSLen Brown if (!(do_dts || do_ptm)) 4451889facbeSLen Brown return 0; 4452889facbeSLen Brown 4453889facbeSLen Brown /* this is a per-package concept */ 4454889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4455889facbeSLen Brown return 0; 4456889facbeSLen Brown 4457889facbeSLen Brown cpu = t->cpu_id; 4458889facbeSLen Brown if (cpu_migrate(cpu)) { 4459b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4460889facbeSLen Brown return -1; 4461889facbeSLen Brown } 4462889facbeSLen Brown 4463889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4464889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 4465b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 4466889facbeSLen Brown cpu, tcc_activation_temp); 4467889facbeSLen Brown return 0; 4468889facbeSLen Brown } 4469889facbeSLen Brown 4470889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4471d7899447SLen Brown if (!do_nhm_platform_info) 4472889facbeSLen Brown goto guess; 4473889facbeSLen Brown 44747ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4475889facbeSLen Brown goto guess; 4476889facbeSLen Brown 44773482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 4478889facbeSLen Brown 447996e47158SLen Brown if (!quiet) 4480b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 4481889facbeSLen Brown cpu, msr, target_c_local); 4482889facbeSLen Brown 44833482124aSJean Delvare if (!target_c_local) 4484889facbeSLen Brown goto guess; 4485889facbeSLen Brown 4486889facbeSLen Brown tcc_activation_temp = target_c_local; 4487889facbeSLen Brown 4488889facbeSLen Brown return 0; 4489889facbeSLen Brown 4490889facbeSLen Brown guess: 4491889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 4492b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 4493889facbeSLen Brown cpu, tcc_activation_temp); 4494889facbeSLen Brown 4495889facbeSLen Brown return 0; 4496889facbeSLen Brown } 449769807a63SLen Brown 4498aa8d8cc7SLen Brown void decode_feature_control_msr(void) 4499aa8d8cc7SLen Brown { 4500aa8d8cc7SLen Brown unsigned long long msr; 4501aa8d8cc7SLen Brown 4502aa8d8cc7SLen Brown if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr)) 4503aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 4504aa8d8cc7SLen Brown base_cpu, msr, 4505aa8d8cc7SLen Brown msr & FEATURE_CONTROL_LOCKED ? "" : "UN-", 4506aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 4507aa8d8cc7SLen Brown } 4508aa8d8cc7SLen Brown 450969807a63SLen Brown void decode_misc_enable_msr(void) 451069807a63SLen Brown { 451169807a63SLen Brown unsigned long long msr; 451269807a63SLen Brown 4513f26b1519SLen Brown if (!genuine_intel) 4514f26b1519SLen Brown return; 4515f26b1519SLen Brown 451669807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 4517e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 451869807a63SLen Brown base_cpu, msr, 4519e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 4520e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 4521fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 4522e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 4523e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 452469807a63SLen Brown } 452569807a63SLen Brown 452633148d67SLen Brown void decode_misc_feature_control(void) 452733148d67SLen Brown { 452833148d67SLen Brown unsigned long long msr; 452933148d67SLen Brown 453033148d67SLen Brown if (!has_misc_feature_control) 453133148d67SLen Brown return; 453233148d67SLen Brown 453333148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 453433148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 453533148d67SLen Brown base_cpu, msr, 453633148d67SLen Brown msr & (0 << 0) ? "No-" : "", 453733148d67SLen Brown msr & (1 << 0) ? "No-" : "", 453833148d67SLen Brown msr & (2 << 0) ? "No-" : "", 453933148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 454033148d67SLen Brown } 4541f0057310SLen Brown /* 4542f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 4543f0057310SLen Brown * 4544f0057310SLen Brown * Decode the bits according to the Nehalem documentation 4545f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 4546f0057310SLen Brown * bit[1] less so... 4547f0057310SLen Brown */ 4548f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 4549f0057310SLen Brown { 4550f0057310SLen Brown unsigned long long msr; 4551f0057310SLen Brown 4552f0057310SLen Brown if (!do_nhm_platform_info) 4553f0057310SLen Brown return; 4554f0057310SLen Brown 4555cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 4556cf4cbe53SLen Brown return; 4557cf4cbe53SLen Brown 4558f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 4559ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 4560f0057310SLen Brown base_cpu, msr, 4561f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 4562ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 4563ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 4564f0057310SLen Brown } 456571616c8eSLen Brown /* 456671616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 456771616c8eSLen Brown * 456871616c8eSLen Brown * This MSRs are present on Silvermont processors, 456971616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 457071616c8eSLen Brown */ 457171616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 457271616c8eSLen Brown { 457371616c8eSLen Brown unsigned long long msr; 457471616c8eSLen Brown 457571616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 457671616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 457771616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 457871616c8eSLen Brown 457971616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 458071616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 458171616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 458271616c8eSLen Brown } 45837f5c258eSLen Brown 4584f5a4c76aSLen Brown /* 4585f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 4586f5a4c76aSLen Brown */ 4587f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 4588f5a4c76aSLen Brown { 4589f5a4c76aSLen Brown 4590f5a4c76aSLen Brown switch(model) { 4591f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 4592f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 4593f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 4594f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 4595f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 4596f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 4597f5a4c76aSLen Brown 4598f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 4599f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 4600f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 4601f5a4c76aSLen Brown 4602f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 4603f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 4604f5a4c76aSLen Brown 4605f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 4606f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 4607f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 4608f5a4c76aSLen Brown 4609f5a4c76aSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: 4610f5a4c76aSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: 4611f5a4c76aSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: 4612f5a4c76aSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: 4613f5a4c76aSLen Brown return INTEL_FAM6_SKYLAKE_MOBILE; 4614937807d3SLen Brown 4615937807d3SLen Brown case INTEL_FAM6_ICELAKE_MOBILE: 4616d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 4617937807d3SLen Brown return INTEL_FAM6_CANNONLAKE_MOBILE; 4618b62b3184SZhang Rui 4619b62b3184SZhang Rui case INTEL_FAM6_ATOM_TREMONT_X: 4620b62b3184SZhang Rui return INTEL_FAM6_ATOM_GOLDMONT_X; 4621f5a4c76aSLen Brown } 4622f5a4c76aSLen Brown return model; 4623f5a4c76aSLen Brown } 4624fcd17211SLen Brown void process_cpuid() 4625103a8feaSLen Brown { 462634041551SLen Brown unsigned int eax, ebx, ecx, edx; 462734041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 4628b3a34e93SLen Brown unsigned int has_turbo; 4629103a8feaSLen Brown 4630103a8feaSLen Brown eax = ebx = ecx = edx = 0; 4631103a8feaSLen Brown 46325aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 4633103a8feaSLen Brown 463434041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 4635103a8feaSLen Brown genuine_intel = 1; 463634041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 463734041551SLen Brown authentic_amd = 1; 4638*c1c10cc7SPu Wen else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e) 4639*c1c10cc7SPu Wen hygon_genuine = 1; 4640103a8feaSLen Brown 464196e47158SLen Brown if (!quiet) 4642b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 4643103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 4644103a8feaSLen Brown 46455aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 4646103a8feaSLen Brown family = (fms >> 8) & 0xf; 4647103a8feaSLen Brown model = (fms >> 4) & 0xf; 4648103a8feaSLen Brown stepping = fms & 0xf; 46495aa3d1a2SCalvin Walton if (family == 0xf) 46505aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 46515aa3d1a2SCalvin Walton if (family >= 6) 4652103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 465334041551SLen Brown ecx_flags = ecx; 465434041551SLen Brown edx_flags = edx; 4655103a8feaSLen Brown 4656103a8feaSLen Brown /* 4657103a8feaSLen Brown * check max extended function levels of CPUID. 4658103a8feaSLen Brown * This is needed to check for invariant TSC. 4659103a8feaSLen Brown * This check is valid for both Intel and AMD. 4660103a8feaSLen Brown */ 4661103a8feaSLen Brown ebx = ecx = edx = 0; 46625aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 4663103a8feaSLen Brown 466434041551SLen Brown if (!quiet) { 466534041551SLen Brown fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 466634041551SLen Brown max_level, max_extended_level, family, model, stepping, family, model, stepping); 466734041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 466834041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 466934041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 467034041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 467134041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 467234041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 467334041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 467434041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 467534041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 467634041551SLen Brown edx_flags & (1 << 28) ? "HT" : "-", 467734041551SLen Brown edx_flags & (1 << 29) ? "TM" : "-"); 467834041551SLen Brown } 4679f5a4c76aSLen Brown if (genuine_intel) 4680f5a4c76aSLen Brown model = intel_model_duplicates(model); 468134041551SLen Brown 468234041551SLen Brown if (!(edx_flags & (1 << 5))) 468334041551SLen Brown errx(1, "CPUID: no MSR"); 468434041551SLen Brown 468561a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 4686103a8feaSLen Brown 4687103a8feaSLen Brown /* 4688103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 4689103a8feaSLen Brown * this check is valid for both Intel and AMD 4690103a8feaSLen Brown */ 46915aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 46928209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 4693d7899447SLen Brown } 4694103a8feaSLen Brown 4695103a8feaSLen Brown /* 4696103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 4697103a8feaSLen Brown * this check is valid for both Intel and AMD 4698103a8feaSLen Brown */ 4699103a8feaSLen Brown 47005aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 47018209e054SThomas Renninger has_aperf = ecx & (1 << 0); 4702812db3f7SLen Brown if (has_aperf) { 4703812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 4704812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 4705812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 4706812db3f7SLen Brown } 4707889facbeSLen Brown do_dts = eax & (1 << 0); 4708812db3f7SLen Brown if (do_dts) 4709812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 4710b3a34e93SLen Brown has_turbo = eax & (1 << 1); 4711889facbeSLen Brown do_ptm = eax & (1 << 6); 4712812db3f7SLen Brown if (do_ptm) 4713812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 47147f5c258eSLen Brown has_hwp = eax & (1 << 7); 47157f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 47167f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 47177f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 47187f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 4719889facbeSLen Brown has_epb = ecx & (1 << 3); 4720889facbeSLen Brown 472196e47158SLen Brown if (!quiet) 4722b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 47237f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 47247f5c258eSLen Brown has_aperf ? "" : "No-", 4725b3a34e93SLen Brown has_turbo ? "" : "No-", 47267f5c258eSLen Brown do_dts ? "" : "No-", 47277f5c258eSLen Brown do_ptm ? "" : "No-", 47287f5c258eSLen Brown has_hwp ? "" : "No-", 47297f5c258eSLen Brown has_hwp_notify ? "" : "No-", 47307f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 47317f5c258eSLen Brown has_hwp_epp ? "" : "No-", 47327f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 47337f5c258eSLen Brown has_epb ? "" : "No-"); 4734103a8feaSLen Brown 473596e47158SLen Brown if (!quiet) 473669807a63SLen Brown decode_misc_enable_msr(); 473769807a63SLen Brown 473833148d67SLen Brown 473996e47158SLen Brown if (max_level >= 0x7 && !quiet) { 4740aa8d8cc7SLen Brown int has_sgx; 4741aa8d8cc7SLen Brown 4742aa8d8cc7SLen Brown ecx = 0; 4743aa8d8cc7SLen Brown 4744aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 4745aa8d8cc7SLen Brown 4746aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 4747aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 4748aa8d8cc7SLen Brown 4749aa8d8cc7SLen Brown if (has_sgx) 4750aa8d8cc7SLen Brown decode_feature_control_msr(); 4751aa8d8cc7SLen Brown } 4752aa8d8cc7SLen Brown 475361a87ba7SLen Brown if (max_level >= 0x15) { 47548a5bdf41SLen Brown unsigned int eax_crystal; 47558a5bdf41SLen Brown unsigned int ebx_tsc; 47568a5bdf41SLen Brown 47578a5bdf41SLen Brown /* 47588a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 47598a5bdf41SLen Brown */ 47608a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 47615aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 47628a5bdf41SLen Brown 47638a5bdf41SLen Brown if (ebx_tsc != 0) { 47648a5bdf41SLen Brown 476596e47158SLen Brown if (!quiet && (ebx != 0)) 4766b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 47678a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 47688a5bdf41SLen Brown 47698a5bdf41SLen Brown if (crystal_hz == 0) 47708a5bdf41SLen Brown switch(model) { 4771869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4772e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 4773e8efbc80SLen Brown break; 4774f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 4775ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 4776ec53e594SLen Brown break; 4777869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4778f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4779e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 47808a5bdf41SLen Brown break; 47818a5bdf41SLen Brown default: 47828a5bdf41SLen Brown crystal_hz = 0; 47838a5bdf41SLen Brown } 47848a5bdf41SLen Brown 47858a5bdf41SLen Brown if (crystal_hz) { 47868a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 478796e47158SLen Brown if (!quiet) 4788b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 47898a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 47908a5bdf41SLen Brown } 47918a5bdf41SLen Brown } 47928a5bdf41SLen Brown } 479361a87ba7SLen Brown if (max_level >= 0x16) { 479461a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 479561a87ba7SLen Brown 479661a87ba7SLen Brown /* 479761a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 479861a87ba7SLen Brown */ 479961a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 480061a87ba7SLen Brown 48015aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 480296e47158SLen Brown if (!quiet) 4803b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 480461a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 480561a87ba7SLen Brown } 48068a5bdf41SLen Brown 4807b2b34dfeSHubert Chrzaniuk if (has_aperf) 4808b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 4809b2b34dfeSHubert Chrzaniuk 4810812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 4811812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 4812812db3f7SLen Brown 4813812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 4814812db3f7SLen Brown do_nhm_platform_info = 1; 4815812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 4816812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 4817812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 4818812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 4819812db3f7SLen Brown } 4820d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 4821812db3f7SLen Brown 4822812db3f7SLen Brown if (do_snb_cstates) 4823812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 4824812db3f7SLen Brown 48255a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 48260f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 48270f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 48280f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 48290f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 48300f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 48310f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 48320f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 48330f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 48340539ba11SLen Brown if (has_slv_msrs(family, model)) { 48350f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 48360f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 48370f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 48380f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 48390539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 48400539ba11SLen Brown use_c1_residency_msr = 1; 48410539ba11SLen Brown } 48427170a374SLen Brown if (is_dnv(family, model)) { 48437170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 48447170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 48457170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 48467170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 48477170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 48487170a374SLen Brown use_c1_residency_msr = 1; 48497170a374SLen Brown } 485034c76197SLen Brown if (is_skx(family, model)) { 485134c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 485234c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 485334c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 485434c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 485534c76197SLen Brown } 4856ade0ebacSLen Brown if (is_bdx(family, model)) { 4857ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 4858ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 4859ade0ebacSLen Brown } 4860570992fcSLen Brown if (has_c8910_msrs(family, model)) { 48610f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 48620f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 48630f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 48640f47c08dSLen Brown } 4865570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 4866a99d8730SLen Brown if (has_skl_msrs(family, model)) { 4867a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 4868a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 4869a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 4870a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 4871a99d8730SLen Brown } 4872144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 4873fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 4874103a8feaSLen Brown 487531a1f15cSLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model)) 4876562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 4877103a8feaSLen Brown 487896e47158SLen Brown if (!quiet) 4879f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 4880f0057310SLen Brown 488196e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 488271616c8eSLen Brown decode_c6_demotion_policy_msr(); 488371616c8eSLen Brown 4884889facbeSLen Brown rapl_probe(family, model); 48853a9a941dSLen Brown perf_limit_reasons_probe(family, model); 4886ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 4887889facbeSLen Brown 488896e47158SLen Brown if (!quiet) 48891b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 4890fcd17211SLen Brown 489141618e63SLen Brown if (!quiet) 489241618e63SLen Brown dump_sysfs_cstate_config(); 48937293fccdSLen Brown if (!quiet) 48947293fccdSLen Brown dump_sysfs_pstate_config(); 489541618e63SLen Brown 4896a2b7b749SLen Brown if (has_skl_msrs(family, model)) 4897a2b7b749SLen Brown calculate_tsc_tweak(); 4898a2b7b749SLen Brown 4899812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 4900812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 4901fdf676e5SLen Brown 4902812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 4903812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 490427d47356SLen Brown 4905be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 4906be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 4907be0e54c4SLen Brown else 4908be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 4909be0e54c4SLen Brown 4910be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK)) 4911be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 4912be0e54c4SLen Brown else 4913be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 4914be0e54c4SLen Brown 491596e47158SLen Brown if (!quiet) 491633148d67SLen Brown decode_misc_feature_control(); 491733148d67SLen Brown 4918889facbeSLen Brown return; 4919103a8feaSLen Brown } 4920103a8feaSLen Brown 4921103a8feaSLen Brown /* 4922103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 4923103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 4924103a8feaSLen Brown */ 4925103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 4926103a8feaSLen Brown { 4927103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 4928103a8feaSLen Brown return 1; 4929103a8feaSLen Brown else 4930103a8feaSLen Brown return 0; 4931103a8feaSLen Brown } 4932103a8feaSLen Brown 4933103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 4934103a8feaSLen Brown { 4935103a8feaSLen Brown return 0; 4936103a8feaSLen Brown } 4937103a8feaSLen Brown 4938c98d5d94SLen Brown void topology_probe() 4939c98d5d94SLen Brown { 4940c98d5d94SLen Brown int i; 4941c98d5d94SLen Brown int max_core_id = 0; 4942c98d5d94SLen Brown int max_package_id = 0; 49436de68fe1SLen Brown int max_die_id = 0; 4944c98d5d94SLen Brown int max_siblings = 0; 4945c98d5d94SLen Brown 4946c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 4947843c5791SPrarit Bhargava set_max_cpu_num(); 4948c98d5d94SLen Brown topo.num_cpus = 0; 4949c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 4950c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 4951812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 4952c98d5d94SLen Brown 4953d8af6f5fSLen Brown if (debug > 1) 4954b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 4955c98d5d94SLen Brown 4956c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 4957b2c95d90SJosh Triplett if (cpus == NULL) 4958b2c95d90SJosh Triplett err(1, "calloc cpus"); 4959c98d5d94SLen Brown 4960c98d5d94SLen Brown /* 4961c98d5d94SLen Brown * Allocate and initialize cpu_present_set 4962c98d5d94SLen Brown */ 4963c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4964b2c95d90SJosh Triplett if (cpu_present_set == NULL) 4965b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4966c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4967c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 4968c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 4969c98d5d94SLen Brown 4970c98d5d94SLen Brown /* 49711ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 49721ef7d21aSLen Brown */ 49731ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 49741ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 49751ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 49761ef7d21aSLen Brown err(1, "cpu%d not present", i); 49771ef7d21aSLen Brown } 49781ef7d21aSLen Brown 49791ef7d21aSLen Brown /* 4980c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 4981c98d5d94SLen Brown */ 4982c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4983b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 4984b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4985c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4986c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 4987c98d5d94SLen Brown 49888cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 4989c98d5d94SLen Brown 4990c98d5d94SLen Brown /* 4991c98d5d94SLen Brown * For online cpus 4992c98d5d94SLen Brown * find max_core_id, max_package_id 4993c98d5d94SLen Brown */ 4994c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 4995c98d5d94SLen Brown int siblings; 4996c98d5d94SLen Brown 4997c98d5d94SLen Brown if (cpu_is_not_present(i)) { 4998d8af6f5fSLen Brown if (debug > 1) 4999b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 5000c98d5d94SLen Brown continue; 5001c98d5d94SLen Brown } 5002c98d5d94SLen Brown 50030e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 50040e2d8f05SLen Brown 50050e2d8f05SLen Brown /* get package information */ 5006c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5007c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5008c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5009c98d5d94SLen Brown 50106de68fe1SLen Brown /* get die information */ 50116de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 50126de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 50136de68fe1SLen Brown max_die_id = cpus[i].die_id; 50146de68fe1SLen Brown 50150e2d8f05SLen Brown /* get numa node information */ 5016ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5017ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5018ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 50190e2d8f05SLen Brown 50200e2d8f05SLen Brown /* get core information */ 50210e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 50220e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 50230e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 50240e2d8f05SLen Brown 50250e2d8f05SLen Brown /* get thread information */ 50260e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5027c98d5d94SLen Brown if (siblings > max_siblings) 5028c98d5d94SLen Brown max_siblings = siblings; 50294f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 50308cb48b32SPrarit Bhargava topo.num_cores++; 5031c98d5d94SLen Brown } 5032ef605741SPrarit Bhargava 503370a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5034d8af6f5fSLen Brown if (debug > 1) 5035b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 503670a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 503770a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5038812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5039c98d5d94SLen Brown 50406de68fe1SLen Brown topo.num_die = max_die_id + 1; 50416de68fe1SLen Brown if (debug > 1) 50426de68fe1SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", 50436de68fe1SLen Brown max_die_id, topo.num_die); 50446de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 50456de68fe1SLen Brown BIC_PRESENT(BIC_Die); 50466de68fe1SLen Brown 5047c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5048d8af6f5fSLen Brown if (debug > 1) 5049b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 5050c98d5d94SLen Brown max_package_id, topo.num_packages); 50517da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5052812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5053c98d5d94SLen Brown 5054ef605741SPrarit Bhargava set_node_data(); 5055ef605741SPrarit Bhargava if (debug > 1) 505670a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 505701235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 505801235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5059ef605741SPrarit Bhargava 506070a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5061d8af6f5fSLen Brown if (debug > 1) 5062b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 50632ffbb224SPrarit Bhargava 50642ffbb224SPrarit Bhargava if (debug < 1) 50652ffbb224SPrarit Bhargava return; 50662ffbb224SPrarit Bhargava 50672ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 50680ec712e3SLen Brown if (cpu_is_not_present(i)) 50690ec712e3SLen Brown continue; 50702ffbb224SPrarit Bhargava fprintf(outf, 50716de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 50726de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 50732ffbb224SPrarit Bhargava cpus[i].physical_node_id, 50742ffbb224SPrarit Bhargava cpus[i].logical_node_id, 50752ffbb224SPrarit Bhargava cpus[i].physical_core_id, 50762ffbb224SPrarit Bhargava cpus[i].thread_id); 50772ffbb224SPrarit Bhargava } 50782ffbb224SPrarit Bhargava 5079c98d5d94SLen Brown } 5080c98d5d94SLen Brown 5081c98d5d94SLen Brown void 508240f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c, 508340f5cfe7SPrarit Bhargava struct pkg_data **p) 5084c98d5d94SLen Brown { 5085c98d5d94SLen Brown int i; 508640f5cfe7SPrarit Bhargava int num_cores = topo.cores_per_node * topo.nodes_per_pkg * 508740f5cfe7SPrarit Bhargava topo.num_packages; 508840f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5089c98d5d94SLen Brown 509040f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5091c98d5d94SLen Brown if (*t == NULL) 5092c98d5d94SLen Brown goto error; 5093c98d5d94SLen Brown 509440f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5095c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5096c98d5d94SLen Brown 509740f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5098c98d5d94SLen Brown if (*c == NULL) 5099c98d5d94SLen Brown goto error; 5100c98d5d94SLen Brown 510140f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5102c98d5d94SLen Brown (*c)[i].core_id = -1; 5103c98d5d94SLen Brown 5104678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5105c98d5d94SLen Brown if (*p == NULL) 5106c98d5d94SLen Brown goto error; 5107c98d5d94SLen Brown 5108c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5109c98d5d94SLen Brown (*p)[i].package_id = i; 5110c98d5d94SLen Brown 5111c98d5d94SLen Brown return; 5112c98d5d94SLen Brown error: 5113b2c95d90SJosh Triplett err(1, "calloc counters"); 5114c98d5d94SLen Brown } 5115c98d5d94SLen Brown /* 5116c98d5d94SLen Brown * init_counter() 5117c98d5d94SLen Brown * 5118c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5119c98d5d94SLen Brown */ 5120c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 51218cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 5122c98d5d94SLen Brown { 51238cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 512440f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 51258cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 51268cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5127c98d5d94SLen Brown struct thread_data *t; 5128c98d5d94SLen Brown struct core_data *c; 5129c98d5d94SLen Brown struct pkg_data *p; 5130c98d5d94SLen Brown 513142dd4520SNathan Ciobanu 513242dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 513342dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 513442dd4520SNathan Ciobanu */ 513542dd4520SNathan Ciobanu if (node_id < 0) 513642dd4520SNathan Ciobanu node_id = 0; 513742dd4520SNathan Ciobanu 513840f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 513940f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 51408cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5141c98d5d94SLen Brown 5142c98d5d94SLen Brown t->cpu_id = cpu_id; 51438cb48b32SPrarit Bhargava if (thread_id == 0) { 5144c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5145c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5146c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5147c98d5d94SLen Brown } 5148c98d5d94SLen Brown 51498cb48b32SPrarit Bhargava c->core_id = core_id; 51508cb48b32SPrarit Bhargava p->package_id = pkg_id; 5151c98d5d94SLen Brown } 5152c98d5d94SLen Brown 5153c98d5d94SLen Brown 5154c98d5d94SLen Brown int initialize_counters(int cpu_id) 5155c98d5d94SLen Brown { 51568cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 51578cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5158c98d5d94SLen Brown return 0; 5159c98d5d94SLen Brown } 5160c98d5d94SLen Brown 5161c98d5d94SLen Brown void allocate_output_buffer() 5162c98d5d94SLen Brown { 5163eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5164c98d5d94SLen Brown outp = output_buffer; 5165b2c95d90SJosh Triplett if (outp == NULL) 5166b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5167c98d5d94SLen Brown } 516836229897SLen Brown void allocate_fd_percpu(void) 516936229897SLen Brown { 517001a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 517136229897SLen Brown if (fd_percpu == NULL) 517236229897SLen Brown err(-1, "calloc fd_percpu"); 517336229897SLen Brown } 5174562a2d37SLen Brown void allocate_irq_buffers(void) 5175562a2d37SLen Brown { 5176562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5177562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5178562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5179c98d5d94SLen Brown 518001a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5181562a2d37SLen Brown if (irqs_per_cpu == NULL) 518201a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5183562a2d37SLen Brown } 5184c98d5d94SLen Brown void setup_all_buffers(void) 5185c98d5d94SLen Brown { 5186c98d5d94SLen Brown topology_probe(); 5187562a2d37SLen Brown allocate_irq_buffers(); 518836229897SLen Brown allocate_fd_percpu(); 5189c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5190c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5191c98d5d94SLen Brown allocate_output_buffer(); 5192c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5193c98d5d94SLen Brown } 51943b4d5c7fSAndy Shevchenko 51957ce7d5deSPrarit Bhargava void set_base_cpu(void) 51967ce7d5deSPrarit Bhargava { 51977ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 51987ce7d5deSPrarit Bhargava if (base_cpu < 0) 51997ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 52007ce7d5deSPrarit Bhargava 52017ce7d5deSPrarit Bhargava if (debug > 1) 5202b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 52037ce7d5deSPrarit Bhargava } 52047ce7d5deSPrarit Bhargava 5205103a8feaSLen Brown void turbostat_init() 5206103a8feaSLen Brown { 52077ce7d5deSPrarit Bhargava setup_all_buffers(); 52087ce7d5deSPrarit Bhargava set_base_cpu(); 5209103a8feaSLen Brown check_dev_msr(); 521098481e79SLen Brown check_permissions(); 5211fcd17211SLen Brown process_cpuid(); 5212103a8feaSLen Brown 5213103a8feaSLen Brown 521496e47158SLen Brown if (!quiet) 52157f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 52167f5c258eSLen Brown 521796e47158SLen Brown if (!quiet) 5218889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5219889facbeSLen Brown 522096e47158SLen Brown if (!quiet) 52213a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 52223a9a941dSLen Brown 522396e47158SLen Brown if (!quiet) 5224889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5225889facbeSLen Brown 5226889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5227889facbeSLen Brown 522896e47158SLen Brown if (!quiet) 5229889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 52305a63426eSLen Brown 523196e47158SLen Brown if (!quiet && do_irtl_snb) 52325a63426eSLen Brown print_irtl(); 5233103a8feaSLen Brown } 5234103a8feaSLen Brown 5235103a8feaSLen Brown int fork_it(char **argv) 5236103a8feaSLen Brown { 5237103a8feaSLen Brown pid_t child_pid; 5238d91bb17cSLen Brown int status; 5239d15cf7c1SLen Brown 5240218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5241d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 52424c2122d4SLen Brown first_counter_read = 0; 5243d91bb17cSLen Brown if (status) 5244d91bb17cSLen Brown exit(status); 5245d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5246d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5247103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5248103a8feaSLen Brown 5249103a8feaSLen Brown child_pid = fork(); 5250103a8feaSLen Brown if (!child_pid) { 5251103a8feaSLen Brown /* child */ 5252103a8feaSLen Brown execvp(argv[0], argv); 52530815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5254103a8feaSLen Brown } else { 5255103a8feaSLen Brown 5256103a8feaSLen Brown /* parent */ 5257b2c95d90SJosh Triplett if (child_pid == -1) 5258b2c95d90SJosh Triplett err(1, "fork"); 5259103a8feaSLen Brown 5260103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5261103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5262b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5263b2c95d90SJosh Triplett err(status, "waitpid"); 52642a954966SDavid Arcari 52652a954966SDavid Arcari if (WIFEXITED(status)) 52662a954966SDavid Arcari status = WEXITSTATUS(status); 5267103a8feaSLen Brown } 5268c98d5d94SLen Brown /* 5269c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5270c98d5d94SLen Brown * because re-starting is problematic when forking 5271c98d5d94SLen Brown */ 5272218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5273c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5274103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5275103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5276ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5277ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5278ba3dec99SLen Brown else { 5279c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5280c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5281ba3dec99SLen Brown } 5282103a8feaSLen Brown 5283b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 5284b7d8c148SLen Brown 5285b7d8c148SLen Brown flush_output_stderr(); 5286103a8feaSLen Brown 5287d91bb17cSLen Brown return status; 5288103a8feaSLen Brown } 5289103a8feaSLen Brown 52903b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 52913b4d5c7fSAndy Shevchenko { 52923b4d5c7fSAndy Shevchenko int status; 52933b4d5c7fSAndy Shevchenko 5294218f0e8dSLen Brown snapshot_proc_sysfs_files(); 52953b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 52963b4d5c7fSAndy Shevchenko if (status) 52973b4d5c7fSAndy Shevchenko return status; 52983b4d5c7fSAndy Shevchenko 52993b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 53003b4d5c7fSAndy Shevchenko if (status) 53013b4d5c7fSAndy Shevchenko return status; 53023b4d5c7fSAndy Shevchenko 5303b7d8c148SLen Brown flush_output_stdout(); 53043b4d5c7fSAndy Shevchenko 53053b4d5c7fSAndy Shevchenko return status; 53063b4d5c7fSAndy Shevchenko } 53073b4d5c7fSAndy Shevchenko 5308d8af6f5fSLen Brown void print_version() { 53090f71d089SLen Brown fprintf(outf, "turbostat version 19.03.20" 5310d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 5311d8af6f5fSLen Brown } 5312d8af6f5fSLen Brown 5313495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5314495c7654SLen Brown unsigned int width, enum counter_scope scope, 531541618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5316388e9c81SLen Brown { 5317388e9c81SLen Brown struct msr_counter *msrp; 5318388e9c81SLen Brown 5319388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5320388e9c81SLen Brown if (msrp == NULL) { 5321388e9c81SLen Brown perror("calloc"); 5322388e9c81SLen Brown exit(1); 5323388e9c81SLen Brown } 5324388e9c81SLen Brown 5325388e9c81SLen Brown msrp->msr_num = msr_num; 5326388e9c81SLen Brown strncpy(msrp->name, name, NAME_BYTES); 5327495c7654SLen Brown if (path) 5328495c7654SLen Brown strncpy(msrp->path, path, PATH_BYTES); 5329388e9c81SLen Brown msrp->width = width; 5330388e9c81SLen Brown msrp->type = type; 5331388e9c81SLen Brown msrp->format = format; 533241618e63SLen Brown msrp->flags = flags; 5333388e9c81SLen Brown 5334388e9c81SLen Brown switch (scope) { 5335388e9c81SLen Brown 5336388e9c81SLen Brown case SCOPE_CPU: 5337388e9c81SLen Brown msrp->next = sys.tp; 5338388e9c81SLen Brown sys.tp = msrp; 5339678a3bd1SLen Brown sys.added_thread_counters++; 53400748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5341678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5342678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5343678a3bd1SLen Brown exit(-1); 5344678a3bd1SLen Brown } 5345388e9c81SLen Brown break; 5346388e9c81SLen Brown 5347388e9c81SLen Brown case SCOPE_CORE: 5348388e9c81SLen Brown msrp->next = sys.cp; 5349388e9c81SLen Brown sys.cp = msrp; 5350678a3bd1SLen Brown sys.added_core_counters++; 5351678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5352678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5353678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5354678a3bd1SLen Brown exit(-1); 5355678a3bd1SLen Brown } 5356388e9c81SLen Brown break; 5357388e9c81SLen Brown 5358388e9c81SLen Brown case SCOPE_PACKAGE: 5359388e9c81SLen Brown msrp->next = sys.pp; 5360388e9c81SLen Brown sys.pp = msrp; 5361678a3bd1SLen Brown sys.added_package_counters++; 5362678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5363678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5364678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5365678a3bd1SLen Brown exit(-1); 5366678a3bd1SLen Brown } 5367388e9c81SLen Brown break; 5368388e9c81SLen Brown } 5369388e9c81SLen Brown 5370388e9c81SLen Brown return 0; 5371388e9c81SLen Brown } 5372388e9c81SLen Brown 5373388e9c81SLen Brown void parse_add_command(char *add_command) 5374388e9c81SLen Brown { 5375388e9c81SLen Brown int msr_num = 0; 5376495c7654SLen Brown char *path = NULL; 53770f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5378388e9c81SLen Brown int width = 64; 5379388e9c81SLen Brown int fail = 0; 5380388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5381388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5382388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5383388e9c81SLen Brown 5384388e9c81SLen Brown while (add_command) { 5385388e9c81SLen Brown 5386388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5387388e9c81SLen Brown goto next; 5388388e9c81SLen Brown 5389388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5390388e9c81SLen Brown goto next; 5391388e9c81SLen Brown 5392495c7654SLen Brown if (*add_command == '/') { 5393495c7654SLen Brown path = add_command; 5394495c7654SLen Brown goto next; 5395495c7654SLen Brown } 5396495c7654SLen Brown 5397388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5398388e9c81SLen Brown if ((width == 32) || (width == 64)) 5399388e9c81SLen Brown goto next; 5400388e9c81SLen Brown width = 64; 5401388e9c81SLen Brown } 5402388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 5403388e9c81SLen Brown scope = SCOPE_CPU; 5404388e9c81SLen Brown goto next; 5405388e9c81SLen Brown } 5406388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 5407388e9c81SLen Brown scope = SCOPE_CORE; 5408388e9c81SLen Brown goto next; 5409388e9c81SLen Brown } 5410388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 5411388e9c81SLen Brown scope = SCOPE_PACKAGE; 5412388e9c81SLen Brown goto next; 5413388e9c81SLen Brown } 5414388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 5415388e9c81SLen Brown type = COUNTER_CYCLES; 5416388e9c81SLen Brown goto next; 5417388e9c81SLen Brown } 5418388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 5419388e9c81SLen Brown type = COUNTER_SECONDS; 5420388e9c81SLen Brown goto next; 5421388e9c81SLen Brown } 542241618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 542341618e63SLen Brown type = COUNTER_USEC; 542441618e63SLen Brown goto next; 542541618e63SLen Brown } 5426388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 5427388e9c81SLen Brown format = FORMAT_RAW; 5428388e9c81SLen Brown goto next; 5429388e9c81SLen Brown } 5430388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 5431388e9c81SLen Brown format = FORMAT_DELTA; 5432388e9c81SLen Brown goto next; 5433388e9c81SLen Brown } 5434388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 5435388e9c81SLen Brown format = FORMAT_PERCENT; 5436388e9c81SLen Brown goto next; 5437388e9c81SLen Brown } 5438388e9c81SLen Brown 5439388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 5440388e9c81SLen Brown char *eos; 5441388e9c81SLen Brown 5442388e9c81SLen Brown eos = strchr(name_buffer, ','); 5443388e9c81SLen Brown if (eos) 5444388e9c81SLen Brown *eos = '\0'; 5445388e9c81SLen Brown goto next; 5446388e9c81SLen Brown } 5447388e9c81SLen Brown 5448388e9c81SLen Brown next: 5449388e9c81SLen Brown add_command = strchr(add_command, ','); 5450495c7654SLen Brown if (add_command) { 5451495c7654SLen Brown *add_command = '\0'; 5452388e9c81SLen Brown add_command++; 5453495c7654SLen Brown } 5454388e9c81SLen Brown 5455388e9c81SLen Brown } 5456495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 5457495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 5458388e9c81SLen Brown fail++; 5459388e9c81SLen Brown } 5460388e9c81SLen Brown 5461388e9c81SLen Brown /* generate default column header */ 5462388e9c81SLen Brown if (*name_buffer == '\0') { 5463388e9c81SLen Brown if (width == 32) 54645f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5465388e9c81SLen Brown else 54665f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5467388e9c81SLen Brown } 5468388e9c81SLen Brown 546941618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 5470388e9c81SLen Brown fail++; 5471388e9c81SLen Brown 5472388e9c81SLen Brown if (fail) { 5473388e9c81SLen Brown help(); 5474388e9c81SLen Brown exit(1); 5475388e9c81SLen Brown } 5476388e9c81SLen Brown } 547741618e63SLen Brown 5478dd778a5eSLen Brown int is_deferred_skip(char *name) 5479dd778a5eSLen Brown { 5480dd778a5eSLen Brown int i; 5481dd778a5eSLen Brown 5482dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 5483dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 5484dd778a5eSLen Brown return 1; 5485dd778a5eSLen Brown return 0; 5486dd778a5eSLen Brown } 5487dd778a5eSLen Brown 548841618e63SLen Brown void probe_sysfs(void) 548941618e63SLen Brown { 549041618e63SLen Brown char path[64]; 549141618e63SLen Brown char name_buf[16]; 549241618e63SLen Brown FILE *input; 549341618e63SLen Brown int state; 549441618e63SLen Brown char *sp; 549541618e63SLen Brown 549641618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 549741618e63SLen Brown return; 549841618e63SLen Brown 54990748eaf0SLen Brown for (state = 10; state >= 0; --state) { 550041618e63SLen Brown 550141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 550241618e63SLen Brown base_cpu, state); 550341618e63SLen Brown input = fopen(path, "r"); 550441618e63SLen Brown if (input == NULL) 550541618e63SLen Brown continue; 55068173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 55078173c336SBen Hutchings err(1, "%s: failed to read file", path); 550841618e63SLen Brown 550941618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 551041618e63SLen Brown sp = strchr(name_buf, '-'); 551141618e63SLen Brown if (!sp) 551241618e63SLen Brown sp = strchrnul(name_buf, '\n'); 551341618e63SLen Brown *sp = '%'; 551441618e63SLen Brown *(sp + 1) = '\0'; 551541618e63SLen Brown 551641618e63SLen Brown fclose(input); 551741618e63SLen Brown 551841618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 551941618e63SLen Brown 5520dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5521dd778a5eSLen Brown continue; 5522dd778a5eSLen Brown 552341618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 552441618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 552541618e63SLen Brown } 552641618e63SLen Brown 55270748eaf0SLen Brown for (state = 10; state >= 0; --state) { 552841618e63SLen Brown 552941618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 553041618e63SLen Brown base_cpu, state); 553141618e63SLen Brown input = fopen(path, "r"); 553241618e63SLen Brown if (input == NULL) 553341618e63SLen Brown continue; 55348173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 55358173c336SBen Hutchings err(1, "%s: failed to read file", path); 553641618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 553741618e63SLen Brown sp = strchr(name_buf, '-'); 553841618e63SLen Brown if (!sp) 553941618e63SLen Brown sp = strchrnul(name_buf, '\n'); 554041618e63SLen Brown *sp = '\0'; 554141618e63SLen Brown fclose(input); 554241618e63SLen Brown 554341618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 554441618e63SLen Brown 5545dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5546dd778a5eSLen Brown continue; 5547dd778a5eSLen Brown 554841618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 554941618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 555041618e63SLen Brown } 555141618e63SLen Brown 555241618e63SLen Brown } 555341618e63SLen Brown 55541ef7d21aSLen Brown 55551ef7d21aSLen Brown /* 55561ef7d21aSLen Brown * parse cpuset with following syntax 55571ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 55581ef7d21aSLen Brown */ 55591ef7d21aSLen Brown void parse_cpu_command(char *optarg) 55601ef7d21aSLen Brown { 55611ef7d21aSLen Brown unsigned int start, end; 55621ef7d21aSLen Brown char *next; 55631ef7d21aSLen Brown 55644e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 55654e4e1e7cSLen Brown if (cpu_subset) 55664e4e1e7cSLen Brown goto error; 55674e4e1e7cSLen Brown show_core_only++; 55684e4e1e7cSLen Brown return; 55694e4e1e7cSLen Brown } 55704e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 55714e4e1e7cSLen Brown if (cpu_subset) 55724e4e1e7cSLen Brown goto error; 55734e4e1e7cSLen Brown show_pkg_only++; 55744e4e1e7cSLen Brown return; 55754e4e1e7cSLen Brown } 55764e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 55774e4e1e7cSLen Brown goto error; 55784e4e1e7cSLen Brown 55791ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 55801ef7d21aSLen Brown if (cpu_subset == NULL) 55811ef7d21aSLen Brown err(3, "CPU_ALLOC"); 55821ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 55831ef7d21aSLen Brown 55841ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 55851ef7d21aSLen Brown 55861ef7d21aSLen Brown next = optarg; 55871ef7d21aSLen Brown 55881ef7d21aSLen Brown while (next && *next) { 55891ef7d21aSLen Brown 55901ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 55911ef7d21aSLen Brown goto error; 55921ef7d21aSLen Brown 55931ef7d21aSLen Brown start = strtoul(next, &next, 10); 55941ef7d21aSLen Brown 55951ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 55961ef7d21aSLen Brown goto error; 55971ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 55981ef7d21aSLen Brown 55991ef7d21aSLen Brown if (*next == '\0') 56001ef7d21aSLen Brown break; 56011ef7d21aSLen Brown 56021ef7d21aSLen Brown if (*next == ',') { 56031ef7d21aSLen Brown next += 1; 56041ef7d21aSLen Brown continue; 56051ef7d21aSLen Brown } 56061ef7d21aSLen Brown 56071ef7d21aSLen Brown if (*next == '-') { 56081ef7d21aSLen Brown next += 1; /* start range */ 56091ef7d21aSLen Brown } else if (*next == '.') { 56101ef7d21aSLen Brown next += 1; 56111ef7d21aSLen Brown if (*next == '.') 56121ef7d21aSLen Brown next += 1; /* start range */ 56131ef7d21aSLen Brown else 56141ef7d21aSLen Brown goto error; 56151ef7d21aSLen Brown } 56161ef7d21aSLen Brown 56171ef7d21aSLen Brown end = strtoul(next, &next, 10); 56181ef7d21aSLen Brown if (end <= start) 56191ef7d21aSLen Brown goto error; 56201ef7d21aSLen Brown 56211ef7d21aSLen Brown while (++start <= end) { 56221ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 56231ef7d21aSLen Brown goto error; 56241ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 56251ef7d21aSLen Brown } 56261ef7d21aSLen Brown 56271ef7d21aSLen Brown if (*next == ',') 56281ef7d21aSLen Brown next += 1; 56291ef7d21aSLen Brown else if (*next != '\0') 56301ef7d21aSLen Brown goto error; 56311ef7d21aSLen Brown } 56321ef7d21aSLen Brown 56331ef7d21aSLen Brown return; 56341ef7d21aSLen Brown 56351ef7d21aSLen Brown error: 56364e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 56374e4e1e7cSLen Brown help(); 56381ef7d21aSLen Brown exit(-1); 56391ef7d21aSLen Brown } 56401ef7d21aSLen Brown 5641812db3f7SLen Brown 5642103a8feaSLen Brown void cmdline(int argc, char **argv) 5643103a8feaSLen Brown { 5644103a8feaSLen Brown int opt; 5645d8af6f5fSLen Brown int option_index = 0; 5646d8af6f5fSLen Brown static struct option long_options[] = { 5647388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 56481ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 5649d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 565096e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 56513f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 5652d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 5653023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 5654d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 5655812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 5656d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 5657c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 5658b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 565996e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 5660812db3f7SLen Brown {"show", required_argument, 0, 's'}, 5661d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 5662d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 5663d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 5664d8af6f5fSLen Brown {0, 0, 0, 0 } 5665d8af6f5fSLen Brown }; 5666103a8feaSLen Brown 5667103a8feaSLen Brown progname = argv[0]; 5668103a8feaSLen Brown 5669023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 5670d8af6f5fSLen Brown long_options, &option_index)) != -1) { 5671103a8feaSLen Brown switch (opt) { 5672388e9c81SLen Brown case 'a': 5673388e9c81SLen Brown parse_add_command(optarg); 5674388e9c81SLen Brown break; 56751ef7d21aSLen Brown case 'c': 56761ef7d21aSLen Brown parse_cpu_command(optarg); 56771ef7d21aSLen Brown break; 5678d8af6f5fSLen Brown case 'D': 5679d8af6f5fSLen Brown dump_only++; 56808e180f3cSLen Brown break; 56813f44a5c6SLen Brown case 'e': 56823f44a5c6SLen Brown /* --enable specified counter */ 56834c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 56843f44a5c6SLen Brown break; 5685d8af6f5fSLen Brown case 'd': 5686d8af6f5fSLen Brown debug++; 56873f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 56882f32edf1SLen Brown break; 5689812db3f7SLen Brown case 'H': 56903f44a5c6SLen Brown /* 56913f44a5c6SLen Brown * --hide: do not show those specified 56923f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 56933f44a5c6SLen Brown */ 56943f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 5695812db3f7SLen Brown break; 5696d8af6f5fSLen Brown case 'h': 5697d8af6f5fSLen Brown default: 5698d8af6f5fSLen Brown help(); 5699d8af6f5fSLen Brown exit(1); 5700d8af6f5fSLen Brown case 'i': 57012a0609c0SLen Brown { 57022a0609c0SLen Brown double interval = strtod(optarg, NULL); 57032a0609c0SLen Brown 57042a0609c0SLen Brown if (interval < 0.001) { 5705b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 57062a0609c0SLen Brown interval); 57072a0609c0SLen Brown exit(2); 57082a0609c0SLen Brown } 57092a0609c0SLen Brown 571047936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 5711b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 571247936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 57132a0609c0SLen Brown } 5714889facbeSLen Brown break; 57155c56be9aSDirk Brandewie case 'J': 57165c56be9aSDirk Brandewie rapl_joules++; 57175c56be9aSDirk Brandewie break; 5718c8ade361SLen Brown case 'l': 57193f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 5720c8ade361SLen Brown list_header_only++; 5721c8ade361SLen Brown quiet++; 5722c8ade361SLen Brown break; 5723b7d8c148SLen Brown case 'o': 5724b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 5725b7d8c148SLen Brown break; 572696e47158SLen Brown case 'q': 572796e47158SLen Brown quiet = 1; 572896e47158SLen Brown break; 5729023fe0acSChen Yu case 'n': 5730023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 5731023fe0acSChen Yu 5732023fe0acSChen Yu if (num_iterations <= 0) { 5733023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 5734023fe0acSChen Yu num_iterations); 5735023fe0acSChen Yu exit(2); 5736023fe0acSChen Yu } 5737023fe0acSChen Yu break; 5738812db3f7SLen Brown case 's': 57393f44a5c6SLen Brown /* 57403f44a5c6SLen Brown * --show: show only those specified 57413f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 57423f44a5c6SLen Brown * subsequent invocations can add to it. 57433f44a5c6SLen Brown */ 57443f44a5c6SLen Brown if (shown == 0) 57453f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 57463f44a5c6SLen Brown else 57473f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 57483f44a5c6SLen Brown shown = 1; 5749812db3f7SLen Brown break; 5750d8af6f5fSLen Brown case 'S': 5751d8af6f5fSLen Brown summary_only++; 5752d8af6f5fSLen Brown break; 5753d8af6f5fSLen Brown case 'T': 5754d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 5755d8af6f5fSLen Brown break; 5756d8af6f5fSLen Brown case 'v': 5757d8af6f5fSLen Brown print_version(); 5758d8af6f5fSLen Brown exit(0); 5759d8af6f5fSLen Brown break; 5760103a8feaSLen Brown } 5761103a8feaSLen Brown } 5762103a8feaSLen Brown } 5763103a8feaSLen Brown 5764103a8feaSLen Brown int main(int argc, char **argv) 5765103a8feaSLen Brown { 5766b7d8c148SLen Brown outf = stderr; 5767103a8feaSLen Brown cmdline(argc, argv); 5768103a8feaSLen Brown 576996e47158SLen Brown if (!quiet) 5770d8af6f5fSLen Brown print_version(); 5771103a8feaSLen Brown 577241618e63SLen Brown probe_sysfs(); 577341618e63SLen Brown 5774103a8feaSLen Brown turbostat_init(); 5775103a8feaSLen Brown 57763b4d5c7fSAndy Shevchenko /* dump counters and exit */ 57773b4d5c7fSAndy Shevchenko if (dump_only) 57783b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 57793b4d5c7fSAndy Shevchenko 5780c8ade361SLen Brown /* list header and exit */ 5781c8ade361SLen Brown if (list_header_only) { 5782c8ade361SLen Brown print_header(","); 5783c8ade361SLen Brown flush_output_stdout(); 5784c8ade361SLen Brown return 0; 5785c8ade361SLen Brown } 5786c8ade361SLen Brown 5787103a8feaSLen Brown /* 5788103a8feaSLen Brown * if any params left, it must be a command to fork 5789103a8feaSLen Brown */ 5790103a8feaSLen Brown if (argc - optind) 5791103a8feaSLen Brown return fork_it(argv + optind); 5792103a8feaSLen Brown else 5793103a8feaSLen Brown turbostat_loop(); 5794103a8feaSLen Brown 5795103a8feaSLen Brown return 0; 5796103a8feaSLen Brown } 5797