1103a8feaSLen Brown /* 2103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 3103a8feaSLen Brown * on modern Intel turbo-capable processors. 4103a8feaSLen Brown * 5144b44b1SLen Brown * Copyright (c) 2013 Intel Corporation. 6103a8feaSLen Brown * Len Brown <len.brown@intel.com> 7103a8feaSLen Brown * 8103a8feaSLen Brown * This program is free software; you can redistribute it and/or modify it 9103a8feaSLen Brown * under the terms and conditions of the GNU General Public License, 10103a8feaSLen Brown * version 2, as published by the Free Software Foundation. 11103a8feaSLen Brown * 12103a8feaSLen Brown * This program is distributed in the hope it will be useful, but WITHOUT 13103a8feaSLen Brown * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14103a8feaSLen Brown * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15103a8feaSLen Brown * more details. 16103a8feaSLen Brown * 17103a8feaSLen Brown * You should have received a copy of the GNU General Public License along with 18103a8feaSLen Brown * this program; if not, write to the Free Software Foundation, Inc., 19103a8feaSLen Brown * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 20103a8feaSLen Brown */ 21103a8feaSLen Brown 2288c3281fSLen Brown #define _GNU_SOURCE 23b731f311SJosh Triplett #include MSRHEADER 24869ce69eSLen Brown #include INTEL_FAMILY_HEADER 2595aebc44SJosh Triplett #include <stdarg.h> 26103a8feaSLen Brown #include <stdio.h> 27b2c95d90SJosh Triplett #include <err.h> 28103a8feaSLen Brown #include <unistd.h> 29103a8feaSLen Brown #include <sys/types.h> 30103a8feaSLen Brown #include <sys/wait.h> 31103a8feaSLen Brown #include <sys/stat.h> 32103a8feaSLen Brown #include <sys/resource.h> 33103a8feaSLen Brown #include <fcntl.h> 34103a8feaSLen Brown #include <signal.h> 35103a8feaSLen Brown #include <sys/time.h> 36103a8feaSLen Brown #include <stdlib.h> 37d8af6f5fSLen Brown #include <getopt.h> 38103a8feaSLen Brown #include <dirent.h> 39103a8feaSLen Brown #include <string.h> 40103a8feaSLen Brown #include <ctype.h> 4188c3281fSLen Brown #include <sched.h> 422a0609c0SLen Brown #include <time.h> 432b92865eSJosh Triplett #include <cpuid.h> 4498481e79SLen Brown #include <linux/capability.h> 4598481e79SLen Brown #include <errno.h> 46103a8feaSLen Brown 47103a8feaSLen Brown char *proc_stat = "/proc/stat"; 48b7d8c148SLen Brown FILE *outf; 4936229897SLen Brown int *fd_percpu; 502a0609c0SLen Brown struct timespec interval_ts = {5, 0}; 51d8af6f5fSLen Brown unsigned int debug; 5296e47158SLen Brown unsigned int quiet; 530de6c0dfSLen Brown unsigned int sums_need_wide_columns; 54d8af6f5fSLen Brown unsigned int rapl_joules; 55d8af6f5fSLen Brown unsigned int summary_only; 56c8ade361SLen Brown unsigned int list_header_only; 57d8af6f5fSLen Brown unsigned int dump_only; 58103a8feaSLen Brown unsigned int do_snb_cstates; 59fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 600b2bb692SLen Brown unsigned int do_skl_residency; 61144b44b1SLen Brown unsigned int do_slm_cstates; 62144b44b1SLen Brown unsigned int use_c1_residency_msr; 63103a8feaSLen Brown unsigned int has_aperf; 64889facbeSLen Brown unsigned int has_epb; 655a63426eSLen Brown unsigned int do_irtl_snb; 665a63426eSLen Brown unsigned int do_irtl_hsw; 67fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 68103a8feaSLen Brown unsigned int genuine_intel; 69103a8feaSLen Brown unsigned int has_invariant_tsc; 70d7899447SLen Brown unsigned int do_nhm_platform_info; 71cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 72b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 73103a8feaSLen Brown double bclk; 74a2b7b749SLen Brown double base_hz; 7521ed5574SLen Brown unsigned int has_base_hz; 76a2b7b749SLen Brown double tsc_tweak = 1.0; 77c98d5d94SLen Brown unsigned int show_pkg_only; 78c98d5d94SLen Brown unsigned int show_core_only; 79c98d5d94SLen Brown char *output_buffer, *outp; 80889facbeSLen Brown unsigned int do_rapl; 81889facbeSLen Brown unsigned int do_dts; 82889facbeSLen Brown unsigned int do_ptm; 83fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 8427d47356SLen Brown unsigned int gfx_cur_mhz; 85889facbeSLen Brown unsigned int tcc_activation_temp; 86889facbeSLen Brown unsigned int tcc_activation_temp_override; 8740ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 8840ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 89889facbeSLen Brown double rapl_joule_counter_range; 903a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 913a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 923a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 938a5bdf41SLen Brown unsigned int crystal_hz; 948a5bdf41SLen Brown unsigned long long tsc_hz; 957ce7d5deSPrarit Bhargava int base_cpu; 9621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 977f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 987f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 997f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1007f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1017f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1027f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 10333148d67SLen Brown unsigned int has_misc_feature_control; 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 */ 1339148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 134889facbeSLen Brown #define TJMAX_DEFAULT 100 135889facbeSLen Brown 136889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 137103a8feaSLen Brown 138388e9c81SLen Brown /* 139388e9c81SLen Brown * buffer size used by sscanf() for added column names 140388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 141388e9c81SLen Brown */ 142388e9c81SLen Brown #define NAME_BYTES 20 143495c7654SLen Brown #define PATH_BYTES 128 144388e9c81SLen Brown 145103a8feaSLen Brown int backwards_count; 146103a8feaSLen Brown char *progname; 147103a8feaSLen Brown 1481ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1491ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1501ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 151678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16 152c98d5d94SLen Brown 153c98d5d94SLen Brown struct thread_data { 154c98d5d94SLen Brown unsigned long long tsc; 155c98d5d94SLen Brown unsigned long long aperf; 156c98d5d94SLen Brown unsigned long long mperf; 157144b44b1SLen Brown unsigned long long c1; 1580de6c0dfSLen Brown unsigned long long irq_count; 1591ed51011SLen Brown unsigned int smi_count; 160c98d5d94SLen Brown unsigned int cpu_id; 161c98d5d94SLen Brown unsigned int flags; 162c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 163c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 164678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 165c98d5d94SLen Brown } *thread_even, *thread_odd; 166c98d5d94SLen Brown 167c98d5d94SLen Brown struct core_data { 168c98d5d94SLen Brown unsigned long long c3; 169c98d5d94SLen Brown unsigned long long c6; 170c98d5d94SLen Brown unsigned long long c7; 1710539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 172889facbeSLen Brown unsigned int core_temp_c; 173c98d5d94SLen Brown unsigned int core_id; 174678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 175c98d5d94SLen Brown } *core_even, *core_odd; 176c98d5d94SLen Brown 177c98d5d94SLen Brown struct pkg_data { 178c98d5d94SLen Brown unsigned long long pc2; 179c98d5d94SLen Brown unsigned long long pc3; 180c98d5d94SLen Brown unsigned long long pc6; 181c98d5d94SLen Brown unsigned long long pc7; 182ca58710fSKristen Carlson Accardi unsigned long long pc8; 183ca58710fSKristen Carlson Accardi unsigned long long pc9; 184ca58710fSKristen Carlson Accardi unsigned long long pc10; 1850b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 1860b2bb692SLen Brown unsigned long long pkg_any_core_c0; 1870b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 1880b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 1899185e988SLen Brown long long gfx_rc6_ms; 19027d47356SLen Brown unsigned int gfx_mhz; 191c98d5d94SLen Brown unsigned int package_id; 192889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 193889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 194889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 195889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 196889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 197889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 198889facbeSLen Brown unsigned int pkg_temp_c; 199678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 200c98d5d94SLen Brown } *package_even, *package_odd; 201c98d5d94SLen Brown 202c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 203c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 204c98d5d94SLen Brown 205c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 206c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 207c98d5d94SLen Brown topo.num_threads_per_core + \ 208c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 209c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 210c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 211c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 212c98d5d94SLen Brown 213388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE}; 21441618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC}; 215388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT}; 216388e9c81SLen Brown 217388e9c81SLen Brown struct msr_counter { 218388e9c81SLen Brown unsigned int msr_num; 219388e9c81SLen Brown char name[NAME_BYTES]; 220495c7654SLen Brown char path[PATH_BYTES]; 221388e9c81SLen Brown unsigned int width; 222388e9c81SLen Brown enum counter_type type; 223388e9c81SLen Brown enum counter_format format; 224388e9c81SLen Brown struct msr_counter *next; 225812db3f7SLen Brown unsigned int flags; 226812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 227812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 22841618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 229388e9c81SLen Brown }; 230388e9c81SLen Brown 231388e9c81SLen Brown struct sys_counters { 232678a3bd1SLen Brown unsigned int added_thread_counters; 233678a3bd1SLen Brown unsigned int added_core_counters; 234678a3bd1SLen Brown unsigned int added_package_counters; 235388e9c81SLen Brown struct msr_counter *tp; 236388e9c81SLen Brown struct msr_counter *cp; 237388e9c81SLen Brown struct msr_counter *pp; 238388e9c81SLen Brown } sys; 239388e9c81SLen Brown 240c98d5d94SLen Brown struct system_summary { 241c98d5d94SLen Brown struct thread_data threads; 242c98d5d94SLen Brown struct core_data cores; 243c98d5d94SLen Brown struct pkg_data packages; 244388e9c81SLen Brown } average; 245c98d5d94SLen Brown 246c98d5d94SLen Brown 247c98d5d94SLen Brown struct topo_params { 248c98d5d94SLen Brown int num_packages; 249103a8feaSLen Brown int num_cpus; 250c98d5d94SLen Brown int num_cores; 251c98d5d94SLen Brown int max_cpu_num; 252c98d5d94SLen Brown int num_cores_per_pkg; 253c98d5d94SLen Brown int num_threads_per_core; 254c98d5d94SLen Brown } topo; 255103a8feaSLen Brown 256c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 257103a8feaSLen Brown 258562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 259562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 260562a2d37SLen Brown 261c98d5d94SLen Brown void setup_all_buffers(void); 262103a8feaSLen Brown 263c98d5d94SLen Brown int cpu_is_not_present(int cpu) 264d15cf7c1SLen Brown { 265c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 266c98d5d94SLen Brown } 267c98d5d94SLen Brown /* 268c98d5d94SLen Brown * run func(thread, core, package) in topology order 269c98d5d94SLen Brown * skip non-present cpus 270c98d5d94SLen Brown */ 271c98d5d94SLen Brown 272c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 273c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 274c98d5d94SLen Brown { 275c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 276c98d5d94SLen Brown 277c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 278c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 279c98d5d94SLen Brown for (thread_no = 0; thread_no < 280c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 281c98d5d94SLen Brown struct thread_data *t; 282c98d5d94SLen Brown struct core_data *c; 283c98d5d94SLen Brown struct pkg_data *p; 284c98d5d94SLen Brown 285c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 286c98d5d94SLen Brown 287c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 288c98d5d94SLen Brown continue; 289c98d5d94SLen Brown 290c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 291c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 292c98d5d94SLen Brown 293c98d5d94SLen Brown retval = func(t, c, p); 294c98d5d94SLen Brown if (retval) 295c98d5d94SLen Brown return retval; 296c98d5d94SLen Brown } 297c98d5d94SLen Brown } 298c98d5d94SLen Brown } 299d15cf7c1SLen Brown return 0; 300d15cf7c1SLen Brown } 301d15cf7c1SLen Brown 30288c3281fSLen Brown int cpu_migrate(int cpu) 30388c3281fSLen Brown { 304c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 305c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 306c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 30788c3281fSLen Brown return -1; 30888c3281fSLen Brown else 30988c3281fSLen Brown return 0; 31088c3281fSLen Brown } 31136229897SLen Brown int get_msr_fd(int cpu) 312103a8feaSLen Brown { 313103a8feaSLen Brown char pathname[32]; 314103a8feaSLen Brown int fd; 315103a8feaSLen Brown 31636229897SLen Brown fd = fd_percpu[cpu]; 31736229897SLen Brown 31836229897SLen Brown if (fd) 31936229897SLen Brown return fd; 32036229897SLen Brown 321103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 322103a8feaSLen Brown fd = open(pathname, O_RDONLY); 32315aaa346SLen Brown if (fd < 0) 32498481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 325103a8feaSLen Brown 32636229897SLen Brown fd_percpu[cpu] = fd; 32736229897SLen Brown 32836229897SLen Brown return fd; 32936229897SLen Brown } 33036229897SLen Brown 33136229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 33236229897SLen Brown { 33336229897SLen Brown ssize_t retval; 33436229897SLen Brown 33536229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 33615aaa346SLen Brown 33798481e79SLen Brown if (retval != sizeof *msr) 338cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 33915aaa346SLen Brown 34015aaa346SLen Brown return 0; 341103a8feaSLen Brown } 342103a8feaSLen Brown 343fc04cc67SLen Brown /* 344812db3f7SLen Brown * Each string in this array is compared in --show and --hide cmdline. 345812db3f7SLen Brown * Thus, strings that are proper sub-sets must follow their more specific peers. 346fc04cc67SLen Brown */ 347812db3f7SLen Brown struct msr_counter bic[] = { 348812db3f7SLen Brown { 0x0, "Package" }, 349812db3f7SLen Brown { 0x0, "Avg_MHz" }, 350812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 351812db3f7SLen Brown { 0x0, "TSC_MHz" }, 352812db3f7SLen Brown { 0x0, "IRQ" }, 353495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 354812db3f7SLen Brown { 0x0, "Busy%" }, 355812db3f7SLen Brown { 0x0, "CPU%c1" }, 356812db3f7SLen Brown { 0x0, "CPU%c3" }, 357812db3f7SLen Brown { 0x0, "CPU%c6" }, 358812db3f7SLen Brown { 0x0, "CPU%c7" }, 359812db3f7SLen Brown { 0x0, "ThreadC" }, 360812db3f7SLen Brown { 0x0, "CoreTmp" }, 361812db3f7SLen Brown { 0x0, "CoreCnt" }, 362812db3f7SLen Brown { 0x0, "PkgTmp" }, 363812db3f7SLen Brown { 0x0, "GFX%rc6" }, 364812db3f7SLen Brown { 0x0, "GFXMHz" }, 365812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 366812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 367812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 368812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 3690f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 3700f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 3710f47c08dSLen Brown { 0x0, "Pkg%pc10" }, 372812db3f7SLen Brown { 0x0, "PkgWatt" }, 373812db3f7SLen Brown { 0x0, "CorWatt" }, 374812db3f7SLen Brown { 0x0, "GFXWatt" }, 375812db3f7SLen Brown { 0x0, "PkgCnt" }, 376812db3f7SLen Brown { 0x0, "RAMWatt" }, 377812db3f7SLen Brown { 0x0, "PKG_%" }, 378812db3f7SLen Brown { 0x0, "RAM_%" }, 379812db3f7SLen Brown { 0x0, "Pkg_J" }, 380812db3f7SLen Brown { 0x0, "Cor_J" }, 381812db3f7SLen Brown { 0x0, "GFX_J" }, 382812db3f7SLen Brown { 0x0, "RAM_J" }, 383812db3f7SLen Brown { 0x0, "Core" }, 384812db3f7SLen Brown { 0x0, "CPU" }, 3850539ba11SLen Brown { 0x0, "Mod%c6" }, 38641618e63SLen Brown { 0x0, "sysfs" }, 387812db3f7SLen Brown }; 388812db3f7SLen Brown 389812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 390812db3f7SLen Brown #define BIC_Package (1ULL << 0) 391812db3f7SLen Brown #define BIC_Avg_MHz (1ULL << 1) 392812db3f7SLen Brown #define BIC_Bzy_MHz (1ULL << 2) 393812db3f7SLen Brown #define BIC_TSC_MHz (1ULL << 3) 394812db3f7SLen Brown #define BIC_IRQ (1ULL << 4) 395812db3f7SLen Brown #define BIC_SMI (1ULL << 5) 396812db3f7SLen Brown #define BIC_Busy (1ULL << 6) 397812db3f7SLen Brown #define BIC_CPU_c1 (1ULL << 7) 398812db3f7SLen Brown #define BIC_CPU_c3 (1ULL << 8) 399812db3f7SLen Brown #define BIC_CPU_c6 (1ULL << 9) 400812db3f7SLen Brown #define BIC_CPU_c7 (1ULL << 10) 401812db3f7SLen Brown #define BIC_ThreadC (1ULL << 11) 402812db3f7SLen Brown #define BIC_CoreTmp (1ULL << 12) 403812db3f7SLen Brown #define BIC_CoreCnt (1ULL << 13) 404812db3f7SLen Brown #define BIC_PkgTmp (1ULL << 14) 405812db3f7SLen Brown #define BIC_GFX_rc6 (1ULL << 15) 406812db3f7SLen Brown #define BIC_GFXMHz (1ULL << 16) 407812db3f7SLen Brown #define BIC_Pkgpc2 (1ULL << 17) 408812db3f7SLen Brown #define BIC_Pkgpc3 (1ULL << 18) 409812db3f7SLen Brown #define BIC_Pkgpc6 (1ULL << 19) 410812db3f7SLen Brown #define BIC_Pkgpc7 (1ULL << 20) 4110f47c08dSLen Brown #define BIC_Pkgpc8 (1ULL << 21) 4120f47c08dSLen Brown #define BIC_Pkgpc9 (1ULL << 22) 4130f47c08dSLen Brown #define BIC_Pkgpc10 (1ULL << 23) 4140f47c08dSLen Brown #define BIC_PkgWatt (1ULL << 24) 4150f47c08dSLen Brown #define BIC_CorWatt (1ULL << 25) 4160f47c08dSLen Brown #define BIC_GFXWatt (1ULL << 26) 4170f47c08dSLen Brown #define BIC_PkgCnt (1ULL << 27) 4180f47c08dSLen Brown #define BIC_RAMWatt (1ULL << 28) 4190f47c08dSLen Brown #define BIC_PKG__ (1ULL << 29) 4200f47c08dSLen Brown #define BIC_RAM__ (1ULL << 30) 4210f47c08dSLen Brown #define BIC_Pkg_J (1ULL << 31) 4220f47c08dSLen Brown #define BIC_Cor_J (1ULL << 32) 4230f47c08dSLen Brown #define BIC_GFX_J (1ULL << 33) 4240f47c08dSLen Brown #define BIC_RAM_J (1ULL << 34) 4250f47c08dSLen Brown #define BIC_Core (1ULL << 35) 4260f47c08dSLen Brown #define BIC_CPU (1ULL << 36) 4270f47c08dSLen Brown #define BIC_Mod_c6 (1ULL << 37) 42841618e63SLen Brown #define BIC_sysfs (1ULL << 38) 429812db3f7SLen Brown 430812db3f7SLen Brown unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL; 43141618e63SLen Brown unsigned long long bic_present = BIC_sysfs; 432812db3f7SLen Brown 433812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 434812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 4350f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 436812db3f7SLen Brown 437dd778a5eSLen Brown #define MAX_DEFERRED 16 438dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 439dd778a5eSLen Brown int deferred_skip_index; 440dd778a5eSLen Brown 441dd778a5eSLen Brown /* 442dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 443dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 444dd778a5eSLen Brown */ 445dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 446dd778a5eSLen Brown 447dd778a5eSLen Brown void help(void) 448dd778a5eSLen Brown { 449dd778a5eSLen Brown fprintf(outf, 450dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 451dd778a5eSLen Brown "\n" 452dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 453dd778a5eSLen Brown "when COMMAND completes.\n" 454dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 455dd778a5eSLen Brown "to print statistics, until interrupted.\n" 456dd778a5eSLen Brown "--add add a counter\n" 457dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 458dd778a5eSLen Brown "--cpu cpu-set limit output to summary plus cpu-set:\n" 459dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 460dd778a5eSLen Brown "--quiet skip decoding system configuration header\n" 461dd778a5eSLen Brown "--interval sec Override default 5-second measurement interval\n" 462dd778a5eSLen Brown "--help print this help message\n" 463dd778a5eSLen Brown "--list list column headers only\n" 464dd778a5eSLen Brown "--out file create or truncate \"file\" for all output\n" 465dd778a5eSLen Brown "--version print version information\n" 466dd778a5eSLen Brown "\n" 467dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 468dd778a5eSLen Brown } 469dd778a5eSLen Brown 470812db3f7SLen Brown /* 471812db3f7SLen Brown * bic_lookup 472812db3f7SLen Brown * for all the strings in comma separate name_list, 473812db3f7SLen Brown * set the approprate bit in return value. 474812db3f7SLen Brown */ 475dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 476812db3f7SLen Brown { 477812db3f7SLen Brown int i; 478812db3f7SLen Brown unsigned long long retval = 0; 479812db3f7SLen Brown 480812db3f7SLen Brown while (name_list) { 481812db3f7SLen Brown char *comma; 482812db3f7SLen Brown 483812db3f7SLen Brown comma = strchr(name_list, ','); 484812db3f7SLen Brown 485812db3f7SLen Brown if (comma) 486812db3f7SLen Brown *comma = '\0'; 487812db3f7SLen Brown 488812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 489812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 490812db3f7SLen Brown retval |= (1ULL << i); 491812db3f7SLen Brown break; 492812db3f7SLen Brown } 493812db3f7SLen Brown } 494812db3f7SLen Brown if (i == MAX_BIC) { 495dd778a5eSLen Brown if (mode == SHOW_LIST) { 496812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 497812db3f7SLen Brown exit(-1); 498812db3f7SLen Brown } 499dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 500dd778a5eSLen Brown if (debug) 501dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 502dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 503dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 504dd778a5eSLen Brown MAX_DEFERRED, name_list); 505dd778a5eSLen Brown help(); 506dd778a5eSLen Brown exit(1); 507dd778a5eSLen Brown } 508dd778a5eSLen Brown } 509812db3f7SLen Brown 510812db3f7SLen Brown name_list = comma; 511812db3f7SLen Brown if (name_list) 512812db3f7SLen Brown name_list++; 513812db3f7SLen Brown 514812db3f7SLen Brown } 515812db3f7SLen Brown return retval; 516812db3f7SLen Brown } 517fc04cc67SLen Brown 518dd778a5eSLen Brown 519c8ade361SLen Brown void print_header(char *delim) 520103a8feaSLen Brown { 521388e9c81SLen Brown struct msr_counter *mp; 5226168c2e0SLen Brown int printed = 0; 523388e9c81SLen Brown 524812db3f7SLen Brown if (DO_BIC(BIC_Package)) 5256168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 526812db3f7SLen Brown if (DO_BIC(BIC_Core)) 5276168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 528812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 5296168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 530812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 5316168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 532812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 5336168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 534812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 5356168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 536812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 5376168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 5381cc21f7bSLen Brown 5390de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 5400de6c0dfSLen Brown if (sums_need_wide_columns) 5416168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 5420de6c0dfSLen Brown else 5436168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 5440de6c0dfSLen Brown } 5450de6c0dfSLen Brown 546812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 5476168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 5481cc21f7bSLen Brown 549388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 550dd778a5eSLen Brown 551388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 552388e9c81SLen Brown if (mp->width == 64) 553dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 554388e9c81SLen Brown else 555dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 556388e9c81SLen Brown } else { 5570de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 558dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 5590de6c0dfSLen Brown else 560dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 561388e9c81SLen Brown } 562388e9c81SLen Brown } 563388e9c81SLen Brown 56441618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 5656168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 566812db3f7SLen Brown if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) 5676168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 568812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 5696168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 570812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 5716168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 572678a3bd1SLen Brown 5730539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 5746168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 575678a3bd1SLen Brown 576812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 5776168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 578388e9c81SLen Brown 579388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 580388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 581388e9c81SLen Brown if (mp->width == 64) 582c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 583388e9c81SLen Brown else 584c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 585388e9c81SLen Brown } else { 5860de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 5870de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 5880de6c0dfSLen Brown else 589c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 590388e9c81SLen Brown } 591388e9c81SLen Brown } 592388e9c81SLen Brown 593812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 5946168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 595889facbeSLen Brown 596812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 5976168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 598fdf676e5SLen Brown 599812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 6006168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 60127d47356SLen Brown 6020b2bb692SLen Brown if (do_skl_residency) { 6036168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 6046168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 6056168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 6066168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 6070b2bb692SLen Brown } 6080b2bb692SLen Brown 6090f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 6106168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 6110f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 6126168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 6130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 6146168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 6150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 6166168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 6170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 6186168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 6190f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 6206168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 6210f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 6226168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 623103a8feaSLen Brown 6245c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 625812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 6266168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 627812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 6286168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 629812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 6306168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 631812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 6326168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 633812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 6346168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 635812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 6366168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 637d7899447SLen Brown } else if (do_rapl && rapl_joules) { 638812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 6396168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 640812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 6416168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 642812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 6436168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 644812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 6456168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 646812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 6476168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 648812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 6496168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 6505c56be9aSDirk Brandewie } 651388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 652388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 653388e9c81SLen Brown if (mp->width == 64) 654c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 655388e9c81SLen Brown else 656c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 657388e9c81SLen Brown } else { 6580de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 6590de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 6600de6c0dfSLen Brown else 661c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 662388e9c81SLen Brown } 663388e9c81SLen Brown } 664388e9c81SLen Brown 665c98d5d94SLen Brown outp += sprintf(outp, "\n"); 666103a8feaSLen Brown } 667103a8feaSLen Brown 668c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 669c98d5d94SLen Brown struct pkg_data *p) 670103a8feaSLen Brown { 671388e9c81SLen Brown int i; 672388e9c81SLen Brown struct msr_counter *mp; 673388e9c81SLen Brown 6743b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 675c98d5d94SLen Brown 676c98d5d94SLen Brown if (t) { 6773b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 6783b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 6793b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 6803b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 6813b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 6823b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 6836886fee4SLen Brown 684812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 6850de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 686812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 687218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 688388e9c81SLen Brown 689388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 690388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 691388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 692388e9c81SLen Brown } 693103a8feaSLen Brown } 694103a8feaSLen Brown 695c98d5d94SLen Brown if (c) { 6963b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 6973b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 6983b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 6993b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 7003b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 701388e9c81SLen Brown 702388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 703388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 704388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 705388e9c81SLen Brown } 7060539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 707c98d5d94SLen Brown } 708103a8feaSLen Brown 709c98d5d94SLen Brown if (p) { 7103b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 7110b2bb692SLen Brown 7120b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 7130b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 7140b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 7150b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 7160b2bb692SLen Brown 7173b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 7180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 7193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 7200f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 7213b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 7220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 7233b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 7243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 7253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 7263b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 7273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 7283b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 7293b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 7303b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 7313b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 7323b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 7333b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 7343b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 7353b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 736388e9c81SLen Brown 737388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 738388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 739388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 740388e9c81SLen Brown } 741c98d5d94SLen Brown } 7423b4d5c7fSAndy Shevchenko 7433b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 7443b4d5c7fSAndy Shevchenko 745c98d5d94SLen Brown return 0; 746103a8feaSLen Brown } 747103a8feaSLen Brown 748e23da037SLen Brown /* 749e23da037SLen Brown * column formatting convention & formats 750e23da037SLen Brown */ 751c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 752c98d5d94SLen Brown struct pkg_data *p) 753103a8feaSLen Brown { 754008d396eSLen Brown double interval_float, tsc; 755fc04cc67SLen Brown char *fmt8; 756388e9c81SLen Brown int i; 757388e9c81SLen Brown struct msr_counter *mp; 7586168c2e0SLen Brown char *delim = "\t"; 7596168c2e0SLen Brown int printed = 0; 760103a8feaSLen Brown 761c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 762c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 763c98d5d94SLen Brown return 0; 764c98d5d94SLen Brown 765c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 766c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 767c98d5d94SLen Brown return 0; 768c98d5d94SLen Brown 7691ef7d21aSLen Brown /*if not summary line and --cpu is used */ 7701ef7d21aSLen Brown if ((t != &average.threads) && 7711ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 7721ef7d21aSLen Brown return 0; 7731ef7d21aSLen Brown 774103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 775103a8feaSLen Brown 776008d396eSLen Brown tsc = t->tsc * tsc_tweak; 777008d396eSLen Brown 778c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 779c98d5d94SLen Brown if (t == &average.threads) { 780812db3f7SLen Brown if (DO_BIC(BIC_Package)) 7816168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 782812db3f7SLen Brown if (DO_BIC(BIC_Core)) 7836168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 784812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 7856168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 786103a8feaSLen Brown } else { 787812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 788c98d5d94SLen Brown if (p) 7896168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 790c98d5d94SLen Brown else 7916168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 792c98d5d94SLen Brown } 793812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 794c98d5d94SLen Brown if (c) 7956168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 796c98d5d94SLen Brown else 7976168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 798c98d5d94SLen Brown } 799812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8006168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 801103a8feaSLen Brown } 802fc04cc67SLen Brown 803812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8046168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 805fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 806fc04cc67SLen Brown 807812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8086168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 809103a8feaSLen Brown 810812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 81121ed5574SLen Brown if (has_base_hz) 8126168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 81321ed5574SLen Brown else 8146168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 815008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 81621ed5574SLen Brown } 817103a8feaSLen Brown 818812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8196168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 820103a8feaSLen Brown 821562a2d37SLen Brown /* IRQ */ 8220de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 8230de6c0dfSLen Brown if (sums_need_wide_columns) 8246168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 8250de6c0dfSLen Brown else 8266168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 8270de6c0dfSLen Brown } 828562a2d37SLen Brown 8291cc21f7bSLen Brown /* SMI */ 830812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 8316168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 8321cc21f7bSLen Brown 833678a3bd1SLen Brown /* Added counters */ 834678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 835678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 836678a3bd1SLen Brown if (mp->width == 32) 8375f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 838678a3bd1SLen Brown else 8396168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 840678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 8410de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8426168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 8430de6c0dfSLen Brown else 8446168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 845678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 84641618e63SLen Brown if (mp->type == COUNTER_USEC) 8476168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 84841618e63SLen Brown else 8496168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 850678a3bd1SLen Brown } 851678a3bd1SLen Brown } 852678a3bd1SLen Brown 85341618e63SLen Brown /* C1 */ 85441618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 8556168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 85641618e63SLen Brown 85741618e63SLen Brown 858c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 859c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 860c98d5d94SLen Brown goto done; 861c98d5d94SLen Brown 862812db3f7SLen Brown if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) 8636168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 864812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 8656168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 866812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 8676168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 868c98d5d94SLen Brown 8690539ba11SLen Brown /* Mod%c6 */ 8700539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 8716168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 8720539ba11SLen Brown 873812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8746168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 875889facbeSLen Brown 876388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 877388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 878388e9c81SLen Brown if (mp->width == 32) 8795f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 880388e9c81SLen Brown else 8816168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 882388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 8830de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 8846168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 8850de6c0dfSLen Brown else 8866168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 887388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 8886168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 889388e9c81SLen Brown } 890388e9c81SLen Brown } 891388e9c81SLen Brown 892c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 893c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 894c98d5d94SLen Brown goto done; 895c98d5d94SLen Brown 8960b2bb692SLen Brown /* PkgTmp */ 897812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 8986168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 899889facbeSLen Brown 900fdf676e5SLen Brown /* GFXrc6 */ 901812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 902ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 9036168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 9049185e988SLen Brown } else { 9056168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 9069185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 9079185e988SLen Brown } 9089185e988SLen Brown } 909fdf676e5SLen Brown 91027d47356SLen Brown /* GFXMHz */ 911812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 9126168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 91327d47356SLen Brown 9140b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 9150b2bb692SLen Brown if (do_skl_residency) { 9166168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 9176168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 9186168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 9196168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 9200b2bb692SLen Brown } 9210b2bb692SLen Brown 9220f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 9236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 9240f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 9256168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 9260f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 9276168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 9280f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 9296168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 9300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 9316168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 9320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 9336168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 9340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 9356168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 936889facbeSLen Brown 937889facbeSLen Brown /* 938889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 939889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 940889facbeSLen Brown */ 941fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 9426168c2e0SLen Brown fmt8 = "%s%.2f"; 943fc04cc67SLen Brown else 944fc04cc67SLen Brown fmt8 = "%6.0f**"; 945889facbeSLen Brown 946812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 9476168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 948812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 9496168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 950812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 9516168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 952812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 9536168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 954812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 9556168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 956812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 9576168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 958812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 9596168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 960812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 9616168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 962812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 9636168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 964812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 9656168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 966812db3f7SLen Brown 967388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 968388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 969388e9c81SLen Brown if (mp->width == 32) 9705f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]); 971388e9c81SLen Brown else 9726168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 973388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 9740de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9756168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 9760de6c0dfSLen Brown else 9776168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 978388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 9796168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc); 980388e9c81SLen Brown } 981388e9c81SLen Brown } 982388e9c81SLen Brown 983c98d5d94SLen Brown done: 984c98d5d94SLen Brown outp += sprintf(outp, "\n"); 985c98d5d94SLen Brown 986c98d5d94SLen Brown return 0; 987103a8feaSLen Brown } 988103a8feaSLen Brown 989b7d8c148SLen Brown void flush_output_stdout(void) 990103a8feaSLen Brown { 991b7d8c148SLen Brown FILE *filep; 992b7d8c148SLen Brown 993b7d8c148SLen Brown if (outf == stderr) 994b7d8c148SLen Brown filep = stdout; 995b7d8c148SLen Brown else 996b7d8c148SLen Brown filep = outf; 997b7d8c148SLen Brown 998b7d8c148SLen Brown fputs(output_buffer, filep); 999b7d8c148SLen Brown fflush(filep); 1000b7d8c148SLen Brown 1001c98d5d94SLen Brown outp = output_buffer; 1002c98d5d94SLen Brown } 1003b7d8c148SLen Brown void flush_output_stderr(void) 1004c98d5d94SLen Brown { 1005b7d8c148SLen Brown fputs(output_buffer, outf); 1006b7d8c148SLen Brown fflush(outf); 1007c98d5d94SLen Brown outp = output_buffer; 1008c98d5d94SLen Brown } 1009c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1010c98d5d94SLen Brown { 1011e23da037SLen Brown static int printed; 1012103a8feaSLen Brown 1013e23da037SLen Brown if (!printed || !summary_only) 1014c8ade361SLen Brown print_header("\t"); 1015103a8feaSLen Brown 1016c98d5d94SLen Brown if (topo.num_cpus > 1) 1017c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 1018c98d5d94SLen Brown &average.packages); 1019103a8feaSLen Brown 1020e23da037SLen Brown printed = 1; 1021e23da037SLen Brown 1022e23da037SLen Brown if (summary_only) 1023e23da037SLen Brown return; 1024e23da037SLen Brown 1025c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1026103a8feaSLen Brown } 1027103a8feaSLen Brown 1028889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 1029889facbeSLen Brown if (new > old) { \ 1030889facbeSLen Brown old = new - old; \ 1031889facbeSLen Brown } else { \ 1032889facbeSLen Brown old = 0x100000000 + new - old; \ 1033889facbeSLen Brown } 1034889facbeSLen Brown 1035ba3dec99SLen Brown int 1036c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1037103a8feaSLen Brown { 1038388e9c81SLen Brown int i; 1039388e9c81SLen Brown struct msr_counter *mp; 10400b2bb692SLen Brown 10410b2bb692SLen Brown if (do_skl_residency) { 10420b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 10430b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 10440b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 10450b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 10460b2bb692SLen Brown } 1047c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 10480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1049c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 10500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1051c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 10520f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1053c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1054ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1055ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1056ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1057889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1058889facbeSLen Brown 10599185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 10609185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 10619185e988SLen Brown old->gfx_rc6_ms = -1; 10629185e988SLen Brown else 1063fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 10649185e988SLen Brown 106527d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 106627d47356SLen Brown 1067889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 1068889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 1069889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 1070889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 1071889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 1072889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 1073ba3dec99SLen Brown 1074388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1075388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1076388e9c81SLen Brown old->counter[i] = new->counter[i]; 1077388e9c81SLen Brown else 1078388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1079388e9c81SLen Brown } 1080388e9c81SLen Brown 1081ba3dec99SLen Brown return 0; 1082103a8feaSLen Brown } 1083103a8feaSLen Brown 1084c98d5d94SLen Brown void 1085c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1086c98d5d94SLen Brown { 1087388e9c81SLen Brown int i; 1088388e9c81SLen Brown struct msr_counter *mp; 1089388e9c81SLen Brown 1090c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1091c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1092c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1093889facbeSLen Brown old->core_temp_c = new->core_temp_c; 10940539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1095388e9c81SLen Brown 1096388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1097388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1098388e9c81SLen Brown old->counter[i] = new->counter[i]; 1099388e9c81SLen Brown else 1100388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1101388e9c81SLen Brown } 1102103a8feaSLen Brown } 1103c98d5d94SLen Brown 1104c3ae331dSLen Brown /* 1105c3ae331dSLen Brown * old = new - old 1106c3ae331dSLen Brown */ 1107ba3dec99SLen Brown int 1108c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1109c98d5d94SLen Brown struct core_data *core_delta) 1110c98d5d94SLen Brown { 1111388e9c81SLen Brown int i; 1112388e9c81SLen Brown struct msr_counter *mp; 1113388e9c81SLen Brown 1114c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1115c98d5d94SLen Brown 1116103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1117b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1118b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1119b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1120b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1121103a8feaSLen Brown 1122c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1123c98d5d94SLen Brown 1124812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 1125c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1126c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1127c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1128c98d5d94SLen Brown } else { 1129ba3dec99SLen Brown return -1; 1130103a8feaSLen Brown } 1131a729617cSLen Brown } 1132103a8feaSLen Brown 1133c98d5d94SLen Brown 1134144b44b1SLen Brown if (use_c1_residency_msr) { 1135144b44b1SLen Brown /* 1136144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1137144b44b1SLen Brown * which should be more accurate than the derivation below. 1138144b44b1SLen Brown */ 1139144b44b1SLen Brown } else { 1140103a8feaSLen Brown /* 1141c3ae331dSLen Brown * As counter collection is not atomic, 1142c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1143103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1144103a8feaSLen Brown */ 114595149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1146c98d5d94SLen Brown old->c1 = 0; 1147c98d5d94SLen Brown else { 1148c98d5d94SLen Brown /* normal case, derive c1 */ 1149008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1150c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1151c98d5d94SLen Brown } 1152144b44b1SLen Brown } 1153c3ae331dSLen Brown 1154c98d5d94SLen Brown if (old->mperf == 0) { 1155b7d8c148SLen Brown if (debug > 1) 1156b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1157c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1158c98d5d94SLen Brown } 1159103a8feaSLen Brown 1160812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1161562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1162562a2d37SLen Brown 1163812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 11641ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1165ba3dec99SLen Brown 1166388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1167388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1168388e9c81SLen Brown old->counter[i] = new->counter[i]; 1169388e9c81SLen Brown else 1170388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1171388e9c81SLen Brown } 1172ba3dec99SLen Brown return 0; 1173103a8feaSLen Brown } 1174c98d5d94SLen Brown 1175c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1176c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1177c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1178c98d5d94SLen Brown { 1179ba3dec99SLen Brown int retval = 0; 1180ba3dec99SLen Brown 1181c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1182c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1183c98d5d94SLen Brown delta_core(c, c2); 1184c98d5d94SLen Brown 1185c98d5d94SLen Brown /* always calculate thread delta */ 1186ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1187ba3dec99SLen Brown if (retval) 1188ba3dec99SLen Brown return retval; 1189c98d5d94SLen Brown 1190c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1191c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1192ba3dec99SLen Brown retval = delta_package(p, p2); 1193c98d5d94SLen Brown 1194ba3dec99SLen Brown return retval; 1195103a8feaSLen Brown } 1196103a8feaSLen Brown 1197c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1198103a8feaSLen Brown { 1199388e9c81SLen Brown int i; 1200388e9c81SLen Brown struct msr_counter *mp; 1201388e9c81SLen Brown 1202c98d5d94SLen Brown t->tsc = 0; 1203c98d5d94SLen Brown t->aperf = 0; 1204c98d5d94SLen Brown t->mperf = 0; 1205c98d5d94SLen Brown t->c1 = 0; 1206103a8feaSLen Brown 1207562a2d37SLen Brown t->irq_count = 0; 1208562a2d37SLen Brown t->smi_count = 0; 1209562a2d37SLen Brown 1210c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1211c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1212c98d5d94SLen Brown 1213c98d5d94SLen Brown c->c3 = 0; 1214c98d5d94SLen Brown c->c6 = 0; 1215c98d5d94SLen Brown c->c7 = 0; 12160539ba11SLen Brown c->mc6_us = 0; 1217889facbeSLen Brown c->core_temp_c = 0; 1218c98d5d94SLen Brown 12190b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 12200b2bb692SLen Brown p->pkg_any_core_c0 = 0; 12210b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 12220b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 12230b2bb692SLen Brown 1224c98d5d94SLen Brown p->pc2 = 0; 12250f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1226c98d5d94SLen Brown p->pc3 = 0; 12270f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1228c98d5d94SLen Brown p->pc6 = 0; 12290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1230c98d5d94SLen Brown p->pc7 = 0; 1231ca58710fSKristen Carlson Accardi p->pc8 = 0; 1232ca58710fSKristen Carlson Accardi p->pc9 = 0; 1233ca58710fSKristen Carlson Accardi p->pc10 = 0; 1234889facbeSLen Brown 1235889facbeSLen Brown p->energy_pkg = 0; 1236889facbeSLen Brown p->energy_dram = 0; 1237889facbeSLen Brown p->energy_cores = 0; 1238889facbeSLen Brown p->energy_gfx = 0; 1239889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1240889facbeSLen Brown p->rapl_dram_perf_status = 0; 1241889facbeSLen Brown p->pkg_temp_c = 0; 124227d47356SLen Brown 1243fdf676e5SLen Brown p->gfx_rc6_ms = 0; 124427d47356SLen Brown p->gfx_mhz = 0; 1245388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1246388e9c81SLen Brown t->counter[i] = 0; 1247388e9c81SLen Brown 1248388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1249388e9c81SLen Brown c->counter[i] = 0; 1250388e9c81SLen Brown 1251388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1252388e9c81SLen Brown p->counter[i] = 0; 1253103a8feaSLen Brown } 1254c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1255c98d5d94SLen Brown struct pkg_data *p) 1256103a8feaSLen Brown { 1257388e9c81SLen Brown int i; 1258388e9c81SLen Brown struct msr_counter *mp; 1259388e9c81SLen Brown 1260c98d5d94SLen Brown average.threads.tsc += t->tsc; 1261c98d5d94SLen Brown average.threads.aperf += t->aperf; 1262c98d5d94SLen Brown average.threads.mperf += t->mperf; 1263c98d5d94SLen Brown average.threads.c1 += t->c1; 126415aaa346SLen Brown 1265562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1266562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1267562a2d37SLen Brown 1268388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1269388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1270388e9c81SLen Brown continue; 1271388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1272388e9c81SLen Brown } 1273388e9c81SLen Brown 1274c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1275c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1276c98d5d94SLen Brown return 0; 1277c98d5d94SLen Brown 1278c98d5d94SLen Brown average.cores.c3 += c->c3; 1279c98d5d94SLen Brown average.cores.c6 += c->c6; 1280c98d5d94SLen Brown average.cores.c7 += c->c7; 12810539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1282c98d5d94SLen Brown 1283889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1284889facbeSLen Brown 1285388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1286388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1287388e9c81SLen Brown continue; 1288388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1289388e9c81SLen Brown } 1290388e9c81SLen Brown 1291c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1292c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1293c98d5d94SLen Brown return 0; 1294c98d5d94SLen Brown 12950b2bb692SLen Brown if (do_skl_residency) { 12960b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 12970b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 12980b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 12990b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 13000b2bb692SLen Brown } 13010b2bb692SLen Brown 1302c98d5d94SLen Brown average.packages.pc2 += p->pc2; 13030f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1304c98d5d94SLen Brown average.packages.pc3 += p->pc3; 13050f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1306c98d5d94SLen Brown average.packages.pc6 += p->pc6; 13070f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1308c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1309ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1310ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1311ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1312c98d5d94SLen Brown 1313889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1314889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1315889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1316889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1317889facbeSLen Brown 1318fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 131927d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 132027d47356SLen Brown 1321889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1322889facbeSLen Brown 1323889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1324889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1325388e9c81SLen Brown 1326388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1327388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1328388e9c81SLen Brown continue; 1329388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1330388e9c81SLen Brown } 1331c98d5d94SLen Brown return 0; 1332c98d5d94SLen Brown } 1333c98d5d94SLen Brown /* 1334c98d5d94SLen Brown * sum the counters for all cpus in the system 1335c98d5d94SLen Brown * compute the weighted average 1336c98d5d94SLen Brown */ 1337c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1338c98d5d94SLen Brown struct pkg_data *p) 1339c98d5d94SLen Brown { 1340388e9c81SLen Brown int i; 1341388e9c81SLen Brown struct msr_counter *mp; 1342388e9c81SLen Brown 1343c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1344c98d5d94SLen Brown 1345c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1346c98d5d94SLen Brown 1347c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1348c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1349c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1350c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1351c98d5d94SLen Brown 13520de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 13530de6c0dfSLen Brown sums_need_wide_columns = 1; 13540de6c0dfSLen Brown 1355c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1356c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1357c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 13580539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1359c98d5d94SLen Brown 13600b2bb692SLen Brown if (do_skl_residency) { 13610b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 13620b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 13630b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 13640b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 13650b2bb692SLen Brown } 13660b2bb692SLen Brown 1367c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 13680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1369c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 13700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1371c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 13720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1373c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1374ca58710fSKristen Carlson Accardi 1375ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1376ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1377ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1378388e9c81SLen Brown 1379388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1380388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1381388e9c81SLen Brown continue; 13820de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 13830de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 13840de6c0dfSLen Brown sums_need_wide_columns = 1; 138541618e63SLen Brown continue; 13860de6c0dfSLen Brown } 1387388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1388388e9c81SLen Brown } 1389388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1390388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1391388e9c81SLen Brown continue; 13920de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 13930de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 13940de6c0dfSLen Brown sums_need_wide_columns = 1; 13950de6c0dfSLen Brown } 1396388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1397388e9c81SLen Brown } 1398388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1399388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1400388e9c81SLen Brown continue; 14010de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 14020de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 14030de6c0dfSLen Brown sums_need_wide_columns = 1; 14040de6c0dfSLen Brown } 1405388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1406388e9c81SLen Brown } 1407c98d5d94SLen Brown } 1408c98d5d94SLen Brown 1409c98d5d94SLen Brown static unsigned long long rdtsc(void) 1410c98d5d94SLen Brown { 1411c98d5d94SLen Brown unsigned int low, high; 1412c98d5d94SLen Brown 1413c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1414c98d5d94SLen Brown 1415c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1416c98d5d94SLen Brown } 1417c98d5d94SLen Brown 1418c98d5d94SLen Brown /* 1419495c7654SLen Brown * Open a file, and exit on failure 1420495c7654SLen Brown */ 1421495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1422495c7654SLen Brown { 1423495c7654SLen Brown FILE *filep = fopen(path, mode); 1424495c7654SLen Brown 1425495c7654SLen Brown if (!filep) 1426495c7654SLen Brown err(1, "%s: open failed", path); 1427495c7654SLen Brown return filep; 1428495c7654SLen Brown } 1429495c7654SLen Brown /* 1430495c7654SLen Brown * snapshot_sysfs_counter() 1431495c7654SLen Brown * 1432495c7654SLen Brown * return snapshot of given counter 1433495c7654SLen Brown */ 1434495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1435495c7654SLen Brown { 1436495c7654SLen Brown FILE *fp; 1437495c7654SLen Brown int retval; 1438495c7654SLen Brown unsigned long long counter; 1439495c7654SLen Brown 1440495c7654SLen Brown fp = fopen_or_die(path, "r"); 1441495c7654SLen Brown 1442495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1443495c7654SLen Brown if (retval != 1) 1444495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1445495c7654SLen Brown 1446495c7654SLen Brown fclose(fp); 1447495c7654SLen Brown 1448495c7654SLen Brown return counter; 1449495c7654SLen Brown } 1450495c7654SLen Brown 1451495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1452495c7654SLen Brown { 1453495c7654SLen Brown if (mp->msr_num != 0) { 1454495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1455495c7654SLen Brown return -1; 1456495c7654SLen Brown } else { 145741618e63SLen Brown char path[128]; 145841618e63SLen Brown 145941618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 146041618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 146141618e63SLen Brown cpu, mp->path); 146241618e63SLen Brown 146341618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 146441618e63SLen Brown } else { 1465495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1466495c7654SLen Brown } 146741618e63SLen Brown } 1468495c7654SLen Brown 1469495c7654SLen Brown return 0; 1470495c7654SLen Brown } 1471495c7654SLen Brown 1472495c7654SLen Brown /* 1473c98d5d94SLen Brown * get_counters(...) 1474c98d5d94SLen Brown * migrate to cpu 1475c98d5d94SLen Brown * acquire and record local counters for that cpu 1476c98d5d94SLen Brown */ 1477c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1478c98d5d94SLen Brown { 1479c98d5d94SLen Brown int cpu = t->cpu_id; 1480889facbeSLen Brown unsigned long long msr; 14810102b067SLen Brown int aperf_mperf_retry_count = 0; 1482388e9c81SLen Brown struct msr_counter *mp; 1483388e9c81SLen Brown int i; 1484c98d5d94SLen Brown 1485e52966c0SLen Brown if (cpu_migrate(cpu)) { 1486b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 148715aaa346SLen Brown return -1; 1488e52966c0SLen Brown } 148915aaa346SLen Brown 14900102b067SLen Brown retry: 1491c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 149215aaa346SLen Brown 1493812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 14940102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 14950102b067SLen Brown 14960102b067SLen Brown /* 14970102b067SLen Brown * The TSC, APERF and MPERF must be read together for 14980102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 14990102b067SLen Brown * 15000102b067SLen Brown * Unfortunately, APERF and MPERF are read by 15010102b067SLen Brown * individual system call, so delays may occur 15020102b067SLen Brown * between them. If the time to read them 15030102b067SLen Brown * varies by a large amount, we re-read them. 15040102b067SLen Brown */ 15050102b067SLen Brown 15060102b067SLen Brown /* 15070102b067SLen Brown * This initial dummy APERF read has been seen to 15080102b067SLen Brown * reduce jitter in the subsequent reads. 15090102b067SLen Brown */ 15100102b067SLen Brown 15119c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1512c98d5d94SLen Brown return -3; 15130102b067SLen Brown 15140102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 15150102b067SLen Brown 15160102b067SLen Brown tsc_before = t->tsc; 15170102b067SLen Brown 15180102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 15190102b067SLen Brown return -3; 15200102b067SLen Brown 15210102b067SLen Brown tsc_between = rdtsc(); 15220102b067SLen Brown 15239c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1524c98d5d94SLen Brown return -4; 15250102b067SLen Brown 15260102b067SLen Brown tsc_after = rdtsc(); 15270102b067SLen Brown 15280102b067SLen Brown aperf_time = tsc_between - tsc_before; 15290102b067SLen Brown mperf_time = tsc_after - tsc_between; 15300102b067SLen Brown 15310102b067SLen Brown /* 15320102b067SLen Brown * If the system call latency to read APERF and MPERF 15330102b067SLen Brown * differ by more than 2x, then try again. 15340102b067SLen Brown */ 15350102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 15360102b067SLen Brown aperf_mperf_retry_count++; 15370102b067SLen Brown if (aperf_mperf_retry_count < 5) 15380102b067SLen Brown goto retry; 15390102b067SLen Brown else 15400102b067SLen Brown warnx("cpu%d jitter %lld %lld", 15410102b067SLen Brown cpu, aperf_time, mperf_time); 15420102b067SLen Brown } 15430102b067SLen Brown aperf_mperf_retry_count = 0; 15440102b067SLen Brown 1545b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1546b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 154788c3281fSLen Brown } 154888c3281fSLen Brown 1549812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1550562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1551812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 15521ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 15531ed51011SLen Brown return -5; 15541ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 15551ed51011SLen Brown } 15560539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1557144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1558144b44b1SLen Brown return -6; 1559144b44b1SLen Brown } 1560144b44b1SLen Brown 1561388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1562495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1563388e9c81SLen Brown return -10; 1564388e9c81SLen Brown } 1565388e9c81SLen Brown 1566c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1567c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1568c98d5d94SLen Brown return 0; 1569c98d5d94SLen Brown 1570812db3f7SLen Brown if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) { 1571c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1572c98d5d94SLen Brown return -6; 1573144b44b1SLen Brown } 1574144b44b1SLen Brown 1575812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) { 1576c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1577c98d5d94SLen Brown return -7; 1578fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1579fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1580fb5d4327SDasaratharaman Chandramouli return -7; 1581103a8feaSLen Brown } 158215aaa346SLen Brown 1583812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 1584c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1585c98d5d94SLen Brown return -8; 1586c98d5d94SLen Brown 15870539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 15880539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 15890539ba11SLen Brown return -8; 15900539ba11SLen Brown 1591812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 1592889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1593889facbeSLen Brown return -9; 1594889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1595889facbeSLen Brown } 1596889facbeSLen Brown 1597388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1598495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 1599388e9c81SLen Brown return -10; 1600388e9c81SLen Brown } 1601889facbeSLen Brown 1602c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1603c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1604c98d5d94SLen Brown return 0; 160515aaa346SLen Brown 16060b2bb692SLen Brown if (do_skl_residency) { 16070b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 16080b2bb692SLen Brown return -10; 16090b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 16100b2bb692SLen Brown return -11; 16110b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 16120b2bb692SLen Brown return -12; 16130b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 16140b2bb692SLen Brown return -13; 16150b2bb692SLen Brown } 16160f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1617c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1618c98d5d94SLen Brown return -9; 16190f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 16200539ba11SLen Brown if (do_slm_cstates) { 16210539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 16220539ba11SLen Brown return -10; 16230539ba11SLen Brown } else { 1624c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1625c98d5d94SLen Brown return -10; 16260539ba11SLen Brown } 16270539ba11SLen Brown } 16280539ba11SLen Brown 16290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 1630c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1631c98d5d94SLen Brown return -11; 16320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1633c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1634c98d5d94SLen Brown return -12; 16350f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 1636ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1637ca58710fSKristen Carlson Accardi return -13; 16380f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 1639ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1640ca58710fSKristen Carlson Accardi return -13; 16410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 1642ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1643ca58710fSKristen Carlson Accardi return -13; 16440f47c08dSLen Brown 1645889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1646889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1647889facbeSLen Brown return -13; 1648889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1649889facbeSLen Brown } 16509148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 1651889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1652889facbeSLen Brown return -14; 1653889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1654889facbeSLen Brown } 1655889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1656889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1657889facbeSLen Brown return -15; 1658889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1659889facbeSLen Brown } 1660889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1661889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1662889facbeSLen Brown return -16; 1663889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1664889facbeSLen Brown } 1665889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1666889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1667889facbeSLen Brown return -16; 1668889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1669889facbeSLen Brown } 1670889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1671889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1672889facbeSLen Brown return -16; 1673889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1674889facbeSLen Brown } 1675812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 1676889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1677889facbeSLen Brown return -17; 1678889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1679889facbeSLen Brown } 1680fdf676e5SLen Brown 1681812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 1682fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 1683fdf676e5SLen Brown 1684812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 168527d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 168627d47356SLen Brown 1687388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1688495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 1689388e9c81SLen Brown return -10; 1690388e9c81SLen Brown } 1691388e9c81SLen Brown 169215aaa346SLen Brown return 0; 1693103a8feaSLen Brown } 1694103a8feaSLen Brown 1695ee7e38e3SLen Brown /* 1696ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1697ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1698ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1699ee7e38e3SLen Brown */ 1700ee7e38e3SLen Brown 1701ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1702ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1703ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1704ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1705ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1706ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1707ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1708ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1709ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1710ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1711ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1712ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 17130b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 17140b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 17150b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1716ee7e38e3SLen Brown 1717ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1718ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 17190b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1720ee7e38e3SLen Brown 1721e9257f5fSLen 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}; 1722e9257f5fSLen 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}; 1723e9257f5fSLen 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}; 17240539ba11SLen 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}; 1725f2642888SLen 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}; 1726e9257f5fSLen 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}; 1727e4085d54SLen Brown int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 1728d8ebb442SLen Brown int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 1729ee7e38e3SLen Brown 1730a2b7b749SLen Brown 1731a2b7b749SLen Brown static void 1732a2b7b749SLen Brown calculate_tsc_tweak() 1733a2b7b749SLen Brown { 1734a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 1735a2b7b749SLen Brown } 1736a2b7b749SLen Brown 1737fcd17211SLen Brown static void 1738fcd17211SLen Brown dump_nhm_platform_info(void) 1739103a8feaSLen Brown { 1740103a8feaSLen Brown unsigned long long msr; 1741103a8feaSLen Brown unsigned int ratio; 1742103a8feaSLen Brown 1743ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 1744103a8feaSLen Brown 1745b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 17466574a5d5SLen Brown 1747103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1748710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 1749103a8feaSLen Brown ratio, bclk, ratio * bclk); 1750103a8feaSLen Brown 1751103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1752710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 1753103a8feaSLen Brown ratio, bclk, ratio * bclk); 1754103a8feaSLen Brown 17557ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1756b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1757bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 175867920418SLen Brown 1759fcd17211SLen Brown return; 1760fcd17211SLen Brown } 1761fcd17211SLen Brown 1762fcd17211SLen Brown static void 1763fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 1764fcd17211SLen Brown { 1765fcd17211SLen Brown unsigned long long msr; 1766fcd17211SLen Brown unsigned int ratio; 1767fcd17211SLen Brown 17687ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 1769fcd17211SLen Brown 1770b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 1771fcd17211SLen Brown 1772fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 1773fcd17211SLen Brown if (ratio) 1774710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 1775fcd17211SLen Brown ratio, bclk, ratio * bclk); 1776fcd17211SLen Brown 1777fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 1778fcd17211SLen Brown if (ratio) 1779710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 1780fcd17211SLen Brown ratio, bclk, ratio * bclk); 1781fcd17211SLen Brown return; 1782fcd17211SLen Brown } 1783fcd17211SLen Brown 1784fcd17211SLen Brown static void 1785fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 1786fcd17211SLen Brown { 1787fcd17211SLen Brown unsigned long long msr; 1788fcd17211SLen Brown unsigned int ratio; 17896574a5d5SLen Brown 17907ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 17916574a5d5SLen Brown 1792b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 17936574a5d5SLen Brown 17946574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 17956574a5d5SLen Brown if (ratio) 1796710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 17976574a5d5SLen Brown ratio, bclk, ratio * bclk); 17986574a5d5SLen Brown 17996574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 18006574a5d5SLen Brown if (ratio) 1801710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 18026574a5d5SLen Brown ratio, bclk, ratio * bclk); 18036574a5d5SLen Brown 18046574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 18056574a5d5SLen Brown if (ratio) 1806710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 18076574a5d5SLen Brown ratio, bclk, ratio * bclk); 18086574a5d5SLen Brown 18096574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 18106574a5d5SLen Brown if (ratio) 1811710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 18126574a5d5SLen Brown ratio, bclk, ratio * bclk); 18136574a5d5SLen Brown 18146574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 18156574a5d5SLen Brown if (ratio) 1816710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 18176574a5d5SLen Brown ratio, bclk, ratio * bclk); 18186574a5d5SLen Brown 18196574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 18206574a5d5SLen Brown if (ratio) 1821710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 18226574a5d5SLen Brown ratio, bclk, ratio * bclk); 18236574a5d5SLen Brown 18246574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 18256574a5d5SLen Brown if (ratio) 1826710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 18276574a5d5SLen Brown ratio, bclk, ratio * bclk); 18286574a5d5SLen Brown 18296574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 18306574a5d5SLen Brown if (ratio) 1831710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 18326574a5d5SLen Brown ratio, bclk, ratio * bclk); 1833103a8feaSLen Brown return; 1834fcd17211SLen Brown } 183531e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 183631e07522SLen Brown { 183731e07522SLen Brown 183831e07522SLen Brown if (!genuine_intel) 183931e07522SLen Brown return 0; 184031e07522SLen Brown 184131e07522SLen Brown switch (model) { 184231e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 184331e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 184431e07522SLen Brown case INTEL_FAM6_ATOM_DENVERTON: 184531e07522SLen Brown return 1; 184631e07522SLen Brown } 184731e07522SLen Brown return 0; 184831e07522SLen Brown } 1849fcd17211SLen Brown 1850fcd17211SLen Brown static void 185131e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 1852fcd17211SLen Brown { 185331e07522SLen Brown unsigned long long msr, core_counts; 185431e07522SLen Brown unsigned int ratio, group_size; 1855103a8feaSLen Brown 18567ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1857b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 18586574a5d5SLen Brown 185931e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 186031e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 186131e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 186231e07522SLen Brown } else { 186331e07522SLen Brown core_counts = 0x0807060504030201; 186431e07522SLen Brown } 186531e07522SLen Brown 18666574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 186731e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 18686574a5d5SLen Brown if (ratio) 186931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 187031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 18716574a5d5SLen Brown 18726574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 187331e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 18746574a5d5SLen Brown if (ratio) 187531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 187631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 18776574a5d5SLen Brown 18786574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 187931e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 18806574a5d5SLen Brown if (ratio) 188131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 188231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 18836574a5d5SLen Brown 18846574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 188531e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 18866574a5d5SLen Brown if (ratio) 188731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 188831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 18896574a5d5SLen Brown 1890103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 189131e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 1892103a8feaSLen Brown if (ratio) 189331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 189431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1895103a8feaSLen Brown 1896103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 189731e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 1898103a8feaSLen Brown if (ratio) 189931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 190031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1901103a8feaSLen Brown 1902103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 190331e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 1904103a8feaSLen Brown if (ratio) 190531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 190631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1907103a8feaSLen Brown 1908103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 190931e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 1910103a8feaSLen Brown if (ratio) 191131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 191231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1913fcd17211SLen Brown return; 1914fcd17211SLen Brown } 19153a9a941dSLen Brown 1916fcd17211SLen Brown static void 19170f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 19180f7887c4SLen Brown { 19190f7887c4SLen Brown unsigned long long msr; 19200f7887c4SLen Brown unsigned int ratio; 19210f7887c4SLen Brown 19220f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 19230f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 19240f7887c4SLen Brown 19250f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 19260f7887c4SLen Brown if (ratio) 19270f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 19280f7887c4SLen Brown ratio, bclk, ratio * bclk); 19290f7887c4SLen Brown 19300f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 19310f7887c4SLen Brown if (ratio) 19320f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 19330f7887c4SLen Brown ratio, bclk, ratio * bclk); 19340f7887c4SLen Brown 19350f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 19360f7887c4SLen Brown if (ratio) 19370f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 19380f7887c4SLen Brown ratio, bclk, ratio * bclk); 19390f7887c4SLen Brown 19400f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 19410f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 19420f7887c4SLen Brown 19430f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 19440f7887c4SLen Brown if (ratio) 19450f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 19460f7887c4SLen Brown ratio, bclk, ratio * bclk); 19470f7887c4SLen Brown 19480f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 19490f7887c4SLen Brown if (ratio) 19500f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 19510f7887c4SLen Brown ratio, bclk, ratio * bclk); 19520f7887c4SLen Brown 19530f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 19540f7887c4SLen Brown if (ratio) 19550f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 19560f7887c4SLen Brown ratio, bclk, ratio * bclk); 19570f7887c4SLen Brown 19580f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 19590f7887c4SLen Brown if (ratio) 19600f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 19610f7887c4SLen Brown ratio, bclk, ratio * bclk); 19620f7887c4SLen Brown } 19630f7887c4SLen Brown 19640f7887c4SLen Brown static void 1965fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 1966fb5d4327SDasaratharaman Chandramouli { 1967cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 1968cbf97abaSHubert Chrzaniuk 1969fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 1970cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 1971cbf97abaSHubert Chrzaniuk int i, b_nr; 1972cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 1973cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 1974fb5d4327SDasaratharaman Chandramouli 1975ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1976fb5d4327SDasaratharaman Chandramouli 1977b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 1978bfae2052SLen Brown base_cpu, msr); 1979fb5d4327SDasaratharaman Chandramouli 1980fb5d4327SDasaratharaman Chandramouli /** 1981fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 1982cbf97abaSHubert Chrzaniuk * [0] -- Reserved 1983cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 1984fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 1985fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 1986fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 1987fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 1988fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 1989fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 1990fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 1991fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 1992fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 1993fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 1994fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 1995fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 1996fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 1997fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 1998fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 1999fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2000fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2001fb5d4327SDasaratharaman Chandramouli */ 2002fb5d4327SDasaratharaman Chandramouli 2003cbf97abaSHubert Chrzaniuk b_nr = 0; 2004cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2005cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2006cbf97abaSHubert Chrzaniuk 2007cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2008fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2009cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2010fb5d4327SDasaratharaman Chandramouli 2011cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2012cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2013cbf97abaSHubert Chrzaniuk b_nr++; 2014fb5d4327SDasaratharaman Chandramouli } 2015cbf97abaSHubert Chrzaniuk 2016cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2017cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2018b7d8c148SLen Brown fprintf(outf, 2019710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2020cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2021fb5d4327SDasaratharaman Chandramouli } 2022fb5d4327SDasaratharaman Chandramouli 2023fb5d4327SDasaratharaman Chandramouli static void 2024fcd17211SLen Brown dump_nhm_cst_cfg(void) 2025fcd17211SLen Brown { 2026fcd17211SLen Brown unsigned long long msr; 2027fcd17211SLen Brown 20281df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2029fcd17211SLen Brown 2030fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 2031fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 2032fcd17211SLen Brown 20331df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2034fcd17211SLen Brown 2035b7d8c148SLen Brown fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", 2036fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2037fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2038fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2039fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2040fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 20416c34f160SLen Brown (unsigned int)msr & 0xF, 2042fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2043fcd17211SLen Brown return; 2044103a8feaSLen Brown } 2045103a8feaSLen Brown 20466fb3143bSLen Brown static void 20476fb3143bSLen Brown dump_config_tdp(void) 20486fb3143bSLen Brown { 20496fb3143bSLen Brown unsigned long long msr; 20506fb3143bSLen Brown 20516fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2052b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2053685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 20546fb3143bSLen Brown 20556fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2056b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 20576fb3143bSLen Brown if (msr) { 2058685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2059685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2060685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2061685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 20626fb3143bSLen Brown } 2063b7d8c148SLen Brown fprintf(outf, ")\n"); 20646fb3143bSLen Brown 20656fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2066b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 20676fb3143bSLen Brown if (msr) { 2068685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2069685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2070685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2071685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 20726fb3143bSLen Brown } 2073b7d8c148SLen Brown fprintf(outf, ")\n"); 20746fb3143bSLen Brown 20756fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2076b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 20776fb3143bSLen Brown if ((msr) & 0x3) 2078b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2079b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2080b7d8c148SLen Brown fprintf(outf, ")\n"); 20816fb3143bSLen Brown 20826fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2083b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2084685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2085b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2086b7d8c148SLen Brown fprintf(outf, ")\n"); 20876fb3143bSLen Brown } 20885a63426eSLen Brown 20895a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 20905a63426eSLen Brown 20915a63426eSLen Brown void print_irtl(void) 20925a63426eSLen Brown { 20935a63426eSLen Brown unsigned long long msr; 20945a63426eSLen Brown 20955a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 20965a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 20975a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 20985a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 20995a63426eSLen Brown 21005a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 21015a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 21025a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 21035a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 21045a63426eSLen Brown 21055a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 21065a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 21075a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 21085a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 21095a63426eSLen Brown 21105a63426eSLen Brown if (!do_irtl_hsw) 21115a63426eSLen Brown return; 21125a63426eSLen Brown 21135a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 21145a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 21155a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 21165a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 21175a63426eSLen Brown 21185a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 21195a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 21205a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 21215a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 21225a63426eSLen Brown 21235a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 21245a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 21255a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 21265a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 21275a63426eSLen Brown 21285a63426eSLen Brown } 212936229897SLen Brown void free_fd_percpu(void) 213036229897SLen Brown { 213136229897SLen Brown int i; 213236229897SLen Brown 213301a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 213436229897SLen Brown if (fd_percpu[i] != 0) 213536229897SLen Brown close(fd_percpu[i]); 213636229897SLen Brown } 213736229897SLen Brown 213836229897SLen Brown free(fd_percpu); 21396fb3143bSLen Brown } 21406fb3143bSLen Brown 2141c98d5d94SLen Brown void free_all_buffers(void) 2142103a8feaSLen Brown { 2143c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2144c98d5d94SLen Brown cpu_present_set = NULL; 214536229897SLen Brown cpu_present_setsize = 0; 2146103a8feaSLen Brown 2147c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2148c98d5d94SLen Brown cpu_affinity_set = NULL; 2149c98d5d94SLen Brown cpu_affinity_setsize = 0; 2150103a8feaSLen Brown 2151c98d5d94SLen Brown free(thread_even); 2152c98d5d94SLen Brown free(core_even); 2153c98d5d94SLen Brown free(package_even); 2154103a8feaSLen Brown 2155c98d5d94SLen Brown thread_even = NULL; 2156c98d5d94SLen Brown core_even = NULL; 2157c98d5d94SLen Brown package_even = NULL; 2158103a8feaSLen Brown 2159c98d5d94SLen Brown free(thread_odd); 2160c98d5d94SLen Brown free(core_odd); 2161c98d5d94SLen Brown free(package_odd); 2162103a8feaSLen Brown 2163c98d5d94SLen Brown thread_odd = NULL; 2164c98d5d94SLen Brown core_odd = NULL; 2165c98d5d94SLen Brown package_odd = NULL; 2166103a8feaSLen Brown 2167c98d5d94SLen Brown free(output_buffer); 2168c98d5d94SLen Brown output_buffer = NULL; 2169c98d5d94SLen Brown outp = NULL; 217036229897SLen Brown 217136229897SLen Brown free_fd_percpu(); 2172562a2d37SLen Brown 2173562a2d37SLen Brown free(irq_column_2_cpu); 2174562a2d37SLen Brown free(irqs_per_cpu); 2175103a8feaSLen Brown } 2176103a8feaSLen Brown 217757a42a34SJosh Triplett 217857a42a34SJosh Triplett /* 217995aebc44SJosh Triplett * Parse a file containing a single int. 218095aebc44SJosh Triplett */ 218195aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 218295aebc44SJosh Triplett { 218395aebc44SJosh Triplett va_list args; 218495aebc44SJosh Triplett char path[PATH_MAX]; 218595aebc44SJosh Triplett FILE *filep; 218695aebc44SJosh Triplett int value; 218795aebc44SJosh Triplett 218895aebc44SJosh Triplett va_start(args, fmt); 218995aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 219095aebc44SJosh Triplett va_end(args); 219157a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 2192b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2193b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 219495aebc44SJosh Triplett fclose(filep); 219595aebc44SJosh Triplett return value; 219695aebc44SJosh Triplett } 219795aebc44SJosh Triplett 219895aebc44SJosh Triplett /* 2199e275b388SDasaratharaman Chandramouli * get_cpu_position_in_core(cpu) 2200e275b388SDasaratharaman Chandramouli * return the position of the CPU among its HT siblings in the core 2201e275b388SDasaratharaman Chandramouli * return -1 if the sibling is not in list 2202103a8feaSLen Brown */ 2203e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu) 2204103a8feaSLen Brown { 2205e275b388SDasaratharaman Chandramouli char path[64]; 2206e275b388SDasaratharaman Chandramouli FILE *filep; 2207e275b388SDasaratharaman Chandramouli int this_cpu; 2208e275b388SDasaratharaman Chandramouli char character; 2209e275b388SDasaratharaman Chandramouli int i; 2210e275b388SDasaratharaman Chandramouli 2211e275b388SDasaratharaman Chandramouli sprintf(path, 2212e275b388SDasaratharaman Chandramouli "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", 2213e275b388SDasaratharaman Chandramouli cpu); 2214e275b388SDasaratharaman Chandramouli filep = fopen(path, "r"); 2215e275b388SDasaratharaman Chandramouli if (filep == NULL) { 2216e275b388SDasaratharaman Chandramouli perror(path); 2217e275b388SDasaratharaman Chandramouli exit(1); 2218e275b388SDasaratharaman Chandramouli } 2219e275b388SDasaratharaman Chandramouli 2220e275b388SDasaratharaman Chandramouli for (i = 0; i < topo.num_threads_per_core; i++) { 2221e275b388SDasaratharaman Chandramouli fscanf(filep, "%d", &this_cpu); 2222e275b388SDasaratharaman Chandramouli if (this_cpu == cpu) { 2223e275b388SDasaratharaman Chandramouli fclose(filep); 2224e275b388SDasaratharaman Chandramouli return i; 2225e275b388SDasaratharaman Chandramouli } 2226e275b388SDasaratharaman Chandramouli 2227e275b388SDasaratharaman Chandramouli /* Account for no separator after last thread*/ 2228e275b388SDasaratharaman Chandramouli if (i != (topo.num_threads_per_core - 1)) 2229e275b388SDasaratharaman Chandramouli fscanf(filep, "%c", &character); 2230e275b388SDasaratharaman Chandramouli } 2231e275b388SDasaratharaman Chandramouli 2232e275b388SDasaratharaman Chandramouli fclose(filep); 2233e275b388SDasaratharaman Chandramouli return -1; 2234c98d5d94SLen Brown } 2235103a8feaSLen Brown 2236c98d5d94SLen Brown /* 2237c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2238c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2239c98d5d94SLen Brown */ 2240c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2241c98d5d94SLen Brown { 224295aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2243103a8feaSLen Brown } 2244103a8feaSLen Brown 2245103a8feaSLen Brown int get_physical_package_id(int cpu) 2246103a8feaSLen Brown { 224795aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2248103a8feaSLen Brown } 2249103a8feaSLen Brown 2250103a8feaSLen Brown int get_core_id(int cpu) 2251103a8feaSLen Brown { 225295aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2253103a8feaSLen Brown } 2254103a8feaSLen Brown 2255c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 2256c98d5d94SLen Brown { 2257c98d5d94SLen Brown char path[80]; 2258c98d5d94SLen Brown FILE *filep; 2259e275b388SDasaratharaman Chandramouli int sib1; 2260e275b388SDasaratharaman Chandramouli int matches = 0; 2261c98d5d94SLen Brown char character; 2262e275b388SDasaratharaman Chandramouli char str[100]; 2263e275b388SDasaratharaman Chandramouli char *ch; 2264c98d5d94SLen Brown 2265c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 226657a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 2267e275b388SDasaratharaman Chandramouli 2268103a8feaSLen Brown /* 2269c98d5d94SLen Brown * file format: 2270e275b388SDasaratharaman Chandramouli * A ',' separated or '-' separated set of numbers 2271e275b388SDasaratharaman Chandramouli * (eg 1-2 or 1,3,4,5) 2272c98d5d94SLen Brown */ 2273e275b388SDasaratharaman Chandramouli fscanf(filep, "%d%c\n", &sib1, &character); 2274e275b388SDasaratharaman Chandramouli fseek(filep, 0, SEEK_SET); 2275e275b388SDasaratharaman Chandramouli fgets(str, 100, filep); 2276e275b388SDasaratharaman Chandramouli ch = strchr(str, character); 2277e275b388SDasaratharaman Chandramouli while (ch != NULL) { 2278e275b388SDasaratharaman Chandramouli matches++; 2279e275b388SDasaratharaman Chandramouli ch = strchr(ch+1, character); 2280e275b388SDasaratharaman Chandramouli } 2281c98d5d94SLen Brown 2282c98d5d94SLen Brown fclose(filep); 2283e275b388SDasaratharaman Chandramouli return matches+1; 2284c98d5d94SLen Brown } 2285c98d5d94SLen Brown 2286c98d5d94SLen Brown /* 2287c98d5d94SLen Brown * run func(thread, core, package) in topology order 2288c98d5d94SLen Brown * skip non-present cpus 2289103a8feaSLen Brown */ 2290103a8feaSLen Brown 2291c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2292c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2293c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2294c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2295c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2296c98d5d94SLen Brown struct pkg_data *pkg_base2) 2297c98d5d94SLen Brown { 2298c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 2299c98d5d94SLen Brown 2300c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 2301c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 2302c98d5d94SLen Brown for (thread_no = 0; thread_no < 2303c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 2304c98d5d94SLen Brown struct thread_data *t, *t2; 2305c98d5d94SLen Brown struct core_data *c, *c2; 2306c98d5d94SLen Brown struct pkg_data *p, *p2; 2307c98d5d94SLen Brown 2308c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 2309c98d5d94SLen Brown 2310c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2311c98d5d94SLen Brown continue; 2312c98d5d94SLen Brown 2313c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 2314c98d5d94SLen Brown 2315c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 2316c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 2317c98d5d94SLen Brown 2318c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2319c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2320c98d5d94SLen Brown 2321c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2322c98d5d94SLen Brown if (retval) 2323c98d5d94SLen Brown return retval; 2324c98d5d94SLen Brown } 2325c98d5d94SLen Brown } 2326c98d5d94SLen Brown } 2327c98d5d94SLen Brown return 0; 2328c98d5d94SLen Brown } 2329c98d5d94SLen Brown 2330c98d5d94SLen Brown /* 2331c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2332c98d5d94SLen Brown * return max_cpu number 2333c98d5d94SLen Brown */ 2334c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2335103a8feaSLen Brown { 2336103a8feaSLen Brown FILE *fp; 2337c98d5d94SLen Brown int cpu_num; 2338103a8feaSLen Brown int retval; 2339103a8feaSLen Brown 234057a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2341103a8feaSLen Brown 2342103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2343b2c95d90SJosh Triplett if (retval != 0) 2344b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2345103a8feaSLen Brown 2346c98d5d94SLen Brown while (1) { 2347c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2348103a8feaSLen Brown if (retval != 1) 2349103a8feaSLen Brown break; 2350103a8feaSLen Brown 2351c98d5d94SLen Brown retval = func(cpu_num); 2352c98d5d94SLen Brown if (retval) { 2353c98d5d94SLen Brown fclose(fp); 2354c98d5d94SLen Brown return(retval); 2355c98d5d94SLen Brown } 2356103a8feaSLen Brown } 2357103a8feaSLen Brown fclose(fp); 2358c98d5d94SLen Brown return 0; 2359103a8feaSLen Brown } 2360103a8feaSLen Brown 2361103a8feaSLen Brown void re_initialize(void) 2362103a8feaSLen Brown { 2363c98d5d94SLen Brown free_all_buffers(); 2364c98d5d94SLen Brown setup_all_buffers(); 2365c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2366103a8feaSLen Brown } 2367103a8feaSLen Brown 2368c98d5d94SLen Brown 2369103a8feaSLen Brown /* 2370c98d5d94SLen Brown * count_cpus() 2371c98d5d94SLen Brown * remember the last one seen, it will be the max 2372103a8feaSLen Brown */ 2373c98d5d94SLen Brown int count_cpus(int cpu) 2374103a8feaSLen Brown { 2375c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 2376c98d5d94SLen Brown topo.max_cpu_num = cpu; 2377103a8feaSLen Brown 2378c98d5d94SLen Brown topo.num_cpus += 1; 2379c98d5d94SLen Brown return 0; 2380103a8feaSLen Brown } 2381c98d5d94SLen Brown int mark_cpu_present(int cpu) 2382c98d5d94SLen Brown { 2383c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 238415aaa346SLen Brown return 0; 2385103a8feaSLen Brown } 2386103a8feaSLen Brown 2387562a2d37SLen Brown /* 2388562a2d37SLen Brown * snapshot_proc_interrupts() 2389562a2d37SLen Brown * 2390562a2d37SLen Brown * read and record summary of /proc/interrupts 2391562a2d37SLen Brown * 2392562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2393562a2d37SLen Brown */ 2394562a2d37SLen Brown int snapshot_proc_interrupts(void) 2395562a2d37SLen Brown { 2396562a2d37SLen Brown static FILE *fp; 2397562a2d37SLen Brown int column, retval; 2398562a2d37SLen Brown 2399562a2d37SLen Brown if (fp == NULL) 2400562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2401562a2d37SLen Brown else 2402562a2d37SLen Brown rewind(fp); 2403562a2d37SLen Brown 2404562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2405562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2406562a2d37SLen Brown int cpu_number; 2407562a2d37SLen Brown 2408562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2409562a2d37SLen Brown if (retval != 1) 2410562a2d37SLen Brown break; 2411562a2d37SLen Brown 2412562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2413562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2414562a2d37SLen Brown return 1; 2415562a2d37SLen Brown } 2416562a2d37SLen Brown 2417562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2418562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2419562a2d37SLen Brown } 2420562a2d37SLen Brown 2421562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2422562a2d37SLen Brown while (1) { 2423562a2d37SLen Brown int column; 2424562a2d37SLen Brown char buf[64]; 2425562a2d37SLen Brown 2426562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2427562a2d37SLen Brown if (retval != 1) 2428562a2d37SLen Brown break; 2429562a2d37SLen Brown 2430562a2d37SLen Brown /* read the count per cpu */ 2431562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2432562a2d37SLen Brown 2433562a2d37SLen Brown int cpu_number, irq_count; 2434562a2d37SLen Brown 2435562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2436562a2d37SLen Brown if (retval != 1) 2437562a2d37SLen Brown break; 2438562a2d37SLen Brown 2439562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2440562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2441562a2d37SLen Brown 2442562a2d37SLen Brown } 2443562a2d37SLen Brown 2444562a2d37SLen Brown while (getc(fp) != '\n') 2445562a2d37SLen Brown ; /* flush interrupt description */ 2446562a2d37SLen Brown 2447562a2d37SLen Brown } 2448562a2d37SLen Brown return 0; 2449562a2d37SLen Brown } 245027d47356SLen Brown /* 2451fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2452fdf676e5SLen Brown * 2453fdf676e5SLen Brown * record snapshot of 2454fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2455fdf676e5SLen Brown * 2456fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2457fdf676e5SLen Brown */ 2458fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2459fdf676e5SLen Brown { 2460fdf676e5SLen Brown FILE *fp; 2461fdf676e5SLen Brown int retval; 2462fdf676e5SLen Brown 2463fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 2464fdf676e5SLen Brown 2465fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 2466fdf676e5SLen Brown if (retval != 1) 2467fdf676e5SLen Brown err(1, "GFX rc6"); 2468fdf676e5SLen Brown 2469fdf676e5SLen Brown fclose(fp); 2470fdf676e5SLen Brown 2471fdf676e5SLen Brown return 0; 2472fdf676e5SLen Brown } 2473fdf676e5SLen Brown /* 247427d47356SLen Brown * snapshot_gfx_mhz() 247527d47356SLen Brown * 247627d47356SLen Brown * record snapshot of 247727d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 247827d47356SLen Brown * 247927d47356SLen Brown * return 1 if config change requires a restart, else return 0 248027d47356SLen Brown */ 248127d47356SLen Brown int snapshot_gfx_mhz(void) 248227d47356SLen Brown { 248327d47356SLen Brown static FILE *fp; 248427d47356SLen Brown int retval; 248527d47356SLen Brown 248627d47356SLen Brown if (fp == NULL) 248727d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 248822048c54SLen Brown else { 248927d47356SLen Brown rewind(fp); 249022048c54SLen Brown fflush(fp); 249122048c54SLen Brown } 249227d47356SLen Brown 249327d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 249427d47356SLen Brown if (retval != 1) 249527d47356SLen Brown err(1, "GFX MHz"); 249627d47356SLen Brown 249727d47356SLen Brown return 0; 249827d47356SLen Brown } 2499562a2d37SLen Brown 2500562a2d37SLen Brown /* 2501562a2d37SLen Brown * snapshot /proc and /sys files 2502562a2d37SLen Brown * 2503562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 2504562a2d37SLen Brown */ 2505562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 2506562a2d37SLen Brown { 2507218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 2508562a2d37SLen Brown if (snapshot_proc_interrupts()) 2509562a2d37SLen Brown return 1; 2510562a2d37SLen Brown 2511812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2512fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 2513fdf676e5SLen Brown 2514812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 251527d47356SLen Brown snapshot_gfx_mhz(); 251627d47356SLen Brown 2517562a2d37SLen Brown return 0; 2518562a2d37SLen Brown } 2519562a2d37SLen Brown 2520103a8feaSLen Brown void turbostat_loop() 2521103a8feaSLen Brown { 2522c98d5d94SLen Brown int retval; 2523e52966c0SLen Brown int restarted = 0; 2524c98d5d94SLen Brown 2525103a8feaSLen Brown restart: 2526e52966c0SLen Brown restarted++; 2527e52966c0SLen Brown 2528562a2d37SLen Brown snapshot_proc_sysfs_files(); 2529c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2530d91bb17cSLen Brown if (retval < -1) { 2531d91bb17cSLen Brown exit(retval); 2532d91bb17cSLen Brown } else if (retval == -1) { 2533e52966c0SLen Brown if (restarted > 1) { 2534e52966c0SLen Brown exit(retval); 2535e52966c0SLen Brown } 2536c98d5d94SLen Brown re_initialize(); 2537c98d5d94SLen Brown goto restart; 2538c98d5d94SLen Brown } 2539e52966c0SLen Brown restarted = 0; 2540103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2541103a8feaSLen Brown 2542103a8feaSLen Brown while (1) { 2543c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 2544103a8feaSLen Brown re_initialize(); 2545103a8feaSLen Brown goto restart; 2546103a8feaSLen Brown } 25472a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2548562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2549562a2d37SLen Brown goto restart; 2550c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 2551d91bb17cSLen Brown if (retval < -1) { 2552d91bb17cSLen Brown exit(retval); 2553d91bb17cSLen Brown } else if (retval == -1) { 255415aaa346SLen Brown re_initialize(); 255515aaa346SLen Brown goto restart; 255615aaa346SLen Brown } 2557103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2558103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2559ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 2560ba3dec99SLen Brown re_initialize(); 2561ba3dec99SLen Brown goto restart; 2562ba3dec99SLen Brown } 2563c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2564c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2565b7d8c148SLen Brown flush_output_stdout(); 25662a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2567562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2568562a2d37SLen Brown goto restart; 2569c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2570d91bb17cSLen Brown if (retval < -1) { 2571d91bb17cSLen Brown exit(retval); 2572d91bb17cSLen Brown } else if (retval == -1) { 2573103a8feaSLen Brown re_initialize(); 2574103a8feaSLen Brown goto restart; 2575103a8feaSLen Brown } 2576103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2577103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 2578ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 2579ba3dec99SLen Brown re_initialize(); 2580ba3dec99SLen Brown goto restart; 2581ba3dec99SLen Brown } 2582c98d5d94SLen Brown compute_average(ODD_COUNTERS); 2583c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 2584b7d8c148SLen Brown flush_output_stdout(); 2585103a8feaSLen Brown } 2586103a8feaSLen Brown } 2587103a8feaSLen Brown 2588103a8feaSLen Brown void check_dev_msr() 2589103a8feaSLen Brown { 2590103a8feaSLen Brown struct stat sb; 25917ce7d5deSPrarit Bhargava char pathname[32]; 2592103a8feaSLen Brown 25937ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 25947ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 2595a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 2596d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 2597103a8feaSLen Brown } 2598103a8feaSLen Brown 259998481e79SLen Brown void check_permissions() 2600103a8feaSLen Brown { 260198481e79SLen Brown struct __user_cap_header_struct cap_header_data; 260298481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 260398481e79SLen Brown struct __user_cap_data_struct cap_data_data; 260498481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 260598481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 260698481e79SLen Brown int do_exit = 0; 26077ce7d5deSPrarit Bhargava char pathname[32]; 260898481e79SLen Brown 260998481e79SLen Brown /* check for CAP_SYS_RAWIO */ 261098481e79SLen Brown cap_header->pid = getpid(); 261198481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 261298481e79SLen Brown if (capget(cap_header, cap_data) < 0) 261398481e79SLen Brown err(-6, "capget(2) failed"); 261498481e79SLen Brown 261598481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 261698481e79SLen Brown do_exit++; 261798481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 261898481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 261998481e79SLen Brown } 262098481e79SLen Brown 262198481e79SLen Brown /* test file permissions */ 26227ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 26237ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 262498481e79SLen Brown do_exit++; 262598481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 262698481e79SLen Brown } 262798481e79SLen Brown 262898481e79SLen Brown /* if all else fails, thell them to be root */ 262998481e79SLen Brown if (do_exit) 2630b2c95d90SJosh Triplett if (getuid() != 0) 2631d7899447SLen Brown warnx("... or simply run as root"); 263298481e79SLen Brown 263398481e79SLen Brown if (do_exit) 263498481e79SLen Brown exit(-6); 2635103a8feaSLen Brown } 2636103a8feaSLen Brown 2637d7899447SLen Brown /* 2638d7899447SLen Brown * NHM adds support for additional MSRs: 2639d7899447SLen Brown * 2640d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 2641d7899447SLen Brown * 2642ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 26431df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 2644d7899447SLen Brown * 2645cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 2646cf4cbe53SLen Brown * 2647d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 2648d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 2649d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 2650d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 2651d7899447SLen Brown * 2652ee7e38e3SLen Brown * Side effect: 26531df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 265433148d67SLen Brown * sets has_misc_feature_control 2655d7899447SLen Brown */ 2656ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 2657103a8feaSLen Brown { 2658ee7e38e3SLen Brown unsigned long long msr; 265921ed5574SLen Brown unsigned int base_ratio; 2660ee7e38e3SLen Brown int *pkg_cstate_limits; 2661ee7e38e3SLen Brown 2662103a8feaSLen Brown if (!genuine_intel) 2663103a8feaSLen Brown return 0; 2664103a8feaSLen Brown 2665103a8feaSLen Brown if (family != 6) 2666103a8feaSLen Brown return 0; 2667103a8feaSLen Brown 266821ed5574SLen Brown bclk = discover_bclk(family, model); 266921ed5574SLen Brown 2670103a8feaSLen Brown switch (model) { 2671869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 2672869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 2673103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 2674869ce69eSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 2675869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 2676869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 2677869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 2678ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 2679ee7e38e3SLen Brown break; 2680869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 2681869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 2682869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 2683869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 2684ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 268533148d67SLen Brown has_misc_feature_control = 1; 2686ee7e38e3SLen Brown break; 2687869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2688869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2689869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2690869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2691869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2692869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2693869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2694869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2695869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2696869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2697869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2698869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2699ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 270033148d67SLen Brown has_misc_feature_control = 1; 2701ee7e38e3SLen Brown break; 2702d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 2703d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 270433148d67SLen Brown has_misc_feature_control = 1; 2705d8ebb442SLen Brown break; 2706869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 2707cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 2708869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 2709ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 2710ee7e38e3SLen Brown break; 2711869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 2712ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 2713cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 2714ee7e38e3SLen Brown break; 2715869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 2716005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2717ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 2718ee7e38e3SLen Brown break; 2719869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 2720ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 2721869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 2722e4085d54SLen Brown pkg_cstate_limits = bxt_pkg_cstate_limits; 2723e4085d54SLen Brown break; 2724103a8feaSLen Brown default: 2725103a8feaSLen Brown return 0; 2726103a8feaSLen Brown } 27271df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2728e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 2729ee7e38e3SLen Brown 2730ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 273121ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 273221ed5574SLen Brown 273321ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 273421ed5574SLen Brown has_base_hz = 1; 2735ee7e38e3SLen Brown return 1; 2736103a8feaSLen Brown } 27370f7887c4SLen Brown /* 2738495c7654SLen Brown * SLV client has support for unique MSRs: 27390f7887c4SLen Brown * 27400f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 27410f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 27420f7887c4SLen Brown */ 27430f7887c4SLen Brown 27440f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 27450f7887c4SLen Brown { 27460f7887c4SLen Brown if (!genuine_intel) 27470f7887c4SLen Brown return 0; 27480f7887c4SLen Brown 27490f7887c4SLen Brown switch (model) { 27500f7887c4SLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: 27510f7887c4SLen Brown case INTEL_FAM6_ATOM_MERRIFIELD: 27520f7887c4SLen Brown case INTEL_FAM6_ATOM_MOOREFIELD: 27530f7887c4SLen Brown return 1; 27540f7887c4SLen Brown } 27550f7887c4SLen Brown return 0; 27560f7887c4SLen Brown } 27577170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 27587170a374SLen Brown { 27597170a374SLen Brown 27607170a374SLen Brown if (!genuine_intel) 27617170a374SLen Brown return 0; 27627170a374SLen Brown 27637170a374SLen Brown switch (model) { 27647170a374SLen Brown case INTEL_FAM6_ATOM_DENVERTON: 27657170a374SLen Brown return 1; 27667170a374SLen Brown } 27677170a374SLen Brown return 0; 27687170a374SLen Brown } 2769ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 2770ade0ebacSLen Brown { 2771ade0ebacSLen Brown 2772ade0ebacSLen Brown if (!genuine_intel) 2773ade0ebacSLen Brown return 0; 2774ade0ebacSLen Brown 2775ade0ebacSLen Brown switch (model) { 2776ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 2777ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: 2778ade0ebacSLen Brown return 1; 2779ade0ebacSLen Brown } 2780ade0ebacSLen Brown return 0; 2781ade0ebacSLen Brown } 278234c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 278334c76197SLen Brown { 278434c76197SLen Brown 278534c76197SLen Brown if (!genuine_intel) 278634c76197SLen Brown return 0; 278734c76197SLen Brown 278834c76197SLen Brown switch (model) { 278934c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 279034c76197SLen Brown return 1; 279134c76197SLen Brown } 279234c76197SLen Brown return 0; 279334c76197SLen Brown } 27940f7887c4SLen Brown 279531e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 2796d7899447SLen Brown { 27970f7887c4SLen Brown if (has_slv_msrs(family, model)) 27980f7887c4SLen Brown return 0; 27990f7887c4SLen Brown 2800d7899447SLen Brown switch (model) { 2801d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 2802869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 2803869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 2804869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 2805005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2806d7899447SLen Brown return 0; 2807d7899447SLen Brown default: 2808d7899447SLen Brown return 1; 2809d7899447SLen Brown } 2810d7899447SLen Brown } 28110f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 28120f7887c4SLen Brown { 28130f7887c4SLen Brown if (has_slv_msrs(family, model)) 28140f7887c4SLen Brown return 1; 28150f7887c4SLen Brown 28160f7887c4SLen Brown return 0; 28170f7887c4SLen Brown } 28186574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 28196574a5d5SLen Brown { 28206574a5d5SLen Brown if (!genuine_intel) 28216574a5d5SLen Brown return 0; 28226574a5d5SLen Brown 28236574a5d5SLen Brown if (family != 6) 28246574a5d5SLen Brown return 0; 28256574a5d5SLen Brown 28266574a5d5SLen Brown switch (model) { 2827869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 2828869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 28296574a5d5SLen Brown return 1; 28306574a5d5SLen Brown default: 28316574a5d5SLen Brown return 0; 28326574a5d5SLen Brown } 28336574a5d5SLen Brown } 2834fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 2835fcd17211SLen Brown { 2836fcd17211SLen Brown if (!genuine_intel) 2837fcd17211SLen Brown return 0; 2838fcd17211SLen Brown 2839fcd17211SLen Brown if (family != 6) 2840fcd17211SLen Brown return 0; 2841fcd17211SLen Brown 2842fcd17211SLen Brown switch (model) { 2843869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 2844fcd17211SLen Brown return 1; 2845fcd17211SLen Brown default: 2846fcd17211SLen Brown return 0; 2847fcd17211SLen Brown } 2848fcd17211SLen Brown } 2849fcd17211SLen Brown 2850fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 2851fb5d4327SDasaratharaman Chandramouli { 2852fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 2853fb5d4327SDasaratharaman Chandramouli return 0; 2854fb5d4327SDasaratharaman Chandramouli 2855fb5d4327SDasaratharaman Chandramouli if (family != 6) 2856fb5d4327SDasaratharaman Chandramouli return 0; 2857fb5d4327SDasaratharaman Chandramouli 2858fb5d4327SDasaratharaman Chandramouli switch (model) { 2859869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 2860005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2861fb5d4327SDasaratharaman Chandramouli return 1; 2862fb5d4327SDasaratharaman Chandramouli default: 2863fb5d4327SDasaratharaman Chandramouli return 0; 2864fb5d4327SDasaratharaman Chandramouli } 2865fb5d4327SDasaratharaman Chandramouli } 286631e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 286731e07522SLen Brown { 286831e07522SLen Brown if (!genuine_intel) 286931e07522SLen Brown return 0; 287031e07522SLen Brown 287131e07522SLen Brown if (family != 6) 287231e07522SLen Brown return 0; 287331e07522SLen Brown 287431e07522SLen Brown switch (model) { 287531e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 287631e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 287731e07522SLen Brown return 1; 287831e07522SLen Brown default: 287931e07522SLen Brown return 0; 288031e07522SLen Brown } 288131e07522SLen Brown } 28826fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 28836fb3143bSLen Brown { 28846fb3143bSLen Brown if (!genuine_intel) 28856fb3143bSLen Brown return 0; 28866fb3143bSLen Brown 28876fb3143bSLen Brown if (family != 6) 28886fb3143bSLen Brown return 0; 28896fb3143bSLen Brown 28906fb3143bSLen Brown switch (model) { 2891869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 2892869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2893869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2894869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2895869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2896869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2897869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2898869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2899869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2900869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2901869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2902869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2903869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2904869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 29056fb3143bSLen Brown 2906869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 2907005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 29086fb3143bSLen Brown return 1; 29096fb3143bSLen Brown default: 29106fb3143bSLen Brown return 0; 29116fb3143bSLen Brown } 29126fb3143bSLen Brown } 29136fb3143bSLen Brown 2914fcd17211SLen Brown static void 29151b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 2916fcd17211SLen Brown { 2917fcd17211SLen Brown if (!do_nhm_platform_info) 2918fcd17211SLen Brown return; 2919fcd17211SLen Brown 2920fcd17211SLen Brown dump_nhm_platform_info(); 2921fcd17211SLen Brown 2922fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 2923fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 2924fcd17211SLen Brown 2925fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 2926fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 2927fcd17211SLen Brown 292831e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 292931e07522SLen Brown dump_turbo_ratio_limits(family, model); 2930fcd17211SLen Brown 29310f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 29320f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 29330f7887c4SLen Brown 2934fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 2935fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 2936fb5d4327SDasaratharaman Chandramouli 29376fb3143bSLen Brown if (has_config_tdp(family, model)) 29386fb3143bSLen Brown dump_config_tdp(); 29396fb3143bSLen Brown 2940fcd17211SLen Brown dump_nhm_cst_cfg(); 2941fcd17211SLen Brown } 2942fcd17211SLen Brown 294341618e63SLen Brown static void 294441618e63SLen Brown dump_sysfs_cstate_config(void) 294541618e63SLen Brown { 294641618e63SLen Brown char path[64]; 294741618e63SLen Brown char name_buf[16]; 294841618e63SLen Brown char desc[64]; 294941618e63SLen Brown FILE *input; 295041618e63SLen Brown int state; 295141618e63SLen Brown char *sp; 295241618e63SLen Brown 295341618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 295441618e63SLen Brown return; 295541618e63SLen Brown 295641618e63SLen Brown for (state = 0; state < 10; ++state) { 295741618e63SLen Brown 295841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 295941618e63SLen Brown base_cpu, state); 296041618e63SLen Brown input = fopen(path, "r"); 296141618e63SLen Brown if (input == NULL) 296241618e63SLen Brown continue; 296341618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 296441618e63SLen Brown 296541618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 296641618e63SLen Brown sp = strchr(name_buf, '-'); 296741618e63SLen Brown if (!sp) 296841618e63SLen Brown sp = strchrnul(name_buf, '\n'); 296941618e63SLen Brown *sp = '\0'; 297041618e63SLen Brown 297141618e63SLen Brown fclose(input); 297241618e63SLen Brown 297341618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 297441618e63SLen Brown base_cpu, state); 297541618e63SLen Brown input = fopen(path, "r"); 297641618e63SLen Brown if (input == NULL) 297741618e63SLen Brown continue; 297841618e63SLen Brown fgets(desc, sizeof(desc), input); 297941618e63SLen Brown 298041618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 298141618e63SLen Brown fclose(input); 298241618e63SLen Brown } 298341618e63SLen Brown } 29847293fccdSLen Brown static void 29857293fccdSLen Brown dump_sysfs_pstate_config(void) 29867293fccdSLen Brown { 29877293fccdSLen Brown char path[64]; 29887293fccdSLen Brown char driver_buf[64]; 29897293fccdSLen Brown char governor_buf[64]; 29907293fccdSLen Brown FILE *input; 29917293fccdSLen Brown int turbo; 29927293fccdSLen Brown 29937293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 29947293fccdSLen Brown base_cpu); 29957293fccdSLen Brown input = fopen(path, "r"); 29967293fccdSLen Brown if (input == NULL) { 29977293fccdSLen Brown fprintf(stderr, "NSFOD %s\n", path); 29987293fccdSLen Brown return; 29997293fccdSLen Brown } 30007293fccdSLen Brown fgets(driver_buf, sizeof(driver_buf), input); 30017293fccdSLen Brown fclose(input); 30027293fccdSLen Brown 30037293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 30047293fccdSLen Brown base_cpu); 30057293fccdSLen Brown input = fopen(path, "r"); 30067293fccdSLen Brown if (input == NULL) { 30077293fccdSLen Brown fprintf(stderr, "NSFOD %s\n", path); 30087293fccdSLen Brown return; 30097293fccdSLen Brown } 30107293fccdSLen Brown fgets(governor_buf, sizeof(governor_buf), input); 30117293fccdSLen Brown fclose(input); 30127293fccdSLen Brown 30137293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 30147293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 30157293fccdSLen Brown 30167293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 30177293fccdSLen Brown input = fopen(path, "r"); 30187293fccdSLen Brown if (input != NULL) { 30197293fccdSLen Brown fscanf(input, "%d", &turbo); 30207293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 30217293fccdSLen Brown fclose(input); 30227293fccdSLen Brown } 30237293fccdSLen Brown 30247293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 30257293fccdSLen Brown input = fopen(path, "r"); 30267293fccdSLen Brown if (input != NULL) { 30277293fccdSLen Brown fscanf(input, "%d", &turbo); 30287293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 30297293fccdSLen Brown fclose(input); 30307293fccdSLen Brown } 30317293fccdSLen Brown } 303241618e63SLen Brown 30336574a5d5SLen Brown 3034889facbeSLen Brown /* 3035889facbeSLen Brown * print_epb() 3036889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3037889facbeSLen Brown */ 3038889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3039889facbeSLen Brown { 3040889facbeSLen Brown unsigned long long msr; 3041889facbeSLen Brown char *epb_string; 3042889facbeSLen Brown int cpu; 3043889facbeSLen Brown 3044889facbeSLen Brown if (!has_epb) 3045889facbeSLen Brown return 0; 3046889facbeSLen Brown 3047889facbeSLen Brown cpu = t->cpu_id; 3048889facbeSLen Brown 3049889facbeSLen Brown /* EPB is per-package */ 3050889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3051889facbeSLen Brown return 0; 3052889facbeSLen Brown 3053889facbeSLen Brown if (cpu_migrate(cpu)) { 3054b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3055889facbeSLen Brown return -1; 3056889facbeSLen Brown } 3057889facbeSLen Brown 3058889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 3059889facbeSLen Brown return 0; 3060889facbeSLen Brown 3061e9be7dd6SLen Brown switch (msr & 0xF) { 3062889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3063889facbeSLen Brown epb_string = "performance"; 3064889facbeSLen Brown break; 3065889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3066889facbeSLen Brown epb_string = "balanced"; 3067889facbeSLen Brown break; 3068889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3069889facbeSLen Brown epb_string = "powersave"; 3070889facbeSLen Brown break; 3071889facbeSLen Brown default: 3072889facbeSLen Brown epb_string = "custom"; 3073889facbeSLen Brown break; 3074889facbeSLen Brown } 3075b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 3076889facbeSLen Brown 3077889facbeSLen Brown return 0; 3078889facbeSLen Brown } 30797f5c258eSLen Brown /* 30807f5c258eSLen Brown * print_hwp() 30817f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 30827f5c258eSLen Brown */ 30837f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 30847f5c258eSLen Brown { 30857f5c258eSLen Brown unsigned long long msr; 30867f5c258eSLen Brown int cpu; 30877f5c258eSLen Brown 30887f5c258eSLen Brown if (!has_hwp) 30897f5c258eSLen Brown return 0; 30907f5c258eSLen Brown 30917f5c258eSLen Brown cpu = t->cpu_id; 30927f5c258eSLen Brown 30937f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 30947f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 30957f5c258eSLen Brown return 0; 30967f5c258eSLen Brown 30977f5c258eSLen Brown if (cpu_migrate(cpu)) { 3098b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 30997f5c258eSLen Brown return -1; 31007f5c258eSLen Brown } 31017f5c258eSLen Brown 31027f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 31037f5c258eSLen Brown return 0; 31047f5c258eSLen Brown 3105b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 31067f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 31077f5c258eSLen Brown 31087f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 31097f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 31107f5c258eSLen Brown return 0; 31117f5c258eSLen Brown 31127f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 31137f5c258eSLen Brown return 0; 31147f5c258eSLen Brown 3115b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 31166dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 31177f5c258eSLen Brown cpu, msr, 31187f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 31197f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 31207f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 31217f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 31227f5c258eSLen Brown 31237f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 31247f5c258eSLen Brown return 0; 31257f5c258eSLen Brown 3126b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 31276dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 31287f5c258eSLen Brown cpu, msr, 31297f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 31307f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 31317f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 31327f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 31337f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 31347f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 31357f5c258eSLen Brown 31367f5c258eSLen Brown if (has_hwp_pkg) { 31377f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 31387f5c258eSLen Brown return 0; 31397f5c258eSLen Brown 3140b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 31416dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 31427f5c258eSLen Brown cpu, msr, 31437f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 31447f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 31457f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 31467f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 31477f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 31487f5c258eSLen Brown } 31497f5c258eSLen Brown if (has_hwp_notify) { 31507f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 31517f5c258eSLen Brown return 0; 31527f5c258eSLen Brown 3153b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 31547f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 31557f5c258eSLen Brown cpu, msr, 31567f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 31577f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 31587f5c258eSLen Brown } 31597f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 31607f5c258eSLen Brown return 0; 31617f5c258eSLen Brown 3162b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 31637f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 31647f5c258eSLen Brown cpu, msr, 31657f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 31667f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 3167889facbeSLen Brown 3168889facbeSLen Brown return 0; 3169889facbeSLen Brown } 3170889facbeSLen Brown 31713a9a941dSLen Brown /* 31723a9a941dSLen Brown * print_perf_limit() 31733a9a941dSLen Brown */ 31743a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 31753a9a941dSLen Brown { 31763a9a941dSLen Brown unsigned long long msr; 31773a9a941dSLen Brown int cpu; 31783a9a941dSLen Brown 31793a9a941dSLen Brown cpu = t->cpu_id; 31803a9a941dSLen Brown 31813a9a941dSLen Brown /* per-package */ 31823a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 31833a9a941dSLen Brown return 0; 31843a9a941dSLen Brown 31853a9a941dSLen Brown if (cpu_migrate(cpu)) { 3186b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 31873a9a941dSLen Brown return -1; 31883a9a941dSLen Brown } 31893a9a941dSLen Brown 31903a9a941dSLen Brown if (do_core_perf_limit_reasons) { 31913a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 3192b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3193b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 3194e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 31953a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 3196e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 3197e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 3198e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 3199e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 3200e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 3201e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 3202e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 3203e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 3204e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 3205e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 3206e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 3207e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 3208b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 3209e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 32103a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 3211e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 3212e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 3213e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 3214e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 3215e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 3216e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 3217e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 3218e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 3219e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 3220e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 3221e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 3222e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 32233a9a941dSLen Brown 32243a9a941dSLen Brown } 32253a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 32263a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 3227b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3228b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 32293a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 32303a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 32313a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 32323a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 32333a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 32343a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 32353a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 32363a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3237b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 32383a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 32393a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 32403a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 32413a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 32423a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 32433a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 32443a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 32453a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 32463a9a941dSLen Brown } 32473a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 32483a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 3249b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3250b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 32513a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 32523a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 32533a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 32543a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 32553a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 32563a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3257b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 32583a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 32593a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 32603a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 32613a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 32623a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 32633a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 32643a9a941dSLen Brown } 32653a9a941dSLen Brown return 0; 32663a9a941dSLen Brown } 32673a9a941dSLen Brown 3268889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 3269889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 3270889facbeSLen Brown 32711b69317dSColin Ian King double get_tdp(unsigned int model) 3272144b44b1SLen Brown { 3273144b44b1SLen Brown unsigned long long msr; 3274144b44b1SLen Brown 3275144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 32767ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 3277144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 3278144b44b1SLen Brown 3279144b44b1SLen Brown switch (model) { 3280869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: 3281869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: 3282144b44b1SLen Brown return 30.0; 3283144b44b1SLen Brown default: 3284144b44b1SLen Brown return 135.0; 3285144b44b1SLen Brown } 3286144b44b1SLen Brown } 3287144b44b1SLen Brown 328840ee8e3bSAndrey Semin /* 328940ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 329040ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 329140ee8e3bSAndrey Semin */ 329240ee8e3bSAndrey Semin static double 329340ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 329440ee8e3bSAndrey Semin { 329540ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 329640ee8e3bSAndrey Semin 329740ee8e3bSAndrey Semin switch (model) { 3298869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3299869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3300869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3301869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3302005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 330340ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 330440ee8e3bSAndrey Semin default: 330540ee8e3bSAndrey Semin return (rapl_energy_units); 330640ee8e3bSAndrey Semin } 330740ee8e3bSAndrey Semin } 330840ee8e3bSAndrey Semin 3309144b44b1SLen Brown 3310889facbeSLen Brown /* 3311889facbeSLen Brown * rapl_probe() 3312889facbeSLen Brown * 3313144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 3314889facbeSLen Brown */ 3315889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 3316889facbeSLen Brown { 3317889facbeSLen Brown unsigned long long msr; 3318144b44b1SLen Brown unsigned int time_unit; 3319889facbeSLen Brown double tdp; 3320889facbeSLen Brown 3321889facbeSLen Brown if (!genuine_intel) 3322889facbeSLen Brown return; 3323889facbeSLen Brown 3324889facbeSLen Brown if (family != 6) 3325889facbeSLen Brown return; 3326889facbeSLen Brown 3327889facbeSLen Brown switch (model) { 3328869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3329869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 3330869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3331869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3332869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3333869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3334869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3335144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 3336812db3f7SLen Brown if (rapl_joules) { 3337812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3338812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3339812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 3340812db3f7SLen Brown } else { 3341812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3342812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3343812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 3344812db3f7SLen Brown } 3345889facbeSLen Brown break; 3346869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3347ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 3348e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 3349812db3f7SLen Brown if (rapl_joules) 3350812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3351812db3f7SLen Brown else 3352812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3353e4085d54SLen Brown break; 3354869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3355869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3356869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3357869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 335881824921SLen 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; 3359812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3360812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3361812db3f7SLen Brown if (rapl_joules) { 3362812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3363812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3364812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 336581824921SLen Brown BIC_PRESENT(BIC_GFX_J); 3366812db3f7SLen Brown } else { 3367812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3368812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3369812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 337081824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 3371812db3f7SLen Brown } 33720b2bb692SLen Brown break; 3373869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3374869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3375869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3376869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3377869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3378005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 33790b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 3380812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3381812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3382812db3f7SLen Brown if (rapl_joules) { 3383812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3384812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3385812db3f7SLen Brown } else { 3386812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3387812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3388812db3f7SLen Brown } 3389e6f9bb3cSLen Brown break; 3390869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3391869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 33920b2bb692SLen 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; 3393812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3394812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3395812db3f7SLen Brown if (rapl_joules) { 3396812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3397812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3398812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3399812db3f7SLen Brown } else { 3400812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3401812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3402812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3403812db3f7SLen Brown } 3404144b44b1SLen Brown break; 3405869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 3406869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 3407144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 3408812db3f7SLen Brown if (rapl_joules) { 3409812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3410812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3411812db3f7SLen Brown } else { 3412812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3413812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3414812db3f7SLen Brown } 3415889facbeSLen Brown break; 3416869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 34170f644909SJacob 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; 3418812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3419812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3420812db3f7SLen Brown if (rapl_joules) { 3421812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3422812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3423812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3424812db3f7SLen Brown } else { 3425812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3426812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3427812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3428812db3f7SLen Brown } 34290f644909SJacob Pan break; 3430889facbeSLen Brown default: 3431889facbeSLen Brown return; 3432889facbeSLen Brown } 3433889facbeSLen Brown 3434889facbeSLen Brown /* units on package 0, verify later other packages match */ 34357ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 3436889facbeSLen Brown return; 3437889facbeSLen Brown 3438889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 3439869ce69eSLen Brown if (model == INTEL_FAM6_ATOM_SILVERMONT1) 3440144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 3441144b44b1SLen Brown else 3442889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 3443889facbeSLen Brown 344440ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 344540ee8e3bSAndrey Semin 3446144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 3447144b44b1SLen Brown if (time_unit == 0) 3448144b44b1SLen Brown time_unit = 0xA; 3449889facbeSLen Brown 3450144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 3451144b44b1SLen Brown 3452144b44b1SLen Brown tdp = get_tdp(model); 3453889facbeSLen Brown 3454889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 345596e47158SLen Brown if (!quiet) 3456b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 3457889facbeSLen Brown 3458889facbeSLen Brown return; 3459889facbeSLen Brown } 3460889facbeSLen Brown 34611b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 34623a9a941dSLen Brown { 34633a9a941dSLen Brown if (!genuine_intel) 34643a9a941dSLen Brown return; 34653a9a941dSLen Brown 34663a9a941dSLen Brown if (family != 6) 34673a9a941dSLen Brown return; 34683a9a941dSLen Brown 34693a9a941dSLen Brown switch (model) { 3470869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3471869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3472869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 34733a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 3474869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 34753a9a941dSLen Brown do_core_perf_limit_reasons = 1; 34763a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 34773a9a941dSLen Brown default: 34783a9a941dSLen Brown return; 34793a9a941dSLen Brown } 34803a9a941dSLen Brown } 34813a9a941dSLen Brown 3482889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3483889facbeSLen Brown { 3484889facbeSLen Brown unsigned long long msr; 3485f4896fa5SLen Brown unsigned int dts, dts2; 3486889facbeSLen Brown int cpu; 3487889facbeSLen Brown 3488889facbeSLen Brown if (!(do_dts || do_ptm)) 3489889facbeSLen Brown return 0; 3490889facbeSLen Brown 3491889facbeSLen Brown cpu = t->cpu_id; 3492889facbeSLen Brown 3493889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 3494889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 3495889facbeSLen Brown return 0; 3496889facbeSLen Brown 3497889facbeSLen Brown if (cpu_migrate(cpu)) { 3498b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3499889facbeSLen Brown return -1; 3500889facbeSLen Brown } 3501889facbeSLen Brown 3502889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 3503889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 3504889facbeSLen Brown return 0; 3505889facbeSLen Brown 3506889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3507b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 3508889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 3509889facbeSLen Brown 3510889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 3511889facbeSLen Brown return 0; 3512889facbeSLen Brown 3513889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3514889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3515b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3516889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3517889facbeSLen Brown } 3518889facbeSLen Brown 3519889facbeSLen Brown 3520f4896fa5SLen Brown if (do_dts && debug) { 3521889facbeSLen Brown unsigned int resolution; 3522889facbeSLen Brown 3523889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 3524889facbeSLen Brown return 0; 3525889facbeSLen Brown 3526889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3527889facbeSLen Brown resolution = (msr >> 27) & 0xF; 3528b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 3529889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 3530889facbeSLen Brown 3531889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 3532889facbeSLen Brown return 0; 3533889facbeSLen Brown 3534889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3535889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3536b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3537889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3538889facbeSLen Brown } 3539889facbeSLen Brown 3540889facbeSLen Brown return 0; 3541889facbeSLen Brown } 3542889facbeSLen Brown 3543889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 3544889facbeSLen Brown { 3545b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 3546889facbeSLen Brown cpu, label, 3547889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 3548889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 3549889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 3550889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 3551889facbeSLen Brown 3552889facbeSLen Brown return; 3553889facbeSLen Brown } 3554889facbeSLen Brown 3555889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3556889facbeSLen Brown { 3557889facbeSLen Brown unsigned long long msr; 3558889facbeSLen Brown int cpu; 3559889facbeSLen Brown 3560889facbeSLen Brown if (!do_rapl) 3561889facbeSLen Brown return 0; 3562889facbeSLen Brown 3563889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 3564889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3565889facbeSLen Brown return 0; 3566889facbeSLen Brown 3567889facbeSLen Brown cpu = t->cpu_id; 3568889facbeSLen Brown if (cpu_migrate(cpu)) { 3569b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3570889facbeSLen Brown return -1; 3571889facbeSLen Brown } 3572889facbeSLen Brown 3573889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 3574889facbeSLen Brown return -1; 3575889facbeSLen Brown 357696e47158SLen Brown fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr, 3577144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 357896e47158SLen Brown 3579144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 3580144b44b1SLen Brown 3581889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 3582889facbeSLen Brown return -5; 3583889facbeSLen Brown 3584889facbeSLen Brown 3585b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 3586889facbeSLen Brown cpu, msr, 3587889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3588889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3589889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3590889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 3591889facbeSLen Brown 3592144b44b1SLen Brown } 3593144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 3594144b44b1SLen Brown 3595889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 3596889facbeSLen Brown return -9; 3597889facbeSLen Brown 3598b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 3599889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 3600889facbeSLen Brown 3601889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 3602b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 3603889facbeSLen Brown cpu, 3604889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 3605889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 3606889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 3607889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 3608889facbeSLen Brown } 3609889facbeSLen Brown 36100b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 3611889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 3612889facbeSLen Brown return -6; 3613889facbeSLen Brown 3614b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 3615889facbeSLen Brown cpu, msr, 3616889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3617889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3618889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3619889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 36200b2bb692SLen Brown } 36210b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 3622889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 3623889facbeSLen Brown return -9; 3624b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 3625889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3626889facbeSLen Brown 3627889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 3628889facbeSLen Brown } 3629144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 3630889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 3631889facbeSLen Brown return -7; 3632889facbeSLen Brown 3633b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 3634144b44b1SLen Brown } 36359148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 3636889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 3637889facbeSLen Brown return -9; 3638b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 3639889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3640889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 3641889facbeSLen Brown } 3642889facbeSLen Brown if (do_rapl & RAPL_GFX) { 3643889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 3644889facbeSLen Brown return -8; 3645889facbeSLen Brown 3646b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 3647889facbeSLen Brown 3648889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 3649889facbeSLen Brown return -9; 3650b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 3651889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3652889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 3653889facbeSLen Brown } 3654889facbeSLen Brown return 0; 3655889facbeSLen Brown } 3656889facbeSLen Brown 3657d7899447SLen Brown /* 3658d7899447SLen Brown * SNB adds support for additional MSRs: 3659d7899447SLen Brown * 3660d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 3661d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 3662d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 3663d7899447SLen Brown */ 3664103a8feaSLen Brown 3665d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 3666103a8feaSLen Brown { 3667103a8feaSLen Brown if (!genuine_intel) 3668103a8feaSLen Brown return 0; 3669103a8feaSLen Brown 3670103a8feaSLen Brown switch (model) { 3671869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3672869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3673869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3674869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3675869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3676869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 3677869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3678869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3679869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3680869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3681869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3682869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3683869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3684869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3685869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3686869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3687869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3688869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3689ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 36905bbac26eSXiaolong Wang case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 3691103a8feaSLen Brown return 1; 3692103a8feaSLen Brown } 3693103a8feaSLen Brown return 0; 3694103a8feaSLen Brown } 3695103a8feaSLen Brown 3696d7899447SLen Brown /* 3697d7899447SLen Brown * HSW adds support for additional MSRs: 3698d7899447SLen Brown * 3699d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 3700d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 3701d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 37025a63426eSLen Brown * 37035a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 37045a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 37055a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 37065a63426eSLen Brown * 3707d7899447SLen Brown */ 3708d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 3709ca58710fSKristen Carlson Accardi { 3710ca58710fSKristen Carlson Accardi if (!genuine_intel) 3711ca58710fSKristen Carlson Accardi return 0; 3712ca58710fSKristen Carlson Accardi 3713ca58710fSKristen Carlson Accardi switch (model) { 3714869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3715869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3716869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3717869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3718869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3719869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3720869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3721ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 3722ca58710fSKristen Carlson Accardi return 1; 3723ca58710fSKristen Carlson Accardi } 3724ca58710fSKristen Carlson Accardi return 0; 3725ca58710fSKristen Carlson Accardi } 3726ca58710fSKristen Carlson Accardi 37270b2bb692SLen Brown /* 37280b2bb692SLen Brown * SKL adds support for additional MSRS: 37290b2bb692SLen Brown * 37300b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 37310b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 37320b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 37330b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 37340b2bb692SLen Brown */ 37350b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 37360b2bb692SLen Brown { 37370b2bb692SLen Brown if (!genuine_intel) 37380b2bb692SLen Brown return 0; 37390b2bb692SLen Brown 37400b2bb692SLen Brown switch (model) { 3741869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3742869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3743869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3744869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 37450b2bb692SLen Brown return 1; 37460b2bb692SLen Brown } 37470b2bb692SLen Brown return 0; 37480b2bb692SLen Brown } 37490b2bb692SLen Brown 3750144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 3751144b44b1SLen Brown { 3752144b44b1SLen Brown if (!genuine_intel) 3753144b44b1SLen Brown return 0; 3754144b44b1SLen Brown switch (model) { 3755869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 3756869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 3757144b44b1SLen Brown return 1; 3758144b44b1SLen Brown } 3759144b44b1SLen Brown return 0; 3760144b44b1SLen Brown } 3761144b44b1SLen Brown 3762fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 3763fb5d4327SDasaratharaman Chandramouli { 3764fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3765fb5d4327SDasaratharaman Chandramouli return 0; 3766fb5d4327SDasaratharaman Chandramouli switch (model) { 3767869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3768005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3769fb5d4327SDasaratharaman Chandramouli return 1; 3770fb5d4327SDasaratharaman Chandramouli } 3771fb5d4327SDasaratharaman Chandramouli return 0; 3772fb5d4327SDasaratharaman Chandramouli } 3773fb5d4327SDasaratharaman Chandramouli 3774b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 3775b2b34dfeSHubert Chrzaniuk { 3776b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 3777b2b34dfeSHubert Chrzaniuk return 1024; 3778b2b34dfeSHubert Chrzaniuk return 1; 3779b2b34dfeSHubert Chrzaniuk } 3780b2b34dfeSHubert Chrzaniuk 3781144b44b1SLen Brown #define SLM_BCLK_FREQS 5 3782144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 3783144b44b1SLen Brown 3784144b44b1SLen Brown double slm_bclk(void) 3785144b44b1SLen Brown { 3786144b44b1SLen Brown unsigned long long msr = 3; 3787144b44b1SLen Brown unsigned int i; 3788144b44b1SLen Brown double freq; 3789144b44b1SLen Brown 37907ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 3791b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 3792144b44b1SLen Brown 3793144b44b1SLen Brown i = msr & 0xf; 3794144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 3795b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 37960a91e551SColin Ian King i = 3; 3797144b44b1SLen Brown } 3798144b44b1SLen Brown freq = slm_freq_table[i]; 3799144b44b1SLen Brown 380096e47158SLen Brown if (!quiet) 3801b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 3802144b44b1SLen Brown 3803144b44b1SLen Brown return freq; 3804144b44b1SLen Brown } 3805144b44b1SLen Brown 3806103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 3807103a8feaSLen Brown { 3808121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 3809103a8feaSLen Brown return 100.00; 3810144b44b1SLen Brown else if (is_slm(family, model)) 3811144b44b1SLen Brown return slm_bclk(); 3812103a8feaSLen Brown else 3813103a8feaSLen Brown return 133.33; 3814103a8feaSLen Brown } 3815103a8feaSLen Brown 3816889facbeSLen Brown /* 3817889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 3818889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 3819889facbeSLen Brown * This is usually equal to tjMax. 3820889facbeSLen Brown * 3821889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 3822889facbeSLen Brown * but also allow cmdline over-ride with -T. 3823889facbeSLen Brown * 3824889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 3825889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 3826889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 3827889facbeSLen Brown */ 3828889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3829889facbeSLen Brown { 3830889facbeSLen Brown unsigned long long msr; 3831889facbeSLen Brown unsigned int target_c_local; 3832889facbeSLen Brown int cpu; 3833889facbeSLen Brown 3834889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 3835889facbeSLen Brown if (!(do_dts || do_ptm)) 3836889facbeSLen Brown return 0; 3837889facbeSLen Brown 3838889facbeSLen Brown /* this is a per-package concept */ 3839889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3840889facbeSLen Brown return 0; 3841889facbeSLen Brown 3842889facbeSLen Brown cpu = t->cpu_id; 3843889facbeSLen Brown if (cpu_migrate(cpu)) { 3844b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3845889facbeSLen Brown return -1; 3846889facbeSLen Brown } 3847889facbeSLen Brown 3848889facbeSLen Brown if (tcc_activation_temp_override != 0) { 3849889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 3850b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 3851889facbeSLen Brown cpu, tcc_activation_temp); 3852889facbeSLen Brown return 0; 3853889facbeSLen Brown } 3854889facbeSLen Brown 3855889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 3856d7899447SLen Brown if (!do_nhm_platform_info) 3857889facbeSLen Brown goto guess; 3858889facbeSLen Brown 38597ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 3860889facbeSLen Brown goto guess; 3861889facbeSLen Brown 38623482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 3863889facbeSLen Brown 386496e47158SLen Brown if (!quiet) 3865b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 3866889facbeSLen Brown cpu, msr, target_c_local); 3867889facbeSLen Brown 38683482124aSJean Delvare if (!target_c_local) 3869889facbeSLen Brown goto guess; 3870889facbeSLen Brown 3871889facbeSLen Brown tcc_activation_temp = target_c_local; 3872889facbeSLen Brown 3873889facbeSLen Brown return 0; 3874889facbeSLen Brown 3875889facbeSLen Brown guess: 3876889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 3877b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 3878889facbeSLen Brown cpu, tcc_activation_temp); 3879889facbeSLen Brown 3880889facbeSLen Brown return 0; 3881889facbeSLen Brown } 388269807a63SLen Brown 3883aa8d8cc7SLen Brown void decode_feature_control_msr(void) 3884aa8d8cc7SLen Brown { 3885aa8d8cc7SLen Brown unsigned long long msr; 3886aa8d8cc7SLen Brown 3887aa8d8cc7SLen Brown if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr)) 3888aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 3889aa8d8cc7SLen Brown base_cpu, msr, 3890aa8d8cc7SLen Brown msr & FEATURE_CONTROL_LOCKED ? "" : "UN-", 3891aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 3892aa8d8cc7SLen Brown } 3893aa8d8cc7SLen Brown 389469807a63SLen Brown void decode_misc_enable_msr(void) 389569807a63SLen Brown { 389669807a63SLen Brown unsigned long long msr; 389769807a63SLen Brown 389869807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 3899e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 390069807a63SLen Brown base_cpu, msr, 3901e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 3902e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 3903e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "", 3904e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 3905e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 390669807a63SLen Brown } 390769807a63SLen Brown 390833148d67SLen Brown void decode_misc_feature_control(void) 390933148d67SLen Brown { 391033148d67SLen Brown unsigned long long msr; 391133148d67SLen Brown 391233148d67SLen Brown if (!has_misc_feature_control) 391333148d67SLen Brown return; 391433148d67SLen Brown 391533148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 391633148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 391733148d67SLen Brown base_cpu, msr, 391833148d67SLen Brown msr & (0 << 0) ? "No-" : "", 391933148d67SLen Brown msr & (1 << 0) ? "No-" : "", 392033148d67SLen Brown msr & (2 << 0) ? "No-" : "", 392133148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 392233148d67SLen Brown } 3923f0057310SLen Brown /* 3924f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 3925f0057310SLen Brown * 3926f0057310SLen Brown * Decode the bits according to the Nehalem documentation 3927f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 3928f0057310SLen Brown * bit[1] less so... 3929f0057310SLen Brown */ 3930f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 3931f0057310SLen Brown { 3932f0057310SLen Brown unsigned long long msr; 3933f0057310SLen Brown 3934f0057310SLen Brown if (!do_nhm_platform_info) 3935f0057310SLen Brown return; 3936f0057310SLen Brown 3937cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 3938cf4cbe53SLen Brown return; 3939cf4cbe53SLen Brown 3940f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 3941ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 3942f0057310SLen Brown base_cpu, msr, 3943f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 3944ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 3945ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 3946f0057310SLen Brown } 394771616c8eSLen Brown /* 394871616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 394971616c8eSLen Brown * 395071616c8eSLen Brown * This MSRs are present on Silvermont processors, 395171616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 395271616c8eSLen Brown */ 395371616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 395471616c8eSLen Brown { 395571616c8eSLen Brown unsigned long long msr; 395671616c8eSLen Brown 395771616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 395871616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 395971616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 396071616c8eSLen Brown 396171616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 396271616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 396371616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 396471616c8eSLen Brown } 39657f5c258eSLen Brown 3966fcd17211SLen Brown void process_cpuid() 3967103a8feaSLen Brown { 396861a87ba7SLen Brown unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; 3969103a8feaSLen Brown unsigned int fms, family, model, stepping; 3970b3a34e93SLen Brown unsigned int has_turbo; 3971103a8feaSLen Brown 3972103a8feaSLen Brown eax = ebx = ecx = edx = 0; 3973103a8feaSLen Brown 39745aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 3975103a8feaSLen Brown 3976103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 3977103a8feaSLen Brown genuine_intel = 1; 3978103a8feaSLen Brown 397996e47158SLen Brown if (!quiet) 3980b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 3981103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 3982103a8feaSLen Brown 39835aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 3984103a8feaSLen Brown family = (fms >> 8) & 0xf; 3985103a8feaSLen Brown model = (fms >> 4) & 0xf; 3986103a8feaSLen Brown stepping = fms & 0xf; 3987103a8feaSLen Brown if (family == 6 || family == 0xf) 3988103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 3989103a8feaSLen Brown 399096e47158SLen Brown if (!quiet) { 3991b7d8c148SLen Brown fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 3992103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 3993aa8d8cc7SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n", 399469807a63SLen Brown ecx & (1 << 0) ? "SSE3" : "-", 399569807a63SLen Brown ecx & (1 << 3) ? "MONITOR" : "-", 3996aa8d8cc7SLen Brown ecx & (1 << 6) ? "SMX" : "-", 399769807a63SLen Brown ecx & (1 << 7) ? "EIST" : "-", 399869807a63SLen Brown ecx & (1 << 8) ? "TM2" : "-", 399969807a63SLen Brown edx & (1 << 4) ? "TSC" : "-", 400069807a63SLen Brown edx & (1 << 5) ? "MSR" : "-", 400169807a63SLen Brown edx & (1 << 22) ? "ACPI-TM" : "-", 400269807a63SLen Brown edx & (1 << 29) ? "TM" : "-"); 400369807a63SLen Brown } 4004103a8feaSLen Brown 4005b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 4006b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 4007103a8feaSLen Brown 4008103a8feaSLen Brown /* 4009103a8feaSLen Brown * check max extended function levels of CPUID. 4010103a8feaSLen Brown * This is needed to check for invariant TSC. 4011103a8feaSLen Brown * This check is valid for both Intel and AMD. 4012103a8feaSLen Brown */ 4013103a8feaSLen Brown ebx = ecx = edx = 0; 40145aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 4015103a8feaSLen Brown 401661a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 4017103a8feaSLen Brown 4018103a8feaSLen Brown /* 4019103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 4020103a8feaSLen Brown * this check is valid for both Intel and AMD 4021103a8feaSLen Brown */ 40225aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 40238209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 4024d7899447SLen Brown } 4025103a8feaSLen Brown 4026103a8feaSLen Brown /* 4027103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 4028103a8feaSLen Brown * this check is valid for both Intel and AMD 4029103a8feaSLen Brown */ 4030103a8feaSLen Brown 40315aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 40328209e054SThomas Renninger has_aperf = ecx & (1 << 0); 4033812db3f7SLen Brown if (has_aperf) { 4034812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 4035812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 4036812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 4037812db3f7SLen Brown } 4038889facbeSLen Brown do_dts = eax & (1 << 0); 4039812db3f7SLen Brown if (do_dts) 4040812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 4041b3a34e93SLen Brown has_turbo = eax & (1 << 1); 4042889facbeSLen Brown do_ptm = eax & (1 << 6); 4043812db3f7SLen Brown if (do_ptm) 4044812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 40457f5c258eSLen Brown has_hwp = eax & (1 << 7); 40467f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 40477f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 40487f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 40497f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 4050889facbeSLen Brown has_epb = ecx & (1 << 3); 4051889facbeSLen Brown 405296e47158SLen Brown if (!quiet) 4053b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 40547f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 40557f5c258eSLen Brown has_aperf ? "" : "No-", 4056b3a34e93SLen Brown has_turbo ? "" : "No-", 40577f5c258eSLen Brown do_dts ? "" : "No-", 40587f5c258eSLen Brown do_ptm ? "" : "No-", 40597f5c258eSLen Brown has_hwp ? "" : "No-", 40607f5c258eSLen Brown has_hwp_notify ? "" : "No-", 40617f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 40627f5c258eSLen Brown has_hwp_epp ? "" : "No-", 40637f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 40647f5c258eSLen Brown has_epb ? "" : "No-"); 4065103a8feaSLen Brown 406696e47158SLen Brown if (!quiet) 406769807a63SLen Brown decode_misc_enable_msr(); 406869807a63SLen Brown 406933148d67SLen Brown 407096e47158SLen Brown if (max_level >= 0x7 && !quiet) { 4071aa8d8cc7SLen Brown int has_sgx; 4072aa8d8cc7SLen Brown 4073aa8d8cc7SLen Brown ecx = 0; 4074aa8d8cc7SLen Brown 4075aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 4076aa8d8cc7SLen Brown 4077aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 4078aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 4079aa8d8cc7SLen Brown 4080aa8d8cc7SLen Brown if (has_sgx) 4081aa8d8cc7SLen Brown decode_feature_control_msr(); 4082aa8d8cc7SLen Brown } 4083aa8d8cc7SLen Brown 408461a87ba7SLen Brown if (max_level >= 0x15) { 40858a5bdf41SLen Brown unsigned int eax_crystal; 40868a5bdf41SLen Brown unsigned int ebx_tsc; 40878a5bdf41SLen Brown 40888a5bdf41SLen Brown /* 40898a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 40908a5bdf41SLen Brown */ 40918a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 40925aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 40938a5bdf41SLen Brown 40948a5bdf41SLen Brown if (ebx_tsc != 0) { 40958a5bdf41SLen Brown 409696e47158SLen Brown if (!quiet && (ebx != 0)) 4097b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 40988a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 40998a5bdf41SLen Brown 41008a5bdf41SLen Brown if (crystal_hz == 0) 41018a5bdf41SLen Brown switch(model) { 4102869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4103869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4104869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4105869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4106e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 4107e8efbc80SLen Brown break; 4108869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 41097268d407SLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 4110ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 4111ec53e594SLen Brown break; 4112869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4113ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 4114e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 41158a5bdf41SLen Brown break; 41168a5bdf41SLen Brown default: 41178a5bdf41SLen Brown crystal_hz = 0; 41188a5bdf41SLen Brown } 41198a5bdf41SLen Brown 41208a5bdf41SLen Brown if (crystal_hz) { 41218a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 412296e47158SLen Brown if (!quiet) 4123b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 41248a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 41258a5bdf41SLen Brown } 41268a5bdf41SLen Brown } 41278a5bdf41SLen Brown } 412861a87ba7SLen Brown if (max_level >= 0x16) { 412961a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 413061a87ba7SLen Brown 413161a87ba7SLen Brown /* 413261a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 413361a87ba7SLen Brown */ 413461a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 413561a87ba7SLen Brown 41365aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 413796e47158SLen Brown if (!quiet) 4138b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 413961a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 414061a87ba7SLen Brown } 41418a5bdf41SLen Brown 4142b2b34dfeSHubert Chrzaniuk if (has_aperf) 4143b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 4144b2b34dfeSHubert Chrzaniuk 4145812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 4146812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 4147812db3f7SLen Brown 4148812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 4149812db3f7SLen Brown do_nhm_platform_info = 1; 4150812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 4151812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 4152812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 4153812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 4154812db3f7SLen Brown } 4155d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 4156812db3f7SLen Brown 4157812db3f7SLen Brown if (do_snb_cstates) 4158812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 4159812db3f7SLen Brown 41605a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 41610f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 41620f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 41630f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 41640f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 41650f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 41660f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 41670f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 41680f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 41690539ba11SLen Brown if (has_slv_msrs(family, model)) { 41700f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 41710f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 41720f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 41730f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 41740539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 41750539ba11SLen Brown use_c1_residency_msr = 1; 41760539ba11SLen Brown } 41777170a374SLen Brown if (is_dnv(family, model)) { 41787170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 41797170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 41807170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 41817170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 41827170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 41837170a374SLen Brown use_c1_residency_msr = 1; 41847170a374SLen Brown } 418534c76197SLen Brown if (is_skx(family, model)) { 418634c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 418734c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 418834c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 418934c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 419034c76197SLen Brown } 4191ade0ebacSLen Brown if (is_bdx(family, model)) { 4192ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 4193ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 4194ade0ebacSLen Brown } 41950f47c08dSLen Brown if (has_hsw_msrs(family, model)) { 41960f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 41970f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 41980f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 41990f47c08dSLen Brown } 42005a63426eSLen Brown do_irtl_hsw = has_hsw_msrs(family, model); 42010b2bb692SLen Brown do_skl_residency = has_skl_msrs(family, model); 4202144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 4203fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 4204103a8feaSLen Brown 420596e47158SLen Brown if (!quiet) 4206f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 4207f0057310SLen Brown 420896e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 420971616c8eSLen Brown decode_c6_demotion_policy_msr(); 421071616c8eSLen Brown 4211889facbeSLen Brown rapl_probe(family, model); 42123a9a941dSLen Brown perf_limit_reasons_probe(family, model); 4213889facbeSLen Brown 421496e47158SLen Brown if (!quiet) 42151b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 4216fcd17211SLen Brown 421741618e63SLen Brown if (!quiet) 421841618e63SLen Brown dump_sysfs_cstate_config(); 42197293fccdSLen Brown if (!quiet) 42207293fccdSLen Brown dump_sysfs_pstate_config(); 422141618e63SLen Brown 4222a2b7b749SLen Brown if (has_skl_msrs(family, model)) 4223a2b7b749SLen Brown calculate_tsc_tweak(); 4224a2b7b749SLen Brown 4225812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 4226812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 4227fdf676e5SLen Brown 4228812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 4229812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 423027d47356SLen Brown 423196e47158SLen Brown if (!quiet) 423233148d67SLen Brown decode_misc_feature_control(); 423333148d67SLen Brown 4234889facbeSLen Brown return; 4235103a8feaSLen Brown } 4236103a8feaSLen Brown 4237103a8feaSLen Brown 4238103a8feaSLen Brown /* 4239103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 4240103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 4241103a8feaSLen Brown */ 4242103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 4243103a8feaSLen Brown { 4244103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 4245103a8feaSLen Brown return 1; 4246103a8feaSLen Brown else 4247103a8feaSLen Brown return 0; 4248103a8feaSLen Brown } 4249103a8feaSLen Brown 4250103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 4251103a8feaSLen Brown { 4252103a8feaSLen Brown return 0; 4253103a8feaSLen Brown } 4254103a8feaSLen Brown 4255c98d5d94SLen Brown void topology_probe() 4256c98d5d94SLen Brown { 4257c98d5d94SLen Brown int i; 4258c98d5d94SLen Brown int max_core_id = 0; 4259c98d5d94SLen Brown int max_package_id = 0; 4260c98d5d94SLen Brown int max_siblings = 0; 4261c98d5d94SLen Brown struct cpu_topology { 4262c98d5d94SLen Brown int core_id; 4263c98d5d94SLen Brown int physical_package_id; 4264c98d5d94SLen Brown } *cpus; 4265c98d5d94SLen Brown 4266c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 4267c98d5d94SLen Brown topo.num_cpus = 0; 4268c98d5d94SLen Brown topo.max_cpu_num = 0; 4269c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 4270c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 4271812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 4272c98d5d94SLen Brown 4273d8af6f5fSLen Brown if (debug > 1) 4274b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 4275c98d5d94SLen Brown 4276c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 4277b2c95d90SJosh Triplett if (cpus == NULL) 4278b2c95d90SJosh Triplett err(1, "calloc cpus"); 4279c98d5d94SLen Brown 4280c98d5d94SLen Brown /* 4281c98d5d94SLen Brown * Allocate and initialize cpu_present_set 4282c98d5d94SLen Brown */ 4283c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4284b2c95d90SJosh Triplett if (cpu_present_set == NULL) 4285b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4286c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4287c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 4288c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 4289c98d5d94SLen Brown 4290c98d5d94SLen Brown /* 42911ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 42921ef7d21aSLen Brown */ 42931ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 42941ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 42951ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 42961ef7d21aSLen Brown err(1, "cpu%d not present", i); 42971ef7d21aSLen Brown } 42981ef7d21aSLen Brown 42991ef7d21aSLen Brown /* 4300c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 4301c98d5d94SLen Brown */ 4302c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4303b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 4304b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4305c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4306c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 4307c98d5d94SLen Brown 4308c98d5d94SLen Brown 4309c98d5d94SLen Brown /* 4310c98d5d94SLen Brown * For online cpus 4311c98d5d94SLen Brown * find max_core_id, max_package_id 4312c98d5d94SLen Brown */ 4313c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 4314c98d5d94SLen Brown int siblings; 4315c98d5d94SLen Brown 4316c98d5d94SLen Brown if (cpu_is_not_present(i)) { 4317d8af6f5fSLen Brown if (debug > 1) 4318b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 4319c98d5d94SLen Brown continue; 4320c98d5d94SLen Brown } 4321c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 4322c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 4323c98d5d94SLen Brown max_core_id = cpus[i].core_id; 4324c98d5d94SLen Brown 4325c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 4326c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 4327c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 4328c98d5d94SLen Brown 4329c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 4330c98d5d94SLen Brown if (siblings > max_siblings) 4331c98d5d94SLen Brown max_siblings = siblings; 4332d8af6f5fSLen Brown if (debug > 1) 4333b7d8c148SLen Brown fprintf(outf, "cpu %d pkg %d core %d\n", 4334c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 4335c98d5d94SLen Brown } 4336c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 4337d8af6f5fSLen Brown if (debug > 1) 4338b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 4339c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 43400f47c08dSLen Brown if (!summary_only && topo.num_cores_per_pkg > 1) 4341812db3f7SLen Brown BIC_PRESENT(BIC_Core); 4342c98d5d94SLen Brown 4343c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 4344d8af6f5fSLen Brown if (debug > 1) 4345b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 4346c98d5d94SLen Brown max_package_id, topo.num_packages); 43477da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 4348812db3f7SLen Brown BIC_PRESENT(BIC_Package); 4349c98d5d94SLen Brown 4350c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 4351d8af6f5fSLen Brown if (debug > 1) 4352b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 4353c98d5d94SLen Brown 4354c98d5d94SLen Brown free(cpus); 4355c98d5d94SLen Brown } 4356c98d5d94SLen Brown 4357c98d5d94SLen Brown void 4358c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 4359c98d5d94SLen Brown { 4360c98d5d94SLen Brown int i; 4361c98d5d94SLen Brown 4362c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 4363678a3bd1SLen Brown topo.num_packages, sizeof(struct thread_data)); 4364c98d5d94SLen Brown if (*t == NULL) 4365c98d5d94SLen Brown goto error; 4366c98d5d94SLen Brown 4367c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 4368c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 4369c98d5d94SLen Brown (*t)[i].cpu_id = -1; 4370c98d5d94SLen Brown 4371c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 4372678a3bd1SLen Brown sizeof(struct core_data)); 4373c98d5d94SLen Brown if (*c == NULL) 4374c98d5d94SLen Brown goto error; 4375c98d5d94SLen Brown 4376c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 4377c98d5d94SLen Brown (*c)[i].core_id = -1; 4378c98d5d94SLen Brown 4379678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 4380c98d5d94SLen Brown if (*p == NULL) 4381c98d5d94SLen Brown goto error; 4382c98d5d94SLen Brown 4383c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 4384c98d5d94SLen Brown (*p)[i].package_id = i; 4385c98d5d94SLen Brown 4386c98d5d94SLen Brown return; 4387c98d5d94SLen Brown error: 4388b2c95d90SJosh Triplett err(1, "calloc counters"); 4389c98d5d94SLen Brown } 4390c98d5d94SLen Brown /* 4391c98d5d94SLen Brown * init_counter() 4392c98d5d94SLen Brown * 4393c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 4394c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 4395c98d5d94SLen Brown * 4396c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 4397c98d5d94SLen Brown */ 4398c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 4399c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 4400c98d5d94SLen Brown int pkg_num, int cpu_id) 4401c98d5d94SLen Brown { 4402c98d5d94SLen Brown struct thread_data *t; 4403c98d5d94SLen Brown struct core_data *c; 4404c98d5d94SLen Brown struct pkg_data *p; 4405c98d5d94SLen Brown 4406c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 4407c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 4408c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 4409c98d5d94SLen Brown 4410c98d5d94SLen Brown t->cpu_id = cpu_id; 4411c98d5d94SLen Brown if (thread_num == 0) { 4412c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 4413c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 4414c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 4415c98d5d94SLen Brown } 4416c98d5d94SLen Brown 4417c98d5d94SLen Brown c->core_id = core_num; 4418c98d5d94SLen Brown p->package_id = pkg_num; 4419c98d5d94SLen Brown } 4420c98d5d94SLen Brown 4421c98d5d94SLen Brown 4422c98d5d94SLen Brown int initialize_counters(int cpu_id) 4423c98d5d94SLen Brown { 4424c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 4425c98d5d94SLen Brown 4426c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 4427c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 4428e275b388SDasaratharaman Chandramouli my_thread_id = get_cpu_position_in_core(cpu_id); 4429e275b388SDasaratharaman Chandramouli if (!my_thread_id) 4430c98d5d94SLen Brown topo.num_cores++; 4431c98d5d94SLen Brown 4432c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 4433c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 4434c98d5d94SLen Brown return 0; 4435c98d5d94SLen Brown } 4436c98d5d94SLen Brown 4437c98d5d94SLen Brown void allocate_output_buffer() 4438c98d5d94SLen Brown { 44393b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 4440c98d5d94SLen Brown outp = output_buffer; 4441b2c95d90SJosh Triplett if (outp == NULL) 4442b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 4443c98d5d94SLen Brown } 444436229897SLen Brown void allocate_fd_percpu(void) 444536229897SLen Brown { 444601a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 444736229897SLen Brown if (fd_percpu == NULL) 444836229897SLen Brown err(-1, "calloc fd_percpu"); 444936229897SLen Brown } 4450562a2d37SLen Brown void allocate_irq_buffers(void) 4451562a2d37SLen Brown { 4452562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 4453562a2d37SLen Brown if (irq_column_2_cpu == NULL) 4454562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 4455c98d5d94SLen Brown 445601a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 4457562a2d37SLen Brown if (irqs_per_cpu == NULL) 445801a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 4459562a2d37SLen Brown } 4460c98d5d94SLen Brown void setup_all_buffers(void) 4461c98d5d94SLen Brown { 4462c98d5d94SLen Brown topology_probe(); 4463562a2d37SLen Brown allocate_irq_buffers(); 446436229897SLen Brown allocate_fd_percpu(); 4465c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 4466c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 4467c98d5d94SLen Brown allocate_output_buffer(); 4468c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 4469c98d5d94SLen Brown } 44703b4d5c7fSAndy Shevchenko 44717ce7d5deSPrarit Bhargava void set_base_cpu(void) 44727ce7d5deSPrarit Bhargava { 44737ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 44747ce7d5deSPrarit Bhargava if (base_cpu < 0) 44757ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 44767ce7d5deSPrarit Bhargava 44777ce7d5deSPrarit Bhargava if (debug > 1) 4478b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 44797ce7d5deSPrarit Bhargava } 44807ce7d5deSPrarit Bhargava 4481103a8feaSLen Brown void turbostat_init() 4482103a8feaSLen Brown { 44837ce7d5deSPrarit Bhargava setup_all_buffers(); 44847ce7d5deSPrarit Bhargava set_base_cpu(); 4485103a8feaSLen Brown check_dev_msr(); 448698481e79SLen Brown check_permissions(); 4487fcd17211SLen Brown process_cpuid(); 4488103a8feaSLen Brown 4489103a8feaSLen Brown 449096e47158SLen Brown if (!quiet) 44917f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 44927f5c258eSLen Brown 449396e47158SLen Brown if (!quiet) 4494889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 4495889facbeSLen Brown 449696e47158SLen Brown if (!quiet) 44973a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 44983a9a941dSLen Brown 449996e47158SLen Brown if (!quiet) 4500889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 4501889facbeSLen Brown 4502889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 4503889facbeSLen Brown 450496e47158SLen Brown if (!quiet) 4505889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 45065a63426eSLen Brown 450796e47158SLen Brown if (!quiet && do_irtl_snb) 45085a63426eSLen Brown print_irtl(); 4509103a8feaSLen Brown } 4510103a8feaSLen Brown 4511103a8feaSLen Brown int fork_it(char **argv) 4512103a8feaSLen Brown { 4513103a8feaSLen Brown pid_t child_pid; 4514d91bb17cSLen Brown int status; 4515d15cf7c1SLen Brown 4516218f0e8dSLen Brown snapshot_proc_sysfs_files(); 4517d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 4518d91bb17cSLen Brown if (status) 4519d91bb17cSLen Brown exit(status); 4520d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 4521d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 4522103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 4523103a8feaSLen Brown 4524103a8feaSLen Brown child_pid = fork(); 4525103a8feaSLen Brown if (!child_pid) { 4526103a8feaSLen Brown /* child */ 4527103a8feaSLen Brown execvp(argv[0], argv); 45280815a3d0SLen Brown err(errno, "exec %s", argv[0]); 4529103a8feaSLen Brown } else { 4530103a8feaSLen Brown 4531103a8feaSLen Brown /* parent */ 4532b2c95d90SJosh Triplett if (child_pid == -1) 4533b2c95d90SJosh Triplett err(1, "fork"); 4534103a8feaSLen Brown 4535103a8feaSLen Brown signal(SIGINT, SIG_IGN); 4536103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 4537b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 4538b2c95d90SJosh Triplett err(status, "waitpid"); 4539103a8feaSLen Brown } 4540c98d5d94SLen Brown /* 4541c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 4542c98d5d94SLen Brown * because re-starting is problematic when forking 4543c98d5d94SLen Brown */ 4544218f0e8dSLen Brown snapshot_proc_sysfs_files(); 4545c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 4546103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 4547103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 4548ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 4549ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 4550ba3dec99SLen Brown else { 4551c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 4552c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 4553ba3dec99SLen Brown } 4554103a8feaSLen Brown 4555b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 4556b7d8c148SLen Brown 4557b7d8c148SLen Brown flush_output_stderr(); 4558103a8feaSLen Brown 4559d91bb17cSLen Brown return status; 4560103a8feaSLen Brown } 4561103a8feaSLen Brown 45623b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 45633b4d5c7fSAndy Shevchenko { 45643b4d5c7fSAndy Shevchenko int status; 45653b4d5c7fSAndy Shevchenko 4566218f0e8dSLen Brown snapshot_proc_sysfs_files(); 45673b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 45683b4d5c7fSAndy Shevchenko if (status) 45693b4d5c7fSAndy Shevchenko return status; 45703b4d5c7fSAndy Shevchenko 45713b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 45723b4d5c7fSAndy Shevchenko if (status) 45733b4d5c7fSAndy Shevchenko return status; 45743b4d5c7fSAndy Shevchenko 4575b7d8c148SLen Brown flush_output_stdout(); 45763b4d5c7fSAndy Shevchenko 45773b4d5c7fSAndy Shevchenko return status; 45783b4d5c7fSAndy Shevchenko } 45793b4d5c7fSAndy Shevchenko 4580d8af6f5fSLen Brown void print_version() { 4581*5f9bf02aSLen Brown fprintf(outf, "turbostat version 17.04.12" 4582d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 4583d8af6f5fSLen Brown } 4584d8af6f5fSLen Brown 4585495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 4586495c7654SLen Brown unsigned int width, enum counter_scope scope, 458741618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 4588388e9c81SLen Brown { 4589388e9c81SLen Brown struct msr_counter *msrp; 4590388e9c81SLen Brown 4591388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 4592388e9c81SLen Brown if (msrp == NULL) { 4593388e9c81SLen Brown perror("calloc"); 4594388e9c81SLen Brown exit(1); 4595388e9c81SLen Brown } 4596388e9c81SLen Brown 4597388e9c81SLen Brown msrp->msr_num = msr_num; 4598388e9c81SLen Brown strncpy(msrp->name, name, NAME_BYTES); 4599495c7654SLen Brown if (path) 4600495c7654SLen Brown strncpy(msrp->path, path, PATH_BYTES); 4601388e9c81SLen Brown msrp->width = width; 4602388e9c81SLen Brown msrp->type = type; 4603388e9c81SLen Brown msrp->format = format; 460441618e63SLen Brown msrp->flags = flags; 4605388e9c81SLen Brown 4606388e9c81SLen Brown switch (scope) { 4607388e9c81SLen Brown 4608388e9c81SLen Brown case SCOPE_CPU: 4609388e9c81SLen Brown msrp->next = sys.tp; 4610388e9c81SLen Brown sys.tp = msrp; 4611678a3bd1SLen Brown sys.added_thread_counters++; 4612678a3bd1SLen Brown if (sys.added_thread_counters > MAX_ADDED_COUNTERS) { 4613678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 4614678a3bd1SLen Brown MAX_ADDED_COUNTERS); 4615678a3bd1SLen Brown exit(-1); 4616678a3bd1SLen Brown } 4617388e9c81SLen Brown break; 4618388e9c81SLen Brown 4619388e9c81SLen Brown case SCOPE_CORE: 4620388e9c81SLen Brown msrp->next = sys.cp; 4621388e9c81SLen Brown sys.cp = msrp; 4622678a3bd1SLen Brown sys.added_core_counters++; 4623678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 4624678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 4625678a3bd1SLen Brown MAX_ADDED_COUNTERS); 4626678a3bd1SLen Brown exit(-1); 4627678a3bd1SLen Brown } 4628388e9c81SLen Brown break; 4629388e9c81SLen Brown 4630388e9c81SLen Brown case SCOPE_PACKAGE: 4631388e9c81SLen Brown msrp->next = sys.pp; 4632388e9c81SLen Brown sys.pp = msrp; 4633678a3bd1SLen Brown sys.added_package_counters++; 4634678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 4635678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 4636678a3bd1SLen Brown MAX_ADDED_COUNTERS); 4637678a3bd1SLen Brown exit(-1); 4638678a3bd1SLen Brown } 4639388e9c81SLen Brown break; 4640388e9c81SLen Brown } 4641388e9c81SLen Brown 4642388e9c81SLen Brown return 0; 4643388e9c81SLen Brown } 4644388e9c81SLen Brown 4645388e9c81SLen Brown void parse_add_command(char *add_command) 4646388e9c81SLen Brown { 4647388e9c81SLen Brown int msr_num = 0; 4648495c7654SLen Brown char *path = NULL; 46490f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 4650388e9c81SLen Brown int width = 64; 4651388e9c81SLen Brown int fail = 0; 4652388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 4653388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 4654388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 4655388e9c81SLen Brown 4656388e9c81SLen Brown while (add_command) { 4657388e9c81SLen Brown 4658388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 4659388e9c81SLen Brown goto next; 4660388e9c81SLen Brown 4661388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 4662388e9c81SLen Brown goto next; 4663388e9c81SLen Brown 4664495c7654SLen Brown if (*add_command == '/') { 4665495c7654SLen Brown path = add_command; 4666495c7654SLen Brown goto next; 4667495c7654SLen Brown } 4668495c7654SLen Brown 4669388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 4670388e9c81SLen Brown if ((width == 32) || (width == 64)) 4671388e9c81SLen Brown goto next; 4672388e9c81SLen Brown width = 64; 4673388e9c81SLen Brown } 4674388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 4675388e9c81SLen Brown scope = SCOPE_CPU; 4676388e9c81SLen Brown goto next; 4677388e9c81SLen Brown } 4678388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 4679388e9c81SLen Brown scope = SCOPE_CORE; 4680388e9c81SLen Brown goto next; 4681388e9c81SLen Brown } 4682388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 4683388e9c81SLen Brown scope = SCOPE_PACKAGE; 4684388e9c81SLen Brown goto next; 4685388e9c81SLen Brown } 4686388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 4687388e9c81SLen Brown type = COUNTER_CYCLES; 4688388e9c81SLen Brown goto next; 4689388e9c81SLen Brown } 4690388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 4691388e9c81SLen Brown type = COUNTER_SECONDS; 4692388e9c81SLen Brown goto next; 4693388e9c81SLen Brown } 469441618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 469541618e63SLen Brown type = COUNTER_USEC; 469641618e63SLen Brown goto next; 469741618e63SLen Brown } 4698388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 4699388e9c81SLen Brown format = FORMAT_RAW; 4700388e9c81SLen Brown goto next; 4701388e9c81SLen Brown } 4702388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 4703388e9c81SLen Brown format = FORMAT_DELTA; 4704388e9c81SLen Brown goto next; 4705388e9c81SLen Brown } 4706388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 4707388e9c81SLen Brown format = FORMAT_PERCENT; 4708388e9c81SLen Brown goto next; 4709388e9c81SLen Brown } 4710388e9c81SLen Brown 4711388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 4712388e9c81SLen Brown char *eos; 4713388e9c81SLen Brown 4714388e9c81SLen Brown eos = strchr(name_buffer, ','); 4715388e9c81SLen Brown if (eos) 4716388e9c81SLen Brown *eos = '\0'; 4717388e9c81SLen Brown goto next; 4718388e9c81SLen Brown } 4719388e9c81SLen Brown 4720388e9c81SLen Brown next: 4721388e9c81SLen Brown add_command = strchr(add_command, ','); 4722495c7654SLen Brown if (add_command) { 4723495c7654SLen Brown *add_command = '\0'; 4724388e9c81SLen Brown add_command++; 4725495c7654SLen Brown } 4726388e9c81SLen Brown 4727388e9c81SLen Brown } 4728495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 4729495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 4730388e9c81SLen Brown fail++; 4731388e9c81SLen Brown } 4732388e9c81SLen Brown 4733388e9c81SLen Brown /* generate default column header */ 4734388e9c81SLen Brown if (*name_buffer == '\0') { 4735388e9c81SLen Brown if (width == 32) 47365f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 4737388e9c81SLen Brown else 47385f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 4739388e9c81SLen Brown } 4740388e9c81SLen Brown 474141618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 4742388e9c81SLen Brown fail++; 4743388e9c81SLen Brown 4744388e9c81SLen Brown if (fail) { 4745388e9c81SLen Brown help(); 4746388e9c81SLen Brown exit(1); 4747388e9c81SLen Brown } 4748388e9c81SLen Brown } 474941618e63SLen Brown 4750dd778a5eSLen Brown int is_deferred_skip(char *name) 4751dd778a5eSLen Brown { 4752dd778a5eSLen Brown int i; 4753dd778a5eSLen Brown 4754dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 4755dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 4756dd778a5eSLen Brown return 1; 4757dd778a5eSLen Brown return 0; 4758dd778a5eSLen Brown } 4759dd778a5eSLen Brown 476041618e63SLen Brown void probe_sysfs(void) 476141618e63SLen Brown { 476241618e63SLen Brown char path[64]; 476341618e63SLen Brown char name_buf[16]; 476441618e63SLen Brown FILE *input; 476541618e63SLen Brown int state; 476641618e63SLen Brown char *sp; 476741618e63SLen Brown 476841618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 476941618e63SLen Brown return; 477041618e63SLen Brown 477141618e63SLen Brown for (state = 10; state > 0; --state) { 477241618e63SLen Brown 477341618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 477441618e63SLen Brown base_cpu, state); 477541618e63SLen Brown input = fopen(path, "r"); 477641618e63SLen Brown if (input == NULL) 477741618e63SLen Brown continue; 477841618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 477941618e63SLen Brown 478041618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 478141618e63SLen Brown sp = strchr(name_buf, '-'); 478241618e63SLen Brown if (!sp) 478341618e63SLen Brown sp = strchrnul(name_buf, '\n'); 478441618e63SLen Brown *sp = '%'; 478541618e63SLen Brown *(sp + 1) = '\0'; 478641618e63SLen Brown 478741618e63SLen Brown fclose(input); 478841618e63SLen Brown 478941618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 479041618e63SLen Brown 4791dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 4792dd778a5eSLen Brown continue; 4793dd778a5eSLen Brown 479441618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 479541618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 479641618e63SLen Brown } 479741618e63SLen Brown 479841618e63SLen Brown for (state = 10; state > 0; --state) { 479941618e63SLen Brown 480041618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 480141618e63SLen Brown base_cpu, state); 480241618e63SLen Brown input = fopen(path, "r"); 480341618e63SLen Brown if (input == NULL) 480441618e63SLen Brown continue; 480541618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 480641618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 480741618e63SLen Brown sp = strchr(name_buf, '-'); 480841618e63SLen Brown if (!sp) 480941618e63SLen Brown sp = strchrnul(name_buf, '\n'); 481041618e63SLen Brown *sp = '\0'; 481141618e63SLen Brown fclose(input); 481241618e63SLen Brown 481341618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 481441618e63SLen Brown 4815dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 4816dd778a5eSLen Brown continue; 4817dd778a5eSLen Brown 481841618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 481941618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 482041618e63SLen Brown } 482141618e63SLen Brown 482241618e63SLen Brown } 482341618e63SLen Brown 48241ef7d21aSLen Brown 48251ef7d21aSLen Brown /* 48261ef7d21aSLen Brown * parse cpuset with following syntax 48271ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 48281ef7d21aSLen Brown */ 48291ef7d21aSLen Brown void parse_cpu_command(char *optarg) 48301ef7d21aSLen Brown { 48311ef7d21aSLen Brown unsigned int start, end; 48321ef7d21aSLen Brown char *next; 48331ef7d21aSLen Brown 48344e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 48354e4e1e7cSLen Brown if (cpu_subset) 48364e4e1e7cSLen Brown goto error; 48374e4e1e7cSLen Brown show_core_only++; 48384e4e1e7cSLen Brown return; 48394e4e1e7cSLen Brown } 48404e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 48414e4e1e7cSLen Brown if (cpu_subset) 48424e4e1e7cSLen Brown goto error; 48434e4e1e7cSLen Brown show_pkg_only++; 48444e4e1e7cSLen Brown return; 48454e4e1e7cSLen Brown } 48464e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 48474e4e1e7cSLen Brown goto error; 48484e4e1e7cSLen Brown 48491ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 48501ef7d21aSLen Brown if (cpu_subset == NULL) 48511ef7d21aSLen Brown err(3, "CPU_ALLOC"); 48521ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 48531ef7d21aSLen Brown 48541ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 48551ef7d21aSLen Brown 48561ef7d21aSLen Brown next = optarg; 48571ef7d21aSLen Brown 48581ef7d21aSLen Brown while (next && *next) { 48591ef7d21aSLen Brown 48601ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 48611ef7d21aSLen Brown goto error; 48621ef7d21aSLen Brown 48631ef7d21aSLen Brown start = strtoul(next, &next, 10); 48641ef7d21aSLen Brown 48651ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 48661ef7d21aSLen Brown goto error; 48671ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 48681ef7d21aSLen Brown 48691ef7d21aSLen Brown if (*next == '\0') 48701ef7d21aSLen Brown break; 48711ef7d21aSLen Brown 48721ef7d21aSLen Brown if (*next == ',') { 48731ef7d21aSLen Brown next += 1; 48741ef7d21aSLen Brown continue; 48751ef7d21aSLen Brown } 48761ef7d21aSLen Brown 48771ef7d21aSLen Brown if (*next == '-') { 48781ef7d21aSLen Brown next += 1; /* start range */ 48791ef7d21aSLen Brown } else if (*next == '.') { 48801ef7d21aSLen Brown next += 1; 48811ef7d21aSLen Brown if (*next == '.') 48821ef7d21aSLen Brown next += 1; /* start range */ 48831ef7d21aSLen Brown else 48841ef7d21aSLen Brown goto error; 48851ef7d21aSLen Brown } 48861ef7d21aSLen Brown 48871ef7d21aSLen Brown end = strtoul(next, &next, 10); 48881ef7d21aSLen Brown if (end <= start) 48891ef7d21aSLen Brown goto error; 48901ef7d21aSLen Brown 48911ef7d21aSLen Brown while (++start <= end) { 48921ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 48931ef7d21aSLen Brown goto error; 48941ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 48951ef7d21aSLen Brown } 48961ef7d21aSLen Brown 48971ef7d21aSLen Brown if (*next == ',') 48981ef7d21aSLen Brown next += 1; 48991ef7d21aSLen Brown else if (*next != '\0') 49001ef7d21aSLen Brown goto error; 49011ef7d21aSLen Brown } 49021ef7d21aSLen Brown 49031ef7d21aSLen Brown return; 49041ef7d21aSLen Brown 49051ef7d21aSLen Brown error: 49064e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 49074e4e1e7cSLen Brown help(); 49081ef7d21aSLen Brown exit(-1); 49091ef7d21aSLen Brown } 49101ef7d21aSLen Brown 4911812db3f7SLen Brown int shown; 4912812db3f7SLen Brown /* 4913812db3f7SLen Brown * parse_show_hide() - process cmdline to set default counter action 4914812db3f7SLen Brown */ 4915812db3f7SLen Brown void parse_show_hide(char *optarg, enum show_hide_mode new_mode) 4916812db3f7SLen Brown { 4917812db3f7SLen Brown /* 4918812db3f7SLen Brown * --show: show only those specified 4919812db3f7SLen Brown * The 1st invocation will clear and replace the enabled mask 4920812db3f7SLen Brown * subsequent invocations can add to it. 4921812db3f7SLen Brown */ 4922812db3f7SLen Brown if (new_mode == SHOW_LIST) { 4923812db3f7SLen Brown if (shown == 0) 4924dd778a5eSLen Brown bic_enabled = bic_lookup(optarg, new_mode); 4925812db3f7SLen Brown else 4926dd778a5eSLen Brown bic_enabled |= bic_lookup(optarg, new_mode); 4927812db3f7SLen Brown shown = 1; 4928812db3f7SLen Brown 4929812db3f7SLen Brown return; 4930812db3f7SLen Brown } 4931812db3f7SLen Brown 4932812db3f7SLen Brown /* 4933812db3f7SLen Brown * --hide: do not show those specified 4934812db3f7SLen Brown * multiple invocations simply clear more bits in enabled mask 4935812db3f7SLen Brown */ 4936dd778a5eSLen Brown bic_enabled &= ~bic_lookup(optarg, new_mode); 493741618e63SLen Brown 4938812db3f7SLen Brown } 4939812db3f7SLen Brown 4940103a8feaSLen Brown void cmdline(int argc, char **argv) 4941103a8feaSLen Brown { 4942103a8feaSLen Brown int opt; 4943d8af6f5fSLen Brown int option_index = 0; 4944d8af6f5fSLen Brown static struct option long_options[] = { 4945388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 49461ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 4947d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 494896e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 4949d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 4950d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 4951812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 4952d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 4953c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 4954b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 495596e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 4956812db3f7SLen Brown {"show", required_argument, 0, 's'}, 4957d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 4958d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 4959d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 4960d8af6f5fSLen Brown {0, 0, 0, 0 } 4961d8af6f5fSLen Brown }; 4962103a8feaSLen Brown 4963103a8feaSLen Brown progname = argv[0]; 4964103a8feaSLen Brown 49654e4e1e7cSLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:qST:v", 4966d8af6f5fSLen Brown long_options, &option_index)) != -1) { 4967103a8feaSLen Brown switch (opt) { 4968388e9c81SLen Brown case 'a': 4969388e9c81SLen Brown parse_add_command(optarg); 4970388e9c81SLen Brown break; 49711ef7d21aSLen Brown case 'c': 49721ef7d21aSLen Brown parse_cpu_command(optarg); 49731ef7d21aSLen Brown break; 4974d8af6f5fSLen Brown case 'D': 4975d8af6f5fSLen Brown dump_only++; 49768e180f3cSLen Brown break; 4977d8af6f5fSLen Brown case 'd': 4978d8af6f5fSLen Brown debug++; 49792f32edf1SLen Brown break; 4980812db3f7SLen Brown case 'H': 4981812db3f7SLen Brown parse_show_hide(optarg, HIDE_LIST); 4982812db3f7SLen Brown break; 4983d8af6f5fSLen Brown case 'h': 4984d8af6f5fSLen Brown default: 4985d8af6f5fSLen Brown help(); 4986d8af6f5fSLen Brown exit(1); 4987d8af6f5fSLen Brown case 'i': 49882a0609c0SLen Brown { 49892a0609c0SLen Brown double interval = strtod(optarg, NULL); 49902a0609c0SLen Brown 49912a0609c0SLen Brown if (interval < 0.001) { 4992b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 49932a0609c0SLen Brown interval); 49942a0609c0SLen Brown exit(2); 49952a0609c0SLen Brown } 49962a0609c0SLen Brown 49972a0609c0SLen Brown interval_ts.tv_sec = interval; 49982a0609c0SLen Brown interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 49992a0609c0SLen Brown } 5000889facbeSLen Brown break; 50015c56be9aSDirk Brandewie case 'J': 50025c56be9aSDirk Brandewie rapl_joules++; 50035c56be9aSDirk Brandewie break; 5004c8ade361SLen Brown case 'l': 5005c8ade361SLen Brown list_header_only++; 5006c8ade361SLen Brown quiet++; 5007c8ade361SLen Brown break; 5008b7d8c148SLen Brown case 'o': 5009b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 5010b7d8c148SLen Brown break; 501196e47158SLen Brown case 'q': 501296e47158SLen Brown quiet = 1; 501396e47158SLen Brown break; 5014812db3f7SLen Brown case 's': 5015812db3f7SLen Brown parse_show_hide(optarg, SHOW_LIST); 5016812db3f7SLen Brown break; 5017d8af6f5fSLen Brown case 'S': 5018d8af6f5fSLen Brown summary_only++; 5019d8af6f5fSLen Brown break; 5020d8af6f5fSLen Brown case 'T': 5021d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 5022d8af6f5fSLen Brown break; 5023d8af6f5fSLen Brown case 'v': 5024d8af6f5fSLen Brown print_version(); 5025d8af6f5fSLen Brown exit(0); 5026d8af6f5fSLen Brown break; 5027103a8feaSLen Brown } 5028103a8feaSLen Brown } 5029103a8feaSLen Brown } 5030103a8feaSLen Brown 5031103a8feaSLen Brown int main(int argc, char **argv) 5032103a8feaSLen Brown { 5033b7d8c148SLen Brown outf = stderr; 5034b7d8c148SLen Brown 5035103a8feaSLen Brown cmdline(argc, argv); 5036103a8feaSLen Brown 503796e47158SLen Brown if (!quiet) 5038d8af6f5fSLen Brown print_version(); 5039103a8feaSLen Brown 504041618e63SLen Brown probe_sysfs(); 504141618e63SLen Brown 5042103a8feaSLen Brown turbostat_init(); 5043103a8feaSLen Brown 50443b4d5c7fSAndy Shevchenko /* dump counters and exit */ 50453b4d5c7fSAndy Shevchenko if (dump_only) 50463b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 50473b4d5c7fSAndy Shevchenko 5048c8ade361SLen Brown /* list header and exit */ 5049c8ade361SLen Brown if (list_header_only) { 5050c8ade361SLen Brown print_header(","); 5051c8ade361SLen Brown flush_output_stdout(); 5052c8ade361SLen Brown return 0; 5053c8ade361SLen Brown } 5054c8ade361SLen Brown 5055103a8feaSLen Brown /* 5056103a8feaSLen Brown * if any params left, it must be a command to fork 5057103a8feaSLen Brown */ 5058103a8feaSLen Brown if (argc - optind) 5059103a8feaSLen Brown return fork_it(argv + optind); 5060103a8feaSLen Brown else 5061103a8feaSLen Brown turbostat_loop(); 5062103a8feaSLen Brown 5063103a8feaSLen Brown return 0; 5064103a8feaSLen Brown } 5065