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; 1124c2122d4SLen Brown unsigned int first_counter_read = 1; 113889facbeSLen Brown 114889facbeSLen Brown #define RAPL_PKG (1 << 0) 115e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 116e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 117e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 118e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 119e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 120e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 121e6f9bb3cSLen Brown 122889facbeSLen Brown #define RAPL_DRAM (1 << 3) 123e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 124e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 125e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 126e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1270b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1280b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 129e6f9bb3cSLen Brown 1309148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 131e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1320b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 133e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 134e6f9bb3cSLen Brown 1350b2bb692SLen Brown #define RAPL_GFX (1 << 8) 136e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 137e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 138e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1399148494cSJacob Pan 1409148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1419148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1429148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 143889facbeSLen Brown #define TJMAX_DEFAULT 100 144889facbeSLen Brown 145889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 146103a8feaSLen Brown 147388e9c81SLen Brown /* 148388e9c81SLen Brown * buffer size used by sscanf() for added column names 149388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 150388e9c81SLen Brown */ 151388e9c81SLen Brown #define NAME_BYTES 20 152495c7654SLen Brown #define PATH_BYTES 128 153388e9c81SLen Brown 154103a8feaSLen Brown int backwards_count; 155103a8feaSLen Brown char *progname; 156103a8feaSLen Brown 1571ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1581ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1591ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1600748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1610748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1620e2d8f05SLen Brown #define BITMASK_SIZE 32 163c98d5d94SLen Brown 164c98d5d94SLen Brown struct thread_data { 165f4fdf2b4SLen Brown struct timeval tv_begin; 166f4fdf2b4SLen Brown struct timeval tv_end; 167c98d5d94SLen Brown unsigned long long tsc; 168c98d5d94SLen Brown unsigned long long aperf; 169c98d5d94SLen Brown unsigned long long mperf; 170144b44b1SLen Brown unsigned long long c1; 1710de6c0dfSLen Brown unsigned long long irq_count; 1721ed51011SLen Brown unsigned int smi_count; 173c98d5d94SLen Brown unsigned int cpu_id; 1744c2122d4SLen Brown unsigned int apic_id; 1754c2122d4SLen Brown unsigned int x2apic_id; 176c98d5d94SLen Brown unsigned int flags; 177c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 178c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1790748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 180c98d5d94SLen Brown } *thread_even, *thread_odd; 181c98d5d94SLen Brown 182c98d5d94SLen Brown struct core_data { 183c98d5d94SLen Brown unsigned long long c3; 184c98d5d94SLen Brown unsigned long long c6; 185c98d5d94SLen Brown unsigned long long c7; 1860539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 187889facbeSLen Brown unsigned int core_temp_c; 188c98d5d94SLen Brown unsigned int core_id; 189678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 190c98d5d94SLen Brown } *core_even, *core_odd; 191c98d5d94SLen Brown 192c98d5d94SLen Brown struct pkg_data { 193c98d5d94SLen Brown unsigned long long pc2; 194c98d5d94SLen Brown unsigned long long pc3; 195c98d5d94SLen Brown unsigned long long pc6; 196c98d5d94SLen Brown unsigned long long pc7; 197ca58710fSKristen Carlson Accardi unsigned long long pc8; 198ca58710fSKristen Carlson Accardi unsigned long long pc9; 199ca58710fSKristen Carlson Accardi unsigned long long pc10; 200be0e54c4SLen Brown unsigned long long cpu_lpi; 201be0e54c4SLen Brown unsigned long long sys_lpi; 2020b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2030b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2040b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2050b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2069185e988SLen Brown long long gfx_rc6_ms; 20727d47356SLen Brown unsigned int gfx_mhz; 208c98d5d94SLen Brown unsigned int package_id; 209889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 210889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 211889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 212889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 213889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 214889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 215889facbeSLen Brown unsigned int pkg_temp_c; 216678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 217c98d5d94SLen Brown } *package_even, *package_odd; 218c98d5d94SLen Brown 219c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 220c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 221c98d5d94SLen Brown 22240f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 22340f5cfe7SPrarit Bhargava ((thread_base) + \ 22440f5cfe7SPrarit Bhargava ((pkg_no) * \ 22540f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 22640f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 22740f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 22840f5cfe7SPrarit Bhargava (thread_no)) 22940f5cfe7SPrarit Bhargava 23040f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 23140f5cfe7SPrarit Bhargava ((core_base) + \ 23240f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 23340f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 23440f5cfe7SPrarit Bhargava (core_no)) 23540f5cfe7SPrarit Bhargava 23640f5cfe7SPrarit Bhargava 237c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 238c98d5d94SLen Brown 239388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE}; 24041618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC}; 241388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT}; 242388e9c81SLen Brown 243388e9c81SLen Brown struct msr_counter { 244388e9c81SLen Brown unsigned int msr_num; 245388e9c81SLen Brown char name[NAME_BYTES]; 246495c7654SLen Brown char path[PATH_BYTES]; 247388e9c81SLen Brown unsigned int width; 248388e9c81SLen Brown enum counter_type type; 249388e9c81SLen Brown enum counter_format format; 250388e9c81SLen Brown struct msr_counter *next; 251812db3f7SLen Brown unsigned int flags; 252812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 253812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 25441618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 255388e9c81SLen Brown }; 256388e9c81SLen Brown 257388e9c81SLen Brown struct sys_counters { 258678a3bd1SLen Brown unsigned int added_thread_counters; 259678a3bd1SLen Brown unsigned int added_core_counters; 260678a3bd1SLen Brown unsigned int added_package_counters; 261388e9c81SLen Brown struct msr_counter *tp; 262388e9c81SLen Brown struct msr_counter *cp; 263388e9c81SLen Brown struct msr_counter *pp; 264388e9c81SLen Brown } sys; 265388e9c81SLen Brown 266c98d5d94SLen Brown struct system_summary { 267c98d5d94SLen Brown struct thread_data threads; 268c98d5d94SLen Brown struct core_data cores; 269c98d5d94SLen Brown struct pkg_data packages; 270388e9c81SLen Brown } average; 271c98d5d94SLen Brown 2720e2d8f05SLen Brown struct cpu_topology { 2730e2d8f05SLen Brown int physical_package_id; 2740e2d8f05SLen Brown int logical_cpu_id; 275ef605741SPrarit Bhargava int physical_node_id; 276ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 2770e2d8f05SLen Brown int physical_core_id; 2788cb48b32SPrarit Bhargava int thread_id; 2790e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 2800e2d8f05SLen Brown } *cpus; 281c98d5d94SLen Brown 282c98d5d94SLen Brown struct topo_params { 283c98d5d94SLen Brown int num_packages; 284103a8feaSLen Brown int num_cpus; 285c98d5d94SLen Brown int num_cores; 286c98d5d94SLen Brown int max_cpu_num; 287ef605741SPrarit Bhargava int max_node_num; 28870a9c6e8SPrarit Bhargava int nodes_per_pkg; 28970a9c6e8SPrarit Bhargava int cores_per_node; 29070a9c6e8SPrarit Bhargava int threads_per_core; 291c98d5d94SLen Brown } topo; 292103a8feaSLen Brown 293c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 294103a8feaSLen Brown 295562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 296562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 297562a2d37SLen Brown 298c98d5d94SLen Brown void setup_all_buffers(void); 299103a8feaSLen Brown 300c98d5d94SLen Brown int cpu_is_not_present(int cpu) 301d15cf7c1SLen Brown { 302c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 303c98d5d94SLen Brown } 304c98d5d94SLen Brown /* 305c98d5d94SLen Brown * run func(thread, core, package) in topology order 306c98d5d94SLen Brown * skip non-present cpus 307c98d5d94SLen Brown */ 308c98d5d94SLen Brown 309c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 310c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 311c98d5d94SLen Brown { 31240f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 313c98d5d94SLen Brown 314c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 31570a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 31640f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; 31740f5cfe7SPrarit Bhargava node_no++) { 318c98d5d94SLen Brown for (thread_no = 0; thread_no < 31970a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 320c98d5d94SLen Brown struct thread_data *t; 321c98d5d94SLen Brown struct core_data *c; 322c98d5d94SLen Brown struct pkg_data *p; 323c98d5d94SLen Brown 32440f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 32540f5cfe7SPrarit Bhargava core_no, node_no, 32640f5cfe7SPrarit Bhargava pkg_no); 327c98d5d94SLen Brown 328c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 329c98d5d94SLen Brown continue; 330c98d5d94SLen Brown 33140f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 33240f5cfe7SPrarit Bhargava node_no, pkg_no); 333c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 334c98d5d94SLen Brown 335c98d5d94SLen Brown retval = func(t, c, p); 336c98d5d94SLen Brown if (retval) 337c98d5d94SLen Brown return retval; 338c98d5d94SLen Brown } 339c98d5d94SLen Brown } 340c98d5d94SLen Brown } 34140f5cfe7SPrarit Bhargava } 342d15cf7c1SLen Brown return 0; 343d15cf7c1SLen Brown } 344d15cf7c1SLen Brown 34588c3281fSLen Brown int cpu_migrate(int cpu) 34688c3281fSLen Brown { 347c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 348c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 349c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 35088c3281fSLen Brown return -1; 35188c3281fSLen Brown else 35288c3281fSLen Brown return 0; 35388c3281fSLen Brown } 35436229897SLen Brown int get_msr_fd(int cpu) 355103a8feaSLen Brown { 356103a8feaSLen Brown char pathname[32]; 357103a8feaSLen Brown int fd; 358103a8feaSLen Brown 35936229897SLen Brown fd = fd_percpu[cpu]; 36036229897SLen Brown 36136229897SLen Brown if (fd) 36236229897SLen Brown return fd; 36336229897SLen Brown 364103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 365103a8feaSLen Brown fd = open(pathname, O_RDONLY); 36615aaa346SLen Brown if (fd < 0) 36798481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 368103a8feaSLen Brown 36936229897SLen Brown fd_percpu[cpu] = fd; 37036229897SLen Brown 37136229897SLen Brown return fd; 37236229897SLen Brown } 37336229897SLen Brown 37436229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 37536229897SLen Brown { 37636229897SLen Brown ssize_t retval; 37736229897SLen Brown 37836229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 37915aaa346SLen Brown 38098481e79SLen Brown if (retval != sizeof *msr) 381cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 38215aaa346SLen Brown 38315aaa346SLen Brown return 0; 384103a8feaSLen Brown } 385103a8feaSLen Brown 386fc04cc67SLen Brown /* 387bdd5ae3aSLen Brown * This list matches the column headers, except 388bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 389bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 390bdd5ae3aSLen Brown * matching on them for --show and --hide. 391fc04cc67SLen Brown */ 392812db3f7SLen Brown struct msr_counter bic[] = { 3933f44a5c6SLen Brown { 0x0, "usec" }, 3943f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 395812db3f7SLen Brown { 0x0, "Package" }, 396bdd5ae3aSLen Brown { 0x0, "Node" }, 397812db3f7SLen Brown { 0x0, "Avg_MHz" }, 398bdd5ae3aSLen Brown { 0x0, "Busy%" }, 399812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 400812db3f7SLen Brown { 0x0, "TSC_MHz" }, 401812db3f7SLen Brown { 0x0, "IRQ" }, 402495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 403bdd5ae3aSLen Brown { 0x0, "sysfs" }, 404812db3f7SLen Brown { 0x0, "CPU%c1" }, 405812db3f7SLen Brown { 0x0, "CPU%c3" }, 406812db3f7SLen Brown { 0x0, "CPU%c6" }, 407812db3f7SLen Brown { 0x0, "CPU%c7" }, 408812db3f7SLen Brown { 0x0, "ThreadC" }, 409812db3f7SLen Brown { 0x0, "CoreTmp" }, 410812db3f7SLen Brown { 0x0, "CoreCnt" }, 411812db3f7SLen Brown { 0x0, "PkgTmp" }, 412812db3f7SLen Brown { 0x0, "GFX%rc6" }, 413812db3f7SLen Brown { 0x0, "GFXMHz" }, 414812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 415812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 416812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 417812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 4180f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 4190f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 4204bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 421be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 422be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 423812db3f7SLen Brown { 0x0, "PkgWatt" }, 424812db3f7SLen Brown { 0x0, "CorWatt" }, 425812db3f7SLen Brown { 0x0, "GFXWatt" }, 426812db3f7SLen Brown { 0x0, "PkgCnt" }, 427812db3f7SLen Brown { 0x0, "RAMWatt" }, 428812db3f7SLen Brown { 0x0, "PKG_%" }, 429812db3f7SLen Brown { 0x0, "RAM_%" }, 430812db3f7SLen Brown { 0x0, "Pkg_J" }, 431812db3f7SLen Brown { 0x0, "Cor_J" }, 432812db3f7SLen Brown { 0x0, "GFX_J" }, 433812db3f7SLen Brown { 0x0, "RAM_J" }, 4340539ba11SLen Brown { 0x0, "Mod%c6" }, 435a99d8730SLen Brown { 0x0, "Totl%C0" }, 436a99d8730SLen Brown { 0x0, "Any%C0" }, 437a99d8730SLen Brown { 0x0, "GFX%C0" }, 438a99d8730SLen Brown { 0x0, "CPUGFX%" }, 439bdd5ae3aSLen Brown { 0x0, "Core" }, 440bdd5ae3aSLen Brown { 0x0, "CPU" }, 4414c2122d4SLen Brown { 0x0, "APIC" }, 4424c2122d4SLen Brown { 0x0, "X2APIC" }, 443812db3f7SLen Brown }; 444812db3f7SLen Brown 445812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 4463f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 4473f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 4483f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 449bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 450bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 451bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 452bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 453bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 454bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 455bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 456bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 457bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 458bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 459bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 460bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 461bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 462bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 463bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 464bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 465bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 466bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 467bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 468bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 469bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 470bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 471bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 472bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 473bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 474bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 475bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 476bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 477bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 478bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 479bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 480bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 481bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 482bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 483bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 484bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 485bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 486bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 487bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 488bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 489bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 490bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 491bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 492bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 493bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 4944c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 4954c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 496812db3f7SLen Brown 4974c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 4983f44a5c6SLen Brown 4993f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 5004c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 501812db3f7SLen Brown 502812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 5033f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 504812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 5050f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 506812db3f7SLen Brown 5073f44a5c6SLen Brown 508dd778a5eSLen Brown #define MAX_DEFERRED 16 509dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 510dd778a5eSLen Brown int deferred_skip_index; 511dd778a5eSLen Brown 512dd778a5eSLen Brown /* 513dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 514dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 515dd778a5eSLen Brown */ 516dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 517dd778a5eSLen Brown 518dd778a5eSLen Brown void help(void) 519dd778a5eSLen Brown { 520dd778a5eSLen Brown fprintf(outf, 521dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 522dd778a5eSLen Brown "\n" 523dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 524dd778a5eSLen Brown "when COMMAND completes.\n" 525dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 526dd778a5eSLen Brown "to print statistics, until interrupted.\n" 527cc481650SNathan Ciobanu " -a, --add add a counter\n" 528dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 529cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 530dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 5319ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 5329ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 5339ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 5349ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 5359ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 5369ce80578SNathan Ciobanu " hide the specified column(s)\n" 537cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 538cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 5399ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 540cc481650SNathan Ciobanu " -l, --list list column headers only\n" 541cc481650SNathan Ciobanu " -n, --num_iterations num\n" 542cc481650SNathan Ciobanu " number of the measurement iterations\n" 543cc481650SNathan Ciobanu " -o, --out file\n" 544cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 545cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 5469ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 5479ce80578SNathan Ciobanu " show only the specified column(s)\n" 5489ce80578SNathan Ciobanu " -S, --Summary\n" 5499ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 5509ce80578SNathan Ciobanu " -T, --TCC temperature\n" 5519ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 5529ce80578SNathan Ciobanu " degrees Celsius\n" 553cc481650SNathan Ciobanu " -h, --help print this help message\n" 554cc481650SNathan Ciobanu " -v, --version print version information\n" 555dd778a5eSLen Brown "\n" 556dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 557dd778a5eSLen Brown } 558dd778a5eSLen Brown 559812db3f7SLen Brown /* 560812db3f7SLen Brown * bic_lookup 561812db3f7SLen Brown * for all the strings in comma separate name_list, 562812db3f7SLen Brown * set the approprate bit in return value. 563812db3f7SLen Brown */ 564dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 565812db3f7SLen Brown { 566812db3f7SLen Brown int i; 567812db3f7SLen Brown unsigned long long retval = 0; 568812db3f7SLen Brown 569812db3f7SLen Brown while (name_list) { 570812db3f7SLen Brown char *comma; 571812db3f7SLen Brown 572812db3f7SLen Brown comma = strchr(name_list, ','); 573812db3f7SLen Brown 574812db3f7SLen Brown if (comma) 575812db3f7SLen Brown *comma = '\0'; 576812db3f7SLen Brown 5773f44a5c6SLen Brown if (!strcmp(name_list, "all")) 5783f44a5c6SLen Brown return ~0; 5793f44a5c6SLen Brown 580812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 581812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 582812db3f7SLen Brown retval |= (1ULL << i); 583812db3f7SLen Brown break; 584812db3f7SLen Brown } 585812db3f7SLen Brown } 586812db3f7SLen Brown if (i == MAX_BIC) { 587dd778a5eSLen Brown if (mode == SHOW_LIST) { 588812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 589812db3f7SLen Brown exit(-1); 590812db3f7SLen Brown } 591dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 592dd778a5eSLen Brown if (debug) 593dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 594dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 595dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 596dd778a5eSLen Brown MAX_DEFERRED, name_list); 597dd778a5eSLen Brown help(); 598dd778a5eSLen Brown exit(1); 599dd778a5eSLen Brown } 600dd778a5eSLen Brown } 601812db3f7SLen Brown 602812db3f7SLen Brown name_list = comma; 603812db3f7SLen Brown if (name_list) 604812db3f7SLen Brown name_list++; 605812db3f7SLen Brown 606812db3f7SLen Brown } 607812db3f7SLen Brown return retval; 608812db3f7SLen Brown } 609fc04cc67SLen Brown 610dd778a5eSLen Brown 611c8ade361SLen Brown void print_header(char *delim) 612103a8feaSLen Brown { 613388e9c81SLen Brown struct msr_counter *mp; 6146168c2e0SLen Brown int printed = 0; 615388e9c81SLen Brown 6163f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 6173f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 6183f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 6193f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 620812db3f7SLen Brown if (DO_BIC(BIC_Package)) 6216168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 62201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 62301235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 624812db3f7SLen Brown if (DO_BIC(BIC_Core)) 6256168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 626812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 6276168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 6284c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 6294c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 6304c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 6314c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 632812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 6336168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 634812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 6356168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 636812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 6376168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 638812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 6396168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 6401cc21f7bSLen Brown 6410de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 6420de6c0dfSLen Brown if (sums_need_wide_columns) 6436168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 6440de6c0dfSLen Brown else 6456168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 6460de6c0dfSLen Brown } 6470de6c0dfSLen Brown 648812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 6496168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 6501cc21f7bSLen Brown 651388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 652dd778a5eSLen Brown 653388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 654388e9c81SLen Brown if (mp->width == 64) 655dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 656388e9c81SLen Brown else 657dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 658388e9c81SLen Brown } else { 6590de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 660dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 6610de6c0dfSLen Brown else 662dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 663388e9c81SLen Brown } 664388e9c81SLen Brown } 665388e9c81SLen Brown 66641618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 6676168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 668997e5395SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) 6696168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 670812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 6716168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 672812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 6736168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 674678a3bd1SLen Brown 6750539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 6766168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 677678a3bd1SLen Brown 678812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 6796168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 680388e9c81SLen Brown 681388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 682388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 683388e9c81SLen Brown if (mp->width == 64) 684c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 685388e9c81SLen Brown else 686c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 687388e9c81SLen Brown } else { 6880de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 6890de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 6900de6c0dfSLen Brown else 691c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 692388e9c81SLen Brown } 693388e9c81SLen Brown } 694388e9c81SLen Brown 695812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 6966168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 697889facbeSLen Brown 698812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 6996168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 700fdf676e5SLen Brown 701812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 7026168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 70327d47356SLen Brown 704a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 7056168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 706a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 7076168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 708a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 7096168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 710a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 7116168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 7120b2bb692SLen Brown 7130f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 7146168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 7150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 7166168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 7170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 7186168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 7190f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 7206168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 7210f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 7226168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 7230f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 7246168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 7250f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 7266168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 727be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 728be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 729be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 730be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 731103a8feaSLen Brown 7325c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 733812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 7346168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 735812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 7366168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 737812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 7386168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 739812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 7406168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 741812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 7426168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 743812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 7446168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 745d7899447SLen Brown } else if (do_rapl && rapl_joules) { 746812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 7476168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 748812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 7496168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 750812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 7516168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 752812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 7536168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 754812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 7556168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 756812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 7576168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 7585c56be9aSDirk Brandewie } 759388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 760388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 761388e9c81SLen Brown if (mp->width == 64) 762c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 763388e9c81SLen Brown else 764c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 765388e9c81SLen Brown } else { 7660de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 7670de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 7680de6c0dfSLen Brown else 769c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 770388e9c81SLen Brown } 771388e9c81SLen Brown } 772388e9c81SLen Brown 773c98d5d94SLen Brown outp += sprintf(outp, "\n"); 774103a8feaSLen Brown } 775103a8feaSLen Brown 776c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 777c98d5d94SLen Brown struct pkg_data *p) 778103a8feaSLen Brown { 779388e9c81SLen Brown int i; 780388e9c81SLen Brown struct msr_counter *mp; 781388e9c81SLen Brown 7823b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 783c98d5d94SLen Brown 784c98d5d94SLen Brown if (t) { 7853b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 7863b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 7873b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 7883b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 7893b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 7903b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 7916886fee4SLen Brown 792812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 7930de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 794812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 795218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 796388e9c81SLen Brown 797388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 798388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 799388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 800388e9c81SLen Brown } 801103a8feaSLen Brown } 802103a8feaSLen Brown 803c98d5d94SLen Brown if (c) { 8043b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 8053b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 8063b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 8073b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 8083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 809388e9c81SLen Brown 810388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 811388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 812388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 813388e9c81SLen Brown } 8140539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 815c98d5d94SLen Brown } 816103a8feaSLen Brown 817c98d5d94SLen Brown if (p) { 8183b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 8190b2bb692SLen Brown 8200b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 8210b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 8220b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 8230b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 8240b2bb692SLen Brown 8253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 8260f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 8273b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 8280f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 8293b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 8300f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 8313b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 8323b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 8333b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 8343b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 835be0e54c4SLen Brown outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 836be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 837be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 8383b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 8393b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 8403b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 8413b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 8423b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 8433b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 8443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 8453b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 8463b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 847388e9c81SLen Brown 848388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 849388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 850388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 851388e9c81SLen Brown } 852c98d5d94SLen Brown } 8533b4d5c7fSAndy Shevchenko 8543b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 8553b4d5c7fSAndy Shevchenko 856c98d5d94SLen Brown return 0; 857103a8feaSLen Brown } 858103a8feaSLen Brown 859e23da037SLen Brown /* 860e23da037SLen Brown * column formatting convention & formats 861e23da037SLen Brown */ 862c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 863c98d5d94SLen Brown struct pkg_data *p) 864103a8feaSLen Brown { 865008d396eSLen Brown double interval_float, tsc; 866fc04cc67SLen Brown char *fmt8; 867388e9c81SLen Brown int i; 868388e9c81SLen Brown struct msr_counter *mp; 8696168c2e0SLen Brown char *delim = "\t"; 8706168c2e0SLen Brown int printed = 0; 871103a8feaSLen Brown 872c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 873c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 874c98d5d94SLen Brown return 0; 875c98d5d94SLen Brown 876c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 877c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 878c98d5d94SLen Brown return 0; 879c98d5d94SLen Brown 8801ef7d21aSLen Brown /*if not summary line and --cpu is used */ 8811ef7d21aSLen Brown if ((t != &average.threads) && 8821ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 8831ef7d21aSLen Brown return 0; 8841ef7d21aSLen Brown 8853f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 886f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 887f4fdf2b4SLen Brown struct timeval tv; 888f4fdf2b4SLen Brown 889f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 890f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 891f4fdf2b4SLen Brown } 892f4fdf2b4SLen Brown 8933f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 8943f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 8953f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 8963f44a5c6SLen Brown 897103a8feaSLen Brown interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 898103a8feaSLen Brown 899008d396eSLen Brown tsc = t->tsc * tsc_tweak; 900008d396eSLen Brown 901c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 902c98d5d94SLen Brown if (t == &average.threads) { 903812db3f7SLen Brown if (DO_BIC(BIC_Package)) 9046168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 90501235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 90601235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 907812db3f7SLen Brown if (DO_BIC(BIC_Core)) 9086168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 909812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 9106168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9114c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 9124c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9134c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 9144c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 915103a8feaSLen Brown } else { 916812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 917c98d5d94SLen Brown if (p) 9186168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 919c98d5d94SLen Brown else 9206168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 921c98d5d94SLen Brown } 92201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 92301235041SPrarit Bhargava if (t) 92401235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 92501235041SPrarit Bhargava (printed++ ? delim : ""), 92601235041SPrarit Bhargava cpus[t->cpu_id].physical_node_id); 92701235041SPrarit Bhargava else 92801235041SPrarit Bhargava outp += sprintf(outp, "%s-", 92901235041SPrarit Bhargava (printed++ ? delim : "")); 93001235041SPrarit Bhargava } 931812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 932c98d5d94SLen Brown if (c) 9336168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 934c98d5d94SLen Brown else 9356168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 936c98d5d94SLen Brown } 937812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 9386168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 9394c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 9404c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 9414c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 9424c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 943103a8feaSLen Brown } 944fc04cc67SLen Brown 945812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 9466168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 947fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 948fc04cc67SLen Brown 949812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 9506168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 951103a8feaSLen Brown 952812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 95321ed5574SLen Brown if (has_base_hz) 9546168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 95521ed5574SLen Brown else 9566168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 957008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 95821ed5574SLen Brown } 959103a8feaSLen Brown 960812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 9616168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 962103a8feaSLen Brown 963562a2d37SLen Brown /* IRQ */ 9640de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 9650de6c0dfSLen Brown if (sums_need_wide_columns) 9666168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 9670de6c0dfSLen Brown else 9686168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 9690de6c0dfSLen Brown } 970562a2d37SLen Brown 9711cc21f7bSLen Brown /* SMI */ 972812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 9736168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 9741cc21f7bSLen Brown 975678a3bd1SLen Brown /* Added counters */ 976678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 977678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 978678a3bd1SLen Brown if (mp->width == 32) 9795f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 980678a3bd1SLen Brown else 9816168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 982678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 9830de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 9846168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 9850de6c0dfSLen Brown else 9866168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 987678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 98841618e63SLen Brown if (mp->type == COUNTER_USEC) 9896168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 99041618e63SLen Brown else 9916168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 992678a3bd1SLen Brown } 993678a3bd1SLen Brown } 994678a3bd1SLen Brown 99541618e63SLen Brown /* C1 */ 99641618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 9976168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 99841618e63SLen Brown 99941618e63SLen Brown 1000c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1001c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1002c98d5d94SLen Brown goto done; 1003c98d5d94SLen Brown 1004997e5395SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) 10056168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 1006812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 10076168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 1008812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 10096168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 1010c98d5d94SLen Brown 10110539ba11SLen Brown /* Mod%c6 */ 10120539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 10136168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 10140539ba11SLen Brown 1015812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 10166168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1017889facbeSLen Brown 1018388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1019388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1020388e9c81SLen Brown if (mp->width == 32) 10215f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 1022388e9c81SLen Brown else 10236168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1024388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 10250de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10266168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 10270de6c0dfSLen Brown else 10286168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1029388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 10306168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 1031388e9c81SLen Brown } 1032388e9c81SLen Brown } 1033388e9c81SLen Brown 1034c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1035c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1036c98d5d94SLen Brown goto done; 1037c98d5d94SLen Brown 10380b2bb692SLen Brown /* PkgTmp */ 1039812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 10406168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1041889facbeSLen Brown 1042fdf676e5SLen Brown /* GFXrc6 */ 1043812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1044ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 10456168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 10469185e988SLen Brown } else { 10476168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 10489185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 10499185e988SLen Brown } 10509185e988SLen Brown } 1051fdf676e5SLen Brown 105227d47356SLen Brown /* GFXMHz */ 1053812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 10546168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 105527d47356SLen Brown 10560b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1057a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 10586168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 1059a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 10606168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 1061a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 10626168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 1063a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 10646168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 10650b2bb692SLen Brown 10660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 10676168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 10680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 10696168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 10700f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 10716168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 10720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 10736168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 10740f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 10756168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 10760f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 10776168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 10780f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 10796168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 1080889facbeSLen Brown 1081be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1082be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1083be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1084be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1085be0e54c4SLen Brown 1086889facbeSLen Brown /* 1087889facbeSLen Brown * If measurement interval exceeds minimum RAPL Joule Counter range, 1088889facbeSLen Brown * indicate that results are suspect by printing "**" in fraction place. 1089889facbeSLen Brown */ 1090fc04cc67SLen Brown if (interval_float < rapl_joule_counter_range) 10916168c2e0SLen Brown fmt8 = "%s%.2f"; 1092fc04cc67SLen Brown else 1093fc04cc67SLen Brown fmt8 = "%6.0f**"; 1094889facbeSLen Brown 1095812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 10966168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 1097812db3f7SLen Brown if (DO_BIC(BIC_CorWatt)) 10986168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1099812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 11006168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1101812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 11026168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 1103812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 11046168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 1105812db3f7SLen Brown if (DO_BIC(BIC_Cor_J)) 11066168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1107812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 11086168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1109812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 11106168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1111812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 11126168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1113812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 11146168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1115812db3f7SLen Brown 1116388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1117388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1118388e9c81SLen Brown if (mp->width == 32) 11195f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]); 1120388e9c81SLen Brown else 11216168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1122388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 11230de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11246168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 11250de6c0dfSLen Brown else 11266168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1127388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 11286168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc); 1129388e9c81SLen Brown } 1130388e9c81SLen Brown } 1131388e9c81SLen Brown 1132c98d5d94SLen Brown done: 113394d6ab4bSLen Brown if (*(outp - 1) != '\n') 1134c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1135c98d5d94SLen Brown 1136c98d5d94SLen Brown return 0; 1137103a8feaSLen Brown } 1138103a8feaSLen Brown 1139b7d8c148SLen Brown void flush_output_stdout(void) 1140103a8feaSLen Brown { 1141b7d8c148SLen Brown FILE *filep; 1142b7d8c148SLen Brown 1143b7d8c148SLen Brown if (outf == stderr) 1144b7d8c148SLen Brown filep = stdout; 1145b7d8c148SLen Brown else 1146b7d8c148SLen Brown filep = outf; 1147b7d8c148SLen Brown 1148b7d8c148SLen Brown fputs(output_buffer, filep); 1149b7d8c148SLen Brown fflush(filep); 1150b7d8c148SLen Brown 1151c98d5d94SLen Brown outp = output_buffer; 1152c98d5d94SLen Brown } 1153b7d8c148SLen Brown void flush_output_stderr(void) 1154c98d5d94SLen Brown { 1155b7d8c148SLen Brown fputs(output_buffer, outf); 1156b7d8c148SLen Brown fflush(outf); 1157c98d5d94SLen Brown outp = output_buffer; 1158c98d5d94SLen Brown } 1159c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1160c98d5d94SLen Brown { 1161e23da037SLen Brown static int printed; 1162103a8feaSLen Brown 1163e23da037SLen Brown if (!printed || !summary_only) 1164c8ade361SLen Brown print_header("\t"); 1165103a8feaSLen Brown 11669d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1167103a8feaSLen Brown 1168e23da037SLen Brown printed = 1; 1169e23da037SLen Brown 1170e23da037SLen Brown if (summary_only) 1171e23da037SLen Brown return; 1172e23da037SLen Brown 1173c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1174103a8feaSLen Brown } 1175103a8feaSLen Brown 1176889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 1177889facbeSLen Brown if (new > old) { \ 1178889facbeSLen Brown old = new - old; \ 1179889facbeSLen Brown } else { \ 1180889facbeSLen Brown old = 0x100000000 + new - old; \ 1181889facbeSLen Brown } 1182889facbeSLen Brown 1183ba3dec99SLen Brown int 1184c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1185103a8feaSLen Brown { 1186388e9c81SLen Brown int i; 1187388e9c81SLen Brown struct msr_counter *mp; 11880b2bb692SLen Brown 1189a99d8730SLen Brown 1190a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 11910b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1192a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 11930b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1194a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 11950b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1196a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 11970b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1198a99d8730SLen Brown 1199c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 12000f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1201c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 12020f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1203c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 12040f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1205c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1206ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1207ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1208ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1209be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1210be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1211889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1212889facbeSLen Brown 12139185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 12149185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 12159185e988SLen Brown old->gfx_rc6_ms = -1; 12169185e988SLen Brown else 1217fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 12189185e988SLen Brown 121927d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 122027d47356SLen Brown 1221889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 1222889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 1223889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 1224889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 1225889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 1226889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 1227ba3dec99SLen Brown 1228388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1229388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1230388e9c81SLen Brown old->counter[i] = new->counter[i]; 1231388e9c81SLen Brown else 1232388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1233388e9c81SLen Brown } 1234388e9c81SLen Brown 1235ba3dec99SLen Brown return 0; 1236103a8feaSLen Brown } 1237103a8feaSLen Brown 1238c98d5d94SLen Brown void 1239c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1240c98d5d94SLen Brown { 1241388e9c81SLen Brown int i; 1242388e9c81SLen Brown struct msr_counter *mp; 1243388e9c81SLen Brown 1244c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1245c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1246c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1247889facbeSLen Brown old->core_temp_c = new->core_temp_c; 12480539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1249388e9c81SLen Brown 1250388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1251388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1252388e9c81SLen Brown old->counter[i] = new->counter[i]; 1253388e9c81SLen Brown else 1254388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1255388e9c81SLen Brown } 1256103a8feaSLen Brown } 1257c98d5d94SLen Brown 1258c3ae331dSLen Brown /* 1259c3ae331dSLen Brown * old = new - old 1260c3ae331dSLen Brown */ 1261ba3dec99SLen Brown int 1262c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1263c98d5d94SLen Brown struct core_data *core_delta) 1264c98d5d94SLen Brown { 1265388e9c81SLen Brown int i; 1266388e9c81SLen Brown struct msr_counter *mp; 1267388e9c81SLen Brown 12684c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 12694c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 12704c2122d4SLen Brown new->apic_id = old->apic_id; 12714c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 12724c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 12734c2122d4SLen Brown 12743f44a5c6SLen Brown /* 12753f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 12763f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 12773f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 12783f44a5c6SLen Brown */ 12793f44a5c6SLen Brown 12803f44a5c6SLen Brown old->tv_begin = new->tv_begin; 12813f44a5c6SLen Brown old->tv_end = new->tv_end; 12823f44a5c6SLen Brown 1283c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1284c98d5d94SLen Brown 1285103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1286b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1287b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1288b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1289b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1290103a8feaSLen Brown 1291c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1292c98d5d94SLen Brown 1293812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 1294c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1295c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1296c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1297c98d5d94SLen Brown } else { 1298ba3dec99SLen Brown return -1; 1299103a8feaSLen Brown } 1300a729617cSLen Brown } 1301103a8feaSLen Brown 1302c98d5d94SLen Brown 1303144b44b1SLen Brown if (use_c1_residency_msr) { 1304144b44b1SLen Brown /* 1305144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1306144b44b1SLen Brown * which should be more accurate than the derivation below. 1307144b44b1SLen Brown */ 1308144b44b1SLen Brown } else { 1309103a8feaSLen Brown /* 1310c3ae331dSLen Brown * As counter collection is not atomic, 1311c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1312103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1313103a8feaSLen Brown */ 131495149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1315c98d5d94SLen Brown old->c1 = 0; 1316c98d5d94SLen Brown else { 1317c98d5d94SLen Brown /* normal case, derive c1 */ 1318008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1319c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1320c98d5d94SLen Brown } 1321144b44b1SLen Brown } 1322c3ae331dSLen Brown 1323c98d5d94SLen Brown if (old->mperf == 0) { 1324b7d8c148SLen Brown if (debug > 1) 1325b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1326c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1327c98d5d94SLen Brown } 1328103a8feaSLen Brown 1329812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1330562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1331562a2d37SLen Brown 1332812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 13331ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1334ba3dec99SLen Brown 1335388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1336388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1337388e9c81SLen Brown old->counter[i] = new->counter[i]; 1338388e9c81SLen Brown else 1339388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1340388e9c81SLen Brown } 1341ba3dec99SLen Brown return 0; 1342103a8feaSLen Brown } 1343c98d5d94SLen Brown 1344c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1345c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1346c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1347c98d5d94SLen Brown { 1348ba3dec99SLen Brown int retval = 0; 1349ba3dec99SLen Brown 1350c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1351c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1352c98d5d94SLen Brown delta_core(c, c2); 1353c98d5d94SLen Brown 1354c98d5d94SLen Brown /* always calculate thread delta */ 1355ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1356ba3dec99SLen Brown if (retval) 1357ba3dec99SLen Brown return retval; 1358c98d5d94SLen Brown 1359c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1360c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1361ba3dec99SLen Brown retval = delta_package(p, p2); 1362c98d5d94SLen Brown 1363ba3dec99SLen Brown return retval; 1364103a8feaSLen Brown } 1365103a8feaSLen Brown 1366c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1367103a8feaSLen Brown { 1368388e9c81SLen Brown int i; 1369388e9c81SLen Brown struct msr_counter *mp; 1370388e9c81SLen Brown 13713f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 13723f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 13733f44a5c6SLen Brown t->tv_end.tv_sec = 0; 13743f44a5c6SLen Brown t->tv_end.tv_usec = 0; 13753f44a5c6SLen Brown 1376c98d5d94SLen Brown t->tsc = 0; 1377c98d5d94SLen Brown t->aperf = 0; 1378c98d5d94SLen Brown t->mperf = 0; 1379c98d5d94SLen Brown t->c1 = 0; 1380103a8feaSLen Brown 1381562a2d37SLen Brown t->irq_count = 0; 1382562a2d37SLen Brown t->smi_count = 0; 1383562a2d37SLen Brown 1384c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1385c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1386c98d5d94SLen Brown 1387c98d5d94SLen Brown c->c3 = 0; 1388c98d5d94SLen Brown c->c6 = 0; 1389c98d5d94SLen Brown c->c7 = 0; 13900539ba11SLen Brown c->mc6_us = 0; 1391889facbeSLen Brown c->core_temp_c = 0; 1392c98d5d94SLen Brown 13930b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 13940b2bb692SLen Brown p->pkg_any_core_c0 = 0; 13950b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 13960b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 13970b2bb692SLen Brown 1398c98d5d94SLen Brown p->pc2 = 0; 13990f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1400c98d5d94SLen Brown p->pc3 = 0; 14010f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1402c98d5d94SLen Brown p->pc6 = 0; 14030f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1404c98d5d94SLen Brown p->pc7 = 0; 1405ca58710fSKristen Carlson Accardi p->pc8 = 0; 1406ca58710fSKristen Carlson Accardi p->pc9 = 0; 1407ca58710fSKristen Carlson Accardi p->pc10 = 0; 1408be0e54c4SLen Brown p->cpu_lpi = 0; 1409be0e54c4SLen Brown p->sys_lpi = 0; 1410889facbeSLen Brown 1411889facbeSLen Brown p->energy_pkg = 0; 1412889facbeSLen Brown p->energy_dram = 0; 1413889facbeSLen Brown p->energy_cores = 0; 1414889facbeSLen Brown p->energy_gfx = 0; 1415889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1416889facbeSLen Brown p->rapl_dram_perf_status = 0; 1417889facbeSLen Brown p->pkg_temp_c = 0; 141827d47356SLen Brown 1419fdf676e5SLen Brown p->gfx_rc6_ms = 0; 142027d47356SLen Brown p->gfx_mhz = 0; 1421388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1422388e9c81SLen Brown t->counter[i] = 0; 1423388e9c81SLen Brown 1424388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1425388e9c81SLen Brown c->counter[i] = 0; 1426388e9c81SLen Brown 1427388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1428388e9c81SLen Brown p->counter[i] = 0; 1429103a8feaSLen Brown } 1430c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1431c98d5d94SLen Brown struct pkg_data *p) 1432103a8feaSLen Brown { 1433388e9c81SLen Brown int i; 1434388e9c81SLen Brown struct msr_counter *mp; 1435388e9c81SLen Brown 14364c2122d4SLen Brown /* copy un-changing apic_id's */ 14374c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14384c2122d4SLen Brown average.threads.apic_id = t->apic_id; 14394c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14404c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 14414c2122d4SLen Brown 14423f44a5c6SLen Brown /* remember first tv_begin */ 14433f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 14443f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 14453f44a5c6SLen Brown 14463f44a5c6SLen Brown /* remember last tv_end */ 14473f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 14483f44a5c6SLen Brown 1449c98d5d94SLen Brown average.threads.tsc += t->tsc; 1450c98d5d94SLen Brown average.threads.aperf += t->aperf; 1451c98d5d94SLen Brown average.threads.mperf += t->mperf; 1452c98d5d94SLen Brown average.threads.c1 += t->c1; 145315aaa346SLen Brown 1454562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1455562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1456562a2d37SLen Brown 1457388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1458388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1459388e9c81SLen Brown continue; 1460388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1461388e9c81SLen Brown } 1462388e9c81SLen Brown 1463c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1464c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1465c98d5d94SLen Brown return 0; 1466c98d5d94SLen Brown 1467c98d5d94SLen Brown average.cores.c3 += c->c3; 1468c98d5d94SLen Brown average.cores.c6 += c->c6; 1469c98d5d94SLen Brown average.cores.c7 += c->c7; 14700539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1471c98d5d94SLen Brown 1472889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1473889facbeSLen Brown 1474388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1475388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1476388e9c81SLen Brown continue; 1477388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1478388e9c81SLen Brown } 1479388e9c81SLen Brown 1480c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1481c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1482c98d5d94SLen Brown return 0; 1483c98d5d94SLen Brown 1484a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 14850b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1486a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 14870b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1488a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 14890b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1490a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 14910b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 14920b2bb692SLen Brown 1493c98d5d94SLen Brown average.packages.pc2 += p->pc2; 14940f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1495c98d5d94SLen Brown average.packages.pc3 += p->pc3; 14960f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1497c98d5d94SLen Brown average.packages.pc6 += p->pc6; 14980f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1499c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1500ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1501ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1502ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1503c98d5d94SLen Brown 1504be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1505be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1506be0e54c4SLen Brown 1507889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1508889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1509889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1510889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1511889facbeSLen Brown 1512fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 151327d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 151427d47356SLen Brown 1515889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1516889facbeSLen Brown 1517889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1518889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1519388e9c81SLen Brown 1520388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1521388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1522388e9c81SLen Brown continue; 1523388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1524388e9c81SLen Brown } 1525c98d5d94SLen Brown return 0; 1526c98d5d94SLen Brown } 1527c98d5d94SLen Brown /* 1528c98d5d94SLen Brown * sum the counters for all cpus in the system 1529c98d5d94SLen Brown * compute the weighted average 1530c98d5d94SLen Brown */ 1531c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1532c98d5d94SLen Brown struct pkg_data *p) 1533c98d5d94SLen Brown { 1534388e9c81SLen Brown int i; 1535388e9c81SLen Brown struct msr_counter *mp; 1536388e9c81SLen Brown 1537c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1538c98d5d94SLen Brown 1539c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1540c98d5d94SLen Brown 1541c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1542c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1543c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1544c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1545c98d5d94SLen Brown 15460de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 15470de6c0dfSLen Brown sums_need_wide_columns = 1; 15480de6c0dfSLen Brown 1549c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1550c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1551c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 15520539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1553c98d5d94SLen Brown 1554a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 15550b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1556a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 15570b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1558a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 15590b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1560a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 15610b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 15620b2bb692SLen Brown 1563c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 15640f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1565c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 15660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1567c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 15680f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1569c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1570ca58710fSKristen Carlson Accardi 1571ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1572ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1573ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1574388e9c81SLen Brown 1575388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1576388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1577388e9c81SLen Brown continue; 15780de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 15790de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 15800de6c0dfSLen Brown sums_need_wide_columns = 1; 158141618e63SLen Brown continue; 15820de6c0dfSLen Brown } 1583388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1584388e9c81SLen Brown } 1585388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1586388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1587388e9c81SLen Brown continue; 15880de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 15890de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 15900de6c0dfSLen Brown sums_need_wide_columns = 1; 15910de6c0dfSLen Brown } 1592388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1593388e9c81SLen Brown } 1594388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1595388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1596388e9c81SLen Brown continue; 15970de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 15980de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 15990de6c0dfSLen Brown sums_need_wide_columns = 1; 16000de6c0dfSLen Brown } 1601388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1602388e9c81SLen Brown } 1603c98d5d94SLen Brown } 1604c98d5d94SLen Brown 1605c98d5d94SLen Brown static unsigned long long rdtsc(void) 1606c98d5d94SLen Brown { 1607c98d5d94SLen Brown unsigned int low, high; 1608c98d5d94SLen Brown 1609c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1610c98d5d94SLen Brown 1611c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1612c98d5d94SLen Brown } 1613c98d5d94SLen Brown 1614c98d5d94SLen Brown /* 1615495c7654SLen Brown * Open a file, and exit on failure 1616495c7654SLen Brown */ 1617495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1618495c7654SLen Brown { 1619495c7654SLen Brown FILE *filep = fopen(path, mode); 1620495c7654SLen Brown 1621495c7654SLen Brown if (!filep) 1622495c7654SLen Brown err(1, "%s: open failed", path); 1623495c7654SLen Brown return filep; 1624495c7654SLen Brown } 1625495c7654SLen Brown /* 1626495c7654SLen Brown * snapshot_sysfs_counter() 1627495c7654SLen Brown * 1628495c7654SLen Brown * return snapshot of given counter 1629495c7654SLen Brown */ 1630495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1631495c7654SLen Brown { 1632495c7654SLen Brown FILE *fp; 1633495c7654SLen Brown int retval; 1634495c7654SLen Brown unsigned long long counter; 1635495c7654SLen Brown 1636495c7654SLen Brown fp = fopen_or_die(path, "r"); 1637495c7654SLen Brown 1638495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1639495c7654SLen Brown if (retval != 1) 1640495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1641495c7654SLen Brown 1642495c7654SLen Brown fclose(fp); 1643495c7654SLen Brown 1644495c7654SLen Brown return counter; 1645495c7654SLen Brown } 1646495c7654SLen Brown 1647495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1648495c7654SLen Brown { 1649495c7654SLen Brown if (mp->msr_num != 0) { 1650495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1651495c7654SLen Brown return -1; 1652495c7654SLen Brown } else { 165346c27978SLen Brown char path[128 + PATH_BYTES]; 165441618e63SLen Brown 165541618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 165641618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 165741618e63SLen Brown cpu, mp->path); 165841618e63SLen Brown 165941618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 166041618e63SLen Brown } else { 1661495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1662495c7654SLen Brown } 166341618e63SLen Brown } 1664495c7654SLen Brown 1665495c7654SLen Brown return 0; 1666495c7654SLen Brown } 1667495c7654SLen Brown 16684c2122d4SLen Brown void get_apic_id(struct thread_data *t) 16694c2122d4SLen Brown { 16704c2122d4SLen Brown unsigned int eax, ebx, ecx, edx, max_level; 16714c2122d4SLen Brown 16724c2122d4SLen Brown eax = ebx = ecx = edx = 0; 16734c2122d4SLen Brown 16744c2122d4SLen Brown if (!genuine_intel) 16754c2122d4SLen Brown return; 16764c2122d4SLen Brown 16774c2122d4SLen Brown __cpuid(0, max_level, ebx, ecx, edx); 16784c2122d4SLen Brown 16794c2122d4SLen Brown __cpuid(1, eax, ebx, ecx, edx); 16804c2122d4SLen Brown t->apic_id = (ebx >> 24) & 0xf; 16814c2122d4SLen Brown 16824c2122d4SLen Brown if (max_level < 0xb) 16834c2122d4SLen Brown return; 16844c2122d4SLen Brown 16854c2122d4SLen Brown if (!DO_BIC(BIC_X2APIC)) 16864c2122d4SLen Brown return; 16874c2122d4SLen Brown 16884c2122d4SLen Brown ecx = 0; 16894c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 16904c2122d4SLen Brown t->x2apic_id = edx; 16914c2122d4SLen Brown 16924c2122d4SLen Brown if (debug && (t->apic_id != t->x2apic_id)) 1693cfce494dSLen Brown fprintf(outf, "cpu%d: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id); 16944c2122d4SLen Brown } 16954c2122d4SLen Brown 1696495c7654SLen Brown /* 1697c98d5d94SLen Brown * get_counters(...) 1698c98d5d94SLen Brown * migrate to cpu 1699c98d5d94SLen Brown * acquire and record local counters for that cpu 1700c98d5d94SLen Brown */ 1701c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1702c98d5d94SLen Brown { 1703c98d5d94SLen Brown int cpu = t->cpu_id; 1704889facbeSLen Brown unsigned long long msr; 17050102b067SLen Brown int aperf_mperf_retry_count = 0; 1706388e9c81SLen Brown struct msr_counter *mp; 1707388e9c81SLen Brown int i; 1708c98d5d94SLen Brown 1709f4fdf2b4SLen Brown gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1710f4fdf2b4SLen Brown 1711e52966c0SLen Brown if (cpu_migrate(cpu)) { 1712b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 171315aaa346SLen Brown return -1; 1714e52966c0SLen Brown } 171515aaa346SLen Brown 17164c2122d4SLen Brown if (first_counter_read) 17174c2122d4SLen Brown get_apic_id(t); 17180102b067SLen Brown retry: 1719c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 172015aaa346SLen Brown 1721812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) { 17220102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 17230102b067SLen Brown 17240102b067SLen Brown /* 17250102b067SLen Brown * The TSC, APERF and MPERF must be read together for 17260102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 17270102b067SLen Brown * 17280102b067SLen Brown * Unfortunately, APERF and MPERF are read by 17290102b067SLen Brown * individual system call, so delays may occur 17300102b067SLen Brown * between them. If the time to read them 17310102b067SLen Brown * varies by a large amount, we re-read them. 17320102b067SLen Brown */ 17330102b067SLen Brown 17340102b067SLen Brown /* 17350102b067SLen Brown * This initial dummy APERF read has been seen to 17360102b067SLen Brown * reduce jitter in the subsequent reads. 17370102b067SLen Brown */ 17380102b067SLen Brown 17399c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1740c98d5d94SLen Brown return -3; 17410102b067SLen Brown 17420102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 17430102b067SLen Brown 17440102b067SLen Brown tsc_before = t->tsc; 17450102b067SLen Brown 17460102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 17470102b067SLen Brown return -3; 17480102b067SLen Brown 17490102b067SLen Brown tsc_between = rdtsc(); 17500102b067SLen Brown 17519c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1752c98d5d94SLen Brown return -4; 17530102b067SLen Brown 17540102b067SLen Brown tsc_after = rdtsc(); 17550102b067SLen Brown 17560102b067SLen Brown aperf_time = tsc_between - tsc_before; 17570102b067SLen Brown mperf_time = tsc_after - tsc_between; 17580102b067SLen Brown 17590102b067SLen Brown /* 17600102b067SLen Brown * If the system call latency to read APERF and MPERF 17610102b067SLen Brown * differ by more than 2x, then try again. 17620102b067SLen Brown */ 17630102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 17640102b067SLen Brown aperf_mperf_retry_count++; 17650102b067SLen Brown if (aperf_mperf_retry_count < 5) 17660102b067SLen Brown goto retry; 17670102b067SLen Brown else 17680102b067SLen Brown warnx("cpu%d jitter %lld %lld", 17690102b067SLen Brown cpu, aperf_time, mperf_time); 17700102b067SLen Brown } 17710102b067SLen Brown aperf_mperf_retry_count = 0; 17720102b067SLen Brown 1773b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1774b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 177588c3281fSLen Brown } 177688c3281fSLen Brown 1777812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1778562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1779812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 17801ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 17811ed51011SLen Brown return -5; 17821ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 17831ed51011SLen Brown } 17840539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1785144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1786144b44b1SLen Brown return -6; 1787144b44b1SLen Brown } 1788144b44b1SLen Brown 1789388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1790495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1791388e9c81SLen Brown return -10; 1792388e9c81SLen Brown } 1793388e9c81SLen Brown 1794c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1795c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1796f4fdf2b4SLen Brown goto done; 1797c98d5d94SLen Brown 1798997e5395SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) { 1799c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1800c98d5d94SLen Brown return -6; 1801144b44b1SLen Brown } 1802144b44b1SLen Brown 1803812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) { 1804c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1805c98d5d94SLen Brown return -7; 1806fb5d4327SDasaratharaman Chandramouli } else if (do_knl_cstates) { 1807fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1808fb5d4327SDasaratharaman Chandramouli return -7; 1809103a8feaSLen Brown } 181015aaa346SLen Brown 1811812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 1812c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1813c98d5d94SLen Brown return -8; 1814c98d5d94SLen Brown 18150539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 18160539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 18170539ba11SLen Brown return -8; 18180539ba11SLen Brown 1819812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 1820889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1821889facbeSLen Brown return -9; 1822889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1823889facbeSLen Brown } 1824889facbeSLen Brown 1825388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1826495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 1827388e9c81SLen Brown return -10; 1828388e9c81SLen Brown } 1829889facbeSLen Brown 1830c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1831c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1832f4fdf2b4SLen Brown goto done; 183315aaa346SLen Brown 1834a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 18350b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 18360b2bb692SLen Brown return -10; 1837a99d8730SLen Brown } 1838a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 18390b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 18400b2bb692SLen Brown return -11; 1841a99d8730SLen Brown } 1842a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 18430b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 18440b2bb692SLen Brown return -12; 1845a99d8730SLen Brown } 1846a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 18470b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 18480b2bb692SLen Brown return -13; 18490b2bb692SLen Brown } 18500f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1851c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1852c98d5d94SLen Brown return -9; 18530f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 18540539ba11SLen Brown if (do_slm_cstates) { 18550539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 18560539ba11SLen Brown return -10; 18570539ba11SLen Brown } else { 1858c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1859c98d5d94SLen Brown return -10; 18600539ba11SLen Brown } 18610539ba11SLen Brown } 18620539ba11SLen Brown 18630f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 1864c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1865c98d5d94SLen Brown return -11; 18660f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1867c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1868c98d5d94SLen Brown return -12; 18690f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 1870ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1871ca58710fSKristen Carlson Accardi return -13; 18720f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 1873ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1874ca58710fSKristen Carlson Accardi return -13; 18750f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 1876ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1877ca58710fSKristen Carlson Accardi return -13; 18780f47c08dSLen Brown 1879be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1880be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 1881be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1882be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 1883be0e54c4SLen Brown 1884889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1885889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1886889facbeSLen Brown return -13; 1887889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1888889facbeSLen Brown } 18899148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 1890889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1891889facbeSLen Brown return -14; 1892889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1893889facbeSLen Brown } 1894889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1895889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1896889facbeSLen Brown return -15; 1897889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1898889facbeSLen Brown } 1899889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1900889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1901889facbeSLen Brown return -16; 1902889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1903889facbeSLen Brown } 1904889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1905889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1906889facbeSLen Brown return -16; 1907889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1908889facbeSLen Brown } 1909889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1910889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1911889facbeSLen Brown return -16; 1912889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1913889facbeSLen Brown } 1914812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 1915889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 1916889facbeSLen Brown return -17; 1917889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1918889facbeSLen Brown } 1919fdf676e5SLen Brown 1920812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 1921fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 1922fdf676e5SLen Brown 1923812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 192427d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 192527d47356SLen Brown 1926388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1927495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 1928388e9c81SLen Brown return -10; 1929388e9c81SLen Brown } 1930f4fdf2b4SLen Brown done: 1931f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 1932388e9c81SLen Brown 193315aaa346SLen Brown return 0; 1934103a8feaSLen Brown } 1935103a8feaSLen Brown 1936ee7e38e3SLen Brown /* 1937ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 1938ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 1939ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 1940ee7e38e3SLen Brown */ 1941ee7e38e3SLen Brown 1942ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 1943ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 1944ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 1945ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 1946ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 1947ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 1948ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 1949ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 1950ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 1951ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 1952ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 1953ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 19540b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 19550b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 19560b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */ 1957ee7e38e3SLen Brown 1958ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 1959ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 19600b2bb692SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"}; 1961ee7e38e3SLen Brown 1962e9257f5fSLen 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}; 1963e9257f5fSLen 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}; 1964e9257f5fSLen 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}; 19650539ba11SLen 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}; 1966f2642888SLen 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}; 1967e9257f5fSLen 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}; 1968e4085d54SLen 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}; 19692085e124SArtem 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}; 1970ee7e38e3SLen Brown 1971a2b7b749SLen Brown 1972a2b7b749SLen Brown static void 1973a2b7b749SLen Brown calculate_tsc_tweak() 1974a2b7b749SLen Brown { 1975a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 1976a2b7b749SLen Brown } 1977a2b7b749SLen Brown 1978fcd17211SLen Brown static void 1979fcd17211SLen Brown dump_nhm_platform_info(void) 1980103a8feaSLen Brown { 1981103a8feaSLen Brown unsigned long long msr; 1982103a8feaSLen Brown unsigned int ratio; 1983103a8feaSLen Brown 1984ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 1985103a8feaSLen Brown 1986b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 19876574a5d5SLen Brown 1988103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 1989710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 1990103a8feaSLen Brown ratio, bclk, ratio * bclk); 1991103a8feaSLen Brown 1992103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 1993710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 1994103a8feaSLen Brown ratio, bclk, ratio * bclk); 1995103a8feaSLen Brown 19967ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 1997b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 1998bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 199967920418SLen Brown 2000fcd17211SLen Brown return; 2001fcd17211SLen Brown } 2002fcd17211SLen Brown 2003fcd17211SLen Brown static void 2004fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 2005fcd17211SLen Brown { 2006fcd17211SLen Brown unsigned long long msr; 2007fcd17211SLen Brown unsigned int ratio; 2008fcd17211SLen Brown 20097ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2010fcd17211SLen Brown 2011b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2012fcd17211SLen Brown 2013fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2014fcd17211SLen Brown if (ratio) 2015710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 2016fcd17211SLen Brown ratio, bclk, ratio * bclk); 2017fcd17211SLen Brown 2018fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2019fcd17211SLen Brown if (ratio) 2020710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 2021fcd17211SLen Brown ratio, bclk, ratio * bclk); 2022fcd17211SLen Brown return; 2023fcd17211SLen Brown } 2024fcd17211SLen Brown 2025fcd17211SLen Brown static void 2026fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 2027fcd17211SLen Brown { 2028fcd17211SLen Brown unsigned long long msr; 2029fcd17211SLen Brown unsigned int ratio; 20306574a5d5SLen Brown 20317ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 20326574a5d5SLen Brown 2033b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 20346574a5d5SLen Brown 20356574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 20366574a5d5SLen Brown if (ratio) 2037710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 20386574a5d5SLen Brown ratio, bclk, ratio * bclk); 20396574a5d5SLen Brown 20406574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 20416574a5d5SLen Brown if (ratio) 2042710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 20436574a5d5SLen Brown ratio, bclk, ratio * bclk); 20446574a5d5SLen Brown 20456574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 20466574a5d5SLen Brown if (ratio) 2047710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 20486574a5d5SLen Brown ratio, bclk, ratio * bclk); 20496574a5d5SLen Brown 20506574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 20516574a5d5SLen Brown if (ratio) 2052710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 20536574a5d5SLen Brown ratio, bclk, ratio * bclk); 20546574a5d5SLen Brown 20556574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 20566574a5d5SLen Brown if (ratio) 2057710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 20586574a5d5SLen Brown ratio, bclk, ratio * bclk); 20596574a5d5SLen Brown 20606574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 20616574a5d5SLen Brown if (ratio) 2062710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 20636574a5d5SLen Brown ratio, bclk, ratio * bclk); 20646574a5d5SLen Brown 20656574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 20666574a5d5SLen Brown if (ratio) 2067710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 20686574a5d5SLen Brown ratio, bclk, ratio * bclk); 20696574a5d5SLen Brown 20706574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 20716574a5d5SLen Brown if (ratio) 2072710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 20736574a5d5SLen Brown ratio, bclk, ratio * bclk); 2074103a8feaSLen Brown return; 2075fcd17211SLen Brown } 207631e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 207731e07522SLen Brown { 207831e07522SLen Brown 207931e07522SLen Brown if (!genuine_intel) 208031e07522SLen Brown return 0; 208131e07522SLen Brown 208231e07522SLen Brown switch (model) { 208331e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 208431e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 2085*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: 208631e07522SLen Brown return 1; 208731e07522SLen Brown } 208831e07522SLen Brown return 0; 208931e07522SLen Brown } 2090fcd17211SLen Brown 2091fcd17211SLen Brown static void 209231e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 2093fcd17211SLen Brown { 209431e07522SLen Brown unsigned long long msr, core_counts; 209531e07522SLen Brown unsigned int ratio, group_size; 2096103a8feaSLen Brown 20977ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2098b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 20996574a5d5SLen Brown 210031e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 210131e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 210231e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 210331e07522SLen Brown } else { 210431e07522SLen Brown core_counts = 0x0807060504030201; 210531e07522SLen Brown } 210631e07522SLen Brown 21076574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 210831e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 21096574a5d5SLen Brown if (ratio) 211031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 211131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 21126574a5d5SLen Brown 21136574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 211431e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 21156574a5d5SLen Brown if (ratio) 211631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 211731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 21186574a5d5SLen Brown 21196574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 212031e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 21216574a5d5SLen Brown if (ratio) 212231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 212331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 21246574a5d5SLen Brown 21256574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 212631e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 21276574a5d5SLen Brown if (ratio) 212831e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 212931e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 21306574a5d5SLen Brown 2131103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 213231e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2133103a8feaSLen Brown if (ratio) 213431e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 213531e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2136103a8feaSLen Brown 2137103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 213831e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2139103a8feaSLen Brown if (ratio) 214031e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 214131e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2142103a8feaSLen Brown 2143103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 214431e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2145103a8feaSLen Brown if (ratio) 214631e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 214731e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2148103a8feaSLen Brown 2149103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 215031e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2151103a8feaSLen Brown if (ratio) 215231e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 215331e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2154fcd17211SLen Brown return; 2155fcd17211SLen Brown } 21563a9a941dSLen Brown 2157fcd17211SLen Brown static void 21580f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 21590f7887c4SLen Brown { 21600f7887c4SLen Brown unsigned long long msr; 21610f7887c4SLen Brown unsigned int ratio; 21620f7887c4SLen Brown 21630f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 21640f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 21650f7887c4SLen Brown 21660f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 21670f7887c4SLen Brown if (ratio) 21680f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 21690f7887c4SLen Brown ratio, bclk, ratio * bclk); 21700f7887c4SLen Brown 21710f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 21720f7887c4SLen Brown if (ratio) 21730f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 21740f7887c4SLen Brown ratio, bclk, ratio * bclk); 21750f7887c4SLen Brown 21760f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 21770f7887c4SLen Brown if (ratio) 21780f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 21790f7887c4SLen Brown ratio, bclk, ratio * bclk); 21800f7887c4SLen Brown 21810f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 21820f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 21830f7887c4SLen Brown 21840f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 21850f7887c4SLen Brown if (ratio) 21860f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 21870f7887c4SLen Brown ratio, bclk, ratio * bclk); 21880f7887c4SLen Brown 21890f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 21900f7887c4SLen Brown if (ratio) 21910f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 21920f7887c4SLen Brown ratio, bclk, ratio * bclk); 21930f7887c4SLen Brown 21940f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 21950f7887c4SLen Brown if (ratio) 21960f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 21970f7887c4SLen Brown ratio, bclk, ratio * bclk); 21980f7887c4SLen Brown 21990f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 22000f7887c4SLen Brown if (ratio) 22010f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 22020f7887c4SLen Brown ratio, bclk, ratio * bclk); 22030f7887c4SLen Brown } 22040f7887c4SLen Brown 22050f7887c4SLen Brown static void 2206fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2207fb5d4327SDasaratharaman Chandramouli { 2208cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2209cbf97abaSHubert Chrzaniuk 2210fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2211cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2212cbf97abaSHubert Chrzaniuk int i, b_nr; 2213cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2214cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2215fb5d4327SDasaratharaman Chandramouli 2216ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2217fb5d4327SDasaratharaman Chandramouli 2218b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2219bfae2052SLen Brown base_cpu, msr); 2220fb5d4327SDasaratharaman Chandramouli 2221fb5d4327SDasaratharaman Chandramouli /** 2222fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2223cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2224cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2225fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2226fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2227fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2228fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2229fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2230fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2231fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2232fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2233fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2234fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2235fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2236fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2237fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2238fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2239fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2240fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2241fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2242fb5d4327SDasaratharaman Chandramouli */ 2243fb5d4327SDasaratharaman Chandramouli 2244cbf97abaSHubert Chrzaniuk b_nr = 0; 2245cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2246cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2247cbf97abaSHubert Chrzaniuk 2248cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2249fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2250cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2251fb5d4327SDasaratharaman Chandramouli 2252cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2253cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2254cbf97abaSHubert Chrzaniuk b_nr++; 2255fb5d4327SDasaratharaman Chandramouli } 2256cbf97abaSHubert Chrzaniuk 2257cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2258cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2259b7d8c148SLen Brown fprintf(outf, 2260710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2261cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2262fb5d4327SDasaratharaman Chandramouli } 2263fb5d4327SDasaratharaman Chandramouli 2264fb5d4327SDasaratharaman Chandramouli static void 2265fcd17211SLen Brown dump_nhm_cst_cfg(void) 2266fcd17211SLen Brown { 2267fcd17211SLen Brown unsigned long long msr; 2268fcd17211SLen Brown 22691df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2270fcd17211SLen Brown 22711df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2272fcd17211SLen Brown 22733e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2274fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2275fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2276fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2277fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2278fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 22796c34f160SLen Brown (unsigned int)msr & 0xF, 2280fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2281ac980e13SArtem Bityutskiy 2282ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2283ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2284ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2285ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2286ac980e13SArtem Bityutskiy } 2287ac980e13SArtem Bityutskiy 2288ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2289ac980e13SArtem Bityutskiy 2290fcd17211SLen Brown return; 2291103a8feaSLen Brown } 2292103a8feaSLen Brown 22936fb3143bSLen Brown static void 22946fb3143bSLen Brown dump_config_tdp(void) 22956fb3143bSLen Brown { 22966fb3143bSLen Brown unsigned long long msr; 22976fb3143bSLen Brown 22986fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2299b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2300685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 23016fb3143bSLen Brown 23026fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2303b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 23046fb3143bSLen Brown if (msr) { 2305685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2306685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2307685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2308685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 23096fb3143bSLen Brown } 2310b7d8c148SLen Brown fprintf(outf, ")\n"); 23116fb3143bSLen Brown 23126fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2313b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 23146fb3143bSLen Brown if (msr) { 2315685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2316685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2317685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2318685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 23196fb3143bSLen Brown } 2320b7d8c148SLen Brown fprintf(outf, ")\n"); 23216fb3143bSLen Brown 23226fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2323b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 23246fb3143bSLen Brown if ((msr) & 0x3) 2325b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2326b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2327b7d8c148SLen Brown fprintf(outf, ")\n"); 23286fb3143bSLen Brown 23296fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2330b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2331685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2332b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2333b7d8c148SLen Brown fprintf(outf, ")\n"); 23346fb3143bSLen Brown } 23355a63426eSLen Brown 23365a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 23375a63426eSLen Brown 23385a63426eSLen Brown void print_irtl(void) 23395a63426eSLen Brown { 23405a63426eSLen Brown unsigned long long msr; 23415a63426eSLen Brown 23425a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 23435a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 23445a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 23455a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 23465a63426eSLen Brown 23475a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 23485a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 23495a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 23505a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 23515a63426eSLen Brown 23525a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 23535a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 23545a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 23555a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 23565a63426eSLen Brown 23575a63426eSLen Brown if (!do_irtl_hsw) 23585a63426eSLen Brown return; 23595a63426eSLen Brown 23605a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 23615a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 23625a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 23635a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 23645a63426eSLen Brown 23655a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 23665a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 23675a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 23685a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 23695a63426eSLen Brown 23705a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 23715a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 23725a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 23735a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 23745a63426eSLen Brown 23755a63426eSLen Brown } 237636229897SLen Brown void free_fd_percpu(void) 237736229897SLen Brown { 237836229897SLen Brown int i; 237936229897SLen Brown 238001a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 238136229897SLen Brown if (fd_percpu[i] != 0) 238236229897SLen Brown close(fd_percpu[i]); 238336229897SLen Brown } 238436229897SLen Brown 238536229897SLen Brown free(fd_percpu); 23866fb3143bSLen Brown } 23876fb3143bSLen Brown 2388c98d5d94SLen Brown void free_all_buffers(void) 2389103a8feaSLen Brown { 23900e2d8f05SLen Brown int i; 23910e2d8f05SLen Brown 2392c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2393c98d5d94SLen Brown cpu_present_set = NULL; 239436229897SLen Brown cpu_present_setsize = 0; 2395103a8feaSLen Brown 2396c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2397c98d5d94SLen Brown cpu_affinity_set = NULL; 2398c98d5d94SLen Brown cpu_affinity_setsize = 0; 2399103a8feaSLen Brown 2400c98d5d94SLen Brown free(thread_even); 2401c98d5d94SLen Brown free(core_even); 2402c98d5d94SLen Brown free(package_even); 2403103a8feaSLen Brown 2404c98d5d94SLen Brown thread_even = NULL; 2405c98d5d94SLen Brown core_even = NULL; 2406c98d5d94SLen Brown package_even = NULL; 2407103a8feaSLen Brown 2408c98d5d94SLen Brown free(thread_odd); 2409c98d5d94SLen Brown free(core_odd); 2410c98d5d94SLen Brown free(package_odd); 2411103a8feaSLen Brown 2412c98d5d94SLen Brown thread_odd = NULL; 2413c98d5d94SLen Brown core_odd = NULL; 2414c98d5d94SLen Brown package_odd = NULL; 2415103a8feaSLen Brown 2416c98d5d94SLen Brown free(output_buffer); 2417c98d5d94SLen Brown output_buffer = NULL; 2418c98d5d94SLen Brown outp = NULL; 241936229897SLen Brown 242036229897SLen Brown free_fd_percpu(); 2421562a2d37SLen Brown 2422562a2d37SLen Brown free(irq_column_2_cpu); 2423562a2d37SLen Brown free(irqs_per_cpu); 24240e2d8f05SLen Brown 24250e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 24260e2d8f05SLen Brown if (cpus[i].put_ids) 24270e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 24280e2d8f05SLen Brown } 24290e2d8f05SLen Brown free(cpus); 2430103a8feaSLen Brown } 2431103a8feaSLen Brown 243257a42a34SJosh Triplett 243357a42a34SJosh Triplett /* 243495aebc44SJosh Triplett * Parse a file containing a single int. 243595aebc44SJosh Triplett */ 243695aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 243795aebc44SJosh Triplett { 243895aebc44SJosh Triplett va_list args; 243995aebc44SJosh Triplett char path[PATH_MAX]; 244095aebc44SJosh Triplett FILE *filep; 244195aebc44SJosh Triplett int value; 244295aebc44SJosh Triplett 244395aebc44SJosh Triplett va_start(args, fmt); 244495aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 244595aebc44SJosh Triplett va_end(args); 244657a42a34SJosh Triplett filep = fopen_or_die(path, "r"); 2447b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2448b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 244995aebc44SJosh Triplett fclose(filep); 245095aebc44SJosh Triplett return value; 245195aebc44SJosh Triplett } 245295aebc44SJosh Triplett 245395aebc44SJosh Triplett /* 2454c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2455c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2456c98d5d94SLen Brown */ 2457c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2458c98d5d94SLen Brown { 245995aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2460103a8feaSLen Brown } 2461103a8feaSLen Brown 2462103a8feaSLen Brown int get_physical_package_id(int cpu) 2463103a8feaSLen Brown { 246495aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2465103a8feaSLen Brown } 2466103a8feaSLen Brown 2467103a8feaSLen Brown int get_core_id(int cpu) 2468103a8feaSLen Brown { 246995aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2470103a8feaSLen Brown } 2471103a8feaSLen Brown 2472ef605741SPrarit Bhargava void set_node_data(void) 2473ef605741SPrarit Bhargava { 24742ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 24752ffbb224SPrarit Bhargava int cpu_count; 2476ef605741SPrarit Bhargava 24772ffbb224SPrarit Bhargava /* initialize logical_node_id */ 24782ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 24792ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2480ef605741SPrarit Bhargava 24812ffbb224SPrarit Bhargava cpu_count = 0; 24822ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 24832ffbb224SPrarit Bhargava lnode = 0; 24842ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 24852ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2486ef605741SPrarit Bhargava continue; 24872ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 24882ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 24892ffbb224SPrarit Bhargava continue; 24902ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2491ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 24922ffbb224SPrarit Bhargava cpu_count++; 24932ffbb224SPrarit Bhargava /* 24942ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 24952ffbb224SPrarit Bhargava * the logical_node_id 24962ffbb224SPrarit Bhargava */ 24972ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 24982ffbb224SPrarit Bhargava if ((cpus[cpux].physical_package_id == pkg) && 24992ffbb224SPrarit Bhargava (cpus[cpux].physical_node_id == node)) { 25002ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 25012ffbb224SPrarit Bhargava cpu_count++; 2502ef605741SPrarit Bhargava } 25032ffbb224SPrarit Bhargava } 25042ffbb224SPrarit Bhargava lnode++; 25052ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 25062ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 25072ffbb224SPrarit Bhargava } 25082ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 25092ffbb224SPrarit Bhargava break; 25102ffbb224SPrarit Bhargava } 2511ef605741SPrarit Bhargava } 2512ef605741SPrarit Bhargava 2513ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2514c98d5d94SLen Brown { 2515c98d5d94SLen Brown char path[80]; 2516c98d5d94SLen Brown FILE *filep; 25170e2d8f05SLen Brown int i; 25180e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2519c98d5d94SLen Brown 25200e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 25210e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 25220e2d8f05SLen Brown cpu, i); 25230e2d8f05SLen Brown filep = fopen(path, "r"); 25240e2d8f05SLen Brown if (!filep) 25250e2d8f05SLen Brown continue; 25260e2d8f05SLen Brown fclose(filep); 25270e2d8f05SLen Brown return i; 25280e2d8f05SLen Brown } 25290e2d8f05SLen Brown return -1; 2530e275b388SDasaratharaman Chandramouli } 2531c98d5d94SLen Brown 25320e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 25330e2d8f05SLen Brown { 25340e2d8f05SLen Brown char path[80], character; 25350e2d8f05SLen Brown FILE *filep; 25360e2d8f05SLen Brown unsigned long map; 25378cb48b32SPrarit Bhargava int so, shift, sib_core; 25380e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 25390e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 25400e2d8f05SLen Brown size_t size; 25418cb48b32SPrarit Bhargava int thread_id = 0; 25420e2d8f05SLen Brown 25430e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 25448cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 25458cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 25460e2d8f05SLen Brown if (!thiscpu->put_ids) 25470e2d8f05SLen Brown return -1; 25480e2d8f05SLen Brown 25490e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 25500e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 25510e2d8f05SLen Brown 25520e2d8f05SLen Brown sprintf(path, 25530e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 25540e2d8f05SLen Brown filep = fopen_or_die(path, "r"); 25550e2d8f05SLen Brown do { 25560e2d8f05SLen Brown offset -= BITMASK_SIZE; 25570e2d8f05SLen Brown fscanf(filep, "%lx%c", &map, &character); 25580e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 25590e2d8f05SLen Brown if ((map >> shift) & 0x1) { 25608cb48b32SPrarit Bhargava so = shift + offset; 25618cb48b32SPrarit Bhargava sib_core = get_core_id(so); 25628cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 25638cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 25648cb48b32SPrarit Bhargava if ((so != cpu) && 25658cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 25668cb48b32SPrarit Bhargava cpus[so].thread_id = 25678cb48b32SPrarit Bhargava thread_id++; 25688cb48b32SPrarit Bhargava } 25690e2d8f05SLen Brown } 25700e2d8f05SLen Brown } 25710e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2572c98d5d94SLen Brown fclose(filep); 25730e2d8f05SLen Brown 25740e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2575c98d5d94SLen Brown } 2576c98d5d94SLen Brown 2577c98d5d94SLen Brown /* 2578c98d5d94SLen Brown * run func(thread, core, package) in topology order 2579c98d5d94SLen Brown * skip non-present cpus 2580103a8feaSLen Brown */ 2581103a8feaSLen Brown 2582c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2583c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2584c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2585c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2586c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2587c98d5d94SLen Brown struct pkg_data *pkg_base2) 2588c98d5d94SLen Brown { 258940f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2590c98d5d94SLen Brown 2591c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 259240f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 259340f5cfe7SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; 259440f5cfe7SPrarit Bhargava ++core_no) { 2595c98d5d94SLen Brown for (thread_no = 0; thread_no < 259670a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2597c98d5d94SLen Brown struct thread_data *t, *t2; 2598c98d5d94SLen Brown struct core_data *c, *c2; 2599c98d5d94SLen Brown struct pkg_data *p, *p2; 2600c98d5d94SLen Brown 260140f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 260240f5cfe7SPrarit Bhargava core_no, node_no, 260340f5cfe7SPrarit Bhargava pkg_no); 2604c98d5d94SLen Brown 2605c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2606c98d5d94SLen Brown continue; 2607c98d5d94SLen Brown 260840f5cfe7SPrarit Bhargava t2 = GET_THREAD(thread_base2, thread_no, 260940f5cfe7SPrarit Bhargava core_no, node_no, 261040f5cfe7SPrarit Bhargava pkg_no); 2611c98d5d94SLen Brown 261240f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 261340f5cfe7SPrarit Bhargava node_no, pkg_no); 261440f5cfe7SPrarit Bhargava c2 = GET_CORE(core_base2, core_no, 261540f5cfe7SPrarit Bhargava node_no, 261640f5cfe7SPrarit Bhargava pkg_no); 2617c98d5d94SLen Brown 2618c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2619c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2620c98d5d94SLen Brown 2621c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2622c98d5d94SLen Brown if (retval) 2623c98d5d94SLen Brown return retval; 2624c98d5d94SLen Brown } 2625c98d5d94SLen Brown } 2626c98d5d94SLen Brown } 262740f5cfe7SPrarit Bhargava } 2628c98d5d94SLen Brown return 0; 2629c98d5d94SLen Brown } 2630c98d5d94SLen Brown 2631c98d5d94SLen Brown /* 2632c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2633c98d5d94SLen Brown * return max_cpu number 2634c98d5d94SLen Brown */ 2635c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2636103a8feaSLen Brown { 2637103a8feaSLen Brown FILE *fp; 2638c98d5d94SLen Brown int cpu_num; 2639103a8feaSLen Brown int retval; 2640103a8feaSLen Brown 264157a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2642103a8feaSLen Brown 2643103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2644b2c95d90SJosh Triplett if (retval != 0) 2645b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2646103a8feaSLen Brown 2647c98d5d94SLen Brown while (1) { 2648c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2649103a8feaSLen Brown if (retval != 1) 2650103a8feaSLen Brown break; 2651103a8feaSLen Brown 2652c98d5d94SLen Brown retval = func(cpu_num); 2653c98d5d94SLen Brown if (retval) { 2654c98d5d94SLen Brown fclose(fp); 2655c98d5d94SLen Brown return(retval); 2656c98d5d94SLen Brown } 2657103a8feaSLen Brown } 2658103a8feaSLen Brown fclose(fp); 2659c98d5d94SLen Brown return 0; 2660103a8feaSLen Brown } 2661103a8feaSLen Brown 2662103a8feaSLen Brown void re_initialize(void) 2663103a8feaSLen Brown { 2664c98d5d94SLen Brown free_all_buffers(); 2665c98d5d94SLen Brown setup_all_buffers(); 2666c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2667103a8feaSLen Brown } 2668103a8feaSLen Brown 2669843c5791SPrarit Bhargava void set_max_cpu_num(void) 2670843c5791SPrarit Bhargava { 2671843c5791SPrarit Bhargava FILE *filep; 2672843c5791SPrarit Bhargava unsigned long dummy; 2673843c5791SPrarit Bhargava 2674843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2675843c5791SPrarit Bhargava filep = fopen_or_die( 2676843c5791SPrarit Bhargava "/sys/devices/system/cpu/cpu0/topology/thread_siblings", 2677843c5791SPrarit Bhargava "r"); 2678843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 26790e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2680843c5791SPrarit Bhargava fclose(filep); 2681843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2682843c5791SPrarit Bhargava } 2683c98d5d94SLen Brown 2684103a8feaSLen Brown /* 2685c98d5d94SLen Brown * count_cpus() 2686c98d5d94SLen Brown * remember the last one seen, it will be the max 2687103a8feaSLen Brown */ 2688c98d5d94SLen Brown int count_cpus(int cpu) 2689103a8feaSLen Brown { 2690843c5791SPrarit Bhargava topo.num_cpus++; 2691c98d5d94SLen Brown return 0; 2692103a8feaSLen Brown } 2693c98d5d94SLen Brown int mark_cpu_present(int cpu) 2694c98d5d94SLen Brown { 2695c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 269615aaa346SLen Brown return 0; 2697103a8feaSLen Brown } 2698103a8feaSLen Brown 26998cb48b32SPrarit Bhargava int init_thread_id(int cpu) 27008cb48b32SPrarit Bhargava { 27018cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 27028cb48b32SPrarit Bhargava return 0; 27038cb48b32SPrarit Bhargava } 27048cb48b32SPrarit Bhargava 2705562a2d37SLen Brown /* 2706562a2d37SLen Brown * snapshot_proc_interrupts() 2707562a2d37SLen Brown * 2708562a2d37SLen Brown * read and record summary of /proc/interrupts 2709562a2d37SLen Brown * 2710562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2711562a2d37SLen Brown */ 2712562a2d37SLen Brown int snapshot_proc_interrupts(void) 2713562a2d37SLen Brown { 2714562a2d37SLen Brown static FILE *fp; 2715562a2d37SLen Brown int column, retval; 2716562a2d37SLen Brown 2717562a2d37SLen Brown if (fp == NULL) 2718562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2719562a2d37SLen Brown else 2720562a2d37SLen Brown rewind(fp); 2721562a2d37SLen Brown 2722562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2723562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2724562a2d37SLen Brown int cpu_number; 2725562a2d37SLen Brown 2726562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2727562a2d37SLen Brown if (retval != 1) 2728562a2d37SLen Brown break; 2729562a2d37SLen Brown 2730562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2731562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2732562a2d37SLen Brown return 1; 2733562a2d37SLen Brown } 2734562a2d37SLen Brown 2735562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2736562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2737562a2d37SLen Brown } 2738562a2d37SLen Brown 2739562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2740562a2d37SLen Brown while (1) { 2741562a2d37SLen Brown int column; 2742562a2d37SLen Brown char buf[64]; 2743562a2d37SLen Brown 2744562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2745562a2d37SLen Brown if (retval != 1) 2746562a2d37SLen Brown break; 2747562a2d37SLen Brown 2748562a2d37SLen Brown /* read the count per cpu */ 2749562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2750562a2d37SLen Brown 2751562a2d37SLen Brown int cpu_number, irq_count; 2752562a2d37SLen Brown 2753562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2754562a2d37SLen Brown if (retval != 1) 2755562a2d37SLen Brown break; 2756562a2d37SLen Brown 2757562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2758562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2759562a2d37SLen Brown 2760562a2d37SLen Brown } 2761562a2d37SLen Brown 2762562a2d37SLen Brown while (getc(fp) != '\n') 2763562a2d37SLen Brown ; /* flush interrupt description */ 2764562a2d37SLen Brown 2765562a2d37SLen Brown } 2766562a2d37SLen Brown return 0; 2767562a2d37SLen Brown } 276827d47356SLen Brown /* 2769fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2770fdf676e5SLen Brown * 2771fdf676e5SLen Brown * record snapshot of 2772fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2773fdf676e5SLen Brown * 2774fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2775fdf676e5SLen Brown */ 2776fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2777fdf676e5SLen Brown { 2778fdf676e5SLen Brown FILE *fp; 2779fdf676e5SLen Brown int retval; 2780fdf676e5SLen Brown 2781fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 2782fdf676e5SLen Brown 2783fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 2784fdf676e5SLen Brown if (retval != 1) 2785fdf676e5SLen Brown err(1, "GFX rc6"); 2786fdf676e5SLen Brown 2787fdf676e5SLen Brown fclose(fp); 2788fdf676e5SLen Brown 2789fdf676e5SLen Brown return 0; 2790fdf676e5SLen Brown } 2791fdf676e5SLen Brown /* 279227d47356SLen Brown * snapshot_gfx_mhz() 279327d47356SLen Brown * 279427d47356SLen Brown * record snapshot of 279527d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 279627d47356SLen Brown * 279727d47356SLen Brown * return 1 if config change requires a restart, else return 0 279827d47356SLen Brown */ 279927d47356SLen Brown int snapshot_gfx_mhz(void) 280027d47356SLen Brown { 280127d47356SLen Brown static FILE *fp; 280227d47356SLen Brown int retval; 280327d47356SLen Brown 280427d47356SLen Brown if (fp == NULL) 280527d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 280622048c54SLen Brown else { 280727d47356SLen Brown rewind(fp); 280822048c54SLen Brown fflush(fp); 280922048c54SLen Brown } 281027d47356SLen Brown 281127d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 281227d47356SLen Brown if (retval != 1) 281327d47356SLen Brown err(1, "GFX MHz"); 281427d47356SLen Brown 281527d47356SLen Brown return 0; 281627d47356SLen Brown } 2817562a2d37SLen Brown 2818562a2d37SLen Brown /* 2819be0e54c4SLen Brown * snapshot_cpu_lpi() 2820be0e54c4SLen Brown * 2821be0e54c4SLen Brown * record snapshot of 2822be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 2823be0e54c4SLen Brown * 2824be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2825be0e54c4SLen Brown */ 2826be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 2827be0e54c4SLen Brown { 2828be0e54c4SLen Brown FILE *fp; 2829be0e54c4SLen Brown int retval; 2830be0e54c4SLen Brown 2831be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 2832be0e54c4SLen Brown 2833be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 2834be0e54c4SLen Brown if (retval != 1) 2835be0e54c4SLen Brown err(1, "CPU LPI"); 2836be0e54c4SLen Brown 2837be0e54c4SLen Brown fclose(fp); 2838be0e54c4SLen Brown 2839be0e54c4SLen Brown return 0; 2840be0e54c4SLen Brown } 2841be0e54c4SLen Brown /* 2842be0e54c4SLen Brown * snapshot_sys_lpi() 2843be0e54c4SLen Brown * 2844be0e54c4SLen Brown * record snapshot of 2845be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us 2846be0e54c4SLen Brown * 2847be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2848be0e54c4SLen Brown */ 2849be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 2850be0e54c4SLen Brown { 2851be0e54c4SLen Brown FILE *fp; 2852be0e54c4SLen Brown int retval; 2853be0e54c4SLen Brown 2854be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r"); 2855be0e54c4SLen Brown 2856be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 2857be0e54c4SLen Brown if (retval != 1) 2858be0e54c4SLen Brown err(1, "SYS LPI"); 2859be0e54c4SLen Brown 2860be0e54c4SLen Brown fclose(fp); 2861be0e54c4SLen Brown 2862be0e54c4SLen Brown return 0; 2863be0e54c4SLen Brown } 2864be0e54c4SLen Brown /* 2865562a2d37SLen Brown * snapshot /proc and /sys files 2866562a2d37SLen Brown * 2867562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 2868562a2d37SLen Brown */ 2869562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 2870562a2d37SLen Brown { 2871218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 2872562a2d37SLen Brown if (snapshot_proc_interrupts()) 2873562a2d37SLen Brown return 1; 2874562a2d37SLen Brown 2875812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2876fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 2877fdf676e5SLen Brown 2878812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 287927d47356SLen Brown snapshot_gfx_mhz(); 288027d47356SLen Brown 2881be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2882be0e54c4SLen Brown snapshot_cpu_lpi_us(); 2883be0e54c4SLen Brown 2884be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2885be0e54c4SLen Brown snapshot_sys_lpi_us(); 2886be0e54c4SLen Brown 2887562a2d37SLen Brown return 0; 2888562a2d37SLen Brown } 2889562a2d37SLen Brown 28908aa2ed0bSLen Brown int exit_requested; 28918aa2ed0bSLen Brown 28928aa2ed0bSLen Brown static void signal_handler (int signal) 28938aa2ed0bSLen Brown { 28948aa2ed0bSLen Brown switch (signal) { 28958aa2ed0bSLen Brown case SIGINT: 28968aa2ed0bSLen Brown exit_requested = 1; 28978aa2ed0bSLen Brown if (debug) 28988aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 28998aa2ed0bSLen Brown break; 290007211960SLen Brown case SIGUSR1: 290107211960SLen Brown if (debug > 1) 290207211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 290307211960SLen Brown break; 29048aa2ed0bSLen Brown } 2905b9ad8ee0SLen Brown /* make sure this manually-invoked interval is at least 1ms long */ 2906b9ad8ee0SLen Brown nanosleep(&one_msec, NULL); 29078aa2ed0bSLen Brown } 29088aa2ed0bSLen Brown 29098aa2ed0bSLen Brown void setup_signal_handler(void) 29108aa2ed0bSLen Brown { 29118aa2ed0bSLen Brown struct sigaction sa; 29128aa2ed0bSLen Brown 29138aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 29148aa2ed0bSLen Brown 29158aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 29168aa2ed0bSLen Brown 29178aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 29188aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 291907211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 292007211960SLen Brown err(1, "sigaction SIGUSR1"); 29218aa2ed0bSLen Brown } 2922b9ad8ee0SLen Brown 292347936f94SArtem Bityutskiy void do_sleep(void) 2924b9ad8ee0SLen Brown { 2925b9ad8ee0SLen Brown struct timeval select_timeout; 2926b9ad8ee0SLen Brown fd_set readfds; 2927b9ad8ee0SLen Brown int retval; 2928b9ad8ee0SLen Brown 2929b9ad8ee0SLen Brown FD_ZERO(&readfds); 2930b9ad8ee0SLen Brown FD_SET(0, &readfds); 2931b9ad8ee0SLen Brown 293247936f94SArtem Bityutskiy if (!isatty(fileno(stdin))) { 293347936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 293447936f94SArtem Bityutskiy return; 293547936f94SArtem Bityutskiy } 2936b9ad8ee0SLen Brown 293747936f94SArtem Bityutskiy select_timeout = interval_tv; 2938b9ad8ee0SLen Brown retval = select(1, &readfds, NULL, NULL, &select_timeout); 2939b9ad8ee0SLen Brown 2940b9ad8ee0SLen Brown if (retval == 1) { 2941b9ad8ee0SLen Brown switch (getc(stdin)) { 2942b9ad8ee0SLen Brown case 'q': 2943b9ad8ee0SLen Brown exit_requested = 1; 2944b9ad8ee0SLen Brown break; 2945b9ad8ee0SLen Brown } 2946b9ad8ee0SLen Brown /* make sure this manually-invoked interval is at least 1ms long */ 2947b9ad8ee0SLen Brown nanosleep(&one_msec, NULL); 2948b9ad8ee0SLen Brown } 2949b9ad8ee0SLen Brown } 295047936f94SArtem Bityutskiy 29514c2122d4SLen Brown 2952103a8feaSLen Brown void turbostat_loop() 2953103a8feaSLen Brown { 2954c98d5d94SLen Brown int retval; 2955e52966c0SLen Brown int restarted = 0; 2956023fe0acSChen Yu int done_iters = 0; 2957c98d5d94SLen Brown 29588aa2ed0bSLen Brown setup_signal_handler(); 29598aa2ed0bSLen Brown 2960103a8feaSLen Brown restart: 2961e52966c0SLen Brown restarted++; 2962e52966c0SLen Brown 2963562a2d37SLen Brown snapshot_proc_sysfs_files(); 2964c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 29654c2122d4SLen Brown first_counter_read = 0; 2966d91bb17cSLen Brown if (retval < -1) { 2967d91bb17cSLen Brown exit(retval); 2968d91bb17cSLen Brown } else if (retval == -1) { 2969e52966c0SLen Brown if (restarted > 1) { 2970e52966c0SLen Brown exit(retval); 2971e52966c0SLen Brown } 2972c98d5d94SLen Brown re_initialize(); 2973c98d5d94SLen Brown goto restart; 2974c98d5d94SLen Brown } 2975e52966c0SLen Brown restarted = 0; 2976023fe0acSChen Yu done_iters = 0; 2977103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 2978103a8feaSLen Brown 2979103a8feaSLen Brown while (1) { 2980c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 2981103a8feaSLen Brown re_initialize(); 2982103a8feaSLen Brown goto restart; 2983103a8feaSLen Brown } 2984b9ad8ee0SLen Brown do_sleep(); 2985562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 2986562a2d37SLen Brown goto restart; 2987c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 2988d91bb17cSLen Brown if (retval < -1) { 2989d91bb17cSLen Brown exit(retval); 2990d91bb17cSLen Brown } else if (retval == -1) { 299115aaa346SLen Brown re_initialize(); 299215aaa346SLen Brown goto restart; 299315aaa346SLen Brown } 2994103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 2995103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 2996ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 2997ba3dec99SLen Brown re_initialize(); 2998ba3dec99SLen Brown goto restart; 2999ba3dec99SLen Brown } 3000c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3001c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3002b7d8c148SLen Brown flush_output_stdout(); 30038aa2ed0bSLen Brown if (exit_requested) 30048aa2ed0bSLen Brown break; 3005023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3006023fe0acSChen Yu break; 3007b9ad8ee0SLen Brown do_sleep(); 3008562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3009562a2d37SLen Brown goto restart; 3010c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3011d91bb17cSLen Brown if (retval < -1) { 3012d91bb17cSLen Brown exit(retval); 3013d91bb17cSLen Brown } else if (retval == -1) { 3014103a8feaSLen Brown re_initialize(); 3015103a8feaSLen Brown goto restart; 3016103a8feaSLen Brown } 3017103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3018103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3019ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3020ba3dec99SLen Brown re_initialize(); 3021ba3dec99SLen Brown goto restart; 3022ba3dec99SLen Brown } 3023c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3024c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3025b7d8c148SLen Brown flush_output_stdout(); 30268aa2ed0bSLen Brown if (exit_requested) 30278aa2ed0bSLen Brown break; 3028023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3029023fe0acSChen Yu break; 3030103a8feaSLen Brown } 3031103a8feaSLen Brown } 3032103a8feaSLen Brown 3033103a8feaSLen Brown void check_dev_msr() 3034103a8feaSLen Brown { 3035103a8feaSLen Brown struct stat sb; 30367ce7d5deSPrarit Bhargava char pathname[32]; 3037103a8feaSLen Brown 30387ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 30397ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3040a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3041d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3042103a8feaSLen Brown } 3043103a8feaSLen Brown 304498481e79SLen Brown void check_permissions() 3045103a8feaSLen Brown { 304698481e79SLen Brown struct __user_cap_header_struct cap_header_data; 304798481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 304898481e79SLen Brown struct __user_cap_data_struct cap_data_data; 304998481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 305098481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 305198481e79SLen Brown int do_exit = 0; 30527ce7d5deSPrarit Bhargava char pathname[32]; 305398481e79SLen Brown 305498481e79SLen Brown /* check for CAP_SYS_RAWIO */ 305598481e79SLen Brown cap_header->pid = getpid(); 305698481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 305798481e79SLen Brown if (capget(cap_header, cap_data) < 0) 305898481e79SLen Brown err(-6, "capget(2) failed"); 305998481e79SLen Brown 306098481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 306198481e79SLen Brown do_exit++; 306298481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 306398481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 306498481e79SLen Brown } 306598481e79SLen Brown 306698481e79SLen Brown /* test file permissions */ 30677ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 30687ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 306998481e79SLen Brown do_exit++; 307098481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 307198481e79SLen Brown } 307298481e79SLen Brown 307398481e79SLen Brown /* if all else fails, thell them to be root */ 307498481e79SLen Brown if (do_exit) 3075b2c95d90SJosh Triplett if (getuid() != 0) 3076d7899447SLen Brown warnx("... or simply run as root"); 307798481e79SLen Brown 307898481e79SLen Brown if (do_exit) 307998481e79SLen Brown exit(-6); 3080103a8feaSLen Brown } 3081103a8feaSLen Brown 3082d7899447SLen Brown /* 3083d7899447SLen Brown * NHM adds support for additional MSRs: 3084d7899447SLen Brown * 3085d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3086d7899447SLen Brown * 3087ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 30881df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3089d7899447SLen Brown * 3090cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3091cf4cbe53SLen Brown * 3092d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3093d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3094d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3095d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3096d7899447SLen Brown * 3097ee7e38e3SLen Brown * Side effect: 30981df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 309933148d67SLen Brown * sets has_misc_feature_control 3100d7899447SLen Brown */ 3101ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3102103a8feaSLen Brown { 3103ee7e38e3SLen Brown unsigned long long msr; 310421ed5574SLen Brown unsigned int base_ratio; 3105ee7e38e3SLen Brown int *pkg_cstate_limits; 3106ee7e38e3SLen Brown 3107103a8feaSLen Brown if (!genuine_intel) 3108103a8feaSLen Brown return 0; 3109103a8feaSLen Brown 3110103a8feaSLen Brown if (family != 6) 3111103a8feaSLen Brown return 0; 3112103a8feaSLen Brown 311321ed5574SLen Brown bclk = discover_bclk(family, model); 311421ed5574SLen Brown 3115103a8feaSLen Brown switch (model) { 3116869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 3117869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3118103a8feaSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 3119869ce69eSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 3120869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 3121869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3122869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 3123ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3124ee7e38e3SLen Brown break; 3125869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3126869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3127869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3128869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3129ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 313033148d67SLen Brown has_misc_feature_control = 1; 3131ee7e38e3SLen Brown break; 3132869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3133869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3134869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3135869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3136869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3137869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3138869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3139869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3140869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3141869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3142869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3143869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3144997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3145ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 314633148d67SLen Brown has_misc_feature_control = 1; 3147ee7e38e3SLen Brown break; 3148d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3149d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 315033148d67SLen Brown has_misc_feature_control = 1; 3151d8ebb442SLen Brown break; 3152*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3153cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3154*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 3155ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3156ee7e38e3SLen Brown break; 3157869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3158ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3159cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3160ee7e38e3SLen Brown break; 3161869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3162005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3163ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3164ee7e38e3SLen Brown break; 3165869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3166*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 3167*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 3168e4085d54SLen Brown pkg_cstate_limits = bxt_pkg_cstate_limits; 3169e4085d54SLen Brown break; 3170103a8feaSLen Brown default: 3171103a8feaSLen Brown return 0; 3172103a8feaSLen Brown } 31731df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3174e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3175ee7e38e3SLen Brown 3176ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 317721ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 317821ed5574SLen Brown 317921ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 318021ed5574SLen Brown has_base_hz = 1; 3181ee7e38e3SLen Brown return 1; 3182103a8feaSLen Brown } 31830f7887c4SLen Brown /* 3184495c7654SLen Brown * SLV client has support for unique MSRs: 31850f7887c4SLen Brown * 31860f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 31870f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 31880f7887c4SLen Brown */ 31890f7887c4SLen Brown 31900f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 31910f7887c4SLen Brown { 31920f7887c4SLen Brown if (!genuine_intel) 31930f7887c4SLen Brown return 0; 31940f7887c4SLen Brown 31950f7887c4SLen Brown switch (model) { 3196*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3197*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3198*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 31990f7887c4SLen Brown return 1; 32000f7887c4SLen Brown } 32010f7887c4SLen Brown return 0; 32020f7887c4SLen Brown } 32037170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 32047170a374SLen Brown { 32057170a374SLen Brown 32067170a374SLen Brown if (!genuine_intel) 32077170a374SLen Brown return 0; 32087170a374SLen Brown 32097170a374SLen Brown switch (model) { 3210*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: 32117170a374SLen Brown return 1; 32127170a374SLen Brown } 32137170a374SLen Brown return 0; 32147170a374SLen Brown } 3215ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3216ade0ebacSLen Brown { 3217ade0ebacSLen Brown 3218ade0ebacSLen Brown if (!genuine_intel) 3219ade0ebacSLen Brown return 0; 3220ade0ebacSLen Brown 3221ade0ebacSLen Brown switch (model) { 3222ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3223ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: 3224ade0ebacSLen Brown return 1; 3225ade0ebacSLen Brown } 3226ade0ebacSLen Brown return 0; 3227ade0ebacSLen Brown } 322834c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 322934c76197SLen Brown { 323034c76197SLen Brown 323134c76197SLen Brown if (!genuine_intel) 323234c76197SLen Brown return 0; 323334c76197SLen Brown 323434c76197SLen Brown switch (model) { 323534c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 323634c76197SLen Brown return 1; 323734c76197SLen Brown } 323834c76197SLen Brown return 0; 323934c76197SLen Brown } 32400f7887c4SLen Brown 324131e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3242d7899447SLen Brown { 32430f7887c4SLen Brown if (has_slv_msrs(family, model)) 32440f7887c4SLen Brown return 0; 32450f7887c4SLen Brown 3246d7899447SLen Brown switch (model) { 3247d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3248869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3249869ce69eSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 3250869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3251005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3252d7899447SLen Brown return 0; 3253d7899447SLen Brown default: 3254d7899447SLen Brown return 1; 3255d7899447SLen Brown } 3256d7899447SLen Brown } 32570f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 32580f7887c4SLen Brown { 32590f7887c4SLen Brown if (has_slv_msrs(family, model)) 32600f7887c4SLen Brown return 1; 32610f7887c4SLen Brown 32620f7887c4SLen Brown return 0; 32630f7887c4SLen Brown } 32646574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 32656574a5d5SLen Brown { 32666574a5d5SLen Brown if (!genuine_intel) 32676574a5d5SLen Brown return 0; 32686574a5d5SLen Brown 32696574a5d5SLen Brown if (family != 6) 32706574a5d5SLen Brown return 0; 32716574a5d5SLen Brown 32726574a5d5SLen Brown switch (model) { 3273869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3274869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 32756574a5d5SLen Brown return 1; 32766574a5d5SLen Brown default: 32776574a5d5SLen Brown return 0; 32786574a5d5SLen Brown } 32796574a5d5SLen Brown } 3280fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3281fcd17211SLen Brown { 3282fcd17211SLen Brown if (!genuine_intel) 3283fcd17211SLen Brown return 0; 3284fcd17211SLen Brown 3285fcd17211SLen Brown if (family != 6) 3286fcd17211SLen Brown return 0; 3287fcd17211SLen Brown 3288fcd17211SLen Brown switch (model) { 3289869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3290fcd17211SLen Brown return 1; 3291fcd17211SLen Brown default: 3292fcd17211SLen Brown return 0; 3293fcd17211SLen Brown } 3294fcd17211SLen Brown } 3295fcd17211SLen Brown 3296fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3297fb5d4327SDasaratharaman Chandramouli { 3298fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3299fb5d4327SDasaratharaman Chandramouli return 0; 3300fb5d4327SDasaratharaman Chandramouli 3301fb5d4327SDasaratharaman Chandramouli if (family != 6) 3302fb5d4327SDasaratharaman Chandramouli return 0; 3303fb5d4327SDasaratharaman Chandramouli 3304fb5d4327SDasaratharaman Chandramouli switch (model) { 3305869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3306005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 3307fb5d4327SDasaratharaman Chandramouli return 1; 3308fb5d4327SDasaratharaman Chandramouli default: 3309fb5d4327SDasaratharaman Chandramouli return 0; 3310fb5d4327SDasaratharaman Chandramouli } 3311fb5d4327SDasaratharaman Chandramouli } 331231e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 331331e07522SLen Brown { 331431e07522SLen Brown if (!genuine_intel) 331531e07522SLen Brown return 0; 331631e07522SLen Brown 331731e07522SLen Brown if (family != 6) 331831e07522SLen Brown return 0; 331931e07522SLen Brown 332031e07522SLen Brown switch (model) { 332131e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 332231e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 332331e07522SLen Brown return 1; 332431e07522SLen Brown default: 332531e07522SLen Brown return 0; 332631e07522SLen Brown } 332731e07522SLen Brown } 33286fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 33296fb3143bSLen Brown { 33306fb3143bSLen Brown if (!genuine_intel) 33316fb3143bSLen Brown return 0; 33326fb3143bSLen Brown 33336fb3143bSLen Brown if (family != 6) 33346fb3143bSLen Brown return 0; 33356fb3143bSLen Brown 33366fb3143bSLen Brown switch (model) { 3337869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3338869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3339869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3340869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3341869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3342869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3343869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3344869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3345869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3346869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3347869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3348869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3349869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3350997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3351869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 33526fb3143bSLen Brown 3353869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3354005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 33556fb3143bSLen Brown return 1; 33566fb3143bSLen Brown default: 33576fb3143bSLen Brown return 0; 33586fb3143bSLen Brown } 33596fb3143bSLen Brown } 33606fb3143bSLen Brown 3361fcd17211SLen Brown static void 33621b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3363fcd17211SLen Brown { 3364fcd17211SLen Brown if (!do_nhm_platform_info) 3365fcd17211SLen Brown return; 3366fcd17211SLen Brown 3367fcd17211SLen Brown dump_nhm_platform_info(); 3368fcd17211SLen Brown 3369fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3370fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3371fcd17211SLen Brown 3372fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3373fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3374fcd17211SLen Brown 337531e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 337631e07522SLen Brown dump_turbo_ratio_limits(family, model); 3377fcd17211SLen Brown 33780f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 33790f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 33800f7887c4SLen Brown 3381fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3382fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3383fb5d4327SDasaratharaman Chandramouli 33846fb3143bSLen Brown if (has_config_tdp(family, model)) 33856fb3143bSLen Brown dump_config_tdp(); 33866fb3143bSLen Brown 3387fcd17211SLen Brown dump_nhm_cst_cfg(); 3388fcd17211SLen Brown } 3389fcd17211SLen Brown 339041618e63SLen Brown static void 339141618e63SLen Brown dump_sysfs_cstate_config(void) 339241618e63SLen Brown { 339341618e63SLen Brown char path[64]; 339441618e63SLen Brown char name_buf[16]; 339541618e63SLen Brown char desc[64]; 339641618e63SLen Brown FILE *input; 339741618e63SLen Brown int state; 339841618e63SLen Brown char *sp; 339941618e63SLen Brown 340041618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 340141618e63SLen Brown return; 340241618e63SLen Brown 340341618e63SLen Brown for (state = 0; state < 10; ++state) { 340441618e63SLen Brown 340541618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 340641618e63SLen Brown base_cpu, state); 340741618e63SLen Brown input = fopen(path, "r"); 340841618e63SLen Brown if (input == NULL) 340941618e63SLen Brown continue; 341041618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 341141618e63SLen Brown 341241618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 341341618e63SLen Brown sp = strchr(name_buf, '-'); 341441618e63SLen Brown if (!sp) 341541618e63SLen Brown sp = strchrnul(name_buf, '\n'); 341641618e63SLen Brown *sp = '\0'; 341741618e63SLen Brown 341841618e63SLen Brown fclose(input); 341941618e63SLen Brown 342041618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 342141618e63SLen Brown base_cpu, state); 342241618e63SLen Brown input = fopen(path, "r"); 342341618e63SLen Brown if (input == NULL) 342441618e63SLen Brown continue; 342541618e63SLen Brown fgets(desc, sizeof(desc), input); 342641618e63SLen Brown 342741618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 342841618e63SLen Brown fclose(input); 342941618e63SLen Brown } 343041618e63SLen Brown } 34317293fccdSLen Brown static void 34327293fccdSLen Brown dump_sysfs_pstate_config(void) 34337293fccdSLen Brown { 34347293fccdSLen Brown char path[64]; 34357293fccdSLen Brown char driver_buf[64]; 34367293fccdSLen Brown char governor_buf[64]; 34377293fccdSLen Brown FILE *input; 34387293fccdSLen Brown int turbo; 34397293fccdSLen Brown 34407293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 34417293fccdSLen Brown base_cpu); 34427293fccdSLen Brown input = fopen(path, "r"); 34437293fccdSLen Brown if (input == NULL) { 34447293fccdSLen Brown fprintf(stderr, "NSFOD %s\n", path); 34457293fccdSLen Brown return; 34467293fccdSLen Brown } 34477293fccdSLen Brown fgets(driver_buf, sizeof(driver_buf), input); 34487293fccdSLen Brown fclose(input); 34497293fccdSLen Brown 34507293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 34517293fccdSLen Brown base_cpu); 34527293fccdSLen Brown input = fopen(path, "r"); 34537293fccdSLen Brown if (input == NULL) { 34547293fccdSLen Brown fprintf(stderr, "NSFOD %s\n", path); 34557293fccdSLen Brown return; 34567293fccdSLen Brown } 34577293fccdSLen Brown fgets(governor_buf, sizeof(governor_buf), input); 34587293fccdSLen Brown fclose(input); 34597293fccdSLen Brown 34607293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 34617293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 34627293fccdSLen Brown 34637293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 34647293fccdSLen Brown input = fopen(path, "r"); 34657293fccdSLen Brown if (input != NULL) { 34667293fccdSLen Brown fscanf(input, "%d", &turbo); 34677293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 34687293fccdSLen Brown fclose(input); 34697293fccdSLen Brown } 34707293fccdSLen Brown 34717293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 34727293fccdSLen Brown input = fopen(path, "r"); 34737293fccdSLen Brown if (input != NULL) { 34747293fccdSLen Brown fscanf(input, "%d", &turbo); 34757293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 34767293fccdSLen Brown fclose(input); 34777293fccdSLen Brown } 34787293fccdSLen Brown } 347941618e63SLen Brown 34806574a5d5SLen Brown 3481889facbeSLen Brown /* 3482889facbeSLen Brown * print_epb() 3483889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3484889facbeSLen Brown */ 3485889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3486889facbeSLen Brown { 3487889facbeSLen Brown unsigned long long msr; 3488889facbeSLen Brown char *epb_string; 3489889facbeSLen Brown int cpu; 3490889facbeSLen Brown 3491889facbeSLen Brown if (!has_epb) 3492889facbeSLen Brown return 0; 3493889facbeSLen Brown 3494889facbeSLen Brown cpu = t->cpu_id; 3495889facbeSLen Brown 3496889facbeSLen Brown /* EPB is per-package */ 3497889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3498889facbeSLen Brown return 0; 3499889facbeSLen Brown 3500889facbeSLen Brown if (cpu_migrate(cpu)) { 3501b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3502889facbeSLen Brown return -1; 3503889facbeSLen Brown } 3504889facbeSLen Brown 3505889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 3506889facbeSLen Brown return 0; 3507889facbeSLen Brown 3508e9be7dd6SLen Brown switch (msr & 0xF) { 3509889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3510889facbeSLen Brown epb_string = "performance"; 3511889facbeSLen Brown break; 3512889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3513889facbeSLen Brown epb_string = "balanced"; 3514889facbeSLen Brown break; 3515889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3516889facbeSLen Brown epb_string = "powersave"; 3517889facbeSLen Brown break; 3518889facbeSLen Brown default: 3519889facbeSLen Brown epb_string = "custom"; 3520889facbeSLen Brown break; 3521889facbeSLen Brown } 3522b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 3523889facbeSLen Brown 3524889facbeSLen Brown return 0; 3525889facbeSLen Brown } 35267f5c258eSLen Brown /* 35277f5c258eSLen Brown * print_hwp() 35287f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 35297f5c258eSLen Brown */ 35307f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 35317f5c258eSLen Brown { 35327f5c258eSLen Brown unsigned long long msr; 35337f5c258eSLen Brown int cpu; 35347f5c258eSLen Brown 35357f5c258eSLen Brown if (!has_hwp) 35367f5c258eSLen Brown return 0; 35377f5c258eSLen Brown 35387f5c258eSLen Brown cpu = t->cpu_id; 35397f5c258eSLen Brown 35407f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 35417f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 35427f5c258eSLen Brown return 0; 35437f5c258eSLen Brown 35447f5c258eSLen Brown if (cpu_migrate(cpu)) { 3545b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 35467f5c258eSLen Brown return -1; 35477f5c258eSLen Brown } 35487f5c258eSLen Brown 35497f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 35507f5c258eSLen Brown return 0; 35517f5c258eSLen Brown 3552b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 35537f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 35547f5c258eSLen Brown 35557f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 35567f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 35577f5c258eSLen Brown return 0; 35587f5c258eSLen Brown 35597f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 35607f5c258eSLen Brown return 0; 35617f5c258eSLen Brown 3562b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 35636dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 35647f5c258eSLen Brown cpu, msr, 35657f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 35667f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 35677f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 35687f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 35697f5c258eSLen Brown 35707f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 35717f5c258eSLen Brown return 0; 35727f5c258eSLen Brown 3573b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 35746dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 35757f5c258eSLen Brown cpu, msr, 35767f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 35777f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 35787f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 35797f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 35807f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 35817f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 35827f5c258eSLen Brown 35837f5c258eSLen Brown if (has_hwp_pkg) { 35847f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 35857f5c258eSLen Brown return 0; 35867f5c258eSLen Brown 3587b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 35886dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 35897f5c258eSLen Brown cpu, msr, 35907f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 35917f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 35927f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 35937f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 35947f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 35957f5c258eSLen Brown } 35967f5c258eSLen Brown if (has_hwp_notify) { 35977f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 35987f5c258eSLen Brown return 0; 35997f5c258eSLen Brown 3600b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 36017f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 36027f5c258eSLen Brown cpu, msr, 36037f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 36047f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 36057f5c258eSLen Brown } 36067f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 36077f5c258eSLen Brown return 0; 36087f5c258eSLen Brown 3609b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 36107f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 36117f5c258eSLen Brown cpu, msr, 36127f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 36137f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 3614889facbeSLen Brown 3615889facbeSLen Brown return 0; 3616889facbeSLen Brown } 3617889facbeSLen Brown 36183a9a941dSLen Brown /* 36193a9a941dSLen Brown * print_perf_limit() 36203a9a941dSLen Brown */ 36213a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 36223a9a941dSLen Brown { 36233a9a941dSLen Brown unsigned long long msr; 36243a9a941dSLen Brown int cpu; 36253a9a941dSLen Brown 36263a9a941dSLen Brown cpu = t->cpu_id; 36273a9a941dSLen Brown 36283a9a941dSLen Brown /* per-package */ 36293a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 36303a9a941dSLen Brown return 0; 36313a9a941dSLen Brown 36323a9a941dSLen Brown if (cpu_migrate(cpu)) { 3633b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 36343a9a941dSLen Brown return -1; 36353a9a941dSLen Brown } 36363a9a941dSLen Brown 36373a9a941dSLen Brown if (do_core_perf_limit_reasons) { 36383a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 3639b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3640b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 3641e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 36423a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 3643e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 3644e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 3645e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 3646e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 3647e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 3648e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 3649e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 3650e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 3651e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 3652e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 3653e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 3654e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 3655b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 3656e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 36573a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 3658e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 3659e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 3660e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 3661e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 3662e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 3663e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 3664e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 3665e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 3666e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 3667e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 3668e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 3669e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 36703a9a941dSLen Brown 36713a9a941dSLen Brown } 36723a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 36733a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 3674b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3675b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 36763a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 36773a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 36783a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 36793a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 36803a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 36813a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 36823a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 36833a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3684b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 36853a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 36863a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 36873a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 36883a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 36893a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 36903a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 36913a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 36923a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 36933a9a941dSLen Brown } 36943a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 36953a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 3696b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3697b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 36983a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 36993a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 37003a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 37013a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 37023a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 37033a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3704b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 37053a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 37063a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 37073a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 37083a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 37093a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 37103a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 37113a9a941dSLen Brown } 37123a9a941dSLen Brown return 0; 37133a9a941dSLen Brown } 37143a9a941dSLen Brown 3715889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 3716889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 3717889facbeSLen Brown 37181b69317dSColin Ian King double get_tdp(unsigned int model) 3719144b44b1SLen Brown { 3720144b44b1SLen Brown unsigned long long msr; 3721144b44b1SLen Brown 3722144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 37237ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 3724144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 3725144b44b1SLen Brown 3726144b44b1SLen Brown switch (model) { 3727*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3728*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: 3729144b44b1SLen Brown return 30.0; 3730144b44b1SLen Brown default: 3731144b44b1SLen Brown return 135.0; 3732144b44b1SLen Brown } 3733144b44b1SLen Brown } 3734144b44b1SLen Brown 373540ee8e3bSAndrey Semin /* 373640ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 373740ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 373840ee8e3bSAndrey Semin */ 373940ee8e3bSAndrey Semin static double 374040ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 374140ee8e3bSAndrey Semin { 374240ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 374340ee8e3bSAndrey Semin 374440ee8e3bSAndrey Semin switch (model) { 3745869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3746869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3747869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3748869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3749005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 375040ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 375140ee8e3bSAndrey Semin default: 375240ee8e3bSAndrey Semin return (rapl_energy_units); 375340ee8e3bSAndrey Semin } 375440ee8e3bSAndrey Semin } 375540ee8e3bSAndrey Semin 3756144b44b1SLen Brown 3757889facbeSLen Brown /* 3758889facbeSLen Brown * rapl_probe() 3759889facbeSLen Brown * 3760144b44b1SLen Brown * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 3761889facbeSLen Brown */ 3762889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model) 3763889facbeSLen Brown { 3764889facbeSLen Brown unsigned long long msr; 3765144b44b1SLen Brown unsigned int time_unit; 3766889facbeSLen Brown double tdp; 3767889facbeSLen Brown 3768889facbeSLen Brown if (!genuine_intel) 3769889facbeSLen Brown return; 3770889facbeSLen Brown 3771889facbeSLen Brown if (family != 6) 3772889facbeSLen Brown return; 3773889facbeSLen Brown 3774889facbeSLen Brown switch (model) { 3775869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3776869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 3777869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3778869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3779869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3780869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3781869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3782144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 3783812db3f7SLen Brown if (rapl_joules) { 3784812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3785812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3786812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 3787812db3f7SLen Brown } else { 3788812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3789812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3790812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 3791812db3f7SLen Brown } 3792889facbeSLen Brown break; 3793869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3794*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 3795e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 3796812db3f7SLen Brown if (rapl_joules) 3797812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3798812db3f7SLen Brown else 3799812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3800e4085d54SLen Brown break; 3801869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3802869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 3803869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 3804869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 3805997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 380681824921SLen 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; 3807812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3808812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3809812db3f7SLen Brown if (rapl_joules) { 3810812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3811812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3812812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 381381824921SLen Brown BIC_PRESENT(BIC_GFX_J); 3814812db3f7SLen Brown } else { 3815812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3816812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3817812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 381881824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 3819812db3f7SLen Brown } 38200b2bb692SLen Brown break; 3821869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3822869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3823869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 3824869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3825869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 3826005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 38270b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 3828812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3829812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3830812db3f7SLen Brown if (rapl_joules) { 3831812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3832812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3833812db3f7SLen Brown } else { 3834812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3835812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3836812db3f7SLen Brown } 3837e6f9bb3cSLen Brown break; 3838869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3839869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 38400b2bb692SLen 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; 3841812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3842812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3843812db3f7SLen Brown if (rapl_joules) { 3844812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3845812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3846812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3847812db3f7SLen Brown } else { 3848812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3849812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3850812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3851812db3f7SLen Brown } 3852144b44b1SLen Brown break; 3853*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3854*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 3855144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 3856812db3f7SLen Brown if (rapl_joules) { 3857812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3858812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3859812db3f7SLen Brown } else { 3860812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3861812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3862812db3f7SLen Brown } 3863889facbeSLen Brown break; 3864*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 38650f644909SJacob 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; 3866812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3867812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3868812db3f7SLen Brown if (rapl_joules) { 3869812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3870812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3871812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3872812db3f7SLen Brown } else { 3873812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3874812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3875812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3876812db3f7SLen Brown } 38770f644909SJacob Pan break; 3878889facbeSLen Brown default: 3879889facbeSLen Brown return; 3880889facbeSLen Brown } 3881889facbeSLen Brown 3882889facbeSLen Brown /* units on package 0, verify later other packages match */ 38837ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 3884889facbeSLen Brown return; 3885889facbeSLen Brown 3886889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 3887*f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 3888144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 3889144b44b1SLen Brown else 3890889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 3891889facbeSLen Brown 389240ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 389340ee8e3bSAndrey Semin 3894144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 3895144b44b1SLen Brown if (time_unit == 0) 3896144b44b1SLen Brown time_unit = 0xA; 3897889facbeSLen Brown 3898144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 3899144b44b1SLen Brown 3900144b44b1SLen Brown tdp = get_tdp(model); 3901889facbeSLen Brown 3902889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 390396e47158SLen Brown if (!quiet) 3904b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 3905889facbeSLen Brown 3906889facbeSLen Brown return; 3907889facbeSLen Brown } 3908889facbeSLen Brown 39091b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 39103a9a941dSLen Brown { 39113a9a941dSLen Brown if (!genuine_intel) 39123a9a941dSLen Brown return; 39133a9a941dSLen Brown 39143a9a941dSLen Brown if (family != 6) 39153a9a941dSLen Brown return; 39163a9a941dSLen Brown 39173a9a941dSLen Brown switch (model) { 3918869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3919869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3920869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 39213a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 3922869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 39233a9a941dSLen Brown do_core_perf_limit_reasons = 1; 39243a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 39253a9a941dSLen Brown default: 39263a9a941dSLen Brown return; 39273a9a941dSLen Brown } 39283a9a941dSLen Brown } 39293a9a941dSLen Brown 3930ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 3931ac980e13SArtem Bityutskiy { 3932ac980e13SArtem Bityutskiy if (is_skx(family, model) || is_bdx(family, model)) 3933ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 3934ac980e13SArtem Bityutskiy } 3935ac980e13SArtem Bityutskiy 3936889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3937889facbeSLen Brown { 3938889facbeSLen Brown unsigned long long msr; 3939f4896fa5SLen Brown unsigned int dts, dts2; 3940889facbeSLen Brown int cpu; 3941889facbeSLen Brown 3942889facbeSLen Brown if (!(do_dts || do_ptm)) 3943889facbeSLen Brown return 0; 3944889facbeSLen Brown 3945889facbeSLen Brown cpu = t->cpu_id; 3946889facbeSLen Brown 3947889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 3948889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 3949889facbeSLen Brown return 0; 3950889facbeSLen Brown 3951889facbeSLen Brown if (cpu_migrate(cpu)) { 3952b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3953889facbeSLen Brown return -1; 3954889facbeSLen Brown } 3955889facbeSLen Brown 3956889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 3957889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 3958889facbeSLen Brown return 0; 3959889facbeSLen Brown 3960889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3961b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 3962889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 3963889facbeSLen Brown 3964889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 3965889facbeSLen Brown return 0; 3966889facbeSLen Brown 3967889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3968889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3969b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3970889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3971889facbeSLen Brown } 3972889facbeSLen Brown 3973889facbeSLen Brown 3974f4896fa5SLen Brown if (do_dts && debug) { 3975889facbeSLen Brown unsigned int resolution; 3976889facbeSLen Brown 3977889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 3978889facbeSLen Brown return 0; 3979889facbeSLen Brown 3980889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3981889facbeSLen Brown resolution = (msr >> 27) & 0xF; 3982b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 3983889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 3984889facbeSLen Brown 3985889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 3986889facbeSLen Brown return 0; 3987889facbeSLen Brown 3988889facbeSLen Brown dts = (msr >> 16) & 0x7F; 3989889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 3990b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 3991889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 3992889facbeSLen Brown } 3993889facbeSLen Brown 3994889facbeSLen Brown return 0; 3995889facbeSLen Brown } 3996889facbeSLen Brown 3997889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 3998889facbeSLen Brown { 3999b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4000889facbeSLen Brown cpu, label, 4001889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4002889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4003889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4004889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4005889facbeSLen Brown 4006889facbeSLen Brown return; 4007889facbeSLen Brown } 4008889facbeSLen Brown 4009889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4010889facbeSLen Brown { 4011889facbeSLen Brown unsigned long long msr; 4012889facbeSLen Brown int cpu; 4013889facbeSLen Brown 4014889facbeSLen Brown if (!do_rapl) 4015889facbeSLen Brown return 0; 4016889facbeSLen Brown 4017889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4018889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4019889facbeSLen Brown return 0; 4020889facbeSLen Brown 4021889facbeSLen Brown cpu = t->cpu_id; 4022889facbeSLen Brown if (cpu_migrate(cpu)) { 4023b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4024889facbeSLen Brown return -1; 4025889facbeSLen Brown } 4026889facbeSLen Brown 4027889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4028889facbeSLen Brown return -1; 4029889facbeSLen Brown 403096e47158SLen Brown fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr, 4031144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 403296e47158SLen Brown 4033144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4034144b44b1SLen Brown 4035889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4036889facbeSLen Brown return -5; 4037889facbeSLen Brown 4038889facbeSLen Brown 4039b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4040889facbeSLen Brown cpu, msr, 4041889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4042889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4043889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4044889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4045889facbeSLen Brown 4046144b44b1SLen Brown } 4047144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4048144b44b1SLen Brown 4049889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4050889facbeSLen Brown return -9; 4051889facbeSLen Brown 4052b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4053889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4054889facbeSLen Brown 4055889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4056b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4057889facbeSLen Brown cpu, 4058889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4059889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4060889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4061889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4062889facbeSLen Brown } 4063889facbeSLen Brown 40640b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4065889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4066889facbeSLen Brown return -6; 4067889facbeSLen Brown 4068b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4069889facbeSLen Brown cpu, msr, 4070889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4071889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4072889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4073889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 40740b2bb692SLen Brown } 40750b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4076889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4077889facbeSLen Brown return -9; 4078b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4079889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4080889facbeSLen Brown 4081889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4082889facbeSLen Brown } 4083144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4084889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4085889facbeSLen Brown return -7; 4086889facbeSLen Brown 4087b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4088144b44b1SLen Brown } 40899148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4090889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4091889facbeSLen Brown return -9; 4092b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4093889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4094889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4095889facbeSLen Brown } 4096889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4097889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4098889facbeSLen Brown return -8; 4099889facbeSLen Brown 4100b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4101889facbeSLen Brown 4102889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4103889facbeSLen Brown return -9; 4104b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4105889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4106889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4107889facbeSLen Brown } 4108889facbeSLen Brown return 0; 4109889facbeSLen Brown } 4110889facbeSLen Brown 4111d7899447SLen Brown /* 4112d7899447SLen Brown * SNB adds support for additional MSRs: 4113d7899447SLen Brown * 4114d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4115d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4116d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4117d7899447SLen Brown */ 4118103a8feaSLen Brown 4119d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4120103a8feaSLen Brown { 4121103a8feaSLen Brown if (!genuine_intel) 4122103a8feaSLen Brown return 0; 4123103a8feaSLen Brown 4124103a8feaSLen Brown switch (model) { 4125869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4126869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4127869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4128869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4129869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 4130869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 4131869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4132869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 4133869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4134869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 4135869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4136869ce69eSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 4137869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4138869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4139869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4140869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4141997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4142869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4143869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4144*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4145*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 4146103a8feaSLen Brown return 1; 4147103a8feaSLen Brown } 4148103a8feaSLen Brown return 0; 4149103a8feaSLen Brown } 4150103a8feaSLen Brown 4151d7899447SLen Brown /* 4152d7899447SLen Brown * HSW adds support for additional MSRs: 4153d7899447SLen Brown * 4154d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4155d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4156d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 41575a63426eSLen Brown * 41585a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 41595a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 41605a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 41615a63426eSLen Brown * 4162d7899447SLen Brown */ 4163d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model) 4164ca58710fSKristen Carlson Accardi { 4165ca58710fSKristen Carlson Accardi if (!genuine_intel) 4166ca58710fSKristen Carlson Accardi return 0; 4167ca58710fSKristen Carlson Accardi 4168ca58710fSKristen Carlson Accardi switch (model) { 4169869ce69eSLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4170869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4171869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4172869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4173869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4174869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4175997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4176869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4177*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4178ca58710fSKristen Carlson Accardi return 1; 4179ca58710fSKristen Carlson Accardi } 4180ca58710fSKristen Carlson Accardi return 0; 4181ca58710fSKristen Carlson Accardi } 4182ca58710fSKristen Carlson Accardi 41830b2bb692SLen Brown /* 41840b2bb692SLen Brown * SKL adds support for additional MSRS: 41850b2bb692SLen Brown * 41860b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 41870b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 41880b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 41890b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 41900b2bb692SLen Brown */ 41910b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 41920b2bb692SLen Brown { 41930b2bb692SLen Brown if (!genuine_intel) 41940b2bb692SLen Brown return 0; 41950b2bb692SLen Brown 41960b2bb692SLen Brown switch (model) { 4197869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4198869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4199869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4200869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4201997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 42020b2bb692SLen Brown return 1; 42030b2bb692SLen Brown } 42040b2bb692SLen Brown return 0; 42050b2bb692SLen Brown } 42060b2bb692SLen Brown 4207144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4208144b44b1SLen Brown { 4209144b44b1SLen Brown if (!genuine_intel) 4210144b44b1SLen Brown return 0; 4211144b44b1SLen Brown switch (model) { 4212*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 4213*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 4214144b44b1SLen Brown return 1; 4215144b44b1SLen Brown } 4216144b44b1SLen Brown return 0; 4217144b44b1SLen Brown } 4218144b44b1SLen Brown 4219fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4220fb5d4327SDasaratharaman Chandramouli { 4221fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4222fb5d4327SDasaratharaman Chandramouli return 0; 4223fb5d4327SDasaratharaman Chandramouli switch (model) { 4224869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4225005c82d6SLen Brown case INTEL_FAM6_XEON_PHI_KNM: 4226fb5d4327SDasaratharaman Chandramouli return 1; 4227fb5d4327SDasaratharaman Chandramouli } 4228fb5d4327SDasaratharaman Chandramouli return 0; 4229fb5d4327SDasaratharaman Chandramouli } 4230fb5d4327SDasaratharaman Chandramouli 4231997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4232997e5395SSrinivas Pandruvada { 4233997e5395SSrinivas Pandruvada if (!genuine_intel) 4234997e5395SSrinivas Pandruvada return 0; 4235997e5395SSrinivas Pandruvada 4236997e5395SSrinivas Pandruvada switch (model) { 4237997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4238997e5395SSrinivas Pandruvada return 1; 4239997e5395SSrinivas Pandruvada } 4240997e5395SSrinivas Pandruvada 4241997e5395SSrinivas Pandruvada return 0; 4242997e5395SSrinivas Pandruvada } 4243997e5395SSrinivas Pandruvada 4244b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4245b2b34dfeSHubert Chrzaniuk { 4246b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4247b2b34dfeSHubert Chrzaniuk return 1024; 4248b2b34dfeSHubert Chrzaniuk return 1; 4249b2b34dfeSHubert Chrzaniuk } 4250b2b34dfeSHubert Chrzaniuk 4251144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4252144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4253144b44b1SLen Brown 4254144b44b1SLen Brown double slm_bclk(void) 4255144b44b1SLen Brown { 4256144b44b1SLen Brown unsigned long long msr = 3; 4257144b44b1SLen Brown unsigned int i; 4258144b44b1SLen Brown double freq; 4259144b44b1SLen Brown 42607ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4261b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4262144b44b1SLen Brown 4263144b44b1SLen Brown i = msr & 0xf; 4264144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4265b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 42660a91e551SColin Ian King i = 3; 4267144b44b1SLen Brown } 4268144b44b1SLen Brown freq = slm_freq_table[i]; 4269144b44b1SLen Brown 427096e47158SLen Brown if (!quiet) 4271b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4272144b44b1SLen Brown 4273144b44b1SLen Brown return freq; 4274144b44b1SLen Brown } 4275144b44b1SLen Brown 4276103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4277103a8feaSLen Brown { 4278121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4279103a8feaSLen Brown return 100.00; 4280144b44b1SLen Brown else if (is_slm(family, model)) 4281144b44b1SLen Brown return slm_bclk(); 4282103a8feaSLen Brown else 4283103a8feaSLen Brown return 133.33; 4284103a8feaSLen Brown } 4285103a8feaSLen Brown 4286889facbeSLen Brown /* 4287889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4288889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4289889facbeSLen Brown * This is usually equal to tjMax. 4290889facbeSLen Brown * 4291889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4292889facbeSLen Brown * but also allow cmdline over-ride with -T. 4293889facbeSLen Brown * 4294889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4295889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4296889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4297889facbeSLen Brown */ 4298889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4299889facbeSLen Brown { 4300889facbeSLen Brown unsigned long long msr; 4301889facbeSLen Brown unsigned int target_c_local; 4302889facbeSLen Brown int cpu; 4303889facbeSLen Brown 4304889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4305889facbeSLen Brown if (!(do_dts || do_ptm)) 4306889facbeSLen Brown return 0; 4307889facbeSLen Brown 4308889facbeSLen Brown /* this is a per-package concept */ 4309889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4310889facbeSLen Brown return 0; 4311889facbeSLen Brown 4312889facbeSLen Brown cpu = t->cpu_id; 4313889facbeSLen Brown if (cpu_migrate(cpu)) { 4314b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4315889facbeSLen Brown return -1; 4316889facbeSLen Brown } 4317889facbeSLen Brown 4318889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4319889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 4320b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 4321889facbeSLen Brown cpu, tcc_activation_temp); 4322889facbeSLen Brown return 0; 4323889facbeSLen Brown } 4324889facbeSLen Brown 4325889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4326d7899447SLen Brown if (!do_nhm_platform_info) 4327889facbeSLen Brown goto guess; 4328889facbeSLen Brown 43297ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4330889facbeSLen Brown goto guess; 4331889facbeSLen Brown 43323482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 4333889facbeSLen Brown 433496e47158SLen Brown if (!quiet) 4335b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 4336889facbeSLen Brown cpu, msr, target_c_local); 4337889facbeSLen Brown 43383482124aSJean Delvare if (!target_c_local) 4339889facbeSLen Brown goto guess; 4340889facbeSLen Brown 4341889facbeSLen Brown tcc_activation_temp = target_c_local; 4342889facbeSLen Brown 4343889facbeSLen Brown return 0; 4344889facbeSLen Brown 4345889facbeSLen Brown guess: 4346889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 4347b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 4348889facbeSLen Brown cpu, tcc_activation_temp); 4349889facbeSLen Brown 4350889facbeSLen Brown return 0; 4351889facbeSLen Brown } 435269807a63SLen Brown 4353aa8d8cc7SLen Brown void decode_feature_control_msr(void) 4354aa8d8cc7SLen Brown { 4355aa8d8cc7SLen Brown unsigned long long msr; 4356aa8d8cc7SLen Brown 4357aa8d8cc7SLen Brown if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr)) 4358aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 4359aa8d8cc7SLen Brown base_cpu, msr, 4360aa8d8cc7SLen Brown msr & FEATURE_CONTROL_LOCKED ? "" : "UN-", 4361aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 4362aa8d8cc7SLen Brown } 4363aa8d8cc7SLen Brown 436469807a63SLen Brown void decode_misc_enable_msr(void) 436569807a63SLen Brown { 436669807a63SLen Brown unsigned long long msr; 436769807a63SLen Brown 4368f26b1519SLen Brown if (!genuine_intel) 4369f26b1519SLen Brown return; 4370f26b1519SLen Brown 437169807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 4372e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 437369807a63SLen Brown base_cpu, msr, 4374e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 4375e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 4376fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 4377e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 4378e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 437969807a63SLen Brown } 438069807a63SLen Brown 438133148d67SLen Brown void decode_misc_feature_control(void) 438233148d67SLen Brown { 438333148d67SLen Brown unsigned long long msr; 438433148d67SLen Brown 438533148d67SLen Brown if (!has_misc_feature_control) 438633148d67SLen Brown return; 438733148d67SLen Brown 438833148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 438933148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 439033148d67SLen Brown base_cpu, msr, 439133148d67SLen Brown msr & (0 << 0) ? "No-" : "", 439233148d67SLen Brown msr & (1 << 0) ? "No-" : "", 439333148d67SLen Brown msr & (2 << 0) ? "No-" : "", 439433148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 439533148d67SLen Brown } 4396f0057310SLen Brown /* 4397f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 4398f0057310SLen Brown * 4399f0057310SLen Brown * Decode the bits according to the Nehalem documentation 4400f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 4401f0057310SLen Brown * bit[1] less so... 4402f0057310SLen Brown */ 4403f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 4404f0057310SLen Brown { 4405f0057310SLen Brown unsigned long long msr; 4406f0057310SLen Brown 4407f0057310SLen Brown if (!do_nhm_platform_info) 4408f0057310SLen Brown return; 4409f0057310SLen Brown 4410cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 4411cf4cbe53SLen Brown return; 4412cf4cbe53SLen Brown 4413f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 4414ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 4415f0057310SLen Brown base_cpu, msr, 4416f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 4417ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 4418ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 4419f0057310SLen Brown } 442071616c8eSLen Brown /* 442171616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 442271616c8eSLen Brown * 442371616c8eSLen Brown * This MSRs are present on Silvermont processors, 442471616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 442571616c8eSLen Brown */ 442671616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 442771616c8eSLen Brown { 442871616c8eSLen Brown unsigned long long msr; 442971616c8eSLen Brown 443071616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 443171616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 443271616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 443371616c8eSLen Brown 443471616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 443571616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 443671616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 443771616c8eSLen Brown } 44387f5c258eSLen Brown 4439fcd17211SLen Brown void process_cpuid() 4440103a8feaSLen Brown { 444161a87ba7SLen Brown unsigned int eax, ebx, ecx, edx, max_level, max_extended_level; 4442103a8feaSLen Brown unsigned int fms, family, model, stepping; 4443b3a34e93SLen Brown unsigned int has_turbo; 4444103a8feaSLen Brown 4445103a8feaSLen Brown eax = ebx = ecx = edx = 0; 4446103a8feaSLen Brown 44475aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 4448103a8feaSLen Brown 4449103a8feaSLen Brown if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e) 4450103a8feaSLen Brown genuine_intel = 1; 4451103a8feaSLen Brown 445296e47158SLen Brown if (!quiet) 4453b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 4454103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 4455103a8feaSLen Brown 44565aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 4457103a8feaSLen Brown family = (fms >> 8) & 0xf; 4458103a8feaSLen Brown model = (fms >> 4) & 0xf; 4459103a8feaSLen Brown stepping = fms & 0xf; 44605aa3d1a2SCalvin Walton if (family == 0xf) 44615aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 44625aa3d1a2SCalvin Walton if (family >= 6) 4463103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 4464103a8feaSLen Brown 446596e47158SLen Brown if (!quiet) { 4466b7d8c148SLen Brown fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 4467103a8feaSLen Brown max_level, family, model, stepping, family, model, stepping); 4468d9d226ffSLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 446969807a63SLen Brown ecx & (1 << 0) ? "SSE3" : "-", 447069807a63SLen Brown ecx & (1 << 3) ? "MONITOR" : "-", 4471aa8d8cc7SLen Brown ecx & (1 << 6) ? "SMX" : "-", 447269807a63SLen Brown ecx & (1 << 7) ? "EIST" : "-", 447369807a63SLen Brown ecx & (1 << 8) ? "TM2" : "-", 447469807a63SLen Brown edx & (1 << 4) ? "TSC" : "-", 447569807a63SLen Brown edx & (1 << 5) ? "MSR" : "-", 447669807a63SLen Brown edx & (1 << 22) ? "ACPI-TM" : "-", 4477d9d226ffSLen Brown edx & (1 << 28) ? "HT" : "-", 447869807a63SLen Brown edx & (1 << 29) ? "TM" : "-"); 447969807a63SLen Brown } 4480103a8feaSLen Brown 4481b2c95d90SJosh Triplett if (!(edx & (1 << 5))) 4482b2c95d90SJosh Triplett errx(1, "CPUID: no MSR"); 4483103a8feaSLen Brown 4484103a8feaSLen Brown /* 4485103a8feaSLen Brown * check max extended function levels of CPUID. 4486103a8feaSLen Brown * This is needed to check for invariant TSC. 4487103a8feaSLen Brown * This check is valid for both Intel and AMD. 4488103a8feaSLen Brown */ 4489103a8feaSLen Brown ebx = ecx = edx = 0; 44905aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 4491103a8feaSLen Brown 449261a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 4493103a8feaSLen Brown 4494103a8feaSLen Brown /* 4495103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 4496103a8feaSLen Brown * this check is valid for both Intel and AMD 4497103a8feaSLen Brown */ 44985aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 44998209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 4500d7899447SLen Brown } 4501103a8feaSLen Brown 4502103a8feaSLen Brown /* 4503103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 4504103a8feaSLen Brown * this check is valid for both Intel and AMD 4505103a8feaSLen Brown */ 4506103a8feaSLen Brown 45075aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 45088209e054SThomas Renninger has_aperf = ecx & (1 << 0); 4509812db3f7SLen Brown if (has_aperf) { 4510812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 4511812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 4512812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 4513812db3f7SLen Brown } 4514889facbeSLen Brown do_dts = eax & (1 << 0); 4515812db3f7SLen Brown if (do_dts) 4516812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 4517b3a34e93SLen Brown has_turbo = eax & (1 << 1); 4518889facbeSLen Brown do_ptm = eax & (1 << 6); 4519812db3f7SLen Brown if (do_ptm) 4520812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 45217f5c258eSLen Brown has_hwp = eax & (1 << 7); 45227f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 45237f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 45247f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 45257f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 4526889facbeSLen Brown has_epb = ecx & (1 << 3); 4527889facbeSLen Brown 452896e47158SLen Brown if (!quiet) 4529b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 45307f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 45317f5c258eSLen Brown has_aperf ? "" : "No-", 4532b3a34e93SLen Brown has_turbo ? "" : "No-", 45337f5c258eSLen Brown do_dts ? "" : "No-", 45347f5c258eSLen Brown do_ptm ? "" : "No-", 45357f5c258eSLen Brown has_hwp ? "" : "No-", 45367f5c258eSLen Brown has_hwp_notify ? "" : "No-", 45377f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 45387f5c258eSLen Brown has_hwp_epp ? "" : "No-", 45397f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 45407f5c258eSLen Brown has_epb ? "" : "No-"); 4541103a8feaSLen Brown 454296e47158SLen Brown if (!quiet) 454369807a63SLen Brown decode_misc_enable_msr(); 454469807a63SLen Brown 454533148d67SLen Brown 454696e47158SLen Brown if (max_level >= 0x7 && !quiet) { 4547aa8d8cc7SLen Brown int has_sgx; 4548aa8d8cc7SLen Brown 4549aa8d8cc7SLen Brown ecx = 0; 4550aa8d8cc7SLen Brown 4551aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 4552aa8d8cc7SLen Brown 4553aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 4554aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 4555aa8d8cc7SLen Brown 4556aa8d8cc7SLen Brown if (has_sgx) 4557aa8d8cc7SLen Brown decode_feature_control_msr(); 4558aa8d8cc7SLen Brown } 4559aa8d8cc7SLen Brown 456061a87ba7SLen Brown if (max_level >= 0x15) { 45618a5bdf41SLen Brown unsigned int eax_crystal; 45628a5bdf41SLen Brown unsigned int ebx_tsc; 45638a5bdf41SLen Brown 45648a5bdf41SLen Brown /* 45658a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 45668a5bdf41SLen Brown */ 45678a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 45685aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 45698a5bdf41SLen Brown 45708a5bdf41SLen Brown if (ebx_tsc != 0) { 45718a5bdf41SLen Brown 457296e47158SLen Brown if (!quiet && (ebx != 0)) 4573b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 45748a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 45758a5bdf41SLen Brown 45768a5bdf41SLen Brown if (crystal_hz == 0) 45778a5bdf41SLen Brown switch(model) { 4578869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4579869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */ 4580869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */ 4581869ce69eSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */ 4582e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 4583e8efbc80SLen Brown break; 4584*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 4585ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 4586ec53e594SLen Brown break; 4587869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4588*f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4589e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 45908a5bdf41SLen Brown break; 45918a5bdf41SLen Brown default: 45928a5bdf41SLen Brown crystal_hz = 0; 45938a5bdf41SLen Brown } 45948a5bdf41SLen Brown 45958a5bdf41SLen Brown if (crystal_hz) { 45968a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 459796e47158SLen Brown if (!quiet) 4598b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 45998a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 46008a5bdf41SLen Brown } 46018a5bdf41SLen Brown } 46028a5bdf41SLen Brown } 460361a87ba7SLen Brown if (max_level >= 0x16) { 460461a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 460561a87ba7SLen Brown 460661a87ba7SLen Brown /* 460761a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 460861a87ba7SLen Brown */ 460961a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 461061a87ba7SLen Brown 46115aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 461296e47158SLen Brown if (!quiet) 4613b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 461461a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 461561a87ba7SLen Brown } 46168a5bdf41SLen Brown 4617b2b34dfeSHubert Chrzaniuk if (has_aperf) 4618b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 4619b2b34dfeSHubert Chrzaniuk 4620812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 4621812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 4622812db3f7SLen Brown 4623812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 4624812db3f7SLen Brown do_nhm_platform_info = 1; 4625812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 4626812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 4627812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 4628812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 4629812db3f7SLen Brown } 4630d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 4631812db3f7SLen Brown 4632812db3f7SLen Brown if (do_snb_cstates) 4633812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 4634812db3f7SLen Brown 46355a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 46360f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 46370f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 46380f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 46390f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 46400f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 46410f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 46420f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 46430f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 46440539ba11SLen Brown if (has_slv_msrs(family, model)) { 46450f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 46460f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 46470f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 46480f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 46490539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 46500539ba11SLen Brown use_c1_residency_msr = 1; 46510539ba11SLen Brown } 46527170a374SLen Brown if (is_dnv(family, model)) { 46537170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 46547170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 46557170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 46567170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 46577170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 46587170a374SLen Brown use_c1_residency_msr = 1; 46597170a374SLen Brown } 466034c76197SLen Brown if (is_skx(family, model)) { 466134c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 466234c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 466334c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 466434c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 466534c76197SLen Brown } 4666ade0ebacSLen Brown if (is_bdx(family, model)) { 4667ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 4668ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 4669ade0ebacSLen Brown } 46700f47c08dSLen Brown if (has_hsw_msrs(family, model)) { 46710f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 46720f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 46730f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 46740f47c08dSLen Brown } 46755a63426eSLen Brown do_irtl_hsw = has_hsw_msrs(family, model); 4676a99d8730SLen Brown if (has_skl_msrs(family, model)) { 4677a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 4678a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 4679a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 4680a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 4681a99d8730SLen Brown } 4682144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 4683fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 4684997e5395SSrinivas Pandruvada do_cnl_cstates = is_cnl(family, model); 4685103a8feaSLen Brown 468696e47158SLen Brown if (!quiet) 4687f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 4688f0057310SLen Brown 468996e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 469071616c8eSLen Brown decode_c6_demotion_policy_msr(); 469171616c8eSLen Brown 4692889facbeSLen Brown rapl_probe(family, model); 46933a9a941dSLen Brown perf_limit_reasons_probe(family, model); 4694ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 4695889facbeSLen Brown 469696e47158SLen Brown if (!quiet) 46971b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 4698fcd17211SLen Brown 469941618e63SLen Brown if (!quiet) 470041618e63SLen Brown dump_sysfs_cstate_config(); 47017293fccdSLen Brown if (!quiet) 47027293fccdSLen Brown dump_sysfs_pstate_config(); 470341618e63SLen Brown 4704a2b7b749SLen Brown if (has_skl_msrs(family, model)) 4705a2b7b749SLen Brown calculate_tsc_tweak(); 4706a2b7b749SLen Brown 4707812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 4708812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 4709fdf676e5SLen Brown 4710812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 4711812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 471227d47356SLen Brown 4713be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 4714be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 4715be0e54c4SLen Brown else 4716be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 4717be0e54c4SLen Brown 4718be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK)) 4719be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 4720be0e54c4SLen Brown else 4721be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 4722be0e54c4SLen Brown 472396e47158SLen Brown if (!quiet) 472433148d67SLen Brown decode_misc_feature_control(); 472533148d67SLen Brown 4726889facbeSLen Brown return; 4727103a8feaSLen Brown } 4728103a8feaSLen Brown 4729103a8feaSLen Brown /* 4730103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 4731103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 4732103a8feaSLen Brown */ 4733103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 4734103a8feaSLen Brown { 4735103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 4736103a8feaSLen Brown return 1; 4737103a8feaSLen Brown else 4738103a8feaSLen Brown return 0; 4739103a8feaSLen Brown } 4740103a8feaSLen Brown 4741103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 4742103a8feaSLen Brown { 4743103a8feaSLen Brown return 0; 4744103a8feaSLen Brown } 4745103a8feaSLen Brown 4746c98d5d94SLen Brown void topology_probe() 4747c98d5d94SLen Brown { 4748c98d5d94SLen Brown int i; 4749c98d5d94SLen Brown int max_core_id = 0; 4750c98d5d94SLen Brown int max_package_id = 0; 4751c98d5d94SLen Brown int max_siblings = 0; 4752c98d5d94SLen Brown 4753c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 4754843c5791SPrarit Bhargava set_max_cpu_num(); 4755c98d5d94SLen Brown topo.num_cpus = 0; 4756c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 4757c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 4758812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 4759c98d5d94SLen Brown 4760d8af6f5fSLen Brown if (debug > 1) 4761b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 4762c98d5d94SLen Brown 4763c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 4764b2c95d90SJosh Triplett if (cpus == NULL) 4765b2c95d90SJosh Triplett err(1, "calloc cpus"); 4766c98d5d94SLen Brown 4767c98d5d94SLen Brown /* 4768c98d5d94SLen Brown * Allocate and initialize cpu_present_set 4769c98d5d94SLen Brown */ 4770c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4771b2c95d90SJosh Triplett if (cpu_present_set == NULL) 4772b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4773c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4774c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 4775c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 4776c98d5d94SLen Brown 4777c98d5d94SLen Brown /* 47781ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 47791ef7d21aSLen Brown */ 47801ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 47811ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 47821ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 47831ef7d21aSLen Brown err(1, "cpu%d not present", i); 47841ef7d21aSLen Brown } 47851ef7d21aSLen Brown 47861ef7d21aSLen Brown /* 4787c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 4788c98d5d94SLen Brown */ 4789c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4790b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 4791b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4792c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4793c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 4794c98d5d94SLen Brown 47958cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 4796c98d5d94SLen Brown 4797c98d5d94SLen Brown /* 4798c98d5d94SLen Brown * For online cpus 4799c98d5d94SLen Brown * find max_core_id, max_package_id 4800c98d5d94SLen Brown */ 4801c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 4802c98d5d94SLen Brown int siblings; 4803c98d5d94SLen Brown 4804c98d5d94SLen Brown if (cpu_is_not_present(i)) { 4805d8af6f5fSLen Brown if (debug > 1) 4806b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 4807c98d5d94SLen Brown continue; 4808c98d5d94SLen Brown } 4809c98d5d94SLen Brown 48100e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 48110e2d8f05SLen Brown 48120e2d8f05SLen Brown /* get package information */ 4813c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 4814c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 4815c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 4816c98d5d94SLen Brown 48170e2d8f05SLen Brown /* get numa node information */ 4818ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 4819ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 4820ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 48210e2d8f05SLen Brown 48220e2d8f05SLen Brown /* get core information */ 48230e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 48240e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 48250e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 48260e2d8f05SLen Brown 48270e2d8f05SLen Brown /* get thread information */ 48280e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 4829c98d5d94SLen Brown if (siblings > max_siblings) 4830c98d5d94SLen Brown max_siblings = siblings; 48314f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 48328cb48b32SPrarit Bhargava topo.num_cores++; 4833c98d5d94SLen Brown } 4834ef605741SPrarit Bhargava 483570a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 4836d8af6f5fSLen Brown if (debug > 1) 4837b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 483870a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 483970a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 4840812db3f7SLen Brown BIC_PRESENT(BIC_Core); 4841c98d5d94SLen Brown 4842c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 4843d8af6f5fSLen Brown if (debug > 1) 4844b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 4845c98d5d94SLen Brown max_package_id, topo.num_packages); 48467da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 4847812db3f7SLen Brown BIC_PRESENT(BIC_Package); 4848c98d5d94SLen Brown 4849ef605741SPrarit Bhargava set_node_data(); 4850ef605741SPrarit Bhargava if (debug > 1) 485170a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 485201235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 485301235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 4854ef605741SPrarit Bhargava 485570a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 4856d8af6f5fSLen Brown if (debug > 1) 4857b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 48582ffbb224SPrarit Bhargava 48592ffbb224SPrarit Bhargava if (debug < 1) 48602ffbb224SPrarit Bhargava return; 48612ffbb224SPrarit Bhargava 48622ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 48632ffbb224SPrarit Bhargava fprintf(outf, 48642ffbb224SPrarit Bhargava "cpu %d pkg %d node %d lnode %d core %d thread %d\n", 48652ffbb224SPrarit Bhargava i, cpus[i].physical_package_id, 48662ffbb224SPrarit Bhargava cpus[i].physical_node_id, 48672ffbb224SPrarit Bhargava cpus[i].logical_node_id, 48682ffbb224SPrarit Bhargava cpus[i].physical_core_id, 48692ffbb224SPrarit Bhargava cpus[i].thread_id); 48702ffbb224SPrarit Bhargava } 48712ffbb224SPrarit Bhargava 4872c98d5d94SLen Brown } 4873c98d5d94SLen Brown 4874c98d5d94SLen Brown void 487540f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c, 487640f5cfe7SPrarit Bhargava struct pkg_data **p) 4877c98d5d94SLen Brown { 4878c98d5d94SLen Brown int i; 487940f5cfe7SPrarit Bhargava int num_cores = topo.cores_per_node * topo.nodes_per_pkg * 488040f5cfe7SPrarit Bhargava topo.num_packages; 488140f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 4882c98d5d94SLen Brown 488340f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 4884c98d5d94SLen Brown if (*t == NULL) 4885c98d5d94SLen Brown goto error; 4886c98d5d94SLen Brown 488740f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 4888c98d5d94SLen Brown (*t)[i].cpu_id = -1; 4889c98d5d94SLen Brown 489040f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 4891c98d5d94SLen Brown if (*c == NULL) 4892c98d5d94SLen Brown goto error; 4893c98d5d94SLen Brown 489440f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 4895c98d5d94SLen Brown (*c)[i].core_id = -1; 4896c98d5d94SLen Brown 4897678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 4898c98d5d94SLen Brown if (*p == NULL) 4899c98d5d94SLen Brown goto error; 4900c98d5d94SLen Brown 4901c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 4902c98d5d94SLen Brown (*p)[i].package_id = i; 4903c98d5d94SLen Brown 4904c98d5d94SLen Brown return; 4905c98d5d94SLen Brown error: 4906b2c95d90SJosh Triplett err(1, "calloc counters"); 4907c98d5d94SLen Brown } 4908c98d5d94SLen Brown /* 4909c98d5d94SLen Brown * init_counter() 4910c98d5d94SLen Brown * 4911c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 4912c98d5d94SLen Brown */ 4913c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 49148cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 4915c98d5d94SLen Brown { 49168cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 491740f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 49188cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 49198cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 4920c98d5d94SLen Brown struct thread_data *t; 4921c98d5d94SLen Brown struct core_data *c; 4922c98d5d94SLen Brown struct pkg_data *p; 4923c98d5d94SLen Brown 492442dd4520SNathan Ciobanu 492542dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 492642dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 492742dd4520SNathan Ciobanu */ 492842dd4520SNathan Ciobanu if (node_id < 0) 492942dd4520SNathan Ciobanu node_id = 0; 493042dd4520SNathan Ciobanu 493140f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 493240f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 49338cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 4934c98d5d94SLen Brown 4935c98d5d94SLen Brown t->cpu_id = cpu_id; 49368cb48b32SPrarit Bhargava if (thread_id == 0) { 4937c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 4938c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 4939c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 4940c98d5d94SLen Brown } 4941c98d5d94SLen Brown 49428cb48b32SPrarit Bhargava c->core_id = core_id; 49438cb48b32SPrarit Bhargava p->package_id = pkg_id; 4944c98d5d94SLen Brown } 4945c98d5d94SLen Brown 4946c98d5d94SLen Brown 4947c98d5d94SLen Brown int initialize_counters(int cpu_id) 4948c98d5d94SLen Brown { 49498cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 49508cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 4951c98d5d94SLen Brown return 0; 4952c98d5d94SLen Brown } 4953c98d5d94SLen Brown 4954c98d5d94SLen Brown void allocate_output_buffer() 4955c98d5d94SLen Brown { 49563b4d5c7fSAndy Shevchenko output_buffer = calloc(1, (1 + topo.num_cpus) * 1024); 4957c98d5d94SLen Brown outp = output_buffer; 4958b2c95d90SJosh Triplett if (outp == NULL) 4959b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 4960c98d5d94SLen Brown } 496136229897SLen Brown void allocate_fd_percpu(void) 496236229897SLen Brown { 496301a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 496436229897SLen Brown if (fd_percpu == NULL) 496536229897SLen Brown err(-1, "calloc fd_percpu"); 496636229897SLen Brown } 4967562a2d37SLen Brown void allocate_irq_buffers(void) 4968562a2d37SLen Brown { 4969562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 4970562a2d37SLen Brown if (irq_column_2_cpu == NULL) 4971562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 4972c98d5d94SLen Brown 497301a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 4974562a2d37SLen Brown if (irqs_per_cpu == NULL) 497501a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 4976562a2d37SLen Brown } 4977c98d5d94SLen Brown void setup_all_buffers(void) 4978c98d5d94SLen Brown { 4979c98d5d94SLen Brown topology_probe(); 4980562a2d37SLen Brown allocate_irq_buffers(); 498136229897SLen Brown allocate_fd_percpu(); 4982c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 4983c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 4984c98d5d94SLen Brown allocate_output_buffer(); 4985c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 4986c98d5d94SLen Brown } 49873b4d5c7fSAndy Shevchenko 49887ce7d5deSPrarit Bhargava void set_base_cpu(void) 49897ce7d5deSPrarit Bhargava { 49907ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 49917ce7d5deSPrarit Bhargava if (base_cpu < 0) 49927ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 49937ce7d5deSPrarit Bhargava 49947ce7d5deSPrarit Bhargava if (debug > 1) 4995b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 49967ce7d5deSPrarit Bhargava } 49977ce7d5deSPrarit Bhargava 4998103a8feaSLen Brown void turbostat_init() 4999103a8feaSLen Brown { 50007ce7d5deSPrarit Bhargava setup_all_buffers(); 50017ce7d5deSPrarit Bhargava set_base_cpu(); 5002103a8feaSLen Brown check_dev_msr(); 500398481e79SLen Brown check_permissions(); 5004fcd17211SLen Brown process_cpuid(); 5005103a8feaSLen Brown 5006103a8feaSLen Brown 500796e47158SLen Brown if (!quiet) 50087f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 50097f5c258eSLen Brown 501096e47158SLen Brown if (!quiet) 5011889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5012889facbeSLen Brown 501396e47158SLen Brown if (!quiet) 50143a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 50153a9a941dSLen Brown 501696e47158SLen Brown if (!quiet) 5017889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5018889facbeSLen Brown 5019889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5020889facbeSLen Brown 502196e47158SLen Brown if (!quiet) 5022889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 50235a63426eSLen Brown 502496e47158SLen Brown if (!quiet && do_irtl_snb) 50255a63426eSLen Brown print_irtl(); 5026103a8feaSLen Brown } 5027103a8feaSLen Brown 5028103a8feaSLen Brown int fork_it(char **argv) 5029103a8feaSLen Brown { 5030103a8feaSLen Brown pid_t child_pid; 5031d91bb17cSLen Brown int status; 5032d15cf7c1SLen Brown 5033218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5034d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 50354c2122d4SLen Brown first_counter_read = 0; 5036d91bb17cSLen Brown if (status) 5037d91bb17cSLen Brown exit(status); 5038d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5039d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5040103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5041103a8feaSLen Brown 5042103a8feaSLen Brown child_pid = fork(); 5043103a8feaSLen Brown if (!child_pid) { 5044103a8feaSLen Brown /* child */ 5045103a8feaSLen Brown execvp(argv[0], argv); 50460815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5047103a8feaSLen Brown } else { 5048103a8feaSLen Brown 5049103a8feaSLen Brown /* parent */ 5050b2c95d90SJosh Triplett if (child_pid == -1) 5051b2c95d90SJosh Triplett err(1, "fork"); 5052103a8feaSLen Brown 5053103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5054103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5055b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5056b2c95d90SJosh Triplett err(status, "waitpid"); 5057103a8feaSLen Brown } 5058c98d5d94SLen Brown /* 5059c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5060c98d5d94SLen Brown * because re-starting is problematic when forking 5061c98d5d94SLen Brown */ 5062218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5063c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5064103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5065103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5066ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5067ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5068ba3dec99SLen Brown else { 5069c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5070c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5071ba3dec99SLen Brown } 5072103a8feaSLen Brown 5073b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 5074b7d8c148SLen Brown 5075b7d8c148SLen Brown flush_output_stderr(); 5076103a8feaSLen Brown 5077d91bb17cSLen Brown return status; 5078103a8feaSLen Brown } 5079103a8feaSLen Brown 50803b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 50813b4d5c7fSAndy Shevchenko { 50823b4d5c7fSAndy Shevchenko int status; 50833b4d5c7fSAndy Shevchenko 5084218f0e8dSLen Brown snapshot_proc_sysfs_files(); 50853b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 50863b4d5c7fSAndy Shevchenko if (status) 50873b4d5c7fSAndy Shevchenko return status; 50883b4d5c7fSAndy Shevchenko 50893b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 50903b4d5c7fSAndy Shevchenko if (status) 50913b4d5c7fSAndy Shevchenko return status; 50923b4d5c7fSAndy Shevchenko 5093b7d8c148SLen Brown flush_output_stdout(); 50943b4d5c7fSAndy Shevchenko 50953b4d5c7fSAndy Shevchenko return status; 50963b4d5c7fSAndy Shevchenko } 50973b4d5c7fSAndy Shevchenko 5098d8af6f5fSLen Brown void print_version() { 5099538c48f2SLen Brown fprintf(outf, "turbostat version 18.07.27" 5100d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 5101d8af6f5fSLen Brown } 5102d8af6f5fSLen Brown 5103495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5104495c7654SLen Brown unsigned int width, enum counter_scope scope, 510541618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5106388e9c81SLen Brown { 5107388e9c81SLen Brown struct msr_counter *msrp; 5108388e9c81SLen Brown 5109388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5110388e9c81SLen Brown if (msrp == NULL) { 5111388e9c81SLen Brown perror("calloc"); 5112388e9c81SLen Brown exit(1); 5113388e9c81SLen Brown } 5114388e9c81SLen Brown 5115388e9c81SLen Brown msrp->msr_num = msr_num; 5116388e9c81SLen Brown strncpy(msrp->name, name, NAME_BYTES); 5117495c7654SLen Brown if (path) 5118495c7654SLen Brown strncpy(msrp->path, path, PATH_BYTES); 5119388e9c81SLen Brown msrp->width = width; 5120388e9c81SLen Brown msrp->type = type; 5121388e9c81SLen Brown msrp->format = format; 512241618e63SLen Brown msrp->flags = flags; 5123388e9c81SLen Brown 5124388e9c81SLen Brown switch (scope) { 5125388e9c81SLen Brown 5126388e9c81SLen Brown case SCOPE_CPU: 5127388e9c81SLen Brown msrp->next = sys.tp; 5128388e9c81SLen Brown sys.tp = msrp; 5129678a3bd1SLen Brown sys.added_thread_counters++; 51300748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5131678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5132678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5133678a3bd1SLen Brown exit(-1); 5134678a3bd1SLen Brown } 5135388e9c81SLen Brown break; 5136388e9c81SLen Brown 5137388e9c81SLen Brown case SCOPE_CORE: 5138388e9c81SLen Brown msrp->next = sys.cp; 5139388e9c81SLen Brown sys.cp = msrp; 5140678a3bd1SLen Brown sys.added_core_counters++; 5141678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5142678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5143678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5144678a3bd1SLen Brown exit(-1); 5145678a3bd1SLen Brown } 5146388e9c81SLen Brown break; 5147388e9c81SLen Brown 5148388e9c81SLen Brown case SCOPE_PACKAGE: 5149388e9c81SLen Brown msrp->next = sys.pp; 5150388e9c81SLen Brown sys.pp = msrp; 5151678a3bd1SLen Brown sys.added_package_counters++; 5152678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5153678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5154678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5155678a3bd1SLen Brown exit(-1); 5156678a3bd1SLen Brown } 5157388e9c81SLen Brown break; 5158388e9c81SLen Brown } 5159388e9c81SLen Brown 5160388e9c81SLen Brown return 0; 5161388e9c81SLen Brown } 5162388e9c81SLen Brown 5163388e9c81SLen Brown void parse_add_command(char *add_command) 5164388e9c81SLen Brown { 5165388e9c81SLen Brown int msr_num = 0; 5166495c7654SLen Brown char *path = NULL; 51670f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5168388e9c81SLen Brown int width = 64; 5169388e9c81SLen Brown int fail = 0; 5170388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5171388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5172388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5173388e9c81SLen Brown 5174388e9c81SLen Brown while (add_command) { 5175388e9c81SLen Brown 5176388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5177388e9c81SLen Brown goto next; 5178388e9c81SLen Brown 5179388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5180388e9c81SLen Brown goto next; 5181388e9c81SLen Brown 5182495c7654SLen Brown if (*add_command == '/') { 5183495c7654SLen Brown path = add_command; 5184495c7654SLen Brown goto next; 5185495c7654SLen Brown } 5186495c7654SLen Brown 5187388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5188388e9c81SLen Brown if ((width == 32) || (width == 64)) 5189388e9c81SLen Brown goto next; 5190388e9c81SLen Brown width = 64; 5191388e9c81SLen Brown } 5192388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 5193388e9c81SLen Brown scope = SCOPE_CPU; 5194388e9c81SLen Brown goto next; 5195388e9c81SLen Brown } 5196388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 5197388e9c81SLen Brown scope = SCOPE_CORE; 5198388e9c81SLen Brown goto next; 5199388e9c81SLen Brown } 5200388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 5201388e9c81SLen Brown scope = SCOPE_PACKAGE; 5202388e9c81SLen Brown goto next; 5203388e9c81SLen Brown } 5204388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 5205388e9c81SLen Brown type = COUNTER_CYCLES; 5206388e9c81SLen Brown goto next; 5207388e9c81SLen Brown } 5208388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 5209388e9c81SLen Brown type = COUNTER_SECONDS; 5210388e9c81SLen Brown goto next; 5211388e9c81SLen Brown } 521241618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 521341618e63SLen Brown type = COUNTER_USEC; 521441618e63SLen Brown goto next; 521541618e63SLen Brown } 5216388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 5217388e9c81SLen Brown format = FORMAT_RAW; 5218388e9c81SLen Brown goto next; 5219388e9c81SLen Brown } 5220388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 5221388e9c81SLen Brown format = FORMAT_DELTA; 5222388e9c81SLen Brown goto next; 5223388e9c81SLen Brown } 5224388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 5225388e9c81SLen Brown format = FORMAT_PERCENT; 5226388e9c81SLen Brown goto next; 5227388e9c81SLen Brown } 5228388e9c81SLen Brown 5229388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 5230388e9c81SLen Brown char *eos; 5231388e9c81SLen Brown 5232388e9c81SLen Brown eos = strchr(name_buffer, ','); 5233388e9c81SLen Brown if (eos) 5234388e9c81SLen Brown *eos = '\0'; 5235388e9c81SLen Brown goto next; 5236388e9c81SLen Brown } 5237388e9c81SLen Brown 5238388e9c81SLen Brown next: 5239388e9c81SLen Brown add_command = strchr(add_command, ','); 5240495c7654SLen Brown if (add_command) { 5241495c7654SLen Brown *add_command = '\0'; 5242388e9c81SLen Brown add_command++; 5243495c7654SLen Brown } 5244388e9c81SLen Brown 5245388e9c81SLen Brown } 5246495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 5247495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 5248388e9c81SLen Brown fail++; 5249388e9c81SLen Brown } 5250388e9c81SLen Brown 5251388e9c81SLen Brown /* generate default column header */ 5252388e9c81SLen Brown if (*name_buffer == '\0') { 5253388e9c81SLen Brown if (width == 32) 52545f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5255388e9c81SLen Brown else 52565f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5257388e9c81SLen Brown } 5258388e9c81SLen Brown 525941618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 5260388e9c81SLen Brown fail++; 5261388e9c81SLen Brown 5262388e9c81SLen Brown if (fail) { 5263388e9c81SLen Brown help(); 5264388e9c81SLen Brown exit(1); 5265388e9c81SLen Brown } 5266388e9c81SLen Brown } 526741618e63SLen Brown 5268dd778a5eSLen Brown int is_deferred_skip(char *name) 5269dd778a5eSLen Brown { 5270dd778a5eSLen Brown int i; 5271dd778a5eSLen Brown 5272dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 5273dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 5274dd778a5eSLen Brown return 1; 5275dd778a5eSLen Brown return 0; 5276dd778a5eSLen Brown } 5277dd778a5eSLen Brown 527841618e63SLen Brown void probe_sysfs(void) 527941618e63SLen Brown { 528041618e63SLen Brown char path[64]; 528141618e63SLen Brown char name_buf[16]; 528241618e63SLen Brown FILE *input; 528341618e63SLen Brown int state; 528441618e63SLen Brown char *sp; 528541618e63SLen Brown 528641618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 528741618e63SLen Brown return; 528841618e63SLen Brown 52890748eaf0SLen Brown for (state = 10; state >= 0; --state) { 529041618e63SLen Brown 529141618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 529241618e63SLen Brown base_cpu, state); 529341618e63SLen Brown input = fopen(path, "r"); 529441618e63SLen Brown if (input == NULL) 529541618e63SLen Brown continue; 529641618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 529741618e63SLen Brown 529841618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 529941618e63SLen Brown sp = strchr(name_buf, '-'); 530041618e63SLen Brown if (!sp) 530141618e63SLen Brown sp = strchrnul(name_buf, '\n'); 530241618e63SLen Brown *sp = '%'; 530341618e63SLen Brown *(sp + 1) = '\0'; 530441618e63SLen Brown 530541618e63SLen Brown fclose(input); 530641618e63SLen Brown 530741618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 530841618e63SLen Brown 5309dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5310dd778a5eSLen Brown continue; 5311dd778a5eSLen Brown 531241618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 531341618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 531441618e63SLen Brown } 531541618e63SLen Brown 53160748eaf0SLen Brown for (state = 10; state >= 0; --state) { 531741618e63SLen Brown 531841618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 531941618e63SLen Brown base_cpu, state); 532041618e63SLen Brown input = fopen(path, "r"); 532141618e63SLen Brown if (input == NULL) 532241618e63SLen Brown continue; 532341618e63SLen Brown fgets(name_buf, sizeof(name_buf), input); 532441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 532541618e63SLen Brown sp = strchr(name_buf, '-'); 532641618e63SLen Brown if (!sp) 532741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 532841618e63SLen Brown *sp = '\0'; 532941618e63SLen Brown fclose(input); 533041618e63SLen Brown 533141618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 533241618e63SLen Brown 5333dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5334dd778a5eSLen Brown continue; 5335dd778a5eSLen Brown 533641618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 533741618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 533841618e63SLen Brown } 533941618e63SLen Brown 534041618e63SLen Brown } 534141618e63SLen Brown 53421ef7d21aSLen Brown 53431ef7d21aSLen Brown /* 53441ef7d21aSLen Brown * parse cpuset with following syntax 53451ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 53461ef7d21aSLen Brown */ 53471ef7d21aSLen Brown void parse_cpu_command(char *optarg) 53481ef7d21aSLen Brown { 53491ef7d21aSLen Brown unsigned int start, end; 53501ef7d21aSLen Brown char *next; 53511ef7d21aSLen Brown 53524e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 53534e4e1e7cSLen Brown if (cpu_subset) 53544e4e1e7cSLen Brown goto error; 53554e4e1e7cSLen Brown show_core_only++; 53564e4e1e7cSLen Brown return; 53574e4e1e7cSLen Brown } 53584e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 53594e4e1e7cSLen Brown if (cpu_subset) 53604e4e1e7cSLen Brown goto error; 53614e4e1e7cSLen Brown show_pkg_only++; 53624e4e1e7cSLen Brown return; 53634e4e1e7cSLen Brown } 53644e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 53654e4e1e7cSLen Brown goto error; 53664e4e1e7cSLen Brown 53671ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 53681ef7d21aSLen Brown if (cpu_subset == NULL) 53691ef7d21aSLen Brown err(3, "CPU_ALLOC"); 53701ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 53711ef7d21aSLen Brown 53721ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 53731ef7d21aSLen Brown 53741ef7d21aSLen Brown next = optarg; 53751ef7d21aSLen Brown 53761ef7d21aSLen Brown while (next && *next) { 53771ef7d21aSLen Brown 53781ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 53791ef7d21aSLen Brown goto error; 53801ef7d21aSLen Brown 53811ef7d21aSLen Brown start = strtoul(next, &next, 10); 53821ef7d21aSLen Brown 53831ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 53841ef7d21aSLen Brown goto error; 53851ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 53861ef7d21aSLen Brown 53871ef7d21aSLen Brown if (*next == '\0') 53881ef7d21aSLen Brown break; 53891ef7d21aSLen Brown 53901ef7d21aSLen Brown if (*next == ',') { 53911ef7d21aSLen Brown next += 1; 53921ef7d21aSLen Brown continue; 53931ef7d21aSLen Brown } 53941ef7d21aSLen Brown 53951ef7d21aSLen Brown if (*next == '-') { 53961ef7d21aSLen Brown next += 1; /* start range */ 53971ef7d21aSLen Brown } else if (*next == '.') { 53981ef7d21aSLen Brown next += 1; 53991ef7d21aSLen Brown if (*next == '.') 54001ef7d21aSLen Brown next += 1; /* start range */ 54011ef7d21aSLen Brown else 54021ef7d21aSLen Brown goto error; 54031ef7d21aSLen Brown } 54041ef7d21aSLen Brown 54051ef7d21aSLen Brown end = strtoul(next, &next, 10); 54061ef7d21aSLen Brown if (end <= start) 54071ef7d21aSLen Brown goto error; 54081ef7d21aSLen Brown 54091ef7d21aSLen Brown while (++start <= end) { 54101ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 54111ef7d21aSLen Brown goto error; 54121ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 54131ef7d21aSLen Brown } 54141ef7d21aSLen Brown 54151ef7d21aSLen Brown if (*next == ',') 54161ef7d21aSLen Brown next += 1; 54171ef7d21aSLen Brown else if (*next != '\0') 54181ef7d21aSLen Brown goto error; 54191ef7d21aSLen Brown } 54201ef7d21aSLen Brown 54211ef7d21aSLen Brown return; 54221ef7d21aSLen Brown 54231ef7d21aSLen Brown error: 54244e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 54254e4e1e7cSLen Brown help(); 54261ef7d21aSLen Brown exit(-1); 54271ef7d21aSLen Brown } 54281ef7d21aSLen Brown 5429812db3f7SLen Brown 5430103a8feaSLen Brown void cmdline(int argc, char **argv) 5431103a8feaSLen Brown { 5432103a8feaSLen Brown int opt; 5433d8af6f5fSLen Brown int option_index = 0; 5434d8af6f5fSLen Brown static struct option long_options[] = { 5435388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 54361ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 5437d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 543896e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 54393f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 5440d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 5441023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 5442d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 5443812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 5444d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 5445c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 5446b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 544796e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 5448812db3f7SLen Brown {"show", required_argument, 0, 's'}, 5449d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 5450d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 5451d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 5452d8af6f5fSLen Brown {0, 0, 0, 0 } 5453d8af6f5fSLen Brown }; 5454103a8feaSLen Brown 5455103a8feaSLen Brown progname = argv[0]; 5456103a8feaSLen Brown 5457023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 5458d8af6f5fSLen Brown long_options, &option_index)) != -1) { 5459103a8feaSLen Brown switch (opt) { 5460388e9c81SLen Brown case 'a': 5461388e9c81SLen Brown parse_add_command(optarg); 5462388e9c81SLen Brown break; 54631ef7d21aSLen Brown case 'c': 54641ef7d21aSLen Brown parse_cpu_command(optarg); 54651ef7d21aSLen Brown break; 5466d8af6f5fSLen Brown case 'D': 5467d8af6f5fSLen Brown dump_only++; 54688e180f3cSLen Brown break; 54693f44a5c6SLen Brown case 'e': 54703f44a5c6SLen Brown /* --enable specified counter */ 54714c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 54723f44a5c6SLen Brown break; 5473d8af6f5fSLen Brown case 'd': 5474d8af6f5fSLen Brown debug++; 54753f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 54762f32edf1SLen Brown break; 5477812db3f7SLen Brown case 'H': 54783f44a5c6SLen Brown /* 54793f44a5c6SLen Brown * --hide: do not show those specified 54803f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 54813f44a5c6SLen Brown */ 54823f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 5483812db3f7SLen Brown break; 5484d8af6f5fSLen Brown case 'h': 5485d8af6f5fSLen Brown default: 5486d8af6f5fSLen Brown help(); 5487d8af6f5fSLen Brown exit(1); 5488d8af6f5fSLen Brown case 'i': 54892a0609c0SLen Brown { 54902a0609c0SLen Brown double interval = strtod(optarg, NULL); 54912a0609c0SLen Brown 54922a0609c0SLen Brown if (interval < 0.001) { 5493b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 54942a0609c0SLen Brown interval); 54952a0609c0SLen Brown exit(2); 54962a0609c0SLen Brown } 54972a0609c0SLen Brown 549847936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 5499b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 550047936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 55012a0609c0SLen Brown } 5502889facbeSLen Brown break; 55035c56be9aSDirk Brandewie case 'J': 55045c56be9aSDirk Brandewie rapl_joules++; 55055c56be9aSDirk Brandewie break; 5506c8ade361SLen Brown case 'l': 55073f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 5508c8ade361SLen Brown list_header_only++; 5509c8ade361SLen Brown quiet++; 5510c8ade361SLen Brown break; 5511b7d8c148SLen Brown case 'o': 5512b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 5513b7d8c148SLen Brown break; 551496e47158SLen Brown case 'q': 551596e47158SLen Brown quiet = 1; 551696e47158SLen Brown break; 5517023fe0acSChen Yu case 'n': 5518023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 5519023fe0acSChen Yu 5520023fe0acSChen Yu if (num_iterations <= 0) { 5521023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 5522023fe0acSChen Yu num_iterations); 5523023fe0acSChen Yu exit(2); 5524023fe0acSChen Yu } 5525023fe0acSChen Yu break; 5526812db3f7SLen Brown case 's': 55273f44a5c6SLen Brown /* 55283f44a5c6SLen Brown * --show: show only those specified 55293f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 55303f44a5c6SLen Brown * subsequent invocations can add to it. 55313f44a5c6SLen Brown */ 55323f44a5c6SLen Brown if (shown == 0) 55333f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 55343f44a5c6SLen Brown else 55353f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 55363f44a5c6SLen Brown shown = 1; 5537812db3f7SLen Brown break; 5538d8af6f5fSLen Brown case 'S': 5539d8af6f5fSLen Brown summary_only++; 5540d8af6f5fSLen Brown break; 5541d8af6f5fSLen Brown case 'T': 5542d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 5543d8af6f5fSLen Brown break; 5544d8af6f5fSLen Brown case 'v': 5545d8af6f5fSLen Brown print_version(); 5546d8af6f5fSLen Brown exit(0); 5547d8af6f5fSLen Brown break; 5548103a8feaSLen Brown } 5549103a8feaSLen Brown } 5550103a8feaSLen Brown } 5551103a8feaSLen Brown 5552103a8feaSLen Brown int main(int argc, char **argv) 5553103a8feaSLen Brown { 5554b7d8c148SLen Brown outf = stderr; 5555103a8feaSLen Brown cmdline(argc, argv); 5556103a8feaSLen Brown 555796e47158SLen Brown if (!quiet) 5558d8af6f5fSLen Brown print_version(); 5559103a8feaSLen Brown 556041618e63SLen Brown probe_sysfs(); 556141618e63SLen Brown 5562103a8feaSLen Brown turbostat_init(); 5563103a8feaSLen Brown 55643b4d5c7fSAndy Shevchenko /* dump counters and exit */ 55653b4d5c7fSAndy Shevchenko if (dump_only) 55663b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 55673b4d5c7fSAndy Shevchenko 5568c8ade361SLen Brown /* list header and exit */ 5569c8ade361SLen Brown if (list_header_only) { 5570c8ade361SLen Brown print_header(","); 5571c8ade361SLen Brown flush_output_stdout(); 5572c8ade361SLen Brown return 0; 5573c8ade361SLen Brown } 5574c8ade361SLen Brown 5575103a8feaSLen Brown /* 5576103a8feaSLen Brown * if any params left, it must be a command to fork 5577103a8feaSLen Brown */ 5578103a8feaSLen Brown if (argc - optind) 5579103a8feaSLen Brown return fork_it(argv + optind); 5580103a8feaSLen Brown else 5581103a8feaSLen Brown turbostat_loop(); 5582103a8feaSLen Brown 5583103a8feaSLen Brown return 0; 5584103a8feaSLen Brown } 5585