1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2103a8feaSLen Brown /* 3103a8feaSLen Brown * turbostat -- show CPU frequency and C-state residency 434041551SLen Brown * on modern Intel and AMD processors. 5103a8feaSLen Brown * 6144b44b1SLen Brown * Copyright (c) 2013 Intel Corporation. 7103a8feaSLen Brown * Len Brown <len.brown@intel.com> 8103a8feaSLen Brown */ 9103a8feaSLen Brown 1088c3281fSLen Brown #define _GNU_SOURCE 11b731f311SJosh Triplett #include MSRHEADER 12869ce69eSLen Brown #include INTEL_FAMILY_HEADER 1395aebc44SJosh Triplett #include <stdarg.h> 14103a8feaSLen Brown #include <stdio.h> 15b2c95d90SJosh Triplett #include <err.h> 16103a8feaSLen Brown #include <unistd.h> 17103a8feaSLen Brown #include <sys/types.h> 18103a8feaSLen Brown #include <sys/wait.h> 19103a8feaSLen Brown #include <sys/stat.h> 20b9ad8ee0SLen Brown #include <sys/select.h> 21103a8feaSLen Brown #include <sys/resource.h> 22103a8feaSLen Brown #include <fcntl.h> 23103a8feaSLen Brown #include <signal.h> 24103a8feaSLen Brown #include <sys/time.h> 25103a8feaSLen Brown #include <stdlib.h> 26d8af6f5fSLen Brown #include <getopt.h> 27103a8feaSLen Brown #include <dirent.h> 28103a8feaSLen Brown #include <string.h> 29103a8feaSLen Brown #include <ctype.h> 3088c3281fSLen Brown #include <sched.h> 312a0609c0SLen Brown #include <time.h> 322b92865eSJosh Triplett #include <cpuid.h> 3398481e79SLen Brown #include <linux/capability.h> 3498481e79SLen Brown #include <errno.h> 359392bd98SCalvin Walton #include <math.h> 36103a8feaSLen Brown 37103a8feaSLen Brown char *proc_stat = "/proc/stat"; 38b7d8c148SLen Brown FILE *outf; 3936229897SLen Brown int *fd_percpu; 40b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0}; 4147936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0}; 42023fe0acSChen Yu unsigned int num_iterations; 43d8af6f5fSLen Brown unsigned int debug; 4496e47158SLen Brown unsigned int quiet; 453f44a5c6SLen Brown unsigned int shown; 460de6c0dfSLen Brown unsigned int sums_need_wide_columns; 47d8af6f5fSLen Brown unsigned int rapl_joules; 48d8af6f5fSLen Brown unsigned int summary_only; 49c8ade361SLen Brown unsigned int list_header_only; 50d8af6f5fSLen Brown unsigned int dump_only; 51103a8feaSLen Brown unsigned int do_snb_cstates; 52fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates; 53144b44b1SLen Brown unsigned int do_slm_cstates; 54144b44b1SLen Brown unsigned int use_c1_residency_msr; 55103a8feaSLen Brown unsigned int has_aperf; 56889facbeSLen Brown unsigned int has_epb; 575a63426eSLen Brown unsigned int do_irtl_snb; 585a63426eSLen Brown unsigned int do_irtl_hsw; 59fc04cc67SLen Brown unsigned int units = 1000000; /* MHz etc */ 60103a8feaSLen Brown unsigned int genuine_intel; 6134041551SLen Brown unsigned int authentic_amd; 6234041551SLen Brown unsigned int max_level, max_extended_level; 63103a8feaSLen Brown unsigned int has_invariant_tsc; 64d7899447SLen Brown unsigned int do_nhm_platform_info; 65cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT; 66b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1; 67103a8feaSLen Brown double bclk; 68a2b7b749SLen Brown double base_hz; 6921ed5574SLen Brown unsigned int has_base_hz; 70a2b7b749SLen Brown double tsc_tweak = 1.0; 71c98d5d94SLen Brown unsigned int show_pkg_only; 72c98d5d94SLen Brown unsigned int show_core_only; 73c98d5d94SLen Brown char *output_buffer, *outp; 74889facbeSLen Brown unsigned int do_rapl; 75889facbeSLen Brown unsigned int do_dts; 76889facbeSLen Brown unsigned int do_ptm; 77fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms; 78be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us; 79be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us; 8027d47356SLen Brown unsigned int gfx_cur_mhz; 81889facbeSLen Brown unsigned int tcc_activation_temp; 82889facbeSLen Brown unsigned int tcc_activation_temp_override; 8340ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units; 8440ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units; 85889facbeSLen Brown double rapl_joule_counter_range; 863a9a941dSLen Brown unsigned int do_core_perf_limit_reasons; 87ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion; 883a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons; 893a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons; 908a5bdf41SLen Brown unsigned int crystal_hz; 918a5bdf41SLen Brown unsigned long long tsc_hz; 927ce7d5deSPrarit Bhargava int base_cpu; 9321ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model); 947f5c258eSLen Brown unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */ 957f5c258eSLen Brown /* IA32_HWP_REQUEST, IA32_HWP_STATUS */ 967f5c258eSLen Brown unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */ 977f5c258eSLen Brown unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */ 987f5c258eSLen Brown unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */ 997f5c258eSLen Brown unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */ 10033148d67SLen Brown unsigned int has_misc_feature_control; 1014c2122d4SLen Brown unsigned int first_counter_read = 1; 102c026c236SArtem Bityutskiy int ignore_stdin; 103889facbeSLen Brown 104889facbeSLen Brown #define RAPL_PKG (1 << 0) 105e6f9bb3cSLen Brown /* 0x610 MSR_PKG_POWER_LIMIT */ 106e6f9bb3cSLen Brown /* 0x611 MSR_PKG_ENERGY_STATUS */ 107e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS (1 << 1) 108e6f9bb3cSLen Brown /* 0x613 MSR_PKG_PERF_STATUS */ 109e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO (1 << 2) 110e6f9bb3cSLen Brown /* 0x614 MSR_PKG_POWER_INFO */ 111e6f9bb3cSLen Brown 112889facbeSLen Brown #define RAPL_DRAM (1 << 3) 113e6f9bb3cSLen Brown /* 0x618 MSR_DRAM_POWER_LIMIT */ 114e6f9bb3cSLen Brown /* 0x619 MSR_DRAM_ENERGY_STATUS */ 115e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS (1 << 4) 116e6f9bb3cSLen Brown /* 0x61b MSR_DRAM_PERF_STATUS */ 1170b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO (1 << 5) 1180b2bb692SLen Brown /* 0x61c MSR_DRAM_POWER_INFO */ 119e6f9bb3cSLen Brown 1209148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT (1 << 6) 121e6f9bb3cSLen Brown /* 0x638 MSR_PP0_POWER_LIMIT */ 1220b2bb692SLen Brown #define RAPL_CORE_POLICY (1 << 7) 123e6f9bb3cSLen Brown /* 0x63a MSR_PP0_POLICY */ 124e6f9bb3cSLen Brown 1250b2bb692SLen Brown #define RAPL_GFX (1 << 8) 126e6f9bb3cSLen Brown /* 0x640 MSR_PP1_POWER_LIMIT */ 127e6f9bb3cSLen Brown /* 0x641 MSR_PP1_ENERGY_STATUS */ 128e6f9bb3cSLen Brown /* 0x642 MSR_PP1_POLICY */ 1299148494cSJacob Pan 1309148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS (1 << 9) 1319148494cSJacob Pan /* 0x639 MSR_PP0_ENERGY_STATUS */ 1329392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY (1 << 10) 1339392bd98SCalvin Walton /* Indicates cores energy collection is per-core, 1349392bd98SCalvin Walton * not per-package. */ 1359392bd98SCalvin Walton #define RAPL_AMD_F17H (1 << 11) 1369392bd98SCalvin Walton /* 0xc0010299 MSR_RAPL_PWR_UNIT */ 1379392bd98SCalvin Walton /* 0xc001029a MSR_CORE_ENERGY_STAT */ 1389392bd98SCalvin Walton /* 0xc001029b MSR_PKG_ENERGY_STAT */ 1399148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT) 140889facbeSLen Brown #define TJMAX_DEFAULT 100 141889facbeSLen Brown 1429392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */ 1439392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT 0xc0010299 1449392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT 0xc001029a 1459392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT 0xc001029b 1469392bd98SCalvin Walton 147889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b)) 148103a8feaSLen Brown 149388e9c81SLen Brown /* 150388e9c81SLen Brown * buffer size used by sscanf() for added column names 151388e9c81SLen Brown * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters 152388e9c81SLen Brown */ 153388e9c81SLen Brown #define NAME_BYTES 20 154495c7654SLen Brown #define PATH_BYTES 128 155388e9c81SLen Brown 156103a8feaSLen Brown int backwards_count; 157103a8feaSLen Brown char *progname; 158103a8feaSLen Brown 1591ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */ 1601ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset; 1611ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size; 1620748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8 1630748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24 1640e2d8f05SLen Brown #define BITMASK_SIZE 32 165c98d5d94SLen Brown 166c98d5d94SLen Brown struct thread_data { 167f4fdf2b4SLen Brown struct timeval tv_begin; 168f4fdf2b4SLen Brown struct timeval tv_end; 169d4794f25SYazen Ghannam struct timeval tv_delta; 170c98d5d94SLen Brown unsigned long long tsc; 171c98d5d94SLen Brown unsigned long long aperf; 172c98d5d94SLen Brown unsigned long long mperf; 173144b44b1SLen Brown unsigned long long c1; 1740de6c0dfSLen Brown unsigned long long irq_count; 1751ed51011SLen Brown unsigned int smi_count; 176c98d5d94SLen Brown unsigned int cpu_id; 1774c2122d4SLen Brown unsigned int apic_id; 1784c2122d4SLen Brown unsigned int x2apic_id; 179c98d5d94SLen Brown unsigned int flags; 180c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE 0x2 181c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4 1820748eaf0SLen Brown unsigned long long counter[MAX_ADDED_THREAD_COUNTERS]; 183c98d5d94SLen Brown } *thread_even, *thread_odd; 184c98d5d94SLen Brown 185c98d5d94SLen Brown struct core_data { 186c98d5d94SLen Brown unsigned long long c3; 187c98d5d94SLen Brown unsigned long long c6; 188c98d5d94SLen Brown unsigned long long c7; 1890539ba11SLen Brown unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */ 190889facbeSLen Brown unsigned int core_temp_c; 1919392bd98SCalvin Walton unsigned int core_energy; /* MSR_CORE_ENERGY_STAT */ 192c98d5d94SLen Brown unsigned int core_id; 193678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 194c98d5d94SLen Brown } *core_even, *core_odd; 195c98d5d94SLen Brown 196c98d5d94SLen Brown struct pkg_data { 197c98d5d94SLen Brown unsigned long long pc2; 198c98d5d94SLen Brown unsigned long long pc3; 199c98d5d94SLen Brown unsigned long long pc6; 200c98d5d94SLen Brown unsigned long long pc7; 201ca58710fSKristen Carlson Accardi unsigned long long pc8; 202ca58710fSKristen Carlson Accardi unsigned long long pc9; 203ca58710fSKristen Carlson Accardi unsigned long long pc10; 204be0e54c4SLen Brown unsigned long long cpu_lpi; 205be0e54c4SLen Brown unsigned long long sys_lpi; 2060b2bb692SLen Brown unsigned long long pkg_wtd_core_c0; 2070b2bb692SLen Brown unsigned long long pkg_any_core_c0; 2080b2bb692SLen Brown unsigned long long pkg_any_gfxe_c0; 2090b2bb692SLen Brown unsigned long long pkg_both_core_gfxe_c0; 2109185e988SLen Brown long long gfx_rc6_ms; 21127d47356SLen Brown unsigned int gfx_mhz; 212c98d5d94SLen Brown unsigned int package_id; 213889facbeSLen Brown unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */ 214889facbeSLen Brown unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */ 215889facbeSLen Brown unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */ 216889facbeSLen Brown unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */ 217889facbeSLen Brown unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */ 218889facbeSLen Brown unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */ 219889facbeSLen Brown unsigned int pkg_temp_c; 220678a3bd1SLen Brown unsigned long long counter[MAX_ADDED_COUNTERS]; 221c98d5d94SLen Brown } *package_even, *package_odd; 222c98d5d94SLen Brown 223c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd 224c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even 225c98d5d94SLen Brown 22640f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no) \ 22740f5cfe7SPrarit Bhargava ((thread_base) + \ 22840f5cfe7SPrarit Bhargava ((pkg_no) * \ 22940f5cfe7SPrarit Bhargava topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \ 23040f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node * topo.threads_per_core) + \ 23140f5cfe7SPrarit Bhargava ((core_no) * topo.threads_per_core) + \ 23240f5cfe7SPrarit Bhargava (thread_no)) 23340f5cfe7SPrarit Bhargava 23440f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no) \ 23540f5cfe7SPrarit Bhargava ((core_base) + \ 23640f5cfe7SPrarit Bhargava ((pkg_no) * topo.nodes_per_pkg * topo.cores_per_node) + \ 23740f5cfe7SPrarit Bhargava ((node_no) * topo.cores_per_node) + \ 23840f5cfe7SPrarit Bhargava (core_no)) 23940f5cfe7SPrarit Bhargava 24040f5cfe7SPrarit Bhargava 241c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no) 242c98d5d94SLen Brown 243388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE}; 24441618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC}; 245388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT}; 246388e9c81SLen Brown 247388e9c81SLen Brown struct msr_counter { 248388e9c81SLen Brown unsigned int msr_num; 249388e9c81SLen Brown char name[NAME_BYTES]; 250495c7654SLen Brown char path[PATH_BYTES]; 251388e9c81SLen Brown unsigned int width; 252388e9c81SLen Brown enum counter_type type; 253388e9c81SLen Brown enum counter_format format; 254388e9c81SLen Brown struct msr_counter *next; 255812db3f7SLen Brown unsigned int flags; 256812db3f7SLen Brown #define FLAGS_HIDE (1 << 0) 257812db3f7SLen Brown #define FLAGS_SHOW (1 << 1) 25841618e63SLen Brown #define SYSFS_PERCPU (1 << 1) 259388e9c81SLen Brown }; 260388e9c81SLen Brown 261388e9c81SLen Brown struct sys_counters { 262678a3bd1SLen Brown unsigned int added_thread_counters; 263678a3bd1SLen Brown unsigned int added_core_counters; 264678a3bd1SLen Brown unsigned int added_package_counters; 265388e9c81SLen Brown struct msr_counter *tp; 266388e9c81SLen Brown struct msr_counter *cp; 267388e9c81SLen Brown struct msr_counter *pp; 268388e9c81SLen Brown } sys; 269388e9c81SLen Brown 270c98d5d94SLen Brown struct system_summary { 271c98d5d94SLen Brown struct thread_data threads; 272c98d5d94SLen Brown struct core_data cores; 273c98d5d94SLen Brown struct pkg_data packages; 274388e9c81SLen Brown } average; 275c98d5d94SLen Brown 2760e2d8f05SLen Brown struct cpu_topology { 2770e2d8f05SLen Brown int physical_package_id; 2786de68fe1SLen Brown int die_id; 2790e2d8f05SLen Brown int logical_cpu_id; 280ef605741SPrarit Bhargava int physical_node_id; 281ef605741SPrarit Bhargava int logical_node_id; /* 0-based count within the package */ 2820e2d8f05SLen Brown int physical_core_id; 2838cb48b32SPrarit Bhargava int thread_id; 2840e2d8f05SLen Brown cpu_set_t *put_ids; /* Processing Unit/Thread IDs */ 2850e2d8f05SLen Brown } *cpus; 286c98d5d94SLen Brown 287c98d5d94SLen Brown struct topo_params { 288c98d5d94SLen Brown int num_packages; 2896de68fe1SLen Brown int num_die; 290103a8feaSLen Brown int num_cpus; 291c98d5d94SLen Brown int num_cores; 292c98d5d94SLen Brown int max_cpu_num; 293ef605741SPrarit Bhargava int max_node_num; 29470a9c6e8SPrarit Bhargava int nodes_per_pkg; 29570a9c6e8SPrarit Bhargava int cores_per_node; 29670a9c6e8SPrarit Bhargava int threads_per_core; 297c98d5d94SLen Brown } topo; 298103a8feaSLen Brown 299c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta; 300103a8feaSLen Brown 301562a2d37SLen Brown int *irq_column_2_cpu; /* /proc/interrupts column numbers */ 302562a2d37SLen Brown int *irqs_per_cpu; /* indexed by cpu_num */ 303562a2d37SLen Brown 304c98d5d94SLen Brown void setup_all_buffers(void); 305103a8feaSLen Brown 306c98d5d94SLen Brown int cpu_is_not_present(int cpu) 307d15cf7c1SLen Brown { 308c98d5d94SLen Brown return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set); 309c98d5d94SLen Brown } 310c98d5d94SLen Brown /* 311c98d5d94SLen Brown * run func(thread, core, package) in topology order 312c98d5d94SLen Brown * skip non-present cpus 313c98d5d94SLen Brown */ 314c98d5d94SLen Brown 315c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *), 316c98d5d94SLen Brown struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base) 317c98d5d94SLen Brown { 31840f5cfe7SPrarit Bhargava int retval, pkg_no, core_no, thread_no, node_no; 319c98d5d94SLen Brown 320c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 321df2f677dSLen Brown for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) { 32270a9c6e8SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; ++core_no) { 323c98d5d94SLen Brown for (thread_no = 0; thread_no < 32470a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 325c98d5d94SLen Brown struct thread_data *t; 326c98d5d94SLen Brown struct core_data *c; 327c98d5d94SLen Brown struct pkg_data *p; 328c98d5d94SLen Brown 32940f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 33040f5cfe7SPrarit Bhargava core_no, node_no, 33140f5cfe7SPrarit Bhargava pkg_no); 332c98d5d94SLen Brown 333c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 334c98d5d94SLen Brown continue; 335c98d5d94SLen Brown 33640f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 33740f5cfe7SPrarit Bhargava node_no, pkg_no); 338c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 339c98d5d94SLen Brown 340c98d5d94SLen Brown retval = func(t, c, p); 341c98d5d94SLen Brown if (retval) 342c98d5d94SLen Brown return retval; 343c98d5d94SLen Brown } 344c98d5d94SLen Brown } 345c98d5d94SLen Brown } 34640f5cfe7SPrarit Bhargava } 347d15cf7c1SLen Brown return 0; 348d15cf7c1SLen Brown } 349d15cf7c1SLen Brown 35088c3281fSLen Brown int cpu_migrate(int cpu) 35188c3281fSLen Brown { 352c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 353c98d5d94SLen Brown CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set); 354c98d5d94SLen Brown if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) 35588c3281fSLen Brown return -1; 35688c3281fSLen Brown else 35788c3281fSLen Brown return 0; 35888c3281fSLen Brown } 35936229897SLen Brown int get_msr_fd(int cpu) 360103a8feaSLen Brown { 361103a8feaSLen Brown char pathname[32]; 362103a8feaSLen Brown int fd; 363103a8feaSLen Brown 36436229897SLen Brown fd = fd_percpu[cpu]; 36536229897SLen Brown 36636229897SLen Brown if (fd) 36736229897SLen Brown return fd; 36836229897SLen Brown 369103a8feaSLen Brown sprintf(pathname, "/dev/cpu/%d/msr", cpu); 370103a8feaSLen Brown fd = open(pathname, O_RDONLY); 37115aaa346SLen Brown if (fd < 0) 37298481e79SLen Brown err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname); 373103a8feaSLen Brown 37436229897SLen Brown fd_percpu[cpu] = fd; 37536229897SLen Brown 37636229897SLen Brown return fd; 37736229897SLen Brown } 37836229897SLen Brown 37936229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr) 38036229897SLen Brown { 38136229897SLen Brown ssize_t retval; 38236229897SLen Brown 38336229897SLen Brown retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset); 38415aaa346SLen Brown 38598481e79SLen Brown if (retval != sizeof *msr) 386cf4cbe53SLen Brown err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset); 38715aaa346SLen Brown 38815aaa346SLen Brown return 0; 389103a8feaSLen Brown } 390103a8feaSLen Brown 391fc04cc67SLen Brown /* 392bdd5ae3aSLen Brown * This list matches the column headers, except 393bdd5ae3aSLen Brown * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time 394bdd5ae3aSLen Brown * 2. Core and CPU are moved to the end, we can't have strings that contain them 395bdd5ae3aSLen Brown * matching on them for --show and --hide. 396fc04cc67SLen Brown */ 397812db3f7SLen Brown struct msr_counter bic[] = { 3983f44a5c6SLen Brown { 0x0, "usec" }, 3993f44a5c6SLen Brown { 0x0, "Time_Of_Day_Seconds" }, 400812db3f7SLen Brown { 0x0, "Package" }, 401bdd5ae3aSLen Brown { 0x0, "Node" }, 402812db3f7SLen Brown { 0x0, "Avg_MHz" }, 403bdd5ae3aSLen Brown { 0x0, "Busy%" }, 404812db3f7SLen Brown { 0x0, "Bzy_MHz" }, 405812db3f7SLen Brown { 0x0, "TSC_MHz" }, 406812db3f7SLen Brown { 0x0, "IRQ" }, 407495c7654SLen Brown { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL}, 408bdd5ae3aSLen Brown { 0x0, "sysfs" }, 409812db3f7SLen Brown { 0x0, "CPU%c1" }, 410812db3f7SLen Brown { 0x0, "CPU%c3" }, 411812db3f7SLen Brown { 0x0, "CPU%c6" }, 412812db3f7SLen Brown { 0x0, "CPU%c7" }, 413812db3f7SLen Brown { 0x0, "ThreadC" }, 414812db3f7SLen Brown { 0x0, "CoreTmp" }, 415812db3f7SLen Brown { 0x0, "CoreCnt" }, 416812db3f7SLen Brown { 0x0, "PkgTmp" }, 417812db3f7SLen Brown { 0x0, "GFX%rc6" }, 418812db3f7SLen Brown { 0x0, "GFXMHz" }, 419812db3f7SLen Brown { 0x0, "Pkg%pc2" }, 420812db3f7SLen Brown { 0x0, "Pkg%pc3" }, 421812db3f7SLen Brown { 0x0, "Pkg%pc6" }, 422812db3f7SLen Brown { 0x0, "Pkg%pc7" }, 4230f47c08dSLen Brown { 0x0, "Pkg%pc8" }, 4240f47c08dSLen Brown { 0x0, "Pkg%pc9" }, 4254bd1f8f2SLen Brown { 0x0, "Pk%pc10" }, 426be0e54c4SLen Brown { 0x0, "CPU%LPI" }, 427be0e54c4SLen Brown { 0x0, "SYS%LPI" }, 428812db3f7SLen Brown { 0x0, "PkgWatt" }, 429812db3f7SLen Brown { 0x0, "CorWatt" }, 430812db3f7SLen Brown { 0x0, "GFXWatt" }, 431812db3f7SLen Brown { 0x0, "PkgCnt" }, 432812db3f7SLen Brown { 0x0, "RAMWatt" }, 433812db3f7SLen Brown { 0x0, "PKG_%" }, 434812db3f7SLen Brown { 0x0, "RAM_%" }, 435812db3f7SLen Brown { 0x0, "Pkg_J" }, 436812db3f7SLen Brown { 0x0, "Cor_J" }, 437812db3f7SLen Brown { 0x0, "GFX_J" }, 438812db3f7SLen Brown { 0x0, "RAM_J" }, 4390539ba11SLen Brown { 0x0, "Mod%c6" }, 440a99d8730SLen Brown { 0x0, "Totl%C0" }, 441a99d8730SLen Brown { 0x0, "Any%C0" }, 442a99d8730SLen Brown { 0x0, "GFX%C0" }, 443a99d8730SLen Brown { 0x0, "CPUGFX%" }, 444bdd5ae3aSLen Brown { 0x0, "Core" }, 445bdd5ae3aSLen Brown { 0x0, "CPU" }, 4464c2122d4SLen Brown { 0x0, "APIC" }, 4474c2122d4SLen Brown { 0x0, "X2APIC" }, 4486de68fe1SLen Brown { 0x0, "Die" }, 449812db3f7SLen Brown }; 450812db3f7SLen Brown 451812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter)) 4523f44a5c6SLen Brown #define BIC_USEC (1ULL << 0) 4533f44a5c6SLen Brown #define BIC_TOD (1ULL << 1) 4543f44a5c6SLen Brown #define BIC_Package (1ULL << 2) 455bdd5ae3aSLen Brown #define BIC_Node (1ULL << 3) 456bdd5ae3aSLen Brown #define BIC_Avg_MHz (1ULL << 4) 457bdd5ae3aSLen Brown #define BIC_Busy (1ULL << 5) 458bdd5ae3aSLen Brown #define BIC_Bzy_MHz (1ULL << 6) 459bdd5ae3aSLen Brown #define BIC_TSC_MHz (1ULL << 7) 460bdd5ae3aSLen Brown #define BIC_IRQ (1ULL << 8) 461bdd5ae3aSLen Brown #define BIC_SMI (1ULL << 9) 462bdd5ae3aSLen Brown #define BIC_sysfs (1ULL << 10) 463bdd5ae3aSLen Brown #define BIC_CPU_c1 (1ULL << 11) 464bdd5ae3aSLen Brown #define BIC_CPU_c3 (1ULL << 12) 465bdd5ae3aSLen Brown #define BIC_CPU_c6 (1ULL << 13) 466bdd5ae3aSLen Brown #define BIC_CPU_c7 (1ULL << 14) 467bdd5ae3aSLen Brown #define BIC_ThreadC (1ULL << 15) 468bdd5ae3aSLen Brown #define BIC_CoreTmp (1ULL << 16) 469bdd5ae3aSLen Brown #define BIC_CoreCnt (1ULL << 17) 470bdd5ae3aSLen Brown #define BIC_PkgTmp (1ULL << 18) 471bdd5ae3aSLen Brown #define BIC_GFX_rc6 (1ULL << 19) 472bdd5ae3aSLen Brown #define BIC_GFXMHz (1ULL << 20) 473bdd5ae3aSLen Brown #define BIC_Pkgpc2 (1ULL << 21) 474bdd5ae3aSLen Brown #define BIC_Pkgpc3 (1ULL << 22) 475bdd5ae3aSLen Brown #define BIC_Pkgpc6 (1ULL << 23) 476bdd5ae3aSLen Brown #define BIC_Pkgpc7 (1ULL << 24) 477bdd5ae3aSLen Brown #define BIC_Pkgpc8 (1ULL << 25) 478bdd5ae3aSLen Brown #define BIC_Pkgpc9 (1ULL << 26) 479bdd5ae3aSLen Brown #define BIC_Pkgpc10 (1ULL << 27) 480bdd5ae3aSLen Brown #define BIC_CPU_LPI (1ULL << 28) 481bdd5ae3aSLen Brown #define BIC_SYS_LPI (1ULL << 29) 482bdd5ae3aSLen Brown #define BIC_PkgWatt (1ULL << 30) 483bdd5ae3aSLen Brown #define BIC_CorWatt (1ULL << 31) 484bdd5ae3aSLen Brown #define BIC_GFXWatt (1ULL << 32) 485bdd5ae3aSLen Brown #define BIC_PkgCnt (1ULL << 33) 486bdd5ae3aSLen Brown #define BIC_RAMWatt (1ULL << 34) 487bdd5ae3aSLen Brown #define BIC_PKG__ (1ULL << 35) 488bdd5ae3aSLen Brown #define BIC_RAM__ (1ULL << 36) 489bdd5ae3aSLen Brown #define BIC_Pkg_J (1ULL << 37) 490bdd5ae3aSLen Brown #define BIC_Cor_J (1ULL << 38) 491bdd5ae3aSLen Brown #define BIC_GFX_J (1ULL << 39) 492bdd5ae3aSLen Brown #define BIC_RAM_J (1ULL << 40) 493bdd5ae3aSLen Brown #define BIC_Mod_c6 (1ULL << 41) 494bdd5ae3aSLen Brown #define BIC_Totl_c0 (1ULL << 42) 495bdd5ae3aSLen Brown #define BIC_Any_c0 (1ULL << 43) 496bdd5ae3aSLen Brown #define BIC_GFX_c0 (1ULL << 44) 497bdd5ae3aSLen Brown #define BIC_CPUGFX (1ULL << 45) 498bdd5ae3aSLen Brown #define BIC_Core (1ULL << 46) 499bdd5ae3aSLen Brown #define BIC_CPU (1ULL << 47) 5004c2122d4SLen Brown #define BIC_APIC (1ULL << 48) 5014c2122d4SLen Brown #define BIC_X2APIC (1ULL << 49) 5026de68fe1SLen Brown #define BIC_Die (1ULL << 50) 503812db3f7SLen Brown 5044c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC) 5053f44a5c6SLen Brown 5063f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT); 5074c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC; 508812db3f7SLen Brown 509812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME) 510*1e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME) 5113f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME) 512812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT) 5130f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT) 514812db3f7SLen Brown 5153f44a5c6SLen Brown 516dd778a5eSLen Brown #define MAX_DEFERRED 16 517dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED]; 518dd778a5eSLen Brown int deferred_skip_index; 519dd778a5eSLen Brown 520dd778a5eSLen Brown /* 521dd778a5eSLen Brown * HIDE_LIST - hide this list of counters, show the rest [default] 522dd778a5eSLen Brown * SHOW_LIST - show this list of counters, hide the rest 523dd778a5eSLen Brown */ 524dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST; 525dd778a5eSLen Brown 526dd778a5eSLen Brown void help(void) 527dd778a5eSLen Brown { 528dd778a5eSLen Brown fprintf(outf, 529dd778a5eSLen Brown "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n" 530dd778a5eSLen Brown "\n" 531dd778a5eSLen Brown "Turbostat forks the specified COMMAND and prints statistics\n" 532dd778a5eSLen Brown "when COMMAND completes.\n" 533dd778a5eSLen Brown "If no COMMAND is specified, turbostat wakes every 5-seconds\n" 534dd778a5eSLen Brown "to print statistics, until interrupted.\n" 535cc481650SNathan Ciobanu " -a, --add add a counter\n" 536dd778a5eSLen Brown " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n" 537cc481650SNathan Ciobanu " -c, --cpu cpu-set limit output to summary plus cpu-set:\n" 538dd778a5eSLen Brown " {core | package | j,k,l..m,n-p }\n" 5399ce80578SNathan Ciobanu " -d, --debug displays usec, Time_Of_Day_Seconds and more debugging\n" 5409ce80578SNathan Ciobanu " -D, --Dump displays the raw counter values\n" 5419ce80578SNathan Ciobanu " -e, --enable [all | column]\n" 5429ce80578SNathan Ciobanu " shows all or the specified disabled column\n" 5439ce80578SNathan Ciobanu " -H, --hide [column|column,column,...]\n" 5449ce80578SNathan Ciobanu " hide the specified column(s)\n" 545cc481650SNathan Ciobanu " -i, --interval sec.subsec\n" 546cc481650SNathan Ciobanu " Override default 5-second measurement interval\n" 5479ce80578SNathan Ciobanu " -J, --Joules displays energy in Joules instead of Watts\n" 548cc481650SNathan Ciobanu " -l, --list list column headers only\n" 549cc481650SNathan Ciobanu " -n, --num_iterations num\n" 550cc481650SNathan Ciobanu " number of the measurement iterations\n" 551cc481650SNathan Ciobanu " -o, --out file\n" 552cc481650SNathan Ciobanu " create or truncate \"file\" for all output\n" 553cc481650SNathan Ciobanu " -q, --quiet skip decoding system configuration header\n" 5549ce80578SNathan Ciobanu " -s, --show [column|column,column,...]\n" 5559ce80578SNathan Ciobanu " show only the specified column(s)\n" 5569ce80578SNathan Ciobanu " -S, --Summary\n" 5579ce80578SNathan Ciobanu " limits output to 1-line system summary per interval\n" 5589ce80578SNathan Ciobanu " -T, --TCC temperature\n" 5599ce80578SNathan Ciobanu " sets the Thermal Control Circuit temperature in\n" 5609ce80578SNathan Ciobanu " degrees Celsius\n" 561cc481650SNathan Ciobanu " -h, --help print this help message\n" 562cc481650SNathan Ciobanu " -v, --version print version information\n" 563dd778a5eSLen Brown "\n" 564dd778a5eSLen Brown "For more help, run \"man turbostat\"\n"); 565dd778a5eSLen Brown } 566dd778a5eSLen Brown 567812db3f7SLen Brown /* 568812db3f7SLen Brown * bic_lookup 569812db3f7SLen Brown * for all the strings in comma separate name_list, 570812db3f7SLen Brown * set the approprate bit in return value. 571812db3f7SLen Brown */ 572dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode) 573812db3f7SLen Brown { 574812db3f7SLen Brown int i; 575812db3f7SLen Brown unsigned long long retval = 0; 576812db3f7SLen Brown 577812db3f7SLen Brown while (name_list) { 578812db3f7SLen Brown char *comma; 579812db3f7SLen Brown 580812db3f7SLen Brown comma = strchr(name_list, ','); 581812db3f7SLen Brown 582812db3f7SLen Brown if (comma) 583812db3f7SLen Brown *comma = '\0'; 584812db3f7SLen Brown 5853f44a5c6SLen Brown if (!strcmp(name_list, "all")) 5863f44a5c6SLen Brown return ~0; 5873f44a5c6SLen Brown 588812db3f7SLen Brown for (i = 0; i < MAX_BIC; ++i) { 589812db3f7SLen Brown if (!strcmp(name_list, bic[i].name)) { 590812db3f7SLen Brown retval |= (1ULL << i); 591812db3f7SLen Brown break; 592812db3f7SLen Brown } 593812db3f7SLen Brown } 594812db3f7SLen Brown if (i == MAX_BIC) { 595dd778a5eSLen Brown if (mode == SHOW_LIST) { 596812db3f7SLen Brown fprintf(stderr, "Invalid counter name: %s\n", name_list); 597812db3f7SLen Brown exit(-1); 598812db3f7SLen Brown } 599dd778a5eSLen Brown deferred_skip_names[deferred_skip_index++] = name_list; 600dd778a5eSLen Brown if (debug) 601dd778a5eSLen Brown fprintf(stderr, "deferred \"%s\"\n", name_list); 602dd778a5eSLen Brown if (deferred_skip_index >= MAX_DEFERRED) { 603dd778a5eSLen Brown fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n", 604dd778a5eSLen Brown MAX_DEFERRED, name_list); 605dd778a5eSLen Brown help(); 606dd778a5eSLen Brown exit(1); 607dd778a5eSLen Brown } 608dd778a5eSLen Brown } 609812db3f7SLen Brown 610812db3f7SLen Brown name_list = comma; 611812db3f7SLen Brown if (name_list) 612812db3f7SLen Brown name_list++; 613812db3f7SLen Brown 614812db3f7SLen Brown } 615812db3f7SLen Brown return retval; 616812db3f7SLen Brown } 617fc04cc67SLen Brown 618dd778a5eSLen Brown 619c8ade361SLen Brown void print_header(char *delim) 620103a8feaSLen Brown { 621388e9c81SLen Brown struct msr_counter *mp; 6226168c2e0SLen Brown int printed = 0; 623388e9c81SLen Brown 6243f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) 6253f44a5c6SLen Brown outp += sprintf(outp, "%susec", (printed++ ? delim : "")); 6263f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 6273f44a5c6SLen Brown outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : "")); 628812db3f7SLen Brown if (DO_BIC(BIC_Package)) 6296168c2e0SLen Brown outp += sprintf(outp, "%sPackage", (printed++ ? delim : "")); 6306de68fe1SLen Brown if (DO_BIC(BIC_Die)) 6316de68fe1SLen Brown outp += sprintf(outp, "%sDie", (printed++ ? delim : "")); 63201235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 63301235041SPrarit Bhargava outp += sprintf(outp, "%sNode", (printed++ ? delim : "")); 634812db3f7SLen Brown if (DO_BIC(BIC_Core)) 6356168c2e0SLen Brown outp += sprintf(outp, "%sCore", (printed++ ? delim : "")); 636812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 6376168c2e0SLen Brown outp += sprintf(outp, "%sCPU", (printed++ ? delim : "")); 6384c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 6394c2122d4SLen Brown outp += sprintf(outp, "%sAPIC", (printed++ ? delim : "")); 6404c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 6414c2122d4SLen Brown outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : "")); 642812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 6436168c2e0SLen Brown outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : "")); 644812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 6456168c2e0SLen Brown outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : "")); 646812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) 6476168c2e0SLen Brown outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : "")); 648812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 6496168c2e0SLen Brown outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : "")); 6501cc21f7bSLen Brown 6510de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 6520de6c0dfSLen Brown if (sums_need_wide_columns) 6536168c2e0SLen Brown outp += sprintf(outp, "%s IRQ", (printed++ ? delim : "")); 6540de6c0dfSLen Brown else 6556168c2e0SLen Brown outp += sprintf(outp, "%sIRQ", (printed++ ? delim : "")); 6560de6c0dfSLen Brown } 6570de6c0dfSLen Brown 658812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 6596168c2e0SLen Brown outp += sprintf(outp, "%sSMI", (printed++ ? delim : "")); 6601cc21f7bSLen Brown 661388e9c81SLen Brown for (mp = sys.tp; mp; mp = mp->next) { 662dd778a5eSLen Brown 663388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 664388e9c81SLen Brown if (mp->width == 64) 665dd778a5eSLen Brown outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name); 666388e9c81SLen Brown else 667dd778a5eSLen Brown outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name); 668388e9c81SLen Brown } else { 6690de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 670dd778a5eSLen Brown outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name); 6710de6c0dfSLen Brown else 672dd778a5eSLen Brown outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name); 673388e9c81SLen Brown } 674388e9c81SLen Brown } 675388e9c81SLen Brown 67641618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 6776168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : "")); 678562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 6796168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : "")); 680812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 6816168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : "")); 682812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 6836168c2e0SLen Brown outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : "")); 684678a3bd1SLen Brown 6850539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 6866168c2e0SLen Brown outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : "")); 687678a3bd1SLen Brown 688812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 6896168c2e0SLen Brown outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : "")); 690388e9c81SLen Brown 6919392bd98SCalvin Walton if (do_rapl && !rapl_joules) { 6929392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 6939392bd98SCalvin Walton outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 6949392bd98SCalvin Walton } else if (do_rapl && rapl_joules) { 6959392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 6969392bd98SCalvin Walton outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 6979392bd98SCalvin Walton } 6989392bd98SCalvin Walton 699388e9c81SLen Brown for (mp = sys.cp; mp; mp = mp->next) { 700388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 701388e9c81SLen Brown if (mp->width == 64) 702c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 703388e9c81SLen Brown else 704c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 705388e9c81SLen Brown } else { 7060de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 7070de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 7080de6c0dfSLen Brown else 709c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 710388e9c81SLen Brown } 711388e9c81SLen Brown } 712388e9c81SLen Brown 713812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 7146168c2e0SLen Brown outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : "")); 715889facbeSLen Brown 716812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 7176168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : "")); 718fdf676e5SLen Brown 719812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 7206168c2e0SLen Brown outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : "")); 72127d47356SLen Brown 722a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 7236168c2e0SLen Brown outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : "")); 724a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 7256168c2e0SLen Brown outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : "")); 726a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 7276168c2e0SLen Brown outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : "")); 728a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 7296168c2e0SLen Brown outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : "")); 7300b2bb692SLen Brown 7310f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 7326168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : "")); 7330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 7346168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : "")); 7350f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 7366168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : "")); 7370f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 7386168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : "")); 7390f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 7406168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : "")); 7410f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 7426168c2e0SLen Brown outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : "")); 7430f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 7446168c2e0SLen Brown outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : "")); 745be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 746be0e54c4SLen Brown outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : "")); 747be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 748be0e54c4SLen Brown outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : "")); 749103a8feaSLen Brown 7505c56be9aSDirk Brandewie if (do_rapl && !rapl_joules) { 751812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 7526168c2e0SLen Brown outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : "")); 7539392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 7546168c2e0SLen Brown outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : "")); 755812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 7566168c2e0SLen Brown outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : "")); 757812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 7586168c2e0SLen Brown outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : "")); 759812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 7606168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 761812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 7626168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 763d7899447SLen Brown } else if (do_rapl && rapl_joules) { 764812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 7656168c2e0SLen Brown outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : "")); 7669392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 7676168c2e0SLen Brown outp += sprintf(outp, "%sCor_J", (printed++ ? delim : "")); 768812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 7696168c2e0SLen Brown outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : "")); 770812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 7716168c2e0SLen Brown outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : "")); 772812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 7736168c2e0SLen Brown outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : "")); 774812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 7756168c2e0SLen Brown outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : "")); 7765c56be9aSDirk Brandewie } 777388e9c81SLen Brown for (mp = sys.pp; mp; mp = mp->next) { 778388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 779388e9c81SLen Brown if (mp->width == 64) 780c8ade361SLen Brown outp += sprintf(outp, "%s%18.18s", delim, mp->name); 781388e9c81SLen Brown else 782c8ade361SLen Brown outp += sprintf(outp, "%s%10.10s", delim, mp->name); 783388e9c81SLen Brown } else { 7840de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 7850de6c0dfSLen Brown outp += sprintf(outp, "%s%8s", delim, mp->name); 7860de6c0dfSLen Brown else 787c8ade361SLen Brown outp += sprintf(outp, "%s%s", delim, mp->name); 788388e9c81SLen Brown } 789388e9c81SLen Brown } 790388e9c81SLen Brown 791c98d5d94SLen Brown outp += sprintf(outp, "\n"); 792103a8feaSLen Brown } 793103a8feaSLen Brown 794c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, 795c98d5d94SLen Brown struct pkg_data *p) 796103a8feaSLen Brown { 797388e9c81SLen Brown int i; 798388e9c81SLen Brown struct msr_counter *mp; 799388e9c81SLen Brown 8003b4d5c7fSAndy Shevchenko outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p); 801c98d5d94SLen Brown 802c98d5d94SLen Brown if (t) { 8033b4d5c7fSAndy Shevchenko outp += sprintf(outp, "CPU: %d flags 0x%x\n", 8043b4d5c7fSAndy Shevchenko t->cpu_id, t->flags); 8053b4d5c7fSAndy Shevchenko outp += sprintf(outp, "TSC: %016llX\n", t->tsc); 8063b4d5c7fSAndy Shevchenko outp += sprintf(outp, "aperf: %016llX\n", t->aperf); 8073b4d5c7fSAndy Shevchenko outp += sprintf(outp, "mperf: %016llX\n", t->mperf); 8083b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c1: %016llX\n", t->c1); 8096886fee4SLen Brown 810812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 8110de6c0dfSLen Brown outp += sprintf(outp, "IRQ: %lld\n", t->irq_count); 812812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 813218f0e8dSLen Brown outp += sprintf(outp, "SMI: %d\n", t->smi_count); 814388e9c81SLen Brown 815388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 816388e9c81SLen Brown outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", 817388e9c81SLen Brown i, mp->msr_num, t->counter[i]); 818388e9c81SLen Brown } 819103a8feaSLen Brown } 820103a8feaSLen Brown 821c98d5d94SLen Brown if (c) { 8223b4d5c7fSAndy Shevchenko outp += sprintf(outp, "core: %d\n", c->core_id); 8233b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c3: %016llX\n", c->c3); 8243b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c6: %016llX\n", c->c6); 8253b4d5c7fSAndy Shevchenko outp += sprintf(outp, "c7: %016llX\n", c->c7); 8263b4d5c7fSAndy Shevchenko outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c); 8279392bd98SCalvin Walton outp += sprintf(outp, "Joules: %0X\n", c->core_energy); 828388e9c81SLen Brown 829388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 830388e9c81SLen Brown outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", 831388e9c81SLen Brown i, mp->msr_num, c->counter[i]); 832388e9c81SLen Brown } 8330539ba11SLen Brown outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us); 834c98d5d94SLen Brown } 835103a8feaSLen Brown 836c98d5d94SLen Brown if (p) { 8373b4d5c7fSAndy Shevchenko outp += sprintf(outp, "package: %d\n", p->package_id); 8380b2bb692SLen Brown 8390b2bb692SLen Brown outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0); 8400b2bb692SLen Brown outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0); 8410b2bb692SLen Brown outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0); 8420b2bb692SLen Brown outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0); 8430b2bb692SLen Brown 8443b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc2: %016llX\n", p->pc2); 8450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 8463b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc3: %016llX\n", p->pc3); 8470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 8483b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc6: %016llX\n", p->pc6); 8490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 8503b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc7: %016llX\n", p->pc7); 8513b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc8: %016llX\n", p->pc8); 8523b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc9: %016llX\n", p->pc9); 8533b4d5c7fSAndy Shevchenko outp += sprintf(outp, "pc10: %016llX\n", p->pc10); 854be0e54c4SLen Brown outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi); 855be0e54c4SLen Brown outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi); 8563b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg); 8573b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores); 8583b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx); 8593b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram); 8603b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle PKG: %0X\n", 8613b4d5c7fSAndy Shevchenko p->rapl_pkg_perf_status); 8623b4d5c7fSAndy Shevchenko outp += sprintf(outp, "Throttle RAM: %0X\n", 8633b4d5c7fSAndy Shevchenko p->rapl_dram_perf_status); 8643b4d5c7fSAndy Shevchenko outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c); 865388e9c81SLen Brown 866388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 867388e9c81SLen Brown outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", 868388e9c81SLen Brown i, mp->msr_num, p->counter[i]); 869388e9c81SLen Brown } 870c98d5d94SLen Brown } 8713b4d5c7fSAndy Shevchenko 8723b4d5c7fSAndy Shevchenko outp += sprintf(outp, "\n"); 8733b4d5c7fSAndy Shevchenko 874c98d5d94SLen Brown return 0; 875103a8feaSLen Brown } 876103a8feaSLen Brown 877e23da037SLen Brown /* 878e23da037SLen Brown * column formatting convention & formats 879e23da037SLen Brown */ 880c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c, 881c98d5d94SLen Brown struct pkg_data *p) 882103a8feaSLen Brown { 883008d396eSLen Brown double interval_float, tsc; 884fc04cc67SLen Brown char *fmt8; 885388e9c81SLen Brown int i; 886388e9c81SLen Brown struct msr_counter *mp; 8876168c2e0SLen Brown char *delim = "\t"; 8886168c2e0SLen Brown int printed = 0; 889103a8feaSLen Brown 890c98d5d94SLen Brown /* if showing only 1st thread in core and this isn't one, bail out */ 891c98d5d94SLen Brown if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 892c98d5d94SLen Brown return 0; 893c98d5d94SLen Brown 894c98d5d94SLen Brown /* if showing only 1st thread in pkg and this isn't one, bail out */ 895c98d5d94SLen Brown if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 896c98d5d94SLen Brown return 0; 897c98d5d94SLen Brown 8981ef7d21aSLen Brown /*if not summary line and --cpu is used */ 8991ef7d21aSLen Brown if ((t != &average.threads) && 9001ef7d21aSLen Brown (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset))) 9011ef7d21aSLen Brown return 0; 9021ef7d21aSLen Brown 9033f44a5c6SLen Brown if (DO_BIC(BIC_USEC)) { 904f4fdf2b4SLen Brown /* on each row, print how many usec each timestamp took to gather */ 905f4fdf2b4SLen Brown struct timeval tv; 906f4fdf2b4SLen Brown 907f4fdf2b4SLen Brown timersub(&t->tv_end, &t->tv_begin, &tv); 908f4fdf2b4SLen Brown outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec); 909f4fdf2b4SLen Brown } 910f4fdf2b4SLen Brown 9113f44a5c6SLen Brown /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */ 9123f44a5c6SLen Brown if (DO_BIC(BIC_TOD)) 9133f44a5c6SLen Brown outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec); 9143f44a5c6SLen Brown 915d4794f25SYazen Ghannam interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0; 916103a8feaSLen Brown 917008d396eSLen Brown tsc = t->tsc * tsc_tweak; 918008d396eSLen Brown 919c98d5d94SLen Brown /* topo columns, print blanks on 1st (average) line */ 920c98d5d94SLen Brown if (t == &average.threads) { 921812db3f7SLen Brown if (DO_BIC(BIC_Package)) 9226168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9236de68fe1SLen Brown if (DO_BIC(BIC_Die)) 9246de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 92501235041SPrarit Bhargava if (DO_BIC(BIC_Node)) 92601235041SPrarit Bhargava outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 927812db3f7SLen Brown if (DO_BIC(BIC_Core)) 9286168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 929812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 9306168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9314c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 9324c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9334c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 9344c2122d4SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 935103a8feaSLen Brown } else { 936812db3f7SLen Brown if (DO_BIC(BIC_Package)) { 937c98d5d94SLen Brown if (p) 9386168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id); 939c98d5d94SLen Brown else 9406168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 941c98d5d94SLen Brown } 9426de68fe1SLen Brown if (DO_BIC(BIC_Die)) { 9436de68fe1SLen Brown if (c) 9446de68fe1SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id); 9456de68fe1SLen Brown else 9466de68fe1SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 9476de68fe1SLen Brown } 94801235041SPrarit Bhargava if (DO_BIC(BIC_Node)) { 94901235041SPrarit Bhargava if (t) 95001235041SPrarit Bhargava outp += sprintf(outp, "%s%d", 95101235041SPrarit Bhargava (printed++ ? delim : ""), 95201235041SPrarit Bhargava cpus[t->cpu_id].physical_node_id); 95301235041SPrarit Bhargava else 95401235041SPrarit Bhargava outp += sprintf(outp, "%s-", 95501235041SPrarit Bhargava (printed++ ? delim : "")); 95601235041SPrarit Bhargava } 957812db3f7SLen Brown if (DO_BIC(BIC_Core)) { 958c98d5d94SLen Brown if (c) 9596168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id); 960c98d5d94SLen Brown else 9616168c2e0SLen Brown outp += sprintf(outp, "%s-", (printed++ ? delim : "")); 962c98d5d94SLen Brown } 963812db3f7SLen Brown if (DO_BIC(BIC_CPU)) 9646168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id); 9654c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 9664c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id); 9674c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 9684c2122d4SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id); 969103a8feaSLen Brown } 970fc04cc67SLen Brown 971812db3f7SLen Brown if (DO_BIC(BIC_Avg_MHz)) 9726168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 973fc04cc67SLen Brown 1.0 / units * t->aperf / interval_float); 974fc04cc67SLen Brown 975812db3f7SLen Brown if (DO_BIC(BIC_Busy)) 9766168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc); 977103a8feaSLen Brown 978812db3f7SLen Brown if (DO_BIC(BIC_Bzy_MHz)) { 97921ed5574SLen Brown if (has_base_hz) 9806168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf); 98121ed5574SLen Brown else 9826168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 983008d396eSLen Brown tsc / units * t->aperf / t->mperf / interval_float); 98421ed5574SLen Brown } 985103a8feaSLen Brown 986812db3f7SLen Brown if (DO_BIC(BIC_TSC_MHz)) 9876168c2e0SLen Brown outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float); 988103a8feaSLen Brown 989562a2d37SLen Brown /* IRQ */ 9900de6c0dfSLen Brown if (DO_BIC(BIC_IRQ)) { 9910de6c0dfSLen Brown if (sums_need_wide_columns) 9926168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count); 9930de6c0dfSLen Brown else 9946168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count); 9950de6c0dfSLen Brown } 996562a2d37SLen Brown 9971cc21f7bSLen Brown /* SMI */ 998812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 9996168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count); 10001cc21f7bSLen Brown 1001678a3bd1SLen Brown /* Added counters */ 1002678a3bd1SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1003678a3bd1SLen Brown if (mp->format == FORMAT_RAW) { 1004678a3bd1SLen Brown if (mp->width == 32) 10055f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]); 1006678a3bd1SLen Brown else 10076168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]); 1008678a3bd1SLen Brown } else if (mp->format == FORMAT_DELTA) { 10090de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10106168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]); 10110de6c0dfSLen Brown else 10126168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]); 1013678a3bd1SLen Brown } else if (mp->format == FORMAT_PERCENT) { 101441618e63SLen Brown if (mp->type == COUNTER_USEC) 10156168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000); 101641618e63SLen Brown else 10176168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc); 1018678a3bd1SLen Brown } 1019678a3bd1SLen Brown } 1020678a3bd1SLen Brown 102141618e63SLen Brown /* C1 */ 102241618e63SLen Brown if (DO_BIC(BIC_CPU_c1)) 10236168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc); 102441618e63SLen Brown 102541618e63SLen Brown 1026c98d5d94SLen Brown /* print per-core data only for 1st thread in core */ 1027c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1028c98d5d94SLen Brown goto done; 1029c98d5d94SLen Brown 1030562855eeSLen Brown if (DO_BIC(BIC_CPU_c3)) 10316168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc); 1032812db3f7SLen Brown if (DO_BIC(BIC_CPU_c6)) 10336168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc); 1034812db3f7SLen Brown if (DO_BIC(BIC_CPU_c7)) 10356168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc); 1036c98d5d94SLen Brown 10370539ba11SLen Brown /* Mod%c6 */ 10380539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 10396168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc); 10400539ba11SLen Brown 1041812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) 10426168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c); 1043889facbeSLen Brown 1044388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1045388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1046388e9c81SLen Brown if (mp->width == 32) 10475f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]); 1048388e9c81SLen Brown else 10496168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]); 1050388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 10510de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 10526168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]); 10530de6c0dfSLen Brown else 10546168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]); 1055388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 10566168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc); 1057388e9c81SLen Brown } 1058388e9c81SLen Brown } 1059388e9c81SLen Brown 10609392bd98SCalvin Walton /* 10619392bd98SCalvin Walton * If measurement interval exceeds minimum RAPL Joule Counter range, 10629392bd98SCalvin Walton * indicate that results are suspect by printing "**" in fraction place. 10639392bd98SCalvin Walton */ 10649392bd98SCalvin Walton if (interval_float < rapl_joule_counter_range) 10659392bd98SCalvin Walton fmt8 = "%s%.2f"; 10669392bd98SCalvin Walton else 10679392bd98SCalvin Walton fmt8 = "%6.0f**"; 10689392bd98SCalvin Walton 10699392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY)) 10709392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float); 10719392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY)) 10729392bd98SCalvin Walton outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units); 10739392bd98SCalvin Walton 1074c98d5d94SLen Brown /* print per-package data only for 1st core in package */ 1075c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1076c98d5d94SLen Brown goto done; 1077c98d5d94SLen Brown 10780b2bb692SLen Brown /* PkgTmp */ 1079812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) 10806168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c); 1081889facbeSLen Brown 1082fdf676e5SLen Brown /* GFXrc6 */ 1083812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) { 1084ba3dec99SLen Brown if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */ 10856168c2e0SLen Brown outp += sprintf(outp, "%s**.**", (printed++ ? delim : "")); 10869185e988SLen Brown } else { 10876168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 10889185e988SLen Brown p->gfx_rc6_ms / 10.0 / interval_float); 10899185e988SLen Brown } 10909185e988SLen Brown } 1091fdf676e5SLen Brown 109227d47356SLen Brown /* GFXMHz */ 1093812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 10946168c2e0SLen Brown outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz); 109527d47356SLen Brown 10960b2bb692SLen Brown /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */ 1097a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 10986168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc); 1099a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 11006168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc); 1101a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 11026168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc); 1103a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 11046168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc); 11050b2bb692SLen Brown 11060f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 11076168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc); 11080f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 11096168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc); 11100f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 11116168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc); 11120f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 11136168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc); 11140f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 11156168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc); 11160f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 11176168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc); 11180f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 11196168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc); 1120889facbeSLen Brown 1121be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1122be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float); 1123be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1124be0e54c4SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float); 1125be0e54c4SLen Brown 1126812db3f7SLen Brown if (DO_BIC(BIC_PkgWatt)) 11276168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float); 11289392bd98SCalvin Walton if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 11296168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float); 1130812db3f7SLen Brown if (DO_BIC(BIC_GFXWatt)) 11316168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float); 1132812db3f7SLen Brown if (DO_BIC(BIC_RAMWatt)) 11336168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float); 1134812db3f7SLen Brown if (DO_BIC(BIC_Pkg_J)) 11356168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units); 11369392bd98SCalvin Walton if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY)) 11376168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units); 1138812db3f7SLen Brown if (DO_BIC(BIC_GFX_J)) 11396168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units); 1140812db3f7SLen Brown if (DO_BIC(BIC_RAM_J)) 11416168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units); 1142812db3f7SLen Brown if (DO_BIC(BIC_PKG__)) 11436168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float); 1144812db3f7SLen Brown if (DO_BIC(BIC_RAM__)) 11456168c2e0SLen Brown outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float); 1146812db3f7SLen Brown 1147388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1148388e9c81SLen Brown if (mp->format == FORMAT_RAW) { 1149388e9c81SLen Brown if (mp->width == 32) 11505f3aea57SLen Brown outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]); 1151388e9c81SLen Brown else 11526168c2e0SLen Brown outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]); 1153388e9c81SLen Brown } else if (mp->format == FORMAT_DELTA) { 11540de6c0dfSLen Brown if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) 11556168c2e0SLen Brown outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]); 11560de6c0dfSLen Brown else 11576168c2e0SLen Brown outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]); 1158388e9c81SLen Brown } else if (mp->format == FORMAT_PERCENT) { 11596168c2e0SLen Brown outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc); 1160388e9c81SLen Brown } 1161388e9c81SLen Brown } 1162388e9c81SLen Brown 1163c98d5d94SLen Brown done: 116494d6ab4bSLen Brown if (*(outp - 1) != '\n') 1165c98d5d94SLen Brown outp += sprintf(outp, "\n"); 1166c98d5d94SLen Brown 1167c98d5d94SLen Brown return 0; 1168103a8feaSLen Brown } 1169103a8feaSLen Brown 1170b7d8c148SLen Brown void flush_output_stdout(void) 1171103a8feaSLen Brown { 1172b7d8c148SLen Brown FILE *filep; 1173b7d8c148SLen Brown 1174b7d8c148SLen Brown if (outf == stderr) 1175b7d8c148SLen Brown filep = stdout; 1176b7d8c148SLen Brown else 1177b7d8c148SLen Brown filep = outf; 1178b7d8c148SLen Brown 1179b7d8c148SLen Brown fputs(output_buffer, filep); 1180b7d8c148SLen Brown fflush(filep); 1181b7d8c148SLen Brown 1182c98d5d94SLen Brown outp = output_buffer; 1183c98d5d94SLen Brown } 1184b7d8c148SLen Brown void flush_output_stderr(void) 1185c98d5d94SLen Brown { 1186b7d8c148SLen Brown fputs(output_buffer, outf); 1187b7d8c148SLen Brown fflush(outf); 1188c98d5d94SLen Brown outp = output_buffer; 1189c98d5d94SLen Brown } 1190c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1191c98d5d94SLen Brown { 1192e23da037SLen Brown static int printed; 1193103a8feaSLen Brown 1194e23da037SLen Brown if (!printed || !summary_only) 1195c8ade361SLen Brown print_header("\t"); 1196103a8feaSLen Brown 11979d83601aSLen Brown format_counters(&average.threads, &average.cores, &average.packages); 1198103a8feaSLen Brown 1199e23da037SLen Brown printed = 1; 1200e23da037SLen Brown 1201e23da037SLen Brown if (summary_only) 1202e23da037SLen Brown return; 1203e23da037SLen Brown 1204c98d5d94SLen Brown for_all_cpus(format_counters, t, c, p); 1205103a8feaSLen Brown } 1206103a8feaSLen Brown 1207889facbeSLen Brown #define DELTA_WRAP32(new, old) \ 1208889facbeSLen Brown if (new > old) { \ 1209889facbeSLen Brown old = new - old; \ 1210889facbeSLen Brown } else { \ 1211889facbeSLen Brown old = 0x100000000 + new - old; \ 1212889facbeSLen Brown } 1213889facbeSLen Brown 1214ba3dec99SLen Brown int 1215c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old) 1216103a8feaSLen Brown { 1217388e9c81SLen Brown int i; 1218388e9c81SLen Brown struct msr_counter *mp; 12190b2bb692SLen Brown 1220a99d8730SLen Brown 1221a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 12220b2bb692SLen Brown old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0; 1223a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 12240b2bb692SLen Brown old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0; 1225a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 12260b2bb692SLen Brown old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0; 1227a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 12280b2bb692SLen Brown old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0; 1229a99d8730SLen Brown 1230c98d5d94SLen Brown old->pc2 = new->pc2 - old->pc2; 12310f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1232c98d5d94SLen Brown old->pc3 = new->pc3 - old->pc3; 12330f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1234c98d5d94SLen Brown old->pc6 = new->pc6 - old->pc6; 12350f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1236c98d5d94SLen Brown old->pc7 = new->pc7 - old->pc7; 1237ca58710fSKristen Carlson Accardi old->pc8 = new->pc8 - old->pc8; 1238ca58710fSKristen Carlson Accardi old->pc9 = new->pc9 - old->pc9; 1239ca58710fSKristen Carlson Accardi old->pc10 = new->pc10 - old->pc10; 1240be0e54c4SLen Brown old->cpu_lpi = new->cpu_lpi - old->cpu_lpi; 1241be0e54c4SLen Brown old->sys_lpi = new->sys_lpi - old->sys_lpi; 1242889facbeSLen Brown old->pkg_temp_c = new->pkg_temp_c; 1243889facbeSLen Brown 12449185e988SLen Brown /* flag an error when rc6 counter resets/wraps */ 12459185e988SLen Brown if (old->gfx_rc6_ms > new->gfx_rc6_ms) 12469185e988SLen Brown old->gfx_rc6_ms = -1; 12479185e988SLen Brown else 1248fdf676e5SLen Brown old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms; 12499185e988SLen Brown 125027d47356SLen Brown old->gfx_mhz = new->gfx_mhz; 125127d47356SLen Brown 1252889facbeSLen Brown DELTA_WRAP32(new->energy_pkg, old->energy_pkg); 1253889facbeSLen Brown DELTA_WRAP32(new->energy_cores, old->energy_cores); 1254889facbeSLen Brown DELTA_WRAP32(new->energy_gfx, old->energy_gfx); 1255889facbeSLen Brown DELTA_WRAP32(new->energy_dram, old->energy_dram); 1256889facbeSLen Brown DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status); 1257889facbeSLen Brown DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status); 1258ba3dec99SLen Brown 1259388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1260388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1261388e9c81SLen Brown old->counter[i] = new->counter[i]; 1262388e9c81SLen Brown else 1263388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1264388e9c81SLen Brown } 1265388e9c81SLen Brown 1266ba3dec99SLen Brown return 0; 1267103a8feaSLen Brown } 1268103a8feaSLen Brown 1269c98d5d94SLen Brown void 1270c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old) 1271c98d5d94SLen Brown { 1272388e9c81SLen Brown int i; 1273388e9c81SLen Brown struct msr_counter *mp; 1274388e9c81SLen Brown 1275c98d5d94SLen Brown old->c3 = new->c3 - old->c3; 1276c98d5d94SLen Brown old->c6 = new->c6 - old->c6; 1277c98d5d94SLen Brown old->c7 = new->c7 - old->c7; 1278889facbeSLen Brown old->core_temp_c = new->core_temp_c; 12790539ba11SLen Brown old->mc6_us = new->mc6_us - old->mc6_us; 1280388e9c81SLen Brown 12819392bd98SCalvin Walton DELTA_WRAP32(new->core_energy, old->core_energy); 12829392bd98SCalvin Walton 1283388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1284388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1285388e9c81SLen Brown old->counter[i] = new->counter[i]; 1286388e9c81SLen Brown else 1287388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1288388e9c81SLen Brown } 1289103a8feaSLen Brown } 1290c98d5d94SLen Brown 1291*1e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic) 1292*1e9042b9SSrinivas Pandruvada { 1293*1e9042b9SSrinivas Pandruvada if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr) 1294*1e9042b9SSrinivas Pandruvada return 0; 1295*1e9042b9SSrinivas Pandruvada 1296*1e9042b9SSrinivas Pandruvada return DO_BIC_READ(bic); 1297*1e9042b9SSrinivas Pandruvada } 1298*1e9042b9SSrinivas Pandruvada 1299c3ae331dSLen Brown /* 1300c3ae331dSLen Brown * old = new - old 1301c3ae331dSLen Brown */ 1302ba3dec99SLen Brown int 1303c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old, 1304c98d5d94SLen Brown struct core_data *core_delta) 1305c98d5d94SLen Brown { 1306388e9c81SLen Brown int i; 1307388e9c81SLen Brown struct msr_counter *mp; 1308388e9c81SLen Brown 13094c2122d4SLen Brown /* we run cpuid just the 1st time, copy the results */ 13104c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 13114c2122d4SLen Brown new->apic_id = old->apic_id; 13124c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 13134c2122d4SLen Brown new->x2apic_id = old->x2apic_id; 13144c2122d4SLen Brown 13153f44a5c6SLen Brown /* 13163f44a5c6SLen Brown * the timestamps from start of measurement interval are in "old" 13173f44a5c6SLen Brown * the timestamp from end of measurement interval are in "new" 13183f44a5c6SLen Brown * over-write old w/ new so we can print end of interval values 13193f44a5c6SLen Brown */ 13203f44a5c6SLen Brown 1321d4794f25SYazen Ghannam timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta); 13223f44a5c6SLen Brown old->tv_begin = new->tv_begin; 13233f44a5c6SLen Brown old->tv_end = new->tv_end; 13243f44a5c6SLen Brown 1325c98d5d94SLen Brown old->tsc = new->tsc - old->tsc; 1326c98d5d94SLen Brown 1327103a8feaSLen Brown /* check for TSC < 1 Mcycles over interval */ 1328b2c95d90SJosh Triplett if (old->tsc < (1000 * 1000)) 1329b2c95d90SJosh Triplett errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" 1330b2c95d90SJosh Triplett "You can disable all c-states by booting with \"idle=poll\"\n" 1331b2c95d90SJosh Triplett "or just the deep ones with \"processor.max_cstate=1\""); 1332103a8feaSLen Brown 1333c98d5d94SLen Brown old->c1 = new->c1 - old->c1; 1334c98d5d94SLen Brown 1335*1e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 1336*1e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 1337c98d5d94SLen Brown if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) { 1338c98d5d94SLen Brown old->aperf = new->aperf - old->aperf; 1339c98d5d94SLen Brown old->mperf = new->mperf - old->mperf; 1340c98d5d94SLen Brown } else { 1341ba3dec99SLen Brown return -1; 1342103a8feaSLen Brown } 1343a729617cSLen Brown } 1344103a8feaSLen Brown 1345c98d5d94SLen Brown 1346144b44b1SLen Brown if (use_c1_residency_msr) { 1347144b44b1SLen Brown /* 1348144b44b1SLen Brown * Some models have a dedicated C1 residency MSR, 1349144b44b1SLen Brown * which should be more accurate than the derivation below. 1350144b44b1SLen Brown */ 1351144b44b1SLen Brown } else { 1352103a8feaSLen Brown /* 1353c3ae331dSLen Brown * As counter collection is not atomic, 1354c3ae331dSLen Brown * it is possible for mperf's non-halted cycles + idle states 1355103a8feaSLen Brown * to exceed TSC's all cycles: show c1 = 0% in that case. 1356103a8feaSLen Brown */ 135795149369SLen Brown if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak)) 1358c98d5d94SLen Brown old->c1 = 0; 1359c98d5d94SLen Brown else { 1360c98d5d94SLen Brown /* normal case, derive c1 */ 1361008d396eSLen Brown old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3 1362c98d5d94SLen Brown - core_delta->c6 - core_delta->c7; 1363c98d5d94SLen Brown } 1364144b44b1SLen Brown } 1365c3ae331dSLen Brown 1366c98d5d94SLen Brown if (old->mperf == 0) { 1367b7d8c148SLen Brown if (debug > 1) 1368b7d8c148SLen Brown fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id); 1369c98d5d94SLen Brown old->mperf = 1; /* divide by 0 protection */ 1370c98d5d94SLen Brown } 1371103a8feaSLen Brown 1372812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1373562a2d37SLen Brown old->irq_count = new->irq_count - old->irq_count; 1374562a2d37SLen Brown 1375812db3f7SLen Brown if (DO_BIC(BIC_SMI)) 13761ed51011SLen Brown old->smi_count = new->smi_count - old->smi_count; 1377ba3dec99SLen Brown 1378388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1379388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1380388e9c81SLen Brown old->counter[i] = new->counter[i]; 1381388e9c81SLen Brown else 1382388e9c81SLen Brown old->counter[i] = new->counter[i] - old->counter[i]; 1383388e9c81SLen Brown } 1384ba3dec99SLen Brown return 0; 1385103a8feaSLen Brown } 1386c98d5d94SLen Brown 1387c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c, 1388c98d5d94SLen Brown struct pkg_data *p, struct thread_data *t2, 1389c98d5d94SLen Brown struct core_data *c2, struct pkg_data *p2) 1390c98d5d94SLen Brown { 1391ba3dec99SLen Brown int retval = 0; 1392ba3dec99SLen Brown 1393c98d5d94SLen Brown /* calculate core delta only for 1st thread in core */ 1394c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE) 1395c98d5d94SLen Brown delta_core(c, c2); 1396c98d5d94SLen Brown 1397c98d5d94SLen Brown /* always calculate thread delta */ 1398ba3dec99SLen Brown retval = delta_thread(t, t2, c2); /* c2 is core delta */ 1399ba3dec99SLen Brown if (retval) 1400ba3dec99SLen Brown return retval; 1401c98d5d94SLen Brown 1402c98d5d94SLen Brown /* calculate package delta only for 1st core in package */ 1403c98d5d94SLen Brown if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) 1404ba3dec99SLen Brown retval = delta_package(p, p2); 1405c98d5d94SLen Brown 1406ba3dec99SLen Brown return retval; 1407103a8feaSLen Brown } 1408103a8feaSLen Brown 1409c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1410103a8feaSLen Brown { 1411388e9c81SLen Brown int i; 1412388e9c81SLen Brown struct msr_counter *mp; 1413388e9c81SLen Brown 14143f44a5c6SLen Brown t->tv_begin.tv_sec = 0; 14153f44a5c6SLen Brown t->tv_begin.tv_usec = 0; 14163f44a5c6SLen Brown t->tv_end.tv_sec = 0; 14173f44a5c6SLen Brown t->tv_end.tv_usec = 0; 1418d4794f25SYazen Ghannam t->tv_delta.tv_sec = 0; 1419d4794f25SYazen Ghannam t->tv_delta.tv_usec = 0; 14203f44a5c6SLen Brown 1421c98d5d94SLen Brown t->tsc = 0; 1422c98d5d94SLen Brown t->aperf = 0; 1423c98d5d94SLen Brown t->mperf = 0; 1424c98d5d94SLen Brown t->c1 = 0; 1425103a8feaSLen Brown 1426562a2d37SLen Brown t->irq_count = 0; 1427562a2d37SLen Brown t->smi_count = 0; 1428562a2d37SLen Brown 1429c98d5d94SLen Brown /* tells format_counters to dump all fields from this set */ 1430c98d5d94SLen Brown t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE; 1431c98d5d94SLen Brown 1432c98d5d94SLen Brown c->c3 = 0; 1433c98d5d94SLen Brown c->c6 = 0; 1434c98d5d94SLen Brown c->c7 = 0; 14350539ba11SLen Brown c->mc6_us = 0; 1436889facbeSLen Brown c->core_temp_c = 0; 14379392bd98SCalvin Walton c->core_energy = 0; 1438c98d5d94SLen Brown 14390b2bb692SLen Brown p->pkg_wtd_core_c0 = 0; 14400b2bb692SLen Brown p->pkg_any_core_c0 = 0; 14410b2bb692SLen Brown p->pkg_any_gfxe_c0 = 0; 14420b2bb692SLen Brown p->pkg_both_core_gfxe_c0 = 0; 14430b2bb692SLen Brown 1444c98d5d94SLen Brown p->pc2 = 0; 14450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1446c98d5d94SLen Brown p->pc3 = 0; 14470f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1448c98d5d94SLen Brown p->pc6 = 0; 14490f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1450c98d5d94SLen Brown p->pc7 = 0; 1451ca58710fSKristen Carlson Accardi p->pc8 = 0; 1452ca58710fSKristen Carlson Accardi p->pc9 = 0; 1453ca58710fSKristen Carlson Accardi p->pc10 = 0; 1454be0e54c4SLen Brown p->cpu_lpi = 0; 1455be0e54c4SLen Brown p->sys_lpi = 0; 1456889facbeSLen Brown 1457889facbeSLen Brown p->energy_pkg = 0; 1458889facbeSLen Brown p->energy_dram = 0; 1459889facbeSLen Brown p->energy_cores = 0; 1460889facbeSLen Brown p->energy_gfx = 0; 1461889facbeSLen Brown p->rapl_pkg_perf_status = 0; 1462889facbeSLen Brown p->rapl_dram_perf_status = 0; 1463889facbeSLen Brown p->pkg_temp_c = 0; 146427d47356SLen Brown 1465fdf676e5SLen Brown p->gfx_rc6_ms = 0; 146627d47356SLen Brown p->gfx_mhz = 0; 1467388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) 1468388e9c81SLen Brown t->counter[i] = 0; 1469388e9c81SLen Brown 1470388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) 1471388e9c81SLen Brown c->counter[i] = 0; 1472388e9c81SLen Brown 1473388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) 1474388e9c81SLen Brown p->counter[i] = 0; 1475103a8feaSLen Brown } 1476c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, 1477c98d5d94SLen Brown struct pkg_data *p) 1478103a8feaSLen Brown { 1479388e9c81SLen Brown int i; 1480388e9c81SLen Brown struct msr_counter *mp; 1481388e9c81SLen Brown 14824c2122d4SLen Brown /* copy un-changing apic_id's */ 14834c2122d4SLen Brown if (DO_BIC(BIC_APIC)) 14844c2122d4SLen Brown average.threads.apic_id = t->apic_id; 14854c2122d4SLen Brown if (DO_BIC(BIC_X2APIC)) 14864c2122d4SLen Brown average.threads.x2apic_id = t->x2apic_id; 14874c2122d4SLen Brown 14883f44a5c6SLen Brown /* remember first tv_begin */ 14893f44a5c6SLen Brown if (average.threads.tv_begin.tv_sec == 0) 14903f44a5c6SLen Brown average.threads.tv_begin = t->tv_begin; 14913f44a5c6SLen Brown 14923f44a5c6SLen Brown /* remember last tv_end */ 14933f44a5c6SLen Brown average.threads.tv_end = t->tv_end; 14943f44a5c6SLen Brown 1495c98d5d94SLen Brown average.threads.tsc += t->tsc; 1496c98d5d94SLen Brown average.threads.aperf += t->aperf; 1497c98d5d94SLen Brown average.threads.mperf += t->mperf; 1498c98d5d94SLen Brown average.threads.c1 += t->c1; 149915aaa346SLen Brown 1500562a2d37SLen Brown average.threads.irq_count += t->irq_count; 1501562a2d37SLen Brown average.threads.smi_count += t->smi_count; 1502562a2d37SLen Brown 1503388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1504388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1505388e9c81SLen Brown continue; 1506388e9c81SLen Brown average.threads.counter[i] += t->counter[i]; 1507388e9c81SLen Brown } 1508388e9c81SLen Brown 1509c98d5d94SLen Brown /* sum per-core values only for 1st thread in core */ 1510c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1511c98d5d94SLen Brown return 0; 1512c98d5d94SLen Brown 1513c98d5d94SLen Brown average.cores.c3 += c->c3; 1514c98d5d94SLen Brown average.cores.c6 += c->c6; 1515c98d5d94SLen Brown average.cores.c7 += c->c7; 15160539ba11SLen Brown average.cores.mc6_us += c->mc6_us; 1517c98d5d94SLen Brown 1518889facbeSLen Brown average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c); 1519889facbeSLen Brown 15209392bd98SCalvin Walton average.cores.core_energy += c->core_energy; 15219392bd98SCalvin Walton 1522388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1523388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1524388e9c81SLen Brown continue; 1525388e9c81SLen Brown average.cores.counter[i] += c->counter[i]; 1526388e9c81SLen Brown } 1527388e9c81SLen Brown 1528c98d5d94SLen Brown /* sum per-pkg values only for 1st core in pkg */ 1529c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1530c98d5d94SLen Brown return 0; 1531c98d5d94SLen Brown 1532a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 15330b2bb692SLen Brown average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0; 1534a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 15350b2bb692SLen Brown average.packages.pkg_any_core_c0 += p->pkg_any_core_c0; 1536a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 15370b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0; 1538a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 15390b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0; 15400b2bb692SLen Brown 1541c98d5d94SLen Brown average.packages.pc2 += p->pc2; 15420f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1543c98d5d94SLen Brown average.packages.pc3 += p->pc3; 15440f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1545c98d5d94SLen Brown average.packages.pc6 += p->pc6; 15460f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1547c98d5d94SLen Brown average.packages.pc7 += p->pc7; 1548ca58710fSKristen Carlson Accardi average.packages.pc8 += p->pc8; 1549ca58710fSKristen Carlson Accardi average.packages.pc9 += p->pc9; 1550ca58710fSKristen Carlson Accardi average.packages.pc10 += p->pc10; 1551c98d5d94SLen Brown 1552be0e54c4SLen Brown average.packages.cpu_lpi = p->cpu_lpi; 1553be0e54c4SLen Brown average.packages.sys_lpi = p->sys_lpi; 1554be0e54c4SLen Brown 1555889facbeSLen Brown average.packages.energy_pkg += p->energy_pkg; 1556889facbeSLen Brown average.packages.energy_dram += p->energy_dram; 1557889facbeSLen Brown average.packages.energy_cores += p->energy_cores; 1558889facbeSLen Brown average.packages.energy_gfx += p->energy_gfx; 1559889facbeSLen Brown 1560fdf676e5SLen Brown average.packages.gfx_rc6_ms = p->gfx_rc6_ms; 156127d47356SLen Brown average.packages.gfx_mhz = p->gfx_mhz; 156227d47356SLen Brown 1563889facbeSLen Brown average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c); 1564889facbeSLen Brown 1565889facbeSLen Brown average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status; 1566889facbeSLen Brown average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status; 1567388e9c81SLen Brown 1568388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1569388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1570388e9c81SLen Brown continue; 1571388e9c81SLen Brown average.packages.counter[i] += p->counter[i]; 1572388e9c81SLen Brown } 1573c98d5d94SLen Brown return 0; 1574c98d5d94SLen Brown } 1575c98d5d94SLen Brown /* 1576c98d5d94SLen Brown * sum the counters for all cpus in the system 1577c98d5d94SLen Brown * compute the weighted average 1578c98d5d94SLen Brown */ 1579c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c, 1580c98d5d94SLen Brown struct pkg_data *p) 1581c98d5d94SLen Brown { 1582388e9c81SLen Brown int i; 1583388e9c81SLen Brown struct msr_counter *mp; 1584388e9c81SLen Brown 1585c98d5d94SLen Brown clear_counters(&average.threads, &average.cores, &average.packages); 1586c98d5d94SLen Brown 1587c98d5d94SLen Brown for_all_cpus(sum_counters, t, c, p); 1588c98d5d94SLen Brown 1589d4794f25SYazen Ghannam /* Use the global time delta for the average. */ 1590d4794f25SYazen Ghannam average.threads.tv_delta = tv_delta; 1591d4794f25SYazen Ghannam 1592c98d5d94SLen Brown average.threads.tsc /= topo.num_cpus; 1593c98d5d94SLen Brown average.threads.aperf /= topo.num_cpus; 1594c98d5d94SLen Brown average.threads.mperf /= topo.num_cpus; 1595c98d5d94SLen Brown average.threads.c1 /= topo.num_cpus; 1596c98d5d94SLen Brown 15970de6c0dfSLen Brown if (average.threads.irq_count > 9999999) 15980de6c0dfSLen Brown sums_need_wide_columns = 1; 15990de6c0dfSLen Brown 1600c98d5d94SLen Brown average.cores.c3 /= topo.num_cores; 1601c98d5d94SLen Brown average.cores.c6 /= topo.num_cores; 1602c98d5d94SLen Brown average.cores.c7 /= topo.num_cores; 16030539ba11SLen Brown average.cores.mc6_us /= topo.num_cores; 1604c98d5d94SLen Brown 1605a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) 16060b2bb692SLen Brown average.packages.pkg_wtd_core_c0 /= topo.num_packages; 1607a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) 16080b2bb692SLen Brown average.packages.pkg_any_core_c0 /= topo.num_packages; 1609a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) 16100b2bb692SLen Brown average.packages.pkg_any_gfxe_c0 /= topo.num_packages; 1611a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) 16120b2bb692SLen Brown average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages; 16130b2bb692SLen Brown 1614c98d5d94SLen Brown average.packages.pc2 /= topo.num_packages; 16150f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1616c98d5d94SLen Brown average.packages.pc3 /= topo.num_packages; 16170f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) 1618c98d5d94SLen Brown average.packages.pc6 /= topo.num_packages; 16190f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1620c98d5d94SLen Brown average.packages.pc7 /= topo.num_packages; 1621ca58710fSKristen Carlson Accardi 1622ca58710fSKristen Carlson Accardi average.packages.pc8 /= topo.num_packages; 1623ca58710fSKristen Carlson Accardi average.packages.pc9 /= topo.num_packages; 1624ca58710fSKristen Carlson Accardi average.packages.pc10 /= topo.num_packages; 1625388e9c81SLen Brown 1626388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1627388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1628388e9c81SLen Brown continue; 16290de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 16300de6c0dfSLen Brown if (average.threads.counter[i] > 9999999) 16310de6c0dfSLen Brown sums_need_wide_columns = 1; 163241618e63SLen Brown continue; 16330de6c0dfSLen Brown } 1634388e9c81SLen Brown average.threads.counter[i] /= topo.num_cpus; 1635388e9c81SLen Brown } 1636388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1637388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1638388e9c81SLen Brown continue; 16390de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 16400de6c0dfSLen Brown if (average.cores.counter[i] > 9999999) 16410de6c0dfSLen Brown sums_need_wide_columns = 1; 16420de6c0dfSLen Brown } 1643388e9c81SLen Brown average.cores.counter[i] /= topo.num_cores; 1644388e9c81SLen Brown } 1645388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 1646388e9c81SLen Brown if (mp->format == FORMAT_RAW) 1647388e9c81SLen Brown continue; 16480de6c0dfSLen Brown if (mp->type == COUNTER_ITEMS) { 16490de6c0dfSLen Brown if (average.packages.counter[i] > 9999999) 16500de6c0dfSLen Brown sums_need_wide_columns = 1; 16510de6c0dfSLen Brown } 1652388e9c81SLen Brown average.packages.counter[i] /= topo.num_packages; 1653388e9c81SLen Brown } 1654c98d5d94SLen Brown } 1655c98d5d94SLen Brown 1656c98d5d94SLen Brown static unsigned long long rdtsc(void) 1657c98d5d94SLen Brown { 1658c98d5d94SLen Brown unsigned int low, high; 1659c98d5d94SLen Brown 1660c98d5d94SLen Brown asm volatile("rdtsc" : "=a" (low), "=d" (high)); 1661c98d5d94SLen Brown 1662c98d5d94SLen Brown return low | ((unsigned long long)high) << 32; 1663c98d5d94SLen Brown } 1664c98d5d94SLen Brown 1665c98d5d94SLen Brown /* 1666495c7654SLen Brown * Open a file, and exit on failure 1667495c7654SLen Brown */ 1668495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode) 1669495c7654SLen Brown { 1670495c7654SLen Brown FILE *filep = fopen(path, mode); 1671495c7654SLen Brown 1672495c7654SLen Brown if (!filep) 1673495c7654SLen Brown err(1, "%s: open failed", path); 1674495c7654SLen Brown return filep; 1675495c7654SLen Brown } 1676495c7654SLen Brown /* 1677495c7654SLen Brown * snapshot_sysfs_counter() 1678495c7654SLen Brown * 1679495c7654SLen Brown * return snapshot of given counter 1680495c7654SLen Brown */ 1681495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path) 1682495c7654SLen Brown { 1683495c7654SLen Brown FILE *fp; 1684495c7654SLen Brown int retval; 1685495c7654SLen Brown unsigned long long counter; 1686495c7654SLen Brown 1687495c7654SLen Brown fp = fopen_or_die(path, "r"); 1688495c7654SLen Brown 1689495c7654SLen Brown retval = fscanf(fp, "%lld", &counter); 1690495c7654SLen Brown if (retval != 1) 1691495c7654SLen Brown err(1, "snapshot_sysfs_counter(%s)", path); 1692495c7654SLen Brown 1693495c7654SLen Brown fclose(fp); 1694495c7654SLen Brown 1695495c7654SLen Brown return counter; 1696495c7654SLen Brown } 1697495c7654SLen Brown 1698495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp) 1699495c7654SLen Brown { 1700495c7654SLen Brown if (mp->msr_num != 0) { 1701495c7654SLen Brown if (get_msr(cpu, mp->msr_num, counterp)) 1702495c7654SLen Brown return -1; 1703495c7654SLen Brown } else { 170446c27978SLen Brown char path[128 + PATH_BYTES]; 170541618e63SLen Brown 170641618e63SLen Brown if (mp->flags & SYSFS_PERCPU) { 170741618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", 170841618e63SLen Brown cpu, mp->path); 170941618e63SLen Brown 171041618e63SLen Brown *counterp = snapshot_sysfs_counter(path); 171141618e63SLen Brown } else { 1712495c7654SLen Brown *counterp = snapshot_sysfs_counter(mp->path); 1713495c7654SLen Brown } 171441618e63SLen Brown } 1715495c7654SLen Brown 1716495c7654SLen Brown return 0; 1717495c7654SLen Brown } 1718495c7654SLen Brown 17194c2122d4SLen Brown void get_apic_id(struct thread_data *t) 17204c2122d4SLen Brown { 172134041551SLen Brown unsigned int eax, ebx, ecx, edx; 172234041551SLen Brown 172334041551SLen Brown if (DO_BIC(BIC_APIC)) { 172434041551SLen Brown eax = ebx = ecx = edx = 0; 172534041551SLen Brown __cpuid(1, eax, ebx, ecx, edx); 172634041551SLen Brown 172734041551SLen Brown t->apic_id = (ebx >> 24) & 0xff; 172834041551SLen Brown } 172934041551SLen Brown 173034041551SLen Brown if (!DO_BIC(BIC_X2APIC)) 173134041551SLen Brown return; 173234041551SLen Brown 173334041551SLen Brown if (authentic_amd) { 173434041551SLen Brown unsigned int topology_extensions; 173534041551SLen Brown 173634041551SLen Brown if (max_extended_level < 0x8000001e) 173734041551SLen Brown return; 17384c2122d4SLen Brown 17394c2122d4SLen Brown eax = ebx = ecx = edx = 0; 174034041551SLen Brown __cpuid(0x80000001, eax, ebx, ecx, edx); 174134041551SLen Brown topology_extensions = ecx & (1 << 22); 174234041551SLen Brown 174334041551SLen Brown if (topology_extensions == 0) 174434041551SLen Brown return; 174534041551SLen Brown 174634041551SLen Brown eax = ebx = ecx = edx = 0; 174734041551SLen Brown __cpuid(0x8000001e, eax, ebx, ecx, edx); 174834041551SLen Brown 174934041551SLen Brown t->x2apic_id = eax; 175034041551SLen Brown return; 175134041551SLen Brown } 17524c2122d4SLen Brown 17534c2122d4SLen Brown if (!genuine_intel) 17544c2122d4SLen Brown return; 17554c2122d4SLen Brown 17564c2122d4SLen Brown if (max_level < 0xb) 17574c2122d4SLen Brown return; 17584c2122d4SLen Brown 17594c2122d4SLen Brown ecx = 0; 17604c2122d4SLen Brown __cpuid(0xb, eax, ebx, ecx, edx); 17614c2122d4SLen Brown t->x2apic_id = edx; 17624c2122d4SLen Brown 176334041551SLen Brown if (debug && (t->apic_id != (t->x2apic_id & 0xff))) 176434041551SLen Brown fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", 176534041551SLen Brown t->cpu_id, t->apic_id, t->x2apic_id); 17664c2122d4SLen Brown } 17674c2122d4SLen Brown 1768495c7654SLen Brown /* 1769c98d5d94SLen Brown * get_counters(...) 1770c98d5d94SLen Brown * migrate to cpu 1771c98d5d94SLen Brown * acquire and record local counters for that cpu 1772c98d5d94SLen Brown */ 1773c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) 1774c98d5d94SLen Brown { 1775c98d5d94SLen Brown int cpu = t->cpu_id; 1776889facbeSLen Brown unsigned long long msr; 17770102b067SLen Brown int aperf_mperf_retry_count = 0; 1778388e9c81SLen Brown struct msr_counter *mp; 1779388e9c81SLen Brown int i; 1780c98d5d94SLen Brown 1781e52966c0SLen Brown if (cpu_migrate(cpu)) { 1782b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 178315aaa346SLen Brown return -1; 1784e52966c0SLen Brown } 178515aaa346SLen Brown 1786d4794f25SYazen Ghannam gettimeofday(&t->tv_begin, (struct timezone *)NULL); 1787d4794f25SYazen Ghannam 17884c2122d4SLen Brown if (first_counter_read) 17894c2122d4SLen Brown get_apic_id(t); 17900102b067SLen Brown retry: 1791c98d5d94SLen Brown t->tsc = rdtsc(); /* we are running on local CPU of interest */ 179215aaa346SLen Brown 1793*1e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || 1794*1e9042b9SSrinivas Pandruvada soft_c1_residency_display(BIC_Avg_MHz)) { 17950102b067SLen Brown unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time; 17960102b067SLen Brown 17970102b067SLen Brown /* 17980102b067SLen Brown * The TSC, APERF and MPERF must be read together for 17990102b067SLen Brown * APERF/MPERF and MPERF/TSC to give accurate results. 18000102b067SLen Brown * 18010102b067SLen Brown * Unfortunately, APERF and MPERF are read by 18020102b067SLen Brown * individual system call, so delays may occur 18030102b067SLen Brown * between them. If the time to read them 18040102b067SLen Brown * varies by a large amount, we re-read them. 18050102b067SLen Brown */ 18060102b067SLen Brown 18070102b067SLen Brown /* 18080102b067SLen Brown * This initial dummy APERF read has been seen to 18090102b067SLen Brown * reduce jitter in the subsequent reads. 18100102b067SLen Brown */ 18110102b067SLen Brown 18129c63a650SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 1813c98d5d94SLen Brown return -3; 18140102b067SLen Brown 18150102b067SLen Brown t->tsc = rdtsc(); /* re-read close to APERF */ 18160102b067SLen Brown 18170102b067SLen Brown tsc_before = t->tsc; 18180102b067SLen Brown 18190102b067SLen Brown if (get_msr(cpu, MSR_IA32_APERF, &t->aperf)) 18200102b067SLen Brown return -3; 18210102b067SLen Brown 18220102b067SLen Brown tsc_between = rdtsc(); 18230102b067SLen Brown 18249c63a650SLen Brown if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf)) 1825c98d5d94SLen Brown return -4; 18260102b067SLen Brown 18270102b067SLen Brown tsc_after = rdtsc(); 18280102b067SLen Brown 18290102b067SLen Brown aperf_time = tsc_between - tsc_before; 18300102b067SLen Brown mperf_time = tsc_after - tsc_between; 18310102b067SLen Brown 18320102b067SLen Brown /* 18330102b067SLen Brown * If the system call latency to read APERF and MPERF 18340102b067SLen Brown * differ by more than 2x, then try again. 18350102b067SLen Brown */ 18360102b067SLen Brown if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) { 18370102b067SLen Brown aperf_mperf_retry_count++; 18380102b067SLen Brown if (aperf_mperf_retry_count < 5) 18390102b067SLen Brown goto retry; 18400102b067SLen Brown else 18410102b067SLen Brown warnx("cpu%d jitter %lld %lld", 18420102b067SLen Brown cpu, aperf_time, mperf_time); 18430102b067SLen Brown } 18440102b067SLen Brown aperf_mperf_retry_count = 0; 18450102b067SLen Brown 1846b2b34dfeSHubert Chrzaniuk t->aperf = t->aperf * aperf_mperf_multiplier; 1847b2b34dfeSHubert Chrzaniuk t->mperf = t->mperf * aperf_mperf_multiplier; 184888c3281fSLen Brown } 184988c3281fSLen Brown 1850812db3f7SLen Brown if (DO_BIC(BIC_IRQ)) 1851562a2d37SLen Brown t->irq_count = irqs_per_cpu[cpu]; 1852812db3f7SLen Brown if (DO_BIC(BIC_SMI)) { 18531ed51011SLen Brown if (get_msr(cpu, MSR_SMI_COUNT, &msr)) 18541ed51011SLen Brown return -5; 18551ed51011SLen Brown t->smi_count = msr & 0xFFFFFFFF; 18561ed51011SLen Brown } 18570539ba11SLen Brown if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) { 1858144b44b1SLen Brown if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1)) 1859144b44b1SLen Brown return -6; 1860144b44b1SLen Brown } 1861144b44b1SLen Brown 1862388e9c81SLen Brown for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) { 1863495c7654SLen Brown if (get_mp(cpu, mp, &t->counter[i])) 1864388e9c81SLen Brown return -10; 1865388e9c81SLen Brown } 1866388e9c81SLen Brown 1867c98d5d94SLen Brown /* collect core counters only for 1st thread in core */ 1868c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 1869f4fdf2b4SLen Brown goto done; 1870c98d5d94SLen Brown 1871*1e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) { 1872c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3)) 1873c98d5d94SLen Brown return -6; 1874144b44b1SLen Brown } 1875144b44b1SLen Brown 1876*1e9042b9SSrinivas Pandruvada if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) { 1877c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6)) 1878c98d5d94SLen Brown return -7; 1879*1e9042b9SSrinivas Pandruvada } else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) { 1880fb5d4327SDasaratharaman Chandramouli if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6)) 1881fb5d4327SDasaratharaman Chandramouli return -7; 1882103a8feaSLen Brown } 188315aaa346SLen Brown 1884*1e9042b9SSrinivas Pandruvada if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) 1885c98d5d94SLen Brown if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7)) 1886c98d5d94SLen Brown return -8; 1887c98d5d94SLen Brown 18880539ba11SLen Brown if (DO_BIC(BIC_Mod_c6)) 18890539ba11SLen Brown if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us)) 18900539ba11SLen Brown return -8; 18910539ba11SLen Brown 1892812db3f7SLen Brown if (DO_BIC(BIC_CoreTmp)) { 1893889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 1894889facbeSLen Brown return -9; 1895889facbeSLen Brown c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 1896889facbeSLen Brown } 1897889facbeSLen Brown 18989392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 18999392bd98SCalvin Walton if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr)) 19009392bd98SCalvin Walton return -14; 19019392bd98SCalvin Walton c->core_energy = msr & 0xFFFFFFFF; 19029392bd98SCalvin Walton } 19039392bd98SCalvin Walton 1904388e9c81SLen Brown for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) { 1905495c7654SLen Brown if (get_mp(cpu, mp, &c->counter[i])) 1906388e9c81SLen Brown return -10; 1907388e9c81SLen Brown } 1908889facbeSLen Brown 1909c98d5d94SLen Brown /* collect package counters only for 1st core in package */ 1910c98d5d94SLen Brown if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 1911f4fdf2b4SLen Brown goto done; 191215aaa346SLen Brown 1913a99d8730SLen Brown if (DO_BIC(BIC_Totl_c0)) { 19140b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0)) 19150b2bb692SLen Brown return -10; 1916a99d8730SLen Brown } 1917a99d8730SLen Brown if (DO_BIC(BIC_Any_c0)) { 19180b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0)) 19190b2bb692SLen Brown return -11; 1920a99d8730SLen Brown } 1921a99d8730SLen Brown if (DO_BIC(BIC_GFX_c0)) { 19220b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0)) 19230b2bb692SLen Brown return -12; 1924a99d8730SLen Brown } 1925a99d8730SLen Brown if (DO_BIC(BIC_CPUGFX)) { 19260b2bb692SLen Brown if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0)) 19270b2bb692SLen Brown return -13; 19280b2bb692SLen Brown } 19290f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc3)) 1930c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3)) 1931c98d5d94SLen Brown return -9; 19320f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc6)) { 19330539ba11SLen Brown if (do_slm_cstates) { 19340539ba11SLen Brown if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6)) 19350539ba11SLen Brown return -10; 19360539ba11SLen Brown } else { 1937c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6)) 1938c98d5d94SLen Brown return -10; 19390539ba11SLen Brown } 19400539ba11SLen Brown } 19410539ba11SLen Brown 19420f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc2)) 1943c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2)) 1944c98d5d94SLen Brown return -11; 19450f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc7)) 1946c98d5d94SLen Brown if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7)) 1947c98d5d94SLen Brown return -12; 19480f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc8)) 1949ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8)) 1950ca58710fSKristen Carlson Accardi return -13; 19510f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc9)) 1952ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9)) 1953ca58710fSKristen Carlson Accardi return -13; 19540f47c08dSLen Brown if (DO_BIC(BIC_Pkgpc10)) 1955ca58710fSKristen Carlson Accardi if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10)) 1956ca58710fSKristen Carlson Accardi return -13; 19570f47c08dSLen Brown 1958be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 1959be0e54c4SLen Brown p->cpu_lpi = cpuidle_cur_cpu_lpi_us; 1960be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 1961be0e54c4SLen Brown p->sys_lpi = cpuidle_cur_sys_lpi_us; 1962be0e54c4SLen Brown 1963889facbeSLen Brown if (do_rapl & RAPL_PKG) { 1964889facbeSLen Brown if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr)) 1965889facbeSLen Brown return -13; 1966889facbeSLen Brown p->energy_pkg = msr & 0xFFFFFFFF; 1967889facbeSLen Brown } 19689148494cSJacob Pan if (do_rapl & RAPL_CORES_ENERGY_STATUS) { 1969889facbeSLen Brown if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr)) 1970889facbeSLen Brown return -14; 1971889facbeSLen Brown p->energy_cores = msr & 0xFFFFFFFF; 1972889facbeSLen Brown } 1973889facbeSLen Brown if (do_rapl & RAPL_DRAM) { 1974889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr)) 1975889facbeSLen Brown return -15; 1976889facbeSLen Brown p->energy_dram = msr & 0xFFFFFFFF; 1977889facbeSLen Brown } 1978889facbeSLen Brown if (do_rapl & RAPL_GFX) { 1979889facbeSLen Brown if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr)) 1980889facbeSLen Brown return -16; 1981889facbeSLen Brown p->energy_gfx = msr & 0xFFFFFFFF; 1982889facbeSLen Brown } 1983889facbeSLen Brown if (do_rapl & RAPL_PKG_PERF_STATUS) { 1984889facbeSLen Brown if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr)) 1985889facbeSLen Brown return -16; 1986889facbeSLen Brown p->rapl_pkg_perf_status = msr & 0xFFFFFFFF; 1987889facbeSLen Brown } 1988889facbeSLen Brown if (do_rapl & RAPL_DRAM_PERF_STATUS) { 1989889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr)) 1990889facbeSLen Brown return -16; 1991889facbeSLen Brown p->rapl_dram_perf_status = msr & 0xFFFFFFFF; 1992889facbeSLen Brown } 19933316f99aSCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 19943316f99aSCalvin Walton if (get_msr(cpu, MSR_PKG_ENERGY_STAT, &msr)) 19953316f99aSCalvin Walton return -13; 19963316f99aSCalvin Walton p->energy_pkg = msr & 0xFFFFFFFF; 19973316f99aSCalvin Walton } 1998812db3f7SLen Brown if (DO_BIC(BIC_PkgTmp)) { 1999889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 2000889facbeSLen Brown return -17; 2001889facbeSLen Brown p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F); 2002889facbeSLen Brown } 2003fdf676e5SLen Brown 2004812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2005fdf676e5SLen Brown p->gfx_rc6_ms = gfx_cur_rc6_ms; 2006fdf676e5SLen Brown 2007812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 200827d47356SLen Brown p->gfx_mhz = gfx_cur_mhz; 200927d47356SLen Brown 2010388e9c81SLen Brown for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) { 2011495c7654SLen Brown if (get_mp(cpu, mp, &p->counter[i])) 2012388e9c81SLen Brown return -10; 2013388e9c81SLen Brown } 2014f4fdf2b4SLen Brown done: 2015f4fdf2b4SLen Brown gettimeofday(&t->tv_end, (struct timezone *)NULL); 2016388e9c81SLen Brown 201715aaa346SLen Brown return 0; 2018103a8feaSLen Brown } 2019103a8feaSLen Brown 2020ee7e38e3SLen Brown /* 2021ee7e38e3SLen Brown * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit: 2022ee7e38e3SLen Brown * If you change the values, note they are used both in comparisons 2023ee7e38e3SLen Brown * (>= PCL__7) and to index pkg_cstate_limit_strings[]. 2024ee7e38e3SLen Brown */ 2025ee7e38e3SLen Brown 2026ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */ 2027ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */ 2028ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */ 2029ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */ 2030ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */ 2031ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */ 2032ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */ 2033ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */ 2034ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */ 2035ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */ 2036ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */ 2037ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */ 20380b2bb692SLen Brown #define PCL__8 12 /* PC8 */ 20390b2bb692SLen Brown #define PCL__9 13 /* PC9 */ 2040445640a5SLen Brown #define PCL_10 14 /* PC10 */ 2041445640a5SLen Brown #define PCLUNL 15 /* Unlimited */ 2042ee7e38e3SLen Brown 2043ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN; 2044ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2", 2045445640a5SLen Brown "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"}; 2046ee7e38e3SLen Brown 2047e9257f5fSLen 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}; 2048e9257f5fSLen 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}; 2049e9257f5fSLen 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}; 20500539ba11SLen 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}; 2051f2642888SLen 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}; 2052e9257f5fSLen 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}; 2053445640a5SLen Brown int glm_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV}; 20542085e124SArtem 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}; 2055ee7e38e3SLen Brown 2056a2b7b749SLen Brown 2057a2b7b749SLen Brown static void 2058a2b7b749SLen Brown calculate_tsc_tweak() 2059a2b7b749SLen Brown { 2060a2b7b749SLen Brown tsc_tweak = base_hz / tsc_hz; 2061a2b7b749SLen Brown } 2062a2b7b749SLen Brown 2063fcd17211SLen Brown static void 2064fcd17211SLen Brown dump_nhm_platform_info(void) 2065103a8feaSLen Brown { 2066103a8feaSLen Brown unsigned long long msr; 2067103a8feaSLen Brown unsigned int ratio; 2068103a8feaSLen Brown 2069ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 2070103a8feaSLen Brown 2071b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr); 20726574a5d5SLen Brown 2073103a8feaSLen Brown ratio = (msr >> 40) & 0xFF; 2074710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", 2075103a8feaSLen Brown ratio, bclk, ratio * bclk); 2076103a8feaSLen Brown 2077103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 2078710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 2079103a8feaSLen Brown ratio, bclk, ratio * bclk); 2080103a8feaSLen Brown 20817ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr); 2082b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n", 2083bfae2052SLen Brown base_cpu, msr, msr & 0x2 ? "EN" : "DIS"); 208467920418SLen Brown 2085fcd17211SLen Brown return; 2086fcd17211SLen Brown } 2087fcd17211SLen Brown 2088fcd17211SLen Brown static void 2089fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void) 2090fcd17211SLen Brown { 2091fcd17211SLen Brown unsigned long long msr; 2092fcd17211SLen Brown unsigned int ratio; 2093fcd17211SLen Brown 20947ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr); 2095fcd17211SLen Brown 2096b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr); 2097fcd17211SLen Brown 2098fcd17211SLen Brown ratio = (msr >> 8) & 0xFF; 2099fcd17211SLen Brown if (ratio) 2100710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", 2101fcd17211SLen Brown ratio, bclk, ratio * bclk); 2102fcd17211SLen Brown 2103fcd17211SLen Brown ratio = (msr >> 0) & 0xFF; 2104fcd17211SLen Brown if (ratio) 2105710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", 2106fcd17211SLen Brown ratio, bclk, ratio * bclk); 2107fcd17211SLen Brown return; 2108fcd17211SLen Brown } 2109fcd17211SLen Brown 2110fcd17211SLen Brown static void 2111fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void) 2112fcd17211SLen Brown { 2113fcd17211SLen Brown unsigned long long msr; 2114fcd17211SLen Brown unsigned int ratio; 21156574a5d5SLen Brown 21167ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr); 21176574a5d5SLen Brown 2118b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr); 21196574a5d5SLen Brown 21206574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 21216574a5d5SLen Brown if (ratio) 2122710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", 21236574a5d5SLen Brown ratio, bclk, ratio * bclk); 21246574a5d5SLen Brown 21256574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 21266574a5d5SLen Brown if (ratio) 2127710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", 21286574a5d5SLen Brown ratio, bclk, ratio * bclk); 21296574a5d5SLen Brown 21306574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 21316574a5d5SLen Brown if (ratio) 2132710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", 21336574a5d5SLen Brown ratio, bclk, ratio * bclk); 21346574a5d5SLen Brown 21356574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 21366574a5d5SLen Brown if (ratio) 2137710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", 21386574a5d5SLen Brown ratio, bclk, ratio * bclk); 21396574a5d5SLen Brown 21406574a5d5SLen Brown ratio = (msr >> 24) & 0xFF; 21416574a5d5SLen Brown if (ratio) 2142710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", 21436574a5d5SLen Brown ratio, bclk, ratio * bclk); 21446574a5d5SLen Brown 21456574a5d5SLen Brown ratio = (msr >> 16) & 0xFF; 21466574a5d5SLen Brown if (ratio) 2147710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", 21486574a5d5SLen Brown ratio, bclk, ratio * bclk); 21496574a5d5SLen Brown 21506574a5d5SLen Brown ratio = (msr >> 8) & 0xFF; 21516574a5d5SLen Brown if (ratio) 2152710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", 21536574a5d5SLen Brown ratio, bclk, ratio * bclk); 21546574a5d5SLen Brown 21556574a5d5SLen Brown ratio = (msr >> 0) & 0xFF; 21566574a5d5SLen Brown if (ratio) 2157710f273bSLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", 21586574a5d5SLen Brown ratio, bclk, ratio * bclk); 2159103a8feaSLen Brown return; 2160fcd17211SLen Brown } 216131e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model) 216231e07522SLen Brown { 216331e07522SLen Brown 216431e07522SLen Brown if (!genuine_intel) 216531e07522SLen Brown return 0; 216631e07522SLen Brown 216731e07522SLen Brown switch (model) { 216831e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 216931e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 2170f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: 217131e07522SLen Brown return 1; 217231e07522SLen Brown } 217331e07522SLen Brown return 0; 217431e07522SLen Brown } 2175fcd17211SLen Brown 2176fcd17211SLen Brown static void 217731e07522SLen Brown dump_turbo_ratio_limits(int family, int model) 2178fcd17211SLen Brown { 217931e07522SLen Brown unsigned long long msr, core_counts; 218031e07522SLen Brown unsigned int ratio, group_size; 2181103a8feaSLen Brown 21827ce7d5deSPrarit Bhargava get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2183b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr); 21846574a5d5SLen Brown 218531e07522SLen Brown if (has_turbo_ratio_group_limits(family, model)) { 218631e07522SLen Brown get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts); 218731e07522SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts); 218831e07522SLen Brown } else { 218931e07522SLen Brown core_counts = 0x0807060504030201; 219031e07522SLen Brown } 219131e07522SLen Brown 21926574a5d5SLen Brown ratio = (msr >> 56) & 0xFF; 219331e07522SLen Brown group_size = (core_counts >> 56) & 0xFF; 21946574a5d5SLen Brown if (ratio) 219531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 219631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 21976574a5d5SLen Brown 21986574a5d5SLen Brown ratio = (msr >> 48) & 0xFF; 219931e07522SLen Brown group_size = (core_counts >> 48) & 0xFF; 22006574a5d5SLen Brown if (ratio) 220131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 220231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 22036574a5d5SLen Brown 22046574a5d5SLen Brown ratio = (msr >> 40) & 0xFF; 220531e07522SLen Brown group_size = (core_counts >> 40) & 0xFF; 22066574a5d5SLen Brown if (ratio) 220731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 220831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 22096574a5d5SLen Brown 22106574a5d5SLen Brown ratio = (msr >> 32) & 0xFF; 221131e07522SLen Brown group_size = (core_counts >> 32) & 0xFF; 22126574a5d5SLen Brown if (ratio) 221331e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 221431e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 22156574a5d5SLen Brown 2216103a8feaSLen Brown ratio = (msr >> 24) & 0xFF; 221731e07522SLen Brown group_size = (core_counts >> 24) & 0xFF; 2218103a8feaSLen Brown if (ratio) 221931e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 222031e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2221103a8feaSLen Brown 2222103a8feaSLen Brown ratio = (msr >> 16) & 0xFF; 222331e07522SLen Brown group_size = (core_counts >> 16) & 0xFF; 2224103a8feaSLen Brown if (ratio) 222531e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 222631e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2227103a8feaSLen Brown 2228103a8feaSLen Brown ratio = (msr >> 8) & 0xFF; 222931e07522SLen Brown group_size = (core_counts >> 8) & 0xFF; 2230103a8feaSLen Brown if (ratio) 223131e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 223231e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2233103a8feaSLen Brown 2234103a8feaSLen Brown ratio = (msr >> 0) & 0xFF; 223531e07522SLen Brown group_size = (core_counts >> 0) & 0xFF; 2236103a8feaSLen Brown if (ratio) 223731e07522SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n", 223831e07522SLen Brown ratio, bclk, ratio * bclk, group_size); 2239fcd17211SLen Brown return; 2240fcd17211SLen Brown } 22413a9a941dSLen Brown 2242fcd17211SLen Brown static void 22430f7887c4SLen Brown dump_atom_turbo_ratio_limits(void) 22440f7887c4SLen Brown { 22450f7887c4SLen Brown unsigned long long msr; 22460f7887c4SLen Brown unsigned int ratio; 22470f7887c4SLen Brown 22480f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr); 22490f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 22500f7887c4SLen Brown 22510f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 22520f7887c4SLen Brown if (ratio) 22530f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", 22540f7887c4SLen Brown ratio, bclk, ratio * bclk); 22550f7887c4SLen Brown 22560f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 22570f7887c4SLen Brown if (ratio) 22580f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", 22590f7887c4SLen Brown ratio, bclk, ratio * bclk); 22600f7887c4SLen Brown 22610f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 22620f7887c4SLen Brown if (ratio) 22630f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", 22640f7887c4SLen Brown ratio, bclk, ratio * bclk); 22650f7887c4SLen Brown 22660f7887c4SLen Brown get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr); 22670f7887c4SLen Brown fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF); 22680f7887c4SLen Brown 22690f7887c4SLen Brown ratio = (msr >> 24) & 0x3F; 22700f7887c4SLen Brown if (ratio) 22710f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", 22720f7887c4SLen Brown ratio, bclk, ratio * bclk); 22730f7887c4SLen Brown 22740f7887c4SLen Brown ratio = (msr >> 16) & 0x3F; 22750f7887c4SLen Brown if (ratio) 22760f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", 22770f7887c4SLen Brown ratio, bclk, ratio * bclk); 22780f7887c4SLen Brown 22790f7887c4SLen Brown ratio = (msr >> 8) & 0x3F; 22800f7887c4SLen Brown if (ratio) 22810f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", 22820f7887c4SLen Brown ratio, bclk, ratio * bclk); 22830f7887c4SLen Brown 22840f7887c4SLen Brown ratio = (msr >> 0) & 0x3F; 22850f7887c4SLen Brown if (ratio) 22860f7887c4SLen Brown fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", 22870f7887c4SLen Brown ratio, bclk, ratio * bclk); 22880f7887c4SLen Brown } 22890f7887c4SLen Brown 22900f7887c4SLen Brown static void 2291fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void) 2292fb5d4327SDasaratharaman Chandramouli { 2293cbf97abaSHubert Chrzaniuk const unsigned int buckets_no = 7; 2294cbf97abaSHubert Chrzaniuk 2295fb5d4327SDasaratharaman Chandramouli unsigned long long msr; 2296cbf97abaSHubert Chrzaniuk int delta_cores, delta_ratio; 2297cbf97abaSHubert Chrzaniuk int i, b_nr; 2298cbf97abaSHubert Chrzaniuk unsigned int cores[buckets_no]; 2299cbf97abaSHubert Chrzaniuk unsigned int ratio[buckets_no]; 2300fb5d4327SDasaratharaman Chandramouli 2301ebf5926aSSrinivas Pandruvada get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr); 2302fb5d4327SDasaratharaman Chandramouli 2303b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", 2304bfae2052SLen Brown base_cpu, msr); 2305fb5d4327SDasaratharaman Chandramouli 2306fb5d4327SDasaratharaman Chandramouli /** 2307fb5d4327SDasaratharaman Chandramouli * Turbo encoding in KNL is as follows: 2308cbf97abaSHubert Chrzaniuk * [0] -- Reserved 2309cbf97abaSHubert Chrzaniuk * [7:1] -- Base value of number of active cores of bucket 1. 2310fb5d4327SDasaratharaman Chandramouli * [15:8] -- Base value of freq ratio of bucket 1. 2311fb5d4327SDasaratharaman Chandramouli * [20:16] -- +ve delta of number of active cores of bucket 2. 2312fb5d4327SDasaratharaman Chandramouli * i.e. active cores of bucket 2 = 2313fb5d4327SDasaratharaman Chandramouli * active cores of bucket 1 + delta 2314fb5d4327SDasaratharaman Chandramouli * [23:21] -- Negative delta of freq ratio of bucket 2. 2315fb5d4327SDasaratharaman Chandramouli * i.e. freq ratio of bucket 2 = 2316fb5d4327SDasaratharaman Chandramouli * freq ratio of bucket 1 - delta 2317fb5d4327SDasaratharaman Chandramouli * [28:24]-- +ve delta of number of active cores of bucket 3. 2318fb5d4327SDasaratharaman Chandramouli * [31:29]-- -ve delta of freq ratio of bucket 3. 2319fb5d4327SDasaratharaman Chandramouli * [36:32]-- +ve delta of number of active cores of bucket 4. 2320fb5d4327SDasaratharaman Chandramouli * [39:37]-- -ve delta of freq ratio of bucket 4. 2321fb5d4327SDasaratharaman Chandramouli * [44:40]-- +ve delta of number of active cores of bucket 5. 2322fb5d4327SDasaratharaman Chandramouli * [47:45]-- -ve delta of freq ratio of bucket 5. 2323fb5d4327SDasaratharaman Chandramouli * [52:48]-- +ve delta of number of active cores of bucket 6. 2324fb5d4327SDasaratharaman Chandramouli * [55:53]-- -ve delta of freq ratio of bucket 6. 2325fb5d4327SDasaratharaman Chandramouli * [60:56]-- +ve delta of number of active cores of bucket 7. 2326fb5d4327SDasaratharaman Chandramouli * [63:61]-- -ve delta of freq ratio of bucket 7. 2327fb5d4327SDasaratharaman Chandramouli */ 2328fb5d4327SDasaratharaman Chandramouli 2329cbf97abaSHubert Chrzaniuk b_nr = 0; 2330cbf97abaSHubert Chrzaniuk cores[b_nr] = (msr & 0xFF) >> 1; 2331cbf97abaSHubert Chrzaniuk ratio[b_nr] = (msr >> 8) & 0xFF; 2332cbf97abaSHubert Chrzaniuk 2333cbf97abaSHubert Chrzaniuk for (i = 16; i < 64; i += 8) { 2334fb5d4327SDasaratharaman Chandramouli delta_cores = (msr >> i) & 0x1F; 2335cbf97abaSHubert Chrzaniuk delta_ratio = (msr >> (i + 5)) & 0x7; 2336fb5d4327SDasaratharaman Chandramouli 2337cbf97abaSHubert Chrzaniuk cores[b_nr + 1] = cores[b_nr] + delta_cores; 2338cbf97abaSHubert Chrzaniuk ratio[b_nr + 1] = ratio[b_nr] - delta_ratio; 2339cbf97abaSHubert Chrzaniuk b_nr++; 2340fb5d4327SDasaratharaman Chandramouli } 2341cbf97abaSHubert Chrzaniuk 2342cbf97abaSHubert Chrzaniuk for (i = buckets_no - 1; i >= 0; i--) 2343cbf97abaSHubert Chrzaniuk if (i > 0 ? ratio[i] != ratio[i - 1] : 1) 2344b7d8c148SLen Brown fprintf(outf, 2345710f273bSLen Brown "%d * %.1f = %.1f MHz max turbo %d active cores\n", 2346cbf97abaSHubert Chrzaniuk ratio[i], bclk, ratio[i] * bclk, cores[i]); 2347fb5d4327SDasaratharaman Chandramouli } 2348fb5d4327SDasaratharaman Chandramouli 2349fb5d4327SDasaratharaman Chandramouli static void 2350fcd17211SLen Brown dump_nhm_cst_cfg(void) 2351fcd17211SLen Brown { 2352fcd17211SLen Brown unsigned long long msr; 2353fcd17211SLen Brown 23541df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 2355fcd17211SLen Brown 23561df2e55aSLen Brown fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr); 2357fcd17211SLen Brown 23583e8b62bfSArtem Bityutskiy fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)", 2359fcd17211SLen Brown (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "", 2360fcd17211SLen Brown (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "", 2361fcd17211SLen Brown (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "", 2362fcd17211SLen Brown (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "", 2363fcd17211SLen Brown (msr & (1 << 15)) ? "" : "UN", 23646c34f160SLen Brown (unsigned int)msr & 0xF, 2365fcd17211SLen Brown pkg_cstate_limit_strings[pkg_cstate_limit]); 2366ac980e13SArtem Bityutskiy 2367ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION (1UL << 16) 2368ac980e13SArtem Bityutskiy if (has_automatic_cstate_conversion) { 2369ac980e13SArtem Bityutskiy fprintf(outf, ", automatic c-state conversion=%s", 2370ac980e13SArtem Bityutskiy (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off"); 2371ac980e13SArtem Bityutskiy } 2372ac980e13SArtem Bityutskiy 2373ac980e13SArtem Bityutskiy fprintf(outf, ")\n"); 2374ac980e13SArtem Bityutskiy 2375fcd17211SLen Brown return; 2376103a8feaSLen Brown } 2377103a8feaSLen Brown 23786fb3143bSLen Brown static void 23796fb3143bSLen Brown dump_config_tdp(void) 23806fb3143bSLen Brown { 23816fb3143bSLen Brown unsigned long long msr; 23826fb3143bSLen Brown 23836fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr); 2384b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr); 2385685b535bSChen Yu fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF); 23866fb3143bSLen Brown 23876fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr); 2388b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr); 23896fb3143bSLen Brown if (msr) { 2390685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2391685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2392685b535bSChen Yu fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2393685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF); 23946fb3143bSLen Brown } 2395b7d8c148SLen Brown fprintf(outf, ")\n"); 23966fb3143bSLen Brown 23976fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr); 2398b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr); 23996fb3143bSLen Brown if (msr) { 2400685b535bSChen Yu fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF); 2401685b535bSChen Yu fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF); 2402685b535bSChen Yu fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF); 2403685b535bSChen Yu fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF); 24046fb3143bSLen Brown } 2405b7d8c148SLen Brown fprintf(outf, ")\n"); 24066fb3143bSLen Brown 24076fb3143bSLen Brown get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr); 2408b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr); 24096fb3143bSLen Brown if ((msr) & 0x3) 2410b7d8c148SLen Brown fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3); 2411b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2412b7d8c148SLen Brown fprintf(outf, ")\n"); 24136fb3143bSLen Brown 24146fb3143bSLen Brown get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr); 2415b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr); 2416685b535bSChen Yu fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF); 2417b7d8c148SLen Brown fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1); 2418b7d8c148SLen Brown fprintf(outf, ")\n"); 24196fb3143bSLen Brown } 24205a63426eSLen Brown 24215a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 }; 24225a63426eSLen Brown 24235a63426eSLen Brown void print_irtl(void) 24245a63426eSLen Brown { 24255a63426eSLen Brown unsigned long long msr; 24265a63426eSLen Brown 24275a63426eSLen Brown get_msr(base_cpu, MSR_PKGC3_IRTL, &msr); 24285a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr); 24295a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24305a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24315a63426eSLen Brown 24325a63426eSLen Brown get_msr(base_cpu, MSR_PKGC6_IRTL, &msr); 24335a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr); 24345a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24355a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24365a63426eSLen Brown 24375a63426eSLen Brown get_msr(base_cpu, MSR_PKGC7_IRTL, &msr); 24385a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr); 24395a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24405a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24415a63426eSLen Brown 24425a63426eSLen Brown if (!do_irtl_hsw) 24435a63426eSLen Brown return; 24445a63426eSLen Brown 24455a63426eSLen Brown get_msr(base_cpu, MSR_PKGC8_IRTL, &msr); 24465a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr); 24475a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24485a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24495a63426eSLen Brown 24505a63426eSLen Brown get_msr(base_cpu, MSR_PKGC9_IRTL, &msr); 24515a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr); 24525a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24535a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24545a63426eSLen Brown 24555a63426eSLen Brown get_msr(base_cpu, MSR_PKGC10_IRTL, &msr); 24565a63426eSLen Brown fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr); 24575a63426eSLen Brown fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT", 24585a63426eSLen Brown (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]); 24595a63426eSLen Brown 24605a63426eSLen Brown } 246136229897SLen Brown void free_fd_percpu(void) 246236229897SLen Brown { 246336229897SLen Brown int i; 246436229897SLen Brown 246501a67adfSMika Westerberg for (i = 0; i < topo.max_cpu_num + 1; ++i) { 246636229897SLen Brown if (fd_percpu[i] != 0) 246736229897SLen Brown close(fd_percpu[i]); 246836229897SLen Brown } 246936229897SLen Brown 247036229897SLen Brown free(fd_percpu); 24716fb3143bSLen Brown } 24726fb3143bSLen Brown 2473c98d5d94SLen Brown void free_all_buffers(void) 2474103a8feaSLen Brown { 24750e2d8f05SLen Brown int i; 24760e2d8f05SLen Brown 2477c98d5d94SLen Brown CPU_FREE(cpu_present_set); 2478c98d5d94SLen Brown cpu_present_set = NULL; 247936229897SLen Brown cpu_present_setsize = 0; 2480103a8feaSLen Brown 2481c98d5d94SLen Brown CPU_FREE(cpu_affinity_set); 2482c98d5d94SLen Brown cpu_affinity_set = NULL; 2483c98d5d94SLen Brown cpu_affinity_setsize = 0; 2484103a8feaSLen Brown 2485c98d5d94SLen Brown free(thread_even); 2486c98d5d94SLen Brown free(core_even); 2487c98d5d94SLen Brown free(package_even); 2488103a8feaSLen Brown 2489c98d5d94SLen Brown thread_even = NULL; 2490c98d5d94SLen Brown core_even = NULL; 2491c98d5d94SLen Brown package_even = NULL; 2492103a8feaSLen Brown 2493c98d5d94SLen Brown free(thread_odd); 2494c98d5d94SLen Brown free(core_odd); 2495c98d5d94SLen Brown free(package_odd); 2496103a8feaSLen Brown 2497c98d5d94SLen Brown thread_odd = NULL; 2498c98d5d94SLen Brown core_odd = NULL; 2499c98d5d94SLen Brown package_odd = NULL; 2500103a8feaSLen Brown 2501c98d5d94SLen Brown free(output_buffer); 2502c98d5d94SLen Brown output_buffer = NULL; 2503c98d5d94SLen Brown outp = NULL; 250436229897SLen Brown 250536229897SLen Brown free_fd_percpu(); 2506562a2d37SLen Brown 2507562a2d37SLen Brown free(irq_column_2_cpu); 2508562a2d37SLen Brown free(irqs_per_cpu); 25090e2d8f05SLen Brown 25100e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 25110e2d8f05SLen Brown if (cpus[i].put_ids) 25120e2d8f05SLen Brown CPU_FREE(cpus[i].put_ids); 25130e2d8f05SLen Brown } 25140e2d8f05SLen Brown free(cpus); 2515103a8feaSLen Brown } 2516103a8feaSLen Brown 251757a42a34SJosh Triplett 251857a42a34SJosh Triplett /* 251995aebc44SJosh Triplett * Parse a file containing a single int. 25206de68fe1SLen Brown * Return 0 if file can not be opened 25216de68fe1SLen Brown * Exit if file can be opened, but can not be parsed 252295aebc44SJosh Triplett */ 252395aebc44SJosh Triplett int parse_int_file(const char *fmt, ...) 252495aebc44SJosh Triplett { 252595aebc44SJosh Triplett va_list args; 252695aebc44SJosh Triplett char path[PATH_MAX]; 252795aebc44SJosh Triplett FILE *filep; 252895aebc44SJosh Triplett int value; 252995aebc44SJosh Triplett 253095aebc44SJosh Triplett va_start(args, fmt); 253195aebc44SJosh Triplett vsnprintf(path, sizeof(path), fmt, args); 253295aebc44SJosh Triplett va_end(args); 25336de68fe1SLen Brown filep = fopen(path, "r"); 25346de68fe1SLen Brown if (!filep) 25356de68fe1SLen Brown return 0; 2536b2c95d90SJosh Triplett if (fscanf(filep, "%d", &value) != 1) 2537b2c95d90SJosh Triplett err(1, "%s: failed to parse number from file", path); 253895aebc44SJosh Triplett fclose(filep); 253995aebc44SJosh Triplett return value; 254095aebc44SJosh Triplett } 254195aebc44SJosh Triplett 254295aebc44SJosh Triplett /* 2543c98d5d94SLen Brown * cpu_is_first_core_in_package(cpu) 2544c98d5d94SLen Brown * return 1 if given CPU is 1st core in package 2545c98d5d94SLen Brown */ 2546c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu) 2547c98d5d94SLen Brown { 254895aebc44SJosh Triplett return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); 2549103a8feaSLen Brown } 2550103a8feaSLen Brown 2551103a8feaSLen Brown int get_physical_package_id(int cpu) 2552103a8feaSLen Brown { 255395aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); 2554103a8feaSLen Brown } 2555103a8feaSLen Brown 25566de68fe1SLen Brown int get_die_id(int cpu) 25576de68fe1SLen Brown { 25586de68fe1SLen Brown return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu); 25596de68fe1SLen Brown } 25606de68fe1SLen Brown 2561103a8feaSLen Brown int get_core_id(int cpu) 2562103a8feaSLen Brown { 256395aebc44SJosh Triplett return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); 2564103a8feaSLen Brown } 2565103a8feaSLen Brown 2566ef605741SPrarit Bhargava void set_node_data(void) 2567ef605741SPrarit Bhargava { 25682ffbb224SPrarit Bhargava int pkg, node, lnode, cpu, cpux; 25692ffbb224SPrarit Bhargava int cpu_count; 2570ef605741SPrarit Bhargava 25712ffbb224SPrarit Bhargava /* initialize logical_node_id */ 25722ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) 25732ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = -1; 2574ef605741SPrarit Bhargava 25752ffbb224SPrarit Bhargava cpu_count = 0; 25762ffbb224SPrarit Bhargava for (pkg = 0; pkg < topo.num_packages; pkg++) { 25772ffbb224SPrarit Bhargava lnode = 0; 25782ffbb224SPrarit Bhargava for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) { 25792ffbb224SPrarit Bhargava if (cpus[cpu].physical_package_id != pkg) 2580ef605741SPrarit Bhargava continue; 25812ffbb224SPrarit Bhargava /* find a cpu with an unset logical_node_id */ 25822ffbb224SPrarit Bhargava if (cpus[cpu].logical_node_id != -1) 25832ffbb224SPrarit Bhargava continue; 25842ffbb224SPrarit Bhargava cpus[cpu].logical_node_id = lnode; 2585ef605741SPrarit Bhargava node = cpus[cpu].physical_node_id; 25862ffbb224SPrarit Bhargava cpu_count++; 25872ffbb224SPrarit Bhargava /* 25882ffbb224SPrarit Bhargava * find all matching cpus on this pkg and set 25892ffbb224SPrarit Bhargava * the logical_node_id 25902ffbb224SPrarit Bhargava */ 25912ffbb224SPrarit Bhargava for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) { 25922ffbb224SPrarit Bhargava if ((cpus[cpux].physical_package_id == pkg) && 25932ffbb224SPrarit Bhargava (cpus[cpux].physical_node_id == node)) { 25942ffbb224SPrarit Bhargava cpus[cpux].logical_node_id = lnode; 25952ffbb224SPrarit Bhargava cpu_count++; 2596ef605741SPrarit Bhargava } 25972ffbb224SPrarit Bhargava } 25982ffbb224SPrarit Bhargava lnode++; 25992ffbb224SPrarit Bhargava if (lnode > topo.nodes_per_pkg) 26002ffbb224SPrarit Bhargava topo.nodes_per_pkg = lnode; 26012ffbb224SPrarit Bhargava } 26022ffbb224SPrarit Bhargava if (cpu_count >= topo.max_cpu_num) 26032ffbb224SPrarit Bhargava break; 26042ffbb224SPrarit Bhargava } 2605ef605741SPrarit Bhargava } 2606ef605741SPrarit Bhargava 2607ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu) 2608c98d5d94SLen Brown { 2609c98d5d94SLen Brown char path[80]; 2610c98d5d94SLen Brown FILE *filep; 26110e2d8f05SLen Brown int i; 26120e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 2613c98d5d94SLen Brown 26140e2d8f05SLen Brown for (i = 0; i <= topo.max_cpu_num; i++) { 26150e2d8f05SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", 26160e2d8f05SLen Brown cpu, i); 26170e2d8f05SLen Brown filep = fopen(path, "r"); 26180e2d8f05SLen Brown if (!filep) 26190e2d8f05SLen Brown continue; 26200e2d8f05SLen Brown fclose(filep); 26210e2d8f05SLen Brown return i; 26220e2d8f05SLen Brown } 26230e2d8f05SLen Brown return -1; 2624e275b388SDasaratharaman Chandramouli } 2625c98d5d94SLen Brown 26260e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu) 26270e2d8f05SLen Brown { 26280e2d8f05SLen Brown char path[80], character; 26290e2d8f05SLen Brown FILE *filep; 26300e2d8f05SLen Brown unsigned long map; 26318cb48b32SPrarit Bhargava int so, shift, sib_core; 26320e2d8f05SLen Brown int cpu = thiscpu->logical_cpu_id; 26330e2d8f05SLen Brown int offset = topo.max_cpu_num + 1; 26340e2d8f05SLen Brown size_t size; 26358cb48b32SPrarit Bhargava int thread_id = 0; 26360e2d8f05SLen Brown 26370e2d8f05SLen Brown thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1)); 26388cb48b32SPrarit Bhargava if (thiscpu->thread_id < 0) 26398cb48b32SPrarit Bhargava thiscpu->thread_id = thread_id++; 26400e2d8f05SLen Brown if (!thiscpu->put_ids) 26410e2d8f05SLen Brown return -1; 26420e2d8f05SLen Brown 26430e2d8f05SLen Brown size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 26440e2d8f05SLen Brown CPU_ZERO_S(size, thiscpu->put_ids); 26450e2d8f05SLen Brown 26460e2d8f05SLen Brown sprintf(path, 26470e2d8f05SLen Brown "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu); 26480e2d8f05SLen Brown filep = fopen_or_die(path, "r"); 26490e2d8f05SLen Brown do { 26500e2d8f05SLen Brown offset -= BITMASK_SIZE; 26518173c336SBen Hutchings if (fscanf(filep, "%lx%c", &map, &character) != 2) 26528173c336SBen Hutchings err(1, "%s: failed to parse file", path); 26530e2d8f05SLen Brown for (shift = 0; shift < BITMASK_SIZE; shift++) { 26540e2d8f05SLen Brown if ((map >> shift) & 0x1) { 26558cb48b32SPrarit Bhargava so = shift + offset; 26568cb48b32SPrarit Bhargava sib_core = get_core_id(so); 26578cb48b32SPrarit Bhargava if (sib_core == thiscpu->physical_core_id) { 26588cb48b32SPrarit Bhargava CPU_SET_S(so, size, thiscpu->put_ids); 26598cb48b32SPrarit Bhargava if ((so != cpu) && 26608cb48b32SPrarit Bhargava (cpus[so].thread_id < 0)) 26618cb48b32SPrarit Bhargava cpus[so].thread_id = 26628cb48b32SPrarit Bhargava thread_id++; 26638cb48b32SPrarit Bhargava } 26640e2d8f05SLen Brown } 26650e2d8f05SLen Brown } 26660e2d8f05SLen Brown } while (!strncmp(&character, ",", 1)); 2667c98d5d94SLen Brown fclose(filep); 26680e2d8f05SLen Brown 26690e2d8f05SLen Brown return CPU_COUNT_S(size, thiscpu->put_ids); 2670c98d5d94SLen Brown } 2671c98d5d94SLen Brown 2672c98d5d94SLen Brown /* 2673c98d5d94SLen Brown * run func(thread, core, package) in topology order 2674c98d5d94SLen Brown * skip non-present cpus 2675103a8feaSLen Brown */ 2676103a8feaSLen Brown 2677c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *, 2678c98d5d94SLen Brown struct pkg_data *, struct thread_data *, struct core_data *, 2679c98d5d94SLen Brown struct pkg_data *), struct thread_data *thread_base, 2680c98d5d94SLen Brown struct core_data *core_base, struct pkg_data *pkg_base, 2681c98d5d94SLen Brown struct thread_data *thread_base2, struct core_data *core_base2, 2682c98d5d94SLen Brown struct pkg_data *pkg_base2) 2683c98d5d94SLen Brown { 268440f5cfe7SPrarit Bhargava int retval, pkg_no, node_no, core_no, thread_no; 2685c98d5d94SLen Brown 2686c98d5d94SLen Brown for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) { 268740f5cfe7SPrarit Bhargava for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) { 268840f5cfe7SPrarit Bhargava for (core_no = 0; core_no < topo.cores_per_node; 268940f5cfe7SPrarit Bhargava ++core_no) { 2690c98d5d94SLen Brown for (thread_no = 0; thread_no < 269170a9c6e8SPrarit Bhargava topo.threads_per_core; ++thread_no) { 2692c98d5d94SLen Brown struct thread_data *t, *t2; 2693c98d5d94SLen Brown struct core_data *c, *c2; 2694c98d5d94SLen Brown struct pkg_data *p, *p2; 2695c98d5d94SLen Brown 269640f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_no, 269740f5cfe7SPrarit Bhargava core_no, node_no, 269840f5cfe7SPrarit Bhargava pkg_no); 2699c98d5d94SLen Brown 2700c98d5d94SLen Brown if (cpu_is_not_present(t->cpu_id)) 2701c98d5d94SLen Brown continue; 2702c98d5d94SLen Brown 270340f5cfe7SPrarit Bhargava t2 = GET_THREAD(thread_base2, thread_no, 270440f5cfe7SPrarit Bhargava core_no, node_no, 270540f5cfe7SPrarit Bhargava pkg_no); 2706c98d5d94SLen Brown 270740f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_no, 270840f5cfe7SPrarit Bhargava node_no, pkg_no); 270940f5cfe7SPrarit Bhargava c2 = GET_CORE(core_base2, core_no, 271040f5cfe7SPrarit Bhargava node_no, 271140f5cfe7SPrarit Bhargava pkg_no); 2712c98d5d94SLen Brown 2713c98d5d94SLen Brown p = GET_PKG(pkg_base, pkg_no); 2714c98d5d94SLen Brown p2 = GET_PKG(pkg_base2, pkg_no); 2715c98d5d94SLen Brown 2716c98d5d94SLen Brown retval = func(t, c, p, t2, c2, p2); 2717c98d5d94SLen Brown if (retval) 2718c98d5d94SLen Brown return retval; 2719c98d5d94SLen Brown } 2720c98d5d94SLen Brown } 2721c98d5d94SLen Brown } 272240f5cfe7SPrarit Bhargava } 2723c98d5d94SLen Brown return 0; 2724c98d5d94SLen Brown } 2725c98d5d94SLen Brown 2726c98d5d94SLen Brown /* 2727c98d5d94SLen Brown * run func(cpu) on every cpu in /proc/stat 2728c98d5d94SLen Brown * return max_cpu number 2729c98d5d94SLen Brown */ 2730c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int)) 2731103a8feaSLen Brown { 2732103a8feaSLen Brown FILE *fp; 2733c98d5d94SLen Brown int cpu_num; 2734103a8feaSLen Brown int retval; 2735103a8feaSLen Brown 273657a42a34SJosh Triplett fp = fopen_or_die(proc_stat, "r"); 2737103a8feaSLen Brown 2738103a8feaSLen Brown retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); 2739b2c95d90SJosh Triplett if (retval != 0) 2740b2c95d90SJosh Triplett err(1, "%s: failed to parse format", proc_stat); 2741103a8feaSLen Brown 2742c98d5d94SLen Brown while (1) { 2743c98d5d94SLen Brown retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); 2744103a8feaSLen Brown if (retval != 1) 2745103a8feaSLen Brown break; 2746103a8feaSLen Brown 2747c98d5d94SLen Brown retval = func(cpu_num); 2748c98d5d94SLen Brown if (retval) { 2749c98d5d94SLen Brown fclose(fp); 2750c98d5d94SLen Brown return(retval); 2751c98d5d94SLen Brown } 2752103a8feaSLen Brown } 2753103a8feaSLen Brown fclose(fp); 2754c98d5d94SLen Brown return 0; 2755103a8feaSLen Brown } 2756103a8feaSLen Brown 2757103a8feaSLen Brown void re_initialize(void) 2758103a8feaSLen Brown { 2759c98d5d94SLen Brown free_all_buffers(); 2760c98d5d94SLen Brown setup_all_buffers(); 2761c98d5d94SLen Brown printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus); 2762103a8feaSLen Brown } 2763103a8feaSLen Brown 2764843c5791SPrarit Bhargava void set_max_cpu_num(void) 2765843c5791SPrarit Bhargava { 2766843c5791SPrarit Bhargava FILE *filep; 2767843c5791SPrarit Bhargava unsigned long dummy; 2768843c5791SPrarit Bhargava 2769843c5791SPrarit Bhargava topo.max_cpu_num = 0; 2770843c5791SPrarit Bhargava filep = fopen_or_die( 2771843c5791SPrarit Bhargava "/sys/devices/system/cpu/cpu0/topology/thread_siblings", 2772843c5791SPrarit Bhargava "r"); 2773843c5791SPrarit Bhargava while (fscanf(filep, "%lx,", &dummy) == 1) 27740e2d8f05SLen Brown topo.max_cpu_num += BITMASK_SIZE; 2775843c5791SPrarit Bhargava fclose(filep); 2776843c5791SPrarit Bhargava topo.max_cpu_num--; /* 0 based */ 2777843c5791SPrarit Bhargava } 2778c98d5d94SLen Brown 2779103a8feaSLen Brown /* 2780c98d5d94SLen Brown * count_cpus() 2781c98d5d94SLen Brown * remember the last one seen, it will be the max 2782103a8feaSLen Brown */ 2783c98d5d94SLen Brown int count_cpus(int cpu) 2784103a8feaSLen Brown { 2785843c5791SPrarit Bhargava topo.num_cpus++; 2786c98d5d94SLen Brown return 0; 2787103a8feaSLen Brown } 2788c98d5d94SLen Brown int mark_cpu_present(int cpu) 2789c98d5d94SLen Brown { 2790c98d5d94SLen Brown CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set); 279115aaa346SLen Brown return 0; 2792103a8feaSLen Brown } 2793103a8feaSLen Brown 27948cb48b32SPrarit Bhargava int init_thread_id(int cpu) 27958cb48b32SPrarit Bhargava { 27968cb48b32SPrarit Bhargava cpus[cpu].thread_id = -1; 27978cb48b32SPrarit Bhargava return 0; 27988cb48b32SPrarit Bhargava } 27998cb48b32SPrarit Bhargava 2800562a2d37SLen Brown /* 2801562a2d37SLen Brown * snapshot_proc_interrupts() 2802562a2d37SLen Brown * 2803562a2d37SLen Brown * read and record summary of /proc/interrupts 2804562a2d37SLen Brown * 2805562a2d37SLen Brown * return 1 if config change requires a restart, else return 0 2806562a2d37SLen Brown */ 2807562a2d37SLen Brown int snapshot_proc_interrupts(void) 2808562a2d37SLen Brown { 2809562a2d37SLen Brown static FILE *fp; 2810562a2d37SLen Brown int column, retval; 2811562a2d37SLen Brown 2812562a2d37SLen Brown if (fp == NULL) 2813562a2d37SLen Brown fp = fopen_or_die("/proc/interrupts", "r"); 2814562a2d37SLen Brown else 2815562a2d37SLen Brown rewind(fp); 2816562a2d37SLen Brown 2817562a2d37SLen Brown /* read 1st line of /proc/interrupts to get cpu* name for each column */ 2818562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2819562a2d37SLen Brown int cpu_number; 2820562a2d37SLen Brown 2821562a2d37SLen Brown retval = fscanf(fp, " CPU%d", &cpu_number); 2822562a2d37SLen Brown if (retval != 1) 2823562a2d37SLen Brown break; 2824562a2d37SLen Brown 2825562a2d37SLen Brown if (cpu_number > topo.max_cpu_num) { 2826562a2d37SLen Brown warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num); 2827562a2d37SLen Brown return 1; 2828562a2d37SLen Brown } 2829562a2d37SLen Brown 2830562a2d37SLen Brown irq_column_2_cpu[column] = cpu_number; 2831562a2d37SLen Brown irqs_per_cpu[cpu_number] = 0; 2832562a2d37SLen Brown } 2833562a2d37SLen Brown 2834562a2d37SLen Brown /* read /proc/interrupt count lines and sum up irqs per cpu */ 2835562a2d37SLen Brown while (1) { 2836562a2d37SLen Brown int column; 2837562a2d37SLen Brown char buf[64]; 2838562a2d37SLen Brown 2839562a2d37SLen Brown retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */ 2840562a2d37SLen Brown if (retval != 1) 2841562a2d37SLen Brown break; 2842562a2d37SLen Brown 2843562a2d37SLen Brown /* read the count per cpu */ 2844562a2d37SLen Brown for (column = 0; column < topo.num_cpus; ++column) { 2845562a2d37SLen Brown 2846562a2d37SLen Brown int cpu_number, irq_count; 2847562a2d37SLen Brown 2848562a2d37SLen Brown retval = fscanf(fp, " %d", &irq_count); 2849562a2d37SLen Brown if (retval != 1) 2850562a2d37SLen Brown break; 2851562a2d37SLen Brown 2852562a2d37SLen Brown cpu_number = irq_column_2_cpu[column]; 2853562a2d37SLen Brown irqs_per_cpu[cpu_number] += irq_count; 2854562a2d37SLen Brown 2855562a2d37SLen Brown } 2856562a2d37SLen Brown 2857562a2d37SLen Brown while (getc(fp) != '\n') 2858562a2d37SLen Brown ; /* flush interrupt description */ 2859562a2d37SLen Brown 2860562a2d37SLen Brown } 2861562a2d37SLen Brown return 0; 2862562a2d37SLen Brown } 286327d47356SLen Brown /* 2864fdf676e5SLen Brown * snapshot_gfx_rc6_ms() 2865fdf676e5SLen Brown * 2866fdf676e5SLen Brown * record snapshot of 2867fdf676e5SLen Brown * /sys/class/drm/card0/power/rc6_residency_ms 2868fdf676e5SLen Brown * 2869fdf676e5SLen Brown * return 1 if config change requires a restart, else return 0 2870fdf676e5SLen Brown */ 2871fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void) 2872fdf676e5SLen Brown { 2873fdf676e5SLen Brown FILE *fp; 2874fdf676e5SLen Brown int retval; 2875fdf676e5SLen Brown 2876fdf676e5SLen Brown fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r"); 2877fdf676e5SLen Brown 2878fdf676e5SLen Brown retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms); 2879fdf676e5SLen Brown if (retval != 1) 2880fdf676e5SLen Brown err(1, "GFX rc6"); 2881fdf676e5SLen Brown 2882fdf676e5SLen Brown fclose(fp); 2883fdf676e5SLen Brown 2884fdf676e5SLen Brown return 0; 2885fdf676e5SLen Brown } 2886fdf676e5SLen Brown /* 288727d47356SLen Brown * snapshot_gfx_mhz() 288827d47356SLen Brown * 288927d47356SLen Brown * record snapshot of 289027d47356SLen Brown * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz 289127d47356SLen Brown * 289227d47356SLen Brown * return 1 if config change requires a restart, else return 0 289327d47356SLen Brown */ 289427d47356SLen Brown int snapshot_gfx_mhz(void) 289527d47356SLen Brown { 289627d47356SLen Brown static FILE *fp; 289727d47356SLen Brown int retval; 289827d47356SLen Brown 289927d47356SLen Brown if (fp == NULL) 290027d47356SLen Brown fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r"); 290122048c54SLen Brown else { 290227d47356SLen Brown rewind(fp); 290322048c54SLen Brown fflush(fp); 290422048c54SLen Brown } 290527d47356SLen Brown 290627d47356SLen Brown retval = fscanf(fp, "%d", &gfx_cur_mhz); 290727d47356SLen Brown if (retval != 1) 290827d47356SLen Brown err(1, "GFX MHz"); 290927d47356SLen Brown 291027d47356SLen Brown return 0; 291127d47356SLen Brown } 2912562a2d37SLen Brown 2913562a2d37SLen Brown /* 2914be0e54c4SLen Brown * snapshot_cpu_lpi() 2915be0e54c4SLen Brown * 2916be0e54c4SLen Brown * record snapshot of 2917be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us 2918be0e54c4SLen Brown * 2919be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2920be0e54c4SLen Brown */ 2921be0e54c4SLen Brown int snapshot_cpu_lpi_us(void) 2922be0e54c4SLen Brown { 2923be0e54c4SLen Brown FILE *fp; 2924be0e54c4SLen Brown int retval; 2925be0e54c4SLen Brown 2926be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r"); 2927be0e54c4SLen Brown 2928be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us); 29295ea7647bSPrarit Bhargava if (retval != 1) { 29305ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle CPU output\n"); 29315ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_CPU_LPI); 2932605736c6SGustavo A. R. Silva fclose(fp); 29335ea7647bSPrarit Bhargava return -1; 29345ea7647bSPrarit Bhargava } 2935be0e54c4SLen Brown 2936be0e54c4SLen Brown fclose(fp); 2937be0e54c4SLen Brown 2938be0e54c4SLen Brown return 0; 2939be0e54c4SLen Brown } 2940be0e54c4SLen Brown /* 2941be0e54c4SLen Brown * snapshot_sys_lpi() 2942be0e54c4SLen Brown * 2943be0e54c4SLen Brown * record snapshot of 2944be0e54c4SLen Brown * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us 2945be0e54c4SLen Brown * 2946be0e54c4SLen Brown * return 1 if config change requires a restart, else return 0 2947be0e54c4SLen Brown */ 2948be0e54c4SLen Brown int snapshot_sys_lpi_us(void) 2949be0e54c4SLen Brown { 2950be0e54c4SLen Brown FILE *fp; 2951be0e54c4SLen Brown int retval; 2952be0e54c4SLen Brown 2953be0e54c4SLen Brown fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r"); 2954be0e54c4SLen Brown 2955be0e54c4SLen Brown retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us); 29565ea7647bSPrarit Bhargava if (retval != 1) { 29575ea7647bSPrarit Bhargava fprintf(stderr, "Disabling Low Power Idle System output\n"); 29585ea7647bSPrarit Bhargava BIC_NOT_PRESENT(BIC_SYS_LPI); 295915423b95SColin Ian King fclose(fp); 29605ea7647bSPrarit Bhargava return -1; 29615ea7647bSPrarit Bhargava } 2962be0e54c4SLen Brown fclose(fp); 2963be0e54c4SLen Brown 2964be0e54c4SLen Brown return 0; 2965be0e54c4SLen Brown } 2966be0e54c4SLen Brown /* 2967562a2d37SLen Brown * snapshot /proc and /sys files 2968562a2d37SLen Brown * 2969562a2d37SLen Brown * return 1 if configuration restart needed, else return 0 2970562a2d37SLen Brown */ 2971562a2d37SLen Brown int snapshot_proc_sysfs_files(void) 2972562a2d37SLen Brown { 2973218f0e8dSLen Brown if (DO_BIC(BIC_IRQ)) 2974562a2d37SLen Brown if (snapshot_proc_interrupts()) 2975562a2d37SLen Brown return 1; 2976562a2d37SLen Brown 2977812db3f7SLen Brown if (DO_BIC(BIC_GFX_rc6)) 2978fdf676e5SLen Brown snapshot_gfx_rc6_ms(); 2979fdf676e5SLen Brown 2980812db3f7SLen Brown if (DO_BIC(BIC_GFXMHz)) 298127d47356SLen Brown snapshot_gfx_mhz(); 298227d47356SLen Brown 2983be0e54c4SLen Brown if (DO_BIC(BIC_CPU_LPI)) 2984be0e54c4SLen Brown snapshot_cpu_lpi_us(); 2985be0e54c4SLen Brown 2986be0e54c4SLen Brown if (DO_BIC(BIC_SYS_LPI)) 2987be0e54c4SLen Brown snapshot_sys_lpi_us(); 2988be0e54c4SLen Brown 2989562a2d37SLen Brown return 0; 2990562a2d37SLen Brown } 2991562a2d37SLen Brown 29928aa2ed0bSLen Brown int exit_requested; 29938aa2ed0bSLen Brown 29948aa2ed0bSLen Brown static void signal_handler (int signal) 29958aa2ed0bSLen Brown { 29968aa2ed0bSLen Brown switch (signal) { 29978aa2ed0bSLen Brown case SIGINT: 29988aa2ed0bSLen Brown exit_requested = 1; 29998aa2ed0bSLen Brown if (debug) 30008aa2ed0bSLen Brown fprintf(stderr, " SIGINT\n"); 30018aa2ed0bSLen Brown break; 300207211960SLen Brown case SIGUSR1: 300307211960SLen Brown if (debug > 1) 300407211960SLen Brown fprintf(stderr, "SIGUSR1\n"); 300507211960SLen Brown break; 30068aa2ed0bSLen Brown } 30078aa2ed0bSLen Brown } 30088aa2ed0bSLen Brown 30098aa2ed0bSLen Brown void setup_signal_handler(void) 30108aa2ed0bSLen Brown { 30118aa2ed0bSLen Brown struct sigaction sa; 30128aa2ed0bSLen Brown 30138aa2ed0bSLen Brown memset(&sa, 0, sizeof(sa)); 30148aa2ed0bSLen Brown 30158aa2ed0bSLen Brown sa.sa_handler = &signal_handler; 30168aa2ed0bSLen Brown 30178aa2ed0bSLen Brown if (sigaction(SIGINT, &sa, NULL) < 0) 30188aa2ed0bSLen Brown err(1, "sigaction SIGINT"); 301907211960SLen Brown if (sigaction(SIGUSR1, &sa, NULL) < 0) 302007211960SLen Brown err(1, "sigaction SIGUSR1"); 30218aa2ed0bSLen Brown } 3022b9ad8ee0SLen Brown 302347936f94SArtem Bityutskiy void do_sleep(void) 3024b9ad8ee0SLen Brown { 3025c026c236SArtem Bityutskiy struct timeval tout; 3026c026c236SArtem Bityutskiy struct timespec rest; 3027b9ad8ee0SLen Brown fd_set readfds; 3028b9ad8ee0SLen Brown int retval; 3029b9ad8ee0SLen Brown 3030b9ad8ee0SLen Brown FD_ZERO(&readfds); 3031b9ad8ee0SLen Brown FD_SET(0, &readfds); 3032b9ad8ee0SLen Brown 3033c026c236SArtem Bityutskiy if (ignore_stdin) { 303447936f94SArtem Bityutskiy nanosleep(&interval_ts, NULL); 303547936f94SArtem Bityutskiy return; 303647936f94SArtem Bityutskiy } 3037b9ad8ee0SLen Brown 3038c026c236SArtem Bityutskiy tout = interval_tv; 3039c026c236SArtem Bityutskiy retval = select(1, &readfds, NULL, NULL, &tout); 3040b9ad8ee0SLen Brown 3041b9ad8ee0SLen Brown if (retval == 1) { 3042b9ad8ee0SLen Brown switch (getc(stdin)) { 3043b9ad8ee0SLen Brown case 'q': 3044b9ad8ee0SLen Brown exit_requested = 1; 3045b9ad8ee0SLen Brown break; 3046c026c236SArtem Bityutskiy case EOF: 3047c026c236SArtem Bityutskiy /* 3048c026c236SArtem Bityutskiy * 'stdin' is a pipe closed on the other end. There 3049c026c236SArtem Bityutskiy * won't be any further input. 3050c026c236SArtem Bityutskiy */ 3051c026c236SArtem Bityutskiy ignore_stdin = 1; 3052c026c236SArtem Bityutskiy /* Sleep the rest of the time */ 3053c026c236SArtem Bityutskiy rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000); 3054c026c236SArtem Bityutskiy rest.tv_nsec = (tout.tv_usec % 1000000) * 1000; 3055c026c236SArtem Bityutskiy nanosleep(&rest, NULL); 3056b9ad8ee0SLen Brown } 3057b9ad8ee0SLen Brown } 3058b9ad8ee0SLen Brown } 305947936f94SArtem Bityutskiy 30604c2122d4SLen Brown 3061103a8feaSLen Brown void turbostat_loop() 3062103a8feaSLen Brown { 3063c98d5d94SLen Brown int retval; 3064e52966c0SLen Brown int restarted = 0; 3065023fe0acSChen Yu int done_iters = 0; 3066c98d5d94SLen Brown 30678aa2ed0bSLen Brown setup_signal_handler(); 30688aa2ed0bSLen Brown 3069103a8feaSLen Brown restart: 3070e52966c0SLen Brown restarted++; 3071e52966c0SLen Brown 3072562a2d37SLen Brown snapshot_proc_sysfs_files(); 3073c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 30744c2122d4SLen Brown first_counter_read = 0; 3075d91bb17cSLen Brown if (retval < -1) { 3076d91bb17cSLen Brown exit(retval); 3077d91bb17cSLen Brown } else if (retval == -1) { 3078e52966c0SLen Brown if (restarted > 1) { 3079e52966c0SLen Brown exit(retval); 3080e52966c0SLen Brown } 3081c98d5d94SLen Brown re_initialize(); 3082c98d5d94SLen Brown goto restart; 3083c98d5d94SLen Brown } 3084e52966c0SLen Brown restarted = 0; 3085023fe0acSChen Yu done_iters = 0; 3086103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3087103a8feaSLen Brown 3088103a8feaSLen Brown while (1) { 3089c98d5d94SLen Brown if (for_all_proc_cpus(cpu_is_not_present)) { 3090103a8feaSLen Brown re_initialize(); 3091103a8feaSLen Brown goto restart; 3092103a8feaSLen Brown } 3093b9ad8ee0SLen Brown do_sleep(); 3094562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3095562a2d37SLen Brown goto restart; 3096c98d5d94SLen Brown retval = for_all_cpus(get_counters, ODD_COUNTERS); 3097d91bb17cSLen Brown if (retval < -1) { 3098d91bb17cSLen Brown exit(retval); 3099d91bb17cSLen Brown } else if (retval == -1) { 310015aaa346SLen Brown re_initialize(); 310115aaa346SLen Brown goto restart; 310215aaa346SLen Brown } 3103103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 3104103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 3105ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) { 3106ba3dec99SLen Brown re_initialize(); 3107ba3dec99SLen Brown goto restart; 3108ba3dec99SLen Brown } 3109c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 3110c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 3111b7d8c148SLen Brown flush_output_stdout(); 31128aa2ed0bSLen Brown if (exit_requested) 31138aa2ed0bSLen Brown break; 3114023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3115023fe0acSChen Yu break; 3116b9ad8ee0SLen Brown do_sleep(); 3117562a2d37SLen Brown if (snapshot_proc_sysfs_files()) 3118562a2d37SLen Brown goto restart; 3119c98d5d94SLen Brown retval = for_all_cpus(get_counters, EVEN_COUNTERS); 3120d91bb17cSLen Brown if (retval < -1) { 3121d91bb17cSLen Brown exit(retval); 3122d91bb17cSLen Brown } else if (retval == -1) { 3123103a8feaSLen Brown re_initialize(); 3124103a8feaSLen Brown goto restart; 3125103a8feaSLen Brown } 3126103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 3127103a8feaSLen Brown timersub(&tv_even, &tv_odd, &tv_delta); 3128ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) { 3129ba3dec99SLen Brown re_initialize(); 3130ba3dec99SLen Brown goto restart; 3131ba3dec99SLen Brown } 3132c98d5d94SLen Brown compute_average(ODD_COUNTERS); 3133c98d5d94SLen Brown format_all_counters(ODD_COUNTERS); 3134b7d8c148SLen Brown flush_output_stdout(); 31358aa2ed0bSLen Brown if (exit_requested) 31368aa2ed0bSLen Brown break; 3137023fe0acSChen Yu if (num_iterations && ++done_iters >= num_iterations) 3138023fe0acSChen Yu break; 3139103a8feaSLen Brown } 3140103a8feaSLen Brown } 3141103a8feaSLen Brown 3142103a8feaSLen Brown void check_dev_msr() 3143103a8feaSLen Brown { 3144103a8feaSLen Brown struct stat sb; 31457ce7d5deSPrarit Bhargava char pathname[32]; 3146103a8feaSLen Brown 31477ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 31487ce7d5deSPrarit Bhargava if (stat(pathname, &sb)) 3149a21d38c8SLen Brown if (system("/sbin/modprobe msr > /dev/null 2>&1")) 3150d7899447SLen Brown err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" "); 3151103a8feaSLen Brown } 3152103a8feaSLen Brown 315398481e79SLen Brown void check_permissions() 3154103a8feaSLen Brown { 315598481e79SLen Brown struct __user_cap_header_struct cap_header_data; 315698481e79SLen Brown cap_user_header_t cap_header = &cap_header_data; 315798481e79SLen Brown struct __user_cap_data_struct cap_data_data; 315898481e79SLen Brown cap_user_data_t cap_data = &cap_data_data; 315998481e79SLen Brown extern int capget(cap_user_header_t hdrp, cap_user_data_t datap); 316098481e79SLen Brown int do_exit = 0; 31617ce7d5deSPrarit Bhargava char pathname[32]; 316298481e79SLen Brown 316398481e79SLen Brown /* check for CAP_SYS_RAWIO */ 316498481e79SLen Brown cap_header->pid = getpid(); 316598481e79SLen Brown cap_header->version = _LINUX_CAPABILITY_VERSION; 316698481e79SLen Brown if (capget(cap_header, cap_data) < 0) 316798481e79SLen Brown err(-6, "capget(2) failed"); 316898481e79SLen Brown 316998481e79SLen Brown if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) { 317098481e79SLen Brown do_exit++; 317198481e79SLen Brown warnx("capget(CAP_SYS_RAWIO) failed," 317298481e79SLen Brown " try \"# setcap cap_sys_rawio=ep %s\"", progname); 317398481e79SLen Brown } 317498481e79SLen Brown 317598481e79SLen Brown /* test file permissions */ 31767ce7d5deSPrarit Bhargava sprintf(pathname, "/dev/cpu/%d/msr", base_cpu); 31777ce7d5deSPrarit Bhargava if (euidaccess(pathname, R_OK)) { 317898481e79SLen Brown do_exit++; 317998481e79SLen Brown warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr"); 318098481e79SLen Brown } 318198481e79SLen Brown 318298481e79SLen Brown /* if all else fails, thell them to be root */ 318398481e79SLen Brown if (do_exit) 3184b2c95d90SJosh Triplett if (getuid() != 0) 3185d7899447SLen Brown warnx("... or simply run as root"); 318698481e79SLen Brown 318798481e79SLen Brown if (do_exit) 318898481e79SLen Brown exit(-6); 3189103a8feaSLen Brown } 3190103a8feaSLen Brown 3191d7899447SLen Brown /* 3192d7899447SLen Brown * NHM adds support for additional MSRs: 3193d7899447SLen Brown * 3194d7899447SLen Brown * MSR_SMI_COUNT 0x00000034 3195d7899447SLen Brown * 3196ec0adc53SLen Brown * MSR_PLATFORM_INFO 0x000000ce 31971df2e55aSLen Brown * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 3198d7899447SLen Brown * 3199cf4cbe53SLen Brown * MSR_MISC_PWR_MGMT 0x000001aa 3200cf4cbe53SLen Brown * 3201d7899447SLen Brown * MSR_PKG_C3_RESIDENCY 0x000003f8 3202d7899447SLen Brown * MSR_PKG_C6_RESIDENCY 0x000003f9 3203d7899447SLen Brown * MSR_CORE_C3_RESIDENCY 0x000003fc 3204d7899447SLen Brown * MSR_CORE_C6_RESIDENCY 0x000003fd 3205d7899447SLen Brown * 3206ee7e38e3SLen Brown * Side effect: 32071df2e55aSLen Brown * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL 320833148d67SLen Brown * sets has_misc_feature_control 3209d7899447SLen Brown */ 3210ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model) 3211103a8feaSLen Brown { 3212ee7e38e3SLen Brown unsigned long long msr; 321321ed5574SLen Brown unsigned int base_ratio; 3214ee7e38e3SLen Brown int *pkg_cstate_limits; 3215ee7e38e3SLen Brown 3216103a8feaSLen Brown if (!genuine_intel) 3217103a8feaSLen Brown return 0; 3218103a8feaSLen Brown 3219103a8feaSLen Brown if (family != 6) 3220103a8feaSLen Brown return 0; 3221103a8feaSLen Brown 322221ed5574SLen Brown bclk = discover_bclk(family, model); 322321ed5574SLen Brown 3224103a8feaSLen Brown switch (model) { 3225869ce69eSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 3226869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3227ee7e38e3SLen Brown pkg_cstate_limits = nhm_pkg_cstate_limits; 3228ee7e38e3SLen Brown break; 3229869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: /* SNB */ 3230869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */ 3231869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3232869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3233ee7e38e3SLen Brown pkg_cstate_limits = snb_pkg_cstate_limits; 323433148d67SLen Brown has_misc_feature_control = 1; 3235ee7e38e3SLen Brown break; 3236869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3237869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3238cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3239869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3240869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3241869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3242869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3243869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3244997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3245ee7e38e3SLen Brown pkg_cstate_limits = hsw_pkg_cstate_limits; 324633148d67SLen Brown has_misc_feature_control = 1; 3247ee7e38e3SLen Brown break; 3248d8ebb442SLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3249d8ebb442SLen Brown pkg_cstate_limits = skx_pkg_cstate_limits; 325033148d67SLen Brown has_misc_feature_control = 1; 3251d8ebb442SLen Brown break; 3252f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3253cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3254f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 3255ee7e38e3SLen Brown pkg_cstate_limits = slv_pkg_cstate_limits; 3256ee7e38e3SLen Brown break; 3257869ce69eSLen Brown case INTEL_FAM6_ATOM_AIRMONT: /* AMT */ 3258ee7e38e3SLen Brown pkg_cstate_limits = amt_pkg_cstate_limits; 3259cf4cbe53SLen Brown no_MSR_MISC_PWR_MGMT = 1; 3260ee7e38e3SLen Brown break; 3261869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI */ 3262ee7e38e3SLen Brown pkg_cstate_limits = phi_pkg_cstate_limits; 3263ee7e38e3SLen Brown break; 3264869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3265f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 3266f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 3267445640a5SLen Brown pkg_cstate_limits = glm_pkg_cstate_limits; 3268e4085d54SLen Brown break; 3269103a8feaSLen Brown default: 3270103a8feaSLen Brown return 0; 3271103a8feaSLen Brown } 32721df2e55aSLen Brown get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr); 3273e9257f5fSLen Brown pkg_cstate_limit = pkg_cstate_limits[msr & 0xF]; 3274ee7e38e3SLen Brown 3275ec0adc53SLen Brown get_msr(base_cpu, MSR_PLATFORM_INFO, &msr); 327621ed5574SLen Brown base_ratio = (msr >> 8) & 0xFF; 327721ed5574SLen Brown 327821ed5574SLen Brown base_hz = base_ratio * bclk * 1000000; 327921ed5574SLen Brown has_base_hz = 1; 3280ee7e38e3SLen Brown return 1; 3281103a8feaSLen Brown } 32820f7887c4SLen Brown /* 3283495c7654SLen Brown * SLV client has support for unique MSRs: 32840f7887c4SLen Brown * 32850f7887c4SLen Brown * MSR_CC6_DEMOTION_POLICY_CONFIG 32860f7887c4SLen Brown * MSR_MC6_DEMOTION_POLICY_CONFIG 32870f7887c4SLen Brown */ 32880f7887c4SLen Brown 32890f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model) 32900f7887c4SLen Brown { 32910f7887c4SLen Brown if (!genuine_intel) 32920f7887c4SLen Brown return 0; 32930f7887c4SLen Brown 32940f7887c4SLen Brown switch (model) { 3295f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3296f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_MID: 3297f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_AIRMONT_MID: 32980f7887c4SLen Brown return 1; 32990f7887c4SLen Brown } 33000f7887c4SLen Brown return 0; 33010f7887c4SLen Brown } 33027170a374SLen Brown int is_dnv(unsigned int family, unsigned int model) 33037170a374SLen Brown { 33047170a374SLen Brown 33057170a374SLen Brown if (!genuine_intel) 33067170a374SLen Brown return 0; 33077170a374SLen Brown 33087170a374SLen Brown switch (model) { 3309f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: 33107170a374SLen Brown return 1; 33117170a374SLen Brown } 33127170a374SLen Brown return 0; 33137170a374SLen Brown } 3314ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model) 3315ade0ebacSLen Brown { 3316ade0ebacSLen Brown 3317ade0ebacSLen Brown if (!genuine_intel) 3318ade0ebacSLen Brown return 0; 3319ade0ebacSLen Brown 3320ade0ebacSLen Brown switch (model) { 3321ade0ebacSLen Brown case INTEL_FAM6_BROADWELL_X: 3322ade0ebacSLen Brown return 1; 3323ade0ebacSLen Brown } 3324ade0ebacSLen Brown return 0; 3325ade0ebacSLen Brown } 332634c76197SLen Brown int is_skx(unsigned int family, unsigned int model) 332734c76197SLen Brown { 332834c76197SLen Brown 332934c76197SLen Brown if (!genuine_intel) 333034c76197SLen Brown return 0; 333134c76197SLen Brown 333234c76197SLen Brown switch (model) { 333334c76197SLen Brown case INTEL_FAM6_SKYLAKE_X: 333434c76197SLen Brown return 1; 333534c76197SLen Brown } 333634c76197SLen Brown return 0; 333734c76197SLen Brown } 33380f7887c4SLen Brown 333931e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model) 3340d7899447SLen Brown { 33410f7887c4SLen Brown if (has_slv_msrs(family, model)) 33420f7887c4SLen Brown return 0; 33430f7887c4SLen Brown 3344d7899447SLen Brown switch (model) { 3345d7899447SLen Brown /* Nehalem compatible, but do not include turbo-ratio limit support */ 3346869ce69eSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 3347869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */ 3348d7899447SLen Brown return 0; 3349d7899447SLen Brown default: 3350d7899447SLen Brown return 1; 3351d7899447SLen Brown } 3352d7899447SLen Brown } 33530f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model) 33540f7887c4SLen Brown { 33550f7887c4SLen Brown if (has_slv_msrs(family, model)) 33560f7887c4SLen Brown return 1; 33570f7887c4SLen Brown 33580f7887c4SLen Brown return 0; 33590f7887c4SLen Brown } 33606574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model) 33616574a5d5SLen Brown { 33626574a5d5SLen Brown if (!genuine_intel) 33636574a5d5SLen Brown return 0; 33646574a5d5SLen Brown 33656574a5d5SLen Brown if (family != 6) 33666574a5d5SLen Brown return 0; 33676574a5d5SLen Brown 33686574a5d5SLen Brown switch (model) { 3369869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 3370869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 33716574a5d5SLen Brown return 1; 33726574a5d5SLen Brown default: 33736574a5d5SLen Brown return 0; 33746574a5d5SLen Brown } 33756574a5d5SLen Brown } 3376fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model) 3377fcd17211SLen Brown { 3378fcd17211SLen Brown if (!genuine_intel) 3379fcd17211SLen Brown return 0; 3380fcd17211SLen Brown 3381fcd17211SLen Brown if (family != 6) 3382fcd17211SLen Brown return 0; 3383fcd17211SLen Brown 3384fcd17211SLen Brown switch (model) { 3385869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW Xeon */ 3386fcd17211SLen Brown return 1; 3387fcd17211SLen Brown default: 3388fcd17211SLen Brown return 0; 3389fcd17211SLen Brown } 3390fcd17211SLen Brown } 3391fcd17211SLen Brown 3392fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model) 3393fb5d4327SDasaratharaman Chandramouli { 3394fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 3395fb5d4327SDasaratharaman Chandramouli return 0; 3396fb5d4327SDasaratharaman Chandramouli 3397fb5d4327SDasaratharaman Chandramouli if (family != 6) 3398fb5d4327SDasaratharaman Chandramouli return 0; 3399fb5d4327SDasaratharaman Chandramouli 3400fb5d4327SDasaratharaman Chandramouli switch (model) { 3401869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 3402fb5d4327SDasaratharaman Chandramouli return 1; 3403fb5d4327SDasaratharaman Chandramouli default: 3404fb5d4327SDasaratharaman Chandramouli return 0; 3405fb5d4327SDasaratharaman Chandramouli } 3406fb5d4327SDasaratharaman Chandramouli } 340731e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model) 340831e07522SLen Brown { 340931e07522SLen Brown if (!genuine_intel) 341031e07522SLen Brown return 0; 341131e07522SLen Brown 341231e07522SLen Brown if (family != 6) 341331e07522SLen Brown return 0; 341431e07522SLen Brown 341531e07522SLen Brown switch (model) { 341631e07522SLen Brown case INTEL_FAM6_ATOM_GOLDMONT: 341731e07522SLen Brown case INTEL_FAM6_SKYLAKE_X: 341831e07522SLen Brown return 1; 341931e07522SLen Brown default: 342031e07522SLen Brown return 0; 342131e07522SLen Brown } 342231e07522SLen Brown } 34236fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model) 34246fb3143bSLen Brown { 34256fb3143bSLen Brown if (!genuine_intel) 34266fb3143bSLen Brown return 0; 34276fb3143bSLen Brown 34286fb3143bSLen Brown if (family != 6) 34296fb3143bSLen Brown return 0; 34306fb3143bSLen Brown 34316fb3143bSLen Brown switch (model) { 3432869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 3433869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3434869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3435cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3436869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3437869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3438869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3439869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3440869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3441997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 3442869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 34436fb3143bSLen Brown 3444869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */ 34456fb3143bSLen Brown return 1; 34466fb3143bSLen Brown default: 34476fb3143bSLen Brown return 0; 34486fb3143bSLen Brown } 34496fb3143bSLen Brown } 34506fb3143bSLen Brown 3451fcd17211SLen Brown static void 34521b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model) 3453fcd17211SLen Brown { 3454fcd17211SLen Brown if (!do_nhm_platform_info) 3455fcd17211SLen Brown return; 3456fcd17211SLen Brown 3457fcd17211SLen Brown dump_nhm_platform_info(); 3458fcd17211SLen Brown 3459fcd17211SLen Brown if (has_hsw_turbo_ratio_limit(family, model)) 3460fcd17211SLen Brown dump_hsw_turbo_ratio_limits(); 3461fcd17211SLen Brown 3462fcd17211SLen Brown if (has_ivt_turbo_ratio_limit(family, model)) 3463fcd17211SLen Brown dump_ivt_turbo_ratio_limits(); 3464fcd17211SLen Brown 346531e07522SLen Brown if (has_turbo_ratio_limit(family, model)) 346631e07522SLen Brown dump_turbo_ratio_limits(family, model); 3467fcd17211SLen Brown 34680f7887c4SLen Brown if (has_atom_turbo_ratio_limit(family, model)) 34690f7887c4SLen Brown dump_atom_turbo_ratio_limits(); 34700f7887c4SLen Brown 3471fb5d4327SDasaratharaman Chandramouli if (has_knl_turbo_ratio_limit(family, model)) 3472fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(); 3473fb5d4327SDasaratharaman Chandramouli 34746fb3143bSLen Brown if (has_config_tdp(family, model)) 34756fb3143bSLen Brown dump_config_tdp(); 34766fb3143bSLen Brown 3477fcd17211SLen Brown dump_nhm_cst_cfg(); 3478fcd17211SLen Brown } 3479fcd17211SLen Brown 348041618e63SLen Brown static void 348141618e63SLen Brown dump_sysfs_cstate_config(void) 348241618e63SLen Brown { 348341618e63SLen Brown char path[64]; 348441618e63SLen Brown char name_buf[16]; 348541618e63SLen Brown char desc[64]; 348641618e63SLen Brown FILE *input; 348741618e63SLen Brown int state; 348841618e63SLen Brown char *sp; 348941618e63SLen Brown 349041618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 349141618e63SLen Brown return; 349241618e63SLen Brown 349341618e63SLen Brown for (state = 0; state < 10; ++state) { 349441618e63SLen Brown 349541618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 349641618e63SLen Brown base_cpu, state); 349741618e63SLen Brown input = fopen(path, "r"); 349841618e63SLen Brown if (input == NULL) 349941618e63SLen Brown continue; 35008173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 35018173c336SBen Hutchings err(1, "%s: failed to read file", path); 350241618e63SLen Brown 350341618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 350441618e63SLen Brown sp = strchr(name_buf, '-'); 350541618e63SLen Brown if (!sp) 350641618e63SLen Brown sp = strchrnul(name_buf, '\n'); 350741618e63SLen Brown *sp = '\0'; 350841618e63SLen Brown fclose(input); 350941618e63SLen Brown 351041618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", 351141618e63SLen Brown base_cpu, state); 351241618e63SLen Brown input = fopen(path, "r"); 351341618e63SLen Brown if (input == NULL) 351441618e63SLen Brown continue; 35158173c336SBen Hutchings if (!fgets(desc, sizeof(desc), input)) 35168173c336SBen Hutchings err(1, "%s: failed to read file", path); 351741618e63SLen Brown 351841618e63SLen Brown fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); 351941618e63SLen Brown fclose(input); 352041618e63SLen Brown } 352141618e63SLen Brown } 35227293fccdSLen Brown static void 35237293fccdSLen Brown dump_sysfs_pstate_config(void) 35247293fccdSLen Brown { 35257293fccdSLen Brown char path[64]; 35267293fccdSLen Brown char driver_buf[64]; 35277293fccdSLen Brown char governor_buf[64]; 35287293fccdSLen Brown FILE *input; 35297293fccdSLen Brown int turbo; 35307293fccdSLen Brown 35317293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", 35327293fccdSLen Brown base_cpu); 35337293fccdSLen Brown input = fopen(path, "r"); 35347293fccdSLen Brown if (input == NULL) { 35350a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 35367293fccdSLen Brown return; 35377293fccdSLen Brown } 35388173c336SBen Hutchings if (!fgets(driver_buf, sizeof(driver_buf), input)) 35398173c336SBen Hutchings err(1, "%s: failed to read file", path); 35407293fccdSLen Brown fclose(input); 35417293fccdSLen Brown 35427293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", 35437293fccdSLen Brown base_cpu); 35447293fccdSLen Brown input = fopen(path, "r"); 35457293fccdSLen Brown if (input == NULL) { 35460a42d235SPrarit Bhargava fprintf(outf, "NSFOD %s\n", path); 35477293fccdSLen Brown return; 35487293fccdSLen Brown } 35498173c336SBen Hutchings if (!fgets(governor_buf, sizeof(governor_buf), input)) 35508173c336SBen Hutchings err(1, "%s: failed to read file", path); 35517293fccdSLen Brown fclose(input); 35527293fccdSLen Brown 35537293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); 35547293fccdSLen Brown fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf); 35557293fccdSLen Brown 35567293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); 35577293fccdSLen Brown input = fopen(path, "r"); 35587293fccdSLen Brown if (input != NULL) { 35598173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 35608173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 35617293fccdSLen Brown fprintf(outf, "cpufreq boost: %d\n", turbo); 35627293fccdSLen Brown fclose(input); 35637293fccdSLen Brown } 35647293fccdSLen Brown 35657293fccdSLen Brown sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); 35667293fccdSLen Brown input = fopen(path, "r"); 35677293fccdSLen Brown if (input != NULL) { 35688173c336SBen Hutchings if (fscanf(input, "%d", &turbo) != 1) 35698173c336SBen Hutchings err(1, "%s: failed to parse number from file", path); 35707293fccdSLen Brown fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); 35717293fccdSLen Brown fclose(input); 35727293fccdSLen Brown } 35737293fccdSLen Brown } 357441618e63SLen Brown 35756574a5d5SLen Brown 3576889facbeSLen Brown /* 3577889facbeSLen Brown * print_epb() 3578889facbeSLen Brown * Decode the ENERGY_PERF_BIAS MSR 3579889facbeSLen Brown */ 3580889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) 3581889facbeSLen Brown { 3582889facbeSLen Brown unsigned long long msr; 3583889facbeSLen Brown char *epb_string; 3584889facbeSLen Brown int cpu; 3585889facbeSLen Brown 3586889facbeSLen Brown if (!has_epb) 3587889facbeSLen Brown return 0; 3588889facbeSLen Brown 3589889facbeSLen Brown cpu = t->cpu_id; 3590889facbeSLen Brown 3591889facbeSLen Brown /* EPB is per-package */ 3592889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 3593889facbeSLen Brown return 0; 3594889facbeSLen Brown 3595889facbeSLen Brown if (cpu_migrate(cpu)) { 3596b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 3597889facbeSLen Brown return -1; 3598889facbeSLen Brown } 3599889facbeSLen Brown 3600889facbeSLen Brown if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr)) 3601889facbeSLen Brown return 0; 3602889facbeSLen Brown 3603e9be7dd6SLen Brown switch (msr & 0xF) { 3604889facbeSLen Brown case ENERGY_PERF_BIAS_PERFORMANCE: 3605889facbeSLen Brown epb_string = "performance"; 3606889facbeSLen Brown break; 3607889facbeSLen Brown case ENERGY_PERF_BIAS_NORMAL: 3608889facbeSLen Brown epb_string = "balanced"; 3609889facbeSLen Brown break; 3610889facbeSLen Brown case ENERGY_PERF_BIAS_POWERSAVE: 3611889facbeSLen Brown epb_string = "powersave"; 3612889facbeSLen Brown break; 3613889facbeSLen Brown default: 3614889facbeSLen Brown epb_string = "custom"; 3615889facbeSLen Brown break; 3616889facbeSLen Brown } 3617b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string); 3618889facbeSLen Brown 3619889facbeSLen Brown return 0; 3620889facbeSLen Brown } 36217f5c258eSLen Brown /* 36227f5c258eSLen Brown * print_hwp() 36237f5c258eSLen Brown * Decode the MSR_HWP_CAPABILITIES 36247f5c258eSLen Brown */ 36257f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p) 36267f5c258eSLen Brown { 36277f5c258eSLen Brown unsigned long long msr; 36287f5c258eSLen Brown int cpu; 36297f5c258eSLen Brown 36307f5c258eSLen Brown if (!has_hwp) 36317f5c258eSLen Brown return 0; 36327f5c258eSLen Brown 36337f5c258eSLen Brown cpu = t->cpu_id; 36347f5c258eSLen Brown 36357f5c258eSLen Brown /* MSR_HWP_CAPABILITIES is per-package */ 36367f5c258eSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 36377f5c258eSLen Brown return 0; 36387f5c258eSLen Brown 36397f5c258eSLen Brown if (cpu_migrate(cpu)) { 3640b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 36417f5c258eSLen Brown return -1; 36427f5c258eSLen Brown } 36437f5c258eSLen Brown 36447f5c258eSLen Brown if (get_msr(cpu, MSR_PM_ENABLE, &msr)) 36457f5c258eSLen Brown return 0; 36467f5c258eSLen Brown 3647b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", 36487f5c258eSLen Brown cpu, msr, (msr & (1 << 0)) ? "" : "No-"); 36497f5c258eSLen Brown 36507f5c258eSLen Brown /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */ 36517f5c258eSLen Brown if ((msr & (1 << 0)) == 0) 36527f5c258eSLen Brown return 0; 36537f5c258eSLen Brown 36547f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr)) 36557f5c258eSLen Brown return 0; 36567f5c258eSLen Brown 3657b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx " 36586dbd25a2SLen Brown "(high %d guar %d eff %d low %d)\n", 36597f5c258eSLen Brown cpu, msr, 36607f5c258eSLen Brown (unsigned int)HWP_HIGHEST_PERF(msr), 36617f5c258eSLen Brown (unsigned int)HWP_GUARANTEED_PERF(msr), 36627f5c258eSLen Brown (unsigned int)HWP_MOSTEFFICIENT_PERF(msr), 36637f5c258eSLen Brown (unsigned int)HWP_LOWEST_PERF(msr)); 36647f5c258eSLen Brown 36657f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST, &msr)) 36667f5c258eSLen Brown return 0; 36677f5c258eSLen Brown 3668b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx " 36696dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n", 36707f5c258eSLen Brown cpu, msr, 36717f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 36727f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 36737f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 36747f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 36757f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3), 36767f5c258eSLen Brown (unsigned int)(((msr) >> 42) & 0x1)); 36777f5c258eSLen Brown 36787f5c258eSLen Brown if (has_hwp_pkg) { 36797f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr)) 36807f5c258eSLen Brown return 0; 36817f5c258eSLen Brown 3682b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx " 36836dbd25a2SLen Brown "(min %d max %d des %d epp 0x%x window 0x%x)\n", 36847f5c258eSLen Brown cpu, msr, 36857f5c258eSLen Brown (unsigned int)(((msr) >> 0) & 0xff), 36867f5c258eSLen Brown (unsigned int)(((msr) >> 8) & 0xff), 36877f5c258eSLen Brown (unsigned int)(((msr) >> 16) & 0xff), 36887f5c258eSLen Brown (unsigned int)(((msr) >> 24) & 0xff), 36897f5c258eSLen Brown (unsigned int)(((msr) >> 32) & 0xff3)); 36907f5c258eSLen Brown } 36917f5c258eSLen Brown if (has_hwp_notify) { 36927f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr)) 36937f5c258eSLen Brown return 0; 36947f5c258eSLen Brown 3695b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx " 36967f5c258eSLen Brown "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n", 36977f5c258eSLen Brown cpu, msr, 36987f5c258eSLen Brown ((msr) & 0x1) ? "EN" : "Dis", 36997f5c258eSLen Brown ((msr) & 0x2) ? "EN" : "Dis"); 37007f5c258eSLen Brown } 37017f5c258eSLen Brown if (get_msr(cpu, MSR_HWP_STATUS, &msr)) 37027f5c258eSLen Brown return 0; 37037f5c258eSLen Brown 3704b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx " 37057f5c258eSLen Brown "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n", 37067f5c258eSLen Brown cpu, msr, 37077f5c258eSLen Brown ((msr) & 0x1) ? "" : "No-", 37087f5c258eSLen Brown ((msr) & 0x2) ? "" : "No-"); 3709889facbeSLen Brown 3710889facbeSLen Brown return 0; 3711889facbeSLen Brown } 3712889facbeSLen Brown 37133a9a941dSLen Brown /* 37143a9a941dSLen Brown * print_perf_limit() 37153a9a941dSLen Brown */ 37163a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p) 37173a9a941dSLen Brown { 37183a9a941dSLen Brown unsigned long long msr; 37193a9a941dSLen Brown int cpu; 37203a9a941dSLen Brown 37213a9a941dSLen Brown cpu = t->cpu_id; 37223a9a941dSLen Brown 37233a9a941dSLen Brown /* per-package */ 37243a9a941dSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 37253a9a941dSLen Brown return 0; 37263a9a941dSLen Brown 37273a9a941dSLen Brown if (cpu_migrate(cpu)) { 3728b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 37293a9a941dSLen Brown return -1; 37303a9a941dSLen Brown } 37313a9a941dSLen Brown 37323a9a941dSLen Brown if (do_core_perf_limit_reasons) { 37333a9a941dSLen Brown get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr); 3734b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3735b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)", 3736e33cbe85SLen Brown (msr & 1 << 15) ? "bit15, " : "", 37373a9a941dSLen Brown (msr & 1 << 14) ? "bit14, " : "", 3738e33cbe85SLen Brown (msr & 1 << 13) ? "Transitions, " : "", 3739e33cbe85SLen Brown (msr & 1 << 12) ? "MultiCoreTurbo, " : "", 3740e33cbe85SLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : "", 3741e33cbe85SLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 3742e33cbe85SLen Brown (msr & 1 << 9) ? "CorePwr, " : "", 3743e33cbe85SLen Brown (msr & 1 << 8) ? "Amps, " : "", 3744e33cbe85SLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 3745e33cbe85SLen Brown (msr & 1 << 5) ? "Auto-HWP, " : "", 3746e33cbe85SLen Brown (msr & 1 << 4) ? "Graphics, " : "", 3747e33cbe85SLen Brown (msr & 1 << 2) ? "bit2, " : "", 3748e33cbe85SLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 3749e33cbe85SLen Brown (msr & 1 << 0) ? "PROCHOT, " : ""); 3750b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n", 3751e33cbe85SLen Brown (msr & 1 << 31) ? "bit31, " : "", 37523a9a941dSLen Brown (msr & 1 << 30) ? "bit30, " : "", 3753e33cbe85SLen Brown (msr & 1 << 29) ? "Transitions, " : "", 3754e33cbe85SLen Brown (msr & 1 << 28) ? "MultiCoreTurbo, " : "", 3755e33cbe85SLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : "", 3756e33cbe85SLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 3757e33cbe85SLen Brown (msr & 1 << 25) ? "CorePwr, " : "", 3758e33cbe85SLen Brown (msr & 1 << 24) ? "Amps, " : "", 3759e33cbe85SLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 3760e33cbe85SLen Brown (msr & 1 << 21) ? "Auto-HWP, " : "", 3761e33cbe85SLen Brown (msr & 1 << 20) ? "Graphics, " : "", 3762e33cbe85SLen Brown (msr & 1 << 18) ? "bit18, " : "", 3763e33cbe85SLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 3764e33cbe85SLen Brown (msr & 1 << 16) ? "PROCHOT, " : ""); 37653a9a941dSLen Brown 37663a9a941dSLen Brown } 37673a9a941dSLen Brown if (do_gfx_perf_limit_reasons) { 37683a9a941dSLen Brown get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr); 3769b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3770b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)", 37713a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 37723a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 37733a9a941dSLen Brown (msr & 1 << 4) ? "Graphics, " : "", 37743a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 37753a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 37763a9a941dSLen Brown (msr & 1 << 9) ? "GFXPwr, " : "", 37773a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 37783a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3779b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n", 37803a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 37813a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 37823a9a941dSLen Brown (msr & 1 << 20) ? "Graphics, " : "", 37833a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 37843a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 37853a9a941dSLen Brown (msr & 1 << 25) ? "GFXPwr, " : "", 37863a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 37873a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 37883a9a941dSLen Brown } 37893a9a941dSLen Brown if (do_ring_perf_limit_reasons) { 37903a9a941dSLen Brown get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr); 3791b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr); 3792b7d8c148SLen Brown fprintf(outf, " (Active: %s%s%s%s%s%s)", 37933a9a941dSLen Brown (msr & 1 << 0) ? "PROCHOT, " : "", 37943a9a941dSLen Brown (msr & 1 << 1) ? "ThermStatus, " : "", 37953a9a941dSLen Brown (msr & 1 << 6) ? "VR-Therm, " : "", 37963a9a941dSLen Brown (msr & 1 << 8) ? "Amps, " : "", 37973a9a941dSLen Brown (msr & 1 << 10) ? "PkgPwrL1, " : "", 37983a9a941dSLen Brown (msr & 1 << 11) ? "PkgPwrL2, " : ""); 3799b7d8c148SLen Brown fprintf(outf, " (Logged: %s%s%s%s%s%s)\n", 38003a9a941dSLen Brown (msr & 1 << 16) ? "PROCHOT, " : "", 38013a9a941dSLen Brown (msr & 1 << 17) ? "ThermStatus, " : "", 38023a9a941dSLen Brown (msr & 1 << 22) ? "VR-Therm, " : "", 38033a9a941dSLen Brown (msr & 1 << 24) ? "Amps, " : "", 38043a9a941dSLen Brown (msr & 1 << 26) ? "PkgPwrL1, " : "", 38053a9a941dSLen Brown (msr & 1 << 27) ? "PkgPwrL2, " : ""); 38063a9a941dSLen Brown } 38073a9a941dSLen Brown return 0; 38083a9a941dSLen Brown } 38093a9a941dSLen Brown 3810889facbeSLen Brown #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */ 3811889facbeSLen Brown #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */ 3812889facbeSLen Brown 38139392bd98SCalvin Walton double get_tdp_intel(unsigned int model) 3814144b44b1SLen Brown { 3815144b44b1SLen Brown unsigned long long msr; 3816144b44b1SLen Brown 3817144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) 38187ce7d5deSPrarit Bhargava if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr)) 3819144b44b1SLen Brown return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units; 3820144b44b1SLen Brown 3821144b44b1SLen Brown switch (model) { 3822f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: 3823f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: 3824144b44b1SLen Brown return 30.0; 3825144b44b1SLen Brown default: 3826144b44b1SLen Brown return 135.0; 3827144b44b1SLen Brown } 3828144b44b1SLen Brown } 3829144b44b1SLen Brown 38309392bd98SCalvin Walton double get_tdp_amd(unsigned int family) 38319392bd98SCalvin Walton { 38329392bd98SCalvin Walton switch (family) { 38339392bd98SCalvin Walton case 0x17: 38349392bd98SCalvin Walton default: 38359392bd98SCalvin Walton /* This is the max stock TDP of HEDT/Server Fam17h chips */ 38369392bd98SCalvin Walton return 250.0; 38379392bd98SCalvin Walton } 38389392bd98SCalvin Walton } 38399392bd98SCalvin Walton 384040ee8e3bSAndrey Semin /* 384140ee8e3bSAndrey Semin * rapl_dram_energy_units_probe() 384240ee8e3bSAndrey Semin * Energy units are either hard-coded, or come from RAPL Energy Unit MSR. 384340ee8e3bSAndrey Semin */ 384440ee8e3bSAndrey Semin static double 384540ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int model, double rapl_energy_units) 384640ee8e3bSAndrey Semin { 384740ee8e3bSAndrey Semin /* only called for genuine_intel, family 6 */ 384840ee8e3bSAndrey Semin 384940ee8e3bSAndrey Semin switch (model) { 3850869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3851869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3852869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 385340ee8e3bSAndrey Semin return (rapl_dram_energy_units = 15.3 / 1000000); 385440ee8e3bSAndrey Semin default: 385540ee8e3bSAndrey Semin return (rapl_energy_units); 385640ee8e3bSAndrey Semin } 385740ee8e3bSAndrey Semin } 385840ee8e3bSAndrey Semin 38599392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model) 3860889facbeSLen Brown { 3861889facbeSLen Brown unsigned long long msr; 3862144b44b1SLen Brown unsigned int time_unit; 3863889facbeSLen Brown double tdp; 3864889facbeSLen Brown 3865889facbeSLen Brown if (family != 6) 3866889facbeSLen Brown return; 3867889facbeSLen Brown 3868889facbeSLen Brown switch (model) { 3869869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 3870869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: 3871869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 3872cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 3873869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 3874869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 3875869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 3876144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO; 3877812db3f7SLen Brown if (rapl_joules) { 3878812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3879812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3880812db3f7SLen Brown BIC_PRESENT(BIC_GFX_J); 3881812db3f7SLen Brown } else { 3882812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3883812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3884812db3f7SLen Brown BIC_PRESENT(BIC_GFXWatt); 3885812db3f7SLen Brown } 3886889facbeSLen Brown break; 3887869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 3888f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 3889e4085d54SLen Brown do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO; 3890812db3f7SLen Brown if (rapl_joules) 3891812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3892812db3f7SLen Brown else 3893812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3894e4085d54SLen Brown break; 3895869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 3896997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 389781824921SLen 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; 3898812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3899812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3900812db3f7SLen Brown if (rapl_joules) { 3901812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3902812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3903812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 390481824921SLen Brown BIC_PRESENT(BIC_GFX_J); 3905812db3f7SLen Brown } else { 3906812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3907812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3908812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 390981824921SLen Brown BIC_PRESENT(BIC_GFXWatt); 3910812db3f7SLen Brown } 39110b2bb692SLen Brown break; 3912869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 3913869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 3914869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 3915869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 39160b2bb692SLen Brown do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO; 3917812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3918812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3919812db3f7SLen Brown if (rapl_joules) { 3920812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3921812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3922812db3f7SLen Brown } else { 3923812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3924812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3925812db3f7SLen Brown } 3926e6f9bb3cSLen Brown break; 3927869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 3928869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: 39290b2bb692SLen 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; 3930812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3931812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3932812db3f7SLen Brown if (rapl_joules) { 3933812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3934812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3935812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3936812db3f7SLen Brown } else { 3937812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3938812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3939812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3940812db3f7SLen Brown } 3941144b44b1SLen Brown break; 3942f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 3943f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 3944144b44b1SLen Brown do_rapl = RAPL_PKG | RAPL_CORES; 3945812db3f7SLen Brown if (rapl_joules) { 3946812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3947812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3948812db3f7SLen Brown } else { 3949812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3950812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3951812db3f7SLen Brown } 3952889facbeSLen Brown break; 3953f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 39540f644909SJacob 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; 3955812db3f7SLen Brown BIC_PRESENT(BIC_PKG__); 3956812db3f7SLen Brown BIC_PRESENT(BIC_RAM__); 3957812db3f7SLen Brown if (rapl_joules) { 3958812db3f7SLen Brown BIC_PRESENT(BIC_Pkg_J); 3959812db3f7SLen Brown BIC_PRESENT(BIC_Cor_J); 3960812db3f7SLen Brown BIC_PRESENT(BIC_RAM_J); 3961812db3f7SLen Brown } else { 3962812db3f7SLen Brown BIC_PRESENT(BIC_PkgWatt); 3963812db3f7SLen Brown BIC_PRESENT(BIC_CorWatt); 3964812db3f7SLen Brown BIC_PRESENT(BIC_RAMWatt); 3965812db3f7SLen Brown } 39660f644909SJacob Pan break; 3967889facbeSLen Brown default: 3968889facbeSLen Brown return; 3969889facbeSLen Brown } 3970889facbeSLen Brown 3971889facbeSLen Brown /* units on package 0, verify later other packages match */ 39727ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr)) 3973889facbeSLen Brown return; 3974889facbeSLen Brown 3975889facbeSLen Brown rapl_power_units = 1.0 / (1 << (msr & 0xF)); 3976f2c4db1bSPeter Zijlstra if (model == INTEL_FAM6_ATOM_SILVERMONT) 3977144b44b1SLen Brown rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000; 3978144b44b1SLen Brown else 3979889facbeSLen Brown rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F)); 3980889facbeSLen Brown 398140ee8e3bSAndrey Semin rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units); 398240ee8e3bSAndrey Semin 3983144b44b1SLen Brown time_unit = msr >> 16 & 0xF; 3984144b44b1SLen Brown if (time_unit == 0) 3985144b44b1SLen Brown time_unit = 0xA; 3986889facbeSLen Brown 3987144b44b1SLen Brown rapl_time_units = 1.0 / (1 << (time_unit)); 3988144b44b1SLen Brown 39899392bd98SCalvin Walton tdp = get_tdp_intel(model); 3990889facbeSLen Brown 3991889facbeSLen Brown rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 399296e47158SLen Brown if (!quiet) 3993b7d8c148SLen Brown fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 39949392bd98SCalvin Walton } 3995889facbeSLen Brown 39969392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model) 39979392bd98SCalvin Walton { 39989392bd98SCalvin Walton unsigned long long msr; 39999392bd98SCalvin Walton unsigned int eax, ebx, ecx, edx; 40009392bd98SCalvin Walton unsigned int has_rapl = 0; 40019392bd98SCalvin Walton double tdp; 40029392bd98SCalvin Walton 40039392bd98SCalvin Walton if (max_extended_level >= 0x80000007) { 40049392bd98SCalvin Walton __cpuid(0x80000007, eax, ebx, ecx, edx); 40059392bd98SCalvin Walton /* RAPL (Fam 17h) */ 40069392bd98SCalvin Walton has_rapl = edx & (1 << 14); 40079392bd98SCalvin Walton } 40089392bd98SCalvin Walton 40099392bd98SCalvin Walton if (!has_rapl) 4010889facbeSLen Brown return; 40119392bd98SCalvin Walton 40129392bd98SCalvin Walton switch (family) { 40139392bd98SCalvin Walton case 0x17: /* Zen, Zen+ */ 40149392bd98SCalvin Walton do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY; 40153316f99aSCalvin Walton if (rapl_joules) { 40163316f99aSCalvin Walton BIC_PRESENT(BIC_Pkg_J); 40179392bd98SCalvin Walton BIC_PRESENT(BIC_Cor_J); 40183316f99aSCalvin Walton } else { 40193316f99aSCalvin Walton BIC_PRESENT(BIC_PkgWatt); 40209392bd98SCalvin Walton BIC_PRESENT(BIC_CorWatt); 40213316f99aSCalvin Walton } 40229392bd98SCalvin Walton break; 40239392bd98SCalvin Walton default: 40249392bd98SCalvin Walton return; 40259392bd98SCalvin Walton } 40269392bd98SCalvin Walton 40279392bd98SCalvin Walton if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr)) 40289392bd98SCalvin Walton return; 40299392bd98SCalvin Walton 40309392bd98SCalvin Walton rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf)); 40319392bd98SCalvin Walton rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f)); 40329392bd98SCalvin Walton rapl_power_units = ldexp(1.0, -(msr & 0xf)); 40339392bd98SCalvin Walton 40349392bd98SCalvin Walton tdp = get_tdp_amd(model); 40359392bd98SCalvin Walton 40369392bd98SCalvin Walton rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp; 40379392bd98SCalvin Walton if (!quiet) 40389392bd98SCalvin Walton fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp); 40399392bd98SCalvin Walton } 40409392bd98SCalvin Walton 40419392bd98SCalvin Walton /* 40429392bd98SCalvin Walton * rapl_probe() 40439392bd98SCalvin Walton * 40449392bd98SCalvin Walton * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units 40459392bd98SCalvin Walton */ 40469392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model) 40479392bd98SCalvin Walton { 40489392bd98SCalvin Walton if (genuine_intel) 40499392bd98SCalvin Walton rapl_probe_intel(family, model); 40509392bd98SCalvin Walton if (authentic_amd) 40519392bd98SCalvin Walton rapl_probe_amd(family, model); 4052889facbeSLen Brown } 4053889facbeSLen Brown 40541b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model) 40553a9a941dSLen Brown { 40563a9a941dSLen Brown if (!genuine_intel) 40573a9a941dSLen Brown return; 40583a9a941dSLen Brown 40593a9a941dSLen Brown if (family != 6) 40603a9a941dSLen Brown return; 40613a9a941dSLen Brown 40623a9a941dSLen Brown switch (model) { 4063869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 4064cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4065869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 40663a9a941dSLen Brown do_gfx_perf_limit_reasons = 1; 4067869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSX */ 40683a9a941dSLen Brown do_core_perf_limit_reasons = 1; 40693a9a941dSLen Brown do_ring_perf_limit_reasons = 1; 40703a9a941dSLen Brown default: 40713a9a941dSLen Brown return; 40723a9a941dSLen Brown } 40733a9a941dSLen Brown } 40743a9a941dSLen Brown 4075ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model) 4076ac980e13SArtem Bityutskiy { 4077ac980e13SArtem Bityutskiy if (is_skx(family, model) || is_bdx(family, model)) 4078ac980e13SArtem Bityutskiy has_automatic_cstate_conversion = 1; 4079ac980e13SArtem Bityutskiy } 4080ac980e13SArtem Bityutskiy 4081889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4082889facbeSLen Brown { 4083889facbeSLen Brown unsigned long long msr; 4084f4896fa5SLen Brown unsigned int dts, dts2; 4085889facbeSLen Brown int cpu; 4086889facbeSLen Brown 4087889facbeSLen Brown if (!(do_dts || do_ptm)) 4088889facbeSLen Brown return 0; 4089889facbeSLen Brown 4090889facbeSLen Brown cpu = t->cpu_id; 4091889facbeSLen Brown 4092889facbeSLen Brown /* DTS is per-core, no need to print for each thread */ 4093889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 4094889facbeSLen Brown return 0; 4095889facbeSLen Brown 4096889facbeSLen Brown if (cpu_migrate(cpu)) { 4097b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4098889facbeSLen Brown return -1; 4099889facbeSLen Brown } 4100889facbeSLen Brown 4101889facbeSLen Brown if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) { 4102889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr)) 4103889facbeSLen Brown return 0; 4104889facbeSLen Brown 4105889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4106b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", 4107889facbeSLen Brown cpu, msr, tcc_activation_temp - dts); 4108889facbeSLen Brown 4109889facbeSLen Brown if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr)) 4110889facbeSLen Brown return 0; 4111889facbeSLen Brown 4112889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4113889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4114b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4115889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4116889facbeSLen Brown } 4117889facbeSLen Brown 4118889facbeSLen Brown 4119f4896fa5SLen Brown if (do_dts && debug) { 4120889facbeSLen Brown unsigned int resolution; 4121889facbeSLen Brown 4122889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr)) 4123889facbeSLen Brown return 0; 4124889facbeSLen Brown 4125889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4126889facbeSLen Brown resolution = (msr >> 27) & 0xF; 4127b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n", 4128889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, resolution); 4129889facbeSLen Brown 4130889facbeSLen Brown if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr)) 4131889facbeSLen Brown return 0; 4132889facbeSLen Brown 4133889facbeSLen Brown dts = (msr >> 16) & 0x7F; 4134889facbeSLen Brown dts2 = (msr >> 8) & 0x7F; 4135b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n", 4136889facbeSLen Brown cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2); 4137889facbeSLen Brown } 4138889facbeSLen Brown 4139889facbeSLen Brown return 0; 4140889facbeSLen Brown } 4141889facbeSLen Brown 4142889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label) 4143889facbeSLen Brown { 4144b7d8c148SLen Brown fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n", 4145889facbeSLen Brown cpu, label, 4146889facbeSLen Brown ((msr >> 15) & 1) ? "EN" : "DIS", 4147889facbeSLen Brown ((msr >> 0) & 0x7FFF) * rapl_power_units, 4148889facbeSLen Brown (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units, 4149889facbeSLen Brown (((msr >> 16) & 1) ? "EN" : "DIS")); 4150889facbeSLen Brown 4151889facbeSLen Brown return; 4152889facbeSLen Brown } 4153889facbeSLen Brown 4154889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4155889facbeSLen Brown { 4156889facbeSLen Brown unsigned long long msr; 41579392bd98SCalvin Walton const char *msr_name; 4158889facbeSLen Brown int cpu; 4159889facbeSLen Brown 4160889facbeSLen Brown if (!do_rapl) 4161889facbeSLen Brown return 0; 4162889facbeSLen Brown 4163889facbeSLen Brown /* RAPL counters are per package, so print only for 1st thread/package */ 4164889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4165889facbeSLen Brown return 0; 4166889facbeSLen Brown 4167889facbeSLen Brown cpu = t->cpu_id; 4168889facbeSLen Brown if (cpu_migrate(cpu)) { 4169b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4170889facbeSLen Brown return -1; 4171889facbeSLen Brown } 4172889facbeSLen Brown 41739392bd98SCalvin Walton if (do_rapl & RAPL_AMD_F17H) { 41749392bd98SCalvin Walton msr_name = "MSR_RAPL_PWR_UNIT"; 41759392bd98SCalvin Walton if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr)) 41769392bd98SCalvin Walton return -1; 41779392bd98SCalvin Walton } else { 41789392bd98SCalvin Walton msr_name = "MSR_RAPL_POWER_UNIT"; 4179889facbeSLen Brown if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr)) 4180889facbeSLen Brown return -1; 41819392bd98SCalvin Walton } 4182889facbeSLen Brown 41839392bd98SCalvin Walton fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr, 4184144b44b1SLen Brown rapl_power_units, rapl_energy_units, rapl_time_units); 418596e47158SLen Brown 4186144b44b1SLen Brown if (do_rapl & RAPL_PKG_POWER_INFO) { 4187144b44b1SLen Brown 4188889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr)) 4189889facbeSLen Brown return -5; 4190889facbeSLen Brown 4191889facbeSLen Brown 4192b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4193889facbeSLen Brown cpu, msr, 4194889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4195889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4196889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4197889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 4198889facbeSLen Brown 4199144b44b1SLen Brown } 4200144b44b1SLen Brown if (do_rapl & RAPL_PKG) { 4201144b44b1SLen Brown 4202889facbeSLen Brown if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr)) 4203889facbeSLen Brown return -9; 4204889facbeSLen Brown 4205b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n", 4206889facbeSLen Brown cpu, msr, (msr >> 63) & 1 ? "" : "UN"); 4207889facbeSLen Brown 4208889facbeSLen Brown print_power_limit_msr(cpu, msr, "PKG Limit #1"); 4209b7d8c148SLen Brown fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n", 4210889facbeSLen Brown cpu, 4211889facbeSLen Brown ((msr >> 47) & 1) ? "EN" : "DIS", 4212889facbeSLen Brown ((msr >> 32) & 0x7FFF) * rapl_power_units, 4213889facbeSLen Brown (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units, 4214889facbeSLen Brown ((msr >> 48) & 1) ? "EN" : "DIS"); 4215889facbeSLen Brown } 4216889facbeSLen Brown 42170b2bb692SLen Brown if (do_rapl & RAPL_DRAM_POWER_INFO) { 4218889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr)) 4219889facbeSLen Brown return -6; 4220889facbeSLen Brown 4221b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n", 4222889facbeSLen Brown cpu, msr, 4223889facbeSLen Brown ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4224889facbeSLen Brown ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4225889facbeSLen Brown ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units, 4226889facbeSLen Brown ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units); 42270b2bb692SLen Brown } 42280b2bb692SLen Brown if (do_rapl & RAPL_DRAM) { 4229889facbeSLen Brown if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr)) 4230889facbeSLen Brown return -9; 4231b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n", 4232889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4233889facbeSLen Brown 4234889facbeSLen Brown print_power_limit_msr(cpu, msr, "DRAM Limit"); 4235889facbeSLen Brown } 4236144b44b1SLen Brown if (do_rapl & RAPL_CORE_POLICY) { 4237889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POLICY, &msr)) 4238889facbeSLen Brown return -7; 4239889facbeSLen Brown 4240b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF); 4241144b44b1SLen Brown } 42429148494cSJacob Pan if (do_rapl & RAPL_CORES_POWER_LIMIT) { 4243889facbeSLen Brown if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr)) 4244889facbeSLen Brown return -9; 4245b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n", 4246889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4247889facbeSLen Brown print_power_limit_msr(cpu, msr, "Cores Limit"); 4248889facbeSLen Brown } 4249889facbeSLen Brown if (do_rapl & RAPL_GFX) { 4250889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POLICY, &msr)) 4251889facbeSLen Brown return -8; 4252889facbeSLen Brown 4253b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF); 4254889facbeSLen Brown 4255889facbeSLen Brown if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr)) 4256889facbeSLen Brown return -9; 4257b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n", 4258889facbeSLen Brown cpu, msr, (msr >> 31) & 1 ? "" : "UN"); 4259889facbeSLen Brown print_power_limit_msr(cpu, msr, "GFX Limit"); 4260889facbeSLen Brown } 4261889facbeSLen Brown return 0; 4262889facbeSLen Brown } 4263889facbeSLen Brown 4264d7899447SLen Brown /* 4265d7899447SLen Brown * SNB adds support for additional MSRs: 4266d7899447SLen Brown * 4267d7899447SLen Brown * MSR_PKG_C7_RESIDENCY 0x000003fa 4268d7899447SLen Brown * MSR_CORE_C7_RESIDENCY 0x000003fe 4269d7899447SLen Brown * MSR_PKG_C2_RESIDENCY 0x0000060d 4270d7899447SLen Brown */ 4271103a8feaSLen Brown 4272d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model) 4273103a8feaSLen Brown { 4274103a8feaSLen Brown if (!genuine_intel) 4275103a8feaSLen Brown return 0; 4276103a8feaSLen Brown 4277103a8feaSLen Brown switch (model) { 4278869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE: 4279869ce69eSLen Brown case INTEL_FAM6_SANDYBRIDGE_X: 4280869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE: /* IVB */ 4281869ce69eSLen Brown case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */ 4282869ce69eSLen Brown case INTEL_FAM6_HASWELL_CORE: /* HSW */ 4283869ce69eSLen Brown case INTEL_FAM6_HASWELL_X: /* HSW */ 4284cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4285869ce69eSLen Brown case INTEL_FAM6_HASWELL_GT3E: /* HSW */ 4286869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4287869ce69eSLen Brown case INTEL_FAM6_BROADWELL_GT3E: /* BDW */ 4288869ce69eSLen Brown case INTEL_FAM6_BROADWELL_X: /* BDX */ 4289869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4290997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4291869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_X: /* SKX */ 4292869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4293f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4294f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 4295103a8feaSLen Brown return 1; 4296103a8feaSLen Brown } 4297103a8feaSLen Brown return 0; 4298103a8feaSLen Brown } 4299103a8feaSLen Brown 4300d7899447SLen Brown /* 4301570992fcSLen Brown * HSW ULT added support for C8/C9/C10 MSRs: 4302d7899447SLen Brown * 4303d7899447SLen Brown * MSR_PKG_C8_RESIDENCY 0x00000630 4304d7899447SLen Brown * MSR_PKG_C9_RESIDENCY 0x00000631 4305d7899447SLen Brown * MSR_PKG_C10_RESIDENCY 0x00000632 43065a63426eSLen Brown * 43075a63426eSLen Brown * MSR_PKGC8_IRTL 0x00000633 43085a63426eSLen Brown * MSR_PKGC9_IRTL 0x00000634 43095a63426eSLen Brown * MSR_PKGC10_IRTL 0x00000635 43105a63426eSLen Brown * 4311d7899447SLen Brown */ 4312570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model) 4313ca58710fSKristen Carlson Accardi { 4314ca58710fSKristen Carlson Accardi if (!genuine_intel) 4315ca58710fSKristen Carlson Accardi return 0; 4316ca58710fSKristen Carlson Accardi 4317ca58710fSKristen Carlson Accardi switch (model) { 4318cd188af5SLen Brown case INTEL_FAM6_HASWELL_ULT: /* HSW */ 4319869ce69eSLen Brown case INTEL_FAM6_BROADWELL_CORE: /* BDW */ 4320869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4321997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4322869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4323f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4324ca58710fSKristen Carlson Accardi return 1; 4325ca58710fSKristen Carlson Accardi } 4326ca58710fSKristen Carlson Accardi return 0; 4327ca58710fSKristen Carlson Accardi } 4328ca58710fSKristen Carlson Accardi 43290b2bb692SLen Brown /* 43300b2bb692SLen Brown * SKL adds support for additional MSRS: 43310b2bb692SLen Brown * 43320b2bb692SLen Brown * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658 43330b2bb692SLen Brown * MSR_PKG_ANY_CORE_C0_RES 0x00000659 43340b2bb692SLen Brown * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A 43350b2bb692SLen Brown * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B 43360b2bb692SLen Brown */ 43370b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model) 43380b2bb692SLen Brown { 43390b2bb692SLen Brown if (!genuine_intel) 43400b2bb692SLen Brown return 0; 43410b2bb692SLen Brown 43420b2bb692SLen Brown switch (model) { 4343869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4344997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 43450b2bb692SLen Brown return 1; 43460b2bb692SLen Brown } 43470b2bb692SLen Brown return 0; 43480b2bb692SLen Brown } 43490b2bb692SLen Brown 4350144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model) 4351144b44b1SLen Brown { 4352144b44b1SLen Brown if (!genuine_intel) 4353144b44b1SLen Brown return 0; 4354144b44b1SLen Brown switch (model) { 4355f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT: /* BYT */ 4356f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_SILVERMONT_X: /* AVN */ 4357144b44b1SLen Brown return 1; 4358144b44b1SLen Brown } 4359144b44b1SLen Brown return 0; 4360144b44b1SLen Brown } 4361144b44b1SLen Brown 4362fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model) 4363fb5d4327SDasaratharaman Chandramouli { 4364fb5d4327SDasaratharaman Chandramouli if (!genuine_intel) 4365fb5d4327SDasaratharaman Chandramouli return 0; 4366fb5d4327SDasaratharaman Chandramouli switch (model) { 4367869ce69eSLen Brown case INTEL_FAM6_XEON_PHI_KNL: /* KNL */ 4368fb5d4327SDasaratharaman Chandramouli return 1; 4369fb5d4327SDasaratharaman Chandramouli } 4370fb5d4327SDasaratharaman Chandramouli return 0; 4371fb5d4327SDasaratharaman Chandramouli } 4372fb5d4327SDasaratharaman Chandramouli 4373997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model) 4374997e5395SSrinivas Pandruvada { 4375997e5395SSrinivas Pandruvada if (!genuine_intel) 4376997e5395SSrinivas Pandruvada return 0; 4377997e5395SSrinivas Pandruvada 4378997e5395SSrinivas Pandruvada switch (model) { 4379997e5395SSrinivas Pandruvada case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */ 4380997e5395SSrinivas Pandruvada return 1; 4381997e5395SSrinivas Pandruvada } 4382997e5395SSrinivas Pandruvada 4383997e5395SSrinivas Pandruvada return 0; 4384997e5395SSrinivas Pandruvada } 4385997e5395SSrinivas Pandruvada 4386b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model) 4387b2b34dfeSHubert Chrzaniuk { 4388b2b34dfeSHubert Chrzaniuk if (is_knl(family, model)) 4389b2b34dfeSHubert Chrzaniuk return 1024; 4390b2b34dfeSHubert Chrzaniuk return 1; 4391b2b34dfeSHubert Chrzaniuk } 4392b2b34dfeSHubert Chrzaniuk 4393144b44b1SLen Brown #define SLM_BCLK_FREQS 5 4394144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0}; 4395144b44b1SLen Brown 4396144b44b1SLen Brown double slm_bclk(void) 4397144b44b1SLen Brown { 4398144b44b1SLen Brown unsigned long long msr = 3; 4399144b44b1SLen Brown unsigned int i; 4400144b44b1SLen Brown double freq; 4401144b44b1SLen Brown 44027ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_FSB_FREQ, &msr)) 4403b7d8c148SLen Brown fprintf(outf, "SLM BCLK: unknown\n"); 4404144b44b1SLen Brown 4405144b44b1SLen Brown i = msr & 0xf; 4406144b44b1SLen Brown if (i >= SLM_BCLK_FREQS) { 4407b7d8c148SLen Brown fprintf(outf, "SLM BCLK[%d] invalid\n", i); 44080a91e551SColin Ian King i = 3; 4409144b44b1SLen Brown } 4410144b44b1SLen Brown freq = slm_freq_table[i]; 4411144b44b1SLen Brown 441296e47158SLen Brown if (!quiet) 4413b7d8c148SLen Brown fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq); 4414144b44b1SLen Brown 4415144b44b1SLen Brown return freq; 4416144b44b1SLen Brown } 4417144b44b1SLen Brown 4418103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model) 4419103a8feaSLen Brown { 4420121b48bbSChrzaniuk, Hubert if (has_snb_msrs(family, model) || is_knl(family, model)) 4421103a8feaSLen Brown return 100.00; 4422144b44b1SLen Brown else if (is_slm(family, model)) 4423144b44b1SLen Brown return slm_bclk(); 4424103a8feaSLen Brown else 4425103a8feaSLen Brown return 133.33; 4426103a8feaSLen Brown } 4427103a8feaSLen Brown 4428889facbeSLen Brown /* 4429889facbeSLen Brown * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where 4430889facbeSLen Brown * the Thermal Control Circuit (TCC) activates. 4431889facbeSLen Brown * This is usually equal to tjMax. 4432889facbeSLen Brown * 4433889facbeSLen Brown * Older processors do not have this MSR, so there we guess, 4434889facbeSLen Brown * but also allow cmdline over-ride with -T. 4435889facbeSLen Brown * 4436889facbeSLen Brown * Several MSR temperature values are in units of degrees-C 4437889facbeSLen Brown * below this value, including the Digital Thermal Sensor (DTS), 4438889facbeSLen Brown * Package Thermal Management Sensor (PTM), and thermal event thresholds. 4439889facbeSLen Brown */ 4440889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p) 4441889facbeSLen Brown { 4442889facbeSLen Brown unsigned long long msr; 4443889facbeSLen Brown unsigned int target_c_local; 4444889facbeSLen Brown int cpu; 4445889facbeSLen Brown 4446889facbeSLen Brown /* tcc_activation_temp is used only for dts or ptm */ 4447889facbeSLen Brown if (!(do_dts || do_ptm)) 4448889facbeSLen Brown return 0; 4449889facbeSLen Brown 4450889facbeSLen Brown /* this is a per-package concept */ 4451889facbeSLen Brown if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) 4452889facbeSLen Brown return 0; 4453889facbeSLen Brown 4454889facbeSLen Brown cpu = t->cpu_id; 4455889facbeSLen Brown if (cpu_migrate(cpu)) { 4456b7d8c148SLen Brown fprintf(outf, "Could not migrate to CPU %d\n", cpu); 4457889facbeSLen Brown return -1; 4458889facbeSLen Brown } 4459889facbeSLen Brown 4460889facbeSLen Brown if (tcc_activation_temp_override != 0) { 4461889facbeSLen Brown tcc_activation_temp = tcc_activation_temp_override; 4462b7d8c148SLen Brown fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", 4463889facbeSLen Brown cpu, tcc_activation_temp); 4464889facbeSLen Brown return 0; 4465889facbeSLen Brown } 4466889facbeSLen Brown 4467889facbeSLen Brown /* Temperature Target MSR is Nehalem and newer only */ 4468d7899447SLen Brown if (!do_nhm_platform_info) 4469889facbeSLen Brown goto guess; 4470889facbeSLen Brown 44717ce7d5deSPrarit Bhargava if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr)) 4472889facbeSLen Brown goto guess; 4473889facbeSLen Brown 44743482124aSJean Delvare target_c_local = (msr >> 16) & 0xFF; 4475889facbeSLen Brown 447696e47158SLen Brown if (!quiet) 4477b7d8c148SLen Brown fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", 4478889facbeSLen Brown cpu, msr, target_c_local); 4479889facbeSLen Brown 44803482124aSJean Delvare if (!target_c_local) 4481889facbeSLen Brown goto guess; 4482889facbeSLen Brown 4483889facbeSLen Brown tcc_activation_temp = target_c_local; 4484889facbeSLen Brown 4485889facbeSLen Brown return 0; 4486889facbeSLen Brown 4487889facbeSLen Brown guess: 4488889facbeSLen Brown tcc_activation_temp = TJMAX_DEFAULT; 4489b7d8c148SLen Brown fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", 4490889facbeSLen Brown cpu, tcc_activation_temp); 4491889facbeSLen Brown 4492889facbeSLen Brown return 0; 4493889facbeSLen Brown } 449469807a63SLen Brown 4495aa8d8cc7SLen Brown void decode_feature_control_msr(void) 4496aa8d8cc7SLen Brown { 4497aa8d8cc7SLen Brown unsigned long long msr; 4498aa8d8cc7SLen Brown 4499aa8d8cc7SLen Brown if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr)) 4500aa8d8cc7SLen Brown fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n", 4501aa8d8cc7SLen Brown base_cpu, msr, 4502aa8d8cc7SLen Brown msr & FEATURE_CONTROL_LOCKED ? "" : "UN-", 4503aa8d8cc7SLen Brown msr & (1 << 18) ? "SGX" : ""); 4504aa8d8cc7SLen Brown } 4505aa8d8cc7SLen Brown 450669807a63SLen Brown void decode_misc_enable_msr(void) 450769807a63SLen Brown { 450869807a63SLen Brown unsigned long long msr; 450969807a63SLen Brown 4510f26b1519SLen Brown if (!genuine_intel) 4511f26b1519SLen Brown return; 4512f26b1519SLen Brown 451369807a63SLen Brown if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr)) 4514e6512624SLen Brown fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n", 451569807a63SLen Brown base_cpu, msr, 4516e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-", 4517e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-", 4518fd3933caSLen Brown msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-", 4519e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "", 4520e6512624SLen Brown msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : ""); 452169807a63SLen Brown } 452269807a63SLen Brown 452333148d67SLen Brown void decode_misc_feature_control(void) 452433148d67SLen Brown { 452533148d67SLen Brown unsigned long long msr; 452633148d67SLen Brown 452733148d67SLen Brown if (!has_misc_feature_control) 452833148d67SLen Brown return; 452933148d67SLen Brown 453033148d67SLen Brown if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr)) 453133148d67SLen Brown fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n", 453233148d67SLen Brown base_cpu, msr, 453333148d67SLen Brown msr & (0 << 0) ? "No-" : "", 453433148d67SLen Brown msr & (1 << 0) ? "No-" : "", 453533148d67SLen Brown msr & (2 << 0) ? "No-" : "", 453633148d67SLen Brown msr & (3 << 0) ? "No-" : ""); 453733148d67SLen Brown } 4538f0057310SLen Brown /* 4539f0057310SLen Brown * Decode MSR_MISC_PWR_MGMT 4540f0057310SLen Brown * 4541f0057310SLen Brown * Decode the bits according to the Nehalem documentation 4542f0057310SLen Brown * bit[0] seems to continue to have same meaning going forward 4543f0057310SLen Brown * bit[1] less so... 4544f0057310SLen Brown */ 4545f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void) 4546f0057310SLen Brown { 4547f0057310SLen Brown unsigned long long msr; 4548f0057310SLen Brown 4549f0057310SLen Brown if (!do_nhm_platform_info) 4550f0057310SLen Brown return; 4551f0057310SLen Brown 4552cf4cbe53SLen Brown if (no_MSR_MISC_PWR_MGMT) 4553cf4cbe53SLen Brown return; 4554cf4cbe53SLen Brown 4555f0057310SLen Brown if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr)) 4556ddadb8adSSrinivas Pandruvada fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n", 4557f0057310SLen Brown base_cpu, msr, 4558f0057310SLen Brown msr & (1 << 0) ? "DIS" : "EN", 4559ddadb8adSSrinivas Pandruvada msr & (1 << 1) ? "EN" : "DIS", 4560ddadb8adSSrinivas Pandruvada msr & (1 << 8) ? "EN" : "DIS"); 4561f0057310SLen Brown } 456271616c8eSLen Brown /* 456371616c8eSLen Brown * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG 456471616c8eSLen Brown * 456571616c8eSLen Brown * This MSRs are present on Silvermont processors, 456671616c8eSLen Brown * Intel Atom processor E3000 series (Baytrail), and friends. 456771616c8eSLen Brown */ 456871616c8eSLen Brown void decode_c6_demotion_policy_msr(void) 456971616c8eSLen Brown { 457071616c8eSLen Brown unsigned long long msr; 457171616c8eSLen Brown 457271616c8eSLen Brown if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr)) 457371616c8eSLen Brown fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n", 457471616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 457571616c8eSLen Brown 457671616c8eSLen Brown if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr)) 457771616c8eSLen Brown fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n", 457871616c8eSLen Brown base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS"); 457971616c8eSLen Brown } 45807f5c258eSLen Brown 4581f5a4c76aSLen Brown /* 4582f5a4c76aSLen Brown * When models are the same, for the purpose of turbostat, reuse 4583f5a4c76aSLen Brown */ 4584f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model) 4585f5a4c76aSLen Brown { 4586f5a4c76aSLen Brown 4587f5a4c76aSLen Brown switch(model) { 4588f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */ 4589f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */ 4590f5a4c76aSLen Brown case 0x1F: /* Core i7 and i5 Processor - Nehalem */ 4591f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */ 4592f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */ 4593f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM; 4594f5a4c76aSLen Brown 4595f5a4c76aSLen Brown case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */ 4596f5a4c76aSLen Brown case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */ 4597f5a4c76aSLen Brown return INTEL_FAM6_NEHALEM_EX; 4598f5a4c76aSLen Brown 4599f5a4c76aSLen Brown case INTEL_FAM6_XEON_PHI_KNM: 4600f5a4c76aSLen Brown return INTEL_FAM6_XEON_PHI_KNL; 4601f5a4c76aSLen Brown 4602f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_X: 4603f5a4c76aSLen Brown case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */ 4604f5a4c76aSLen Brown return INTEL_FAM6_BROADWELL_X; 4605f5a4c76aSLen Brown 4606f5a4c76aSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: 4607f5a4c76aSLen Brown case INTEL_FAM6_SKYLAKE_DESKTOP: 4608f5a4c76aSLen Brown case INTEL_FAM6_KABYLAKE_MOBILE: 4609f5a4c76aSLen Brown case INTEL_FAM6_KABYLAKE_DESKTOP: 4610f5a4c76aSLen Brown return INTEL_FAM6_SKYLAKE_MOBILE; 4611937807d3SLen Brown 4612937807d3SLen Brown case INTEL_FAM6_ICELAKE_MOBILE: 4613d93ea567SRajneesh Bhardwaj case INTEL_FAM6_ICELAKE_NNPI: 4614937807d3SLen Brown return INTEL_FAM6_CANNONLAKE_MOBILE; 4615b62b3184SZhang Rui 4616b62b3184SZhang Rui case INTEL_FAM6_ATOM_TREMONT_X: 4617b62b3184SZhang Rui return INTEL_FAM6_ATOM_GOLDMONT_X; 4618f5a4c76aSLen Brown } 4619f5a4c76aSLen Brown return model; 4620f5a4c76aSLen Brown } 4621fcd17211SLen Brown void process_cpuid() 4622103a8feaSLen Brown { 462334041551SLen Brown unsigned int eax, ebx, ecx, edx; 462434041551SLen Brown unsigned int fms, family, model, stepping, ecx_flags, edx_flags; 4625b3a34e93SLen Brown unsigned int has_turbo; 4626103a8feaSLen Brown 4627103a8feaSLen Brown eax = ebx = ecx = edx = 0; 4628103a8feaSLen Brown 46295aea2f7fSLen Brown __cpuid(0, max_level, ebx, ecx, edx); 4630103a8feaSLen Brown 463134041551SLen Brown if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69) 4632103a8feaSLen Brown genuine_intel = 1; 463334041551SLen Brown else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65) 463434041551SLen Brown authentic_amd = 1; 4635103a8feaSLen Brown 463696e47158SLen Brown if (!quiet) 4637b7d8c148SLen Brown fprintf(outf, "CPUID(0): %.4s%.4s%.4s ", 4638103a8feaSLen Brown (char *)&ebx, (char *)&edx, (char *)&ecx); 4639103a8feaSLen Brown 46405aea2f7fSLen Brown __cpuid(1, fms, ebx, ecx, edx); 4641103a8feaSLen Brown family = (fms >> 8) & 0xf; 4642103a8feaSLen Brown model = (fms >> 4) & 0xf; 4643103a8feaSLen Brown stepping = fms & 0xf; 46445aa3d1a2SCalvin Walton if (family == 0xf) 46455aa3d1a2SCalvin Walton family += (fms >> 20) & 0xff; 46465aa3d1a2SCalvin Walton if (family >= 6) 4647103a8feaSLen Brown model += ((fms >> 16) & 0xf) << 4; 464834041551SLen Brown ecx_flags = ecx; 464934041551SLen Brown edx_flags = edx; 4650103a8feaSLen Brown 4651103a8feaSLen Brown /* 4652103a8feaSLen Brown * check max extended function levels of CPUID. 4653103a8feaSLen Brown * This is needed to check for invariant TSC. 4654103a8feaSLen Brown * This check is valid for both Intel and AMD. 4655103a8feaSLen Brown */ 4656103a8feaSLen Brown ebx = ecx = edx = 0; 46575aea2f7fSLen Brown __cpuid(0x80000000, max_extended_level, ebx, ecx, edx); 4658103a8feaSLen Brown 465934041551SLen Brown if (!quiet) { 466034041551SLen Brown fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", 466134041551SLen Brown max_level, max_extended_level, family, model, stepping, family, model, stepping); 466234041551SLen Brown fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n", 466334041551SLen Brown ecx_flags & (1 << 0) ? "SSE3" : "-", 466434041551SLen Brown ecx_flags & (1 << 3) ? "MONITOR" : "-", 466534041551SLen Brown ecx_flags & (1 << 6) ? "SMX" : "-", 466634041551SLen Brown ecx_flags & (1 << 7) ? "EIST" : "-", 466734041551SLen Brown ecx_flags & (1 << 8) ? "TM2" : "-", 466834041551SLen Brown edx_flags & (1 << 4) ? "TSC" : "-", 466934041551SLen Brown edx_flags & (1 << 5) ? "MSR" : "-", 467034041551SLen Brown edx_flags & (1 << 22) ? "ACPI-TM" : "-", 467134041551SLen Brown edx_flags & (1 << 28) ? "HT" : "-", 467234041551SLen Brown edx_flags & (1 << 29) ? "TM" : "-"); 467334041551SLen Brown } 4674f5a4c76aSLen Brown if (genuine_intel) 4675f5a4c76aSLen Brown model = intel_model_duplicates(model); 467634041551SLen Brown 467734041551SLen Brown if (!(edx_flags & (1 << 5))) 467834041551SLen Brown errx(1, "CPUID: no MSR"); 467934041551SLen Brown 468061a87ba7SLen Brown if (max_extended_level >= 0x80000007) { 4681103a8feaSLen Brown 4682103a8feaSLen Brown /* 4683103a8feaSLen Brown * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 4684103a8feaSLen Brown * this check is valid for both Intel and AMD 4685103a8feaSLen Brown */ 46865aea2f7fSLen Brown __cpuid(0x80000007, eax, ebx, ecx, edx); 46878209e054SThomas Renninger has_invariant_tsc = edx & (1 << 8); 4688d7899447SLen Brown } 4689103a8feaSLen Brown 4690103a8feaSLen Brown /* 4691103a8feaSLen Brown * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 4692103a8feaSLen Brown * this check is valid for both Intel and AMD 4693103a8feaSLen Brown */ 4694103a8feaSLen Brown 46955aea2f7fSLen Brown __cpuid(0x6, eax, ebx, ecx, edx); 46968209e054SThomas Renninger has_aperf = ecx & (1 << 0); 4697812db3f7SLen Brown if (has_aperf) { 4698812db3f7SLen Brown BIC_PRESENT(BIC_Avg_MHz); 4699812db3f7SLen Brown BIC_PRESENT(BIC_Busy); 4700812db3f7SLen Brown BIC_PRESENT(BIC_Bzy_MHz); 4701812db3f7SLen Brown } 4702889facbeSLen Brown do_dts = eax & (1 << 0); 4703812db3f7SLen Brown if (do_dts) 4704812db3f7SLen Brown BIC_PRESENT(BIC_CoreTmp); 4705b3a34e93SLen Brown has_turbo = eax & (1 << 1); 4706889facbeSLen Brown do_ptm = eax & (1 << 6); 4707812db3f7SLen Brown if (do_ptm) 4708812db3f7SLen Brown BIC_PRESENT(BIC_PkgTmp); 47097f5c258eSLen Brown has_hwp = eax & (1 << 7); 47107f5c258eSLen Brown has_hwp_notify = eax & (1 << 8); 47117f5c258eSLen Brown has_hwp_activity_window = eax & (1 << 9); 47127f5c258eSLen Brown has_hwp_epp = eax & (1 << 10); 47137f5c258eSLen Brown has_hwp_pkg = eax & (1 << 11); 4714889facbeSLen Brown has_epb = ecx & (1 << 3); 4715889facbeSLen Brown 471696e47158SLen Brown if (!quiet) 4717b3a34e93SLen Brown fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, " 47187f5c258eSLen Brown "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n", 47197f5c258eSLen Brown has_aperf ? "" : "No-", 4720b3a34e93SLen Brown has_turbo ? "" : "No-", 47217f5c258eSLen Brown do_dts ? "" : "No-", 47227f5c258eSLen Brown do_ptm ? "" : "No-", 47237f5c258eSLen Brown has_hwp ? "" : "No-", 47247f5c258eSLen Brown has_hwp_notify ? "" : "No-", 47257f5c258eSLen Brown has_hwp_activity_window ? "" : "No-", 47267f5c258eSLen Brown has_hwp_epp ? "" : "No-", 47277f5c258eSLen Brown has_hwp_pkg ? "" : "No-", 47287f5c258eSLen Brown has_epb ? "" : "No-"); 4729103a8feaSLen Brown 473096e47158SLen Brown if (!quiet) 473169807a63SLen Brown decode_misc_enable_msr(); 473269807a63SLen Brown 473333148d67SLen Brown 473496e47158SLen Brown if (max_level >= 0x7 && !quiet) { 4735aa8d8cc7SLen Brown int has_sgx; 4736aa8d8cc7SLen Brown 4737aa8d8cc7SLen Brown ecx = 0; 4738aa8d8cc7SLen Brown 4739aa8d8cc7SLen Brown __cpuid_count(0x7, 0, eax, ebx, ecx, edx); 4740aa8d8cc7SLen Brown 4741aa8d8cc7SLen Brown has_sgx = ebx & (1 << 2); 4742aa8d8cc7SLen Brown fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-"); 4743aa8d8cc7SLen Brown 4744aa8d8cc7SLen Brown if (has_sgx) 4745aa8d8cc7SLen Brown decode_feature_control_msr(); 4746aa8d8cc7SLen Brown } 4747aa8d8cc7SLen Brown 474861a87ba7SLen Brown if (max_level >= 0x15) { 47498a5bdf41SLen Brown unsigned int eax_crystal; 47508a5bdf41SLen Brown unsigned int ebx_tsc; 47518a5bdf41SLen Brown 47528a5bdf41SLen Brown /* 47538a5bdf41SLen Brown * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz 47548a5bdf41SLen Brown */ 47558a5bdf41SLen Brown eax_crystal = ebx_tsc = crystal_hz = edx = 0; 47565aea2f7fSLen Brown __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx); 47578a5bdf41SLen Brown 47588a5bdf41SLen Brown if (ebx_tsc != 0) { 47598a5bdf41SLen Brown 476096e47158SLen Brown if (!quiet && (ebx != 0)) 4761b7d8c148SLen Brown fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n", 47628a5bdf41SLen Brown eax_crystal, ebx_tsc, crystal_hz); 47638a5bdf41SLen Brown 47648a5bdf41SLen Brown if (crystal_hz == 0) 47658a5bdf41SLen Brown switch(model) { 4766869ce69eSLen Brown case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */ 4767e8efbc80SLen Brown crystal_hz = 24000000; /* 24.0 MHz */ 4768e8efbc80SLen Brown break; 4769f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_X: /* DNV */ 4770ec53e594SLen Brown crystal_hz = 25000000; /* 25.0 MHz */ 4771ec53e594SLen Brown break; 4772869ce69eSLen Brown case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */ 4773f2c4db1bSPeter Zijlstra case INTEL_FAM6_ATOM_GOLDMONT_PLUS: 4774e8efbc80SLen Brown crystal_hz = 19200000; /* 19.2 MHz */ 47758a5bdf41SLen Brown break; 47768a5bdf41SLen Brown default: 47778a5bdf41SLen Brown crystal_hz = 0; 47788a5bdf41SLen Brown } 47798a5bdf41SLen Brown 47808a5bdf41SLen Brown if (crystal_hz) { 47818a5bdf41SLen Brown tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal; 478296e47158SLen Brown if (!quiet) 4783b7d8c148SLen Brown fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n", 47848a5bdf41SLen Brown tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal); 47858a5bdf41SLen Brown } 47868a5bdf41SLen Brown } 47878a5bdf41SLen Brown } 478861a87ba7SLen Brown if (max_level >= 0x16) { 478961a87ba7SLen Brown unsigned int base_mhz, max_mhz, bus_mhz, edx; 479061a87ba7SLen Brown 479161a87ba7SLen Brown /* 479261a87ba7SLen Brown * CPUID 16H Base MHz, Max MHz, Bus MHz 479361a87ba7SLen Brown */ 479461a87ba7SLen Brown base_mhz = max_mhz = bus_mhz = edx = 0; 479561a87ba7SLen Brown 47965aea2f7fSLen Brown __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx); 479796e47158SLen Brown if (!quiet) 4798b7d8c148SLen Brown fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n", 479961a87ba7SLen Brown base_mhz, max_mhz, bus_mhz); 480061a87ba7SLen Brown } 48018a5bdf41SLen Brown 4802b2b34dfeSHubert Chrzaniuk if (has_aperf) 4803b2b34dfeSHubert Chrzaniuk aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model); 4804b2b34dfeSHubert Chrzaniuk 4805812db3f7SLen Brown BIC_PRESENT(BIC_IRQ); 4806812db3f7SLen Brown BIC_PRESENT(BIC_TSC_MHz); 4807812db3f7SLen Brown 4808812db3f7SLen Brown if (probe_nhm_msrs(family, model)) { 4809812db3f7SLen Brown do_nhm_platform_info = 1; 4810812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c1); 4811812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c3); 4812812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c6); 4813812db3f7SLen Brown BIC_PRESENT(BIC_SMI); 4814812db3f7SLen Brown } 4815d7899447SLen Brown do_snb_cstates = has_snb_msrs(family, model); 4816812db3f7SLen Brown 4817812db3f7SLen Brown if (do_snb_cstates) 4818812db3f7SLen Brown BIC_PRESENT(BIC_CPU_c7); 4819812db3f7SLen Brown 48205a63426eSLen Brown do_irtl_snb = has_snb_msrs(family, model); 48210f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__2)) 48220f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc2); 48230f47c08dSLen Brown if (pkg_cstate_limit >= PCL__3) 48240f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc3); 48250f47c08dSLen Brown if (pkg_cstate_limit >= PCL__6) 48260f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 48270f47c08dSLen Brown if (do_snb_cstates && (pkg_cstate_limit >= PCL__7)) 48280f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc7); 48290539ba11SLen Brown if (has_slv_msrs(family, model)) { 48300f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc2); 48310f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 48320f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc6); 48330f47c08dSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 48340539ba11SLen Brown BIC_PRESENT(BIC_Mod_c6); 48350539ba11SLen Brown use_c1_residency_msr = 1; 48360539ba11SLen Brown } 48377170a374SLen Brown if (is_dnv(family, model)) { 48387170a374SLen Brown BIC_PRESENT(BIC_CPU_c1); 48397170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 48407170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 48417170a374SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 48427170a374SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 48437170a374SLen Brown use_c1_residency_msr = 1; 48447170a374SLen Brown } 484534c76197SLen Brown if (is_skx(family, model)) { 484634c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 484734c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc3); 484834c76197SLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 484934c76197SLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 485034c76197SLen Brown } 4851ade0ebacSLen Brown if (is_bdx(family, model)) { 4852ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_CPU_c7); 4853ade0ebacSLen Brown BIC_NOT_PRESENT(BIC_Pkgpc7); 4854ade0ebacSLen Brown } 4855570992fcSLen Brown if (has_c8910_msrs(family, model)) { 48560f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc8); 48570f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc9); 48580f47c08dSLen Brown BIC_PRESENT(BIC_Pkgpc10); 48590f47c08dSLen Brown } 4860570992fcSLen Brown do_irtl_hsw = has_c8910_msrs(family, model); 4861a99d8730SLen Brown if (has_skl_msrs(family, model)) { 4862a99d8730SLen Brown BIC_PRESENT(BIC_Totl_c0); 4863a99d8730SLen Brown BIC_PRESENT(BIC_Any_c0); 4864a99d8730SLen Brown BIC_PRESENT(BIC_GFX_c0); 4865a99d8730SLen Brown BIC_PRESENT(BIC_CPUGFX); 4866a99d8730SLen Brown } 4867144b44b1SLen Brown do_slm_cstates = is_slm(family, model); 4868fb5d4327SDasaratharaman Chandramouli do_knl_cstates = is_knl(family, model); 4869103a8feaSLen Brown 487031a1f15cSLen Brown if (do_slm_cstates || do_knl_cstates || is_cnl(family, model)) 4871562855eeSLen Brown BIC_NOT_PRESENT(BIC_CPU_c3); 4872103a8feaSLen Brown 487396e47158SLen Brown if (!quiet) 4874f0057310SLen Brown decode_misc_pwr_mgmt_msr(); 4875f0057310SLen Brown 487696e47158SLen Brown if (!quiet && has_slv_msrs(family, model)) 487771616c8eSLen Brown decode_c6_demotion_policy_msr(); 487871616c8eSLen Brown 4879889facbeSLen Brown rapl_probe(family, model); 48803a9a941dSLen Brown perf_limit_reasons_probe(family, model); 4881ac980e13SArtem Bityutskiy automatic_cstate_conversion_probe(family, model); 4882889facbeSLen Brown 488396e47158SLen Brown if (!quiet) 48841b69317dSColin Ian King dump_cstate_pstate_config_info(family, model); 4885fcd17211SLen Brown 488641618e63SLen Brown if (!quiet) 488741618e63SLen Brown dump_sysfs_cstate_config(); 48887293fccdSLen Brown if (!quiet) 48897293fccdSLen Brown dump_sysfs_pstate_config(); 489041618e63SLen Brown 4891a2b7b749SLen Brown if (has_skl_msrs(family, model)) 4892a2b7b749SLen Brown calculate_tsc_tweak(); 4893a2b7b749SLen Brown 4894812db3f7SLen Brown if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK)) 4895812db3f7SLen Brown BIC_PRESENT(BIC_GFX_rc6); 4896fdf676e5SLen Brown 4897812db3f7SLen Brown if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK)) 4898812db3f7SLen Brown BIC_PRESENT(BIC_GFXMHz); 489927d47356SLen Brown 4900be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK)) 4901be0e54c4SLen Brown BIC_PRESENT(BIC_CPU_LPI); 4902be0e54c4SLen Brown else 4903be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_CPU_LPI); 4904be0e54c4SLen Brown 4905be0e54c4SLen Brown if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK)) 4906be0e54c4SLen Brown BIC_PRESENT(BIC_SYS_LPI); 4907be0e54c4SLen Brown else 4908be0e54c4SLen Brown BIC_NOT_PRESENT(BIC_SYS_LPI); 4909be0e54c4SLen Brown 491096e47158SLen Brown if (!quiet) 491133148d67SLen Brown decode_misc_feature_control(); 491233148d67SLen Brown 4913889facbeSLen Brown return; 4914103a8feaSLen Brown } 4915103a8feaSLen Brown 4916103a8feaSLen Brown /* 4917103a8feaSLen Brown * in /dev/cpu/ return success for names that are numbers 4918103a8feaSLen Brown * ie. filter out ".", "..", "microcode". 4919103a8feaSLen Brown */ 4920103a8feaSLen Brown int dir_filter(const struct dirent *dirp) 4921103a8feaSLen Brown { 4922103a8feaSLen Brown if (isdigit(dirp->d_name[0])) 4923103a8feaSLen Brown return 1; 4924103a8feaSLen Brown else 4925103a8feaSLen Brown return 0; 4926103a8feaSLen Brown } 4927103a8feaSLen Brown 4928103a8feaSLen Brown int open_dev_cpu_msr(int dummy1) 4929103a8feaSLen Brown { 4930103a8feaSLen Brown return 0; 4931103a8feaSLen Brown } 4932103a8feaSLen Brown 4933c98d5d94SLen Brown void topology_probe() 4934c98d5d94SLen Brown { 4935c98d5d94SLen Brown int i; 4936c98d5d94SLen Brown int max_core_id = 0; 4937c98d5d94SLen Brown int max_package_id = 0; 49386de68fe1SLen Brown int max_die_id = 0; 4939c98d5d94SLen Brown int max_siblings = 0; 4940c98d5d94SLen Brown 4941c98d5d94SLen Brown /* Initialize num_cpus, max_cpu_num */ 4942843c5791SPrarit Bhargava set_max_cpu_num(); 4943c98d5d94SLen Brown topo.num_cpus = 0; 4944c98d5d94SLen Brown for_all_proc_cpus(count_cpus); 4945c98d5d94SLen Brown if (!summary_only && topo.num_cpus > 1) 4946812db3f7SLen Brown BIC_PRESENT(BIC_CPU); 4947c98d5d94SLen Brown 4948d8af6f5fSLen Brown if (debug > 1) 4949b7d8c148SLen Brown fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); 4950c98d5d94SLen Brown 4951c98d5d94SLen Brown cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); 4952b2c95d90SJosh Triplett if (cpus == NULL) 4953b2c95d90SJosh Triplett err(1, "calloc cpus"); 4954c98d5d94SLen Brown 4955c98d5d94SLen Brown /* 4956c98d5d94SLen Brown * Allocate and initialize cpu_present_set 4957c98d5d94SLen Brown */ 4958c98d5d94SLen Brown cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4959b2c95d90SJosh Triplett if (cpu_present_set == NULL) 4960b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4961c98d5d94SLen Brown cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4962c98d5d94SLen Brown CPU_ZERO_S(cpu_present_setsize, cpu_present_set); 4963c98d5d94SLen Brown for_all_proc_cpus(mark_cpu_present); 4964c98d5d94SLen Brown 4965c98d5d94SLen Brown /* 49661ef7d21aSLen Brown * Validate that all cpus in cpu_subset are also in cpu_present_set 49671ef7d21aSLen Brown */ 49681ef7d21aSLen Brown for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) { 49691ef7d21aSLen Brown if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset)) 49701ef7d21aSLen Brown if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set)) 49711ef7d21aSLen Brown err(1, "cpu%d not present", i); 49721ef7d21aSLen Brown } 49731ef7d21aSLen Brown 49741ef7d21aSLen Brown /* 4975c98d5d94SLen Brown * Allocate and initialize cpu_affinity_set 4976c98d5d94SLen Brown */ 4977c98d5d94SLen Brown cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); 4978b2c95d90SJosh Triplett if (cpu_affinity_set == NULL) 4979b2c95d90SJosh Triplett err(3, "CPU_ALLOC"); 4980c98d5d94SLen Brown cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); 4981c98d5d94SLen Brown CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); 4982c98d5d94SLen Brown 49838cb48b32SPrarit Bhargava for_all_proc_cpus(init_thread_id); 4984c98d5d94SLen Brown 4985c98d5d94SLen Brown /* 4986c98d5d94SLen Brown * For online cpus 4987c98d5d94SLen Brown * find max_core_id, max_package_id 4988c98d5d94SLen Brown */ 4989c98d5d94SLen Brown for (i = 0; i <= topo.max_cpu_num; ++i) { 4990c98d5d94SLen Brown int siblings; 4991c98d5d94SLen Brown 4992c98d5d94SLen Brown if (cpu_is_not_present(i)) { 4993d8af6f5fSLen Brown if (debug > 1) 4994b7d8c148SLen Brown fprintf(outf, "cpu%d NOT PRESENT\n", i); 4995c98d5d94SLen Brown continue; 4996c98d5d94SLen Brown } 4997c98d5d94SLen Brown 49980e2d8f05SLen Brown cpus[i].logical_cpu_id = i; 49990e2d8f05SLen Brown 50000e2d8f05SLen Brown /* get package information */ 5001c98d5d94SLen Brown cpus[i].physical_package_id = get_physical_package_id(i); 5002c98d5d94SLen Brown if (cpus[i].physical_package_id > max_package_id) 5003c98d5d94SLen Brown max_package_id = cpus[i].physical_package_id; 5004c98d5d94SLen Brown 50056de68fe1SLen Brown /* get die information */ 50066de68fe1SLen Brown cpus[i].die_id = get_die_id(i); 50076de68fe1SLen Brown if (cpus[i].die_id > max_die_id) 50086de68fe1SLen Brown max_die_id = cpus[i].die_id; 50096de68fe1SLen Brown 50100e2d8f05SLen Brown /* get numa node information */ 5011ef605741SPrarit Bhargava cpus[i].physical_node_id = get_physical_node_id(&cpus[i]); 5012ef605741SPrarit Bhargava if (cpus[i].physical_node_id > topo.max_node_num) 5013ef605741SPrarit Bhargava topo.max_node_num = cpus[i].physical_node_id; 50140e2d8f05SLen Brown 50150e2d8f05SLen Brown /* get core information */ 50160e2d8f05SLen Brown cpus[i].physical_core_id = get_core_id(i); 50170e2d8f05SLen Brown if (cpus[i].physical_core_id > max_core_id) 50180e2d8f05SLen Brown max_core_id = cpus[i].physical_core_id; 50190e2d8f05SLen Brown 50200e2d8f05SLen Brown /* get thread information */ 50210e2d8f05SLen Brown siblings = get_thread_siblings(&cpus[i]); 5022c98d5d94SLen Brown if (siblings > max_siblings) 5023c98d5d94SLen Brown max_siblings = siblings; 50244f206a0fSArtem Bityutskiy if (cpus[i].thread_id == 0) 50258cb48b32SPrarit Bhargava topo.num_cores++; 5026c98d5d94SLen Brown } 5027ef605741SPrarit Bhargava 502870a9c6e8SPrarit Bhargava topo.cores_per_node = max_core_id + 1; 5029d8af6f5fSLen Brown if (debug > 1) 5030b7d8c148SLen Brown fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", 503170a9c6e8SPrarit Bhargava max_core_id, topo.cores_per_node); 503270a9c6e8SPrarit Bhargava if (!summary_only && topo.cores_per_node > 1) 5033812db3f7SLen Brown BIC_PRESENT(BIC_Core); 5034c98d5d94SLen Brown 50356de68fe1SLen Brown topo.num_die = max_die_id + 1; 50366de68fe1SLen Brown if (debug > 1) 50376de68fe1SLen Brown fprintf(outf, "max_die_id %d, sizing for %d die\n", 50386de68fe1SLen Brown max_die_id, topo.num_die); 50396de68fe1SLen Brown if (!summary_only && topo.num_die > 1) 50406de68fe1SLen Brown BIC_PRESENT(BIC_Die); 50416de68fe1SLen Brown 5042c98d5d94SLen Brown topo.num_packages = max_package_id + 1; 5043d8af6f5fSLen Brown if (debug > 1) 5044b7d8c148SLen Brown fprintf(outf, "max_package_id %d, sizing for %d packages\n", 5045c98d5d94SLen Brown max_package_id, topo.num_packages); 50467da6e3e2SLen Brown if (!summary_only && topo.num_packages > 1) 5047812db3f7SLen Brown BIC_PRESENT(BIC_Package); 5048c98d5d94SLen Brown 5049ef605741SPrarit Bhargava set_node_data(); 5050ef605741SPrarit Bhargava if (debug > 1) 505170a9c6e8SPrarit Bhargava fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg); 505201235041SPrarit Bhargava if (!summary_only && topo.nodes_per_pkg > 1) 505301235041SPrarit Bhargava BIC_PRESENT(BIC_Node); 5054ef605741SPrarit Bhargava 505570a9c6e8SPrarit Bhargava topo.threads_per_core = max_siblings; 5056d8af6f5fSLen Brown if (debug > 1) 5057b7d8c148SLen Brown fprintf(outf, "max_siblings %d\n", max_siblings); 50582ffbb224SPrarit Bhargava 50592ffbb224SPrarit Bhargava if (debug < 1) 50602ffbb224SPrarit Bhargava return; 50612ffbb224SPrarit Bhargava 50622ffbb224SPrarit Bhargava for (i = 0; i <= topo.max_cpu_num; ++i) { 50630ec712e3SLen Brown if (cpu_is_not_present(i)) 50640ec712e3SLen Brown continue; 50652ffbb224SPrarit Bhargava fprintf(outf, 50666de68fe1SLen Brown "cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n", 50676de68fe1SLen Brown i, cpus[i].physical_package_id, cpus[i].die_id, 50682ffbb224SPrarit Bhargava cpus[i].physical_node_id, 50692ffbb224SPrarit Bhargava cpus[i].logical_node_id, 50702ffbb224SPrarit Bhargava cpus[i].physical_core_id, 50712ffbb224SPrarit Bhargava cpus[i].thread_id); 50722ffbb224SPrarit Bhargava } 50732ffbb224SPrarit Bhargava 5074c98d5d94SLen Brown } 5075c98d5d94SLen Brown 5076c98d5d94SLen Brown void 507740f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c, 507840f5cfe7SPrarit Bhargava struct pkg_data **p) 5079c98d5d94SLen Brown { 5080c98d5d94SLen Brown int i; 508140f5cfe7SPrarit Bhargava int num_cores = topo.cores_per_node * topo.nodes_per_pkg * 508240f5cfe7SPrarit Bhargava topo.num_packages; 508340f5cfe7SPrarit Bhargava int num_threads = topo.threads_per_core * num_cores; 5084c98d5d94SLen Brown 508540f5cfe7SPrarit Bhargava *t = calloc(num_threads, sizeof(struct thread_data)); 5086c98d5d94SLen Brown if (*t == NULL) 5087c98d5d94SLen Brown goto error; 5088c98d5d94SLen Brown 508940f5cfe7SPrarit Bhargava for (i = 0; i < num_threads; i++) 5090c98d5d94SLen Brown (*t)[i].cpu_id = -1; 5091c98d5d94SLen Brown 509240f5cfe7SPrarit Bhargava *c = calloc(num_cores, sizeof(struct core_data)); 5093c98d5d94SLen Brown if (*c == NULL) 5094c98d5d94SLen Brown goto error; 5095c98d5d94SLen Brown 509640f5cfe7SPrarit Bhargava for (i = 0; i < num_cores; i++) 5097c98d5d94SLen Brown (*c)[i].core_id = -1; 5098c98d5d94SLen Brown 5099678a3bd1SLen Brown *p = calloc(topo.num_packages, sizeof(struct pkg_data)); 5100c98d5d94SLen Brown if (*p == NULL) 5101c98d5d94SLen Brown goto error; 5102c98d5d94SLen Brown 5103c98d5d94SLen Brown for (i = 0; i < topo.num_packages; i++) 5104c98d5d94SLen Brown (*p)[i].package_id = i; 5105c98d5d94SLen Brown 5106c98d5d94SLen Brown return; 5107c98d5d94SLen Brown error: 5108b2c95d90SJosh Triplett err(1, "calloc counters"); 5109c98d5d94SLen Brown } 5110c98d5d94SLen Brown /* 5111c98d5d94SLen Brown * init_counter() 5112c98d5d94SLen Brown * 5113c98d5d94SLen Brown * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE 5114c98d5d94SLen Brown */ 5115c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, 51168cb48b32SPrarit Bhargava struct pkg_data *pkg_base, int cpu_id) 5117c98d5d94SLen Brown { 51188cb48b32SPrarit Bhargava int pkg_id = cpus[cpu_id].physical_package_id; 511940f5cfe7SPrarit Bhargava int node_id = cpus[cpu_id].logical_node_id; 51208cb48b32SPrarit Bhargava int core_id = cpus[cpu_id].physical_core_id; 51218cb48b32SPrarit Bhargava int thread_id = cpus[cpu_id].thread_id; 5122c98d5d94SLen Brown struct thread_data *t; 5123c98d5d94SLen Brown struct core_data *c; 5124c98d5d94SLen Brown struct pkg_data *p; 5125c98d5d94SLen Brown 512642dd4520SNathan Ciobanu 512742dd4520SNathan Ciobanu /* Workaround for systems where physical_node_id==-1 512842dd4520SNathan Ciobanu * and logical_node_id==(-1 - topo.num_cpus) 512942dd4520SNathan Ciobanu */ 513042dd4520SNathan Ciobanu if (node_id < 0) 513142dd4520SNathan Ciobanu node_id = 0; 513242dd4520SNathan Ciobanu 513340f5cfe7SPrarit Bhargava t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id); 513440f5cfe7SPrarit Bhargava c = GET_CORE(core_base, core_id, node_id, pkg_id); 51358cb48b32SPrarit Bhargava p = GET_PKG(pkg_base, pkg_id); 5136c98d5d94SLen Brown 5137c98d5d94SLen Brown t->cpu_id = cpu_id; 51388cb48b32SPrarit Bhargava if (thread_id == 0) { 5139c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_THREAD_IN_CORE; 5140c98d5d94SLen Brown if (cpu_is_first_core_in_package(cpu_id)) 5141c98d5d94SLen Brown t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE; 5142c98d5d94SLen Brown } 5143c98d5d94SLen Brown 51448cb48b32SPrarit Bhargava c->core_id = core_id; 51458cb48b32SPrarit Bhargava p->package_id = pkg_id; 5146c98d5d94SLen Brown } 5147c98d5d94SLen Brown 5148c98d5d94SLen Brown 5149c98d5d94SLen Brown int initialize_counters(int cpu_id) 5150c98d5d94SLen Brown { 51518cb48b32SPrarit Bhargava init_counter(EVEN_COUNTERS, cpu_id); 51528cb48b32SPrarit Bhargava init_counter(ODD_COUNTERS, cpu_id); 5153c98d5d94SLen Brown return 0; 5154c98d5d94SLen Brown } 5155c98d5d94SLen Brown 5156c98d5d94SLen Brown void allocate_output_buffer() 5157c98d5d94SLen Brown { 5158eeb71c95SNaoya Horiguchi output_buffer = calloc(1, (1 + topo.num_cpus) * 2048); 5159c98d5d94SLen Brown outp = output_buffer; 5160b2c95d90SJosh Triplett if (outp == NULL) 5161b2c95d90SJosh Triplett err(-1, "calloc output buffer"); 5162c98d5d94SLen Brown } 516336229897SLen Brown void allocate_fd_percpu(void) 516436229897SLen Brown { 516501a67adfSMika Westerberg fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 516636229897SLen Brown if (fd_percpu == NULL) 516736229897SLen Brown err(-1, "calloc fd_percpu"); 516836229897SLen Brown } 5169562a2d37SLen Brown void allocate_irq_buffers(void) 5170562a2d37SLen Brown { 5171562a2d37SLen Brown irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int)); 5172562a2d37SLen Brown if (irq_column_2_cpu == NULL) 5173562a2d37SLen Brown err(-1, "calloc %d", topo.num_cpus); 5174c98d5d94SLen Brown 517501a67adfSMika Westerberg irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int)); 5176562a2d37SLen Brown if (irqs_per_cpu == NULL) 517701a67adfSMika Westerberg err(-1, "calloc %d", topo.max_cpu_num + 1); 5178562a2d37SLen Brown } 5179c98d5d94SLen Brown void setup_all_buffers(void) 5180c98d5d94SLen Brown { 5181c98d5d94SLen Brown topology_probe(); 5182562a2d37SLen Brown allocate_irq_buffers(); 518336229897SLen Brown allocate_fd_percpu(); 5184c98d5d94SLen Brown allocate_counters(&thread_even, &core_even, &package_even); 5185c98d5d94SLen Brown allocate_counters(&thread_odd, &core_odd, &package_odd); 5186c98d5d94SLen Brown allocate_output_buffer(); 5187c98d5d94SLen Brown for_all_proc_cpus(initialize_counters); 5188c98d5d94SLen Brown } 51893b4d5c7fSAndy Shevchenko 51907ce7d5deSPrarit Bhargava void set_base_cpu(void) 51917ce7d5deSPrarit Bhargava { 51927ce7d5deSPrarit Bhargava base_cpu = sched_getcpu(); 51937ce7d5deSPrarit Bhargava if (base_cpu < 0) 51947ce7d5deSPrarit Bhargava err(-ENODEV, "No valid cpus found"); 51957ce7d5deSPrarit Bhargava 51967ce7d5deSPrarit Bhargava if (debug > 1) 5197b7d8c148SLen Brown fprintf(outf, "base_cpu = %d\n", base_cpu); 51987ce7d5deSPrarit Bhargava } 51997ce7d5deSPrarit Bhargava 5200103a8feaSLen Brown void turbostat_init() 5201103a8feaSLen Brown { 52027ce7d5deSPrarit Bhargava setup_all_buffers(); 52037ce7d5deSPrarit Bhargava set_base_cpu(); 5204103a8feaSLen Brown check_dev_msr(); 520598481e79SLen Brown check_permissions(); 5206fcd17211SLen Brown process_cpuid(); 5207103a8feaSLen Brown 5208103a8feaSLen Brown 520996e47158SLen Brown if (!quiet) 52107f5c258eSLen Brown for_all_cpus(print_hwp, ODD_COUNTERS); 52117f5c258eSLen Brown 521296e47158SLen Brown if (!quiet) 5213889facbeSLen Brown for_all_cpus(print_epb, ODD_COUNTERS); 5214889facbeSLen Brown 521596e47158SLen Brown if (!quiet) 52163a9a941dSLen Brown for_all_cpus(print_perf_limit, ODD_COUNTERS); 52173a9a941dSLen Brown 521896e47158SLen Brown if (!quiet) 5219889facbeSLen Brown for_all_cpus(print_rapl, ODD_COUNTERS); 5220889facbeSLen Brown 5221889facbeSLen Brown for_all_cpus(set_temperature_target, ODD_COUNTERS); 5222889facbeSLen Brown 522396e47158SLen Brown if (!quiet) 5224889facbeSLen Brown for_all_cpus(print_thermal, ODD_COUNTERS); 52255a63426eSLen Brown 522696e47158SLen Brown if (!quiet && do_irtl_snb) 52275a63426eSLen Brown print_irtl(); 5228103a8feaSLen Brown } 5229103a8feaSLen Brown 5230103a8feaSLen Brown int fork_it(char **argv) 5231103a8feaSLen Brown { 5232103a8feaSLen Brown pid_t child_pid; 5233d91bb17cSLen Brown int status; 5234d15cf7c1SLen Brown 5235218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5236d91bb17cSLen Brown status = for_all_cpus(get_counters, EVEN_COUNTERS); 52374c2122d4SLen Brown first_counter_read = 0; 5238d91bb17cSLen Brown if (status) 5239d91bb17cSLen Brown exit(status); 5240d15cf7c1SLen Brown /* clear affinity side-effect of get_counters() */ 5241d15cf7c1SLen Brown sched_setaffinity(0, cpu_present_setsize, cpu_present_set); 5242103a8feaSLen Brown gettimeofday(&tv_even, (struct timezone *)NULL); 5243103a8feaSLen Brown 5244103a8feaSLen Brown child_pid = fork(); 5245103a8feaSLen Brown if (!child_pid) { 5246103a8feaSLen Brown /* child */ 5247103a8feaSLen Brown execvp(argv[0], argv); 52480815a3d0SLen Brown err(errno, "exec %s", argv[0]); 5249103a8feaSLen Brown } else { 5250103a8feaSLen Brown 5251103a8feaSLen Brown /* parent */ 5252b2c95d90SJosh Triplett if (child_pid == -1) 5253b2c95d90SJosh Triplett err(1, "fork"); 5254103a8feaSLen Brown 5255103a8feaSLen Brown signal(SIGINT, SIG_IGN); 5256103a8feaSLen Brown signal(SIGQUIT, SIG_IGN); 5257b2c95d90SJosh Triplett if (waitpid(child_pid, &status, 0) == -1) 5258b2c95d90SJosh Triplett err(status, "waitpid"); 52592a954966SDavid Arcari 52602a954966SDavid Arcari if (WIFEXITED(status)) 52612a954966SDavid Arcari status = WEXITSTATUS(status); 5262103a8feaSLen Brown } 5263c98d5d94SLen Brown /* 5264c98d5d94SLen Brown * n.b. fork_it() does not check for errors from for_all_cpus() 5265c98d5d94SLen Brown * because re-starting is problematic when forking 5266c98d5d94SLen Brown */ 5267218f0e8dSLen Brown snapshot_proc_sysfs_files(); 5268c98d5d94SLen Brown for_all_cpus(get_counters, ODD_COUNTERS); 5269103a8feaSLen Brown gettimeofday(&tv_odd, (struct timezone *)NULL); 5270103a8feaSLen Brown timersub(&tv_odd, &tv_even, &tv_delta); 5271ba3dec99SLen Brown if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) 5272ba3dec99SLen Brown fprintf(outf, "%s: Counter reset detected\n", progname); 5273ba3dec99SLen Brown else { 5274c98d5d94SLen Brown compute_average(EVEN_COUNTERS); 5275c98d5d94SLen Brown format_all_counters(EVEN_COUNTERS); 5276ba3dec99SLen Brown } 5277103a8feaSLen Brown 5278b7d8c148SLen Brown fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0); 5279b7d8c148SLen Brown 5280b7d8c148SLen Brown flush_output_stderr(); 5281103a8feaSLen Brown 5282d91bb17cSLen Brown return status; 5283103a8feaSLen Brown } 5284103a8feaSLen Brown 52853b4d5c7fSAndy Shevchenko int get_and_dump_counters(void) 52863b4d5c7fSAndy Shevchenko { 52873b4d5c7fSAndy Shevchenko int status; 52883b4d5c7fSAndy Shevchenko 5289218f0e8dSLen Brown snapshot_proc_sysfs_files(); 52903b4d5c7fSAndy Shevchenko status = for_all_cpus(get_counters, ODD_COUNTERS); 52913b4d5c7fSAndy Shevchenko if (status) 52923b4d5c7fSAndy Shevchenko return status; 52933b4d5c7fSAndy Shevchenko 52943b4d5c7fSAndy Shevchenko status = for_all_cpus(dump_counters, ODD_COUNTERS); 52953b4d5c7fSAndy Shevchenko if (status) 52963b4d5c7fSAndy Shevchenko return status; 52973b4d5c7fSAndy Shevchenko 5298b7d8c148SLen Brown flush_output_stdout(); 52993b4d5c7fSAndy Shevchenko 53003b4d5c7fSAndy Shevchenko return status; 53013b4d5c7fSAndy Shevchenko } 53023b4d5c7fSAndy Shevchenko 5303d8af6f5fSLen Brown void print_version() { 53040f71d089SLen Brown fprintf(outf, "turbostat version 19.03.20" 5305d8af6f5fSLen Brown " - Len Brown <lenb@kernel.org>\n"); 5306d8af6f5fSLen Brown } 5307d8af6f5fSLen Brown 5308495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name, 5309495c7654SLen Brown unsigned int width, enum counter_scope scope, 531041618e63SLen Brown enum counter_type type, enum counter_format format, int flags) 5311388e9c81SLen Brown { 5312388e9c81SLen Brown struct msr_counter *msrp; 5313388e9c81SLen Brown 5314388e9c81SLen Brown msrp = calloc(1, sizeof(struct msr_counter)); 5315388e9c81SLen Brown if (msrp == NULL) { 5316388e9c81SLen Brown perror("calloc"); 5317388e9c81SLen Brown exit(1); 5318388e9c81SLen Brown } 5319388e9c81SLen Brown 5320388e9c81SLen Brown msrp->msr_num = msr_num; 5321388e9c81SLen Brown strncpy(msrp->name, name, NAME_BYTES); 5322495c7654SLen Brown if (path) 5323495c7654SLen Brown strncpy(msrp->path, path, PATH_BYTES); 5324388e9c81SLen Brown msrp->width = width; 5325388e9c81SLen Brown msrp->type = type; 5326388e9c81SLen Brown msrp->format = format; 532741618e63SLen Brown msrp->flags = flags; 5328388e9c81SLen Brown 5329388e9c81SLen Brown switch (scope) { 5330388e9c81SLen Brown 5331388e9c81SLen Brown case SCOPE_CPU: 5332388e9c81SLen Brown msrp->next = sys.tp; 5333388e9c81SLen Brown sys.tp = msrp; 5334678a3bd1SLen Brown sys.added_thread_counters++; 53350748eaf0SLen Brown if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) { 5336678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added thread counters\n", 5337678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5338678a3bd1SLen Brown exit(-1); 5339678a3bd1SLen Brown } 5340388e9c81SLen Brown break; 5341388e9c81SLen Brown 5342388e9c81SLen Brown case SCOPE_CORE: 5343388e9c81SLen Brown msrp->next = sys.cp; 5344388e9c81SLen Brown sys.cp = msrp; 5345678a3bd1SLen Brown sys.added_core_counters++; 5346678a3bd1SLen Brown if (sys.added_core_counters > MAX_ADDED_COUNTERS) { 5347678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added core counters\n", 5348678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5349678a3bd1SLen Brown exit(-1); 5350678a3bd1SLen Brown } 5351388e9c81SLen Brown break; 5352388e9c81SLen Brown 5353388e9c81SLen Brown case SCOPE_PACKAGE: 5354388e9c81SLen Brown msrp->next = sys.pp; 5355388e9c81SLen Brown sys.pp = msrp; 5356678a3bd1SLen Brown sys.added_package_counters++; 5357678a3bd1SLen Brown if (sys.added_package_counters > MAX_ADDED_COUNTERS) { 5358678a3bd1SLen Brown fprintf(stderr, "exceeded max %d added package counters\n", 5359678a3bd1SLen Brown MAX_ADDED_COUNTERS); 5360678a3bd1SLen Brown exit(-1); 5361678a3bd1SLen Brown } 5362388e9c81SLen Brown break; 5363388e9c81SLen Brown } 5364388e9c81SLen Brown 5365388e9c81SLen Brown return 0; 5366388e9c81SLen Brown } 5367388e9c81SLen Brown 5368388e9c81SLen Brown void parse_add_command(char *add_command) 5369388e9c81SLen Brown { 5370388e9c81SLen Brown int msr_num = 0; 5371495c7654SLen Brown char *path = NULL; 53720f47c08dSLen Brown char name_buffer[NAME_BYTES] = ""; 5373388e9c81SLen Brown int width = 64; 5374388e9c81SLen Brown int fail = 0; 5375388e9c81SLen Brown enum counter_scope scope = SCOPE_CPU; 5376388e9c81SLen Brown enum counter_type type = COUNTER_CYCLES; 5377388e9c81SLen Brown enum counter_format format = FORMAT_DELTA; 5378388e9c81SLen Brown 5379388e9c81SLen Brown while (add_command) { 5380388e9c81SLen Brown 5381388e9c81SLen Brown if (sscanf(add_command, "msr0x%x", &msr_num) == 1) 5382388e9c81SLen Brown goto next; 5383388e9c81SLen Brown 5384388e9c81SLen Brown if (sscanf(add_command, "msr%d", &msr_num) == 1) 5385388e9c81SLen Brown goto next; 5386388e9c81SLen Brown 5387495c7654SLen Brown if (*add_command == '/') { 5388495c7654SLen Brown path = add_command; 5389495c7654SLen Brown goto next; 5390495c7654SLen Brown } 5391495c7654SLen Brown 5392388e9c81SLen Brown if (sscanf(add_command, "u%d", &width) == 1) { 5393388e9c81SLen Brown if ((width == 32) || (width == 64)) 5394388e9c81SLen Brown goto next; 5395388e9c81SLen Brown width = 64; 5396388e9c81SLen Brown } 5397388e9c81SLen Brown if (!strncmp(add_command, "cpu", strlen("cpu"))) { 5398388e9c81SLen Brown scope = SCOPE_CPU; 5399388e9c81SLen Brown goto next; 5400388e9c81SLen Brown } 5401388e9c81SLen Brown if (!strncmp(add_command, "core", strlen("core"))) { 5402388e9c81SLen Brown scope = SCOPE_CORE; 5403388e9c81SLen Brown goto next; 5404388e9c81SLen Brown } 5405388e9c81SLen Brown if (!strncmp(add_command, "package", strlen("package"))) { 5406388e9c81SLen Brown scope = SCOPE_PACKAGE; 5407388e9c81SLen Brown goto next; 5408388e9c81SLen Brown } 5409388e9c81SLen Brown if (!strncmp(add_command, "cycles", strlen("cycles"))) { 5410388e9c81SLen Brown type = COUNTER_CYCLES; 5411388e9c81SLen Brown goto next; 5412388e9c81SLen Brown } 5413388e9c81SLen Brown if (!strncmp(add_command, "seconds", strlen("seconds"))) { 5414388e9c81SLen Brown type = COUNTER_SECONDS; 5415388e9c81SLen Brown goto next; 5416388e9c81SLen Brown } 541741618e63SLen Brown if (!strncmp(add_command, "usec", strlen("usec"))) { 541841618e63SLen Brown type = COUNTER_USEC; 541941618e63SLen Brown goto next; 542041618e63SLen Brown } 5421388e9c81SLen Brown if (!strncmp(add_command, "raw", strlen("raw"))) { 5422388e9c81SLen Brown format = FORMAT_RAW; 5423388e9c81SLen Brown goto next; 5424388e9c81SLen Brown } 5425388e9c81SLen Brown if (!strncmp(add_command, "delta", strlen("delta"))) { 5426388e9c81SLen Brown format = FORMAT_DELTA; 5427388e9c81SLen Brown goto next; 5428388e9c81SLen Brown } 5429388e9c81SLen Brown if (!strncmp(add_command, "percent", strlen("percent"))) { 5430388e9c81SLen Brown format = FORMAT_PERCENT; 5431388e9c81SLen Brown goto next; 5432388e9c81SLen Brown } 5433388e9c81SLen Brown 5434388e9c81SLen Brown if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */ 5435388e9c81SLen Brown char *eos; 5436388e9c81SLen Brown 5437388e9c81SLen Brown eos = strchr(name_buffer, ','); 5438388e9c81SLen Brown if (eos) 5439388e9c81SLen Brown *eos = '\0'; 5440388e9c81SLen Brown goto next; 5441388e9c81SLen Brown } 5442388e9c81SLen Brown 5443388e9c81SLen Brown next: 5444388e9c81SLen Brown add_command = strchr(add_command, ','); 5445495c7654SLen Brown if (add_command) { 5446495c7654SLen Brown *add_command = '\0'; 5447388e9c81SLen Brown add_command++; 5448495c7654SLen Brown } 5449388e9c81SLen Brown 5450388e9c81SLen Brown } 5451495c7654SLen Brown if ((msr_num == 0) && (path == NULL)) { 5452495c7654SLen Brown fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n"); 5453388e9c81SLen Brown fail++; 5454388e9c81SLen Brown } 5455388e9c81SLen Brown 5456388e9c81SLen Brown /* generate default column header */ 5457388e9c81SLen Brown if (*name_buffer == '\0') { 5458388e9c81SLen Brown if (width == 32) 54595f3aea57SLen Brown sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5460388e9c81SLen Brown else 54615f3aea57SLen Brown sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : ""); 5462388e9c81SLen Brown } 5463388e9c81SLen Brown 546441618e63SLen Brown if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0)) 5465388e9c81SLen Brown fail++; 5466388e9c81SLen Brown 5467388e9c81SLen Brown if (fail) { 5468388e9c81SLen Brown help(); 5469388e9c81SLen Brown exit(1); 5470388e9c81SLen Brown } 5471388e9c81SLen Brown } 547241618e63SLen Brown 5473dd778a5eSLen Brown int is_deferred_skip(char *name) 5474dd778a5eSLen Brown { 5475dd778a5eSLen Brown int i; 5476dd778a5eSLen Brown 5477dd778a5eSLen Brown for (i = 0; i < deferred_skip_index; ++i) 5478dd778a5eSLen Brown if (!strcmp(name, deferred_skip_names[i])) 5479dd778a5eSLen Brown return 1; 5480dd778a5eSLen Brown return 0; 5481dd778a5eSLen Brown } 5482dd778a5eSLen Brown 548341618e63SLen Brown void probe_sysfs(void) 548441618e63SLen Brown { 548541618e63SLen Brown char path[64]; 548641618e63SLen Brown char name_buf[16]; 548741618e63SLen Brown FILE *input; 548841618e63SLen Brown int state; 548941618e63SLen Brown char *sp; 549041618e63SLen Brown 549141618e63SLen Brown if (!DO_BIC(BIC_sysfs)) 549241618e63SLen Brown return; 549341618e63SLen Brown 54940748eaf0SLen Brown for (state = 10; state >= 0; --state) { 549541618e63SLen Brown 549641618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 549741618e63SLen Brown base_cpu, state); 549841618e63SLen Brown input = fopen(path, "r"); 549941618e63SLen Brown if (input == NULL) 550041618e63SLen Brown continue; 55018173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 55028173c336SBen Hutchings err(1, "%s: failed to read file", path); 550341618e63SLen Brown 550441618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 550541618e63SLen Brown sp = strchr(name_buf, '-'); 550641618e63SLen Brown if (!sp) 550741618e63SLen Brown sp = strchrnul(name_buf, '\n'); 550841618e63SLen Brown *sp = '%'; 550941618e63SLen Brown *(sp + 1) = '\0'; 551041618e63SLen Brown 551141618e63SLen Brown fclose(input); 551241618e63SLen Brown 551341618e63SLen Brown sprintf(path, "cpuidle/state%d/time", state); 551441618e63SLen Brown 5515dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5516dd778a5eSLen Brown continue; 5517dd778a5eSLen Brown 551841618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, 551941618e63SLen Brown FORMAT_PERCENT, SYSFS_PERCPU); 552041618e63SLen Brown } 552141618e63SLen Brown 55220748eaf0SLen Brown for (state = 10; state >= 0; --state) { 552341618e63SLen Brown 552441618e63SLen Brown sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", 552541618e63SLen Brown base_cpu, state); 552641618e63SLen Brown input = fopen(path, "r"); 552741618e63SLen Brown if (input == NULL) 552841618e63SLen Brown continue; 55298173c336SBen Hutchings if (!fgets(name_buf, sizeof(name_buf), input)) 55308173c336SBen Hutchings err(1, "%s: failed to read file", path); 553141618e63SLen Brown /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ 553241618e63SLen Brown sp = strchr(name_buf, '-'); 553341618e63SLen Brown if (!sp) 553441618e63SLen Brown sp = strchrnul(name_buf, '\n'); 553541618e63SLen Brown *sp = '\0'; 553641618e63SLen Brown fclose(input); 553741618e63SLen Brown 553841618e63SLen Brown sprintf(path, "cpuidle/state%d/usage", state); 553941618e63SLen Brown 5540dd778a5eSLen Brown if (is_deferred_skip(name_buf)) 5541dd778a5eSLen Brown continue; 5542dd778a5eSLen Brown 554341618e63SLen Brown add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, 554441618e63SLen Brown FORMAT_DELTA, SYSFS_PERCPU); 554541618e63SLen Brown } 554641618e63SLen Brown 554741618e63SLen Brown } 554841618e63SLen Brown 55491ef7d21aSLen Brown 55501ef7d21aSLen Brown /* 55511ef7d21aSLen Brown * parse cpuset with following syntax 55521ef7d21aSLen Brown * 1,2,4..6,8-10 and set bits in cpu_subset 55531ef7d21aSLen Brown */ 55541ef7d21aSLen Brown void parse_cpu_command(char *optarg) 55551ef7d21aSLen Brown { 55561ef7d21aSLen Brown unsigned int start, end; 55571ef7d21aSLen Brown char *next; 55581ef7d21aSLen Brown 55594e4e1e7cSLen Brown if (!strcmp(optarg, "core")) { 55604e4e1e7cSLen Brown if (cpu_subset) 55614e4e1e7cSLen Brown goto error; 55624e4e1e7cSLen Brown show_core_only++; 55634e4e1e7cSLen Brown return; 55644e4e1e7cSLen Brown } 55654e4e1e7cSLen Brown if (!strcmp(optarg, "package")) { 55664e4e1e7cSLen Brown if (cpu_subset) 55674e4e1e7cSLen Brown goto error; 55684e4e1e7cSLen Brown show_pkg_only++; 55694e4e1e7cSLen Brown return; 55704e4e1e7cSLen Brown } 55714e4e1e7cSLen Brown if (show_core_only || show_pkg_only) 55724e4e1e7cSLen Brown goto error; 55734e4e1e7cSLen Brown 55741ef7d21aSLen Brown cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS); 55751ef7d21aSLen Brown if (cpu_subset == NULL) 55761ef7d21aSLen Brown err(3, "CPU_ALLOC"); 55771ef7d21aSLen Brown cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS); 55781ef7d21aSLen Brown 55791ef7d21aSLen Brown CPU_ZERO_S(cpu_subset_size, cpu_subset); 55801ef7d21aSLen Brown 55811ef7d21aSLen Brown next = optarg; 55821ef7d21aSLen Brown 55831ef7d21aSLen Brown while (next && *next) { 55841ef7d21aSLen Brown 55851ef7d21aSLen Brown if (*next == '-') /* no negative cpu numbers */ 55861ef7d21aSLen Brown goto error; 55871ef7d21aSLen Brown 55881ef7d21aSLen Brown start = strtoul(next, &next, 10); 55891ef7d21aSLen Brown 55901ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 55911ef7d21aSLen Brown goto error; 55921ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 55931ef7d21aSLen Brown 55941ef7d21aSLen Brown if (*next == '\0') 55951ef7d21aSLen Brown break; 55961ef7d21aSLen Brown 55971ef7d21aSLen Brown if (*next == ',') { 55981ef7d21aSLen Brown next += 1; 55991ef7d21aSLen Brown continue; 56001ef7d21aSLen Brown } 56011ef7d21aSLen Brown 56021ef7d21aSLen Brown if (*next == '-') { 56031ef7d21aSLen Brown next += 1; /* start range */ 56041ef7d21aSLen Brown } else if (*next == '.') { 56051ef7d21aSLen Brown next += 1; 56061ef7d21aSLen Brown if (*next == '.') 56071ef7d21aSLen Brown next += 1; /* start range */ 56081ef7d21aSLen Brown else 56091ef7d21aSLen Brown goto error; 56101ef7d21aSLen Brown } 56111ef7d21aSLen Brown 56121ef7d21aSLen Brown end = strtoul(next, &next, 10); 56131ef7d21aSLen Brown if (end <= start) 56141ef7d21aSLen Brown goto error; 56151ef7d21aSLen Brown 56161ef7d21aSLen Brown while (++start <= end) { 56171ef7d21aSLen Brown if (start >= CPU_SUBSET_MAXCPUS) 56181ef7d21aSLen Brown goto error; 56191ef7d21aSLen Brown CPU_SET_S(start, cpu_subset_size, cpu_subset); 56201ef7d21aSLen Brown } 56211ef7d21aSLen Brown 56221ef7d21aSLen Brown if (*next == ',') 56231ef7d21aSLen Brown next += 1; 56241ef7d21aSLen Brown else if (*next != '\0') 56251ef7d21aSLen Brown goto error; 56261ef7d21aSLen Brown } 56271ef7d21aSLen Brown 56281ef7d21aSLen Brown return; 56291ef7d21aSLen Brown 56301ef7d21aSLen Brown error: 56314e4e1e7cSLen Brown fprintf(stderr, "\"--cpu %s\" malformed\n", optarg); 56324e4e1e7cSLen Brown help(); 56331ef7d21aSLen Brown exit(-1); 56341ef7d21aSLen Brown } 56351ef7d21aSLen Brown 5636812db3f7SLen Brown 5637103a8feaSLen Brown void cmdline(int argc, char **argv) 5638103a8feaSLen Brown { 5639103a8feaSLen Brown int opt; 5640d8af6f5fSLen Brown int option_index = 0; 5641d8af6f5fSLen Brown static struct option long_options[] = { 5642388e9c81SLen Brown {"add", required_argument, 0, 'a'}, 56431ef7d21aSLen Brown {"cpu", required_argument, 0, 'c'}, 5644d8af6f5fSLen Brown {"Dump", no_argument, 0, 'D'}, 564596e47158SLen Brown {"debug", no_argument, 0, 'd'}, /* internal, not documented */ 56463f44a5c6SLen Brown {"enable", required_argument, 0, 'e'}, 5647d8af6f5fSLen Brown {"interval", required_argument, 0, 'i'}, 5648023fe0acSChen Yu {"num_iterations", required_argument, 0, 'n'}, 5649d8af6f5fSLen Brown {"help", no_argument, 0, 'h'}, 5650812db3f7SLen Brown {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help 5651d8af6f5fSLen Brown {"Joules", no_argument, 0, 'J'}, 5652c8ade361SLen Brown {"list", no_argument, 0, 'l'}, 5653b7d8c148SLen Brown {"out", required_argument, 0, 'o'}, 565496e47158SLen Brown {"quiet", no_argument, 0, 'q'}, 5655812db3f7SLen Brown {"show", required_argument, 0, 's'}, 5656d8af6f5fSLen Brown {"Summary", no_argument, 0, 'S'}, 5657d8af6f5fSLen Brown {"TCC", required_argument, 0, 'T'}, 5658d8af6f5fSLen Brown {"version", no_argument, 0, 'v' }, 5659d8af6f5fSLen Brown {0, 0, 0, 0 } 5660d8af6f5fSLen Brown }; 5661103a8feaSLen Brown 5662103a8feaSLen Brown progname = argv[0]; 5663103a8feaSLen Brown 5664023fe0acSChen Yu while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", 5665d8af6f5fSLen Brown long_options, &option_index)) != -1) { 5666103a8feaSLen Brown switch (opt) { 5667388e9c81SLen Brown case 'a': 5668388e9c81SLen Brown parse_add_command(optarg); 5669388e9c81SLen Brown break; 56701ef7d21aSLen Brown case 'c': 56711ef7d21aSLen Brown parse_cpu_command(optarg); 56721ef7d21aSLen Brown break; 5673d8af6f5fSLen Brown case 'D': 5674d8af6f5fSLen Brown dump_only++; 56758e180f3cSLen Brown break; 56763f44a5c6SLen Brown case 'e': 56773f44a5c6SLen Brown /* --enable specified counter */ 56784c2122d4SLen Brown bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST); 56793f44a5c6SLen Brown break; 5680d8af6f5fSLen Brown case 'd': 5681d8af6f5fSLen Brown debug++; 56823f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 56832f32edf1SLen Brown break; 5684812db3f7SLen Brown case 'H': 56853f44a5c6SLen Brown /* 56863f44a5c6SLen Brown * --hide: do not show those specified 56873f44a5c6SLen Brown * multiple invocations simply clear more bits in enabled mask 56883f44a5c6SLen Brown */ 56893f44a5c6SLen Brown bic_enabled &= ~bic_lookup(optarg, HIDE_LIST); 5690812db3f7SLen Brown break; 5691d8af6f5fSLen Brown case 'h': 5692d8af6f5fSLen Brown default: 5693d8af6f5fSLen Brown help(); 5694d8af6f5fSLen Brown exit(1); 5695d8af6f5fSLen Brown case 'i': 56962a0609c0SLen Brown { 56972a0609c0SLen Brown double interval = strtod(optarg, NULL); 56982a0609c0SLen Brown 56992a0609c0SLen Brown if (interval < 0.001) { 5700b7d8c148SLen Brown fprintf(outf, "interval %f seconds is too small\n", 57012a0609c0SLen Brown interval); 57022a0609c0SLen Brown exit(2); 57032a0609c0SLen Brown } 57042a0609c0SLen Brown 570547936f94SArtem Bityutskiy interval_tv.tv_sec = interval_ts.tv_sec = interval; 5706b9ad8ee0SLen Brown interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; 570747936f94SArtem Bityutskiy interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000; 57082a0609c0SLen Brown } 5709889facbeSLen Brown break; 57105c56be9aSDirk Brandewie case 'J': 57115c56be9aSDirk Brandewie rapl_joules++; 57125c56be9aSDirk Brandewie break; 5713c8ade361SLen Brown case 'l': 57143f44a5c6SLen Brown ENABLE_BIC(BIC_DISABLED_BY_DEFAULT); 5715c8ade361SLen Brown list_header_only++; 5716c8ade361SLen Brown quiet++; 5717c8ade361SLen Brown break; 5718b7d8c148SLen Brown case 'o': 5719b7d8c148SLen Brown outf = fopen_or_die(optarg, "w"); 5720b7d8c148SLen Brown break; 572196e47158SLen Brown case 'q': 572296e47158SLen Brown quiet = 1; 572396e47158SLen Brown break; 5724023fe0acSChen Yu case 'n': 5725023fe0acSChen Yu num_iterations = strtod(optarg, NULL); 5726023fe0acSChen Yu 5727023fe0acSChen Yu if (num_iterations <= 0) { 5728023fe0acSChen Yu fprintf(outf, "iterations %d should be positive number\n", 5729023fe0acSChen Yu num_iterations); 5730023fe0acSChen Yu exit(2); 5731023fe0acSChen Yu } 5732023fe0acSChen Yu break; 5733812db3f7SLen Brown case 's': 57343f44a5c6SLen Brown /* 57353f44a5c6SLen Brown * --show: show only those specified 57363f44a5c6SLen Brown * The 1st invocation will clear and replace the enabled mask 57373f44a5c6SLen Brown * subsequent invocations can add to it. 57383f44a5c6SLen Brown */ 57393f44a5c6SLen Brown if (shown == 0) 57403f44a5c6SLen Brown bic_enabled = bic_lookup(optarg, SHOW_LIST); 57413f44a5c6SLen Brown else 57423f44a5c6SLen Brown bic_enabled |= bic_lookup(optarg, SHOW_LIST); 57433f44a5c6SLen Brown shown = 1; 5744812db3f7SLen Brown break; 5745d8af6f5fSLen Brown case 'S': 5746d8af6f5fSLen Brown summary_only++; 5747d8af6f5fSLen Brown break; 5748d8af6f5fSLen Brown case 'T': 5749d8af6f5fSLen Brown tcc_activation_temp_override = atoi(optarg); 5750d8af6f5fSLen Brown break; 5751d8af6f5fSLen Brown case 'v': 5752d8af6f5fSLen Brown print_version(); 5753d8af6f5fSLen Brown exit(0); 5754d8af6f5fSLen Brown break; 5755103a8feaSLen Brown } 5756103a8feaSLen Brown } 5757103a8feaSLen Brown } 5758103a8feaSLen Brown 5759103a8feaSLen Brown int main(int argc, char **argv) 5760103a8feaSLen Brown { 5761b7d8c148SLen Brown outf = stderr; 5762103a8feaSLen Brown cmdline(argc, argv); 5763103a8feaSLen Brown 576496e47158SLen Brown if (!quiet) 5765d8af6f5fSLen Brown print_version(); 5766103a8feaSLen Brown 576741618e63SLen Brown probe_sysfs(); 576841618e63SLen Brown 5769103a8feaSLen Brown turbostat_init(); 5770103a8feaSLen Brown 57713b4d5c7fSAndy Shevchenko /* dump counters and exit */ 57723b4d5c7fSAndy Shevchenko if (dump_only) 57733b4d5c7fSAndy Shevchenko return get_and_dump_counters(); 57743b4d5c7fSAndy Shevchenko 5775c8ade361SLen Brown /* list header and exit */ 5776c8ade361SLen Brown if (list_header_only) { 5777c8ade361SLen Brown print_header(","); 5778c8ade361SLen Brown flush_output_stdout(); 5779c8ade361SLen Brown return 0; 5780c8ade361SLen Brown } 5781c8ade361SLen Brown 5782103a8feaSLen Brown /* 5783103a8feaSLen Brown * if any params left, it must be a command to fork 5784103a8feaSLen Brown */ 5785103a8feaSLen Brown if (argc - optind) 5786103a8feaSLen Brown return fork_it(argv + optind); 5787103a8feaSLen Brown else 5788103a8feaSLen Brown turbostat_loop(); 5789103a8feaSLen Brown 5790103a8feaSLen Brown return 0; 5791103a8feaSLen Brown } 5792