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; 53d8af6f5fSLen Brown unsigned int rapl_joules; 54d8af6f5fSLen Brown unsigned int summary_only; 55d8af6f5fSLen Brown unsigned int dump_only; 56103a8feaSLen Brown unsigned int do_snb_cstates; 57fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 580b2bb692SLen Brown unsigned int do_skl_residency; 59144b44b1SLen Brown unsigned int do_slm_cstates; 60144b44b1SLen Brown unsigned int use_c1_residency_msr; 61103a8feaSLen Brown unsigned int has_aperf; 62889facbeSLen Brown unsigned int has_epb; 635a63426eSLen Brown unsigned int do_irtl_snb; 645a63426eSLen Brown unsigned int do_irtl_hsw; 65fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 66103a8feaSLen Brown unsigned int genuine_intel; 67103a8feaSLen Brown unsigned int has_invariant_tsc; 68d7899447SLen Brown unsigned int do_nhm_platform_info; 69cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 70b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 71103a8feaSLen Brown double bclk; 72a2b7b749SLen Brown double base_hz; 7321ed5574SLen Brown unsigned int has_base_hz; 74a2b7b749SLen Brown double tsc_tweak = 1.0; 75c98d5d94SLen Brown unsigned int show_pkg_only; 76c98d5d94SLen Brown unsigned int show_core_only; 77c98d5d94SLen Brown char *output_buffer, *outp; 78889facbeSLen Brown unsigned int do_rapl; 79889facbeSLen Brown unsigned int do_dts; 80889facbeSLen Brown unsigned int do_ptm; 81fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 8227d47356SLen Brown unsigned int gfx_cur_mhz; 83889facbeSLen Brown unsigned int tcc_activation_temp; 84889facbeSLen Brown unsigned int tcc_activation_temp_override; 8540ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 8640ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 87889facbeSLen Brown double rapl_joule_counter_range; 883a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 893a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 903a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 918a5bdf41SLen Brown unsigned int crystal_hz; 928a5bdf41SLen Brown unsigned long long tsc_hz; 937ce7d5deSPrarit Bhargava int base_cpu; 9421ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 957f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 967f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 977f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 987f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 997f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1007f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 10133148d67SLen Brown unsigned int has_misc_feature_control; 102889facbeSLen Brown 103889facbeSLen Brown #define RAPL_PKG (1 << 0) 104e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 105e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 106e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 107e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 108e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 109e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 110e6f9bb3cSLen Brown 111889facbeSLen Brown #define RAPL_DRAM (1 << 3) 112e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 113e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 114e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 115e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1160b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1170b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 118e6f9bb3cSLen Brown 1199148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 120e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1210b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 122e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 123e6f9bb3cSLen Brown 1240b2bb692SLen Brown #define RAPL_GFX (1 << 8) 125e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 126e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 127e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1289148494cSJacob Pan 1299148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1309148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1319148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 132889facbeSLen Brown #define TJMAX_DEFAULT 100 133889facbeSLen Brown 134889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 135103a8feaSLen Brown 136388e9c81SLen Brown /* 137388e9c81SLen Brown * buffer size used by sscanf() for added column names 138388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 139388e9c81SLen Brown */ 140388e9c81SLen Brown #define NAME_BYTES 20 141495c7654SLen Brown #define PATH_BYTES 128 142388e9c81SLen Brown 143103a8feaSLen Brown int backwards_count; 144103a8feaSLen Brown char *progname; 145103a8feaSLen Brown 146*1ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 147*1ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 148*1ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 149678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16 150c98d5d94SLen Brown 151c98d5d94SLen Brown struct thread_data { 152c98d5d94SLen Brown unsigned long long tsc; 153c98d5d94SLen Brown unsigned long long aperf; 154c98d5d94SLen Brown unsigned long long mperf; 155144b44b1SLen Brown unsigned long long c1; 156562a2d37SLen Brown unsigned int irq_count; 1571ed51011SLen Brown unsigned int smi_count; 158c98d5d94SLen Brown unsigned int cpu_id; 159c98d5d94SLen Brown unsigned int flags; 160c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 161c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 162678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 163c98d5d94SLen Brown } *thread_even, *thread_odd; 164c98d5d94SLen Brown 165c98d5d94SLen Brown struct core_data { 166c98d5d94SLen Brown unsigned long long c3; 167c98d5d94SLen Brown unsigned long long c6; 168c98d5d94SLen Brown unsigned long long c7; 1690539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 170889facbeSLen Brown unsigned int core_temp_c; 171c98d5d94SLen Brown unsigned int core_id; 172678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 173c98d5d94SLen Brown } *core_even, *core_odd; 174c98d5d94SLen Brown 175c98d5d94SLen Brown struct pkg_data { 176c98d5d94SLen Brown unsigned long long pc2; 177c98d5d94SLen Brown unsigned long long pc3; 178c98d5d94SLen Brown unsigned long long pc6; 179c98d5d94SLen Brown unsigned long long pc7; 180ca58710fSKristen Carlson Accardi unsigned long long pc8; 181ca58710fSKristen Carlson Accardi unsigned long long pc9; 182ca58710fSKristen Carlson Accardi unsigned long long pc10; 1830b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 1840b2bb692SLen Brown unsigned long long pkg_any_core_c0; 1850b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 1860b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 1879185e988SLen Brown long long gfx_rc6_ms; 18827d47356SLen Brown unsigned int gfx_mhz; 189c98d5d94SLen Brown unsigned int package_id; 190889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 191889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 192889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 193889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 194889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 195889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 196889facbeSLen Brown unsigned int pkg_temp_c; 197678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 198c98d5d94SLen Brown } *package_even, *package_odd; 199c98d5d94SLen Brown 200c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 201c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 202c98d5d94SLen Brown 203c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 204c98d5d94SLen Brown (thread_base + (pkg_no) * topo.num_cores_per_pkg * \ 205c98d5d94SLen Brown topo.num_threads_per_core + \ 206c98d5d94SLen Brown (core_no) * topo.num_threads_per_core + (thread_no)) 207c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 208c98d5d94SLen Brown (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no)) 209c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 210c98d5d94SLen Brown 211388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE}; 21241618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC}; 213388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT}; 214388e9c81SLen Brown 215388e9c81SLen Brown struct msr_counter { 216388e9c81SLen Brown unsigned int msr_num; 217388e9c81SLen Brown char name[NAME_BYTES]; 218495c7654SLen Brown char path[PATH_BYTES]; 219388e9c81SLen Brown unsigned int width; 220388e9c81SLen Brown enum counter_type type; 221388e9c81SLen Brown enum counter_format format; 222388e9c81SLen Brown struct msr_counter *next; 223812db3f7SLen Brown unsigned int flags; 224812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 225812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 22641618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 227388e9c81SLen Brown }; 228388e9c81SLen Brown 229388e9c81SLen Brown struct sys_counters { 230678a3bd1SLen Brown unsigned int added_thread_counters; 231678a3bd1SLen Brown unsigned int added_core_counters; 232678a3bd1SLen Brown unsigned int added_package_counters; 233388e9c81SLen Brown struct msr_counter *tp; 234388e9c81SLen Brown struct msr_counter *cp; 235388e9c81SLen Brown struct msr_counter *pp; 236388e9c81SLen Brown } sys; 237388e9c81SLen Brown 238c98d5d94SLen Brown struct system_summary { 239c98d5d94SLen Brown struct thread_data threads; 240c98d5d94SLen Brown struct core_data cores; 241c98d5d94SLen Brown struct pkg_data packages; 242388e9c81SLen Brown } average; 243c98d5d94SLen Brown 244c98d5d94SLen Brown 245c98d5d94SLen Brown struct topo_params { 246c98d5d94SLen Brown int num_packages; 247103a8feaSLen Brown int num_cpus; 248c98d5d94SLen Brown int num_cores; 249c98d5d94SLen Brown int max_cpu_num; 250c98d5d94SLen Brown int num_cores_per_pkg; 251c98d5d94SLen Brown int num_threads_per_core; 252c98d5d94SLen Brown } topo; 253103a8feaSLen Brown 254c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 255103a8feaSLen Brown 256562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 257562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 258562a2d37SLen Brown 259c98d5d94SLen Brown void setup_all_buffers(void); 260103a8feaSLen Brown 261c98d5d94SLen Brown int cpu_is_not_present(int cpu) 262d15cf7c1SLen Brown { 263c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 264c98d5d94SLen Brown } 265c98d5d94SLen Brown /* 266c98d5d94SLen Brown * run func(thread, core, package) in topology order 267c98d5d94SLen Brown * skip non-present cpus 268c98d5d94SLen Brown */ 269c98d5d94SLen Brown 270c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 271c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 272c98d5d94SLen Brown { 273c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 274c98d5d94SLen Brown 275c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 276c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 277c98d5d94SLen Brown for (thread_no = 0; thread_no < 278c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 279c98d5d94SLen Brown struct thread_data *t; 280c98d5d94SLen Brown struct core_data *c; 281c98d5d94SLen Brown struct pkg_data *p; 282c98d5d94SLen Brown 283c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 284c98d5d94SLen Brown 285c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 286c98d5d94SLen Brown continue; 287c98d5d94SLen Brown 288c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 289c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 290c98d5d94SLen Brown 291c98d5d94SLen Brown retval = func(t, c, p); 292c98d5d94SLen Brown if (retval) 293c98d5d94SLen Brown return retval; 294c98d5d94SLen Brown } 295c98d5d94SLen Brown } 296c98d5d94SLen Brown } 297d15cf7c1SLen Brown return 0; 298d15cf7c1SLen Brown } 299d15cf7c1SLen Brown 30088c3281fSLen Brown int cpu_migrate(int cpu) 30188c3281fSLen Brown { 302c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 303c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 304c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 30588c3281fSLen Brown return -1; 30688c3281fSLen Brown else 30788c3281fSLen Brown return 0; 30888c3281fSLen Brown } 30936229897SLen Brown int get_msr_fd(int cpu) 310103a8feaSLen Brown { 311103a8feaSLen Brown char pathname[32]; 312103a8feaSLen Brown int fd; 313103a8feaSLen Brown 31436229897SLen Brown fd = fd_percpu[cpu]; 31536229897SLen Brown 31636229897SLen Brown if (fd) 31736229897SLen Brown return fd; 31836229897SLen Brown 319103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 320103a8feaSLen Brown fd = open(pathname, O_RDONLY); 32115aaa346SLen Brown if (fd < 0) 32298481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 323103a8feaSLen Brown 32436229897SLen Brown fd_percpu[cpu] = fd; 32536229897SLen Brown 32636229897SLen Brown return fd; 32736229897SLen Brown } 32836229897SLen Brown 32936229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 33036229897SLen Brown { 33136229897SLen Brown ssize_t retval; 33236229897SLen Brown 33336229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 33415aaa346SLen Brown 33598481e79SLen Brown if (retval != sizeof *msr) 336cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 33715aaa346SLen Brown 33815aaa346SLen Brown return 0; 339103a8feaSLen Brown } 340103a8feaSLen Brown 341fc04cc67SLen Brown /* 342812db3f7SLen Brown * Each string in this array is compared in --show and --hide cmdline. 343812db3f7SLen Brown * Thus, strings that are proper sub-sets must follow their more specific peers. 344fc04cc67SLen Brown */ 345812db3f7SLen Brown struct msr_counter bic[] = { 346812db3f7SLen Brown { 0x0, "Package" }, 347812db3f7SLen Brown { 0x0, "Avg_MHz" }, 348812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 349812db3f7SLen Brown { 0x0, "TSC_MHz" }, 350812db3f7SLen Brown { 0x0, "IRQ" }, 351495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 352812db3f7SLen Brown { 0x0, "Busy%" }, 353812db3f7SLen Brown { 0x0, "CPU%c1" }, 354812db3f7SLen Brown { 0x0, "CPU%c3" }, 355812db3f7SLen Brown { 0x0, "CPU%c6" }, 356812db3f7SLen Brown { 0x0, "CPU%c7" }, 357812db3f7SLen Brown { 0x0, "ThreadC" }, 358812db3f7SLen Brown { 0x0, "CoreTmp" }, 359812db3f7SLen Brown { 0x0, "CoreCnt" }, 360812db3f7SLen Brown { 0x0, "PkgTmp" }, 361812db3f7SLen Brown { 0x0, "GFX%rc6" }, 362812db3f7SLen Brown { 0x0, "GFXMHz" }, 363812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 364812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 365812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 366812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 3670f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 3680f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 3690f47c08dSLen Brown { 0x0, "Pkg%pc10" }, 370812db3f7SLen Brown { 0x0, "PkgWatt" }, 371812db3f7SLen Brown { 0x0, "CorWatt" }, 372812db3f7SLen Brown { 0x0, "GFXWatt" }, 373812db3f7SLen Brown { 0x0, "PkgCnt" }, 374812db3f7SLen Brown { 0x0, "RAMWatt" }, 375812db3f7SLen Brown { 0x0, "PKG_%" }, 376812db3f7SLen Brown { 0x0, "RAM_%" }, 377812db3f7SLen Brown { 0x0, "Pkg_J" }, 378812db3f7SLen Brown { 0x0, "Cor_J" }, 379812db3f7SLen Brown { 0x0, "GFX_J" }, 380812db3f7SLen Brown { 0x0, "RAM_J" }, 381812db3f7SLen Brown { 0x0, "Core" }, 382812db3f7SLen Brown { 0x0, "CPU" }, 3830539ba11SLen Brown { 0x0, "Mod%c6" }, 38441618e63SLen Brown { 0x0, "sysfs" }, 385812db3f7SLen Brown }; 386812db3f7SLen Brown 387812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 388812db3f7SLen Brown #define BIC_Package (1ULL << 0) 389812db3f7SLen Brown #define BIC_Avg_MHz (1ULL << 1) 390812db3f7SLen Brown #define BIC_Bzy_MHz (1ULL << 2) 391812db3f7SLen Brown #define BIC_TSC_MHz (1ULL << 3) 392812db3f7SLen Brown #define BIC_IRQ (1ULL << 4) 393812db3f7SLen Brown #define BIC_SMI (1ULL << 5) 394812db3f7SLen Brown #define BIC_Busy (1ULL << 6) 395812db3f7SLen Brown #define BIC_CPU_c1 (1ULL << 7) 396812db3f7SLen Brown #define BIC_CPU_c3 (1ULL << 8) 397812db3f7SLen Brown #define BIC_CPU_c6 (1ULL << 9) 398812db3f7SLen Brown #define BIC_CPU_c7 (1ULL << 10) 399812db3f7SLen Brown #define BIC_ThreadC (1ULL << 11) 400812db3f7SLen Brown #define BIC_CoreTmp (1ULL << 12) 401812db3f7SLen Brown #define BIC_CoreCnt (1ULL << 13) 402812db3f7SLen Brown #define BIC_PkgTmp (1ULL << 14) 403812db3f7SLen Brown #define BIC_GFX_rc6 (1ULL << 15) 404812db3f7SLen Brown #define BIC_GFXMHz (1ULL << 16) 405812db3f7SLen Brown #define BIC_Pkgpc2 (1ULL << 17) 406812db3f7SLen Brown #define BIC_Pkgpc3 (1ULL << 18) 407812db3f7SLen Brown #define BIC_Pkgpc6 (1ULL << 19) 408812db3f7SLen Brown #define BIC_Pkgpc7 (1ULL << 20) 4090f47c08dSLen Brown #define BIC_Pkgpc8 (1ULL << 21) 4100f47c08dSLen Brown #define BIC_Pkgpc9 (1ULL << 22) 4110f47c08dSLen Brown #define BIC_Pkgpc10 (1ULL << 23) 4120f47c08dSLen Brown #define BIC_PkgWatt (1ULL << 24) 4130f47c08dSLen Brown #define BIC_CorWatt (1ULL << 25) 4140f47c08dSLen Brown #define BIC_GFXWatt (1ULL << 26) 4150f47c08dSLen Brown #define BIC_PkgCnt (1ULL << 27) 4160f47c08dSLen Brown #define BIC_RAMWatt (1ULL << 28) 4170f47c08dSLen Brown #define BIC_PKG__ (1ULL << 29) 4180f47c08dSLen Brown #define BIC_RAM__ (1ULL << 30) 4190f47c08dSLen Brown #define BIC_Pkg_J (1ULL << 31) 4200f47c08dSLen Brown #define BIC_Cor_J (1ULL << 32) 4210f47c08dSLen Brown #define BIC_GFX_J (1ULL << 33) 4220f47c08dSLen Brown #define BIC_RAM_J (1ULL << 34) 4230f47c08dSLen Brown #define BIC_Core (1ULL << 35) 4240f47c08dSLen Brown #define BIC_CPU (1ULL << 36) 4250f47c08dSLen Brown #define BIC_Mod_c6 (1ULL << 37) 42641618e63SLen Brown #define BIC_sysfs (1ULL << 38) 427812db3f7SLen Brown 428812db3f7SLen Brown unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL; 42941618e63SLen Brown unsigned long long bic_present = BIC_sysfs; 430812db3f7SLen Brown 431812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 432812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 4330f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 434812db3f7SLen Brown 435812db3f7SLen Brown /* 436812db3f7SLen Brown * bic_lookup 437812db3f7SLen Brown * for all the strings in comma separate name_list, 438812db3f7SLen Brown * set the approprate bit in return value. 439812db3f7SLen Brown */ 440812db3f7SLen Brown unsigned long long bic_lookup(char *name_list) 441812db3f7SLen Brown { 442812db3f7SLen Brown int i; 443812db3f7SLen Brown unsigned long long retval = 0; 444812db3f7SLen Brown 445812db3f7SLen Brown while (name_list) { 446812db3f7SLen Brown char *comma; 447812db3f7SLen Brown 448812db3f7SLen Brown comma = strchr(name_list, ','); 449812db3f7SLen Brown 450812db3f7SLen Brown if (comma) 451812db3f7SLen Brown *comma = '\0'; 452812db3f7SLen Brown 453812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 454812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 455812db3f7SLen Brown retval |= (1ULL << i); 456812db3f7SLen Brown break; 457812db3f7SLen Brown } 458812db3f7SLen Brown } 459812db3f7SLen Brown if (i == MAX_BIC) { 460812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 461812db3f7SLen Brown exit(-1); 462812db3f7SLen Brown } 463812db3f7SLen Brown 464812db3f7SLen Brown name_list = comma; 465812db3f7SLen Brown if (name_list) 466812db3f7SLen Brown name_list++; 467812db3f7SLen Brown 468812db3f7SLen Brown } 469812db3f7SLen Brown return retval; 470812db3f7SLen Brown } 471fc04cc67SLen Brown 472a829eb4dSLen Brown void print_header(void) 473103a8feaSLen Brown { 474388e9c81SLen Brown struct msr_counter *mp; 475388e9c81SLen Brown 476812db3f7SLen Brown if (DO_BIC(BIC_Package)) 4773d109de2SLen Brown outp += sprintf(outp, "\tPackage"); 478812db3f7SLen Brown if (DO_BIC(BIC_Core)) 4793d109de2SLen Brown outp += sprintf(outp, "\tCore"); 480812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 4813d109de2SLen Brown outp += sprintf(outp, "\tCPU"); 482812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 4833d109de2SLen Brown outp += sprintf(outp, "\tAvg_MHz"); 484812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 4853d109de2SLen Brown outp += sprintf(outp, "\tBusy%%"); 486812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 4873d109de2SLen Brown outp += sprintf(outp, "\tBzy_MHz"); 488812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 4893d109de2SLen Brown outp += sprintf(outp, "\tTSC_MHz"); 4901cc21f7bSLen Brown 491812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 4923d109de2SLen Brown outp += sprintf(outp, "\tIRQ"); 493812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 4943d109de2SLen Brown outp += sprintf(outp, "\tSMI"); 4951cc21f7bSLen Brown 496388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 497388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 498388e9c81SLen Brown if (mp->width == 64) 499388e9c81SLen Brown outp += sprintf(outp, "\t%18.18s", mp->name); 500388e9c81SLen Brown else 501388e9c81SLen Brown outp += sprintf(outp, "\t%10.10s", mp->name); 502388e9c81SLen Brown } else { 50341618e63SLen Brown outp += sprintf(outp, "\t%s", mp->name); 504388e9c81SLen Brown } 505388e9c81SLen Brown } 506388e9c81SLen Brown 50741618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 50841618e63SLen Brown outp += sprintf(outp, "\tCPU%%c1"); 509812db3f7SLen Brown if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) 510678a3bd1SLen Brown outp += sprintf(outp, "\tCPU%%c3"); 511812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 512678a3bd1SLen Brown outp += sprintf(outp, "\tCPU%%c6"); 513812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 514678a3bd1SLen Brown outp += sprintf(outp, "\tCPU%%c7"); 515678a3bd1SLen Brown 5160539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 5170539ba11SLen Brown outp += sprintf(outp, "\tMod%%c6"); 518678a3bd1SLen Brown 519812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 5203d109de2SLen Brown outp += sprintf(outp, "\tCoreTmp"); 521388e9c81SLen Brown 522388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 523388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 524388e9c81SLen Brown if (mp->width == 64) 525388e9c81SLen Brown outp += sprintf(outp, "\t%18.18s", mp->name); 526388e9c81SLen Brown else 527388e9c81SLen Brown outp += sprintf(outp, "\t%10.10s", mp->name); 528388e9c81SLen Brown } else { 52941618e63SLen Brown outp += sprintf(outp, "\t%s", mp->name); 530388e9c81SLen Brown } 531388e9c81SLen Brown } 532388e9c81SLen Brown 533812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 5343d109de2SLen Brown outp += sprintf(outp, "\tPkgTmp"); 535889facbeSLen Brown 536812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 5373d109de2SLen Brown outp += sprintf(outp, "\tGFX%%rc6"); 538fdf676e5SLen Brown 539812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 5403d109de2SLen Brown outp += sprintf(outp, "\tGFXMHz"); 54127d47356SLen Brown 5420b2bb692SLen Brown if (do_skl_residency) { 5433d109de2SLen Brown outp += sprintf(outp, "\tTotl%%C0"); 5443d109de2SLen Brown outp += sprintf(outp, "\tAny%%C0"); 5453d109de2SLen Brown outp += sprintf(outp, "\tGFX%%C0"); 5463d109de2SLen Brown outp += sprintf(outp, "\tCPUGFX%%"); 5470b2bb692SLen Brown } 5480b2bb692SLen Brown 5490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 5503d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc2"); 5510f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 5523d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc3"); 5530f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 5543d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc6"); 5550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 5563d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc7"); 5570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 5583d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc8"); 5590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 5603d109de2SLen Brown outp += sprintf(outp, "\tPkg%%pc9"); 5610f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 5623d109de2SLen Brown outp += sprintf(outp, "\tPk%%pc10"); 563103a8feaSLen Brown 5645c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 565812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 5663d109de2SLen Brown outp += sprintf(outp, "\tPkgWatt"); 567812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 5683d109de2SLen Brown outp += sprintf(outp, "\tCorWatt"); 569812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 5703d109de2SLen Brown outp += sprintf(outp, "\tGFXWatt"); 571812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 5723d109de2SLen Brown outp += sprintf(outp, "\tRAMWatt"); 573812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 5743d109de2SLen Brown outp += sprintf(outp, "\tPKG_%%"); 575812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 5763d109de2SLen Brown outp += sprintf(outp, "\tRAM_%%"); 577d7899447SLen Brown } else if (do_rapl && rapl_joules) { 578812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 5793d109de2SLen Brown outp += sprintf(outp, "\tPkg_J"); 580812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 5813d109de2SLen Brown outp += sprintf(outp, "\tCor_J"); 582812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 5833d109de2SLen Brown outp += sprintf(outp, "\tGFX_J"); 584812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 5853d109de2SLen Brown outp += sprintf(outp, "\tRAM_J"); 586812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 5873d109de2SLen Brown outp += sprintf(outp, "\tPKG_%%"); 588812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 5893d109de2SLen Brown outp += sprintf(outp, "\tRAM_%%"); 5905c56be9aSDirk Brandewie } 591388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 592388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 593388e9c81SLen Brown if (mp->width == 64) 594388e9c81SLen Brown outp += sprintf(outp, "\t%18.18s", mp->name); 595388e9c81SLen Brown else 596388e9c81SLen Brown outp += sprintf(outp, "\t%10.10s", mp->name); 597388e9c81SLen Brown } else { 59841618e63SLen Brown outp += sprintf(outp, "\t%s", mp->name); 599388e9c81SLen Brown } 600388e9c81SLen Brown } 601388e9c81SLen Brown 602c98d5d94SLen Brown outp += sprintf(outp, "\n"); 603103a8feaSLen Brown } 604103a8feaSLen Brown 605c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 606c98d5d94SLen Brown struct pkg_data *p) 607103a8feaSLen Brown { 608388e9c81SLen Brown int i; 609388e9c81SLen Brown struct msr_counter *mp; 610388e9c81SLen Brown 6113b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 612c98d5d94SLen Brown 613c98d5d94SLen Brown if (t) { 6143b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 6153b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 6163b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 6173b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 6183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 6193b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 6206886fee4SLen Brown 621812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 622562a2d37SLen Brown outp += sprintf(outp, "IRQ: %08X\n", t->irq_count); 623812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 6243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "SMI: %08X\n", t->smi_count); 625388e9c81SLen Brown 626388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 627388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 628388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 629388e9c81SLen Brown } 630103a8feaSLen Brown } 631103a8feaSLen Brown 632c98d5d94SLen Brown if (c) { 6333b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 6343b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 6353b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 6363b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 6373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 638388e9c81SLen Brown 639388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 640388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 641388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 642388e9c81SLen Brown } 6430539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 644c98d5d94SLen Brown } 645103a8feaSLen Brown 646c98d5d94SLen Brown if (p) { 6473b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 6480b2bb692SLen Brown 6490b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 6500b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 6510b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 6520b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 6530b2bb692SLen Brown 6543b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 6550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 6563b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 6570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 6583b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 6590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 6603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 6613b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 6623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 6633b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 6643b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 6653b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 6663b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 6673b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 6683b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 6693b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 6703b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 6713b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 6723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 673388e9c81SLen Brown 674388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 675388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 676388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 677388e9c81SLen Brown } 678c98d5d94SLen Brown } 6793b4d5c7fSAndy Shevchenko 6803b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 6813b4d5c7fSAndy Shevchenko 682c98d5d94SLen Brown return 0; 683103a8feaSLen Brown } 684103a8feaSLen Brown 685e23da037SLen Brown /* 686e23da037SLen Brown * column formatting convention & formats 687e23da037SLen Brown */ 688c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 689c98d5d94SLen Brown struct pkg_data *p) 690103a8feaSLen Brown { 691008d396eSLen Brown double interval_float, tsc; 692fc04cc67SLen Brown char *fmt8; 693388e9c81SLen Brown int i; 694388e9c81SLen Brown struct msr_counter *mp; 695103a8feaSLen Brown 696c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 697c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 698c98d5d94SLen Brown return 0; 699c98d5d94SLen Brown 700c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 701c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 702c98d5d94SLen Brown return 0; 703c98d5d94SLen Brown 704*1ef7d21aSLen Brown /*if not summary line and --cpu is used */ 705*1ef7d21aSLen Brown if ((t != &average.threads) && 706*1ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 707*1ef7d21aSLen Brown return 0; 708*1ef7d21aSLen Brown 709103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 710103a8feaSLen Brown 711008d396eSLen Brown tsc = t->tsc * tsc_tweak; 712008d396eSLen Brown 713c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 714c98d5d94SLen Brown if (t == &average.threads) { 715812db3f7SLen Brown if (DO_BIC(BIC_Package)) 7163d109de2SLen Brown outp += sprintf(outp, "\t-"); 717812db3f7SLen Brown if (DO_BIC(BIC_Core)) 7183d109de2SLen Brown outp += sprintf(outp, "\t-"); 719812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 7203d109de2SLen Brown outp += sprintf(outp, "\t-"); 721103a8feaSLen Brown } else { 722812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 723c98d5d94SLen Brown if (p) 7243d109de2SLen Brown outp += sprintf(outp, "\t%d", p->package_id); 725c98d5d94SLen Brown else 7263d109de2SLen Brown outp += sprintf(outp, "\t-"); 727c98d5d94SLen Brown } 728812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 729c98d5d94SLen Brown if (c) 7303d109de2SLen Brown outp += sprintf(outp, "\t%d", c->core_id); 731c98d5d94SLen Brown else 7323d109de2SLen Brown outp += sprintf(outp, "\t-"); 733c98d5d94SLen Brown } 734812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 7353d109de2SLen Brown outp += sprintf(outp, "\t%d", t->cpu_id); 736103a8feaSLen Brown } 737fc04cc67SLen Brown 738812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 7393d109de2SLen Brown outp += sprintf(outp, "\t%.0f", 740fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 741fc04cc67SLen Brown 742812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 743008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/tsc); 744103a8feaSLen Brown 745812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 74621ed5574SLen Brown if (has_base_hz) 7473d109de2SLen Brown outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf); 74821ed5574SLen Brown else 7493d109de2SLen Brown outp += sprintf(outp, "\t%.0f", 750008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 75121ed5574SLen Brown } 752103a8feaSLen Brown 753812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 7543d109de2SLen Brown outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float); 755103a8feaSLen Brown 756562a2d37SLen Brown /* IRQ */ 757812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 7583d109de2SLen Brown outp += sprintf(outp, "\t%d", t->irq_count); 759562a2d37SLen Brown 7601cc21f7bSLen Brown /* SMI */ 761812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 7623d109de2SLen Brown outp += sprintf(outp, "\t%d", t->smi_count); 7631cc21f7bSLen Brown 764678a3bd1SLen Brown /* Added counters */ 765678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 766678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 767678a3bd1SLen Brown if (mp->width == 32) 768678a3bd1SLen Brown outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]); 769678a3bd1SLen Brown else 770678a3bd1SLen Brown outp += sprintf(outp, "\t0x%016llx", t->counter[i]); 771678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 772678a3bd1SLen Brown outp += sprintf(outp, "\t%lld", t->counter[i]); 773678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 77441618e63SLen Brown if (mp->type == COUNTER_USEC) 77541618e63SLen Brown outp += sprintf(outp, "\t%.2f", t->counter[i]/interval_float/10000); 77641618e63SLen Brown else 777008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/tsc); 778678a3bd1SLen Brown } 779678a3bd1SLen Brown } 780678a3bd1SLen Brown 78141618e63SLen Brown /* C1 */ 78241618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 78341618e63SLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/tsc); 78441618e63SLen Brown 78541618e63SLen Brown 786c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 787c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 788c98d5d94SLen Brown goto done; 789c98d5d94SLen Brown 790812db3f7SLen Brown if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) 791008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/tsc); 792812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 793008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/tsc); 794812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 795008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/tsc); 796c98d5d94SLen Brown 7970539ba11SLen Brown /* Mod%c6 */ 7980539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 799008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / tsc); 8000539ba11SLen Brown 801812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 8023d109de2SLen Brown outp += sprintf(outp, "\t%d", c->core_temp_c); 803889facbeSLen Brown 804388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 805388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 806388e9c81SLen Brown if (mp->width == 32) 807388e9c81SLen Brown outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]); 808388e9c81SLen Brown else 809388e9c81SLen Brown outp += sprintf(outp, "\t0x%016llx", c->counter[i]); 810388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 811678a3bd1SLen Brown outp += sprintf(outp, "\t%lld", c->counter[i]); 812388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 813008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/tsc); 814388e9c81SLen Brown } 815388e9c81SLen Brown } 816388e9c81SLen Brown 817c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 818c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 819c98d5d94SLen Brown goto done; 820c98d5d94SLen Brown 8210b2bb692SLen Brown /* PkgTmp */ 822812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 8233d109de2SLen Brown outp += sprintf(outp, "\t%d", p->pkg_temp_c); 824889facbeSLen Brown 825fdf676e5SLen Brown /* GFXrc6 */ 826812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 827ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 8283d109de2SLen Brown outp += sprintf(outp, "\t**.**"); 8299185e988SLen Brown } else { 8303d109de2SLen Brown outp += sprintf(outp, "\t%.2f", 8319185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 8329185e988SLen Brown } 8339185e988SLen Brown } 834fdf676e5SLen Brown 83527d47356SLen Brown /* GFXMHz */ 836812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 8373d109de2SLen Brown outp += sprintf(outp, "\t%d", p->gfx_mhz); 83827d47356SLen Brown 8390b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 8400b2bb692SLen Brown if (do_skl_residency) { 841008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/tsc); 842008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/tsc); 843008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/tsc); 844008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/tsc); 8450b2bb692SLen Brown } 8460b2bb692SLen Brown 8470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 848008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/tsc); 8490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 850008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/tsc); 8510f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 852008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/tsc); 8530f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 854008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/tsc); 8550f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 856008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/tsc); 8570f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 858008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/tsc); 8590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 860008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/tsc); 861889facbeSLen Brown 862889facbeSLen Brown /* 863889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 864889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 865889facbeSLen Brown */ 866fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 8673d109de2SLen Brown fmt8 = "\t%.2f"; 868fc04cc67SLen Brown else 869fc04cc67SLen Brown fmt8 = "%6.0f**"; 870889facbeSLen Brown 871812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 872fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float); 873812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 874fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float); 875812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 876fc04cc67SLen Brown outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float); 877812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 87840ee8e3bSAndrey Semin outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float); 879812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 880812db3f7SLen Brown outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units); 881812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 882812db3f7SLen Brown outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units); 883812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 884812db3f7SLen Brown outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units); 885812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 886812db3f7SLen Brown outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units); 887812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 888fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 889812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 890fc04cc67SLen Brown outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 891812db3f7SLen Brown 892388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 893388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 894388e9c81SLen Brown if (mp->width == 32) 895388e9c81SLen Brown outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]); 896388e9c81SLen Brown else 897388e9c81SLen Brown outp += sprintf(outp, "\t0x%016llx", p->counter[i]); 898388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 899678a3bd1SLen Brown outp += sprintf(outp, "\t%lld", p->counter[i]); 900388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 901008d396eSLen Brown outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/tsc); 902388e9c81SLen Brown } 903388e9c81SLen Brown } 904388e9c81SLen Brown 905c98d5d94SLen Brown done: 906c98d5d94SLen Brown outp += sprintf(outp, "\n"); 907c98d5d94SLen Brown 908c98d5d94SLen Brown return 0; 909103a8feaSLen Brown } 910103a8feaSLen Brown 911b7d8c148SLen Brown void flush_output_stdout(void) 912103a8feaSLen Brown { 913b7d8c148SLen Brown FILE *filep; 914b7d8c148SLen Brown 915b7d8c148SLen Brown if (outf == stderr) 916b7d8c148SLen Brown filep = stdout; 917b7d8c148SLen Brown else 918b7d8c148SLen Brown filep = outf; 919b7d8c148SLen Brown 920b7d8c148SLen Brown fputs(output_buffer, filep); 921b7d8c148SLen Brown fflush(filep); 922b7d8c148SLen Brown 923c98d5d94SLen Brown outp = output_buffer; 924c98d5d94SLen Brown } 925b7d8c148SLen Brown void flush_output_stderr(void) 926c98d5d94SLen Brown { 927b7d8c148SLen Brown fputs(output_buffer, outf); 928b7d8c148SLen Brown fflush(outf); 929c98d5d94SLen Brown outp = output_buffer; 930c98d5d94SLen Brown } 931c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 932c98d5d94SLen Brown { 933e23da037SLen Brown static int printed; 934103a8feaSLen Brown 935e23da037SLen Brown if (!printed || !summary_only) 936103a8feaSLen Brown print_header(); 937103a8feaSLen Brown 938c98d5d94SLen Brown if (topo.num_cpus > 1) 939c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 940c98d5d94SLen Brown &average.packages); 941103a8feaSLen Brown 942e23da037SLen Brown printed = 1; 943e23da037SLen Brown 944e23da037SLen Brown if (summary_only) 945e23da037SLen Brown return; 946e23da037SLen Brown 947c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 948103a8feaSLen Brown } 949103a8feaSLen Brown 950889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 951889facbeSLen Brown if (new > old) { \ 952889facbeSLen Brown old = new - old; \ 953889facbeSLen Brown } else { \ 954889facbeSLen Brown old = 0x100000000 + new - old; \ 955889facbeSLen Brown } 956889facbeSLen Brown 957ba3dec99SLen Brown int 958c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 959103a8feaSLen Brown { 960388e9c81SLen Brown int i; 961388e9c81SLen Brown struct msr_counter *mp; 9620b2bb692SLen Brown 9630b2bb692SLen Brown if (do_skl_residency) { 9640b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 9650b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 9660b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 9670b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 9680b2bb692SLen Brown } 969c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 9700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 971c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 9720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 973c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 9740f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 975c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 976ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 977ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 978ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 979889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 980889facbeSLen Brown 9819185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 9829185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 9839185e988SLen Brown old->gfx_rc6_ms = -1; 9849185e988SLen Brown else 985fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 9869185e988SLen Brown 98727d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 98827d47356SLen Brown 989889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 990889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 991889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 992889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 993889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 994889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 995ba3dec99SLen Brown 996388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 997388e9c81SLen Brown if (mp->format == FORMAT_RAW) 998388e9c81SLen Brown old->counter[i] = new->counter[i]; 999388e9c81SLen Brown else 1000388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1001388e9c81SLen Brown } 1002388e9c81SLen Brown 1003ba3dec99SLen Brown return 0; 1004103a8feaSLen Brown } 1005103a8feaSLen Brown 1006c98d5d94SLen Brown void 1007c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1008c98d5d94SLen Brown { 1009388e9c81SLen Brown int i; 1010388e9c81SLen Brown struct msr_counter *mp; 1011388e9c81SLen Brown 1012c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1013c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1014c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1015889facbeSLen Brown old->core_temp_c = new->core_temp_c; 10160539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1017388e9c81SLen Brown 1018388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1019388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1020388e9c81SLen Brown old->counter[i] = new->counter[i]; 1021388e9c81SLen Brown else 1022388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1023388e9c81SLen Brown } 1024103a8feaSLen Brown } 1025c98d5d94SLen Brown 1026c3ae331dSLen Brown /* 1027c3ae331dSLen Brown * old = new - old 1028c3ae331dSLen Brown */ 1029ba3dec99SLen Brown int 1030c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1031c98d5d94SLen Brown struct core_data *core_delta) 1032c98d5d94SLen Brown { 1033388e9c81SLen Brown int i; 1034388e9c81SLen Brown struct msr_counter *mp; 1035388e9c81SLen Brown 1036c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1037c98d5d94SLen Brown 1038103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1039b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1040b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1041b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1042b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1043103a8feaSLen Brown 1044c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1045c98d5d94SLen Brown 1046812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 1047c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1048c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1049c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1050c98d5d94SLen Brown } else { 1051ba3dec99SLen Brown return -1; 1052103a8feaSLen Brown } 1053a729617cSLen Brown } 1054103a8feaSLen Brown 1055c98d5d94SLen Brown 1056144b44b1SLen Brown if (use_c1_residency_msr) { 1057144b44b1SLen Brown /* 1058144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1059144b44b1SLen Brown * which should be more accurate than the derivation below. 1060144b44b1SLen Brown */ 1061144b44b1SLen Brown } else { 1062103a8feaSLen Brown /* 1063c3ae331dSLen Brown * As counter collection is not atomic, 1064c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1065103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1066103a8feaSLen Brown */ 1067c3ae331dSLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc) 1068c98d5d94SLen Brown old->c1 = 0; 1069c98d5d94SLen Brown else { 1070c98d5d94SLen Brown /* normal case, derive c1 */ 1071008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1072c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1073c98d5d94SLen Brown } 1074144b44b1SLen Brown } 1075c3ae331dSLen Brown 1076c98d5d94SLen Brown if (old->mperf == 0) { 1077b7d8c148SLen Brown if (debug > 1) 1078b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1079c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1080c98d5d94SLen Brown } 1081103a8feaSLen Brown 1082812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1083562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1084562a2d37SLen Brown 1085812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 10861ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1087ba3dec99SLen Brown 1088388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1089388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1090388e9c81SLen Brown old->counter[i] = new->counter[i]; 1091388e9c81SLen Brown else 1092388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1093388e9c81SLen Brown } 1094ba3dec99SLen Brown return 0; 1095103a8feaSLen Brown } 1096c98d5d94SLen Brown 1097c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1098c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1099c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1100c98d5d94SLen Brown { 1101ba3dec99SLen Brown int retval = 0; 1102ba3dec99SLen Brown 1103c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1104c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1105c98d5d94SLen Brown delta_core(c, c2); 1106c98d5d94SLen Brown 1107c98d5d94SLen Brown /* always calculate thread delta */ 1108ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1109ba3dec99SLen Brown if (retval) 1110ba3dec99SLen Brown return retval; 1111c98d5d94SLen Brown 1112c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1113c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1114ba3dec99SLen Brown retval = delta_package(p, p2); 1115c98d5d94SLen Brown 1116ba3dec99SLen Brown return retval; 1117103a8feaSLen Brown } 1118103a8feaSLen Brown 1119c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1120103a8feaSLen Brown { 1121388e9c81SLen Brown int i; 1122388e9c81SLen Brown struct msr_counter *mp; 1123388e9c81SLen Brown 1124c98d5d94SLen Brown t->tsc = 0; 1125c98d5d94SLen Brown t->aperf = 0; 1126c98d5d94SLen Brown t->mperf = 0; 1127c98d5d94SLen Brown t->c1 = 0; 1128103a8feaSLen Brown 1129562a2d37SLen Brown t->irq_count = 0; 1130562a2d37SLen Brown t->smi_count = 0; 1131562a2d37SLen Brown 1132c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1133c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1134c98d5d94SLen Brown 1135c98d5d94SLen Brown c->c3 = 0; 1136c98d5d94SLen Brown c->c6 = 0; 1137c98d5d94SLen Brown c->c7 = 0; 11380539ba11SLen Brown c->mc6_us = 0; 1139889facbeSLen Brown c->core_temp_c = 0; 1140c98d5d94SLen Brown 11410b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 11420b2bb692SLen Brown p->pkg_any_core_c0 = 0; 11430b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 11440b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 11450b2bb692SLen Brown 1146c98d5d94SLen Brown p->pc2 = 0; 11470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1148c98d5d94SLen Brown p->pc3 = 0; 11490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1150c98d5d94SLen Brown p->pc6 = 0; 11510f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1152c98d5d94SLen Brown p->pc7 = 0; 1153ca58710fSKristen Carlson Accardi p->pc8 = 0; 1154ca58710fSKristen Carlson Accardi p->pc9 = 0; 1155ca58710fSKristen Carlson Accardi p->pc10 = 0; 1156889facbeSLen Brown 1157889facbeSLen Brown p->energy_pkg = 0; 1158889facbeSLen Brown p->energy_dram = 0; 1159889facbeSLen Brown p->energy_cores = 0; 1160889facbeSLen Brown p->energy_gfx = 0; 1161889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1162889facbeSLen Brown p->rapl_dram_perf_status = 0; 1163889facbeSLen Brown p->pkg_temp_c = 0; 116427d47356SLen Brown 1165fdf676e5SLen Brown p->gfx_rc6_ms = 0; 116627d47356SLen Brown p->gfx_mhz = 0; 1167388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1168388e9c81SLen Brown t->counter[i] = 0; 1169388e9c81SLen Brown 1170388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1171388e9c81SLen Brown c->counter[i] = 0; 1172388e9c81SLen Brown 1173388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1174388e9c81SLen Brown p->counter[i] = 0; 1175103a8feaSLen Brown } 1176c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1177c98d5d94SLen Brown struct pkg_data *p) 1178103a8feaSLen Brown { 1179388e9c81SLen Brown int i; 1180388e9c81SLen Brown struct msr_counter *mp; 1181388e9c81SLen Brown 1182c98d5d94SLen Brown average.threads.tsc += t->tsc; 1183c98d5d94SLen Brown average.threads.aperf += t->aperf; 1184c98d5d94SLen Brown average.threads.mperf += t->mperf; 1185c98d5d94SLen Brown average.threads.c1 += t->c1; 118615aaa346SLen Brown 1187562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1188562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1189562a2d37SLen Brown 1190388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1191388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1192388e9c81SLen Brown continue; 1193388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1194388e9c81SLen Brown } 1195388e9c81SLen Brown 1196c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1197c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1198c98d5d94SLen Brown return 0; 1199c98d5d94SLen Brown 1200c98d5d94SLen Brown average.cores.c3 += c->c3; 1201c98d5d94SLen Brown average.cores.c6 += c->c6; 1202c98d5d94SLen Brown average.cores.c7 += c->c7; 12030539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1204c98d5d94SLen Brown 1205889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1206889facbeSLen Brown 1207388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1208388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1209388e9c81SLen Brown continue; 1210388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1211388e9c81SLen Brown } 1212388e9c81SLen Brown 1213c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1214c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1215c98d5d94SLen Brown return 0; 1216c98d5d94SLen Brown 12170b2bb692SLen Brown if (do_skl_residency) { 12180b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 12190b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 12200b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 12210b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 12220b2bb692SLen Brown } 12230b2bb692SLen Brown 1224c98d5d94SLen Brown average.packages.pc2 += p->pc2; 12250f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1226c98d5d94SLen Brown average.packages.pc3 += p->pc3; 12270f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1228c98d5d94SLen Brown average.packages.pc6 += p->pc6; 12290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1230c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1231ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1232ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1233ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1234c98d5d94SLen Brown 1235889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1236889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1237889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1238889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1239889facbeSLen Brown 1240fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 124127d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 124227d47356SLen Brown 1243889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1244889facbeSLen Brown 1245889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1246889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1247388e9c81SLen Brown 1248388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1249388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1250388e9c81SLen Brown continue; 1251388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1252388e9c81SLen Brown } 1253c98d5d94SLen Brown return 0; 1254c98d5d94SLen Brown } 1255c98d5d94SLen Brown /* 1256c98d5d94SLen Brown * sum the counters for all cpus in the system 1257c98d5d94SLen Brown * compute the weighted average 1258c98d5d94SLen Brown */ 1259c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1260c98d5d94SLen Brown struct pkg_data *p) 1261c98d5d94SLen Brown { 1262388e9c81SLen Brown int i; 1263388e9c81SLen Brown struct msr_counter *mp; 1264388e9c81SLen Brown 1265c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1266c98d5d94SLen Brown 1267c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1268c98d5d94SLen Brown 1269c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1270c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1271c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1272c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1273c98d5d94SLen Brown 1274c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1275c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1276c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 12770539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1278c98d5d94SLen Brown 12790b2bb692SLen Brown if (do_skl_residency) { 12800b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 12810b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 12820b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 12830b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 12840b2bb692SLen Brown } 12850b2bb692SLen Brown 1286c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 12870f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1288c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 12890f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1290c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 12910f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1292c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1293ca58710fSKristen Carlson Accardi 1294ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1295ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1296ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1297388e9c81SLen Brown 1298388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1299388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1300388e9c81SLen Brown continue; 130141618e63SLen Brown if (mp->flags & SYSFS_PERCPU && mp->type == COUNTER_ITEMS) 130241618e63SLen Brown continue; 1303388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1304388e9c81SLen Brown } 1305388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1306388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1307388e9c81SLen Brown continue; 1308388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1309388e9c81SLen Brown } 1310388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1311388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1312388e9c81SLen Brown continue; 1313388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1314388e9c81SLen Brown } 1315c98d5d94SLen Brown } 1316c98d5d94SLen Brown 1317c98d5d94SLen Brown static unsigned long long rdtsc(void) 1318c98d5d94SLen Brown { 1319c98d5d94SLen Brown unsigned int low, high; 1320c98d5d94SLen Brown 1321c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1322c98d5d94SLen Brown 1323c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1324c98d5d94SLen Brown } 1325c98d5d94SLen Brown 1326c98d5d94SLen Brown /* 1327495c7654SLen Brown * Open a file, and exit on failure 1328495c7654SLen Brown */ 1329495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1330495c7654SLen Brown { 1331495c7654SLen Brown FILE *filep = fopen(path, mode); 1332495c7654SLen Brown 1333495c7654SLen Brown if (!filep) 1334495c7654SLen Brown err(1, "%s: open failed", path); 1335495c7654SLen Brown return filep; 1336495c7654SLen Brown } 1337495c7654SLen Brown /* 1338495c7654SLen Brown * snapshot_sysfs_counter() 1339495c7654SLen Brown * 1340495c7654SLen Brown * return snapshot of given counter 1341495c7654SLen Brown */ 1342495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1343495c7654SLen Brown { 1344495c7654SLen Brown FILE *fp; 1345495c7654SLen Brown int retval; 1346495c7654SLen Brown unsigned long long counter; 1347495c7654SLen Brown 1348495c7654SLen Brown fp = fopen_or_die(path, "r"); 1349495c7654SLen Brown 1350495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1351495c7654SLen Brown if (retval != 1) 1352495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1353495c7654SLen Brown 1354495c7654SLen Brown fclose(fp); 1355495c7654SLen Brown 1356495c7654SLen Brown return counter; 1357495c7654SLen Brown } 1358495c7654SLen Brown 1359495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1360495c7654SLen Brown { 1361495c7654SLen Brown if (mp->msr_num != 0) { 1362495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1363495c7654SLen Brown return -1; 1364495c7654SLen Brown } else { 136541618e63SLen Brown char path[128]; 136641618e63SLen Brown 136741618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 136841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 136941618e63SLen Brown cpu, mp->path); 137041618e63SLen Brown 137141618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 137241618e63SLen Brown } else { 1373495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1374495c7654SLen Brown } 137541618e63SLen Brown } 1376495c7654SLen Brown 1377495c7654SLen Brown return 0; 1378495c7654SLen Brown } 1379495c7654SLen Brown 1380495c7654SLen Brown /* 1381c98d5d94SLen Brown * get_counters(...) 1382c98d5d94SLen Brown * migrate to cpu 1383c98d5d94SLen Brown * acquire and record local counters for that cpu 1384c98d5d94SLen Brown */ 1385c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1386c98d5d94SLen Brown { 1387c98d5d94SLen Brown int cpu = t->cpu_id; 1388889facbeSLen Brown unsigned long long msr; 13890102b067SLen Brown int aperf_mperf_retry_count = 0; 1390388e9c81SLen Brown struct msr_counter *mp; 1391388e9c81SLen Brown int i; 1392c98d5d94SLen Brown 1393e52966c0SLen Brown if (cpu_migrate(cpu)) { 1394b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 139515aaa346SLen Brown return -1; 1396e52966c0SLen Brown } 139715aaa346SLen Brown 13980102b067SLen Brown retry: 1399c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 140015aaa346SLen Brown 1401812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 14020102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 14030102b067SLen Brown 14040102b067SLen Brown /* 14050102b067SLen Brown * The TSC, APERF and MPERF must be read together for 14060102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 14070102b067SLen Brown * 14080102b067SLen Brown * Unfortunately, APERF and MPERF are read by 14090102b067SLen Brown * individual system call, so delays may occur 14100102b067SLen Brown * between them. If the time to read them 14110102b067SLen Brown * varies by a large amount, we re-read them. 14120102b067SLen Brown */ 14130102b067SLen Brown 14140102b067SLen Brown /* 14150102b067SLen Brown * This initial dummy APERF read has been seen to 14160102b067SLen Brown * reduce jitter in the subsequent reads. 14170102b067SLen Brown */ 14180102b067SLen Brown 14199c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1420c98d5d94SLen Brown return -3; 14210102b067SLen Brown 14220102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 14230102b067SLen Brown 14240102b067SLen Brown tsc_before = t->tsc; 14250102b067SLen Brown 14260102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 14270102b067SLen Brown return -3; 14280102b067SLen Brown 14290102b067SLen Brown tsc_between = rdtsc(); 14300102b067SLen Brown 14319c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1432c98d5d94SLen Brown return -4; 14330102b067SLen Brown 14340102b067SLen Brown tsc_after = rdtsc(); 14350102b067SLen Brown 14360102b067SLen Brown aperf_time = tsc_between - tsc_before; 14370102b067SLen Brown mperf_time = tsc_after - tsc_between; 14380102b067SLen Brown 14390102b067SLen Brown /* 14400102b067SLen Brown * If the system call latency to read APERF and MPERF 14410102b067SLen Brown * differ by more than 2x, then try again. 14420102b067SLen Brown */ 14430102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 14440102b067SLen Brown aperf_mperf_retry_count++; 14450102b067SLen Brown if (aperf_mperf_retry_count < 5) 14460102b067SLen Brown goto retry; 14470102b067SLen Brown else 14480102b067SLen Brown warnx("cpu%d jitter %lld %lld", 14490102b067SLen Brown cpu, aperf_time, mperf_time); 14500102b067SLen Brown } 14510102b067SLen Brown aperf_mperf_retry_count = 0; 14520102b067SLen Brown 1453b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1454b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 145588c3281fSLen Brown } 145688c3281fSLen Brown 1457812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1458562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1459812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 14601ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 14611ed51011SLen Brown return -5; 14621ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 14631ed51011SLen Brown } 14640539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1465144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1466144b44b1SLen Brown return -6; 1467144b44b1SLen Brown } 1468144b44b1SLen Brown 1469388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1470495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1471388e9c81SLen Brown return -10; 1472388e9c81SLen Brown } 1473388e9c81SLen Brown 1474c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1475c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1476c98d5d94SLen Brown return 0; 1477c98d5d94SLen Brown 1478812db3f7SLen Brown if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) { 1479c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1480c98d5d94SLen Brown return -6; 1481144b44b1SLen Brown } 1482144b44b1SLen Brown 1483812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) { 1484c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1485c98d5d94SLen Brown return -7; 1486fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1487fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1488fb5d4327SDasaratharaman Chandramouli return -7; 1489103a8feaSLen Brown } 149015aaa346SLen Brown 1491812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 1492c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1493c98d5d94SLen Brown return -8; 1494c98d5d94SLen Brown 14950539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 14960539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 14970539ba11SLen Brown return -8; 14980539ba11SLen Brown 1499812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 1500889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1501889facbeSLen Brown return -9; 1502889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1503889facbeSLen Brown } 1504889facbeSLen Brown 1505388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1506495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 1507388e9c81SLen Brown return -10; 1508388e9c81SLen Brown } 1509889facbeSLen Brown 1510c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1511c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1512c98d5d94SLen Brown return 0; 151315aaa346SLen Brown 15140b2bb692SLen Brown if (do_skl_residency) { 15150b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 15160b2bb692SLen Brown return -10; 15170b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 15180b2bb692SLen Brown return -11; 15190b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 15200b2bb692SLen Brown return -12; 15210b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 15220b2bb692SLen Brown return -13; 15230b2bb692SLen Brown } 15240f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1525c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1526c98d5d94SLen Brown return -9; 15270f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 15280539ba11SLen Brown if (do_slm_cstates) { 15290539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 15300539ba11SLen Brown return -10; 15310539ba11SLen Brown } else { 1532c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1533c98d5d94SLen Brown return -10; 15340539ba11SLen Brown } 15350539ba11SLen Brown } 15360539ba11SLen Brown 15370f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 1538c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1539c98d5d94SLen Brown return -11; 15400f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1541c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1542c98d5d94SLen Brown return -12; 15430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 1544ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1545ca58710fSKristen Carlson Accardi return -13; 15460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 1547ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1548ca58710fSKristen Carlson Accardi return -13; 15490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 1550ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1551ca58710fSKristen Carlson Accardi return -13; 15520f47c08dSLen Brown 1553889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1554889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1555889facbeSLen Brown return -13; 1556889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1557889facbeSLen Brown } 15589148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 1559889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1560889facbeSLen Brown return -14; 1561889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1562889facbeSLen Brown } 1563889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1564889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1565889facbeSLen Brown return -15; 1566889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1567889facbeSLen Brown } 1568889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1569889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1570889facbeSLen Brown return -16; 1571889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1572889facbeSLen Brown } 1573889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1574889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1575889facbeSLen Brown return -16; 1576889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1577889facbeSLen Brown } 1578889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1579889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1580889facbeSLen Brown return -16; 1581889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1582889facbeSLen Brown } 1583812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 1584889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1585889facbeSLen Brown return -17; 1586889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1587889facbeSLen Brown } 1588fdf676e5SLen Brown 1589812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 1590fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 1591fdf676e5SLen Brown 1592812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 159327d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 159427d47356SLen Brown 1595388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1596495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 1597388e9c81SLen Brown return -10; 1598388e9c81SLen Brown } 1599388e9c81SLen Brown 160015aaa346SLen Brown return 0; 1601103a8feaSLen Brown } 1602103a8feaSLen Brown 1603ee7e38e3SLen Brown /* 1604ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1605ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1606ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1607ee7e38e3SLen Brown */ 1608ee7e38e3SLen Brown 1609ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1610ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1611ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1612ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1613ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1614ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1615ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1616ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1617ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1618ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1619ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1620ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 16210b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 16220b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 16230b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1624ee7e38e3SLen Brown 1625ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1626ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 16270b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1628ee7e38e3SLen Brown 1629e9257f5fSLen 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}; 1630e9257f5fSLen 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}; 1631e9257f5fSLen 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}; 16320539ba11SLen 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}; 1633f2642888SLen 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}; 1634e9257f5fSLen 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}; 1635e4085d54SLen 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}; 1636d8ebb442SLen 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}; 1637ee7e38e3SLen Brown 1638a2b7b749SLen Brown 1639a2b7b749SLen Brown static void 1640a2b7b749SLen Brown calculate_tsc_tweak() 1641a2b7b749SLen Brown { 1642a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 1643a2b7b749SLen Brown } 1644a2b7b749SLen Brown 1645fcd17211SLen Brown static void 1646fcd17211SLen Brown dump_nhm_platform_info(void) 1647103a8feaSLen Brown { 1648103a8feaSLen Brown unsigned long long msr; 1649103a8feaSLen Brown unsigned int ratio; 1650103a8feaSLen Brown 1651ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 1652103a8feaSLen Brown 1653b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 16546574a5d5SLen Brown 1655103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1656710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 1657103a8feaSLen Brown ratio, bclk, ratio * bclk); 1658103a8feaSLen Brown 1659103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1660710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 1661103a8feaSLen Brown ratio, bclk, ratio * bclk); 1662103a8feaSLen Brown 16637ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1664b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1665bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 166667920418SLen Brown 1667fcd17211SLen Brown return; 1668fcd17211SLen Brown } 1669fcd17211SLen Brown 1670fcd17211SLen Brown static void 1671fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 1672fcd17211SLen Brown { 1673fcd17211SLen Brown unsigned long long msr; 1674fcd17211SLen Brown unsigned int ratio; 1675fcd17211SLen Brown 16767ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 1677fcd17211SLen Brown 1678b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 1679fcd17211SLen Brown 1680fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 1681fcd17211SLen Brown if (ratio) 1682710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 1683fcd17211SLen Brown ratio, bclk, ratio * bclk); 1684fcd17211SLen Brown 1685fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 1686fcd17211SLen Brown if (ratio) 1687710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 1688fcd17211SLen Brown ratio, bclk, ratio * bclk); 1689fcd17211SLen Brown return; 1690fcd17211SLen Brown } 1691fcd17211SLen Brown 1692fcd17211SLen Brown static void 1693fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 1694fcd17211SLen Brown { 1695fcd17211SLen Brown unsigned long long msr; 1696fcd17211SLen Brown unsigned int ratio; 16976574a5d5SLen Brown 16987ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 16996574a5d5SLen Brown 1700b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 17016574a5d5SLen Brown 17026574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 17036574a5d5SLen Brown if (ratio) 1704710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 17056574a5d5SLen Brown ratio, bclk, ratio * bclk); 17066574a5d5SLen Brown 17076574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 17086574a5d5SLen Brown if (ratio) 1709710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 17106574a5d5SLen Brown ratio, bclk, ratio * bclk); 17116574a5d5SLen Brown 17126574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 17136574a5d5SLen Brown if (ratio) 1714710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 17156574a5d5SLen Brown ratio, bclk, ratio * bclk); 17166574a5d5SLen Brown 17176574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 17186574a5d5SLen Brown if (ratio) 1719710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 17206574a5d5SLen Brown ratio, bclk, ratio * bclk); 17216574a5d5SLen Brown 17226574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 17236574a5d5SLen Brown if (ratio) 1724710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 17256574a5d5SLen Brown ratio, bclk, ratio * bclk); 17266574a5d5SLen Brown 17276574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 17286574a5d5SLen Brown if (ratio) 1729710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 17306574a5d5SLen Brown ratio, bclk, ratio * bclk); 17316574a5d5SLen Brown 17326574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 17336574a5d5SLen Brown if (ratio) 1734710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 17356574a5d5SLen Brown ratio, bclk, ratio * bclk); 17366574a5d5SLen Brown 17376574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 17386574a5d5SLen Brown if (ratio) 1739710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 17406574a5d5SLen Brown ratio, bclk, ratio * bclk); 1741103a8feaSLen Brown return; 1742fcd17211SLen Brown } 174331e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 174431e07522SLen Brown { 174531e07522SLen Brown 174631e07522SLen Brown if (!genuine_intel) 174731e07522SLen Brown return 0; 174831e07522SLen Brown 174931e07522SLen Brown switch (model) { 175031e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 175131e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 175231e07522SLen Brown case INTEL_FAM6_ATOM_DENVERTON: 175331e07522SLen Brown return 1; 175431e07522SLen Brown } 175531e07522SLen Brown return 0; 175631e07522SLen Brown } 1757fcd17211SLen Brown 1758fcd17211SLen Brown static void 175931e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 1760fcd17211SLen Brown { 176131e07522SLen Brown unsigned long long msr, core_counts; 176231e07522SLen Brown unsigned int ratio, group_size; 1763103a8feaSLen Brown 17647ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1765b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 17666574a5d5SLen Brown 176731e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 176831e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 176931e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 177031e07522SLen Brown } else { 177131e07522SLen Brown core_counts = 0x0807060504030201; 177231e07522SLen Brown } 177331e07522SLen Brown 17746574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 177531e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 17766574a5d5SLen Brown if (ratio) 177731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 177831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 17796574a5d5SLen Brown 17806574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 178131e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 17826574a5d5SLen Brown if (ratio) 178331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 178431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 17856574a5d5SLen Brown 17866574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 178731e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 17886574a5d5SLen Brown if (ratio) 178931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 179031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 17916574a5d5SLen Brown 17926574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 179331e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 17946574a5d5SLen Brown if (ratio) 179531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 179631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 17976574a5d5SLen Brown 1798103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 179931e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 1800103a8feaSLen Brown if (ratio) 180131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 180231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1803103a8feaSLen Brown 1804103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 180531e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 1806103a8feaSLen Brown if (ratio) 180731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 180831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1809103a8feaSLen Brown 1810103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 181131e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 1812103a8feaSLen Brown if (ratio) 181331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 181431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1815103a8feaSLen Brown 1816103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 181731e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 1818103a8feaSLen Brown if (ratio) 181931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 182031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 1821fcd17211SLen Brown return; 1822fcd17211SLen Brown } 18233a9a941dSLen Brown 1824fcd17211SLen Brown static void 18250f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 18260f7887c4SLen Brown { 18270f7887c4SLen Brown unsigned long long msr; 18280f7887c4SLen Brown unsigned int ratio; 18290f7887c4SLen Brown 18300f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 18310f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 18320f7887c4SLen Brown 18330f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 18340f7887c4SLen Brown if (ratio) 18350f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 18360f7887c4SLen Brown ratio, bclk, ratio * bclk); 18370f7887c4SLen Brown 18380f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 18390f7887c4SLen Brown if (ratio) 18400f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 18410f7887c4SLen Brown ratio, bclk, ratio * bclk); 18420f7887c4SLen Brown 18430f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 18440f7887c4SLen Brown if (ratio) 18450f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 18460f7887c4SLen Brown ratio, bclk, ratio * bclk); 18470f7887c4SLen Brown 18480f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 18490f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 18500f7887c4SLen Brown 18510f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 18520f7887c4SLen Brown if (ratio) 18530f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 18540f7887c4SLen Brown ratio, bclk, ratio * bclk); 18550f7887c4SLen Brown 18560f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 18570f7887c4SLen Brown if (ratio) 18580f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 18590f7887c4SLen Brown ratio, bclk, ratio * bclk); 18600f7887c4SLen Brown 18610f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 18620f7887c4SLen Brown if (ratio) 18630f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 18640f7887c4SLen Brown ratio, bclk, ratio * bclk); 18650f7887c4SLen Brown 18660f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 18670f7887c4SLen Brown if (ratio) 18680f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 18690f7887c4SLen Brown ratio, bclk, ratio * bclk); 18700f7887c4SLen Brown } 18710f7887c4SLen Brown 18720f7887c4SLen Brown static void 1873fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 1874fb5d4327SDasaratharaman Chandramouli { 1875cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 1876cbf97abaSHubert Chrzaniuk 1877fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 1878cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 1879cbf97abaSHubert Chrzaniuk int i, b_nr; 1880cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 1881cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 1882fb5d4327SDasaratharaman Chandramouli 1883ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1884fb5d4327SDasaratharaman Chandramouli 1885b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 1886bfae2052SLen Brown base_cpu, msr); 1887fb5d4327SDasaratharaman Chandramouli 1888fb5d4327SDasaratharaman Chandramouli /** 1889fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 1890cbf97abaSHubert Chrzaniuk * [0] -- Reserved 1891cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 1892fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 1893fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 1894fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 1895fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 1896fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 1897fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 1898fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 1899fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 1900fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 1901fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 1902fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 1903fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 1904fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 1905fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 1906fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 1907fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 1908fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 1909fb5d4327SDasaratharaman Chandramouli */ 1910fb5d4327SDasaratharaman Chandramouli 1911cbf97abaSHubert Chrzaniuk b_nr = 0; 1912cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 1913cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 1914cbf97abaSHubert Chrzaniuk 1915cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 1916fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 1917cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 1918fb5d4327SDasaratharaman Chandramouli 1919cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 1920cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 1921cbf97abaSHubert Chrzaniuk b_nr++; 1922fb5d4327SDasaratharaman Chandramouli } 1923cbf97abaSHubert Chrzaniuk 1924cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 1925cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 1926b7d8c148SLen Brown fprintf(outf, 1927710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 1928cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 1929fb5d4327SDasaratharaman Chandramouli } 1930fb5d4327SDasaratharaman Chandramouli 1931fb5d4327SDasaratharaman Chandramouli static void 1932fcd17211SLen Brown dump_nhm_cst_cfg(void) 1933fcd17211SLen Brown { 1934fcd17211SLen Brown unsigned long long msr; 1935fcd17211SLen Brown 19361df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 1937fcd17211SLen Brown 1938fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE (1UL << 27) 1939fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE (1UL << 28) 1940fcd17211SLen Brown 19411df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 1942fcd17211SLen Brown 1943b7d8c148SLen Brown fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n", 1944fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 1945fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 1946fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 1947fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 1948fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 19496c34f160SLen Brown (unsigned int)msr & 0xF, 1950fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 1951fcd17211SLen Brown return; 1952103a8feaSLen Brown } 1953103a8feaSLen Brown 19546fb3143bSLen Brown static void 19556fb3143bSLen Brown dump_config_tdp(void) 19566fb3143bSLen Brown { 19576fb3143bSLen Brown unsigned long long msr; 19586fb3143bSLen Brown 19596fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 1960b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 1961685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 19626fb3143bSLen Brown 19636fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 1964b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 19656fb3143bSLen Brown if (msr) { 1966685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 1967685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 1968685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 1969685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 19706fb3143bSLen Brown } 1971b7d8c148SLen Brown fprintf(outf, ")\n"); 19726fb3143bSLen Brown 19736fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 1974b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 19756fb3143bSLen Brown if (msr) { 1976685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 1977685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 1978685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 1979685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 19806fb3143bSLen Brown } 1981b7d8c148SLen Brown fprintf(outf, ")\n"); 19826fb3143bSLen Brown 19836fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 1984b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 19856fb3143bSLen Brown if ((msr) & 0x3) 1986b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 1987b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 1988b7d8c148SLen Brown fprintf(outf, ")\n"); 19896fb3143bSLen Brown 19906fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 1991b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 1992685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 1993b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 1994b7d8c148SLen Brown fprintf(outf, ")\n"); 19956fb3143bSLen Brown } 19965a63426eSLen Brown 19975a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 19985a63426eSLen Brown 19995a63426eSLen Brown void print_irtl(void) 20005a63426eSLen Brown { 20015a63426eSLen Brown unsigned long long msr; 20025a63426eSLen Brown 20035a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 20045a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 20055a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 20065a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 20075a63426eSLen Brown 20085a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 20095a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 20105a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 20115a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 20125a63426eSLen Brown 20135a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 20145a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 20155a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 20165a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 20175a63426eSLen Brown 20185a63426eSLen Brown if (!do_irtl_hsw) 20195a63426eSLen Brown return; 20205a63426eSLen Brown 20215a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 20225a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 20235a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 20245a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 20255a63426eSLen Brown 20265a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 20275a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 20285a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 20295a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 20305a63426eSLen Brown 20315a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 20325a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 20335a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 20345a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 20355a63426eSLen Brown 20365a63426eSLen Brown } 203736229897SLen Brown void free_fd_percpu(void) 203836229897SLen Brown { 203936229897SLen Brown int i; 204036229897SLen Brown 204101a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 204236229897SLen Brown if (fd_percpu[i] != 0) 204336229897SLen Brown close(fd_percpu[i]); 204436229897SLen Brown } 204536229897SLen Brown 204636229897SLen Brown free(fd_percpu); 20476fb3143bSLen Brown } 20486fb3143bSLen Brown 2049c98d5d94SLen Brown void free_all_buffers(void) 2050103a8feaSLen Brown { 2051c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2052c98d5d94SLen Brown cpu_present_set = NULL; 205336229897SLen Brown cpu_present_setsize = 0; 2054103a8feaSLen Brown 2055c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2056c98d5d94SLen Brown cpu_affinity_set = NULL; 2057c98d5d94SLen Brown cpu_affinity_setsize = 0; 2058103a8feaSLen Brown 2059c98d5d94SLen Brown free(thread_even); 2060c98d5d94SLen Brown free(core_even); 2061c98d5d94SLen Brown free(package_even); 2062103a8feaSLen Brown 2063c98d5d94SLen Brown thread_even = NULL; 2064c98d5d94SLen Brown core_even = NULL; 2065c98d5d94SLen Brown package_even = NULL; 2066103a8feaSLen Brown 2067c98d5d94SLen Brown free(thread_odd); 2068c98d5d94SLen Brown free(core_odd); 2069c98d5d94SLen Brown free(package_odd); 2070103a8feaSLen Brown 2071c98d5d94SLen Brown thread_odd = NULL; 2072c98d5d94SLen Brown core_odd = NULL; 2073c98d5d94SLen Brown package_odd = NULL; 2074103a8feaSLen Brown 2075c98d5d94SLen Brown free(output_buffer); 2076c98d5d94SLen Brown output_buffer = NULL; 2077c98d5d94SLen Brown outp = NULL; 207836229897SLen Brown 207936229897SLen Brown free_fd_percpu(); 2080562a2d37SLen Brown 2081562a2d37SLen Brown free(irq_column_2_cpu); 2082562a2d37SLen Brown free(irqs_per_cpu); 2083103a8feaSLen Brown } 2084103a8feaSLen Brown 208557a42a34SJosh Triplett 208657a42a34SJosh Triplett /* 208795aebc44SJosh Triplett * Parse a file containing a single int. 208895aebc44SJosh Triplett */ 208995aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 209095aebc44SJosh Triplett { 209195aebc44SJosh Triplett va_list args; 209295aebc44SJosh Triplett char path[PATH_MAX]; 209395aebc44SJosh Triplett FILE *filep; 209495aebc44SJosh Triplett int value; 209595aebc44SJosh Triplett 209695aebc44SJosh Triplett va_start(args, fmt); 209795aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 209895aebc44SJosh Triplett va_end(args); 209957a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 2100b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2101b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 210295aebc44SJosh Triplett fclose(filep); 210395aebc44SJosh Triplett return value; 210495aebc44SJosh Triplett } 210595aebc44SJosh Triplett 210695aebc44SJosh Triplett /* 2107e275b388SDasaratharaman Chandramouli * get_cpu_position_in_core(cpu) 2108e275b388SDasaratharaman Chandramouli * return the position of the CPU among its HT siblings in the core 2109e275b388SDasaratharaman Chandramouli * return -1 if the sibling is not in list 2110103a8feaSLen Brown */ 2111e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu) 2112103a8feaSLen Brown { 2113e275b388SDasaratharaman Chandramouli char path[64]; 2114e275b388SDasaratharaman Chandramouli FILE *filep; 2115e275b388SDasaratharaman Chandramouli int this_cpu; 2116e275b388SDasaratharaman Chandramouli char character; 2117e275b388SDasaratharaman Chandramouli int i; 2118e275b388SDasaratharaman Chandramouli 2119e275b388SDasaratharaman Chandramouli sprintf(path, 2120e275b388SDasaratharaman Chandramouli "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", 2121e275b388SDasaratharaman Chandramouli cpu); 2122e275b388SDasaratharaman Chandramouli filep = fopen(path, "r"); 2123e275b388SDasaratharaman Chandramouli if (filep == NULL) { 2124e275b388SDasaratharaman Chandramouli perror(path); 2125e275b388SDasaratharaman Chandramouli exit(1); 2126e275b388SDasaratharaman Chandramouli } 2127e275b388SDasaratharaman Chandramouli 2128e275b388SDasaratharaman Chandramouli for (i = 0; i < topo.num_threads_per_core; i++) { 2129e275b388SDasaratharaman Chandramouli fscanf(filep, "%d", &this_cpu); 2130e275b388SDasaratharaman Chandramouli if (this_cpu == cpu) { 2131e275b388SDasaratharaman Chandramouli fclose(filep); 2132e275b388SDasaratharaman Chandramouli return i; 2133e275b388SDasaratharaman Chandramouli } 2134e275b388SDasaratharaman Chandramouli 2135e275b388SDasaratharaman Chandramouli /* Account for no separator after last thread*/ 2136e275b388SDasaratharaman Chandramouli if (i != (topo.num_threads_per_core - 1)) 2137e275b388SDasaratharaman Chandramouli fscanf(filep, "%c", &character); 2138e275b388SDasaratharaman Chandramouli } 2139e275b388SDasaratharaman Chandramouli 2140e275b388SDasaratharaman Chandramouli fclose(filep); 2141e275b388SDasaratharaman Chandramouli return -1; 2142c98d5d94SLen Brown } 2143103a8feaSLen Brown 2144c98d5d94SLen Brown /* 2145c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2146c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2147c98d5d94SLen Brown */ 2148c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2149c98d5d94SLen Brown { 215095aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2151103a8feaSLen Brown } 2152103a8feaSLen Brown 2153103a8feaSLen Brown int get_physical_package_id(int cpu) 2154103a8feaSLen Brown { 215595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2156103a8feaSLen Brown } 2157103a8feaSLen Brown 2158103a8feaSLen Brown int get_core_id(int cpu) 2159103a8feaSLen Brown { 216095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2161103a8feaSLen Brown } 2162103a8feaSLen Brown 2163c98d5d94SLen Brown int get_num_ht_siblings(int cpu) 2164c98d5d94SLen Brown { 2165c98d5d94SLen Brown char path[80]; 2166c98d5d94SLen Brown FILE *filep; 2167e275b388SDasaratharaman Chandramouli int sib1; 2168e275b388SDasaratharaman Chandramouli int matches = 0; 2169c98d5d94SLen Brown char character; 2170e275b388SDasaratharaman Chandramouli char str[100]; 2171e275b388SDasaratharaman Chandramouli char *ch; 2172c98d5d94SLen Brown 2173c98d5d94SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); 217457a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 2175e275b388SDasaratharaman Chandramouli 2176103a8feaSLen Brown /* 2177c98d5d94SLen Brown * file format: 2178e275b388SDasaratharaman Chandramouli * A ',' separated or '-' separated set of numbers 2179e275b388SDasaratharaman Chandramouli * (eg 1-2 or 1,3,4,5) 2180c98d5d94SLen Brown */ 2181e275b388SDasaratharaman Chandramouli fscanf(filep, "%d%c\n", &sib1, &character); 2182e275b388SDasaratharaman Chandramouli fseek(filep, 0, SEEK_SET); 2183e275b388SDasaratharaman Chandramouli fgets(str, 100, filep); 2184e275b388SDasaratharaman Chandramouli ch = strchr(str, character); 2185e275b388SDasaratharaman Chandramouli while (ch != NULL) { 2186e275b388SDasaratharaman Chandramouli matches++; 2187e275b388SDasaratharaman Chandramouli ch = strchr(ch+1, character); 2188e275b388SDasaratharaman Chandramouli } 2189c98d5d94SLen Brown 2190c98d5d94SLen Brown fclose(filep); 2191e275b388SDasaratharaman Chandramouli return matches+1; 2192c98d5d94SLen Brown } 2193c98d5d94SLen Brown 2194c98d5d94SLen Brown /* 2195c98d5d94SLen Brown * run func(thread, core, package) in topology order 2196c98d5d94SLen Brown * skip non-present cpus 2197103a8feaSLen Brown */ 2198103a8feaSLen Brown 2199c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2200c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2201c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2202c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2203c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2204c98d5d94SLen Brown struct pkg_data *pkg_base2) 2205c98d5d94SLen Brown { 2206c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 2207c98d5d94SLen Brown 2208c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 2209c98d5d94SLen Brown for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) { 2210c98d5d94SLen Brown for (thread_no = 0; thread_no < 2211c98d5d94SLen Brown topo.num_threads_per_core; ++thread_no) { 2212c98d5d94SLen Brown struct thread_data *t, *t2; 2213c98d5d94SLen Brown struct core_data *c, *c2; 2214c98d5d94SLen Brown struct pkg_data *p, *p2; 2215c98d5d94SLen Brown 2216c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 2217c98d5d94SLen Brown 2218c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2219c98d5d94SLen Brown continue; 2220c98d5d94SLen Brown 2221c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 2222c98d5d94SLen Brown 2223c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 2224c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 2225c98d5d94SLen Brown 2226c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2227c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2228c98d5d94SLen Brown 2229c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2230c98d5d94SLen Brown if (retval) 2231c98d5d94SLen Brown return retval; 2232c98d5d94SLen Brown } 2233c98d5d94SLen Brown } 2234c98d5d94SLen Brown } 2235c98d5d94SLen Brown return 0; 2236c98d5d94SLen Brown } 2237c98d5d94SLen Brown 2238c98d5d94SLen Brown /* 2239c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2240c98d5d94SLen Brown * return max_cpu number 2241c98d5d94SLen Brown */ 2242c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2243103a8feaSLen Brown { 2244103a8feaSLen Brown FILE *fp; 2245c98d5d94SLen Brown int cpu_num; 2246103a8feaSLen Brown int retval; 2247103a8feaSLen Brown 224857a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2249103a8feaSLen Brown 2250103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2251b2c95d90SJosh Triplett if (retval != 0) 2252b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2253103a8feaSLen Brown 2254c98d5d94SLen Brown while (1) { 2255c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2256103a8feaSLen Brown if (retval != 1) 2257103a8feaSLen Brown break; 2258103a8feaSLen Brown 2259c98d5d94SLen Brown retval = func(cpu_num); 2260c98d5d94SLen Brown if (retval) { 2261c98d5d94SLen Brown fclose(fp); 2262c98d5d94SLen Brown return(retval); 2263c98d5d94SLen Brown } 2264103a8feaSLen Brown } 2265103a8feaSLen Brown fclose(fp); 2266c98d5d94SLen Brown return 0; 2267103a8feaSLen Brown } 2268103a8feaSLen Brown 2269103a8feaSLen Brown void re_initialize(void) 2270103a8feaSLen Brown { 2271c98d5d94SLen Brown free_all_buffers(); 2272c98d5d94SLen Brown setup_all_buffers(); 2273c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2274103a8feaSLen Brown } 2275103a8feaSLen Brown 2276c98d5d94SLen Brown 2277103a8feaSLen Brown /* 2278c98d5d94SLen Brown * count_cpus() 2279c98d5d94SLen Brown * remember the last one seen, it will be the max 2280103a8feaSLen Brown */ 2281c98d5d94SLen Brown int count_cpus(int cpu) 2282103a8feaSLen Brown { 2283c98d5d94SLen Brown if (topo.max_cpu_num < cpu) 2284c98d5d94SLen Brown topo.max_cpu_num = cpu; 2285103a8feaSLen Brown 2286c98d5d94SLen Brown topo.num_cpus += 1; 2287c98d5d94SLen Brown return 0; 2288103a8feaSLen Brown } 2289c98d5d94SLen Brown int mark_cpu_present(int cpu) 2290c98d5d94SLen Brown { 2291c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 229215aaa346SLen Brown return 0; 2293103a8feaSLen Brown } 2294103a8feaSLen Brown 2295562a2d37SLen Brown /* 2296562a2d37SLen Brown * snapshot_proc_interrupts() 2297562a2d37SLen Brown * 2298562a2d37SLen Brown * read and record summary of /proc/interrupts 2299562a2d37SLen Brown * 2300562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2301562a2d37SLen Brown */ 2302562a2d37SLen Brown int snapshot_proc_interrupts(void) 2303562a2d37SLen Brown { 2304562a2d37SLen Brown static FILE *fp; 2305562a2d37SLen Brown int column, retval; 2306562a2d37SLen Brown 2307562a2d37SLen Brown if (fp == NULL) 2308562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2309562a2d37SLen Brown else 2310562a2d37SLen Brown rewind(fp); 2311562a2d37SLen Brown 2312562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2313562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2314562a2d37SLen Brown int cpu_number; 2315562a2d37SLen Brown 2316562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2317562a2d37SLen Brown if (retval != 1) 2318562a2d37SLen Brown break; 2319562a2d37SLen Brown 2320562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2321562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2322562a2d37SLen Brown return 1; 2323562a2d37SLen Brown } 2324562a2d37SLen Brown 2325562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2326562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2327562a2d37SLen Brown } 2328562a2d37SLen Brown 2329562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2330562a2d37SLen Brown while (1) { 2331562a2d37SLen Brown int column; 2332562a2d37SLen Brown char buf[64]; 2333562a2d37SLen Brown 2334562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2335562a2d37SLen Brown if (retval != 1) 2336562a2d37SLen Brown break; 2337562a2d37SLen Brown 2338562a2d37SLen Brown /* read the count per cpu */ 2339562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2340562a2d37SLen Brown 2341562a2d37SLen Brown int cpu_number, irq_count; 2342562a2d37SLen Brown 2343562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2344562a2d37SLen Brown if (retval != 1) 2345562a2d37SLen Brown break; 2346562a2d37SLen Brown 2347562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2348562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2349562a2d37SLen Brown 2350562a2d37SLen Brown } 2351562a2d37SLen Brown 2352562a2d37SLen Brown while (getc(fp) != '\n') 2353562a2d37SLen Brown ; /* flush interrupt description */ 2354562a2d37SLen Brown 2355562a2d37SLen Brown } 2356562a2d37SLen Brown return 0; 2357562a2d37SLen Brown } 235827d47356SLen Brown /* 2359fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2360fdf676e5SLen Brown * 2361fdf676e5SLen Brown * record snapshot of 2362fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2363fdf676e5SLen Brown * 2364fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2365fdf676e5SLen Brown */ 2366fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2367fdf676e5SLen Brown { 2368fdf676e5SLen Brown FILE *fp; 2369fdf676e5SLen Brown int retval; 2370fdf676e5SLen Brown 2371fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 2372fdf676e5SLen Brown 2373fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 2374fdf676e5SLen Brown if (retval != 1) 2375fdf676e5SLen Brown err(1, "GFX rc6"); 2376fdf676e5SLen Brown 2377fdf676e5SLen Brown fclose(fp); 2378fdf676e5SLen Brown 2379fdf676e5SLen Brown return 0; 2380fdf676e5SLen Brown } 2381fdf676e5SLen Brown /* 238227d47356SLen Brown * snapshot_gfx_mhz() 238327d47356SLen Brown * 238427d47356SLen Brown * record snapshot of 238527d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 238627d47356SLen Brown * 238727d47356SLen Brown * return 1 if config change requires a restart, else return 0 238827d47356SLen Brown */ 238927d47356SLen Brown int snapshot_gfx_mhz(void) 239027d47356SLen Brown { 239127d47356SLen Brown static FILE *fp; 239227d47356SLen Brown int retval; 239327d47356SLen Brown 239427d47356SLen Brown if (fp == NULL) 239527d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 239627d47356SLen Brown else 239727d47356SLen Brown rewind(fp); 239827d47356SLen Brown 239927d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 240027d47356SLen Brown if (retval != 1) 240127d47356SLen Brown err(1, "GFX MHz"); 240227d47356SLen Brown 240327d47356SLen Brown return 0; 240427d47356SLen Brown } 2405562a2d37SLen Brown 2406562a2d37SLen Brown /* 2407562a2d37SLen Brown * snapshot /proc and /sys files 2408562a2d37SLen Brown * 2409562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 2410562a2d37SLen Brown */ 2411562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 2412562a2d37SLen Brown { 2413562a2d37SLen Brown if (snapshot_proc_interrupts()) 2414562a2d37SLen Brown return 1; 2415562a2d37SLen Brown 2416812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2417fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 2418fdf676e5SLen Brown 2419812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 242027d47356SLen Brown snapshot_gfx_mhz(); 242127d47356SLen Brown 2422562a2d37SLen Brown return 0; 2423562a2d37SLen Brown } 2424562a2d37SLen Brown 2425103a8feaSLen Brown void turbostat_loop() 2426103a8feaSLen Brown { 2427c98d5d94SLen Brown int retval; 2428e52966c0SLen Brown int restarted = 0; 2429c98d5d94SLen Brown 2430103a8feaSLen Brown restart: 2431e52966c0SLen Brown restarted++; 2432e52966c0SLen Brown 2433562a2d37SLen Brown snapshot_proc_sysfs_files(); 2434c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2435d91bb17cSLen Brown if (retval < -1) { 2436d91bb17cSLen Brown exit(retval); 2437d91bb17cSLen Brown } else if (retval == -1) { 2438e52966c0SLen Brown if (restarted > 1) { 2439e52966c0SLen Brown exit(retval); 2440e52966c0SLen Brown } 2441c98d5d94SLen Brown re_initialize(); 2442c98d5d94SLen Brown goto restart; 2443c98d5d94SLen Brown } 2444e52966c0SLen Brown restarted = 0; 2445103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2446103a8feaSLen Brown 2447103a8feaSLen Brown while (1) { 2448c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 2449103a8feaSLen Brown re_initialize(); 2450103a8feaSLen Brown goto restart; 2451103a8feaSLen Brown } 24522a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2453562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2454562a2d37SLen Brown goto restart; 2455c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 2456d91bb17cSLen Brown if (retval < -1) { 2457d91bb17cSLen Brown exit(retval); 2458d91bb17cSLen Brown } else if (retval == -1) { 245915aaa346SLen Brown re_initialize(); 246015aaa346SLen Brown goto restart; 246115aaa346SLen Brown } 2462103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2463103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2464ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 2465ba3dec99SLen Brown re_initialize(); 2466ba3dec99SLen Brown goto restart; 2467ba3dec99SLen Brown } 2468c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2469c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2470b7d8c148SLen Brown flush_output_stdout(); 24712a0609c0SLen Brown nanosleep(&interval_ts, NULL); 2472562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2473562a2d37SLen Brown goto restart; 2474c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2475d91bb17cSLen Brown if (retval < -1) { 2476d91bb17cSLen Brown exit(retval); 2477d91bb17cSLen Brown } else if (retval == -1) { 2478103a8feaSLen Brown re_initialize(); 2479103a8feaSLen Brown goto restart; 2480103a8feaSLen Brown } 2481103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2482103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 2483ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 2484ba3dec99SLen Brown re_initialize(); 2485ba3dec99SLen Brown goto restart; 2486ba3dec99SLen Brown } 2487c98d5d94SLen Brown compute_average(ODD_COUNTERS); 2488c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 2489b7d8c148SLen Brown flush_output_stdout(); 2490103a8feaSLen Brown } 2491103a8feaSLen Brown } 2492103a8feaSLen Brown 2493103a8feaSLen Brown void check_dev_msr() 2494103a8feaSLen Brown { 2495103a8feaSLen Brown struct stat sb; 24967ce7d5deSPrarit Bhargava char pathname[32]; 2497103a8feaSLen Brown 24987ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 24997ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 2500a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 2501d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 2502103a8feaSLen Brown } 2503103a8feaSLen Brown 250498481e79SLen Brown void check_permissions() 2505103a8feaSLen Brown { 250698481e79SLen Brown struct __user_cap_header_struct cap_header_data; 250798481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 250898481e79SLen Brown struct __user_cap_data_struct cap_data_data; 250998481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 251098481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 251198481e79SLen Brown int do_exit = 0; 25127ce7d5deSPrarit Bhargava char pathname[32]; 251398481e79SLen Brown 251498481e79SLen Brown /* check for CAP_SYS_RAWIO */ 251598481e79SLen Brown cap_header->pid = getpid(); 251698481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 251798481e79SLen Brown if (capget(cap_header, cap_data) < 0) 251898481e79SLen Brown err(-6, "capget(2) failed"); 251998481e79SLen Brown 252098481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 252198481e79SLen Brown do_exit++; 252298481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 252398481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 252498481e79SLen Brown } 252598481e79SLen Brown 252698481e79SLen Brown /* test file permissions */ 25277ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 25287ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 252998481e79SLen Brown do_exit++; 253098481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 253198481e79SLen Brown } 253298481e79SLen Brown 253398481e79SLen Brown /* if all else fails, thell them to be root */ 253498481e79SLen Brown if (do_exit) 2535b2c95d90SJosh Triplett if (getuid() != 0) 2536d7899447SLen Brown warnx("... or simply run as root"); 253798481e79SLen Brown 253898481e79SLen Brown if (do_exit) 253998481e79SLen Brown exit(-6); 2540103a8feaSLen Brown } 2541103a8feaSLen Brown 2542d7899447SLen Brown /* 2543d7899447SLen Brown * NHM adds support for additional MSRs: 2544d7899447SLen Brown * 2545d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 2546d7899447SLen Brown * 2547ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 25481df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 2549d7899447SLen Brown * 2550cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 2551cf4cbe53SLen Brown * 2552d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 2553d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 2554d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 2555d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 2556d7899447SLen Brown * 2557ee7e38e3SLen Brown * Side effect: 25581df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 255933148d67SLen Brown * sets has_misc_feature_control 2560d7899447SLen Brown */ 2561ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 2562103a8feaSLen Brown { 2563ee7e38e3SLen Brown unsigned long long msr; 256421ed5574SLen Brown unsigned int base_ratio; 2565ee7e38e3SLen Brown int *pkg_cstate_limits; 2566ee7e38e3SLen Brown 2567103a8feaSLen Brown if (!genuine_intel) 2568103a8feaSLen Brown return 0; 2569103a8feaSLen Brown 2570103a8feaSLen Brown if (family != 6) 2571103a8feaSLen Brown return 0; 2572103a8feaSLen Brown 257321ed5574SLen Brown bclk = discover_bclk(family, model); 257421ed5574SLen Brown 2575103a8feaSLen Brown switch (model) { 2576869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 2577869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 2578103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 2579869ce69eSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 2580869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 2581869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 2582869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 2583ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 2584ee7e38e3SLen Brown break; 2585869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 2586869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 2587869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 2588869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 2589ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 259033148d67SLen Brown has_misc_feature_control = 1; 2591ee7e38e3SLen Brown break; 2592869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2593869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2594869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2595869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2596869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2597869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2598869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2599869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2600869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2601869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2602869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2603869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2604ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 260533148d67SLen Brown has_misc_feature_control = 1; 2606ee7e38e3SLen Brown break; 2607d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 2608d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 260933148d67SLen Brown has_misc_feature_control = 1; 2610d8ebb442SLen Brown break; 2611869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 2612cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 2613869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 2614ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 2615ee7e38e3SLen Brown break; 2616869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 2617ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 2618cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 2619ee7e38e3SLen Brown break; 2620869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 2621005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2622ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 2623ee7e38e3SLen Brown break; 2624869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 2625ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 2626869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 2627e4085d54SLen Brown pkg_cstate_limits = bxt_pkg_cstate_limits; 2628e4085d54SLen Brown break; 2629103a8feaSLen Brown default: 2630103a8feaSLen Brown return 0; 2631103a8feaSLen Brown } 26321df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2633e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 2634ee7e38e3SLen Brown 2635ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 263621ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 263721ed5574SLen Brown 263821ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 263921ed5574SLen Brown has_base_hz = 1; 2640ee7e38e3SLen Brown return 1; 2641103a8feaSLen Brown } 26420f7887c4SLen Brown /* 2643495c7654SLen Brown * SLV client has support for unique MSRs: 26440f7887c4SLen Brown * 26450f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 26460f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 26470f7887c4SLen Brown */ 26480f7887c4SLen Brown 26490f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 26500f7887c4SLen Brown { 26510f7887c4SLen Brown if (!genuine_intel) 26520f7887c4SLen Brown return 0; 26530f7887c4SLen Brown 26540f7887c4SLen Brown switch (model) { 26550f7887c4SLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: 26560f7887c4SLen Brown case INTEL_FAM6_ATOM_MERRIFIELD: 26570f7887c4SLen Brown case INTEL_FAM6_ATOM_MOOREFIELD: 26580f7887c4SLen Brown return 1; 26590f7887c4SLen Brown } 26600f7887c4SLen Brown return 0; 26610f7887c4SLen Brown } 26627170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 26637170a374SLen Brown { 26647170a374SLen Brown 26657170a374SLen Brown if (!genuine_intel) 26667170a374SLen Brown return 0; 26677170a374SLen Brown 26687170a374SLen Brown switch (model) { 26697170a374SLen Brown case INTEL_FAM6_ATOM_DENVERTON: 26707170a374SLen Brown return 1; 26717170a374SLen Brown } 26727170a374SLen Brown return 0; 26737170a374SLen Brown } 2674ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 2675ade0ebacSLen Brown { 2676ade0ebacSLen Brown 2677ade0ebacSLen Brown if (!genuine_intel) 2678ade0ebacSLen Brown return 0; 2679ade0ebacSLen Brown 2680ade0ebacSLen Brown switch (model) { 2681ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 2682ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: 2683ade0ebacSLen Brown return 1; 2684ade0ebacSLen Brown } 2685ade0ebacSLen Brown return 0; 2686ade0ebacSLen Brown } 268734c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 268834c76197SLen Brown { 268934c76197SLen Brown 269034c76197SLen Brown if (!genuine_intel) 269134c76197SLen Brown return 0; 269234c76197SLen Brown 269334c76197SLen Brown switch (model) { 269434c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 269534c76197SLen Brown return 1; 269634c76197SLen Brown } 269734c76197SLen Brown return 0; 269834c76197SLen Brown } 26990f7887c4SLen Brown 270031e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 2701d7899447SLen Brown { 27020f7887c4SLen Brown if (has_slv_msrs(family, model)) 27030f7887c4SLen Brown return 0; 27040f7887c4SLen Brown 2705d7899447SLen Brown switch (model) { 2706d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 2707869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 2708869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 2709869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 2710005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2711d7899447SLen Brown return 0; 2712d7899447SLen Brown default: 2713d7899447SLen Brown return 1; 2714d7899447SLen Brown } 2715d7899447SLen Brown } 27160f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 27170f7887c4SLen Brown { 27180f7887c4SLen Brown if (has_slv_msrs(family, model)) 27190f7887c4SLen Brown return 1; 27200f7887c4SLen Brown 27210f7887c4SLen Brown return 0; 27220f7887c4SLen Brown } 27236574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 27246574a5d5SLen Brown { 27256574a5d5SLen Brown if (!genuine_intel) 27266574a5d5SLen Brown return 0; 27276574a5d5SLen Brown 27286574a5d5SLen Brown if (family != 6) 27296574a5d5SLen Brown return 0; 27306574a5d5SLen Brown 27316574a5d5SLen Brown switch (model) { 2732869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 2733869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 27346574a5d5SLen Brown return 1; 27356574a5d5SLen Brown default: 27366574a5d5SLen Brown return 0; 27376574a5d5SLen Brown } 27386574a5d5SLen Brown } 2739fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 2740fcd17211SLen Brown { 2741fcd17211SLen Brown if (!genuine_intel) 2742fcd17211SLen Brown return 0; 2743fcd17211SLen Brown 2744fcd17211SLen Brown if (family != 6) 2745fcd17211SLen Brown return 0; 2746fcd17211SLen Brown 2747fcd17211SLen Brown switch (model) { 2748869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 2749fcd17211SLen Brown return 1; 2750fcd17211SLen Brown default: 2751fcd17211SLen Brown return 0; 2752fcd17211SLen Brown } 2753fcd17211SLen Brown } 2754fcd17211SLen Brown 2755fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 2756fb5d4327SDasaratharaman Chandramouli { 2757fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 2758fb5d4327SDasaratharaman Chandramouli return 0; 2759fb5d4327SDasaratharaman Chandramouli 2760fb5d4327SDasaratharaman Chandramouli if (family != 6) 2761fb5d4327SDasaratharaman Chandramouli return 0; 2762fb5d4327SDasaratharaman Chandramouli 2763fb5d4327SDasaratharaman Chandramouli switch (model) { 2764869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 2765005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 2766fb5d4327SDasaratharaman Chandramouli return 1; 2767fb5d4327SDasaratharaman Chandramouli default: 2768fb5d4327SDasaratharaman Chandramouli return 0; 2769fb5d4327SDasaratharaman Chandramouli } 2770fb5d4327SDasaratharaman Chandramouli } 277131e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 277231e07522SLen Brown { 277331e07522SLen Brown if (!genuine_intel) 277431e07522SLen Brown return 0; 277531e07522SLen Brown 277631e07522SLen Brown if (family != 6) 277731e07522SLen Brown return 0; 277831e07522SLen Brown 277931e07522SLen Brown switch (model) { 278031e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 278131e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 278231e07522SLen Brown return 1; 278331e07522SLen Brown default: 278431e07522SLen Brown return 0; 278531e07522SLen Brown } 278631e07522SLen Brown } 27876fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 27886fb3143bSLen Brown { 27896fb3143bSLen Brown if (!genuine_intel) 27906fb3143bSLen Brown return 0; 27916fb3143bSLen Brown 27926fb3143bSLen Brown if (family != 6) 27936fb3143bSLen Brown return 0; 27946fb3143bSLen Brown 27956fb3143bSLen Brown switch (model) { 2796869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 2797869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 2798869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 2799869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 2800869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 2801869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 2802869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 2803869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 2804869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 2805869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 2806869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 2807869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 2808869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 2809869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 28106fb3143bSLen Brown 2811869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 2812005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 28136fb3143bSLen Brown return 1; 28146fb3143bSLen Brown default: 28156fb3143bSLen Brown return 0; 28166fb3143bSLen Brown } 28176fb3143bSLen Brown } 28186fb3143bSLen Brown 2819fcd17211SLen Brown static void 28201b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 2821fcd17211SLen Brown { 2822fcd17211SLen Brown if (!do_nhm_platform_info) 2823fcd17211SLen Brown return; 2824fcd17211SLen Brown 2825fcd17211SLen Brown dump_nhm_platform_info(); 2826fcd17211SLen Brown 2827fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 2828fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 2829fcd17211SLen Brown 2830fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 2831fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 2832fcd17211SLen Brown 283331e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 283431e07522SLen Brown dump_turbo_ratio_limits(family, model); 2835fcd17211SLen Brown 28360f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 28370f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 28380f7887c4SLen Brown 2839fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 2840fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 2841fb5d4327SDasaratharaman Chandramouli 28426fb3143bSLen Brown if (has_config_tdp(family, model)) 28436fb3143bSLen Brown dump_config_tdp(); 28446fb3143bSLen Brown 2845fcd17211SLen Brown dump_nhm_cst_cfg(); 2846fcd17211SLen Brown } 2847fcd17211SLen Brown 284841618e63SLen Brown static void 284941618e63SLen Brown dump_sysfs_cstate_config(void) 285041618e63SLen Brown { 285141618e63SLen Brown char path[64]; 285241618e63SLen Brown char name_buf[16]; 285341618e63SLen Brown char desc[64]; 285441618e63SLen Brown FILE *input; 285541618e63SLen Brown int state; 285641618e63SLen Brown char *sp; 285741618e63SLen Brown 285841618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 285941618e63SLen Brown return; 286041618e63SLen Brown 286141618e63SLen Brown for (state = 0; state < 10; ++state) { 286241618e63SLen Brown 286341618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 286441618e63SLen Brown base_cpu, state); 286541618e63SLen Brown input = fopen(path, "r"); 286641618e63SLen Brown if (input == NULL) 286741618e63SLen Brown continue; 286841618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 286941618e63SLen Brown 287041618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 287141618e63SLen Brown sp = strchr(name_buf, '-'); 287241618e63SLen Brown if (!sp) 287341618e63SLen Brown sp = strchrnul(name_buf, '\n'); 287441618e63SLen Brown *sp = '\0'; 287541618e63SLen Brown 287641618e63SLen Brown fclose(input); 287741618e63SLen Brown 287841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 287941618e63SLen Brown base_cpu, state); 288041618e63SLen Brown input = fopen(path, "r"); 288141618e63SLen Brown if (input == NULL) 288241618e63SLen Brown continue; 288341618e63SLen Brown fgets(desc, sizeof(desc), input); 288441618e63SLen Brown 288541618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 288641618e63SLen Brown fclose(input); 288741618e63SLen Brown } 288841618e63SLen Brown } 288941618e63SLen Brown 28906574a5d5SLen Brown 2891889facbeSLen Brown /* 2892889facbeSLen Brown * print_epb() 2893889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 2894889facbeSLen Brown */ 2895889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 2896889facbeSLen Brown { 2897889facbeSLen Brown unsigned long long msr; 2898889facbeSLen Brown char *epb_string; 2899889facbeSLen Brown int cpu; 2900889facbeSLen Brown 2901889facbeSLen Brown if (!has_epb) 2902889facbeSLen Brown return 0; 2903889facbeSLen Brown 2904889facbeSLen Brown cpu = t->cpu_id; 2905889facbeSLen Brown 2906889facbeSLen Brown /* EPB is per-package */ 2907889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 2908889facbeSLen Brown return 0; 2909889facbeSLen Brown 2910889facbeSLen Brown if (cpu_migrate(cpu)) { 2911b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 2912889facbeSLen Brown return -1; 2913889facbeSLen Brown } 2914889facbeSLen Brown 2915889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 2916889facbeSLen Brown return 0; 2917889facbeSLen Brown 2918e9be7dd6SLen Brown switch (msr & 0xF) { 2919889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 2920889facbeSLen Brown epb_string = "performance"; 2921889facbeSLen Brown break; 2922889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 2923889facbeSLen Brown epb_string = "balanced"; 2924889facbeSLen Brown break; 2925889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 2926889facbeSLen Brown epb_string = "powersave"; 2927889facbeSLen Brown break; 2928889facbeSLen Brown default: 2929889facbeSLen Brown epb_string = "custom"; 2930889facbeSLen Brown break; 2931889facbeSLen Brown } 2932b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 2933889facbeSLen Brown 2934889facbeSLen Brown return 0; 2935889facbeSLen Brown } 29367f5c258eSLen Brown /* 29377f5c258eSLen Brown * print_hwp() 29387f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 29397f5c258eSLen Brown */ 29407f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 29417f5c258eSLen Brown { 29427f5c258eSLen Brown unsigned long long msr; 29437f5c258eSLen Brown int cpu; 29447f5c258eSLen Brown 29457f5c258eSLen Brown if (!has_hwp) 29467f5c258eSLen Brown return 0; 29477f5c258eSLen Brown 29487f5c258eSLen Brown cpu = t->cpu_id; 29497f5c258eSLen Brown 29507f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 29517f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 29527f5c258eSLen Brown return 0; 29537f5c258eSLen Brown 29547f5c258eSLen Brown if (cpu_migrate(cpu)) { 2955b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 29567f5c258eSLen Brown return -1; 29577f5c258eSLen Brown } 29587f5c258eSLen Brown 29597f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 29607f5c258eSLen Brown return 0; 29617f5c258eSLen Brown 2962b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 29637f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 29647f5c258eSLen Brown 29657f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 29667f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 29677f5c258eSLen Brown return 0; 29687f5c258eSLen Brown 29697f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 29707f5c258eSLen Brown return 0; 29717f5c258eSLen Brown 2972b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 29737f5c258eSLen Brown "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n", 29747f5c258eSLen Brown cpu, msr, 29757f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 29767f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 29777f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 29787f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 29797f5c258eSLen Brown 29807f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 29817f5c258eSLen Brown return 0; 29827f5c258eSLen Brown 2983b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 29847f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n", 29857f5c258eSLen Brown cpu, msr, 29867f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 29877f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 29887f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 29897f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 29907f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 29917f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 29927f5c258eSLen Brown 29937f5c258eSLen Brown if (has_hwp_pkg) { 29947f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 29957f5c258eSLen Brown return 0; 29967f5c258eSLen Brown 2997b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 29987f5c258eSLen Brown "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n", 29997f5c258eSLen Brown cpu, msr, 30007f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 30017f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 30027f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 30037f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 30047f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 30057f5c258eSLen Brown } 30067f5c258eSLen Brown if (has_hwp_notify) { 30077f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 30087f5c258eSLen Brown return 0; 30097f5c258eSLen Brown 3010b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 30117f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 30127f5c258eSLen Brown cpu, msr, 30137f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 30147f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 30157f5c258eSLen Brown } 30167f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 30177f5c258eSLen Brown return 0; 30187f5c258eSLen Brown 3019b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 30207f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 30217f5c258eSLen Brown cpu, msr, 30227f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 30237f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 3024889facbeSLen Brown 3025889facbeSLen Brown return 0; 3026889facbeSLen Brown } 3027889facbeSLen Brown 30283a9a941dSLen Brown /* 30293a9a941dSLen Brown * print_perf_limit() 30303a9a941dSLen Brown */ 30313a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 30323a9a941dSLen Brown { 30333a9a941dSLen Brown unsigned long long msr; 30343a9a941dSLen Brown int cpu; 30353a9a941dSLen Brown 30363a9a941dSLen Brown cpu = t->cpu_id; 30373a9a941dSLen Brown 30383a9a941dSLen Brown /* per-package */ 30393a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 30403a9a941dSLen Brown return 0; 30413a9a941dSLen Brown 30423a9a941dSLen Brown if (cpu_migrate(cpu)) { 3043b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 30443a9a941dSLen Brown return -1; 30453a9a941dSLen Brown } 30463a9a941dSLen Brown 30473a9a941dSLen Brown if (do_core_perf_limit_reasons) { 30483a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 3049b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3050b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 3051e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 30523a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 3053e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 3054e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 3055e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 3056e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 3057e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 3058e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 3059e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 3060e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 3061e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 3062e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 3063e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 3064e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 3065b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 3066e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 30673a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 3068e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 3069e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 3070e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 3071e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 3072e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 3073e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 3074e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 3075e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 3076e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 3077e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 3078e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 3079e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 30803a9a941dSLen Brown 30813a9a941dSLen Brown } 30823a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 30833a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 3084b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3085b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 30863a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 30873a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 30883a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 30893a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 30903a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 30913a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 30923a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 30933a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3094b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 30953a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 30963a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 30973a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 30983a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 30993a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 31003a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 31013a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 31023a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 31033a9a941dSLen Brown } 31043a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 31053a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 3106b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3107b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 31083a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 31093a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 31103a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 31113a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 31123a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 31133a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3114b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 31153a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 31163a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 31173a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 31183a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 31193a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 31203a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 31213a9a941dSLen Brown } 31223a9a941dSLen Brown return 0; 31233a9a941dSLen Brown } 31243a9a941dSLen Brown 3125889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 3126889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 3127889facbeSLen Brown 31281b69317dSColin Ian King double get_tdp(unsigned int model) 3129144b44b1SLen Brown { 3130144b44b1SLen Brown unsigned long long msr; 3131144b44b1SLen Brown 3132144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 31337ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 3134144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 3135144b44b1SLen Brown 3136144b44b1SLen Brown switch (model) { 3137869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: 3138869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: 3139144b44b1SLen Brown return 30.0; 3140144b44b1SLen Brown default: 3141144b44b1SLen Brown return 135.0; 3142144b44b1SLen Brown } 3143144b44b1SLen Brown } 3144144b44b1SLen Brown 314540ee8e3bSAndrey Semin /* 314640ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 314740ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 314840ee8e3bSAndrey Semin */ 314940ee8e3bSAndrey Semin static double 315040ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 315140ee8e3bSAndrey Semin { 315240ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 315340ee8e3bSAndrey Semin 315440ee8e3bSAndrey Semin switch (model) { 3155869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3156869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3157869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3158869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3159005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 316040ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 316140ee8e3bSAndrey Semin default: 316240ee8e3bSAndrey Semin return (rapl_energy_units); 316340ee8e3bSAndrey Semin } 316440ee8e3bSAndrey Semin } 316540ee8e3bSAndrey Semin 3166144b44b1SLen Brown 3167889facbeSLen Brown /* 3168889facbeSLen Brown * rapl_probe() 3169889facbeSLen Brown * 3170144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 3171889facbeSLen Brown */ 3172889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 3173889facbeSLen Brown { 3174889facbeSLen Brown unsigned long long msr; 3175144b44b1SLen Brown unsigned int time_unit; 3176889facbeSLen Brown double tdp; 3177889facbeSLen Brown 3178889facbeSLen Brown if (!genuine_intel) 3179889facbeSLen Brown return; 3180889facbeSLen Brown 3181889facbeSLen Brown if (family != 6) 3182889facbeSLen Brown return; 3183889facbeSLen Brown 3184889facbeSLen Brown switch (model) { 3185869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3186869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 3187869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3188869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3189869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3190869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3191869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3192144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 3193812db3f7SLen Brown if (rapl_joules) { 3194812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3195812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3196812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 3197812db3f7SLen Brown } else { 3198812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3199812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3200812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 3201812db3f7SLen Brown } 3202889facbeSLen Brown break; 3203869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3204ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 3205e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 3206812db3f7SLen Brown if (rapl_joules) 3207812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3208812db3f7SLen Brown else 3209812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3210e4085d54SLen Brown break; 3211869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3212869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3213869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3214869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 32150b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 3216812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3217812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3218812db3f7SLen Brown if (rapl_joules) { 3219812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3220812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3221812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3222812db3f7SLen Brown } else { 3223812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3224812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3225812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3226812db3f7SLen Brown } 32270b2bb692SLen Brown break; 3228869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3229869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3230869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3231869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3232869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3233005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 32340b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 3235812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3236812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3237812db3f7SLen Brown if (rapl_joules) { 3238812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3239812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3240812db3f7SLen Brown } else { 3241812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3242812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3243812db3f7SLen Brown } 3244e6f9bb3cSLen Brown break; 3245869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3246869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 32470b2bb692SLen 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; 3248812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3249812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3250812db3f7SLen Brown if (rapl_joules) { 3251812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3252812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3253812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3254812db3f7SLen Brown } else { 3255812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3256812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3257812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3258812db3f7SLen Brown } 3259144b44b1SLen Brown break; 3260869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 3261869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 3262144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 3263812db3f7SLen Brown if (rapl_joules) { 3264812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3265812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3266812db3f7SLen Brown } else { 3267812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3268812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3269812db3f7SLen Brown } 3270889facbeSLen Brown break; 3271869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 32720f644909SJacob 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; 3273812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3274812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3275812db3f7SLen Brown if (rapl_joules) { 3276812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3277812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3278812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3279812db3f7SLen Brown } else { 3280812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3281812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3282812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3283812db3f7SLen Brown } 32840f644909SJacob Pan break; 3285889facbeSLen Brown default: 3286889facbeSLen Brown return; 3287889facbeSLen Brown } 3288889facbeSLen Brown 3289889facbeSLen Brown /* units on package 0, verify later other packages match */ 32907ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 3291889facbeSLen Brown return; 3292889facbeSLen Brown 3293889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 3294869ce69eSLen Brown if (model == INTEL_FAM6_ATOM_SILVERMONT1) 3295144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 3296144b44b1SLen Brown else 3297889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 3298889facbeSLen Brown 329940ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 330040ee8e3bSAndrey Semin 3301144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 3302144b44b1SLen Brown if (time_unit == 0) 3303144b44b1SLen Brown time_unit = 0xA; 3304889facbeSLen Brown 3305144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 3306144b44b1SLen Brown 3307144b44b1SLen Brown tdp = get_tdp(model); 3308889facbeSLen Brown 3309889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 331096e47158SLen Brown if (!quiet) 3311b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 3312889facbeSLen Brown 3313889facbeSLen Brown return; 3314889facbeSLen Brown } 3315889facbeSLen Brown 33161b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 33173a9a941dSLen Brown { 33183a9a941dSLen Brown if (!genuine_intel) 33193a9a941dSLen Brown return; 33203a9a941dSLen Brown 33213a9a941dSLen Brown if (family != 6) 33223a9a941dSLen Brown return; 33233a9a941dSLen Brown 33243a9a941dSLen Brown switch (model) { 3325869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3326869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3327869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 33283a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 3329869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 33303a9a941dSLen Brown do_core_perf_limit_reasons = 1; 33313a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 33323a9a941dSLen Brown default: 33333a9a941dSLen Brown return; 33343a9a941dSLen Brown } 33353a9a941dSLen Brown } 33363a9a941dSLen Brown 3337889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3338889facbeSLen Brown { 3339889facbeSLen Brown unsigned long long msr; 3340889facbeSLen Brown unsigned int dts; 3341889facbeSLen Brown int cpu; 3342889facbeSLen Brown 3343889facbeSLen Brown if (!(do_dts || do_ptm)) 3344889facbeSLen Brown return 0; 3345889facbeSLen Brown 3346889facbeSLen Brown cpu = t->cpu_id; 3347889facbeSLen Brown 3348889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 3349889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 3350889facbeSLen Brown return 0; 3351889facbeSLen Brown 3352889facbeSLen Brown if (cpu_migrate(cpu)) { 3353b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3354889facbeSLen Brown return -1; 3355889facbeSLen Brown } 3356889facbeSLen Brown 3357889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 3358889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 3359889facbeSLen Brown return 0; 3360889facbeSLen Brown 3361889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3362b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 3363889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 3364889facbeSLen Brown 3365889facbeSLen Brown #ifdef THERM_DEBUG 3366889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 3367889facbeSLen Brown return 0; 3368889facbeSLen Brown 3369889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3370889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3371b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3372889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3373889facbeSLen Brown #endif 3374889facbeSLen Brown } 3375889facbeSLen Brown 3376889facbeSLen Brown 3377889facbeSLen Brown if (do_dts) { 3378889facbeSLen Brown unsigned int resolution; 3379889facbeSLen Brown 3380889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 3381889facbeSLen Brown return 0; 3382889facbeSLen Brown 3383889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3384889facbeSLen Brown resolution = (msr >> 27) & 0xF; 3385b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 3386889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 3387889facbeSLen Brown 3388889facbeSLen Brown #ifdef THERM_DEBUG 3389889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 3390889facbeSLen Brown return 0; 3391889facbeSLen Brown 3392889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3393889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3394b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3395889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3396889facbeSLen Brown #endif 3397889facbeSLen Brown } 3398889facbeSLen Brown 3399889facbeSLen Brown return 0; 3400889facbeSLen Brown } 3401889facbeSLen Brown 3402889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 3403889facbeSLen Brown { 3404b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 3405889facbeSLen Brown cpu, label, 3406889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 3407889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 3408889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 3409889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 3410889facbeSLen Brown 3411889facbeSLen Brown return; 3412889facbeSLen Brown } 3413889facbeSLen Brown 3414889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3415889facbeSLen Brown { 3416889facbeSLen Brown unsigned long long msr; 3417889facbeSLen Brown int cpu; 3418889facbeSLen Brown 3419889facbeSLen Brown if (!do_rapl) 3420889facbeSLen Brown return 0; 3421889facbeSLen Brown 3422889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 3423889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3424889facbeSLen Brown return 0; 3425889facbeSLen Brown 3426889facbeSLen Brown cpu = t->cpu_id; 3427889facbeSLen Brown if (cpu_migrate(cpu)) { 3428b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3429889facbeSLen Brown return -1; 3430889facbeSLen Brown } 3431889facbeSLen Brown 3432889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 3433889facbeSLen Brown return -1; 3434889facbeSLen Brown 343596e47158SLen Brown fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr, 3436144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 343796e47158SLen Brown 3438144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 3439144b44b1SLen Brown 3440889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 3441889facbeSLen Brown return -5; 3442889facbeSLen Brown 3443889facbeSLen Brown 3444b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 3445889facbeSLen Brown cpu, msr, 3446889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3447889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3448889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3449889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 3450889facbeSLen Brown 3451144b44b1SLen Brown } 3452144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 3453144b44b1SLen Brown 3454889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 3455889facbeSLen Brown return -9; 3456889facbeSLen Brown 3457b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 3458889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 3459889facbeSLen Brown 3460889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 3461b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 3462889facbeSLen Brown cpu, 3463889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 3464889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 3465889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 3466889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 3467889facbeSLen Brown } 3468889facbeSLen Brown 34690b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 3470889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 3471889facbeSLen Brown return -6; 3472889facbeSLen Brown 3473b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 3474889facbeSLen Brown cpu, msr, 3475889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3476889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3477889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3478889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 34790b2bb692SLen Brown } 34800b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 3481889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 3482889facbeSLen Brown return -9; 3483b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 3484889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3485889facbeSLen Brown 3486889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 3487889facbeSLen Brown } 3488144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 3489889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 3490889facbeSLen Brown return -7; 3491889facbeSLen Brown 3492b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 3493144b44b1SLen Brown } 34949148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 3495889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 3496889facbeSLen Brown return -9; 3497b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 3498889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3499889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 3500889facbeSLen Brown } 3501889facbeSLen Brown if (do_rapl & RAPL_GFX) { 3502889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 3503889facbeSLen Brown return -8; 3504889facbeSLen Brown 3505b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 3506889facbeSLen Brown 3507889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 3508889facbeSLen Brown return -9; 3509b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 3510889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3511889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 3512889facbeSLen Brown } 3513889facbeSLen Brown return 0; 3514889facbeSLen Brown } 3515889facbeSLen Brown 3516d7899447SLen Brown /* 3517d7899447SLen Brown * SNB adds support for additional MSRs: 3518d7899447SLen Brown * 3519d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 3520d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 3521d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 3522d7899447SLen Brown */ 3523103a8feaSLen Brown 3524d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 3525103a8feaSLen Brown { 3526103a8feaSLen Brown if (!genuine_intel) 3527103a8feaSLen Brown return 0; 3528103a8feaSLen Brown 3529103a8feaSLen Brown switch (model) { 3530869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3531869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3532869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3533869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3534869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3535869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 3536869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3537869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3538869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3539869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3540869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3541869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3542869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3543869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3544869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3545869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3546869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3547869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3548ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 35495bbac26eSXiaolong Wang case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 3550103a8feaSLen Brown return 1; 3551103a8feaSLen Brown } 3552103a8feaSLen Brown return 0; 3553103a8feaSLen Brown } 3554103a8feaSLen Brown 3555d7899447SLen Brown /* 3556d7899447SLen Brown * HSW adds support for additional MSRs: 3557d7899447SLen Brown * 3558d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 3559d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 3560d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 35615a63426eSLen Brown * 35625a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 35635a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 35645a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 35655a63426eSLen Brown * 3566d7899447SLen Brown */ 3567d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 3568ca58710fSKristen Carlson Accardi { 3569ca58710fSKristen Carlson Accardi if (!genuine_intel) 3570ca58710fSKristen Carlson Accardi return 0; 3571ca58710fSKristen Carlson Accardi 3572ca58710fSKristen Carlson Accardi switch (model) { 3573869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3574869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3575869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3576869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3577869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3578869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3579869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3580ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 3581ca58710fSKristen Carlson Accardi return 1; 3582ca58710fSKristen Carlson Accardi } 3583ca58710fSKristen Carlson Accardi return 0; 3584ca58710fSKristen Carlson Accardi } 3585ca58710fSKristen Carlson Accardi 35860b2bb692SLen Brown /* 35870b2bb692SLen Brown * SKL adds support for additional MSRS: 35880b2bb692SLen Brown * 35890b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 35900b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 35910b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 35920b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 35930b2bb692SLen Brown */ 35940b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 35950b2bb692SLen Brown { 35960b2bb692SLen Brown if (!genuine_intel) 35970b2bb692SLen Brown return 0; 35980b2bb692SLen Brown 35990b2bb692SLen Brown switch (model) { 3600869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3601869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3602869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3603869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 36040b2bb692SLen Brown return 1; 36050b2bb692SLen Brown } 36060b2bb692SLen Brown return 0; 36070b2bb692SLen Brown } 36080b2bb692SLen Brown 3609144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 3610144b44b1SLen Brown { 3611144b44b1SLen Brown if (!genuine_intel) 3612144b44b1SLen Brown return 0; 3613144b44b1SLen Brown switch (model) { 3614869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 3615869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 3616144b44b1SLen Brown return 1; 3617144b44b1SLen Brown } 3618144b44b1SLen Brown return 0; 3619144b44b1SLen Brown } 3620144b44b1SLen Brown 3621fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 3622fb5d4327SDasaratharaman Chandramouli { 3623fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3624fb5d4327SDasaratharaman Chandramouli return 0; 3625fb5d4327SDasaratharaman Chandramouli switch (model) { 3626869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3627005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3628fb5d4327SDasaratharaman Chandramouli return 1; 3629fb5d4327SDasaratharaman Chandramouli } 3630fb5d4327SDasaratharaman Chandramouli return 0; 3631fb5d4327SDasaratharaman Chandramouli } 3632fb5d4327SDasaratharaman Chandramouli 3633b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 3634b2b34dfeSHubert Chrzaniuk { 3635b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 3636b2b34dfeSHubert Chrzaniuk return 1024; 3637b2b34dfeSHubert Chrzaniuk return 1; 3638b2b34dfeSHubert Chrzaniuk } 3639b2b34dfeSHubert Chrzaniuk 3640144b44b1SLen Brown #define SLM_BCLK_FREQS 5 3641144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 3642144b44b1SLen Brown 3643144b44b1SLen Brown double slm_bclk(void) 3644144b44b1SLen Brown { 3645144b44b1SLen Brown unsigned long long msr = 3; 3646144b44b1SLen Brown unsigned int i; 3647144b44b1SLen Brown double freq; 3648144b44b1SLen Brown 36497ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 3650b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 3651144b44b1SLen Brown 3652144b44b1SLen Brown i = msr & 0xf; 3653144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 3654b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 36550a91e551SColin Ian King i = 3; 3656144b44b1SLen Brown } 3657144b44b1SLen Brown freq = slm_freq_table[i]; 3658144b44b1SLen Brown 365996e47158SLen Brown if (!quiet) 3660b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 3661144b44b1SLen Brown 3662144b44b1SLen Brown return freq; 3663144b44b1SLen Brown } 3664144b44b1SLen Brown 3665103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 3666103a8feaSLen Brown { 3667121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 3668103a8feaSLen Brown return 100.00; 3669144b44b1SLen Brown else if (is_slm(family, model)) 3670144b44b1SLen Brown return slm_bclk(); 3671103a8feaSLen Brown else 3672103a8feaSLen Brown return 133.33; 3673103a8feaSLen Brown } 3674103a8feaSLen Brown 3675889facbeSLen Brown /* 3676889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 3677889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 3678889facbeSLen Brown * This is usually equal to tjMax. 3679889facbeSLen Brown * 3680889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 3681889facbeSLen Brown * but also allow cmdline over-ride with -T. 3682889facbeSLen Brown * 3683889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 3684889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 3685889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 3686889facbeSLen Brown */ 3687889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3688889facbeSLen Brown { 3689889facbeSLen Brown unsigned long long msr; 3690889facbeSLen Brown unsigned int target_c_local; 3691889facbeSLen Brown int cpu; 3692889facbeSLen Brown 3693889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 3694889facbeSLen Brown if (!(do_dts || do_ptm)) 3695889facbeSLen Brown return 0; 3696889facbeSLen Brown 3697889facbeSLen Brown /* this is a per-package concept */ 3698889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3699889facbeSLen Brown return 0; 3700889facbeSLen Brown 3701889facbeSLen Brown cpu = t->cpu_id; 3702889facbeSLen Brown if (cpu_migrate(cpu)) { 3703b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3704889facbeSLen Brown return -1; 3705889facbeSLen Brown } 3706889facbeSLen Brown 3707889facbeSLen Brown if (tcc_activation_temp_override != 0) { 3708889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 3709b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 3710889facbeSLen Brown cpu, tcc_activation_temp); 3711889facbeSLen Brown return 0; 3712889facbeSLen Brown } 3713889facbeSLen Brown 3714889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 3715d7899447SLen Brown if (!do_nhm_platform_info) 3716889facbeSLen Brown goto guess; 3717889facbeSLen Brown 37187ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 3719889facbeSLen Brown goto guess; 3720889facbeSLen Brown 37213482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 3722889facbeSLen Brown 372396e47158SLen Brown if (!quiet) 3724b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 3725889facbeSLen Brown cpu, msr, target_c_local); 3726889facbeSLen Brown 37273482124aSJean Delvare if (!target_c_local) 3728889facbeSLen Brown goto guess; 3729889facbeSLen Brown 3730889facbeSLen Brown tcc_activation_temp = target_c_local; 3731889facbeSLen Brown 3732889facbeSLen Brown return 0; 3733889facbeSLen Brown 3734889facbeSLen Brown guess: 3735889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 3736b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 3737889facbeSLen Brown cpu, tcc_activation_temp); 3738889facbeSLen Brown 3739889facbeSLen Brown return 0; 3740889facbeSLen Brown } 374169807a63SLen Brown 3742aa8d8cc7SLen Brown void decode_feature_control_msr(void) 3743aa8d8cc7SLen Brown { 3744aa8d8cc7SLen Brown unsigned long long msr; 3745aa8d8cc7SLen Brown 3746aa8d8cc7SLen Brown if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr)) 3747aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 3748aa8d8cc7SLen Brown base_cpu, msr, 3749aa8d8cc7SLen Brown msr & FEATURE_CONTROL_LOCKED ? "" : "UN-", 3750aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 3751aa8d8cc7SLen Brown } 3752aa8d8cc7SLen Brown 375369807a63SLen Brown void decode_misc_enable_msr(void) 375469807a63SLen Brown { 375569807a63SLen Brown unsigned long long msr; 375669807a63SLen Brown 375769807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 3758e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 375969807a63SLen Brown base_cpu, msr, 3760e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 3761e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 3762e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "", 3763e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 3764e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 376569807a63SLen Brown } 376669807a63SLen Brown 376733148d67SLen Brown void decode_misc_feature_control(void) 376833148d67SLen Brown { 376933148d67SLen Brown unsigned long long msr; 377033148d67SLen Brown 377133148d67SLen Brown if (!has_misc_feature_control) 377233148d67SLen Brown return; 377333148d67SLen Brown 377433148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 377533148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 377633148d67SLen Brown base_cpu, msr, 377733148d67SLen Brown msr & (0 << 0) ? "No-" : "", 377833148d67SLen Brown msr & (1 << 0) ? "No-" : "", 377933148d67SLen Brown msr & (2 << 0) ? "No-" : "", 378033148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 378133148d67SLen Brown } 3782f0057310SLen Brown /* 3783f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 3784f0057310SLen Brown * 3785f0057310SLen Brown * Decode the bits according to the Nehalem documentation 3786f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 3787f0057310SLen Brown * bit[1] less so... 3788f0057310SLen Brown */ 3789f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 3790f0057310SLen Brown { 3791f0057310SLen Brown unsigned long long msr; 3792f0057310SLen Brown 3793f0057310SLen Brown if (!do_nhm_platform_info) 3794f0057310SLen Brown return; 3795f0057310SLen Brown 3796cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 3797cf4cbe53SLen Brown return; 3798cf4cbe53SLen Brown 3799f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 3800ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 3801f0057310SLen Brown base_cpu, msr, 3802f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 3803ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 3804ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 3805f0057310SLen Brown } 380671616c8eSLen Brown /* 380771616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 380871616c8eSLen Brown * 380971616c8eSLen Brown * This MSRs are present on Silvermont processors, 381071616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 381171616c8eSLen Brown */ 381271616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 381371616c8eSLen Brown { 381471616c8eSLen Brown unsigned long long msr; 381571616c8eSLen Brown 381671616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 381771616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 381871616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 381971616c8eSLen Brown 382071616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 382171616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 382271616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 382371616c8eSLen Brown } 38247f5c258eSLen Brown 3825fcd17211SLen Brown void process_cpuid() 3826103a8feaSLen Brown { 382761a87ba7SLen Brown unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; 3828103a8feaSLen Brown unsigned int fms, family, model, stepping; 3829b3a34e93SLen Brown unsigned int has_turbo; 3830103a8feaSLen Brown 3831103a8feaSLen Brown eax = ebx = ecx = edx = 0; 3832103a8feaSLen Brown 38335aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 3834103a8feaSLen Brown 3835103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 3836103a8feaSLen Brown genuine_intel = 1; 3837103a8feaSLen Brown 383896e47158SLen Brown if (!quiet) 3839b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 3840103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 3841103a8feaSLen Brown 38425aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 3843103a8feaSLen Brown family = (fms >> 8) & 0xf; 3844103a8feaSLen Brown model = (fms >> 4) & 0xf; 3845103a8feaSLen Brown stepping = fms & 0xf; 3846103a8feaSLen Brown if (family == 6 || family == 0xf) 3847103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 3848103a8feaSLen Brown 384996e47158SLen Brown if (!quiet) { 3850b7d8c148SLen Brown fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 3851103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 3852aa8d8cc7SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n", 385369807a63SLen Brown ecx & (1 << 0) ? "SSE3" : "-", 385469807a63SLen Brown ecx & (1 << 3) ? "MONITOR" : "-", 3855aa8d8cc7SLen Brown ecx & (1 << 6) ? "SMX" : "-", 385669807a63SLen Brown ecx & (1 << 7) ? "EIST" : "-", 385769807a63SLen Brown ecx & (1 << 8) ? "TM2" : "-", 385869807a63SLen Brown edx & (1 << 4) ? "TSC" : "-", 385969807a63SLen Brown edx & (1 << 5) ? "MSR" : "-", 386069807a63SLen Brown edx & (1 << 22) ? "ACPI-TM" : "-", 386169807a63SLen Brown edx & (1 << 29) ? "TM" : "-"); 386269807a63SLen Brown } 3863103a8feaSLen Brown 3864b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 3865b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 3866103a8feaSLen Brown 3867103a8feaSLen Brown /* 3868103a8feaSLen Brown * check max extended function levels of CPUID. 3869103a8feaSLen Brown * This is needed to check for invariant TSC. 3870103a8feaSLen Brown * This check is valid for both Intel and AMD. 3871103a8feaSLen Brown */ 3872103a8feaSLen Brown ebx = ecx = edx = 0; 38735aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 3874103a8feaSLen Brown 387561a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 3876103a8feaSLen Brown 3877103a8feaSLen Brown /* 3878103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 3879103a8feaSLen Brown * this check is valid for both Intel and AMD 3880103a8feaSLen Brown */ 38815aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 38828209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 3883d7899447SLen Brown } 3884103a8feaSLen Brown 3885103a8feaSLen Brown /* 3886103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 3887103a8feaSLen Brown * this check is valid for both Intel and AMD 3888103a8feaSLen Brown */ 3889103a8feaSLen Brown 38905aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 38918209e054SThomas Renninger has_aperf = ecx & (1 << 0); 3892812db3f7SLen Brown if (has_aperf) { 3893812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 3894812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 3895812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 3896812db3f7SLen Brown } 3897889facbeSLen Brown do_dts = eax & (1 << 0); 3898812db3f7SLen Brown if (do_dts) 3899812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 3900b3a34e93SLen Brown has_turbo = eax & (1 << 1); 3901889facbeSLen Brown do_ptm = eax & (1 << 6); 3902812db3f7SLen Brown if (do_ptm) 3903812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 39047f5c258eSLen Brown has_hwp = eax & (1 << 7); 39057f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 39067f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 39077f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 39087f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 3909889facbeSLen Brown has_epb = ecx & (1 << 3); 3910889facbeSLen Brown 391196e47158SLen Brown if (!quiet) 3912b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 39137f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 39147f5c258eSLen Brown has_aperf ? "" : "No-", 3915b3a34e93SLen Brown has_turbo ? "" : "No-", 39167f5c258eSLen Brown do_dts ? "" : "No-", 39177f5c258eSLen Brown do_ptm ? "" : "No-", 39187f5c258eSLen Brown has_hwp ? "" : "No-", 39197f5c258eSLen Brown has_hwp_notify ? "" : "No-", 39207f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 39217f5c258eSLen Brown has_hwp_epp ? "" : "No-", 39227f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 39237f5c258eSLen Brown has_epb ? "" : "No-"); 3924103a8feaSLen Brown 392596e47158SLen Brown if (!quiet) 392669807a63SLen Brown decode_misc_enable_msr(); 392769807a63SLen Brown 392833148d67SLen Brown 392996e47158SLen Brown if (max_level >= 0x7 && !quiet) { 3930aa8d8cc7SLen Brown int has_sgx; 3931aa8d8cc7SLen Brown 3932aa8d8cc7SLen Brown ecx = 0; 3933aa8d8cc7SLen Brown 3934aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 3935aa8d8cc7SLen Brown 3936aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 3937aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 3938aa8d8cc7SLen Brown 3939aa8d8cc7SLen Brown if (has_sgx) 3940aa8d8cc7SLen Brown decode_feature_control_msr(); 3941aa8d8cc7SLen Brown } 3942aa8d8cc7SLen Brown 394361a87ba7SLen Brown if (max_level >= 0x15) { 39448a5bdf41SLen Brown unsigned int eax_crystal; 39458a5bdf41SLen Brown unsigned int ebx_tsc; 39468a5bdf41SLen Brown 39478a5bdf41SLen Brown /* 39488a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 39498a5bdf41SLen Brown */ 39508a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 39515aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 39528a5bdf41SLen Brown 39538a5bdf41SLen Brown if (ebx_tsc != 0) { 39548a5bdf41SLen Brown 395596e47158SLen Brown if (!quiet && (ebx != 0)) 3956b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 39578a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 39588a5bdf41SLen Brown 39598a5bdf41SLen Brown if (crystal_hz == 0) 39608a5bdf41SLen Brown switch(model) { 3961869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3962869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3963869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3964869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3965e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 3966e8efbc80SLen Brown break; 3967869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 39687268d407SLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 3969ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 3970ec53e594SLen Brown break; 3971869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3972ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 3973e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 39748a5bdf41SLen Brown break; 39758a5bdf41SLen Brown default: 39768a5bdf41SLen Brown crystal_hz = 0; 39778a5bdf41SLen Brown } 39788a5bdf41SLen Brown 39798a5bdf41SLen Brown if (crystal_hz) { 39808a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 398196e47158SLen Brown if (!quiet) 3982b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 39838a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 39848a5bdf41SLen Brown } 39858a5bdf41SLen Brown } 39868a5bdf41SLen Brown } 398761a87ba7SLen Brown if (max_level >= 0x16) { 398861a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 398961a87ba7SLen Brown 399061a87ba7SLen Brown /* 399161a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 399261a87ba7SLen Brown */ 399361a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 399461a87ba7SLen Brown 39955aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 399696e47158SLen Brown if (!quiet) 3997b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 399861a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 399961a87ba7SLen Brown } 40008a5bdf41SLen Brown 4001b2b34dfeSHubert Chrzaniuk if (has_aperf) 4002b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 4003b2b34dfeSHubert Chrzaniuk 4004812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 4005812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 4006812db3f7SLen Brown 4007812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 4008812db3f7SLen Brown do_nhm_platform_info = 1; 4009812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 4010812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 4011812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 4012812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 4013812db3f7SLen Brown } 4014d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 4015812db3f7SLen Brown 4016812db3f7SLen Brown if (do_snb_cstates) 4017812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 4018812db3f7SLen Brown 40195a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 40200f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 40210f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 40220f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 40230f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 40240f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 40250f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 40260f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 40270f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 40280539ba11SLen Brown if (has_slv_msrs(family, model)) { 40290f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 40300f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 40310f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 40320f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 40330539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 40340539ba11SLen Brown use_c1_residency_msr = 1; 40350539ba11SLen Brown } 40367170a374SLen Brown if (is_dnv(family, model)) { 40377170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 40387170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 40397170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 40407170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 40417170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 40427170a374SLen Brown use_c1_residency_msr = 1; 40437170a374SLen Brown } 404434c76197SLen Brown if (is_skx(family, model)) { 404534c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 404634c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 404734c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 404834c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 404934c76197SLen Brown } 4050ade0ebacSLen Brown if (is_bdx(family, model)) { 4051ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 4052ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 4053ade0ebacSLen Brown } 40540f47c08dSLen Brown if (has_hsw_msrs(family, model)) { 40550f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 40560f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 40570f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 40580f47c08dSLen Brown } 40595a63426eSLen Brown do_irtl_hsw = has_hsw_msrs(family, model); 40600b2bb692SLen Brown do_skl_residency = has_skl_msrs(family, model); 4061144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 4062fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 4063103a8feaSLen Brown 406496e47158SLen Brown if (!quiet) 4065f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 4066f0057310SLen Brown 406796e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 406871616c8eSLen Brown decode_c6_demotion_policy_msr(); 406971616c8eSLen Brown 4070889facbeSLen Brown rapl_probe(family, model); 40713a9a941dSLen Brown perf_limit_reasons_probe(family, model); 4072889facbeSLen Brown 407396e47158SLen Brown if (!quiet) 40741b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 4075fcd17211SLen Brown 407641618e63SLen Brown if (!quiet) 407741618e63SLen Brown dump_sysfs_cstate_config(); 407841618e63SLen Brown 4079a2b7b749SLen Brown if (has_skl_msrs(family, model)) 4080a2b7b749SLen Brown calculate_tsc_tweak(); 4081a2b7b749SLen Brown 4082812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 4083812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 4084fdf676e5SLen Brown 4085812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 4086812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 408727d47356SLen Brown 408896e47158SLen Brown if (!quiet) 408933148d67SLen Brown decode_misc_feature_control(); 409033148d67SLen Brown 4091889facbeSLen Brown return; 4092103a8feaSLen Brown } 4093103a8feaSLen Brown 4094d8af6f5fSLen Brown void help() 4095103a8feaSLen Brown { 4096b7d8c148SLen Brown fprintf(outf, 4097d8af6f5fSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 4098d8af6f5fSLen Brown "\n" 4099d8af6f5fSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 4100d8af6f5fSLen Brown "when COMMAND completes.\n" 4101d8af6f5fSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 4102d8af6f5fSLen Brown "to print statistics, until interrupted.\n" 4103388e9c81SLen Brown "--add add a counter\n" 4104388e9c81SLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 4105*1ef7d21aSLen Brown "--cpu cpu-set limit output to summary plus cpu-set cpu-set\n" 410696e47158SLen Brown "--quiet skip decoding system configuration header\n" 4107d8af6f5fSLen Brown "--interval sec Override default 5-second measurement interval\n" 4108d8af6f5fSLen Brown "--help print this help message\n" 4109b7d8c148SLen Brown "--out file create or truncate \"file\" for all output\n" 4110d8af6f5fSLen Brown "--version print version information\n" 4111d8af6f5fSLen Brown "\n" 4112d8af6f5fSLen Brown "For more help, run \"man turbostat\"\n"); 4113103a8feaSLen Brown } 4114103a8feaSLen Brown 4115103a8feaSLen Brown 4116103a8feaSLen Brown /* 4117103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 4118103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 4119103a8feaSLen Brown */ 4120103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 4121103a8feaSLen Brown { 4122103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 4123103a8feaSLen Brown return 1; 4124103a8feaSLen Brown else 4125103a8feaSLen Brown return 0; 4126103a8feaSLen Brown } 4127103a8feaSLen Brown 4128103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 4129103a8feaSLen Brown { 4130103a8feaSLen Brown return 0; 4131103a8feaSLen Brown } 4132103a8feaSLen Brown 4133c98d5d94SLen Brown void topology_probe() 4134c98d5d94SLen Brown { 4135c98d5d94SLen Brown int i; 4136c98d5d94SLen Brown int max_core_id = 0; 4137c98d5d94SLen Brown int max_package_id = 0; 4138c98d5d94SLen Brown int max_siblings = 0; 4139c98d5d94SLen Brown struct cpu_topology { 4140c98d5d94SLen Brown int core_id; 4141c98d5d94SLen Brown int physical_package_id; 4142c98d5d94SLen Brown } *cpus; 4143c98d5d94SLen Brown 4144c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 4145c98d5d94SLen Brown topo.num_cpus = 0; 4146c98d5d94SLen Brown topo.max_cpu_num = 0; 4147c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 4148c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 4149812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 4150c98d5d94SLen Brown 4151d8af6f5fSLen Brown if (debug > 1) 4152b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 4153c98d5d94SLen Brown 4154c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 4155b2c95d90SJosh Triplett if (cpus == NULL) 4156b2c95d90SJosh Triplett err(1, "calloc cpus"); 4157c98d5d94SLen Brown 4158c98d5d94SLen Brown /* 4159c98d5d94SLen Brown * Allocate and initialize cpu_present_set 4160c98d5d94SLen Brown */ 4161c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4162b2c95d90SJosh Triplett if (cpu_present_set == NULL) 4163b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4164c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4165c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 4166c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 4167c98d5d94SLen Brown 4168c98d5d94SLen Brown /* 4169*1ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 4170*1ef7d21aSLen Brown */ 4171*1ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 4172*1ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 4173*1ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 4174*1ef7d21aSLen Brown err(1, "cpu%d not present", i); 4175*1ef7d21aSLen Brown } 4176*1ef7d21aSLen Brown 4177*1ef7d21aSLen Brown /* 4178c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 4179c98d5d94SLen Brown */ 4180c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4181b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 4182b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4183c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4184c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 4185c98d5d94SLen Brown 4186c98d5d94SLen Brown 4187c98d5d94SLen Brown /* 4188c98d5d94SLen Brown * For online cpus 4189c98d5d94SLen Brown * find max_core_id, max_package_id 4190c98d5d94SLen Brown */ 4191c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 4192c98d5d94SLen Brown int siblings; 4193c98d5d94SLen Brown 4194c98d5d94SLen Brown if (cpu_is_not_present(i)) { 4195d8af6f5fSLen Brown if (debug > 1) 4196b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 4197c98d5d94SLen Brown continue; 4198c98d5d94SLen Brown } 4199c98d5d94SLen Brown cpus[i].core_id = get_core_id(i); 4200c98d5d94SLen Brown if (cpus[i].core_id > max_core_id) 4201c98d5d94SLen Brown max_core_id = cpus[i].core_id; 4202c98d5d94SLen Brown 4203c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 4204c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 4205c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 4206c98d5d94SLen Brown 4207c98d5d94SLen Brown siblings = get_num_ht_siblings(i); 4208c98d5d94SLen Brown if (siblings > max_siblings) 4209c98d5d94SLen Brown max_siblings = siblings; 4210d8af6f5fSLen Brown if (debug > 1) 4211b7d8c148SLen Brown fprintf(outf, "cpu %d pkg %d core %d\n", 4212c98d5d94SLen Brown i, cpus[i].physical_package_id, cpus[i].core_id); 4213c98d5d94SLen Brown } 4214c98d5d94SLen Brown topo.num_cores_per_pkg = max_core_id + 1; 4215d8af6f5fSLen Brown if (debug > 1) 4216b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 4217c98d5d94SLen Brown max_core_id, topo.num_cores_per_pkg); 42180f47c08dSLen Brown if (!summary_only && topo.num_cores_per_pkg > 1) 4219812db3f7SLen Brown BIC_PRESENT(BIC_Core); 4220c98d5d94SLen Brown 4221c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 4222d8af6f5fSLen Brown if (debug > 1) 4223b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 4224c98d5d94SLen Brown max_package_id, topo.num_packages); 42251cc21f7bSLen Brown if (debug && !summary_only && topo.num_packages > 1) 4226812db3f7SLen Brown BIC_PRESENT(BIC_Package); 4227c98d5d94SLen Brown 4228c98d5d94SLen Brown topo.num_threads_per_core = max_siblings; 4229d8af6f5fSLen Brown if (debug > 1) 4230b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 4231c98d5d94SLen Brown 4232c98d5d94SLen Brown free(cpus); 4233c98d5d94SLen Brown } 4234c98d5d94SLen Brown 4235c98d5d94SLen Brown void 4236c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 4237c98d5d94SLen Brown { 4238c98d5d94SLen Brown int i; 4239c98d5d94SLen Brown 4240c98d5d94SLen Brown *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg * 4241678a3bd1SLen Brown topo.num_packages, sizeof(struct thread_data)); 4242c98d5d94SLen Brown if (*t == NULL) 4243c98d5d94SLen Brown goto error; 4244c98d5d94SLen Brown 4245c98d5d94SLen Brown for (i = 0; i < topo.num_threads_per_core * 4246c98d5d94SLen Brown topo.num_cores_per_pkg * topo.num_packages; i++) 4247c98d5d94SLen Brown (*t)[i].cpu_id = -1; 4248c98d5d94SLen Brown 4249c98d5d94SLen Brown *c = calloc(topo.num_cores_per_pkg * topo.num_packages, 4250678a3bd1SLen Brown sizeof(struct core_data)); 4251c98d5d94SLen Brown if (*c == NULL) 4252c98d5d94SLen Brown goto error; 4253c98d5d94SLen Brown 4254c98d5d94SLen Brown for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++) 4255c98d5d94SLen Brown (*c)[i].core_id = -1; 4256c98d5d94SLen Brown 4257678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 4258c98d5d94SLen Brown if (*p == NULL) 4259c98d5d94SLen Brown goto error; 4260c98d5d94SLen Brown 4261c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 4262c98d5d94SLen Brown (*p)[i].package_id = i; 4263c98d5d94SLen Brown 4264c98d5d94SLen Brown return; 4265c98d5d94SLen Brown error: 4266b2c95d90SJosh Triplett err(1, "calloc counters"); 4267c98d5d94SLen Brown } 4268c98d5d94SLen Brown /* 4269c98d5d94SLen Brown * init_counter() 4270c98d5d94SLen Brown * 4271c98d5d94SLen Brown * set cpu_id, core_num, pkg_num 4272c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 4273c98d5d94SLen Brown * 4274c98d5d94SLen Brown * increment topo.num_cores when 1st core in pkg seen 4275c98d5d94SLen Brown */ 4276c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 4277c98d5d94SLen Brown struct pkg_data *pkg_base, int thread_num, int core_num, 4278c98d5d94SLen Brown int pkg_num, int cpu_id) 4279c98d5d94SLen Brown { 4280c98d5d94SLen Brown struct thread_data *t; 4281c98d5d94SLen Brown struct core_data *c; 4282c98d5d94SLen Brown struct pkg_data *p; 4283c98d5d94SLen Brown 4284c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_num, core_num, pkg_num); 4285c98d5d94SLen Brown c = GET_CORE(core_base, core_num, pkg_num); 4286c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_num); 4287c98d5d94SLen Brown 4288c98d5d94SLen Brown t->cpu_id = cpu_id; 4289c98d5d94SLen Brown if (thread_num == 0) { 4290c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 4291c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 4292c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 4293c98d5d94SLen Brown } 4294c98d5d94SLen Brown 4295c98d5d94SLen Brown c->core_id = core_num; 4296c98d5d94SLen Brown p->package_id = pkg_num; 4297c98d5d94SLen Brown } 4298c98d5d94SLen Brown 4299c98d5d94SLen Brown 4300c98d5d94SLen Brown int initialize_counters(int cpu_id) 4301c98d5d94SLen Brown { 4302c98d5d94SLen Brown int my_thread_id, my_core_id, my_package_id; 4303c98d5d94SLen Brown 4304c98d5d94SLen Brown my_package_id = get_physical_package_id(cpu_id); 4305c98d5d94SLen Brown my_core_id = get_core_id(cpu_id); 4306e275b388SDasaratharaman Chandramouli my_thread_id = get_cpu_position_in_core(cpu_id); 4307e275b388SDasaratharaman Chandramouli if (!my_thread_id) 4308c98d5d94SLen Brown topo.num_cores++; 4309c98d5d94SLen Brown 4310c98d5d94SLen Brown init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 4311c98d5d94SLen Brown init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id); 4312c98d5d94SLen Brown return 0; 4313c98d5d94SLen Brown } 4314c98d5d94SLen Brown 4315c98d5d94SLen Brown void allocate_output_buffer() 4316c98d5d94SLen Brown { 43173b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 4318c98d5d94SLen Brown outp = output_buffer; 4319b2c95d90SJosh Triplett if (outp == NULL) 4320b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 4321c98d5d94SLen Brown } 432236229897SLen Brown void allocate_fd_percpu(void) 432336229897SLen Brown { 432401a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 432536229897SLen Brown if (fd_percpu == NULL) 432636229897SLen Brown err(-1, "calloc fd_percpu"); 432736229897SLen Brown } 4328562a2d37SLen Brown void allocate_irq_buffers(void) 4329562a2d37SLen Brown { 4330562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 4331562a2d37SLen Brown if (irq_column_2_cpu == NULL) 4332562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 4333c98d5d94SLen Brown 433401a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 4335562a2d37SLen Brown if (irqs_per_cpu == NULL) 433601a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 4337562a2d37SLen Brown } 4338c98d5d94SLen Brown void setup_all_buffers(void) 4339c98d5d94SLen Brown { 4340c98d5d94SLen Brown topology_probe(); 4341562a2d37SLen Brown allocate_irq_buffers(); 434236229897SLen Brown allocate_fd_percpu(); 4343c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 4344c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 4345c98d5d94SLen Brown allocate_output_buffer(); 4346c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 4347c98d5d94SLen Brown } 43483b4d5c7fSAndy Shevchenko 43497ce7d5deSPrarit Bhargava void set_base_cpu(void) 43507ce7d5deSPrarit Bhargava { 43517ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 43527ce7d5deSPrarit Bhargava if (base_cpu < 0) 43537ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 43547ce7d5deSPrarit Bhargava 43557ce7d5deSPrarit Bhargava if (debug > 1) 4356b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 43577ce7d5deSPrarit Bhargava } 43587ce7d5deSPrarit Bhargava 4359103a8feaSLen Brown void turbostat_init() 4360103a8feaSLen Brown { 43617ce7d5deSPrarit Bhargava setup_all_buffers(); 43627ce7d5deSPrarit Bhargava set_base_cpu(); 4363103a8feaSLen Brown check_dev_msr(); 436498481e79SLen Brown check_permissions(); 4365fcd17211SLen Brown process_cpuid(); 4366103a8feaSLen Brown 4367103a8feaSLen Brown 436896e47158SLen Brown if (!quiet) 43697f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 43707f5c258eSLen Brown 437196e47158SLen Brown if (!quiet) 4372889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 4373889facbeSLen Brown 437496e47158SLen Brown if (!quiet) 43753a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 43763a9a941dSLen Brown 437796e47158SLen Brown if (!quiet) 4378889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 4379889facbeSLen Brown 4380889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 4381889facbeSLen Brown 438296e47158SLen Brown if (!quiet) 4383889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 43845a63426eSLen Brown 438596e47158SLen Brown if (!quiet && do_irtl_snb) 43865a63426eSLen Brown print_irtl(); 4387103a8feaSLen Brown } 4388103a8feaSLen Brown 4389103a8feaSLen Brown int fork_it(char **argv) 4390103a8feaSLen Brown { 4391103a8feaSLen Brown pid_t child_pid; 4392d91bb17cSLen Brown int status; 4393d15cf7c1SLen Brown 4394d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 4395d91bb17cSLen Brown if (status) 4396d91bb17cSLen Brown exit(status); 4397d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 4398d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 4399103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 4400103a8feaSLen Brown 4401103a8feaSLen Brown child_pid = fork(); 4402103a8feaSLen Brown if (!child_pid) { 4403103a8feaSLen Brown /* child */ 4404103a8feaSLen Brown execvp(argv[0], argv); 4405103a8feaSLen Brown } else { 4406103a8feaSLen Brown 4407103a8feaSLen Brown /* parent */ 4408b2c95d90SJosh Triplett if (child_pid == -1) 4409b2c95d90SJosh Triplett err(1, "fork"); 4410103a8feaSLen Brown 4411103a8feaSLen Brown signal(SIGINT, SIG_IGN); 4412103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 4413b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 4414b2c95d90SJosh Triplett err(status, "waitpid"); 4415103a8feaSLen Brown } 4416c98d5d94SLen Brown /* 4417c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 4418c98d5d94SLen Brown * because re-starting is problematic when forking 4419c98d5d94SLen Brown */ 4420c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 4421103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 4422103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 4423ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 4424ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 4425ba3dec99SLen Brown else { 4426c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 4427c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 4428ba3dec99SLen Brown } 4429103a8feaSLen Brown 4430b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 4431b7d8c148SLen Brown 4432b7d8c148SLen Brown flush_output_stderr(); 4433103a8feaSLen Brown 4434d91bb17cSLen Brown return status; 4435103a8feaSLen Brown } 4436103a8feaSLen Brown 44373b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 44383b4d5c7fSAndy Shevchenko { 44393b4d5c7fSAndy Shevchenko int status; 44403b4d5c7fSAndy Shevchenko 44413b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 44423b4d5c7fSAndy Shevchenko if (status) 44433b4d5c7fSAndy Shevchenko return status; 44443b4d5c7fSAndy Shevchenko 44453b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 44463b4d5c7fSAndy Shevchenko if (status) 44473b4d5c7fSAndy Shevchenko return status; 44483b4d5c7fSAndy Shevchenko 4449b7d8c148SLen Brown flush_output_stdout(); 44503b4d5c7fSAndy Shevchenko 44513b4d5c7fSAndy Shevchenko return status; 44523b4d5c7fSAndy Shevchenko } 44533b4d5c7fSAndy Shevchenko 4454d8af6f5fSLen Brown void print_version() { 44550539ba11SLen Brown fprintf(outf, "turbostat version 4.17 10 Jan 2017" 4456d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 4457d8af6f5fSLen Brown } 4458d8af6f5fSLen Brown 4459495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 4460495c7654SLen Brown unsigned int width, enum counter_scope scope, 446141618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 4462388e9c81SLen Brown { 4463388e9c81SLen Brown struct msr_counter *msrp; 4464388e9c81SLen Brown 4465388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 4466388e9c81SLen Brown if (msrp == NULL) { 4467388e9c81SLen Brown perror("calloc"); 4468388e9c81SLen Brown exit(1); 4469388e9c81SLen Brown } 4470388e9c81SLen Brown 4471388e9c81SLen Brown msrp->msr_num = msr_num; 4472388e9c81SLen Brown strncpy(msrp->name, name, NAME_BYTES); 4473495c7654SLen Brown if (path) 4474495c7654SLen Brown strncpy(msrp->path, path, PATH_BYTES); 4475388e9c81SLen Brown msrp->width = width; 4476388e9c81SLen Brown msrp->type = type; 4477388e9c81SLen Brown msrp->format = format; 447841618e63SLen Brown msrp->flags = flags; 4479388e9c81SLen Brown 4480388e9c81SLen Brown switch (scope) { 4481388e9c81SLen Brown 4482388e9c81SLen Brown case SCOPE_CPU: 4483388e9c81SLen Brown msrp->next = sys.tp; 4484388e9c81SLen Brown sys.tp = msrp; 4485678a3bd1SLen Brown sys.added_thread_counters++; 4486678a3bd1SLen Brown if (sys.added_thread_counters > MAX_ADDED_COUNTERS) { 4487678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 4488678a3bd1SLen Brown MAX_ADDED_COUNTERS); 4489678a3bd1SLen Brown exit(-1); 4490678a3bd1SLen Brown } 4491388e9c81SLen Brown break; 4492388e9c81SLen Brown 4493388e9c81SLen Brown case SCOPE_CORE: 4494388e9c81SLen Brown msrp->next = sys.cp; 4495388e9c81SLen Brown sys.cp = msrp; 4496678a3bd1SLen Brown sys.added_core_counters++; 4497678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 4498678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 4499678a3bd1SLen Brown MAX_ADDED_COUNTERS); 4500678a3bd1SLen Brown exit(-1); 4501678a3bd1SLen Brown } 4502388e9c81SLen Brown break; 4503388e9c81SLen Brown 4504388e9c81SLen Brown case SCOPE_PACKAGE: 4505388e9c81SLen Brown msrp->next = sys.pp; 4506388e9c81SLen Brown sys.pp = msrp; 4507678a3bd1SLen Brown sys.added_package_counters++; 4508678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 4509678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 4510678a3bd1SLen Brown MAX_ADDED_COUNTERS); 4511678a3bd1SLen Brown exit(-1); 4512678a3bd1SLen Brown } 4513388e9c81SLen Brown break; 4514388e9c81SLen Brown } 4515388e9c81SLen Brown 4516388e9c81SLen Brown return 0; 4517388e9c81SLen Brown } 4518388e9c81SLen Brown 4519388e9c81SLen Brown void parse_add_command(char *add_command) 4520388e9c81SLen Brown { 4521388e9c81SLen Brown int msr_num = 0; 4522495c7654SLen Brown char *path = NULL; 45230f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 4524388e9c81SLen Brown int width = 64; 4525388e9c81SLen Brown int fail = 0; 4526388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 4527388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 4528388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 4529388e9c81SLen Brown 4530388e9c81SLen Brown while (add_command) { 4531388e9c81SLen Brown 4532388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 4533388e9c81SLen Brown goto next; 4534388e9c81SLen Brown 4535388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 4536388e9c81SLen Brown goto next; 4537388e9c81SLen Brown 4538495c7654SLen Brown if (*add_command == '/') { 4539495c7654SLen Brown path = add_command; 4540495c7654SLen Brown goto next; 4541495c7654SLen Brown } 4542495c7654SLen Brown 4543388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 4544388e9c81SLen Brown if ((width == 32) || (width == 64)) 4545388e9c81SLen Brown goto next; 4546388e9c81SLen Brown width = 64; 4547388e9c81SLen Brown } 4548388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 4549388e9c81SLen Brown scope = SCOPE_CPU; 4550388e9c81SLen Brown goto next; 4551388e9c81SLen Brown } 4552388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 4553388e9c81SLen Brown scope = SCOPE_CORE; 4554388e9c81SLen Brown goto next; 4555388e9c81SLen Brown } 4556388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 4557388e9c81SLen Brown scope = SCOPE_PACKAGE; 4558388e9c81SLen Brown goto next; 4559388e9c81SLen Brown } 4560388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 4561388e9c81SLen Brown type = COUNTER_CYCLES; 4562388e9c81SLen Brown goto next; 4563388e9c81SLen Brown } 4564388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 4565388e9c81SLen Brown type = COUNTER_SECONDS; 4566388e9c81SLen Brown goto next; 4567388e9c81SLen Brown } 456841618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 456941618e63SLen Brown type = COUNTER_USEC; 457041618e63SLen Brown goto next; 457141618e63SLen Brown } 4572388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 4573388e9c81SLen Brown format = FORMAT_RAW; 4574388e9c81SLen Brown goto next; 4575388e9c81SLen Brown } 4576388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 4577388e9c81SLen Brown format = FORMAT_DELTA; 4578388e9c81SLen Brown goto next; 4579388e9c81SLen Brown } 4580388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 4581388e9c81SLen Brown format = FORMAT_PERCENT; 4582388e9c81SLen Brown goto next; 4583388e9c81SLen Brown } 4584388e9c81SLen Brown 4585388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 4586388e9c81SLen Brown char *eos; 4587388e9c81SLen Brown 4588388e9c81SLen Brown eos = strchr(name_buffer, ','); 4589388e9c81SLen Brown if (eos) 4590388e9c81SLen Brown *eos = '\0'; 4591388e9c81SLen Brown goto next; 4592388e9c81SLen Brown } 4593388e9c81SLen Brown 4594388e9c81SLen Brown next: 4595388e9c81SLen Brown add_command = strchr(add_command, ','); 4596495c7654SLen Brown if (add_command) { 4597495c7654SLen Brown *add_command = '\0'; 4598388e9c81SLen Brown add_command++; 4599495c7654SLen Brown } 4600388e9c81SLen Brown 4601388e9c81SLen Brown } 4602495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 4603495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 4604388e9c81SLen Brown fail++; 4605388e9c81SLen Brown } 4606388e9c81SLen Brown 4607388e9c81SLen Brown /* generate default column header */ 4608388e9c81SLen Brown if (*name_buffer == '\0') { 4609388e9c81SLen Brown if (format == FORMAT_RAW) { 4610388e9c81SLen Brown if (width == 32) 4611388e9c81SLen Brown sprintf(name_buffer, "msr%d", msr_num); 4612388e9c81SLen Brown else 4613388e9c81SLen Brown sprintf(name_buffer, "MSR%d", msr_num); 4614388e9c81SLen Brown } else if (format == FORMAT_DELTA) { 4615388e9c81SLen Brown if (width == 32) 4616388e9c81SLen Brown sprintf(name_buffer, "cnt%d", msr_num); 4617388e9c81SLen Brown else 4618388e9c81SLen Brown sprintf(name_buffer, "CNT%d", msr_num); 4619388e9c81SLen Brown } else if (format == FORMAT_PERCENT) { 4620388e9c81SLen Brown if (width == 32) 4621388e9c81SLen Brown sprintf(name_buffer, "msr%d%%", msr_num); 4622388e9c81SLen Brown else 4623388e9c81SLen Brown sprintf(name_buffer, "MSR%d%%", msr_num); 4624388e9c81SLen Brown } 4625388e9c81SLen Brown } 4626388e9c81SLen Brown 462741618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 4628388e9c81SLen Brown fail++; 4629388e9c81SLen Brown 4630388e9c81SLen Brown if (fail) { 4631388e9c81SLen Brown help(); 4632388e9c81SLen Brown exit(1); 4633388e9c81SLen Brown } 4634388e9c81SLen Brown } 463541618e63SLen Brown 463641618e63SLen Brown void probe_sysfs(void) 463741618e63SLen Brown { 463841618e63SLen Brown char path[64]; 463941618e63SLen Brown char name_buf[16]; 464041618e63SLen Brown FILE *input; 464141618e63SLen Brown int state; 464241618e63SLen Brown char *sp; 464341618e63SLen Brown 464441618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 464541618e63SLen Brown return; 464641618e63SLen Brown 464741618e63SLen Brown for (state = 10; state > 0; --state) { 464841618e63SLen Brown 464941618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 465041618e63SLen Brown base_cpu, state); 465141618e63SLen Brown input = fopen(path, "r"); 465241618e63SLen Brown if (input == NULL) 465341618e63SLen Brown continue; 465441618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 465541618e63SLen Brown 465641618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 465741618e63SLen Brown sp = strchr(name_buf, '-'); 465841618e63SLen Brown if (!sp) 465941618e63SLen Brown sp = strchrnul(name_buf, '\n'); 466041618e63SLen Brown *sp = '%'; 466141618e63SLen Brown *(sp + 1) = '\0'; 466241618e63SLen Brown 466341618e63SLen Brown fclose(input); 466441618e63SLen Brown 466541618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 466641618e63SLen Brown 466741618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 466841618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 466941618e63SLen Brown } 467041618e63SLen Brown 467141618e63SLen Brown for (state = 10; state > 0; --state) { 467241618e63SLen Brown 467341618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 467441618e63SLen Brown base_cpu, state); 467541618e63SLen Brown input = fopen(path, "r"); 467641618e63SLen Brown if (input == NULL) 467741618e63SLen Brown continue; 467841618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 467941618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 468041618e63SLen Brown sp = strchr(name_buf, '-'); 468141618e63SLen Brown if (!sp) 468241618e63SLen Brown sp = strchrnul(name_buf, '\n'); 468341618e63SLen Brown *sp = '\0'; 468441618e63SLen Brown fclose(input); 468541618e63SLen Brown 468641618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 468741618e63SLen Brown 468841618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 468941618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 469041618e63SLen Brown } 469141618e63SLen Brown 469241618e63SLen Brown } 469341618e63SLen Brown 4694*1ef7d21aSLen Brown 4695*1ef7d21aSLen Brown /* 4696*1ef7d21aSLen Brown * parse cpuset with following syntax 4697*1ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 4698*1ef7d21aSLen Brown */ 4699*1ef7d21aSLen Brown void parse_cpu_command(char *optarg) 4700*1ef7d21aSLen Brown { 4701*1ef7d21aSLen Brown unsigned int start, end; 4702*1ef7d21aSLen Brown char *next; 4703*1ef7d21aSLen Brown 4704*1ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 4705*1ef7d21aSLen Brown if (cpu_subset == NULL) 4706*1ef7d21aSLen Brown err(3, "CPU_ALLOC"); 4707*1ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 4708*1ef7d21aSLen Brown 4709*1ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 4710*1ef7d21aSLen Brown 4711*1ef7d21aSLen Brown next = optarg; 4712*1ef7d21aSLen Brown 4713*1ef7d21aSLen Brown while (next && *next) { 4714*1ef7d21aSLen Brown 4715*1ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 4716*1ef7d21aSLen Brown goto error; 4717*1ef7d21aSLen Brown 4718*1ef7d21aSLen Brown start = strtoul(next, &next, 10); 4719*1ef7d21aSLen Brown 4720*1ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 4721*1ef7d21aSLen Brown goto error; 4722*1ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 4723*1ef7d21aSLen Brown 4724*1ef7d21aSLen Brown if (*next == '\0') 4725*1ef7d21aSLen Brown break; 4726*1ef7d21aSLen Brown 4727*1ef7d21aSLen Brown if (*next == ',') { 4728*1ef7d21aSLen Brown next += 1; 4729*1ef7d21aSLen Brown continue; 4730*1ef7d21aSLen Brown } 4731*1ef7d21aSLen Brown 4732*1ef7d21aSLen Brown if (*next == '-') { 4733*1ef7d21aSLen Brown next += 1; /* start range */ 4734*1ef7d21aSLen Brown } else if (*next == '.') { 4735*1ef7d21aSLen Brown next += 1; 4736*1ef7d21aSLen Brown if (*next == '.') 4737*1ef7d21aSLen Brown next += 1; /* start range */ 4738*1ef7d21aSLen Brown else 4739*1ef7d21aSLen Brown goto error; 4740*1ef7d21aSLen Brown } 4741*1ef7d21aSLen Brown 4742*1ef7d21aSLen Brown end = strtoul(next, &next, 10); 4743*1ef7d21aSLen Brown if (end <= start) 4744*1ef7d21aSLen Brown goto error; 4745*1ef7d21aSLen Brown 4746*1ef7d21aSLen Brown while (++start <= end) { 4747*1ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 4748*1ef7d21aSLen Brown goto error; 4749*1ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 4750*1ef7d21aSLen Brown } 4751*1ef7d21aSLen Brown 4752*1ef7d21aSLen Brown if (*next == ',') 4753*1ef7d21aSLen Brown next += 1; 4754*1ef7d21aSLen Brown else if (*next != '\0') 4755*1ef7d21aSLen Brown goto error; 4756*1ef7d21aSLen Brown } 4757*1ef7d21aSLen Brown 4758*1ef7d21aSLen Brown return; 4759*1ef7d21aSLen Brown 4760*1ef7d21aSLen Brown error: 4761*1ef7d21aSLen Brown fprintf(stderr, "'--cpu %s' malformed\n", optarg); 4762*1ef7d21aSLen Brown exit(-1); 4763*1ef7d21aSLen Brown } 4764*1ef7d21aSLen Brown 4765812db3f7SLen Brown /* 4766812db3f7SLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 4767812db3f7SLen Brown * SHOW_LIST - show this list of counters, hide the rest 4768812db3f7SLen Brown */ 4769812db3f7SLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 4770812db3f7SLen Brown 4771812db3f7SLen Brown int shown; 4772812db3f7SLen Brown /* 4773812db3f7SLen Brown * parse_show_hide() - process cmdline to set default counter action 4774812db3f7SLen Brown */ 4775812db3f7SLen Brown void parse_show_hide(char *optarg, enum show_hide_mode new_mode) 4776812db3f7SLen Brown { 4777812db3f7SLen Brown /* 4778812db3f7SLen Brown * --show: show only those specified 4779812db3f7SLen Brown * The 1st invocation will clear and replace the enabled mask 4780812db3f7SLen Brown * subsequent invocations can add to it. 4781812db3f7SLen Brown */ 4782812db3f7SLen Brown if (new_mode == SHOW_LIST) { 4783812db3f7SLen Brown if (shown == 0) 4784812db3f7SLen Brown bic_enabled = bic_lookup(optarg); 4785812db3f7SLen Brown else 4786812db3f7SLen Brown bic_enabled |= bic_lookup(optarg); 4787812db3f7SLen Brown shown = 1; 4788812db3f7SLen Brown 4789812db3f7SLen Brown return; 4790812db3f7SLen Brown } 4791812db3f7SLen Brown 4792812db3f7SLen Brown /* 4793812db3f7SLen Brown * --hide: do not show those specified 4794812db3f7SLen Brown * multiple invocations simply clear more bits in enabled mask 4795812db3f7SLen Brown */ 4796812db3f7SLen Brown bic_enabled &= ~bic_lookup(optarg); 479741618e63SLen Brown 4798812db3f7SLen Brown } 4799812db3f7SLen Brown 4800103a8feaSLen Brown void cmdline(int argc, char **argv) 4801103a8feaSLen Brown { 4802103a8feaSLen Brown int opt; 4803d8af6f5fSLen Brown int option_index = 0; 4804d8af6f5fSLen Brown static struct option long_options[] = { 4805388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 4806*1ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 4807d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 480896e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 4809d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 4810d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 4811812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 4812d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 4813b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 4814d8af6f5fSLen Brown {"Package", no_argument, 0, 'p'}, 4815d8af6f5fSLen Brown {"processor", no_argument, 0, 'p'}, 481696e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 4817812db3f7SLen Brown {"show", required_argument, 0, 's'}, 4818d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 4819d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 4820d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 4821d8af6f5fSLen Brown {0, 0, 0, 0 } 4822d8af6f5fSLen Brown }; 4823103a8feaSLen Brown 4824103a8feaSLen Brown progname = argv[0]; 4825103a8feaSLen Brown 482696e47158SLen Brown while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v", 4827d8af6f5fSLen Brown long_options, &option_index)) != -1) { 4828103a8feaSLen Brown switch (opt) { 4829388e9c81SLen Brown case 'a': 4830388e9c81SLen Brown parse_add_command(optarg); 4831388e9c81SLen Brown break; 4832*1ef7d21aSLen Brown case 'c': 4833*1ef7d21aSLen Brown parse_cpu_command(optarg); 4834*1ef7d21aSLen Brown break; 4835d8af6f5fSLen Brown case 'D': 4836d8af6f5fSLen Brown dump_only++; 48378e180f3cSLen Brown break; 4838d8af6f5fSLen Brown case 'd': 4839d8af6f5fSLen Brown debug++; 48402f32edf1SLen Brown break; 4841812db3f7SLen Brown case 'H': 4842812db3f7SLen Brown parse_show_hide(optarg, HIDE_LIST); 4843812db3f7SLen Brown break; 4844d8af6f5fSLen Brown case 'h': 4845d8af6f5fSLen Brown default: 4846d8af6f5fSLen Brown help(); 4847d8af6f5fSLen Brown exit(1); 4848d8af6f5fSLen Brown case 'i': 48492a0609c0SLen Brown { 48502a0609c0SLen Brown double interval = strtod(optarg, NULL); 48512a0609c0SLen Brown 48522a0609c0SLen Brown if (interval < 0.001) { 4853b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 48542a0609c0SLen Brown interval); 48552a0609c0SLen Brown exit(2); 48562a0609c0SLen Brown } 48572a0609c0SLen Brown 48582a0609c0SLen Brown interval_ts.tv_sec = interval; 48592a0609c0SLen Brown interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 48602a0609c0SLen Brown } 4861889facbeSLen Brown break; 48625c56be9aSDirk Brandewie case 'J': 48635c56be9aSDirk Brandewie rapl_joules++; 48645c56be9aSDirk Brandewie break; 4865b7d8c148SLen Brown case 'o': 4866b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 4867b7d8c148SLen Brown break; 4868d8af6f5fSLen Brown case 'P': 4869d8af6f5fSLen Brown show_pkg_only++; 4870d8af6f5fSLen Brown break; 4871d8af6f5fSLen Brown case 'p': 4872d8af6f5fSLen Brown show_core_only++; 4873d8af6f5fSLen Brown break; 487496e47158SLen Brown case 'q': 487596e47158SLen Brown quiet = 1; 487696e47158SLen Brown break; 4877812db3f7SLen Brown case 's': 4878812db3f7SLen Brown parse_show_hide(optarg, SHOW_LIST); 4879812db3f7SLen Brown break; 4880d8af6f5fSLen Brown case 'S': 4881d8af6f5fSLen Brown summary_only++; 4882d8af6f5fSLen Brown break; 4883d8af6f5fSLen Brown case 'T': 4884d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 4885d8af6f5fSLen Brown break; 4886d8af6f5fSLen Brown case 'v': 4887d8af6f5fSLen Brown print_version(); 4888d8af6f5fSLen Brown exit(0); 4889d8af6f5fSLen Brown break; 4890103a8feaSLen Brown } 4891103a8feaSLen Brown } 4892103a8feaSLen Brown } 4893103a8feaSLen Brown 4894103a8feaSLen Brown int main(int argc, char **argv) 4895103a8feaSLen Brown { 4896b7d8c148SLen Brown outf = stderr; 4897b7d8c148SLen Brown 4898103a8feaSLen Brown cmdline(argc, argv); 4899103a8feaSLen Brown 490096e47158SLen Brown if (!quiet) 4901d8af6f5fSLen Brown print_version(); 4902103a8feaSLen Brown 490341618e63SLen Brown probe_sysfs(); 490441618e63SLen Brown 4905103a8feaSLen Brown turbostat_init(); 4906103a8feaSLen Brown 49073b4d5c7fSAndy Shevchenko /* dump counters and exit */ 49083b4d5c7fSAndy Shevchenko if (dump_only) 49093b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 49103b4d5c7fSAndy Shevchenko 4911103a8feaSLen Brown /* 4912103a8feaSLen Brown * if any params left, it must be a command to fork 4913103a8feaSLen Brown */ 4914103a8feaSLen Brown if (argc - optind) 4915103a8feaSLen Brown return fork_it(argv + optind); 4916103a8feaSLen Brown else 4917103a8feaSLen Brown turbostat_loop(); 4918103a8feaSLen Brown 4919103a8feaSLen Brown return 0; 4920103a8feaSLen Brown } 4921