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> 32b9ad8ee0SLen Brown #include <sys/select.h> 33103a8feaSLen Brown #include <sys/resource.h> 34103a8feaSLen Brown #include <fcntl.h> 35103a8feaSLen Brown #include <signal.h> 36103a8feaSLen Brown #include <sys/time.h> 37103a8feaSLen Brown #include <stdlib.h> 38d8af6f5fSLen Brown #include <getopt.h> 39103a8feaSLen Brown #include <dirent.h> 40103a8feaSLen Brown #include <string.h> 41103a8feaSLen Brown #include <ctype.h> 4288c3281fSLen Brown #include <sched.h> 432a0609c0SLen Brown #include <time.h> 442b92865eSJosh Triplett #include <cpuid.h> 4598481e79SLen Brown #include <linux/capability.h> 4698481e79SLen Brown #include <errno.h> 47103a8feaSLen Brown 48103a8feaSLen Brown char *proc_stat = "/proc/stat"; 49b7d8c148SLen Brown FILE *outf; 5036229897SLen Brown int *fd_percpu; 51b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0}; 5247936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0}; 53b9ad8ee0SLen Brown struct timespec one_msec = {0, 1000000}; 54023fe0acSChen Yu unsigned int num_iterations; 55d8af6f5fSLen Brown unsigned int debug; 5696e47158SLen Brown unsigned int quiet; 573f44a5c6SLen Brown unsigned int shown; 580de6c0dfSLen Brown unsigned int sums_need_wide_columns; 59d8af6f5fSLen Brown unsigned int rapl_joules; 60d8af6f5fSLen Brown unsigned int summary_only; 61c8ade361SLen Brown unsigned int list_header_only; 62d8af6f5fSLen Brown unsigned int dump_only; 63103a8feaSLen Brown unsigned int do_snb_cstates; 64fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 65144b44b1SLen Brown unsigned int do_slm_cstates; 66997e5395SSrinivas Pandruvada unsigned int do_cnl_cstates; 67144b44b1SLen Brown unsigned int use_c1_residency_msr; 68103a8feaSLen Brown unsigned int has_aperf; 69889facbeSLen Brown unsigned int has_epb; 705a63426eSLen Brown unsigned int do_irtl_snb; 715a63426eSLen Brown unsigned int do_irtl_hsw; 72fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 73103a8feaSLen Brown unsigned int genuine_intel; 74103a8feaSLen Brown unsigned int has_invariant_tsc; 75d7899447SLen Brown unsigned int do_nhm_platform_info; 76cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 77b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 78103a8feaSLen Brown double bclk; 79a2b7b749SLen Brown double base_hz; 8021ed5574SLen Brown unsigned int has_base_hz; 81a2b7b749SLen Brown double tsc_tweak = 1.0; 82c98d5d94SLen Brown unsigned int show_pkg_only; 83c98d5d94SLen Brown unsigned int show_core_only; 84c98d5d94SLen Brown char *output_buffer, *outp; 85889facbeSLen Brown unsigned int do_rapl; 86889facbeSLen Brown unsigned int do_dts; 87889facbeSLen Brown unsigned int do_ptm; 88fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 89be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 90be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 9127d47356SLen Brown unsigned int gfx_cur_mhz; 92889facbeSLen Brown unsigned int tcc_activation_temp; 93889facbeSLen Brown unsigned int tcc_activation_temp_override; 9440ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 9540ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 96889facbeSLen Brown double rapl_joule_counter_range; 973a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 98ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 993a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 1003a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 1018a5bdf41SLen Brown unsigned int crystal_hz; 1028a5bdf41SLen Brown unsigned long long tsc_hz; 1037ce7d5deSPrarit Bhargava int base_cpu; 10421ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 1057f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 1067f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 1077f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 1087f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 1097f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 1107f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 11133148d67SLen Brown unsigned int has_misc_feature_control; 112889facbeSLen Brown 113889facbeSLen Brown #define RAPL_PKG (1 << 0) 114e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 115e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 116e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 117e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 118e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 119e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 120e6f9bb3cSLen Brown 121889facbeSLen Brown #define RAPL_DRAM (1 << 3) 122e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 123e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 124e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 125e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1260b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1270b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 128e6f9bb3cSLen Brown 1299148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 130e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1310b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 132e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 133e6f9bb3cSLen Brown 1340b2bb692SLen Brown #define RAPL_GFX (1 << 8) 135e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 136e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 137e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1389148494cSJacob Pan 1399148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1409148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1419148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 142889facbeSLen Brown #define TJMAX_DEFAULT 100 143889facbeSLen Brown 144889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 145103a8feaSLen Brown 146388e9c81SLen Brown /* 147388e9c81SLen Brown * buffer size used by sscanf() for added column names 148388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 149388e9c81SLen Brown */ 150388e9c81SLen Brown #define NAME_BYTES 20 151495c7654SLen Brown #define PATH_BYTES 128 152388e9c81SLen Brown 153103a8feaSLen Brown int backwards_count; 154103a8feaSLen Brown char *progname; 155103a8feaSLen Brown 1561ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1571ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1581ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1590748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1600748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1610e2d8f05SLen Brown #define BITMASK_SIZE 32 162c98d5d94SLen Brown 163c98d5d94SLen Brown struct thread_data { 164f4fdf2b4SLen Brown struct timeval tv_begin; 165f4fdf2b4SLen Brown struct timeval tv_end; 166c98d5d94SLen Brown unsigned long long tsc; 167c98d5d94SLen Brown unsigned long long aperf; 168c98d5d94SLen Brown unsigned long long mperf; 169144b44b1SLen Brown unsigned long long c1; 1700de6c0dfSLen Brown unsigned long long irq_count; 1711ed51011SLen Brown unsigned int smi_count; 172c98d5d94SLen Brown unsigned int cpu_id; 173c98d5d94SLen Brown unsigned int flags; 174c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 175c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1760748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 177c98d5d94SLen Brown } *thread_even, *thread_odd; 178c98d5d94SLen Brown 179c98d5d94SLen Brown struct core_data { 180c98d5d94SLen Brown unsigned long long c3; 181c98d5d94SLen Brown unsigned long long c6; 182c98d5d94SLen Brown unsigned long long c7; 1830539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 184889facbeSLen Brown unsigned int core_temp_c; 185c98d5d94SLen Brown unsigned int core_id; 186678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 187c98d5d94SLen Brown } *core_even, *core_odd; 188c98d5d94SLen Brown 189c98d5d94SLen Brown struct pkg_data { 190c98d5d94SLen Brown unsigned long long pc2; 191c98d5d94SLen Brown unsigned long long pc3; 192c98d5d94SLen Brown unsigned long long pc6; 193c98d5d94SLen Brown unsigned long long pc7; 194ca58710fSKristen Carlson Accardi unsigned long long pc8; 195ca58710fSKristen Carlson Accardi unsigned long long pc9; 196ca58710fSKristen Carlson Accardi unsigned long long pc10; 197be0e54c4SLen Brown unsigned long long cpu_lpi; 198be0e54c4SLen Brown unsigned long long sys_lpi; 1990b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2000b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2010b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2020b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2039185e988SLen Brown long long gfx_rc6_ms; 20427d47356SLen Brown unsigned int gfx_mhz; 205c98d5d94SLen Brown unsigned int package_id; 206889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 207889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 208889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 209889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 210889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 211889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 212889facbeSLen Brown unsigned int pkg_temp_c; 213678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 214c98d5d94SLen Brown } *package_even, *package_odd; 215c98d5d94SLen Brown 216c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 217c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 218c98d5d94SLen Brown 219c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \ 220*70a9c6e8SPrarit Bhargava (thread_base + (pkg_no) * topo.cores_per_node * \ 221*70a9c6e8SPrarit Bhargava topo.threads_per_core + \ 222*70a9c6e8SPrarit Bhargava (core_no) * topo.threads_per_core + (thread_no)) 223c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \ 224*70a9c6e8SPrarit Bhargava (core_base + (pkg_no) * topo.cores_per_node + (core_no)) 225c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 226c98d5d94SLen Brown 227388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE}; 22841618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC}; 229388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT}; 230388e9c81SLen Brown 231388e9c81SLen Brown struct msr_counter { 232388e9c81SLen Brown unsigned int msr_num; 233388e9c81SLen Brown char name[NAME_BYTES]; 234495c7654SLen Brown char path[PATH_BYTES]; 235388e9c81SLen Brown unsigned int width; 236388e9c81SLen Brown enum counter_type type; 237388e9c81SLen Brown enum counter_format format; 238388e9c81SLen Brown struct msr_counter *next; 239812db3f7SLen Brown unsigned int flags; 240812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 241812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 24241618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 243388e9c81SLen Brown }; 244388e9c81SLen Brown 245388e9c81SLen Brown struct sys_counters { 246678a3bd1SLen Brown unsigned int added_thread_counters; 247678a3bd1SLen Brown unsigned int added_core_counters; 248678a3bd1SLen Brown unsigned int added_package_counters; 249388e9c81SLen Brown struct msr_counter *tp; 250388e9c81SLen Brown struct msr_counter *cp; 251388e9c81SLen Brown struct msr_counter *pp; 252388e9c81SLen Brown } sys; 253388e9c81SLen Brown 254c98d5d94SLen Brown struct system_summary { 255c98d5d94SLen Brown struct thread_data threads; 256c98d5d94SLen Brown struct core_data cores; 257c98d5d94SLen Brown struct pkg_data packages; 258388e9c81SLen Brown } average; 259c98d5d94SLen Brown 2600e2d8f05SLen Brown struct cpu_topology { 2610e2d8f05SLen Brown int physical_package_id; 2620e2d8f05SLen Brown int logical_cpu_id; 263ef605741SPrarit Bhargava int physical_node_id; 264ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 2650e2d8f05SLen Brown int physical_core_id; 2668cb48b32SPrarit Bhargava int thread_id; 2670e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 2680e2d8f05SLen Brown } *cpus; 269c98d5d94SLen Brown 270c98d5d94SLen Brown struct topo_params { 271c98d5d94SLen Brown int num_packages; 272103a8feaSLen Brown int num_cpus; 273c98d5d94SLen Brown int num_cores; 274c98d5d94SLen Brown int max_cpu_num; 275ef605741SPrarit Bhargava int max_node_num; 276*70a9c6e8SPrarit Bhargava int nodes_per_pkg; 277*70a9c6e8SPrarit Bhargava int cores_per_node; 278*70a9c6e8SPrarit Bhargava int threads_per_core; 279c98d5d94SLen Brown } topo; 280103a8feaSLen Brown 281c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 282103a8feaSLen Brown 283562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 284562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 285562a2d37SLen Brown 286c98d5d94SLen Brown void setup_all_buffers(void); 287103a8feaSLen Brown 288c98d5d94SLen Brown int cpu_is_not_present(int cpu) 289d15cf7c1SLen Brown { 290c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 291c98d5d94SLen Brown } 292c98d5d94SLen Brown /* 293c98d5d94SLen Brown * run func(thread, core, package) in topology order 294c98d5d94SLen Brown * skip non-present cpus 295c98d5d94SLen Brown */ 296c98d5d94SLen Brown 297c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 298c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 299c98d5d94SLen Brown { 300c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 301c98d5d94SLen Brown 302c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 303*70a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 304c98d5d94SLen Brown for (thread_no = 0; thread_no < 305*70a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 306c98d5d94SLen Brown struct thread_data *t; 307c98d5d94SLen Brown struct core_data *c; 308c98d5d94SLen Brown struct pkg_data *p; 309c98d5d94SLen Brown 310c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 311c98d5d94SLen Brown 312c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 313c98d5d94SLen Brown continue; 314c98d5d94SLen Brown 315c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 316c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 317c98d5d94SLen Brown 318c98d5d94SLen Brown retval = func(t, c, p); 319c98d5d94SLen Brown if (retval) 320c98d5d94SLen Brown return retval; 321c98d5d94SLen Brown } 322c98d5d94SLen Brown } 323c98d5d94SLen Brown } 324d15cf7c1SLen Brown return 0; 325d15cf7c1SLen Brown } 326d15cf7c1SLen Brown 32788c3281fSLen Brown int cpu_migrate(int cpu) 32888c3281fSLen Brown { 329c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 330c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 331c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 33288c3281fSLen Brown return -1; 33388c3281fSLen Brown else 33488c3281fSLen Brown return 0; 33588c3281fSLen Brown } 33636229897SLen Brown int get_msr_fd(int cpu) 337103a8feaSLen Brown { 338103a8feaSLen Brown char pathname[32]; 339103a8feaSLen Brown int fd; 340103a8feaSLen Brown 34136229897SLen Brown fd = fd_percpu[cpu]; 34236229897SLen Brown 34336229897SLen Brown if (fd) 34436229897SLen Brown return fd; 34536229897SLen Brown 346103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 347103a8feaSLen Brown fd = open(pathname, O_RDONLY); 34815aaa346SLen Brown if (fd < 0) 34998481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 350103a8feaSLen Brown 35136229897SLen Brown fd_percpu[cpu] = fd; 35236229897SLen Brown 35336229897SLen Brown return fd; 35436229897SLen Brown } 35536229897SLen Brown 35636229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 35736229897SLen Brown { 35836229897SLen Brown ssize_t retval; 35936229897SLen Brown 36036229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 36115aaa346SLen Brown 36298481e79SLen Brown if (retval != sizeof *msr) 363cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 36415aaa346SLen Brown 36515aaa346SLen Brown return 0; 366103a8feaSLen Brown } 367103a8feaSLen Brown 368fc04cc67SLen Brown /* 369812db3f7SLen Brown * Each string in this array is compared in --show and --hide cmdline. 370812db3f7SLen Brown * Thus, strings that are proper sub-sets must follow their more specific peers. 371fc04cc67SLen Brown */ 372812db3f7SLen Brown struct msr_counter bic[] = { 3733f44a5c6SLen Brown { 0x0, "usec" }, 3743f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 375812db3f7SLen Brown { 0x0, "Package" }, 376812db3f7SLen Brown { 0x0, "Avg_MHz" }, 377812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 378812db3f7SLen Brown { 0x0, "TSC_MHz" }, 379812db3f7SLen Brown { 0x0, "IRQ" }, 380495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 381812db3f7SLen Brown { 0x0, "Busy%" }, 382812db3f7SLen Brown { 0x0, "CPU%c1" }, 383812db3f7SLen Brown { 0x0, "CPU%c3" }, 384812db3f7SLen Brown { 0x0, "CPU%c6" }, 385812db3f7SLen Brown { 0x0, "CPU%c7" }, 386812db3f7SLen Brown { 0x0, "ThreadC" }, 387812db3f7SLen Brown { 0x0, "CoreTmp" }, 388812db3f7SLen Brown { 0x0, "CoreCnt" }, 389812db3f7SLen Brown { 0x0, "PkgTmp" }, 390812db3f7SLen Brown { 0x0, "GFX%rc6" }, 391812db3f7SLen Brown { 0x0, "GFXMHz" }, 392812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 393812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 394812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 395812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 3960f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 3970f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 3984bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 399be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 400be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 401812db3f7SLen Brown { 0x0, "PkgWatt" }, 402812db3f7SLen Brown { 0x0, "CorWatt" }, 403812db3f7SLen Brown { 0x0, "GFXWatt" }, 404812db3f7SLen Brown { 0x0, "PkgCnt" }, 405812db3f7SLen Brown { 0x0, "RAMWatt" }, 406812db3f7SLen Brown { 0x0, "PKG_%" }, 407812db3f7SLen Brown { 0x0, "RAM_%" }, 408812db3f7SLen Brown { 0x0, "Pkg_J" }, 409812db3f7SLen Brown { 0x0, "Cor_J" }, 410812db3f7SLen Brown { 0x0, "GFX_J" }, 411812db3f7SLen Brown { 0x0, "RAM_J" }, 412812db3f7SLen Brown { 0x0, "Core" }, 413812db3f7SLen Brown { 0x0, "CPU" }, 4140539ba11SLen Brown { 0x0, "Mod%c6" }, 41541618e63SLen Brown { 0x0, "sysfs" }, 416a99d8730SLen Brown { 0x0, "Totl%C0" }, 417a99d8730SLen Brown { 0x0, "Any%C0" }, 418a99d8730SLen Brown { 0x0, "GFX%C0" }, 419a99d8730SLen Brown { 0x0, "CPUGFX%" }, 420be0e54c4SLen Brown { 0x0, "Node%" }, 421812db3f7SLen Brown }; 422812db3f7SLen Brown 423a99d8730SLen Brown 424a99d8730SLen Brown 425812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 4263f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 4273f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 4283f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 4293f44a5c6SLen Brown #define BIC_Avg_MHz (1ULL << 3) 4303f44a5c6SLen Brown #define BIC_Bzy_MHz (1ULL << 4) 4313f44a5c6SLen Brown #define BIC_TSC_MHz (1ULL << 5) 4323f44a5c6SLen Brown #define BIC_IRQ (1ULL << 6) 4333f44a5c6SLen Brown #define BIC_SMI (1ULL << 7) 4343f44a5c6SLen Brown #define BIC_Busy (1ULL << 8) 4353f44a5c6SLen Brown #define BIC_CPU_c1 (1ULL << 9) 4363f44a5c6SLen Brown #define BIC_CPU_c3 (1ULL << 10) 4373f44a5c6SLen Brown #define BIC_CPU_c6 (1ULL << 11) 4383f44a5c6SLen Brown #define BIC_CPU_c7 (1ULL << 12) 4393f44a5c6SLen Brown #define BIC_ThreadC (1ULL << 13) 4403f44a5c6SLen Brown #define BIC_CoreTmp (1ULL << 14) 4413f44a5c6SLen Brown #define BIC_CoreCnt (1ULL << 15) 4423f44a5c6SLen Brown #define BIC_PkgTmp (1ULL << 16) 4433f44a5c6SLen Brown #define BIC_GFX_rc6 (1ULL << 17) 4443f44a5c6SLen Brown #define BIC_GFXMHz (1ULL << 18) 4453f44a5c6SLen Brown #define BIC_Pkgpc2 (1ULL << 19) 4463f44a5c6SLen Brown #define BIC_Pkgpc3 (1ULL << 20) 4473f44a5c6SLen Brown #define BIC_Pkgpc6 (1ULL << 21) 4483f44a5c6SLen Brown #define BIC_Pkgpc7 (1ULL << 22) 4493f44a5c6SLen Brown #define BIC_Pkgpc8 (1ULL << 23) 4503f44a5c6SLen Brown #define BIC_Pkgpc9 (1ULL << 24) 4513f44a5c6SLen Brown #define BIC_Pkgpc10 (1ULL << 25) 452be0e54c4SLen Brown #define BIC_CPU_LPI (1ULL << 26) 453be0e54c4SLen Brown #define BIC_SYS_LPI (1ULL << 27) 4543f44a5c6SLen Brown #define BIC_PkgWatt (1ULL << 26) 4553f44a5c6SLen Brown #define BIC_CorWatt (1ULL << 27) 4563f44a5c6SLen Brown #define BIC_GFXWatt (1ULL << 28) 4573f44a5c6SLen Brown #define BIC_PkgCnt (1ULL << 29) 4583f44a5c6SLen Brown #define BIC_RAMWatt (1ULL << 30) 4593f44a5c6SLen Brown #define BIC_PKG__ (1ULL << 31) 4603f44a5c6SLen Brown #define BIC_RAM__ (1ULL << 32) 4613f44a5c6SLen Brown #define BIC_Pkg_J (1ULL << 33) 4623f44a5c6SLen Brown #define BIC_Cor_J (1ULL << 34) 4633f44a5c6SLen Brown #define BIC_GFX_J (1ULL << 35) 4643f44a5c6SLen Brown #define BIC_RAM_J (1ULL << 36) 4653f44a5c6SLen Brown #define BIC_Core (1ULL << 37) 4663f44a5c6SLen Brown #define BIC_CPU (1ULL << 38) 4673f44a5c6SLen Brown #define BIC_Mod_c6 (1ULL << 39) 4683f44a5c6SLen Brown #define BIC_sysfs (1ULL << 40) 4693f44a5c6SLen Brown #define BIC_Totl_c0 (1ULL << 41) 4703f44a5c6SLen Brown #define BIC_Any_c0 (1ULL << 42) 4713f44a5c6SLen Brown #define BIC_GFX_c0 (1ULL << 43) 4723f44a5c6SLen Brown #define BIC_CPUGFX (1ULL << 44) 473812db3f7SLen Brown 4743f44a5c6SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD) 4753f44a5c6SLen Brown 4763f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 4773f44a5c6SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs; 478812db3f7SLen Brown 479812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 4803f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 481812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 4820f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 483812db3f7SLen Brown 4843f44a5c6SLen Brown 485dd778a5eSLen Brown #define MAX_DEFERRED 16 486dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 487dd778a5eSLen Brown int deferred_skip_index; 488dd778a5eSLen Brown 489dd778a5eSLen Brown /* 490dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 491dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 492dd778a5eSLen Brown */ 493dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 494dd778a5eSLen Brown 495dd778a5eSLen Brown void help(void) 496dd778a5eSLen Brown { 497dd778a5eSLen Brown fprintf(outf, 498dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 499dd778a5eSLen Brown "\n" 500dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 501dd778a5eSLen Brown "when COMMAND completes.\n" 502dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 503dd778a5eSLen Brown "to print statistics, until interrupted.\n" 504dd778a5eSLen Brown "--add add a counter\n" 505dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 506dd778a5eSLen Brown "--cpu cpu-set limit output to summary plus cpu-set:\n" 507dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 508dd778a5eSLen Brown "--quiet skip decoding system configuration header\n" 509b9ad8ee0SLen Brown "--interval sec.subsec Override default 5-second measurement interval\n" 510dd778a5eSLen Brown "--help print this help message\n" 511dd778a5eSLen Brown "--list list column headers only\n" 512023fe0acSChen Yu "--num_iterations num number of the measurement iterations\n" 513dd778a5eSLen Brown "--out file create or truncate \"file\" for all output\n" 514dd778a5eSLen Brown "--version print version information\n" 515dd778a5eSLen Brown "\n" 516dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 517dd778a5eSLen Brown } 518dd778a5eSLen Brown 519812db3f7SLen Brown /* 520812db3f7SLen Brown * bic_lookup 521812db3f7SLen Brown * for all the strings in comma separate name_list, 522812db3f7SLen Brown * set the approprate bit in return value. 523812db3f7SLen Brown */ 524dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 525812db3f7SLen Brown { 526812db3f7SLen Brown int i; 527812db3f7SLen Brown unsigned long long retval = 0; 528812db3f7SLen Brown 529812db3f7SLen Brown while (name_list) { 530812db3f7SLen Brown char *comma; 531812db3f7SLen Brown 532812db3f7SLen Brown comma = strchr(name_list, ','); 533812db3f7SLen Brown 534812db3f7SLen Brown if (comma) 535812db3f7SLen Brown *comma = '\0'; 536812db3f7SLen Brown 5373f44a5c6SLen Brown if (!strcmp(name_list, "all")) 5383f44a5c6SLen Brown return ~0; 5393f44a5c6SLen Brown 540812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 541812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 542812db3f7SLen Brown retval |= (1ULL << i); 543812db3f7SLen Brown break; 544812db3f7SLen Brown } 545812db3f7SLen Brown } 546812db3f7SLen Brown if (i == MAX_BIC) { 547dd778a5eSLen Brown if (mode == SHOW_LIST) { 548812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 549812db3f7SLen Brown exit(-1); 550812db3f7SLen Brown } 551dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 552dd778a5eSLen Brown if (debug) 553dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 554dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 555dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 556dd778a5eSLen Brown MAX_DEFERRED, name_list); 557dd778a5eSLen Brown help(); 558dd778a5eSLen Brown exit(1); 559dd778a5eSLen Brown } 560dd778a5eSLen Brown } 561812db3f7SLen Brown 562812db3f7SLen Brown name_list = comma; 563812db3f7SLen Brown if (name_list) 564812db3f7SLen Brown name_list++; 565812db3f7SLen Brown 566812db3f7SLen Brown } 567812db3f7SLen Brown return retval; 568812db3f7SLen Brown } 569fc04cc67SLen Brown 570dd778a5eSLen Brown 571c8ade361SLen Brown void print_header(char *delim) 572103a8feaSLen Brown { 573388e9c81SLen Brown struct msr_counter *mp; 5746168c2e0SLen Brown int printed = 0; 575388e9c81SLen Brown 5763f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 5773f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 5783f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 5793f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 580812db3f7SLen Brown if (DO_BIC(BIC_Package)) 5816168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 582812db3f7SLen Brown if (DO_BIC(BIC_Core)) 5836168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 584812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 5856168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 586812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 5876168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 588812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 5896168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 590812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 5916168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 592812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 5936168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 5941cc21f7bSLen Brown 5950de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 5960de6c0dfSLen Brown if (sums_need_wide_columns) 5976168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 5980de6c0dfSLen Brown else 5996168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 6000de6c0dfSLen Brown } 6010de6c0dfSLen Brown 602812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 6036168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 6041cc21f7bSLen Brown 605388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 606dd778a5eSLen Brown 607388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 608388e9c81SLen Brown if (mp->width == 64) 609dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 610388e9c81SLen Brown else 611dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 612388e9c81SLen Brown } else { 6130de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 614dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 6150de6c0dfSLen Brown else 616dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 617388e9c81SLen Brown } 618388e9c81SLen Brown } 619388e9c81SLen Brown 62041618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 6216168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 622997e5395SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) 6236168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 624812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 6256168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 626812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 6276168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 628678a3bd1SLen Brown 6290539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 6306168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 631678a3bd1SLen Brown 632812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 6336168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 634388e9c81SLen Brown 635388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 636388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 637388e9c81SLen Brown if (mp->width == 64) 638c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 639388e9c81SLen Brown else 640c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 641388e9c81SLen Brown } else { 6420de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 6430de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 6440de6c0dfSLen Brown else 645c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 646388e9c81SLen Brown } 647388e9c81SLen Brown } 648388e9c81SLen Brown 649812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 6506168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 651889facbeSLen Brown 652812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 6536168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 654fdf676e5SLen Brown 655812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 6566168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 65727d47356SLen Brown 658a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 6596168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 660a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 6616168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 662a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 6636168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 664a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 6656168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 6660b2bb692SLen Brown 6670f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 6686168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 6690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 6706168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 6710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 6726168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 6730f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 6746168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 6750f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 6766168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 6770f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 6786168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 6790f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 6806168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 681be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 682be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 683be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 684be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 685103a8feaSLen Brown 6865c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 687812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 6886168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 689812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 6906168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 691812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 6926168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 693812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 6946168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 695812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 6966168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 697812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 6986168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 699d7899447SLen Brown } else if (do_rapl && rapl_joules) { 700812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 7016168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 702812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 7036168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 704812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 7056168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 706812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 7076168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 708812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 7096168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 710812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 7116168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 7125c56be9aSDirk Brandewie } 713388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 714388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 715388e9c81SLen Brown if (mp->width == 64) 716c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 717388e9c81SLen Brown else 718c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 719388e9c81SLen Brown } else { 7200de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 7210de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 7220de6c0dfSLen Brown else 723c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 724388e9c81SLen Brown } 725388e9c81SLen Brown } 726388e9c81SLen Brown 727c98d5d94SLen Brown outp += sprintf(outp, "\n"); 728103a8feaSLen Brown } 729103a8feaSLen Brown 730c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 731c98d5d94SLen Brown struct pkg_data *p) 732103a8feaSLen Brown { 733388e9c81SLen Brown int i; 734388e9c81SLen Brown struct msr_counter *mp; 735388e9c81SLen Brown 7363b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 737c98d5d94SLen Brown 738c98d5d94SLen Brown if (t) { 7393b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 7403b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 7413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 7423b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 7433b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 7443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 7456886fee4SLen Brown 746812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 7470de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 748812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 749218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 750388e9c81SLen Brown 751388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 752388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 753388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 754388e9c81SLen Brown } 755103a8feaSLen Brown } 756103a8feaSLen Brown 757c98d5d94SLen Brown if (c) { 7583b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 7593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 7603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 7613b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 7623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 763388e9c81SLen Brown 764388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 765388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 766388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 767388e9c81SLen Brown } 7680539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 769c98d5d94SLen Brown } 770103a8feaSLen Brown 771c98d5d94SLen Brown if (p) { 7723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 7730b2bb692SLen Brown 7740b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 7750b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 7760b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 7770b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 7780b2bb692SLen Brown 7793b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 7800f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 7813b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 7820f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 7833b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 7840f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 7853b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 7863b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 7873b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 7883b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 789be0e54c4SLen Brown outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 790be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 791be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 7923b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 7933b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 7943b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 7953b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 7963b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 7973b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 7983b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 7993b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 8003b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 801388e9c81SLen Brown 802388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 803388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 804388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 805388e9c81SLen Brown } 806c98d5d94SLen Brown } 8073b4d5c7fSAndy Shevchenko 8083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 8093b4d5c7fSAndy Shevchenko 810c98d5d94SLen Brown return 0; 811103a8feaSLen Brown } 812103a8feaSLen Brown 813e23da037SLen Brown /* 814e23da037SLen Brown * column formatting convention & formats 815e23da037SLen Brown */ 816c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 817c98d5d94SLen Brown struct pkg_data *p) 818103a8feaSLen Brown { 819008d396eSLen Brown double interval_float, tsc; 820fc04cc67SLen Brown char *fmt8; 821388e9c81SLen Brown int i; 822388e9c81SLen Brown struct msr_counter *mp; 8236168c2e0SLen Brown char *delim = "\t"; 8246168c2e0SLen Brown int printed = 0; 825103a8feaSLen Brown 826c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 827c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 828c98d5d94SLen Brown return 0; 829c98d5d94SLen Brown 830c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 831c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 832c98d5d94SLen Brown return 0; 833c98d5d94SLen Brown 8341ef7d21aSLen Brown /*if not summary line and --cpu is used */ 8351ef7d21aSLen Brown if ((t != &average.threads) && 8361ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 8371ef7d21aSLen Brown return 0; 8381ef7d21aSLen Brown 8393f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 840f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 841f4fdf2b4SLen Brown struct timeval tv; 842f4fdf2b4SLen Brown 843f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 844f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 845f4fdf2b4SLen Brown } 846f4fdf2b4SLen Brown 8473f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 8483f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 8493f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 8503f44a5c6SLen Brown 851103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 852103a8feaSLen Brown 853008d396eSLen Brown tsc = t->tsc * tsc_tweak; 854008d396eSLen Brown 855c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 856c98d5d94SLen Brown if (t == &average.threads) { 857812db3f7SLen Brown if (DO_BIC(BIC_Package)) 8586168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 859812db3f7SLen Brown if (DO_BIC(BIC_Core)) 8606168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 861812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8626168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 863103a8feaSLen Brown } else { 864812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 865c98d5d94SLen Brown if (p) 8666168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 867c98d5d94SLen Brown else 8686168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 869c98d5d94SLen Brown } 870812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 871c98d5d94SLen Brown if (c) 8726168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 873c98d5d94SLen Brown else 8746168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 875c98d5d94SLen Brown } 876812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 8776168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 878103a8feaSLen Brown } 879fc04cc67SLen Brown 880812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 8816168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 882fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 883fc04cc67SLen Brown 884812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 8856168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 886103a8feaSLen Brown 887812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 88821ed5574SLen Brown if (has_base_hz) 8896168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 89021ed5574SLen Brown else 8916168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 892008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 89321ed5574SLen Brown } 894103a8feaSLen Brown 895812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 8966168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 897103a8feaSLen Brown 898562a2d37SLen Brown /* IRQ */ 8990de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 9000de6c0dfSLen Brown if (sums_need_wide_columns) 9016168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 9020de6c0dfSLen Brown else 9036168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 9040de6c0dfSLen Brown } 905562a2d37SLen Brown 9061cc21f7bSLen Brown /* SMI */ 907812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 9086168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 9091cc21f7bSLen Brown 910678a3bd1SLen Brown /* Added counters */ 911678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 912678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 913678a3bd1SLen Brown if (mp->width == 32) 9145f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 915678a3bd1SLen Brown else 9166168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 917678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 9180de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9196168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 9200de6c0dfSLen Brown else 9216168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 922678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 92341618e63SLen Brown if (mp->type == COUNTER_USEC) 9246168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 92541618e63SLen Brown else 9266168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 927678a3bd1SLen Brown } 928678a3bd1SLen Brown } 929678a3bd1SLen Brown 93041618e63SLen Brown /* C1 */ 93141618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 9326168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 93341618e63SLen Brown 93441618e63SLen Brown 935c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 936c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 937c98d5d94SLen Brown goto done; 938c98d5d94SLen Brown 939997e5395SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) 9406168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 941812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 9426168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 943812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 9446168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 945c98d5d94SLen Brown 9460539ba11SLen Brown /* Mod%c6 */ 9470539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 9486168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 9490539ba11SLen Brown 950812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 9516168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 952889facbeSLen Brown 953388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 954388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 955388e9c81SLen Brown if (mp->width == 32) 9565f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 957388e9c81SLen Brown else 9586168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 959388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 9600de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9616168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 9620de6c0dfSLen Brown else 9636168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 964388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 9656168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 966388e9c81SLen Brown } 967388e9c81SLen Brown } 968388e9c81SLen Brown 969c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 970c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 971c98d5d94SLen Brown goto done; 972c98d5d94SLen Brown 9730b2bb692SLen Brown /* PkgTmp */ 974812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 9756168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 976889facbeSLen Brown 977fdf676e5SLen Brown /* GFXrc6 */ 978812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 979ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 9806168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 9819185e988SLen Brown } else { 9826168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 9839185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 9849185e988SLen Brown } 9859185e988SLen Brown } 986fdf676e5SLen Brown 98727d47356SLen Brown /* GFXMHz */ 988812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 9896168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 99027d47356SLen Brown 9910b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 992a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 9936168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 994a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 9956168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 996a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 9976168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 998a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 9996168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 10000b2bb692SLen Brown 10010f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 10026168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 10030f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10046168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 10050f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10066168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 10070f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10086168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 10090f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 10106168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 10110f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 10126168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 10130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 10146168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 1015889facbeSLen Brown 1016be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1017be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1018be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1019be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1020be0e54c4SLen Brown 1021889facbeSLen Brown /* 1022889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 1023889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 1024889facbeSLen Brown */ 1025fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 10266168c2e0SLen Brown fmt8 = "%s%.2f"; 1027fc04cc67SLen Brown else 1028fc04cc67SLen Brown fmt8 = "%6.0f**"; 1029889facbeSLen Brown 1030812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 10316168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 1032812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 10336168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1034812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 10356168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1036812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 10376168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 1038812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 10396168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 1040812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 10416168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1042812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 10436168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1044812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 10456168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1046812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 10476168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1048812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 10496168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1050812db3f7SLen Brown 1051388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1052388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1053388e9c81SLen Brown if (mp->width == 32) 10545f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]); 1055388e9c81SLen Brown else 10566168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1057388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 10580de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10596168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 10600de6c0dfSLen Brown else 10616168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1062388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 10636168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc); 1064388e9c81SLen Brown } 1065388e9c81SLen Brown } 1066388e9c81SLen Brown 1067c98d5d94SLen Brown done: 106894d6ab4bSLen Brown if (*(outp - 1) != '\n') 1069c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1070c98d5d94SLen Brown 1071c98d5d94SLen Brown return 0; 1072103a8feaSLen Brown } 1073103a8feaSLen Brown 1074b7d8c148SLen Brown void flush_output_stdout(void) 1075103a8feaSLen Brown { 1076b7d8c148SLen Brown FILE *filep; 1077b7d8c148SLen Brown 1078b7d8c148SLen Brown if (outf == stderr) 1079b7d8c148SLen Brown filep = stdout; 1080b7d8c148SLen Brown else 1081b7d8c148SLen Brown filep = outf; 1082b7d8c148SLen Brown 1083b7d8c148SLen Brown fputs(output_buffer, filep); 1084b7d8c148SLen Brown fflush(filep); 1085b7d8c148SLen Brown 1086c98d5d94SLen Brown outp = output_buffer; 1087c98d5d94SLen Brown } 1088b7d8c148SLen Brown void flush_output_stderr(void) 1089c98d5d94SLen Brown { 1090b7d8c148SLen Brown fputs(output_buffer, outf); 1091b7d8c148SLen Brown fflush(outf); 1092c98d5d94SLen Brown outp = output_buffer; 1093c98d5d94SLen Brown } 1094c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1095c98d5d94SLen Brown { 1096e23da037SLen Brown static int printed; 1097103a8feaSLen Brown 1098e23da037SLen Brown if (!printed || !summary_only) 1099c8ade361SLen Brown print_header("\t"); 1100103a8feaSLen Brown 1101c98d5d94SLen Brown if (topo.num_cpus > 1) 1102c98d5d94SLen Brown format_counters(&average.threads, &average.cores, 1103c98d5d94SLen Brown &average.packages); 1104103a8feaSLen Brown 1105e23da037SLen Brown printed = 1; 1106e23da037SLen Brown 1107e23da037SLen Brown if (summary_only) 1108e23da037SLen Brown return; 1109e23da037SLen Brown 1110c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1111103a8feaSLen Brown } 1112103a8feaSLen Brown 1113889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 1114889facbeSLen Brown if (new > old) { \ 1115889facbeSLen Brown old = new - old; \ 1116889facbeSLen Brown } else { \ 1117889facbeSLen Brown old = 0x100000000 + new - old; \ 1118889facbeSLen Brown } 1119889facbeSLen Brown 1120ba3dec99SLen Brown int 1121c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1122103a8feaSLen Brown { 1123388e9c81SLen Brown int i; 1124388e9c81SLen Brown struct msr_counter *mp; 11250b2bb692SLen Brown 1126a99d8730SLen Brown 1127a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 11280b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1129a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 11300b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1131a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 11320b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1133a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 11340b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1135a99d8730SLen Brown 1136c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 11370f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1138c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 11390f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1140c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 11410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1142c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1143ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1144ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1145ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1146be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1147be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1148889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1149889facbeSLen Brown 11509185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 11519185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 11529185e988SLen Brown old->gfx_rc6_ms = -1; 11539185e988SLen Brown else 1154fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 11559185e988SLen Brown 115627d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 115727d47356SLen Brown 1158889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 1159889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 1160889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 1161889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 1162889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 1163889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 1164ba3dec99SLen Brown 1165388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1166388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1167388e9c81SLen Brown old->counter[i] = new->counter[i]; 1168388e9c81SLen Brown else 1169388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1170388e9c81SLen Brown } 1171388e9c81SLen Brown 1172ba3dec99SLen Brown return 0; 1173103a8feaSLen Brown } 1174103a8feaSLen Brown 1175c98d5d94SLen Brown void 1176c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1177c98d5d94SLen Brown { 1178388e9c81SLen Brown int i; 1179388e9c81SLen Brown struct msr_counter *mp; 1180388e9c81SLen Brown 1181c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1182c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1183c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1184889facbeSLen Brown old->core_temp_c = new->core_temp_c; 11850539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1186388e9c81SLen Brown 1187388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1188388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1189388e9c81SLen Brown old->counter[i] = new->counter[i]; 1190388e9c81SLen Brown else 1191388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1192388e9c81SLen Brown } 1193103a8feaSLen Brown } 1194c98d5d94SLen Brown 1195c3ae331dSLen Brown /* 1196c3ae331dSLen Brown * old = new - old 1197c3ae331dSLen Brown */ 1198ba3dec99SLen Brown int 1199c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1200c98d5d94SLen Brown struct core_data *core_delta) 1201c98d5d94SLen Brown { 1202388e9c81SLen Brown int i; 1203388e9c81SLen Brown struct msr_counter *mp; 1204388e9c81SLen Brown 12053f44a5c6SLen Brown /* 12063f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 12073f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 12083f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 12093f44a5c6SLen Brown */ 12103f44a5c6SLen Brown 12113f44a5c6SLen Brown old->tv_begin = new->tv_begin; 12123f44a5c6SLen Brown old->tv_end = new->tv_end; 12133f44a5c6SLen Brown 1214c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1215c98d5d94SLen Brown 1216103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1217b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1218b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1219b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1220b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1221103a8feaSLen Brown 1222c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1223c98d5d94SLen Brown 1224812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 1225c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1226c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1227c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1228c98d5d94SLen Brown } else { 1229ba3dec99SLen Brown return -1; 1230103a8feaSLen Brown } 1231a729617cSLen Brown } 1232103a8feaSLen Brown 1233c98d5d94SLen Brown 1234144b44b1SLen Brown if (use_c1_residency_msr) { 1235144b44b1SLen Brown /* 1236144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1237144b44b1SLen Brown * which should be more accurate than the derivation below. 1238144b44b1SLen Brown */ 1239144b44b1SLen Brown } else { 1240103a8feaSLen Brown /* 1241c3ae331dSLen Brown * As counter collection is not atomic, 1242c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1243103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1244103a8feaSLen Brown */ 124595149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1246c98d5d94SLen Brown old->c1 = 0; 1247c98d5d94SLen Brown else { 1248c98d5d94SLen Brown /* normal case, derive c1 */ 1249008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1250c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1251c98d5d94SLen Brown } 1252144b44b1SLen Brown } 1253c3ae331dSLen Brown 1254c98d5d94SLen Brown if (old->mperf == 0) { 1255b7d8c148SLen Brown if (debug > 1) 1256b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1257c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1258c98d5d94SLen Brown } 1259103a8feaSLen Brown 1260812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1261562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1262562a2d37SLen Brown 1263812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 12641ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1265ba3dec99SLen Brown 1266388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1267388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1268388e9c81SLen Brown old->counter[i] = new->counter[i]; 1269388e9c81SLen Brown else 1270388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1271388e9c81SLen Brown } 1272ba3dec99SLen Brown return 0; 1273103a8feaSLen Brown } 1274c98d5d94SLen Brown 1275c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1276c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1277c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1278c98d5d94SLen Brown { 1279ba3dec99SLen Brown int retval = 0; 1280ba3dec99SLen Brown 1281c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1282c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1283c98d5d94SLen Brown delta_core(c, c2); 1284c98d5d94SLen Brown 1285c98d5d94SLen Brown /* always calculate thread delta */ 1286ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1287ba3dec99SLen Brown if (retval) 1288ba3dec99SLen Brown return retval; 1289c98d5d94SLen Brown 1290c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1291c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1292ba3dec99SLen Brown retval = delta_package(p, p2); 1293c98d5d94SLen Brown 1294ba3dec99SLen Brown return retval; 1295103a8feaSLen Brown } 1296103a8feaSLen Brown 1297c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1298103a8feaSLen Brown { 1299388e9c81SLen Brown int i; 1300388e9c81SLen Brown struct msr_counter *mp; 1301388e9c81SLen Brown 13023f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 13033f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 13043f44a5c6SLen Brown t->tv_end.tv_sec = 0; 13053f44a5c6SLen Brown t->tv_end.tv_usec = 0; 13063f44a5c6SLen Brown 1307c98d5d94SLen Brown t->tsc = 0; 1308c98d5d94SLen Brown t->aperf = 0; 1309c98d5d94SLen Brown t->mperf = 0; 1310c98d5d94SLen Brown t->c1 = 0; 1311103a8feaSLen Brown 1312562a2d37SLen Brown t->irq_count = 0; 1313562a2d37SLen Brown t->smi_count = 0; 1314562a2d37SLen Brown 1315c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1316c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1317c98d5d94SLen Brown 1318c98d5d94SLen Brown c->c3 = 0; 1319c98d5d94SLen Brown c->c6 = 0; 1320c98d5d94SLen Brown c->c7 = 0; 13210539ba11SLen Brown c->mc6_us = 0; 1322889facbeSLen Brown c->core_temp_c = 0; 1323c98d5d94SLen Brown 13240b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 13250b2bb692SLen Brown p->pkg_any_core_c0 = 0; 13260b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 13270b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 13280b2bb692SLen Brown 1329c98d5d94SLen Brown p->pc2 = 0; 13300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1331c98d5d94SLen Brown p->pc3 = 0; 13320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1333c98d5d94SLen Brown p->pc6 = 0; 13340f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1335c98d5d94SLen Brown p->pc7 = 0; 1336ca58710fSKristen Carlson Accardi p->pc8 = 0; 1337ca58710fSKristen Carlson Accardi p->pc9 = 0; 1338ca58710fSKristen Carlson Accardi p->pc10 = 0; 1339be0e54c4SLen Brown p->cpu_lpi = 0; 1340be0e54c4SLen Brown p->sys_lpi = 0; 1341889facbeSLen Brown 1342889facbeSLen Brown p->energy_pkg = 0; 1343889facbeSLen Brown p->energy_dram = 0; 1344889facbeSLen Brown p->energy_cores = 0; 1345889facbeSLen Brown p->energy_gfx = 0; 1346889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1347889facbeSLen Brown p->rapl_dram_perf_status = 0; 1348889facbeSLen Brown p->pkg_temp_c = 0; 134927d47356SLen Brown 1350fdf676e5SLen Brown p->gfx_rc6_ms = 0; 135127d47356SLen Brown p->gfx_mhz = 0; 1352388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1353388e9c81SLen Brown t->counter[i] = 0; 1354388e9c81SLen Brown 1355388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1356388e9c81SLen Brown c->counter[i] = 0; 1357388e9c81SLen Brown 1358388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1359388e9c81SLen Brown p->counter[i] = 0; 1360103a8feaSLen Brown } 1361c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1362c98d5d94SLen Brown struct pkg_data *p) 1363103a8feaSLen Brown { 1364388e9c81SLen Brown int i; 1365388e9c81SLen Brown struct msr_counter *mp; 1366388e9c81SLen Brown 13673f44a5c6SLen Brown /* remember first tv_begin */ 13683f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 13693f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 13703f44a5c6SLen Brown 13713f44a5c6SLen Brown /* remember last tv_end */ 13723f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 13733f44a5c6SLen Brown 1374c98d5d94SLen Brown average.threads.tsc += t->tsc; 1375c98d5d94SLen Brown average.threads.aperf += t->aperf; 1376c98d5d94SLen Brown average.threads.mperf += t->mperf; 1377c98d5d94SLen Brown average.threads.c1 += t->c1; 137815aaa346SLen Brown 1379562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1380562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1381562a2d37SLen Brown 1382388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1383388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1384388e9c81SLen Brown continue; 1385388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1386388e9c81SLen Brown } 1387388e9c81SLen Brown 1388c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1389c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1390c98d5d94SLen Brown return 0; 1391c98d5d94SLen Brown 1392c98d5d94SLen Brown average.cores.c3 += c->c3; 1393c98d5d94SLen Brown average.cores.c6 += c->c6; 1394c98d5d94SLen Brown average.cores.c7 += c->c7; 13950539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1396c98d5d94SLen Brown 1397889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1398889facbeSLen Brown 1399388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1400388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1401388e9c81SLen Brown continue; 1402388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1403388e9c81SLen Brown } 1404388e9c81SLen Brown 1405c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1406c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1407c98d5d94SLen Brown return 0; 1408c98d5d94SLen Brown 1409a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14100b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1411a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14120b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1413a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14140b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1415a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14160b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 14170b2bb692SLen Brown 1418c98d5d94SLen Brown average.packages.pc2 += p->pc2; 14190f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1420c98d5d94SLen Brown average.packages.pc3 += p->pc3; 14210f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1422c98d5d94SLen Brown average.packages.pc6 += p->pc6; 14230f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1424c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1425ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1426ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1427ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1428c98d5d94SLen Brown 1429be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1430be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1431be0e54c4SLen Brown 1432889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1433889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1434889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1435889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1436889facbeSLen Brown 1437fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 143827d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 143927d47356SLen Brown 1440889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1441889facbeSLen Brown 1442889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1443889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1444388e9c81SLen Brown 1445388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1446388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1447388e9c81SLen Brown continue; 1448388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1449388e9c81SLen Brown } 1450c98d5d94SLen Brown return 0; 1451c98d5d94SLen Brown } 1452c98d5d94SLen Brown /* 1453c98d5d94SLen Brown * sum the counters for all cpus in the system 1454c98d5d94SLen Brown * compute the weighted average 1455c98d5d94SLen Brown */ 1456c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1457c98d5d94SLen Brown struct pkg_data *p) 1458c98d5d94SLen Brown { 1459388e9c81SLen Brown int i; 1460388e9c81SLen Brown struct msr_counter *mp; 1461388e9c81SLen Brown 1462c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1463c98d5d94SLen Brown 1464c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1465c98d5d94SLen Brown 1466c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1467c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1468c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1469c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1470c98d5d94SLen Brown 14710de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 14720de6c0dfSLen Brown sums_need_wide_columns = 1; 14730de6c0dfSLen Brown 1474c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1475c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1476c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 14770539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1478c98d5d94SLen Brown 1479a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14800b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1481a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14820b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1483a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14840b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1485a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14860b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 14870b2bb692SLen Brown 1488c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 14890f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1490c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 14910f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1492c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 14930f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1494c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1495ca58710fSKristen Carlson Accardi 1496ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1497ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1498ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1499388e9c81SLen Brown 1500388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1501388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1502388e9c81SLen Brown continue; 15030de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 15040de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 15050de6c0dfSLen Brown sums_need_wide_columns = 1; 150641618e63SLen Brown continue; 15070de6c0dfSLen Brown } 1508388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1509388e9c81SLen Brown } 1510388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1511388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1512388e9c81SLen Brown continue; 15130de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 15140de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 15150de6c0dfSLen Brown sums_need_wide_columns = 1; 15160de6c0dfSLen Brown } 1517388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1518388e9c81SLen Brown } 1519388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1520388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1521388e9c81SLen Brown continue; 15220de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 15230de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 15240de6c0dfSLen Brown sums_need_wide_columns = 1; 15250de6c0dfSLen Brown } 1526388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1527388e9c81SLen Brown } 1528c98d5d94SLen Brown } 1529c98d5d94SLen Brown 1530c98d5d94SLen Brown static unsigned long long rdtsc(void) 1531c98d5d94SLen Brown { 1532c98d5d94SLen Brown unsigned int low, high; 1533c98d5d94SLen Brown 1534c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1535c98d5d94SLen Brown 1536c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1537c98d5d94SLen Brown } 1538c98d5d94SLen Brown 1539c98d5d94SLen Brown /* 1540495c7654SLen Brown * Open a file, and exit on failure 1541495c7654SLen Brown */ 1542495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1543495c7654SLen Brown { 1544495c7654SLen Brown FILE *filep = fopen(path, mode); 1545495c7654SLen Brown 1546495c7654SLen Brown if (!filep) 1547495c7654SLen Brown err(1, "%s: open failed", path); 1548495c7654SLen Brown return filep; 1549495c7654SLen Brown } 1550495c7654SLen Brown /* 1551495c7654SLen Brown * snapshot_sysfs_counter() 1552495c7654SLen Brown * 1553495c7654SLen Brown * return snapshot of given counter 1554495c7654SLen Brown */ 1555495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1556495c7654SLen Brown { 1557495c7654SLen Brown FILE *fp; 1558495c7654SLen Brown int retval; 1559495c7654SLen Brown unsigned long long counter; 1560495c7654SLen Brown 1561495c7654SLen Brown fp = fopen_or_die(path, "r"); 1562495c7654SLen Brown 1563495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1564495c7654SLen Brown if (retval != 1) 1565495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1566495c7654SLen Brown 1567495c7654SLen Brown fclose(fp); 1568495c7654SLen Brown 1569495c7654SLen Brown return counter; 1570495c7654SLen Brown } 1571495c7654SLen Brown 1572495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1573495c7654SLen Brown { 1574495c7654SLen Brown if (mp->msr_num != 0) { 1575495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1576495c7654SLen Brown return -1; 1577495c7654SLen Brown } else { 157846c27978SLen Brown char path[128 + PATH_BYTES]; 157941618e63SLen Brown 158041618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 158141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 158241618e63SLen Brown cpu, mp->path); 158341618e63SLen Brown 158441618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 158541618e63SLen Brown } else { 1586495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1587495c7654SLen Brown } 158841618e63SLen Brown } 1589495c7654SLen Brown 1590495c7654SLen Brown return 0; 1591495c7654SLen Brown } 1592495c7654SLen Brown 1593495c7654SLen Brown /* 1594c98d5d94SLen Brown * get_counters(...) 1595c98d5d94SLen Brown * migrate to cpu 1596c98d5d94SLen Brown * acquire and record local counters for that cpu 1597c98d5d94SLen Brown */ 1598c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1599c98d5d94SLen Brown { 1600c98d5d94SLen Brown int cpu = t->cpu_id; 1601889facbeSLen Brown unsigned long long msr; 16020102b067SLen Brown int aperf_mperf_retry_count = 0; 1603388e9c81SLen Brown struct msr_counter *mp; 1604388e9c81SLen Brown int i; 1605c98d5d94SLen Brown 1606f4fdf2b4SLen Brown 1607f4fdf2b4SLen Brown gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1608f4fdf2b4SLen Brown 1609e52966c0SLen Brown if (cpu_migrate(cpu)) { 1610b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 161115aaa346SLen Brown return -1; 1612e52966c0SLen Brown } 161315aaa346SLen Brown 16140102b067SLen Brown retry: 1615c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 161615aaa346SLen Brown 1617812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 16180102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 16190102b067SLen Brown 16200102b067SLen Brown /* 16210102b067SLen Brown * The TSC, APERF and MPERF must be read together for 16220102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 16230102b067SLen Brown * 16240102b067SLen Brown * Unfortunately, APERF and MPERF are read by 16250102b067SLen Brown * individual system call, so delays may occur 16260102b067SLen Brown * between them. If the time to read them 16270102b067SLen Brown * varies by a large amount, we re-read them. 16280102b067SLen Brown */ 16290102b067SLen Brown 16300102b067SLen Brown /* 16310102b067SLen Brown * This initial dummy APERF read has been seen to 16320102b067SLen Brown * reduce jitter in the subsequent reads. 16330102b067SLen Brown */ 16340102b067SLen Brown 16359c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1636c98d5d94SLen Brown return -3; 16370102b067SLen Brown 16380102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 16390102b067SLen Brown 16400102b067SLen Brown tsc_before = t->tsc; 16410102b067SLen Brown 16420102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 16430102b067SLen Brown return -3; 16440102b067SLen Brown 16450102b067SLen Brown tsc_between = rdtsc(); 16460102b067SLen Brown 16479c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1648c98d5d94SLen Brown return -4; 16490102b067SLen Brown 16500102b067SLen Brown tsc_after = rdtsc(); 16510102b067SLen Brown 16520102b067SLen Brown aperf_time = tsc_between - tsc_before; 16530102b067SLen Brown mperf_time = tsc_after - tsc_between; 16540102b067SLen Brown 16550102b067SLen Brown /* 16560102b067SLen Brown * If the system call latency to read APERF and MPERF 16570102b067SLen Brown * differ by more than 2x, then try again. 16580102b067SLen Brown */ 16590102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 16600102b067SLen Brown aperf_mperf_retry_count++; 16610102b067SLen Brown if (aperf_mperf_retry_count < 5) 16620102b067SLen Brown goto retry; 16630102b067SLen Brown else 16640102b067SLen Brown warnx("cpu%d jitter %lld %lld", 16650102b067SLen Brown cpu, aperf_time, mperf_time); 16660102b067SLen Brown } 16670102b067SLen Brown aperf_mperf_retry_count = 0; 16680102b067SLen Brown 1669b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1670b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 167188c3281fSLen Brown } 167288c3281fSLen Brown 1673812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1674562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1675812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 16761ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 16771ed51011SLen Brown return -5; 16781ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 16791ed51011SLen Brown } 16800539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1681144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1682144b44b1SLen Brown return -6; 1683144b44b1SLen Brown } 1684144b44b1SLen Brown 1685388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1686495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1687388e9c81SLen Brown return -10; 1688388e9c81SLen Brown } 1689388e9c81SLen Brown 1690c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1691c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1692f4fdf2b4SLen Brown goto done; 1693c98d5d94SLen Brown 1694997e5395SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) { 1695c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1696c98d5d94SLen Brown return -6; 1697144b44b1SLen Brown } 1698144b44b1SLen Brown 1699812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) { 1700c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1701c98d5d94SLen Brown return -7; 1702fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1703fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1704fb5d4327SDasaratharaman Chandramouli return -7; 1705103a8feaSLen Brown } 170615aaa346SLen Brown 1707812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 1708c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1709c98d5d94SLen Brown return -8; 1710c98d5d94SLen Brown 17110539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 17120539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 17130539ba11SLen Brown return -8; 17140539ba11SLen Brown 1715812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 1716889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1717889facbeSLen Brown return -9; 1718889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1719889facbeSLen Brown } 1720889facbeSLen Brown 1721388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1722495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 1723388e9c81SLen Brown return -10; 1724388e9c81SLen Brown } 1725889facbeSLen Brown 1726c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1727c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1728f4fdf2b4SLen Brown goto done; 172915aaa346SLen Brown 1730a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 17310b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 17320b2bb692SLen Brown return -10; 1733a99d8730SLen Brown } 1734a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 17350b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 17360b2bb692SLen Brown return -11; 1737a99d8730SLen Brown } 1738a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 17390b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 17400b2bb692SLen Brown return -12; 1741a99d8730SLen Brown } 1742a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 17430b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 17440b2bb692SLen Brown return -13; 17450b2bb692SLen Brown } 17460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1747c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1748c98d5d94SLen Brown return -9; 17490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 17500539ba11SLen Brown if (do_slm_cstates) { 17510539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 17520539ba11SLen Brown return -10; 17530539ba11SLen Brown } else { 1754c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1755c98d5d94SLen Brown return -10; 17560539ba11SLen Brown } 17570539ba11SLen Brown } 17580539ba11SLen Brown 17590f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 1760c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1761c98d5d94SLen Brown return -11; 17620f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1763c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1764c98d5d94SLen Brown return -12; 17650f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 1766ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1767ca58710fSKristen Carlson Accardi return -13; 17680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 1769ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1770ca58710fSKristen Carlson Accardi return -13; 17710f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 1772ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1773ca58710fSKristen Carlson Accardi return -13; 17740f47c08dSLen Brown 1775be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1776be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 1777be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1778be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 1779be0e54c4SLen Brown 1780889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1781889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1782889facbeSLen Brown return -13; 1783889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1784889facbeSLen Brown } 17859148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 1786889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1787889facbeSLen Brown return -14; 1788889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1789889facbeSLen Brown } 1790889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1791889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1792889facbeSLen Brown return -15; 1793889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1794889facbeSLen Brown } 1795889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1796889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1797889facbeSLen Brown return -16; 1798889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1799889facbeSLen Brown } 1800889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1801889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1802889facbeSLen Brown return -16; 1803889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1804889facbeSLen Brown } 1805889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1806889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1807889facbeSLen Brown return -16; 1808889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1809889facbeSLen Brown } 1810812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 1811889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1812889facbeSLen Brown return -17; 1813889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1814889facbeSLen Brown } 1815fdf676e5SLen Brown 1816812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 1817fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 1818fdf676e5SLen Brown 1819812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 182027d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 182127d47356SLen Brown 1822388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1823495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 1824388e9c81SLen Brown return -10; 1825388e9c81SLen Brown } 1826f4fdf2b4SLen Brown done: 1827f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 1828388e9c81SLen Brown 182915aaa346SLen Brown return 0; 1830103a8feaSLen Brown } 1831103a8feaSLen Brown 1832ee7e38e3SLen Brown /* 1833ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1834ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1835ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1836ee7e38e3SLen Brown */ 1837ee7e38e3SLen Brown 1838ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1839ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1840ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1841ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1842ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1843ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1844ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1845ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1846ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1847ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1848ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1849ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 18500b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 18510b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 18520b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1853ee7e38e3SLen Brown 1854ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1855ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 18560b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1857ee7e38e3SLen Brown 1858e9257f5fSLen 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}; 1859e9257f5fSLen 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}; 1860e9257f5fSLen 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}; 18610539ba11SLen 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}; 1862f2642888SLen 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}; 1863e9257f5fSLen 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}; 1864e4085d54SLen 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}; 18652085e124SArtem Bityutskiy int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 1866ee7e38e3SLen Brown 1867a2b7b749SLen Brown 1868a2b7b749SLen Brown static void 1869a2b7b749SLen Brown calculate_tsc_tweak() 1870a2b7b749SLen Brown { 1871a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 1872a2b7b749SLen Brown } 1873a2b7b749SLen Brown 1874fcd17211SLen Brown static void 1875fcd17211SLen Brown dump_nhm_platform_info(void) 1876103a8feaSLen Brown { 1877103a8feaSLen Brown unsigned long long msr; 1878103a8feaSLen Brown unsigned int ratio; 1879103a8feaSLen Brown 1880ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 1881103a8feaSLen Brown 1882b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 18836574a5d5SLen Brown 1884103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1885710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 1886103a8feaSLen Brown ratio, bclk, ratio * bclk); 1887103a8feaSLen Brown 1888103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1889710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 1890103a8feaSLen Brown ratio, bclk, ratio * bclk); 1891103a8feaSLen Brown 18927ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1893b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1894bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 189567920418SLen Brown 1896fcd17211SLen Brown return; 1897fcd17211SLen Brown } 1898fcd17211SLen Brown 1899fcd17211SLen Brown static void 1900fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 1901fcd17211SLen Brown { 1902fcd17211SLen Brown unsigned long long msr; 1903fcd17211SLen Brown unsigned int ratio; 1904fcd17211SLen Brown 19057ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 1906fcd17211SLen Brown 1907b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 1908fcd17211SLen Brown 1909fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 1910fcd17211SLen Brown if (ratio) 1911710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 1912fcd17211SLen Brown ratio, bclk, ratio * bclk); 1913fcd17211SLen Brown 1914fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 1915fcd17211SLen Brown if (ratio) 1916710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 1917fcd17211SLen Brown ratio, bclk, ratio * bclk); 1918fcd17211SLen Brown return; 1919fcd17211SLen Brown } 1920fcd17211SLen Brown 1921fcd17211SLen Brown static void 1922fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 1923fcd17211SLen Brown { 1924fcd17211SLen Brown unsigned long long msr; 1925fcd17211SLen Brown unsigned int ratio; 19266574a5d5SLen Brown 19277ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 19286574a5d5SLen Brown 1929b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 19306574a5d5SLen Brown 19316574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 19326574a5d5SLen Brown if (ratio) 1933710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 19346574a5d5SLen Brown ratio, bclk, ratio * bclk); 19356574a5d5SLen Brown 19366574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 19376574a5d5SLen Brown if (ratio) 1938710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 19396574a5d5SLen Brown ratio, bclk, ratio * bclk); 19406574a5d5SLen Brown 19416574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 19426574a5d5SLen Brown if (ratio) 1943710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 19446574a5d5SLen Brown ratio, bclk, ratio * bclk); 19456574a5d5SLen Brown 19466574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 19476574a5d5SLen Brown if (ratio) 1948710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 19496574a5d5SLen Brown ratio, bclk, ratio * bclk); 19506574a5d5SLen Brown 19516574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 19526574a5d5SLen Brown if (ratio) 1953710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 19546574a5d5SLen Brown ratio, bclk, ratio * bclk); 19556574a5d5SLen Brown 19566574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 19576574a5d5SLen Brown if (ratio) 1958710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 19596574a5d5SLen Brown ratio, bclk, ratio * bclk); 19606574a5d5SLen Brown 19616574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 19626574a5d5SLen Brown if (ratio) 1963710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 19646574a5d5SLen Brown ratio, bclk, ratio * bclk); 19656574a5d5SLen Brown 19666574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 19676574a5d5SLen Brown if (ratio) 1968710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 19696574a5d5SLen Brown ratio, bclk, ratio * bclk); 1970103a8feaSLen Brown return; 1971fcd17211SLen Brown } 197231e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 197331e07522SLen Brown { 197431e07522SLen Brown 197531e07522SLen Brown if (!genuine_intel) 197631e07522SLen Brown return 0; 197731e07522SLen Brown 197831e07522SLen Brown switch (model) { 197931e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 198031e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 198131e07522SLen Brown case INTEL_FAM6_ATOM_DENVERTON: 198231e07522SLen Brown return 1; 198331e07522SLen Brown } 198431e07522SLen Brown return 0; 198531e07522SLen Brown } 1986fcd17211SLen Brown 1987fcd17211SLen Brown static void 198831e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 1989fcd17211SLen Brown { 199031e07522SLen Brown unsigned long long msr, core_counts; 199131e07522SLen Brown unsigned int ratio, group_size; 1992103a8feaSLen Brown 19937ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 1994b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 19956574a5d5SLen Brown 199631e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 199731e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 199831e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 199931e07522SLen Brown } else { 200031e07522SLen Brown core_counts = 0x0807060504030201; 200131e07522SLen Brown } 200231e07522SLen Brown 20036574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 200431e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 20056574a5d5SLen Brown if (ratio) 200631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 200731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 20086574a5d5SLen Brown 20096574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 201031e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 20116574a5d5SLen Brown if (ratio) 201231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 201331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 20146574a5d5SLen Brown 20156574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 201631e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 20176574a5d5SLen Brown if (ratio) 201831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 201931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 20206574a5d5SLen Brown 20216574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 202231e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 20236574a5d5SLen Brown if (ratio) 202431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 202531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 20266574a5d5SLen Brown 2027103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 202831e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2029103a8feaSLen Brown if (ratio) 203031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 203131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2032103a8feaSLen Brown 2033103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 203431e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2035103a8feaSLen Brown if (ratio) 203631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 203731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2038103a8feaSLen Brown 2039103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 204031e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2041103a8feaSLen Brown if (ratio) 204231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 204331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2044103a8feaSLen Brown 2045103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 204631e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2047103a8feaSLen Brown if (ratio) 204831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 204931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2050fcd17211SLen Brown return; 2051fcd17211SLen Brown } 20523a9a941dSLen Brown 2053fcd17211SLen Brown static void 20540f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 20550f7887c4SLen Brown { 20560f7887c4SLen Brown unsigned long long msr; 20570f7887c4SLen Brown unsigned int ratio; 20580f7887c4SLen Brown 20590f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 20600f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 20610f7887c4SLen Brown 20620f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 20630f7887c4SLen Brown if (ratio) 20640f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 20650f7887c4SLen Brown ratio, bclk, ratio * bclk); 20660f7887c4SLen Brown 20670f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 20680f7887c4SLen Brown if (ratio) 20690f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 20700f7887c4SLen Brown ratio, bclk, ratio * bclk); 20710f7887c4SLen Brown 20720f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 20730f7887c4SLen Brown if (ratio) 20740f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 20750f7887c4SLen Brown ratio, bclk, ratio * bclk); 20760f7887c4SLen Brown 20770f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 20780f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 20790f7887c4SLen Brown 20800f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 20810f7887c4SLen Brown if (ratio) 20820f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 20830f7887c4SLen Brown ratio, bclk, ratio * bclk); 20840f7887c4SLen Brown 20850f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 20860f7887c4SLen Brown if (ratio) 20870f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 20880f7887c4SLen Brown ratio, bclk, ratio * bclk); 20890f7887c4SLen Brown 20900f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 20910f7887c4SLen Brown if (ratio) 20920f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 20930f7887c4SLen Brown ratio, bclk, ratio * bclk); 20940f7887c4SLen Brown 20950f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 20960f7887c4SLen Brown if (ratio) 20970f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 20980f7887c4SLen Brown ratio, bclk, ratio * bclk); 20990f7887c4SLen Brown } 21000f7887c4SLen Brown 21010f7887c4SLen Brown static void 2102fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2103fb5d4327SDasaratharaman Chandramouli { 2104cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2105cbf97abaSHubert Chrzaniuk 2106fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2107cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2108cbf97abaSHubert Chrzaniuk int i, b_nr; 2109cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2110cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2111fb5d4327SDasaratharaman Chandramouli 2112ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2113fb5d4327SDasaratharaman Chandramouli 2114b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2115bfae2052SLen Brown base_cpu, msr); 2116fb5d4327SDasaratharaman Chandramouli 2117fb5d4327SDasaratharaman Chandramouli /** 2118fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2119cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2120cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2121fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2122fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2123fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2124fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2125fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2126fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2127fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2128fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2129fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2130fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2131fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2132fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2133fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2134fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2135fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2136fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2137fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2138fb5d4327SDasaratharaman Chandramouli */ 2139fb5d4327SDasaratharaman Chandramouli 2140cbf97abaSHubert Chrzaniuk b_nr = 0; 2141cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2142cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2143cbf97abaSHubert Chrzaniuk 2144cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2145fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2146cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2147fb5d4327SDasaratharaman Chandramouli 2148cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2149cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2150cbf97abaSHubert Chrzaniuk b_nr++; 2151fb5d4327SDasaratharaman Chandramouli } 2152cbf97abaSHubert Chrzaniuk 2153cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2154cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2155b7d8c148SLen Brown fprintf(outf, 2156710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2157cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2158fb5d4327SDasaratharaman Chandramouli } 2159fb5d4327SDasaratharaman Chandramouli 2160fb5d4327SDasaratharaman Chandramouli static void 2161fcd17211SLen Brown dump_nhm_cst_cfg(void) 2162fcd17211SLen Brown { 2163fcd17211SLen Brown unsigned long long msr; 2164fcd17211SLen Brown 21651df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2166fcd17211SLen Brown 21671df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2168fcd17211SLen Brown 21693e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2170fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2171fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2172fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2173fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2174fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 21756c34f160SLen Brown (unsigned int)msr & 0xF, 2176fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2177ac980e13SArtem Bityutskiy 2178ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2179ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2180ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2181ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2182ac980e13SArtem Bityutskiy } 2183ac980e13SArtem Bityutskiy 2184ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2185ac980e13SArtem Bityutskiy 2186fcd17211SLen Brown return; 2187103a8feaSLen Brown } 2188103a8feaSLen Brown 21896fb3143bSLen Brown static void 21906fb3143bSLen Brown dump_config_tdp(void) 21916fb3143bSLen Brown { 21926fb3143bSLen Brown unsigned long long msr; 21936fb3143bSLen Brown 21946fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2195b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2196685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 21976fb3143bSLen Brown 21986fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2199b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 22006fb3143bSLen Brown if (msr) { 2201685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2202685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2203685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2204685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 22056fb3143bSLen Brown } 2206b7d8c148SLen Brown fprintf(outf, ")\n"); 22076fb3143bSLen Brown 22086fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2209b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 22106fb3143bSLen Brown if (msr) { 2211685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2212685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2213685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2214685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 22156fb3143bSLen Brown } 2216b7d8c148SLen Brown fprintf(outf, ")\n"); 22176fb3143bSLen Brown 22186fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2219b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 22206fb3143bSLen Brown if ((msr) & 0x3) 2221b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2222b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2223b7d8c148SLen Brown fprintf(outf, ")\n"); 22246fb3143bSLen Brown 22256fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2226b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2227685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2228b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2229b7d8c148SLen Brown fprintf(outf, ")\n"); 22306fb3143bSLen Brown } 22315a63426eSLen Brown 22325a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 22335a63426eSLen Brown 22345a63426eSLen Brown void print_irtl(void) 22355a63426eSLen Brown { 22365a63426eSLen Brown unsigned long long msr; 22375a63426eSLen Brown 22385a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 22395a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 22405a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 22415a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 22425a63426eSLen Brown 22435a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 22445a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 22455a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 22465a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 22475a63426eSLen Brown 22485a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 22495a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 22505a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 22515a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 22525a63426eSLen Brown 22535a63426eSLen Brown if (!do_irtl_hsw) 22545a63426eSLen Brown return; 22555a63426eSLen Brown 22565a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 22575a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 22585a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 22595a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 22605a63426eSLen Brown 22615a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 22625a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 22635a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 22645a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 22655a63426eSLen Brown 22665a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 22675a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 22685a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 22695a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 22705a63426eSLen Brown 22715a63426eSLen Brown } 227236229897SLen Brown void free_fd_percpu(void) 227336229897SLen Brown { 227436229897SLen Brown int i; 227536229897SLen Brown 227601a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 227736229897SLen Brown if (fd_percpu[i] != 0) 227836229897SLen Brown close(fd_percpu[i]); 227936229897SLen Brown } 228036229897SLen Brown 228136229897SLen Brown free(fd_percpu); 22826fb3143bSLen Brown } 22836fb3143bSLen Brown 2284c98d5d94SLen Brown void free_all_buffers(void) 2285103a8feaSLen Brown { 22860e2d8f05SLen Brown int i; 22870e2d8f05SLen Brown 2288c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2289c98d5d94SLen Brown cpu_present_set = NULL; 229036229897SLen Brown cpu_present_setsize = 0; 2291103a8feaSLen Brown 2292c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2293c98d5d94SLen Brown cpu_affinity_set = NULL; 2294c98d5d94SLen Brown cpu_affinity_setsize = 0; 2295103a8feaSLen Brown 2296c98d5d94SLen Brown free(thread_even); 2297c98d5d94SLen Brown free(core_even); 2298c98d5d94SLen Brown free(package_even); 2299103a8feaSLen Brown 2300c98d5d94SLen Brown thread_even = NULL; 2301c98d5d94SLen Brown core_even = NULL; 2302c98d5d94SLen Brown package_even = NULL; 2303103a8feaSLen Brown 2304c98d5d94SLen Brown free(thread_odd); 2305c98d5d94SLen Brown free(core_odd); 2306c98d5d94SLen Brown free(package_odd); 2307103a8feaSLen Brown 2308c98d5d94SLen Brown thread_odd = NULL; 2309c98d5d94SLen Brown core_odd = NULL; 2310c98d5d94SLen Brown package_odd = NULL; 2311103a8feaSLen Brown 2312c98d5d94SLen Brown free(output_buffer); 2313c98d5d94SLen Brown output_buffer = NULL; 2314c98d5d94SLen Brown outp = NULL; 231536229897SLen Brown 231636229897SLen Brown free_fd_percpu(); 2317562a2d37SLen Brown 2318562a2d37SLen Brown free(irq_column_2_cpu); 2319562a2d37SLen Brown free(irqs_per_cpu); 23200e2d8f05SLen Brown 23210e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 23220e2d8f05SLen Brown if (cpus[i].put_ids) 23230e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 23240e2d8f05SLen Brown } 23250e2d8f05SLen Brown free(cpus); 2326103a8feaSLen Brown } 2327103a8feaSLen Brown 232857a42a34SJosh Triplett 232957a42a34SJosh Triplett /* 233095aebc44SJosh Triplett * Parse a file containing a single int. 233195aebc44SJosh Triplett */ 233295aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 233395aebc44SJosh Triplett { 233495aebc44SJosh Triplett va_list args; 233595aebc44SJosh Triplett char path[PATH_MAX]; 233695aebc44SJosh Triplett FILE *filep; 233795aebc44SJosh Triplett int value; 233895aebc44SJosh Triplett 233995aebc44SJosh Triplett va_start(args, fmt); 234095aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 234195aebc44SJosh Triplett va_end(args); 234257a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 2343b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2344b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 234595aebc44SJosh Triplett fclose(filep); 234695aebc44SJosh Triplett return value; 234795aebc44SJosh Triplett } 234895aebc44SJosh Triplett 234995aebc44SJosh Triplett /* 2350c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2351c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2352c98d5d94SLen Brown */ 2353c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2354c98d5d94SLen Brown { 235595aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2356103a8feaSLen Brown } 2357103a8feaSLen Brown 2358103a8feaSLen Brown int get_physical_package_id(int cpu) 2359103a8feaSLen Brown { 236095aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2361103a8feaSLen Brown } 2362103a8feaSLen Brown 2363103a8feaSLen Brown int get_core_id(int cpu) 2364103a8feaSLen Brown { 236595aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2366103a8feaSLen Brown } 2367103a8feaSLen Brown 2368ef605741SPrarit Bhargava void set_node_data(void) 2369ef605741SPrarit Bhargava { 2370ef605741SPrarit Bhargava char path[80]; 2371ef605741SPrarit Bhargava FILE *filep; 2372ef605741SPrarit Bhargava int pkg, node, cpu; 2373ef605741SPrarit Bhargava 2374ef605741SPrarit Bhargava struct pkg_node_info { 2375ef605741SPrarit Bhargava int count; 2376ef605741SPrarit Bhargava int min; 2377ef605741SPrarit Bhargava } *pni; 2378ef605741SPrarit Bhargava 2379ef605741SPrarit Bhargava pni = calloc(topo.num_packages, sizeof(struct pkg_node_info)); 2380ef605741SPrarit Bhargava if (!pni) 2381ef605741SPrarit Bhargava err(1, "calloc pkg_node_count"); 2382ef605741SPrarit Bhargava 2383ef605741SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) 2384ef605741SPrarit Bhargava pni[pkg].min = topo.num_cpus; 2385ef605741SPrarit Bhargava 2386ef605741SPrarit Bhargava for (node = 0; node <= topo.max_node_num; node++) { 2387ef605741SPrarit Bhargava /* find the "first" cpu in the node */ 2388ef605741SPrarit Bhargava sprintf(path, "/sys/bus/node/devices/node%d/cpulist", node); 2389ef605741SPrarit Bhargava filep = fopen(path, "r"); 2390ef605741SPrarit Bhargava if (!filep) 2391ef605741SPrarit Bhargava continue; 2392ef605741SPrarit Bhargava fscanf(filep, "%d", &cpu); 2393ef605741SPrarit Bhargava fclose(filep); 2394ef605741SPrarit Bhargava 2395ef605741SPrarit Bhargava pkg = cpus[cpu].physical_package_id; 2396ef605741SPrarit Bhargava pni[pkg].count++; 2397ef605741SPrarit Bhargava 2398ef605741SPrarit Bhargava if (node < pni[pkg].min) 2399ef605741SPrarit Bhargava pni[pkg].min = node; 2400ef605741SPrarit Bhargava } 2401ef605741SPrarit Bhargava 2402ef605741SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) 2403*70a9c6e8SPrarit Bhargava if (pni[pkg].count > topo.nodes_per_pkg) 2404*70a9c6e8SPrarit Bhargava topo.nodes_per_pkg = pni[0].count; 2405ef605741SPrarit Bhargava 2406ef605741SPrarit Bhargava for (cpu = 0; cpu < topo.num_cpus; cpu++) { 2407ef605741SPrarit Bhargava pkg = cpus[cpu].physical_package_id; 2408ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 2409ef605741SPrarit Bhargava cpus[cpu].logical_node_id = node - pni[pkg].min; 2410ef605741SPrarit Bhargava } 2411ef605741SPrarit Bhargava free(pni); 2412ef605741SPrarit Bhargava 2413ef605741SPrarit Bhargava } 2414ef605741SPrarit Bhargava 2415ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2416c98d5d94SLen Brown { 2417c98d5d94SLen Brown char path[80]; 2418c98d5d94SLen Brown FILE *filep; 24190e2d8f05SLen Brown int i; 24200e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2421c98d5d94SLen Brown 24220e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 24230e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 24240e2d8f05SLen Brown cpu, i); 24250e2d8f05SLen Brown filep = fopen(path, "r"); 24260e2d8f05SLen Brown if (!filep) 24270e2d8f05SLen Brown continue; 24280e2d8f05SLen Brown fclose(filep); 24290e2d8f05SLen Brown return i; 24300e2d8f05SLen Brown } 24310e2d8f05SLen Brown return -1; 2432e275b388SDasaratharaman Chandramouli } 2433c98d5d94SLen Brown 24340e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 24350e2d8f05SLen Brown { 24360e2d8f05SLen Brown char path[80], character; 24370e2d8f05SLen Brown FILE *filep; 24380e2d8f05SLen Brown unsigned long map; 24398cb48b32SPrarit Bhargava int so, shift, sib_core; 24400e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 24410e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 24420e2d8f05SLen Brown size_t size; 24438cb48b32SPrarit Bhargava int thread_id = 0; 24440e2d8f05SLen Brown 24450e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 24468cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 24478cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 24480e2d8f05SLen Brown if (!thiscpu->put_ids) 24490e2d8f05SLen Brown return -1; 24500e2d8f05SLen Brown 24510e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 24520e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 24530e2d8f05SLen Brown 24540e2d8f05SLen Brown sprintf(path, 24550e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 24560e2d8f05SLen Brown filep = fopen_or_die(path, "r"); 24570e2d8f05SLen Brown do { 24580e2d8f05SLen Brown offset -= BITMASK_SIZE; 24590e2d8f05SLen Brown fscanf(filep, "%lx%c", &map, &character); 24600e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 24610e2d8f05SLen Brown if ((map >> shift) & 0x1) { 24628cb48b32SPrarit Bhargava so = shift + offset; 24638cb48b32SPrarit Bhargava sib_core = get_core_id(so); 24648cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 24658cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 24668cb48b32SPrarit Bhargava if ((so != cpu) && 24678cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 24688cb48b32SPrarit Bhargava cpus[so].thread_id = 24698cb48b32SPrarit Bhargava thread_id++; 24708cb48b32SPrarit Bhargava } 24710e2d8f05SLen Brown } 24720e2d8f05SLen Brown } 24730e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2474c98d5d94SLen Brown fclose(filep); 24750e2d8f05SLen Brown 24760e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2477c98d5d94SLen Brown } 2478c98d5d94SLen Brown 2479c98d5d94SLen Brown /* 2480c98d5d94SLen Brown * run func(thread, core, package) in topology order 2481c98d5d94SLen Brown * skip non-present cpus 2482103a8feaSLen Brown */ 2483103a8feaSLen Brown 2484c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2485c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2486c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2487c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2488c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2489c98d5d94SLen Brown struct pkg_data *pkg_base2) 2490c98d5d94SLen Brown { 2491c98d5d94SLen Brown int retval, pkg_no, core_no, thread_no; 2492c98d5d94SLen Brown 2493c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 2494*70a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 2495c98d5d94SLen Brown for (thread_no = 0; thread_no < 2496*70a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2497c98d5d94SLen Brown struct thread_data *t, *t2; 2498c98d5d94SLen Brown struct core_data *c, *c2; 2499c98d5d94SLen Brown struct pkg_data *p, *p2; 2500c98d5d94SLen Brown 2501c98d5d94SLen Brown t = GET_THREAD(thread_base, thread_no, core_no, pkg_no); 2502c98d5d94SLen Brown 2503c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2504c98d5d94SLen Brown continue; 2505c98d5d94SLen Brown 2506c98d5d94SLen Brown t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no); 2507c98d5d94SLen Brown 2508c98d5d94SLen Brown c = GET_CORE(core_base, core_no, pkg_no); 2509c98d5d94SLen Brown c2 = GET_CORE(core_base2, core_no, pkg_no); 2510c98d5d94SLen Brown 2511c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2512c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2513c98d5d94SLen Brown 2514c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2515c98d5d94SLen Brown if (retval) 2516c98d5d94SLen Brown return retval; 2517c98d5d94SLen Brown } 2518c98d5d94SLen Brown } 2519c98d5d94SLen Brown } 2520c98d5d94SLen Brown return 0; 2521c98d5d94SLen Brown } 2522c98d5d94SLen Brown 2523c98d5d94SLen Brown /* 2524c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2525c98d5d94SLen Brown * return max_cpu number 2526c98d5d94SLen Brown */ 2527c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2528103a8feaSLen Brown { 2529103a8feaSLen Brown FILE *fp; 2530c98d5d94SLen Brown int cpu_num; 2531103a8feaSLen Brown int retval; 2532103a8feaSLen Brown 253357a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2534103a8feaSLen Brown 2535103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2536b2c95d90SJosh Triplett if (retval != 0) 2537b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2538103a8feaSLen Brown 2539c98d5d94SLen Brown while (1) { 2540c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2541103a8feaSLen Brown if (retval != 1) 2542103a8feaSLen Brown break; 2543103a8feaSLen Brown 2544c98d5d94SLen Brown retval = func(cpu_num); 2545c98d5d94SLen Brown if (retval) { 2546c98d5d94SLen Brown fclose(fp); 2547c98d5d94SLen Brown return(retval); 2548c98d5d94SLen Brown } 2549103a8feaSLen Brown } 2550103a8feaSLen Brown fclose(fp); 2551c98d5d94SLen Brown return 0; 2552103a8feaSLen Brown } 2553103a8feaSLen Brown 2554103a8feaSLen Brown void re_initialize(void) 2555103a8feaSLen Brown { 2556c98d5d94SLen Brown free_all_buffers(); 2557c98d5d94SLen Brown setup_all_buffers(); 2558c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2559103a8feaSLen Brown } 2560103a8feaSLen Brown 2561843c5791SPrarit Bhargava void set_max_cpu_num(void) 2562843c5791SPrarit Bhargava { 2563843c5791SPrarit Bhargava FILE *filep; 2564843c5791SPrarit Bhargava unsigned long dummy; 2565843c5791SPrarit Bhargava 2566843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2567843c5791SPrarit Bhargava filep = fopen_or_die( 2568843c5791SPrarit Bhargava "/sys/devices/system/cpu/cpu0/topology/thread_siblings", 2569843c5791SPrarit Bhargava "r"); 2570843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 25710e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2572843c5791SPrarit Bhargava fclose(filep); 2573843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2574843c5791SPrarit Bhargava } 2575c98d5d94SLen Brown 2576103a8feaSLen Brown /* 2577c98d5d94SLen Brown * count_cpus() 2578c98d5d94SLen Brown * remember the last one seen, it will be the max 2579103a8feaSLen Brown */ 2580c98d5d94SLen Brown int count_cpus(int cpu) 2581103a8feaSLen Brown { 2582843c5791SPrarit Bhargava topo.num_cpus++; 2583c98d5d94SLen Brown return 0; 2584103a8feaSLen Brown } 2585c98d5d94SLen Brown int mark_cpu_present(int cpu) 2586c98d5d94SLen Brown { 2587c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 258815aaa346SLen Brown return 0; 2589103a8feaSLen Brown } 2590103a8feaSLen Brown 25918cb48b32SPrarit Bhargava int init_thread_id(int cpu) 25928cb48b32SPrarit Bhargava { 25938cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 25948cb48b32SPrarit Bhargava return 0; 25958cb48b32SPrarit Bhargava } 25968cb48b32SPrarit Bhargava 2597562a2d37SLen Brown /* 2598562a2d37SLen Brown * snapshot_proc_interrupts() 2599562a2d37SLen Brown * 2600562a2d37SLen Brown * read and record summary of /proc/interrupts 2601562a2d37SLen Brown * 2602562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2603562a2d37SLen Brown */ 2604562a2d37SLen Brown int snapshot_proc_interrupts(void) 2605562a2d37SLen Brown { 2606562a2d37SLen Brown static FILE *fp; 2607562a2d37SLen Brown int column, retval; 2608562a2d37SLen Brown 2609562a2d37SLen Brown if (fp == NULL) 2610562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2611562a2d37SLen Brown else 2612562a2d37SLen Brown rewind(fp); 2613562a2d37SLen Brown 2614562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2615562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2616562a2d37SLen Brown int cpu_number; 2617562a2d37SLen Brown 2618562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2619562a2d37SLen Brown if (retval != 1) 2620562a2d37SLen Brown break; 2621562a2d37SLen Brown 2622562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2623562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2624562a2d37SLen Brown return 1; 2625562a2d37SLen Brown } 2626562a2d37SLen Brown 2627562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2628562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2629562a2d37SLen Brown } 2630562a2d37SLen Brown 2631562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2632562a2d37SLen Brown while (1) { 2633562a2d37SLen Brown int column; 2634562a2d37SLen Brown char buf[64]; 2635562a2d37SLen Brown 2636562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2637562a2d37SLen Brown if (retval != 1) 2638562a2d37SLen Brown break; 2639562a2d37SLen Brown 2640562a2d37SLen Brown /* read the count per cpu */ 2641562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2642562a2d37SLen Brown 2643562a2d37SLen Brown int cpu_number, irq_count; 2644562a2d37SLen Brown 2645562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2646562a2d37SLen Brown if (retval != 1) 2647562a2d37SLen Brown break; 2648562a2d37SLen Brown 2649562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2650562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2651562a2d37SLen Brown 2652562a2d37SLen Brown } 2653562a2d37SLen Brown 2654562a2d37SLen Brown while (getc(fp) != '\n') 2655562a2d37SLen Brown ; /* flush interrupt description */ 2656562a2d37SLen Brown 2657562a2d37SLen Brown } 2658562a2d37SLen Brown return 0; 2659562a2d37SLen Brown } 266027d47356SLen Brown /* 2661fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2662fdf676e5SLen Brown * 2663fdf676e5SLen Brown * record snapshot of 2664fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2665fdf676e5SLen Brown * 2666fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2667fdf676e5SLen Brown */ 2668fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2669fdf676e5SLen Brown { 2670fdf676e5SLen Brown FILE *fp; 2671fdf676e5SLen Brown int retval; 2672fdf676e5SLen Brown 2673fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 2674fdf676e5SLen Brown 2675fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 2676fdf676e5SLen Brown if (retval != 1) 2677fdf676e5SLen Brown err(1, "GFX rc6"); 2678fdf676e5SLen Brown 2679fdf676e5SLen Brown fclose(fp); 2680fdf676e5SLen Brown 2681fdf676e5SLen Brown return 0; 2682fdf676e5SLen Brown } 2683fdf676e5SLen Brown /* 268427d47356SLen Brown * snapshot_gfx_mhz() 268527d47356SLen Brown * 268627d47356SLen Brown * record snapshot of 268727d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 268827d47356SLen Brown * 268927d47356SLen Brown * return 1 if config change requires a restart, else return 0 269027d47356SLen Brown */ 269127d47356SLen Brown int snapshot_gfx_mhz(void) 269227d47356SLen Brown { 269327d47356SLen Brown static FILE *fp; 269427d47356SLen Brown int retval; 269527d47356SLen Brown 269627d47356SLen Brown if (fp == NULL) 269727d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 269822048c54SLen Brown else { 269927d47356SLen Brown rewind(fp); 270022048c54SLen Brown fflush(fp); 270122048c54SLen Brown } 270227d47356SLen Brown 270327d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 270427d47356SLen Brown if (retval != 1) 270527d47356SLen Brown err(1, "GFX MHz"); 270627d47356SLen Brown 270727d47356SLen Brown return 0; 270827d47356SLen Brown } 2709562a2d37SLen Brown 2710562a2d37SLen Brown /* 2711be0e54c4SLen Brown * snapshot_cpu_lpi() 2712be0e54c4SLen Brown * 2713be0e54c4SLen Brown * record snapshot of 2714be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 2715be0e54c4SLen Brown * 2716be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2717be0e54c4SLen Brown */ 2718be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 2719be0e54c4SLen Brown { 2720be0e54c4SLen Brown FILE *fp; 2721be0e54c4SLen Brown int retval; 2722be0e54c4SLen Brown 2723be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 2724be0e54c4SLen Brown 2725be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 2726be0e54c4SLen Brown if (retval != 1) 2727be0e54c4SLen Brown err(1, "CPU LPI"); 2728be0e54c4SLen Brown 2729be0e54c4SLen Brown fclose(fp); 2730be0e54c4SLen Brown 2731be0e54c4SLen Brown return 0; 2732be0e54c4SLen Brown } 2733be0e54c4SLen Brown /* 2734be0e54c4SLen Brown * snapshot_sys_lpi() 2735be0e54c4SLen Brown * 2736be0e54c4SLen Brown * record snapshot of 2737be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us 2738be0e54c4SLen Brown * 2739be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2740be0e54c4SLen Brown */ 2741be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 2742be0e54c4SLen Brown { 2743be0e54c4SLen Brown FILE *fp; 2744be0e54c4SLen Brown int retval; 2745be0e54c4SLen Brown 2746be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r"); 2747be0e54c4SLen Brown 2748be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 2749be0e54c4SLen Brown if (retval != 1) 2750be0e54c4SLen Brown err(1, "SYS LPI"); 2751be0e54c4SLen Brown 2752be0e54c4SLen Brown fclose(fp); 2753be0e54c4SLen Brown 2754be0e54c4SLen Brown return 0; 2755be0e54c4SLen Brown } 2756be0e54c4SLen Brown /* 2757562a2d37SLen Brown * snapshot /proc and /sys files 2758562a2d37SLen Brown * 2759562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 2760562a2d37SLen Brown */ 2761562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 2762562a2d37SLen Brown { 2763218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 2764562a2d37SLen Brown if (snapshot_proc_interrupts()) 2765562a2d37SLen Brown return 1; 2766562a2d37SLen Brown 2767812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2768fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 2769fdf676e5SLen Brown 2770812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 277127d47356SLen Brown snapshot_gfx_mhz(); 277227d47356SLen Brown 2773be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2774be0e54c4SLen Brown snapshot_cpu_lpi_us(); 2775be0e54c4SLen Brown 2776be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2777be0e54c4SLen Brown snapshot_sys_lpi_us(); 2778be0e54c4SLen Brown 2779562a2d37SLen Brown return 0; 2780562a2d37SLen Brown } 2781562a2d37SLen Brown 27828aa2ed0bSLen Brown int exit_requested; 27838aa2ed0bSLen Brown 27848aa2ed0bSLen Brown static void signal_handler (int signal) 27858aa2ed0bSLen Brown { 27868aa2ed0bSLen Brown switch (signal) { 27878aa2ed0bSLen Brown case SIGINT: 27888aa2ed0bSLen Brown exit_requested = 1; 27898aa2ed0bSLen Brown if (debug) 27908aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 27918aa2ed0bSLen Brown break; 279207211960SLen Brown case SIGUSR1: 279307211960SLen Brown if (debug > 1) 279407211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 279507211960SLen Brown break; 27968aa2ed0bSLen Brown } 2797b9ad8ee0SLen Brown /* make sure this manually-invoked interval is at least 1ms long */ 2798b9ad8ee0SLen Brown nanosleep(&one_msec, NULL); 27998aa2ed0bSLen Brown } 28008aa2ed0bSLen Brown 28018aa2ed0bSLen Brown void setup_signal_handler(void) 28028aa2ed0bSLen Brown { 28038aa2ed0bSLen Brown struct sigaction sa; 28048aa2ed0bSLen Brown 28058aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 28068aa2ed0bSLen Brown 28078aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 28088aa2ed0bSLen Brown 28098aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 28108aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 281107211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 281207211960SLen Brown err(1, "sigaction SIGUSR1"); 28138aa2ed0bSLen Brown } 2814b9ad8ee0SLen Brown 281547936f94SArtem Bityutskiy void do_sleep(void) 2816b9ad8ee0SLen Brown { 2817b9ad8ee0SLen Brown struct timeval select_timeout; 2818b9ad8ee0SLen Brown fd_set readfds; 2819b9ad8ee0SLen Brown int retval; 2820b9ad8ee0SLen Brown 2821b9ad8ee0SLen Brown FD_ZERO(&readfds); 2822b9ad8ee0SLen Brown FD_SET(0, &readfds); 2823b9ad8ee0SLen Brown 282447936f94SArtem Bityutskiy if (!isatty(fileno(stdin))) { 282547936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 282647936f94SArtem Bityutskiy return; 282747936f94SArtem Bityutskiy } 2828b9ad8ee0SLen Brown 282947936f94SArtem Bityutskiy select_timeout = interval_tv; 2830b9ad8ee0SLen Brown retval = select(1, &readfds, NULL, NULL, &select_timeout); 2831b9ad8ee0SLen Brown 2832b9ad8ee0SLen Brown if (retval == 1) { 2833b9ad8ee0SLen Brown switch (getc(stdin)) { 2834b9ad8ee0SLen Brown case 'q': 2835b9ad8ee0SLen Brown exit_requested = 1; 2836b9ad8ee0SLen Brown break; 2837b9ad8ee0SLen Brown } 2838b9ad8ee0SLen Brown /* make sure this manually-invoked interval is at least 1ms long */ 2839b9ad8ee0SLen Brown nanosleep(&one_msec, NULL); 2840b9ad8ee0SLen Brown } 2841b9ad8ee0SLen Brown } 284247936f94SArtem Bityutskiy 2843103a8feaSLen Brown void turbostat_loop() 2844103a8feaSLen Brown { 2845c98d5d94SLen Brown int retval; 2846e52966c0SLen Brown int restarted = 0; 2847023fe0acSChen Yu int done_iters = 0; 2848c98d5d94SLen Brown 28498aa2ed0bSLen Brown setup_signal_handler(); 28508aa2ed0bSLen Brown 2851103a8feaSLen Brown restart: 2852e52966c0SLen Brown restarted++; 2853e52966c0SLen Brown 2854562a2d37SLen Brown snapshot_proc_sysfs_files(); 2855c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2856d91bb17cSLen Brown if (retval < -1) { 2857d91bb17cSLen Brown exit(retval); 2858d91bb17cSLen Brown } else if (retval == -1) { 2859e52966c0SLen Brown if (restarted > 1) { 2860e52966c0SLen Brown exit(retval); 2861e52966c0SLen Brown } 2862c98d5d94SLen Brown re_initialize(); 2863c98d5d94SLen Brown goto restart; 2864c98d5d94SLen Brown } 2865e52966c0SLen Brown restarted = 0; 2866023fe0acSChen Yu done_iters = 0; 2867103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2868103a8feaSLen Brown 2869103a8feaSLen Brown while (1) { 2870c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 2871103a8feaSLen Brown re_initialize(); 2872103a8feaSLen Brown goto restart; 2873103a8feaSLen Brown } 2874b9ad8ee0SLen Brown do_sleep(); 2875562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2876562a2d37SLen Brown goto restart; 2877c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 2878d91bb17cSLen Brown if (retval < -1) { 2879d91bb17cSLen Brown exit(retval); 2880d91bb17cSLen Brown } else if (retval == -1) { 288115aaa346SLen Brown re_initialize(); 288215aaa346SLen Brown goto restart; 288315aaa346SLen Brown } 2884103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2885103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2886ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 2887ba3dec99SLen Brown re_initialize(); 2888ba3dec99SLen Brown goto restart; 2889ba3dec99SLen Brown } 2890c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 2891c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 2892b7d8c148SLen Brown flush_output_stdout(); 28938aa2ed0bSLen Brown if (exit_requested) 28948aa2ed0bSLen Brown break; 2895023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 2896023fe0acSChen Yu break; 2897b9ad8ee0SLen Brown do_sleep(); 2898562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2899562a2d37SLen Brown goto restart; 2900c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 2901d91bb17cSLen Brown if (retval < -1) { 2902d91bb17cSLen Brown exit(retval); 2903d91bb17cSLen Brown } else if (retval == -1) { 2904103a8feaSLen Brown re_initialize(); 2905103a8feaSLen Brown goto restart; 2906103a8feaSLen Brown } 2907103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2908103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 2909ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 2910ba3dec99SLen Brown re_initialize(); 2911ba3dec99SLen Brown goto restart; 2912ba3dec99SLen Brown } 2913c98d5d94SLen Brown compute_average(ODD_COUNTERS); 2914c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 2915b7d8c148SLen Brown flush_output_stdout(); 29168aa2ed0bSLen Brown if (exit_requested) 29178aa2ed0bSLen Brown break; 2918023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 2919023fe0acSChen Yu break; 2920103a8feaSLen Brown } 2921103a8feaSLen Brown } 2922103a8feaSLen Brown 2923103a8feaSLen Brown void check_dev_msr() 2924103a8feaSLen Brown { 2925103a8feaSLen Brown struct stat sb; 29267ce7d5deSPrarit Bhargava char pathname[32]; 2927103a8feaSLen Brown 29287ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 29297ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 2930a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 2931d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 2932103a8feaSLen Brown } 2933103a8feaSLen Brown 293498481e79SLen Brown void check_permissions() 2935103a8feaSLen Brown { 293698481e79SLen Brown struct __user_cap_header_struct cap_header_data; 293798481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 293898481e79SLen Brown struct __user_cap_data_struct cap_data_data; 293998481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 294098481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 294198481e79SLen Brown int do_exit = 0; 29427ce7d5deSPrarit Bhargava char pathname[32]; 294398481e79SLen Brown 294498481e79SLen Brown /* check for CAP_SYS_RAWIO */ 294598481e79SLen Brown cap_header->pid = getpid(); 294698481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 294798481e79SLen Brown if (capget(cap_header, cap_data) < 0) 294898481e79SLen Brown err(-6, "capget(2) failed"); 294998481e79SLen Brown 295098481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 295198481e79SLen Brown do_exit++; 295298481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 295398481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 295498481e79SLen Brown } 295598481e79SLen Brown 295698481e79SLen Brown /* test file permissions */ 29577ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 29587ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 295998481e79SLen Brown do_exit++; 296098481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 296198481e79SLen Brown } 296298481e79SLen Brown 296398481e79SLen Brown /* if all else fails, thell them to be root */ 296498481e79SLen Brown if (do_exit) 2965b2c95d90SJosh Triplett if (getuid() != 0) 2966d7899447SLen Brown warnx("... or simply run as root"); 296798481e79SLen Brown 296898481e79SLen Brown if (do_exit) 296998481e79SLen Brown exit(-6); 2970103a8feaSLen Brown } 2971103a8feaSLen Brown 2972d7899447SLen Brown /* 2973d7899447SLen Brown * NHM adds support for additional MSRs: 2974d7899447SLen Brown * 2975d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 2976d7899447SLen Brown * 2977ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 29781df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 2979d7899447SLen Brown * 2980cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 2981cf4cbe53SLen Brown * 2982d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 2983d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 2984d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 2985d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 2986d7899447SLen Brown * 2987ee7e38e3SLen Brown * Side effect: 29881df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 298933148d67SLen Brown * sets has_misc_feature_control 2990d7899447SLen Brown */ 2991ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 2992103a8feaSLen Brown { 2993ee7e38e3SLen Brown unsigned long long msr; 299421ed5574SLen Brown unsigned int base_ratio; 2995ee7e38e3SLen Brown int *pkg_cstate_limits; 2996ee7e38e3SLen Brown 2997103a8feaSLen Brown if (!genuine_intel) 2998103a8feaSLen Brown return 0; 2999103a8feaSLen Brown 3000103a8feaSLen Brown if (family != 6) 3001103a8feaSLen Brown return 0; 3002103a8feaSLen Brown 300321ed5574SLen Brown bclk = discover_bclk(family, model); 300421ed5574SLen Brown 3005103a8feaSLen Brown switch (model) { 3006869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 3007869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3008103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 3009869ce69eSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 3010869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 3011869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3012869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 3013ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3014ee7e38e3SLen Brown break; 3015869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3016869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3017869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3018869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3019ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 302033148d67SLen Brown has_misc_feature_control = 1; 3021ee7e38e3SLen Brown break; 3022869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3023869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3024869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3025869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3026869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3027869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3028869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3029869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3030869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3031869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3032869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3033869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3034997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3035ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 303633148d67SLen Brown has_misc_feature_control = 1; 3037ee7e38e3SLen Brown break; 3038d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3039d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 304033148d67SLen Brown has_misc_feature_control = 1; 3041d8ebb442SLen Brown break; 3042869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 3043cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3044869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 3045ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3046ee7e38e3SLen Brown break; 3047869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3048ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3049cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3050ee7e38e3SLen Brown break; 3051869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3052005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3053ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3054ee7e38e3SLen Brown break; 3055869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3056ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 3057869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 3058e4085d54SLen Brown pkg_cstate_limits = bxt_pkg_cstate_limits; 3059e4085d54SLen Brown break; 3060103a8feaSLen Brown default: 3061103a8feaSLen Brown return 0; 3062103a8feaSLen Brown } 30631df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3064e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3065ee7e38e3SLen Brown 3066ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 306721ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 306821ed5574SLen Brown 306921ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 307021ed5574SLen Brown has_base_hz = 1; 3071ee7e38e3SLen Brown return 1; 3072103a8feaSLen Brown } 30730f7887c4SLen Brown /* 3074495c7654SLen Brown * SLV client has support for unique MSRs: 30750f7887c4SLen Brown * 30760f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 30770f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 30780f7887c4SLen Brown */ 30790f7887c4SLen Brown 30800f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 30810f7887c4SLen Brown { 30820f7887c4SLen Brown if (!genuine_intel) 30830f7887c4SLen Brown return 0; 30840f7887c4SLen Brown 30850f7887c4SLen Brown switch (model) { 30860f7887c4SLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: 30870f7887c4SLen Brown case INTEL_FAM6_ATOM_MERRIFIELD: 30880f7887c4SLen Brown case INTEL_FAM6_ATOM_MOOREFIELD: 30890f7887c4SLen Brown return 1; 30900f7887c4SLen Brown } 30910f7887c4SLen Brown return 0; 30920f7887c4SLen Brown } 30937170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 30947170a374SLen Brown { 30957170a374SLen Brown 30967170a374SLen Brown if (!genuine_intel) 30977170a374SLen Brown return 0; 30987170a374SLen Brown 30997170a374SLen Brown switch (model) { 31007170a374SLen Brown case INTEL_FAM6_ATOM_DENVERTON: 31017170a374SLen Brown return 1; 31027170a374SLen Brown } 31037170a374SLen Brown return 0; 31047170a374SLen Brown } 3105ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3106ade0ebacSLen Brown { 3107ade0ebacSLen Brown 3108ade0ebacSLen Brown if (!genuine_intel) 3109ade0ebacSLen Brown return 0; 3110ade0ebacSLen Brown 3111ade0ebacSLen Brown switch (model) { 3112ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3113ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: 3114ade0ebacSLen Brown return 1; 3115ade0ebacSLen Brown } 3116ade0ebacSLen Brown return 0; 3117ade0ebacSLen Brown } 311834c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 311934c76197SLen Brown { 312034c76197SLen Brown 312134c76197SLen Brown if (!genuine_intel) 312234c76197SLen Brown return 0; 312334c76197SLen Brown 312434c76197SLen Brown switch (model) { 312534c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 312634c76197SLen Brown return 1; 312734c76197SLen Brown } 312834c76197SLen Brown return 0; 312934c76197SLen Brown } 31300f7887c4SLen Brown 313131e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3132d7899447SLen Brown { 31330f7887c4SLen Brown if (has_slv_msrs(family, model)) 31340f7887c4SLen Brown return 0; 31350f7887c4SLen Brown 3136d7899447SLen Brown switch (model) { 3137d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3138869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3139869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 3140869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3141005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3142d7899447SLen Brown return 0; 3143d7899447SLen Brown default: 3144d7899447SLen Brown return 1; 3145d7899447SLen Brown } 3146d7899447SLen Brown } 31470f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 31480f7887c4SLen Brown { 31490f7887c4SLen Brown if (has_slv_msrs(family, model)) 31500f7887c4SLen Brown return 1; 31510f7887c4SLen Brown 31520f7887c4SLen Brown return 0; 31530f7887c4SLen Brown } 31546574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 31556574a5d5SLen Brown { 31566574a5d5SLen Brown if (!genuine_intel) 31576574a5d5SLen Brown return 0; 31586574a5d5SLen Brown 31596574a5d5SLen Brown if (family != 6) 31606574a5d5SLen Brown return 0; 31616574a5d5SLen Brown 31626574a5d5SLen Brown switch (model) { 3163869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3164869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 31656574a5d5SLen Brown return 1; 31666574a5d5SLen Brown default: 31676574a5d5SLen Brown return 0; 31686574a5d5SLen Brown } 31696574a5d5SLen Brown } 3170fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3171fcd17211SLen Brown { 3172fcd17211SLen Brown if (!genuine_intel) 3173fcd17211SLen Brown return 0; 3174fcd17211SLen Brown 3175fcd17211SLen Brown if (family != 6) 3176fcd17211SLen Brown return 0; 3177fcd17211SLen Brown 3178fcd17211SLen Brown switch (model) { 3179869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3180fcd17211SLen Brown return 1; 3181fcd17211SLen Brown default: 3182fcd17211SLen Brown return 0; 3183fcd17211SLen Brown } 3184fcd17211SLen Brown } 3185fcd17211SLen Brown 3186fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3187fb5d4327SDasaratharaman Chandramouli { 3188fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3189fb5d4327SDasaratharaman Chandramouli return 0; 3190fb5d4327SDasaratharaman Chandramouli 3191fb5d4327SDasaratharaman Chandramouli if (family != 6) 3192fb5d4327SDasaratharaman Chandramouli return 0; 3193fb5d4327SDasaratharaman Chandramouli 3194fb5d4327SDasaratharaman Chandramouli switch (model) { 3195869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3196005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3197fb5d4327SDasaratharaman Chandramouli return 1; 3198fb5d4327SDasaratharaman Chandramouli default: 3199fb5d4327SDasaratharaman Chandramouli return 0; 3200fb5d4327SDasaratharaman Chandramouli } 3201fb5d4327SDasaratharaman Chandramouli } 320231e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 320331e07522SLen Brown { 320431e07522SLen Brown if (!genuine_intel) 320531e07522SLen Brown return 0; 320631e07522SLen Brown 320731e07522SLen Brown if (family != 6) 320831e07522SLen Brown return 0; 320931e07522SLen Brown 321031e07522SLen Brown switch (model) { 321131e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 321231e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 321331e07522SLen Brown return 1; 321431e07522SLen Brown default: 321531e07522SLen Brown return 0; 321631e07522SLen Brown } 321731e07522SLen Brown } 32186fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 32196fb3143bSLen Brown { 32206fb3143bSLen Brown if (!genuine_intel) 32216fb3143bSLen Brown return 0; 32226fb3143bSLen Brown 32236fb3143bSLen Brown if (family != 6) 32246fb3143bSLen Brown return 0; 32256fb3143bSLen Brown 32266fb3143bSLen Brown switch (model) { 3227869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3228869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3229869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3230869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3231869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3232869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3233869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3234869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3235869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3236869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3237869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3238869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3239869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3240997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3241869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 32426fb3143bSLen Brown 3243869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3244005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 32456fb3143bSLen Brown return 1; 32466fb3143bSLen Brown default: 32476fb3143bSLen Brown return 0; 32486fb3143bSLen Brown } 32496fb3143bSLen Brown } 32506fb3143bSLen Brown 3251fcd17211SLen Brown static void 32521b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3253fcd17211SLen Brown { 3254fcd17211SLen Brown if (!do_nhm_platform_info) 3255fcd17211SLen Brown return; 3256fcd17211SLen Brown 3257fcd17211SLen Brown dump_nhm_platform_info(); 3258fcd17211SLen Brown 3259fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3260fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3261fcd17211SLen Brown 3262fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3263fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3264fcd17211SLen Brown 326531e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 326631e07522SLen Brown dump_turbo_ratio_limits(family, model); 3267fcd17211SLen Brown 32680f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 32690f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 32700f7887c4SLen Brown 3271fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3272fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3273fb5d4327SDasaratharaman Chandramouli 32746fb3143bSLen Brown if (has_config_tdp(family, model)) 32756fb3143bSLen Brown dump_config_tdp(); 32766fb3143bSLen Brown 3277fcd17211SLen Brown dump_nhm_cst_cfg(); 3278fcd17211SLen Brown } 3279fcd17211SLen Brown 328041618e63SLen Brown static void 328141618e63SLen Brown dump_sysfs_cstate_config(void) 328241618e63SLen Brown { 328341618e63SLen Brown char path[64]; 328441618e63SLen Brown char name_buf[16]; 328541618e63SLen Brown char desc[64]; 328641618e63SLen Brown FILE *input; 328741618e63SLen Brown int state; 328841618e63SLen Brown char *sp; 328941618e63SLen Brown 329041618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 329141618e63SLen Brown return; 329241618e63SLen Brown 329341618e63SLen Brown for (state = 0; state < 10; ++state) { 329441618e63SLen Brown 329541618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 329641618e63SLen Brown base_cpu, state); 329741618e63SLen Brown input = fopen(path, "r"); 329841618e63SLen Brown if (input == NULL) 329941618e63SLen Brown continue; 330041618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 330141618e63SLen Brown 330241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 330341618e63SLen Brown sp = strchr(name_buf, '-'); 330441618e63SLen Brown if (!sp) 330541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 330641618e63SLen Brown *sp = '\0'; 330741618e63SLen Brown 330841618e63SLen Brown fclose(input); 330941618e63SLen Brown 331041618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 331141618e63SLen Brown base_cpu, state); 331241618e63SLen Brown input = fopen(path, "r"); 331341618e63SLen Brown if (input == NULL) 331441618e63SLen Brown continue; 331541618e63SLen Brown fgets(desc, sizeof(desc), input); 331641618e63SLen Brown 331741618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 331841618e63SLen Brown fclose(input); 331941618e63SLen Brown } 332041618e63SLen Brown } 33217293fccdSLen Brown static void 33227293fccdSLen Brown dump_sysfs_pstate_config(void) 33237293fccdSLen Brown { 33247293fccdSLen Brown char path[64]; 33257293fccdSLen Brown char driver_buf[64]; 33267293fccdSLen Brown char governor_buf[64]; 33277293fccdSLen Brown FILE *input; 33287293fccdSLen Brown int turbo; 33297293fccdSLen Brown 33307293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 33317293fccdSLen Brown base_cpu); 33327293fccdSLen Brown input = fopen(path, "r"); 33337293fccdSLen Brown if (input == NULL) { 33347293fccdSLen Brown fprintf(stderr, "NSFOD %s\n", path); 33357293fccdSLen Brown return; 33367293fccdSLen Brown } 33377293fccdSLen Brown fgets(driver_buf, sizeof(driver_buf), input); 33387293fccdSLen Brown fclose(input); 33397293fccdSLen Brown 33407293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 33417293fccdSLen Brown base_cpu); 33427293fccdSLen Brown input = fopen(path, "r"); 33437293fccdSLen Brown if (input == NULL) { 33447293fccdSLen Brown fprintf(stderr, "NSFOD %s\n", path); 33457293fccdSLen Brown return; 33467293fccdSLen Brown } 33477293fccdSLen Brown fgets(governor_buf, sizeof(governor_buf), input); 33487293fccdSLen Brown fclose(input); 33497293fccdSLen Brown 33507293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 33517293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 33527293fccdSLen Brown 33537293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 33547293fccdSLen Brown input = fopen(path, "r"); 33557293fccdSLen Brown if (input != NULL) { 33567293fccdSLen Brown fscanf(input, "%d", &turbo); 33577293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 33587293fccdSLen Brown fclose(input); 33597293fccdSLen Brown } 33607293fccdSLen Brown 33617293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 33627293fccdSLen Brown input = fopen(path, "r"); 33637293fccdSLen Brown if (input != NULL) { 33647293fccdSLen Brown fscanf(input, "%d", &turbo); 33657293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 33667293fccdSLen Brown fclose(input); 33677293fccdSLen Brown } 33687293fccdSLen Brown } 336941618e63SLen Brown 33706574a5d5SLen Brown 3371889facbeSLen Brown /* 3372889facbeSLen Brown * print_epb() 3373889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3374889facbeSLen Brown */ 3375889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3376889facbeSLen Brown { 3377889facbeSLen Brown unsigned long long msr; 3378889facbeSLen Brown char *epb_string; 3379889facbeSLen Brown int cpu; 3380889facbeSLen Brown 3381889facbeSLen Brown if (!has_epb) 3382889facbeSLen Brown return 0; 3383889facbeSLen Brown 3384889facbeSLen Brown cpu = t->cpu_id; 3385889facbeSLen Brown 3386889facbeSLen Brown /* EPB is per-package */ 3387889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3388889facbeSLen Brown return 0; 3389889facbeSLen Brown 3390889facbeSLen Brown if (cpu_migrate(cpu)) { 3391b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3392889facbeSLen Brown return -1; 3393889facbeSLen Brown } 3394889facbeSLen Brown 3395889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 3396889facbeSLen Brown return 0; 3397889facbeSLen Brown 3398e9be7dd6SLen Brown switch (msr & 0xF) { 3399889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3400889facbeSLen Brown epb_string = "performance"; 3401889facbeSLen Brown break; 3402889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3403889facbeSLen Brown epb_string = "balanced"; 3404889facbeSLen Brown break; 3405889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3406889facbeSLen Brown epb_string = "powersave"; 3407889facbeSLen Brown break; 3408889facbeSLen Brown default: 3409889facbeSLen Brown epb_string = "custom"; 3410889facbeSLen Brown break; 3411889facbeSLen Brown } 3412b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 3413889facbeSLen Brown 3414889facbeSLen Brown return 0; 3415889facbeSLen Brown } 34167f5c258eSLen Brown /* 34177f5c258eSLen Brown * print_hwp() 34187f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 34197f5c258eSLen Brown */ 34207f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 34217f5c258eSLen Brown { 34227f5c258eSLen Brown unsigned long long msr; 34237f5c258eSLen Brown int cpu; 34247f5c258eSLen Brown 34257f5c258eSLen Brown if (!has_hwp) 34267f5c258eSLen Brown return 0; 34277f5c258eSLen Brown 34287f5c258eSLen Brown cpu = t->cpu_id; 34297f5c258eSLen Brown 34307f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 34317f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 34327f5c258eSLen Brown return 0; 34337f5c258eSLen Brown 34347f5c258eSLen Brown if (cpu_migrate(cpu)) { 3435b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 34367f5c258eSLen Brown return -1; 34377f5c258eSLen Brown } 34387f5c258eSLen Brown 34397f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 34407f5c258eSLen Brown return 0; 34417f5c258eSLen Brown 3442b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 34437f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 34447f5c258eSLen Brown 34457f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 34467f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 34477f5c258eSLen Brown return 0; 34487f5c258eSLen Brown 34497f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 34507f5c258eSLen Brown return 0; 34517f5c258eSLen Brown 3452b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 34536dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 34547f5c258eSLen Brown cpu, msr, 34557f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 34567f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 34577f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 34587f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 34597f5c258eSLen Brown 34607f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 34617f5c258eSLen Brown return 0; 34627f5c258eSLen Brown 3463b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 34646dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 34657f5c258eSLen Brown cpu, msr, 34667f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 34677f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 34687f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 34697f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 34707f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 34717f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 34727f5c258eSLen Brown 34737f5c258eSLen Brown if (has_hwp_pkg) { 34747f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 34757f5c258eSLen Brown return 0; 34767f5c258eSLen Brown 3477b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 34786dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 34797f5c258eSLen Brown cpu, msr, 34807f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 34817f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 34827f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 34837f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 34847f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 34857f5c258eSLen Brown } 34867f5c258eSLen Brown if (has_hwp_notify) { 34877f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 34887f5c258eSLen Brown return 0; 34897f5c258eSLen Brown 3490b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 34917f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 34927f5c258eSLen Brown cpu, msr, 34937f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 34947f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 34957f5c258eSLen Brown } 34967f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 34977f5c258eSLen Brown return 0; 34987f5c258eSLen Brown 3499b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 35007f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 35017f5c258eSLen Brown cpu, msr, 35027f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 35037f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 3504889facbeSLen Brown 3505889facbeSLen Brown return 0; 3506889facbeSLen Brown } 3507889facbeSLen Brown 35083a9a941dSLen Brown /* 35093a9a941dSLen Brown * print_perf_limit() 35103a9a941dSLen Brown */ 35113a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 35123a9a941dSLen Brown { 35133a9a941dSLen Brown unsigned long long msr; 35143a9a941dSLen Brown int cpu; 35153a9a941dSLen Brown 35163a9a941dSLen Brown cpu = t->cpu_id; 35173a9a941dSLen Brown 35183a9a941dSLen Brown /* per-package */ 35193a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 35203a9a941dSLen Brown return 0; 35213a9a941dSLen Brown 35223a9a941dSLen Brown if (cpu_migrate(cpu)) { 3523b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 35243a9a941dSLen Brown return -1; 35253a9a941dSLen Brown } 35263a9a941dSLen Brown 35273a9a941dSLen Brown if (do_core_perf_limit_reasons) { 35283a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 3529b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3530b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 3531e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 35323a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 3533e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 3534e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 3535e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 3536e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 3537e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 3538e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 3539e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 3540e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 3541e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 3542e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 3543e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 3544e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 3545b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 3546e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 35473a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 3548e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 3549e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 3550e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 3551e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 3552e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 3553e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 3554e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 3555e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 3556e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 3557e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 3558e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 3559e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 35603a9a941dSLen Brown 35613a9a941dSLen Brown } 35623a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 35633a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 3564b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3565b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 35663a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 35673a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 35683a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 35693a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 35703a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 35713a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 35723a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 35733a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3574b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 35753a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 35763a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 35773a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 35783a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 35793a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 35803a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 35813a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 35823a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 35833a9a941dSLen Brown } 35843a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 35853a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 3586b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3587b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 35883a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 35893a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 35903a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 35913a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 35923a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 35933a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3594b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 35953a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 35963a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 35973a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 35983a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 35993a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 36003a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 36013a9a941dSLen Brown } 36023a9a941dSLen Brown return 0; 36033a9a941dSLen Brown } 36043a9a941dSLen Brown 3605889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 3606889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 3607889facbeSLen Brown 36081b69317dSColin Ian King double get_tdp(unsigned int model) 3609144b44b1SLen Brown { 3610144b44b1SLen Brown unsigned long long msr; 3611144b44b1SLen Brown 3612144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 36137ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 3614144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 3615144b44b1SLen Brown 3616144b44b1SLen Brown switch (model) { 3617869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: 3618869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: 3619144b44b1SLen Brown return 30.0; 3620144b44b1SLen Brown default: 3621144b44b1SLen Brown return 135.0; 3622144b44b1SLen Brown } 3623144b44b1SLen Brown } 3624144b44b1SLen Brown 362540ee8e3bSAndrey Semin /* 362640ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 362740ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 362840ee8e3bSAndrey Semin */ 362940ee8e3bSAndrey Semin static double 363040ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 363140ee8e3bSAndrey Semin { 363240ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 363340ee8e3bSAndrey Semin 363440ee8e3bSAndrey Semin switch (model) { 3635869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3636869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3637869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3638869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3639005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 364040ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 364140ee8e3bSAndrey Semin default: 364240ee8e3bSAndrey Semin return (rapl_energy_units); 364340ee8e3bSAndrey Semin } 364440ee8e3bSAndrey Semin } 364540ee8e3bSAndrey Semin 3646144b44b1SLen Brown 3647889facbeSLen Brown /* 3648889facbeSLen Brown * rapl_probe() 3649889facbeSLen Brown * 3650144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 3651889facbeSLen Brown */ 3652889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 3653889facbeSLen Brown { 3654889facbeSLen Brown unsigned long long msr; 3655144b44b1SLen Brown unsigned int time_unit; 3656889facbeSLen Brown double tdp; 3657889facbeSLen Brown 3658889facbeSLen Brown if (!genuine_intel) 3659889facbeSLen Brown return; 3660889facbeSLen Brown 3661889facbeSLen Brown if (family != 6) 3662889facbeSLen Brown return; 3663889facbeSLen Brown 3664889facbeSLen Brown switch (model) { 3665869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3666869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 3667869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3668869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3669869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3670869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3671869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3672144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 3673812db3f7SLen Brown if (rapl_joules) { 3674812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3675812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3676812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 3677812db3f7SLen Brown } else { 3678812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3679812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3680812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 3681812db3f7SLen Brown } 3682889facbeSLen Brown break; 3683869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3684ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 3685e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 3686812db3f7SLen Brown if (rapl_joules) 3687812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3688812db3f7SLen Brown else 3689812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3690e4085d54SLen Brown break; 3691869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3692869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3693869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3694869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3695997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 369681824921SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO; 3697812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3698812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3699812db3f7SLen Brown if (rapl_joules) { 3700812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3701812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3702812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 370381824921SLen Brown BIC_PRESENT(BIC_GFX_J); 3704812db3f7SLen Brown } else { 3705812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3706812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3707812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 370881824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 3709812db3f7SLen Brown } 37100b2bb692SLen Brown break; 3711869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3712869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3713869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3714869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3715869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3716005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 37170b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 3718812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3719812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3720812db3f7SLen Brown if (rapl_joules) { 3721812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3722812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3723812db3f7SLen Brown } else { 3724812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3725812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3726812db3f7SLen Brown } 3727e6f9bb3cSLen Brown break; 3728869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3729869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 37300b2bb692SLen 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; 3731812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3732812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3733812db3f7SLen Brown if (rapl_joules) { 3734812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3735812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3736812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3737812db3f7SLen Brown } else { 3738812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3739812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3740812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3741812db3f7SLen Brown } 3742144b44b1SLen Brown break; 3743869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 3744869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 3745144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 3746812db3f7SLen Brown if (rapl_joules) { 3747812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3748812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3749812db3f7SLen Brown } else { 3750812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3751812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3752812db3f7SLen Brown } 3753889facbeSLen Brown break; 3754869ce69eSLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 37550f644909SJacob 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; 3756812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3757812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3758812db3f7SLen Brown if (rapl_joules) { 3759812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3760812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3761812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3762812db3f7SLen Brown } else { 3763812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3764812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3765812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3766812db3f7SLen Brown } 37670f644909SJacob Pan break; 3768889facbeSLen Brown default: 3769889facbeSLen Brown return; 3770889facbeSLen Brown } 3771889facbeSLen Brown 3772889facbeSLen Brown /* units on package 0, verify later other packages match */ 37737ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 3774889facbeSLen Brown return; 3775889facbeSLen Brown 3776889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 3777869ce69eSLen Brown if (model == INTEL_FAM6_ATOM_SILVERMONT1) 3778144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 3779144b44b1SLen Brown else 3780889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 3781889facbeSLen Brown 378240ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 378340ee8e3bSAndrey Semin 3784144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 3785144b44b1SLen Brown if (time_unit == 0) 3786144b44b1SLen Brown time_unit = 0xA; 3787889facbeSLen Brown 3788144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 3789144b44b1SLen Brown 3790144b44b1SLen Brown tdp = get_tdp(model); 3791889facbeSLen Brown 3792889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 379396e47158SLen Brown if (!quiet) 3794b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 3795889facbeSLen Brown 3796889facbeSLen Brown return; 3797889facbeSLen Brown } 3798889facbeSLen Brown 37991b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 38003a9a941dSLen Brown { 38013a9a941dSLen Brown if (!genuine_intel) 38023a9a941dSLen Brown return; 38033a9a941dSLen Brown 38043a9a941dSLen Brown if (family != 6) 38053a9a941dSLen Brown return; 38063a9a941dSLen Brown 38073a9a941dSLen Brown switch (model) { 3808869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3809869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3810869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 38113a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 3812869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 38133a9a941dSLen Brown do_core_perf_limit_reasons = 1; 38143a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 38153a9a941dSLen Brown default: 38163a9a941dSLen Brown return; 38173a9a941dSLen Brown } 38183a9a941dSLen Brown } 38193a9a941dSLen Brown 3820ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 3821ac980e13SArtem Bityutskiy { 3822ac980e13SArtem Bityutskiy if (is_skx(family, model) || is_bdx(family, model)) 3823ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 3824ac980e13SArtem Bityutskiy } 3825ac980e13SArtem Bityutskiy 3826889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3827889facbeSLen Brown { 3828889facbeSLen Brown unsigned long long msr; 3829f4896fa5SLen Brown unsigned int dts, dts2; 3830889facbeSLen Brown int cpu; 3831889facbeSLen Brown 3832889facbeSLen Brown if (!(do_dts || do_ptm)) 3833889facbeSLen Brown return 0; 3834889facbeSLen Brown 3835889facbeSLen Brown cpu = t->cpu_id; 3836889facbeSLen Brown 3837889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 3838889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 3839889facbeSLen Brown return 0; 3840889facbeSLen Brown 3841889facbeSLen Brown if (cpu_migrate(cpu)) { 3842b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3843889facbeSLen Brown return -1; 3844889facbeSLen Brown } 3845889facbeSLen Brown 3846889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 3847889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 3848889facbeSLen Brown return 0; 3849889facbeSLen Brown 3850889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3851b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 3852889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 3853889facbeSLen Brown 3854889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 3855889facbeSLen Brown return 0; 3856889facbeSLen Brown 3857889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3858889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3859b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3860889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3861889facbeSLen Brown } 3862889facbeSLen Brown 3863889facbeSLen Brown 3864f4896fa5SLen Brown if (do_dts && debug) { 3865889facbeSLen Brown unsigned int resolution; 3866889facbeSLen Brown 3867889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 3868889facbeSLen Brown return 0; 3869889facbeSLen Brown 3870889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3871889facbeSLen Brown resolution = (msr >> 27) & 0xF; 3872b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 3873889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 3874889facbeSLen Brown 3875889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 3876889facbeSLen Brown return 0; 3877889facbeSLen Brown 3878889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3879889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3880b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3881889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3882889facbeSLen Brown } 3883889facbeSLen Brown 3884889facbeSLen Brown return 0; 3885889facbeSLen Brown } 3886889facbeSLen Brown 3887889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 3888889facbeSLen Brown { 3889b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 3890889facbeSLen Brown cpu, label, 3891889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 3892889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 3893889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 3894889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 3895889facbeSLen Brown 3896889facbeSLen Brown return; 3897889facbeSLen Brown } 3898889facbeSLen Brown 3899889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3900889facbeSLen Brown { 3901889facbeSLen Brown unsigned long long msr; 3902889facbeSLen Brown int cpu; 3903889facbeSLen Brown 3904889facbeSLen Brown if (!do_rapl) 3905889facbeSLen Brown return 0; 3906889facbeSLen Brown 3907889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 3908889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3909889facbeSLen Brown return 0; 3910889facbeSLen Brown 3911889facbeSLen Brown cpu = t->cpu_id; 3912889facbeSLen Brown if (cpu_migrate(cpu)) { 3913b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3914889facbeSLen Brown return -1; 3915889facbeSLen Brown } 3916889facbeSLen Brown 3917889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 3918889facbeSLen Brown return -1; 3919889facbeSLen Brown 392096e47158SLen Brown fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr, 3921144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 392296e47158SLen Brown 3923144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 3924144b44b1SLen Brown 3925889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 3926889facbeSLen Brown return -5; 3927889facbeSLen Brown 3928889facbeSLen Brown 3929b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 3930889facbeSLen Brown cpu, msr, 3931889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3932889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3933889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3934889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 3935889facbeSLen Brown 3936144b44b1SLen Brown } 3937144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 3938144b44b1SLen Brown 3939889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 3940889facbeSLen Brown return -9; 3941889facbeSLen Brown 3942b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 3943889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 3944889facbeSLen Brown 3945889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 3946b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 3947889facbeSLen Brown cpu, 3948889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 3949889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 3950889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 3951889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 3952889facbeSLen Brown } 3953889facbeSLen Brown 39540b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 3955889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 3956889facbeSLen Brown return -6; 3957889facbeSLen Brown 3958b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 3959889facbeSLen Brown cpu, msr, 3960889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3961889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3962889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 3963889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 39640b2bb692SLen Brown } 39650b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 3966889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 3967889facbeSLen Brown return -9; 3968b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 3969889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3970889facbeSLen Brown 3971889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 3972889facbeSLen Brown } 3973144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 3974889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 3975889facbeSLen Brown return -7; 3976889facbeSLen Brown 3977b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 3978144b44b1SLen Brown } 39799148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 3980889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 3981889facbeSLen Brown return -9; 3982b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 3983889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3984889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 3985889facbeSLen Brown } 3986889facbeSLen Brown if (do_rapl & RAPL_GFX) { 3987889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 3988889facbeSLen Brown return -8; 3989889facbeSLen Brown 3990b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 3991889facbeSLen Brown 3992889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 3993889facbeSLen Brown return -9; 3994b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 3995889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 3996889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 3997889facbeSLen Brown } 3998889facbeSLen Brown return 0; 3999889facbeSLen Brown } 4000889facbeSLen Brown 4001d7899447SLen Brown /* 4002d7899447SLen Brown * SNB adds support for additional MSRs: 4003d7899447SLen Brown * 4004d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4005d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4006d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4007d7899447SLen Brown */ 4008103a8feaSLen Brown 4009d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4010103a8feaSLen Brown { 4011103a8feaSLen Brown if (!genuine_intel) 4012103a8feaSLen Brown return 0; 4013103a8feaSLen Brown 4014103a8feaSLen Brown switch (model) { 4015869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4016869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4017869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4018869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4019869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 4020869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 4021869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4022869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 4023869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4024869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 4025869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4026869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 4027869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4028869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4029869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4030869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4031997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4032869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4033869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4034ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 40355bbac26eSXiaolong Wang case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 4036103a8feaSLen Brown return 1; 4037103a8feaSLen Brown } 4038103a8feaSLen Brown return 0; 4039103a8feaSLen Brown } 4040103a8feaSLen Brown 4041d7899447SLen Brown /* 4042d7899447SLen Brown * HSW adds support for additional MSRs: 4043d7899447SLen Brown * 4044d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4045d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4046d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 40475a63426eSLen Brown * 40485a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 40495a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 40505a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 40515a63426eSLen Brown * 4052d7899447SLen Brown */ 4053d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 4054ca58710fSKristen Carlson Accardi { 4055ca58710fSKristen Carlson Accardi if (!genuine_intel) 4056ca58710fSKristen Carlson Accardi return 0; 4057ca58710fSKristen Carlson Accardi 4058ca58710fSKristen Carlson Accardi switch (model) { 4059869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4060869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4061869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4062869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4063869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4064869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4065997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4066869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4067ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 4068ca58710fSKristen Carlson Accardi return 1; 4069ca58710fSKristen Carlson Accardi } 4070ca58710fSKristen Carlson Accardi return 0; 4071ca58710fSKristen Carlson Accardi } 4072ca58710fSKristen Carlson Accardi 40730b2bb692SLen Brown /* 40740b2bb692SLen Brown * SKL adds support for additional MSRS: 40750b2bb692SLen Brown * 40760b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 40770b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 40780b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 40790b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 40800b2bb692SLen Brown */ 40810b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 40820b2bb692SLen Brown { 40830b2bb692SLen Brown if (!genuine_intel) 40840b2bb692SLen Brown return 0; 40850b2bb692SLen Brown 40860b2bb692SLen Brown switch (model) { 4087869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4088869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4089869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4090869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4091997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 40920b2bb692SLen Brown return 1; 40930b2bb692SLen Brown } 40940b2bb692SLen Brown return 0; 40950b2bb692SLen Brown } 40960b2bb692SLen Brown 4097144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4098144b44b1SLen Brown { 4099144b44b1SLen Brown if (!genuine_intel) 4100144b44b1SLen Brown return 0; 4101144b44b1SLen Brown switch (model) { 4102869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */ 4103869ce69eSLen Brown case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */ 4104144b44b1SLen Brown return 1; 4105144b44b1SLen Brown } 4106144b44b1SLen Brown return 0; 4107144b44b1SLen Brown } 4108144b44b1SLen Brown 4109fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4110fb5d4327SDasaratharaman Chandramouli { 4111fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4112fb5d4327SDasaratharaman Chandramouli return 0; 4113fb5d4327SDasaratharaman Chandramouli switch (model) { 4114869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4115005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 4116fb5d4327SDasaratharaman Chandramouli return 1; 4117fb5d4327SDasaratharaman Chandramouli } 4118fb5d4327SDasaratharaman Chandramouli return 0; 4119fb5d4327SDasaratharaman Chandramouli } 4120fb5d4327SDasaratharaman Chandramouli 4121997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4122997e5395SSrinivas Pandruvada { 4123997e5395SSrinivas Pandruvada if (!genuine_intel) 4124997e5395SSrinivas Pandruvada return 0; 4125997e5395SSrinivas Pandruvada 4126997e5395SSrinivas Pandruvada switch (model) { 4127997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4128997e5395SSrinivas Pandruvada return 1; 4129997e5395SSrinivas Pandruvada } 4130997e5395SSrinivas Pandruvada 4131997e5395SSrinivas Pandruvada return 0; 4132997e5395SSrinivas Pandruvada } 4133997e5395SSrinivas Pandruvada 4134b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4135b2b34dfeSHubert Chrzaniuk { 4136b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4137b2b34dfeSHubert Chrzaniuk return 1024; 4138b2b34dfeSHubert Chrzaniuk return 1; 4139b2b34dfeSHubert Chrzaniuk } 4140b2b34dfeSHubert Chrzaniuk 4141144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4142144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4143144b44b1SLen Brown 4144144b44b1SLen Brown double slm_bclk(void) 4145144b44b1SLen Brown { 4146144b44b1SLen Brown unsigned long long msr = 3; 4147144b44b1SLen Brown unsigned int i; 4148144b44b1SLen Brown double freq; 4149144b44b1SLen Brown 41507ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4151b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4152144b44b1SLen Brown 4153144b44b1SLen Brown i = msr & 0xf; 4154144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4155b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 41560a91e551SColin Ian King i = 3; 4157144b44b1SLen Brown } 4158144b44b1SLen Brown freq = slm_freq_table[i]; 4159144b44b1SLen Brown 416096e47158SLen Brown if (!quiet) 4161b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4162144b44b1SLen Brown 4163144b44b1SLen Brown return freq; 4164144b44b1SLen Brown } 4165144b44b1SLen Brown 4166103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4167103a8feaSLen Brown { 4168121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4169103a8feaSLen Brown return 100.00; 4170144b44b1SLen Brown else if (is_slm(family, model)) 4171144b44b1SLen Brown return slm_bclk(); 4172103a8feaSLen Brown else 4173103a8feaSLen Brown return 133.33; 4174103a8feaSLen Brown } 4175103a8feaSLen Brown 4176889facbeSLen Brown /* 4177889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4178889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4179889facbeSLen Brown * This is usually equal to tjMax. 4180889facbeSLen Brown * 4181889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4182889facbeSLen Brown * but also allow cmdline over-ride with -T. 4183889facbeSLen Brown * 4184889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4185889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4186889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4187889facbeSLen Brown */ 4188889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4189889facbeSLen Brown { 4190889facbeSLen Brown unsigned long long msr; 4191889facbeSLen Brown unsigned int target_c_local; 4192889facbeSLen Brown int cpu; 4193889facbeSLen Brown 4194889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4195889facbeSLen Brown if (!(do_dts || do_ptm)) 4196889facbeSLen Brown return 0; 4197889facbeSLen Brown 4198889facbeSLen Brown /* this is a per-package concept */ 4199889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4200889facbeSLen Brown return 0; 4201889facbeSLen Brown 4202889facbeSLen Brown cpu = t->cpu_id; 4203889facbeSLen Brown if (cpu_migrate(cpu)) { 4204b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4205889facbeSLen Brown return -1; 4206889facbeSLen Brown } 4207889facbeSLen Brown 4208889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4209889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 4210b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 4211889facbeSLen Brown cpu, tcc_activation_temp); 4212889facbeSLen Brown return 0; 4213889facbeSLen Brown } 4214889facbeSLen Brown 4215889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4216d7899447SLen Brown if (!do_nhm_platform_info) 4217889facbeSLen Brown goto guess; 4218889facbeSLen Brown 42197ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4220889facbeSLen Brown goto guess; 4221889facbeSLen Brown 42223482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 4223889facbeSLen Brown 422496e47158SLen Brown if (!quiet) 4225b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 4226889facbeSLen Brown cpu, msr, target_c_local); 4227889facbeSLen Brown 42283482124aSJean Delvare if (!target_c_local) 4229889facbeSLen Brown goto guess; 4230889facbeSLen Brown 4231889facbeSLen Brown tcc_activation_temp = target_c_local; 4232889facbeSLen Brown 4233889facbeSLen Brown return 0; 4234889facbeSLen Brown 4235889facbeSLen Brown guess: 4236889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 4237b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 4238889facbeSLen Brown cpu, tcc_activation_temp); 4239889facbeSLen Brown 4240889facbeSLen Brown return 0; 4241889facbeSLen Brown } 424269807a63SLen Brown 4243aa8d8cc7SLen Brown void decode_feature_control_msr(void) 4244aa8d8cc7SLen Brown { 4245aa8d8cc7SLen Brown unsigned long long msr; 4246aa8d8cc7SLen Brown 4247aa8d8cc7SLen Brown if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr)) 4248aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 4249aa8d8cc7SLen Brown base_cpu, msr, 4250aa8d8cc7SLen Brown msr & FEATURE_CONTROL_LOCKED ? "" : "UN-", 4251aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 4252aa8d8cc7SLen Brown } 4253aa8d8cc7SLen Brown 425469807a63SLen Brown void decode_misc_enable_msr(void) 425569807a63SLen Brown { 425669807a63SLen Brown unsigned long long msr; 425769807a63SLen Brown 4258f26b1519SLen Brown if (!genuine_intel) 4259f26b1519SLen Brown return; 4260f26b1519SLen Brown 426169807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 4262e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 426369807a63SLen Brown base_cpu, msr, 4264e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 4265e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 4266fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 4267e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 4268e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 426969807a63SLen Brown } 427069807a63SLen Brown 427133148d67SLen Brown void decode_misc_feature_control(void) 427233148d67SLen Brown { 427333148d67SLen Brown unsigned long long msr; 427433148d67SLen Brown 427533148d67SLen Brown if (!has_misc_feature_control) 427633148d67SLen Brown return; 427733148d67SLen Brown 427833148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 427933148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 428033148d67SLen Brown base_cpu, msr, 428133148d67SLen Brown msr & (0 << 0) ? "No-" : "", 428233148d67SLen Brown msr & (1 << 0) ? "No-" : "", 428333148d67SLen Brown msr & (2 << 0) ? "No-" : "", 428433148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 428533148d67SLen Brown } 4286f0057310SLen Brown /* 4287f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 4288f0057310SLen Brown * 4289f0057310SLen Brown * Decode the bits according to the Nehalem documentation 4290f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 4291f0057310SLen Brown * bit[1] less so... 4292f0057310SLen Brown */ 4293f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 4294f0057310SLen Brown { 4295f0057310SLen Brown unsigned long long msr; 4296f0057310SLen Brown 4297f0057310SLen Brown if (!do_nhm_platform_info) 4298f0057310SLen Brown return; 4299f0057310SLen Brown 4300cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 4301cf4cbe53SLen Brown return; 4302cf4cbe53SLen Brown 4303f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 4304ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 4305f0057310SLen Brown base_cpu, msr, 4306f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 4307ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 4308ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 4309f0057310SLen Brown } 431071616c8eSLen Brown /* 431171616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 431271616c8eSLen Brown * 431371616c8eSLen Brown * This MSRs are present on Silvermont processors, 431471616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 431571616c8eSLen Brown */ 431671616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 431771616c8eSLen Brown { 431871616c8eSLen Brown unsigned long long msr; 431971616c8eSLen Brown 432071616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 432171616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 432271616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 432371616c8eSLen Brown 432471616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 432571616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 432671616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 432771616c8eSLen Brown } 43287f5c258eSLen Brown 4329fcd17211SLen Brown void process_cpuid() 4330103a8feaSLen Brown { 433161a87ba7SLen Brown unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; 4332103a8feaSLen Brown unsigned int fms, family, model, stepping; 4333b3a34e93SLen Brown unsigned int has_turbo; 4334103a8feaSLen Brown 4335103a8feaSLen Brown eax = ebx = ecx = edx = 0; 4336103a8feaSLen Brown 43375aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 4338103a8feaSLen Brown 4339103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 4340103a8feaSLen Brown genuine_intel = 1; 4341103a8feaSLen Brown 434296e47158SLen Brown if (!quiet) 4343b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 4344103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 4345103a8feaSLen Brown 43465aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 4347103a8feaSLen Brown family = (fms >> 8) & 0xf; 4348103a8feaSLen Brown model = (fms >> 4) & 0xf; 4349103a8feaSLen Brown stepping = fms & 0xf; 4350103a8feaSLen Brown if (family == 6 || family == 0xf) 4351103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 4352103a8feaSLen Brown 435396e47158SLen Brown if (!quiet) { 4354b7d8c148SLen Brown fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 4355103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 4356aa8d8cc7SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n", 435769807a63SLen Brown ecx & (1 << 0) ? "SSE3" : "-", 435869807a63SLen Brown ecx & (1 << 3) ? "MONITOR" : "-", 4359aa8d8cc7SLen Brown ecx & (1 << 6) ? "SMX" : "-", 436069807a63SLen Brown ecx & (1 << 7) ? "EIST" : "-", 436169807a63SLen Brown ecx & (1 << 8) ? "TM2" : "-", 436269807a63SLen Brown edx & (1 << 4) ? "TSC" : "-", 436369807a63SLen Brown edx & (1 << 5) ? "MSR" : "-", 436469807a63SLen Brown edx & (1 << 22) ? "ACPI-TM" : "-", 436569807a63SLen Brown edx & (1 << 29) ? "TM" : "-"); 436669807a63SLen Brown } 4367103a8feaSLen Brown 4368b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 4369b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 4370103a8feaSLen Brown 4371103a8feaSLen Brown /* 4372103a8feaSLen Brown * check max extended function levels of CPUID. 4373103a8feaSLen Brown * This is needed to check for invariant TSC. 4374103a8feaSLen Brown * This check is valid for both Intel and AMD. 4375103a8feaSLen Brown */ 4376103a8feaSLen Brown ebx = ecx = edx = 0; 43775aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 4378103a8feaSLen Brown 437961a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 4380103a8feaSLen Brown 4381103a8feaSLen Brown /* 4382103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 4383103a8feaSLen Brown * this check is valid for both Intel and AMD 4384103a8feaSLen Brown */ 43855aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 43868209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 4387d7899447SLen Brown } 4388103a8feaSLen Brown 4389103a8feaSLen Brown /* 4390103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 4391103a8feaSLen Brown * this check is valid for both Intel and AMD 4392103a8feaSLen Brown */ 4393103a8feaSLen Brown 43945aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 43958209e054SThomas Renninger has_aperf = ecx & (1 << 0); 4396812db3f7SLen Brown if (has_aperf) { 4397812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 4398812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 4399812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 4400812db3f7SLen Brown } 4401889facbeSLen Brown do_dts = eax & (1 << 0); 4402812db3f7SLen Brown if (do_dts) 4403812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 4404b3a34e93SLen Brown has_turbo = eax & (1 << 1); 4405889facbeSLen Brown do_ptm = eax & (1 << 6); 4406812db3f7SLen Brown if (do_ptm) 4407812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 44087f5c258eSLen Brown has_hwp = eax & (1 << 7); 44097f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 44107f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 44117f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 44127f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 4413889facbeSLen Brown has_epb = ecx & (1 << 3); 4414889facbeSLen Brown 441596e47158SLen Brown if (!quiet) 4416b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 44177f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 44187f5c258eSLen Brown has_aperf ? "" : "No-", 4419b3a34e93SLen Brown has_turbo ? "" : "No-", 44207f5c258eSLen Brown do_dts ? "" : "No-", 44217f5c258eSLen Brown do_ptm ? "" : "No-", 44227f5c258eSLen Brown has_hwp ? "" : "No-", 44237f5c258eSLen Brown has_hwp_notify ? "" : "No-", 44247f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 44257f5c258eSLen Brown has_hwp_epp ? "" : "No-", 44267f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 44277f5c258eSLen Brown has_epb ? "" : "No-"); 4428103a8feaSLen Brown 442996e47158SLen Brown if (!quiet) 443069807a63SLen Brown decode_misc_enable_msr(); 443169807a63SLen Brown 443233148d67SLen Brown 443396e47158SLen Brown if (max_level >= 0x7 && !quiet) { 4434aa8d8cc7SLen Brown int has_sgx; 4435aa8d8cc7SLen Brown 4436aa8d8cc7SLen Brown ecx = 0; 4437aa8d8cc7SLen Brown 4438aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 4439aa8d8cc7SLen Brown 4440aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 4441aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 4442aa8d8cc7SLen Brown 4443aa8d8cc7SLen Brown if (has_sgx) 4444aa8d8cc7SLen Brown decode_feature_control_msr(); 4445aa8d8cc7SLen Brown } 4446aa8d8cc7SLen Brown 444761a87ba7SLen Brown if (max_level >= 0x15) { 44488a5bdf41SLen Brown unsigned int eax_crystal; 44498a5bdf41SLen Brown unsigned int ebx_tsc; 44508a5bdf41SLen Brown 44518a5bdf41SLen Brown /* 44528a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 44538a5bdf41SLen Brown */ 44548a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 44555aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 44568a5bdf41SLen Brown 44578a5bdf41SLen Brown if (ebx_tsc != 0) { 44588a5bdf41SLen Brown 445996e47158SLen Brown if (!quiet && (ebx != 0)) 4460b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 44618a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 44628a5bdf41SLen Brown 44638a5bdf41SLen Brown if (crystal_hz == 0) 44648a5bdf41SLen Brown switch(model) { 4465869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4466869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4467869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4468869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4469e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 4470e8efbc80SLen Brown break; 44717268d407SLen Brown case INTEL_FAM6_ATOM_DENVERTON: /* DNV */ 4472ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 4473ec53e594SLen Brown break; 4474869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4475ac01ac13SLen Brown case INTEL_FAM6_ATOM_GEMINI_LAKE: 4476e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 44778a5bdf41SLen Brown break; 44788a5bdf41SLen Brown default: 44798a5bdf41SLen Brown crystal_hz = 0; 44808a5bdf41SLen Brown } 44818a5bdf41SLen Brown 44828a5bdf41SLen Brown if (crystal_hz) { 44838a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 448496e47158SLen Brown if (!quiet) 4485b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 44868a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 44878a5bdf41SLen Brown } 44888a5bdf41SLen Brown } 44898a5bdf41SLen Brown } 449061a87ba7SLen Brown if (max_level >= 0x16) { 449161a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 449261a87ba7SLen Brown 449361a87ba7SLen Brown /* 449461a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 449561a87ba7SLen Brown */ 449661a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 449761a87ba7SLen Brown 44985aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 449996e47158SLen Brown if (!quiet) 4500b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 450161a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 450261a87ba7SLen Brown } 45038a5bdf41SLen Brown 4504b2b34dfeSHubert Chrzaniuk if (has_aperf) 4505b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 4506b2b34dfeSHubert Chrzaniuk 4507812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 4508812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 4509812db3f7SLen Brown 4510812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 4511812db3f7SLen Brown do_nhm_platform_info = 1; 4512812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 4513812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 4514812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 4515812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 4516812db3f7SLen Brown } 4517d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 4518812db3f7SLen Brown 4519812db3f7SLen Brown if (do_snb_cstates) 4520812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 4521812db3f7SLen Brown 45225a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 45230f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 45240f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 45250f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 45260f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 45270f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 45280f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 45290f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 45300f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 45310539ba11SLen Brown if (has_slv_msrs(family, model)) { 45320f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 45330f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 45340f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 45350f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 45360539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 45370539ba11SLen Brown use_c1_residency_msr = 1; 45380539ba11SLen Brown } 45397170a374SLen Brown if (is_dnv(family, model)) { 45407170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 45417170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 45427170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 45437170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 45447170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 45457170a374SLen Brown use_c1_residency_msr = 1; 45467170a374SLen Brown } 454734c76197SLen Brown if (is_skx(family, model)) { 454834c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 454934c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 455034c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 455134c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 455234c76197SLen Brown } 4553ade0ebacSLen Brown if (is_bdx(family, model)) { 4554ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 4555ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 4556ade0ebacSLen Brown } 45570f47c08dSLen Brown if (has_hsw_msrs(family, model)) { 45580f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 45590f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 45600f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 45610f47c08dSLen Brown } 45625a63426eSLen Brown do_irtl_hsw = has_hsw_msrs(family, model); 4563a99d8730SLen Brown if (has_skl_msrs(family, model)) { 4564a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 4565a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 4566a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 4567a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 4568a99d8730SLen Brown } 4569144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 4570fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 4571997e5395SSrinivas Pandruvada do_cnl_cstates = is_cnl(family, model); 4572103a8feaSLen Brown 457396e47158SLen Brown if (!quiet) 4574f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 4575f0057310SLen Brown 457696e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 457771616c8eSLen Brown decode_c6_demotion_policy_msr(); 457871616c8eSLen Brown 4579889facbeSLen Brown rapl_probe(family, model); 45803a9a941dSLen Brown perf_limit_reasons_probe(family, model); 4581ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 4582889facbeSLen Brown 458396e47158SLen Brown if (!quiet) 45841b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 4585fcd17211SLen Brown 458641618e63SLen Brown if (!quiet) 458741618e63SLen Brown dump_sysfs_cstate_config(); 45887293fccdSLen Brown if (!quiet) 45897293fccdSLen Brown dump_sysfs_pstate_config(); 459041618e63SLen Brown 4591a2b7b749SLen Brown if (has_skl_msrs(family, model)) 4592a2b7b749SLen Brown calculate_tsc_tweak(); 4593a2b7b749SLen Brown 4594812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 4595812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 4596fdf676e5SLen Brown 4597812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 4598812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 459927d47356SLen Brown 4600be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 4601be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 4602be0e54c4SLen Brown else 4603be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 4604be0e54c4SLen Brown 4605be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK)) 4606be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 4607be0e54c4SLen Brown else 4608be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 4609be0e54c4SLen Brown 461096e47158SLen Brown if (!quiet) 461133148d67SLen Brown decode_misc_feature_control(); 461233148d67SLen Brown 4613889facbeSLen Brown return; 4614103a8feaSLen Brown } 4615103a8feaSLen Brown 4616103a8feaSLen Brown 4617103a8feaSLen Brown /* 4618103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 4619103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 4620103a8feaSLen Brown */ 4621103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 4622103a8feaSLen Brown { 4623103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 4624103a8feaSLen Brown return 1; 4625103a8feaSLen Brown else 4626103a8feaSLen Brown return 0; 4627103a8feaSLen Brown } 4628103a8feaSLen Brown 4629103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 4630103a8feaSLen Brown { 4631103a8feaSLen Brown return 0; 4632103a8feaSLen Brown } 4633103a8feaSLen Brown 4634c98d5d94SLen Brown void topology_probe() 4635c98d5d94SLen Brown { 4636c98d5d94SLen Brown int i; 4637c98d5d94SLen Brown int max_core_id = 0; 4638c98d5d94SLen Brown int max_package_id = 0; 4639c98d5d94SLen Brown int max_siblings = 0; 4640c98d5d94SLen Brown 4641c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 4642843c5791SPrarit Bhargava set_max_cpu_num(); 4643c98d5d94SLen Brown topo.num_cpus = 0; 4644c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 4645c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 4646812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 4647c98d5d94SLen Brown 4648d8af6f5fSLen Brown if (debug > 1) 4649b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 4650c98d5d94SLen Brown 4651c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 4652b2c95d90SJosh Triplett if (cpus == NULL) 4653b2c95d90SJosh Triplett err(1, "calloc cpus"); 4654c98d5d94SLen Brown 4655c98d5d94SLen Brown /* 4656c98d5d94SLen Brown * Allocate and initialize cpu_present_set 4657c98d5d94SLen Brown */ 4658c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4659b2c95d90SJosh Triplett if (cpu_present_set == NULL) 4660b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4661c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4662c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 4663c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 4664c98d5d94SLen Brown 4665c98d5d94SLen Brown /* 46661ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 46671ef7d21aSLen Brown */ 46681ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 46691ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 46701ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 46711ef7d21aSLen Brown err(1, "cpu%d not present", i); 46721ef7d21aSLen Brown } 46731ef7d21aSLen Brown 46741ef7d21aSLen Brown /* 4675c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 4676c98d5d94SLen Brown */ 4677c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4678b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 4679b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4680c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4681c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 4682c98d5d94SLen Brown 46838cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 4684c98d5d94SLen Brown 4685c98d5d94SLen Brown /* 4686c98d5d94SLen Brown * For online cpus 4687c98d5d94SLen Brown * find max_core_id, max_package_id 4688c98d5d94SLen Brown */ 4689c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 4690c98d5d94SLen Brown int siblings; 4691c98d5d94SLen Brown 4692c98d5d94SLen Brown if (cpu_is_not_present(i)) { 4693d8af6f5fSLen Brown if (debug > 1) 4694b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 4695c98d5d94SLen Brown continue; 4696c98d5d94SLen Brown } 4697c98d5d94SLen Brown 46980e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 46990e2d8f05SLen Brown 47000e2d8f05SLen Brown /* get package information */ 4701c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 4702c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 4703c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 4704c98d5d94SLen Brown 47050e2d8f05SLen Brown /* get numa node information */ 4706ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 4707ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 4708ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 47090e2d8f05SLen Brown 47100e2d8f05SLen Brown /* get core information */ 47110e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 47120e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 47130e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 47140e2d8f05SLen Brown 47150e2d8f05SLen Brown /* get thread information */ 47160e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 4717c98d5d94SLen Brown if (siblings > max_siblings) 4718c98d5d94SLen Brown max_siblings = siblings; 47198cb48b32SPrarit Bhargava if (cpus[i].thread_id != -1) 47208cb48b32SPrarit Bhargava topo.num_cores++; 47210e2d8f05SLen Brown 4722d8af6f5fSLen Brown if (debug > 1) 47238cb48b32SPrarit Bhargava fprintf(outf, 47248cb48b32SPrarit Bhargava "cpu %d pkg %d node %d core %d thread %d\n", 47250e2d8f05SLen Brown i, cpus[i].physical_package_id, 4726ef605741SPrarit Bhargava cpus[i].physical_node_id, 47278cb48b32SPrarit Bhargava cpus[i].physical_core_id, 47288cb48b32SPrarit Bhargava cpus[i].thread_id); 4729c98d5d94SLen Brown } 4730ef605741SPrarit Bhargava 4731*70a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 4732d8af6f5fSLen Brown if (debug > 1) 4733b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 4734*70a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 4735*70a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 4736812db3f7SLen Brown BIC_PRESENT(BIC_Core); 4737c98d5d94SLen Brown 4738c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 4739d8af6f5fSLen Brown if (debug > 1) 4740b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 4741c98d5d94SLen Brown max_package_id, topo.num_packages); 47427da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 4743812db3f7SLen Brown BIC_PRESENT(BIC_Package); 4744c98d5d94SLen Brown 4745ef605741SPrarit Bhargava set_node_data(); 4746ef605741SPrarit Bhargava if (debug > 1) 4747*70a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 4748ef605741SPrarit Bhargava 4749*70a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 4750d8af6f5fSLen Brown if (debug > 1) 4751b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 4752c98d5d94SLen Brown } 4753c98d5d94SLen Brown 4754c98d5d94SLen Brown void 4755c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p) 4756c98d5d94SLen Brown { 4757c98d5d94SLen Brown int i; 4758c98d5d94SLen Brown 4759*70a9c6e8SPrarit Bhargava *t = calloc(topo.threads_per_core * topo.cores_per_node * 4760678a3bd1SLen Brown topo.num_packages, sizeof(struct thread_data)); 4761c98d5d94SLen Brown if (*t == NULL) 4762c98d5d94SLen Brown goto error; 4763c98d5d94SLen Brown 4764*70a9c6e8SPrarit Bhargava for (i = 0; i < topo.threads_per_core * 4765*70a9c6e8SPrarit Bhargava topo.cores_per_node * topo.num_packages; i++) 4766c98d5d94SLen Brown (*t)[i].cpu_id = -1; 4767c98d5d94SLen Brown 4768*70a9c6e8SPrarit Bhargava *c = calloc(topo.cores_per_node * topo.num_packages, 4769678a3bd1SLen Brown sizeof(struct core_data)); 4770c98d5d94SLen Brown if (*c == NULL) 4771c98d5d94SLen Brown goto error; 4772c98d5d94SLen Brown 4773*70a9c6e8SPrarit Bhargava for (i = 0; i < topo.cores_per_node * topo.num_packages; i++) 4774c98d5d94SLen Brown (*c)[i].core_id = -1; 4775c98d5d94SLen Brown 4776678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 4777c98d5d94SLen Brown if (*p == NULL) 4778c98d5d94SLen Brown goto error; 4779c98d5d94SLen Brown 4780c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 4781c98d5d94SLen Brown (*p)[i].package_id = i; 4782c98d5d94SLen Brown 4783c98d5d94SLen Brown return; 4784c98d5d94SLen Brown error: 4785b2c95d90SJosh Triplett err(1, "calloc counters"); 4786c98d5d94SLen Brown } 4787c98d5d94SLen Brown /* 4788c98d5d94SLen Brown * init_counter() 4789c98d5d94SLen Brown * 4790c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 4791c98d5d94SLen Brown */ 4792c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 47938cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 4794c98d5d94SLen Brown { 47958cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 47968cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 47978cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 4798c98d5d94SLen Brown struct thread_data *t; 4799c98d5d94SLen Brown struct core_data *c; 4800c98d5d94SLen Brown struct pkg_data *p; 4801c98d5d94SLen Brown 48028cb48b32SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, pkg_id); 48038cb48b32SPrarit Bhargava c = GET_CORE(core_base, core_id, pkg_id); 48048cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 4805c98d5d94SLen Brown 4806c98d5d94SLen Brown t->cpu_id = cpu_id; 48078cb48b32SPrarit Bhargava if (thread_id == 0) { 4808c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 4809c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 4810c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 4811c98d5d94SLen Brown } 4812c98d5d94SLen Brown 48138cb48b32SPrarit Bhargava c->core_id = core_id; 48148cb48b32SPrarit Bhargava p->package_id = pkg_id; 4815c98d5d94SLen Brown } 4816c98d5d94SLen Brown 4817c98d5d94SLen Brown 4818c98d5d94SLen Brown int initialize_counters(int cpu_id) 4819c98d5d94SLen Brown { 48208cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 48218cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 4822c98d5d94SLen Brown return 0; 4823c98d5d94SLen Brown } 4824c98d5d94SLen Brown 4825c98d5d94SLen Brown void allocate_output_buffer() 4826c98d5d94SLen Brown { 48273b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 4828c98d5d94SLen Brown outp = output_buffer; 4829b2c95d90SJosh Triplett if (outp == NULL) 4830b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 4831c98d5d94SLen Brown } 483236229897SLen Brown void allocate_fd_percpu(void) 483336229897SLen Brown { 483401a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 483536229897SLen Brown if (fd_percpu == NULL) 483636229897SLen Brown err(-1, "calloc fd_percpu"); 483736229897SLen Brown } 4838562a2d37SLen Brown void allocate_irq_buffers(void) 4839562a2d37SLen Brown { 4840562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 4841562a2d37SLen Brown if (irq_column_2_cpu == NULL) 4842562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 4843c98d5d94SLen Brown 484401a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 4845562a2d37SLen Brown if (irqs_per_cpu == NULL) 484601a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 4847562a2d37SLen Brown } 4848c98d5d94SLen Brown void setup_all_buffers(void) 4849c98d5d94SLen Brown { 4850c98d5d94SLen Brown topology_probe(); 4851562a2d37SLen Brown allocate_irq_buffers(); 485236229897SLen Brown allocate_fd_percpu(); 4853c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 4854c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 4855c98d5d94SLen Brown allocate_output_buffer(); 4856c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 4857c98d5d94SLen Brown } 48583b4d5c7fSAndy Shevchenko 48597ce7d5deSPrarit Bhargava void set_base_cpu(void) 48607ce7d5deSPrarit Bhargava { 48617ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 48627ce7d5deSPrarit Bhargava if (base_cpu < 0) 48637ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 48647ce7d5deSPrarit Bhargava 48657ce7d5deSPrarit Bhargava if (debug > 1) 4866b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 48677ce7d5deSPrarit Bhargava } 48687ce7d5deSPrarit Bhargava 4869103a8feaSLen Brown void turbostat_init() 4870103a8feaSLen Brown { 48717ce7d5deSPrarit Bhargava setup_all_buffers(); 48727ce7d5deSPrarit Bhargava set_base_cpu(); 4873103a8feaSLen Brown check_dev_msr(); 487498481e79SLen Brown check_permissions(); 4875fcd17211SLen Brown process_cpuid(); 4876103a8feaSLen Brown 4877103a8feaSLen Brown 487896e47158SLen Brown if (!quiet) 48797f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 48807f5c258eSLen Brown 488196e47158SLen Brown if (!quiet) 4882889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 4883889facbeSLen Brown 488496e47158SLen Brown if (!quiet) 48853a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 48863a9a941dSLen Brown 488796e47158SLen Brown if (!quiet) 4888889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 4889889facbeSLen Brown 4890889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 4891889facbeSLen Brown 489296e47158SLen Brown if (!quiet) 4893889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 48945a63426eSLen Brown 489596e47158SLen Brown if (!quiet && do_irtl_snb) 48965a63426eSLen Brown print_irtl(); 4897103a8feaSLen Brown } 4898103a8feaSLen Brown 4899103a8feaSLen Brown int fork_it(char **argv) 4900103a8feaSLen Brown { 4901103a8feaSLen Brown pid_t child_pid; 4902d91bb17cSLen Brown int status; 4903d15cf7c1SLen Brown 4904218f0e8dSLen Brown snapshot_proc_sysfs_files(); 4905d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 4906d91bb17cSLen Brown if (status) 4907d91bb17cSLen Brown exit(status); 4908d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 4909d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 4910103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 4911103a8feaSLen Brown 4912103a8feaSLen Brown child_pid = fork(); 4913103a8feaSLen Brown if (!child_pid) { 4914103a8feaSLen Brown /* child */ 4915103a8feaSLen Brown execvp(argv[0], argv); 49160815a3d0SLen Brown err(errno, "exec %s", argv[0]); 4917103a8feaSLen Brown } else { 4918103a8feaSLen Brown 4919103a8feaSLen Brown /* parent */ 4920b2c95d90SJosh Triplett if (child_pid == -1) 4921b2c95d90SJosh Triplett err(1, "fork"); 4922103a8feaSLen Brown 4923103a8feaSLen Brown signal(SIGINT, SIG_IGN); 4924103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 4925b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 4926b2c95d90SJosh Triplett err(status, "waitpid"); 4927103a8feaSLen Brown } 4928c98d5d94SLen Brown /* 4929c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 4930c98d5d94SLen Brown * because re-starting is problematic when forking 4931c98d5d94SLen Brown */ 4932218f0e8dSLen Brown snapshot_proc_sysfs_files(); 4933c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 4934103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 4935103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 4936ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 4937ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 4938ba3dec99SLen Brown else { 4939c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 4940c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 4941ba3dec99SLen Brown } 4942103a8feaSLen Brown 4943b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 4944b7d8c148SLen Brown 4945b7d8c148SLen Brown flush_output_stderr(); 4946103a8feaSLen Brown 4947d91bb17cSLen Brown return status; 4948103a8feaSLen Brown } 4949103a8feaSLen Brown 49503b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 49513b4d5c7fSAndy Shevchenko { 49523b4d5c7fSAndy Shevchenko int status; 49533b4d5c7fSAndy Shevchenko 4954218f0e8dSLen Brown snapshot_proc_sysfs_files(); 49553b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 49563b4d5c7fSAndy Shevchenko if (status) 49573b4d5c7fSAndy Shevchenko return status; 49583b4d5c7fSAndy Shevchenko 49593b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 49603b4d5c7fSAndy Shevchenko if (status) 49613b4d5c7fSAndy Shevchenko return status; 49623b4d5c7fSAndy Shevchenko 4963b7d8c148SLen Brown flush_output_stdout(); 49643b4d5c7fSAndy Shevchenko 49653b4d5c7fSAndy Shevchenko return status; 49663b4d5c7fSAndy Shevchenko } 49673b4d5c7fSAndy Shevchenko 4968d8af6f5fSLen Brown void print_version() { 4969f7d44a8fSLen Brown fprintf(outf, "turbostat version 17.06.23" 4970d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 4971d8af6f5fSLen Brown } 4972d8af6f5fSLen Brown 4973495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 4974495c7654SLen Brown unsigned int width, enum counter_scope scope, 497541618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 4976388e9c81SLen Brown { 4977388e9c81SLen Brown struct msr_counter *msrp; 4978388e9c81SLen Brown 4979388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 4980388e9c81SLen Brown if (msrp == NULL) { 4981388e9c81SLen Brown perror("calloc"); 4982388e9c81SLen Brown exit(1); 4983388e9c81SLen Brown } 4984388e9c81SLen Brown 4985388e9c81SLen Brown msrp->msr_num = msr_num; 4986388e9c81SLen Brown strncpy(msrp->name, name, NAME_BYTES); 4987495c7654SLen Brown if (path) 4988495c7654SLen Brown strncpy(msrp->path, path, PATH_BYTES); 4989388e9c81SLen Brown msrp->width = width; 4990388e9c81SLen Brown msrp->type = type; 4991388e9c81SLen Brown msrp->format = format; 499241618e63SLen Brown msrp->flags = flags; 4993388e9c81SLen Brown 4994388e9c81SLen Brown switch (scope) { 4995388e9c81SLen Brown 4996388e9c81SLen Brown case SCOPE_CPU: 4997388e9c81SLen Brown msrp->next = sys.tp; 4998388e9c81SLen Brown sys.tp = msrp; 4999678a3bd1SLen Brown sys.added_thread_counters++; 50000748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5001678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5002678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5003678a3bd1SLen Brown exit(-1); 5004678a3bd1SLen Brown } 5005388e9c81SLen Brown break; 5006388e9c81SLen Brown 5007388e9c81SLen Brown case SCOPE_CORE: 5008388e9c81SLen Brown msrp->next = sys.cp; 5009388e9c81SLen Brown sys.cp = msrp; 5010678a3bd1SLen Brown sys.added_core_counters++; 5011678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5012678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5013678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5014678a3bd1SLen Brown exit(-1); 5015678a3bd1SLen Brown } 5016388e9c81SLen Brown break; 5017388e9c81SLen Brown 5018388e9c81SLen Brown case SCOPE_PACKAGE: 5019388e9c81SLen Brown msrp->next = sys.pp; 5020388e9c81SLen Brown sys.pp = msrp; 5021678a3bd1SLen Brown sys.added_package_counters++; 5022678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5023678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5024678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5025678a3bd1SLen Brown exit(-1); 5026678a3bd1SLen Brown } 5027388e9c81SLen Brown break; 5028388e9c81SLen Brown } 5029388e9c81SLen Brown 5030388e9c81SLen Brown return 0; 5031388e9c81SLen Brown } 5032388e9c81SLen Brown 5033388e9c81SLen Brown void parse_add_command(char *add_command) 5034388e9c81SLen Brown { 5035388e9c81SLen Brown int msr_num = 0; 5036495c7654SLen Brown char *path = NULL; 50370f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5038388e9c81SLen Brown int width = 64; 5039388e9c81SLen Brown int fail = 0; 5040388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5041388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5042388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5043388e9c81SLen Brown 5044388e9c81SLen Brown while (add_command) { 5045388e9c81SLen Brown 5046388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5047388e9c81SLen Brown goto next; 5048388e9c81SLen Brown 5049388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5050388e9c81SLen Brown goto next; 5051388e9c81SLen Brown 5052495c7654SLen Brown if (*add_command == '/') { 5053495c7654SLen Brown path = add_command; 5054495c7654SLen Brown goto next; 5055495c7654SLen Brown } 5056495c7654SLen Brown 5057388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5058388e9c81SLen Brown if ((width == 32) || (width == 64)) 5059388e9c81SLen Brown goto next; 5060388e9c81SLen Brown width = 64; 5061388e9c81SLen Brown } 5062388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 5063388e9c81SLen Brown scope = SCOPE_CPU; 5064388e9c81SLen Brown goto next; 5065388e9c81SLen Brown } 5066388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 5067388e9c81SLen Brown scope = SCOPE_CORE; 5068388e9c81SLen Brown goto next; 5069388e9c81SLen Brown } 5070388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 5071388e9c81SLen Brown scope = SCOPE_PACKAGE; 5072388e9c81SLen Brown goto next; 5073388e9c81SLen Brown } 5074388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 5075388e9c81SLen Brown type = COUNTER_CYCLES; 5076388e9c81SLen Brown goto next; 5077388e9c81SLen Brown } 5078388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 5079388e9c81SLen Brown type = COUNTER_SECONDS; 5080388e9c81SLen Brown goto next; 5081388e9c81SLen Brown } 508241618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 508341618e63SLen Brown type = COUNTER_USEC; 508441618e63SLen Brown goto next; 508541618e63SLen Brown } 5086388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 5087388e9c81SLen Brown format = FORMAT_RAW; 5088388e9c81SLen Brown goto next; 5089388e9c81SLen Brown } 5090388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 5091388e9c81SLen Brown format = FORMAT_DELTA; 5092388e9c81SLen Brown goto next; 5093388e9c81SLen Brown } 5094388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 5095388e9c81SLen Brown format = FORMAT_PERCENT; 5096388e9c81SLen Brown goto next; 5097388e9c81SLen Brown } 5098388e9c81SLen Brown 5099388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 5100388e9c81SLen Brown char *eos; 5101388e9c81SLen Brown 5102388e9c81SLen Brown eos = strchr(name_buffer, ','); 5103388e9c81SLen Brown if (eos) 5104388e9c81SLen Brown *eos = '\0'; 5105388e9c81SLen Brown goto next; 5106388e9c81SLen Brown } 5107388e9c81SLen Brown 5108388e9c81SLen Brown next: 5109388e9c81SLen Brown add_command = strchr(add_command, ','); 5110495c7654SLen Brown if (add_command) { 5111495c7654SLen Brown *add_command = '\0'; 5112388e9c81SLen Brown add_command++; 5113495c7654SLen Brown } 5114388e9c81SLen Brown 5115388e9c81SLen Brown } 5116495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 5117495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 5118388e9c81SLen Brown fail++; 5119388e9c81SLen Brown } 5120388e9c81SLen Brown 5121388e9c81SLen Brown /* generate default column header */ 5122388e9c81SLen Brown if (*name_buffer == '\0') { 5123388e9c81SLen Brown if (width == 32) 51245f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5125388e9c81SLen Brown else 51265f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5127388e9c81SLen Brown } 5128388e9c81SLen Brown 512941618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 5130388e9c81SLen Brown fail++; 5131388e9c81SLen Brown 5132388e9c81SLen Brown if (fail) { 5133388e9c81SLen Brown help(); 5134388e9c81SLen Brown exit(1); 5135388e9c81SLen Brown } 5136388e9c81SLen Brown } 513741618e63SLen Brown 5138dd778a5eSLen Brown int is_deferred_skip(char *name) 5139dd778a5eSLen Brown { 5140dd778a5eSLen Brown int i; 5141dd778a5eSLen Brown 5142dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 5143dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 5144dd778a5eSLen Brown return 1; 5145dd778a5eSLen Brown return 0; 5146dd778a5eSLen Brown } 5147dd778a5eSLen Brown 514841618e63SLen Brown void probe_sysfs(void) 514941618e63SLen Brown { 515041618e63SLen Brown char path[64]; 515141618e63SLen Brown char name_buf[16]; 515241618e63SLen Brown FILE *input; 515341618e63SLen Brown int state; 515441618e63SLen Brown char *sp; 515541618e63SLen Brown 515641618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 515741618e63SLen Brown return; 515841618e63SLen Brown 51590748eaf0SLen Brown for (state = 10; state >= 0; --state) { 516041618e63SLen Brown 516141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 516241618e63SLen Brown base_cpu, state); 516341618e63SLen Brown input = fopen(path, "r"); 516441618e63SLen Brown if (input == NULL) 516541618e63SLen Brown continue; 516641618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 516741618e63SLen Brown 516841618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 516941618e63SLen Brown sp = strchr(name_buf, '-'); 517041618e63SLen Brown if (!sp) 517141618e63SLen Brown sp = strchrnul(name_buf, '\n'); 517241618e63SLen Brown *sp = '%'; 517341618e63SLen Brown *(sp + 1) = '\0'; 517441618e63SLen Brown 517541618e63SLen Brown fclose(input); 517641618e63SLen Brown 517741618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 517841618e63SLen Brown 5179dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5180dd778a5eSLen Brown continue; 5181dd778a5eSLen Brown 518241618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 518341618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 518441618e63SLen Brown } 518541618e63SLen Brown 51860748eaf0SLen Brown for (state = 10; state >= 0; --state) { 518741618e63SLen Brown 518841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 518941618e63SLen Brown base_cpu, state); 519041618e63SLen Brown input = fopen(path, "r"); 519141618e63SLen Brown if (input == NULL) 519241618e63SLen Brown continue; 519341618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 519441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 519541618e63SLen Brown sp = strchr(name_buf, '-'); 519641618e63SLen Brown if (!sp) 519741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 519841618e63SLen Brown *sp = '\0'; 519941618e63SLen Brown fclose(input); 520041618e63SLen Brown 520141618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 520241618e63SLen Brown 5203dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5204dd778a5eSLen Brown continue; 5205dd778a5eSLen Brown 520641618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 520741618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 520841618e63SLen Brown } 520941618e63SLen Brown 521041618e63SLen Brown } 521141618e63SLen Brown 52121ef7d21aSLen Brown 52131ef7d21aSLen Brown /* 52141ef7d21aSLen Brown * parse cpuset with following syntax 52151ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 52161ef7d21aSLen Brown */ 52171ef7d21aSLen Brown void parse_cpu_command(char *optarg) 52181ef7d21aSLen Brown { 52191ef7d21aSLen Brown unsigned int start, end; 52201ef7d21aSLen Brown char *next; 52211ef7d21aSLen Brown 52224e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 52234e4e1e7cSLen Brown if (cpu_subset) 52244e4e1e7cSLen Brown goto error; 52254e4e1e7cSLen Brown show_core_only++; 52264e4e1e7cSLen Brown return; 52274e4e1e7cSLen Brown } 52284e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 52294e4e1e7cSLen Brown if (cpu_subset) 52304e4e1e7cSLen Brown goto error; 52314e4e1e7cSLen Brown show_pkg_only++; 52324e4e1e7cSLen Brown return; 52334e4e1e7cSLen Brown } 52344e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 52354e4e1e7cSLen Brown goto error; 52364e4e1e7cSLen Brown 52371ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 52381ef7d21aSLen Brown if (cpu_subset == NULL) 52391ef7d21aSLen Brown err(3, "CPU_ALLOC"); 52401ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 52411ef7d21aSLen Brown 52421ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 52431ef7d21aSLen Brown 52441ef7d21aSLen Brown next = optarg; 52451ef7d21aSLen Brown 52461ef7d21aSLen Brown while (next && *next) { 52471ef7d21aSLen Brown 52481ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 52491ef7d21aSLen Brown goto error; 52501ef7d21aSLen Brown 52511ef7d21aSLen Brown start = strtoul(next, &next, 10); 52521ef7d21aSLen Brown 52531ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 52541ef7d21aSLen Brown goto error; 52551ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 52561ef7d21aSLen Brown 52571ef7d21aSLen Brown if (*next == '\0') 52581ef7d21aSLen Brown break; 52591ef7d21aSLen Brown 52601ef7d21aSLen Brown if (*next == ',') { 52611ef7d21aSLen Brown next += 1; 52621ef7d21aSLen Brown continue; 52631ef7d21aSLen Brown } 52641ef7d21aSLen Brown 52651ef7d21aSLen Brown if (*next == '-') { 52661ef7d21aSLen Brown next += 1; /* start range */ 52671ef7d21aSLen Brown } else if (*next == '.') { 52681ef7d21aSLen Brown next += 1; 52691ef7d21aSLen Brown if (*next == '.') 52701ef7d21aSLen Brown next += 1; /* start range */ 52711ef7d21aSLen Brown else 52721ef7d21aSLen Brown goto error; 52731ef7d21aSLen Brown } 52741ef7d21aSLen Brown 52751ef7d21aSLen Brown end = strtoul(next, &next, 10); 52761ef7d21aSLen Brown if (end <= start) 52771ef7d21aSLen Brown goto error; 52781ef7d21aSLen Brown 52791ef7d21aSLen Brown while (++start <= end) { 52801ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 52811ef7d21aSLen Brown goto error; 52821ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 52831ef7d21aSLen Brown } 52841ef7d21aSLen Brown 52851ef7d21aSLen Brown if (*next == ',') 52861ef7d21aSLen Brown next += 1; 52871ef7d21aSLen Brown else if (*next != '\0') 52881ef7d21aSLen Brown goto error; 52891ef7d21aSLen Brown } 52901ef7d21aSLen Brown 52911ef7d21aSLen Brown return; 52921ef7d21aSLen Brown 52931ef7d21aSLen Brown error: 52944e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 52954e4e1e7cSLen Brown help(); 52961ef7d21aSLen Brown exit(-1); 52971ef7d21aSLen Brown } 52981ef7d21aSLen Brown 5299812db3f7SLen Brown 5300103a8feaSLen Brown void cmdline(int argc, char **argv) 5301103a8feaSLen Brown { 5302103a8feaSLen Brown int opt; 5303d8af6f5fSLen Brown int option_index = 0; 5304d8af6f5fSLen Brown static struct option long_options[] = { 5305388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 53061ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 5307d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 530896e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 53093f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 5310d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 5311023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 5312d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 5313812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 5314d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 5315c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 5316b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 531796e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 5318812db3f7SLen Brown {"show", required_argument, 0, 's'}, 5319d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 5320d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 5321d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 5322d8af6f5fSLen Brown {0, 0, 0, 0 } 5323d8af6f5fSLen Brown }; 5324103a8feaSLen Brown 5325103a8feaSLen Brown progname = argv[0]; 5326103a8feaSLen Brown 5327023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 5328d8af6f5fSLen Brown long_options, &option_index)) != -1) { 5329103a8feaSLen Brown switch (opt) { 5330388e9c81SLen Brown case 'a': 5331388e9c81SLen Brown parse_add_command(optarg); 5332388e9c81SLen Brown break; 53331ef7d21aSLen Brown case 'c': 53341ef7d21aSLen Brown parse_cpu_command(optarg); 53351ef7d21aSLen Brown break; 5336d8af6f5fSLen Brown case 'D': 5337d8af6f5fSLen Brown dump_only++; 53388e180f3cSLen Brown break; 53393f44a5c6SLen Brown case 'e': 53403f44a5c6SLen Brown /* --enable specified counter */ 53413f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 53423f44a5c6SLen Brown break; 5343d8af6f5fSLen Brown case 'd': 5344d8af6f5fSLen Brown debug++; 53453f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 53462f32edf1SLen Brown break; 5347812db3f7SLen Brown case 'H': 53483f44a5c6SLen Brown /* 53493f44a5c6SLen Brown * --hide: do not show those specified 53503f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 53513f44a5c6SLen Brown */ 53523f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 5353812db3f7SLen Brown break; 5354d8af6f5fSLen Brown case 'h': 5355d8af6f5fSLen Brown default: 5356d8af6f5fSLen Brown help(); 5357d8af6f5fSLen Brown exit(1); 5358d8af6f5fSLen Brown case 'i': 53592a0609c0SLen Brown { 53602a0609c0SLen Brown double interval = strtod(optarg, NULL); 53612a0609c0SLen Brown 53622a0609c0SLen Brown if (interval < 0.001) { 5363b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 53642a0609c0SLen Brown interval); 53652a0609c0SLen Brown exit(2); 53662a0609c0SLen Brown } 53672a0609c0SLen Brown 536847936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 5369b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 537047936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 53712a0609c0SLen Brown } 5372889facbeSLen Brown break; 53735c56be9aSDirk Brandewie case 'J': 53745c56be9aSDirk Brandewie rapl_joules++; 53755c56be9aSDirk Brandewie break; 5376c8ade361SLen Brown case 'l': 53773f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 5378c8ade361SLen Brown list_header_only++; 5379c8ade361SLen Brown quiet++; 5380c8ade361SLen Brown break; 5381b7d8c148SLen Brown case 'o': 5382b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 5383b7d8c148SLen Brown break; 538496e47158SLen Brown case 'q': 538596e47158SLen Brown quiet = 1; 538696e47158SLen Brown break; 5387023fe0acSChen Yu case 'n': 5388023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 5389023fe0acSChen Yu 5390023fe0acSChen Yu if (num_iterations <= 0) { 5391023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 5392023fe0acSChen Yu num_iterations); 5393023fe0acSChen Yu exit(2); 5394023fe0acSChen Yu } 5395023fe0acSChen Yu break; 5396812db3f7SLen Brown case 's': 53973f44a5c6SLen Brown /* 53983f44a5c6SLen Brown * --show: show only those specified 53993f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 54003f44a5c6SLen Brown * subsequent invocations can add to it. 54013f44a5c6SLen Brown */ 54023f44a5c6SLen Brown if (shown == 0) 54033f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 54043f44a5c6SLen Brown else 54053f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 54063f44a5c6SLen Brown shown = 1; 5407812db3f7SLen Brown break; 5408d8af6f5fSLen Brown case 'S': 5409d8af6f5fSLen Brown summary_only++; 5410d8af6f5fSLen Brown break; 5411d8af6f5fSLen Brown case 'T': 5412d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 5413d8af6f5fSLen Brown break; 5414d8af6f5fSLen Brown case 'v': 5415d8af6f5fSLen Brown print_version(); 5416d8af6f5fSLen Brown exit(0); 5417d8af6f5fSLen Brown break; 5418103a8feaSLen Brown } 5419103a8feaSLen Brown } 5420103a8feaSLen Brown } 5421103a8feaSLen Brown 5422103a8feaSLen Brown int main(int argc, char **argv) 5423103a8feaSLen Brown { 5424b7d8c148SLen Brown outf = stderr; 5425b7d8c148SLen Brown 5426103a8feaSLen Brown cmdline(argc, argv); 5427103a8feaSLen Brown 542896e47158SLen Brown if (!quiet) 5429d8af6f5fSLen Brown print_version(); 5430103a8feaSLen Brown 543141618e63SLen Brown probe_sysfs(); 543241618e63SLen Brown 5433103a8feaSLen Brown turbostat_init(); 5434103a8feaSLen Brown 54353b4d5c7fSAndy Shevchenko /* dump counters and exit */ 54363b4d5c7fSAndy Shevchenko if (dump_only) 54373b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 54383b4d5c7fSAndy Shevchenko 5439c8ade361SLen Brown /* list header and exit */ 5440c8ade361SLen Brown if (list_header_only) { 5441c8ade361SLen Brown print_header(","); 5442c8ade361SLen Brown flush_output_stdout(); 5443c8ade361SLen Brown return 0; 5444c8ade361SLen Brown } 5445c8ade361SLen Brown 5446103a8feaSLen Brown /* 5447103a8feaSLen Brown * if any params left, it must be a command to fork 5448103a8feaSLen Brown */ 5449103a8feaSLen Brown if (argc - optind) 5450103a8feaSLen Brown return fork_it(argv + optind); 5451103a8feaSLen Brown else 5452103a8feaSLen Brown turbostat_loop(); 5453103a8feaSLen Brown 5454103a8feaSLen Brown return 0; 5455103a8feaSLen Brown } 5456